def __init__(self, model, chain_name): """ Note:- Chain can never be instantiated independently. It has to have a Model instance as the parent. """ self.chain_name = chain_name self.residues = OrderedDict() self.children = self.residues if isinstance(model, Model): self.parent = model
def __init__(self, pdbtree, model_num): """ Note:- Model can never be instantiated independently. It has to have a PDBTree instance as the parent. """ self.model_num = model_num self.chains = OrderedDict() if isinstance(pdbtree, PDBTree): self.parent = pdbtree self.children = self.chains
class Model(object): """ Description:- child of PDBTree and parent to Chain(s) """ def __init__(self, pdbtree, model_num): """ Note:- Model can never be instantiated independently. It has to have a PDBTree instance as the parent. """ self.model_num = model_num self.chains = OrderedDict() if isinstance(pdbtree, PDBTree): self.parent = pdbtree self.children = self.chains def add(self, chain_name): self.chains[chain_name] = Chain(self, chain_name) return self.chains[chain_name] def chain(self, chain_name): ''' Returns Chain object. --------------------------------------------------------- Args: `chain_name` : `str` (chain identifier) Usage: TODO ''' try: return self.chains[chain_name] except KeyError as e: return None def __getattr__(self, arg): models_now = self.chains.keys() if 'chain' in arg and len(arg) > 5: try: chain_name = arg[5:] return self.chain(chain_name) except TypeError: return None def __iter__(self): return self.chains.values().__iter__() def __repr__(self): return 'Model object: Model {}'.format(self.model_num) def __str__(self): return 'Model object: I have {} chains as children'.format( len(self.children))
def __init__(self, chain, res_num, res_name): """ Note:- Residue can never be instantiated independently. It has to have a Model instance as the parent. """ super(Residue, self).__init__() self.res_name = res_name self.res_num = res_num self.atoms = OrderedDict() self.children = self.atoms if isinstance(chain, Chain): self.parent = chain self.logger = self.parent.logger
class PDBTree(object): """ Description:- Data structure to store the hierarchy of a .pdb file """ def __init__(self): super(PDBTree, self).__init__() self.models = OrderedDict() self.children = self.models def add(self, model_num): self.models[model_num] = Model(self, model_num) def model(self, model_num): ''' Returns Model object. --------------------------------------------------------- Args: `model_num` : `int` (model number) Usage: Eg: pdb_tree.model(1) or pdb_tree.model1 ''' try: return self.models[model_num] except KeyError as e: try: return self.models[str(model_num)] except KeyError as e: return None def __getattr__(self, arg): models_now = self.models.keys() if 'model' in arg and len(arg) > 5: try: num = int(arg[5:]) return self.model(num) except TypeError as e: return None if 'parent' in arg: return self def __str__(self): return 'PDBTree object: I have {} different model(s) as children.'.format( len(self.models)) def __iter__(self): """ Utility:- Can iterate through self's children """ return self.children.values().__iter__()
def __init__(self): super(PDBTree, self).__init__() self.models = OrderedDict() self.children = self.models
class Residue(object): """ Description:- child of Chain and parent to Atom(s) """ def __init__(self, chain, res_num, res_name): """ Note:- Residue can never be instantiated independently. It has to have a Model instance as the parent. """ super(Residue, self).__init__() self.res_name = res_name self.res_num = res_num self.atoms = OrderedDict() self.children = self.atoms if isinstance(chain, Chain): self.parent = chain self.logger = self.parent.logger def add(self, serial, name): atom_name = name.strip() self.atoms[atom_name] = Atom(self, serial, name) return self.atoms[atom_name] def get_dihedral(self): try: prev_res = self.parent.residues[self.res_num - 1] except KeyError: prev_res = None try: next_res = self.parent.residues[self.res_num + 1] except KeyError: next_res = None N2vec = self.atoms['N'].position CA2vec = self.atoms['CA'].position C2vec = self.atoms['C'].position phi = 180 psi = 180 if prev_res: C1vec = prev_res.atoms['C'].position phi = C1vec.dihedral(N2vec, CA2vec, C2vec) if next_res: N3vec = next_res.atoms['N'].position psi = N2vec.dihedral(CA2vec, C2vec, N3vec) # print 'Phi:{}, Psi:{}'.format(phi,psi) return phi, psi def __iter__(self): return self.atoms.values().__iter__() def __str__(self): return 'Residue object: I have {} atoms as children'.format( len(self.children)) def __repr__(self): return 'Residue object: Residue {} {}'.format(self.res_num, self.res_name) def __getattr__(self, arg): atom_names = ['N', 'CA', 'C', 'O'] if arg in atom_names: return self.atoms[arg] elif arg == 'name': return self.res_name elif arg == 'serial': return self.res_num def translate(self, xt, yt, zt, atoms='ALL'): if atoms == 'ALL': atoms = self.atoms.keys() for a in atoms: atom = self.get_atom(str(a)) atom.translate(xt, yt, zt)
class Chain(object): """ Description:- child of Model and parent to Residue(s) """ def __init__(self, model, chain_name): """ Note:- Chain can never be instantiated independently. It has to have a Model instance as the parent. """ self.chain_name = chain_name self.residues = OrderedDict() self.children = self.residues if isinstance(model, Model): self.parent = model def add(self, res_num, res_name): self.residues[res_num] = Residue(self, res_num, res_name) return self.residues[res_num] def __iter__(self): return self.residues.values().__iter__() def residue(self, res_num): ''' Returns Chain object. --------------------------------------------------------- Args: `res_num` : `int` (residue number) Usage: TODO ''' try: return self.residues[res_num] except KeyError as e: return None def get_dihedrals(self): dihedrals = [] residues = self.residues.values() for res in residues: dihedrals.append([(res.res_num, res.res_name), res.get_dihedral()]) return dihedrals def __getattr__(self, arg): models_now = self.residues.keys() if 'residue' in arg and len(arg) > 7: try: res_num = int(arg[7:]) return self.residue(res_num) except TypeError: #TODO return None elif 'res' in arg and len(arg) > 3: try: res_num = int(arg[3:]) return self.residue(res_num) except TypeError: #TODO return None def __str__(self): t = self.children.values() return 'Chain object: I have {} residues as children'.format( len(self.children)) def __repr__(self): return 'Chain object: Chain {}'.format(self.chain_name) def translate(self, xt, yt, zt, residues='ALL'): if residues == 'ALL': residues = self.residues.keys() for r in residues: residue = self.residues[r] residue.translate(xt, yt, zt)