# Path: src/python/pipelines/rdkit/sanify_utils.py
# def enumerateStereoIsomers(mol):
#     out = []
#     chiralCentres = Chem.FindMolChiralCenters(mol, includeUnassigned=True)
#     #return the molecule object when no chiral centres where identified
#     if chiralCentres == []:
#         return [mol]
# 
#     #All bit permutations with number of bits equals number of chiralCentres
#     elements = _spam(len(chiralCentres))
# 
#     for isoId,element in enumerate(elements):
#         for centreId,i in enumerate(element):
#             atomId = chiralCentres[centreId][0]
#             if i == 0:
#                 mol.GetAtomWithIdx(atomId).SetChiralTag(Chem.rdchem.ChiralType.CHI_TETRAHEDRAL_CW)
#             elif i == 1:
#                 mol.GetAtomWithIdx(atomId).SetChiralTag(Chem.rdchem.ChiralType.CHI_TETRAHEDRAL_CCW)
#         outmol = copy(mol)
#         utils.log("Enumerated ", Chem.MolToSmiles(mol, isomericSmiles=True))
#         out.append(outmol)
#     return out
# 
# def enumerateTautomers(mol):
#     """
#     Get all of the Tautomers of a given molecule
#     :param mol: the input molecule
#     :return: a list of Tautomers
#     """
#     smiles = Chem.MolToSmiles(mol,isomericSmiles=True)
#     tauts = enumerate_tautomers_smiles(smiles)
#     ##TODO Append Parent molecule name
#     return  [Chem.MolFromSmiles(x) for x in tauts]
# 
# STANDARD_MOL_METHODS = {"molvs": molVsStandardizer, "flatkinson": flatkinsonStandardizer}


import sys, argparse
from pipelines_utils import parameter_utils, utils
from pipelines_utils_rdkit import rdkit_utils
from rdkit import Chem
from rdkit.Chem import AllChem
from .sanify_utils import enumerateStereoIsomers,enumerateTautomers,STANDARD_MOL_METHODS
    ]

    oformat = utils.determine_output_format(args.outformat)

    input,output,suppl,writer,output_base = rdkit_utils. \
        default_open_input_output(args.input, args.informat, args.output,
                                  'sanifier', args.outformat,
                                  thinOutput=False, valueClassMappings=clsMappings,
                                  datasetMetaProps=datasetMetaProps,
                                  fieldMetaProps=fieldMetaProps)


    i=0
    count=0
    errors=0
    for mol in suppl:
        i +=1
        if mol is None: continue

        if args.standardize:
            # we keep the original UUID as there is still a 1-to-1 relationship between the input and outputs
            oldUUID = mol.GetProp("uuid")
            inputCanSmiles = Chem.MolToSmiles(mol, isomericSmiles=True, canonical=True)
            try:
                std = getStandardMolecule(mol)
                outputCanSmiles = Chem.MolToSmiles(std, isomericSmiles=True, canonical=True)
                if oldUUID:
                    std.SetProp("uuid", oldUUID)
                #utils.log("Standardized", i, inputCanSmiles, ">>", outputCanSmiles)
                if inputCanSmiles == outputCanSmiles:

# Path: src/python/pipelines/rdkit/sanify_utils.py
# def enumerateStereoIsomers(mol):
#     out = []
#     chiralCentres = Chem.FindMolChiralCenters(mol, includeUnassigned=True)
#     #return the molecule object when no chiral centres where identified
#     if chiralCentres == []:
#         return [mol]
# 
#     #All bit permutations with number of bits equals number of chiralCentres
#     elements = _spam(len(chiralCentres))
# 
#     for isoId,element in enumerate(elements):
#         for centreId,i in enumerate(element):
#             atomId = chiralCentres[centreId][0]
#             if i == 0:
#                 mol.GetAtomWithIdx(atomId).SetChiralTag(Chem.rdchem.ChiralType.CHI_TETRAHEDRAL_CW)
#             elif i == 1:
#                 mol.GetAtomWithIdx(atomId).SetChiralTag(Chem.rdchem.ChiralType.CHI_TETRAHEDRAL_CCW)
#         outmol = copy(mol)
#         utils.log("Enumerated ", Chem.MolToSmiles(mol, isomericSmiles=True))
#         out.append(outmol)
#     return out
# 
# def enumerateTautomers(mol):
#     """
#     Get all of the Tautomers of a given molecule
#     :param mol: the input molecule
#     :return: a list of Tautomers
#     """
#     smiles = Chem.MolToSmiles(mol,isomericSmiles=True)
#     tauts = enumerate_tautomers_smiles(smiles)
#     ##TODO Append Parent molecule name
#     return  [Chem.MolFromSmiles(x) for x in tauts]
# 
# STANDARD_MOL_METHODS = {"molvs": molVsStandardizer, "flatkinson": flatkinsonStandardizer}


# Path: src/python/pipelines/rdkit/sanifier.py
import sys, argparse
from pipelines_utils import parameter_utils, utils
from pipelines_utils_rdkit import rdkit_utils
from rdkit import Chem
from rdkit.Chem import AllChem
from .sanify_utils import enumerateStereoIsomers,enumerateTautomers,STANDARD_MOL_METHODS

    ]

    oformat = utils.determine_output_format(args.outformat)

    input,output,suppl,writer,output_base = rdkit_utils. \
        default_open_input_output(args.input, args.informat, args.output,
                                  'sanifier', args.outformat,
                                  thinOutput=False, valueClassMappings=clsMappings,
                                  datasetMetaProps=datasetMetaProps,
                                  fieldMetaProps=fieldMetaProps)


    i=0
    count=0
    errors=0
    for mol in suppl:
        i +=1
        if mol is None: continue

        if args.standardize:
            # we keep the original UUID as there is still a 1-to-1 relationship between the input and outputs
            oldUUID = mol.GetProp("uuid")
            inputCanSmiles = Chem.MolToSmiles(mol, isomericSmiles=True, canonical=True)
            try:
                std = getStandardMolecule(mol)
                outputCanSmiles = Chem.MolToSmiles(std, isomericSmiles=True, canonical=True)
                if oldUUID:
                    std.SetProp("uuid", oldUUID)
                #utils.log("Standardized", i, inputCanSmiles, ">>", outputCanSmiles)
                if inputCanSmiles == outputCanSmiles:

                    std.SetProp("Standardized", "False")