Example #1
0
def detectChiralCenters(mol, atom_types=None):
    """
    Detect chiral centers

    Parameters
    ----------
    mol: Molecule
        Molecule to detect chiral centers

    Return
    ------
    results: List of tuples
        List of chircal centers, where the chiral centers are tuples made of an atom index and a label ('S', 'R', '?').

    Examples
    --------
    >>> from parameterize.home import home
    >>> from moleculekit.molecule import Molecule

    >>> molFile = os.path.join(home('test-param'), 'H2O2.mol2')
    >>> mol = Molecule(molFile)
    >>> detectChiralCenters(mol)
    []

    >>> molFile = os.path.join(home('test-param'), 'fluorchlorcyclopronol.mol2')
    >>> mol = Molecule(molFile)
    >>> detectChiralCenters(mol)
    [(0, 'R'), (2, 'S'), (4, 'R')]

    >>> molFile = os.path.join(home('test-param'), 'fluorchlorcyclopronol.mol2')
    >>> mol = Molecule(molFile)
    >>> detectChiralCenters(mol, atom_types=mol.atomtype)
    [(0, 'R'), (2, 'S'), (4, 'R')]
    """

    from moleculekit.molecule import Molecule
    from moleculekit.smallmol.smallmol import SmallMol
    from rdkit.Chem import AssignAtomChiralTagsFromStructure, FindMolChiralCenters

    if not isinstance(mol, Molecule):
        raise TypeError('"mol" has to be instance of {}'.format(Molecule))
    if mol.numFrames != 1:
        raise ValueError('"mol" can have just one frame, but it has {}'.format(
            mol.numFrames))

    # Set atom types, overwise rdkit refuse to read some MOL2 files
    htmd_mol = mol.copy()
    if atom_types is not None:
        htmd_mol.atomtype = atom_types

    # Detect chiral centers and assign their labels
    sm = SmallMol(htmd_mol,
                  fixHs=False,
                  removeHs=False,
                  verbose=False,
                  force_reading=True)
    AssignAtomChiralTagsFromStructure(sm._mol)
    chiral_centers = FindMolChiralCenters(sm._mol, includeUnassigned=True)

    return chiral_centers
Example #2
0
def detectChiralCenters(mol):
    """
    Detect chiral centers

    Parameters
    ----------
    mol: Molecule
        Molecule to detect chiral centers

    Return
    ------
    results: List of tuples
        List of chircal centers, where the chiral centers are tuples made of an atom index and a label ('S', 'R', '?').

    Examples
    --------
    >>> from htmd.home import home
    >>> from htmd.molecule.molecule import Molecule

    >>> molFile = os.path.join(home('test-param'), 'H2O2.mol2')
    >>> mol = Molecule(molFile)
    >>> detectChiralCenters(mol)
    []

    >>> molFile = os.path.join(home('test-param'), 'fluorchlorcyclopronol.mol2')
    >>> mol = Molecule(molFile)
    >>> detectChiralCenters(mol)
    [(0, 'R'), (2, 'S'), (4, 'R')]
    """

    from htmd.molecule.molecule import Molecule
    from rdkit.Chem import MolFromMol2File, AssignAtomChiralTagsFromStructure, FindMolChiralCenters

    if not isinstance(mol, Molecule):
        raise TypeError('"mol" has to be instance of {}'.format(Molecule))
    if mol.numFrames != 1:
        raise ValueError('"mol" can have just one frame, but it has {}'.format(
            mol.numFrames))

    # Set atom types to elements, overwise rdkit refuse to read a MOL2 file
    htmd_mol = mol.copy()
    htmd_mol.atomtype = htmd_mol.element

    # Convert Molecule to rdkit Mol
    with TemporaryDirectory() as tmpDir:
        filename = os.path.join(tmpDir, 'mol.mol2')
        htmd_mol.write(filename)
        rdkit_mol = MolFromMol2File(filename, removeHs=False)
    assert mol.numAtoms == rdkit_mol.GetNumAtoms()

    # Detect chiral centers and assign their labels
    AssignAtomChiralTagsFromStructure(rdkit_mol)
    chiral_centers = FindMolChiralCenters(rdkit_mol, includeUnassigned=True)

    return chiral_centers
def open_molecule_file(uploadedfile, logfile=os.devnull, filetype=None):

    #charset = 'utf-8'
    #if "charset" in uploadedfile and uploadedfile.charset is not None:
    #charset = uploadedfile.charset
    if filetype is None:
        if "filetype" not in uploadedfile or uploadedfile.filetype is None:
            basename, ext = os.path.splitext(uploadedfile.name)
            ext = ext.lower()
            ext = ext.strip('.')
            if ext in MOLECULE_EXTENSION_TYPES.keys():
                filetype = MOLECULE_EXTENSION_TYPES[ext]
                uploadedfile.filetype = filetype
            else:
                raise InvalidMoleculeFileExtension(ext=ext)

        else:
            filetype = uploadedfile.filetype

    with stdout_redirected(to=logfile, stdout=sys.stderr):
        with stdout_redirected(to=logfile, stdout=sys.stdout):
            print('Loading molecule...')
            uploadedfile.seek(0)
            if filetype == 'sdf' or filetype == 'mol':

                suppl = ForwardSDMolSupplier(uploadedfile, removeHs=False)
                mol = next(suppl)
                try:
                    next(suppl)
                except StopIteration:
                    pass
                except:
                    raise
                else:
                    raise MultipleMoleculesinSDF()
                finally:
                    del suppl
                if mol is None:
                    if filetype == 'sdf':
                        raise ParsingError("Invalid SDFile file.")
                    else:
                        raise ParsingError("Invalid MDL Mol file.")
            print('Assigning chirality from struture...')
            AssignAtomChiralTagsFromStructure(mol, replaceExistingTags=False)
            print('Finished loading molecule.')

    return mol
Example #4
0
def _assignAtomChiralTagsFromStructure(mol):
    return AssignAtomChiralTagsFromStructure(mol)