#!usr/bin/perl -w
#	
# By J. Deegan 
# November 2007
#
# This script can be used when you have a whole load of terms with, for example, 'lung'
# in the term name, and you want to add an identical synonym to each with 'pulmonary'
# instead of lung. 
#
# You will need a tab delimited file containing the term ids and names of the terms to which you
# want to add synonyms.
#
# You will also need to replace the id prefix and substitutions below with your choice. 


use strict;
my %idhash;
my $id;
my $term_id_of_interest;
my $term_name_of_interest;
my $replacement;
my %hash_of_numbers = ();      
my $interested;
my $line;

# Open the numbers file for reading
# This file should have two columns, the first is the id of each term
# and the second is the name of the term.
open (TERMS, "numbermap.txt") or die "can't open GO term file \n";

# This section puts the ids and names next to each other in a big hash table. 
while (my $line=<TERMS>){
	chomp $line;
	my @linecols = split(/\t/, $line);
	$idhash{$linecols[0]}=$linecols[1];
	}

# Print out the hash table and close the file.
foreach my $id (keys %idhash){print ("$id\t$idhash{$id}\n")}
close FILE;	

#open the ontology file for reading.
open (FILE, "lung.obo") || die "Can't open lung.obo.\n";

# The $interested variable stores the fact that the computer is either looking at a term listed
# in the hash table or not. When we find a term listed in the hash table we change 
# $interested from o to 1.

$interested=0;
while(<FILE>){
		$line = $_;
		if ($interested==0){ 
			print $line;
			# if the line contains a GO:id that is listed in the hash table....
			if (($line=~m/^id: (LG:[0-9]+)$/)           
					&& (defined $idhash{$1}))   {
					$interested = 1;

					# put the term name and id (from the hash table) into two new variables. 						 
					$term_id_of_interest = $1;
					$term_name_of_interest = $idhash{$1};
					#print "$term_id_of_interest\n";
					#print "$term_name_of_interest\n";

					# swap 'lung' for 'pulmonary'.		
					$term_name_of_interest=~s/lung/pulmonary/;
						
				}
			} else {

				# If the line does not contain a GO:id  and is one of the stanza lines that comes 
				# before the synonym lines in the stanza then just print it. 		
				if ($line=~m/^((is_anonymous)|(name)|(alt_id)|(namespace)|(def)): /){
				 print $line;
				} else {
					# if we are on an interesting term and we have reached the synonym section of the stanza then
					# print the new synonym line. 
					print "synonym:  \"$term_name_of_interest\" EXACT []\n" ;
		
					#now stop being interested until another GO:id from the numbers file appears.
					$interested=0;
					# Print all other lines in the stanza.
					print $line;
					}
		
		#this is the end of the block that started with deciding whether or not we were interested in the line.
		}
#end of while
}





