File Coverage

blib/lib/OBO/APO/APO_ID_Term_Map.pm
Criterion Covered Total %
statement 39 50 78.0
branch 4 12 33.3
condition 1 9 11.1
subroutine 7 8 87.5
pod 1 2 50.0
total 52 81 64.2


line stmt bran cond sub pod time code
1             # $Id: APO_ID_Term_Map.pm 2013-02-20 erick.antezana $
2             #
3             # Module : APO_ID_Term_Map.pm
4             # Purpose : A (birectional) map APO_ID vs Term name.
5             # License : Copyright (c) 2006-2015 by Erick Antezana. All rights reserved.
6             # This program is free software; you can redistribute it and/or
7             # modify it under the same terms as Perl itself.
8             # Contact : Erick Antezana
9             #
10             package OBO::APO::APO_ID_Term_Map;
11              
12             =head1 NAME
13              
14             OBO::APO::APO_ID_Term_Map - A map between APO IDs and term names.
15            
16             =head1 SYNOPSIS
17              
18             use OBO::APO::APO_ID_Term_Map;
19              
20             $apo_id_set = APO_ID_Term_Map -> new;
21              
22             $apo_id_set->file("ontology.ids");
23              
24             $file = $apo_id_set -> file;
25              
26             $size = $apo_id_set -> size;
27              
28             $apo_id_set->file("APO");
29              
30             if ($apo_id_set->add("APO:C1234567")) { ... }
31              
32             $new_id = $apo_id_set->get_new_id("APO", "C");
33              
34             =head1 DESCRIPTION
35              
36             The OBO::APO::APO_ID_Term_Map class implements a map for storing APO IDs and their corresponding names.
37              
38             =head1 AUTHOR
39              
40             Erick Antezana, Eerick.antezana -@- gmail.comE
41              
42             =head1 COPYRIGHT AND LICENSE
43              
44             Copyright (c) 2006-2015 by Erick Antezana. All rights reserved.
45              
46             This library is free software; you can redistribute it and/or modify
47             it under the same terms as Perl itself, either Perl version 5.8.7 or,
48             at your option, any later version of Perl 5 you may have available.
49              
50             =cut
51              
52             our @ISA = qw(OBO::XO::OBO_ID_Term_Map);
53 1     1   24048 use OBO::XO::OBO_ID_Term_Map;
  1         4  
  1         30  
54 1     1   7 use Carp;
  1         1  
  1         54  
55 1     1   5 use strict;
  1         2  
  1         23  
56              
57 1     1   5 use open qw(:std :utf8); # Make All I/O Default to UTF-8
  1         2  
  1         6  
58              
59 1     1   646 use OBO::APO::APO_ID_Set;
  1         2  
  1         532  
60              
61             sub new {
62 1     1 0 15 my $class = shift;
63 1         2 my $self = {};
64 1         4 $self->{FILE} = shift;
65              
66 1         3 %{ $self->{MAP_BY_ID} } = (); # key=apo_id; value=term name
  1         4  
67 1         2 %{ $self->{MAP_BY_TERM} } = (); # key=term name; value=apo_id
  1         3  
68 1         21 $self->{KEYS} = OBO::APO::APO_ID_Set->new();
69              
70 1         2 bless( $self, $class );
71              
72 1 50       12 confess if ( !defined $self->{FILE} );
73              
74             # if the file exists:
75 1 50 33     46 if ( -e $self->{FILE} && -r $self->{FILE} ) {
76 1         35 open( APO_ID_MAP_IN_FH, "<$self->{FILE}" );
77 1         34 while () {
78 8         17 chomp;
79 8 50       52 if ( $_ =~ /(APO:[A-Z][a-z]?[0-9]{7})\s+(.*)/ ) { ### vlmir
80 8         27 my ( $key, $value ) = ( $1, $2 ); # e.g.: APO:I1234567 test
81 8         31 $self->{MAP_BY_ID}->{$key} = $value; # put
82 8         50 $self->{MAP_BY_TERM}->{$value} = $key; # put
83             } else {
84 0         0 warn "\nThe following entry: '", $_, "' found in '", $self->{FILE}, "' is not recognized as a valid APO key-value pair!";
85             }
86             }
87 1         10 close APO_ID_MAP_IN_FH;
88             }
89             else {
90 0         0 open( APO_ID_MAP_IN_FH, "$self->{FILE}" );
91             # TODO Should I include a date?
92 0         0 close APO_ID_MAP_IN_FH;
93             }
94              
95 1         3 $self->{KEYS}->add_all_as_string( keys( %{ $self->{MAP_BY_ID} } ) );
  1         18  
96 1         3 return $self;
97             }
98              
99             sub _is_valid_id () {
100 2     2   4 my $new_name = $_[0];
101 2 50       17 return ($new_name =~ /APO:[A-Z]\d{7}/)?1:0;
102             }
103              
104             =head2 get_new_id
105              
106             Usage - $map->get_new_id("APO", "P", "cell cycle") or $map->get_new_id("APO", "Pa", "cell cycle")
107             Returns - a new APO ID (string)
108             Args - idspace (string), subnamespace (string), term (string)
109             Function - get a new APO ID and insert it (put) into this map
110            
111             =cut
112              
113             sub get_new_id () {
114 0     0 1   my ( $self, $idspace, $subnamespace, $term ) = @_;
115 0           my $result;
116 0 0 0       if ( $idspace && $subnamespace && $term ) {
      0        
117 0 0         if ( $self->is_empty() ) {
118 0           $result = $idspace.':'.$subnamespace.'0000001';
119             } else {
120 0           $result = $self->{KEYS}->get_new_id($idspace, $subnamespace);
121             }
122 0           $self->put( $result, $term ); # put
123             }
124 0           return $result;
125             }
126              
127             1;