I couldn't figure out how to do this in a straightforward manner, so I went
full caveman. The following code isn't pretty, but seems to do the job:

from rdkit import Chem
from rdkit.Chem import AllChem
import itertools

smiles1 = "C([O-])([O-])=O.[Na+].[Na+]"
smiles2 = "[Cl-].[Li+].C(C)(C)[Mg]Cl"

rxn1 =
AllChem.ReactionFromSmarts('[Pd,Cu,Fe,N,Na,K,Li,Cs,Mg;+;!v4:1].[#1,F,Cl,Br,I,B,C,c,O,N,n;-;!v4:2]>>[*+0:1]-[*+0:2]')
rxn2 =
AllChem.ReactionFromSmarts('[Pd,Cu,Fe,Mg;+2;!v4:1].[#1,F,Cl,Br,I,B,C,c,O,N,n;-1;!v4:2]>>[*+1:1]-[*+0:2]')

neutrals = []
charged = []
smiles = smiles1 #smiles2
frags = smiles.split('.')
for frag in frags:
    mol = Chem.MolFromSmiles(frag)
    charge = Chem.GetFormalCharge(mol)
    if charge == 0:
        neutrals.append(mol)
    else:
        charged.append(mol)

while len(charged) > 1:
    copy = charged.copy()
    parent = copy.pop()
    for mol in copy:
        prods = rxn1.RunReactants((parent,mol))
        if not prods:
            prods = rxn1.RunReactants((mol,parent))
            if not prods:
                prods = rxn2.RunReactants((mol,parent))
                if not prods:
                    prods = rxn2.RunReactants((parent,mol))
        if(prods):
            charged.remove(mol)
            nmol = prods[0][0]
            Chem.SanitizeMol(nmol)
            charge = Chem.GetFormalCharge(nmol)
            if charge == 0:
                neutrals.append(nmol)
            else:
                charged.append(nmol)
            break
    charged.remove(parent)

smiles = ".".join([Chem.MolToSmiles(x) for x in neutrals+charged])
print(smiles)



On Mon, Feb 22, 2021 at 7:41 PM Good Eats <goodeat...@gmail.com> wrote:

> Hi all,
>
> I am trying to convert ionic salts into their covalent forms. I devised a
> reaction transformation to do this, but am running into problems getting to
> work correctly.
> e.g.   C([O-])([O-])=O.[Na+].[Na+]   -->  C([O][Na])([O][Na])=O
>          [Cl-].[Li+].C(C)(C)[Mg]Cl    -->    [Cl][Li].C(C)(C)[Mg]Cl
>
> If I feed the disconnected form of NaHCO3 into the code below, it works
> okay. However once I go to a salt with more than two disconnected
> components (like Na2CO3), only the first bond is formed and the other
> sodium ion vanishes.
> Similarly if there are other neutral fragments in the input (see mol2 in
> the code), those fragments also vaish when I run the reaction.
>
> I think I must be missing something in my reaction SMARTS. Anyone have
> some pointers for me?
>
> Many thanks!
>
>
> from rdkit import Chem
> from rdkit.Chem import AllChem
>
> mol1 = Chem.MolFromSmiles("C([O-])([O-])=O.[Na+].[Na+]")
> mol2 = Chem.MolFromSmiles("[Cl-].[Li+].C(C)(C)[Mg]Cl")
> rxn =
> AllChem.ReactionFromSmarts('([N,Na,K,Li,Cs,Mg;+,++:1].[H,F,Cl,Br,I,B,C,c,O,N,n;-:2])>>[*+0:1]-[*+0:2]')
>
> prods = rxn.RunReactants((mol1,))
> for prod in prods:
> print(Chem.MolToSmiles(prod[0]))
>
> prods = rxn.RunReactants((mol2,))
> for prod in prods:
> print(Chem.MolToSmiles(prod[0]))
>
> Output:
> O=C([O-])O[Na]
> O=C([O-])O[Na]
> O=C([O-])O[Na]
> O=C([O-])O[Na]
> [Li]Cl
>
_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to