def exportDef(self, stream=sys.stdout, convention=constants.INTERNAL): """export definitions to stream""" io.printf(stream, "\t#---------------------------------------------------------------\n") io.printf(stream, "\tDIHEDRAL %-8s\n", self.name) io.printf(stream, "\t#---------------------------------------------------------------\n") if convention == constants.INTERNAL: atms = self.atoms else: # convert atoms atms = [] for resId, atmName in self.atoms: if resId != 0: nTwarning("DihedralDef.exportDef: %s topology (%d,%s) skipped translation", self, resId, atmName) atms.append((resId, atmName)) elif not atmName in self.residueDef: nTerror("DihedralDef.exportDef: %s topology (%d,%s) not decoded", self, resId, atmName) atms.append((resId, atmName)) else: atm = self.residueDef[atmName] atms.append((resId, atm.translate(convention))) # end if # end for # print 'atms', atms # end if io.printf(stream, "\t\t%s = %s\n", "atoms", repr(atms)) for attr in ["karplus"]: io.printf(stream, "\t\t%s = %s\n", attr, repr(self[attr])) # end for io.printf(stream, "\tEND_DIHEDRAL\n")
def getArchiveIdFromDirectoryName(dirName): ''' From input such as: Return a valid id such as: or None on error. ''' nTdebug("In `getArchiveIdFromDirectoryName`, with %s as dirName" % dirName) if not dirName: nTerror( "Failed to map dirName [%s] because baseName evaluates to False." % dirName) return None # end if baseName = None for baseTry in results_baseList: nTdebug("baseTry: %s" % baseTry) if baseTry in dirName: baseName = baseTry break # end if # end def nTdebug("Returning %s as archiveID" % mapBase2Archive[baseName]) if not baseName in mapBase2Archive.keys(): nTwarning( "Failed to map dirName [%s] with baseName [%s] because baseName is an unenumerated baseName." % (dirName, baseName)) return None # end if return mapBase2Archive[baseName]
def getDsspPercentList( res ): """ Returns None for error, or a three item list with the percentage for each of the types. For residues without dssp properties set it will return a three item list with all None's. """ bogusResult = [ None, None, None ] secStructList = res.getDeepByKeys(DSSP_STR,SECSTRUCT_STR) if not secStructList: # nTdebug('Failed to get dssp secStructList for %s' % res) return bogusResult # end if result = [ 0., 0., 0. ] # Use floats for division later on. secStructList = to3StateDssp( secStructList ) n = 0 for secStruct in secStructList: secStructIdx = mapDssp2Int[secStruct] if secStructIdx == None: nTwarning('Found no map for secStruct: "%s" excluded' % secStruct) continue # end if result[secStructIdx] += 1 n += 1 # end for if n < 1: nTwarning('Failed to find any valid secStruct for %s. Returning empties.' % res) return bogusResult # end for # Use Python list comprehension grammar. result = [ 100.*result[i]/n for i in range(len(result)) ] # nTdebug('Returning in getDsspPercentList for %s with list: %s' % (res, str(result))) return result
def getDsspPercentList(res): """ Returns None for error, or a three item list with the percentage for each of the types. For residues without dssp properties set it will return a three item list with all None's. """ bogusResult = [None, None, None] secStructList = res.getDeepByKeys(DSSP_STR, SECSTRUCT_STR) if not secStructList: # nTdebug('Failed to get dssp secStructList for %s' % res) return bogusResult # end if result = [0., 0., 0.] # Use floats for division later on. secStructList = to3StateDssp(secStructList) n = 0 for secStruct in secStructList: secStructIdx = mapDssp2Int[secStruct] if secStructIdx == None: nTwarning('Found no map for secStruct: "%s" excluded' % secStruct) continue # end if result[secStructIdx] += 1 n += 1 # end for if n < 1: nTwarning( 'Failed to find any valid secStruct for %s. Returning empties.' % res) return bogusResult # end for # Use Python list comprehension grammar. result = [100. * result[i] / n for i in range(len(result))] # nTdebug('Returning in getDsspPercentList for %s with list: %s' % (res, str(result))) return result
def getArchiveIdFromDirectoryName(dirName): ''' From input such as: Return a valid id such as: or None on error. ''' nTdebug("In `getArchiveIdFromDirectoryName`, with %s as dirName" % dirName) if not dirName: nTerror("Failed to map dirName [%s] because baseName evaluates to False." % dirName) return None # end if baseName = None for baseTry in results_baseList: nTdebug("baseTry: %s" % baseTry) if baseTry in dirName: baseName = baseTry break # end if # end def nTdebug("Returning %s as archiveID" % mapBase2Archive[baseName]) if not baseName in mapBase2Archive.keys(): nTwarning("Failed to map dirName [%s] with baseName [%s] because baseName is an unenumerated baseName." % (dirName, baseName)) return None # end if return mapBase2Archive[baseName]
def translateWithDefault(self, convention): """Translate atomDef.name to nomenclature of convention. Return atomDef.name if not defined for convention """ newName = self.translate(convention) if newName: return newName nTwarning( 'AtomDef.translateWithDefault: Failed to find translation to "%s" for atom: %s; Using CING name "%s" instead.', convention, self, self.name, ) return self.name
def exportDef(self, stream=sys.stdout, convention=constants.INTERNAL): """export definitions to stream""" io.printf(stream, "\t#---------------------------------------------------------------\n") io.printf(stream, "\tATOM %-8s\n", self.translate(convention)) io.printf(stream, "\t#---------------------------------------------------------------\n") # Topology; optionally convert if convention == constants.INTERNAL: top2 = self.topology else: # convert topology top2 = [] for resId, atmName in self.topology: if resId != 0: nTwarning("AtomDef.exportDef: %s topology (%d,%s) skipped translation", self, resId, atmName) top2.append((resId, atmName)) elif not atmName in self.residueDef: nTerror("AtomDef.exportDef: %s topology (%d,%s) not decoded", self, resId, atmName) top2.append((resId, atmName)) else: atm = self.residueDef[atmName] top2.append((resId, atm.translate(convention))) # end if # end for # print 'top2', top2 # end if io.printf(stream, "\t\t%s = %s\n", "topology", repr(top2)) # clean the properties list props = [] for prop in self.properties: # Do not store name and residueDef.name as property. Add those dynamically upon reading if ( not prop in [self.name, self.residueDef.name, self.residueDef.shortName, self.spinType] and not prop in props ): props.append(prop) # end if # end for io.printf(stream, "\t\t%s = %s\n", "properties", repr(props)) # Others for attr in ["nameDict", "aliases", "pseudo", "real", "type", "spinType", "shift", "hetatm"]: if self.has_key(attr): io.printf(stream, "\t\t%s = %s\n", attr, repr(self[attr])) # end for io.printf(stream, "\tEND_ATOM\n")
def stringMeansBooleanTrue(inputStr): """ Returns True if it's a string that is either 1 (any non-zero), True, etc. Optimized for speed. See unit test. """ if inputStr == None: return False if not isinstance(inputStr, str): return False inputStrlower = inputStr.lower() if inputStrlower == 'true': return True if inputStrlower == 'false': return False if inputStrlower == 't': return True if inputStrlower == 'f': return False if inputStrlower == 'y': return True if inputStrlower == 'n': return False if inputStrlower == 'yes': return True if inputStrlower == 'no': return False try: inputInt = int(inputStr) except: nTwarning("Failed to get integer after testing string possibilities") inputInt = 0 if inputInt: return True return False
def initRestraints( self ): """ Initialize restraints from restraint lists Only distances for now """ nTmessage('==> Queeny adding restraints (# elements = %d)', len(self)) for dme in self.itervalues(): dme.upperChange = 0.0 #end for nkeys = len(self) #print '>', nkeys # count = 0 for drl in self.project.distances: for dr in drl: if len(dr.atomPairs) == 1: atm1,atm2 = dr.atomPairs[0] upper = dr.upper if dr.upper is None: # sometimes happens; i.e. entry 1but upper = DmElement.upperDefault lower = dr.lower if dr.lower is None: # lower values sometimes set to None lower = DmElement.lowerDefault self.initDmElement(atm1, atm2, lower, upper) else: # ambiguous restraints rm6distances = self._calculateAverage( dr ) if rm6distances is None: nTwarning('Queeny.initRestraints: failure to analyze %s', dr) break #endif upper = dr.upper if dr.upper is None: # sometimes happens; i.e. entry 1but upper = DmElement.upperDefault lower = dr.lower if dr.lower is None: # lower values sometimes set to None lower = DmElement.lowerDefault # hr: total uncertainty change associated with this restraint # dH = hmax - hr : change in uncertainty # hi = hmax - dH*frac : relative R-6 contribution of atom pair: sum(hi) = hr hmax = DmElement.uncertaintyDefault hr = math.log(upper-lower) dH = hmax - hr pair = 0 for atm1,atm2 in dr.atomPairs: hi = hmax - rm6distances[pair]*dH self.initDmElement(atm1, atm2, lower, lower+math.exp(hi)) pair += 1 #end for #end if # count += 1 #end for #end for # nTdebug('Queeny.initRestraints: %d restraints added (# elements = %d)', count, len(self)) self.setNeighbors(nkeys) # update the neighbors for newly added
def runShiftx(project, parseOnly=False, model=None): """ Use shiftx program to predict chemical shifts Works only for protein residues. Adds ShiftxResult instance to validation container of atoms LEGACY: Adds a NTlist object with predicted values for each model as shiftx attribute to each atom for which there are predictions, or empty list otherwise. Throws warnings for non-protein residues. Returns True on error. Shiftx works on pdb files, uses only one model (first), so we have to write the files separately and analyze them one at the time. """ # LEGACY: if parseOnly: return parseShiftx(project) if cdefs.cingPaths.shiftx is None: nTmessage("runShiftx: no shiftx executable, skipping") return False # Gracefully return if project.molecule is None: nTerror("runShiftx: no molecule defined") return True if project.molecule.modelCount == 0: nTwarning('runShiftx: no models for "%s"', project.molecule) return True if model is not None and model >= project.molecule.modelCount: nTerror('runShiftx: invalid model (%d) for "%s"', model, project.molecule) return True if not project.molecule.hasAminoAcid(): nTmessage("==> Skipping runShiftx because no amino acids are present.") return False nTmessage("==> Running shiftx") skippedAtoms = [] # Keep a list of skipped atoms for later skippedResidues = [] # Only used for presenting to end user not actually used for skipping. skippedChains = [] for chain in project.molecule.allChains(): skippChain = True for res in chain.allResidues(): if not res.hasProperties("protein"): if not res.hasProperties("HOH"): # don't report waters skippedResidues.append(res) for atm in res.allAtoms(): atm.pdbSkipRecord = True skippedAtoms.append(atm) # end for else: skippChain = False # end if if skippChain: skippedChains.append(chain) # end for # end for if skippedResidues: nTmessage("==> runShiftx: %s non-protein residues will be skipped." % len(skippedResidues)) defs = project.getStatusDict(constants.SHIFTX_KEY, **shiftxStatus()) if model is not None: defs.models = NTlist(model) else: defs.models = NTlist(*range(project.molecule.modelCount)) defs.baseName = "model_%03d" defs.completed = False defs.parsed = False defs.chains = [] # initialize the shiftx attributes _resetShiftx(project) path = project.validationPath(defs.directory) if not path: return True if path.exists(): nTdebug("runShiftx: removing %s with prior data", path) path.rmdir() path.makedirs() doShiftx = ExecuteProgram(pathToProgram=cdefs.cingPaths.shiftx, rootPath=path, redirectOutput=False) startTime = io.now() for model in defs.models: # set filenames rootname = defs.baseName % model nTdebug("runShiftx: doing model %s, path %s, rootname %s", model, path, rootname) # generate a pdbfile pdbFile = project.molecule.toPDB(model=model, convention=constants.IUPAC) if not pdbFile: nTerror("runShiftx: Failed to generate a pdb file for model: %s", model) return True pdbFile.save(path / rootname + ".pdb") del pdbFile for chain in project.molecule.allChains(): if chain not in skippedChains: # nTdebug('Doing chain code [%s]' % (chain.name)) # quotes needed because by default the chain id is a space now. # chainId = "'" + chain.name + "'" # According to the readme in shiftx with the source this is the way to call it. chainId = "1" + chain.name outputFile = rootname + "_" + chain.name + ".out" defs.chains.append((chain.name, outputFile)) doShiftx(chainId, rootname + ".pdb", outputFile) # end if # end for # end for # cleanup for pdbfile in path.glob("*.pdb"): pdbfile.remove() # Restore the 'default' state for atm in skippedAtoms: atm.pdbSkipRecord = False defs.completed = True # parse the results if parseShiftx(project): return True defs.date = io.now() defs.version = __version__ defs.molecule = project.molecule.asPid defs.remark = "Shiftx on %s completed in %.1f seconds on %s; data in %s" % ( project.molecule, defs.date - startTime, defs.date, path, ) project.history(defs.remark) nTmessage("==> %s", defs.remark) return False
residue_definitions = NTdb.allResidueDefs() for res_def in residue_definitions: correct_termini_stap(res_def) remove_pseudo_atoms(res_def) def save_database(db): """Resolve the path of the database db and save it to SML.""" path = os.path.realpath(os.path.join(cingPythonCingDir, 'Database', db)) saveToSML(NTdb, path, db) if __name__ == '__main__': """Add STAP convention to INTERNAL_1 database. Execute as: cing --script=addStapConv.py --noProject """ # DEFAULT: 1 disable only when needed. if 1: nTwarning( "Don't execute this script %s by accident. It damages CING." % getCallerFileName()) sys.exit(1) cing.verbosity = cing.verbosityDebug copy_from_convention(from_convention='XPLOR', new_convention='STAP') correct_xplor_stap() save_database(db='INTERNAL_1')
if not protein_only: residue_definitions = NTdb.allResidueDefs() for res_def in residue_definitions: correct_termini_stap(res_def) remove_pseudo_atoms(res_def) def save_database(db): """Resolve the path of the database db and save it to SML.""" path = os.path.realpath(os.path.join(cingPythonCingDir, 'Database', db)) saveToSML(NTdb, path, db) if __name__ == '__main__': """Add STAP convention to INTERNAL_1 database. Execute as: cing --script=addStapConv.py --noProject """ # DEFAULT: 1 disable only when needed. if 1: nTwarning("Don't execute this script %s by accident. It damages CING." % getCallerFileName()) sys.exit(1) cing.verbosity = cing.verbosityDebug copy_from_convention(from_convention='XPLOR', new_convention='STAP') correct_xplor_stap() save_database(db='INTERNAL_1')
def runTalosPlus(project, tmp=None, parseOnly=False): """Perform a talos+ analysis; parses the results; put into new CING dihedral restraint list Returns True on error. Returns False when talos is absent or when all is fine. """ #LEGACY: if parseOnly: return parseTalosPlus(project) if project is None: nTerror("runTalosPlus: No project defined") return True # check executable if cdefs.cingPaths.talos is None: nTmessage('runTalosPlus: no talosPlus executable defined, skipping') return False # Gracefully return status, output = getOsResult('which '+ cdefs.cingPaths.talos ) if len(output) == 0: nTmessage('runTalosPlus: invalid talosPlus executable defined (%s), skipping', cdefs.cingPaths.talos) return False # Gracefully return if project.molecule is None: nTmessage("runTalosPlus: No molecule defined") return True residues = project.molecule.residuesWithProperties('protein') if not residues: nTmessage('runTalosPlus: no amino acids defined') return False if len( project.molecule.resonanceSources ) == 0: nTmessage("==> runTalosPlus: No resonances defined so no sense in running.") # JFD: This doesn't catch all cases. return False talosDefs = project.getStatusDict(constants.TALOSPLUS_KEY, **talosDefaults) talosDefs.molecule = project.molecule.asPid talosDefs.directory = constants.TALOSPLUS_KEY path = project.validationPath( talosDefs.directory ) if not path: return True if path.exists(): nTdebug('runTalosPlus: removing %s with prior data', path) path.rmdir() path.makedirs() startTime = io.now() talosDefs.completed = False talosDefs.parsed = False _resetTalosPlus(project) # Exporting the shifts fileName = path / talosDefs.tableFile if exportShifts2TalosPlus(project, fileName=fileName): nTwarning("runTalosPlus: Failed to exportShifts2TalosPlus; this is normal for empty CS list.") return False # running TalosPlus talosProgram = ExecuteProgram(cdefs.cingPaths.talos, rootPath=path, redirectOutput=True) nTmessageNoEOL('==> Running talos+ ... ') talosProgram( '-in ' + talosDefs.tableFile + ' -sum ' + talosDefs.predFile ) nTmessage('Done!') if _findTalosOutputFiles(path, talosDefs): return True talosDefs.date = io.now() talosDefs.completed=True talosDefs.version = __version__ talosDefs.molecule = project.molecule.asPid talosDefs.remark = 'TalosPlus on %s completed in %.1f seconds on %s; data in %s' % \ (project.molecule, talosDefs.date-startTime, talosDefs.date, path) # Importing the results if parseTalosPlus(project): nTerror("runTalosPlus: Failed parseTalosPlus") return True project.history(talosDefs.remark) nTmessage('==> %s', talosDefs.remark) return False