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
;
39 use Bio
::SearchIO
::SearchWriterI
;
41 use base
qw( Bio::Root::Root Bio::SearchIO::SearchWriterI );
44 my ($self, $result, @args) = @_;
47 my $hits_reported = 0;
49 foreach my $hit($result->hits) {
51 # If this is a PSI-BLAST report, only report novel hits
52 if( $result->psiblast ) {
53 # Note that we could have supplied this has a -HIT_FILTER function
54 # when we defined our input SearchIO object. Then we wouldn't need
55 # to define a custom writer.
56 next unless $hit->iteration > 1 and not $hit->found_again;
60 printf STDERR
"$hit\n";
62 $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",
63 $result->query_name, $result->query_length, $hit->name,
64 $hit->length, $hit->frac_identical('query'), $hit->length_aln,
65 $hit->expect, $hit->score, $hit->bits,
66 $hit->gaps('total'), $hit->num_hsps, $hit->iteration || '-';
69 printf STDERR
"\n%d hits written\n", $hits_reported;
77 #===================================================
79 #===================================================
88 my $in = Bio
::SearchIO
->new( -format
=> 'blast',
91 my $writer = MyBlastWriter
->new();
92 my $out = Bio
::SearchIO
->new( -format
=> 'blast',
94 -file
=> ">custom_writer.out" );
96 while ( my $result = $in->next_result() ) {
97 printf STDERR
"Report %d: $result\n", $in->result_count;
98 $out->write_result($result);
101 printf STDERR
"\n%d Results processed.\n", $in->result_count;
102 printf STDERR
"Output sent to file: %s\n", $out->file if $out->file;