#!/usr/bin/env perl -w

use strict;

use Getopt::Long;
use Pod::Usage;
use Term::ANSIColor;

my $flag_help       = 0;
my $debug           = 0;
my $param_species   = "monosigaovata";
my $param_build     = "build2";
my $param_trackname = "DummyTrack";
my $param_comment   = "CommentHere";
my $param_option    = "";

GetOptions( 'help|?'      => \$flag_help,
            'species=s'   => \$param_species,
            'build=s'     => \$param_build,
            'optattr=s'   => \$param_option,
            'trackname=s' => \$param_trackname,
            'comment=s'   => \$param_comment,
	        'debug'       => \$debug
          );
pod2usage(1) if $flag_help;

my $number_of_warnings = 0;

#show header
print "[GenomeVersion]\n";
print "Species=$param_species\n";
print "Revision=$param_build\n";
print "\n";
print "[TrackHeader]\n";
print "TrackName=$param_trackname\n";
print "TrackComment=$param_comment\n";
print "TrackOptattr=$param_option\n";
print "\n";
print "[Data]\n";
#main loop
while(<>) {
	s/\r?\n$//;
	s/^\s+//;
	next if(/^$/);
	if(/^scaffold\s+(\d+)\s+(\d+)-(\d+)\s+([+\-])[123]\s+([\d\.e\+\-]+)\s+(\d+)\s+(\d+)\s+(\d+)-(\d+)\/(\d+)\s+(.*)$/) {
		my $scaffoldnum = $1;
		my $startpos_on_scaffold_inclusive_1origin = $2;
		my $endpos_on_scaffold_inclusive_1origin   = $3;
		my $direction = $4; my $is_plus_strand = $direction eq '+';
		my $blast_evalue = $5;
		my $blast_score = $6;
		my $blast_alignmentlength = $7;
		my $startpos_on_query_inclusive_1origin = $8;
		my $endpos_on_query_inclusive_1origin = $9;
		my $length_of_query = $10;
		my $description = $11; $description =~ s/"/'/g;
		my $shortdescription = ($description =~ /^([^\.\|]+)/) ? $1 : $description;
		print "TargetScaffold=scaffold$scaffoldnum";
		print ",TargetRange=${startpos_on_scaffold_inclusive_1origin}-${endpos_on_scaffold_inclusive_1origin}";
		print ",\"TargetExons=${startpos_on_scaffold_inclusive_1origin}-${endpos_on_scaffold_inclusive_1origin}\"";
		print ",TargetStrand=" . ($is_plus_strand ? '+' : '-');
		print ",Name=$shortdescription";
		print ",\"BLAST e-value=$blast_evalue\"";
		print ",\"BLAST score=$blast_score\"";
		print ",\"BLAST alignment length=$blast_alignmentlength\"";
		print ",QueryRange=${startpos_on_query_inclusive_1origin}-${endpos_on_query_inclusive_1origin}";
		print ",QueryLength=$length_of_query";
		print ",\"Description=$description\"";
		print "\n";
	} else {
		print color("red") . "WARNING: Could not parse at line $." . color("reset") . "\n";
		print "$_\n";
		$number_of_warnings++;
	}
}

if($number_of_warnings > 0) {
	print color("red") . "$number_of_warnings warnings in total." . color("reset") . "\n";
}

=pod

=head1 NAME

blastsummary2easyfeaturetrack.pl - converts BLAST summary file to EasyFeatureTrack file for UTGB.

=head1 SYNOPSIS

blastsummary2easyfeaturetrack.pl [options...] < input > output

Options:
   -help            brief help message

=head1 OPTIONS

=over 8

=item B<-help>

Print a brief help message and exits.

=item B<-species>

Sets the name of species, which will appear in the header of EasyGeneTrack.
The default value is "-species=monosigaovata".

=item B<-build>

Sets the name of assembly build, which will appear in the header of EasyGeneTrack.
The default value is "-build=build2".

=item B<-track>

Sets the name of genome browser track, which will appear in the header of EasyGeneTrack.
The default value is "-track=DummyTrack".

=item B<-comment>

Sets the name of genome browser track comment, which will appear in the header of EasyGeneTrack.
The default value is "-comment=CommentHere".


=back

=head1 DESCRIPTION

B<blastsummary2easyfeaturetrack.pl> inputs BLAST summary file, which is defined elsewhere,
and convert it into EasyFeatureTrack file, which can be directly passed to
UT genome browser. Please see the web page below for EasyFeatureTrack specs.

http://www.utgenome.org/wiki/index.php?Manual%2FEasyFeatureTrack

=cut

