AssemblySliceAdaptor.pm 6.79 KB
Newer Older
1 2
=head1 LICENSE

3
Copyright [1999-2013] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
4

5 6 7 8 9 10 11 12 13 14 15 16 17
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut
18 19 20 21 22


=head1 CONTACT

  Please email comments or questions to the public Ensembl
Magali Ruffier's avatar
Magali Ruffier committed
23
  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
24 25

  Questions may also be sent to the Ensembl help desk at
Magali Ruffier's avatar
Magali Ruffier committed
26
  <http://www.ensembl.org/Help/Contact>.
27 28

=cut
29 30 31

=head1 NAME

32 33
Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor - adaptor/factory for MappedSlices
representing alternative assemblies
34 35 36

=head1 SYNOPSIS

37 38
  my $slice =
    $slice_adaptor->fetch_by_region( 'chromosome', 14, 900000, 950000 );
39

40
  my $msc = Bio::EnsEMBL::MappedSliceContainer->new( -SLICE => $slice );
41

42
  my $asa = Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor->new;
43

44
  my ($mapped_slice) = @{ $asa->fetch_by_version( $msc, 'NCBIM36' ) };
45 46 47

=head1 DESCRIPTION

48 49
NOTE: this code is under development and not fully functional nor tested
yet.  Use only for development.
50

51 52 53 54
This adaptor is a factory for creating MappedSlices representing
alternative assemblies and attaching them to a MappedSliceContainer. A
mapper will be created to map between the reference slice and the common
container slice coordinate system.
55 56 57

=head1 METHODS

58 59
  new
  fetch_by_version
60 61 62

=head1 REALTED MODULES

63 64 65 66 67 68
  Bio::EnsEMBL::MappedSlice
  Bio::EnsEMBL::MappedSliceContainer
  Bio::EnsEMBL::Compara::AlignSlice
  Bio::EnsEMBL::Compara::AlignSlice::Slice
  Bio::EnsEMBL::AlignStrainSlice
  Bio::EnsEMBL::StrainSlice
69 70 71

=cut

72
package Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor;
73 74 75 76 77 78 79 80 81 82 83 84 85 86

use strict;
use warnings;
no warnings 'uninitialized';

use Bio::EnsEMBL::Utils::Argument qw(rearrange);
use Bio::EnsEMBL::Utils::Exception qw(throw warning);
use Bio::EnsEMBL::MappedSlice;
use Bio::EnsEMBL::Mapper;
use Bio::EnsEMBL::DBSQL::BaseAdaptor;

our @ISA = qw(Bio::EnsEMBL::DBSQL::BaseAdaptor);


87 88 89 90 91 92 93 94 95 96 97 98 99
=head2 new

  Example     : my $assembly_slice_adaptor =
                  Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor->new;
  Description : Constructor.
  Return type : Bio::EnsEMBL::DBSQL::AssemblySliceAdaptor
  Exceptions  : none
  Caller      : general
  Status      : At Risk
              : under development

=cut

100
sub new {
Patrick Meidl's avatar
Patrick Meidl committed
101
  my $caller = shift;
102 103 104 105 106 107 108 109

  my $class = ref($caller) || $caller;
  my $self = $class->SUPER::new(@_);
  
  return $self;
}


110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
=head2 fetch_by_version

  Arg[1]      : Bio::EnsEMBL::MappedSliceContainer $container - the container
                  to attach MappedSlices to
  Arg[2]      : String $version - the assembly version to fetch
  Example     : my ($mapped_slice) = @{ $msc->fetch_by_version('NCBIM36') };
  Description : Creates a MappedSlice representing an alternative assembly
                version of the container's reference slice.
  Return type : listref of Bio::EnsEMBL::MappedSlice
  Exceptions  : thrown on wrong or missing arguments
  Caller      : general, Bio::EnsEMBL::MappedSliceContainer
  Status      : At Risk
              : under development

=cut

Patrick Meidl's avatar
Patrick Meidl committed
126
sub fetch_by_version {
127 128 129 130 131 132 133 134 135 136 137 138 139 140
  my $self = shift;
  my $container = shift;
  my $version = shift;

  # arguement check
  unless ($container and ref($container) and
          $container->isa('Bio::EnsEMBL::MappedSliceContainer')) {
    throw("Need a MappedSliceContainer.");
  }

  unless ($version) {
    throw("Need an assembly version.");
  }

141
  my $slice = $container->ref_slice;
Patrick Meidl's avatar
Patrick Meidl committed
142

143 144
  # project slice onto other assembly and construct MappedSlice for result
  my $mapped_slice = Bio::EnsEMBL::MappedSlice->new(
Patrick Meidl's avatar
Patrick Meidl committed
145
      -ADAPTOR   => $self,
146
      -CONTAINER => $container,
147
      -NAME      => $slice->name."\#mapped_$version",
148 149 150 151 152 153 154 155
  );

  my $cs_name = $slice->coord_system_name;

  foreach my $seg (@{ $slice->project($cs_name, $version) }) {
  
    my $proj_slice = $seg->to_Slice;
    
156 157
    # create a Mapper to map to/from the mapped_slice artificial coord system
    my $mapper = Bio::EnsEMBL::Mapper->new('mapped_slice', 'native_slice');  
158 159
    
    # tell the mapper how to map this segment
Patrick Meidl's avatar
Patrick Meidl committed
160
    $mapper->add_map_coordinates(
161
        'mapped_slice',
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
        $seg->from_start,
        $seg->from_end,
        ($slice->strand * $proj_slice->strand),
        $proj_slice->seq_region_name,
        $proj_slice->start,
        $proj_slice->end
    );
    
    # add the Slice/Mapper pair to the MappedSlice
    $mapped_slice->add_Slice_Mapper_pair($proj_slice, $mapper);
  }

  return [$mapped_slice];
}


178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
=head2 fetch_by_name

  Arg[1]      : Bio::EnsEMBL::MappedSliceContainer $container - the container
                  to attach MappedSlices to
  Arg[2]      : String $name - the assembly name to fetch
  Arg[3]      : (optional) String $version -- the version for the new assembly
  Example     : my ($mapped_slice) = @{ $msc->fetch_by_name('LRG1','1') };
  Description : Creates a MappedSlice representing an alternative assembly
                version of the container's reference slice.
  Return type : listref of Bio::EnsEMBL::MappedSlice
  Exceptions  : thrown on wrong or missing arguments
  Caller      : general, Bio::EnsEMBL::MappedSliceContainer
  Status      : At Risk
              : under development

=cut

sub fetch_by_name {
  my $self = shift;
  my $container = shift;
  my $name = shift;
  my $version = shift; 

  # arguement check
  unless ($container and ref($container) and
          $container->isa('Bio::EnsEMBL::MappedSliceContainer')) {
    throw("Need a MappedSliceContainer.");
  }

  unless ($name) {
    throw("Need an assembly name.");
  }

  $version ||= '';
  my $slice = $container->ref_slice;

  # project slice onto other assembly and construct MappedSlice for result
  my $mapped_slice = Bio::EnsEMBL::MappedSlice->new(
      -ADAPTOR   => $self,
      -CONTAINER => $container,
      -NAME      => $slice->name."\#mapped_$name:$version",
  );


  foreach my $seg (@{ $slice->project($name, $version) }) {
  
    my $proj_slice = $seg->to_Slice;
    
    # create a Mapper to map to/from the mapped_slice artificial coord system
    my $mapper = Bio::EnsEMBL::Mapper->new('mapped_slice', 'native_slice');  
    
    # tell the mapper how to map this segment
    $mapper->add_map_coordinates(
        'mapped_slice',
        $seg->from_start,
        $seg->from_end,
        ($slice->strand * $proj_slice->strand),
        $proj_slice->seq_region_name,
        $proj_slice->start,
        $proj_slice->end
    );
    
    # add the Slice/Mapper pair to the MappedSlice
    $mapped_slice->add_Slice_Mapper_pair($proj_slice, $mapper);
  }

  return [$mapped_slice];
}


248 249
1;