| 1 | package Bio::MUST::Apps::Debrief42::RunReport; | ||
| 2 | # ABSTRACT: Internal class for tabular tax-report parser | ||
| 3 | # CONTRIBUTOR: Mick VAN VLIERBERGHE <mvanvlierberghe@doct.uliege.be> | ||
| 4 | $Bio::MUST::Apps::Debrief42::RunReport::VERSION = '0.213470'; | ||
| 5 | use Moose; | ||
| 6 | use namespace::autoclean; | ||
| 7 | |||
| 8 | use autodie; | ||
| 9 | use feature qw(say); | ||
| 10 | |||
| 11 | use Path::Class qw(file); | ||
| 12 | use Tie::IxHash; | ||
| 13 | |||
| 14 | use Bio::MUST::Core; | ||
| 15 | use aliased 'Bio::MUST::Core::SeqId'; | ||
| 16 | use aliased 'Bio::MUST::Apps::Debrief42::TaxReport'; | ||
| 17 | use aliased 'Bio::MUST::Apps::Debrief42::OrgReport'; | ||
| 18 | |||
| 19 | |||
| 20 | has 'tax_reports' => ( | ||
| 21 | traits => ['Array'], | ||
| 22 | is => 'ro', | ||
| 23 | isa => 'ArrayRef[Str]', | ||
| 24 | required => 1, | ||
| 25 | handles => { | ||
| 26 | all_tax_reports => 'elements', | ||
| 27 | }, | ||
| 28 | ); | ||
| 29 | |||
| 30 | |||
| 31 | has 'orgs' => ( | ||
| 32 | traits => ['Array'], | ||
| 33 | is => 'ro', | ||
| 34 | isa => 'ArrayRef[Str]', | ||
| 35 | required => 1, | ||
| 36 | handles => { | ||
| 37 | all_orgs => 'elements', | ||
| 38 | }, | ||
| 39 | ); | ||
| 40 | |||
| 41 | |||
| 42 | has '_org_reports' => ( | ||
| 43 | traits => ['Hash'], | ||
| 44 | is => 'ro', | ||
| 45 | isa => 'HashRef[Bio::MUST::Apps::Debrief42::OrgReport]', | ||
| 46 | init_arg => undef, | ||
| 47 | default => sub { {} }, | ||
| 48 | handles => { | ||
| 49 | _set_org_report_for => 'set', | ||
| 50 | org_report_for => 'get', | ||
| 51 | }, | ||
| 52 | ); | ||
| 53 | |||
| 54 | |||
| 55 | has '_orgs_by_' . $_ => ( | ||
| 56 | traits => ['Hash'], | ||
| 57 | is => 'ro', | ||
| 58 | isa => 'HashRef[Str]', | ||
| 59 | init_arg => undef, | ||
| 60 | lazy => 1, | ||
| 61 | builder => '_build_orgs_by_' . $_, | ||
| 62 | handles => { | ||
| 63 | 'orgs_by_' . $_ => 'keys', | ||
| 64 | }, | ||
| 65 | ) for qw(contamination completeness); | ||
| 66 | |||
| 67 | |||
| 68 | sub BUILD { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | my %new_seqs_by; | ||
| 72 | |||
| 73 | # parse tax-reports | ||
| 74 | for my $file ( @{ $self->tax_reports } ) { | ||
| 75 | my $tax_report = TaxReport->new( file => file($file) ); | ||
| 76 | |||
| 77 | while ( my $new_seq = $tax_report->next_seq ) { | ||
| 78 | my $acc = $new_seq->acc; | ||
| 79 | my $outfile = $new_seq->outfile; | ||
| 80 | |||
| 81 | my $full_id = $new_seq->seq_id; | ||
| 82 | my $seq_id = SeqId->new( full_id => $full_id ); | ||
| 83 | my $org = $seq_id->full_org; | ||
| 84 | |||
| 85 | push @{ $new_seqs_by{$org}{acc }{$acc } }, $new_seq; | ||
| 86 | push @{ $new_seqs_by{$org}{outfile}{$outfile } }, $new_seq; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | # build OrgReport objects | ||
| 91 | # Note: why to store org twice per report here? | ||
| 92 | for my $org ( $self->all_orgs ) { | ||
| 93 | my $org_report = OrgReport->new( | ||
| 94 | org => $org, | ||
| 95 | _new_seqs_by_acc => $new_seqs_by{$org}{acc } // {}, | ||
| 96 | _new_seqs_by_outfile => $new_seqs_by{$org}{outfile} // {}, | ||
| 97 | ); | ||
| 98 | $self->_set_org_report_for( $org => $org_report ); | ||
| 99 | } | ||
| 100 | |||
| 101 | return; | ||
| 102 | } | ||
| 103 | |||
| 104 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||
| 105 | |||
| 106 | sub _build_orgs_by_contamination { | ||
| 107 | my $self = shift; | ||
| 108 | |||
| 109 | my %contamination_for; | ||
| 110 | |||
| 111 | for my $org ( $self->all_orgs ) { | ||
| 112 | my $org_report = $self->org_report_for($org); | ||
| 113 | |||
| 114 | my $total = 0; | ||
| 115 | my $contaminants = 0; | ||
| 116 | |||
| 117 | for my $new_seqs ( $org_report->all_new_seqs_by_acc ) { | ||
| 118 | for my $new_seq ( @{$new_seqs} ) { | ||
| 119 | $contaminants++ if $new_seq->contam_org; | ||
| 120 | } | ||
| 121 | $total += @{$new_seqs}; | ||
| 122 | } | ||
| 123 | |||
| 124 | $contamination_for{$org} = $contaminants / $total | ||
| 125 | unless $total == 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | return _sort_hash( \%contamination_for, 1 ); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _build_orgs_by_completeness { | ||
| 132 | my $self = shift; | ||
| 133 | |||
| 134 | my %completeness_for; | ||
| 135 | |||
| 136 | for my $org ( $self->all_orgs ) { | ||
| 137 | my $org_report = $self->org_report_for($org); | ||
| 138 | |||
| 139 | my %pure_seq_for; | ||
| 140 | |||
| 141 | for my $outfile ( $org_report->all_outfiles ) { | ||
| 142 | my $new_seqs = $org_report->new_seqs_by_outfile_for($outfile); | ||
| 143 | $pure_seq_for{$outfile} = grep { | ||
| 144 | $_->contam_org || $_->lca =~ m/unclassified \s sequences/xms ? 0 : 1 | ||
| 145 | } @{$new_seqs}; | ||
| 146 | } | ||
| 147 | |||
| 148 | my $total = $self->all_tax_reports; | ||
| 149 | my $enriched_alis = keys %pure_seq_for; | ||
| 150 | $completeness_for{$org} = $enriched_alis / $total | ||
| 151 | unless $total == 0; | ||
| 152 | } | ||
| 153 | |||
| 154 | return _sort_hash( \%completeness_for, 0 ); | ||
| 155 | } | ||
| 156 | |||
| 157 | ## use critic | ||
| 158 | |||
| 159 | sub _sort_hash { | ||
| 160 | my $hashref = shift; | ||
| 161 | my $reverse = shift; | ||
| 162 | |||
| 163 | # Note: Can't we work on the hashref here? | ||
| 164 | my %hash = %{$hashref}; | ||
| 165 | |||
| 166 | # Note: improve this if this is too slow to run | ||
| 167 | # what about a mere reverse of the keys afterwards? | ||
| 168 | tie my %sorted_hash, 'Tie::IxHash'; | ||
| 169 | for my $key ( sort { $reverse ? $hash{$b} <=> $hash{$a} | ||
| 170 | : $hash{$a} <=> $hash{$b} } keys %hash ) { | ||
| 171 | $sorted_hash{$key} = $hash{$key}; | ||
| 172 | } | ||
| 173 | |||
| 174 | return \%sorted_hash; | ||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | __PACKAGE__->meta->make_immutable; | ||
| 179 | 1; | ||
| 180 | |||
| 181 | __END__ | ||
| 182 | |||
| 183 | =pod | ||
| 184 | |||
| 185 | =head1 NAME | ||
| 186 | |||
| 187 | Bio::MUST::Apps::Debrief42::RunReport - Internal class for tabular tax-report parser | ||
| 188 | |||
| 189 | =head1 VERSION | ||
| 190 | |||
| 191 | version 0.213470 | ||
| 192 | |||
| 193 | =head1 SYNOPSIS | ||
| 194 | |||
| 195 | # TODO | ||
| 196 | |||
| 197 | =head1 DESCRIPTION | ||
| 198 | |||
| 199 | # TODO | ||
| 200 | |||
| 201 | =head1 AUTHOR | ||
| 202 | |||
| 203 | Denis BAURAIN <denis.baurain@uliege.be> | ||
| 204 | |||
| 205 | =head1 CONTRIBUTOR | ||
| 206 | |||
| 207 | =for stopwords Mick VAN VLIERBERGHE | ||
| 208 | |||
| 209 | Mick VAN VLIERBERGHE <mvanvlierberghe@doct.uliege.be> | ||
| 210 | |||
| 211 | =head1 COPYRIGHT AND LICENSE | ||
| 212 | |||
| 213 | This software is copyright (c) 2013 by University of Liege / Unit of Eukaryotic Phylogenomics / Denis BAURAIN. | ||
| 214 | |||
| 215 | This is free software; you can redistribute it and/or modify it under | ||
| 216 | the same terms as the Perl 5 programming language system itself. | ||
| 217 | |||
| 218 | =cut |