| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: physeter.pl | ||
| 3 | # ABSTRACT: Taxonomic parser for BLAST reports | ||
| 4 | # CONTRIBUTOR: Valerian LUPO <valerian.lupo@doct.uliege.be> | ||
| 5 | # CONTRIBUTOR: Mick VAN VLIERBERGHE <mvanvlierberghe@doct.uliege.be> | ||
| 6 | # CONTRIBUTOR: Luc CORNET <luc.cornet@uliege.be> | ||
| 7 | |||
| 8 | use Modern::Perl '2011'; | ||
| 9 | use autodie; | ||
| 10 | |||
| 11 | use Smart::Comments '###'; | ||
| 12 | |||
| 13 | use Getopt::Euclid qw(:vars); | ||
| 14 | |||
| 15 | use File::Basename; | ||
| 16 | use File::Find::Rule; | ||
| 17 | use File::Slurp; | ||
| 18 | use Path::Class 'file'; | ||
| 19 | use POSIX qw(ceil); | ||
| 20 | use List::AllUtils qw(apply sum count_by shuffle); | ||
| 21 | use Tie::IxHash; | ||
| 22 | |||
| 23 | use Bio::MUST::Core; | ||
| 24 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 25 | use aliased 'Bio::MUST::Core::Taxonomy'; | ||
| 26 | use Bio::FastParsers; | ||
| 27 | use aliased 'Bio::FastParsers::Blast::Table'; | ||
| 28 | |||
| 29 | |||
| 30 | die <<'EOT' if $ARGV_tax_min_hits > $ARGV_tax_max_hits; | ||
| 31 | --tax-max-hits must be greater or equal to --tax-min-hits; aborting! | ||
| 32 | EOT | ||
| 33 | |||
| 34 | # build taxonomy object | ||
| 35 | my $tax = Taxonomy->new_from_cache(tax_dir => $ARGV_taxdir); | ||
| 36 | |||
| 37 | # TODO: fix this (make it optional?) | ||
| 38 | # build labeler | ||
| 39 | my $labeler = $tax->tax_labeler_from_list($ARGV_taxon_list); | ||
| 40 | |||
| 41 | # k-folds method | ||
| 42 | my $subsets; | ||
| 43 | if ($ARGV_kfold) { | ||
| 44 | srand($ARGV_kfold_seed) if $ARGV_kfold_seed; | ||
| 45 | $subsets = split_db($ARGV_kfold); | ||
| 46 | } | ||
| 47 | |||
| 48 | my $seq_n; # improve this | ||
| 49 | my @results; | ||
| 50 | |||
| 51 | FILE: | ||
| 52 | for my $infile (@ARGV_infiles) { | ||
| 53 | |||
| 54 | ### Processing: $infile | ||
| 55 | my ($basename) = fileparse($infile, qr{\.[^.]*}xms); | ||
| 56 | |||
| 57 | # try to fetch expected taxon from CLI... | ||
| 58 | my $query_org = q{}; | ||
| 59 | my $exp_tax = $ARGV_expected_taxon; | ||
| 60 | |||
| 61 | unless ($ARGV_auto_detect) { | ||
| 62 | # ... otherwise determine it (and query org) from infile basename | ||
| 63 | unless ($exp_tax) { | ||
| 64 | # TODO: allow for a regex to capture GCA number | ||
| 65 | my @taxonomy = $tax->get_taxonomy($basename); | ||
| 66 | $query_org = $taxonomy[-1]; | ||
| 67 | $exp_tax = $labeler->classify( | ||
| 68 | \@taxonomy, { greedy => $ARGV_greedy_taxa } | ||
| 69 | ); | ||
| 70 | } | ||
| 71 | |||
| 72 | # skip infiles with undetermined expected taxon | ||
| 73 | unless ($exp_tax) { | ||
| 74 | warn <<"EOT"; | ||
| 75 | Warning: cannot determine expected taxon for organism; skipping infile! | ||
| 76 | Check <infiles> basename ($basename) or --taxon-list ($ARGV_taxon_list) content. | ||
| 77 | EOT | ||
| 78 | next FILE; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | # determine number of seqs | ||
| 83 | my $fasfile = file($ARGV_fasta_dir, "$basename.fasta"); | ||
| 84 | $seq_n = Ali->instant_count($fasfile); | ||
| 85 | |||
| 86 | unless ($ARGV_kfold) { | ||
| 87 | # read BLAST report infile and compute LCAs | ||
| 88 | my $lca_for = parse_report($infile, $query_org); | ||
| 89 | |||
| 90 | # determine expected taxon from BLAST infile | ||
| 91 | $exp_tax = auto_detect($lca_for) if $ARGV_auto_detect; | ||
| 92 | |||
| 93 | # create kraken report if asked to do so | ||
| 94 | if ($ARGV_kraken) { | ||
| 95 | my $outfile = "$basename-kraken.tsv"; | ||
| 96 | store_kraken($outfile, $lca_for); | ||
| 97 | } | ||
| 98 | |||
| 99 | # create anvio report if asked to do so | ||
| 100 | if ($ARGV_anvio) { | ||
| 101 | my $outfile = "$basename-anvio.tsv"; | ||
| 102 | store_anvio($outfile, $lca_for); | ||
| 103 | } | ||
| 104 | |||
| 105 | # create krona report if asked to do so | ||
| 106 | # TODO: account for unclassified? | ||
| 107 | if ($ARGV_krona) { | ||
| 108 | my $outfile = "$basename-krona.tsv"; | ||
| 109 | store_krona($outfile, $lca_for); | ||
| 110 | } | ||
| 111 | |||
| 112 | # create a LCA report if asked to do so | ||
| 113 | if ($ARGV_lca) { | ||
| 114 | my $outfile = "$basename-lca.tsv"; | ||
| 115 | store_lca($outfile, $lca_for); | ||
| 116 | } | ||
| 117 | |||
| 118 | format_results($lca_for, $basename, $exp_tax); | ||
| 119 | } | ||
| 120 | |||
| 121 | if ($ARGV_kfold) { | ||
| 122 | |||
| 123 | # read BLAST report infile in kfold mode | ||
| 124 | my @kfold_lcas = map { | ||
| 125 | parse_report($infile, $query_org, $_) | ||
| 126 | } @{$subsets}; | ||
| 127 | |||
| 128 | # TODO: try to refactor to avoid code redundancy here | ||
| 129 | if ($ARGV_auto_detect) { | ||
| 130 | # determine expected taxon from BLAST infile | ||
| 131 | my @exp_taxs = map { auto_detect($_) } @kfold_lcas; | ||
| 132 | my $len = @kfold_lcas; | ||
| 133 | format_results( $kfold_lcas[$_], $basename, $exp_taxs[$_] ) | ||
| 134 | for 0..$len-1; | ||
| 135 | } | ||
| 136 | else { | ||
| 137 | format_results( $_, $basename, $exp_tax ) | ||
| 138 | for @kfold_lcas; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | # store default report | ||
| 144 | store_results($ARGV_outfile, \@results); | ||
| 145 | |||
| 146 | # functions | ||
| 147 | |||
| 148 | sub split_db { | ||
| 149 | my $infile = shift; | ||
| 150 | |||
| 151 | # TODO: use slurp method from Path::Class to avoid another module | ||
| 152 | # TODO: slightly revise naming (e.g. no plural for @shuffle) | ||
| 153 | my @taxids = read_file($infile, chomp => 1); | ||
| 154 | my @shuffle = shuffle @taxids; | ||
| 155 | my $sub_size = ceil( @shuffle / 10 ); | ||
| 156 | |||
| 157 | my @subsets; | ||
| 158 | while (@shuffle) { | ||
| 159 | push @subsets, [ splice @shuffle, 0, $sub_size ]; | ||
| 160 | } | ||
| 161 | |||
| 162 | return \@subsets; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub parse_report { | ||
| 166 | my $infile = shift; | ||
| 167 | my $query_org = shift; | ||
| 168 | my $taxids = shift // (); | ||
| 169 | |||
| 170 | # setup boolean filter for current database subset (if any) | ||
| 171 | my %unwanted = map { $_ => 1 } @{$taxids}; | ||
| 172 | |||
| 173 | my $report = Table->new( file => $infile ); | ||
| 174 | |||
| 175 | tie my %lca_for, 'Tie::IxHash'; | ||
| 176 | |||
| 177 | my $curr_query = q{}; | ||
| 178 | my $best_score; | ||
| 179 | my @relatives; | ||
| 180 | |||
| 181 | my $method = 'next_hit'; | ||
| 182 | |||
| 183 | HIT: | ||
| 184 | while (my $hit = $report->$method) { | ||
| 185 | |||
| 186 | # at each new query... | ||
| 187 | if ($hit->query_id ne $curr_query) { | ||
| 188 | |||
| 189 | # store query LCA if enough relatives | ||
| 190 | # TODO: make Taxonomy:: class for collections of lineages | ||
| 191 | if (@relatives >= $ARGV_tax_min_hits) { | ||
| 192 | $lca_for{$curr_query} = { | ||
| 193 | taxon => scalar $tax->compute_lca( | ||
| 194 | $ARGV_tax_min_lca_freq, @relatives), | ||
| 195 | rel_n => scalar @relatives, | ||
| 196 | }; | ||
| 197 | } | ||
| 198 | |||
| 199 | # prepare analysis of new query | ||
| 200 | $curr_query = q{}; | ||
| 201 | $best_score = undef; | ||
| 202 | @relatives = (); | ||
| 203 | |||
| 204 | $method = 'next_hit'; | ||
| 205 | } | ||
| 206 | |||
| 207 | # skip extra (unused) relatives | ||
| 208 | if (@relatives >= $ARGV_tax_max_hits) { | ||
| 209 | $method = 'next_query'; | ||
| 210 | next HIT; | ||
| 211 | } | ||
| 212 | |||
| 213 | # skip weak hits (classical mode) | ||
| 214 | next HIT if $hit->hsp_length < $ARGV_tax_min_len; | ||
| 215 | next HIT if $hit->percent_identity < $ARGV_tax_min_ident; | ||
| 216 | next HIT if $hit->bit_score < $ARGV_tax_min_score; | ||
| 217 | |||
| 218 | # fetch hit taxonomy and org | ||
| 219 | # optimized code (requires taxon_id|accession seq_ids) | ||
| 220 | my $taxon_id = ( split m{\|}xms, $hit->hit_id )[0]; | ||
| 221 | |||
| 222 | # k-folds mode (skip hits from current database subset) | ||
| 223 | next HIT if $ARGV_kfold && $unwanted{$taxon_id}; | ||
| 224 | |||
| 225 | my @taxonomy = $tax->get_taxonomy($taxon_id); | ||
| 226 | # regular code (more general) | ||
| 227 | # my @taxonomy = $tax->get_taxonomy_from_seq_id( $hit->hit_id ); | ||
| 228 | my $hit_org = $taxonomy[-1]; | ||
| 229 | |||
| 230 | # skip self hits (corresponding to query_org) | ||
| 231 | next HIT if $hit_org eq $query_org; | ||
| 232 | |||
| 233 | # skip weak hits (MEGAN-like mode) | ||
| 234 | $best_score //= $hit->bit_score; | ||
| 235 | next HIT if $hit->bit_score < $ARGV_tax_score_mul * $best_score; | ||
| 236 | |||
| 237 | # retain hit taxonomy as relative | ||
| 238 | $curr_query = $hit->query_id; | ||
| 239 | push @relatives, \@taxonomy; | ||
| 240 | } | ||
| 241 | |||
| 242 | # store last query LCA if enough relatives | ||
| 243 | if ($curr_query && @relatives >= $ARGV_tax_min_hits) { | ||
| 244 | $lca_for{$curr_query} = { | ||
| 245 | taxon => scalar $tax->compute_lca( | ||
| 246 | $ARGV_tax_min_lca_freq, @relatives), | ||
| 247 | rel_n => scalar @relatives, | ||
| 248 | }; | ||
| 249 | } | ||
| 250 | |||
| 251 | return \%lca_for; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub auto_detect { | ||
| 255 | my $lca_for = shift; | ||
| 256 | |||
| 257 | # TODO: implement and compare the opposite approach where we first label | ||
| 258 | # then determine the exp_tax... | ||
| 259 | |||
| 260 | # count LCA lineage occurrences (i.e., sort | uniq -c) | ||
| 261 | my %count_for = count_by { join q{;}, @{ $_->{taxon} } } values %{$lca_for}; | ||
| 262 | |||
| 263 | # compute LCA proportions | ||
| 264 | my @taxa = sort { | ||
| 265 | $count_for{$b} <=> $count_for{$a} | ||
| 266 | } keys %count_for; | ||
| 267 | |||
| 268 | my $exp_tax = $labeler->classify( | ||
| 269 | $taxa[0], { greedy => $ARGV_greedy_taxa } | ||
| 270 | ); | ||
| 271 | |||
| 272 | return $exp_tax; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub tabulate_lcas { | ||
| 276 | my $lca_for = shift; | ||
| 277 | my $exp_tax = shift; | ||
| 278 | |||
| 279 | my $self_n = 0; | ||
| 280 | my $foreign_n = 0; | ||
| 281 | my $unknown_n = 0; | ||
| 282 | |||
| 283 | my %count_for; | ||
| 284 | |||
| 285 | # tabulate LCAs | ||
| 286 | while ( my ($query_id, $lca) = each %{$lca_for} ) { | ||
| 287 | |||
| 288 | # label LCA | ||
| 289 | my $got_tax = $labeler->classify( | ||
| 290 | $lca->{taxon}, { greedy => $ARGV_greedy_taxa } | ||
| 291 | ); | ||
| 292 | |||
| 293 | # update counts based on LCA label | ||
| 294 | unless ($got_tax) { $unknown_n++ } | ||
| 295 | elsif ($got_tax eq $exp_tax) { $self_n++ } | ||
| 296 | else { $foreign_n++; | ||
| 297 | $count_for{$got_tax}++; | ||
| 298 | } | ||
| 299 | } | ||
| 300 | |||
| 301 | # compute class proportions | ||
| 302 | # TODO: decide on a data structure to include all this | ||
| 303 | my $self_p = sprintf "%.2f", 100 * $self_n / $seq_n; | ||
| 304 | my $foreign_p = sprintf "%.2f", 100 * $foreign_n / $seq_n; | ||
| 305 | my $unknown_p = sprintf "%.2f", 100 * $unknown_n / $seq_n; | ||
| 306 | my $unclass_p = sprintf "%.2f", 100 - ($self_p + $foreign_p + $unknown_p); | ||
| 307 | |||
| 308 | # compute LCA proportions | ||
| 309 | my @taxa = sort { | ||
| 310 | $count_for{$b} <=> $count_for{$a} || $a cmp $b | ||
| 311 | } keys %count_for; | ||
| 312 | my @details = map { | ||
| 313 | sprintf "%s=%.2f", $_, 100 * $count_for{$_} / $seq_n | ||
| 314 | } @taxa; | ||
| 315 | |||
| 316 | my $result = join "\t", $self_p, $foreign_p, $unknown_p, $unclass_p, | ||
| 317 | join q{,}, @details; | ||
| 318 | |||
| 319 | return $result; | ||
| 320 | } | ||
| 321 | |||
| 322 | sub format_results { | ||
| 323 | my $lca_for = shift; | ||
| 324 | my $basename = shift; | ||
| 325 | my $exp_tax = shift; | ||
| 326 | |||
| 327 | # compute mean number of relatives used for LCA inference | ||
| 328 | my @rel_counts = map { $_->{rel_n} } values %{$lca_for}; | ||
| 329 | my $rel_mean | ||
| 330 | = @rel_counts ? sprintf "%.2f", sum(@rel_counts) / @rel_counts : 'NA'; | ||
| 331 | |||
| 332 | # format default report line | ||
| 333 | # TODO: better specify this format | ||
| 334 | push @results, join "\t", $basename, $exp_tax, | ||
| 335 | tabulate_lcas($lca_for, $exp_tax), $rel_mean; | ||
| 336 | |||
| 337 | return; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub store_results { | ||
| 341 | my $outfile = shift; | ||
| 342 | my $results = shift; | ||
| 343 | |||
| 344 | open my $out, '>', $outfile; | ||
| 345 | say {$out} join "\n", @{$results}; | ||
| 346 | |||
| 347 | return; | ||
| 348 | } | ||
| 349 | |||
| 350 | # TODO: improve and move to BMC::Taxonomy | ||
| 351 | sub store_lca { | ||
| 352 | my $outfile = shift; | ||
| 353 | my $lca_for = shift; | ||
| 354 | |||
| 355 | open my $out, '>', $outfile; | ||
| 356 | |||
| 357 | say {$out} '# ' . join "\t", qw(query_id rel_n lca_lineage); | ||
| 358 | while ( my ($query_id, $lca) = each %{$lca_for} ) { | ||
| 359 | say {$out} join "\t", | ||
| 360 | $query_id, $lca->{rel_n}, join q{; }, @{ $lca->{taxon} }; | ||
| 361 | } | ||
| 362 | |||
| 363 | return; | ||
| 364 | } | ||
| 365 | |||
| 366 | # TODO: move to BMC::Taxonomy | ||
| 367 | sub store_anvio { | ||
| 368 | my $outfile = shift; | ||
| 369 | my $lca_for = shift; | ||
| 370 | |||
| 371 | open my $out, '>', $outfile; | ||
| 372 | my @ranks = qw(superkingdom phylum class order family genus species); | ||
| 373 | say {$out} join "\t", 'gene_callers_id', map { "t_$_" } @ranks; | ||
| 374 | |||
| 375 | while ( my ($query_id, $lca) = each %{$lca_for} ) { | ||
| 376 | my $taxon_id = $tax->get_taxid_from_taxonomy( $lca->{taxon} ); | ||
| 377 | my @taxa = $tax->get_taxa_from_taxid($taxon_id, @ranks); | ||
| 378 | say {$out} join "\t", $query_id, apply { s/undef//xms } @taxa; | ||
| 379 | } | ||
| 380 | |||
| 381 | return; | ||
| 382 | } | ||
| 383 | |||
| 384 | # TODO: move to BMC::Taxonomy | ||
| 385 | # TODO: check how to avoid OTUsamples2krona.sh (different format then) | ||
| 386 | sub store_krona { | ||
| 387 | my $outfile = shift; | ||
| 388 | my $lca_for = shift; | ||
| 389 | |||
| 390 | open my $out, '>', $outfile; | ||
| 391 | say {$out} join "\t", qw(sample lineage); | ||
| 392 | |||
| 393 | my %count_for = count_by { join q{;}, @{ $_->{taxon} } } values %{$lca_for}; | ||
| 394 | |||
| 395 | for my $lineage (sort keys %count_for) { | ||
| 396 | my $count = $count_for{$lineage}; | ||
| 397 | $lineage =~ s/\s/_/xmsg; | ||
| 398 | say {$out} join "\t", $count, $lineage; | ||
| 399 | } | ||
| 400 | |||
| 401 | return; | ||
| 402 | } | ||
| 403 | |||
| 404 | # TODO: move to BMC::Taxonomy | ||
| 405 | sub store_kraken { | ||
| 406 | my $outfile = shift; | ||
| 407 | my $lca_for = shift; | ||
| 408 | |||
| 409 | # 4. A rank code, indicating (U)nclassified, (R)oot, (D)omain, (K)ingdom, | ||
| 410 | # (P)hylum, (C)lass, (O)rder, (F)amily, (G)enus, or (S)pecies. | ||
| 411 | # Taxa that are not at any of these 10 ranks have a rank code that is | ||
| 412 | # formed by using the rank code of the closest ancestor rank with | ||
| 413 | # a number indicating the distance from that rank. E.g., "G2" is a | ||
| 414 | # rank code indicating a taxon is between genus and species and the | ||
| 415 | # grandparent taxon is at the genus rank. | ||
| 416 | |||
| 417 | open my $out, '>', $outfile; | ||
| 418 | |||
| 419 | # count LCA lineage occurrences (i.e., sort | uniq -c) | ||
| 420 | my %count_for = count_by { join q{;}, @{ $_->{taxon} } } values %{$lca_for}; | ||
| 421 | |||
| 422 | # compute unclassified number | ||
| 423 | my $unclass_n = $seq_n - sum values %count_for; | ||
| 424 | |||
| 425 | # generate kraken-like rank_codes | ||
| 426 | my %code_for = map { | ||
| 427 | $_ eq 'domain' ? 'superkingdom' : $_ => uc substr($_, 0, 1) | ||
| 428 | } qw(unclassified root domain kingdom phylum class order family genus species); | ||
| 429 | |||
| 430 | # setup two main levels of taxonomic tree | ||
| 431 | my %tree = ( | ||
| 432 | unclassified => { | ||
| 433 | taxon_id => 0, | ||
| 434 | rank_code => $code_for{unclassified}, | ||
| 435 | coverage => $unclass_n, | ||
| 436 | count => $unclass_n, | ||
| 437 | # no children for unclassified | ||
| 438 | }, | ||
| 439 | root => { | ||
| 440 | taxon_id => 1, | ||
| 441 | rank_code => $code_for{root}, | ||
| 442 | coverage => 0, | ||
| 443 | count => 0, | ||
| 444 | children => {}, | ||
| 445 | }, | ||
| 446 | ); | ||
| 447 | |||
| 448 | # break all LCA lineages down into decorated taxonomic tree | ||
| 449 | for my $lineage (reverse sort keys %count_for) { | ||
| 450 | my $count = $count_for{$lineage}; | ||
| 451 | |||
| 452 | # fetch taxonomy with ranks from lineage | ||
| 453 | my $taxon_id = $tax->get_taxid_from_taxonomy($lineage); | ||
| 454 | my @taxonomy = $tax->get_taxonomy_with_levels($taxon_id); | ||
| 455 | |||
| 456 | # start count propagation with tree root | ||
| 457 | # Note: could we get root count different from zero? | ||
| 458 | $tree{root}{coverage} += $count; | ||
| 459 | |||
| 460 | # setup lineage break-down into taxa | ||
| 461 | my $tree_ref = $tree{root}{children}; | ||
| 462 | my $curr_rank_code = $code_for{root}; | ||
| 463 | my @taxa; # to keep track of partial lineage | ||
| 464 | |||
| 465 | while (my $taxon = shift @taxonomy) { | ||
| 466 | |||
| 467 | # extract next taxon and rank | ||
| 468 | my ($taxon, $rank) = @{$taxon}; | ||
| 469 | push @taxa, $taxon; | ||
| 470 | |||
| 471 | # create taxon entry (if not yet existing) | ||
| 472 | unless ($tree_ref->{$taxon}{taxon_id}) { | ||
| 473 | |||
| 474 | # fetch taxon_id for taxon (using partial lineage as fall-back) | ||
| 475 | $tree_ref->{$taxon}{taxon_id} | ||
| 476 | = $tax->get_taxid_from_taxonomy( \@taxa ); | ||
| 477 | |||
| 478 | # recode rank (= kraken-like rank_code) | ||
| 479 | my $rank_code = $code_for{$rank}; # try standard rank | ||
| 480 | $rank_code //= length($curr_rank_code) == 1 # or incr last | ||
| 481 | ? $curr_rank_code . '1' : ++$curr_rank_code; # rank (from 1) | ||
| 482 | $tree_ref->{$taxon}{rank_code} = $rank_code; | ||
| 483 | |||
| 484 | # setup count (this avoids tracking undef later) | ||
| 485 | $tree_ref->{$taxon}{count} = 0; | ||
| 486 | } | ||
| 487 | |||
| 488 | # propagate count to taxon (= kraken-like coverage) | ||
| 489 | $tree_ref->{$taxon}{coverage} += $count; | ||
| 490 | |||
| 491 | # setup children entry if lineage not yet exausted | ||
| 492 | # ... and remember rank_code (in case next taxon has no rank) | ||
| 493 | if (@taxonomy) { | ||
| 494 | $curr_rank_code = $tree_ref->{$taxon}{rank_code}; | ||
| 495 | $tree_ref->{$taxon}{children} //= {}; | ||
| 496 | $tree_ref = $tree_ref->{$taxon}{children}; | ||
| 497 | } | ||
| 498 | |||
| 499 | # otherwise cumulate count for taxon (= kraken-like count) | ||
| 500 | else { | ||
| 501 | $tree_ref->{$taxon}{count} += $count; | ||
| 502 | } | ||
| 503 | } | ||
| 504 | } | ||
| 505 | |||
| 506 | # output taxonomic tree recursively | ||
| 507 | _dump_tree_level( $out, 0, $_, $tree{$_} ) for qw(unclassified root); | ||
| 508 | |||
| 509 | return; | ||
| 510 | } | ||
| 511 | |||
| 512 | sub _dump_tree_level { | ||
| 513 | my ($out, $depth, $taxon, $tree_ref) = @_; | ||
| 514 | |||
| 515 | say {$out} join "\t", | ||
| 516 | (sprintf "%5.2f", 100.0 * $tree_ref->{coverage} / $seq_n), | ||
| 517 | $tree_ref->{coverage}, | ||
| 518 | $tree_ref->{count}, | ||
| 519 | $tree_ref->{rank_code}, | ||
| 520 | $tree_ref->{taxon_id}, | ||
| 521 | q{ } x ($depth * 2) . $taxon | ||
| 522 | ; | ||
| 523 | |||
| 524 | my $children_for = $tree_ref->{children}; | ||
| 525 | return unless $children_for; | ||
| 526 | |||
| 527 | my @children = sort { | ||
| 528 | $children_for->{$b}{coverage} <=> $children_for->{$a}{coverage} | ||
| 529 | } keys %{$children_for}; | ||
| 530 | |||
| 531 | _dump_tree_level( $out, $depth+1, $_, $children_for->{$_} ) for @children; | ||
| 532 | |||
| 533 | return; | ||
| 534 | } | ||
| 535 | |||
| 536 | __END__ | ||
| 537 | |||
| 538 | =pod | ||
| 539 | |||
| 540 | =head1 NAME | ||
| 541 | |||
| 542 | physeter.pl - Taxonomic parser for BLAST reports | ||
| 543 | |||
| 544 | =head1 VERSION | ||
| 545 | |||
| 546 | version 0.202960 | ||
| 547 | |||
| 548 | =head1 USAGE | ||
| 549 | |||
| 550 | |||||\\\\\\\\\\\\\\\\\||| | ||
| 551 | ||\\\\|||| |||\\\|| | ||
| 552 | ||\\\||| |\\\ | ||
| 553 | |\\\| |||||| ||\\\\\\@@@\| ||||| | ||
| 554 | @ |\\\| |\\\\\\\\\\\|| \\\\@@@@@@@@@@@\\ |\\||||\\| | ||
| 555 | \@@| \|||\\| \| |\\\| |\\\\ \\@@@@@@@@@@@@@@@@\\\ |\\ | ||
| 556 | \@@@\| \\||\ |\| \\\ |\\\\\\|\@@@@@@@@@@@@@@@@@@| \\| | ||
| 557 | \@@@@@\| |\||\| \\\\ ||\\\\\\\\\\@@@\\@@@@@@@@@@@@@@@@@@@\ |\\\\\\\\\\ | ||
| 558 | \@@@@@@@@\ \\|| |\\\| \\\| @@@@@\\@@@@@@@@@@@@@@@@@@@@@| |\\ |\\ | ||
| 559 | \@@@@@@@@@@\| \\||\\\\\| @@@@@@\\\\\@@@@@@@@@@@@@@@@@@@@@\ \\ \\| | ||
| 560 | \\@@@@@@@@@@@@@@\\ \\| @@@@@@@@\\\@@@@@@@@@@@@@@@@@@@@@@@@\\ |\\ \\ | ||
| 561 | \\@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\\@@@@@@@@@@@@@\\@@@@@@@@@@@@@@@@@@@@@@@@\\||\\\\ |\\| | ||
| 562 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|@@@@@@@@@@@@\\@@@@@@@@@@@@@| |@@@@@\\\\|||\\\\ |\\| | ||
| 563 | |@@@@@@@@\\| |\\@@@@@@@@@@@@@@@@@@@@\\@@@@@@@@@@@@\@@@@@@@@@|@@@@@@@@\\||||@@@@@@@\ \\ | ||
| 564 | \@@@@@\\| \\@@@@@@@@@@@@@@@@@\\@@@@@@@@@@@@@@@@@@@@@@@\|@@@@@@\\\\\\\@@@@@\ \\ | ||
| 565 | \@@@\\| \\@@@@@@@@@@@@@@\\@@@@@@@@@@@@@@@@@@@@@@@@\\@@@@@\||\||||\| \\ | ||
| 566 | \\@@@@@@@@@@@\\@@@@@@@@@@@@@@@@@@@@@@@@@@\\\@\\\|\\|||\ \ | ||
| 567 | |\@@@@@@@\\\@@@@@@@@@@@@@@@@@@@@@@@@@@@\\|| |\|\|||\| | ||
| 568 | |@@@\\\@@@@@@@@@@@@@@@@@@@@@@@@\\\\ |\|\||\| | ||
| 569 | |\\\\\\\||@@@@@@@@@@@@@@@@@@@@@\| |\| \|\||\ | ||
| 570 | |\\\\|| \@@@\| |\| \|\|\ | ||
| 571 | |@@\ |\\ |\\\\\\| | ||
| 572 | \@ \| \||\\||\ | ||
| 573 | |\ |\|\||\ | ||
| 574 | |\\\| | ||
| 575 | \\ | ||
| 576 | |||
| 577 | physeter.pl <infiles> --outfile=<file> --taxdir=<dir> --taxon-list=<file> \ | ||
| 578 | [optional arguments] | ||
| 579 | |||
| 580 | =head1 REQUIRED ARGUMENTS | ||
| 581 | |||
| 582 | =over | ||
| 583 | |||
| 584 | =item <infiles> | ||
| 585 | |||
| 586 | Path to input BLAST report files [repeatable argument]. | ||
| 587 | |||
| 588 | Report files must be named <assembly_accession.blastx> unless the organism does | ||
| 589 | not have an assembly accession. In the latter case, use the C<--exp-tax> option | ||
| 590 | to provide the expected taxonomy of the organism. | ||
| 591 | |||
| 592 | =for Euclid: infiles.type: readable | ||
| 593 | repeatable | ||
| 594 | |||
| 595 | =item --outfile=<file> | ||
| 596 | |||
| 597 | Path to output file. | ||
| 598 | |||
| 599 | =for Euclid: file.type: writable | ||
| 600 | |||
| 601 | =for Euclid: file.type: readable | ||
| 602 | |||
| 603 | =item --taxdir=<dir> | ||
| 604 | |||
| 605 | Path to local mirror of the NCBI Taxonomy database. | ||
| 606 | |||
| 607 | =for Euclid: dir.type: string | ||
| 608 | |||
| 609 | =item --taxon-list=<file> | ||
| 610 | |||
| 611 | List of taxa to consider when looking for foreign sequences. This labeler file | ||
| 612 | is used throughout the program to truncate LCA lineages at specific taxonomic | ||
| 613 | levels (which can vary from one lineage to the other). | ||
| 614 | |||
| 615 | =back | ||
| 616 | |||
| 617 | =head1 OPTIONS | ||
| 618 | |||
| 619 | =over | ||
| 620 | |||
| 621 | =item --fasta-dir=<dir> | ||
| 622 | |||
| 623 | Path to the directory holding the FASTA query files [default: dir.default]. | ||
| 624 | FASTA files must have the same basenames as BLAST infiles. | ||
| 625 | |||
| 626 | =for Euclid: dir.type: readable | ||
| 627 | dir.default: './' | ||
| 628 | |||
| 629 | =item --exp[ected]-tax[on]=<string> | ||
| 630 | |||
| 631 | Organism taxon [default: automatic]. Use this option when the organism does not | ||
| 632 | have an assembly accession. The specified taxon must be in the file provided | ||
| 633 | with the C<--taxon-list> option. | ||
| 634 | |||
| 635 | =for Euclid: string.type: string | ||
| 636 | |||
| 637 | =item --auto-detect | ||
| 638 | |||
| 639 | Determine organism taxon based on BLAST infile [default: no]. | ||
| 640 | |||
| 641 | =item --greedy-taxa | ||
| 642 | |||
| 643 | Enable greedy behavior when interpreting the ambiguous taxa provided in the | ||
| 644 | required argument C<--taxon-list> [default: no]. | ||
| 645 | |||
| 646 | =item --tax-min-ident=<n> | ||
| 647 | |||
| 648 | Minimum identity percentage to consider a hit when computing a LCA [default: | ||
| 649 | n.default]. | ||
| 650 | |||
| 651 | =for Euclid: n.type: +number | ||
| 652 | n.default: 0 | ||
| 653 | |||
| 654 | =item --tax-min-len=<n> | ||
| 655 | |||
| 656 | Minimum alignment length to consider a hit when computing a LCA [default: | ||
| 657 | n.default]. | ||
| 658 | |||
| 659 | =for Euclid: n.type: +integer | ||
| 660 | n.default: 0 | ||
| 661 | |||
| 662 | =item --tax-min-score=<n> | ||
| 663 | |||
| 664 | Minimum bit score to consider a hit when computing a LCA [default: n.default]. | ||
| 665 | |||
| 666 | =for Euclid: n.type: +integer | ||
| 667 | n.default: 80 | ||
| 668 | |||
| 669 | =item --tax-score-mul=<n> | ||
| 670 | |||
| 671 | Bit score reduction allowed when accumulating hits for LCA inference (MEGAN-like | ||
| 672 | algorithm) [default: n.default]. | ||
| 673 | |||
| 674 | =for Euclid: n.type: +number | ||
| 675 | n.default: 0.95 | ||
| 676 | |||
| 677 | =item --tax-min-hits=<n> | ||
| 678 | |||
| 679 | Minimum number of hits to use when computing LCAs [default: n.default]. Must | ||
| 680 | be lower or equal to the next optional argument (C<--tax-max-hits>). | ||
| 681 | |||
| 682 | =for Euclid: n.type: 0+integer | ||
| 683 | n.default: 1 | ||
| 684 | |||
| 685 | =item --tax-max-hits=<n> | ||
| 686 | |||
| 687 | Maximum number of hits to use when computing LCAs [default: n.default]. Must | ||
| 688 | be greater or equal to the previous optional argument (C<--tax-min-hits>). | ||
| 689 | |||
| 690 | =for Euclid: n.type: 0+integer | ||
| 691 | n.default: 1 | ||
| 692 | |||
| 693 | =item --tax-min-lca-freq=<n> | ||
| 694 | |||
| 695 | Minimum frequency for the common taxon when computing LCA [default: n.default]. | ||
| 696 | When specified and lower than 1.0, the LCA inference algorithm returns the | ||
| 697 | lowest taxon that is found in at least this fraction of lineages (instead of | ||
| 698 | returning the lowest taxon found in all lineages). | ||
| 699 | |||
| 700 | =for Euclid: n.type: 0+number | ||
| 701 | n.default: 1.0 | ||
| 702 | |||
| 703 | =item --kraken | ||
| 704 | |||
| 705 | Write KRAKEN-like report file [default: no]. | ||
| 706 | |||
| 707 | =item --anvio | ||
| 708 | |||
| 709 | Write ANVIO-like report file [default: no]. | ||
| 710 | |||
| 711 | =item --krona | ||
| 712 | |||
| 713 | Write KRONA-compatible report file [default: no]. | ||
| 714 | |||
| 715 | =item --lca | ||
| 716 | |||
| 717 | Write LCA report file (including the lineage of each query) [default: no]. | ||
| 718 | |||
| 719 | =item --kfold=<file> | ||
| 720 | |||
| 721 | Enable the k-fold mode [default: no]. The provided file must contain the list of | ||
| 722 | the NCBI GCA/GCF accessions of all the genomes composing the complete reference | ||
| 723 | database (one accession per line). | ||
| 724 | |||
| 725 | =item --kfold-seed=<n> | ||
| 726 | |||
| 727 | Seed for the random number generator [default: none]. Use this to obtain | ||
| 728 | predictable subsets of the database in k-fold mode. | ||
| 729 | |||
| 730 | =for Euclid: n.type: integer | ||
| 731 | |||
| 732 | =for Euclid: file.type: readable | ||
| 733 | |||
| 734 | =item --version | ||
| 735 | |||
| 736 | =item --usage | ||
| 737 | |||
| 738 | =item --help | ||
| 739 | |||
| 740 | =item --man | ||
| 741 | |||
| 742 | Print the usual program information | ||
| 743 | |||
| 744 | =back | ||
| 745 | |||
| 746 | =head1 COLOPHON | ||
| 747 | |||
| 748 | C<physeter.pl> is based on the script originally developed by Luc CORNET and | ||
| 749 | Denis BAURAIN for the article Cornet, L., et al. (2018). "Consensus assessment | ||
| 750 | of the contamination level of publicly available cyanobacterial genomes." PLoS | ||
| 751 | One 13(7): e0200323. [L<PubMed|https://pubmed.ncbi.nlm.nih.gov/30044797/>]. The | ||
| 752 | code was first completely rewritten by Valerian LUPO to use the C<Bio::MUST> | ||
| 753 | modules and then further reviewed and refactored by D. BAURAIN. Mick VAN | ||
| 754 | VLIERBERGHE greatly contributed to the taxonomic methods offered by C<Bio::MUST> | ||
| 755 | modules. | ||
| 756 | |||
| 757 | =head1 AUTHOR | ||
| 758 | |||
| 759 | Denis BAURAIN <denis.baurain@uliege.be> | ||
| 760 | |||
| 761 | =head1 CONTRIBUTORS | ||
| 762 | |||
| 763 | =for stopwords Valerian LUPO Mick VAN VLIERBERGHE Luc CORNET | ||
| 764 | |||
| 765 | =over 4 | ||
| 766 | |||
| 767 | =item * | ||
| 768 | |||
| 769 | Valerian LUPO <valerian.lupo@doct.uliege.be> | ||
| 770 | |||
| 771 | =item * | ||
| 772 | |||
| 773 | Mick VAN VLIERBERGHE <mvanvlierberghe@doct.uliege.be> | ||
| 774 | |||
| 775 | =item * | ||
| 776 | |||
| 777 | Luc CORNET <luc.cornet@uliege.be> | ||
| 778 | |||
| 779 | =back | ||
| 780 | |||
| 781 | =head1 COPYRIGHT AND LICENSE | ||
| 782 | |||
| 783 | This software is copyright (c) 2020 by University of Liege / Unit of Eukaryotic Phylogenomics / Denis BAURAIN. | ||
| 784 | |||
| 785 | This is free software; you can redistribute it and/or modify it under | ||
| 786 | the same terms as the Perl 5 programming language system itself. | ||
| 787 | |||
| 788 | =cut |