EnsEMBL/IdMapping/MappingList.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 103 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';
77 use strict;
78 use warnings;
79 no warnings 'uninitialized';
80  
81 use Bio::EnsEMBL::IdMapping::Serialisable;
82 our @ISA = qw(Bio::EnsEMBL::IdMapping::Serialisable);
83  
84 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
85 use Bio::EnsEMBL::Utils::ScriptUtils qw(path_append);
86  
87  
88 =head2 new
89  
90 Arg[1-N] : see superclass
91 Example : my $gene_mappings = Bio::EnsEMBL::IdMapping::MappingList->new(
92 -DUMP_PATH => $dump_path,
93 -CACHE_FILE => 'gene_mappings.ser',
94 );
95 Description : Constructor.
96 Return type : Bio::EnsEMBL::IdMapping::MappingList
97 Exceptions : none
98 Caller : general
99 Status : At Risk
100 : under development
101  
102 =cut
103  
104 sub new {
105 my $caller = shift;
106 my $class = ref($caller) || $caller;
107 my $self = $class->SUPER::new(@_);
108  
109 # initialise internal datastructure unless we loaded a serialised object
110 unless ($self->loaded) {
111 $self->{'cache'}->{'entries'} = [];
112 }
113  
114 return $self;
115 }
116  
117  
118 =head2 add_Entry
119  
120 Arg[1] : Bio::EnsEMBL::IdMapping::Entry - Entry to add
121 Example : $mappings->add_Entry($entry);
122 Description : Adds an Entry to the MappingList.
123 Return type : none
124 Exceptions : thrown on wrong or missing argument
125 Caller : general
126 Status : At Risk
127 : under development
128  
129 =cut
130  
131 sub add_Entry {
132 my $self = shift;
133 my $entry = shift;
134  
135 unless ($entry and $entry->isa('Bio::EnsEMBL::IdMapping::Entry')) {
136 throw("Need a Bio::EnsEMBL::IdMapping::Entry");
137 }
138  
139 push @{ $self->{'cache'}->{'entries'} }, $entry;
140 }
141  
142  
143 =head2 get_all_Entries
144  
145 Example : foreach my $entry (@{ $mappings->get_all_Entries }) {
146 # do something with the entry
147 }
148 Description : Gets all Entries in the MappingList.
149 Return type : Arrayref of Bio::EnsEMBL::IdMapping::Entry
150 Exceptions : none
151 Caller : general
152 Status : At Risk
153 : under development
154  
155 =cut
156  
157 sub get_all_Entries {
158 my $self = shift;
159 return $self->{'cache'}->{'entries'};
160 }
161  
162  
163 =head2 add_all
164  
165 Arg[1] : List of Bio::EnsEMBL::IdMapping::Entry objects
166 Example : my @entries = ($entry1, $entry2);
167 $mappings->add_all(@entries);
168 Description : Adds a list of Entries to the MappingList.
169 Return type : none
170 Exceptions : thrown on wrong argument
171 Caller : general
172 Status : At Risk
173 : under development
174  
175 =cut
176  
177 sub add_all {
178 my $self = shift;
179 my @mappings = @_;
180  
181 foreach my $mapping (@mappings) {
182
183 unless ($mapping->isa('Bio::EnsEMBL::IdMapping::MappingList')) {
184 throw("Need a Bio::EnsEMBL::IdMapping::MappingList");
185 }
186  
187 push @{ $self->{'cache'}->{'entries'} }, @{ $mapping->get_all_Entries };
188 }
189 }
190  
191  
192 =head2 get_entry_count
193  
194 Example : my $num_entries = $mappings->get_entry_count;
195 Description : Returns the number of Entries in the MappingList.
196 Return type : Int
197 Exceptions : none
198 Caller : general
199 Status : At Risk
200 : under development
201  
202 =cut
203  
204 sub get_entry_count {
205 my $self = shift;
206 return scalar(@{ $self->{'cache'}->{'entries'} });
207 }
208  
209  
210 =head2 log
211  
212 Arg[1] : String $type - object type (e.g. 'gene')
213 Arg[2] : String $dump_path - path for writing output
214 Example : $mappings->log('gene', $conf->param('basedir'));
215 Description : Logs all Entries in the MappingList to a file. Used for
216 debugging.
217 Return type : none
218 Exceptions : thrown on I/0 error
219 Caller : general
220 Status : At Risk
221 : under development
222  
223 =cut
224  
225 sub log {
226 my $self = shift;
227 my $type = shift;
228 my $dump_path = shift;
229
230 my $debug_path = path_append($dump_path, 'debug');
231 my $logfile = "$debug_path/${type}_final_scores.txt";
232
233 open(my $fh, '>', $logfile) or
234 throw("Unable to open $logfile for writing: $!");
235  
236 foreach my $entry (@{ $self->get_all_Entries }) {
237 print $fh ($entry->to_string."\n");
238 }
239  
240 close($fh);
241 }
242  
243  
244 =head2 to_string
245  
246 Example : print LOG $mappings->to_string, "\n";
247 Description : Returns a string representation of the MappingList. This is
248 simply a multi-line string, where each line is a stringified
249 Entry.
250 Useful for debugging and logging.
251 Return type : String
252 Exceptions : none
253 Caller : general
254 Status : At Risk
255 : under development
256  
257 =cut
258  
259 sub to_string {
260 my $self = shift;
261
262 my $string = '';
263
264 foreach my $entry (@{ $self->get_all_Entries }) {
265 $string .= $entry->to_string."\n";
266 }
267  
268 return $string;
269 }
270  
271  
272 1;
273