| 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 | use File::Basename; | ||
| 12 | use File::Find::Rule; | ||
| 13 | use List::AllUtils qw(uniq); | ||
| 14 | use Path::Class qw(dir file); | ||
| 15 | use Tie::IxHash; | ||
| 16 | |||
| 17 | use Bio::MUST::Core; | ||
| 18 | use aliased 'Bio::MUST::Core::SeqId'; | ||
| 19 | use aliased 'Bio::MUST::Core::IdList'; | ||
| 20 | use aliased 'Bio::MUST::Core::Taxonomy'; | ||
| 21 | use aliased 'Bio::MUST::Apps::Debrief42::TaxReport'; | ||
| 22 | |||
| 23 | |||
| 24 | ## no critic (RequireLocalizedPunctuationVars) | ||
| 25 | BEGIN{ | ||
| 26 | $ENV{Smart_Comments} = $ARGV_verbosity | ||
| 27 | ? join q{ }, map { '#' x (2 + $_) } 1..$ARGV_verbosity | ||
| 28 | : q{} | ||
| 29 | ; | ||
| 30 | } | ||
| 31 | use Smart::Comments -ENV; | ||
| 32 | |||
| 33 | |||
| 34 | die <<'EOT' if !$ARGV_taxdir && ($ARGV_seq_labeling || $ARGV_contam_labeling); | ||
| 35 | Missing required arguments: | ||
| 36 | --taxdir=<dir> | ||
| 37 | EOT | ||
| 38 | |||
| 39 | # optionally build taxonomy object | ||
| 40 | my $tax; | ||
| 41 | if ($ARGV_taxdir) { | ||
| 42 | $tax = Taxonomy->new_from_cache( tax_dir => $ARGV_taxdir ); | ||
| 43 | } | ||
| 44 | |||
| 45 | # optionally build classifier objects | ||
| 46 | my ($seq_classifier, $contam_classifier); | ||
| 47 | if ($ARGV_seq_labeling) { | ||
| 48 | my $seq_labeling_list = IdList->load($ARGV_seq_labeling); | ||
| 49 | $seq_classifier = $tax->tax_labeler_from_list($seq_labeling_list); | ||
| 50 | } | ||
| 51 | if ($ARGV_contam_labeling) { | ||
| 52 | my $contam_labeling_list = IdList->load($ARGV_contam_labeling); | ||
| 53 | $contam_classifier = $tax->tax_labeler_from_list($contam_labeling_list); | ||
| 54 | } | ||
| 55 | |||
| 56 | ### Processing: $ARGV_indir | ||
| 57 | my @tax_reports = File::Find::Rule | ||
| 58 | ->file() | ||
| 59 | ->name( qr{ $ARGV_in_strip \.tax-report\z}xmsi ) | ||
| 60 | ->maxdepth(1) | ||
| 61 | ->in($ARGV_indir) | ||
| 62 | ; | ||
| 63 | my $ali_test = @tax_reports; | ||
| 64 | #### @tax_reports | ||
| 65 | |||
| 66 | # TODO: rewrap long lines (>80 chars) | ||
| 67 | my %data_for; | ||
| 68 | |||
| 69 | ### Processing tax-reports... | ||
| 70 | for my $report (@tax_reports) { | ||
| 71 | ### Processing: $report | ||
| 72 | |||
| 73 | # Loading file as a TaxReport object | ||
| 74 | my $tax_report = TaxReport->new( file => file($report) ); | ||
| 75 | #### $tax_report | ||
| 76 | |||
| 77 | # Deriving MSA/OG basename | ||
| 78 | $report =~ s/$ARGV_in_strip//xms; | ||
| 79 | my ($ali) = fileparse($report, qr{\.[^.]*}xms); | ||
| 80 | #### $ali | ||
| 81 | |||
| 82 | NEWSEQ: | ||
| 83 | while ( my $new_seq = $tax_report->next_seq ) { | ||
| 84 | |||
| 85 | next NEWSEQ unless $new_seq->lca || $new_seq->lineage; | ||
| 86 | my $full_id = $new_seq->seq_id; | ||
| 87 | $full_id =~ s/(GC[AF])\s/$1_/xms; | ||
| 88 | my $seq_id = SeqId->new(full_id => $full_id); | ||
| 89 | |||
| 90 | # TODO: check that literal strings are still up-to-date with current tax | ||
| 91 | my ($contam, $unclass, $unknown, $self) = (0, 0, 0, 0); | ||
| 92 | $unknown = 1 if map { $new_seq->lca =~ m/$_/xmsg } 'cellular\sorganisms', 'Eukaryota', 'Bacteria', 'Archaea' , 'unclassified sequences'; | ||
| 93 | $unclass = 1 if $new_seq->lineage eq 'unclassified sequences'; | ||
| 94 | #### $new_seq | ||
| 95 | #### $full_id | ||
| 96 | #### $seq_id | ||
| 97 | #### $unknown | ||
| 98 | #### $unclass | ||
| 99 | |||
| 100 | unless ( $unknown or $unclass ) { | ||
| 101 | |||
| 102 | if ($seq_classifier) { | ||
| 103 | #### new_seq: $full_id | ||
| 104 | #### affiliation: $new_seq->lineage | ||
| 105 | ($self, $contam) = $tax->eq_tax($full_id, $new_seq->lineage, $seq_classifier) ? (1, 0) : (0, 1); | ||
| 106 | #### $self | ||
| 107 | #### $contam | ||
| 108 | } | ||
| 109 | else { | ||
| 110 | $contam = 1 if defined $seq_id->tag && $seq_id->tag eq 'c'; | ||
| 111 | $unclass = 1 if defined $seq_id->tag && $seq_id->tag eq 'u'; | ||
| 112 | $self = 1 unless $contam || $unclass; | ||
| 113 | #### tag#: $seq_id->tag | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | my $bank = $seq_id->full_org || $seq_id->taxon_id; | ||
| 118 | #### $bank | ||
| 119 | |||
| 120 | # Fill up data hash... | ||
| 121 | $data_for{$bank}{seq}{phyla}{$new_seq->lca}++ if $contam; | ||
| 122 | $data_for{$bank}{lineages}{$new_seq->lineage}++ if $contam; | ||
| 123 | |||
| 124 | $data_for{$bank}{seq}{c}++ if $contam; | ||
| 125 | $data_for{$bank}{seq}{nc}++ if $self; | ||
| 126 | $data_for{$bank}{seq}{uc}++ if $unclass; | ||
| 127 | $data_for{$bank}{seq}{uk}++ if $unknown; | ||
| 128 | $data_for{$bank}{seq}{total}++; | ||
| 129 | #### get: $data_for{$bank}{seq} | ||
| 130 | |||
| 131 | $data_for{$bank}{ali}{c}{$ali} = 1 if $contam; | ||
| 132 | $data_for{$bank}{ali}{uc}{$ali} = 1 if $unclass; | ||
| 133 | $data_for{$bank}{ali}{uk}{$ali} = 1 if $unknown; | ||
| 134 | $data_for{$bank}{ali}{nc}{$ali} = 1 if $self; | ||
| 135 | $data_for{$bank}{ali}{total}{$ali} = 1; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | #### %data_for | ||
| 139 | |||
| 140 | # analyse contaminating phyla and try to remap them to a higher level... | ||
| 141 | # ... in order to alleviate contam reports | ||
| 142 | my %contam_data_for; | ||
| 143 | my @banks = keys %data_for; | ||
| 144 | PROCESS_DATA: | ||
| 145 | for my $bank ( @banks ) { | ||
| 146 | |||
| 147 | my %lineages_for; | ||
| 148 | my $contam_lineage_count = $data_for{$bank}{lineages}; | ||
| 149 | |||
| 150 | for my $lineage ( keys %$contam_lineage_count ) { | ||
| 151 | |||
| 152 | my $contam_taxon = $contam_classifier ? $contam_classifier->classify($lineage) : (split /;\s/xms, $lineage)[-1]; | ||
| 153 | $contam_taxon ||= 'other taxa'; | ||
| 154 | push @{ $lineages_for{$contam_taxon} }, $lineage for 1..$contam_lineage_count->{$lineage}; | ||
| 155 | #### $lineage | ||
| 156 | #### classification: $contam_taxon | ||
| 157 | } | ||
| 158 | #### Considering: $bank | ||
| 159 | #### $contam_lineage_count | ||
| 160 | #### %lineages_for | ||
| 161 | |||
| 162 | for my $contam_taxon ( keys %lineages_for ) { | ||
| 163 | my $seq_count = eval { scalar @{ $lineages_for{$contam_taxon} } } || 0; | ||
| 164 | $contam_data_for{$bank}{$contam_taxon} = $seq_count; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | #### %contam_data_for | ||
| 168 | |||
| 169 | # TODO: consolidate KRONA output with Physeter's subs | ||
| 170 | |||
| 171 | my $dir = '.'; # default to working dir | ||
| 172 | if ($ARGV_outdir) { | ||
| 173 | $dir = dir($ARGV_outdir)->relative; | ||
| 174 | $dir->mkpath(); | ||
| 175 | } | ||
| 176 | |||
| 177 | ### Writing output TSV files for KRONA... | ||
| 178 | for my $bank (@banks) { | ||
| 179 | ( my $serie = $bank ) =~ s/\s/_/xmsg; | ||
| 180 | open my $lineout, '>', file($dir, $serie . $ARGV_in_strip . $ARGV_out_suffix . '.tsv'); | ||
| 181 | say {$lineout} join "\t", $serie, 'domain;phylum;class;order;family;genus;species'; | ||
| 182 | say {$lineout} join "\t", ( $data_for{$bank}{lineages}{$_}, $_ ) for keys %{ $data_for{$bank}{lineages} }; | ||
| 183 | say {$lineout} join "\t", $data_for{$bank}{seq}{nc} || 0, 'SELF'; | ||
| 184 | say {$lineout} join "\t", $data_for{$bank}{seq}{uk} || 0, 'unknown'; | ||
| 185 | say {$lineout} join "\t", $data_for{$bank}{seq}{uc} || 0, 'unclassified'; | ||
| 186 | } | ||
| 187 | |||
| 188 | my @elected_lineages = uniq( sort map { keys %{ $contam_data_for{$_} } } @banks ); | ||
| 189 | my @phyla = map { (split /;\s/xms)[-1] } @elected_lineages; | ||
| 190 | my @mod_phyla = map { $_ =~ s/\s/_/xmsg; $_ } @phyla; | ||
| 191 | #### @elected_lineages | ||
| 192 | #### @phyla | ||
| 193 | |||
| 194 | ### Writing summary output files... | ||
| 195 | STATS_FILES: | ||
| 196 | |||
| 197 | open my $out, '>', file($dir, 'per-genome' . $ARGV_in_strip . $ARGV_out_suffix . '.stats'); | ||
| 198 | open my $out_sum, '>', file($dir, 'per-genome' . $ARGV_in_strip . $ARGV_out_suffix . '.stats-sum'); | ||
| 199 | |||
| 200 | say {$out} join "\t", 'bank', 'tested_genes', 'added_ali', 'clean_ali', | ||
| 201 | 'contam_ali', 'completeness', 'added_seq', 'clean_seq', 'contam_seq', | ||
| 202 | 'unclass_contam_seq', 'unknown_seq', @mod_phyla, 'foreign_phyla', | ||
| 203 | 'contam_perc', 'class_contam_perc', 'unclass_contam_perc', 'unknown_perc' | ||
| 204 | ; | ||
| 205 | |||
| 206 | say {$out_sum} join "\t", 'bank', 'tested_genes', 'added_ali', 'clean_ali', | ||
| 207 | 'contam_ali', 'completeness', 'added_seq', 'clean_seq', 'contam_seq', | ||
| 208 | 'unclass_contam_seq', 'unknown_seq', 'foreign_phyla', 'contam_perc', | ||
| 209 | 'class_contam_perc', 'unclass_contam_perc', 'unknown_perc'; | ||
| 210 | |||
| 211 | my @ali_totals; | ||
| 212 | tie my %line_for, 'Tie::IxHash'; | ||
| 213 | for my $bank (@banks) { | ||
| 214 | |||
| 215 | # ali stats | ||
| 216 | my $ali_c = scalar keys %{ $data_for{$bank}{ali}{c} } // 0; | ||
| 217 | my $ali_nc = scalar keys %{ $data_for{$bank}{ali}{nc} } // 0; | ||
| 218 | my $ali_total = scalar keys %{ $data_for{$bank}{ali}{total} } // 0; | ||
| 219 | my $completeness = eval { ( $ali_nc / $ali_test ) * 100 }; | ||
| 220 | |||
| 221 | # seq stats | ||
| 222 | my $seq_c = $data_for{$bank}{seq}{c} // 0; | ||
| 223 | my $seq_nc = $data_for{$bank}{seq}{nc} // 0; | ||
| 224 | my $seq_total = $data_for{$bank}{seq}{total} // 0; | ||
| 225 | |||
| 226 | # taxonomic data | ||
| 227 | my @phyla_data = map { $contam_data_for{$bank}{$_} // 0 } @elected_lineages; | ||
| 228 | my $foreign_phyla = scalar keys %{ $contam_data_for{$bank} } // 0; | ||
| 229 | my $unclassified = $data_for{$bank}{seq}{uc} // 0; | ||
| 230 | my $unknown = $data_for{$bank}{seq}{uk} // 0; | ||
| 231 | |||
| 232 | my $all_c_p = eval { ( ($seq_c + $unclassified) * 100)/$seq_total } // 0; | ||
| 233 | my $contam_p = eval { ( $seq_c * 100)/$seq_total } // 0; | ||
| 234 | my $unclas_p = eval { ( $unclassified * 100)/$seq_total } // 0; | ||
| 235 | my $unknwn_p = eval { ( $unknown * 100)/$seq_total } // 0; | ||
| 236 | |||
| 237 | $bank =~ s/\s/_/xms; | ||
| 238 | |||
| 239 | $line_for{$bank} = [ | ||
| 240 | $bank, $ali_test, $ali_total, $ali_nc, $ali_c, $completeness, | ||
| 241 | $seq_total, $seq_nc, $seq_c, $unclassified, $unknown, | ||
| 242 | @phyla_data, $foreign_phyla >= 0 ? $foreign_phyla : 0, | ||
| 243 | $all_c_p, $contam_p, $unclas_p, $unknwn_p | ||
| 244 | ]; | ||
| 245 | |||
| 246 | push @ali_totals, $ali_total; | ||
| 247 | } | ||
| 248 | #### %line_for | ||
| 249 | |||
| 250 | my @sort_all_banks = sort { @{ $line_for{$b} }[-3] <=> @{ $line_for{$a} }[-3] } keys %line_for; | ||
| 251 | #### @sort_all_banks | ||
| 252 | |||
| 253 | # Write file contents | ||
| 254 | say {$out} join "\t", @{ $line_for{$_} } for @sort_all_banks; | ||
| 255 | say {$out_sum} join "\t", @{ $line_for{$_} }[0..8,-7..-1] for @sort_all_banks; | ||
| 256 | |||
| 257 | ### Done! | ||
| 258 | |||
| 259 | |||
| 260 | ##################################### SUBS ##################################### | ||
| 261 | |||
| 262 | sub compute_percentage { | ||
| 263 | my $array = shift; | ||
| 264 | my $total = shift; | ||
| 265 | |||
| 266 | my @results; | ||
| 267 | my $percentage; | ||
| 268 | CALC: | ||
| 269 | for my $value (@$array) { | ||
| 270 | if ($value == 0) { | ||
| 271 | $percentage = 0; | ||
| 272 | push @results, '-/-'; | ||
| 273 | next CALC; | ||
| 274 | } | ||
| 275 | else { | ||
| 276 | $percentage = $value / $total; | ||
| 277 | # $percentage = $value * 100 / $total; | ||
| 278 | } | ||
| 279 | $percentage = sprintf("%.2f", $percentage); | ||
| 280 | push @results, $value . '/' . $percentage; | ||
| 281 | } | ||
| 282 | return \@results; | ||
| 283 | } | ||
| 284 | |||
| 285 | # for testing: | ||
| 286 | # perl -Ilib bin/debrief-42.pl --indir=xtest/tax_reports/ \ | ||
| 287 | # --in-strip=-42-camera-megan99-tf --taxdir=../Bio-MUST-Core/test/taxdump \ | ||
| 288 | # --seq_labeling=xtest/seq-labels.idl --contam_labeling=xtest/contam-labels.idl \ | ||
| 289 | # --outdir=dbout | ||
| 290 | |||
| 291 | __END__ |