| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: inst-tax-filter.pl | ||
| 3 | # ABSTRACT: Apply a taxonomic filter to a (UniProt) FASTA database (optimized) | ||
| 4 | # CONTRIBUTOR: Valerian LUPO <valerian.lupo@uliege.be> | ||
| 5 | |||
| 6 | use Modern::Perl '2011'; | ||
| 7 | use autodie; | ||
| 8 | |||
| 9 | use File::Basename; | ||
| 10 | use Getopt::Euclid qw(:vars); | ||
| 11 | use Smart::Comments; | ||
| 12 | |||
| 13 | use Bio::MUST::Core; | ||
| 14 | use Bio::MUST::Core::Utils qw(secure_outfile); | ||
| 15 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 16 | use aliased 'Bio::MUST::Core::SeqId'; | ||
| 17 | use aliased 'Bio::MUST::Core::Taxonomy'; | ||
| 18 | |||
| 19 | # build taxonomy and filter objects | ||
| 20 | my $tax = Taxonomy->new_from_cache( tax_dir => $ARGV_taxdir ); | ||
| 21 | my $filter = $tax->tax_filter($ARGV_filter); | ||
| 22 | ### Active filter: $filter->all_specs | ||
| 23 | |||
| 24 | # regexes for capturing org component | ||
| 25 | my %regex_for = ( | ||
| 26 | ':UNI' => qr/OS=(.*) \s+ OX=/xms, | ||
| 27 | ); | ||
| 28 | |||
| 29 | # setup (optional) regex (first use input as hash key then as regex) | ||
| 30 | my $regex; | ||
| 31 | if ($ARGV_id_regex) { | ||
| 32 | $regex = $regex_for{$ARGV_id_regex} // $ARGV_id_regex; | ||
| 33 | ### Using seq id regex: $regex | ||
| 34 | } | ||
| 35 | |||
| 36 | my $tax_filter = sub { | ||
| 37 | my $seq = shift; | ||
| 38 | my $seq_id = $seq->seq_id; | ||
| 39 | |||
| 40 | # optionally extract org and update seq_id using specified regex | ||
| 41 | if ($regex) { | ||
| 42 | my ($org) = $seq_id->full_id =~ $regex; | ||
| 43 | # skip unknown orgs | ||
| 44 | return unless $org; # TODO: issue warning? | ||
| 45 | $seq_id = SeqId->new_with( | ||
| 46 | org => $org, accession => '1', keep_strain => 1 | ||
| 47 | ); | ||
| 48 | } | ||
| 49 | |||
| 50 | # skip unallowed seqs | ||
| 51 | return unless $filter->is_allowed($seq_id); | ||
| 52 | |||
| 53 | # store allowed seqs | ||
| 54 | my $str = '>' . $seq->full_id . "\n"; # use original seq_id | ||
| 55 | $str .= $seq->wrapped_str; | ||
| 56 | |||
| 57 | return $str; | ||
| 58 | }; | ||
| 59 | |||
| 60 | for my $infile (@ARGV_infiles) { | ||
| 61 | my ($filename) = fileparse($ARGV_filter, qr{\.[^.]*}xms); | ||
| 62 | my $outfile = secure_outfile($infile, "-$filename"); | ||
| 63 | |||
| 64 | ### Processing: $infile | ||
| 65 | Ali->instant_store( | ||
| 66 | $outfile, { infile => $infile, coderef => $tax_filter } | ||
| 67 | ); | ||
| 68 | } | ||
| 69 | |||
| 70 | __END__ |