| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: fetch-tax.pl | ||
| 3 | # ABSTRACT: Fetch (and format) information from the NCBI Taxonomy database | ||
| 4 | |||
| 5 | use Modern::Perl '2011'; | ||
| 6 | use autodie; | ||
| 7 | |||
| 8 | use Getopt::Euclid qw(:vars); | ||
| 9 | use List::AllUtils qw(apply each_array); | ||
| 10 | use Scalar::Util qw(looks_like_number); | ||
| 11 | use Smart::Comments; | ||
| 12 | |||
| 13 | use Bio::MUST::Core; | ||
| 14 | use Bio::MUST::Core::Constants qw(:ncbi); | ||
| 15 | use Bio::MUST::Core::Utils qw(change_suffix); | ||
| 16 | use aliased 'Bio::MUST::Core::IdList'; | ||
| 17 | use aliased 'Bio::MUST::Core::SeqId'; | ||
| 18 | use aliased 'Bio::MUST::Core::Taxonomy'; | ||
| 19 | |||
| 20 | |||
| 21 | # build taxonomy object | ||
| 22 | my $tax = Taxonomy->new_from_cache( tax_dir => $ARGV_taxdir ); | ||
| 23 | |||
| 24 | # setup org-mapper (or NOM) IDM format if required | ||
| 25 | my $suffix = '.tax'; | ||
| 26 | my $sep = "\t"; | ||
| 27 | my $sort = 0; | ||
| 28 | my $classifier; | ||
| 29 | if ($ARGV_org_mapper) { | ||
| 30 | $ARGV_noitem = 1; | ||
| 31 | $ARGV_nomustid = 0; | ||
| 32 | $ARGV_notaxid = 0; | ||
| 33 | $ARGV_nolineage = 1; | ||
| 34 | $suffix = '.org-idm'; | ||
| 35 | } | ||
| 36 | if ($ARGV_legacy_nom) { | ||
| 37 | $ARGV_noitem = 0; # give precedence to items | ||
| 38 | $ARGV_nomustid = 1; # to ensure back-compatibility | ||
| 39 | $ARGV_notaxid = 1; | ||
| 40 | $ARGV_nolineage = 0; | ||
| 41 | $suffix = '.nom'; | ||
| 42 | $sep = q{ , }; | ||
| 43 | $sort = 1; | ||
| 44 | $ARGV_missing ||= 'UNKNOWN'; # default to q{} in Euclid... | ||
| 45 | @ARGV_levels = ( $ARGV_legacy_nom ); | ||
| 46 | |||
| 47 | # build classifier for legacy NOM file if legacy_nom is a readable FRA file | ||
| 48 | $classifier = $tax->tax_labeler_from_systematic_frame($ARGV_legacy_nom) | ||
| 49 | if $ARGV_legacy_nom =~ m/.fra$/xmgi && -e $ARGV_legacy_nom; | ||
| 50 | } | ||
| 51 | |||
| 52 | if (@ARGV_levels) { | ||
| 53 | ### Specified levels: @ARGV_levels | ||
| 54 | } | ||
| 55 | |||
| 56 | ### --item-type: $ARGV_item_type | ||
| 57 | |||
| 58 | # method, args and anon sub dispatch tables... | ||
| 59 | # ... for reading infiles | ||
| 60 | my $method = $ARGV_from_must ? 'load_lis' : 'load'; | ||
| 61 | my $args = { | ||
| 62 | column => $ARGV_column - 1, # 0-based numbering in lib | ||
| 63 | separator => $ARGV_separator, | ||
| 64 | }; | ||
| 65 | |||
| 66 | # .. and for fetching taxon_ids from items | ||
| 67 | my %fetch_from = ( | ||
| 68 | mustid => sub { map { $tax->get_taxid_from_seq_id( $_) } @_ }, | ||
| 69 | baseid => sub { map { $tax->get_taxid_from_seq_id( $_) } | ||
| 70 | map { $_ . '@1' } @_ }, | ||
| 71 | # we build a mustid from the baseid | ||
| 72 | strain => sub { map { $tax->get_taxid_from_seq_id( $_) } | ||
| 73 | map { SeqId->new_with( | ||
| 74 | org => $_, | ||
| 75 | accession => 1, | ||
| 76 | keep_strain => 1, | ||
| 77 | ) } @_ }, | ||
| 78 | # we build a mustid from the strain name | ||
| 79 | name => sub { map { $tax->get_taxid_from_name( $_) } @_ }, | ||
| 80 | # we directly use ..._from_name to use the whole item | ||
| 81 | lineage => sub { map { $tax->get_taxid_from_taxonomy( $_) } @_ }, | ||
| 82 | # we use the "ambiguous-taxa resistant" method based on lineages | ||
| 83 | gi => sub { map { $tax->get_taxid_from_seq_id( $_) } | ||
| 84 | map { $_ =~ $PKEYONLY ? 'gi|' . $_ : $_ } @_ }, | ||
| 85 | # we let ..._from_seq_id doing the GI number parsing | ||
| 86 | taxid => sub { @_ }, | ||
| 87 | ); | ||
| 88 | |||
| 89 | for my $infile (@ARGV_infiles) { | ||
| 90 | |||
| 91 | ### Processing: $infile | ||
| 92 | my $list = IdList->$method($infile, $args); | ||
| 93 | my $comment_n = $list->count_comments; | ||
| 94 | |||
| 95 | # fetch and clean up list items | ||
| 96 | # Note: using apply instead of map for satisfying Perl::Critic | ||
| 97 | my @items = apply { | ||
| 98 | s{\s+}{ }xmsg; # convert runs of whitespace to single whitespace | ||
| 99 | s{^\s+}{}xmsg; # trim spaces at beginning... | ||
| 100 | s{\s+$}{}xmsg; # ... and end of items | ||
| 101 | $_ | ||
| 102 | } $list->all_ids; | ||
| 103 | |||
| 104 | my @rows; | ||
| 105 | |||
| 106 | # fetch taxon_ids and assemble taxonomy lines | ||
| 107 | my @taxon_ids = $fetch_from{$ARGV_item_type}->(@items); | ||
| 108 | my $ea = each_array @items, @taxon_ids; | ||
| 109 | while (my ($item, $taxon_id) = $ea->() ) { | ||
| 110 | |||
| 111 | my ($must_id, $lineage); | ||
| 112 | if ($taxon_id) { | ||
| 113 | |||
| 114 | # fetch full taxonomy and lowest taxon | ||
| 115 | my @taxonomy = $tax->get_taxonomy($taxon_id); | ||
| 116 | my $org = $taxonomy[-1]; | ||
| 117 | |||
| 118 | # proceed only if valid taxon_id | ||
| 119 | if ($org) { | ||
| 120 | |||
| 121 | # build base MUST id... | ||
| 122 | $must_id = SeqId->new_with( | ||
| 123 | org => $org, | ||
| 124 | taxon_id => $taxon_id, | ||
| 125 | keep_strain => $ARGV_keep_strain, | ||
| 126 | )->full_id; | ||
| 127 | |||
| 128 | # ... and NCBI lineage | ||
| 129 | # ... using a classifier based on a systematic frame | ||
| 130 | if ($classifier) { | ||
| 131 | my $full_id = $taxon_id . '|X'; | ||
| 132 | @taxonomy = ( $classifier->classify($full_id) ); | ||
| 133 | } # Note: all this part is very inefficient but easy to code | ||
| 134 | |||
| 135 | # ...or using rank or rank numbers | ||
| 136 | elsif (@ARGV_levels) { # optionally filter (and reorder) taxa | ||
| 137 | @taxonomy = map { # handle both ranks and numeric levels | ||
| 138 | looks_like_number($_) ? $taxonomy[$_-1] // q{} | ||
| 139 | : $tax->get_term_at_level($taxon_id, $_) | ||
| 140 | } @ARGV_levels; | ||
| 141 | } | ||
| 142 | $lineage = join '; ', @taxonomy; | ||
| 143 | } | ||
| 144 | |||
| 145 | # otherwise nullify taxon_id (e.g., user-provided) | ||
| 146 | else { | ||
| 147 | $taxon_id = q{}; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | # ... depending on boolean switches! | ||
| 152 | my @data; | ||
| 153 | push @data, $item unless $ARGV_noitem; | ||
| 154 | push @data, ($must_id || $ARGV_missing) unless $ARGV_nomustid; | ||
| 155 | push @data, ($taxon_id || $ARGV_missing) unless $ARGV_notaxid; | ||
| 156 | push @data, ($lineage || $ARGV_missing) unless $ARGV_nolineage; | ||
| 157 | push @rows, \@data; | ||
| 158 | } | ||
| 159 | |||
| 160 | # output assembled taxonomy lines | ||
| 161 | my $outfile = change_suffix($infile, $suffix); | ||
| 162 | open my $out, '>', $outfile; | ||
| 163 | say {$out} '#' for 1..$comment_n; | ||
| 164 | @rows = sort { $a->[0] cmp $b->[0] } @rows if $sort; | ||
| 165 | say {$out} join $sep, @{$_} for @rows; | ||
| 166 | } | ||
| 167 | |||
| 168 | __END__ |