#! /usr/bin/perl -w #This script was written by Xiaodong Bai #command line usage: #perl ParseBlast.pl Blast_output_file Blast_parsed_output_file #It reads the output file of Blast search #gives one file named as adding ".all" to the end of user selected Blast_parsed_output_file #that store all queries and blast information #AND, gives another file named as adding ".hits" to the end of user selected Blast_parsed_output_file #that stores only queries with hits and first three hits and E value, if existed my $Lines = 0; my @HitLines; my @Data; my @Results; my @Query; open INPUT, "$ARGV[0]" or die "Cannot open input file: $!"; while () { chomp; push @Data, $_; $Lines += 1; if ($_ =~ s/^Query=\s+?(.+?)$/$1/) { push @Query, $1; } if ($_ =~ /^Searching..../) { push @HitLines, $Lines; } } close INPUT or die "Cannot close input file: $!"; foreach (@HitLines) { if ($Data[$_ + 1] =~ /No\s*hits\s*found/) { push @Results, $Data[$_ + 1]; } else { my $temp = ""; for (my $i = 0; $i < 3; $i ++) { if ($Data[$_ + 4 + $i] =~ /^(gi|\w)/) { $temp .= "$Data[$_ + 4 + $i]\n"; } } push @Results, $temp; } } my $Query = @Query; my $all = $ARGV[1] . ".all"; open OUTPUT, ">$all" or die "Cannot open the file to store all queries with or without hits: $!"; for (my $j = 0; $j < $Query; $j ++) { print OUTPUT ">$Query[$j]\n"; print OUTPUT "$Results[$j]\n\n"; } close OUTPUT or die "Cannot close the file to store all queries with or without hits: $!"; my $hits = $ARGV[1] . ".hits"; open OUTPUT2, ">$hits" or die "Cannot open the file to store all queries with hits: $!"; for (my $k = 0; $k < $Query; $k ++) { if ($Results[$k] =~ /^(gi|\w)/) { print OUTPUT2 ">$Query[$k]\n"; print OUTPUT2 "$Results[$k]\n\n"; } } close OUTPUT2 or die "Cannot close the file to store all queries with hits: $!";