BEGIN{
    $ENV{'PATH'}=$ENV{'PATH'}.":/local/workspace01/zhangju/script/perl";
    push(@INC,"/local/workspace01/zhangju/script/perl");
}

use Bio::SeqIO;
use strict;
my $gb1=shift;
my $gb2=shift;
my $format="genbank";
my $seqio1=Bio::SeqIO->new(-file=>$gb1, -format=>$format);
my $seq_object1 = $seqio1->next_seq;
my %locus1=();
my %desc1=();
my @tags=("product","note","pseudo");
for my $feat_object1 ($seq_object1->get_SeqFeatures) { 
	if ($feat_object1->has_tag("locus_tag")){
		my $tag=($feat_object1->get_tag_values("locus_tag"))[0];
		if ($tag eq "Cthe_1765"){
			print "pause";
		}
		if (not (exists $locus1{$tag})){
			$locus1{$tag} = $feat_object1->spliced_seq->seq;
		}
		#elsif(not ($locus1{$tag} eq $feat_object1->spliced_seq->seq)){
		#	print "annotation sequence mismatching for locus tag - ".$tag."\n";
		#}
		if (not exists $desc1{$tag}){
			$desc1{$tag}="";
		}
		for my $tag1 (@tags){
			if ($feat_object1->has_tag($tag1)){
				$desc1{$tag}=$desc1{$tag}."|".$tag1." - ".($feat_object1->get_tag_values($tag1))[0];
			}
		}
	}
}
my $seqio2=Bio::SeqIO->new(-file=>$gb2, -format=>$format);
my $seq_object2 = $seqio2->next_seq;
my %locus2=();
my %desc2=();
for my $feat_object2 ($seq_object2->get_SeqFeatures) { 
	if ($feat_object2->has_tag("locus_tag")){
		my $tag=($feat_object2->get_tag_values("locus_tag"))[0];
		if ($tag eq "Cthe_1765"){
#			print "pause";
		}
		if (not exists $locus2{$tag}){
			$locus2{$tag} = $feat_object2->spliced_seq->seq;
		}
		if (not exists $desc1{$tag}){
			$desc1{$tag}="";
		}
		for my $tag1 (@tags){
			if ($feat_object2->has_tag($tag1)){
				$desc2{$tag}=$desc2{$tag}."|".$tag1." - ".($feat_object2->get_tag_values($tag1))[0];
			}
		}
	}
}
open(diffF, ">".$gb1."_".$gb2."_diff_seq.txt");
for (keys %locus1){
	if (exists $locus2{$_}){
		if (not ($locus1{$_} eq $locus2{$_})){
			print diffF "locus_tag - ".$_." from two genbank files are different\n";
			print diffF "file 1 gene -".$desc1{$_}."\n";
			print diffF $locus1{$_}."\n";
			print diffF "file 2 gene -".$desc2{$_}."\n";
			print diffF $locus2{$_}."\n";
		}
	}
}
close(diffF);
open(uniqLocusF1,">".$gb1."_uniqLocusTag_from_".$gb2.".txt");
for (keys %locus1){
	if (not exists $locus2{$_}){
		print uniqLocusF1 $_." - ".$desc1{$_}."\n";
	}
}
close(uniqLocusF1);
open(uniqLocusF2,">".$gb2."_uniqLocusTag_from_".$gb1.".txt");
for (keys %locus2){
	if (not exists $locus1{$_}){
		print uniqLocusF2 $_." - ".$desc2{$_}."\n";
	}
}
close(uniqLocusF2)

