| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | search_overview -- Render a SearchIO parser report into a simple overview graphic | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | search_overview -i filename [-f format] [-o outputfilename] [--labels] | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | This script will take any Bio::SearchIO parseable report and turn it | ||
| 14 | into a simple overview graphic of the report. For our purposes we are | ||
| 15 | assuming BLAST and the BLAST scores when assigning colors. Output is | ||
| 16 | a PNG format file. | ||
| 17 | |||
| 18 | This is not intended to be an overly customized script, rather it | ||
| 19 | should probably just be either a quick and dirty look at a report or a | ||
| 20 | starting point for more complicated implementations. | ||
| 21 | |||
| 22 | The color is determined by the hit score which is currently pegged to the NCBI | ||
| 23 | scheme which looks like this | ||
| 24 | |||
| 25 | RED E<gt>= 200 | ||
| 26 | PURPLE 80-200 | ||
| 27 | GREEN 50-80 | ||
| 28 | BLUE 40-50 | ||
| 29 | BLACK E<lt>40 | ||
| 30 | |||
| 31 | Options: | ||
| 32 | -i/--input The input filename, otherwise input is assumed from STDIN | ||
| 33 | -o/--output The output filename, this is optional, if you do not | ||
| 34 | provide the output filename the script will create a file | ||
| 35 | using the name of the query sequence and will process | ||
| 36 | all the sequences in the file. If an output filename | ||
| 37 | IS provided the script will only display an image for the | ||
| 38 | first one. | ||
| 39 | -f/--format The SearchIO format parser to use, if not provided | ||
| 40 | SearchIO will guess based on the file extension. | ||
| 41 | -l/--labels Display the hit sequence name as a label in the overview. | ||
| 42 | For lots of sequences this will make the image very long | ||
| 43 | so by default it is turned off. | ||
| 44 | |||
| 45 | =head1 AUTHOR Jason Stajich | ||
| 46 | |||
| 47 | Jason Stajich, jason[-at-]open-bio[-dot-]org. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | use strict; | ||
| 52 | |||
| 53 | use Bio::Graphics::Panel; | ||
| 54 | use Bio::Graphics::Feature; | ||
| 55 | use Bio::Graphics::FeatureFile; | ||
| 56 | use Bio::SearchIO; | ||
| 57 | use Getopt::Long; | ||
| 58 | |||
| 59 | use constant WIDTH => 600; # default width | ||
| 60 | |||
| 61 | my ($in,$format,$out); | ||
| 62 | |||
| 63 | my $showlabels = 0; | ||
| 64 | |||
| 65 | # This defines the color order | ||
| 66 | # For NCBI it is typically defined like this | ||
| 67 | # Score | ||
| 68 | # RED >= 200 | ||
| 69 | # PURPLE 80-200 | ||
| 70 | # GREEN 50-80 | ||
| 71 | # BLUE 40-50 | ||
| 72 | # BLACK <40 | ||
| 73 | my @COLORS = qw(red magenta green blue black); | ||
| 74 | my @SCORES = (200,80,50,40,0); | ||
| 75 | |||
| 76 | GetOptions( | ||
| 77 | 'i|in|input:s' => \$in, | ||
| 78 | 'f|format:s' => \$format, | ||
| 79 | 'o|output:s' => \$out, | ||
| 80 | 'l|labels' => \$showlabels | ||
| 81 | ); | ||
| 82 | |||
| 83 | |||
| 84 | my $parser = new Bio::SearchIO(-file => $in, | ||
| 85 | -format => $format); | ||
| 86 | |||
| 87 | while( my $r = $parser->next_result ) { | ||
| 88 | |||
| 89 | my ($qname,$qlen) = ($r->query_name, $r->query_length); | ||
| 90 | my $max = 0; | ||
| 91 | my (@features,@configs); | ||
| 92 | while(my $h = $r->next_hit ) { | ||
| 93 | next if $h->num_hsps == 0; | ||
| 94 | my ($left,$right) = ( $h->start('query'), | ||
| 95 | $h->end ('query') ); | ||
| 96 | |||
| 97 | if( ! $qlen ) { | ||
| 98 | $max = MAX($max,abs($right-$left)); | ||
| 99 | } | ||
| 100 | my $bin = 0; | ||
| 101 | my $score = $h->score; | ||
| 102 | for my $s ( @SCORES ) { | ||
| 103 | last if( $score > $s); | ||
| 104 | $bin++; | ||
| 105 | } | ||
| 106 | push @features, Bio::Graphics::Feature->new(-start => $left, | ||
| 107 | -stop => $right, | ||
| 108 | -type => 'similarity', | ||
| 109 | -name => $h->name, | ||
| 110 | -desc => $h->description | ||
| 111 | ); | ||
| 112 | push @configs, [ ( -glyph => 'segments', | ||
| 113 | -bgcolor => $COLORS[$bin], | ||
| 114 | -fgcolor => $COLORS[$bin], | ||
| 115 | -label => $showlabels, | ||
| 116 | -height => 1, | ||
| 117 | )]; | ||
| 118 | |||
| 119 | } | ||
| 120 | my $panel = Bio::Graphics::Panel->new(-length => $qlen || $max, | ||
| 121 | -bgcolor => 'white', | ||
| 122 | -pad_left=> 10, | ||
| 123 | -pad_right=> 10); | ||
| 124 | $panel->add_track('arrow' => Bio::Graphics::Feature->new | ||
| 125 | (-start => 1, | ||
| 126 | -end => $qlen || $max), | ||
| 127 | -bump => 0, | ||
| 128 | -double => 1, | ||
| 129 | -tick => 2, | ||
| 130 | ); | ||
| 131 | foreach my $f ( @features ) { | ||
| 132 | my $c = shift @configs; | ||
| 133 | $panel->add_track($f, @$c); | ||
| 134 | } | ||
| 135 | if( $out ) { | ||
| 136 | open(OUT,">$out") || die("cannot open $out: $!"); | ||
| 137 | binmode(OUT); | ||
| 138 | print OUT $panel->png; | ||
| 139 | close(OUT); | ||
| 140 | if( $parser->result_count > 1 ) { | ||
| 141 | print STDERR "only printing the first result, do not provide a outfile name if you want to see them all\n"; | ||
| 142 | } | ||
| 143 | last; | ||
| 144 | } else { | ||
| 145 | open(OUT, ">$qname.png") || die("$qname: $!"); | ||
| 146 | binmode(OUT); | ||
| 147 | print OUT $panel->png; | ||
| 148 | close(OUT); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | sub MAX {return $_[0] < $_[1] ? $_[1] : $_[0] } | ||
| 153 | sub MIN {return $_[0] > $_[1] ? $_[1] : $_[0] } |