#!/bin/sh

if test "$1" = ""
then echo "Please enter input file name (e.g., CLIP_names_absent_from_TargetScan). Must be a list of gene names."
     read file
else file=$1
fi

#echo "Gene Synonym" > Synchronized_annotations_from_$file # To complete after interruption of the run
#for old in `sed 's|,| |g' $file`
for old in `sed 's|,| |g' $file | tail -n +2080`
do rm -f out out2
   ID=''
   new=''
#   echo " *** Now working on old = "$old" ***"
   if test `echo $old | grep -c '^gi|'` -eq 0 # exclude all these things which are no proper gene names
   then wget -O out http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene\&term=Mus+musculus+[orgn]+\"$old\"[gene]
        ID=`grep '^<Id>' out | sed -e 's|^<Id>||' -e 's|</Id>||'`
        sleep 0.5
#        echo "    *** ID="$ID" ***"
        for id in `echo $ID`
        do wget -O out2 http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=gene\&id=$id
           for new in `sed -e '/^  gene {/,/^  prot {/ !d' -e '/^ *syn {/,/^ *}/ !d' out2 | grep -v '^ *syn {' | grep -v '^ *}' | sed -e 's|^ *||' -e 's|"||g ' -e 's|,$||'`
           do echo $old $new >> Synchronized_annotations_from_$file
           done
           sleep 0.5
        done
   fi
done

echo "Done. Output file is named 'Synchronized_annotations_from_"$file"'."
