Bio/DB/Taxonomy/flatfile.pm: Fix for issue #80, changed DESTROY
[bioperl-live.git] / examples / searchio / custom_writer.pl
blobfd75e314c86bd6627e6878da9c443b683fa15983
1 #!/usr/bin/env perl
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
5 # input stream.
7 # Here we define a custom SearchWriterI object that ouputs just the data we want
8 # from each BLAST report.
9 #
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
15 # illustrate this.
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.
26 # Usage:
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;
36 use strict;
37 use lib '../../';
38 use Bio::Root::Root;
39 use Bio::SearchIO::SearchWriterI;
41 use base qw( Bio::Root::Root Bio::SearchIO::SearchWriterI );
43 sub to_string {
44 my ($self, $result, @args) = @_;
45 my $str = '';
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;
59 $hits_reported++;
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;
71 $str;
75 package main;
77 #===================================================
78 # Start of script
79 #===================================================
81 use strict;
83 use lib '../../../';
84 use Bio::SearchIO;
86 select STDOUT; $|=1;
88 my $in = Bio::SearchIO->new( -format => 'blast',
89 -fh => \*ARGV,
90 -signif => 0.1 );
91 my $writer = MyBlastWriter->new();
92 my $out = Bio::SearchIO->new( -format => 'blast',
93 -writer => $writer,
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;