#!/usr/bin/perl -w

######################################################
#
# This substitutes and text in the go file for any other text where multiple sets of before
# and aftercan be assigned in a tab-delimited mapping file.
#
#  
#  For information about some useful applications for this script, and for the instructions
#  on how to run it please see the oboedit wiki. 
# 
######################################################

use strict;

#use GO.terms_and_ids as input cos we only need ID and term name

open (TERMS, "numbermap.txt") or die "can't open GO term file \n";
open (SOURCE, "$ARGV[0]") or die "can't open input file \n"; # starting file
open (OUT, ">output_terms") or die "can't open output file \n"; # file with term names instead of IDs

my %idhash;
my $id;
my $l = 0;

while (my $line=<TERMS>){

    chomp $line;
    my @linecols = split(/\t/, $line);
	$idhash{$linecols[0]}=$linecols[1];

}

# Print out the results.

foreach my $id (keys %idhash){print ("$id\t$idhash{$id}\n")}

while (my $annot=<SOURCE>) {
    $l += 1;
    if ($l%1000 == 0) { print "$l\n" }
    foreach my $id (keys %idhash){
	$annot =~ s/\Q$id\E/$idhash{$id}/
	}
    print OUT "$annot";

}

__END__
