#!usr/bin/perl

#### STEP 1 - parse the gff3 in gene families

open (GFF3, $ARGV[0]) or die "Cannot open gff3 file!!";

@infile = <GFF3>;
$infile = join "", @infile;
@gff3 = split /\tgene\t/, $infile;

close GFF3;

foreach $gene (@gff3){
	if ($gene =~ /^##/){
		next;
	}
	else{
		@lines = split "\n", $gene;
		foreach $line (@lines){
			$line =~ s/\s+$//;
			if ($line =~ /\texon\t/){
				@tab = split "\t", $line;
				$exon_length = $tab[4] - $tab[3];
				$CDS_sequence = $CDS_sequence +$exon_length;
			}
			if ($line =~ /\tmRNA\t/){
				$mRNA = $line;
				@tab = split "\t", $line;
			
				@semi = split ";", $tab[8];
				@comma = split ",", $semi[2];
				
				$number_of_transcripts = scalar @comma;

				if (scalar @comma < 2){
						 $net_transcripts = $net_transcripts + $number_of_transcripts;
						#print $line, "\n";
					foreach $transcript (@comma){
						$transcript =~ s/Target\=//;
						@space = split " ", $transcript;
						
							#print $space[0], "\t", $space[1], "\t", $space[2], "\n";

						$alignment_aa = $space[2] - $space[1];
						$alignment_nt = $alignment_aa * 3;
						$total = $total + $alignment_nt;
							#print $space[0], "\t";
							$transcript_name = $space[0];
						$hash{$space[0]} = $space[0];
						$transcript_name = $space[0];
					
					}
				}	
			}
		}
	}
 $alignment_difference = $CDS_sequence - $total;
        $total = 0;
        #print $alignment_difference, "\n\n";
        $CDS_sequence = 0;
                $abs = abs($alignment_difference);
                if ($abs <= 5){
			$passers{$transcript_name} = $transcript_name;
		}
}

print $net_transcripts, "\n", scalar keys %passers, "\n";
