def LoadKeggCompounds(kegg_json_filename=COMPOUND_FILE, draw_thumbnails=True): parsed_json = json.load(gzip.open(kegg_json_filename, 'r')) for cd in parsed_json: try: cid = cd['CID'] logging.debug('Handling compound %s', cid) formula = cd.get('formula') mass = cd.get('mass') if mass is not None: mass = float(mass) inchi = cd.get('InChI') num_electrons = cd.get('num_electrons') group_vector = cd.get('group_vector') name = models.CommonName.GetOrCreate(cd['name']) """ if formula is None: raise KeyError('Missing formula for CID %s' % cid) if mass is None: raise KeyError('Missing mass for CID %s' % cid) if inchi is None: raise KeyError('Missing inchi for CID %s' % cid) """ c = models.Compound(kegg_id=cid, formula=formula, inchi=inchi, mass=mass, name=name, group_vector=group_vector) if num_electrons is not None: c.num_electrons = int(num_electrons) if draw_thumbnails: c.WriteStructureThumbnail() c.save() # Add the thermodynamic data. pmaps = cd.get('pmaps') if not pmaps: error = cd.get('error') if error: c.no_dg_explanation = error else: for pmap in pmaps: AddPmapToCompound(pmap, c) # Add the common names. names = GetOrCreateNames(cd['names']) for n in names: c.common_names.add(n) c.save() except Exception, e: logging.error(e) continue
def testDeltaG(self): # Create a test compound. species = [models.Specie(number_of_hydrogens=12, net_charge=0, formation_energy=-10.5), models.Specie(number_of_hydrogens=11, net_charge=-1, formation_energy=-12.1), models.Specie(number_of_hydrogens=10, net_charge=-2, formation_energy=-13.4)] species_group = models.SpeciesGroup() species_group._all_species = species compound = models.Compound(kegg_id='fake compound') compound._species_group_to_use = species_group # Format: ph, ionic strength, dG. test_data = ((6.5, 0.1, 361.094), (7.0, 0.1, 389.619), (7.5, 0.1, 418.143), (7.0, 0.0001, 386.118), (7.0, 0.001, 386.473), (7.0, 0.2, 390.505)) for ph, i_s, expected_dg in test_data: actual_dg = compound.DeltaG(pH=ph, ionic_strength=i_s) self.assertAlmostEqual(expected_dg, actual_dg, 3, 'ph: %f, i_s: %f, expected dG: %f, actual dG: %f' % (ph, i_s, expected_dg, actual_dg))
def testHasData(self): compound = models.Compound(kegg_id='fake compound') self.assertFalse(compound.HasData()) compound.formula = 'C12H22O11' self.assertFalse(compound.HasData()) compound.mass = 0.0 self.assertFalse(compound.HasData()) compound.mass = 14.5 self.assertTrue(compound.HasData())
def testGetAtomBag(self): compound = models.Compound(kegg_id='fake compound') self.assertEqual(None, compound.GetAtomBag()) compound.formula = 'C12H22O11' expected_atom_bag = {'C': 12, 'H': 22, 'O': 11} self.assertEqual(expected_atom_bag, compound.GetAtomBag()) compound.formula = 'C10H16N5O12P3S' expected_atom_bag = {'C': 10, 'H': 16, 'N': 5, 'O': 12, 'P': 3, 'S': 1} self.assertEqual(expected_atom_bag, compound.GetAtomBag()) # Contains an R group. compound.formula = 'C10R11H16N5O12P3S' self.assertEqual(None, compound.GetAtomBag())
def testHashableReactionString(self): """Ensure that hashable strings for different reactions are different.""" compound_a = models.Compound(kegg_id='C00010') compound_b = models.Compound(kegg_id='C00009') compound_c = models.Compound(kegg_id='C00021') compound_d = models.Compound(kegg_id='C00032') compound_e = models.Compound(kegg_id='C00190') hydrogen = models.Compound(kegg_id='C00080') # 1 a + 1 b = 1 c + 1 d reactants_a = [models.Reactant(coeff=1, compound=compound_a), models.Reactant(coeff=1, compound=compound_b)] products_a = [models.Reactant(coeff=1, compound=compound_c), models.Reactant(coeff=1, compound=compound_d)] hashable_a = models.StoredReaction.HashableReactionString(reactants_a, products_a) # 1 a + 1 b = 1 c + 2 d reactants_b = [models.Reactant(coeff=1, compound=compound_a), models.Reactant(coeff=1, compound=compound_b)] products_b = [models.Reactant(coeff=1, compound=compound_c), models.Reactant(coeff=2, compound=compound_d)] hashable_b = models.StoredReaction.HashableReactionString(reactants_b, products_b) # 1 a + 1 b = 1 c + 1 e reactants_c = [models.Reactant(coeff=1, compound=compound_a), models.Reactant(coeff=1, compound=compound_b)] products_c = [models.Reactant(coeff=1, compound=compound_c), models.Reactant(coeff=1, compound=compound_e)] hashable_c = models.StoredReaction.HashableReactionString(reactants_c, products_c) # 3 a + 1 b = 2 c reactants_d = [models.Reactant(coeff=3, compound=compound_a), models.Reactant(coeff=1, compound=compound_b)] products_d = [models.Reactant(coeff=2, compound=compound_c)] hashable_d = models.StoredReaction.HashableReactionString(reactants_d, products_d) all_hashables = (hashable_a, hashable_b, hashable_c, hashable_d) for a, b in itertools.combinations(all_hashables, 2): self.assertNotEqual(a, b) # 1 a + 1 b = 1 d + 1 c reactants_a2 = [models.Reactant(coeff=1, compound=compound_a), models.Reactant(coeff=1, compound=compound_b)] products_a2 = [models.Reactant(coeff=1, compound=compound_d), models.Reactant(coeff=1, compound=compound_c)] hashable_a2 = models.StoredReaction.HashableReactionString(reactants_a2, products_a2) self.assertEqual(hashable_a2, hashable_a) # 1 d + 1 c = 1 a + 1 b products_a3 = [models.Reactant(coeff=1, compound=compound_a), models.Reactant(coeff=1, compound=compound_b)] reactants_a3 = [models.Reactant(coeff=1, compound=compound_d), models.Reactant(coeff=1, compound=compound_c)] hashable_a3 = models.StoredReaction.HashableReactionString(reactants_a3, products_a3) self.assertEqual(hashable_a3, hashable_a) # 1 b + 3 a = 2 c reactants_d2 = [models.Reactant(coeff=1, compound=compound_b), models.Reactant(coeff=3, compound=compound_a)] products_d2 = [models.Reactant(coeff=2, compound=compound_c)] hashable_d2 = models.StoredReaction.HashableReactionString(reactants_d2, products_d2) self.assertEqual(hashable_d2, hashable_d) # 2 c = 1 b + 3 a products_d3 = [models.Reactant(coeff=1, compound=compound_b), models.Reactant(coeff=3, compound=compound_a)] reactants_d3 = [models.Reactant(coeff=2, compound=compound_c)] hashable_d3 = models.StoredReaction.HashableReactionString(reactants_d3, products_d3) self.assertEqual(hashable_d3, hashable_d) # 1 b + 3 a = 2 c + 2 h+ reactants_d4 = [models.Reactant(coeff=1, compound=compound_b), models.Reactant(coeff=3, compound=compound_a)] products_d4 = [models.Reactant(coeff=2, compound=compound_c), models.Reactant(coeff=2, compound=hydrogen)] hashable_d4 = models.StoredReaction.HashableReactionString(reactants_d4, products_d4) self.assertEqual(hashable_d4, hashable_d)
def setUp(self): self.compound = models.Compound(kegg_id='fake compound', formula='C10H16N5O12P3S')
def testHashableReactionString(self): """ Ensure that hashable strings for different reactions are different and the same for equivalent reactions. """ compound_a = models.Compound(kegg_id='C00010') compound_b = models.Compound(kegg_id='C00009') compound_c = models.Compound(kegg_id='C00021') compound_d = models.Compound(kegg_id='C00032') compound_e = models.Compound(kegg_id='C00190') # 1 a + 1 b = 1 c + 1 d reactants_a = [models.Reactant(coeff=-1, compound=compound_a), models.Reactant(coeff=-1, compound=compound_b), models.Reactant(coeff=1, compound=compound_c), models.Reactant(coeff=1, compound=compound_d)] sr_a = models.StoredReaction.HashableReactionString(reactants_a) # 1 a + 1 b = 1 c + 2 d reactants_b = [models.Reactant(coeff=-1, compound=compound_a), models.Reactant(coeff=-1, compound=compound_b), models.Reactant(coeff=1, compound=compound_c), models.Reactant(coeff=2, compound=compound_d)] sr_b = models.StoredReaction.HashableReactionString(reactants_b) # 1 a + 1 b = 1 c + 1 e reactants_c = [models.Reactant(coeff=-1, compound=compound_a), models.Reactant(coeff=-1, compound=compound_b), models.Reactant(coeff=1, compound=compound_c), models.Reactant(coeff=1, compound=compound_e)] sr_c = models.StoredReaction.HashableReactionString(reactants_c) # 3 a + 1 b = 2 c reactants_d = [models.Reactant(coeff=-3, compound=compound_a), models.Reactant(coeff=-1, compound=compound_b), models.Reactant(coeff=2, compound=compound_c)] sr_d = models.StoredReaction.HashableReactionString(reactants_d) # 1 a + 1 b = 1 d + 1 c reactants_a2 = [models.Reactant(coeff=-1, compound=compound_a), models.Reactant(coeff=-1, compound=compound_b), models.Reactant(coeff=1, compound=compound_d), models.Reactant(coeff=1, compound=compound_c)] sr_a2 = models.StoredReaction.HashableReactionString(reactants_a2) # 1 d + 1 c = 1 a + 1 b reactants_a3 = [models.Reactant(coeff=1, compound=compound_a), models.Reactant(coeff=1, compound=compound_b), models.Reactant(coeff=-1, compound=compound_d), models.Reactant(coeff=-1, compound=compound_c)] sr_a3 = models.StoredReaction.HashableReactionString(reactants_a3) # 1 b + 3 a = 2 c reactants_d2 = [models.Reactant(coeff=-1, compound=compound_b), models.Reactant(coeff=-3, compound=compound_a), models.Reactant(coeff=2, compound=compound_c)] sr_d2 = models.StoredReaction.HashableReactionString(reactants_d2) # 2 c = 1 b + 3 a reactants_d3 = [models.Reactant(coeff=1, compound=compound_b), models.Reactant(coeff=3, compound=compound_a), models.Reactant(coeff=-2, compound=compound_c)] sr_d3 = models.StoredReaction.HashableReactionString(reactants_d3) # 1 b + 3 a = 2 c + 2 h+ reactants_d4 = [models.Reactant(coeff=-1, compound=compound_b), models.Reactant(coeff=-3, compound=compound_a), models.Reactant(coeff=2, compound=compound_c)] sr_d4 = models.StoredReaction.HashableReactionString(reactants_d4) # make sure all the 4 reactions have different hashable strings for a, b in itertools.combinations((sr_a, sr_b, sr_c, sr_d), 2): self.assertNotEqual(a, b) # make sure all the 3 versions of reaction a have the same # hashable strings for a, b in itertools.combinations((sr_a, sr_a2, sr_a3), 2): self.assertEqual(a, b) # make sure all the 4 versions of reaction a have the same # hashable strings for a, b in itertools.combinations((sr_d, sr_d2, sr_d3, sr_d4), 2): self.assertEqual(a, b)