| 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::RepeatMaskedSlice - Arbitary Slice of a genome | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | $sa = $db->get_SliceAdaptor(); | ||
| 38 | |||
| 39 | $slice = | ||
| 40 | $sa->fetch_by_region( 'chromosome', 'X', 1_000_000, 2_000_000 ); | ||
| 41 | |||
| 42 | $repeat_masked_slice = $slice->get_repeatmasked_seq(); | ||
| 43 | |||
| 44 | # get repeat masked sequence: | ||
| 45 | my $dna = $repeat_masked_slice->seq(); | ||
| 46 | $dna = $repeat_masked_slice->subseq( 1, 1000 ); | ||
| 47 | |||
| 48 | =head1 DESCRIPTION | ||
| 49 | |||
| 50 | This is a specialised Bio::EnsEMBL::Slice class that is used to retrieve | ||
| 51 | repeat masked genomic sequence rather than normal genomic sequence. | ||
| 52 | |||
| 53 | =head1 METHODS | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | package Bio::EnsEMBL::RepeatMaskedSlice; | ||
| 58 | $Bio::EnsEMBL::RepeatMaskedSlice::VERSION = '114.0.0_50'; # TRIAL | ||
| 59 | $Bio::EnsEMBL::RepeatMaskedSlice::VERSION = '114.0.050'; | ||
| 60 | use strict; | ||
| 61 | use warnings; | ||
| 62 | |||
| 63 | use Bio::EnsEMBL::Slice; | ||
| 64 | use Bio::EnsEMBL::Utils::Argument qw(rearrange); | ||
| 65 | use Bio::EnsEMBL::Utils::Sequence qw(reverse_comp); | ||
| 66 | use Bio::EnsEMBL::Utils::Exception; | ||
| 67 | |||
| 68 | use vars qw(@ISA); | ||
| 69 | |||
| 70 | @ISA = ('Bio::EnsEMBL::Slice'); | ||
| 71 | |||
| 72 | # The BLOCK_PWR is the lob_bin of the chunksize where you want your repeat features | ||
| 73 | # to be retrieved. This will create repeat feature retrieval calls that are likely | ||
| 74 | # to be on the same slice and hopefully create cache hits and less database traffic | ||
| 75 | my $BLOCK_PWR = 18; | ||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | =head2 new | ||
| 80 | |||
| 81 | Arg [-REPEAT_MASK] : The logic name of the repeats to be used for masking. | ||
| 82 | If not provided, all repeats in the database are used. | ||
| 83 | Arg [...] : Named superclass arguments. See B<Bio::EnsEMBL::Slice>. | ||
| 84 | Example : my $slice = Bio::EnsEMBL::RepeatMaskedSlice->new | ||
| 85 | (-START => $start, | ||
| 86 | -END => $end, | ||
| 87 | -STRAND => $strand, | ||
| 88 | -SEQ_REGION_NAME => $seq_region, | ||
| 89 | -SEQ_REGION_LENGTH => $seq_region_length, | ||
| 90 | -COORD_SYSTEM => $cs, | ||
| 91 | -ADAPTOR => $adaptor, | ||
| 92 | -REPEAT_MASK => ['repeat_masker'], | ||
| 93 | -SOFT_MASK => 1, | ||
| 94 | -NOT_DEFAULT_MASKING_CASES => {"repeat_class_SINE/MIR" => 1, | ||
| 95 | "repeat_name_AluSp" => 0}); | ||
| 96 | Description: Creates a Slice which behaves exactly as a normal slice but | ||
| 97 | that returns repeat masked sequence from the seq method. | ||
| 98 | Returntype : Bio::EnsEMBL::RepeatMaskedSlice | ||
| 99 | Exceptions : none | ||
| 100 | Caller : RawComputes (PredictionTranscript creation code). | ||
| 101 | Status : Stable | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub new { | ||
| 106 | my $caller = shift; | ||
| 107 | my $class = ref($caller) || $caller; | ||
| 108 | |||
| 109 | my ($logic_names, $soft_mask, $not_default_masking_cases) = rearrange(['REPEAT_MASK', | ||
| 110 | 'SOFT_MASK', | ||
| 111 | 'NOT_DEFAULT_MASKING_CASES'], @_); | ||
| 112 | |||
| 113 | my $self = $class->SUPER::new(@_); | ||
| 114 | |||
| 115 | |||
| 116 | $logic_names ||= ['']; | ||
| 117 | if(ref($logic_names) ne 'ARRAY') { | ||
| 118 | throw("Reference to list of logic names argument expected."); | ||
| 119 | } | ||
| 120 | |||
| 121 | $self->{'repeat_mask_logic_names'} = $logic_names; | ||
| 122 | $self->{'soft_mask'} = $soft_mask; | ||
| 123 | $self->{'not_default_masking_cases'} = $not_default_masking_cases; | ||
| 124 | $self->{'not_default_masking_cases'} ||= {}; | ||
| 125 | |||
| 126 | return $self; | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | =head2 repeat_mask_logic_names | ||
| 131 | |||
| 132 | Arg [1] : reference to list of strings $logic_names (optional) | ||
| 133 | Example : $rm_slice->repeat_mask_logic_name(['repeat_masker']); | ||
| 134 | Description: Getter/Setter for the logic_names of the repeats that are used | ||
| 135 | to mask this slices sequence. | ||
| 136 | Returntype : reference to list of strings | ||
| 137 | Exceptions : none | ||
| 138 | Caller : seq() method | ||
| 139 | Status : Stable | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub repeat_mask_logic_names { | ||
| 144 | my $self = shift; | ||
| 145 | |||
| 146 | if(@_) { | ||
| 147 | my $array = shift; | ||
| 148 | if(ref($array) ne 'ARRAY') { | ||
| 149 | throw('Reference to list of logic names argument expected.'); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | return $self->{'repeat_mask_logic_names'}; | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | =head2 soft_mask | ||
| 158 | |||
| 159 | Arg [1] : boolean $soft_mask (optional) | ||
| 160 | Example : $rm_slice->soft_mask(0); | ||
| 161 | Description: Getter/Setter which is used to turn on/off softmasking of the | ||
| 162 | sequence returned by seq. | ||
| 163 | Returntype : boolean | ||
| 164 | Exceptions : none | ||
| 165 | Caller : seq() method | ||
| 166 | Status : Stable | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | sub soft_mask { | ||
| 171 | my $self = shift; | ||
| 172 | $self->{'soft_mask'} = shift if(@_); | ||
| 173 | return $self->{'soft_mask'} || 0; | ||
| 174 | } | ||
| 175 | |||
| 176 | =head2 not_default_masking_cases | ||
| 177 | |||
| 178 | Arg [1] : hash reference $not_default_masking_cases (optional, default is {}) | ||
| 179 | The values are 0 or 1 for hard and soft masking respectively | ||
| 180 | The keys of the hash should be of 2 forms | ||
| 181 | "repeat_class_" . $repeat_consensus->repeat_class, | ||
| 182 | e.g. "repeat_class_SINE/MIR" | ||
| 183 | "repeat_name_" . $repeat_consensus->name | ||
| 184 | e.g. "repeat_name_MIR" | ||
| 185 | depending on which base you want to apply the not default masking either | ||
| 186 | the repeat_class or repeat_name. Both can be specified in the same hash | ||
| 187 | at the same time, but in that case, repeat_name setting has priority over | ||
| 188 | repeat_class. For example, you may have hard masking as default, and | ||
| 189 | you may want soft masking of all repeat_class SINE/MIR, | ||
| 190 | but repeat_name AluSp (which are also from repeat_class SINE/MIR) | ||
| 191 | Example : $rm_slice->not_default_masking_cases({"repeat_class_SINE/MIR" => 1, | ||
| 192 | "repeat_name_AluSp" => 0}); | ||
| 193 | Description: Getter/Setter which is used to escape some repeat class or name from the default | ||
| 194 | masking in place. | ||
| 195 | Returntype : hash reference | ||
| 196 | Exceptions : none | ||
| 197 | Caller : seq() and subseq() methods | ||
| 198 | Status : Stable | ||
| 199 | |||
| 200 | =cut | ||
| 201 | |||
| 202 | sub not_default_masking_cases { | ||
| 203 | my $self = shift; | ||
| 204 | $self->{'not_default_masking_cases'} = shift if (@_); | ||
| 205 | return $self->{'not_default_masking_cases'}; | ||
| 206 | } | ||
| 207 | |||
| 208 | =head2 seq | ||
| 209 | |||
| 210 | Arg [1] : none | ||
| 211 | Example : print $rmslice->seq(), "\n"; | ||
| 212 | Description: Retrieves the entire repeat masked sequence for this slice. | ||
| 213 | See also the B<Bio::EnsEMBL::Slice> implementation of this | ||
| 214 | method. | ||
| 215 | Returntype : string | ||
| 216 | Exceptions : none | ||
| 217 | Caller : general | ||
| 218 | Status : Stable | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub seq { | ||
| 223 | my $self = shift; | ||
| 224 | |||
| 225 | # | ||
| 226 | # get all the features | ||
| 227 | # | ||
| 228 | my $repeats = $self->_get_repeat_features($self); | ||
| 229 | my $soft_mask = $self->soft_mask(); | ||
| 230 | my $not_default_masking_cases = $self->not_default_masking_cases(); | ||
| 231 | |||
| 232 | # | ||
| 233 | # get the dna | ||
| 234 | # | ||
| 235 | my $dna = $self->SUPER::seq(@_); | ||
| 236 | |||
| 237 | # | ||
| 238 | # mask the dna | ||
| 239 | # | ||
| 240 | $self->_mask_features(\$dna,$repeats,$soft_mask,$not_default_masking_cases); | ||
| 241 | return $dna; | ||
| 242 | } | ||
| 243 | |||
| 244 | =head2 subseq | ||
| 245 | |||
| 246 | Arg [1] : none | ||
| 247 | Example : print $rmslice->subseq(1, 1000); | ||
| 248 | Description: Retrieves a repeat masked sequence from a specified subregion | ||
| 249 | of this slice. See also the B<Bio::EnsEMBL::Slice> | ||
| 250 | implementation of this method. | ||
| 251 | Returntype : string | ||
| 252 | Exceptions : none | ||
| 253 | Caller : general | ||
| 254 | Status : Stable | ||
| 255 | |||
| 256 | =cut | ||
| 257 | |||
| 258 | sub subseq { | ||
| 259 | my $self = shift; | ||
| 260 | my $start = shift; | ||
| 261 | my $end = shift; | ||
| 262 | my $strand = shift; | ||
| 263 | |||
| 264 | my $subsequence_slice = $self->sub_Slice($start, $end, $strand); | ||
| 265 | |||
| 266 | # If frequent subseqs happen on repeatMasked sequence this results in | ||
| 267 | # a lot of feature retrieval from the database. To avoid this, features | ||
| 268 | # are only retrieved from subslices with fixed space boundaries. | ||
| 269 | # The access happens in block to make cache hits more likely | ||
| 270 | # ONLY DO IF WE ARE CACHING | ||
| 271 | |||
| 272 | my $subslice; | ||
| 273 | if(! $self->adaptor()->db()->no_cache()) { | ||
| 274 | |||
| 275 | my $seq_region_slice = $self->seq_region_Slice(); | ||
| 276 | # The blocksize can be defined on the top of this module. | ||
| 277 | my $block_min = ($subsequence_slice->start()-1) >> $BLOCK_PWR; | ||
| 278 | my $block_max = ($subsequence_slice->end()-1) >> $BLOCK_PWR; | ||
| 279 | |||
| 280 | my $sub_start = ($block_min << $BLOCK_PWR)+1; | ||
| 281 | my $sub_end = ($block_max+1)<<$BLOCK_PWR; | ||
| 282 | if ($sub_end > $seq_region_slice->length) { | ||
| 283 | $sub_end = $seq_region_slice->length ; | ||
| 284 | } | ||
| 285 | $subslice = $seq_region_slice->sub_Slice($sub_start, $sub_end); | ||
| 286 | } | ||
| 287 | else { | ||
| 288 | $subslice = $subsequence_slice; | ||
| 289 | } | ||
| 290 | |||
| 291 | my $repeats = $self->_get_repeat_features($subslice); | ||
| 292 | my $soft_mask = $self->soft_mask(); | ||
| 293 | my $not_default_masking_cases = $self->not_default_masking_cases(); | ||
| 294 | my $dna = $subsequence_slice->SUPER::seq(); | ||
| 295 | $subsequence_slice->_mask_features(\$dna,$repeats,$soft_mask,$not_default_masking_cases); | ||
| 296 | return $dna; | ||
| 297 | } | ||
| 298 | |||
| 299 | =head2 _get_repeat_features | ||
| 300 | |||
| 301 | Args [1] : Bio::EnsEMBL::Slice to fetch features for | ||
| 302 | Description : Gets repeat features for the given slice | ||
| 303 | Returntype : ArrayRef[Bio::EnsEMBL::RepeatFeature] array of repeats | ||
| 304 | |||
| 305 | =cut | ||
| 306 | |||
| 307 | |||
| 308 | |||
| 309 | sub _get_repeat_features { | ||
| 310 | my ($self, $slice) = @_; | ||
| 311 | my $logic_names = $self->repeat_mask_logic_names(); | ||
| 312 | my @repeats; | ||
| 313 | foreach my $l (@$logic_names) { | ||
| 314 | push @repeats, @{$slice->get_all_RepeatFeatures($l)}; | ||
| 315 | } | ||
| 316 | return \@repeats; | ||
| 317 | } | ||
| 318 | |||
| 319 | 1; |