| 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::Utils::AssemblyProjector - | ||
| 34 | utility class to post-process projections from one assembly to another | ||
| 35 | |||
| 36 | =head1 SYNOPSIS | ||
| 37 | |||
| 38 | # connect to an old database | ||
| 39 | my $dba_old = new Bio::EnsEMBL::DBSQL::DBAdaptor( | ||
| 40 | -host => 'ensembldb.ensembl.org', | ||
| 41 | -port => 3306, | ||
| 42 | -user => ensro, | ||
| 43 | -dbname => 'mus_musculus_core_46_36g', | ||
| 44 | -group => 'core_old', | ||
| 45 | ); | ||
| 46 | |||
| 47 | # connect to the new database containing the mapping between old and | ||
| 48 | # new assembly | ||
| 49 | my $dba_new = new Bio::EnsEMBL::DBSQL::DBAdaptor( | ||
| 50 | -host => 'ensembldb.ensembl.org', | ||
| 51 | -port => 3306, | ||
| 52 | -user => ensro, | ||
| 53 | -dbname => 'mus_musculus_core_47_37', | ||
| 54 | -group => 'core_new', | ||
| 55 | ); | ||
| 56 | |||
| 57 | my $assembly_projector = Bio::EnsEMBL::Utils::AssemblyProjector->new( | ||
| 58 | -OLD_ASSEMBLY => 'NCBIM36', | ||
| 59 | -NEW_ASSEMBLY => 'NCBIM37', | ||
| 60 | -ADAPTOR => $dba_new, | ||
| 61 | -EXTERNAL_SOURCE => 1, | ||
| 62 | -MERGE_FRAGMENTS => 1, | ||
| 63 | -CHECK_LENGTH => 0, | ||
| 64 | ); | ||
| 65 | |||
| 66 | # fetch a slice on the old assembly | ||
| 67 | my $slice_adaptor = $dba_old->get_SliceAdaptor; | ||
| 68 | my $slice = | ||
| 69 | $slice_adaptor->fetch_by_region( 'chromosome', 1, undef, undef, | ||
| 70 | undef, 'NCBIM36' ); | ||
| 71 | |||
| 72 | my $new_slice = $assembly_projector->old_to_new($slice); | ||
| 73 | |||
| 74 | print $new_slice->name, " (", $assembly_projector->last_status, ")\n"; | ||
| 75 | |||
| 76 | =head1 DESCRIPTION | ||
| 77 | |||
| 78 | This class implements some utility functions for converting coordinates | ||
| 79 | between assemblies. A mapping between the two assemblies has to present | ||
| 80 | the database for this to work, see the 'Related Modules' section below | ||
| 81 | on how to generate the mapping. | ||
| 82 | |||
| 83 | In addition to the "raw" projecting of features and slices, the methods | ||
| 84 | in this module also apply some sensible rules to the results of the | ||
| 85 | projection (like discarding unwanted results or merging fragmented | ||
| 86 | projections). These are the rules (depending on configuration): | ||
| 87 | |||
| 88 | Discard the projected feature/slice if: | ||
| 89 | |||
| 90 | 1. it doesn't project at all (no segments returned) | ||
| 91 | 2. [unless MERGE_FRAGMENTS is set] the projection is fragmented (more | ||
| 92 | than one segment) | ||
| 93 | 3. [if CHECK_LENGTH is set] the projection doesn't have the same | ||
| 94 | length as the original feature/slice | ||
| 95 | 4. all segments are on same chromosome and strand | ||
| 96 | |||
| 97 | If a projection fails any of these rules, undef is returned instead of | ||
| 98 | a projected feature/slice. You can use the last_status() method to find | ||
| 99 | out about the results of the rules tests. | ||
| 100 | |||
| 101 | Also note that when projecting features, only a shallow projection is | ||
| 102 | performed, i.e. other features attached to your features (e.g. the | ||
| 103 | transcripts of a gene) are not projected automatically, so it will be | ||
| 104 | the responsability of the user code project all levels of features | ||
| 105 | involved. | ||
| 106 | |||
| 107 | =head1 METHODS | ||
| 108 | |||
| 109 | new | ||
| 110 | project | ||
| 111 | old_to_new | ||
| 112 | new_to_old | ||
| 113 | adaptor | ||
| 114 | external_source | ||
| 115 | old_assembly | ||
| 116 | new_assembly | ||
| 117 | merge_fragments | ||
| 118 | check_length | ||
| 119 | |||
| 120 | =head1 RELATED MODULES | ||
| 121 | |||
| 122 | The process of creating a whole genome alignment between two assemblies | ||
| 123 | (which is the basis for the use of the methods in this class) is done by | ||
| 124 | a series of scripts. Please see | ||
| 125 | |||
| 126 | ensembl/misc-scripts/assembly/README | ||
| 127 | |||
| 128 | for a high-level description of this process, and POD in the individual | ||
| 129 | scripts for the details. | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | package Bio::EnsEMBL::Utils::AssemblyProjector; | ||
| 134 | $Bio::EnsEMBL::Utils::AssemblyProjector::VERSION = '114.0.0_50'; # TRIAL | ||
| 135 | $Bio::EnsEMBL::Utils::AssemblyProjector::VERSION = '114.0.050'; | ||
| 136 | use strict; | ||
| 137 | use warnings; | ||
| 138 | no warnings qw(uninitialized); | ||
| 139 | |||
| 140 | use Bio::EnsEMBL::Utils::Exception qw(throw warning); | ||
| 141 | use Bio::EnsEMBL::Utils::Argument qw(rearrange); | ||
| 142 | use Bio::EnsEMBL::Slice; | ||
| 143 | use Scalar::Util qw(weaken); | ||
| 144 | |||
| 145 | =head2 new | ||
| 146 | |||
| 147 | Arg [ADAPTOR] : Bio::EnsEMBL::DBSQL::DBAdaptor $adaptor - a db adaptor | ||
| 148 | for a database containing the assembly mapping | ||
| 149 | Arg [EXTERNAL_SOURCE] : (optional) Boolean $external_source - indicates if | ||
| 150 | source is from a different database | ||
| 151 | Arg [OLD_ASSEMBLY] : name of the old assembly | ||
| 152 | Arg [OLD_ASSEMBLY] : name of the new assembly | ||
| 153 | Arg [OBJECT_TYPE] : (optional) object type ('slice' or 'feature') | ||
| 154 | Arg [MERGE_FRAGMENTS] : (optional) Boolean - determines if segments are merged | ||
| 155 | to return a single object spanning all segments | ||
| 156 | (default: true) | ||
| 157 | Arg [CHECK_LENGTH] : (optional) Boolean - determines if projected objects | ||
| 158 | have to have same length as original (default: false) | ||
| 159 | Example : my $ap = Bio::EnsEMBL::Utils::AssemblyProjector->new( | ||
| 160 | -DBADAPTOR => $dba, | ||
| 161 | -OLD_ASSEMBLY => NCBIM36, | ||
| 162 | -NEW_ASSEMBLY => NCBIM37, | ||
| 163 | ); | ||
| 164 | Description : Constructor. | ||
| 165 | Return type : a Bio::EnsEMBL::Utils::AssemblyProjector object | ||
| 166 | Exceptions : thrown on missing arguments | ||
| 167 | thrown on invalid OBJECT_TYPE | ||
| 168 | Caller : general | ||
| 169 | Status : At Risk | ||
| 170 | : under development | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | sub new { | ||
| 175 | my $caller = shift; | ||
| 176 | my $class = ref($caller) || $caller; | ||
| 177 | |||
| 178 | my ($adaptor, $external_source, $old_assembly, $new_assembly, | ||
| 179 | $merge_fragments, $check_length) = rearrange([qw(ADAPTOR EXTERNAL_SOURCE | ||
| 180 | OLD_ASSEMBLY NEW_ASSEMBLY MERGE_FRAGMENTS CHECK_LENGTH)], @_); | ||
| 181 | |||
| 182 | unless ($adaptor and ref($adaptor) and | ||
| 183 | $adaptor->isa('Bio::EnsEMBL::DBSQL::DBAdaptor')) { | ||
| 184 | throw("You must provide a DBAdaptor to a database containing the assembly mapping."); | ||
| 185 | } | ||
| 186 | |||
| 187 | unless ($old_assembly and $new_assembly) { | ||
| 188 | throw("You must provide an old and new assembly name."); | ||
| 189 | } | ||
| 190 | |||
| 191 | my $self = {}; | ||
| 192 | bless ($self, $class); | ||
| 193 | |||
| 194 | # initialise | ||
| 195 | $self->adaptor($adaptor); | ||
| 196 | $self->{'old_assembly'} = $old_assembly; | ||
| 197 | $self->{'new_assembly'} = $new_assembly; | ||
| 198 | |||
| 199 | # by default, merge fragments | ||
| 200 | $self->{'merge_fragments'} = $merge_fragments || 1; | ||
| 201 | |||
| 202 | # by default, do not check length | ||
| 203 | $self->{'check_length'} = $check_length || 0; | ||
| 204 | |||
| 205 | # by default, features and slices are expected in same database as the | ||
| 206 | # assembly mapping | ||
| 207 | $self->{'external_source'} = $external_source || 0; | ||
| 208 | |||
| 209 | return $self; | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | =head2 project | ||
| 214 | |||
| 215 | Arg[1] : Bio::EnsEMBL::Slice or Bio::EnsEMBL::Feature $object - | ||
| 216 | the object to project | ||
| 217 | Arg[2] : String $to_assembly - assembly to project to | ||
| 218 | Example : my $new_slice = $assembly_projector->project($old_slice, | ||
| 219 | 'NCBIM37'); | ||
| 220 | Description : Projects a Slice or Feature to the specified assembly. | ||
| 221 | |||
| 222 | Several tests are performed on the result to discard unwanted | ||
| 223 | results. All projection segments have to be on the same | ||
| 224 | seq_region and strand. If -MERGE_FRAGMENTS is set, gaps will be | ||
| 225 | bridged by creating a single object from first_segment_start to | ||
| 226 | last_segment_end. If -CHECK_LENGTH is set, the projected object | ||
| 227 | will have to have the same length as the original. You can use | ||
| 228 | the last_status() method to find out what the result of some of | ||
| 229 | these rule tests were. Please see the comments in the code for | ||
| 230 | more details about these rules. | ||
| 231 | |||
| 232 | The return value of this method will always be a single object, | ||
| 233 | or undef if the projection fails any of the rules. | ||
| 234 | |||
| 235 | Note that when projecting features, only a "shallow" projection | ||
| 236 | is performed, i.e. attached features aren't projected | ||
| 237 | automatically! (e.g. if you project a gene, its transcripts will | ||
| 238 | have to be projected manually before storing the new gene) | ||
| 239 | Return type : same a Arg 1, or undef if projection fails any of the rules | ||
| 240 | Exceptions : thrown on invalid arguments | ||
| 241 | Caller : general, $self->old_to_new, $self->new_to_old | ||
| 242 | Status : At Risk | ||
| 243 | : under development | ||
| 244 | |||
| 245 | =cut | ||
| 246 | |||
| 247 | sub project { | ||
| 248 | my ($self, $object, $to_assembly) = @_; | ||
| 249 | |||
| 250 | throw("Need an assembly version to project to.") unless ($to_assembly); | ||
| 251 | throw("Need an object to project.") unless ($object and ref($object)); | ||
| 252 | |||
| 253 | my ($slice, $object_type); | ||
| 254 | |||
| 255 | if ($object->isa('Bio::EnsEMBL::Feature')) { | ||
| 256 | $object_type = 'feature'; | ||
| 257 | } elsif ($object->isa('Bio::EnsEMBL::Slice')) { | ||
| 258 | $object_type = 'slice'; | ||
| 259 | } else { | ||
| 260 | throw("Need a Feature or Slice to project."); | ||
| 261 | } | ||
| 262 | |||
| 263 | # if the feature or slice is sourced from another db, we have to "transfer" | ||
| 264 | # it to the db that contains the assembly mapping. the transfer is very | ||
| 265 | # shallow but that should do for our purposes | ||
| 266 | if ($self->external_source) { | ||
| 267 | my $slice_adaptor = $self->adaptor->get_SliceAdaptor; | ||
| 268 | |||
| 269 | if ($object_type eq 'feature') { | ||
| 270 | |||
| 271 | # createa a new slice from the target db | ||
| 272 | my $f_slice = $object->slice; | ||
| 273 | my $target_slice = $slice_adaptor->fetch_by_name($f_slice->name); | ||
| 274 | |||
| 275 | # now change the feature so that it appears it's from the target db | ||
| 276 | $object->slice($target_slice); | ||
| 277 | |||
| 278 | } else { | ||
| 279 | |||
| 280 | # createa a new slice from the target db | ||
| 281 | $object = $slice_adaptor->fetch_by_name($object->name); | ||
| 282 | |||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | if ($object_type eq 'feature') { | ||
| 287 | $slice = $object->feature_Slice; | ||
| 288 | } else { | ||
| 289 | $slice = $object; | ||
| 290 | } | ||
| 291 | |||
| 292 | # warn if trying to project to assembly version the object already is on | ||
| 293 | if ($slice->coord_system->version eq $to_assembly) { | ||
| 294 | warning("Assembly version to project to ($to_assembly) is the same as your object's assembly (".$slice->coord_system->version.")."); | ||
| 295 | } | ||
| 296 | |||
| 297 | # now project the slice | ||
| 298 | my $cs_name = $slice->coord_system_name; | ||
| 299 | my @segments = @{ $slice->project($cs_name, $to_assembly) }; | ||
| 300 | |||
| 301 | # we need to reverse the projection segment list if the orignial | ||
| 302 | if ($slice->strand == -1) { | ||
| 303 | @segments = reverse(@segments); | ||
| 304 | } | ||
| 305 | |||
| 306 | # apply rules to projection results | ||
| 307 | # | ||
| 308 | # discard the projected feature/slice if | ||
| 309 | # 1. it doesn't project at all (no segments returned) | ||
| 310 | # 2. [unless MERGE_FRAGMENTS is set] the projection is fragmented (more | ||
| 311 | # than one segment) | ||
| 312 | # 3. [if CHECK_LENGTH is set] the projection doesn't have the same length | ||
| 313 | # as the original feature/slice | ||
| 314 | # 4. all segments are on same chromosome and strand | ||
| 315 | |||
| 316 | # keep track of the status of applied rules | ||
| 317 | my @status = (); | ||
| 318 | |||
| 319 | # test (1) | ||
| 320 | return undef unless (@segments); | ||
| 321 | #warn "DEBUG: passed test 1\n"; | ||
| 322 | |||
| 323 | # test (2) | ||
| 324 | return undef if (!($self->merge_fragments) and scalar(@segments) > 1); | ||
| 325 | push @status, 'fragmented' if (scalar(@segments) > 1); | ||
| 326 | #warn "DEBUG: passed test 2\n"; | ||
| 327 | |||
| 328 | # test (3) | ||
| 329 | my $first_slice = $segments[0]->to_Slice; | ||
| 330 | my $last_slice = $segments[-1]->to_Slice; | ||
| 331 | my $length_mismatch = (($last_slice->end - $first_slice->start + 1) != | ||
| 332 | $object->length); | ||
| 333 | return undef if ($self->check_length and $length_mismatch); | ||
| 334 | push @status, 'length_mismatch' if ($length_mismatch); | ||
| 335 | #warn "DEBUG: passed test 3\n"; | ||
| 336 | |||
| 337 | # test (4) | ||
| 338 | my %sr_names = (); | ||
| 339 | my %strands = (); | ||
| 340 | foreach my $seg (@segments) { | ||
| 341 | my $sl = $seg->to_Slice; | ||
| 342 | $sr_names{$sl->seq_region_name}++; | ||
| 343 | $strands{$sl->strand}++; | ||
| 344 | } | ||
| 345 | return undef if (scalar(keys %sr_names) > 1 or scalar(keys %strands) > 1); | ||
| 346 | #warn "DEBUG: passed test 4\n"; | ||
| 347 | |||
| 348 | # remember rule status | ||
| 349 | $self->last_status(join('|', @status)); | ||
| 350 | |||
| 351 | # everything looks fine, so adjust the coords of your feature/slice | ||
| 352 | my $new_slice = $first_slice; | ||
| 353 | $new_slice->{'end'} = $last_slice->end; | ||
| 354 | |||
| 355 | if ($object_type eq 'slice') { | ||
| 356 | return $new_slice; | ||
| 357 | } else { | ||
| 358 | |||
| 359 | $object->start($new_slice->start); | ||
| 360 | $object->end($new_slice->end); | ||
| 361 | $object->strand($new_slice->strand); | ||
| 362 | $object->slice($new_slice->seq_region_Slice); | ||
| 363 | |||
| 364 | # undef dbID and adaptor so you can store the feature in the target db | ||
| 365 | $object->dbID(undef); | ||
| 366 | $object->adaptor(undef); | ||
| 367 | |||
| 368 | return $object; | ||
| 369 | } | ||
| 370 | |||
| 371 | } | ||
| 372 | |||
| 373 | |||
| 374 | =head2 old_to_new | ||
| 375 | |||
| 376 | Arg[1] : Bio::EnsEMBL::Slice or Bio::EnsEMBL::Feature $object - | ||
| 377 | the object to project | ||
| 378 | Example : my $new_slice = $assembly_projector->old_to_new($old_slice); | ||
| 379 | Description : Projects a Slice or Feature from old to new assembly. | ||
| 380 | This method is just a convenience wrapper for $self->project. | ||
| 381 | Return type : same a Arg 1, or undef | ||
| 382 | Exceptions : none | ||
| 383 | Caller : general | ||
| 384 | Status : At Risk | ||
| 385 | : under development | ||
| 386 | |||
| 387 | =cut | ||
| 388 | |||
| 389 | sub old_to_new { | ||
| 390 | my ($self, $object) = @_; | ||
| 391 | return $self->project($object, $self->new_assembly); | ||
| 392 | } | ||
| 393 | |||
| 394 | |||
| 395 | =head2 new_to_old | ||
| 396 | |||
| 397 | Arg[1] : Bio::EnsEMBL::Slice or Bio::EnsEMBL::Feature $object - | ||
| 398 | the object to project | ||
| 399 | Example : my $old_slice = $assembly_projector->new_to_old($new_slice, 1); | ||
| 400 | Description : Projects a Slice or Feature from new to old assembly. | ||
| 401 | This method is just a convenience wrapper for $self->project. | ||
| 402 | Return type : same a Arg 1, or undef | ||
| 403 | Exceptions : none | ||
| 404 | Caller : general | ||
| 405 | Status : At Risk | ||
| 406 | : under development | ||
| 407 | |||
| 408 | =cut | ||
| 409 | |||
| 410 | sub new_to_old { | ||
| 411 | my ($self, $object) = @_; | ||
| 412 | return $self->project($object, $self->old_assembly); | ||
| 413 | } | ||
| 414 | |||
| 415 | |||
| 416 | # | ||
| 417 | # accessors | ||
| 418 | # | ||
| 419 | sub adaptor { | ||
| 420 | my $self = shift; | ||
| 421 | weaken($self->{'adaptor'} = shift) if (@_); | ||
| 422 | return $self->{'adaptor'}; | ||
| 423 | } | ||
| 424 | |||
| 425 | |||
| 426 | sub external_source { | ||
| 427 | my $self = shift; | ||
| 428 | $self->{'external_source'} = shift if (@_); | ||
| 429 | return $self->{'external_source'}; | ||
| 430 | } | ||
| 431 | |||
| 432 | |||
| 433 | sub old_assembly { | ||
| 434 | my $self = shift; | ||
| 435 | $self->{'old_assembly'} = shift if (@_); | ||
| 436 | return $self->{'old_assembly'}; | ||
| 437 | } | ||
| 438 | |||
| 439 | |||
| 440 | sub new_assembly { | ||
| 441 | my $self = shift; | ||
| 442 | $self->{'new_assembly'} = shift if (@_); | ||
| 443 | return $self->{'new_assembly'}; | ||
| 444 | } | ||
| 445 | |||
| 446 | |||
| 447 | sub merge_fragments { | ||
| 448 | my $self = shift; | ||
| 449 | $self->{'merge_fragments'} = shift if (@_); | ||
| 450 | return $self->{'merge_fragments'}; | ||
| 451 | } | ||
| 452 | |||
| 453 | |||
| 454 | sub check_length { | ||
| 455 | my $self = shift; | ||
| 456 | $self->{'check_length'} = shift if (@_); | ||
| 457 | return $self->{'check_length'}; | ||
| 458 | } | ||
| 459 | |||
| 460 | |||
| 461 | sub last_status { | ||
| 462 | my $self = shift; | ||
| 463 | $self->{'last_status'} = shift if (@_); | ||
| 464 | return $self->{'last_status'}; | ||
| 465 | } | ||
| 466 | |||
| 467 | |||
| 468 | 1; | ||
| 469 | |||
| 470 |