ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/yamap/blasttoxls.pl
Revision: 1.1.1.1 (vendor branch)
Committed: Thu Sep 7 15:35:21 2006 UTC (9 years, 10 months ago) by knirirr
Branch: MAIN, cehox
CVS Tags: start, HEAD
Changes since 1.1: +0 -0 lines
Log Message:
Imported sources

Line File contents
1 #!/usr/bin/perl
2
3 # blasttoxls.pl
4 # a script to read blast files from YAMAP output directories
5 # and write a whopping excel spreadsheet with them all in
6 # OR write all top hits to the same file
7
8 use strict;
9 use File::Basename;
10 use Spreadsheet::WriteExcel;
11 use Getopt::Std;
12
13 # options
14 # t: output top hits
15 # a: output all hits
16 my %opts=();
17 getopts('ta',\%opts);
18
19 unless (@ARGV)
20 {
21 die "Usage: ./blasttoxls.pl [-t/-a] <infiles>\n";
22 }
23
24 my @crunchfiles = @ARGV;
25 my @headers = ("Query id", "Subject id", "% identity", "alignment length", "mismatches", "gap openings", "q. start", "q. end", "s. start", "s. end", "e-value", "bit score");
26
27
28 # all hits into one file, one genome per spreadsheet
29 if (defined $opts{a})
30 {
31 my $outfile = "yamap_out/blast_all.xls";
32 my $workbook = Spreadsheet::WriteExcel->new("$outfile");
33 foreach my $cf (@crunchfiles)
34 {
35 my $sheet_name = &basename($cf);
36 my $worksheet = $workbook->add_worksheet($sheet_name);
37 open (IN, "<$cf") or die "Can't open $cf: $!";
38 for (my $j = 0; $j<=((scalar @headers)-1); $j++)
39 {
40 $worksheet->write(0,$j,$headers[$j]);
41 }
42 my $row = 1;
43 while (my $line = <IN>)
44 {
45 chomp($line);
46 next if ($line =~/^#/);
47 my @parts = split(/\s+/, $line);
48 my $elements = @parts;
49 for (my $i=0;$i<=$elements-1; $i++)
50 {
51 $worksheet->write($row,$i,$parts[$i]);
52 }
53 $row++;
54 }
55 close IN;
56 }
57 }
58 # top hits
59 if (defined $opts{t})
60 {
61 my $outfile = "yamap_out/blast_top.xls";
62 my $workbook = Spreadsheet::WriteExcel->new("$outfile");
63 my $sheet_name = "top_hits";
64 my $worksheet = $workbook->add_worksheet($sheet_name);
65 for (my $j = 0; $j<=((scalar @headers)); $j++)
66 {
67 $worksheet->write(0,$j,$headers[$j]);
68 }
69
70 my $row = 1;
71 foreach my $cf (@crunchfiles)
72 {
73 open (IN, "<$cf") or die "Can't open $cf: $!";
74 my @lines = <IN>;
75 # -m9 is being used, so there are four lines
76 # of comments before the blast results are reached
77 my $line0 = $lines[0];
78 my $line4 = $lines[4];
79 my $line;
80 if ($line0 =~ /^#/)
81 {
82 $line = $line4;
83 }
84 else
85 {
86 $line = $line0;
87 }
88 chomp($line);
89
90 # print it all out to the correct row
91 my $subj_name = [split(/\//, $cf)]->[-1];
92 $worksheet->write($row,0,[split(/\./,$subj_name)]->[0]);
93 my @parts = split(/\s+/, $line);
94 my $elements = @parts;
95 if ($elements == 0)
96 {
97 $worksheet->write($row,1,"no hits");
98 }
99 else
100 {
101 for (my $i=1;$i<=$elements-1; $i++)
102 {
103 $worksheet->write($row,$i,$parts[$i]);
104 }
105 }
106 $row++;
107 close IN;
108 }
109 }