blib/lib/Bio/Community/Tools/ShrapnelCleaner.pm

filter: Statementstmt 65.1% 28 / 43 Branchbran 16.6% 2 / 12 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 52.7% 38 / 72 CC 10 SCAR 35.0
CC
10
Cov
51%
clean
52.2
BEGIN
1.0
BEGIN
1.0
CRAP 33.0
« blib/lib/Bio/Community/Tools/Transformer.pm ↑ Summary blib/lib/Bio/Community/IO/Driver/dummy.pm »
1 # BioPerl module for Bio::Community::Tools::ShrapnelCleaner
2 #
3 # Please direct questions and support issues to <bioperl-l@bioperl.org>
4 #
5 # Copyright 2011-2014 Florent Angly <florent.angly@gmail.com>
6 #
7 # You may distribute this module under the same terms as perl itself
8  
9  
10 =head1 NAME
11  
12 Bio::Community::Tools::ShrapnelCleaner - Remove low-count, low-abundance community members
13  
14 =head1 SYNOPSIS
15  
16 use Bio::Community::Tools::ShrapnelCleaner;
17  
18 # Remove singletons the communities in the given metacommunity
19 my $cleaner = Bio::Community::Tools::ShrapnelCleaner->new(
20 -metacommunity => $meta,
21 );
22 $cleaner->clean;
23  
24 =head1 DESCRIPTION
25  
26 This module takes biological communities (contained in a metacommunity) and
27 removes shrapnel, low abundance, low prevalence members that are likely to be
28 the result of sequencing errors (when doing sequence-based analyses). By default,
29 the cleaner removes only singletons, i.e. community members that appear in only
30 one community (prevalence of 1) and have only 1 count. You can specify your own
31 count and prevalence thresholds though.
32  
33 =head1 AUTHOR
34  
35 Florent Angly L<florent.angly@gmail.com>
36  
37 =head1 SUPPORT AND BUGS
38  
39 User feedback is an integral part of the evolution of this and other Bioperl
40 modules. Please direct usage questions or support issues to the mailing list,
41 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
42 experienced and reponsive experts will be able look at the problem and quickly
43 address it. Please include a thorough description of the problem with code and
44 data examples if at all possible.
45  
46 If you have found a bug, please report it on the BioPerl bug tracking system
47 to help us keep track the bugs and their resolution:
48 L<https://redmine.open-bio.org/projects/bioperl/>
49  
50 =head1 COPYRIGHT
51  
52 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
53  
54 This library is free software; you can redistribute it and/or modify
55 it under the same terms as Perl itself, either Perl version 5.10.1 or,
56 at your option, any later version of Perl 5 you may have available.
57  
58 =head1 APPENDIX
59  
60 The rest of the documentation details each of the object
61 methods. Internal methods are usually preceded with a _
62  
63 =head2 new
64  
65 Function: Create a new Bio::Community::Tool::ShrapnelCleaner object
66 Usage : my $cleaner = Bio::Community::Tool::ShrapnelCleaner->new( );
67 Args : -metacommunity : See metacommunity().
68 -count_threshold : See count_threshold().
69 -prevalence_threshold: See prevalence_threshold().
70 Returns : a new Bio::Community::Tools::ShrapnelCleaner object
71  
72 =cut
73  
74  
75 package Bio::Community::Tools::ShrapnelCleaner;
76  
77 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 1 extends 'Bio::Root::Root';
84  
85  
86 =head2 metacommunity
87  
88 Function: Get or set the communities to process.
89 Usage : my $communities = $cleaner->communities;
90 Args : A Bio::Community::Meta object
91 Returns : A Bio::Community::Meta object
92  
93 =cut
94  
95 1 has metacommunity => (
96 is => 'rw',
97 isa => 'Maybe[Bio::Community::Meta]',
98 required => 0,
99 default => undef,
100 lazy => 1,
101 init_arg => '-metacommunity',
102 );
103  
104  
105 =head2 count_threshold
106  
107 Function: Get or set the count threshold. Community members with a count equal
108 or lower than this threshold are removed (provided they also meet the
109 prevalence_threshold).
110 Usage : my $count_thresh = $cleaner->count_threshold;
111 Args : positive integer for the count
112 Returns : positive integer for the count
113  
114 =cut
115  
116 1 has count_threshold => (
117 is => 'rw',
118 isa => 'Maybe[PositiveNum]',
119 required => 0,
120 default => 1,
121 lazy => 1,
122 init_arg => '-count_threshold',
123 );
124  
125  
126 =head2 prevalence_threshold
127  
128 Function: Get or set the prevalence threshold. Community members with a
129 prevalence (number of communities that the member is found in) equal
130 or lower than this threshold are removed (provided they also meet the
131 count_threshold).
132 Usage : my $prevalence_thresh = $cleaner->prevalence_threshold;
133 Args : positive integer for the prevalence
134 Returns : positive integer for the prevalence
135  
136 =cut
137  
138 1 has prevalence_threshold => (
139 is => 'rw',
140 isa => 'Maybe[PositiveNum]',
141 required => 0,
142 default => 1,
143 lazy => 1,
144 init_arg => '-prevalence_threshold',
145 );
146  
147  
148 =head2 clean
149  
150 Function: Remove singletons from the communities and return the updated
151 metacommunity.
152 Usage : my $meta = $cleaner->clean;
153 Args : none
154 Returns : arrayref of Bio::Community objects
155  
156 =cut
157  
158 1 ▶method clean () {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine clean: called CC=10 SCAR=39.5
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 0 1 1
159 # Sanity check
160 1 my $meta = $self->metacommunity;
161 1 ▶ if ( (not $meta) || ($meta->get_communities_count == 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $meta or $meta->get_communities_count == 0) 1 0 1
Condition33%1/3
not $meta or $meta->get_communities_count == 0
l!l&&r!l&&!r
100
MC/DC0%0/2
not $meta or $meta->get_communities_count == 0
not $meta $meta->get_communities_count == 0
Truth table33%1/3
not(A) or B
A= not $metaB= $meta->get_communities_count == 0
ABresult
1X1
011
000
162 1 $self->throw('Should have a metacommunity containing at least one community');
163 }
164  
165 # Remove singletons
166 0 my $count_thres = $self->count_threshold;
167 0 my $prevalence_thres = $self->prevalence_threshold;
168 0 for my $member ( @{$meta->get_all_members} ) {
169 0 my $total_count = 0; # sum of member counts in all communities
170 0 my $prevalence = 0; # in how many communities was the member seen
171 0 ▶ while (my $community = $meta->next_community) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $community = $meta->next_community) 0 0 0
172 0 my $count = $community->get_count($member);
173 0 ▶ if ($count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 0 0 0
174 0 $prevalence++;
175 0 $total_count += $count;
176 }
177 }
178 0 ▶ if ( ($total_count <= $count_thres) && ($prevalence <= $prevalence_thres) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_count <= $count_thres and $prevalence <= $prevalence_thres) 0 0 0
Condition0%0/3
$total_count <= $count_thres and $prevalence <= $prevalence_thres
!ll&&!rl&&r
000
MC/DC0%0/2
$total_count <= $count_thres and $prevalence <= $prevalence_thres
$total_count <= $count_thres $prevalence <= $prevalence_thres
Truth table0%0/3
A and B
A= $total_count <= $count_thresB= $prevalence <= $prevalence_thres
ABresult
0X0
100
111
179 # Remove all of this member
180 0 ▶ while (my $community = $meta->next_community) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $community = $meta->next_community) 0 0 0
181 0 $community->remove_member($member);
182 }
183 }
184 }
185  
186 0 return $meta;
187 }
188  
189  
190 1 __PACKAGE__->meta->make_immutable;
191  
192 1 1;
« blib/lib/Bio/Community/Tools/Transformer.pm ↑ Summary blib/lib/Bio/Community/IO/Driver/dummy.pm »