blib/script/blast2gff3.pl untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 96 CC 3 SCAR 14.6
CC
3
Cov
0%
write_hsp
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 4.3
1 #!/usr/bin/env perl
2  
3 use warnings;
4 use strict;
5  
6 use Data::Dumper;
7 use Carp;
8  
9 use 5.010;
10  
11 use Bio::Gonzales::Feat::IO::GFF3;
12 use Bio::Gonzales::Feat;
13 use Data::Printer;
14  
15 use Bio::SearchIO;
16 use Getopt::Long::Descriptive;
17  
18 my ( $opt, $usage ) = describe_options(
19 '%c %o <blast_result_file>',
20 [],
21 [ 'output_file|o=s', "output file name. If not set, stdout is used" ],
22 [ 'inv_query_subject|i', 'invert query and subject' ],
23 [ 'description|d', 'include the hit description as Note attribute' ],
24 [ 'help', "print usage message and exit" ],
25 [ 'best_hsp', 'one hit represents all hsps' ],
26 [ 'blast_format=s' , 'set the blast parser for Bio::SearchIO', {default => 'blast'}],
27 );
28  
29 print( $usage->text ), exit if $opt->help;
30  
31 my $infile = shift;
32  
33 die "could not open input file" unless ( $infile && -f $infile );
34 my $blastin = Bio::SearchIO->new(
35 -format => $opt->blast_format,
36 -file => $infile,
37 );
38  
39 my $gffout
40 = Bio::Gonzales::Feat::IO::GFF3->new(
41 ( $opt->output_file ? ( file => $opt->output_file ) : ( fh => \*STDOUT ) ),
42 mode => '>' );
43  
44 my %match_ids;
45 while ( my $result = $blastin->next_result() ) {
46 # Bio::Search::Result::ResultI
47 $gffout->write_comment( "\n# " . $result->query_name );
48 while ( my $hit = $result->next_hit ) {
49 # process the Bio::Search::Hit::HitI object
50  
51 while ( my $hsp = $hit->next_hsp ) {
52 write_hsp( $gffout, $opt, $hit, $hsp, $result );
53 last if ( $opt->best_hsp );
54 }
55 }
56 }
57  
58 sub write_hsp {
59 my ( $gffout, $opt, $hit, $hsp, $result ) = @_;
60  
61 ( my $hit_name = $hit->name ) =~ s/\|$//;
62  
63 my %query_data = (
64 name => $hsp->query->seq_id,
65 start => $hsp->start('query'),
66 end => $hsp->end('query'),
67 strand => $hsp->strand('query'),
68 length => $result->query_length,
69 );
70 my %subject_data = (
71 name => $hit_name, #$hit->name,
72 start => $hsp->start('subject'),
73 end => $hsp->end('subject'),
74 strand => $hsp->strand('subject'),
75 length => $hit->length,
76 );
77  
78 if ( $opt->inv_query_subject ) {
79 my %tmp = %query_data;
80 %query_data = %subject_data;
81 %subject_data = %tmp;
82 }
83  
84 #die Dumper $hsp->query;
85 # process the Bio::Search::HSP::HSPI object
86 my $sfeat = Bio::Gonzales::Feat->new(
87 seq_id => $query_data{name},
88 source => lc( $hit->algorithm ),
89 type => 'match',
90 start => $query_data{start},
91 end => $query_data{end},
92 strand => $query_data{strand},
93 score => $hsp->significance,
94 attributes => {
95 Target => [
96 join( " ",
97 @subject_data{qw/name start end/},
98 Bio::Gonzales::Feat::Convert_strand( $subject_data{strand} ) )
99 ],
100 ID => [ $subject_data{name} . "|hit_" . ++$match_ids{ $subject_data{name} } ],
101 QueryLength => [ $query_data{length} ],
102 SubjectLength => [ $subject_data{length} ],
103  
104 },
105 );
106 $sfeat->add_attr( Note => $hit->description ) if ( $opt->description );
107 $gffout->write_feat($sfeat);
108  
109 }