def test_fileName(self): f = chemkit.MoleculeFile() self.assertEqual(f.fileName(), "") f.setFileName("methane.sdf") self.assertEqual(f.fileName(), "methane.sdf") f.setFileName("") self.assertEqual(f.fileName(), "") f = chemkit.MoleculeFile("ethanol.cml") self.assertEqual(f.fileName(), "ethanol.cml")
def test_methanol(self): f = chemkit.MoleculeFile(dataPath + "methanol.sdf") ok = f.read() if not ok: print f.errorString() self.assertTrue(ok) self.assertEqual(f.moleculeCount(), 1) molecule = f.molecule(0) self.assertIsNotNone(molecule) self.assertEqual(molecule.formula(), "CH4O")
def test_readString(self): f = chemkit.MoleculeFile() f.setFormat("xyz") # string containing the file's data data = "5\n" data += "methane\n" data += "C 0.000000 0.000000 0.000000\n" data += "H 0.000000 0.000000 1.089000\n" data += "H 1.026719 0.000000 -0.363000\n" data += "H -0.513360 -0.889165 -0.363000\n" data += "H -0.513360 0.889165 -0.363000\n" ok = f.readString(data) if not ok: print f.errorString() self.assertTrue(ok) self.assertEqual(f.moleculeCount(), 1) molecule = f.molecule(0) self.assertIsNotNone(molecule) self.assertEqual(molecule.formula(), "CH4")
# make-descriptors-csv-file.py: creates a csv file with descriptor values import os import sys import chemkit if len(sys.argv) < 4: print 'usage: ' + sys.argv[0] + ' [INPUT_FILENAME] [OUTPUT_FILENAME] [DESCRIPTORS...]' input_filename = sys.argv[1] output_filename = sys.argv[2] # read input file input_file = chemkit.MoleculeFile(input_filename) if not input_file.read(): print 'error reading file: ' + input_file.errorString() sys.exit(-1) # create output file output_file = open(output_filename, "w") # get descriptor names descriptors = sys.argv[3:] # write header output_file.write("inchikey,") for descriptor in descriptors: output_file.write(descriptor + ",") output_file.write("\n") for molecule in input_file.molecules():
settings = json.load(file(sys.argv[1])) host = settings['server']['host'] port = settings['server']['port'] collection_name = settings['server']['collection'] except: print 'Failed to load server settings' sys.exit(-1) # connect to mongochem mongo server connection = pymongo.Connection(host, port) db = connection[collection_name] molecules_collection = db['molecules'] # open and read pubchem file fileName = sys.argv[2] moleculeFile = chemkit.MoleculeFile(fileName) if not moleculeFile.read(): print 'Error reading file: ' + moleculeFile.errorString() sys.exit(-1) print 'Read file \'' + os.path.basename( fileName) + '\' (%d molecules)' % moleculeFile.moleculeCount() for molecule in moleculeFile.molecules(): # data values name = molecule.data("PUBCHEM_IUPAC_TRADITIONAL_NAME") if not len(name): name = molecule.data("PUBCHEM_IUPAC_SYSTEMATIC_NAME") formula = molecule.formula() inchi = molecule.formula("inchi")
def importFile(fileName, collection): moleculeFile = chemkit.MoleculeFile(fileName) if not moleculeFile.read(): print 'Error reading file: ' + moleculeFile.errorString() return False elif moleculeFile.isEmpty(): print 'Error: file is empty' return False molecule = moleculeFile.molecule() # data values name = molecule.name() formula = molecule.formula() inchi = molecule.formula("inchi") inchikey = molecule.formula("inchikey") # atom counts atomCount = molecule.atomCount() hydrogenAtomCount = len([atom for atom in molecule.atoms() if atom.isTerminalHydrogen()]) heavyAtomCount = atomCount - hydrogenAtomCount # descriptors mass = molecule.mass() molecularWeight = 0.0 try: molecularWeight = float(molecule.data("Molecular weight")) except: pass monoisotopicWeight = 0.0 try: monoisotopicWeight = float(molecule.data("Monoisotopic weight")) except: pass meltingPoint = 0.0 try: meltingPoint = float(molecule.data("Melting point")) except: pass boilingPoint = 0.0 try: boilingPoint = float(molecule.data("Boiling point")) except: pass # insert molecule document document = { "name" : name, "formula" : formula, "inchi" : inchi, "inchikey" : inchikey, "atomCount" : atomCount, "heavyAtomCount" : heavyAtomCount, "mass" : mass, "descriptors" : {"molecular-weight" : molecularWeight, "monoisotopic-weight" : monoisotopicWeight, "melting-point" : meltingPoint, "boiling-point" : boilingPoint} } collection.update({"inchikey" : inchikey}, document, True) return True