Ejemplo n.º 1
0
def place_native_pdb(native_pdb,
                     native_mtz,
                     f_label,
                     sigf_label,
                     cleanup=True):
    """Place native_pdb into data from native_mtz using phaser with f_label and sigf_label"""
    # get crystal info from pdb
    sym_obj = iotbx.pdb.pdb_input(file_name=native_pdb).crystal_symmetry()
    hall_symbol = sym_obj.space_group().type().hall_symbol()
    unit_cell = sym_obj.unit_cell().parameters()
    molecular_weight = pdb_edit.molecular_weight(native_pdb)

    # Get data from MTZ file
    mtz_object = _check_mtz(native_mtz, labels=[f_label, sigf_label])
    miller_indices = mtz_object.extract_miller_indices()
    fp_obs = mtz_object.get_column(f_label).extract_values().as_double()
    sigfp_obs = mtz_object.get_column(sigf_label).extract_values().as_double()

    # Create root_name for all files from native + mr_mtz
    name1 = os.path.splitext(os.path.basename(native_pdb))[0]
    name2 = os.path.splitext(os.path.basename(native_mtz))[0]
    root_name = "{0}_{1}".format(name1, name2)

    # Run phaser to position native
    phaser_input = phaser.InputMR_AUTO()
    phaser_input.setSPAC_HALL(hall_symbol)
    phaser_input.setCELL6(unit_cell)
    phaser_input.setREFL_F_SIGF(miller_indices, fp_obs, sigfp_obs)
    # Make sure labels same as native_mtz
    phaser_input.setLABI_F_SIGF(f_label, sigf_label)
    phaser_input.setROOT(root_name)
    phaser_input.addENSE_PDB_ID("native", native_pdb, 0.0)
    phaser_input.addCOMP_PROT_MW_NUM(molecular_weight, 1)
    phaser_input.addSEAR_ENSE_NUM("native", 1)
    phaser_input.setMUTE(True)
    phaser_run = phaser.runMR_AUTO(phaser_input)
    del phaser_input
    if not phaser_run.Success():
        raise RuntimeError("PHASER failed: {0} : {1}".format(
            phaser_run.ErrorName(), phaser_run.ErrorMessage()))

    if phaser_run.foundSolutions():
        #print "Phaser has found MR solutions"
        #print "Top LLG = %f" % phaser_run.getTopLLG()
        #print "Top PDB file = %s" % phaser_run.getTopPdbFile()
        #print "Top MTZ file = %s" % phaser_run.getTopMtzFile()
        native_placed_mtz = phaser_run.getTopMtzFile()
    else:
        raise RuntimeError("PHASER could not place native_pdb")

    if cleanup:
        os.unlink(phaser_run.getTopPdbFile())
    return native_placed_mtz
Ejemplo n.º 2
0
def place_native_pdb(native_pdb, native_mtz, f_label, sigf_label, cleanup=True):
    """Place native_pdb into data from native_mtz using phaser with f_label and sigf_label"""
    # get crystal info from pdb
    sym_obj = iotbx.pdb.pdb_input(file_name=native_pdb).crystal_symmetry()
    hall_symbol = sym_obj.space_group().type().hall_symbol()
    unit_cell = sym_obj.unit_cell().parameters()
    molecular_weight = pdb_edit.molecular_weight(native_pdb)

    # Get data from MTZ file
    mtz_object = _check_mtz(native_mtz, labels = [f_label, sigf_label])
    miller_indices = mtz_object.extract_miller_indices()
    fp_obs = mtz_object.get_column(f_label).extract_values().as_double()
    sigfp_obs = mtz_object.get_column(sigf_label).extract_values().as_double()

    # Create root_name for all files from native + mr_mtz
    name1 = os.path.splitext(os.path.basename(native_pdb))[0]
    name2 = os.path.splitext(os.path.basename(native_mtz))[0]
    root_name = "{0}_{1}".format(name1, name2)

    # Run phaser to position native
    phaser_input = phaser.InputMR_AUTO()
    phaser_input.setSPAC_HALL(hall_symbol)
    phaser_input.setCELL6(unit_cell)
    phaser_input.setREFL_F_SIGF(miller_indices, fp_obs, sigfp_obs)
    # Make sure labels same as native_mtz
    phaser_input.setLABI_F_SIGF(f_label,sigf_label)
    phaser_input.setROOT(root_name)
    phaser_input.addENSE_PDB_ID("native", native_pdb, 0.0)
    phaser_input.addCOMP_PROT_MW_NUM(molecular_weight, 1)
    phaser_input.addSEAR_ENSE_NUM("native", 1)
    phaser_input.setMUTE(True)
    phaser_run = phaser.runMR_AUTO(phaser_input)
    del phaser_input
    if not phaser_run.Success():
        raise RuntimeError("PHASER failed: {0} : {1}".format(phaser_run.ErrorName(), phaser_run.ErrorMessage()))

    if phaser_run.foundSolutions():
        native_placed_mtz = phaser_run.getTopMtzFile()
    else:
        raise RuntimeError("PHASER could not place native_pdb")

    if cleanup:
        os.unlink(phaser_run.getTopPdbFile())
    return native_placed_mtz
Ejemplo n.º 3
0
def run_phaser(datafile,
               spacegroup,
               output_id,
               db_settings,
               work_dir=False,
               cif=False,
               pdb=False,
               name=False,
               ncopy=1,
               cell_analysis=False,
               resolution=False,
               large_cell=False,
               run_before=False,
               ):
    """
    Run Phaser and passes results back to RAPD Redis DB
    **Requires Phaser src code!**

    datafile - input data as mtz
    spacegroup - The space group to run MR
    output_id - a Redis key where the results are sent
    db_settings - Redis connection settings for sending results
    work_dir - working directory
    cif - input search model path in mmCIF format (do not use with 'pdb')
    pdb -  input search model path in PDB format (do not use with 'cif')
    name - root name for output files
    ncopy - number of molecules to search for
    cell_analysis - internal RAPD signal so all possible SG's are searched
    resolution - high res limit to run MR (float)
    large_cell - optimizes parameters to speed up MR with large unit cell.
    run_before - signal to run more comprehensive MR
    """
    # Change to work_dir
    if not work_dir:
        work_dir = os.getcwd()
    os.chdir(work_dir)

    if not name:
        name = spacegroup

    # Connect to Redis
    redis = connect_to_redis(db_settings)

    # Read the dataset
    i = phaser.InputMR_DAT()
    i.setHKLI(convert_unicode(datafile))
    i.setLABI_F_SIGF('F', 'SIGF')
    i.setMUTE(True)
    r = phaser.runMR_DAT(i)
    if r.Success():
        i = phaser.InputMR_AUTO()
        # i.setREFL_DATA(r.getREFL_DATA())
        # i.setREFL_DATA(r.DATA_REFL())
        i.setREFL_F_SIGF(r.getMiller(), r.getFobs(), r.getSigFobs())
        i.setCELL6(r.getUnitCell())
        if cif:
            #i.addENSE_CIF_ID('model', cif, 0.7)
            ### Typo in PHASER CODE!!!###
            i.addENSE_CIT_ID('model', convert_unicode(cif), 0.7)
        if pdb:
            i.addENSE_PDB_ID('model', convert_unicode(pdb), 0.7)
        i.addSEAR_ENSE_NUM("model", ncopy)
        i.setSPAC_NAME(spacegroup)
        if cell_analysis:
            i.setSGAL_SELE("ALL")
            # Set it for worst case in orth
            # number of processes to run in parallel where possible
            i.setJOBS(1)
        else:
            i.setSGAL_SELE("NONE")
        if run_before:
            # Picks own resolution
            # Round 2, pick best solution as long as less that 10% clashes
            i.setPACK_SELE("PERCENT")
            i.setPACK_CUTO(0.1)
            #command += "PACK CUTOFF 10\n"
        else:
            # For first round and cell analysis
            # Only set the resolution limit in the first round or cell analysis.
            if resolution:
                i.setRESO_HIGH(resolution)
            else:
                # Otherwise it runs a second MR at full resolution!!
                # I dont think a second round is run anymore.
                # command += "RESOLUTION SEARCH HIGH OFF\n"
                if large_cell:
                    i.setRESO_HIGH(6.0)
                else:
                    i.setRESO_HIGH(4.5)
            i.setSEAR_DEEP(False)
            # Don"t seem to work since it picks the high res limit now.
            # Get an error when it prunes all the solutions away and TF has no input.
            # command += "PEAKS ROT SELECT SIGMA CUTOFF 4.0\n"
            # command += "PEAKS TRA SELECT SIGMA CUTOFF 6.0\n"
        # Turn off pruning in 2.6.0
        i.setSEAR_PRUN(False)
        # Choose more top peaks to help with getting it correct.
        i.setPURG_ROTA_ENAB(True)
        i.setPURG_ROTA_NUMB(3)
        #command += "PURGE ROT ENABLE ON\nPURGE ROT NUMBER 3\n"
        i.setPURG_TRAN_ENAB(True)
        i.setPURG_TRAN_NUMB(1)
        #command += "PURGE TRA ENABLE ON\nPURGE TRA NUMBER 1\n"

        # Only keep the top after refinement.
        i.setPURG_RNP_ENAB(True)
        i.setPURG_RNP_NUMB(1)
        #command += "PURGE RNP ENABLE ON\nPURGE RNP NUMBER 1\n"
        i.setROOT(convert_unicode(name))
        # i.setMUTE(False)
        i.setMUTE(True)
        # Delete the setup results
        del(r)
        # launch the run
        r = phaser.runMR_AUTO(i)
        if r.Success():
            if r.foundSolutions():
                print "Phaser has found MR solutions"
                #print "Top LLG = %f" % r.getTopLLG()
                #print "Top PDB file = %s" % r.getTopPdbFile()
            else:
                print "Phaser has not found any MR solutions"
        else:
            print "Job exit status FAILURE"
            print r.ErrorName(), "ERROR :", r.ErrorMessage()

        with open('phaser.log', 'w') as log:
            log.write(r.logfile())
            log.close()
        with open('phaser_sum.log', 'w') as log:
            log.write(r.summary())
            log.close()

    if r.foundSolutions():
        rfz = None
        tfz = None
        tncs = False
        # Parse results
        for p in r.getTopSet().ANNOTATION.split():
            if p.count('RFZ'):
                if p.count('=') in [1]:
                    rfz = float(p.split('=')[-1])
            if p.count('RF*0'):
                rfz = "NC"
            if p.count('TFZ'):
                if p.count('=') in [1]:
                    tfz = p.split('=')[-1]
                    if tfz == '*':
                        tfz = 'arbitrary'
                    else:
                        tfz = float(tfz)
            if p.count('TF*0'):
                tfz = "NC"
        tncs_test = [1 for line in r.getTopSet().unparse().splitlines()
                     if line.count("+TNCS")]
        tncs = bool(len(tncs_test))
        phaser_result = {"ID": name,
                         "solution": r.foundSolutions(),
                         "pdb": r.getTopPdbFile(),
                         "mtz": r.getTopMtzFile(),
                         "gain": float(r.getTopLLG()),
                         "rfz": rfz,
                         # "tfz": r.getTopTFZ(),
                         "tfz": tfz,
                         "clash": r.getTopSet().PAK,
                         "dir": os.getcwd(),
                         "spacegroup": r.getTopSet().getSpaceGroupName().replace(' ', ''),
                         "tNCS": tncs,
                         "nmol": r.getTopSet().NUM,
                         "adf": None,
                         "peak": None,
                         }
        
        # make tar.bz2 of result files
        # l = ['pdb', 'mtz', 'adf', 'peak']
        # archive = "%s.tar.bz2" % name
        # with tarfile.open(archive, "w:bz2") as tar:
        #     for f in l:
        #         fo = phaser_result.get(f, False)
        #         if fo:
        #             if os.path.exists(fo):
        #                 tar.add(fo)
        #     tar.close()
        # phaser_result['tar'] = os.path.join(work_dir, archive)
        
        # New procedure for making tar of results
        # Create directory
        os.mkdir(name)
        # Go through and copy files to archive directory
        file_types = ("pdb", "mtz", "adf", "peak")
        for file_type in file_types:
            target_file = phaser_result.get(file_type, False)
            if target_file:
                if os.path.exists(target_file):
                    # Copy the file to the directory to be archived
                    shutil.copy(target_file, name+"/.")
        # Create the archive
        archive_result = archive.create_archive(name)
        archive_result["description"] = name
        phaser_result["tar"] = archive_result
        
        phaser_result["pdb_file"] = os.path.join(work_dir, r.getTopPdbFile())
    else:
        phaser_result = {"ID": name,
                         "solution": False,
                         "message": "No solution"}

    # Print the result so it can be seen in the rapd._phaser.log if needed
    print phaser_result

    # Key should be deleted once received, but set the key to expire in 24 hours just in case.
    redis.setex(output_id, 86400, json.dumps(phaser_result))
    # Do a little sleep to make sure results are in Redis for postprocess_phaser
    time.sleep(0.1)
Ejemplo n.º 4
0
    def run(self):
        """Function to run molecular replacement using PHASER

        Returns
        -------
        file
            Output pdb file
        file
            Output log file
        """

        # Make a note of the current working directory
        current_work_dir = os.getcwd()

        # Change to the PHASER working directory
        if os.path.exists(self.work_dir):
            os.chdir(self.work_dir)
        else:
            os.makedirs(self.work_dir)
            os.chdir(self.work_dir)

        # Copy hklin and pdbin to working dire for efficient running of PHASER
        hklin = os.path.join(self.work_dir, os.path.basename(self.hklin))
        shutil.copyfile(self.hklin, hklin)
        pdbin = os.path.join(self.work_dir, os.path.basename(self.pdbin))
        shutil.copyfile(self.pdbin, pdbin)

        i = InputMR_DAT()
        i.setHKLI(hklin)

        if self.hires:
            i.setHIRES(self.hires)
        if self.autohigh:
            i.setRESO_AUTO_HIGH(self.autohigh)
        if self.i != "None" and self.sigi != "None":
            i.setLABI_I_SIGI(self.i, self.sigi)
        elif self.f != "None" and self.sigf != "None":
            i.setLABI_F_SIGF(self.f, self.sigf)
        else:
            msg = "No flags for intensities or amplitudes have been provided"
            raise RuntimeError(msg)
        i.setSGAL_SELE(SGAlternatives[self.sgalternative].value)
        i.setMUTE(True)
        r = runMR_DAT(i)

        if r.Success():
            i = InputMR_AUTO()
            i.setJOBS(1)
            i.setREFL_DATA(r.getREFL_DATA())
            i.setROOT("phaser_mr_output")
            i.addENSE_PDB_ID("PDB", pdbin, 0.7)
            i.setENSE_DISA_CHEC('PDB', True)
            i.setCOMP_BY("SOLVENT")
            i.setCOMP_PERC(self.solvent)
            # nmol set to one for testing
            i.addSEAR_ENSE_NUM('PDB', 1)
            i.setSGAL_SELE(SGAlternatives[self.sgalternative].value)
            if self.timeout != 0:
                i.setKILL_TIME(self.timeout)
            i.setMUTE(True)
            del(r)
            r = runMR_AUTO(i)

            with open(self.logfile, 'w') as f:
                f.write(r.summary())

            shutil.move(r.getTopPdbFile(), self.pdbout)

            # Output original mtz with a change of basis if needed
            original_space_group, _, _ = mtz_util.crystal_data(self.hklin)
            space_group, _, _ = mtz_util.crystal_data(r.getTopMtzFile())
            if original_space_group != space_group:
                mtz_util.reindex(self.hklin, self.hklout, space_group)
            else:
                shutil.copyfile(self.hklin, self.hklout)

        # Return to original working directory
        os.chdir(current_work_dir)

        # Delete any files copied across
        if os.path.isfile(os.path.join(self.work_dir, os.path.basename(self.hklin))):
            os.remove(os.path.join(self.work_dir, os.path.basename(self.hklin)))
        if os.path.isfile(os.path.join(self.work_dir, os.path.basename(self.pdbin))):
            os.remove(os.path.join(self.work_dir, os.path.basename(self.pdbin)))
Ejemplo n.º 5
0
def run_phaser(
    data_file,
    struct_file,
    spacegroup,
    db_settings=False,
    tag=False,
    work_dir=False,
    adf=False,
    name=False,
    ncopy=1,
    cell_analysis=False,
    resolution=False,
    full=False,
):
    """
    Run Phaser and passes results back to RAPD Redis DB
    **Requires Phaser src code!**

    data_file - input data as mtz (required)
    struct_file - input search model path in mmCIF or PDB format (required)
    spacegroup - The space group to run MR (required)

    tag - a Redis key where the results are sent (cluster mode)
    db_settings - Redis connection settings for sending results (cluster mode)
    work_dir - working directory (defaults to current working dir)
    name - root name for output files (defaults to spacegroup)
    ncopy - number of molecules to search for
    cell_analysis - internal RAPD signal so all possible SG's are searched
    resolution - high res limit to run MR (float)
    full - signal to run more comprehensive MR
    """

    phaser_log = False
    # Change to work_dir
    if not work_dir:
        work_dir = os.getcwd()
    os.chdir(work_dir)

    if not name:
        name = spacegroup

    # # Handle CIF file input -> PDB
    # if struct_file[-3:] == "cif":
    #     pdb.cif_as_pdb(struct_file)
    #     struct_file = struct_file.replace(".cif", ".pdb")

    # Read the dataset
    i = phaser.InputMR_DAT()
    i.setHKLI(convert_unicode(data_file))
    i.setLABI_F_SIGF('F', 'SIGF')
    i.setMUTE(True)
    r1 = phaser.runMR_DAT(i)
    # Need to determine Phaser version for keyword changes!
    version = re.search(r'Version:\s*([\d.]+)', r1.logfile()).group(1)

    if r1.Success():
        i = phaser.InputMR_AUTO()
        # i.setREFL_DATA(r1.getREFL_DATA())
        # i.setREFL_DATA(r1.DATA_REFL())
        i.setREFL_F_SIGF(r1.getMiller(), r1.getFobs(), r1.getSigFobs())
        i.setCELL6(r1.getUnitCell())
        if struct_file[-3:].lower() == "cif":
            #i.addENSE_CIF_ID('model', cif, 0.7)
            ### Typo in PHASER CODE!!! <<<CIT>>> ###
            i.addENSE_CIT_ID('model', convert_unicode(struct_file), 0.7)
        else:
            i.addENSE_PDB_ID('model', convert_unicode(struct_file), 0.7)
        i.addSEAR_ENSE_NUM("model", ncopy)
        i.setSPAC_NAME(spacegroup)
        if cell_analysis:
            i.setSGAL_SELE("ALL")
            # Set it for worst case in orth
            # number of processes to run in parallel where possible
            i.setJOBS(1)
        else:
            i.setSGAL_SELE("NONE")
        if full:
            # Picks own resolution
            # Round 2, pick best solution as long as less that 10% clashes
            i.setPACK_SELE("PERCENT")
            i.setPACK_CUTO(0.1)
            #command += "PACK CUTOFF 10\n"
        else:
            # For first round and cell analysis
            # Only set the resolution limit in the first round or cell analysis.
            if resolution:
                i.setRESO_HIGH(resolution)
            else:
                i.setRESO_HIGH(6.0)
            # If Phaser version < 2.6.0
            if int(version.split('.')[1]) <= 6:
                i.setSEAR_DEEP(False)
            else:
                i.setSEAR_METH("FAST")

            # Don"t seem to work since it picks the high res limit now.
            # Get an error when it prunes all the solutions away and TF has no input.
            # command += "PEAKS ROT SELECT SIGMA CUTOFF 4.0\n"
            # command += "PEAKS TRA SELECT SIGMA CUTOFF 6.0\n"
        # Turn off pruning in 2.6.0
        i.setSEAR_PRUN(False)
        # Choose more top peaks to help with getting it correct.
        i.setPURG_ROTA_ENAB(True)
        i.setPURG_ROTA_NUMB(3)
        #command += "PURGE ROT ENABLE ON\nPURGE ROT NUMBER 3\n"
        i.setPURG_TRAN_ENAB(True)
        i.setPURG_TRAN_NUMB(1)
        #command += "PURGE TRA ENABLE ON\nPURGE TRA NUMBER 1\n"

        # Only keep the top after refinement.
        i.setPURG_RNP_ENAB(True)
        i.setPURG_RNP_NUMB(1)
        #command += "PURGE RNP ENABLE ON\nPURGE RNP NUMBER 1\n"
        i.setROOT(convert_unicode(name))
        # i.setMUTE(False)
        i.setMUTE(True)
        # Delete the setup results
        # del(r)
        # launch the run
        # r = phaser.runMR_AUTO(i)

        try:
            r = phaser.runMR_AUTO(i)
        except RuntimeError as e:
            # print "Hit error"
            # Known CIF error - convert to pdb and retry
            if struct_file[-3:] in ('cif', ):
                # print "Convert to pdb"
                pdb.cif_as_pdb((struct_file, ))
                pdb_file = struct_file.replace(".cif", ".pdb")

                i = phaser.InputMR_AUTO()
                # i.setREFL_DATA(r1.getREFL_DATA())
                # i.setREFL_DATA(r1.DATA_REFL())
                i.setREFL_F_SIGF(r1.getMiller(), r1.getFobs(), r1.getSigFobs())
                i.setCELL6(r1.getUnitCell())
                i.addENSE_PDB_ID('model', convert_unicode(pdb_file), 0.7)
                i.addSEAR_ENSE_NUM("model", ncopy)
                i.setSPAC_NAME(spacegroup)
                if cell_analysis:
                    i.setSGAL_SELE("ALL")
                    # Set it for worst case in orth
                    # number of processes to run in parallel where possible
                    i.setJOBS(1)
                else:
                    i.setSGAL_SELE("NONE")
                if full:
                    # Picks own resolution
                    # Round 2, pick best solution as long as less that 10% clashes
                    i.setPACK_SELE("PERCENT")
                    i.setPACK_CUTO(0.1)
                    #command += "PACK CUTOFF 10\n"
                else:
                    # For first round and cell analysis
                    # Only set the resolution limit in the first round or cell analysis.
                    if resolution:
                        i.setRESO_HIGH(resolution)
                    else:
                        i.setRESO_HIGH(6.0)
                    # If Phaser version < 2.6.0
                    if int(version.split('.')[1]) <= 6:
                        i.setSEAR_DEEP(False)
                    else:
                        i.setSEAR_METH("FAST")

                    # Don"t seem to work since it picks the high res limit now.
                    # Get an error when it prunes all the solutions away and TF has no input.
                    # command += "PEAKS ROT SELECT SIGMA CUTOFF 4.0\n"
                    # command += "PEAKS TRA SELECT SIGMA CUTOFF 6.0\n"
                # Turn off pruning in 2.6.0
                i.setSEAR_PRUN(False)
                # Choose more top peaks to help with getting it correct.
                i.setPURG_ROTA_ENAB(True)
                i.setPURG_ROTA_NUMB(3)
                #command += "PURGE ROT ENABLE ON\nPURGE ROT NUMBER 3\n"
                i.setPURG_TRAN_ENAB(True)
                i.setPURG_TRAN_NUMB(1)
                #command += "PURGE TRA ENABLE ON\nPURGE TRA NUMBER 1\n"

                # Only keep the top after refinement.
                i.setPURG_RNP_ENAB(True)
                i.setPURG_RNP_NUMB(1)
                #command += "PURGE RNP ENABLE ON\nPURGE RNP NUMBER 1\n"
                i.setROOT(convert_unicode(name))
                # i.setMUTE(False)
                i.setMUTE(True)
                # Delete the setup results
                # del(r)
                # launch the run
                r = phaser.runMR_AUTO(i)
            else:
                raise e

        if r.Success():
            # print r
            pass
            #if r.foundSolutions():
            #print "Phaser has found MR solutions"
            #print "Top LLG = %f" % r.getTopLLG()
            #print "Top PDB file = %s" % r.getTopPdbFile()
            #else:
            #print "Phaser has not found any MR solutions"
        else:
            print "Job exit status FAILURE"
            print r.ErrorName(), "ERROR :", r.ErrorMessage()

        # Save log files for debugging
        phaser_log = r.logfile()
        with open('phaser.log', 'w') as log:
            log.write(r.logfile())
            log.close()

        if r.foundSolutions():
            rfz = None
            tfz = None
            tncs = False
            # Parse results
            for p in r.getTopSet().ANNOTATION.split():
                # print p
                # For v 2.8.3
                # RF*0\nTF*0\nLLG=30699\nTFZ==174.8\nPAK=0\nLLG=30699\nTFZ==174.8\n
                if p.count('RFZ'):
                    if p.count('=') in [1]:
                        rfz = float(p.split('=')[-1])
                if p.count('RF*0'):
                    rfz = "NC"
                if p.count('TFZ'):
                    if p.count('=') in [1]:
                        tfz = p.split('=')[-1]
                        if tfz == '*':
                            tfz = 'arbitrary'
                        else:
                            tfz = float(tfz)
                if p.count('TF*0'):
                    tfz = "NC"
            tncs_test = [
                1 for line in r.getTopSet().unparse().splitlines()
                if line.count("+TNCS")
            ]
            tncs = bool(len(tncs_test))
            mtz_file = os.path.join(work_dir, r.getTopMtzFile())
            phaser_result = {
                "ID": name,
                "solution": r.foundSolutions(),
                "pdb_file": os.path.join(work_dir, r.getTopPdbFile()),
                "mtz": mtz_file,
                "gain": float(r.getTopLLG()),
                "rfz": rfz,
                # "tfz": r.getTopTFZ(),
                "tfz": tfz,
                "clash": r.getTopSet().PAK,
                "dir": os.getcwd(),
                "spacegroup":
                r.getTopSet().getSpaceGroupName().replace(' ', ''),
                "tNCS": tncs,
                "nmol": r.getTopSet().NUM,
                "adf": None,
                "peak": None,
            }

            # Calculate 2Fo-Fc & Fo-Fc maps
            # foo.mtz begets foo_2mFo-DFc.ccp4 & foo__mFo-DFc.ccp4
            local_subprocess(command="phenix.mtz2map %s" % mtz_file,
                             logfile='map.log',
                             shell=True)

            # Map files should now exist
            map_2_1 = mtz_file.replace(".mtz", "_2mFo-DFc.ccp4")
            map_1_1 = mtz_file.replace(".mtz", "_mFo-DFc.ccp4")

            # Make sure the maps exist and then package them
            if os.path.exists(map_2_1):
                # Compress the map
                arch_prod_file, arch_prod_hash = archive.compress_file(map_2_1)
                # Remove the map that was compressed
                os.unlink(map_2_1)
                # Store information
                map_for_display = {
                    "path": arch_prod_file,
                    "hash": arch_prod_hash,
                    "description": "map_2_1"
                }
                phaser_result["map_2_1"] = map_for_display

            if os.path.exists(map_1_1):
                # Compress the map
                arch_prod_file, arch_prod_hash = archive.compress_file(map_1_1)
                # Remove the map that was compressed
                os.unlink(map_1_1)
                # Store information
                map_for_display = {
                    "path": arch_prod_file,
                    "hash": arch_prod_hash,
                    "description": "map_1_1"
                }
                phaser_result["map_1_1"] = map_for_display

            # If PDB exists, package that too
            if phaser_result.get("pdb_file", False):
                if os.path.exists(phaser_result.get("pdb_file")):
                    # Compress the file
                    arch_prod_file, arch_prod_hash = archive.compress_file(
                        phaser_result.get("pdb_file"))
                    # Remove the map that was compressed
                    # os.unlink(phaser_result.get("pdb"))
                    # Store information
                    pdb_for_display = {
                        "path":
                        arch_prod_file,
                        "hash":
                        arch_prod_hash,
                        "description":
                        os.path.basename(phaser_result.get("pdb_file"))
                    }
                    phaser_result["pdb"] = pdb_for_display

            # Calc ADF map
            if adf:
                if os.path.exists(phaser_result.get(
                        "pdb_file", False)) and os.path.exists(
                            phaser_result.get("mtz", False)):
                    adf_results = calc_ADF_map(data_file=data_file,
                                               mtz=phaser_result["mtz"],
                                               pdb=phaser_result["pdb_file"])
                    if adf_results.get("adf"):
                        phaser_result.update({
                            "adf":
                            os.path.join(work_dir, adf_results.get("adf"))
                        })
                    if adf_results.get("peak"):
                        phaser_result.update({
                            "peak":
                            os.path.join(work_dir, adf_results.get("peak"))
                        })
                    #phaser_result.update({"adf": adf_results.get("adf", None),
                    #                      "peak": adf_results.get("peak", None),})

            # print "1"
            # print name
            # New procedure for making tar of results
            # Create directory
            # Remove the run # from the name
            # new_name = name[:-2]  #
            new_name = phaser_result.get("ID")  #
            # print new_name
            os.mkdir(new_name)
            # # Go through and copy files to archive directory
            file_types = ("pdb_file", "mtz", "adf", "peak")
            for file_type in file_types:
                # print file_type
                target_file = phaser_result.get(file_type, False)
                # print target_file
                if target_file:
                    if os.path.exists(target_file):
                        # Copy the file to the directory to be archived
                        shutil.copy(target_file, new_name + "/.")
            # # Create the archive
            archive_result = archive.create_archive(new_name)
            archive_result["description"] = '%s_files' % new_name
            phaser_result["tar"] = archive_result

            # print "2"

        else:
            phaser_result = {
                "ID": name,
                "solution": False,
                "message": "No solution",
                "spacegroup": spacegroup
            }
        # Add the phaser log
        if phaser_log:
            phaser_result.update({"logs": {"phaser": phaser_log}})

        # print "3"

        if db_settings and tag:
            print "db_settings and tag"
            # Connect to Redis
            redis = connect_to_redis(db_settings)
            # Key should be deleted once received, but set the key to expire in 24 hours just in case.
            redis.setex(tag, 86400, json.dumps(phaser_result))
            # Do a little sleep to make sure results are in Redis for postprocess_phaser
            time.sleep(0.1)
        else:
            # print "Printing phaser_result"
            # Print the result so it can be seen thru the queue by reading stdout
            # print phaser_result
            print json.dumps(phaser_result)
Ejemplo n.º 6
0
    def run(self):
        """Function to run molecular replacement using PHASER

        Returns
        -------
        file
            Output pdb file
        file
            Output log file
        """

        # Make a note of the current working directory
        current_work_dir = os.getcwd()

        # Change to the PHASER working directory
        if os.path.exists(self.work_dir):
            os.chdir(self.work_dir)
        else:
            os.makedirs(self.work_dir)
            os.chdir(self.work_dir)

        # Copy hklin and pdbin to working dire for efficient running of PHASER
        hklin = os.path.join(self.work_dir, os.path.basename(self.hklin))
        shutil.copyfile(self.hklin, hklin)
        pdbin = os.path.join(self.work_dir, os.path.basename(self.pdbin))
        shutil.copyfile(self.pdbin, pdbin)

        i = InputMR_DAT()
        i.setHKLI(hklin)

        if self.hires:
            i.setHIRES(self.hires)
        if self.autohigh:
            i.setRESO_AUTO_HIGH(self.autohigh)
        if self.i != "None" and self.sigi != "None":
            i.setLABI_I_SIGI(self.i, self.sigi)
        elif self.f != "None" and self.sigf != "None":
            i.setLABI_F_SIGF(self.f, self.sigf)
        else:
            msg = "No flags for intensities or amplitudes have been provided"
            raise RuntimeError(msg)
        i.setSGAL_SELE(SGAlternatives[self.sgalternative].value)
        i.setMUTE(True)
        r = runMR_DAT(i)

        if r.Success():
            i = InputMR_AUTO()
            i.setJOBS(1)
            i.setREFL_DATA(r.getREFL_DATA())
            i.setROOT("phaser_mr_output")
            i.addENSE_PDB_ID("PDB", pdbin, 0.7)
            i.setCOMP_BY("SOLVENT")
            i.setCOMP_PERC(self.solvent)
            i.addSEAR_ENSE_NUM('PDB', self.nmol)
            i.setSGAL_SELE(SGAlternatives[self.sgalternative].value)
            if self.timeout != 0:
                i.setKILL_TIME(self.timeout)
            i.setMUTE(True)
            del (r)
            r = runMR_AUTO(i)

            with open(self.logfile, 'w') as f:
                f.write(r.summary())

            shutil.move(r.getTopPdbFile(), self.pdbout)

            # Output original mtz with a change of basis if needed
            space_group, _, _ = mtz_util.crystal_data(r.getTopMtzFile())
            ed = mtz_util.ExperimentalData(self.hklin)
            ed.change_space_group(space_group)
            ed.output_mtz(self.hklout)

        # Return to original working directory
        os.chdir(current_work_dir)

        # Delete any files copied across
        if os.path.isfile(os.path.join(self.work_dir, os.path.basename(self.hklin))):
            os.remove(os.path.join(self.work_dir, os.path.basename(self.hklin)))
        if os.path.isfile(os.path.join(self.work_dir, os.path.basename(self.pdbin))):
            os.remove(os.path.join(self.work_dir, os.path.basename(self.pdbin)))
Ejemplo n.º 7
0
def run_phaser(root, xml_file, xmlroot, docid, output, logfile, data, formfactors, pdbin, copies, rms, pdbin_fixed,
               seqin, ncs_copies, phaser_solvent, sgalternative, sg_list, input_hand, tncs,
               search_lowres, search_highres, rot_peaks, rot_cluster_off,
               rot_samp, tra_samp, search_down, tfz_solved, solutions, purge,
               rescore_strands, rescore_residues, rescore_models, nproc):
  input = phaser.InputMR_AUTO()
  input.setRESO(search_highres, search_lowres)
  # test OpenMP
  if nproc > 1:
    input.setJOBS(nproc)
  else:
    input.setJOBS(1)
  if rot_samp is not None:
    input.setSAMP_ROTA(rot_samp)
  if tra_samp is not None:
    input.setSAMP_TRAN(tra_samp)
  input.setSPAC_HALL(data.getSpaceGroupHall())
  input.setCELL6(data.getUnitCell())
  input.setREFL_DATA(data.getDATA())
  # electron formfactors
  if formfactors == 'electron':
    input.setFORM('ELECTRON')
  input.addENSE_PDB_RMS(root, pdbin, rms)
  input.setROOT(root)
  # fixed solution
  if pdbin_fixed is not None:
    fixed_root = pdbin_fixed[:-4]
    input.addENSE_PDB_RMS(fixed_root, pdbin_fixed, rms)
    input.addSOLU_6DIM_ENSE(fixed_root, [0.0, 0.0, 0.0], False, [0.0, 0.0, 0.0], 0.0, False, False, False, 1.0, 1.0)
  if phaser_solvent is not None:
    input.setCOMP_SOLV()
    input.setCOMP_PERC(phaser_solvent)
  else:
    input.addCOMP_PROT_SEQ_NUM(seqin, ncs_copies)
  input.addSEAR_ENSE_NUM(root, copies)
  if rot_peaks is not None:
    input.setPEAK_ROTA_SELE('NUMBER')
    input.setPEAK_ROTA_CUTO(rot_peaks)
  if rot_cluster_off:
    input.setPEAK_ROTA_CLUS(False)
  if sgalternative:
    input.setSGAL_SELE('ALL')
  elif input_hand:
    input.setSGAL_SELE('NONE')
  elif sg_list is not None:
    input.setSGAL_SELE('LIST')
    for sg in sg_list.split(','):
      input.addSGAL_TEST(sg)
  else:
    input.setSGAL_SELE('HAND')
  input.setTOPF(solutions)
  if rescore_strands or rescore_residues or rescore_models:
    # we don't need to write out the files here
    input.setXYZO(False)
    input.setHKLO(False)
  input.setXYZO_ENSE(False)

  # turn off tNCS correction if required
  if tncs:
    # to turn restore default MR_AUTO behaviour needs this
    input.setTNCS_PATT_PERC(99.9)
  # in desparate cases search further down the RF and TF list
  if search_down is not None:
    input.setSEAR_DOWN_PERC(search_down)
    input.setPURG_TRAN_PERC(75.00 - search_down)
  # turn purging on if requested for multicopy searches
  if purge is not None:
    input.setPURG_RNP_ENAB(True)
    input.setPURG_RNP_NUMB(purge)
   # turn it off otherwise (so more solutions are output)
  else:
    input.setPURG_RNP_ENAB(False)
  # these are not default but fragments should never clash.
  input.setPACK_KEEP_HIGH_TFZ(False)
  input.setSEAR_PRUN(False)
  input.setTRAN_PACK_CUTO(0.0)
  input.setPACK_CUTO(0.0)
  # end non defaults
  input.setZSCO_USE(True)
  input.setZSCO_SOLV(tfz_solved)
  if xml_file is not None and xmlroot is not None:
    input.setOUTP_LEVE('LOGFILE') #i2
  elif docid is not None:
    input.setOUTP_LEVE('LOGFILE') #rvapi
  else:
    input.setOUTP_LEVE('SUMMARY') #command line
  phaser_output = phaser.Output()
  phaser_log = open(logfile, 'w')
  output_object = CallbackObject(xml_file, xmlroot, docid, output)
  phaser_output.setPhenixPackageCallback(output_object)
  phaser_output.set_file_object(phaser_log)
  mr = phaser.runMR_AUTO(input, phaser_output)
  phaser_log.close()
  if mr.Success():
    if mr.foundSolutions() :
      log.info('\nPhaser has found %d solution(s)' % mr.numSolutions())
      if not rescore_strands or rescore_residues:
        log.info('Top solution PDB file = %s' % mr.getTopPdbFile())
      log.info('Top solution LLG = %f' % mr.getTopLLG())
      return mr.getDotSol()
    else:
      log.info('\nPhaser failed to find any solutions')
      return 'No solutions'
  else:
      log.info('\nPhaser failed to find any solutions')
      return 'No solutions'