def __init__(self, filename, **kwargs): super(CRDFile, self).__init__() # kwargs kwargs = lowerKeys(kwargs) inFormat = kwargs.get('informat', 'auto') self._autoFix = kwargs.get('autofix', False) self._verbose = kwargs.get('verbose', False) # self._models = {} self.warnings = [] # Filename self.filename = filename if self._verbose: print 'Parsing data from `%s`\n' % filename # Partitioning self._partitions() # self.header / self.crd / self.footer # Input Formatting if inFormat == 'auto': self._inFormat = self._get_formatting(self.filename) else: self._inFormat = inFormat if self._verbose: print '%s: Input formatting set to `%s`' % (self.filename, self.inFormat) # Processing -- this is simpler than the PDB case because we # do not have multiple models self._build_crd()
def iter_seg(self, **kwargs): """ A generator that returns one `Seg` per iteration. kwargs: `segtypes` The kwarg `segtypes` can be used to specify which segments are iterated over, and in which order. By default, all segments are iterated over in alpha order of `segtype`. >>> thisChain.iter_seg(segtypes=['pro','dna','rna','nuc','good','bad']) """ # kwargs kwargs = lowerKeys(kwargs) segTypes = kwargs.get("segtypes", None) # Default to 'all' segtypes if segTypes is None: segTypes = list(set((atom.segType for atom in self))) segTypes.sort() # Do Work for segType in segTypes: iterator = (atom for atom in self if atom.segType == segType) result = Seg(iterable=iterator, code=self.code, autoFix=False) if result: yield result
def get_molFromCRD(filename, **kwargs): """ A function that returns a single :class:`Mol` object from a single *.crd* text file. **kwargs:** | ``atomobj`` Specify the :class:`Atom`-like constructor to use defaults to :class:`Atom`. | ``informat`` Specify the plaintext formatting of the file, defaults to *'auto'*. """ # kwargs kwargs = lowerKeys(kwargs) AtomObj = kwargs.get("atomobj", Atom) inFormat = kwargs.get("informat", "auto") # if inFormat == "auto": inFormat = get_formatting(filename, **kwargs) if inFormat == "unknown": raise AssertionError("Unknown formatting type, quitting.\n") kwargs["informat"] = inFormat # iterator = (line.lower().rstrip() for line in open(filename)) tmp = [] for line in iterator: try: dummy = AtomObj(line, **kwargs) tmp.append(dummy) except: pass return Mol(tmp, **kwargs)
def __init__(self, text=None, **kwargs): super(MetaAtom, self).__init__() # kwargs kwargs = lowerKeys(kwargs) commentChar = kwargs.get('commentchar', '#') inFormat = kwargs.get('informat', self.__class__._autoInFormat) self._index = kwargs.get('index', MetaAtom._autoIndex) self._autoFix = kwargs.get('autofix', True) # Main if text is None: self._init_null() elif isinstance(text, str): # card format files are whitespace sensitive if inFormat in ['crd', 'cor', 'card', 'short', 'shortcard'] or \ inFormat in ['xcrd', 'xcor', 'xcard', 'long', 'longcard', 'mol2']: self._text = text.lower().rstrip() else: self._text = text.lower().split(commentChar)[0].strip() self.parse(inFormat) self._addr0 = self.addr elif isinstance(text, MetaAtom): selfProp = set(self.__class__._properties.iterkeys()) otherProp = set(text._properties.iterkeys()) for key in selfProp.intersection(otherProp): setattr(self, key, getattr(text, key)) for key in selfProp - otherProp: setattr(self, key, self.__class__._properties[key]) else: raise TypeError("""Invalid input, initialization requires `None`, `str` or `MetaAtom` type.""") # Finally self._set_hash() MetaAtom._autoIndex += 1
def __init__(self, pdbFilename=None, **kwargs): super(BaseAnalysis, self).__init__(pdbFilename, **kwargs) # kwargs kwargs = lowerKeys(kwargs) self.correlAtomSelection = kwargs.get('selection', 'all') # self._correlStart = 0 self._correlStop = -1
def __init__(self, pdbFilename=None, **kwargs): super(INPFile, self).__init__() # kwargs kwargs = lowerKeys(kwargs) self.charmmBin = kwargs.get('charmmbin', self.__class__.defaultBin) # Main if pdbFilename is not None: self.pdbFilename = pdbFilename # self._rtfFilename = None self._prmFilename = None self._psfFilename = None self._crdFilename = None
def gen_cgStruct(self, **kwargs): """ """ kwargs = lowerKeys(kwargs) calcmass = kwargs.get('calcmass', None) if calcmass is None: iterator = self.allHeavyAtoms.iter_res(restype=CGPro) else: iterator = self.allAtoms.iter_res(restype=CGPro) for res in iterator: yield res.get_goBB(**kwargs) try: yield res.get_goSC(**kwargs) except NoAlphaCarbonError: pass
def get_formatting(filename, **kwargs): """ Takes a string representing the path to a file containing molecular coordinate data, in either *.pdb* or *.crd* format, and attempts to return a string representing the exact formatting of that file. This is done by trying to apply an :class:`Atom`-like constructor to lines of text in a brute force manner. If the constructor is called without raising an error the formatting is accepted. Possible return values are: *'pdborg', 'charmm', 'crd', 'xcrd', 'unknown'*. **kwargs:** | ``atomobj`` Specify the :class:`Atom`-like constructor to use defaults to :class:`Atom`. """ # kwargs kwargs = lowerKeys(kwargs) AtomObj = kwargs.get("atomobj", Atom) # def crd_or_pdb(): formatDict = {"pdborg": "pdb", "charmm": "pdb", "crd": "crd", "xcrd": "xcrd"} for k, v in formatDict.items(): for line in open(filename): try: dummy = AtomObj(line, informat=k) return v except: pass return "unknown" tmp = crd_or_pdb() if tmp == "pdb": # differentiate between pdborg and charmm types iterator = (line.lower() for line in open(filename)) iterator = (line for line in iterator if line.startswith(("atom", "hetatm"))) for line in iterator: if line[21:22] == " " and line[72:73] in alphanum: return "charmm" elif ( line[21:22] in alphanum and line[12:14].strip() == line[66:].strip() ): # this might have a funny corner case with a 'he22' type atomType return "pdborg" return "unknown" else: return tmp
def __init__(self, filename=None, **kwargs): super(PDBFile, self).__init__() # kwargs kwargs = lowerKeys(kwargs) inFormat = kwargs.get("informat", "auto") fix_chainid = kwargs.get("fix_chainid", True) fix_resid = kwargs.get("fix_resid", True) self._autoFix = kwargs.get("autoFix", True) self._verbose = kwargs.get("verbose", False) # self.warnings = [] self._mols = {} if filename is not None: # Filename self.filename = filename if self._verbose: print "Parsing data from `%s`\n" % filename # Partitioning self._partitions() # self.header / self.crd / self.footer # Input Formatting if inFormat == "auto": self._inFormat = self._get_formatting(self.filename) else: self._inFormat = inFormat if self._verbose: print "%s: Input formatting set to `%s`" % (self.code, self.inFormat) # auto fixing if fix_chainid: if self._verbose: print "%s: Fixing `chainid`s" % self.code self._fix_chainids() # TODO Implement fix_resids if fix_resid: if self._verbose: print "%s: Fixing `resid`s" % self.code # self._fix_resids() # Processing self._build_models() else: self.filename = "null" self._header = "null" self._crd = "null" self._footer = "null"
def get_goSC(self, **kwargs): """ DOCME """ kwargs = lowerKeys(kwargs) calcmass = kwargs.get('calcmass', None) # tmp = self.get_alphaCarbon() tmp.cart = self.scCom if calcmass is None: tmp.mass = aaMass[self.resName] - aaMass['gly'] else: iterator = ( atom.mass for atom in self if not isBBAA(atom.atomType) ) tmp.mass = sum(iterator) tmp.atomNum = 1 tmp.atomType = 's' tmp.derivedResName = tmp.resName tmp.resName = '%s%d' % (self.chainid, self.resid) tmp.segType = 'ktgo' return tmp
def write_correlInput(self, **kwargs): """ This method writes a single CHARMM .inp file for the purpose of performing a backbone RMSD calculation. This RMSD is done between the original coarse grained .crd file and the reoriented trajectory 'reorient.dcd'. """ kwargs = lowerKeys(kwargs) header = kwargs.get('header', []) comments = kwargs.get('comments', []) # String = self.get_correlInputHeader(header) String.append('! anl :: write') String.append('open unit 100 write card name %s' % self.anlFilename) String.append('') String.append('! copy coordinates to comparison set') String.append('coor copy comp') String.append('') String.append('! select backbone atoms') String.append('defi bb select type b end') String.append('') String.append('traj query unit 10') String.append('correl maxtimesteps %d maxatom %d maxseries 1' % (self.correlArrayLength, self.maxatom)) String.append('enter rmsd rms mass orient') String.append('traj firstu 10 nunit 1 begin %d stop %d skip %d select bb end' % (self.correlStart, self.correlStop, self.correlSkip)) String.append('') String.append('write rmsd card unit 100 ') String.append('* Backbone RMSD') for comment in comments: String.append('* %s' % comment) String.append('*') String.append('') String.append('stop') String = '\n'.join(String) # Write file write_to = open('%s' % self.inpFilename, 'w') write_to.write(String) write_to.close()
def __init__(self, iterable=None, **kwargs): # kwargs kwargs = lowerKeys(kwargs) self._autoFix = kwargs.get('autofix', True) self._code = kwargs.get('code', 'None') self._name = kwargs.get('name', 'None') # self._dict = {} # Gatekeeper if iterable is None: super(BaseStruct, self).__init__() else: if self._autoFix: iterable = ( key for key in iterable if isinstance(key, MetaAtom) ) else: iterable,iterchk = tee(iterable, 2) for key in iterchk: if not isinstance(key, MetaAtom): raise StructError('Only objects derived from `MetaAtom`\ class may be added to a BaseStruct object') super(BaseStruct, self).__init__() self.extend(iterable)
def write_correlInput(self, **kwargs): """ This method writes a single charmm .inp file for a radius of gyration calculation. """ kwargs = lowerKeys(kwargs) header = kwargs.get('header', []) comments = kwargs.get('comments', []) # String = self.get_correlInputHeader(header) String.append('! anl :: write') String.append('open unit 100 write card name %s' % self.anlFilename) String.append('') if self.correlAtomSelection == 'all': String.append('defi taco select all end') else: String.append('defi taco select ') for i,letter in enumerate(self.correlAtomSelection): String.append('segid %s ' % letter.upper()) if i != len(self.correlAtomSelection)-1: String.append('.or. ') String.append('end') String.append('') String.append('traj query unit 10') String.append('correl maxtimesteps %d maxatom %d maxseries 1' % (self.correlArrayLength, self.maxatom)) String.append('enter gyro gyration') String.append('traj firstu 10 nunit 1 begin %d stop %d skip %d select taco end' % (self.correlStart, self.correlStop, self.correlSkip)) String.append('') String.append('write gyro card unit 100 ') String.append('* Radius of Gyration - Selection: %s' % self.correlAtomSelection) for comment in comments: String.append('* %s' % comment) String.append('*') String.append('') String.append('stop') String = '\n'.join(String) # Write file write_to = open('%s' % self.inpFilename, 'w') write_to.write(String) write_to.close()
def iter_res(self,**kwargs): """ A generator that returns one `Res` per iteration. kwargs: `restype` The kwarg `restype` allows you to specify which type of residue is iterated over by passing the class through Examples include: `Pro`, `Res` and `CGPro`. The default behavior is to detect the appropriate one. >>> thisSeg.iter_res(restype=Pro) """ # kwargs kwargs = lowerKeys(kwargs) resType = kwargs.get('restype', None) if resType is None: if self.segType == 'pro': newObj = Pro else: newObj = Res else: newObj = resType result = newObj(iterable=None ,code=self.code, autofix=False) for atom in self: if len(result) == 0: result.append(atom) lastresid = atom.resid elif atom.resid == lastresid: result.append(atom) else: if result: yield result result = newObj(iterable=[atom], code=self.code, autofix=False) lastresid = atom.resid if result: yield result
def write_correlInput(self, **kwargs): kwargs = lowerKeys(kwargs) header = kwargs.get('header', []) # print self.anlPathname if self.anlPathname is None: raise IOError("No directory specified.") mkdir(self.anlPathname) # for i, group in enumerate(grouper(self.nativeContacts, 100)): String = self.get_correlInputHeader(header) String.append('! anl :: write') for j, contact in enumerate(group): String.append('open unit %03d write card name %s/natq%02d%02d.anl' % (j+100, self.anlPathname, i, j)) String.append('') String.append('correl maxtimesteps %d maxatom %d maxseries %d' % (self.correlArrayLength, 1000, len(group))) for j, contact in enumerate(group): String.append('enter n%03d bond bynum %d bynum %d geometry' % (j, contact.i.atomNum, contact.j.atomNum)) String.append('traj firstu 10 nunit 1 begin %d stop %d skip %d select all end' % (self.correlStart, self.correlStop, self.correlSkip)) String.append('') for j, contact in enumerate(group): String.append('write n%03d card unit %03d' % (j, j+100)) String.append('* Contact %02d%02d: between cgAtoms %s and %s' % (i, j, contact.i.addr, contact.j.addr)) String.append('* Native Contact - Interaction between %s and %s' % (contact.i.prmString, contact.j.prmString)) String.append('*') String.append('') String.append('end') String.append('') String.append('rewind unit 10') String.append('') String.append('stop') String = '\n'.join(String) # Write file self.inpFilename = '%s/natq%02d.inp' % (self.inpPathname, i) print '%s' % self.inpFilename write_to = open(self.inpFilename, 'w') write_to.write(String) write_to.close()
def find(self, **kwargs): """ Returns a :class:`BaseStruct` object containing all "atom-like" objects which match the specified search criteria. **Note:** This method will always return a :class:`BaseStruct` object even if it is called by a child class. If you want to then use methods from the child class on this selection, you need to then reinstantize that child class using this result as an iterator. Sub-optimal for sure. **kwargs:** | ``chainid`` | ``segtype`` | ``resid`` | ``atomnum`` | ``atomtype`` | ``resName`` >>> self.find(chainid='a', segtype='pro', resid=3) """ if kwargs: kwargs = lowerKeys(kwargs) chainid = kwargs.get('chainid', None) segtype = kwargs.get('segtype', None) resid = kwargs.get('resid', None) atomnum = kwargs.get('atomnum', None) atomtype = kwargs.get('atomtype', None) resname = kwargs.get('resname', None) chainid0 = kwargs.get('chainid0', None) segtype0 = kwargs.get('segtype0', None) resid0 = kwargs.get('resid0', None) atomnum0 = kwargs.get('atomnum0', None) atomtype0 = kwargs.get('atomtype0', None) resname0 = kwargs.get('resname0', None) else: return BaseStruct([], autofix=False) # try: resid = int(resid) except (ValueError, TypeError): pass try: atomnum = int(atomnum) except (ValueError, TypeError): pass try: resid0 = int(resid0) except (ValueError, TypeError): pass try: atomnum0 = int(atomnum0) except (ValueError, TypeError): pass # iterator = ( atom for atom in self) if chainid: iterator = ( atom for atom in iterator if atom.chainid == chainid ) if segtype: iterator = ( atom for atom in iterator if atom.segType == segtype ) if resid: iterator = ( atom for atom in iterator if atom.resid == resid ) if atomnum: iterator = ( atom for atom in iterator if atom.atomNum == atomnum ) if atomtype: iterator = ( atom for atom in iterator if atom.atomType == atomtype ) if resname: iterator = ( atom for atom in iterator if atom.resName == resname ) if chainid0: iterator = ( atom for atom in iterator if atom.chainid0 == chainid0 ) if segtype0: iterator = ( atom for atom in iterator if atom.segType0 == segtype0 ) if resid0: iterator = ( atom for atom in iterator if atom.resid0 == resid0 ) if atomnum0: iterator = ( atom for atom in iterator if atom.atomNum0 == atomnum0 ) if atomtype0: iterator = ( atom for atom in iterator if atom.atomType0 == atomtype0 ) if resname0: iterator = ( atom for atom in iterator if atom.resName0 == resname0 ) return BaseStruct(iterator, autofix=False)
def Print(self, **kwargs): """ Returns a string representation of the Atom object. Formatting defaults to the `_autoInFormat` formatting. kwargs: `outformat` ["charmm","debug","xdebug","crd","xcrd"] `old_chainid` [False,True] `old_segtype` [False,True] `old_resid` [False,True] `old_atomnum` [False,True] >>> thisAtom.Print(outformat="charmm",old_resid=True) """ # Make kwargs case insensitive kwargs = lowerKeys(kwargs) # kwargs outFormat = kwargs.get('outformat', self.__class__._autoInFormat) old_chainid = kwargs.get('old_chainid', False) old_segType = kwargs.get('old_segtype', False) old_resid = kwargs.get('old_resid', False) old_atomNum = kwargs.get('old_atomnum', False) # Localize variables x, y, z = self.cart if old_chainid: chainid = self.chainid0 else: chainid = self.chainid if old_segType: segType = self.segType0 else: segType = self.segType if old_resid: resid = self.resid0 else: resid = self.resid if old_atomNum: atomNum = self.atomNum0 else: atomNum = self.atomNum # Set formatting if outFormat == 'charmm': taco = '%-6s%5i %4s %4s %4i %8.3f%8.3f%8.3f%6.2f%6.2f %-4s' % \ (self.tag, atomNum, self.atomType, self.resName, resid, x, y, z, self.weight, self.bFactor, chainid) elif outFormat == 'debug': taco = '%-6s%5i %4s %4s %1s%4i %8.3f%8.3f%8.3f' % \ (segType, atomNum, self.atomType, self.resName, chainid, resid, x, y, z) elif outFormat == 'xdebug': taco = '%15s > %15s: %5i %4s%4s %1s%4i %8.3f%8.3f%8.3f%6.2f%6.2f' % \ (self.addr0, self.addr, atomNum, self.atomType, self.resName, chainid, resid, x, y, z) elif outFormat == 'repr': taco = '%15s :: %4s %4s %8.3f %8.3f %8.3f' % \ (self.addr, self.atomType, self.resName, x, y, z) elif outFormat in ['crd', 'cor', 'card', 'short', 'shortcard']: taco = '%5i%5i %-4s %-4s%10.5f%10.5f%10.5f %-4s %-4i%10.5f' % \ (atomNum, self.resIndex, self.resName, self.atomType, x, y, z, chainid, resid, self.weight) elif outFormat in ['xcrd', 'xcor', 'xcard', 'long', 'longcard']: taco = '%10i%10i %-4s %-4s %20.10f%20.10f%20.10f %-5s %-4i %20.10f' % \ (atomNum, self.resIndex, self.resName, self.atomType, x, y, z, chainid + segType, resid, self.weight) else: raise AtomError('Print: unknown inFormat %s' % inFormat) return taco.upper()
def gen_cgStruct(self, **kwargs): """ """ # BTM: I don't understand how calcmass # works, so I am going to leave it out # for now. kwargs = lowerKeys(kwargs) for res in self.allAtoms.iter_res(restype=CGPro): tmp = res.get_goBB(**kwargs) tmp.atomType = ' b' # we can change this later for secondary struct tmp.derivedResName = res.resName tmp.resName = 'b' + self._shortname[res.resName] tmp.segType = 'bln' print "DEBUG: assign %s -> %s" % (tmp.derivedResName,tmp.resName) yield tmp if res.resName == 'gly': # one site residue continue elif res.resName in \ ['phe', 'tyr', 'hsd', 'his', 'trp', 'lys', 'arg']: # three site residue # get a BaseStruct of all of the SC atoms # named taco in honor of Frank ;-). taco = BaseStruct([atom for atom in res if not atom.is_backbone()]) if res.resName == 'phe': # phenlyalanine gets two "C" type beads, one at the beta carbon # position and one at the ring COM. sc1 = copy.deepcopy(res.find(atomtype=' cb ')[0]) sc2l = BaseStruct([a for a in taco if a.atomType.strip() in \ ['cg', 'cd1', 'hd1', 'cd2', 'hd2', 'ce1', \ 'he1','ce2', 'he2', 'cz', 'hz']]) sc1.atomNum = 1 sc1.atomType = ' s' sc1.derivedResName = sc1.resName sc1.resName = 'bf' sc1.segType = 'bln' sc2 = res.get_alphaCarbon() sc2.atomNum = 2 sc2.cart = sc2l.com sc2.atomType = ' s2' sc2.derivedResName = tmp.resName sc2.resName = 'bf' sc2.segType = 'bln' yield sc1 yield sc2 elif res.resName == 'arg' or res.resName == 'lys': # Arginine and Lysine gets a C + Qd, C at the CB, CG, CD COM # and Qd at the COM of the rest of the SC heavy atoms. sc1l = BaseStruct([a for a in taco if a.atomType.strip() in \ ['cb', 'cg', 'cd']]) sc2l = BaseStruct([a for a in taco if a.atomType.strip() in \ ['ce', 'nz', 'cz', 'nh1', 'nh2']]) sc1 = res.get_alphaCarbon() sc1.cart = sc1l.com sc1.atomNum = 1 sc1.atomType = ' s' sc1.derivedResName = sc1.resName sc1.resName = 'b'+ self._shortname[res.resName] sc1.segType = 'bln' yield sc1 sc2 = res.get_alphaCarbon() sc2.cart = sc2l.com sc2.atomNum = 2 sc2.atomType = ' s2' sc2.derivedResName = sc2.resName sc2.resName = 'b'+ self._shortname[res.resName] sc2.segType = 'bln' yield sc2 elif res.resName == 'tyr': # tyrosine gets C + Nd, C for CB, CG, and half the ring and Nd # for the second half of the ring + the hydroxyl group sc1l = BaseStruct([a for a in taco if a.atomType.strip() in \ ['cb', 'cg', 'cd1', 'cd2']]) sc2l = BaseStruct([a for a in taco if a.atomType.strip() in \ ['ce1', 'ce2', 'cz', 'oh']]) sc1 = res.get_alphaCarbon() sc1.cart = sc1l.com sc1.atomNum = 1 sc1.atomType = ' s' sc1.derivedResName = sc1.resName sc1.resName = 'by' sc1.segType = 'bln' yield sc1 sc2 = res.get_alphaCarbon() sc2.cart = sc2l.com sc2.atomNum = 2 sc2.atomType = ' s2' sc2.derivedResName = sc2.resName sc2.resName = 'by' sc2.segType = 'bln' yield sc2 elif res.resName == 'his' or res.resName == 'hsd': # histadine is C + Nda, note that we only deal with # neutral histadine here -- charged histadine would # have a Qda bead. sc1l = BaseStruct([a for a in taco if a.atomType.strip() in \ ['cb', 'cg']]) sc2l = BaseStruct([a for a in taco if a.atomType.strip() in \ ['nd1', 'cd2', 'ce1', 'ne2']]) sc1 = res.get_alphaCarbon() sc1.cart = sc1l.com sc1.atomNum = 1 sc1.atomType = ' s' sc1.derivedResName = sc1.resName sc1.resName = 'bh' sc1.segType = 'bln' yield sc1 sc2 = res.get_alphaCarbon() sc2.cart = sc2l.com sc2.atomNum = 2 sc2.atomType = ' s2' sc2.derivedResName = sc2.resName sc2.resName = 'bh' sc2.segType = 'bln' yield sc2 elif res.resName == 'trp': # tryptophan is similar to histadine, but with a C # bead on its end sc1l = BaseStruct([a for a in taco if a.atomType.strip() in \ ['cb', 'cg', 'cd1', 'ne1']]) sc2l = BaseStruct([a for a in taco if a.atomType.strip() in \ ['cd2', 'ce2', 'cz2', 'ce3', 'cz3', 'ch2']]) sc1 = res.get_alphaCarbon() sc1.cart = sc1l.com sc1.atomNum = 1 sc1.atomType = ' b' sc1.derivedResName = sc1.resName sc1.resName = 'bw' sc1.segType = 'bln' yield sc1 sc2 = res.get_alphaCarbon() sc2.cart = sc2l.com sc2.atomNum = 2 sc2.atomType = ' s2' sc2.derivedResName = sc2.resName sc2.resName = 'bw' sc2.segType = 'bln' yield sc2 else: # two site residue tmp = res.get_goSC(**kwargs) tmp.atomNum = 1 tmp.resName = 'b' + self._shortname[res.resName] tmp.segType = 'bln' tmp.atomType = ' s' yield tmp
def parse(pdbFilename, **kwargs): """ Parse a *.pdb* plain text file into its constituent chains and segments, and print one CHARMM formatted *.pdb* file per chain/segment combination. *kwarg defaults are listed first* **kwargs:** | ``informat`` ['auto', 'pdborg', 'charmm'] # 'auto' -> detects input formatting | ``outformat`` ['charmm', 'pdborg', 'auto'] # 'auto' -> same as input formatting | ``outpath`` ['auto', user_specified_path] # 'auto' -> same as pdbFilename path | ``fix_chainid`` [True, False] | ``autofix`` [True, False] | ``modelnum`` ['auto', 0, 1, 2, ...] # 'auto' -> uses the first model found | ``pickleme`` [False, True] # pickles the PDBFile object for later use | ``verbose`` [False, True] | ``old_resid`` [False, True] >>> parse('~/pychm/1yjp/1yjp.pdb',outpath='~',pickleme=True) """ # kwargs kwargs = lowerKeys(kwargs) inFormat = kwargs.get('informat', 'auto') outFormat = kwargs.get('outformat', 'charmm') outPath = kwargs.get('outpath', 'auto') fix_chainid = kwargs.get('fix_chainid', True) autoFix = kwargs.get('autofix', True) modelNum = kwargs.get('modelnum', 'auto') pickleMe = kwargs.get('pickleme', False) verbose = kwargs.get('verbose', False) old_resid = kwargs.get('old_resid', False) # Repackage the PDBFile kwargs, make pdbFile object pdbFileArgs = {'informat':inFormat, 'fix_chainid':fix_chainid, 'autofix':autoFix, 'verbose':verbose} pdb = PDBFile(pdbFilename, **pdbFileArgs) if verbose: print '%s: Output formatting set to `%s`' % (pdb.code, outFormat) # Get model number... if modelNum == 'auto': thisMol = pdb.iter_models().next() else: thisMol = pdb[modelNum] if verbose: print '%s: Loading `%s`' % (pdb.code, thisMol.name) # Determine explicit output Path if outPath == 'auto': outPath = pdb.path else: outPath = expandPath(outPath) mkdir(outPath) if verbose: print '%s: Output path set to `%s`' % (pdb.code, outPath) # Do Work thisMol.parse() if verbose: print '%s: Parsing `%s`' % (pdb.code, thisMol.name) # Write CHARMMing style output. segDict = {'nuc':'nuc', 'pro':'pro', 'good':'goodhet', 'bad':'het', 'dna':'dna', 'rna':'rna'} stdoutList = [] # Write pdb files writeArgs = {'outformat':outFormat, 'ter':True, 'end':False, 'old_resid':old_resid} for seg in thisMol.iter_seg(): stdoutList.append('%s-%s' % (seg.chainid, segDict[seg.segType])) name = '%s/new_%s-%s-%s.pdb' % (outPath, thisMol.code, seg.chainid, segDict[seg.segType]) if verbose: print '%s: Writing output to file `%s`' % (pdb.code, name) seg.write(filename=name, **writeArgs) # Write pickle (chk) file if pickleMe: pickleFilename = '%s/%s.chk' % (outPath, pdb.code) pickleFile = open(pickleFilename,'w') dump(pdb,pickleFile) pickleFile.close() if verbose: print '%s: Writing pickle to file `%s`' % (pdb.code, pickleFilename) if verbose: print '\n\nEnd of verbosity\n\n' # To STDOUT print 'natom=%d' % len(thisMol) print 'nwarn=%d' % len(thisMol.warnings) if thisMol.warnings: print 'warnings=%r' % thisMol.warnings print 'seg=%r' % stdoutList
def write(self, filename, **kwargs): """ Writes a file containing the molecular information contained in :class:`BaseStruct` object. **kwargs:** | ``outformat`` ["charmm","pdborg","debug","xdebug","crd","xcrd","mol2"] | ``old_chainid`` [False,True] | ``old_segType`` [False,True] | ``old_resid`` [False,True] | ``old_atomNum`` [False,True] | ``ter`` [False,True] | ``end`` True if `outformat` in ["pdborg","charmm"] | ``append`` [False,True] >>> thisSeg.write('~/1yjp.pdb',outformat='charmm',old_resid=True) """ # kwargs kwargs = lowerKeys(kwargs) outFormat = kwargs.get('outformat', 'charmm') end = kwargs.get('end', None) ter = kwargs.get('ter', None) append = kwargs.get('append', False) # filename = expandPath(filename) writeMe = [] if outFormat in ['pdborg', 'charmm']: for atom in self: writeMe.append(atom.Print(**kwargs)) if ter is None: ter = True if end is None: end = True elif outFormat == 'mol2': header = kwargs.get('header', None) bonds = kwargs.get('bonds', None) writeMe.append('@<TRIPOS>MOLECULE') if header: for line in header: writeMe.append(line) writeMe.append('') writeMe.append('@<TRIPOS>ATOM') for atom in self: writeMe.append(atom.Print(**kwargs)) writeMe.append('') writeMe.append('@<TRIPOS>BOND') if bonds: for bond in bonds: writeMe.append(bond.writeOut()) writeMe.append('') elif outFormat in ['debug', 'xdebug']: for atom in self: writeMe.append(atom.Print(**kwargs)) elif outFormat in ['crd', 'cor', 'card', 'short', 'shortcard', 'xcrd', 'xcor', 'xcard', 'long', 'longcard']: writeMe.append('*') writeMe.append(' %d' % len(self)) for atom in self: writeMe.append(atom.Print(**kwargs)) # TER/END if ter: writeMe.append('TER') if end: writeMe.append('END\n') # Write file writeMe = '\n'.join(writeMe) if append: writeTo = open(filename,'a') else: writeTo = open(filename,'w') writeTo.write(writeMe) writeTo.close()
def Print(self, **kwargs): """ Returns a string representation of the :class:`Atom`. Formatting defaults to `self.__class__._autoInFormat`. **kwargs:** | ``outformat`` ["pdborg","charmm","debug","xdebug","crd","xcrd"] | ``old_chainid`` [False,True] | ``old_segtype`` [False,True] | ``old_resid`` [False,True] | ``old_atomnum`` [False,True] >>> thisAtom.Print(outformat="pdborg",old_resid=True) """ # Make kwargs case insensitive kwargs = lowerKeys(kwargs) # kwargs outFormat = kwargs.get('outformat', self.__class__._autoInFormat) old_chainid = kwargs.get('old_chainid', False) old_segType = kwargs.get('old_segtype', False) old_resid = kwargs.get('old_resid', False) old_atomNum = kwargs.get('old_atomnum', False) # Localize variables x, y, z = self.cart if old_chainid: chainid = self.chainid0 else: chainid = self.chainid if old_segType: segType = self.segType0 else: segType = self.segType if old_resid: resid = self.resid0 else: resid = self.resid if old_atomNum: atomNum = self.atomNum0 else: atomNum = self.atomNum # Unretrofit whitespace padding for long (hydrogen) atomType... if len(self.atomType) > 4: tmpAtomType = self.atomType[-4:] else: tmpAtomType = self.atomType # Set formatting if outFormat == 'pdborg': taco = '%-6s%5i %4s%4s %1s%4i %8.3f%8.3f%8.3f%6.2f%6.2f%12s' % \ (self.tag, atomNum, tmpAtomType, self.resName, chainid, resid, x, y, z, self.weight, self.bFactor, self.element) elif outFormat == 'charmm': taco = '%-6s%5i %4s %4s %4i %8.3f%8.3f%8.3f%6.2f%6.2f %-4s' % \ (self.tag, atomNum, tmpAtomType, self.resName, resid, x, y, z, self.weight, self.bFactor, chainid) elif outFormat == 'debug': taco = '%-6s%5i %4s %4s %1s%4i %8.3f%8.3f%8.3f' % \ (segType, atomNum, tmpAtomType, self.resName, chainid, resid, x, y, z) elif outFormat == 'xdebug': taco = '%15s > %15s: %5i %4s%4s %1s%4i %8.3f%8.3f%8.3f%6.2f%6.2f' % \ (self.addr0, self.addr, atomNum, tmpAtomType, self.resName, chainid, resid, x, y, z, self.weight, self.bFactor) elif outFormat == 'repr': taco = '%15s :: %4s %4s %8.3f %8.3f %8.3f' % \ (self.addr, tmpAtomType, self.resName, x, y, z) elif outFormat in ['crd', 'cor', 'card', 'short', 'shortcard']: taco = '%5i%5i %-4s %-4s%10.5f%10.5f%10.5f %-4s %-4i%10.5f' % \ (atomNum, self.resIndex, self.resName, tmpAtomType, x, y, z, chainid, resid, self.weight) elif outFormat in ['xcrd', 'xcor', 'xcard', 'long', 'longcard']: taco = '%10i%10i %-8s %-8s %20.10f%20.10f%20.10f %-8s %-8i %20.10f' % \ (atomNum, self.resIndex, self.resName, tmpAtomType, x, y, z, chainid + segType, resid, self.weight) elif outFormat == 'mol2': taco = '%6i %-8s %9.4f %9.4f %9.4f %-7s %2i %4s' % \ (atomNum, tmpAtomType, x, y, z, self.elementType, resid, self.resName) else: raise AtomError('Print: unknown inFormat %s' % inFormat) return taco.upper()