EnsEMBL/MappedSliceContainer.pm untested

filter: Statementstmt 0.0% 0 / 144 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 56 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 288 CC 22 SCAR 19.8
CC
22
Cov
0%
attach_AssemblySlice
12.0
attach_StrainSlice
12.0
ref_slice
12.0
CRAP 7.2
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::MappedSliceContainer - container for mapped slices
34  
35 =head1 SYNOPSIS
36  
37 # get a reference slice
38 my $slice =
39 $slice_adaptor->fetch_by_region( 'chromosome', 14, 900000, 950000 );
40  
41 # create MappedSliceContainer based on the reference slice
42 my $msc = Bio::EnsEMBL::MappedSliceContainer->new( -SLICE => $slice );
43  
44 # set the adaptor for fetching AssemblySlices
45 my $asa = $slice->adaptor->db->get_AssemblySliceAdaptor;
46 $msc->set_AssemblySliceAdaptor($asa);
47  
48 # add an AssemblySlice to your MappedSliceContainer
49 $msc->attach_AssemblySlice('NCBIM36');
50  
51 foreach my $mapped_slice ( @{ $msc->get_all_MappedSlices } ) {
52 print $mapped_slice->name, "\n";
53  
54 foreach my $sf ( @{ $mapped_slice->get_all_SimpleFeatures } ) {
55 print " ", &to_string($sf), "\n";
56 }
57 }
58  
59 =head1 DESCRIPTION
60  
61 NOTE: this code is under development and not fully functional nor tested
62 yet. Use only for development.
63  
64 A MappedSliceContainer holds a collection of one or more
65 Bio::EnsEMBL::MappedSlices. It is based on a real reference slice and
66 contains an artificial "container slice" which defines the common
67 coordinate system used by all attached MappedSlices. There is also a
68 mapper to convert coordinates between the reference and the container
69 slice.
70  
71 Attaching MappedSlices to the container is delegated to adaptors
72 (which act more as object factories than as traditional Ensembl db
73 adaptors). The adaptors will also modify the container slice and
74 associated mapper if required. This design allows us to keep the
75 MappedSliceContainer generic and encapsulate the data source specific
76 code in the adaptor/factory module.
77  
78 In the simplest use case, all required MappedSlices are attached to the
79 MappedSliceContainer at once (by a single call to the adaptor). This
80 object should also allow "hot-plugging" of MappedSlices (e.g. attach a
81 MappedSlice representing a strain to a container that already contains a
82 multi-species alignment). The methods for attaching new MappedSlice will
83 be responsable to perform the necessary adjustments to coordinates and
84 mapper on the existing MappedSlices.
85  
86 =head1 METHODS
87  
88 new
89 set_adaptor
90 get_adaptor
91 set_AssemblySliceAdaptor
92 get_AssemblySliceAdaptor
93 set_AlignSliceAdaptor (not implemented yet)
94 get_AlignSliceAdaptor (not implemented yet)
95 set_StrainSliceAdaptor (not implemented yet)
96 get_StrainSliceAdaptor (not implemented yet)
97 attach_AssemblySlice
98 attach_AlignSlice (not implemented yet)
99 attach_StrainSlice (not implemented yet)
100 get_all_MappedSlices
101 sub_MappedSliceContainer (not implemented yet)
102 ref_slice
103 container_slice
104 mapper
105 expanded
106  
107 =head1 RELATED MODULES
108  
109 Bio::EnsEMBL::MappedSlice
110 Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor
111 Bio::EnsEMBL::Compara::AlignSlice
112 Bio::EnsEMBL::Compara::AlignSlice::Slice
113 Bio::EnsEMBL::StrainSlice
114  
115 =cut
116  
117 package Bio::EnsEMBL::MappedSliceContainer;
118 $Bio::EnsEMBL::MappedSliceContainer::VERSION = '114.0.0_50'; # TRIAL
119 $Bio::EnsEMBL::MappedSliceContainer::VERSION = '114.0.050';
120 use strict;
121 use warnings;
122 no warnings 'uninitialized';
123  
124 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
125 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
126 use Bio::EnsEMBL::CoordSystem;
127 use Bio::EnsEMBL::Slice;
128 use Bio::EnsEMBL::Mapper;
129  
130  
131 # define avalable adaptormajs to use with this container
132 my %adaptors = map { $_ => 1 } qw(assembly align strain);
133  
134  
135 =head2 new
136  
137 Arg [SLICE] : Bio::EnsEMBL::Slice $slice - the reference slice for this
138 container
139 Arg [EXPANDED] : (optional) Boolean $expanded - set expanded mode (default:
140 collapsed)
141 Example : my $slice = $slice_adaptor->fetch_by_region('chromosome', 1,
142 9000000, 9500000);
143 my $msc = Bio::EnsEMBL::MappedSliceContainer->new(
144 -SLICE => $slice,
145 -EXPANDED => 1,
146 );
147 Description : Constructor. See the general documentation of this module for
148 details about this object. Note that the constructor creates an
149 empty container, so you'll have to attach MappedSlices to it to
150 be useful (this is usually done by an adaptor/factory).
151 Return type : Bio::EnsEMBL::MappedSliceContainer
152 Exceptions : thrown on wrong or missing argument
153 Caller : general
154 Status : At Risk
155 : under development
156  
157 =cut
158  
159 sub new {
160 my $caller = shift;
161 my $class = ref($caller) || $caller;
162  
163 my ($ref_slice, $expanded) = rearrange([qw(SLICE EXPANDED)], @_);
164  
165 # argument check
166 unless ($ref_slice and ref($ref_slice) and
167 ($ref_slice->isa('Bio::EnsEMBL::Slice') or $ref_slice->isa('Bio::EnsEMBL::LRGSlice')) ) {
168 throw("You must provide a reference slice.");
169 }
170  
171 my $self = {};
172 bless ($self, $class);
173  
174 # initialise object
175 $self->{'ref_slice'} = $ref_slice;
176 $self->{'expanded'} = $expanded || 0;
177  
178 $self->{'mapped_slices'} = [];
179  
180 # create the container slice
181 $self->_create_container_slice($ref_slice);
182  
183 return $self;
184 }
185  
186  
187 #
188 # Create an artificial slice which represents the common coordinate system used
189 # for this MappedSliceContainer
190 #
191 sub _create_container_slice {
192 my $self = shift;
193 my $ref_slice = shift;
194  
195 # argument check
196 unless ($ref_slice and ref($ref_slice) and
197 ($ref_slice->isa('Bio::EnsEMBL::Slice') or $ref_slice->isa('Bio::EnsEMBL::LRGSlice')) ) {
198 throw("You must provide a reference slice.");
199 }
200  
201 # create an artificial coordinate system for the container slice
202 my $cs = Bio::EnsEMBL::CoordSystem->new(
203 -NAME => 'container',
204 -RANK => 1,
205 );
206  
207 # Create a new artificial slice spanning your container. Initially this will
208 # simply span your reference slice
209 my $container_slice = Bio::EnsEMBL::Slice->new(
210 -COORD_SYSTEM => $cs,
211 -START => 1,
212 -END => $ref_slice->length,
213 -STRAND => 1,
214 -SEQ_REGION_NAME => 'container',
215 );
216  
217 $self->{'container_slice'} = $container_slice;
218  
219 # Create an Mapper to map to/from the reference slice to the container coord
220 # system.
221 my $mapper = Bio::EnsEMBL::Mapper->new('ref_slice', 'container');
222
223 $mapper->add_map_coordinates(
224 $ref_slice->seq_region_name,
225 $ref_slice->start,
226 $ref_slice->end,
227 1,
228 $container_slice->seq_region_name,
229 $container_slice->start,
230 $container_slice->end,
231 );
232  
233 $self->{'mapper'} = $mapper;
234 }
235  
236  
237 =head2 set_adaptor
238  
239 Arg[1] : String $type - the type of adaptor to set
240 Arg[2] : Adaptor $adaptor - the adaptor to set
241 Example : my $adaptor = Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor->new;
242 $msc->set_adaptor('assembly', $adaptor);
243 Description : Parameterisable wrapper for all methods that set adaptors (see
244 below).
245 Return type : same as Arg 2
246 Exceptions : thrown on missing type
247 Caller : general
248 Status : At Risk
249 : under development
250  
251 =cut
252  
253 sub set_adaptor {
254 my $self = shift;
255 my $type = shift;
256 my $adaptor = shift;
257  
258 # argument check
259 unless ($type and $adaptors{$type}) {
260 throw("Missing or unknown adaptor type.");
261 }
262  
263 $type = ucfirst($type);
264 my $method = "set_${type}SliceAdaptor";
265  
266 return $self->$method($adaptor);
267 }
268  
269  
270 =head2 get_adaptor
271  
272 Arg[1] : String $type - the type of adaptor to get
273 Example : my $assembly_slice_adaptor = $msc->get_adaptor('assembly');
274 Description : Parameterisable wrapper for all methods that get adaptors (see
275 below).
276 Return type : An adaptor for the requested type of MappedSlice.
277 Exceptions : thrown on missing type
278 Caller : general
279 Status : At Risk
280 : under development
281  
282 =cut
283  
284 sub get_adaptor {
285 my $self = shift;
286 my $type = shift;
287  
288 # argument check
289 unless ($type and $adaptors{$type}) {
290 throw("Missing or unknown adaptor type.");
291 }
292  
293 $type = ucfirst($type);
294 my $method = "get_${type}SliceAdaptor";
295  
296 return $self->$method;
297 }
298  
299  
300 =head2 set_AssemblySliceAdaptor
301  
302 Arg[1] : Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor - the adaptor to set
303 Example : my $adaptor = Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor->new;
304 $msc->set_AssemblySliceAdaptor($adaptor);
305 Description : Sets an AssemblySliceAdaptor for this container. The adaptor can
306 be used to attach MappedSlice for alternative assemblies.
307 Return type : Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor
308 Exceptions : thrown on wrong or missing argument
309 Caller : general, $self->get_adaptor
310 Status : At Risk
311 : under development
312  
313 =cut
314  
315 sub set_AssemblySliceAdaptor {
316 my $self = shift;
317 my $assembly_slice_adaptor = shift;
318  
319 unless ($assembly_slice_adaptor and ref($assembly_slice_adaptor) and
320 $assembly_slice_adaptor->isa('Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor')) {
321 throw("Need a Bio::EnsEMBL::AssemblySliceAdaptor.");
322 }
323  
324 $self->{'adaptors'}->{'AssemblySlice'} = $assembly_slice_adaptor;
325 }
326  
327  
328 =head2 get_AssemblySliceAdaptor
329  
330 Example : my $assembly_slice_adaptor = $msc->get_AssemblySliceAdaptor;
331 Description : Gets a AssemblySliceAdaptor from this container. The adaptor can
332 be used to attach MappedSlice for alternative assemblies.
333 Return type : Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor
334 Exceptions : thrown on wrong or missing argument
335 Caller : general, $self->get_adaptor
336 Status : At Risk
337 : under development
338  
339 =cut
340  
341 sub get_AssemblySliceAdaptor {
342 my $self = shift;
343  
344 unless ($self->{'adaptors'}->{'AssemblySlice'}) {
345 warning("No AssemblySliceAdaptor attached to MappedSliceContainer.");
346 }
347  
348 return $self->{'adaptors'}->{'AssemblySlice'};
349 }
350  
351  
352 # [todo]
353 sub set_AlignSliceAdaptor {
354 throw("Not implemented yet!");
355 }
356  
357  
358 # [todo]
359 sub get_AlignSliceAdaptor {
360 throw("Not implemented yet!");
361 }
362  
363  
364 # [todo]
365 sub set_StrainSliceAdaptor {
366 my $self = shift;
367 my $strain_slice_adaptor = shift;
368  
369 unless ($strain_slice_adaptor and ref($strain_slice_adaptor) and
370 $strain_slice_adaptor->isa('Bio::EnsEMBL::Variation::DBSQL::StrainSliceAdaptor')) {
371 throw("Need a Bio::EnsEMBL::Variation::DBSQL::StrainSliceAdaptor.");
372 }
373  
374 $self->{'adaptors'}->{'StrainSlice'} = $strain_slice_adaptor;
375 }
376  
377  
378 # [todo]
379 sub get_StrainSliceAdaptor {
380 my $self = shift;
381  
382 unless ($self->{'adaptors'}->{'StrainSlice'}) {
383 warning("No StrainSliceAdaptor attached to MappedSliceContainer.");
384 }
385  
386 return $self->{'adaptors'}->{'StrainSlice'};
387 }
388  
389  
390 =head2 attach_AssemblySlice
391  
392 Arg[1] : String $version - assembly version to attach
393 Example : $msc->attach_AssemblySlice('NCBIM36');
394 Description : Attaches a MappedSlice for an alternative assembly to this
395 container.
396 Return type : none
397 Exceptions : thrown on missing argument
398 Caller : general, Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor
399 Status : At Risk
400 : under development
401  
402 =cut
403  
404 sub attach_AssemblySlice {
405 my $self = shift;
406 my $version = shift;
407  
408 throw("Need a version.") unless ($version);
409  
410 my $asa = $self->get_AssemblySliceAdaptor;
411 return unless ($asa);
412  
413 my @mapped_slices = @{ $asa->fetch_by_version($self, $version) };
414  
415 push @{ $self->{'mapped_slices'} }, @mapped_slices;
416 }
417  
418  
419 =head2 attach_StrainSlice
420  
421 Arg[1] : String $strain - name of strain to attach
422 Example : $msc->attach_StrainSlice('Watson');
423 Description : Attaches a MappedSlice for an alternative strain to this
424 container.
425 Return type : none
426 Exceptions : thrown on missing argument
427 Caller : general, Bio::EnsEMBL::DBSQL::StrainSliceAdaptor
428 Status : At Risk
429 : under development
430  
431 =cut
432  
433 sub attach_StrainSlice {
434 my $self = shift;
435 my $strain = shift;
436  
437 throw("Need a strain.") unless ($strain);
438  
439 my $ssa = $self->get_StrainSliceAdaptor;
440 return unless ($ssa);
441  
442 my @mapped_slices = @{ $ssa->fetch_by_name($self, $strain) };
443  
444 push @{ $self->{'mapped_slices'} }, @mapped_slices;
445 }
446  
447  
448  
449 =head2 get_all_MappedSlices
450  
451 Example : foreach my $mapped_slice (@{ $msc->get_all_MappedSlices }) {
452 print $mapped_slice->name, "\n";
453 }
454 Description : Returns all MappedSlices attached to this container.
455 Return type : listref of Bio::EnsEMBL::MappedSlice
456 Exceptions : none
457 Caller : general
458 Status : At Risk
459 : under development
460  
461 =cut
462  
463 sub get_all_MappedSlices {
464 my $self = shift;
465 return $self->{'mapped_slices'};
466 }
467  
468  
469 # [todo]
470 sub sub_MappedSliceContainer {
471 throw("Not implemented yet!");
472 }
473  
474  
475 =head2 ref_slice
476  
477 Arg[1] : (optional) Bio::EnsEMBL::Slice - the reference slice to set
478 Example : my $ref_slice = $mapped_slice_container->ref_slice;
479 print "This MappedSliceContainer is based on the reference
480 slice ", $ref_slice->name, "\n";
481 Description : Getter/setter for the reference slice.
482 Return type : Bio::EnsEMBL::Slice
483 Exceptions : thrown on wrong argument type
484 Caller : general
485 Status : At Risk
486 : under development
487  
488 =cut
489  
490 sub ref_slice {
491 my $self = shift;
492
493 if (@_) {
494 my $slice = shift;
495
496 unless (ref($slice) and ($slice->isa('Bio::EnsEMBL::Slice') or $slice->isa('Bio::EnsEMBL::LRGSlice'))) {
497 throw("Need a Bio::EnsEMBL::Slice.");
498 }
499
500 $self->{'ref_slice'} = $slice;
501 }
502  
503 return $self->{'ref_slice'};
504 }
505  
506  
507 =head2 container_slice
508  
509 Arg[1] : (optional) Bio::EnsEMBL::Slice - the container slice to set
510 Example : my $container_slice = $mapped_slice_container->container_slice;
511 print "The common slice used by this MappedSliceContainer is ",
512 $container_slice->name, "\n";
513 Description : Getter/setter for the container slice. This is an artificial
514 slice which defines the common coordinate system used by the
515 MappedSlices attached to this container.
516 Return type : Bio::EnsEMBL::Slice
517 Exceptions : thrown on wrong argument type
518 Caller : general
519 Status : At Risk
520 : under development
521  
522 =cut
523  
524 sub container_slice {
525 my $self = shift;
526
527 if (@_) {
528 my $slice = shift;
529
530 unless (ref($slice) and ($slice->isa('Bio::EnsEMBL::Slice') or $slice->isa('Bio::EnsEMBL::LRGSlice')) ) {
531 throw("Need a Bio::EnsEMBL::Slice.");
532 }
533
534 $self->{'container_slice'} = $slice;
535 }
536  
537 return $self->{'container_slice'};
538 }
539  
540  
541 =head2 mapper
542  
543 Arg[1] : (optional) Bio::EnsEMBL::Mapper - the mapper to set
544 Example : my $mapper = Bio::EnsEMBL::Mapper->new('ref', 'mapped');
545 $mapped_slice_container->mapper($mapper);
546 Description : Getter/setter for the mapper to map between reference slice and
547 the artificial container coord system.
548 Return type : Bio::EnsEMBL::Mapper
549 Exceptions : thrown on wrong argument type
550 Caller : internal, Bio::EnsEMBL::MappedSlice->AUTOLOAD
551 Status : At Risk
552 : under development
553  
554 =cut
555  
556 sub mapper {
557 my $self = shift;
558
559 if (@_) {
560 my $mapper = shift;
561
562 unless (ref($mapper) and $mapper->isa('Bio::EnsEMBL::Mapper')) {
563 throw("Need a Bio::EnsEMBL::Mapper.");
564 }
565
566 $self->{'mapper'} = $mapper;
567 }
568  
569 return $self->{'mapper'};
570 }
571  
572  
573 =head2 expanded
574  
575 Arg[1] : (optional) Boolean - expanded mode to set
576 Example : if ($mapped_slice_container->expanded) {
577 # do more elaborate mapping than in collapsed mode
578 [...]
579 }
580 Description : Getter/setter for expanded mode.
581
582 By default, MappedSliceContainer use collapsed mode, which
583 means that no inserts in the reference sequence are allowed
584 when constructing the MappedSlices. in this mode, the
585 mapped_slice artificial coord system will be identical with the
586 ref_slice coord system.
587
588 By setting expanded mode, you allow inserts in the reference
589 sequence.
590 Return type : Boolean
591 Exceptions : none
592 Caller : general
593 Status : At Risk
594 : under development
595  
596 =cut
597  
598 sub expanded {
599 my $self = shift;
600 $self->{'expanded'} = shift if (@_);
601 return $self->{'expanded'};
602 }
603  
604 =head2 seq
605  
606 Example : my $seq = $container->seq()
607 Description : Retrieves the expanded sequence of the artificial container
608 slice, including "-" characters where there are inserts in any
609 of the attached mapped slices.
610 Return type : String
611 Exceptions : none
612 Caller : general
613 Status : At Risk
614 : under development
615  
616 =cut
617  
618 sub seq {
619 my $self = shift;
620
621 my $container_seq = '';
622
623 # check there's a mapper
624 if(defined($self->mapper)) {
625 my $start = 0;
626 my $slice = $self->ref_slice();
627 my $seq = $slice->seq();
628
629 foreach my $coord($self->mapper->map_coordinates($slice->seq_region_name, $slice->start, $slice->end, $slice->strand, 'ref_slice')) {
630 # if it is a normal coordinate insert sequence
631 if(!$coord->isa('Bio::EnsEMBL::Mapper::IndelCoordinate')) {
632 $container_seq .= substr($seq, $start, $coord->length());
633 $start += $coord->length;
634 }
635
636 # if it is a gap or indel insert "-"
637 else {
638 $container_seq .= '-' x $coord->length();
639 }
640 }
641 }
642
643 return $container_seq;
644 }
645  
646  
647 1;
648