Remove manipulation of @INC and use of lib - require install for use.
[bioperl-live.git] / examples / searchio / custom_writer.pl
blobfb4aa29f2ecebbf6aceb11e5cf92bb7ff25ca4d5
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 Bio::Root::Root;
38 use Bio::SearchIO::SearchWriterI;
40 use base qw( Bio::Root::Root Bio::SearchIO::SearchWriterI );
42 sub to_string {
43 my ($self, $result, @args) = @_;
44 my $str = '';
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;
58 $hits_reported++;
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;
70 $str;
74 package main;
76 #===================================================
77 # Start of script
78 #===================================================
80 use strict;
82 use Bio::SearchIO;
84 select STDOUT; $|=1;
86 my $in = Bio::SearchIO->new( -format => 'blast',
87 -fh => \*ARGV,
88 -signif => 0.1 );
89 my $writer = MyBlastWriter->new();
90 my $out = Bio::SearchIO->new( -format => 'blast',
91 -writer => $writer,
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;