Parse blast stat.pl

From RAJ INFO
Jump to navigation Jump to search


#!/usr/bin/perl -w

# Libraries
	use strict;
	use Bio::SearchIO;

# Variables
	my ($i, $j, $inFile, $result, $hit, $report, $temp, $filecontent) = $_;
	
	my $path        = "/home/raj/bio";
	my $path1       = "$path/user/raja";
	my $path2       = "$path1/parse_blast";

	
# Input prompt

	if (! $ARGV[0]) {
		print "What is the BLAST file to parse? ";
		chomp ($inFile = <STDIN>);
	}
	else {
		$inFile = $ARGV[0];
	}

	my $outfile     = "$path2/$inFile.hits.txt";
	$temp        = "$path2/temp";

	
# Parsing Function
	
	$report = new Bio::SearchIO(
         		-file=>"$inFile",
			-format => "blast");

	$i = 0;
	while($result = $report->next_result) {
	
		$i++;
		my $query = $result->query_accession;

		$j = 0;
		while ( $hit = $result->next_hit) {

			$j++;
		}

		$filecontent = "$i\t$query\t$j\n";

		open (F, ">>".$temp);
		print F "$filecontent";
		close F;

	}
	

# Sort the temp File
	$filecontent = `sort -t '\t' -k3 -nr $temp`;
	
# Making the outfile
	open (FH, ">".$outfile);
	print FH "Query_No\tQuery_Name\tNumber of Hits\n$filecontent";
	close FH;
		
	if (-e $temp) {`rm $temp`};	

# END