EnsEMBL/IdMapping/MappingList.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 9 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 98 CC 4 SCAR 12.5
CC
4
Cov
0%
new
6.0
add_Entry
6.0
add_all
6.0
CRAP 3.5
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::IdMapping::MappingList - object holding a list of Entries
34  
35 =head1 SYNOPSIS
36  
37 # create a new MappingList
38 my $mappings = Bio::EnsEMBL::IdMapping::MappingList->new(
39 -DUMP_PATH => $dump_path,
40 -CACHE_FILE => 'gene_mappings.ser',
41 );
42  
43 # add entries
44 my $mappings->add_Entry($entry1);
45 my $mappings->add_all( $entry2, $entry3 );
46  
47 # serialise to file
48 $mappings->write_to_file;
49  
50 # later, read these mappings from file
51 my $mappings1 = Bio::EnsEMBL::IdMapping::MappingList->new(
52 -DUMP_PATH => $dump_path,
53 -CACHE_FILE => 'gene_mappings.ser',
54 );
55 $mappings1->read_from_file;
56  
57 =head1 DESCRIPTION
58  
59 This object represents a list of Bio::EnsEMBL::IdMapping::Entry
60 objects. It's essentially an OO wrapper for an array with some type
61 checking and convenience methods.
62  
63 =head1 METHODS
64  
65 new
66 add_Entry
67 get_all_Entries
68 add_all
69 get_entry_count
70 log
71 to_string
72  
73 =cut
74  
75 package Bio::EnsEMBL::IdMapping::MappingList;
76 $Bio::EnsEMBL::IdMapping::MappingList::VERSION = '114.0.0_50'; # TRIAL
77 $Bio::EnsEMBL::IdMapping::MappingList::VERSION = '114.0.050';
78 use strict;
79 use warnings;
80 no warnings 'uninitialized';
81  
82 use Bio::EnsEMBL::IdMapping::Serialisable;
83 our @ISA = qw(Bio::EnsEMBL::IdMapping::Serialisable);
84  
85 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
86 use Bio::EnsEMBL::Utils::ScriptUtils qw(path_append);
87  
88  
89 =head2 new
90  
91 Arg[1-N] : see superclass
92 Example : my $gene_mappings = Bio::EnsEMBL::IdMapping::MappingList->new(
93 -DUMP_PATH => $dump_path,
94 -CACHE_FILE => 'gene_mappings.ser',
95 );
96 Description : Constructor.
97 Return type : Bio::EnsEMBL::IdMapping::MappingList
98 Exceptions : none
99 Caller : general
100 Status : At Risk
101 : under development
102  
103 =cut
104  
105 sub new {
106 my $caller = shift;
107 my $class = ref($caller) || $caller;
108 my $self = $class->SUPER::new(@_);
109  
110 # initialise internal datastructure unless we loaded a serialised object
111 unless ($self->loaded) {
112 $self->{'cache'}->{'entries'} = [];
113 }
114  
115 return $self;
116 }
117  
118  
119 =head2 add_Entry
120  
121 Arg[1] : Bio::EnsEMBL::IdMapping::Entry - Entry to add
122 Example : $mappings->add_Entry($entry);
123 Description : Adds an Entry to the MappingList.
124 Return type : none
125 Exceptions : thrown on wrong or missing argument
126 Caller : general
127 Status : At Risk
128 : under development
129  
130 =cut
131  
132 sub add_Entry {
133 my $self = shift;
134 my $entry = shift;
135  
136 unless ($entry and $entry->isa('Bio::EnsEMBL::IdMapping::Entry')) {
137 throw("Need a Bio::EnsEMBL::IdMapping::Entry");
138 }
139  
140 push @{ $self->{'cache'}->{'entries'} }, $entry;
141 }
142  
143  
144 =head2 get_all_Entries
145  
146 Example : foreach my $entry (@{ $mappings->get_all_Entries }) {
147 # do something with the entry
148 }
149 Description : Gets all Entries in the MappingList.
150 Return type : Arrayref of Bio::EnsEMBL::IdMapping::Entry
151 Exceptions : none
152 Caller : general
153 Status : At Risk
154 : under development
155  
156 =cut
157  
158 sub get_all_Entries {
159 my $self = shift;
160 return $self->{'cache'}->{'entries'};
161 }
162  
163  
164 =head2 add_all
165  
166 Arg[1] : List of Bio::EnsEMBL::IdMapping::Entry objects
167 Example : my @entries = ($entry1, $entry2);
168 $mappings->add_all(@entries);
169 Description : Adds a list of Entries to the MappingList.
170 Return type : none
171 Exceptions : thrown on wrong argument
172 Caller : general
173 Status : At Risk
174 : under development
175  
176 =cut
177  
178 sub add_all {
179 my $self = shift;
180 my @mappings = @_;
181  
182 foreach my $mapping (@mappings) {
183
184 unless ($mapping->isa('Bio::EnsEMBL::IdMapping::MappingList')) {
185 throw("Need a Bio::EnsEMBL::IdMapping::MappingList");
186 }
187  
188 push @{ $self->{'cache'}->{'entries'} }, @{ $mapping->get_all_Entries };
189 }
190 }
191  
192  
193 =head2 get_entry_count
194  
195 Example : my $num_entries = $mappings->get_entry_count;
196 Description : Returns the number of Entries in the MappingList.
197 Return type : Int
198 Exceptions : none
199 Caller : general
200 Status : At Risk
201 : under development
202  
203 =cut
204  
205 sub get_entry_count {
206 my $self = shift;
207 return scalar(@{ $self->{'cache'}->{'entries'} });
208 }
209  
210  
211 =head2 log
212  
213 Arg[1] : String $type - object type (e.g. 'gene')
214 Arg[2] : String $dump_path - path for writing output
215 Example : $mappings->log('gene', $conf->param('basedir'));
216 Description : Logs all Entries in the MappingList to a file. Used for
217 debugging.
218 Return type : none
219 Exceptions : thrown on I/0 error
220 Caller : general
221 Status : At Risk
222 : under development
223  
224 =cut
225  
226 sub log {
227 my $self = shift;
228 my $type = shift;
229 my $dump_path = shift;
230
231 my $debug_path = path_append($dump_path, 'debug');
232 my $logfile = "$debug_path/${type}_final_scores.txt";
233
234 open(my $fh, '>', $logfile) or
235 throw("Unable to open $logfile for writing: $!");
236  
237 foreach my $entry (@{ $self->get_all_Entries }) {
238 print $fh ($entry->to_string."\n");
239 }
240  
241 close($fh);
242 }
243  
244  
245 =head2 to_string
246  
247 Example : print LOG $mappings->to_string, "\n";
248 Description : Returns a string representation of the MappingList. This is
249 simply a multi-line string, where each line is a stringified
250 Entry.
251 Useful for debugging and logging.
252 Return type : String
253 Exceptions : none
254 Caller : general
255 Status : At Risk
256 : under development
257  
258 =cut
259  
260 sub to_string {
261 my $self = shift;
262
263 my $string = '';
264
265 foreach my $entry (@{ $self->get_all_Entries }) {
266 $string .= $entry->to_string."\n";
267 }
268  
269 return $string;
270 }
271  
272  
273 1;
274