blib/lib/Bio/Community/TaxonomyUtils.pm

filter: Statementstmt 100% 72 / 72 Branchbran 75.0% 33 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 91.4% 118 / 129 CC 23 SCAR 17.0
CC
23
Cov
91%
clean_lineage_arr
9.1
split_lineage_string
7.1
get_taxon_lineage
5.1
CRAP 5.5
« blib/lib/Bio/Community.pm ↑ Summary blib/lib/Bio/Community/Meta/Beta.pm »
1 # BioPerl module for Bio::Community::TaxonomyUtils
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::TaxonomyUtils - Functions for manipulating taxonomic lineages
13  
14 =head1 SYNOPSIS
15  
16 use Bio::Community::TaxonomyUtils qw(split_lineage_string get_lineage_string);
17  
18 my $lineage = 'Bacteria;WCHB1-60;unidentified';
19 my $lineage_arr = split_lineage_string($lineage);
20 $lineage = get_lineage_string($lineage_arr);
21  
22 print "Lineage is: $lineage\n"; # Bacteria;WCHB1-60
23  
24 =head1 DESCRIPTION
25  
26 This module implements functions to manipulate taxonomic lineages, as arrayref
27 of taxon names or taxon objects.
28  
29 =head1 AUTHOR
30  
31 Florent Angly L<florent.angly@gmail.com>
32  
33 =head1 SUPPORT AND BUGS
34  
35 User feedback is an integral part of the evolution of this and other Bioperl
36 modules. Please direct usage questions or support issues to the mailing list,
37 L<bioperl-l@bioperl.org>, rather than to the module maintainer directly. Many
38 experienced and reponsive experts will be able look at the problem and quickly
39 address it. Please include a thorough description of the problem with code and
40 data examples if at all possible.
41  
42 If you have found a bug, please report it on the BioPerl bug tracking system
43 to help us keep track the bugs and their resolution:
44 L<https://redmine.open-bio.org/projects/bioperl/>
45  
46 =head1 COPYRIGHT
47  
48 Copyright 2011-2014 by Florent Angly <florent.angly@gmail.com>
49  
50 This library is free software; you can redistribute it and/or modify
51 it under the same terms as Perl itself, either Perl version 5.10.1 or,
52 at your option, any later version of Perl 5 you may have available.
53  
54 =head1 APPENDIX
55  
56 The rest of the documentation details each of the object
57 methods. Internal methods are usually preceded with a _
58  
59 =cut
60  
61  
62 package Bio::Community::TaxonomyUtils;
63  
64 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 13 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 13 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
69 13 our @EXPORT = qw(
70 split_lineage_string
71 clean_lineage_arr
72 get_taxon_lineage
73 get_lineage_string
74 );
75  
76 13 ▶use base 'Bio::Root::Root';
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78  
79 13 my $sep = ';'; # separator
80 13 my $wsp = ''; # white space
81 13 my $clean_front_re = qr/^(?:Root)$/i;
82 13 my $clean_rear_re = qr/^(?:\S__||Other|No blast hit|unidentified|uncultured|environmental)$/i;
83  
84  
85 =head2 split_lineage_string
86  
87 Function: Split a lineage string, clean it and autodetect whitespaces.
88 Use the ';' separator is used to split lineages like 'Bacteria;
89 Proteobacteria' into an arrayref of its individual components, e.g.
90 ['Bacteria','Proteobact']. The number and type of spaces after the
91 separator is saved for future use in get_lineage_string(), the
92 reciprocal function. Also, optionally clean the arrayref using
93 clean_lineage_arr().
94 Usage : my $taxa_names = split_lineage($lineage_string);
95 Args : * a lineage string
96 * whether to clean taxonomy or not (default is to clean)
97 Returns : an arrayref of taxon names
98  
99 =cut
100  
101 13 ▶func split_lineage_string ($lineage_str, $clean=1) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine split_lineage_string: called CC=7 SCAR=19.6
Branch: 3 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 49 49
scalar @_ > 1 ? : 20 29 49
if scalar @_ > 2 0 49 49
102 49 ▶ if ( $lineage_str =~ m/$sep(\s*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lineage_str =~ /$sep(\s*)/) 45 4 49
103 45 ▶ if (defined $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) 45 0 45
104 45 $wsp = $1;
105 }
106 }
107 49 my $names = [ split qr/$sep\s*/, $lineage_str ];
108 49 ▶ if ($clean) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean) 29 20 49
109 29 $names = clean_lineage_arr($names);
110 }
111 49 return $names;
112 }
113  
114  
115 =head2 clean_lineage_arr
116  
117 Function: Two step cleanup:
118 1/ At the beginning of the array, remove anything called 'Root'
119 2/ Starting from the end of the array, remove ambiguous taxonomic
120 information such as:
121 '', 'No blast hit', 'unidentified', 'uncultured', 'environmental',
122 'Other', 'g__', 's__', etc
123 Usage : $lineage_arr = clean_lineage_arr($lineage_arr);
124 Args : A lineage arrayref (either taxon names or objects)
125 Returns : A lineage arrayref
126  
127 =cut
128  
129 13 ▶func clean_lineage_arr ($lineage_arr) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine clean_lineage_arr: called CC=9 SCAR=22.0
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 33 33
if scalar @_ > 1 0 33 33
130 # Clean the front
131 33 my $elem = $lineage_arr->[0];
132 33 ▶ if ( defined $elem ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $elem) 33 0 33
133 33 ▶ $elem = $elem->node_name if ref $elem;
Branch: 1 branch
BranchTrueFalseTotal
if ref $elem 4 29 33
134 33 ▶ if ($elem =~ $clean_front_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem =~ /$clean_front_re/) 4 29 33
135 4 shift @$lineage_arr;
136 }
137 }
138 # Clean the rear
139 33 ▶ while ( $elem = $lineage_arr->[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem = $lineage_arr->[-1]) 50 4 54
140 50 ▶ $elem = $elem->node_name if ref $elem;
Branch: 1 branch
BranchTrueFalseTotal
if ref $elem 4 46 50
141 50 ▶ if ($elem =~ $clean_rear_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem =~ /$clean_rear_re/) { } 21 29 50
142 21 pop @$lineage_arr;
143 } else {
144 29 last;
145 }
146 }
147 33 return $lineage_arr;
148 }
149  
150  
151 =head2 get_taxon_lineage
152  
153 Function: Take a taxon object and return its lineage as an arrayref of the
154 taxon itself, preceded by its ancestor taxa.
155 Usage : my $lineage_arr = get_taxon_lineage($taxon);
156 Args : A taxon object
157 Returns : An arrayref of taxon names
158  
159 =cut
160  
161 13 ▶func get_taxon_lineage ($taxon) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine get_taxon_lineage: called CC=5 SCAR=16.3
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 4 4
if scalar @_ > 1 0 4 4
162 4 my @arr;
163 4 ▶ if ($taxon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($taxon) 4 0 4
164 4 @arr = ($taxon);
165 4 my $ancestor = $taxon;
166 4 ▶ while ( $ancestor = $ancestor->ancestor ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ancestor = $ancestor->ancestor) 18 4 22
167 18 unshift @arr, $ancestor;
168 }
169 }
170 4 return \@arr;
171 }
172  
173  
174 ### Rename this join_lineage_arr (and have alias for backward compatibility)
175  
176 =head2 get_lineage_string
177  
178 Function: Take a lineage arrayref and return a full lineage string by joining
179 the elements using the ';' separator. The opposite operation is
180 split_lineage_string().
181 Usage : my $lineage = get_lineage_string(['Bacteria', 'Proteobacteria']);
182 or
183 my $lineage = get_lineage_string([$taxon1, $taxon2]);
184 Args : * Arrayref of taxon names or objects
185 * Optional: whitespace string to include after separator (omit to autodetect)
186 Returns : A lineage string
187  
188 =cut
189  
190 13 ▶func get_lineage_string ($lineage_arr, $space?) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine get_lineage_string: called CC=5 SCAR=16.2
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @_ > 0 0 46 46
if scalar @_ > 2 0 46 46
191 # Use specified or detected whitespaces
192 46 ▶ $space = defined $space ? $space : $wsp;
Branch: 1 branch
BranchTrueFalseTotal
defined $space ? : 40 6 46
193 # Get lineage array and construct lineage string
194 46 ▶ my @names = map { ref $_ ? $_->node_name : $_ } @$lineage_arr;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 29 280 309
195 46 return join( $sep.$space, @names );
196 }
197  
198  
199  
200 13 1;
« blib/lib/Bio/Community.pm ↑ Summary blib/lib/Bio/Community/Meta/Beta.pm »