Bio/OntologyIO/dagflat.pm

filter: Statementstmt 90.4% 284 / 314 Branchbran 74.4% 125 / 168 Conditioncond 66.6% 16 / 24 MC/DCmcdc 37.5% 6 / 16 Subroutinesub 97.5% 40 / 41 Podpod 71.4% 5 / 7 totaltotal 83.5% 476 / 570 CC 101 SCAR 24.2
CC
101
Cov
84%
_parse_flat_file
27.1
_initialize
20.7
_next_term
14.2
CRAP 11.3
« Bio/TreeIO/phyloxml.pm ↑ Summary Bio/AlignIO/arp.pm »
1 #
2 # BioPerl module for Bio::OntologyIO::dagflat
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Hilmar Lapp, hlapp at gmx.net
7 #
8 # (c) Christian M. Zmasek, czmasek-at-burnham.org, 2002.
9 # (c) Hilmar Lapp, hlapp at gmx.net, 2003.
10 # (c) GNF, Genomics Institute of the Novartis Research Foundation, 2002.
11 #
12 # You may distribute this module under the same terms as perl itself.
13 # Refer to the Perl Artistic License (see the license accompanying this
14 # software package, or see http://www.perl.com/language/misc/Artistic.html)
15 # for the terms under which you may use, modify, and redistribute this module.
16 #
17 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
18 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 #
21 # You may distribute this module under the same terms as perl itself
22  
23 # POD documentation - main docs before the code
24  
25 =head1 NAME
26  
27 Bio::OntologyIO::dagflat - a base class parser for GO flat-file type formats
28  
29 =head1 SYNOPSIS
30  
31 use Bio::OntologyIO;
32  
33 # do not use directly -- use via Bio::OntologyIO
34 # e.g., the GO parser is a simple extension of this class
35 my $parser = Bio::OntologyIO->new
36 ( -format => "go",
37 -defs_file => "/home/czmasek/GO/GO.defs",
38 -files => ["/home/czmasek/GO/component.ontology",
39 "/home/czmasek/GO/function.ontology",
40 "/home/czmasek/GO/process.ontology"] );
41  
42 my $go_ontology = $parser->next_ontology();
43  
44 my $IS_A = Bio::Ontology::RelationshipType->get_instance( "IS_A" );
45 my $PART_OF = Bio::Ontology::RelationshipType->get_instance( "PART_OF" );
46 my $RELATED_TO = Bio::Ontology::RelationshipType->get_instance( "RELATED_TO" );
47  
48 =head1 DESCRIPTION
49  
50 Needs Graph.pm from CPAN.
51  
52 =head1 FEEDBACK
53  
54 =head2 Mailing Lists
55  
56 User feedback is an integral part of the evolution of this and other
57 Bioperl modules. Send your comments and suggestions preferably to the
58 Bioperl mailing lists Your participation is much appreciated.
59  
60 bioperl-l@bioperl.org - General discussion
61 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
62  
63 =head2 Support
64  
65 Please direct usage questions or support issues to the mailing list:
66  
67 I<bioperl-l@bioperl.org>
68  
69 rather than to the module maintainer directly. Many experienced and
70 reponsive experts will be able look at the problem and quickly
71 address it. Please include a thorough description of the problem
72 with code and data examples if at all possible.
73  
74 =head2 Reporting Bugs
75  
76 Report bugs to the Bioperl bug tracking system to help us keep track
77 the bugs and their resolution. Bug reports can be submitted via the
78 web:
79  
80 https://github.com/bioperl/bioperl-live/issues
81  
82 =head1 AUTHOR
83  
84 Christian M. Zmasek
85  
86 Email: czmasek-at-burnham.org or cmzmasek@yahoo.com
87  
88 WWW: http://monochrome-effect.net/
89  
90 Address:
91  
92 Genomics Institute of the Novartis Research Foundation
93 10675 John Jay Hopkins Drive
94 San Diego, CA 92121
95  
96 =head2 CONTRIBUTOR
97  
98 Hilmar Lapp, hlapp at gmx.net
99  
100 =head1 APPENDIX
101  
102 The rest of the documentation details each of the object
103 methods. Internal methods are usually preceded with a _
104  
105 =cut
106  
107  
108 # Let the code begin...
109  
110  
111 package Bio::OntologyIO::dagflat;
112 6 $Bio::OntologyIO::dagflat::VERSION = '1.7.8';
113 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 6 ▶use Bio::Root::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 6 ▶use Bio::Ontology::OBOEngine;
Subroutine BEGIN: called CC=1 SCAR=0.0
117 6 ▶use Bio::Ontology::Ontology;
Subroutine BEGIN: called CC=1 SCAR=0.0
118 6 ▶use Bio::Ontology::OntologyStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
119 6 ▶use Bio::Ontology::TermFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
120 6 ▶use Bio::Annotation::DBLink;
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 6 ▶use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 6 ▶use constant FALSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
124  
125  
126 6 ▶use base qw(Bio::OntologyIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128  
129 =head2 new
130  
131 Title : new
132 Usage : $parser = Bio::OntologyIO->new(
133 -format => "go",
134 -defs_file => "/path/to/GO.defs",
135 -files => ["/path/to/component.ontology",
136 "/path/to/function.ontology",
137 "/path/to/process.ontology"] );
138 Function: Creates a new dagflat parser.
139 Returns : A new dagflat parser object, implementing Bio::OntologyIO.
140 Args : -defs_file => the name of the file holding the term
141 definitions
142 -files => a single ontology flat file holding the
143 term relationships, or an array ref holding
144 the file names (for GO, there will usually be
145 3 files: component.ontology, function.ontology,
146 process.ontology)
147 -file => if there is only a single flat file, it may
148 also be specified via the -file parameter
149 -ontology_name => the name of the ontology; if not specified the
150 parser will auto-discover it by using the term
151 that starts with a $, and converting underscores
152 to spaces
153 -engine => the Bio::Ontology::OntologyEngineI object
154 to be reused (will be created otherwise); note
155 that every Bio::Ontology::OntologyI will
156 qualify as well since that one inherits from the
157 former.
158  
159 See L<Bio::OntologyIO>.
160  
161 =cut
162  
163 # in reality, we let OntologyIO::new do the instantiation, and override
164 # _initialize for all initialization work
165 5 ▶sub _initialize {
Subroutine _initialize: called CC=15 SCAR=30.3
166 5 my ($self, %arg) = @_;
167  
168 5 my ( $defs_file_name,$files,$defs_url,$url,$name,$eng ) =
169 $self->_rearrange([qw( DEFS_FILE
170 FILES
171 DEFS_URL
172 URL
173 ONTOLOGY_NAME
174 ENGINE)
175 ],
176 %arg );
177  
178 5 delete($arg{-url}); #b/c GO has 3 files...
179  
180 5 $self->SUPER::_initialize( %arg );
181  
182 5 $self->_done( FALSE );
183 5 $self->_not_first_record( FALSE );
184 5 $self->_term( "" );
185 5 delete $self->{'_ontologies'};
186  
187 # ontology engine (and possibly name if it's an OntologyI)
188 5 ▶ $eng = Bio::Ontology::OBOEngine->new() unless $eng;
Branch: 1 branch
BranchTrueFalseTotal
unless $eng 5 0 5
189 5 ▶ if($eng->isa("Bio::Ontology::OntologyI")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eng->isa("Bio::Ontology::OntologyI")) 0 5 5
190 0 $self->ontology_name($eng->name());
191 0 ▶ $eng = $eng->engine() if $eng->can('engine');
Branch: 1 branch
BranchTrueFalseTotal
if $eng->can("engine") 0 0 0
192 }
193 5 $self->_ont_engine($eng);
194  
195 # flat files to parse
196 5 ▶ if(defined($defs_file_name) && defined($defs_url)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $defs_file_name and defined $defs_url) { } 0 5 5
Condition66%2/3
defined $defs_file_name and defined $defs_url
!ll&&!rl&&r
320
MC/DC0%0/2
defined $defs_file_name and defined $defs_url
defined $defs_file_name defined $defs_url
Truth table66%2/3
A and B
A= defined $defs_file_nameB= defined $defs_url
ABresult
0X0
100
111
197 0 $self->throw('cannot provide both -defs_file and -defs_url');
198 } else {
199 5 ▶ defined($defs_file_name) && $self->defs_file( $defs_file_name );
Branch: 1 branch
BranchTrueFalseTotal
defined $defs_file_name and $self->defs_file($defs_file_name) 2 3 5
200 5 ▶ defined($defs_url) && $self->defs_url( $defs_url );
Branch: 1 branch
BranchTrueFalseTotal
defined $defs_url and $self->defs_url($defs_url) 0 5 5
201 }
202  
203 5 ▶ if(defined($files) && defined($url)){
Branch: 3 branches
BranchTrueFalseTotal
if (defined $files and defined $url) { } 0 5 5
elsif (defined $files) { } 1 4 5
elsif (defined $url) { } 1 3 4
Condition66%2/3
defined $files and defined $url
!ll&&!rl&&r
410
MC/DC0%0/2
defined $files and defined $url
defined $files defined $url
Truth table66%2/3
A and B
A= defined $filesB= defined $url
ABresult
0X0
100
111
204 } elsif(defined($files)){
205 1 ▶ $self->{_flat_files} = $files ? ref($files) ? $files : [$files] : [];
Branch: 2 branches
BranchTrueFalseTotal
$files ? : 1 0 1
ref $files ? : 0 1 1
206 } elsif(defined($url)){
207 1 $self->url($url);
208 }
209  
210 # ontology name (overrides implicit one through OntologyI engine)
211 5 ▶ $self->ontology_name($name) if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 5 5
212  
213 } # _initialize
214  
215 =head2 ontology_name
216  
217 Title : ontology_name
218 Usage : $obj->ontology_name($newval)
219 Function: Get/set the name of the ontology parsed by this module.
220 Example :
221 Returns : value of ontology_name (a scalar)
222 Args : on set, new value (a scalar or undef, optional)
223  
224  
225 =cut
226  
227 15 ▶sub ontology_name{
Subroutine ontology_name: called CC=2 SCAR=6.9
Pod documented
228 15 my $self = shift;
229  
230 15 ▶ return $self->{'ontology_name'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 12 15
231 12 return $self->{'ontology_name'};
232 }
233  
234  
235 =head2 parse
236  
237 Title : parse()
238 Usage : $parser->parse();
239 Function: Parses the files set with "new" or with methods
240 defs_file and _flat_files.
241  
242 Normally you should not need to call this method as it will
243 be called automatically upon the first call to
244 next_ontology().
245  
246 Returns : [Bio::Ontology::OntologyEngineI]
247 Args :
248  
249 =cut
250  
251 5 ▶sub parse {
Subroutine parse: called CC=11 SCAR=26.5
Pod documented
252 5 my $self = shift;
253  
254 #warn "PARSING";
255 # setup the default term factory if not done by anyone yet
256 5 ▶ $self->term_factory(Bio::Ontology::TermFactory->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->term_factory 0 5 5
257 -type => "Bio::Ontology::Term"))
258 unless $self->term_factory();
259  
260 # create the ontology object itself
261 5 my $ont = Bio::Ontology::Ontology->new(-name => $self->ontology_name(),
262 -engine => $self->_ont_engine());
263  
264 # parse definitions
265 5 ▶ while( my $term = $self->_next_term() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $term = $self->_next_term) 9 5 14
266 9 $self->_add_term( $term, $ont );
267 }
268  
269 # set up the ontology of the relationship types
270 5 foreach ($self->_part_of_relationship(), $self->_is_a_relationship(), $self->_related_to_relationship()) {
271 15 $_->ontology($ont);
272 }
273  
274 # pre-seed the IO system with the first flat file if -file wasn't provided
275 5 ▶ if(! $self->_fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_fh) 2 3 5
276 2 ▶ if($self->url){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->url) { } 1 1 2
elsif ($self->_flat_files) { } 1 0 1
277 1 ▶ if(ref($self->url) eq 'ARRAY'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->url eq 'ARRAY') { } 0 1 1
278 #warn "BA";
279 0 foreach my $url (@{ $self->url }){
280 #warn $url;
281 #warn $ont;
282 #warn scalar($ont->get_all_terms());
283 0 $self->_initialize_io(-url => $url);
284 0 $self->_parse_flat_file($ont);
285 }
286 0 $self->close();
287 } else {
288 1 $self->_initialize_io(-url => $self->url);
289 }
290 } elsif($self->_flat_files){
291 1 $self->_initialize_io(-file => shift(@{$self->_flat_files()}));
292 }
293 }
294  
295 5 ▶ while($self->_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_fh) 5 0 5
296 5 $self->_parse_flat_file($ont);
297 # advance to next flat file if more are available
298 4 ▶ if(@{$self->_flat_files()}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_flat_files;}) { } 0 4 4
299 0 $self->close();
300 0 $self->_initialize_io(-file => shift(@{$self->_flat_files()}));
301 } else {
302 4 last; # nothing else to parse so terminate the loop
303 }
304 }
305 4 $self->_add_ontology($ont);
306  
307 # not needed anywhere, only because of backward compatibility
308 4 return $self->_ont_engine();
309 } # parse
310  
311 =head2 next_ontology
312  
313 Title : next_ontology
314 Usage :
315 Function: Get the next available ontology from the parser. This is the
316 method prescribed by Bio::OntologyIO.
317 Example :
318 Returns : An object implementing Bio::Ontology::OntologyI, and undef if
319 there is no more ontology in the input.
320 Args :
321  
322  
323 =cut
324  
325 6 ▶sub next_ontology {
Subroutine next_ontology: called CC=4 SCAR=13.9
Pod documented
326 6 my $self = shift;
327  
328 # parse if not done already
329 6 ▶ $self->parse() unless exists($self->{'_ontologies'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_ontologies"} 5 1 6
330 # return next available ontology
331 5 ▶ if(exists($self->{'_ontologies'})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_ontologies"}) 5 0 5
332 5 my $ont = shift (@{$self->{'_ontologies'}});
333 5 ▶ if($ont){
Branch: 1 branch
BranchTrueFalseTotal
if ($ont) 4 1 5
334 4 my $store = Bio::Ontology::OntologyStore->new();
335 4 $store->register_ontology($ont);
336 4 return $ont;
337 }
338 }
339 1 return;
340 }
341  
342 =head2 defs_file
343  
344 Title : defs_file
345 Usage : $parser->defs_file( "GO.defs" );
346 Function: Set/get for the term definitions filename.
347 Returns : The term definitions file name [string].
348 Args : On set, the term definitions file name [string] (optional).
349  
350 =cut
351  
352 2 ▶sub defs_file {
Subroutine defs_file: called CC=4 SCAR=14.2
Pod documented
353 2 my $self = shift;
354  
355 2 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 0 2
356 2 my $f = shift;
357 2 $self->{ "_defs_file_name" } = $f;
358 2 ▶ $self->_defs_io->close() if $self->_defs_io();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_defs_io 0 2 2
359 2 ▶ if(defined($f)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $f) 2 0 2
360 2 $self->_defs_io( Bio::Root::IO->new( -input => $f ) );
361 }
362 }
363 2 return $self->{ "_defs_file_name" };
364 } # defs_file
365  
366 0 ▶sub defs_url {
Subroutine defs_url: not called CC=3 SCAR=24.8
Pod undocumented
367 0 my $self = shift;
368 0 my $val = shift;
369 0 ▶ if(defined($val)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 0 0
370 0 $self->{'_defs_url'} = $val;
371  
372 0 ▶ $self->_defs_io->close() if $self->_defs_io();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_defs_io 0 0 0
373 0 $self->_defs_io( Bio::Root::IO->new( -url => $val ) );
374 }
375 0 return $self->{'_defs_url'};
376 }
377  
378 5 ▶sub url {
Subroutine url: called CC=2 SCAR=6.9
Pod undocumented
379 5 my $self = shift;
380 5 my $val = shift;
381 5 ▶ if(defined($val)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 1 4 5
382 1 $self->{'_url'} = $val;
383 }
384 5 return $self->{'_url'};
385 }
386  
387 =head2 close
388  
389 Title : close
390 Usage :
391 Function: Closes this ontology stream and associated file handles.
392  
393 Clients should call this method especially when they write
394 ontologies.
395  
396 We need to override this here in order to close the file
397 handle for the term definitions file.
398  
399 Example :
400 Returns : none
401 Args : none
402  
403  
404 =cut
405  
406 5 ▶sub close{
Subroutine close: called CC=2 SCAR=6.9
Pod documented
407 5 my $self = shift;
408  
409 # first call the inherited implementation
410 5 $self->SUPER::close();
411 # then close the defs file io (if there is one)
412 5 ▶ $self->_defs_io->close() if $self->_defs_io();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_defs_io 2 3 5
413 }
414  
415 =head2 _flat_files
416  
417 Title : _flat_files
418 Usage : $files_to_parse = $parser->_flat_files();
419 Function: Get the array of ontology flat files that need to be parsed.
420  
421 Note that this array will decrease in elements over the
422 parsing process. Therefore, it\'s value outside of this
423 module will be limited. Also, be careful not to alter the
424 array unless you know what you are doing.
425  
426 Returns : a reference to an array of zero or more strings
427 Args : none
428  
429 =cut
430  
431 6 ▶sub _flat_files {
Subroutine _flat_files: called CC=2 SCAR=6.9
432 6 my $self = shift;
433  
434 6 ▶ $self->{_flat_files} = [] unless exists($self->{_flat_files});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_flat_files"} 3 3 6
435 6 return $self->{_flat_files};
436 }
437  
438  
439 # INTERNAL METHODS
440 # ----------------
441  
442 =head2 _defs_io
443  
444 Title : _defs_io
445 Usage : $obj->_defs_io($newval)
446 Function: Get/set the Bio::Root::IO instance representing the
447 definition file, if provided (see defs_file()).
448 Example :
449 Returns : value of _defs_io (a Bio::Root::IO object)
450 Args : on set, new value (a Bio::Root::IO object or undef, optional)
451  
452 =cut
453  
454 87 ▶sub _defs_io{
Subroutine _defs_io: called CC=2 SCAR=6.9
455 87 my $self = shift;
456  
457 87 ▶ return $self->{'_defs_io'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 85 87
458 85 return $self->{'_defs_io'};
459 }
460  
461 4 ▶sub _add_ontology {
Subroutine _add_ontology: called CC=6 SCAR=19.6
462 4 my $self = shift;
463 4 ▶ $self->{'_ontologies'} = [] unless exists($self->{'_ontologies'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_ontologies"} 4 0 4
464 4 foreach my $ont (@_) {
465 4 ▶ $self->throw(ref($ont)." does not implement Bio::Ontology::OntologyI")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ont and $ont->isa("Bio::Ontology::OntologyI") 0 4 4
Condition33%1/3
ref $ont and $ont->isa("Bio::Ontology::OntologyI")
!ll&&!rl&&r
004
MC/DC0%0/2
ref $ont and $ont->isa("Bio::Ontology::OntologyI")
ref $ont $ont->isa("Bio::Ontology::OntologyI")
Truth table33%1/3
A and B
A= ref $ontB= $ont->isa("Bio::Ontology::OntologyI")
ABresult
0X0
100
111
466 unless ref($ont) && $ont->isa("Bio::Ontology::OntologyI");
467 # the ontology name may have been auto-discovered while parsing
468 # the file
469 4 ▶ $ont->name($self->ontology_name) unless $ont->name();
Branch: 1 branch
BranchTrueFalseTotal
unless $ont->name 4 0 4
470 4 push(@{$self->{'_ontologies'}}, $ont);
471 }
472 }
473  
474 # This simply delegates. See SimpleGOEngine.
475 227 ▶sub _add_term {
Subroutine _add_term: called CC=3 SCAR=12.5
476 227 my ( $self, $term, $ont ) = @_;
477 227 ▶ $term->ontology($ont) if $ont && (! $term->ontology);
Branch: 1 branch
BranchTrueFalseTotal
if $ont and not $term->ontology 227 0 227
Condition33%1/3
$ont and not $term->ontology
!ll&&!rl&&r
00227
MC/DC0%0/2
$ont and not $term->ontology
$ont not $term->ontology
Truth table33%1/3
A and not(B)
A= $ontB= not $term->ontology
ABresult
0X0
100
111
478 227 $self->_ont_engine()->add_term( $term );
479 } # _add_term
480  
481  
482  
483 # This simply delegates. See SimpleGOEngine
484 52 ▶sub _part_of_relationship {
Subroutine _part_of_relationship: called CC=1 SCAR=0.0
485 52 my $self = shift;
486  
487 52 return $self->_ont_engine()->part_of_relationship(@_);
488 } # _part_of_relationship
489  
490  
491  
492 # This simply delegates. See SimpleGOEngine
493 212 ▶sub _is_a_relationship {
Subroutine _is_a_relationship: called CC=1 SCAR=0.0
494 212 my $self = shift;
495  
496 212 return $self->_ont_engine()->is_a_relationship(@_);
497 } # _is_a_relationship
498  
499 # This simply delegates. See SimpleGOEngine
500 5 ▶sub _related_to_relationship {
Subroutine _related_to_relationship: called CC=1 SCAR=0.0
501 5 my $self = shift;
502  
503 5 return $self->_ont_engine()->related_to_relationship(@_);
504 } # _is_a_relationship
505  
506  
507  
508 # This simply delegates. See SimpleGOEngine
509 254 ▶sub _add_relationship {
Subroutine _add_relationship: called CC=1 SCAR=0.0
510 254 my ( $self, $parent, $child, $type, $ont ) = @_;
511  
512 # note the triple terminology (subject,predicate,object) corresponds to
513 # (child,type,parent)
514 254 $self->_ont_engine()->add_relationship( $child, $type, $parent, $ont );
515  
516  
517 } # _add_relationship
518  
519  
520 # This simply delegates. See SimpleGOEngine
521 258 ▶sub _has_term {
Subroutine _has_term: called CC=1 SCAR=0.0
522 258 my $self = shift;
523  
524 258 return $self->_ont_engine()->has_term( @_ );
525 } # _add_term
526  
527  
528  
529 # This parses the relationships files
530 5 ▶sub _parse_flat_file {
Subroutine _parse_flat_file: called CC=25 SCAR=33.0
531 5 my $self = shift;
532 5 my $ont = shift;
533  
534 5 my @stack = ();
535 5 my $prev_spaces = -1;
536 5 my $prev_term = "";
537  
538 5 ▶ while ( my $line = $self->_readline() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->_readline) 249 4 253
539  
540 249 ▶ if ( $line =~ /^!/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^!/) 26 223 249
541 26 next;
542 }
543  
544 # split into term specifications
545 223 my @termspecs = split(/ (?=[%<])/, $line);
546 # the first element is whitespace only
547 223 ▶ shift(@termspecs) if $termspecs[0] =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $termspecs[0] =~ /^\s*$/ 218 5 223
548  
549 # parse out the focus term
550 223 my $current_term = $self->_get_first_termid( $termspecs[0] );
551 222 my @syns = $self->_get_synonyms( $termspecs[0] );
552 222 my @sec_go_ids = $self->_get_secondary_termids( $termspecs[0] );
553 222 my @cross = $self->_get_db_cross_refs( $termspecs[0] );
554 222 my @cross_refs;
555 222 foreach my $cross_ref (@cross) {
556 81 ▶ $cross_ref eq $current_term && next;
Branch: 1 branch
BranchTrueFalseTotal
$cross_ref eq $current_term and next 77 4 81
557 4 push(@cross_refs, $cross_ref);
558 }
559
560 # parse out the parents of the focus term
561 222 shift(@termspecs);
562 222 my @isa_parents = ();
563 222 my @partof_parents = ();
564 222 foreach my $parent (@termspecs) {
565 36 ▶ if (index($parent, "%") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($parent, '%') == 0) { } 25 11 36
elsif (index($parent, '<') == 0) { } 11 0 11
566 25 push(@isa_parents, $self->_get_first_termid($parent));
567 } elsif (index($parent, "<") == 0) {
568 11 push(@partof_parents, $self->_get_first_termid($parent));
569 } else {
570 0 $self->warn("unhandled relationship type in '".$parent."'");
571 }
572 }
573  
574 222 ▶ if ( ! $self->_has_term( $current_term ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_term($current_term)) 204 18 222
575 204 my $term =$self->_create_ont_entry($self->_get_name($line,
576 $current_term),
577 $current_term );
578 204 $self->_add_term( $term, $ont );
579 }
580  
581 222 my $current_term_object = $self->_ont_engine()->get_terms( $current_term );
582 222 my $anno = $self->_to_annotation(\@cross_refs);
583 222 $current_term_object->add_dbxref(-dbxrefs => $anno);
584 222 $current_term_object->add_secondary_id( @sec_go_ids );
585 222 $current_term_object->add_synonym( @syns );
586 222 ▶ unless ( $line =~ /^\$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\$/) 218 4 222
587 218 $current_term_object->ontology( $ont );
588 }
589 222 foreach my $parent ( @isa_parents ) {
590 25 ▶ if ( ! $self->_has_term( $parent ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_term($parent)) 11 14 25
591 11 my $term = $self->_create_ont_entry($self->_get_name($line,
592 $parent),
593 $parent );
594 11 $self->_add_term( $term, $ont );
595 }
596  
597 25 $self->_add_relationship( $parent,
598 $current_term,
599 $self->_is_a_relationship(),
600 $ont);
601  
602 }
603 222 foreach my $parent ( @partof_parents ) {
604 11 ▶ if ( ! $self->_has_term( $parent ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_term($parent)) 3 8 11
605 3 my $term = $self->_create_ont_entry($self->_get_name($line,
606 $parent),
607 $parent );
608 3 $self->_add_term( $term, $ont );
609 }
610  
611 11 $self->_add_relationship( $parent,
612 $current_term,
613 $self->_part_of_relationship(),
614 $ont);
615 }
616  
617 222 my $current_spaces = $self->_count_spaces( $line );
618  
619 222 ▶ if ( $current_spaces != $prev_spaces ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_spaces != $prev_spaces) 116 106 222
620  
621 116 ▶ if ( $current_spaces == $prev_spaces + 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($current_spaces == $prev_spaces + 1) { } 75 41 116
elsif ($current_spaces < $prev_spaces) { } 41 0 41
622 75 push( @stack, $prev_term );
623 } elsif ( $current_spaces < $prev_spaces ) {
624 41 my $n = $prev_spaces - $current_spaces;
625 41 ▶ for ( my $i = 0; $i < $n; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 60 41 101
626 60 pop( @stack );
627 }
628 } else {
629 0 $self->throw( "format error (file ".$self->file.")" );
630 }
631 }
632  
633 222 my $parent = $stack[ @stack - 1 ];
634  
635 # add a relationship if the line isn\'t the one with the root term
636 # of the ontology (which is also the name of the ontology)
637 222 ▶ if ( index($line,'$') != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($line, "\$") != 0) 218 4 222
638 #adding @reltype@ syntax
639 218 ▶ if ( $line !~ /^\s*([<%~]|\@\w+?\@)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\s*([<%~]|\@\w+?\@)/) 0 218 218
640 0 $self->throw( "format error (file ".$self->file.") offending line:\n$line" );
641 }
642  
643 218 my($relstring) = $line =~ /^\s*([<%~]|\@[^\@]+?\@)/;
644  
645 218 my $reltype;
646  
647 218 ▶ if ($relstring eq '<') {
Branch: 3 branches
BranchTrueFalseTotal
if ($relstring eq '<') { } 36 182 218
elsif ($relstring eq '%') { } 182 0 182
elsif ($relstring eq '~') { } 0 0 0
648 36 $reltype = $self->_part_of_relationship;
649 } elsif ($relstring eq '%') {
650 182 $reltype = $self->_is_a_relationship;
651 } elsif ($relstring eq '~') {
652 0 $reltype = $self->_related_to_relationship;
653 } else {
654 0 $relstring =~ s/\@//g;
655 0 ▶ if ($self->_ont_engine->get_relationship_type($relstring)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_ont_engine->get_relationship_type($relstring)) { } 0 0 0
656 0 $reltype = $self->_ont_engine->get_relationship_type($relstring);
657 } else {
658 0 $self->_ont_engine->add_relationship_type($relstring, $ont);
659 0 $reltype = $self->_ont_engine->get_relationship_type($relstring);
660 }
661 }
662  
663 #my $reltype = ($line =~ /^\s*</) ?
664 #$self->_part_of_relationship() :
665 #$self->_is_a_relationship();
666 218 $self->_add_relationship( $parent, $current_term, $reltype, $ont);
667 }
668  
669 222 $prev_spaces = $current_spaces;
670 222 $prev_term = $current_term;
671 }
672 4 return $ont;
673 } # _parse_relationships_file
674  
675  
676  
677 # Parses the 1st term id number out of line.
678 259 ▶sub _get_first_termid {
Subroutine _get_first_termid: called CC=2 SCAR=6.9
679 259 my ( $self, $line ) = @_;
680 259 ▶ if ( $line =~ /;\s*([A-Z_]{1,8}:\d{1,})/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /;\s*([A-Z_]{1,8}:\d{1,})/) { } 258 1 259
681 # if ( $line =~ /;\s*(\w+:\w+)/ ) {
682 258 return $1;
683 }
684 else {
685 1 $self->throw( "format error: no term id in line \"$line\"" );
686 }
687  
688 } # _get_first_termid
689  
690  
691  
692 # Parses the name out of line.
693 218 ▶sub _get_name {
Subroutine _get_name: called CC=4 SCAR=14.1
694 218 my ( $self, $line, $termid ) = @_;
695  
696 218 ▶ if ( $line =~ /([^;<%~]+);\s*$termid/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /([^;<%~]+);\s*$termid/) { } 218 0 218
697 218 my $name = $1;
698 # remove trailing and leading whitespace
699 218 $name =~ s/\s+$//;
700 218 $name =~ s/^\s+//;
701 218 $name =~ s/\@.+?\@//;
702 # remove leading dollar character; also we default the name of the
703 # ontology to this name unless it is preset to something else
704 218 ▶ if(index($name,'$') == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($name, "\$") == 0) 3 215 218
705 3 $name = substr($name,1);
706 # replace underscores by spaces for setting the ontology name
707 3 ▶ $self->ontology_name(join(" ",split(/_/,$name)))
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ontology_name 3 0 3
708 unless $self->ontology_name();
709 }
710 218 return $name;
711 }
712 else {
713 0 return;
714 }
715 } # _get_name
716  
717  
718 # Parses the synonyms out of line.
719 222 ▶sub _get_synonyms {
Subroutine _get_synonyms: called CC=2 SCAR=6.9
720 222 my ( $self, $line ) = @_;
721  
722 222 my @synonyms = ();
723  
724 222 ▶ while ( $line =~ /synonym\s*:\s*([^;<%~]+)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /synonym\s*:\s*([^;<%~]+)/g) 38 222 260
725 38 my $syn = $1;
726 38 $syn =~ s/\s+$//;
727 38 $syn =~ s/^\s+//;
728 38 push( @synonyms, $syn );
729 }
730 222 return @synonyms;
731  
732 } # _get_synonyms
733  
734  
735  
736 # Parses the db cross refs out of line.
737 222 ▶sub _get_db_cross_refs {
Subroutine _get_db_cross_refs: called CC=5 SCAR=16.1
738 222 my ( $self, $line ) = @_;
739  
740 222 my @refs = ();
741  
742 222 ▶ while ( $line =~ /;([^;<%~:]+:[^;<%~:]+)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /;([^;<%~:]+:[^;<%~:]+)/g) 264 222 486
743 264 my $ref = $1;
744 264 ▶ if ( $ref =~ /synonym/ || $ref =~ /[A-Z]{1,8}:\d{3,}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref =~ /synonym/ or $ref =~ /[A-Z]{1,8}:\d{3,}/) 183 81 264
Condition100%3/3
$ref =~ /synonym/ or $ref =~ /[A-Z]{1,8}:\d{3,}/
l!l&&r!l&&!r
3814581
MC/DC100%2/2
$ref =~ /synonym/ or $ref =~ /[A-Z]{1,8}:\d{3,}/
$ref =~ /synonym/ $ref =~ /[A-Z]{1,8}:\d{3,}/
Truth table100%3/3
A or B
A= $ref =~ /synonym/B= $ref =~ /[A-Z]{1,8}:\d{3,}/
ABresult
1X1
011
000
745 183 next;
746 }
747 81 $ref =~ s/\s+$//;
748 81 $ref =~ s/^\s+//;
749  
750 81 $ref = $self->unescape( $ref );
751  
752 81 ▶ push( @refs, $ref ) if defined $ref;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref 81 0 81
753 }
754 222 return @refs;
755  
756 }
757  
758  
759 # Parses the secondary go ids out of a line
760 222 ▶sub _get_secondary_termids {
Subroutine _get_secondary_termids: called CC=2 SCAR=6.9
761 222 my ( $self, $line ) = @_;
762 222 my @secs = ();
763  
764 # while ( $line =~ /,\s*([A-Z]{1,8}:\d{3,})/g ) {
765 222 ▶ while ( $line =~ /,\s*(\w+:\w+)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /,\s*(\w+:\w+)/g) 1 222 223
766 1 my $sec = $1;
767 1 push( @secs, $sec );
768 }
769 222 return @secs;
770  
771 } # _get_secondary_termids
772  
773  
774 # Counts the spaces at the beginning of a line in the relationships files
775 222 ▶sub _count_spaces {
Subroutine _count_spaces: called CC=2 SCAR=6.9
776 222 my ( $self, $line ) = @_;
777  
778 222 ▶ if ( $line =~ /^(\s+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\s+)/) { } 218 4 222
779 218 return length( $1 );
780 }
781 else {
782 4 return 0;
783 }
784 } # _count_spaces
785  
786  
787 # "next" method for parsing the defintions file
788 14 ▶sub _next_term {
Subroutine _next_term: called CC=14 SCAR=26.5
789 14 my ( $self ) = @_;
790  
791 14 ▶ if ( ($self->_done() == TRUE) || (! $self->_defs_io())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_done == 1 or not $self->_defs_io) 5 9 14
Condition100%3/3
$self->_done == 1 or not $self->_defs_io
l!l&&r!l&&!r
239
MC/DC100%2/2
$self->_done == 1 or not $self->_defs_io
$self->_done == 1 not $self->_defs_io
Truth table100%3/3
A or not(B)
A= $self->_done == 1B= not $self->_defs_io
ABresult
1X1
011
000
792 5 return;
793 }
794  
795 9 my $line = "";
796 9 my $termid = "";
797 9 my $next_term = $self->_term();
798 9 my $def = "";
799 9 my $comment = "";
800 9 my @def_refs = ();
801 9 my $isobsolete;
802  
803 9 ▶ while( $line = ( $self->_defs_io->_readline() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line = $self->_defs_io->_readline) 62 2 64
804 62 ▶ if ( $line !~ /\S/
Branch: 6 branches
BranchTrueFalseTotal
if (not $line =~ /\S/ or $line =~ /^\s*!/) { } 26 36 62
elsif ($line =~ /^\s*term:\s*(.+)/) { } 9 27 36
elsif ($line =~ /^\s*[a-z]{0,8}id:\s*(.+)/) { } 9 18 27
elsif ($line =~ /^\s*definition:\s*(.+)/) { } 9 9 18
elsif ($line =~ /^\s*definition_reference:\s*(.+)/) { } 9 0 9
elsif ($line =~ /^\s*comment:\s*(.+)/) { } 0 0 0
Condition100%3/3
not $line =~ /\S/ or $line =~ /^\s*!/
l!l&&r!l&&!r
121436
MC/DC100%2/2
not $line =~ /\S/ or $line =~ /^\s*!/
not $line =~ /\S/ $line =~ /^\s*!/
Truth table100%3/3
not(A) or B
A= not $line =~ /\S/B= $line =~ /^\s*!/
ABresult
1X1
011
000
805 || $line =~ /^\s*!/ ) {
806 26 next;
807 }
808 elsif ( $line =~ /^\s*term:\s*(.+)/ ) {
809 9 $self->_term( $1 );
810 9 ▶ last if $self->_not_first_record();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_not_first_record 7 2 9
811 2 $next_term = $1;
812 2 $self->_not_first_record( TRUE );
813 }
814 elsif ( $line =~ /^\s*[a-z]{0,8}id:\s*(.+)/ ) {
815 9 $termid = $1;
816 }
817 elsif ( $line =~ /^\s*definition:\s*(.+)/ ) {
818 9 $def = $self->unescape($1);
819 9 ▶ $isobsolete = 1 if index($def,"OBSOLETE") == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($def, "OBSOLETE") == 0 0 9 9
820 }
821 elsif ( $line =~ /^\s*definition_reference:\s*(.+)/ ) {
822 9 push( @def_refs, $self->unescape($1) );
823 }
824 elsif ( $line =~ /^\s*comment:\s*(.+)/ ) {
825 0 $comment = $self->unescape($1);
826 }
827 }
828 9 ▶ $self->_done( TRUE ) unless $line; # we'll come back until done
Branch: 1 branch
BranchTrueFalseTotal
unless $line 2 7 9
829 9 return $self->_create_ont_entry( $next_term, $termid, $def,
830 $comment, \@def_refs, $isobsolete);
831 } # _next_term
832  
833  
834  
835 # Holds the GO engine to be parsed into
836 1244 ▶sub _ont_engine {
Subroutine _ont_engine: called CC=2 SCAR=6.9
837 1244 my ( $self, $value ) = @_;
838  
839 1244 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 5 1239 1244
840 5 $self->{ "_ont_engine" } = $value;
841 }
842  
843 1244 return $self->{ "_ont_engine" };
844 } # _ont_engine
845  
846  
847 # Used to create ontology terms.
848 # Arguments: name, id
849 227 ▶sub _create_ont_entry {
Subroutine _create_ont_entry: called CC=3 SCAR=12.3
850 227 my ( $self, $name, $termid, $def, $cmt, $dbxrefs, $obsolete ) = @_;
851  
852 227 ▶ if((!defined($obsolete)) && (index(lc($name),"obsolete") == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $obsolete and index(lc $name, "obsolete") == 0) 0 227 227
Condition33%1/3
not defined $obsolete and index(lc $name, "obsolete") == 0
!ll&&!rl&&r
02270
MC/DC0%0/2
not defined $obsolete and index(lc $name, "obsolete") == 0
not defined $obsolete index(lc $name, "obsolete") == 0
Truth table33%1/3
not(A) and B
A= not defined $obsoleteB= index(lc $name, "obsolete") == 0
ABresult
0X0
100
111
853 0 $obsolete = 1;
854 }
855 227 my $anno = $self->_to_annotation($dbxrefs);
856 227 my $term = $self->term_factory->create_object(-name => $name,
857 -identifier => $termid,
858 -definition => $def,
859 -comment => $cmt,
860 -dbxrefs => $anno,
861 -is_obsolete => $obsolete);
862  
863 227 return $term;
864 } # _create_ont_entry
865  
866  
867  
868 # Holds whether first record or not
869 16 ▶sub _not_first_record {
Subroutine _not_first_record: called CC=2 SCAR=6.9
870 16 my ( $self, $value ) = @_;
871  
872 16 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 7 9 16
873 7 $self->{ "_not_first_record" } = $value;
874 }
875  
876 16 return $self->{ "_not_first_record" };
877 } # _not_first_record
878  
879  
880  
881 # Holds whether done or not
882 21 ▶sub _done {
Subroutine _done: called CC=2 SCAR=6.9
883 21 my ( $self, $value ) = @_;
884  
885 21 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 7 14 21
886 7 $self->{ "_done" } = $value;
887 }
888  
889 21 return $self->{ "_done" };
890 } # _done
891  
892  
893 # Holds a term.
894 23 ▶sub _term {
Subroutine _term: called CC=2 SCAR=6.9
895 23 my ( $self, $value ) = @_;
896  
897 23 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 14 9 23
898 14 $self->{ "_term" } = $value;
899 }
900  
901 23 return $self->{ "_term" };
902 } # _term
903  
904 # convert simple strings to Bio::Annotation::DBLinks
905 449 ▶sub _to_annotation {
Subroutine _to_annotation: called CC=3 SCAR=11.0
906 449 my ($self , $links) = @_;
907 449 ▶ return unless $links;
Branch: 1 branch
BranchTrueFalseTotal
unless $links 218 231 449
908 231 my @dbxrefs;
909 231 for my $string (@{$links}) {
910 13 my ($db, $id) = split(':',$string);
911 13 push @dbxrefs, Bio::Annotation::DBLink->new(-database => $db, -primary_id => $id);
912 }
913 231 return \@dbxrefs;
914 }
915  
916 6 1;
« Bio/TreeIO/phyloxml.pm ↑ Summary Bio/AlignIO/arp.pm »