Bio/SeqFeature/Tools/IDHandler.pm

filter: Statementstmt 12.5% 8 / 64 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 33.3% 2 / 6 Podpod 100% 4 / 4 totaltotal 11.7% 14 / 119 CC 21 SCAR 41.5
CC
21
Cov
8%
generate_unique_persistent_id
90.0
create_hierarchy_from_ParentIDs
72.0
set_ParentIDs_from_hierarchy
42.0
CRAP 63.2
« Bio/Root/Utilities.pm ↑ Summary Bio/SeqFeature/Tools/Unflattener.pm »
1 #
2 # bioperl module for Bio::SeqFeature::Tools::IDHandler
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Chris Mungall <cjm@fruitfly.org>
7 #
8 # Copyright Chris Mungall
9 #
10 # You may distribute this module under the same terms as perl itself
11  
12 # POD documentation - main docs before the code
13  
14 =head1 NAME
15  
16 Bio::SeqFeature::Tools::IDHandler - maps $seq_feature-E<gt>primary_tag
17  
18 =head1 SYNOPSIS
19  
20 use Bio::SeqIO;
21 use Bio::SeqFeature::Tools::IDHandler;
22  
23  
24 =head1 DESCRIPTION
25  
26 Class to map $seq_feature-E<gt>primary_tag
27  
28  
29 =head1 FEEDBACK
30  
31 =head2 Mailing Lists
32  
33 User feedback is an integral part of the evolution of this and other
34 Bioperl modules. Send your comments and suggestions preferably to the
35 Bioperl mailing lists Your participation is much appreciated.
36  
37 bioperl-l@bioperl.org - General discussion
38 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
39  
40 =head2 Support
41  
42 Please direct usage questions or support issues to the mailing list:
43  
44 I<bioperl-l@bioperl.org>
45  
46 rather than to the module maintainer directly. Many experienced and
47 reponsive experts will be able look at the problem and quickly
48 address it. Please include a thorough description of the problem
49 with code and data examples if at all possible.
50  
51 =head2 Reporting Bugs
52  
53 report bugs to the Bioperl bug tracking system to help us keep track
54 the bugs and their resolution. Bug reports can be submitted via the
55 web:
56  
57 https://github.com/bioperl/bioperl-live/issues
58  
59 =head1 AUTHOR - Chris Mungall
60  
61 Email: cjm@fruitfly.org
62  
63 =head1 APPENDIX
64  
65 The rest of the documentation details each of the object
66 methods. Internal methods are usually preceded with a _
67  
68 =cut
69  
70  
71 # Let the code begin...
72  
73 package Bio::SeqFeature::Tools::IDHandler;
74 1 $Bio::SeqFeature::Tools::IDHandler::VERSION = '1.7.8';
75 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 # Object preamble - inherits from Bio::Root::Root
78  
79 1 ▶use base qw(Bio::Root::Root);
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 =head2 new
82  
83 Title : new
84 Usage : $unflattener = Bio::SeqFeature::Tools::IDHandler->new();
85 Function: constructor
86 Example :
87 Returns : a new Bio::SeqFeature::Tools::IDHandler
88 Args : see below
89  
90  
91 =cut
92  
93 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
94 0 my($class,@args) = @_;
95 0 my $self = $class->SUPER::new(@args);
96  
97 0 my($generate_id_sub) =
98 $self->_rearrange([qw(GENERATE_ID_SUB
99 )],
100 @args);
101  
102 0 return $self; # success - we hope!
103 }
104  
105 =head2 set_ParentIDs_from_hierarchy()
106  
107 Title : set_ParentIDs_from_hierarchy()
108 Usage : $idhandler->set_ParentIDs_from_hierarchy($fholder)
109 Function: populates tags Parent and ID via holder hierarchy
110 Example :
111 Returns :
112 Args : Bio::featureHolderI (either a SeqFeature or a Seq)
113  
114 This is mainly for GFF3 export
115  
116 GFF3 uses the tags ID and Parent to represent the feature containment
117 hierarchy; it does NOT use the feature holder tree
118  
119 This method sets Parent (and ID for any parents not set) based on
120 feature holder/containement hierarchy, ready for GFF3 output
121  
122 =cut
123  
124 # method author: cjm@fruitfly.org
125 0 ▶sub set_ParentIDs_from_hierarchy(){
Subroutine set_ParentIDs_from_hierarchy: not called CC=6 SCAR=37.4
Pod documented
126 0 my $self = shift;
127 0 my ($featholder) = @_;
128  
129 # we will traverse the tree of contained seqfeatures
130 # (a seqfeature is itself a holder)
131  
132 # start with the top-level features
133 0 my @sfs = $featholder->get_SeqFeatures;
134  
135 # clear existing parent tags
136 # (we assume this is the desired behaviour)
137 0 my @all_sfs = $featholder->get_all_SeqFeatures;
138 0 foreach (@all_sfs) {
139 0 ▶ if ($_->has_tag('Parent')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->has_tag("Parent")) 0 0 0
140 0 $_->remove_tag('Parent');
141 }
142 }
143
144  
145 # iterate until entire tree traversed
146 0 ▶ while (@sfs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sfs) 0 0 0
147 0 my $sf = shift @sfs;
148 0 my @subsfs = $sf->get_SeqFeatures;
149  
150 # see if the ID tag
151 0 my $id = $sf->primary_id;
152 0 ▶ if (!$id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id) 0 0 0
153 # the skolem function feature(seq,start,end,type)
154 # is presumed to uniquely identify this feature, and
155 # to also be persistent
156 0 $id = $sf->generate_unique_persistent_id;
157 }
158 0 foreach my $subsf (@subsfs) {
159 0 $subsf->add_tag_value('Parent', $id);
160 }
161
162 # push children on to end of stack (breadth first search)
163 0 push(@sfs, @subsfs);
164 }
165 0 return;
166 }
167  
168 =head2 create_hierarchy_from_ParentIDs
169  
170 Title : create_hierarchy_from_ParentIDs
171 Usage : $idhandler->set_ParentIDs_from_hierarchy($fholder)
172 Function: inverse of set_ParentIDs_from_hierarchy
173 Example :
174 Returns : list of top SeqFeatures
175 Args :
176  
177  
178 =cut
179  
180 0 ▶sub create_hierarchy_from_ParentIDs{
Subroutine create_hierarchy_from_ParentIDs: not called CC=8 SCAR=42.8
Pod documented
181 0 my ($self,$featholder,@args) = @_;
182  
183 0 my @sfs = $featholder->get_all_SeqFeatures;
184 0 my %sf_by_ID = ();
185 0 foreach (@sfs) {
186 0 my $id = $_->primary_id;
187 0 ▶ next unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
188 0 ▶ if ($sf_by_ID{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sf_by_ID{$id}) 0 0 0
189 0 $featholder->throw("DUPLICATE ID: $id");
190 }
191 0 $sf_by_ID{$id} = $_;
192 0 $_->remove_SeqFeatures; # clear existing hierarchy (assume this is desired)
193 }
194 0 ▶ if (!%sf_by_ID) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%sf_by_ID) 0 0 0
195 # warn??
196 # this is actually expected behaviour for some kinds of data;
197 # eg lists of STSs - no containment hierarchy
198 0 return;
199 }
200  
201 my @topsfs =
202 grep {
203 0 my @parents = $_->get_tagset_values('Parent');
204 0 foreach my $parent (@parents) {
205 $sf_by_ID{$parent}->add_SeqFeature($_)
206 0 ▶ if exists $sf_by_ID{$parent};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sf_by_ID{$parent} 0 0 0
207 }
208 0 !@parents;
209 } @sfs;
210 0 $featholder->remove_SeqFeatures;
211 0 $featholder->add_SeqFeature($_) foreach @topsfs;
212 0 return @topsfs;
213 }
214  
215  
216 =head2 generate_unique_persistent_id
217  
218 Title : generate_unique_persistent_id
219 Usage :
220 Function: generates a unique and persistent identifier for this
221 Example :
222 Returns : value of primary_id (a scalar)
223 Args :
224  
225 Will generate an ID, B<and> set primary_id() (see above)
226  
227 The ID is a string generated from
228  
229 seq_id
230 primary_tag
231 start
232 end
233  
234 There are three underlying assumptions: that all the above accessors
235 are set; that seq_id is a persistent and unique identifier for the
236 sequence containing this feature; and that
237  
238 (seq_id, primary_tag, start, end)
239  
240 is a "unique constraint" over features
241  
242 The ID is persistent, so long as none of these values change - if they
243 do, it is considered a separate entity
244  
245 =cut
246  
247 # method author: cjm@fruitfly.org
248 0 ▶sub generate_unique_persistent_id{
Subroutine generate_unique_persistent_id: not called CC=9 SCAR=45.0
Pod documented
249 0 my ($self,$sf,@args) = @_;
250  
251 0 my $id;
252 0 ▶ if (!$sf->isa("Bio::SeqFeatureI")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sf->isa("Bio::SeqFeatureI")) 0 0 0
253 0 $sf->throw("not a Bio::SeqFeatureI");
254 }
255 0 ▶ my $seq_id = $sf->seq_id || $sf->throw("seq_id must be set: ".$sf->display_name);
Condition0%0/3
$sf->seq_id || $sf->throw('seq_id must be set: ' . $sf->display_name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$sf->seq_id || $sf->throw('seq_id must be set: ' . $sf->display_name)
$sf->seq_id $sf->throw('seq_id must be set: ' . $sf->display_name)
Truth table0%0/3
A || B
A= $sf->seq_idB= $sf->throw('seq_id must be set: ' . $sf->display_name)
ABresult
1X1
011
000
256 #my $seq_id = $sf->seq_id || 'unknown_seq';
257 0 ▶ if ($sf->has_tag('transcript_id')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sf->has_tag('transcript_id')) { } 0 0 0
elsif ($sf->has_tag('protein_id')) { } 0 0 0
258 0 ($id) = $sf->get_tag_values('transcript_id');
259 }
260 elsif ($sf->has_tag('protein_id')) {
261 0 ($id) = $sf->get_tag_values('protein_id');
262 }
263 else {
264 0 ▶ my $source = $sf->source_tag || $sf->throw("source tag must be set: ".$sf->display_name);
Condition0%0/3
$sf->source_tag || $sf->throw('source tag must be set: ' . $sf->display_name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$sf->source_tag || $sf->throw('source tag must be set: ' . $sf->display_name)
$sf->source_tag $sf->throw('source tag must be set: ' . $sf->display_name)
Truth table0%0/3
A || B
A= $sf->source_tagB= $sf->throw('source tag must be set: ' . $sf->display_name)
ABresult
1X1
011
000
265 #my $source = $sf->source_tag || 'unknown_source';
266 0 ▶ my $start = $sf->start || $sf->throw("start must be set or is zero: ".$sf->display_name);
Condition0%0/3
$sf->start || $sf->throw('start must be set or is zero: ' . $sf->display_name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$sf->start || $sf->throw('start must be set or is zero: ' . $sf->display_name)
$sf->start $sf->throw('start must be set or is zero: ' . $sf->display_name)
Truth table0%0/3
A || B
A= $sf->startB= $sf->throw('start must be set or is zero: ' . $sf->display_name)
ABresult
1X1
011
000
267 0 ▶ my $end = $sf->end || $sf->throw("end must be set");
Condition0%0/3
$sf->end || $sf->throw('end must be set')
l!l&&r!l&&!r
000
MC/DC0%0/2
$sf->end || $sf->throw('end must be set')
$sf->end $sf->throw('end must be set')
Truth table0%0/3
A || B
A= $sf->endB= $sf->throw('end must be set')
ABresult
1X1
011
000
268 0 ▶ my $type = $sf->primary_tag || $sf->throw("primary_tag/type must be set: ".$sf->display_name);
Condition0%0/3
$sf->primary_tag || $sf->throw('primary_tag/type must be set: ' . $sf->display_name)
l!l&&r!l&&!r
000
MC/DC0%0/2
$sf->primary_tag || $sf->throw('primary_tag/type must be set: ' . $sf->display_name)
$sf->primary_tag $sf->throw('primary_tag/type must be set: ' . $sf->display_name)
Truth table0%0/3
A || B
A= $sf->primary_tagB= $sf->throw('primary_tag/type must be set: ' . $sf->display_name)
ABresult
1X1
011
000
269  
270 0 $id = "$source:$type:$seq_id:$start:$end";
271 }
272 0 $sf->primary_id($id);
273 0 return $id;
274 }
275  
276 1 1;
« Bio/Root/Utilities.pm ↑ Summary Bio/SeqFeature/Tools/Unflattener.pm »