#!/usr/bin/perl -w

#### PS2P-7

#    VERSION: 1 (24 September 2003)
#    PURPOSE: Derived from BlastParser.pl
#             Extracts information from BLAST output
#             Takes only Query header and length
#    INPUT: From file $blast_path, with format:
#       (header)
#       ...
#       Query= [query_name] [query_description]
#                 ([query_length] letters)
#       ...
#
#       NOTE: The Query= line and >Contig line may spill over if the description
#             is too long
# 
#    OUTPUT: Output to screen, with format:
#       query name, length
#       Gives final statistics on number of orfs and sizes

############## LIBRARIES AND PRAGMAS ################

  use strict;

#################### VARIABLES ######################

  my $query_name;             # Name of query (e.g. all0001)
  my $query_length;           # Length of query (e.g. 173 [bp])
  my @query_info;             # Stores all above values
  my $line;                   # One line from input file
  my $LF = "\n";              # Linefeed
  my $length_max = 0;         # Keeps track of length of biggest ORF
  my $length_min = 100000000; # Keeps track of length of smallest ORF
  my $number_of_orfs = 0;     # Keeps track of number of ORFs read

###################### FILES ########################

#### OPEN THE BLAST FILE

  my $blast_path = '71vsnps.txt';
  open BLAST, "<$blast_path" or die "Can't open $blast_path: $!\n";

################### MAIN PROGRAM ####################
#
# Strategy: Look for query line
#           Save name 
#           Save length from next line
#           After all queries found, print all

  $line = <BLAST>;                                 # Read first line
  while (defined $line) {                          # Only if not end of file
     if ($line =~ /^Query=/) {                     # If 'Query' matches beginning of line...
        Get_query_info();                          # Extract info from query lines
     }
     $line = <BLAST>;                              # Read next line
  }

# Print query information and final statistics

  Print_query_info();
  print $LF, "Number of ORFs = $number_of_orfs";
  print $LF, "Smallest ORF = $length_min";
  print $LF, "Biggest ORF = $length_max";
  print $LF;

#################### SUBROUTINES ####################

#### GET_QUERY_INFO
# Extracts query information, saves in @query_info

sub Get_query_info {

   ($query_name) = ($line =~ /^Query=\s+(\w+)/);

   unless (defined $query_name) {
      warn "Didn't find query name on line $.\n";
      return;
   }

   $line = <BLAST>;
   while (defined $line) {
      ($query_length) = ($line =~ /(\d+) letters/);
      if (defined $query_length) {
         unshift @query_info, $query_name, $query_length;
         if ($query_length > $length_max) {$length_max = $query_length}
         if ($query_length < $length_min) {$length_min = $query_length}
         $number_of_orfs = $number_of_orfs + 1;
         return;
      }
      $line = <BLAST>;
   }
}

#### PRINT_QUERY_INFO
# Prints info stored in @query_info
# Takes info out query-by-query until no queries left

sub Print_query_info {
   while (@query_info) {
      $query_name = shift @query_info;
      $query_length = shift @query_info;
      print $query_name, " ", $query_length, $LF;  # Print to monitor
   }
}
