Ejemplo n.º 1
0
 def test_ob_round_trip(self):
     """Test conversion to and from OBMol"""
     for mol in self.test_mols:
         ob_mol = to_ob_mol(mol)
         new_mol = from_ob_mol(Molecule(), ob_mol)
         self.assertTrue(mol.is_isomorphic(new_mol) or self.test_Hbond_free_mol.is_isomorphic(new_mol))
         self.assertEqual(mol.get_element_count(), new_mol.get_element_count())
Ejemplo n.º 2
0
def xyz_to_mol(xyz):
    if isinstance(xyz, dict):
        string = xyz_to_xyz_file_format(xyz)
    elif isinstance(xyz, str):
        if not xyz[0].isdigit():
            atom_num = len(xyz.splitlines())
            string = f'{atom_num}\n\n' + xyz
        else:
            string = xyz
    elif os.path.isfile(xyz):
        string = xyz_to_xyz_file_format(parse_xyz_from_file(xyz))
    else:
        raise ValueError(f'Invalid xyz input, got: {xyz}')
    molecule = pybel.readstring('xyz', string)
    mol = Molecule()
    from_ob_mol(mol, molecule.OBMol)
    return mol
Ejemplo n.º 3
0
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()
        # In OpenBabel 3+ the function obmol.AssignSpinMultiplicity(True) does nothing.
        # We could write our own method here and call obatom.SetSpinMultiplicity on
        # each atom, but instead we will leave them blank for now and fix them 
        # in the from_ob_mol() method.
        if mol is None:
            mol = mm.Molecule()
        output = from_ob_mol(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 = to_ob_mol(input_object)
        output = ob_conversion.WriteString(obmol).strip()
    else:
        raise ValueError('Unexpected input format. Should be a Molecule or a string.')

    return output
def toRMGmol(OBMol):
    rmg_mol = from_ob_mol(rmgpy.molecule.molecule.Molecule(), OBMol)
    return rmg_mol