| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: annotate-ali.pl | ||
| 3 | # ABSTRACT: Annotate sequences by homology search using BLAST | ||
| 4 | # CONTRIBUTOR: Valerian LUPO <valerian.lupo@uliege.be> | ||
| 5 | |||
| 6 | use Modern::Perl '2011'; | ||
| 7 | use autodie; | ||
| 8 | |||
| 9 | use Getopt::Euclid qw(:vars); | ||
| 10 | use Smart::Comments '###'; | ||
| 11 | |||
| 12 | use Tie::IxHash; | ||
| 13 | |||
| 14 | use Bio::MUST::Core; | ||
| 15 | use Bio::MUST::Core::Utils qw(secure_outfile); | ||
| 16 | use Bio::MUST::Drivers; | ||
| 17 | |||
| 18 | |||
| 19 | # TODO: add support for prebuilt reference database (e.g. nr) | ||
| 20 | |||
| 21 | # convert fractional identity threshold to percentage (see Euclid) | ||
| 22 | $ARGV_identity *= 100.0 if 0 < $ARGV_identity && $ARGV_identity <= 1; | ||
| 23 | |||
| 24 | ### Building database: $ARGV_ref_file | ||
| 25 | my $blastdb = Bio::MUST::Drivers::Blast::Database::Temporary->new( | ||
| 26 | seqs => $ARGV_ref_file | ||
| 27 | ); | ||
| 28 | |||
| 29 | for my $infile (@ARGV_infiles) { | ||
| 30 | |||
| 31 | ### Processing: $infile | ||
| 32 | my $queries = Bio::MUST::Core::Ali::Temporary->new( seqs => $infile ); | ||
| 33 | |||
| 34 | # let driver decide BLAST flavor except if nucl:nucl and --tblastx option | ||
| 35 | my $method = $queries->type eq 'nucl' && $blastdb->type eq 'nucl' | ||
| 36 | && $ARGV_tblastx ? 'tblastx' : 'blast'; | ||
| 37 | |||
| 38 | #### Performing BLAST... | ||
| 39 | my $parser = $blastdb->$method($queries, { | ||
| 40 | -outfmt => 6, | ||
| 41 | -evalue => $ARGV_evalue, | ||
| 42 | -max_target_seqs => $ARGV_max_hits | ||
| 43 | } ); | ||
| 44 | |||
| 45 | #### Parsing BLAST report... | ||
| 46 | tie my %ann_for, 'Tie::IxHash'; | ||
| 47 | tie my %hit_id_for, 'Tie::IxHash'; | ||
| 48 | my $curr_id = q{}; | ||
| 49 | |||
| 50 | HIT: | ||
| 51 | while ( my $hit = $parser->next_hit ) { | ||
| 52 | my ($qid, $hid, $identity) | ||
| 53 | = map { $hit->$_ } qw(query_id hit_id percent_identity); | ||
| 54 | |||
| 55 | next HIT if $identity < $ARGV_identity; # skip weak-identity hits | ||
| 56 | |||
| 57 | unless ($ARGV_hit_list) { # optionally | ||
| 58 | next HIT if $qid eq $curr_id; # skip non-first hits | ||
| 59 | $curr_id = $qid; | ||
| 60 | } | ||
| 61 | |||
| 62 | # capture annotation bit in ref seq_id using regex | ||
| 63 | my ($annotation) = $blastdb->long_id_for($hid) =~ $ARGV_ref_regex; | ||
| 64 | my $full_qid = $queries->long_id_for($qid); | ||
| 65 | $ann_for{$full_qid} //= $annotation; # use only first hit | ||
| 66 | |||
| 67 | # extract additional (optionally) wanted fields | ||
| 68 | my @wanted_fields = map { $hit->$_ } @ARGV_fields; | ||
| 69 | |||
| 70 | # collect all hits for at specified (E-value and) identity thresholds | ||
| 71 | push @{ $hit_id_for{$full_qid} }, { | ||
| 72 | annotation => $annotation, | ||
| 73 | fields => \@wanted_fields, | ||
| 74 | }; | ||
| 75 | } | ||
| 76 | ##### Annotations: %ann_for | ||
| 77 | |||
| 78 | # output annotation report | ||
| 79 | my @header = ('tag', 'id', @ARGV_fields); | ||
| 80 | say '# ' . join "\t", @header; | ||
| 81 | while (my ($id, $hits) = each %hit_id_for) { | ||
| 82 | for my $hit ( @{$hits} ) { | ||
| 83 | say join "\t", $hit->{annotation}, $id, @{ $hit->{fields} }; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | # optionally output annotated file | ||
| 88 | if ($ARGV_ann_file) { | ||
| 89 | my $ali = Bio::MUST::Core::Ali->load($infile); | ||
| 90 | $ali->dont_guess if $ARGV_noguessing; | ||
| 91 | my $outfile = secure_outfile($infile, $ARGV_out_suffix); | ||
| 92 | #### Writing annotated file: $outfile->stringify | ||
| 93 | prefix_ids($ali, \%ann_for)->store_fasta($outfile); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | # TODO: replace by or add some --store-id-mapper option? | ||
| 98 | # TODO: move into BMC::Ali | ||
| 99 | sub prefix_ids { | ||
| 100 | my $ali = shift; | ||
| 101 | my $ann_for = shift; | ||
| 102 | |||
| 103 | for my $seq ($ali->all_seqs) { | ||
| 104 | my $prefix = $ann_for->{$seq->full_id}; | ||
| 105 | $seq->set_seq_id( $prefix . q{-} . $seq->full_id ) if $prefix; | ||
| 106 | } | ||
| 107 | |||
| 108 | return $ali; | ||
| 109 | } | ||
| 110 | |||
| 111 | # TODO: check coherence of option layout with cdhit-tax-filter.pl | ||
| 112 | # e.g., replace --ann-file by --store-id-mapper | ||
| 113 | |||
| 114 | __END__ |