Пример #1
0
    def get_max_valence(atom_type):
        """
        Returning max. valence for atom of given type
        :param atom_type:
        :return:
        """

        if atom_type == "S":
            return 6
        else:
            return GetPeriodicTable().GetDefaultValence(
                GetPeriodicTable().GetAtomicNumber(atom_type))
Пример #2
0
    def __call__(self, mol, **kwargs):

        pt = GetPeriodicTable()
        g = [
            mol.GetNumAtoms(),
            mol.GetNumBonds(),
            sum([pt.GetAtomicWeight(a.GetAtomicNum())
                 for a in mol.GetAtoms()]),
        ]

        if self.allowed_charges is not None or self.solvent_environment is not None:
            try:
                feats_info = kwargs["extra_feats_info"]
            except KeyError as e:
                raise KeyError("{} `extra_feats_info` needed for {}.".format(
                    e, self.__class__.__name__))

            if self.allowed_charges is not None:
                g += one_hot_encoding(feats_info["charge"],
                                      self.allowed_charges)

            if self.solvent_environment is not None:
                # if only two solvent_environment, we use 0/1 to denote the feature
                if len(self.solvent_environment) == 2:
                    ft = self.solvent_environment.index(
                        feats_info["environment"])
                    g += [ft]
                # if more than two, we create a one-hot encoding
                else:
                    g += one_hot_encoding(feats_info["environment"],
                                          self.solvent_environment)

        feats = torch.tensor([g], dtype=getattr(torch, self.dtype))

        self._feature_size = feats.shape[1]
        self._feature_name = ["num atoms", "num bonds", "molecule weight"]
        if self.allowed_charges is not None:
            self._feature_name += ["charge one hot"] * len(
                self.allowed_charges)
        if self.solvent_environment is not None:
            if len(self.solvent_environment) == 2:
                self._feature_name += ["solvent"]
            else:
                self._feature_name += ["solvent"] * len(
                    self.solvent_environment)

        return {"feat": feats}
Пример #3
0
class Element:
    """
    Simple wrapper class for getting element info using RDKit.
    """

    pt = GetPeriodicTable()

    def mass(self, identifier):
        return self.pt.GetAtomicWeight(identifier)

    def number(self, identifier):
        return self.pt.GetAtomicNumber(identifier)

    def name(self, identifier):
        return self.pt.GetElementSymbol(identifier)
Пример #4
0
    def replace_atom(self, id, new_at_type):
        """
        Replacing the atom of given id with a new atom of given type
        :param id: id of atom that must be replaced
        :param new_at_type: type (atomic symbol) of the new atom
        :return:
        """

        # Changing atomic number
        self.mol_graph.GetAtomWithIdx(id).SetAtomicNum(
            GetPeriodicTable().GetAtomicNumber(new_at_type))

        # Setting formal charge to 0
        self.mol_graph.GetAtomWithIdx(id).SetFormalCharge(0)

        # Updating the internal representation
        self._update_mol_representation()
Пример #5
0
    "AROMATIC": "A",
    "UNSPECIFIED": "8",
}

charges = {
    0: 0,
    1: 3,
    2: 2,
    3: 1,
    4: 0,
    5: -1,
    6: -2,
    7: -3,
}

PERIODIC_TABLE = GetPeriodicTable()

#-----------------------------------------------------------------------------------------------------------------------


class MarvinJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if type(obj) == _ElementTree:
            return obj.getroot()
        if type(obj) == ObjectifiedElement:
            if obj.tag == 'cml':
                return obj.find('MDocument')
            if obj.tag == 'MDocument':
                return [el for el in obj.iterchildren(tag='MChemicalStruct')]
            if obj.tag == 'MChemicalStruct':
                return obj.find('molecule')
Пример #6
0
def GenerateRxnNet(initial_reactant, reaction_rules):
    """
    Generates reaction network following the algorithm from
    (Ind. Eng. Chem. Res. 2010, 49 (21), 10459-10470)

    Arguments:
    - initial_reactant:     can be smiles or mol
    - reaction_rules:       can be smarts string or rdkit.Chem.rdChemReactions.
                            ChemicalReaction object

    Return:
    - list of reaction intermediates

    Example:
    Ethane C-H scission and C-C scission
    A = generate_rxn_net('CC',['[C:1][H:2]>>[C:1].[H:2]',
                         '[C:1][C:2]>>[C:1].[C:2]'])
    for product in A:
        print(Chem.MolToSmiles(product))

    Tip:
    For dehydrogenation, you must number carbon and hydrogen i.e.
    [C][H]>>[C].[H]             (x)
    [C:1][H:1]>>[C:1].[H:2]     (o)
    Same goes for changing bond order
    [C][C]>>[C]=[C]             (x)
    [C:1][C:2]>>[C:1]=[C:2]     (o)
    Aromatization and Kekulize has some problem with several species. So, we
    don't set aromatization for sanitize, and try kekulize.

    TODO:
    - record reactions as well (make it an option as it's expensive)

    """

    # set-up reactants
    if not isinstance(initial_reactant, list):
        initial_reactant = [initial_reactant]
    if isinstance(initial_reactant[0], str):
        for i in range(0, len(initial_reactant)):
            initial_reactant[i] = Chem.MolFromSmiles(initial_reactant[i],
                                                     sanitize=False)
            # sanitize everything except aromatization set
            _sanitize_except_aromatization(initial_reactant[i])
    # Treatment necessary for radicals
    # (https://github.com/rdkit/rdkit/issues/69)
    for i in range(0, len(initial_reactant)):
        # print Chem.MolToSmiles(initial_reactant[i])
        initial_reactant[i] = Chem.AddHs(initial_reactant[i])
        # sanitize everything except aromatization set
        _sanitize_except_aromatization(initial_reactant[i])
        # Chem.SanitizeMol(initial_reactant[i])
        # Chem.Kekulize(initial_reactant[i])
        for atoms in initial_reactant[i].GetAtoms():
            atoms.SetNoImplicit(True)
        Chem.AssignRadicals(initial_reactant[i])

    # set up reactions
    if not isinstance(reaction_rules, list):
        reaction_rules = [reaction_rules]
    if isinstance(reaction_rules[0], str):
        for i in range(0, len(reaction_rules)):
            try:
                reaction_rules[i] = Read(reaction_rules[i])
            except Exception:
                reaction_rules[i] = ReactionFromSmarts(reaction_rules[i])

    # generator main algorithm
    unprocessed = initial_reactant
    processed = []
    while unprocessed:
        # Pop a molecule and put it in a processed list
        reactant0 = unprocessed[0]
        processed.insert(0, unprocessed[0])
        del unprocessed[0]
        # go through all reactions
        for reaction_rule in reaction_rules:
            # set up reactant list.
            # Generate combinatorial product list of reactants if reaction
            # requires several reactants
            reactant_list = itpd([list(range(1, len(processed)))],
                                 repeat=reaction_rule.
                                 GetNumReactantTemplates()-1)
            # go through each set of reactants
            for reactant_indexes in reactant_list:
                # Reaction
                # Make the reactant mol tuple (Runreactants only accept tuple)
                reactants = (reactant0,)
                for reactant_index in reactant_indexes:
                    reactants += (processed[reactant_index],)
                # React
                ele_reactions = reaction_rule.RunReactants(reactants)

                # Record reactions (TODO)

                # Pre-processing products
                # Go through reactiosn and make a single list of products
                products = []
                for ele_reaction in ele_reactions:
                    for mol in ele_reaction:
                        products.append(mol)
                # Treatment necessary for radicals
                # (https://github.com/rdkit/rdkit/issues/69)
                for mol in products:
                    for atoms in mol.GetAtoms():
                        atoms.SetNoImplicit(True)
                        atoms.UpdatePropertyCache(strict=False)
                    Chem.AssignRadicals(mol)
                    # Remove molecule with atoms with over valence
                for i in range(len(products)-1, -1, -1):
                    for atoms in products[i].GetAtoms():
                        if PeriodicTable.GetDefaultValence(GetPeriodicTable(),
                                                           atoms.GetAtomicNum()
                                                           ) < \
                             atoms.GetTotalValence():
                            del products[i]
                            break
                # remove duplicates
                # TODO. This removes also species with different charges
                for i in range(len(products)-1, -1, -1):
                    for j in range(0, i):
                        if products[i].GetNumAtoms() ==\
                            products[j].GetNumAtoms() and \
                            products[i].GetNumAtoms() ==\
                                len(products[i].
                                    GetSubstructMatch(products[j])):

                            del products[i]
                            break
                # update unprocessed molecule list
                # check for duplicate and append to unprocessed_list if missing
                for mol1 in products:
                    inthelist = 0
                    for mol2 in processed:
                        # first check the nubmer of atoms and then
                        # look for substructure match
                        if mol1.GetNumAtoms() == mol2.GetNumAtoms() and \
                            mol1.GetNumAtoms() == len(mol1.GetSubstructMatch
                                                      (mol2)):
                            # if it's in processed list, break
                            inthelist = 1
                            break
                    # not in the processed list. append to unprocessed
                    if inthelist == 0:
                        unprocessed.insert(0, mol1)
    # Prettify
    for i in range(0, len(processed)):
        # print Chem.MolToSmiles(processed[i])
        processed[i] = Chem.RemoveHs(processed[i], sanitize=False)
        _sanitize_except_aromatization(processed[i])
        # print Chem.MolToSmiles(processed[i])
    return processed
Пример #7
0
def get_atom_symbol(atomic_number):
    return PT.GetElementSymbol(GetPeriodicTable(), atomic_number)
Пример #8
0
## skchem.core.atom

Defining atoms in scikit-chem.
"""

import numpy as np
import pandas as pd

from rdkit import Chem
from rdkit.Chem.rdchem import GetPeriodicTable
from rdkit.Chem.AtomPairs.Utils import NumPiElectrons

from .base import ChemicalObject, PropertyView, ChemicalObjectView
from ..resource import PERIODIC_TABLE

RD_PT = GetPeriodicTable()


class Atom(Chem.rdchem.Atom, ChemicalObject):
    """ Object representing an Atom in scikit-chem. """
    @property
    def index(self):
        """ int: the index of the atom. """

        return self.GetIdx()

    @property
    def owner(self):
        """ skchem.Mol: the owning molecule.

        Warnings:
Пример #9
0
 def p_table() -> PeriodicTable:
     return GetPeriodicTable()