#!/bin/sh

if test "$1" = ""
then echo "Please enter input file name (e.g., orphans). Must be a list of RNA accession numbers."
     read file
else file=$1
fi

rm -f Synchronized_annotations_from_$file
for old in `cat $file`
do rm -f out out2
   ID=''
   new=''
   echo " *** Now working on old = "$old" ***"
   wget -O out http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=nucleotide\&term=Homo+sapiens+[orgn]+$old
   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=nucleotide\&id=$id
      add_new=`grep -A 1 '^ *gene {$' out2 | tail -1 | sed -e 's|^ *locus "||' -e 's|",||'`
      new=`echo $new $add_new`
   done
   echo $old $new >> Synchronized_annotations_from_$file
   sleep 0.5
done

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