import rdkit
from rdkit import Chem

def inorg_or_mix(infile):
    smarts_def = "[!#1;!#6;!#7;!#8;!#9;!#16;!#17;!#35;!#53]"
    # Not sure why the following def does not behave...
    #smarts_def = "[!H;!C;!N;!O;!F;!S;!Cl;!Br;!I]"
    patt = Chem.MolFromSmarts(smarts_def)

    suppl = Chem.SDMolSupplier(infile)
    outname = os.path.basename(infile).split('.')[0] + '_annotate.sdf'
    outfile = os.path.join(path, outname)
    writer = Chem.SDWriter(outfile)

    for mol in suppl:
        if mol is None:
            continue

        # Check if we have a multi-component molecule
        components = len(rdkit.Chem.GetMolFrags(mol))

        # and check if we have any undesired atom-type matches
        organic = 1
        if mol.HasSubstructMatch(patt):
            organic = 0
        
        mol.SetProp('components', str(components))
        mol.SetProp('organic', str(organic))
        writer.write(mol)
    writer.flush()
        
        
if __name__ == "__main__":
    import sys, os
    path = os.getcwd()
    infile = os.path.abspath(sys.argv[1])
    
    inorg_or_mix(infile)
