def test_ob_round_trip(self): """Test conversion to and from OBMol""" for mol in self.test_mols: ob_mol = toOBMol(mol) new_mol = fromOBMol(Molecule(), ob_mol) self.assertTrue(mol.isIsomorphic(new_mol)) self.assertEqual(mol.get_element_count(), new_mol.get_element_count())
def _openbabel_translator(input_object, identifier_type, mol=None): """ Converts between formats using OpenBabel. If input is a :class:`Molecule`, the identifier_type is used to determine the output type. If the input is a `str`, then the identifier_type is used to identify the input, and the desired output is assumed to be a :class:`Molecule` object. Args: input_object: either molecule or string identifier identifier_type: format of string identifier 'inchi' -> InChI 'inchikey' -> InChI Key 'smi' -> SMILES mol: molecule object for output (optional) """ ob_conversion = openbabel.OBConversion() if isinstance(input_object, str): # We are converting from a string identifier to a Molecule ob_conversion.SetInFormat(identifier_type) obmol = openbabel.OBMol() ob_conversion.ReadString(obmol, input_object) obmol.AddHydrogens() obmol.AssignSpinMultiplicity(True) if mol is None: mol = mm.Molecule() output = fromOBMol(mol, obmol) elif isinstance(input_object, mm.Molecule): # We are converting from a Molecule to a string identifier if identifier_type == 'inchi': ob_conversion.SetOutFormat('inchi') ob_conversion.AddOption('w') elif identifier_type == 'inchikey': ob_conversion.SetOutFormat('inchi') ob_conversion.AddOption('w') ob_conversion.AddOption('K') elif identifier_type == 'smi': ob_conversion.SetOutFormat('can') # turn off isomer and stereochemistry information ob_conversion.AddOption('i') else: raise ValueError( 'Unexpected identifier type {0}.'.format(identifier_type)) obmol = toOBMol(input_object) output = ob_conversion.WriteString(obmol).strip() else: raise ValueError( 'Unexpected input format. Should be a Molecule or a string.') return output
def _openbabel_translator(input_object, identifier_type, mol=None): """ Converts between formats using OpenBabel. If input is a :class:`Molecule`, the identifier_type is used to determine the output type. If the input is a `str`, then the identifier_type is used to identify the input, and the desired output is assumed to be a :class:`Molecule` object. Args: input_object: either molecule or string identifier identifier_type: format of string identifier 'inchi' -> InChI 'inchikey' -> InChI Key 'smi' -> SMILES mol: molecule object for output (optional) """ ob_conversion = openbabel.OBConversion() if isinstance(input_object, str): # We are converting from a string identifier to a Molecule ob_conversion.SetInFormat(identifier_type) obmol = openbabel.OBMol() ob_conversion.ReadString(obmol, input_object) obmol.AddHydrogens() obmol.AssignSpinMultiplicity(True) if mol is None: mol = mm.Molecule() output = fromOBMol(mol, obmol) elif isinstance(input_object, mm.Molecule): # We are converting from a Molecule to a string identifier if identifier_type == 'inchi': ob_conversion.SetOutFormat('inchi') ob_conversion.AddOption('w') elif identifier_type == 'inchikey': ob_conversion.SetOutFormat('inchi') ob_conversion.AddOption('w') ob_conversion.AddOption('K') elif identifier_type == 'smi': ob_conversion.SetOutFormat('can') # turn off isomer and stereochemistry information ob_conversion.AddOption('i') else: raise ValueError('Unexpected identifier type {0}.'.format(identifier_type)) obmol = toOBMol(input_object) output = ob_conversion.WriteString(obmol).strip() else: raise ValueError('Unexpected input format. Should be a Molecule or a string.') return output