Bio/OntologyIO/simplehierarchy.pm

filter: Statementstmt 16.0% 32 / 199 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 28.5% 10 / 35 Podpod 100% 5 / 5 totaltotal 12.4% 47 / 377 CC 60 SCAR 39.4
CC
60
Cov
10%
_parse_flat_file
182.0
_initialize
132.0
parse
42.0
CRAP 51.6
« Bio/SeqIO/gbxml.pm ↑ Summary Bio/SeqIO/fasta.pm »
1 #
2 # BioPerl module for Bio::OntologyIO::simplehierarchy
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Allen Day, allenday@ucla.edu
7 #
8 # (c) Allen Day, allenday@ucla.edu, 2003.
9 # (c) Department of Human Genetics, UCLA Medical School, 2003.
10 #
11 # You may distribute this module under the same terms as perl itself.
12 # Refer to the Perl Artistic License (see the license accompanying this
13 # software package, or see http://www.perl.com/language/misc/Artistic.html)
14 # for the terms under which you may use, modify, and redistribute this module.
15 #
16 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
17 # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
18 # MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 #
20 # You may distribute this module under the same terms as perl itself
21  
22 # POD documentation - main docs before the code
23  
24 =head1 NAME
25  
26 Bio::OntologyIO::simplehierarchy - a base class parser for simple hierarchy-by-indentation
27 type formats
28  
29 =head1 SYNOPSIS
30  
31 use Bio::OntologyIO;
32  
33 # do not use directly -- use via Bio::OntologyIO
34 my $parser = Bio::OntologyIO->new
35 ( -format => "simplehierarchy",
36 -file => "pathology_terms.csv",
37 -indent_string => ",",
38 -ontology_name => "eVOC",
39 -term_factory => $fact,
40 );
41  
42 my $ontology = $parser->next_ontology();
43  
44 =head1 DESCRIPTION
45  
46 Needs Graph.pm from CPAN. This class is nearly identical to
47 OntologyIO::dagflat, see L<Bio::OntologyIO::dagflat> for details.
48  
49 =head1 FEEDBACK
50  
51 =head2 Mailing Lists
52  
53 User feedback is an integral part of the evolution of this and other
54 Bioperl modules. Send your comments and suggestions preferably to the
55 Bioperl mailing lists Your participation is much appreciated.
56  
57 bioperl-l@bioperl.org - General discussion
58 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
59  
60 =head2 Support
61  
62 Please direct usage questions or support issues to the mailing list:
63  
64 I<bioperl-l@bioperl.org>
65  
66 rather than to the module maintainer directly. Many experienced and
67 reponsive experts will be able look at the problem and quickly
68 address it. Please include a thorough description of the problem
69 with code and data examples if at all possible.
70  
71 =head2 Reporting Bugs
72  
73 Report bugs to the Bioperl bug tracking system to help us keep track
74 the bugs and their resolution. Bug reports can be submitted via the
75 web:
76  
77 https://github.com/bioperl/bioperl-live/issues
78  
79 =head1 AUTHOR
80  
81 Allen Day
82  
83 Email: allenday@ucla.edu
84  
85 =head2 CONTRIBUTOR
86  
87 Christian Zmasek
88  
89 =head1 APPENDIX
90  
91 The rest of the documentation details each of the object
92 methods. Internal methods are usually preceded with a _
93  
94 =cut
95  
96  
97 # Let the code begin...
98  
99  
100 package Bio::OntologyIO::simplehierarchy;
101 1 $Bio::OntologyIO::simplehierarchy::VERSION = '1.7.8';
102 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 1 ▶use Bio::Root::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 1 ▶use Bio::Ontology::OBOEngine;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 1 ▶use Bio::Ontology::Ontology;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 1 ▶use Bio::Ontology::TermFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
110  
111 1 ▶use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 1 ▶use constant FALSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114  
115 1 ▶use base qw(Bio::OntologyIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117  
118 =head2 new
119  
120 Title : new
121 Usage : see SYNOPSIS
122 Function: Creates a new simplehierarchy parser.
123 Returns : A new simplehierarchy parser object, implementing Bio::OntologyIO.
124 Args : -files => a single ontology flat file holding the
125 term relationships, or an array ref holding
126 the file names
127 -file => if there is only a single flat file, it may
128 also be specified via the -file parameter
129 -ontology_name => the name of the ontology, defaults to
130 "Gene Ontology"
131 -file_is_root => Boolean indicating whether a virtual root
132 term is to be added, the name of which will
133 be derived from the file name. Default is false.
134 Enabling this allows one to parse multiple input
135 files into the same ontology and still have
136 separately rooted.
137 -engine => the L<Bio::Ontology::OntologyEngineI> object
138 to be reused (will be created otherwise); note
139 that every L<Bio::Ontology::OntologyI> will
140 qualify as well since that one inherits from the
141 former.
142 -indent_string => the string used to indent hierarchical
143 levels in the file.
144  
145 For a file like this:
146  
147 term0
148 subterm1A
149 subterm2A
150 subterm1B
151 subterm1C
152  
153 indent_string would be " ". Defaults to
154 one space (" ").
155 -comment_char => Allows specification of a regular
156 expression string to indicate a comment line.
157 Currently defaults to "[\|\-]".
158 Note: this is not yet implemented.
159  
160 See L<Bio::OntologyIO>.
161  
162 =cut
163  
164 # in reality, we let OntologyIO::new do the instantiation, and override
165 # _initialize for all initialization work
166 0 ▶sub _initialize {
Subroutine _initialize: not called CC=11 SCAR=48.8
167 0 my ($self, @args) = @_;
168 0 $self->SUPER::_initialize( @args );
169  
170 0 my ( $indent,$files,$fileisroot,$name,$eng ) =
171 $self->_rearrange([qw(INDENT_STRING
172 FILES
173 FILE_IS_ROOT
174 ONTOLOGY_NAME
175 ENGINE)
176 ], @args);
177  
178 0 $self->_done( FALSE );
179 0 $self->_not_first_record( FALSE );
180 0 $self->_term( "" );
181 0 ▶ $self->file_is_root($fileisroot) if defined($fileisroot);
Branch: 1 branch
BranchTrueFalseTotal
if defined $fileisroot 0 0 0
182 0 ▶ $indent = ' ' unless defined($indent); #reasonable default?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indent 0 0 0
183 # the indentation string may have escaped chars
184 0 ▶ if (($indent =~ /\\/) && ($indent !~ /[\$\`]/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indent =~ /\\/ and not $indent =~ /[\$\`]/) 0 0 0
Condition0%0/3
$indent =~ /\\/ and not $indent =~ /[\$\`]/
!ll&&!rl&&r
000
MC/DC0%0/2
$indent =~ /\\/ and not $indent =~ /[\$\`]/
$indent =~ /\\/ not $indent =~ /[\$\`]/
Truth table0%0/3
A and not(B)
A= $indent =~ /\\/B= not $indent =~ /[\$\`]/
ABresult
0X0
100
111
185 0 $indent = "\$indent = \"$indent\"";
186 0 eval $indent;
187 }
188 0 $self->indent_string($indent);
189 0 delete $self->{'_ontologies'};
190  
191 # ontology engine (and possibly name if it's an OntologyI)
192 0 ▶ $eng = Bio::Ontology::OBOEngine->new() unless $eng;
Branch: 1 branch
BranchTrueFalseTotal
unless $eng 0 0 0
193 0 ▶ if($eng->isa("Bio::Ontology::OntologyI")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eng->isa("Bio::Ontology::OntologyI")) 0 0 0
194 0 $self->ontology_name($eng->name());
195 0 ▶ $eng = $eng->engine() if $eng->can('engine');
Branch: 1 branch
BranchTrueFalseTotal
if $eng->can("engine") 0 0 0
196 }
197 0 $self->_ont_engine($eng);
198  
199 # flat files to parse
200 0 ▶ $self->{_flat_files} = $files ? ref($files) ? $files : [$files] : [];
Branch: 2 branches
BranchTrueFalseTotal
$files ? : 0 0 0
ref $files ? : 0 0 0
201  
202 # ontology name (overrides implicit one through OntologyI engine)
203 0 ▶ $self->ontology_name($name) if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
204  
205 } # _initialize
206  
207 =head2 ontology_name
208  
209 Title : ontology_name
210 Usage : $obj->ontology_name($newval)
211 Function: Get/set the name of the ontology parsed by this module.
212 Example :
213 Returns : value of ontology_name (a scalar)
214 Args : on set, new value (a scalar or undef, optional)
215  
216 =cut
217  
218 0 ▶sub ontology_name{
Subroutine ontology_name: not called CC=2 SCAR=17.9
Pod documented
219 0 my $self = shift;
220  
221 0 ▶ return $self->{'ontology_name'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
222 0 return $self->{'ontology_name'};
223 }
224  
225  
226 =head2 parse
227  
228 Title : parse()
229 Usage : $parser->parse();
230 Function: Parses the files set with "new" or with methods
231 defs_file and _flat_files.
232  
233 Normally you should not need to call this method as it will
234 be called automatically upon the first call to
235 next_ontology().
236  
237 Returns : [Bio::Ontology::OntologyEngineI]
238 Args :
239  
240 =cut
241  
242 0 ▶sub parse {
Subroutine parse: not called CC=6 SCAR=37.4
Pod documented
243 0 my $self = shift;
244  
245 # setup the default term factory if not done by anyone yet
246 0 ▶ $self->term_factory(Bio::Ontology::TermFactory->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->term_factory 0 0 0
247 -type => "Bio::Ontology::Term"))
248 unless $self->term_factory();
249  
250 # create the ontology object itself
251 0 my $ont = Bio::Ontology::Ontology->new(-name => $self->ontology_name(),
252 -engine => $self->_ont_engine());
253  
254 # set up the ontology of the relationship types
255 0 foreach ($self->_part_of_relationship(),
256 $self->_is_a_relationship(),
257 $self->_related_to_relationship()) {
258 0 $_->ontology($ont);
259 }
260  
261 # pre-seed the IO system with the first flat file if -file wasn't provided
262 0 ▶ if(! $self->_fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_fh) 0 0 0
263 0 $self->_initialize_io(-file => shift(@{$self->_flat_files()}));
264 }
265  
266 0 ▶ while($self->_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_fh) 0 0 0
267 0 $self->_parse_flat_file($ont);
268 # advance to next flat file if more are available
269 0 ▶ if(@{$self->_flat_files()}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_flat_files;}) { } 0 0 0
270 0 $self->close();
271 # reset the virtual root so that the next one is generated from
272 # the next file
273 0 $self->_virtual_root(undef);
274 # now re-initialize the IO object
275 0 $self->_initialize_io(-file => shift(@{$self->_flat_files()}));
276 } else {
277 0 last; # nothing else to parse so terminate the loop
278 }
279 }
280 0 $self->_add_ontology($ont);
281 # not needed anywhere, only because of backward compatibility
282 0 return $self->_ont_engine();
283 } # parse
284  
285 =head2 next_ontology
286  
287 Title : next_ontology
288 Usage :
289 Function: Get the next available ontology from the parser. This is the
290 method prescribed by Bio::OntologyIO.
291 Example :
292 Returns : An object implementing Bio::Ontology::OntologyI, and undef if
293 there is no more ontology in the input.
294 Args :
295  
296 =cut
297  
298 0 ▶sub next_ontology{
Subroutine next_ontology: not called CC=3 SCAR=24.8
Pod documented
299 0 my $self = shift;
300  
301 # parse if not done already
302 0 ▶ $self->parse() unless exists($self->{'_ontologies'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_ontologies"} 0 0 0
303 # return next available ontology
304 0 ▶ return shift(@{$self->{'_ontologies'}}) if exists($self->{'_ontologies'});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_ontologies"} 0 0 0
305 0 return;
306 }
307  
308 =head2 _flat_files
309  
310 Title : _flat_files
311 Usage : $files_to_parse = $parser->_flat_files();
312 Function: Get the array of ontology flat files that need to be parsed.
313  
314 Note that this array will decrease in elements over the
315 parsing process. Therefore, it\'s value outside of this
316 module will be limited. Also, be careful not to alter the
317 array unless you know what you are doing.
318  
319 Returns : a reference to an array of zero or more strings
320 Args : none
321  
322 =cut
323  
324 0 ▶sub _flat_files {
Subroutine _flat_files: not called CC=2 SCAR=17.9
325 0 my $self = shift;
326  
327 0 ▶ $self->{_flat_files} = [] unless exists($self->{_flat_files});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_flat_files"} 0 0 0
328 0 return $self->{_flat_files};
329 }
330  
331  
332 # INTERNAL METHODS
333 # ----------------
334  
335 =head2 _defs_io
336  
337 Title : _defs_io
338 Usage : $obj->_defs_io($newval)
339 Function: Get/set the Bio::Root::IO instance representing the
340 definition file, if provided (see defs_file()).
341 Example :
342 Returns : value of _defs_io (a Bio::Root::IO object)
343 Args : on set, new value (a Bio::Root::IO object or undef, optional)
344  
345 =cut
346  
347 0 ▶sub _defs_io{
Subroutine _defs_io: not called CC=2 SCAR=17.9
348 0 my $self = shift;
349  
350 0 ▶ return $self->{'_defs_io'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
351 0 return $self->{'_defs_io'};
352 }
353  
354 0 ▶sub _add_ontology {
Subroutine _add_ontology: not called CC=5 SCAR=34.0
355 0 my $self = shift;
356 0 ▶ $self->{'_ontologies'} = [] unless exists($self->{'_ontologies'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_ontologies"} 0 0 0
357 0 foreach my $ont (@_) {
358 0 ▶ $self->throw(ref($ont)." does not implement Bio::Ontology::OntologyI")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ont and $ont->isa("Bio::Ontology::OntologyI") 0 0 0
Condition0%0/3
ref $ont and $ont->isa("Bio::Ontology::OntologyI")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $ont and $ont->isa("Bio::Ontology::OntologyI")
ref $ont $ont->isa("Bio::Ontology::OntologyI")
Truth table0%0/3
A and B
A= ref $ontB= $ont->isa("Bio::Ontology::OntologyI")
ABresult
0X0
100
111
359 unless ref($ont) && $ont->isa("Bio::Ontology::OntologyI");
360 0 push(@{$self->{'_ontologies'}}, $ont);
361 }
362 }
363  
364 # This simply delegates. See SimpleGOEngine.
365 0 ▶sub _add_term {
Subroutine _add_term: not called CC=3 SCAR=24.8
366 0 my ( $self, $term, $ont ) = @_;
367  
368 0 ▶ $term->ontology($ont) if $ont && (! $term->ontology);
Branch: 1 branch
BranchTrueFalseTotal
if $ont and not $term->ontology 0 0 0
Condition0%0/3
$ont and not $term->ontology
!ll&&!rl&&r
000
MC/DC0%0/2
$ont and not $term->ontology
$ont not $term->ontology
Truth table0%0/3
A and not(B)
A= $ontB= not $term->ontology
ABresult
0X0
100
111
369 0 $self->_ont_engine()->add_term( $term );
370  
371  
372 } # _add_term
373  
374 # This simply delegates. See SimpleGOEngine
375 0 ▶sub _part_of_relationship {
Subroutine _part_of_relationship: not called CC=1 SCAR=6.9
376 0 my ( $self, $term ) = @_;
377  
378 0 return $self->_ont_engine()->part_of_relationship();
379  
380 } # _part_of_relationship
381  
382  
383  
384 # This simply delegates. See SimpleGOEngine
385 0 ▶sub _is_a_relationship {
Subroutine _is_a_relationship: not called CC=1 SCAR=6.9
386 0 my ( $self, $term ) = @_;
387  
388 0 return $self->_ont_engine()->is_a_relationship();
389  
390 } # _is_a_relationship
391  
392  
393 # This simply delegates. See SimpleGOEngine
394 0 ▶sub _related_to_relationship {
Subroutine _related_to_relationship: not called CC=1 SCAR=6.9
395 0 my ( $self, $term ) = @_;
396  
397 0 return $self->_ont_engine()->related_to_relationship();
398  
399 } # _is_a_relationship
400  
401  
402 # This simply delegates. See SimpleGOEngine
403 0 ▶sub _add_relationship {
Subroutine _add_relationship: not called CC=1 SCAR=6.9
404 0 my ( $self, $parent, $child, $type, $ont ) = @_;
405  
406 # note the triple terminology (subject,predicate,object) corresponds to
407 # (child,type,parent)
408 0 $self->_ont_engine()->add_relationship( $child, $type, $parent, $ont );
409  
410  
411 } # _add_relationship
412  
413  
414 # This simply delegates. See SimpleGOEngine
415 0 ▶sub _has_term {
Subroutine _has_term: not called CC=3 SCAR=24.8
416 0 my ( $self, $term ) = @_;
417  
418 0 ▶ $term = $self->ontology_name() .'|'. $term
Branch: 1 branch
BranchTrueFalseTotal
unless ref $term or not $self->ontology_name 0 0 0
Condition0%0/3
ref $term or not $self->ontology_name
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $term or not $self->ontology_name
ref $term not $self->ontology_name
Truth table0%0/3
A or not(B)
A= ref $termB= not $self->ontology_name
ABresult
1X1
011
000
419 unless ref($term) || !$self->ontology_name();
420 0 return $self->_ont_engine()->has_term( $term );
421  
422 } # _add_term
423  
424 # This simply delegates after prefixing the namespace name if it is just a
425 # base identifier. See SimpleGOEngine
426 0 ▶sub _get_terms{
Subroutine _get_terms: not called CC=4 SCAR=30.0
427 0 my $self = shift;
428 0 my @args = ();
429  
430 0 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
431 0 unshift(@args, pop(@_)); # this actually does preserve the order
432 0 ▶ $args[0] = $self->ontology_name() .'|'. $args[0]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args[0] or not $self->ontology_name 0 0 0
Condition0%0/3
ref $args[0] or not $self->ontology_name
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $args[0] or not $self->ontology_name
ref $args[0] not $self->ontology_name
Truth table0%0/3
A or not(B)
A= ref $args[0]B= not $self->ontology_name
ABresult
1X1
011
000
433 unless ref($args[0]) || !$self->ontology_name();
434 }
435 0 return $self->_ont_engine->get_terms(@args);
436 }
437  
438  
439 # This parses the relationships files
440 0 ▶sub _parse_flat_file {
Subroutine _parse_flat_file: not called CC=13 SCAR=52.0
441 0 my $self = shift;
442 0 my $ont = shift;
443  
444 0 my @stack = ();
445 0 my $prev_indent = -1;
446 0 my $parent = "";
447 0 my $prev_term = "";
448  
449 0 my $indent_string = $self->indent_string;
450  
451  
452 0 ▶ while ( my $line = $self->_readline() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->_readline) 0 0 0
453 0 ▶ if ( $line =~ /^[$indent_string]*[\|\-]/ ) { #this is not yet generalized
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^[$indent_string]*[\|\-]/) 0 0 0
454 0 next;
455 }
456  
457 0 my ($current_term) = $line =~ /^[$indent_string]*(.*)/;
458 0 my $current_indent = $self->_count_indents( $line );
459 0 chomp $current_term;
460 # remove extraneous delimiter characters at the end of the name if any
461 0 $current_term =~ s/[$indent_string]+$//;
462 # remove double quotes surrounding the entry, if any
463 0 $current_term =~ s/^\"(.*)\"$/$1/;
464 # also, the name might contain a synonym
465 0 ▶ my $syn = $current_term =~ s/\s+{([^}]+)}// ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$current_term =~ s/\s+{([^}]+)}// ? : 0 0 0
466  
467 0 ▶ if ( ! $self->_has_term( $current_term ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_term($current_term)) 0 0 0
468 0 my $term = $self->_create_ont_entry($current_term);
469 # add synonym(s) if any
470 0 ▶ $term->add_synonym(split(/[;,]\s*/,$syn)) if $syn;
Branch: 1 branch
BranchTrueFalseTotal
if $syn 0 0 0
471 # add to the machine
472 0 $self->_add_term( $term, $ont );
473  
474 #go on to the next term if a root node.
475 0 ▶ if($current_indent == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_indent == 0) 0 0 0
476 # add the virtual root as parent if there is one
477 0 ▶ if($self->_virtual_root()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_virtual_root) 0 0 0
478 0 $self->_add_relationship($self->_virtual_root(),
479 $term,
480 $self->_is_a_relationship(),
481 $ont);
482 }
483 0 $prev_indent = $current_indent;
484 0 $prev_term = $current_term;
485 0 push @stack, $current_term;
486 0 next;
487 }
488 }
489  
490 # note: we are ensured to see the parent first in this type of file,
491 # so we never need to possibly insert the parent here
492  
493 0 ▶ if ( $current_indent != $prev_indent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_indent != $prev_indent) 0 0 0
494 0 ▶ if ( $current_indent == $prev_indent + 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($current_indent == $prev_indent + 1) { } 0 0 0
elsif ($current_indent < $prev_indent) { } 0 0 0
495 0 push( @stack, $prev_term );
496 } elsif ( $current_indent < $prev_indent ) {
497 0 my $n = $prev_indent - $current_indent;
498 0 ▶ for ( my $i = 0; $i < $n; ++$i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 0 0 0
499 0 pop( @stack );
500 }
501 } else {
502 0 $self->throw("format error: indentation level $current_indent "
503 ."is more than one higher than the previous "
504 ."level $prev_indent ('$current_term', "
505 ."file ".$self->file.")" );
506 }
507 }
508  
509 0 $parent = $stack[-1];
510  
511 0 ▶ if($parent ne $current_term) { #this prevents infinite recursion from a parent linking to itself
Branch: 1 branch
BranchTrueFalseTotal
if ($parent ne $current_term) 0 0 0
512 0 $self->_add_relationship($self->_get_terms($parent),
513 $self->_get_terms($current_term),
514 $self->_is_a_relationship(),
515 $ont);
516 }
517  
518 0 $prev_indent = $current_indent;
519 0 $prev_term = $current_term;
520 }
521 0 return $ont;
522 } # _parse_relationships_file
523  
524  
525  
526 # Parses the 1st term id number out of line.
527 0 ▶sub _get_first_termid {
Subroutine _get_first_termid: not called CC=2 SCAR=17.9
528 0 my ( $self, $line ) = @_;
529  
530 0 ▶ if ( $line =~ /;\s*([A-Z]{1,8}:\d{7})/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /;\s*([A-Z]{1,8}:\d{7})/) { } 0 0 0
531 0 return $1;
532 }
533 else {
534 0 $self->throw( "format error: no term id in line \"$line\"" );
535 }
536  
537 } # _get_first_termid
538  
539 # Counts the indents at the beginning of a line in the relationships files
540 0 ▶sub _count_indents {
Subroutine _count_indents: not called CC=2 SCAR=17.9
541 0 my ( $self, $line ) = @_;
542  
543 0 my $indent = $self->indent_string;
544  
545 0 ▶ if ( $line =~ /^($indent+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^($indent+)/) { } 0 0 0
546 0 return (length($1)/length($indent));
547 }
548 else {
549 0 return 0;
550 }
551 } # _count_indents
552  
553  
554 # Holds the GO engine to be parsed into
555 0 ▶sub _ont_engine {
Subroutine _ont_engine: not called CC=2 SCAR=17.9
556 0 my ( $self, $value ) = @_;
557  
558 0 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
559 0 $self->{ "_ont_engine" } = $value;
560 }
561  
562 0 return $self->{ "_ont_engine" };
563 } # _ont_engine
564  
565  
566 # Used to create ontology terms.
567 # Arguments: name, id
568 0 ▶sub _create_ont_entry {
Subroutine _create_ont_entry: not called CC=1 SCAR=6.9
569 0 my ( $self, $name, $termid ) = @_;
570  
571 0 my $term = $self->term_factory->create_object(-name => $name,
572 -identifier => $termid);
573  
574 0 return $term;
575  
576 } # _create_ont_entry
577  
578  
579 # Holds whether first record or not
580 0 ▶sub _not_first_record {
Subroutine _not_first_record: not called CC=4 SCAR=30.0
581 0 my ( $self, $value ) = @_;
582  
583 0 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
584 0 ▶ unless ( $value == FALSE || $value == TRUE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value == 0 or $value == 1) 0 0 0
Condition0%0/3
$value == 0 or $value == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$value == 0 or $value == 1
$value == 0 $value == 1
Truth table0%0/3
A or B
A= $value == 0B= $value == 1
ABresult
1X1
011
000
585 0 $self->throw( "Argument to method \"_not_first_record\" must be either ".TRUE." or ".FALSE );
586 }
587 0 $self->{ "_not_first_record" } = $value;
588 }
589  
590 0 return $self->{ "_not_first_record" };
591 } # _not_first_record
592  
593  
594  
595 # Holds whether done or not
596 0 ▶sub _done {
Subroutine _done: not called CC=4 SCAR=30.0
597 0 my ( $self, $value ) = @_;
598  
599 0 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
600 0 ▶ unless ( $value == FALSE || $value == TRUE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value == 0 or $value == 1) 0 0 0
Condition0%0/3
$value == 0 or $value == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$value == 0 or $value == 1
$value == 0 $value == 1
Truth table0%0/3
A or B
A= $value == 0B= $value == 1
ABresult
1X1
011
000
601 0 $self->throw( "Found [$value] where [" . TRUE
602 ." or " . FALSE . "] expected" );
603 }
604 0 $self->{ "_done" } = $value;
605 }
606  
607 0 return $self->{ "_done" };
608 } # _done
609  
610  
611 # Holds a term.
612 0 ▶sub _term {
Subroutine _term: not called CC=2 SCAR=17.9
613 0 my ( $self, $value ) = @_;
614  
615 0 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
616 0 $self->{ "_term" } = $value;
617 }
618  
619 0 return $self->{ "_term" };
620 } # _term
621  
622 =head2 indent_string
623  
624 Title : indent_string
625 Usage : $obj->indent_string($newval)
626 Function:
627 Example :
628 Returns : value of indent_string (a scalar)
629 Args : on set, new value (a scalar or undef, optional)
630  
631 =cut
632  
633 0 ▶sub indent_string{
Subroutine indent_string: not called CC=2 SCAR=17.9
Pod documented
634 0 my $self = shift;
635  
636 0 ▶ return $self->{'indent_string'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
637 0 return $self->{'indent_string'};
638 }
639  
640 =head2 file_is_root
641  
642 Title : file_is_root
643 Usage : $obj->file_is_root($newval)
644 Function: Boolean indicating whether a virtual root term is to be
645 added, the name of which will be derived from the file
646 name.
647  
648 Enabling this allows one to parse multiple input files into the
649 same ontology and still have separately rooted.
650  
651 Example :
652 Returns : value of file_is_root (a scalar)
653 Args : on set, new value (a scalar or undef, optional)
654  
655 =cut
656  
657 0 ▶sub file_is_root{
Subroutine file_is_root: not called CC=2 SCAR=17.9
Pod documented
658 0 my $self = shift;
659  
660 0 ▶ return $self->{'file_is_root'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
661 0 return $self->{'file_is_root'};
662 }
663  
664 =head2 _virtual_root
665  
666 Title : _virtual_root
667 Usage : $obj->_virtual_root($newval)
668 Function:
669 Example :
670 Returns : value of _virtual_root (a scalar)
671 Args : on set, new value (a scalar or undef, optional)
672  
673 =cut
674  
675 0 ▶sub _virtual_root{
Subroutine _virtual_root: not called CC=5 SCAR=34.0
676 0 my $self = shift;
677  
678 0 ▶ return $self->{'_virtual_root'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
679  
680 # don't return anything if not in file_is_root mode, or if we don't
681 # have a file to derive the root node from
682 0 ▶ return unless $self->file_is_root() && $self->file();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->file_is_root and $self->file 0 0 0
Condition0%0/3
$self->file_is_root and $self->file
!ll&&!rl&&r
000
MC/DC0%0/2
$self->file_is_root and $self->file
$self->file_is_root $self->file
Truth table0%0/3
A and B
A= $self->file_is_rootB= $self->file
ABresult
0X0
100
111
683  
684 # construct it if we haven't done this before
685 0 ▶ if(! $self->{'_virtual_root'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_virtual_root"}) 0 0 0
686 0 my ($rt,undef,undef) = fileparse($self->file(), '\..*');
687 0 $rt =~ s/_/ /g;
688 0 $rt = $self->_create_ont_entry($rt);
689 0 $self->_add_term($rt, $self->ontology_name());
690 0 $self->{'_virtual_root'} = $rt;
691 }
692  
693 0 return $self->{'_virtual_root'};
694 }
695  
696 1 1;
« Bio/SeqIO/gbxml.pm ↑ Summary Bio/SeqIO/fasta.pm »