def makeNewSpecies(structure, label='', reactive=True): """ Attempt to make a new species based on a chemical `structure`, which is a :class:`Structure` object. The proposed species is checked against the list of existing species; if the species already exists, this function returns the existing species. If the species does not exist, a :class:`Species` object is created and returned after being appended to the global species list. """ global speciesCounter # # Recalculate atom types for proposed structure (hopefully not necessary) # structure.simplifyAtomTypes() # structure.updateAtomTypes() # First check cache and return if species is found for i, spec in enumerate(speciesCache): if spec.isIsomorphic(structure): speciesCache.pop(i) speciesCache.insert(0, spec) return spec # Return an existing species if a match is found for spec in speciesList: if spec.isIsomorphic(structure): speciesCache.insert(0, spec) if len(speciesCache) > speciesCacheMaxSize: speciesCache.pop() return spec # Return None if the species has a forbidden structure if thermo.forbiddenStructures is not None: for lbl, struct in thermo.forbiddenStructures.iteritems(): if structure.isSubgraphIsomorphic(struct): return None # Otherwise make a new species if label == '': # label = structure.getFormula() # for atom in structure.atoms(): # if atom.hasFreeElectron(): label += 'J' label = structure.toSMILES() # Note in the log spec = Species(speciesCounter+1, label, structure, reactive) logging.verbose('Creating new species %s' % str(spec)) return processNewSpecies(spec)
def makeNewSpecies(structure, label='', reactive=True): """ Attempt to make a new species based on a chemical `structure`, which is a :class:`Structure` object. The proposed species is checked against the list of existing species; if the species already exists, this function returns the existing species. If the species does not exist, a :class:`Species` object is created and returned after being appended to the global species list. """ global speciesCounter # # Recalculate atom types for proposed structure (hopefully not necessary) # structure.simplifyAtomTypes() # structure.updateAtomTypes() # First check cache and return if species is found for i, spec in enumerate(speciesCache): if spec.isIsomorphic(structure): speciesCache.pop(i) speciesCache.insert(0, spec) return spec # Return an existing species if a match is found for spec in speciesList: if spec.isIsomorphic(structure): speciesCache.insert(0, spec) if len(speciesCache) > speciesCacheMaxSize: speciesCache.pop() return spec # Return None if the species has a forbidden structure if thermo.forbiddenStructures is not None: for lbl, struct in thermo.forbiddenStructures.iteritems(): if structure.isSubgraphIsomorphic(struct): return None # Otherwise make a new species if label == '': # label = structure.getFormula() # for atom in structure.atoms(): # if atom.hasFreeElectron(): label += 'J' label = structure.toSMILES() speciesCounter += 1 spec = Species(speciesCounter, label, structure, reactive) speciesList.insert(0, spec) spec.getResonanceIsomers() if thermoDatabase is not None: spec.getThermoData() # Draw species if settings.drawMolecules: mol = pybel.Molecule(spec.toOBMol()) mol.draw(False, os.path.join(settings.outputDirectory, 'species/' + str(spec) + '.png')) # Note in the log logging.debug('Created new species ' + str(spec) )# + ': ' + spec.toInChI()) # Return the newly created species speciesCache.insert(0, spec) if len(speciesCache) > speciesCacheMaxSize: speciesCache.pop() return spec