#! /bin/bash
# This scripts take all SMILES files in the folder and removes all chiral
# marks from them. It is intended to be applied to entries with
# chiral moieties containing one single chiral mark
# and crystallised in non-chiral space groups, hence forming racemic crystals.
# Removing chiral marks for these compounds is equivalent to racemize them.

for i in `ls *.smi` ; do
   cat $i | grep @ >/dev/null && {   
     tmp=`cat $i | cut -f1`
     key=`cat $i | cut -f2`
     cat $i
     while echo $tmp | grep [[]C@[]] > /dev/null; do   
       tmp=`echo $tmp | sed -e s/[[]C@[]]/C/`
     done
     while echo $tmp | grep [[]C@@[]] > /dev/null; do   
       tmp=`echo $tmp | sed -e s/[[]C@@[]]/C/`
     done
     while echo $tmp | grep [[]C@H[]] > /dev/null; do   
       tmp=`echo $tmp | sed -e s/[[]C@H[]]/C/`
     done
     while echo $tmp | grep [[]C@@H[]] > /dev/null; do   
       tmp=`echo $tmp | sed -e s/[[]C@@H[]]/C/`
     done
     while echo $tmp | grep @ > /dev/null; do   
       tmp=`echo $tmp | sed -e s/@//`
     done
     echo -n $tmp > temporal
     echo -e "\t"$key >> temporal
     rm -f $i
     mv temporal $i
   }
done
