def test_init(self): c = molecule_util.Molecule(id='h2o', name='water', structure=self.h2o['inchi']) self.assertEqual(c.id, 'h2o') self.assertEqual(c.name, 'water') self.assertEqual(c.structure, self.h2o['inchi']) self.assertEqual(c.get_format(), 'inchi') self.assertEqual(c.to_openbabel().GetFormula(), 'H2O') self.assertEqual(c.to_pybel().formula, 'H2O') self.assertEqual(c.to_inchi(), self.h2o['inchi']) self.assertEqual(c.to_mol().split('\n')[2:], self.h2o['mol'].split('\n')[2:]) self.assertEqual(c.to_smiles(), self.h2o['smiles']) self.assertEqual( molecule_util.Molecule( structure=self.h2o['inchi'][6:]).get_format(), None) c = molecule_util.Molecule(structure=self.h2o['inchi'] + '\n') self.assertEqual(c.structure, self.h2o['inchi'] + '\n') c = molecule_util.Molecule(structure=self.h2o['smiles']) self.assertEqual(c.structure, self.h2o['smiles']) c = molecule_util.Molecule(structure=self.h2o['mol']) self.assertEqual(c.structure, self.h2o['mol'])
def calc_reactant_product_pairs(reaction): """ Get list of pairs of similar reactants and products using a greedy algorithm. Args: reaction (:obj:`data_model.Reaction`): reaction Returns: :obj:`list` of :obj:`tuple` of obj:`data_model.Specie`, :obj:`data_model.Specie`: list of pairs of similar reactants and products """ participants = reaction.get_ordered_participants() reactants = list(filter(lambda p: p.coefficient < 0, participants)) products = list(filter(lambda p: p.coefficient > 0, participants)) # sort by structure to ensure result is reproducible key = lambda p: (len(p.specie.structure), p.specie.structure) reactants = sorted(reactants, key=key, reverse=True) products = sorted(products, key=key, reverse=True) # create :obj:`molecule_util.Molecule` objects for each reactant and product reactant_mols = [ molecule_util.Molecule(structure=reactant.specie.structure) for reactant in reactants ] product_mols = [ molecule_util.Molecule(structure=product.specie.structure) for product in products ] # calculate similarities between each reactant and each product similarities = numpy.full((len(reactants), len(products)), numpy.nan) for i_reactant, reactant in enumerate(reactant_mols): for i_product, product in enumerate(product_mols): similarities[i_reactant, i_product] = reactant.get_similarity(product) # initialize pairs of similar reactants and products pairs = [] # iteratively identify the most similar pair of reactants and products for i in range(min(len(reactants), len(products))): index = numpy.argmax(similarities) indices = numpy.unravel_index(index, dims=similarities.shape) i_reactant = indices[0] i_product = indices[1] pairs.append((reactants[i_reactant], products[i_product])) reactants.pop(i_reactant) products.pop(i_product) similarities = numpy.delete(similarities, i_reactant, axis=0) similarities = numpy.delete(similarities, i_product, axis=1) # unpaired products, reactants for reactant in reactants: pairs.append((reactant, None)) for product in products: pairs.append((None, product)) return pairs
def test_get_fingerprint(self): adp = molecule_util.Molecule(structure=self.adp['smiles']) atp = molecule_util.Molecule(structure=self.atp['smiles']) self.assertEqual(adp.get_similarity(adp), 1.) numpy.testing.assert_almost_equal(adp.get_similarity(atp), 0.955, decimal=3) numpy.testing.assert_almost_equal(atp.get_similarity(adp), 0.955, decimal=3)
def get_similarity(self, other, fingerprint_type='fp2'): """ Calculate the similarity with another species Args: other (:obj:`Specie`): a second species fingerprint_type (:obj:`str`, optional): fingerprint type to use to calculate similarity Returns: :obj:`float`: the similarity with the other molecule """ self_mol = molecule_util.Molecule(structure=self.structure) other_mol = molecule_util.Molecule(structure=other.structure) return self_mol.get_similarity(other_mol, fingerprint_type=fingerprint_type)
def to_smiles(self): """ Get the structure in canonical SMILES format Returns: :obj:`str`: structure in canonical SMILES format """ return molecule_util.Molecule(structure=self.structure).to_smiles()
def to_pybel(self): """ Get the structure as a Pybel molecule Returns: :obj:`pybel.Molecule`: structure as a Pybel molecule """ return molecule_util.Molecule(structure=self.structure).to_pybel()
def to_openbabel(self): """ Get the structure as a Open Babel molecule Returns: :obj:`openbabel.OBMol`: structure as a Open Babel molecule """ return molecule_util.Molecule(structure=self.structure).to_openbabel()
def to_mol(self): """ Get the structure in .mol format Returns: :obj:`str`: structure in .mol format """ return molecule_util.Molecule(structure=self.structure).to_mol()
def test__run(self): m = AttrDict() for name, structure in self.molecules.items(): if structure: m[name] = molecule_util.Molecule(structure=structure).to_mol() else: m[name] = None # 1 --> 1: dr1p <==> d5rp print(m.dr1p) results = ezyme.Ezyme()._run([m.dr1p], [m.dr5p]) self.assertEqual(len(results), 1) self.assertEqual(results[0].__dict__, { 'ec_number': '5.4.2', 'score': 16.0 }) # 1 --> 2: f1p ==> glyceraldehyde + t3p2 self.assertEqual( ezyme.Ezyme()._run([m.f1p], [m.glyceraldehyde, m.t3p2])[0].ec_number, "2.2.1") # 2 --> 1 and example with stoichiometric coefficient != 1: H2O + LeuLeu ==> (2) LEU self.assertEqual( ezyme.Ezyme()._run([m.leuleu, m.h2o], [m.leu])[0].ec_number, "3.5.1") # 2 --> 2: t3p1 + s7p <==> x5p + r5p self.assertEqual( ezyme.Ezyme()._run([m.t3p1, m.s7p], [m.x5p, m.r5p])[0].ec_number, "4.1.2") # 3 --> 2: UDP + H + PEP ==> UTP + PYR self.assertEqual( ezyme.Ezyme()._run([m.utp, m.pep, m.h], [m.utp, m.pyr])[0].ec_number, '2.7.3') # 3 --> 2: metthf + gly + h2o <==> thf + ser self.assertEqual( ezyme.Ezyme()._run([m.metthf, m.gly, m.h2o], [m.thf, m.ser])[0].ec_number, '2.1.2') # 3 --> 4: IleIle[e] + ATP[c] + H2O[c] ==> IleIle[c] + PI[c] + H[c] + ADP[c] self.assertEqual( ezyme.Ezyme()._run([m.ileile, m.atp, m.h2o], [m.ileile, m.pi, m.adp])[0].ec_number, "3.6.1") # 4 --> 3: ATP + MET + H2O ==> AMET + PPI + PI + H self.assertEqual( ezyme.Ezyme()._run([m.met, m.atp, m.h2o], [m.amet, m.ppi, m.pi, m.h]), []) """ test that Ezyme predicts the same EC number for a reversible reaction when the substrates and products and swapped """ self.assertEqual(ezyme.Ezyme()._run([m.dr5p], [m.dr1p])[0].ec_number, '5.4.2') """ test that Ezyme returns None for partiicpants with no defined structure """ self.assertEqual(ezyme.Ezyme()._run([m.dr5p], [m.no_structure]), None)
def to_inchi(self, only_formula_and_connectivity=False): """ Get the structure in InChi format Args: only_formula_and_connectivity (:obj:`bool`): if :obj:`True`, return only the formula and connectivity layers Returns: :obj:`str`: structure in InChi format or just the formula and connectivity layers if :obj:`only_formula_and_connectivity` is :obj:`True` """ inchi = molecule_util.Molecule(structure=self.structure).to_inchi() if only_formula_and_connectivity: return molecule_util.InchiMolecule( inchi).get_formula_and_connectivity() else: return inchi
def parse_participants(side, coefficient, reaction, errors): for participant in side.split(' + '): participant = participant.strip() pubchem_compounds = pubchempy.get_compounds(participant, 'name') if len(pubchem_compounds) == 1: structure = pubchem_compounds[0].inchi elif molecule_util.Molecule(structure=participant).get_format(): structure = participant else: structure = '' errors.append(participant) reaction.participants.append(data_model.ReactionParticipant( specie=data_model.Specie(structure=structure), coefficient=coefficient, ))
def _default(self): structure = self.app.pargs.structure format = self.app.pargs.format print(molecule_util.Molecule(structure=structure).to_format(format))
def test_disable_warnings_openbabel(self): warning_util.disable_warnings() with CaptureOutput(termination_delay=0.1) as capturer: molecule_util.Molecule(structure=self.adp).to_inchi() self.assertEqual(capturer.get_text(), '')
def test_get_fingerprint(self): c = molecule_util.Molecule(structure=self.h2o['inchi']) self.assertIsInstance(c.get_fingerprint('fp2'), pybel.Fingerprint)