| 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 | |||
| 46 | my $parser = $db->$pgm($query, { | ||
| 47 | -evalue => $ARGV_evalue, | ||
| 48 | -outfmt => 6, | ||
| 49 | $db->type eq 'nucl' ? ( -task => 'blastn' ) : () | ||
| 50 | } ); | ||
| 51 | |||
| 52 | # parsing | ||
| 53 | my %count_for; | ||
| 54 | while ( my $hit = $parser->next_hit ) { | ||
| 55 | my $query_id = $db->long_id_for( $hit->query_id ); | ||
| 56 | $count_for{$query_id}++; | ||
| 57 | } | ||
| 58 | |||
| 59 | my $outdir = dir($indir)->basename . '-pruned'; | ||
| 60 | |||
| 61 | for (my $t = $ARGV_min_ident; $t <= $ARGV_max_ident; $t += 0.1) { | ||
| 62 | |||
| 63 | my @ids | ||
| 64 | = grep { ($count_for{$_} / keys %count_for) >= $t } | ||
| 65 | grep { $count_for{$_} >= $ARGV_min_hits } keys %count_for | ||
| 66 | ; | ||
| 67 | |||
| 68 | ### threshold: $t . ' - ' . scalar @ids . ' seqs kept out of ' . scalar keys %count_for | ||
| 69 | |||
| 70 | my $ali = Ali->load($infile); | ||
| 71 | $ali->dont_guess if $ARGV_noguessing; | ||
| 72 | |||
| 73 | my $list = IdList->new( ids => \@ids ); | ||
| 74 | my $new_ali = $list->filtered_ali($ali); | ||
| 75 | |||
| 76 | # create output dirs named after input dir and identity threshold | ||
| 77 | my $subdir = dir( $outdir, $t )->relative; | ||
| 78 | $subdir->mkpath(); | ||
| 79 | |||
| 80 | # store Ali in corresponding dir | ||
| 81 | my ($filename) = fileparse($infile); | ||
| 82 | my $outfile = file($subdir, $filename)->stringify; | ||
| 83 | $new_ali->store($outfile); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | } | ||
| 88 | |||
| 89 | __END__ | ||
| 90 | |||
| 91 | =pod | ||
| 92 | |||
| 93 | =head1 NAME | ||
| 94 | |||
| 95 | prune-outliers.pl - Identify and discard outliers based on all-versus-all BLAST searches | ||
| 96 | |||
| 97 | =head1 VERSION | ||
| 98 | |||
| 99 | version 0.213470 | ||
| 100 | |||
| 101 | =head1 USAGE | ||
| 102 | |||
| 103 | prune-outliers.pl <indirs> [options] | ||
| 104 | |||
| 105 | =head1 REQUIRED ARGUMENTS | ||
| 106 | |||
| 107 | =over | ||
| 108 | |||
| 109 | =item <indirs> | ||
| 110 | |||
| 111 | Path to input directories containing ALI (or FASTA) files [repeatable argument]. | ||
| 112 | |||
| 113 | =for Euclid: indirs.type: string | ||
| 114 | repeatable | ||
| 115 | |||
| 116 | =back | ||
| 117 | |||
| 118 | =head1 OPTIONS | ||
| 119 | |||
| 120 | =over | ||
| 121 | |||
| 122 | =item --evalue=<n> | ||
| 123 | |||
| 124 | evalue threshold for a hit to be considered during the all-versus-all BLAST | ||
| 125 | searches [default: n.default]. | ||
| 126 | |||
| 127 | =for Euclid: n.type: num | ||
| 128 | n.default: 1e-10 | ||
| 129 | |||
| 130 | =item --min-ident=<n> | --min_ident=<n> | ||
| 131 | |||
| 132 | Minimal identity value used for selecting sequences that match at least this | ||
| 133 | proportion in the all-versus-all BLAST searches [default: n.default]. An output | ||
| 134 | dir will be created by step of 0.1 between the min threshold and max threshold. | ||
| 135 | |||
| 136 | =for Euclid: n.type: num | ||
| 137 | n.default: 0.3 | ||
| 138 | |||
| 139 | =item --max-ident=<n> | --max_ident=<n> | ||
| 140 | |||
| 141 | Maximum percent value used for selecting sequences that match at least this | ||
| 142 | proportion in the all versus all BLAST searches [default: n.default]. An output | ||
| 143 | dir will be created by step of 0.1 between the min threshold and max threshold. | ||
| 144 | |||
| 145 | =for Euclid: n.type: num | ||
| 146 | n.default: 0.8 | ||
| 147 | |||
| 148 | =item --min-hits=<n> | --min_hits=<n> | ||
| 149 | |||
| 150 | Minimum number of hits in the all-versus-all BLAST searches required for a | ||
| 151 | sequence to be retained in the output file [default: n.default]. | ||
| 152 | |||
| 153 | =for Euclid: n.type: num | ||
| 154 | n.default: 10 | ||
| 155 | |||
| 156 | =item --[no]guessing | ||
| 157 | |||
| 158 | [Don't] guess whether sequences are aligned or not [default: yes]. | ||
| 159 | |||
| 160 | =item --version | ||
| 161 | |||
| 162 | =item --usage | ||
| 163 | |||
| 164 | =item --help | ||
| 165 | |||
| 166 | =item --man | ||
| 167 | |||
| 168 | Print the usual program information | ||
| 169 | |||
| 170 | =back | ||
| 171 | |||
| 172 | =head1 AUTHOR | ||
| 173 | |||
| 174 | Denis BAURAIN <denis.baurain@uliege.be> | ||
| 175 | |||
| 176 | =head1 CONTRIBUTORS | ||
| 177 | |||
| 178 | =for stopwords Amandine BERTRAND Loic MEUNIER | ||
| 179 | |||
| 180 | =over 4 | ||
| 181 | |||
| 182 | =item * | ||
| 183 | |||
| 184 | Amandine BERTRAND <amandine.bertrand@doct.uliege.be> | ||
| 185 | |||
| 186 | =item * | ||
| 187 | |||
| 188 | Loic MEUNIER <loic.meunier@doct.uliege.be> | ||
| 189 | |||
| 190 | =back | ||
| 191 | |||
| 192 | =head1 COPYRIGHT AND LICENSE | ||
| 193 | |||
| 194 | This software is copyright (c) 2013 by University of Liege / Unit of Eukaryotic Phylogenomics / Denis BAURAIN. | ||
| 195 | |||
| 196 | This is free software; you can redistribute it and/or modify it under | ||
| 197 | the same terms as the Perl 5 programming language system itself. | ||
| 198 | |||
| 199 | =cut |