예제 #1
0
파일: tpsm_inmem.py 프로젝트: bsmithers/hpf
def run_mammoth(prediction_id, experiment_id):
    """Runs mammoth on two structures, returns results object, doesn't store"""

    p_structure = session.query(Structure).get(prediction_id)
    assert p_structure
    e_structure = session.query(Structure).get(experiment_id)
    assert e_structure

    base_dir = os.getcwd()
    work_dir = os.path.join(base_dir, "p{0}_e{1}_mammoth".format(prediction_id, experiment_id))
    os.mkdir(work_dir)
    os.chdir(work_dir)
    prediction_file = "p{0}.pdb".format(prediction_id)
    experiment_file = "e{0}.pdb".format(experiment_id)
    write_struct_file(prediction_file, p_structure)
    write_struct_file(experiment_file, e_structure)

    mammoth_file = "p{0}_e{1}.mammoth".format(prediction_id, experiment_id)
    mcl = MammothCL(experiment_file, prediction_file, cwd=work_dir, output=mammoth_file)
    m = Mammoth(mcl, parse=True)
    mscore = m.run()

    os.chdir(base_dir)
    files = os.listdir(work_dir)
    for f in files:
        os.remove(os.path.join(work_dir, f))
    os.removedirs(work_dir)

    session.close()
    return mscore
예제 #2
0
def structure_mammoth(prediction_id, experiment_id, ptype, etype, base_dir=None, dbstore=True, version=1, 
        debug=False, cleanup=True, table_destination="default", fake_mammoth=False):
    """
    Takes the hpf DB structure IDs of two protein structures. Optionally checks for pre-computed
    mammoth in DB (if exists, returns). Fetches PDB files for both structures to local storage,
    runs mammoth on structures, and optionally stores mammoth results in DB (hpf, structure_mammoth tbl).
    
    Creates directory p<prediction_id>_e<experiment_id>_mammoth in given base_dir. Removes this directory
    if cleanup is True.

    version is optional if when specifying dbstore=True, you want to check for and add a specific version 
    (field in hpf.structure_mammoth) of structure->structure mammoth result. Defaults to 1
    """
    from hpf.pdb.mammoth import Mammoth, MammothAlignment, MammothCL
    from hpf.hddb.db import Session, Structure, Mammoth as MammothORM, StructureMammoth, StructureMammoth1186, StructureMammoth_class_lookup, push_to_db

    # Init session and check for pre-existing mammoth (optional)
    #session = Session()
    if dbstore:
        # Check StructureMammoth (hpf.structure_mammoth) for structure IDs
        previous = _sm_query(prediction_id, experiment_id, version)
        if previous:
            if debug:
                print "Previous version of Struct->Struct Mammoth exists: {0}".format(previous)
            return previous
        
        # Check Mammoth (hpf.mammoth) for structure IDs - if exists, transfer values, do not rerun Mammoth
        mammoth_dbo = _mammoth_query(prediction_id, experiment_id)
        if mammoth_dbo:
            if debug:
                print "Struct->Struct comparison exists in Mammoth table (hpf.mammoth): {0}".format(mammoth_dbo),
                print "Adding to StructureMammoth table and returning"
            SM = StructureMammoth_class_lookup[table_destination]
            sm_dbo = SM(
                prediction_id=mammoth_dbo.p_structure_key,
                experiment_id=mammoth_dbo.e_structure_key,
                prediction_type=ptype,
                experiment_type=etype,
                ini_psi=mammoth_dbo.ini_psi,
                ini_rms=mammoth_dbo.ini_rms,
                end_psi=mammoth_dbo.end_psi,
                end_rms=mammoth_dbo.end_rms,
                zscore=mammoth_dbo.zscore,
                evalue=mammoth_dbo.evalue,
                version=version)

            push_to_db(session, sm_dbo, raise_on_duplicate=False)
            if debug:
                print "Added: {0}".format(sm_dbo)
            return sm_dbo
    
    if fake_mammoth:
        #-------------------------------------------------------------------------------------
        # MANUAL BREAK: IF NO RESULTS IN THE DB, RETURN LARGE SCORE TO INDICATE MAMMOTH NECESSARY
        #print "Returning FALSE MAMMOTH"
        SM = StructureMammoth_class_lookup[table_destination]
        return SM(
           prediction_id=prediction_id,
           experiment_id=experiment_id,
           prediction_type=ptype,
           experiment_type=etype,
           ini_psi=0.0,
           ini_rms=0.0,
           end_psi=0.0,
           end_rms=0.0,
           zscore=100000.0,
           evalue=0.0,
           version=version)
        #-------------------------------------------------------------------------------------

    # Check input workdir
    if not base_dir:
        base_dir = os.getcwd()
    if not os.path.isdir(base_dir):
        raise Exception("Work dir '{0}' is not a valid directory".format(base_dir))
    
    # Get prediction and experiment struct DBOs
    p_structure = session.query(Structure).get(prediction_id)
    if not (p_structure):
        raise Exception("Prediction structure ({0}) not found in DB".format(prediction_id,))
    e_structure = session.query(Structure).get(experiment_id)
    if not (e_structure):
        raise Exception("Experiment structure ({0}) not found in DB".format(experiment_id,))

    # Set up working environment
    work_dir = os.path.join(base_dir, "p{0}_e{1}_mammoth".format(prediction_id, experiment_id))
    os.mkdir(work_dir)
    os.chdir(work_dir)
    prediction_file = "p{0}.pdb".format(prediction_id)
    experiment_file = "e{0}.pdb".format(experiment_id)
    write_struct_file(prediction_file, p_structure)
    write_struct_file(experiment_file, e_structure)

    # Run mammoth (via hpf.pdb.mammoth Mammoth), parse results
    mammoth_file = "p{0}_e{1}.mammoth".format(prediction_id, experiment_id)
    mcl = MammothCL(experiment_file, prediction_file, cwd=work_dir, output=mammoth_file)
    m = Mammoth(mcl, parse=True)
    mscore = m.run()

    # (Optional) store structure-structure mammoth in DB
    SM = StructureMammoth_class_lookup[table_destination]
    sm_dbo = SM(
        prediction_id=prediction_id,
        experiment_id=experiment_id,
        prediction_type=ptype,
        experiment_type=etype,
        ini_psi=mscore.ini_psi,
        ini_rms=mscore.ini_rms,
        end_psi=mscore.end_psi,
        end_rms=mscore.end_rms,
        zscore=mscore.zscore,
        evalue=mscore.evalue,
        version=version)

    if dbstore:
        push_to_db(session, sm_dbo, raise_on_duplicate=False)
    
    # Complete and (optional) cleanup
    print "Mammothing {0} against {1} complete (zscore {2})".format(prediction_id, experiment_id, mscore.zscore)
    os.chdir(base_dir)
    if cleanup:
        files = os.listdir(work_dir)
        for file in files: os.remove(os.path.join(work_dir,file))
        os.removedirs(work_dir)

    session.close()
    return sm_dbo