| 1 | package Bio::Gonzales::Graphics::BLAST; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | use 5.010; | ||
| 8 | |||
| 9 | use Log::Log4perl qw(:easy); | ||
| 10 | use Bio::Graphics; | ||
| 11 | use Bio::SearchIO; | ||
| 12 | use Bio::SeqFeature::Generic; | ||
| 13 | use Data::Dumper; | ||
| 14 | |||
| 15 | use base 'Exporter'; | ||
| 16 | our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS ); | ||
| 17 | our $VERSION = '0.058'; # VERSION | ||
| 18 | |||
| 19 | @EXPORT = qw(); | ||
| 20 | %EXPORT_TAGS = (); | ||
| 21 | @EXPORT_OK = qw(render render_sep); | ||
| 22 | |||
| 23 | sub render { | ||
| 24 | my ( $src, $options ) = @_; | ||
| 25 | |||
| 26 | my %opts = ( blast_format => 'blast', img_format => 'png', %{ $options // {} } ); | ||
| 27 | |||
| 28 | my $fh; | ||
| 29 | my $fh_was_open; | ||
| 30 | if ( ref $src && ref $src ne 'SCALAR' ) { | ||
| 31 | $fh = $src; | ||
| 32 | $fh_was_open = 1; | ||
| 33 | } else { | ||
| 34 | open $fh, '<', $src or confess "Can't open filehandle: $!"; | ||
| 35 | } | ||
| 36 | |||
| 37 | my $searchio = Bio::SearchIO->new( | ||
| 38 | -fh => $fh, | ||
| 39 | -format => $opts{format} | ||
| 40 | ) or die "parse failed"; | ||
| 41 | |||
| 42 | my %panel_imgs; | ||
| 43 | while ( my $result = $searchio->next_result() ) { | ||
| 44 | |||
| 45 | my $panel = Bio::Graphics::Panel->new( | ||
| 46 | -length => $result->query_length, | ||
| 47 | -width => 800, | ||
| 48 | -pad_left => 10, | ||
| 49 | -pad_right => 10, | ||
| 50 | ); | ||
| 51 | |||
| 52 | my $full_length = Bio::SeqFeature::Generic->new( | ||
| 53 | -start => 1, | ||
| 54 | -end => $result->query_length, | ||
| 55 | -display_name => $result->query_name, | ||
| 56 | ); | ||
| 57 | $panel->add_track( | ||
| 58 | $full_length, | ||
| 59 | -glyph => 'arrow', | ||
| 60 | -tick => 2, | ||
| 61 | -fgcolor => 'black', | ||
| 62 | -double => 1, | ||
| 63 | -label => 1, | ||
| 64 | ); | ||
| 65 | |||
| 66 | my $track = $panel->add_track( | ||
| 67 | -glyph => 'graded_segments', | ||
| 68 | -label => 1, | ||
| 69 | -connector => 'dashed', | ||
| 70 | -bgcolor => 'blue', | ||
| 71 | -font2color => 'red', | ||
| 72 | -sort_order => 'high_score', | ||
| 73 | -description => sub { | ||
| 74 | my $feature = shift; | ||
| 75 | return unless $feature->has_tag('description'); | ||
| 76 | my ($description) = $feature->each_tag_value('description'); | ||
| 77 | my $score = $feature->score; | ||
| 78 | "$description, score: $score"; | ||
| 79 | }, | ||
| 80 | ); | ||
| 81 | |||
| 82 | while ( my $hit = $result->next_hit ) { | ||
| 83 | next | ||
| 84 | unless ( !exists( $opts{evalue} ) | ||
| 85 | || ( exists( $opts{evalue} ) && $hit->significance < $opts{evalue} ) ); | ||
| 86 | my $feature = Bio::SeqFeature::Generic->new( | ||
| 87 | -score => $hit->raw_score, | ||
| 88 | -display_name => $hit->name, | ||
| 89 | -tag => { description => sprintf( "len: %d", $hit->length ) }, | ||
| 90 | ); | ||
| 91 | while ( my $hsp = $hit->next_hsp ) { | ||
| 92 | $feature->add_sub_SeqFeature( $hsp, 'EXPAND' ); | ||
| 93 | } | ||
| 94 | |||
| 95 | $track->add_feature($feature); | ||
| 96 | } | ||
| 97 | |||
| 98 | my $img_result; | ||
| 99 | if ( $opts{img_format} =~ /svg/i ) { | ||
| 100 | $img_result = $panel->svg; | ||
| 101 | } elsif ( $opts{img_format} =~ /png/i ) { | ||
| 102 | $img_result = $panel->png; | ||
| 103 | } | ||
| 104 | |||
| 105 | $panel_imgs{ $result->query_name } = $img_result; | ||
| 106 | $panel->finished; | ||
| 107 | } | ||
| 108 | $fh->close unless ($fh_was_open); | ||
| 109 | |||
| 110 | return \%panel_imgs; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub render_sep { | ||
| 114 | my ( $src, $options ) = @_; | ||
| 115 | |||
| 116 | my %opts = ( blast_format => 'blast', img_format => 'png', %{ $options // {} } ); | ||
| 117 | |||
| 118 | my $fh; | ||
| 119 | my $fh_was_open; | ||
| 120 | if ( ref $src && ref $src ne 'SCALAR' ) { | ||
| 121 | $fh = $src; | ||
| 122 | $fh_was_open = 1; | ||
| 123 | } else { | ||
| 124 | open $fh, '<', $src or confess "Can't open filehandle: $!"; | ||
| 125 | } | ||
| 126 | |||
| 127 | my $searchio = Bio::SearchIO->new( | ||
| 128 | -fh => $fh, | ||
| 129 | -format => $opts{format} | ||
| 130 | ) or die "parse failed"; | ||
| 131 | |||
| 132 | my %panel_imgs; | ||
| 133 | while ( my $result = $searchio->next_result() ) { | ||
| 134 | |||
| 135 | my $panel = Bio::Graphics::Panel->new( | ||
| 136 | -length => $result->query_length, | ||
| 137 | -width => 1200, | ||
| 138 | -pad_left => 30, | ||
| 139 | -pad_right => 370, | ||
| 140 | ); | ||
| 141 | |||
| 142 | my $full_length = Bio::SeqFeature::Generic->new( | ||
| 143 | -start => 1, | ||
| 144 | -end => $result->query_length, | ||
| 145 | -display_name => $result->query_name, | ||
| 146 | ); | ||
| 147 | $panel->add_track( | ||
| 148 | $full_length, | ||
| 149 | -glyph => 'arrow', | ||
| 150 | -tick => 2, | ||
| 151 | -fgcolor => 'black', | ||
| 152 | -double => 1, | ||
| 153 | -label => 1, | ||
| 154 | ); | ||
| 155 | |||
| 156 | my $track_merged = $panel->add_track( | ||
| 157 | -glyph => 'segments', | ||
| 158 | -label => 1, | ||
| 159 | -connector => 'dashed', | ||
| 160 | -bgcolor => 'blue', | ||
| 161 | -font2color => 'red', | ||
| 162 | -sort_order => 'high_score', | ||
| 163 | -description => sub { | ||
| 164 | my $feature = shift; | ||
| 165 | return "all hits merged" unless $feature->has_tag('description'); | ||
| 166 | my ($description) = $feature->each_tag_value('description'); | ||
| 167 | my $score = $feature->score; | ||
| 168 | "$description, score: $score"; | ||
| 169 | }, | ||
| 170 | ); | ||
| 171 | |||
| 172 | my $track = $panel->add_track( | ||
| 173 | -glyph => 'segments', | ||
| 174 | -label => 1, | ||
| 175 | -bgcolor => 'blue', | ||
| 176 | -font2color => 'red', | ||
| 177 | -description => sub { | ||
| 178 | my $feature = shift; | ||
| 179 | return unless $feature->has_tag('description'); | ||
| 180 | my ($description) = $feature->each_tag_value('description'); | ||
| 181 | my $score = $feature->score; | ||
| 182 | "$description, score: $score"; | ||
| 183 | }, | ||
| 184 | ); | ||
| 185 | |||
| 186 | while ( my $hit = $result->next_hit ) { | ||
| 187 | next | ||
| 188 | unless ( !exists( $opts{evalue} ) | ||
| 189 | || ( exists( $opts{evalue} ) && $hit->significance < $opts{evalue} ) ); | ||
| 190 | |||
| 191 | my $feature_merged = Bio::SeqFeature::Generic->new( | ||
| 192 | -score => $hit->raw_score, | ||
| 193 | -display_name => $hit->name, | ||
| 194 | #-tag => { description => $hit->description }, | ||
| 195 | ); | ||
| 196 | while ( my $hsp = $hit->next_hsp ) { | ||
| 197 | |||
| 198 | #Bio::Search::HSP::GenericHSP | ||
| 199 | #Bio::Search::HSP::HSPI | ||
| 200 | my $qb = $hsp->start('query'); | ||
| 201 | my $qe = $hsp->end('query'); | ||
| 202 | |||
| 203 | my $sb = $hsp->start('subject'); | ||
| 204 | my $se = $hsp->end('subject'); | ||
| 205 | |||
| 206 | my $dist; | ||
| 207 | if ( $qb > $se ) { | ||
| 208 | $dist = $qb - $se; | ||
| 209 | } elsif ( $sb > $qe ) { | ||
| 210 | $dist = $sb - $qe; | ||
| 211 | } else { | ||
| 212 | next; | ||
| 213 | } | ||
| 214 | |||
| 215 | $feature_merged->add_sub_SeqFeature( $hsp, 'EXPAND' ); | ||
| 216 | |||
| 217 | next if ( $dist < 30000 ); | ||
| 218 | |||
| 219 | my $feature = Bio::SeqFeature::Generic->new( | ||
| 220 | -score => $hsp->bits, | ||
| 221 | -start => $hsp->start, | ||
| 222 | -end => $hsp->end, | ||
| 223 | -display_name => $hit->name, | ||
| 224 | -tag => { description => "S: $sb - $se, Q: $qb - $qe" }, | ||
| 225 | ); | ||
| 226 | $track->add_feature($feature); | ||
| 227 | } | ||
| 228 | $track_merged->add_feature($feature_merged); | ||
| 229 | |||
| 230 | } | ||
| 231 | my $img_result; | ||
| 232 | if ( $opts{img_format} =~ /svg/i ) { | ||
| 233 | $img_result = $panel->svg; | ||
| 234 | } elsif ( $opts{img_format} =~ /png/i ) { | ||
| 235 | $img_result = $panel->png; | ||
| 236 | } | ||
| 237 | |||
| 238 | $panel_imgs{ $result->query_name } = $img_result; | ||
| 239 | $panel->finished; | ||
| 240 | } | ||
| 241 | |||
| 242 | $fh->close unless ($fh_was_open); | ||
| 243 | |||
| 244 | return \%panel_imgs; | ||
| 245 | } | ||
| 246 | 1; | ||
| 247 | |||
| 248 | __END__ | ||
| 249 | |||
| 250 | =head1 NAME | ||
| 251 | |||
| 252 | Bio::Gonzales::Graphics::BLAST - make your blast result nice | ||
| 253 | |||
| 254 | =head1 SYNOPSIS | ||
| 255 | |||
| 256 | Bio::Gonzales::Graphics::BLAST qw(render); | ||
| 257 | |||
| 258 | =head1 DESCRIPTION | ||
| 259 | |||
| 260 | =head1 OPTIONS | ||
| 261 | |||
| 262 | =head1 SUBROUTINES | ||
| 263 | |||
| 264 | =over 4 | ||
| 265 | |||
| 266 | =item B<< $raw_image_data = render($file, \%options) >> | ||
| 267 | |||
| 268 | =item B<< $raw_image_data = render($fh, \%options) >> | ||
| 269 | |||
| 270 | Reads C<$file> or uses C<$fh> to get the results. Renders the results either as png or as svg graphics. | ||
| 271 | |||
| 272 | %standard_options = ( | ||
| 273 | blast_format => 'blast', | ||
| 274 | img_format => 'png', | ||
| 275 | evalue => undef, | ||
| 276 | ); | ||
| 277 | |||
| 278 | =back | ||
| 279 | |||
| 280 | |||
| 281 | =head1 SEE ALSO | ||
| 282 | |||
| 283 | =head1 AUTHOR | ||
| 284 | |||
| 285 | jw bargsten, C<< <joachim.bargsten at wur.nl> >> | ||
| 286 | |||
| 287 | =cut |