| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: prune-outliers.pl | ||
| 3 | # ABSTRACT: Identify and discard outliers based on all-versus-all BLAST searches | ||
| 4 | # CONTRIBUTOR: Amandine BERTRAND <amandine.bertrand@doct.uliege.be> | ||
| 5 | # CONTRIBUTOR: Loic MEUNIER <loic.meunier@doct.uliege.be> | ||
| 6 | |||
| 7 | use Modern::Perl '2011'; | ||
| 8 | use autodie; | ||
| 9 | |||
| 10 | use File::Basename; | ||
| 11 | use File::Find::Rule; | ||
| 12 | use Getopt::Euclid qw(:vars); | ||
| 13 | use Path::Class qw(dir file); | ||
| 14 | use Smart::Comments; | ||
| 15 | |||
| 16 | use Bio::MUST::Core; | ||
| 17 | use Bio::MUST::Core::Constants qw(:dirs); | ||
| 18 | use Bio::MUST::Drivers; | ||
| 19 | |||
| 20 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 21 | use aliased 'Bio::MUST::Core::IdList'; | ||
| 22 | |||
| 23 | my $qr_class = 'Bio::MUST::Core::Ali::Temporary'; | ||
| 24 | my $db_class = 'Bio::MUST::Drivers::Blast::Database'; | ||
| 25 | my $db_tmp_class = 'Bio::MUST::Drivers::Blast::Database::Temporary'; | ||
| 26 | |||
| 27 | for my $indir (@ARGV_indirs) { | ||
| 28 | |||
| 29 | ### Processing: $indir | ||
| 30 | my @infiles = File::Find::Rule | ||
| 31 | ->file() | ||
| 32 | ->name( $SUFFICES_FOR{Ali} ) | ||
| 33 | ->in($indir); | ||
| 34 | |||
| 35 | for my $infile (@infiles) { | ||
| 36 | ### Processing: $infile | ||
| 37 | |||
| 38 | # BLASTP/N all versus all | ||
| 39 | my $db = $db_tmp_class->new( seqs => file($infile) ); | ||
| 40 | |||
| 41 | # Note: this is a special case where query and database are identical | ||
| 42 | # (but autodetection won't work because query is just a filename) | ||
| 43 | my $query = $db->filename; | ||
| 44 | my $pgm = $db->type eq 'nucl' ? 'blastn' : 'blastp'; | ||
| 45 | # TODO: support --tblastx option? (see elsewhere) | ||
| 46 | |||
| 47 | my $parser = $db->$pgm($query, { | ||
| 48 | -evalue => $ARGV_evalue, | ||
| 49 | -outfmt => 6, | ||
| 50 | $db->type eq 'nucl' ? ( -task => 'blastn' ) : () | ||
| 51 | } ); | ||
| 52 | |||
| 53 | # parsing | ||
| 54 | my %count_for; | ||
| 55 | while ( my $hit = $parser->next_hit ) { | ||
| 56 | my $query_id = $db->long_id_for( $hit->query_id ); | ||
| 57 | $count_for{$query_id}++; | ||
| 58 | } | ||
| 59 | |||
| 60 | my $outdir = dir($indir)->basename . '-pruned'; | ||
| 61 | |||
| 62 | for (my $t = $ARGV_min_ident; $t <= $ARGV_max_ident; $t += 0.1) { | ||
| 63 | |||
| 64 | my @ids | ||
| 65 | = grep { ($count_for{$_} / keys %count_for) >= $t } | ||
| 66 | grep { $count_for{$_} >= $ARGV_min_hits } keys %count_for | ||
| 67 | ; | ||
| 68 | |||
| 69 | ### threshold: $t . ' - ' . scalar @ids . ' seqs kept out of ' . scalar keys %count_for | ||
| 70 | |||
| 71 | my $ali = Ali->load($infile); | ||
| 72 | $ali->dont_guess if $ARGV_noguessing; | ||
| 73 | |||
| 74 | my $list = IdList->new( ids => \@ids ); | ||
| 75 | my $new_ali = $list->filtered_ali($ali); | ||
| 76 | |||
| 77 | # create output dirs named after input dir and identity threshold | ||
| 78 | my $subdir = dir( $outdir, $t )->relative; | ||
| 79 | $subdir->mkpath(); | ||
| 80 | |||
| 81 | # store Ali in corresponding dir | ||
| 82 | my ($filename) = fileparse($infile); | ||
| 83 | my $outfile = file($subdir, $filename)->stringify; | ||
| 84 | $new_ali->store($outfile); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | } | ||
| 89 | |||
| 90 | __END__ |