| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: debrief-42.pl | ||
| 3 | # ABSTRACT: Summarize the results of a 42 metagenomic run | ||
| 4 | # CONTRIBUTOR: Mick VAN VLIERBERGHE <mvanvlierberghe@doct.ulg.ac.be> | ||
| 5 | |||
| 6 | use autodie; | ||
| 7 | use Modern::Perl '2011'; | ||
| 8 | |||
| 9 | use Getopt::Euclid qw(:vars); | ||
| 10 | |||
| 11 | # TODO: check what args should be actually required | ||
| 12 | # TODO: clean-up obsolete dependencies | ||
| 13 | |||
| 14 | use POSIX; | ||
| 15 | use Storable; | ||
| 16 | use Tie::IxHash; | ||
| 17 | use Config::Any; | ||
| 18 | use File::Basename; | ||
| 19 | use Sort::Naturally; | ||
| 20 | use File::Find::Rule; | ||
| 21 | use Path::Class qw(file); | ||
| 22 | use List::AllUtils qw(uniq); | ||
| 23 | |||
| 24 | use Bio::MUST::Core; | ||
| 25 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 26 | use aliased 'Bio::MUST::Core::SeqId'; | ||
| 27 | use aliased 'Bio::MUST::Core::IdList'; | ||
| 28 | use aliased 'Bio::MUST::Core::Taxonomy'; | ||
| 29 | use aliased 'Bio::MUST::Apps::Debrief42::TaxReport'; | ||
| 30 | use Bio::MUST::Core::Utils qw(change_suffix); | ||
| 31 | |||
| 32 | |||
| 33 | ## no critic (RequireLocalizedPunctuationVars) | ||
| 34 | BEGIN{ | ||
| 35 | $ENV{Smart_Comments} = $ARGV_verbosity | ||
| 36 | ? join q{ }, map { '#' x (2 + $_) } 1..$ARGV_verbosity | ||
| 37 | : q{} | ||
| 38 | ; | ||
| 39 | } | ||
| 40 | use Smart::Comments -ENV; | ||
| 41 | |||
| 42 | ### %ARGV | ||
| 43 | |||
| 44 | ### Processing: $ARGV_indir | ||
| 45 | # list tax-report files | ||
| 46 | my @tax_reports = File::Find::Rule | ||
| 47 | ->file() | ||
| 48 | ->name( qr{ $ARGV_in_strip \.tax-report\z}xmsi ) | ||
| 49 | ->maxdepth(1) | ||
| 50 | ->in($ARGV_indir) | ||
| 51 | ; | ||
| 52 | my $ali_test = @tax_reports; | ||
| 53 | #### @tax_reports | ||
| 54 | |||
| 55 | # optionally build taxonomy object | ||
| 56 | my $tax; | ||
| 57 | if ($ARGV_taxdir) { | ||
| 58 | ### Annotating tree using: $ARGV_taxdir | ||
| 59 | $tax = Taxonomy->new_from_cache( tax_dir => $ARGV_taxdir ); | ||
| 60 | } | ||
| 61 | |||
| 62 | # Build classifier objects | ||
| 63 | my $seq_classifier; | ||
| 64 | my $contam_classifier; | ||
| 65 | if ( $ARGV_seq_labeling ) { | ||
| 66 | my $seq_labeling_list = get_list($ARGV_seq_labeling); | ||
| 67 | $seq_classifier = $tax->tax_labeler_from_list($seq_labeling_list); | ||
| 68 | my $contam_labeling_list = get_list($ARGV_contam_labeling); | ||
| 69 | $contam_classifier = $tax->tax_labeler_from_list($contam_labeling_list); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | # TODO: clean-up obsolete code | ||
| 74 | # TODO: rewrap long lines (>80 chars) | ||
| 75 | |||
| 76 | my $data_for; | ||
| 77 | my %data_for; | ||
| 78 | ### Processing tax-reports | ||
| 79 | #BUILD_DATA: | ||
| 80 | #if ( -e 'data_for' . $ARGV_in_strip . '.bin' ) { | ||
| 81 | # ### Loading data hash from binary cache file... | ||
| 82 | # $data_for = retrieve('data_for' . $ARGV_in_strip . '.bin'); | ||
| 83 | # ### Done ! | ||
| 84 | #} | ||
| 85 | #else { | ||
| 86 | for my $report (@tax_reports) { | ||
| 87 | ### Processing: $report | ||
| 88 | |||
| 89 | # Loading file as a TaxReport object | ||
| 90 | my $tax_report = TaxReport->new( file => file($report) ); | ||
| 91 | #### $tax_report | ||
| 92 | |||
| 93 | # Deriving MSA/OG basename | ||
| 94 | $report =~ s/$ARGV_in_strip//xms; | ||
| 95 | my ($ali) = fileparse($report, qr{\.[^.]*}xms); | ||
| 96 | #### $ali | ||
| 97 | |||
| 98 | NEWSEQ: | ||
| 99 | while ( my $new_seq = $tax_report->next_seq ) { | ||
| 100 | |||
| 101 | next unless $new_seq->lca || $new_seq->lineage; | ||
| 102 | my $full_id = $new_seq->seq_id; | ||
| 103 | $full_id =~ s/(GC[AF])\s/$1_/xms; | ||
| 104 | my $seq_id = SeqId->new(full_id => $full_id); | ||
| 105 | |||
| 106 | # TODO: check that literal strings are still up-to-date with current tax | ||
| 107 | my ($contam, $unclass, $unknown, $self) = (0, 0, 0, 0); | ||
| 108 | $unknown = 1 if map { $new_seq->lca =~ m/$_/xmsg } 'cellular\sorganisms', 'Eukaryota', 'Bacteria', 'Archaea' , 'unclassified sequences'; | ||
| 109 | $unclass = 1 if $new_seq->lineage eq 'unclassified sequences'; | ||
| 110 | #### $new_seq | ||
| 111 | #### $full_id | ||
| 112 | #### $seq_id | ||
| 113 | #### $unknown | ||
| 114 | #### $unclass | ||
| 115 | |||
| 116 | unless ( $unknown or $unclass ) { | ||
| 117 | |||
| 118 | if ($ARGV_seq_labeling) { | ||
| 119 | ### new_seq: $full_id | ||
| 120 | ### affiliation: $new_seq->lineage | ||
| 121 | ($self, $contam) = $tax->eq_tax($full_id, $new_seq->lineage, $seq_classifier) ? (1, 0) : (0, 1); | ||
| 122 | ### $self | ||
| 123 | ### $contam | ||
| 124 | } | ||
| 125 | else { | ||
| 126 | $contam = 1 if defined $seq_id->tag && $seq_id->tag eq 'c'; | ||
| 127 | $unclass = 1 if defined $seq_id->tag && $seq_id->tag eq 'u'; | ||
| 128 | $self = 1 unless $contam || $unclass; | ||
| 129 | #### tag#: $seq_id->tag | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | my $bank = $seq_id->full_org || $seq_id->taxon_id; | ||
| 134 | #### $bank | ||
| 135 | |||
| 136 | # Fill up data hash... | ||
| 137 | $data_for{$bank}{seq}{phyla}{$new_seq->lca}++ if $contam; | ||
| 138 | $data_for{$bank}{lineages}{$new_seq->lineage}++ if $contam; | ||
| 139 | |||
| 140 | $data_for{$bank}{seq}{c}++ if $contam; | ||
| 141 | $data_for{$bank}{seq}{nc}++ if $self; | ||
| 142 | $data_for{$bank}{seq}{uc}++ if $unclass; | ||
| 143 | $data_for{$bank}{seq}{uk}++ if $unknown; | ||
| 144 | $data_for{$bank}{seq}{total}++; | ||
| 145 | ##### get: $data_for{$bank}{seq} | ||
| 146 | |||
| 147 | $data_for{$bank}{ali}{c}{$ali} = 1 if $contam; | ||
| 148 | $data_for{$bank}{ali}{uc}{$ali} = 1 if $unclass; | ||
| 149 | $data_for{$bank}{ali}{uk}{$ali} = 1 if $unknown; | ||
| 150 | $data_for{$bank}{ali}{nc}{$ali} = 1 if $self; | ||
| 151 | $data_for{$bank}{ali}{total}{$ali} = 1; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | $data_for = \%data_for; | ||
| 155 | # store $data_for, 'data_for' . $ARGV_in_strip . '.bin'; | ||
| 156 | #} | ||
| 157 | #### $data_for | ||
| 158 | |||
| 159 | |||
| 160 | # Analyse contaminating phyla and try to remap them to a higher level... | ||
| 161 | # ... in order to alleviate contam reports | ||
| 162 | my %contam_data_for; | ||
| 163 | my @banks = keys %$data_for; | ||
| 164 | PROCESS_DATA: | ||
| 165 | for my $bank ( @banks ) { | ||
| 166 | |||
| 167 | my %lineages_for; | ||
| 168 | my $contam_lineage_count = $data_for->{$bank}{lineages}; | ||
| 169 | |||
| 170 | for my $lineage ( keys %$contam_lineage_count ) { | ||
| 171 | |||
| 172 | # my $contam_taxon = $contam_classifier->classify($lineage) || (split /;\s/xms, $lineage)[-1]; | ||
| 173 | my $contam_taxon = $contam_classifier->classify($lineage) || 'other taxa'; | ||
| 174 | push @{ $lineages_for{$contam_taxon} }, $lineage for 1..$contam_lineage_count->{$lineage}; | ||
| 175 | #### $lineage | ||
| 176 | #### classification: $contam_taxon | ||
| 177 | } | ||
| 178 | #### Considering: $bank | ||
| 179 | #### $contam_lineage_count | ||
| 180 | #### %lineages_for | ||
| 181 | |||
| 182 | for my $contam_taxon ( keys %lineages_for ) { | ||
| 183 | |||
| 184 | my $seq_count = eval { scalar @{ $lineages_for{$contam_taxon} } } || 0; | ||
| 185 | $contam_data_for{$bank}{$contam_taxon} = $seq_count; | ||
| 186 | |||
| 187 | # my $threshold = $data_for->{$bank}{seq}{total} * 0.55; | ||
| 188 | # my $seq_count = eval { scalar @{ $lineages_for{$contam_taxon} } } || 0; | ||
| 189 | # #### $contam_taxon | ||
| 190 | # #### $seq_count | ||
| 191 | # | ||
| 192 | # my $tt_lineage = $lineages_for{$contam_taxon}[0]; | ||
| 193 | # my $index = index($tt_lineage, $contam_taxon); | ||
| 194 | # my $lineage = substr($tt_lineage, 0, $index); | ||
| 195 | # substr($lineage, $index) = $contam_taxon; | ||
| 196 | # | ||
| 197 | # | ||
| 198 | # if ( $seq_count > $threshold ) { | ||
| 199 | # # contam_taxon representing more than $threshold | ||
| 200 | # # ...proceeding to vote to try to get a more precise affilition | ||
| 201 | # my $common_lineage = $tax->get_common_taxonomy_from_seq_ids( 0.90, @{ $lineages_for{$contam_taxon} }); | ||
| 202 | # ### trying to remap: $contam_taxon | ||
| 203 | # ### remapped to: $common_lineage | ||
| 204 | # | ||
| 205 | # # store common_lineage for contam and its count | ||
| 206 | # $contam_data_for{$bank}{join '; ', @$common_lineage} = $seq_count; | ||
| 207 | # ### $common_lineage | ||
| 208 | # ### %contam_data_for | ||
| 209 | # } | ||
| 210 | # else { | ||
| 211 | # # contam_taxon representing less than 55%, no vote... | ||
| 212 | # my $tt_lineage = $lineages_for{$contam_taxon}[0]; | ||
| 213 | # my $index = index($tt_lineage, $contam_taxon); | ||
| 214 | # my $lineage = substr($tt_lineage, 0, $index); | ||
| 215 | # substr($lineage, $index) = $contam_taxon; | ||
| 216 | # | ||
| 217 | # $contam_data_for{$bank}{$lineage} = $seq_count; | ||
| 218 | # #### $contam_taxon | ||
| 219 | # #### $tt_lineage | ||
| 220 | # #### $lineage | ||
| 221 | # } | ||
| 222 | } | ||
| 223 | } | ||
| 224 | ### %contam_data_for | ||
| 225 | ### $data_for | ||
| 226 | |||
| 227 | # TODO: consolidate KRONA output with Physeter's subs | ||
| 228 | |||
| 229 | ### Writing output '.tsv' files for KRONA | ||
| 230 | TSV_FILES: | ||
| 231 | for my $bank ( @banks ) { | ||
| 232 | |||
| 233 | my $serie = $bank; | ||
| 234 | $serie =~ s/\s/_/xmsg; | ||
| 235 | open my $lineout, '>', $serie . $ARGV_in_strip . $ARGV_out_suffix . '.tsv'; | ||
| 236 | |||
| 237 | say {$lineout} join "\t", $serie, 'domain;phylum;class;order;family;genus;species'; | ||
| 238 | say {$lineout} join "\t", ( $data_for->{$bank}{lineages}{$_}, $_ ) for keys %{ $data_for->{$bank}{lineages} }; | ||
| 239 | say {$lineout} join "\t", $data_for->{$bank}{seq}{nc} || 0, 'SELF'; | ||
| 240 | say {$lineout} join "\t", $data_for->{$bank}{seq}{uk} || 0, 'unknown'; | ||
| 241 | say {$lineout} join "\t", $data_for->{$bank}{seq}{uc} || 0, 'unclassified'; | ||
| 242 | } | ||
| 243 | |||
| 244 | my @elected_lineages = uniq( sort map { keys %{ $contam_data_for{$_} } } @banks ); | ||
| 245 | my @phyla = map { (split /;\s/xms)[-1] } @elected_lineages; | ||
| 246 | my @mod_phyla = map { $_ =~ s/\s/_/xmsg; $_ } @phyla; | ||
| 247 | ### @elected_lineages | ||
| 248 | ### @phyla | ||
| 249 | |||
| 250 | ### Writing output files: 'per-genome' . $ARGV_in_strip . $ARGV_out_suffix . '.stats' | ||
| 251 | STATS_FILES: | ||
| 252 | open my $out, '>', 'per-genome' . $ARGV_in_strip . $ARGV_out_suffix . '.stats'; | ||
| 253 | open my $out_sum, '>', 'per-genome' . $ARGV_in_strip . $ARGV_out_suffix . '.stats-sum'; | ||
| 254 | |||
| 255 | say {$out} join "\t", 'bank', 'tested_genes', 'added_ali', 'clean_ali', | ||
| 256 | 'contam_ali', 'completeness', 'added_seq', 'clean_seq', 'contam_seq', | ||
| 257 | 'unclass_contam_seq', 'unknown_seq', @mod_phyla, 'foreign_phyla', | ||
| 258 | 'contam_perc', 'class_contam_perc', 'unclass_contam_perc', 'unknown_perc' | ||
| 259 | ; | ||
| 260 | |||
| 261 | say {$out_sum} join "\t", 'bank', 'tested_genes', 'added_ali', 'clean_ali', | ||
| 262 | 'contam_ali', 'completeness', 'added_seq', 'clean_seq', 'contam_seq', | ||
| 263 | 'unclass_contam_seq', 'unknown_seq', 'foreign_phyla', 'contam_perc', | ||
| 264 | 'class_contam_perc', 'unclass_contam_perc', 'unknown_perc'; | ||
| 265 | |||
| 266 | my @ali_totals; | ||
| 267 | tie my %line_for, 'Tie::IxHash'; | ||
| 268 | for my $bank ( @banks ) { | ||
| 269 | |||
| 270 | # ali stats | ||
| 271 | my $ali_c = scalar keys %{ $data_for->{$bank}{ali}{c} } // 0; | ||
| 272 | my $ali_nc = scalar keys %{ $data_for->{$bank}{ali}{nc} } // 0; | ||
| 273 | my $ali_total = scalar keys %{ $data_for->{$bank}{ali}{total} } // 0; | ||
| 274 | my $completeness = eval { ( $ali_nc / $ali_test ) * 100 }; | ||
| 275 | |||
| 276 | # seq stats | ||
| 277 | my $seq_c = $data_for->{$bank}{seq}{c} // 0; | ||
| 278 | my $seq_nc = $data_for->{$bank}{seq}{nc} // 0; | ||
| 279 | my $seq_total = $data_for->{$bank}{seq}{total} // 0; | ||
| 280 | |||
| 281 | # taxonomic data | ||
| 282 | my @phyla_data = map { $contam_data_for{$bank}{$_} // 0 } @elected_lineages; | ||
| 283 | my $foreign_phyla = scalar keys %{ $contam_data_for{$bank} } // 0; | ||
| 284 | my $unclassified = $data_for->{$bank}{seq}{uc} // 0; | ||
| 285 | my $unknown = $data_for->{$bank}{seq}{uk} // 0; | ||
| 286 | |||
| 287 | my $all_c_p = eval { ( ($seq_c + $unclassified) * 100)/$seq_total } // 0; | ||
| 288 | my $contam_p = eval { ( $seq_c * 100)/$seq_total } // 0; | ||
| 289 | my $unclas_p = eval { ( $unclassified * 100)/$seq_total } // 0; | ||
| 290 | my $unknwn_p = eval { ( $unknown * 100)/$seq_total } // 0; | ||
| 291 | |||
| 292 | $bank =~ s/\s/_/xms; | ||
| 293 | |||
| 294 | $line_for{$bank} = [ | ||
| 295 | $bank, $ali_test, $ali_total, $ali_nc, $ali_c, $completeness, | ||
| 296 | $seq_total, $seq_nc, $seq_c, $unclassified, $unknown, | ||
| 297 | @phyla_data, $foreign_phyla >= 0 ? $foreign_phyla : 0, | ||
| 298 | $all_c_p, $contam_p, $unclas_p, $unknwn_p | ||
| 299 | ]; | ||
| 300 | |||
| 301 | push @ali_totals, $ali_total; | ||
| 302 | } | ||
| 303 | #### %line_for | ||
| 304 | |||
| 305 | my @sort_all_banks = sort { @{ $line_for{$b} }[-3] <=> @{ $line_for{$a} }[-3] } keys %line_for; | ||
| 306 | #### @sort_all_banks | ||
| 307 | |||
| 308 | # Write file contents | ||
| 309 | say {$out} join "\t", @{ $line_for{$_} } for @sort_all_banks; | ||
| 310 | say {$out_sum} join "\t", @{ $line_for{$_} }[0..8,-7..-1] for @sort_all_banks; | ||
| 311 | |||
| 312 | ### Done | ||
| 313 | |||
| 314 | |||
| 315 | ##################################### SUBS ##################################### | ||
| 316 | |||
| 317 | sub get_list { | ||
| 318 | my $infile = shift; | ||
| 319 | my $file = file($infile)->resolve; | ||
| 320 | my @ids; | ||
| 321 | open my $in, '<', $file; | ||
| 322 | while (my $line = <$in>) { | ||
| 323 | chomp $line; | ||
| 324 | push @ids, $line; | ||
| 325 | } | ||
| 326 | return IdList->new(ids => \@ids); | ||
| 327 | } | ||
| 328 | |||
| 329 | sub compute_percentage { | ||
| 330 | my $array = shift; | ||
| 331 | my $total = shift; | ||
| 332 | |||
| 333 | my @results; | ||
| 334 | my $percentage; | ||
| 335 | CALC: | ||
| 336 | for my $value (@$array) { | ||
| 337 | if ($value == 0) { | ||
| 338 | $percentage = 0; | ||
| 339 | push @results, '-/-'; | ||
| 340 | next CALC; | ||
| 341 | } | ||
| 342 | else { | ||
| 343 | $percentage = $value / $total; | ||
| 344 | # $percentage = $value * 100 / $total; | ||
| 345 | } | ||
| 346 | $percentage = sprintf("%.2f", $percentage); | ||
| 347 | push @results, $value . '/' . $percentage; | ||
| 348 | } | ||
| 349 | return \@results; | ||
| 350 | } | ||
| 351 | |||
| 352 | __END__ | ||
| 353 | |||
| 354 | =pod | ||
| 355 | |||
| 356 | =head1 NAME | ||
| 357 | |||
| 358 | debrief-42.pl - Summarize the results of a 42 metagenomic run | ||
| 359 | |||
| 360 | =head1 VERSION | ||
| 361 | |||
| 362 | version 0.210570 | ||
| 363 | |||
| 364 | =head1 USAGE | ||
| 365 | |||
| 366 | debrief-taxR-42.pl --indir=<indir> --in=<str> [optional arguments] | ||
| 367 | |||
| 368 | =head1 REQUIRED ARGUMENTS | ||
| 369 | |||
| 370 | =over | ||
| 371 | |||
| 372 | =item --indir=<indir> | ||
| 373 | |||
| 374 | Path to input directory containing TAX-REPORT files. | ||
| 375 | |||
| 376 | =for Euclid: indir.type: str | ||
| 377 | |||
| 378 | =item --in[-strip]=<str> | ||
| 379 | |||
| 380 | Substring to strip from infile basenames to derive pre-42 filenames. This | ||
| 381 | often corresponds to the C<out_suffix> option of the YAML C<config> file for 42 | ||
| 382 | (e.g., C<-42>). | ||
| 383 | |||
| 384 | =for Euclid: str.type: string | ||
| 385 | |||
| 386 | =back | ||
| 387 | |||
| 388 | =head1 OPTIONAL ARGUMENTS | ||
| 389 | |||
| 390 | =over | ||
| 391 | |||
| 392 | =item --out[-suffix]=<suffix> | ||
| 393 | |||
| 394 | Suffix to append to infile basenames for deriving outfile names [default: none]. | ||
| 395 | |||
| 396 | =for Euclid: suffix.type: string | ||
| 397 | suffix.default: q{} | ||
| 398 | |||
| 399 | =item --taxdir=<dir> | ||
| 400 | |||
| 401 | Path to local mirror of the NCBI Taxonomy database. | ||
| 402 | |||
| 403 | =for Euclid: dir.type: string | ||
| 404 | |||
| 405 | =item --seq_labeling=<file> | ||
| 406 | |||
| 407 | Path to custom IDL file providing low-level taxa for accurate labeling of | ||
| 408 | the sequences newly added by 42 [default: none]. | ||
| 409 | |||
| 410 | =for Euclid: file.type: readable | ||
| 411 | |||
| 412 | =item --contam_labeling=<file> | ||
| 413 | |||
| 414 | Path to custom IDL file providing high-level taxa for broad detection of | ||
| 415 | contaminant sequences among new sequences [default: none]. | ||
| 416 | |||
| 417 | =for Euclid: file.type: readable | ||
| 418 | |||
| 419 | =item --verbosity=<level> | ||
| 420 | |||
| 421 | Verbosity level for logging to STDERR [default: level.default]. Available | ||
| 422 | levels range from 0 to 6. Level 6 corresponds to debugging mode. | ||
| 423 | |||
| 424 | =for Euclid: level.type: int, level >= 0 && level <= 6 | ||
| 425 | level.default: 1 | ||
| 426 | |||
| 427 | =item --version | ||
| 428 | |||
| 429 | =item --usage | ||
| 430 | |||
| 431 | =item --help | ||
| 432 | |||
| 433 | =item --man | ||
| 434 | |||
| 435 | Print the usual program information | ||
| 436 | |||
| 437 | =back | ||
| 438 | |||
| 439 | =head1 AUTHOR | ||
| 440 | |||
| 441 | Denis BAURAIN <denis.baurain@uliege.be> | ||
| 442 | |||
| 443 | =head1 CONTRIBUTOR | ||
| 444 | |||
| 445 | =for stopwords Mick VAN VLIERBERGHE | ||
| 446 | |||
| 447 | Mick VAN VLIERBERGHE <mvanvlierberghe@doct.ulg.ac.be> | ||
| 448 | |||
| 449 | =head1 COPYRIGHT AND LICENSE | ||
| 450 | |||
| 451 | This software is copyright (c) 2013 by University of Liege / Unit of Eukaryotic Phylogenomics / Denis BAURAIN. | ||
| 452 | |||
| 453 | This is free software; you can redistribute it and/or modify it under | ||
| 454 | the same terms as the Perl 5 programming language system itself. | ||
| 455 | |||
| 456 | =cut |