3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
23 Please email comments or questions to the
public Ensembl
24 developers list at <http:
26 Questions may also be sent to the Ensembl help desk at
37 # get a reference slice
39 $slice_adaptor->fetch_by_region(
'chromosome', 14, 900000, 950000 );
41 # create MappedSliceContainer based on the reference slice
44 # set the adaptor for fetching AssemblySlices
46 $msc->set_AssemblySliceAdaptor($asa);
48 # add an AssemblySlice to your MappedSliceContainer
49 $msc->attach_AssemblySlice(
'NCBIM36');
51 foreach my $mapped_slice ( @{ $msc->get_all_MappedSlices } ) {
52 print $mapped_slice->name,
"\n";
54 foreach my $sf ( @{ $mapped_slice->get_all_SimpleFeatures } ) {
55 print
" ", &to_string($sf),
"\n";
61 NOTE:
this code is under development and not fully functional nor tested
62 yet. Use only
for development.
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
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
76 code in the adaptor/factory module.
78 In the simplest use
case, all required MappedSlices are attached to the
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.
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)
98 attach_AlignSlice (not implemented yet)
99 attach_StrainSlice (not implemented yet)
101 sub_MappedSliceContainer (not implemented yet)
107 =head1 RELATED MODULES
111 Bio::EnsEMBL::Compara::AlignSlice
112 Bio::EnsEMBL::Compara::AlignSlice::Slice
113 Bio::EnsEMBL::StrainSlice
117 package Bio::EnsEMBL::MappedSliceContainer;
121 no warnings
'uninitialized';
130 # define avalable adaptormajs to use with this container
131 my %adaptors =
map { $_ => 1 } qw(assembly align strain);
138 Arg [EXPANDED] : (optional) Boolean $expanded - set expanded mode (
default:
140 Example : my $slice = $slice_adaptor->fetch_by_region(
'chromosome', 1,
146 Description : Constructor. See the general documentation of
this module
for
147 details
about this object. Note that the constructor creates an
148 empty container, so you
'll have to attach MappedSlices to it to
149 be useful (this is usually done by an adaptor/factory).
150 Return type : Bio::EnsEMBL::MappedSliceContainer
151 Exceptions : thrown on wrong or missing argument
160 my $class = ref($caller) || $caller;
162 my ($ref_slice, $expanded) = rearrange([qw(SLICE EXPANDED)], @_);
165 unless ($ref_slice and ref($ref_slice) and
167 throw("You must provide a reference slice.");
171 bless ($self, $class);
174 $self->{'ref_slice
'} = $ref_slice;
175 $self->{'expanded
'} = $expanded || 0;
177 $self->{'mapped_slices
'} = [];
179 # create the container slice
180 $self->_create_container_slice($ref_slice);
187 # Create an artificial slice which represents the common coordinate system used
188 # for this MappedSliceContainer
190 sub _create_container_slice {
192 my $ref_slice = shift;
195 unless ($ref_slice and ref($ref_slice) and
197 throw("You must provide a reference slice.");
200 # create an artificial coordinate system for the container slice
201 my $cs = Bio::EnsEMBL::CoordSystem->new(
202 -NAME => 'container
',
206 # Create a new artificial slice spanning your container. Initially this will
207 # simply span your reference slice
208 my $container_slice = Bio::EnsEMBL::Slice->new(
209 -COORD_SYSTEM => $cs,
211 -END => $ref_slice->length,
213 -SEQ_REGION_NAME => 'container
',
216 $self->{'container_slice
'} = $container_slice;
218 # Create an Mapper to map to/from the reference slice to the container coord
220 my $mapper = Bio::EnsEMBL::Mapper->new('ref_slice
', 'container
');
222 $mapper->add_map_coordinates(
223 $ref_slice->seq_region_name,
227 $container_slice->seq_region_name,
228 $container_slice->start,
229 $container_slice->end,
232 $self->{'mapper
'} = $mapper;
238 Arg[1] : String $type - the type of adaptor to set
239 Arg[2] : Adaptor $adaptor - the adaptor to set
240 Example : my $adaptor = Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor->new;
241 $msc->set_adaptor('assembly
', $adaptor);
242 Description : Parameterisable wrapper for all methods that set adaptors (see
244 Return type : same as Arg 2
245 Exceptions : thrown on missing type
258 unless ($type and $adaptors{$type}) {
259 throw("Missing or unknown adaptor type.");
262 $type = ucfirst($type);
263 my $method = "set_${type}SliceAdaptor";
265 return $self->$method($adaptor);
271 Arg[1] : String $type - the type of adaptor to get
272 Example : my $assembly_slice_adaptor = $msc->get_adaptor('assembly
');
273 Description : Parameterisable wrapper for all methods that get adaptors (see
275 Return type : An adaptor for the requested type of MappedSlice.
276 Exceptions : thrown on missing type
288 unless ($type and $adaptors{$type}) {
289 throw("Missing or unknown adaptor type.");
292 $type = ucfirst($type);
293 my $method = "get_${type}SliceAdaptor";
295 return $self->$method;
299 =head2 set_AssemblySliceAdaptor
301 Arg[1] : Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor - the adaptor to set
302 Example : my $adaptor = Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor->new;
303 $msc->set_AssemblySliceAdaptor($adaptor);
304 Description : Sets an AssemblySliceAdaptor for this container. The adaptor can
305 be used to attach MappedSlice for alternative assemblies.
306 Return type : Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor
307 Exceptions : thrown on wrong or missing argument
308 Caller : general, $self->get_adaptor
314 sub set_AssemblySliceAdaptor {
316 my $assembly_slice_adaptor = shift;
318 unless ($assembly_slice_adaptor and ref($assembly_slice_adaptor) and
320 throw("Need a Bio::EnsEMBL::AssemblySliceAdaptor.");
323 $self->{'adaptors
'}->{'AssemblySlice
'} = $assembly_slice_adaptor;
327 =head2 get_AssemblySliceAdaptor
329 Example : my $assembly_slice_adaptor = $msc->get_AssemblySliceAdaptor;
330 Description : Gets a AssemblySliceAdaptor from this container. The adaptor can
331 be used to attach MappedSlice for alternative assemblies.
332 Return type : Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor
333 Exceptions : thrown on wrong or missing argument
334 Caller : general, $self->get_adaptor
340 sub get_AssemblySliceAdaptor {
343 unless ($self->{'adaptors
'}->{'AssemblySlice
'}) {
344 warning("No AssemblySliceAdaptor attached to MappedSliceContainer.");
347 return $self->{'adaptors
'}->{'AssemblySlice
'};
352 sub set_AlignSliceAdaptor {
353 throw("Not implemented yet!");
358 sub get_AlignSliceAdaptor {
359 throw("Not implemented yet!");
364 sub set_StrainSliceAdaptor {
366 my $strain_slice_adaptor = shift;
368 unless ($strain_slice_adaptor and ref($strain_slice_adaptor) and
369 $strain_slice_adaptor->isa('Bio::EnsEMBL::Variation::DBSQL::StrainSliceAdaptor
')) {
370 throw("Need a Bio::EnsEMBL::Variation::DBSQL::StrainSliceAdaptor.");
373 $self->{'adaptors
'}->{'StrainSlice
'} = $strain_slice_adaptor;
378 sub get_StrainSliceAdaptor {
381 unless ($self->{'adaptors
'}->{'StrainSlice
'}) {
382 warning("No StrainSliceAdaptor attached to MappedSliceContainer.");
385 return $self->{'adaptors
'}->{'StrainSlice
'};
389 =head2 attach_AssemblySlice
391 Arg[1] : String $version - assembly version to attach
392 Example : $msc->attach_AssemblySlice('NCBIM36
');
393 Description : Attaches a MappedSlice for an alternative assembly to this
396 Exceptions : thrown on missing argument
397 Caller : general, Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor
403 sub attach_AssemblySlice {
407 throw("Need a version.") unless ($version);
409 my $asa = $self->get_AssemblySliceAdaptor;
410 return unless ($asa);
412 my @mapped_slices = @{ $asa->fetch_by_version($self, $version) };
414 push @{ $self->{'mapped_slices
'} }, @mapped_slices;
418 =head2 attach_StrainSlice
420 Arg[1] : String $strain - name of strain to attach
421 Example : $msc->attach_StrainSlice('Watson
');
422 Description : Attaches a MappedSlice for an alternative strain to this
425 Exceptions : thrown on missing argument
426 Caller : general, Bio::EnsEMBL::DBSQL::StrainSliceAdaptor
432 sub attach_StrainSlice {
436 throw("Need a strain.") unless ($strain);
438 my $ssa = $self->get_StrainSliceAdaptor;
439 return unless ($ssa);
441 my @mapped_slices = @{ $ssa->fetch_by_name($self, $strain) };
443 push @{ $self->{'mapped_slices
'} }, @mapped_slices;
448 =head2 get_all_MappedSlices
450 Example : foreach my $mapped_slice (@{ $msc->get_all_MappedSlices }) {
451 print $mapped_slice->name, "\n";
453 Description : Returns all MappedSlices attached to this container.
454 Return type : listref of Bio::EnsEMBL::MappedSlice
462 sub get_all_MappedSlices {
464 return $self->{'mapped_slices
'};
469 sub sub_MappedSliceContainer {
470 throw("Not implemented yet!");
476 Arg[1] : (optional) Bio::EnsEMBL::Slice - the reference slice to set
477 Example : my $ref_slice = $mapped_slice_container->ref_slice;
478 print "This MappedSliceContainer is based on the reference
479 slice ", $ref_slice->name, "\n";
480 Description : Getter/setter for the reference slice.
481 Return type : Bio::EnsEMBL::Slice
482 Exceptions : thrown on wrong argument type
496 throw("Need a Bio::EnsEMBL::Slice.");
499 $self->{'ref_slice
'} = $slice;
502 return $self->{'ref_slice
'};
506 =head2 container_slice
508 Arg[1] : (optional) Bio::EnsEMBL::Slice - the container slice to set
509 Example : my $container_slice = $mapped_slice_container->container_slice;
510 print "The common slice used by this MappedSliceContainer is ",
511 $container_slice->name, "\n";
512 Description : Getter/setter for the container slice. This is an artificial
513 slice which defines the common coordinate system used by the
514 MappedSlices attached to this container.
515 Return type : Bio::EnsEMBL::Slice
516 Exceptions : thrown on wrong argument type
523 sub container_slice {
530 throw("Need a Bio::EnsEMBL::Slice.");
533 $self->{'container_slice
'} = $slice;
536 return $self->{'container_slice
'};
542 Arg[1] : (optional) Bio::EnsEMBL::Mapper - the mapper to set
543 Example : my $mapper = Bio::EnsEMBL::Mapper->new('ref
', 'mapped
');
544 $mapped_slice_container->mapper($mapper);
545 Description : Getter/setter for the mapper to map between reference slice and
546 the artificial container coord system.
547 Return type : Bio::EnsEMBL::Mapper
548 Exceptions : thrown on wrong argument type
549 Caller : internal, Bio::EnsEMBL::MappedSlice->AUTOLOAD
562 throw("Need a Bio::EnsEMBL::Mapper.");
565 $self->{'mapper
'} = $mapper;
568 return $self->{'mapper
'};
574 Arg[1] : (optional) Boolean - expanded mode to set
575 Example : if ($mapped_slice_container->expanded) {
576 # do more elaborate mapping than in collapsed mode
579 Description : Getter/setter for expanded mode.
581 By default, MappedSliceContainer use collapsed mode, which
582 means that no inserts in the reference sequence are allowed
583 when constructing the MappedSlices. in this mode, the
584 mapped_slice artificial coord system will be identical with the
585 ref_slice coord system.
587 By setting expanded mode, you allow inserts in the reference
589 Return type : Boolean
599 $self->{'expanded
'} = shift if (@_);
600 return $self->{'expanded
'};
605 Example : my $seq = $container->seq()
606 Description : Retrieves the expanded sequence of the artificial container
607 slice, including "-" characters where there are inserts in any
608 of the attached mapped slices.
620 my $container_seq = '';
622 # check there's a mapper
623 if(defined($self->mapper)) {
625 my $slice = $self->ref_slice();
626 my $seq = $slice->seq();
628 foreach my $coord($self->mapper->map_coordinates($slice->seq_region_name, $slice->start, $slice->end, $slice->strand,
'ref_slice')) {
629 # if it is a normal coordinate insert sequence
630 if(!$coord->isa(
'Bio::EnsEMBL::Mapper::IndelCoordinate')) {
631 $container_seq .= substr($seq, $start, $coord->length());
632 $start += $coord->length;
635 # if it is a gap or indel insert "-"
637 $container_seq .=
'-' x $coord->length();
642 return $container_seq;