def rmsd(target, source, fragments, pdb, save=None): matches = [] try: src_file = wwpdb.find(source, [pdb]) if src_file is None: raise IOError("Can't find structure {0} in {1}".format( source, pdb)) src_structure = wwpdb.RegularStructureParser( src_file).parse_structure() except (IOError, structure.Broken3DStructureError, wwpdb.PDBParseError) as ex: matches.append('Error parsing {0:5}: {1!s}'.format(source, ex)) return matches for fn, fragment in enumerate(fragments): try: if fragment.chain not in ('_', '', None): src_chain = src_structure.chains[fragment.chain] else: src_chain = src_structure.first_chain try: query = target.subregion(fragment.qstart, fragment.qend) subject = src_chain.subregion(fragment.start, fragment.end, clone=True) except IndexError: matches.append( 'Fragment {1.source_id:>5} {0.start:>4}-{0.end:>4} is out of range' .format(fragment)) continue si = query.align(subject) match = csb.bio.fragments.FragmentMatch(fragment.id, qstart=fragment.qstart, qend=fragment.qend, probability=None, rmsd=si.rmsd, tm_score=None, qlength=target.length) matches.append(match) if save: dummy = structure.Structure(subject.entry_id) dummy.chains.append(subject) filename = '{0.qstart:}-{0.qend}-{0.source_id}.{1.entry_id}{2}.frag'.format( fragment, query, fn or '') dummy.to_pdb(os.path.join(save, filename)) except (structure.Broken3DStructureError, IOError) as ex: matches.append("Can't superimpose fragment {0}: {1!s}".format( fragment.id, ex)) continue return matches
def testAppend(self): testModel = structure.Structure('test') testModel.model_id = 3 rank = self.ensemble.models.append(testModel) self.assertEqual(rank, 3) self.assertEqual(self.ensemble.models[3], testModel) self.assertEqual(self.ensemble.models[3].model_id, 3) self.assertRaises(Exception, self.ensemble.models.append, 1)
def write_pdb(self, coords, filename): if coords.ndim == 2: coords = coords.reshape(1, -1, 3) ensemble = structure.Ensemble() for i, xyz in enumerate(coords, 1): chain = make_chain(xyz) struct = structure.Structure('') struct.chains.append(chain) struct.model_id = i ensemble.models.append(struct) ensemble.to_pdb(filename)
def format_structure(self, input_pdb, chain_id, output_file): """ Format input_pdb as an HHpred pre-parsed structure file for the current HMM. Formatting means: only chain_id left in the PDB file, residue sequence numbers strictly corresponding to the HMM states' ranks. @param input_pdb: source PDB file name @type input_pdb: str @param chain_id: source chain ID (which chain to pick from the structure) @type chain_id: str @param output_file: save the output PDB file here @type output_file: str @raise StructureFormatError: when the specified PDB chain does not correspond to the HMM. """ hmm = self.parse() s = csb.bio.io.StructureParser(input_pdb).parse_structure() chain = s.chains[chain_id] if chain.length != hmm.layers.length: raise StructureFormatError( "{0}: Incorrect number of residues".format(chain.entry_id)) for layer, residue in zip(hmm.layers, chain.residues): if residue.type == ProteinAlphabet.UNK: residue.type = layer.residue.type if residue.type != layer.residue.type: msg = "Expected residue of type {0} at position {1}" raise StructureFormatError( msg.format(layer.residue.type, layer.rank)) residue.id = str(residue.rank), '$' for residue in chain.residues: residue.id = str(residue.rank), None new_structure = structure.Structure(hmm.id) new_structure.chains.append(chain.clone()) new_structure.to_pdb(output_file)