3 # Demonstrates the use of a SearchIO Blast parser and a SearchWriterI object
4 # for producing custom output of Blast hit data from a Blast report
7 # Here we define a custom SearchWriterI object that ouputs just the data we want
8 # from each BLAST report.
10 # NOTE: If you just want pick and choose which columns you want
11 # in the output table, you don't need to create your own custom
12 # SearchWriterI implementation as we do here. HitTableWriter and HSPTableWriter
13 # are configurable as to what columns and order you want.
14 # The hitwriter*.pl and hspwriter.pl examples in this directory
17 # For a complete list of columns, see the docs for these modules:
18 # Bio::SearchIO::Writer::HitTableWriter
19 # Bio::SearchIO::Writer::HSPTableWriter
21 # This example serves as an illustration of how to use the
22 # SearchWriterI api and plug it in to a SearchIO parser,
23 # which you may want to do if you want to generate data column(s)
24 # not provided by the available writers.
27 # STDIN: stream containing one or more BLAST or PSI-BLAST reports.
28 # STDOUT: none, but generates an output file "custom_writer.out"
29 # containing tab-delimited data on a per-hit basis.
30 # STDERR: Progress info.
32 # Author: Steve Chervitz <sac@bioperl.org>
34 package MyBlastWriter
;
38 use Bio
::SearchIO
::SearchWriterI
;
40 use base
qw( Bio::Root::Root Bio::SearchIO::SearchWriterI );
43 my ($self, $result, @args) = @_;
46 my $hits_reported = 0;
48 foreach my $hit($result->hits) {
50 # If this is a PSI-BLAST report, only report novel hits
51 if( $result->psiblast ) {
52 # Note that we could have supplied this has a -HIT_FILTER function
53 # when we defined our input SearchIO object. Then we wouldn't need
54 # to define a custom writer.
55 next unless $hit->iteration > 1 and not $hit->found_again;
59 printf STDERR
"$hit\n";
61 $str .= sprintf "%s\t%d\t%s\t%d\t%.2f\t%d\t%.1e\t%d\t%d\t%d\t%d\t%s\n",
62 $result->query_name, $result->query_length, $hit->name,
63 $hit->length, $hit->frac_identical('query'), $hit->length_aln,
64 $hit->expect, $hit->score, $hit->bits,
65 $hit->gaps('total'), $hit->num_hsps, $hit->iteration || '-';
68 printf STDERR
"\n%d hits written\n", $hits_reported;
76 #===================================================
78 #===================================================
86 my $in = Bio
::SearchIO
->new( -format
=> 'blast',
89 my $writer = MyBlastWriter
->new();
90 my $out = Bio
::SearchIO
->new( -format
=> 'blast',
92 -file
=> ">custom_writer.out" );
94 while ( my $result = $in->next_result() ) {
95 printf STDERR
"Report %d: $result\n", $in->result_count;
96 $out->write_result($result);
99 printf STDERR
"\n%d Results processed.\n", $in->result_count;
100 printf STDERR
"Output sent to file: %s\n", $out->file if $out->file;