Example #1
0
def main():
    args = parse_args()

    trajectory = load_pdb(args.trajectory)
    average = calc_average(trajectory, args.cutoff)

    prody.writePDB(args.out_file, average)
def extract_heteroatoms_pdbs(pdb,
                             create_file=True,
                             ligand_chain="L",
                             get_ligand=False,
                             output_folder="."):
    """
    From a pdb file, it extracts the chain L and checks if the structure has hydrogens. After that, the chain L is
    written in a new PDB file which will have the following format: "{residue name}.pdb".
    :param pdb: pdb file (with a ligand in the chain L).
    :return: Writes a new pdb file "{residue name}.pdb" with the chain L isolated an returns the residue name (string).
    """
    # Parse the complex file and isolate the ligand core and the fragment
    ligand = complex_to_prody.pdb_parser_ligand(pdb, ligand_chain)
    if ligand is None:
        logger.critical(
            "The ligand can not be found. Ensure that the ligand of {} is the chain {}"
            .format(pdb, ligand_chain))
    # Check if the ligand has H
    complex_to_prody.check_protonation(ligand)
    # Save the ligand in a PDB (the name of the file is the name of the residue)
    ligand_name = ligand.getResnames()[0]
    if create_file:
        prody.writePDB(os.path.join(output_folder, ligand_name), ligand)
        print(
            "The ligand of {} has been extracted and saved in '{}.pdb'".format(
                pdb, os.path.join(output_folder, ligand_name)))
    if get_ligand is True:
        return ligand
    else:
        return ligand_name
Example #3
0
def getResidueSasa(atoms, return_atom_asa=False):
    protein_atoms = atoms.protein.noh
    assert protein_atoms.getHierView().numChains() == 1
    
    pdb_file = tempfile.NamedTemporaryFile(prefix='tmp_naccess_', suffix='.pdb', delete=False)
    
    pdb_filename = pdb_file.name
    prody.writePDB(pdb_filename, protein_atoms)
    pdb_file.close()
    
    prefix, ext = os.path.splitext(pdb_filename)
    #print prefix, ext
    rsa_filename = '{}.rsa'.format(prefix)
    asa_filename = '{}.asa'.format(prefix)
    
    # run naccess on the temporary PDB file, with $CWD set to 
    # the PDB file's directory. This is because naccess dumps
    # all the output files in $CWD, and they aren't necessary
    # after parsing.
    subprocess.Popen(
                    [NACCESS_BINARY, pdb_filename, ], 
                    cwd=os.path.dirname(pdb_filename), 
                        ).communicate()
    
    # read the resulting .rsa file into a table
    rsa_table = read_naccess_rsa(rsa_filename)
    
    if return_atom_asa:
        atom_asa = read_naccess_asa(asa_filename)
        
        assert len(atom_asa) == len(protein_atoms), '{} != {}'.format(len(atom_asa), len(protein_atoms))
        return rsa_table, atom_asa
    return rsa_table
Example #4
0
def selection_to_pdb(selection):
    """
    :param selection: prody selection
    :return: writes a PDB file containing the selection, named using the residue name of the first atom of the selection.
    """
    prody.writePDB(selection.getResnames()[0], selection)
    return "{}.pdb".format(selection.getResnames()[0])
Example #5
0
def prody_biomol(pdbname, **kwargs):
    """Generate biomolecule coordinates.

    :arg pdb:  PDB identifier or filename

    :arg prefix: prefix for output files, default is :file:`_biomol`

    :arg biomol: index of the biomolecule, by default all are generated"""

    import prody
    LOGGER = prody.LOGGER
    prefix, biomol = kwargs.get('prefix', None), kwargs.get('biomol')
    pdb, header = prody.parsePDB(pdbname, header=True)
    if not prefix:
        prefix = pdb.getTitle()

    biomols = prody.buildBiomolecules(header, pdb, biomol=biomol)
    if not isinstance(biomols, list):
        biomols = [biomols]

    for i, biomol in enumerate(biomols):
        if isinstance(biomol, prody.Atomic):
            outfn = '{0}_biomol_{1}.pdb'.format(prefix, i + 1)
            LOGGER.info('Writing {0}'.format(outfn))
            prody.writePDB(outfn, biomol)
        elif isinstance(biomol, tuple):
            for j, part in enumerate(biomol):
                outfn = ('{0}_biomol_{1}_part_{2}.pdb'.format(
                    prefix, i + 1, j + 1))
                LOGGER.info('Writing {0}'.format(outfn))
                prody.writePDB(outfn, part)
Example #6
0
def prody_biomol(opt):
    """Generate biomolecule coordinates based on command line arguments."""
        
    import prody
    LOGGER = prody.LOGGER
    prefix, biomol = opt.prefix, opt.biomol
    pdb, header = prody.parsePDB(opt.pdb, header=True)
    if not prefix:
        prefix = pdb.getTitle()
        
    biomols = prody.buildBiomolecules(header, pdb, biomol=biomol)
    if not isinstance(biomols, list):
        biomols = [biomols]
    
    for i, biomol in enumerate(biomols):
        if isinstance(biomol, prody.Atomic):
            outfn = '{0:s}_biomol_{1:d}.pdb'.format(prefix, i+1)
            LOGGER.info('Writing {0:s}'.format(outfn))
            prody.writePDB(outfn, biomol)
        elif isinstance(biomol, tuple):
            for j, part in enumerate(biomol):
                outfn = ('{0:s}_biomol_{1:d}_part_{2:d}.pdb'
                         .format(prefix, i+1, j+1))
                LOGGER.info('Writing {0:s}'.format(outfn))
                prody.writePDB(outfn, part)
Example #7
0
def prody_biomol(pdbname,**kwargs):
    """Generate biomolecule coordinates.

    :arg pdb:  :term:`PDB` identifier or filename
    
    :arg prefix: prefix for output files, default is :file:`_biomol`
    
    :arg biomol: index of the biomolecule, by default all are generated"""
        
    import prody
    LOGGER = prody.LOGGER
    prefix, biomol = kwargs.get('prefix',None), kwargs.get('biomol')
    pdb, header = prody.parsePDB(pdbname, header=True)
    if not prefix:
        prefix = pdb.getTitle()
        
    biomols = prody.buildBiomolecules(header, pdb, biomol=biomol)
    if not isinstance(biomols, list):
        biomols = [biomols]
    
    for i, biomol in enumerate(biomols):
        if isinstance(biomol, prody.Atomic):
            outfn = '{0:s}_biomol_{1:d}.pdb'.format(prefix, i+1)
            LOGGER.info('Writing {0:s}'.format(outfn))
            prody.writePDB(outfn, biomol)
        elif isinstance(biomol, tuple):
            for j, part in enumerate(biomol):
                outfn = ('{0:s}_biomol_{1:d}_part_{2:d}.pdb'
                         .format(prefix, i+1, j+1))
                LOGGER.info('Writing {0:s}'.format(outfn))
                prody.writePDB(outfn, part)
Example #8
0
def main():
    in_file, trajectory, out_file, force_const = parse_args()
    average = load_pdb(in_file)
    trajectory = load_pdb(trajectory)
    starting_model = get_closest_frame(trajectory, average)
    minimized_protein = run_minimization(average, starting_model, force_const)
    prody.writePDB(out_file, minimized_protein)
 def extract_ligand(self, path):
     """
     Writes the ligand into a file.
     :param path: path to write the ligand.
     :return:
     """
     prody.writePDB(path, self.selection)
Example #10
0
def clean_pdb(pdb_file, chain, write_out=True):
    pdb = pdy.parsePDB(pdb_file)
    p = pdb.select(f'stdaa and chain {chain}')

    # Ensure that all resnums are above 0
    res_nums = [
        res_num for res_num in sorted(list(set(p.getResnums()))) if res_num > 0
    ]

    # Remove Insertion Codes
    clean_res_num = []
    for res_num in res_nums:
        if len(set(p.select(f'resnum {res_num}').getIcodes())) > 1:
            clean_res_num.append(
                f'{res_num}_')  # "_" selects residue with no insertion codes
        else:
            clean_res_num.append(str(res_num))

    clean_protein = p.select('resnum {}'.format(' '.join(clean_res_num)))

    pdb_name = os.path.basename(pdb_file).split('.')[0]

    if write_out:
        pdy.writePDB(f'./pdb_clean_new/{pdb_name}_{chain}.pdb', clean_protein)

    # Select cleaned residues
    return clean_protein
Example #11
0
    def print_cluster(self, mems, label, outdir, number=None):
        """prints pdbs of the top number of hotspots (number) to the output directory (outdir)."""
        if outdir[-1] != '/':
            outdir += '/'
        if number:
            mems = random.sample(mems, number)
        for mem in mems:
            resnum_chid = tuple(self._all_resnum_chid[mem])
            type_ = self._all_type[mem]
            typestr = type_
            resn = self._all_resn[mem]
            vdm_tags = self._all_vdm_tags[mem]

            if typestr == 'PHI_PSI':
                typestr = 'PHI_PSI/' + self.rel_vdm_phipsi_bin[resnum_chid]
            pdbpath = self.rel_vdm_path + typestr + '/pdbs/' + resn + '/'
            filename = 'iFG_' + str(vdm_tags[0]) + '_vdM_' + str(vdm_tags[1]) + '_iFlip_' \
                       + str(vdm_tags[2]) + '_' + self.name + '_' + 'oriented.pdb.gz'
            pdb = pr.parsePDB(pdbpath + filename)
            old_coords = pdb.getCoords()
            new_coords = \
                np.dot((old_coords - self._rois_rot_trans[resnum_chid][type_][resn][1]),
                       self._rois_rot_trans[resnum_chid][type_][resn][0]) \
                + self._rois_rot_trans[resnum_chid][type_][resn][2]
            pdb.setCoords(new_coords)
            newfile_path = outdir + 'cluster/' + label + '/'
            newfile_name = self.name + '_cluster_' + label + '_' \
                           + ''.join(str(x) for x in resnum_chid) + '_' + type_ + '_' + filename
            try:
                os.makedirs(newfile_path)
            except:
                pass
            pr.writePDB(newfile_path + newfile_name, pdb)
Example #12
0
 def print_hotspots(self, outdir, number=25):
     "prints pdbs of the top number of hotspots (number) to the output directory (outdir)."
     if outdir[-1] != '/':
         outdir += '/'
     for i in range(number):
         for resnum_chid, index in self.hotspots[i]:
             resn = self._resn[resnum_chid][index]
             type_ = self._type[resnum_chid][index]
             typestr = type_
             vdm_tags = self._vdm_tags[resnum_chid][index]
             if typestr == 'PHI_PSI':
                 typestr = 'PHI_PSI/' + self.rel_vdm_phipsi_bin[resnum_chid]
             pdbpath = self.rel_vdm_path + typestr + '/pdbs/' + resn + '/'
             filename = 'iFG_' + str(vdm_tags[0]) + '_vdM_' + str(vdm_tags[1]) + '_iFlip_' \
                               + str(vdm_tags[2]) + '_' + self.name + '_' + 'oriented.pdb.gz'
             pdb = pr.parsePDB(pdbpath + filename)
             old_coords = pdb.getCoords()
             new_coords = \
                 np.dot((old_coords - self._rois_rot_trans[resnum_chid][type_][resn][1]),
                        self._rois_rot_trans[resnum_chid][type_][resn][0]) \
                 + self._rois_rot_trans[resnum_chid][type_][resn][2]
             pdb.setCoords(new_coords)
             newfile_path = outdir + 'hotspots/' + str(i + 1) + '/'
             newfile_name = self.name + '_hotspot_' + str(i + 1) + '_' \
                            + ''.join(str(x) for x in resnum_chid) + '_' + type_ + '_' + filename
             try:
                 os.makedirs(newfile_path)
             except:
                 pass
             pr.writePDB(newfile_path + newfile_name, pdb)
Example #13
0
def preprocess_single(model, chain_name):

    hv = model.getHierView()
    for chain in hv.iterChains():
        chain.setChids(chain_name)

    prody.writePDB("tmp.pdb", model)
    model = prody.parsePDB("tmp.pdb")
    check_call(["rm", "tmp.pdb"])

    pos = 1
    apos = 1

    hv = model.getHierView()
    for chain in hv.iterChains():
        # print "chain : " , chain
        for res in chain.iterResidues():
            res.setResnums(pos)
            for atom in res:
                atom.setSerial(apos)
                apos += 1
            if len(res.getIcode()) == 0:
                pos += 1
    hv.update()
    return model
Example #14
0
    def print_hotspot(self, hs_subgr_num, outdir):
        """prints pdbs of the top number of hotspots (number) to the output directory (outdir)."""
        if outdir[-1] != '/':
            outdir += '/'
        for node in self.hotspot_subgraphs[hs_subgr_num]:
            resnum_chid = node[0]
            type_ = self.hotspot_subgraphs[hs_subgr_num].node[node]['type']
            typestr = type_
            resn = self.hotspot_subgraphs[hs_subgr_num].node[node]['resn']
            vdm_tags = self.hotspot_subgraphs[hs_subgr_num].node[node][
                'vdm_tags']

            if typestr == 'PHI_PSI':
                typestr = 'PHI_PSI/' + self.rel_vdm_phipsi_bin[resnum_chid]
            pdbpath = self.rel_vdm_path + typestr + '/pdbs/' + resn + '/'
            filename = 'iFG_' + str(vdm_tags[0]) + '_vdM_' + str(vdm_tags[1]) + '_iFlip_' \
                              + str(vdm_tags[2]) + '_' + self.name + '_' + 'oriented.pdb.gz'
            pdb = pr.parsePDB(pdbpath + filename)
            old_coords = pdb.getCoords()
            new_coords = \
                np.dot((old_coords - self._rois_rot_trans[resnum_chid][type_][resn][1]),
                       self._rois_rot_trans[resnum_chid][type_][resn][0]) \
                + self._rois_rot_trans[resnum_chid][type_][resn][2]
            pdb.setCoords(new_coords)
            newfile_path = outdir + 'hotspots/' + str(hs_subgr_num + 1) + '/'
            newfile_name = self.name + '_hotspot_' + str(hs_subgr_num + 1) + '_' \
                           + ''.join(str(x) for x in resnum_chid) + '_' + type_ + '_' + filename
            try:
                os.makedirs(newfile_path)
            except:
                pass
            pr.writePDB(newfile_path + newfile_name, pdb)
Example #15
0
        def PDB_creator(self, peptides: list, overlaps: list):

            res = er.PepExtractor(database=self.db, resfile=self.resfile)

            last_pep = peptides[0][:-1]
            last_conform = peptides[0][-1]
            r1 = res.extract_result(str(last_pep), int(last_conform))
            seq = last_pep
            for i in range(1, len(peptides)):
                last_pep = peptides[i][:-1]
                last_conform = peptides[i][-1]
                last_overlap = overlaps[i - 1]
                r2 = res.extract_result(str(last_pep), int(last_conform))
                r1_ = r1.select(
                    'resnum 2:{0}'.format(len(seq) + 2)).toAtomGroup()
                r2_ = r2.select('resnum ' + str(last_overlap + 2) +
                                ':5').toAtomGroup()
                c_ = 5
                for r_ in r2_.iterResidues():
                    r_.setResnum(c_)
                    c_ += 1
                r1 = r1_ + r2_
                seq += last_pep[last_overlap:]

                prody.writePDB(
                    '{0}{1}{2}.pdb'.format(self.outdir, str(len(seq)), seq),
                    r1)
Example #16
0
def  main():
    in_file, trajectory, out_file, force_const = parse_args()
    average = load_pdb(in_file)
    trajectory = load_pdb(trajectory)
    starting_model = get_closest_frame(trajectory, average)
    minimized_protein = run_minimization(average, starting_model, force_const)
    prody.writePDB(out_file, minimized_protein)
Example #17
0
def save_representatives(
        representatives,
        pdb_name,
        workspace_handler,
        trajectory_holder,
        do_merged_files_have_correlative_models,
        write_frame_number_instead_of_correlative_model_number,
        keep_remarks=False):
    """
    Saves a pdb file containing the most representative elements of the clustering.

    @param representatives: A list of the representative elements of the clustering we want to extract.

    @param workspace_handler: The workspace handler of this run.

    @param trajectory_holder: The trajectory handler for this run or an array with pdb file paths.

    @param do_merged_files_have_correlative_models: When merging, output file will have models from 0 to M, where M is the total number
    of frames of the merged file.

    @param write_frame_number_instead_of_model_number: When extracting frames, extract those models which number coincides with the
    frame numbers in 'representatives'. Otherwise, extract those models which position coincide with the frame number in
    'representatives'.
    """
    results_directory = workspace_handler["results"]

    # Merge pdbs (in order)
    temporary_merged_trajectory_path = os.path.join(
        workspace_handler["tmp"], "tmp_merged_trajectory.pdb")

    #===========================================================
    # THIS DOES NOT WORK IF USING DCD FILES
    #     merge_pdbs(trajectory_holder,
    #                temporary_merged_trajectory_path,
    #                do_merged_files_have_correlative_models)

    # TEMPORARY HACK TO OVERCOME DCD MERGING BUG

    merged_pdb = trajectory_holder.getMergedStructure()
    prody.writePDB(temporary_merged_trajectory_path, merged_pdb)
    #==========================================================

    # Extract frames from the merged pdb
    file_handler_in = open(temporary_merged_trajectory_path, "r")
    file_handler_out = open(
        os.path.join(results_directory, "%s.pdb" % pdb_name), "w")

    pdb_tools.extract_frames_from_trajectory_sequentially(
        file_handler_in=file_handler_in,
        number_of_frames=pdb_tools.get_number_of_frames(
            temporary_merged_trajectory_path),
        file_handler_out=file_handler_out,
        frames_to_save=representatives,
        write_frame_number_instead_of_correlative_model_number=
        write_frame_number_instead_of_correlative_model_number,
        keep_header=keep_remarks)
    file_handler_in.close()
    file_handler_out.close()

    return os.path.join(results_directory, "%s.pdb" % pdb_name)
Example #18
0
    def _generate_sidechains_scwrl(self):
        if not self.rec is None:
            rec = self.rec.copy()
            rec.setChids('A')
            lig = self._tpl.copy()
            rec = BasePDB(ag=rec)
            lig = BasePDB(ag=lig)
            merged = rec.add_mol(lig, keep_resi=False, keep_chains=True)
            merged.save(self._mrg_file)

            self._make_scwrl_sequence_file()
            call = [
                define.SCWRL_EXE, '-h', '-i', self._mrg_file, '-o',
                self._scw_file, '-s', self._seq_file
            ]
        else:
            prody.writePDB(self._mrg_file, self._tpl)
            call = [
                define.SCWRL_EXE, '-h', '-i', self._mrg_file, '-o',
                self._scw_file
            ]

        # scwrl wants rosetta hydrogen naming
        BasePDB(self._mrg_file).to_rosetta().save(self._mrg_file)

        helpers.shell_call(call)

        pep = prody.parsePDB(self._scw_file)

        # extract peptide and renumber
        pep = BasePDB(ag=pep.select('chain B').copy()).renumber(
            keep_resi=False).ag
        self.pep = pep
Example #19
0
def preprocess_single( model, chain_name ):

	hv = model.getHierView()
	for chain in hv.iterChains():
		chain.setChids( chain_name )
		
	prody.writePDB( 'tmp.pdb', model )
	model = prody.parsePDB( 'tmp.pdb' )
	check_call( ["rm",  "tmp.pdb"] )

	pos=1
	apos=1

	hv = model.getHierView()
	for chain in hv.iterChains():
		#print "chain : " , chain
		for res in chain.iterResidues():
			res.setResnums( pos )
			for atom in res:
				atom.setSerial( apos )
				apos += 1
			if len( res.getIcode() ) == 0:
				pos += 1
	hv.update()
	return model
def savePDB(pdb, filename):
    """
    Saves a prody pdb data structure in a pdb format file (simple wrapper).

    @param pdb: A prody pdb data structure.
    """
    prody.writePDB(filename, pdb)
Example #21
0
def phi2pdb(base_pdb, phi, save_path="./"):
    pdb = prody.parsePDB(base_pdb)
    atoms = [a for a in pdb]
    for a in pdb:
        a.setBeta(0.0)

    phif = open(phi, "r")
    phic = phif.readlines()
    phif.close()

    for l in phic:
        x,y,z,k = l.strip().split(",")
        x = np.float(x)
        y = np.float(y)
        z = np.float(z)
        k = np.float(k)
        
        for a in xrange(len(atoms)):
            X,Y,Z = atoms[a].getCoords()
            if X==x and Y==y and Z==z:
                atoms[a].setBeta(k)
                atoms.pop(a)
                break

    out_pdb = os.path.join(save_path, phi.split(os.sep)[-1][:-4]+".pdb")
    prody.writePDB(out_pdb, pdb)

    return out_pdb
Example #22
0
def main():
    args = parse_args()

    trajectory = load_pdb(args.trajectory)
    average = calc_average(trajectory)

    prody.writePDB(args.out_file, average)
Example #23
0
def _load_traj(ag):
    h, tmp = tempfile.mkstemp(dir='.', suffix='.pdb')
    os.close(h)
    prody.writePDB(tmp, ag)
    traj = md.load_pdb(tmp, frame=0)
    os.remove(tmp)
    return traj
def projectCentralitiesOntoProteinPyMol(centrality, centralityArray, out_file, \
                                        selectedAtoms, scalingFactor):
    """
    Produces PyMol output files for visualizing protein centralities.

    This function writes a pml file and a PDB file that can be viewed in
    VMD. Bfactor field of the protein contains the centrality information.
    The first N residues with the highest centrality are highlighed in VDW
    representation.  that  that contains the centralities on
    on Bfactor field of the pdb.
    The output files can be visualized with VMD (Visual Molecular
    dynamics) program as follows: pymol output.pml

    Parameters
    ----------
    centrality: string
        It can have 'degree', 'betweenness', 'closeness',
        'current_flow_betweenness' or 'current_flow_closeness'.
    centralityArray: A numpy data array ?
        It is a numpy matrix of typically nDCC, LMI or Generalized Correlations.
    out_file: string
        Prefix of the output file. According to the centralty measure, it will be
        extended.
    selectedAtoms: object
        This is a prody.parsePDB object of typically CA atoms of a protein.
    ScalingFactor: float
        Sometimes, the values of the centrality arrays are too small.
        The scaling factor multiplies the array to make the values visible in
        the Bfactor colums.

    Returns
    -------
    Nothing

    """

    percentage = 0.10
    numKeyResidues = int(percentage * len(selectedAtoms))
    PML_FILE = open(out_file + '_' + centrality + '.pml', 'w')

    PML_FILE.write("load " + out_file + "_" + centrality + ".pdb" + "\n")
    PML_FILE.write("cartoon type = tube\n")
    PML_FILE.write("spectrum b\n")
    PML_FILE.write("set sphere_scale, 0.75\n\n")

    vdw_representation_string = "show spheres, chain {0:s} and resi {1:d} and name ca\n"

    sortedList = np.flip(np.argsort(centralityArray))
    for i in range(0, numKeyResidues):
        # print(centralityArray[sortedList[i]])
        PML_FILE.write(vdw_representation_string.\
            format(selectedAtoms.getChids()[sortedList[i]],
                    selectedAtoms.getResnums()[sortedList[i]]))

    selectedAtoms.setBetas([scalingFactor * i for i in centralityArray])

    writePDB(out_file + '_' + centrality + '.pdb', selectedAtoms)

    PML_FILE.close()
Example #25
0
def main():
    in_file, trajectory, out_file, skip_first = parse_args()
    average = load_pdb(in_file)
    trajectory = load_pdb(trajectory)
    starting_model = get_closest_frame(trajectory,
                                       average,
                                       skip_first=skip_first)
    prody.writePDB(out_file, starting_model)
Example #26
0
def make_interactamers(df, an, outpath, comb):
    if outpath[-1] != '/':
        outpath += '/'
    resns = set(df.groupby('resname_vdm').groups).intersection(
        set(interactamer_atoms.keys()))
    for resn in resns:
        for key in interactamer_atoms[resn].keys():
            origin_atom, plane_atom1, plane_atom2 = interactamer_atoms[resn][
                key]
            vdms = parse_interactamers_aa(df, an, resn)
            if vdms:
                pdb_path = outpath + 'pdbs/' + resn + '/' + key + '/'
                try:
                    os.makedirs(pdb_path)
                except:
                    pass
                picklepath = outpath + 'pickle/'
                try:
                    os.makedirs(picklepath)
                except:
                    pass
                rel_vdm_output = []

                print('Making interactamer vdMs for ' + resn + ', ' + key +
                      '...')
                for vdm in vdms:
                    try:
                        ifg_count = int(repr(vdm).split('_')[1])
                        vdm_count = int(repr(vdm).split('_')[3])
                        # if resn == 'PRO':
                        #     bb_coords, sc_coords, ifg_coords, pdbs = make_rel_vdm_coords(vdm, comb, origin_atom,
                        #                                                                  'CD', plane_atom2)
                        # else:
                        bb_coords, sc_coords, sc_coords_ON, sc_coords_CS, ifg_coords, ifg_ON_coords, ifg_CS_coords, pdbs = make_rel_vdm_coords(
                            vdm, comb, origin_atom, plane_atom1, plane_atom2)
                        for i, bbc, scc, sccon, scccs, ic, icon, iccs, pdb in zip(
                                range(len(bb_coords)), bb_coords, sc_coords,
                                sc_coords_ON, sc_coords_CS, ifg_coords,
                                ifg_ON_coords, ifg_CS_coords, pdbs):
                            rel_vdm_output.append([
                                ifg_count, vdm_count, i + 1, bbc, scc, sccon,
                                scccs, ic, icon, iccs, resn
                            ])
                            string = repr(pdb).split()[1].split('_')
                            pr.writePDB(
                                pdb_path + '_'.join(string[:-1]) + '_iFlip_' +
                                str(i + 1) + '_' + string[-1] +
                                '_oriented.pdb.gz', pdb)
                    except Exception:
                        traceback.print_exc()
                # output format = [ifg_count, vdm_count, ifg_flip, bb_coords, sc_coords_all, sc_coords_ON, sc_coords_CS, ifg_coords, ifg_ON_coords, ifg_CS_coords, vdm_resn]

                if rel_vdm_output:
                    with open(picklepath + resn + '_rel_vdms.pickle',
                              'wb') as f:
                        pickle.dump(np.array(rel_vdm_output, dtype=object), f)
                else:
                    shutil.rmtree(pdb_path)
    def write_PDB(self, structure, pdb_name):
        """
        Write a prody protein to a pdb file
        :param protein: protein object from prody
        :param pdb_name: base name for the pdb file
        :return: None
        """

        prody.writePDB(f"{pdb_name}", structure)
Example #28
0
    def write_superposed_pdbs(self, output_pdb_folder, alignments: dict = None):
        """
        Superposes PDBs according to alignment and writes transformed PDBs to files
        (View with Pymol)

        Parameters
        ----------
        alignments
        output_pdb_folder
        """
        if alignments is None:
            alignments = self.alignment
        output_pdb_folder = Path(output_pdb_folder)
        if not output_pdb_folder.exists():
            output_pdb_folder.mkdir()
        reference_name = self.structures[0].name
        reference_pdb = pd.parsePDB(
            str(self.output_folder / f"cleaned_pdb/{self.structures[0].name}.pdb")
        )
        core_indices = np.array(
            [
                i
                for i in range(len(alignments[reference_name]))
                if -1 not in [alignments[n][i] for n in alignments]
            ]
        )
        aln_ref = alignments[reference_name]
        ref_coords_core = (
            reference_pdb[helper.get_alpha_indices(reference_pdb)]
            .getCoords()
            .astype(np.float64)[np.array([aln_ref[c] for c in core_indices])]
        )
        ref_centroid = helper.nb_mean_axis_0(ref_coords_core)
        ref_coords_core -= ref_centroid
        transformation = pd.Transformation(np.eye(3), -ref_centroid)
        reference_pdb = pd.applyTransformation(transformation, reference_pdb)
        pd.writePDB(str(output_pdb_folder / f"{reference_name}.pdb"), reference_pdb)
        for i in range(1, len(self.structures)):
            name = self.structures[i].name
            pdb = pd.parsePDB(
                str(self.output_folder / f"cleaned_pdb/{self.structures[i].name}.pdb")
            )
            aln_name = alignments[name]
            common_coords_2 = (
                pdb[helper.get_alpha_indices(pdb)]
                .getCoords()
                .astype(np.float64)[np.array([aln_name[c] for c in core_indices])]
            )
            (
                rotation_matrix,
                translation_matrix,
            ) = superposition_functions.svd_superimpose(
                ref_coords_core, common_coords_2
            )
            transformation = pd.Transformation(rotation_matrix.T, translation_matrix)
            pdb = pd.applyTransformation(transformation, pdb)
            pd.writePDB(str(output_pdb_folder / f"{name}.pdb"), pdb)
Example #29
0
def main():
    r = parse_args()
    input_pdb_filename = r[0]
    trajectory_filename = r[1]
    output_pdb_filename = r[2]
    force_const = r[3]
    m = Minimizer(input_pdb_filename, trajectory_filename)
    minimized_protein = m.run_minimization(force_const)
    prody.writePDB(output_pdb_filename, minimized_protein)
Example #30
0
def ligand_parse_write(path, out, lig_name):

    pose = parsePDB(path)

    writePDB(out + 'lig_' + path.split('/')[-1],
             pose.select('hetero and resname {}'.format(lig_name)))

    ligand_path = out + 'lig_' + path.split('/')[-1]

    return ligand_path
Example #31
0
    def save(self, pdb, **kwargs):
        if not pdb.endswith('.pdb'):
            raise RuntimeError('Filename must end with .pdb')

        ag = self.ag.copy()
        new_names = ['%4s' % ('%-3s' % a) if not ('0' <= a[0] <= '9') else '%-4s' % a for a in ag.getNames()]
        ag.setNames(new_names)

        prody.writePDB(pdb, ag, **kwargs)
        return pdb
Example #32
0
def main():
    from argparse import ArgumentParser

    parser = ArgumentParser()

    parser.add_argument("--ftfile-line",
                        "-n",
                        type=int,
                        default=0,
                        help="0 indexed")
    parser.add_argument("--limit",
                        "-l",
                        type=int,
                        default=None,
                        help="limit for number of ft lines read")
    parser.add_argument("ligand", help="ligand used for docking")
    parser.add_argument("ft_file",
                        help="ftresults file to draw transformations from.")
    parser.add_argument(
        "rot_file",
        help="Rotation file used during PIPER run to make ft_file.")
    parser.add_argument("--ft_limit",
                        help="Speficy how many ftfile lines to read")

    args = parser.parse_args()

    rotations = read_rotations(args.rot_file)

    #read ft_files
    ftresults = read_ftresults(args.ft_file, args.limit)

    #get center information for original ligands
    lig_orig = parsePDB(args.ligand)
    lig_center = np.mean(lig_orig.getCoords(), axis=0)
    lig_name = args.ligand
    lig_base = lig_name.rsplit('/', 1)[-1][:-4]

    #apply ft lines to the aligned ligands
    coords_apply = apply_ftresults_atom_group(lig_orig,
                                              ftresults,
                                              rotations,
                                              center=lig_center)

    #create new atom group for translated ligand
    lig_new = AtomGroup('%s.%s.pdb' % (lig_base, args.ftfile_line))

    #only choose one coordinate set (0 indexed)
    coords_apply.setACSIndex(args.ftfile_line)
    lig_new.setCoords(coords_apply.getCoords())
    lig_new.setNames(lig_orig.getNames())
    lig_new.setResnames(lig_orig.getResnames())
    lig_new.setResnums(lig_orig.getResnums())

    #write new ligand file
    writePDB("%s.%s.pdb" % (lig_base, args.ftfile_line), lig_new)
Example #33
0
def split_receptor(bucket, table_idx, param,
                   datum):  # todo (maksym) param = params;
    try:  # todo (maksym) datum = pdb_name
        if type(datum).__name__ in ['tuple', 'list']:
            datum = datum[0]

        receptor = datum  # todo receptor = pdb_name !!!!!
        output_folder = param['output_folder']
        output_folder = '{}_{}'.format(table_idx, output_folder)
        input_download_folder = param['input_download_folder']

        input_pdb_dir = os.path.join(data_dir, input_download_folder)
        input_pdb_path = os.path.join(input_pdb_dir, receptor + '.pdb')

        parsed_pdb = prody.parsePDB(input_pdb_path)
        parsed_header = prody.parsePDBHeader(input_pdb_path)

        output_rec_dir = os.path.join(data_dir, output_folder, receptor)
        _makedir(output_rec_dir)

        ligands = []
        for chem in parsed_header['chemicals']:
            chain, resnum, resname = chem.chain, chem.resnum, chem.resname
            ligands.append([chain, str(resnum), resname])

        for chain, resnum, resname in ligands:
            try:
                rec = parsed_pdb.select('not (chain {} resnum {})'.format(
                    chain, resnum))
                rec = rec.select('not water')
                heavy_atom = rec.select('not hydrogen').numAtoms()
                rec_name = '_'.join(
                    [receptor, chain, resnum, resname, 'receptor']) + '.pdb'
                prody.writePDB(os.path.join(output_rec_dir, rec_name), rec)

                record = [
                    receptor, chain, resnum, resname, heavy_atom,
                    parsed_header['experiment'], parsed_header['resolution'],
                    1, 'success'
                ]
                records = [record]
                db.insert(table_idx, records, bucket=bucket)
            except Exception as e:
                record = [
                    receptor, chain, resnum, resname, 0, 0, 0, 0,
                    str(e)
                ]  # datum = failure_message
                records = [record]
                print(records)
                db.insert(table_idx, records, bucket=bucket)

    # TODO: (maksym) I believe this is controllable with logging
    except Exception as e:
        print(e)
        raise Exception(str(e))
Example #34
0
def get_pocket(input_path):
    """
    select protein atoms from pocket file
    """

    output_path = input_path.replace(config.input_data_dir, config.data_dir)
    if not os.path.exists(output_path):
        parsed = prody.parsePDB(pocket_input)
        pocket = parsed.select('protein')
        _mkdir(os.path.dirname(output_path))
        prody.writePDB(output_path, pocket)
def write_pdb(structure, file_name='file.pdb'):
    """Takes a Prody structure prody.AtomGroup and writes it on a pdb file

    Parameters
    -------------
    strucure : prody.AtomGroup
    file_name : str
        default "file.pdb"
    """

    prody.writePDB(file_name, structure)
Example #36
0
def writepdb_and_get_pickle_info(pdb_path, pklout, vdm, comb, origin_atom,
                                 plane_atom1, plane_atom2, resn, ifg_count,
                                 vdm_count):
    '''helper function for make_rel_vdms and make_interactamers.
    Need vdmelem1dists to choose which vdm flipping to keep.'''

    if resn != 'TYR' and resn not in flip_residues:
        bb_coords, sc_coords, sc_coords_ON, sc_coords_CS, ifg_coords, ifg_ON_coords, ifg_CS_coords, pdbs, vdmelem1avgdists = \
            make_rel_vdm_coords(vdm, comb, origin_atom, plane_atom1, plane_atom2)

    elif resn == 'TYR':
        x = make_rel_vdm_coords(vdm, comb, origin_atom, plane_atom1,
                                plane_atom2)
        y = make_rel_vdm_coords(vdm,
                                comb,
                                origin_atom,
                                'CE2',
                                plane_atom2,
                                unflipped=False)
        xelem1dist = x[8]
        yelem1dist = y[8]
        if xelem1dist <= yelem1dist:
            bb_coords, sc_coords, sc_coords_ON, sc_coords_CS, ifg_coords, ifg_ON_coords, ifg_CS_coords, pdbs, vdmelem1avgdists = x
        else:
            bb_coords, sc_coords, sc_coords_ON, sc_coords_CS, ifg_coords, ifg_ON_coords, ifg_CS_coords, pdbs, vdmelem1avgdists = y

    elif resn in flip_residues:
        x = make_rel_vdm_coords(vdm, comb, origin_atom, plane_atom1,
                                plane_atom2)
        y = make_rel_vdm_coords(vdm,
                                comb,
                                origin_atom,
                                plane_atom2,
                                plane_atom1,
                                unflipped=False)
        xelem1dist = x[8]
        yelem1dist = y[8]
        if xelem1dist <= yelem1dist:
            bb_coords, sc_coords, sc_coords_ON, sc_coords_CS, ifg_coords, ifg_ON_coords, ifg_CS_coords, pdbs, vdmelem1avgdists = x
        else:
            bb_coords, sc_coords, sc_coords_ON, sc_coords_CS, ifg_coords, ifg_ON_coords, ifg_CS_coords, pdbs, vdmelem1avgdists = y

    for i, bbc, scc, sccon, scccs, ic, icon, iccs, pdb in zip(range(len(bb_coords)), bb_coords, sc_coords, \
            sc_coords_ON, sc_coords_CS, ifg_coords, ifg_ON_coords, ifg_CS_coords, pdbs):

        pklout.append([
            ifg_count, vdm_count, bbc, scc, sccon, scccs, ic, icon, iccs, resn
        ])
        string = repr(pdb).split()[1].split('_')
        pr.writePDB(
            pdb_path + '_'.join(string[:-1]) + '_' + string[-1] +
            '_oriented.pdb.gz', pdb)
    return pklout
Example #37
0
def write_pdb(structure, file_name="file.pdb"):
    """
    Takes a Prody strucure prody.AtomGroup and writes it on a pdb file

    strucure :: prody.AtomGroup
    
    file_name :: string, default "file.pdb"

    returns nothing
    """

    prody.writePDB(file_name, structure)
Example #38
0
def save_all_clusters(my_params, pdb_params, workspaceHandler, trajectoryHandler, clustering, generatedFiles, timer):
    timer.start("Save clusters")

    #Parameters
    keep_remarks = my_params["keep_remarks"] if "keep_remarks" in my_params else False
    keep_frame_number = my_params["keep_frame_number"] if "keep_frame_number" in my_params else False

    # Places
    results_place = workspaceHandler["results"]
    clusters_place = workspaceHandler["clusters"]
    tmp_place = workspaceHandler["tmp"]

    #===========================================================
    # THIS DOES NOT WORK IF USING DCD OR STRUCTS
    # The real job
#     input_path = os.path.join(tmp_place, "tmp_merged_trajectory.pdb")
#     merge_pdbs(pdb_params, input_path)

    # TEMPORARY HACK TO OVERCOME DCD MERGING BUG

    merged_pdb = trajectoryHandler.getMergedStructure()
    input_path = os.path.join(tmp_place, "tmp_merged_trajectory.pdb")
    prody.writePDB(input_path, merged_pdb)
#==========================================================

    number_of_frames = get_number_of_frames(input_path)
    cluster_files = []
    for cluster in clustering.clusters:
        output_path = os.path.join(clusters_place, "%s.pdb"%(cluster.id))
        cluster_files.append(output_path)
        file_handler_in = open(input_path,"r")
        file_handler_out = open(output_path,"w")
        extract_frames_from_trajectory_sequentially(file_handler_in,
                                                    number_of_frames,
                                                    file_handler_out,
                                                    cluster.all_elements,
                                                    keep_header = keep_remarks,
                                                    write_frame_number_instead_of_correlative_model_number=keep_frame_number)
        file_handler_in.close()
        file_handler_out.close()

    # Add all bz2 files to a tar file
    tar_path = os.path.join(results_place,"clusters.tar.gz")
    tar = tarfile.open(tar_path, "w:gz")
    for comp_file in cluster_files:
        tar.add(comp_file, os.path.basename(comp_file))
    tar.close()
    timer.stop("Save clusters")

    generatedFiles.append({"description":"Clusters",
                                         "path":os.path.abspath(tar_path),
                                         "type":"compressed_pdb"})
Example #39
0
	def _find_ligand(self):
		self._get_file_path(ligand=True)
		protein = parsePDB(self.file_path)
		try:
			seq = protein['A'].getSequence()
		except:
			pass
		else:
			ligand = protein.select('not protein and not water')
			repr(ligand)
			if ligand:
				self.out_filename = self.file_path.split('.')[0] + '_ligand.pdb'
				writePDB(self.out_filename, ligand)
Example #40
0
def get_protein(input_path):
    """
    select protein atoms from protein file
    """
    try:
        output_path = input_path.replace(config.input_data_dir, config.data_dir)
        if not os.path.exists(output_path):
            parsed = prody.parsePDB(input_path)
            pocket = parsed.select('protein')
            _mkdir(os.path.dirname(output_path))
            prody.writePDB(output_path, pocket)
    except Exception as e:
        print e
Example #41
0
def split(receptor, pdb_outpath, init='split_init'):

    init = eval(init)
    rec_dir = os.path.join(init.data_dir, init.rec_folder)
    lig_dir = os.path.join(init.data_dir, init.lig_folder)

    pdb_path = os.path.join(init.data_dir, pdb_outpath)
    parsed_pdb = prody.parsePDB(pdb_path)
    parsed_header = prody.parsePDBHeader(pdb_path)

    ligands = []
    for chem in parsed_header['chemicals']:
        ligands.append([chem.chain, str(chem.resnum), chem.resname])

    splited = []

    for chain, resnum, resname in ligands:

        lig = parsed_pdb.select('chain {} resnum {}'.format(chain, resnum))
        rec = parsed_pdb.select('not (chain {} resnum {})'.format(
            chain, resnum))
        if lig is None:
            continue
        resid = lig.getHierView().iterResidues().next().getResindex()
        resid = str(resid)
        heavy_lig = lig.select('not hydrogen')
        heavy_atom = heavy_lig.numAtoms()
        heavy_coord = heavy_lig.getCoords()
        #max_size_on_axis = max(heavy_coord.max(axis=0) - heavy_coord.min(axis=0))
        #Changing max_size_on_axis to max pairwise distance between coords
        #max_size_on_axis = max(scipy.spatial.distance.pdist(heavy_coord).tolist())
        lig_name = '_'.join([receptor, chain, resnum, resname, 'ligand'
                             ]) + '.pdb'
        if not os.path.exists(os.path.join(lig_dir, receptor)):
            os.makedirs(os.path.join(lig_dir, receptor))
        prody.writePDB(os.path.join(lig_dir, receptor, lig_name), lig)

        rec_name = '_'.join([receptor, chain, resnum, resname, 'receptor'
                             ]) + '.pdb'
        if not os.path.exists(os.path.join(rec_dir, receptor)):
            os.makedirs(os.path.join(rec_dir, receptor))
        prody.writePDB(os.path.join(rec_dir, receptor, rec_name), rec)

        splited.append([
            receptor,
            str(resname),
            os.path.join(init.rec_folder, receptor, rec_name),
            os.path.join(init.lig_folder, receptor, lig_name)
        ])

    return splited
Example #42
0
def write_pdb_dir(pdb_dir, struct_data):

    # import prody for pdb read/write
    import prody

    # create output directory, if not yet present
    if not(os.path.exists(pdb_dir)):
        os.makedirs(pdb_dir)

    # write protein chain pdb files to output directory
    for (pdb_f, struct) in struct_data:
        if not(struct is None):
            out_f = os.path.join(pdb_dir, pdb_f)
            prody.writePDB(out_f, struct)
Example #43
0
def prody_contacts(**kwargs):
    """Identify contacts of a target structure with one or more ligands.
    Contacting atoms (or extended subset of atoms, such as residues) are 
    outputted in PDB file format.
    
    :arg target: target PDB identifier or filename
    
    :arg ligand: ligand PDB identifier(s) or filename(s)

    :arg select: atom selection string for target structure
    
    :arg radius: contact radius (Ã…), default is ``4.0`` 
    
    :arg extend: output same ``'residue'``, ``'chain'``, or ``'segment'`` along 
        with contacting atoms
    
    :arg prefix: prefix for output file, default is *target* filename
    
    :arg suffix: output filename suffix, default is *ligand* filename"""
            
    import prody
    LOGGER = prody.LOGGER

    target = prody.parsePDB(kwargs['target'])
    title = kwargs.get('prefix') or target.getTitle()
    selstr = kwargs.get('select')
    if selstr:
        target = target.select(selstr)
    contacts = prody.Contacts(target)
    suffix = kwargs.get('suffix', '_contacts')
    extend = kwargs.get('extend')
    radius = float(kwargs.get('radius', 4.0))
    ligands = kwargs.get('ligand')
    if len(ligands) > 1:
        outfn = lambda fn: title + suffix + '_' + fn + '.pdb'
    else:
        outfn = lambda fn: title + suffix + '.pdb'
    for pdb in ligands:
        ligand = prody.parsePDB(pdb)
        sel = contacts(radius, ligand)
        if sel:
            LOGGER.info('{0} atoms from {1} contact {2}.'
                        .format(len(sel), pdb, str(target)))
            if extend:
                sel = target.select('same ' + extend + ' as sel', sel=sel)
                LOGGER.info('Selection is extended to {0} atoms of the same '
                            '{1}(s).'.format(len(sel), extend))
            pdbfn = outfn(ligand.getTitle())
            LOGGER.info('Writing contacts into ' + pdbfn)
            prody.writePDB(pdbfn, sel)
Example #44
0
def prody_select(opt):
    """Write selected atoms from a PDB file in PDB format."""

    import prody
    LOGGER = prody.LOGGER
    pdb = prody.parsePDB(opt.pdb)
    prefix = opt.output
    if not prefix:
        prefix = pdb.getTitle() + '_selected'
    pdbselect = pdb.select(opt.selstr)
    if pdbselect is None:
        opt.subparser('Selection "{0:s}" do not match any atoms.'
                      .format(opt.selstr))
    LOGGER.info('Writing ' + prefix + '.pdb')
    prody.writePDB(prefix + '.pdb', pdbselect)
Example #45
0
def prody_align(opt):
    """Align models in a PDB file or a PDB file onto others."""
            
    import prody
    LOGGER = prody.LOGGER

    args = opt.pdb
    if len(args) == 1:
        pdb = args[0]
        LOGGER.info('Aligning multiple models in: ' + pdb)
        selstr, prefix, model = opt.select, opt.prefix, opt.model
        pdb = prody.parsePDB(pdb)
        pdbselect = pdb.select(selstr)
        if pdbselect is None:
            opt.subparser.error('Selection {0:s} do not match any atoms.'
                               .format(repr(selstr)))
        LOGGER.info('{0:d} atoms will be used for alignment.'
                    .format(len(pdbselect)))
        pdbselect.setACSIndex(model-1)
        prody.printRMSD(pdbselect, msg='Before alignment ')
        prody.alignCoordsets(pdbselect)
        prody.printRMSD(pdbselect, msg='After alignment  ')
        if prefix == '':
            prefix = pdb.getTitle() + '_aligned'
        outfn = prefix + '.pdb'
        LOGGER.info('Writing file: ' + outfn)
        prody.writePDB(outfn, pdb)
    else:
        reffn = args.pop(0)
        seqid=opt.seqid
        overlap=opt.overlap
        LOGGER.info('Aligning structures onto: ' + reffn)
        ref = prody.parsePDB(reffn)
        for arg in args:
            if arg == reffn:
                continue
            if '_aligned.pdb' in arg:
                continue
            pdb = prody.parsePDB(arg)
            result = prody.matchAlign(pdb, ref, seqid=seqid, overlap=overlap, 
                                      tarsel=opt.select, allcsets=True,
                                      cslabel='Model', csincr=1) 
            if result:
                outfn = pdb.getTitle() + '_aligned.pdb'
                LOGGER.info('Writing file: ' + outfn)
                prody.writePDB(outfn, pdb)
            else:
                LOGGER.warning('Failed to align ' + arg)
Example #46
0
def fix_openmm():
    # get the whole crystal structure
    # get only the ATOM records
    # and HETAM records for MSE
    # convert MSE to MET
    with open('no_smet.pdb', 'w') as outfile:
        with open('experimental.pdb') as infile:
            for line in infile:
                if line.startswith('ATOM'):
                    outfile.write(line)
                if line.startswith('HETATM'):
                    if line[17:20] == 'MSE':
                        atom_name = line[12:17]
                        if atom_name == 'SE   ':
                            atom_name = ' SD  '
                        line_fixed = 'ATOM  ' + line[6:12] + atom_name + 'MET' + line[20:67] + '\n'
                        outfile.write(line_fixed)

    # load the file into prody
    p = prody.parsePDB('no_smet.pdb')
    p = p.select('not hydrogen')

    # get one of the rosetta models
    r = prody.parsePDB('rosetta.pdb')

    # perform an alignment to find out what part of the crystal structure
    # corresponds to the rosetta file
    match = prody.matchChains(r, p, subset='all', overlap=25, pwalign=True)[0][1]
    print len(match)
    prody.writePDB('chain.pdb', match)

    # now clean it up with pdb fixer
    subprocess.check_call('python ~/Source/PdbFixer/pdbfixer.py chain.pdb', shell=True)

    # now load it with zam
    p = protein.Protein('output.pdb')
    p.Dehydrogen()
    disulfide_pairs = find_disulfide(p)
    for r1, r2 in disulfide_pairs:
        print '    added disulfide between {} and {}'.format(r1, r2)
        p.Res[r1].FullName = 'CYX'
        p.Res[r2].FullName = 'CYX'
    p.WritePdb('start.pdb')

    # now run tleap
    print '    running tleap'
    run_tleap(disulfide_pairs)
def combine_structures(directory_with_pdbs, output_filename):
    search_string = join(directory_with_pdbs, '*.pdb')
    path_list = glob(search_string)

    if len(path_list) > MAX_FRAMES:
        raise RuntimeError('Got %d frames, but only up to %d frames are allowed.')
    else:
        pass

    atom_group = parsePDB(path_list[0])
    for i, path in enumerate(path_list):
        if i == 0:
            continue
        else:
            p = parsePDB(path)
            atom_group.addCoordset(p)
    writePDB(output_filename, atom_group)
Example #48
0
def prody_align(opt):
    """Align models in a PDB file or a PDB file onto others."""
            
    import prody
    LOGGER = prody.LOGGER

    args = opt.pdb
    if len(args) == 1:
        pdb = args[0]
        LOGGER.info('Aligning multiple models in: ' + pdb)
        selstr, prefix, model = opt.select, opt.prefix, opt.model
        pdb = prody.parsePDB(pdb)
        pdbselect = pdb.select(selstr)
        if pdbselect is None:
            LOGGER.warning('Selection "{0:s}" do not match any atoms.'
                           .format(selstr))
            sys.exit(-1)
        LOGGER.info('{0:d} atoms will be used for alignment.'
                               .format(len(pdbselect)))
        pdb.setACSIndex(model-1)
        prody.alignCoordsets(pdb, selstr=selstr)
        rmsd = prody.calcRMSD(pdb)
        LOGGER.info('Max RMSD: {0:0.2f} Mean RMSD: {1:0.2f}'
              .format(rmsd.max(), rmsd.mean()))
        if prefix == '':
            prefix = pdb.getTitle() + '_aligned'
        outfn = prefix + '.pdb'
        LOGGER.info('Writing file: ' + outfn)
        prody.writePDB(outfn, pdb)
    else:
        reffn = args.pop(0)
        LOGGER.info('Aligning structures onto: ' + reffn)
        ref = prody.parsePDB(reffn)
        for arg in args:
            if arg == reffn:
                continue
            if '_aligned.pdb' in arg:
                continue
            pdb = prody.parsePDB(arg)
            if prody.matchAlign(pdb, ref):
                outfn = pdb.getTitle() + '_aligned.pdb'
                LOGGER.info('Writing file: ' + outfn)
                prody.writePDB(outfn, pdb)
            else:
                LOGGER.warning('Failed to align ' + arg)
Example #49
0
def test(pdb='2nwl-mem.pdb', blk='2nwl.blk'):

    from prody import parsePDB
    from numpy import zeros

    pdb = parsePDB(pdb, subset='ca')
    pdb.setData('block', zeros(len(pdb), int))
    with open(blk) as inp:
        for line in inp:
            if line.startswith('BLOCK'):
                _, b, n1, c1, r1, n2, c2, r2 = line.split()
                sel = pdb.select('chain {} and resnum {} to {}'
                                 .format(c1, r1, r2))
                if sel:
                    sel.setData('block', int(b))
    pdb.setBetas(pdb.getData('block'))
    from prody import writePDB
    writePDB('pdb2gb1_truncated.pdb', pdb)
    rtb = RTB('2nwl')
    rtb.buildHessian(pdb, pdb.getData('block'))
    return rtb
def preprocess_pdb(args):
    pdb_file = args[1]
    output = "./" + args[2]+"/"+args[2]
    create_directory("./" + args[2])
    cluster_frames = get_frame_numbers(args)
    pdb = prody.parsePDB(pdb_file)
    # Get a copy of the pdb coords
    input_coordsets = numpy.array(pdb.getCoordsets()[cluster_frames])

    # Empty pdb
    pdb.delCoordset(range(pdb.numCoordsets()))

    # Build another pdb to store it
    input_pdb = prody.parsePDB(pdb_file)
    input_pdb.delCoordset(range(input_pdb.numCoordsets()))
    # And add the chosen coordsets
    for i in range(len(cluster_frames)):
        input_pdb.addCoordset(input_coordsets[i])
    prody.writePDB(output+"_ini.pdb", input_pdb)
    print_matrix(input_pdb.select("name CA").getCoordsets(), output)
    return pdb, input_coordsets, cluster_frames, output
def PCA_Analysis(predID, input_pdbs, tmp_mut_pdb):
    # http://prody.csb.pitt.edu/tutorials/ensemble_analysis/xray_calculations.html#calculations
    ref_structure = prody.parsePDB(tmp_mut_pdb)
    ref_selection = ref_structure.select('calpha')
    for ref_chain in ref_structure.getHierView():
        prody.startLogfile('%s_%s_log'%(predID, ref_chain.getChid()))
        ensemble = prody.PDBEnsemble('%s_%s_ensemble'%(predID, ref_chain.getChid()))
        ensemble.setAtoms(ref_chain.select('calpha'))
        ensemble.setCoords(ref_chain.select('calpha'))
        print 'Generating Ensemble...'
        for input_pdb in input_pdbs:
            structure = prody.parsePDB(input_pdb, subset='calpha')
            mappings = prody.mapOntoChain(structure, ref_chain)
            atommap = mappings[0][0]
            ensemble.addCoordset(atommap, weights=atommap.getFlags('mapped'))
        ensemble.iterpose()
        prody.closeLogfile('%s_%s_log'%(predID, ref_chain.getChid()))
        prody.writePDB('%s_%s_ensemble.pdb'%(predID, ref_chain.getChid()), ensemble)

        pca = prody.PCA('%s_%s_pca'%(predID, ref_chain.getChid()))
        pca.buildCovariance(ensemble)
        pca.calcModes()
        print pca.getEigvecs()
Example #52
0
def calc_sasa(pdb, sel='protein', per_res=False, path_dssp='C:\Python27\Scripts\dssp-2.0.4-win32.exe'):
    import os as os
    import tempfile as tempfile
    import shutil as shutil
    import prody as prody
    import numpy as np

    dssp_in = 'dssp.in'

    prevdir = os.getcwd()
    with tempfile.TemporaryDirectory() as tmpdir:
        os.chdir(os.path.expanduser(tmpdir))

        prody.writePDB(dssp_in+'.pdb', pdb.select(sel))
        pdb_dssp = prody.parsePDB(dssp_in+'.pdb')
        execDSSP(dssp_in+'.pdb', dssp=path_dssp)
        prody.parseDSSP(dssp_in+'.dssp', pdb_dssp)

        if per_res:
            return(pdb_dssp._data['dssp_acc'])
        else:
            return(np.sum(pdb_dssp._data['dssp_acc']))

    os.chdir(prevdir)
Example #53
0
def test(pdb="2nwl-mem.pdb", blk="2nwl.blk"):

    from prody import parsePDB
    from numpy import zeros, linalg

    pdb = parsePDB(pdb, subset="ca")
    pdb.setData("block", zeros(len(pdb), int))
    with open(blk) as inp:
        for line in inp:
            if line.startswith("BLOCK"):
                _, b, n1, c1, r1, n2, c2, r2 = line.split()
                sel = pdb.select("chain {} and resnum {} to {}".format(c1, r1, r2))
                if sel:
                    sel.setData("block", int(b))
    pdb.setBetas(pdb.getData("block"))
    coords = pdb.getCoords()
    blocks = pdb.getBetas()
    from prody import writePDB

    writePDB("pdb2gb1_truncated.pdb", pdb)
    rtb = RTB("2nwl")
    rtb.buildHessian(coords, blocks, scale=64)
    # rtb.calcModes()
    return rtb
Example #54
0
def prody_select(selstr, *pdbs, **kwargs):
    """Write selected atoms from a PDB file in PDB format.
    
    :arg selstr: atom selection string, see :ref:`selections`
    
    :arg pdbs: :term:`PDB` identifier(s) or filename(s)
    
    :arg output: output filename, default is :file:`pdb_selected.pdb`

    :arg prefix: prefix for output file, default is PDB filename
    
    :arg suffix: output filename suffix, default is :file:`_selected`"""

    from os.path import isfile
    from prody import LOGGER, parsePDB, writePDB
    
    #selstr = kwargs.get('selstr')
    if not pdbs:
        raise ValueError('pdb argument must be provided')

    if ((isfile(selstr) or len(selstr) == 4 and selstr[0].isdigit()) and 
        len(pdbs) == 1 and not isfile(pdbs[0])):
        pdbs, selstr = selstr, pdbs[0]
        LOGGER.warn('The order of selstr and pdb arguments have switched '
                    'to support multiple files, old order will be supported '
                    'until v1.4.')
        pdbs = [pdbs]

    prefix = kwargs.get('prefix', None)
    suffix = kwargs.get('suffix', '_selected')
    output = kwargs.get('output', None)
    
    for pdb in pdbs:    
        pdb = parsePDB(pdb)
            
        pdbselect = pdb.select(selstr)
        if pdbselect is None:
            LOGGER.warn('Selection {0:s} did not match any atoms.'
                        .format(repr(selstr)))
            return
        LOGGER.info('Selection {0:s} matched {1:d} atoms.'
                    .format(repr(selstr), len(pdbselect)))

        outname = output or ((prefix or pdb.getTitle()) + suffix)
        LOGGER.info('Selection is written into: ' + 
                    writePDB(outname, pdbselect))
import prody
import numpy
import sys
import os.path

pdb_data = prody.parsePDB(sys.argv[1])
pdb_name = os.path.basename(sys.argv[1])
pdb_trajectory = prody.PDBEnsemble("aligned_CA")

prot = pdb_data.select("name CA not resname CA")
pdb_trajectory.setCoords(pdb_data.getCoordsets()[0])
pdb_trajectory.addCoordset(pdb_data.getCoordsets())
pdb_trajectory.setAtoms(prot)
pdb_trajectory.superpose()

prody.writePDB(pdb_name+".aligned_CA.pdb", pdb_trajectory)
with file(pdb_name+"aligned_CA.coords", 'w') as outfile:
    outfile.write("%d %d %d\n"%pdb_trajectory.getCoordsets().shape)
    for coordset in pdb_trajectory.getCoordsets():
        numpy.savetxt(outfile, coordset)

lig = pdb_data.select("resname BEN not name H1 H2 H3 H4 H5 H6 H7 HN1 HN2")
pdb_trajectory.setAtoms(lig)
rmsds =  pdb_trajectory.getRMSDs()

prody.writePDB(pdb_name+".aligned_BEN.pdb", pdb_trajectory)
with file(pdb_name+".aligned_BEN.coords", 'w') as outfile:
    outfile.write("%d %d %d\n"%pdb_trajectory.getCoordsets().shape)
    for coordset in pdb_trajectory.getCoordsets():
        numpy.savetxt(outfile, coordset)
Example #56
0
def corepagecalculation(pdbfilename, selatom, noma1, nummodes, gamcut, cut1, gam2, cut2, showresults, smodes, snmd, smodel, scollec, massnomass, sample1, modeens, confens, rmsdens, traverse1, modetra, steptra, rmsdtra, modelnumber, caanm, cagnm, nohanm, nohgnm, allanm, allgnm, bbanm, bbgnm, scanm, scgnm, nmdfolder, modesfolder, collectivityfolder, modelnewname, nmdnewname, modesnewname, modesendname, collectivitynewname, collectivityendname, samplenewname, traversenewname, crosscorr=0, corrfolder='', corrname='', corrend='', compmode01='7', compmode02='15', sqflucts=0, sqfluctsfolder='', sqfluctsname='', sqfluctsend='', separatevar1='0', temfac=0, temfacfolder='', temfacname='', temfacend='', fracovar=0, fraconame='', fracoend='', ovlap=0, ovlapfold='', ovlapname='', ovlapend='', ovlaptab=0, ovlaptabname='', ovlaptabend='', comppdbfilename=''):
# modelnumber
	import prody
	import time
	import os
	import Tkinter
	root=Tkinter.Tk()
	root.title('Info')
	onlypage=Tkinter.Frame(root)
	onlypage.pack(side='top')
	Tkinter.Label(onlypage,text='File: '+pdbfilename).grid(row=0,column=0,sticky='w')
	Tkinter.Label(onlypage,text='Atoms: '+selatom).grid(row=1,column=0,sticky='w')
	Tkinter.Label(onlypage,text='Analysis: '+noma1).grid(row=2,column=0,sticky='w')
	path=os.path.join(os.path.expanduser('~'),'.noma/')
	fin = open(path+'savefile.txt','r')
	global savedfile
	savedfile=fin.readlines()
	fin.close()
	i=0
	a=len(savedfile)
	while i<a:
		savedfile[i]=savedfile[i][:-1]
		i+=1
	if gamcut=='0':
		Tkinter.Label(onlypage,text='Gamma: r^'+savedfile[91]).grid(row=3,column=0,sticky='w')
		Tkinter.Label(onlypage,text='Cutoff: '+cut1).grid(row=4,column=0,sticky='w')
	elif gamcut=='1':
		Tkinter.Label(onlypage,text='Gamma: '+gam2).grid(row=3,column=0,sticky='w')
		Tkinter.Label(onlypage,text='Cutoff: '+cut2).grid(row=4,column=0,sticky='w')



	find = 0					#
	while find < len(pdbfilename):			#
		if pdbfilename[-(find+1):-find] == '/':	#
			bgn = len(pdbfilename)-find		#
			break				#
		else:					# helps in the
			find +=1			# saving of files
	try:						#
		float(bgn)				#
	except (NameError):				#
		bgn = 0					#
	find = 0					#
	while bgn+find<len(pdbfilename):			#
		if pdbfilename[bgn+find:bgn+find+1] == '.':	#
			end = len(pdbfilename)-(bgn+find)	#
			break				#
		else:					#
			find +=1			#
	try:						#
		name = pdbfilename[bgn:-end]			#
	except (NameError):				#
		name = pdbfilename[bgn:len(pdbfilename)]		# name of the file
	bgn = pdbfilename[:bgn]				# path for file
	mytimeis = time.asctime(time.localtime(time.time()))
	start = time.time()
	try:
		p38 = prody.parsePDB(pdbfilename,model=int(modelnumber))
	except:
		import tkMessageBox
		tkMessageBox.askokcancel("File Error","""This is not the correct path or name. Try entering /some/path/nameoffile.pdb
If you need help finding the path, open a new terminal and enter:
find -name 'filename.pdb'        use the output as the pdb input
If this doesn't work, make sure the file is in PDB format.""")
		p38 = prody.parsePDB(pdbfilename)
	print 'Submitted: '+pdbfilename+' at '+mytimeis
	Tkinter.Label(onlypage,text='Submitted at: '+mytimeis).grid(row=5,column=0,sticky='w')
	root.update()
	if selatom == "C-alpha" and noma1 == "Gaussian Normal Mode":
		folder = cagnm+'/'
		pro = p38.select('protein and name CA')	# selects only carbon alpahs
	elif selatom == "C-alpha" and noma1 == "Anisotropic Normal Mode":
		folder = caanm+'/'
		pro = p38.select('protein and name CA')
	elif selatom == "Heavy" and noma1 == "Gaussian Normal Mode":
		folder = nohgnm+'/'
		pro = p38.select('protein and not name "[1-9]?H.*"') # gets rid of all Hydrogens
	elif selatom == "Heavy" and noma1 == "Anisotropic Normal Mode":
		folder = nohanm+'/'
		pro = p38.select('protein and not name "[1-9]?H.*"')
	elif selatom == "All" and noma1 == "Gaussian Normal Mode":
		folder = allgnm+'/'
		pro = p38.select('protein')
	elif selatom == "All" and noma1 == "Anisotropic Normal Mode":
		folder = allanm+'/'
		pro = p38.select('protein')
	elif selatom == "Backbone" and noma1 == "Gaussian Normal Mode":
		folder = bbgnm+'/'
		pro = p38.select('protein and name CA C O N H')	# selects backbone
	elif selatom == "Backbone" and noma1 == "Anisotropic Normal Mode":
		folder = bbanm+'/'
		pro = p38.select('protein and name CA C O N H')	# selects backbone
	elif selatom == "Sidechain" and noma1 == "Gaussian Normal Mode":
		folder = scgnm+'/'
		pro = p38.select('protein and not name CA C O N H')	# selects sidechain
	elif selatom == "Sidechain" and noma1 == "Anisotropic Normal Mode":
		folder = scanm+'/'
		pro = p38.select('protein and not name CA C O N H')	# selects sidechain
	try:							#
		open(bgn+folder)				# creates the folders
	except (IOError):					# where the files will
		try:						# be saved only if they
			os.makedirs(bgn+folder)			# are not there
		except (OSError):				#
			mer = 0					#
	if noma1 == "Gaussian Normal Mode":
		print 'Building the Kirchhoff matrix'
		Tkinter.Label(onlypage,text='Building Kirchhoff').grid(row=6,column=0,sticky='w')
		root.update()
		anm = prody.GNM(name)###
		if gamcut=='0':
			anm.buildKirchhoff(pro,cutoff=float(cut1),gamma=gammaDistanceDependent)###
			anm.setKirchhoff(anm.getKirchhoff())
		elif gamcut=='1':
			anm.buildKirchhoff(pro,cutoff=float(cut2),gamma=float(gam2))###
		brat = 2
	elif noma1 == "Anisotropic Normal Mode":
		print 'Building the Hessian matrix'
		Tkinter.Label(onlypage,text='Building Hessian').grid(row=6,column=0,sticky='w')
		root.update()
		anm = prody.ANM(name)###
		if gamcut=='0':
			anm.buildHessian(pro,cutoff=float(cut1),gamma=gammaDistanceDependent)###
			anm.setHessian(anm.getHessian())###
		elif gamcut=='1':
			anm.buildHessian(pro,cutoff=float(cut2),gamma=float(gam2))###
		brat = 7
	print 'Calculating modes'
	Tkinter.Label(onlypage,text='Calculating modes').grid(row=7,column=0,sticky='w')
	root.update()
	anm.calcModes(int(nummodes),zeros = True)###
	numatom=anm.numAtoms()###
	eigval=anm.getEigvals()###
	atomname=pro.getNames()###
	if smodel==1:
		if brat==2:
			modelfilename=bgn+folder+name+modelnewname+'.gnm.npz'
		elif brat==7:
			modelfilename=bgn+folder+name+modelnewname+'.anm.npz'
		print 'Saving Model'
		Tkinter.Label(onlypage,text='Saving Model').grid(row=8,column=0,sticky='w')
		root.update()
		try:
			prody.saveModel(anm,bgn+folder+name+modelnewname,True)###
		except:
			print 'Matrix not saved due to size'
			Tkinter.Label(onlypage,text='Matrix not saved').grid(row=8,column=0,sticky='w')
			root.update()
			prody.saveModel(anm,bgn+folder+name+modelnewname)###
	if snmd==1:
		print 'Saving NMD'
		Tkinter.Label(onlypage,text='Saving NMD').grid(row=9,column=0,sticky='w')
		root.update()
		try:						#
			os.makedirs(bgn+folder+nmdfolder+'/')		#
		except (OSError):				#
			mer = 0					#
		prody.writeNMD(bgn+folder+nmdfolder+'/'+name+nmdnewname+'.nmd',anm[:len(eigval)],pro)###	# this can be viewed in VMD
	if smodes==1:
		print 'Saving Modes'
		Tkinter.Label(onlypage,text='Saving Modes').grid(row=10,column=0,sticky='w')
		root.update()
		try:						#
			os.makedirs(bgn+folder+modesfolder+'/')	#
		except (OSError):				#
			mer = 0					#
		modefile = bgn+folder+modesfolder+'/'+name+modesnewname+'.'+modesendname
		fout = open(modefile,'w')
		mer = 0
		while mer< len(eigval):
			slowest_mode = anm[mer]###
			r = slowest_mode.getEigvec()###
			p = slowest_mode.getEigval()###
			tq = 0
			tt = 0
			ttt = 1
			tttt = 2
			fout.write('MODE {0:3d}		{1:15e}'.format(mer+1,p))
			fout.write("""
-------------------------------------------------
""")
			if noma1 == "Gaussian Normal Mode":
				while tq < numatom:
					fout.write("""{0:4s}{1:15e}
""".format(atomname[tq],r[tq]))
					tq +=1
			elif noma1 == "Anisotropic Normal Mode":
				while tt < numatom*3:
					fout.write("""{0:4s}{1:15e}{2:15e}{3:15e}
""".format(atomname[tq],r[tt],r[ttt],r[tttt]))
					tq+=1
					tt +=3
					ttt+=3
					tttt+=3
			mer +=1
		fout.close()
		if showresults=='1':
			os.system('/usr/bin/gnome-open '+modefile)
	if scollec==1:
		print 'Saving collectivity'
		Tkinter.Label(onlypage,text='Saving collectivity').grid(row=11,column=0,sticky='w')
		root.update()
		try:						#
			os.makedirs(bgn+folder+collectivityfolder+'/')	#
		except (OSError):				#
			mer = 0					#
		mer = 0
		xx = [0]*(numatom) # sets the array to zero and other initial conditions
		i = 0
		aa = 0
		no = 0
		var3 = 0
		sss = [0]*(len(eigval))
		while mer< len(eigval):
			slowest_mode = anm[mer]###
			r = slowest_mode.getEigvec()###
			p = slowest_mode.getEigval()###
			a = 0
			tt = 0
			ttt = 1
			tttt = 2
			while a < numatom:
				atom = atomname[a]
				mass = 0
				while mass < 2:
					if atom[mass] == "N": # all nitrogen
						m = 14.0067
						break
					elif atom[mass] == 'H': # all hydrogen
						m = 1.00794
						break
					elif atom[mass] == "C" : # all carbon
						m = 12.0107
						break
					elif atom[mass] == "O" : # all oxygen
						m = 15.9994
						break
					elif atom[mass] == 'S': # all sulfur
						m = 32.065
						break
					elif atom[mass] == 'P' : # all phosphorus
						m = 30.973762
						break
					else:
						if mass == 0:
							mass +=1
							try:
								atom[mass]
							except (IndexError):
								m = 1
								if no == 0:
									print 'Enter atom '+atom+' in to the system. Its mass was set to 1 in this simulation.'
									no +=1
								break
						else:
							m = 1
							if no == 0:
								print 'Enter atom '+atom+' in to the system. Its mass was set to 1 in this simulation'
								no +=1
							break
				if len(r)/numatom == 3:
					xx[i] = (r[tt]**2 + r[ttt]**2 + r[tttt]**2)/m
					i +=1
					tt +=3
					ttt+=3
					tttt+=3
				else:
					xx[i] = (r[tt]**2)/m
					i +=1
					tt +=1
				a +=1
			var3 = 0
			j = 0
			loop = 1
			while loop == 1:
				if sum(xx) == 0: # need this because you can't divide by 0
					loop = 0
				elif j <(numatom):
					var1 = xx[j]/sum(xx)
					if var1 == 0:
						var2 = 0
					elif var1 != 0:
						from math import log # this means natural log
						var2 = var1* log(var1)
					var3 += var2
					j +=1
				else:
					from math import exp
					k = exp(-var3)/numatom
					sss[aa] = k, aa+1
					aa +=1
					mer +=1
					loop = 0
					i = 0
					xx = [0]*(numatom)  # goes through all this until the big loop is done
		a = 0
		k=[0]*(len(eigval))
		while a < len(eigval):
			k[a]=prody.calcCollectivity(anm[a]),a+1
			a +=1


		collectivefile = bgn+folder+collectivityfolder+'/'+name+collectivitynewname+'.'+collectivityendname
		fout = open(collectivefile,'w')
		if massnomass=='0':
			fout.write('MODE      COLLECTIVITY(mass)')
			fout.write("""
---------------------------
""")
			for h in sorted(sss,reverse=True):
				fout.write(str(h)[-3:-1]+'        '+str(h)[1:19]+"""
""")
			fout.write("""

MODE      COLLECTIVITY(without mass)""")
			fout.write("""
---------------------------
""")
			for hh in sorted(k,reverse=True):
				fout.write(str(hh)[-3:-1]+'        '+str(hh)[1:19]+"""
""")
		elif massnomass=='1':
			fout.write('MODE      COLLECTIVITY(without mass)')
			fout.write("""
---------------------------
""")
			for hh in sorted(k,reverse=True):
				fout.write(str(hh)[-3:-1]+'        '+str(hh)[1:19]+"""
""")
			fout.write("""

MODE      COLLECTIVITY(mass)""")
			fout.write("""
---------------------------
""")
			for h in sorted(sss,reverse=True):
				fout.write(str(h)[-3:-1]+'        '+str(h)[1:19]+"""
""")
		fout.close()
		if showresults=='1':
			os.system('/usr/bin/gnome-open '+collectivefile)

		fin = open(collectivefile,'r')
		lst = fin.readlines()
		hi0 = 2
		looop = 1
		prut=0
		secoll=0
		thicoll=0
		while looop == 1:
			fine = lst[hi0]
			if int(fine[0:2]) >= brat:
				if prut==0:
					prut=fine[0:2]
				elif secoll==0:
					secoll=fine[0:2]
				elif thicoll==0:
					thicoll=fine[0:2]
				else:
					foucoll=fine[0:2]
					looop = 0
			else:
				hi0 +=1
		mostcollective= "Mode "+prut+" is the most collective."
		Tkinter.Label(onlypage,text='Mode '+prut+' is the most collective').grid(row=12,column=0,sticky='w')
		root.update()
		print mostcollective
		fin.close()

	if sample1 == 1:
		print 'Saving sample file'
		Tkinter.Label(onlypage,text='Saving sample file').grid(row=13,column=0,sticky='w')
		root.update()
		a = modeens+' '
		b = [0]*(len(a)+1)
		i = 0
		j = 0
		b1 = 0
		while i < len(a):
			if a[i:i+1] ==' ' or a[i:i+1]==',':
				try:
					b[b1]=int(a[j:i])-1
				except:
					if '1c' in a[j:i]:
						b[b1]=int(prut)-1
					elif '2c' in a[j:i]:
						b[b1]=int(prut)-1
						b1 +=1
						b[b1]=int(secoll)-1
					elif '3c' in a[j:i]:
						b[b1]=int(prut)-1
						b1 +=1
						b[b1]=int(secoll)-1
						b1 +=1
						b[b1]=int(thicoll)-1
					elif '4c' in a[j:i]:
						b[b1]=int(prut)-1
						b1 +=1
						b[b1]=int(secoll)-1
						b1 +=1
						b[b1]=int(thicoll)-1
						b1+=1
						b[b1]=int(foucoll)-1
				j = i+1
				i +=1
				b1 +=1
			else:
				i +=1
		del b[b1:]
		ensemble = prody.sampleModes(anm[b],pro, n_confs=int(confens), rmsd =float(rmsdens))
		p38ens=pro.copy()
		p38ens.delCoordset(0)
		p38ens.addCoordset(ensemble.getCoordsets())
		prody.writePDB(bgn+folder+name+samplenewname+'.pdb',p38ens)


	if traverse1 ==1:
		print 'Saving traverse file'
		Tkinter.Label(onlypage,text='Saving traverse file').grid(row=14,column=0,sticky='w')
		root.update()
		if modetra=='c':
			modefortra=int(prut)-1
		else:
			modefortra=int(modetra)-1
		trajectory=prody.traverseMode(anm[modefortra],pro,n_steps=int(steptra),rmsd=float(rmsdtra))
		prody.calcRMSD(trajectory).round(2)
		p38traj=pro.copy()
		p38traj.delCoordset(0)
		p38traj.addCoordset(trajectory.getCoordsets())
		prody.writePDB(bgn+folder+name+'_mode'+str(modefortra+1)+traversenewname+'.pdb',p38traj)
	if crosscorr==1:
		print 'Saving cross correlation'
		Tkinter.Label(onlypage,text='Saving cross-correlation').grid(row=15,column=0,sticky='w')
		root.update()
		try:						#
			os.makedirs(bgn+folder+corrfolder+'/')	#
		except (OSError):				#
			mer = 0
		i=int(compmode01)
		while i <= int(compmode02):
			x=i-1
			correlationdataname=bgn+folder+corrfolder+'/'+name+corrname+'_mode'+str(x+1)+'.'+corrend
			prody.writeArray(correlationdataname,prody.calcCrossCorr(anm[x]),'%.18e')
			print correlationdataname
			i+=1

##
	if sqflucts==1:
		print 'Saving square fluctuation'
		Tkinter.Label(onlypage,text='Saving square fluctuation').grid(row=16,column=0,sticky='w')
		root.update()
		try:						#
			os.makedirs(bgn+folder+sqfluctsfolder+'/')	#
		except (OSError):				#
			mer = 0
		i=int(compmode01)
		while i < int(compmode02):
			yelp = i-1
			sqfluctdataname = bgn+folder+sqfluctsfolder+'/'+name+sqfluctsname+'_mode'+str(yelp+1)+'.'+sqfluctsend
			fout = open(sqfluctdataname,'w')
			if separatevar1=='0':
				a = 0
				while a < numatom:
					fout.write(str(a))
					fout.write("""	""")
					fout.write(str(prody.calcSqFlucts(anm[yelp])[a]))
					fout.write("""
""")
					a +=1
			elif separatevar1=='1':
				a=0
				while a <numatom:
					firstresnum=int(p38.getResnums()[0:1][0])
					origiresnum=int(p38.getResnums()[0:1][0])
					while firstresnum<(int(numatom*1.0/p38.numChains())+origiresnum):
						fout.write(str(firstresnum))
						fout.write('\t')
						fout.write(str(prody.calcSqFlucts(anm[yelp])[a]))
						fout.write('\n')
						a+=1
						firstresnum+=1
					fout.write('&\n')
			fout.close()
			print sqfluctdataname
			i+=1
	if temfac==1:
		print 'Saving temperature factors'
		Tkinter.Label(onlypage,text='Saving temperature factors').grid(row=17,column=0,sticky='w')
		root.update()
		try:						#
			os.makedirs(bgn+folder+temfacfolder+'/')	#
		except (OSError):				#
			mer = 0

		fin=open(pdbfilename,'r')
		d = [None]*len(atomname)
		e = 0
		for line in fin:
			pair = line.split()
			if 'ATOM  ' in line and e < len(atomname):
				if str(pair[2]) == str(atomname[e]):
					d[e]=str(pair[1])
					e+=1
				else:
					e+=0
			else:
				continue
		fin.close()
		sqf = prody.calcSqFlucts(anm)
		x = sqf/((sqf**2).sum()**.5)
		y = prody.calcTempFactors(anm,pro)
		a = 0
		tempfactorsdataname =bgn+folder+temfacfolder+'/'+name+temfacname+'.'+temfacend
		fout=open(tempfactorsdataname,'w')
		fout.write("""Atom	Residue	      TempFactor   TempFactor with exp beta
""")
		while a < numatom:
			fout.write("""{0:4s}	{1:4d}	{2:15f}	{3:15f}
""".format(d[a],a+1,x[a],y[a]))
			a +=1
		fout.close()
		print tempfactorsdataname
	if fracovar==1:
		try:
			import matplotlib.pyplot as plt
			print 'Saving Fraction of Variance'
			Tkinter.Label(onlypage,text='Saving Fraction of Variance').grid(row=18,column=0,sticky='w')
			root.update()
			try:						#
				os.makedirs(bgn+folder+modesfolder+'/')	#
			except (OSError):				#
				mer = 0					#
			plt.figure(figsize = (5,4))
			prody.showFractVars(anm)
			prody.showCumulFractVars(anm)
			fracvardataname =bgn+folder+modesfolder+'/'+name+fraconame+'.'+fracoend
			plt.savefig(fracvardataname)
			print fracvardataname
			if showresults=='1':
				os.system('/usr/bin/gnome-open '+fracvardataname)
		except:
			print 'Error: Fraction of Variance'
			Tkinter.Label(onlypage,text='Error: Fraction of Variance').grid(row=18,column=0,sticky='w')
			root.update()
			mer=0

	if ovlap==1 or ovlaptab==1:
		try:
			import matplotlib.pyplot as plt
			print 'Saving Overlap'
			Tkinter.Label(onlypage,text='Saving Overlap').grid(row=19,column=0,sticky='w')
			root.update()


			Tkinter.Label(onlypage,text='Comparison: '+comppdbfilename).grid(row=20,column=0,sticky='w')


##
			find = 0
			while find < len(comppdbfilename):
				if comppdbfilename[-(find+1):-find] == '/':
					bgn1 = len(comppdbfilename)-find
					break
				else:
					find +=1
			try:
				float(bgn1)
			except (NameError):
				bgn1 = 0
			find = 0
			while bgn1+find<len(comppdbfilename):
				if comppdbfilename[bgn1+find:bgn1+find+1] == '.':
					end1 = len(comppdbfilename)-(bgn1+find)
					break
				else:
					find +=1
			try:
				name1 = comppdbfilename[bgn1:-end1]
			except (NameError):
				name1 = comppdbfilename[bgn1:len(comppdbfilename)]
			bgn1 = comppdbfilename[:bgn1]
			p381 = prody.parsePDB(comppdbfilename,model=int(modelnumber))
			if selatom == "C-alpha" and noma1 == "Gaussian Normal Mode":
				pro1 = p381.select('protein and name CA')
			elif selatom == "C-alpha" and noma1 == "Anisotropic Normal Mode":
				pro1 = p381.select('protein and name CA')
			elif selatom == "Heavy" and noma1 == "Gaussian Normal Mode":
				pro1 = p381.select('protein and not name "[1-9]?H.*"')
			elif selatom == "Heavy" and noma1 == "Anisotropic Normal Mode":
				pro1 = p381.select('protein and not name "[1-9]?H.*"')
			elif selatom == "All" and noma1 == "Gaussian Normal Mode":
				pro1 = p381.select('protein')
			elif selatom == "All" and noma1 == "Anisotropic Normal Mode":
				pro1 = p381.select('protein')
			elif selatom == "Backbone" and noma1 == "Gaussian Normal Mode":
				pro1 = p381.select('protein and name CA C O N H')
			elif selatom == "Backbone" and noma1 == "Anisotropic Normal Mode":
				pro1 = p381.select('protein and name CA C O N H')
			elif selatom == "Sidechain" and noma1 == "Gaussian Normal Mode":
				pro1 = p381.select('protein and not name CA C O N H')
			elif selatom == "Sidechain" and noma1 == "Anisotropic Normal Mode":
				pro1 = p381.select('protein and not name CA C O N H')
			if noma1 == "Gaussian Normal Mode":
				print 'Building the Kirchhoff matrix'
				Tkinter.Label(onlypage,text='Building Kirchhoff').grid(row=21,column=0,sticky='w')
				root.update()
				anm1 = prody.GNM(name1)
				if gamcut=='0':
					anm1.buildKirchhoff(pro1,cutoff=float(cut1),gamma=gammaDistanceDependent)
					anm1.setKirchhoff(anm1.getKirchhoff())
				elif gamcut=='1':
					anm1.buildKirchhoff(pro1,cutoff=float(cut2),gamma=float(gam2))
				brat = 2
			elif noma1 == "Anisotropic Normal Mode":
				print 'Building the Hessian matrix'
				Tkinter.Label(onlypage,text='Building Hessian').grid(row=21,column=0,sticky='w')
				root.update()
				anm1 = prody.ANM(name1)
				if gamcut=='0':
					anm1.buildHessian(pro1,cutoff=float(cut1),gamma=gammaDistanceDependent)
					anm1.setHessian(anm1.getHessian())
				elif gamcut=='1':
					anm1.buildHessian(pro1,cutoff=float(cut2),gamma=float(gam2))
				brat = 7
			print 'Calculating modes'
			Tkinter.Label(onlypage,text='Calculating modes').grid(row=22,column=0,sticky='w')
			root.update()
			anm1.calcModes(int(nummodes),zeros = True)
##
			try:
				os.makedirs(bgn+folder+ovlapfold+'/')
			except (OSError):
				mer = 0
			if ovlap==1:
				i=int(compmode01)
				while i < int(compmode02):
					a = i-1
					plt.figure(figsize=(5,4))
					prody.showCumulOverlap(anm[a],anm1)
					prody.showOverlap(anm[a],anm1)
					plt.title('Overlap with Mode '+str(a+1)+' from '+name)
					plt.xlabel(name1+' mode index')
					overlapname = bgn+folder+ovlapfold+'/'+name+'_'+name1+ovlapname+'_mode'+str(a+1)+'.'+ovlapend
					plt.savefig(overlapname)
					print overlapname
					i+=1
			if ovlaptab==1:
				plt.figure(figsize=(5,4))
				prody.showOverlapTable(anm1,anm)
				plt.xlim(int(compmode01)-1,int(compmode02))
				plt.ylim(int(compmode01)-1,int(compmode02))
				plt.title(name1+' vs '+name+' Overlap')
				plt.ylabel(name1)
				plt.xlabel(name)
				overlapname = bgn+folder+ovlapfold+'/'+name+'_'+name1+ovlaptabname+'.'+ovlaptabend
				plt.savefig(overlapname)
				print overlapname
		except:
			mer=0


	root.destroy()
	mynewtimeis = float(time.time()-start)
	if mynewtimeis <= 60.00:
		timeittook= "The calculations took %.2f s."%(mynewtimeis)
	elif mynewtimeis > 60.00 and mynewtimeis <= 3600.00:
		timeittook= "The calculations took %.2f min."%((mynewtimeis/60.00))
	else:
		timeittook= "The calculations took %.2f hrs."%((mynewtimeis/3600.00))
	print timeittook
	if smodel==1 and scollec==1:
		return (timeittook,modelfilename,str(int(prut)))
	elif scollec==1:
		return (timeittook,'nofile',str(int(prut)))
	elif smodel==1:
		return (timeittook,modelfilename,'nocoll')
	else:
		return (timeittook,'nofile','nocoll')
import prody
import numpy

pdb_data = prody.parsePDB("../Models/prot_plus_ligand_very_different/not_aligned_offset_prot_plus_ligand.pdb")
pdb_trajectory = prody.PDBEnsemble("aligned_CA")

prot = pdb_data.select("name CA not resname CA")
pdb_trajectory.setCoords(pdb_data.getCoordsets()[0])
pdb_trajectory.addCoordset(pdb_data.getCoordsets())
pdb_trajectory.setAtoms(prot)
pdb_trajectory.superpose()

prody.writePDB("prot_plus_ligand_similar.aligned_CA.pdb", pdb_trajectory)
with file("prot_plus_ligand_similar.aligned_CA.coords", 'w') as outfile:
    outfile.write("%d %d %d\n"%pdb_trajectory.getCoordsets().shape)
    for coordset in pdb_trajectory.getCoordsets():
        numpy.savetxt(outfile, coordset)

lig = pdb_data.select("resname BEN not element H")
pdb_trajectory.setAtoms(lig)
rmsds =  pdb_trajectory.getRMSDs()

prody.writePDB("prot_plus_ligand_similar.aligned_BEN.pdb", pdb_trajectory)
with file("prot_plus_ligand_similar.aligned_BEN.coords", 'w') as outfile:
    outfile.write("%d %d %d\n"%pdb_trajectory.getCoordsets().shape)
    for coordset in pdb_trajectory.getCoordsets():
        numpy.savetxt(outfile, coordset)

print rmsds

numpy.savetxt("prot_plus_ligand_similar.aligned_BEN.rmsd", rmsds)
        pdb, pdb_path = tools.get_pdb_from_remote_or_db(alignment["pdb"]["id"], 
                                      parameters["pdb_preparation"]["load_selection"],
                                      structure_db_path)
        
        main_chains = choose_main_chains(pdb)
        curated_pdb, ligand = curate_struct(pdb, 
                                            main_chains, 
                                            alignment, 
                                            parameters["pdb_preparation"])

        failed_filters = structure_filter.must_be_filtered(curated_pdb)
        if len(failed_filters) == 0 or alignment["pdb"]["id"] in id_exceptions:
            alignment["rejected"] = False
            tmp_path = os.path.join(parameters["global"]["curated_structure_database"], 
                                    os.path.basename(pdb_path))
            prody.writePDB(tmp_path, curated_pdb)
        else:
            alignment["rejected"] = True
            log.write("\t - PDB %s is not going to be stored because it has failed this filters: %s.\n"%(alignment["pdb"]["id"],
                                                                                str(failed_filters)))
        
        if structure_db_path is not None: # We do not want to delete our database!
            os.remove(pdb_path)
    
    # Blast DB creation
    BlastpCommands.create_database_from_alignments(filtered_alignments,
                                                  parameters["blast_database_creation"])
    tools.save_json(filtered_alignments, 
              parameters["blastp"]["alignments_file"])
    
    log.write(datetime.now().strftime("Finished on %A, %d. %B %Y %I:%M%p\n"))
Example #59
0
import prody
import numpy

pdb_data = prody.parsePDB("../Models/prot_stretching/stretching_trajectory_offset_ligand.pdb")
pdb_trajectory = prody.PDBEnsemble("iterposed_CA")

# Write the initial coordsets
prot = pdb_data.select("name CA")
prody.writePDB("stretching_trajectory_offset_ligand.iterposed_all.pdb", prot)
with file("stretching_trajectory_offset_ligand.initial_CA.coords", 'w') as outfile:
    outfile.write("%d %d %d\n"%prot.getCoordsets().shape)
    for coordset in prot.getCoordsets():
        numpy.savetxt(outfile, coordset)

# We only want to work with CAs. If we use the 'all coordinates+atom selection" trick
# Prody will still use all coordinates for iterative superposition
pdb_trajectory.setCoords(prot.getCoordsets()[0])
pdb_trajectory.addCoordset(prot.getCoordsets())
pdb_trajectory.setAtoms(prot)
pdb_trajectory.iterpose()

prody.writePDB("stretching_trajectory_offset_ligand.iterposed_CA.pdb", pdb_trajectory)
with file("stretching_trajectory_offset_ligand.iterposed_CA.coords", 'w') as outfile:
    outfile.write("%d %d %d\n"%pdb_trajectory.getCoordsets().shape)
    for coordset in pdb_trajectory.getCoordsets():
        numpy.savetxt(outfile, coordset)

import sys
import prody 
import numpy
import os.path

pdb_data = prody.parsePDB(sys.argv[1])
pdb_trajectory = prody.PDBEnsemble("aligned_CA")
pdb_name = os.path.splitext(sys.argv[1])[0]

prot = pdb_data.select("name CA not resname CA")
pdb_trajectory.setAtoms(prot)
pdb_trajectory.addCoordset(prot.getCoordsets())
pdb_trajectory.setCoords(prot.getCoordsets()[0])
prody.writePDB(pdb_name+"_CA.pdb", pdb_trajectory)
with file(pdb_name+".CA.coords", 'w') as outfile:
    outfile.write("%d %d %d\n"%pdb_trajectory.getCoordsets().shape)
    for coordset in pdb_trajectory.getCoordsets():
        numpy.savetxt(outfile, coordset)
        
pdb_trajectory = prody.PDBEnsemble("")
lig = pdb_data.select("resname BEN not name H1 H2 H3 H4 H5 H6 H7 HN1 HN2 ")
pdb_trajectory.setAtoms(lig)
pdb_trajectory.addCoordset(lig.getCoordsets())
pdb_trajectory.setCoords(lig.getCoordsets()[0])
prody.writePDB(pdb_name+"_ligand.pdb", pdb_trajectory)
with file(pdb_name+".ligand.coords", 'w') as outfile:
    outfile.write("%d %d %d\n"%pdb_trajectory.getCoordsets().shape)
    for coordset in pdb_trajectory.getCoordsets():
        numpy.savetxt(outfile, coordset)