#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar  7 21:04:15 2017

@author: Aretas
"""

#numerate the scaffolds
#find molecules with chiral centres
#connect smiles and chirality information to a dictionary
#bind the chiral C atom number to variable
#replace C with [C@] and C[@@]
#bind both version to variables and add to the respective keys in the final dictionary
#writes enantiomer SMILES to a .txt file together with IUPAC names, scaffold number
#ONLY deals with molecules with carbon atoms as chiral centres
#and only upto 3 chiral centres per a ligand molecule
#the script assumes that one of the covalently bound atoms to tetrahedral carbon is hydrogen
#if a none of the groups around the chiral centre are hydrogen; the script won't work

import sys, re

list_molecules = open (sys.argv[1]).readlines()  #text file with IUPAC names and SMILES
info_chirality = open (sys.argv[2]).readlines()  #chirality info from obchiral chi_info2.txt

dictionary_smiles = {} # ligand number : SMILES
chir_dict = {}         # ligand number : chiral carbon atom(s)
eta1 = "[C@H]"        # string to mark chirality in SMILES
eta2 = "[C@@H]"
enantiomer_dict = {} #enantiomer dicitonary

for line in list_molecules:
    f = line.split()            #!!!!!!! change these according to your preference
    dictionary_smiles[f[0]] = f[4] #modify these numbers depending on the position of SMILES and IUPAC name in the line of the input

for line in info_chirality: #obchiral input
    match1 = re.search(r'Molecule (.*): Atom ([0-9]+) Is Chiral C3',line)
    match2 = re.search(r'Molecule (.*): Atom ([0-9]+) Is Chiral C3; Atom ([0-9]+) Is Chiral C3',line)
    match3 = re.search(r'Molecule (.*): Atom ([0-9]+) Is Chiral C3; Atom ([0-9]+) Is Chiral C3; Atom ([0-9]+) Is Chiral C3',line)

    if match1:
        mol_no1 = match1.groups()[0]  #ligand number
        atom_no1 = match1.groups()[1]   #position of chiral carbon atom
        chir_dict [mol_no1] = atom_no1  #creating a new entry in the dictionary

    if match2:
        mol_no2 = match2.groups()[0]
        atom_no2 = match2.groups()[1]
        atom_no21 = match2.groups()[2]
        chir_dict [mol_no2] = atom_no2 + " " + atom_no21

    if match3:
        mol_no3 = match3.groups()[0]
        atom_no3 = match3.groups()[1]
        atom_no31 = match3.groups()[2]
        atom_no32 = match3.groups()[3]
        #print (mol_no3, atom_no3, atom_no31, atom_no32)
        chir_dict [mol_no3] = atom_no3 + " " + atom_no31 + " " + atom_no32

#print (chir_dict)

for i in chir_dict.keys():
    dictionary_smiles [i] += ", " + chir_dict[i]
    #adds position of chiral atoms to the key (ligand number) value (SMILES)
    #dictionary_smiles now looks like this: dictionary_smiles [1] = SMILES, 3, 5

##creating enantiomers with eta1 and eta2

for i in chir_dict.keys():  #ligand number in the library
    match_a = re.search(r'(.*), (.*)', dictionary_smiles[i]) #search the key value
    if match_a:
        m_obj1 = match_a.groups()[0]  #SMILES
        m_obj2 = match_a.groups()[1]  #chiral information

        f = m_obj2.split()  #splits chiral information

        count1 = len(f)  #count the number of chiral centres

        if count1 == 3:     #if there are three chiral centres
            first_cc = f[0] #set the position of chiral centres as variables
            second_cc = f[1]
            third_cc = f[2]

            new_string1 = "" #empty strings for each enantiomer
            new_string2 = ""
            new_string3 = ""
            new_string4 = ""
            new_string5 = ""
            new_string6 = ""