EnsEMBL/LRGSlice.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 15 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 186 CC 17 SCAR 41.4
CC
17
Cov
0%
feature_Slice
110.0
get_all_differences
72.0
new
2.0
CRAP 62.7
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::LRGSlice - Arbitary Slice of a genome
34  
35 =head1 SYNOPSIS
36  
37 $sa = $db->get_SliceAdaptor;
38  
39 $slice =
40 $sa->fetch_by_region( 'LRG', 'LRG3');
41  
42 # get some attributes of the slice
43 my $seqname = $slice->seq_region_name();
44 my $start = $slice->start();
45 my $end = $slice->end();
46  
47 # get the sequence from the slice
48 my $seq = $slice->seq();
49  
50 # get some features from the slice
51 foreach $gene ( @{ $slice->get_all_Genes } ) {
52 # do something with a gene
53 }
54  
55 foreach my $feature ( @{ $slice->get_all_DnaAlignFeatures } ) {
56 # do something with dna-dna alignments
57 }
58  
59 =head1 DESCRIPTION
60  
61 A LRG Slice object represents a region of a genome. It can be used to retrieve
62 sequence or features from an area of interest.
63  
64 =head1 METHODS
65  
66 =cut
67  
68 package Bio::EnsEMBL::LRGSlice;
69 $Bio::EnsEMBL::LRGSlice::VERSION = '114.0.0_50'; # TRIAL
70 $Bio::EnsEMBL::LRGSlice::VERSION = '114.0.050';
71 use vars qw(@ISA);
72 use strict;
73  
74 use Bio::PrimarySeqI;
75  
76 use Bio::EnsEMBL::Slice;
77  
78 use vars qw(@ISA);
79  
80 @ISA = qw(Bio::EnsEMBL::Slice);
81  
82 sub new{
83 my $class = shift;
84  
85 my $self = bless {}, $class ;
86  
87 my $slice = $self = $class->SUPER::new( @_);
88  
89 return $self;
90 }
91  
92 sub stable_id {
93 my $self = shift;
94 return $self->seq_region_name;
95 }
96  
97  
98 sub display_xref {
99 my $self = shift;
100 return $self->seq_region_name;
101 }
102  
103 sub feature_Slice {
104 my $self = shift;
105 return $self->{_chrom_slice} if defined($self->{_chrom_slice});
106  
107 my $max;
108 my $min;
109 my $chrom;
110 my $strand;
111 my %segments;
112  
113 foreach my $segment (@{$self->project('chromosome')}) {
114 my $from_start = $segment->from_start();
115 my $from_end = $segment->from_end();
116 my $slice = $segment->to_Slice;
117
118 my $seq_region_id = $slice->adaptor->get_seq_region_id($slice);
119 my $to_name = $slice->seq_region_name();
120
121 next if (!$seq_region_id);
122
123 if (!$segments{$seq_region_id}) {
124 $segments{$seq_region_id}{'chr_name'} = $to_name;
125 $segments{$seq_region_id}{'strand'} = $slice->strand;
126 $max=-99999999999;
127 $min=9999999999;
128 } else {
129 $max = $segments{$seq_region_id}{'max'};
130 $min = $segments{$seq_region_id}{'min'};
131 }
132  
133 my $to_start = $slice->start();
134 my $to_end = $slice->end();
135 if($to_start > $max){
136 $max = $to_start;
137 }
138 if($to_start < $min){
139 $min = $to_start;
140 }
141 if($to_end > $max){
142 $max = $to_end;
143 }
144 if($to_end < $min){
145 $min = $to_end;
146 }
147
148 $segments{$seq_region_id}{'max'} = $max;
149 $segments{$seq_region_id}{'min'} = $min;
150 }
151
152 my @seq_region_ids = sort {$a <=> $b} keys (%segments);
153
154 # Keep the chromosome mapping instead of the patch, if possible
155 if (scalar @seq_region_ids) {
156 my $seq_id = $seq_region_ids[0];
157 $chrom = $segments{$seq_id}{'chr_name'};
158 $max = $segments{$seq_id}{'max'};
159 $min = $segments{$seq_id}{'min'};
160 $strand = $segments{$seq_id}{'strand'};
161 }
162  
163 if(!defined($chrom)){
164 warn "Could not project to chromosome for ".$self->name." ??\n";
165 return undef;
166 }
167 my $chrom_slice = $self->adaptor->fetch_by_region("chromosome",$chrom, $min, $max, $strand);
168 $self->{_chrom_slice} = $chrom_slice;
169 return $self->{_chrom_slice};
170 }
171  
172 sub DESTROY{
173 }
174  
175 sub get_all_differences{
176 my $self = shift;
177
178 my @results;
179
180 # get seq_region_attrib diffs (always same-length substitutions)
181 ################################################################
182
183 my $sth = $self->adaptor->prepare(qq{
184 SELECT sra.value
185 FROM seq_region_attrib sra, attrib_type at
186 WHERE at.code = '_rna_edit'
187 AND at.attrib_type_id = sra.attrib_type_id
188 AND sra.seq_region_id = ?
189 });
190
191 $sth->execute($self->get_seq_region_id);
192
193 my $edit_string;
194
195 $sth->bind_columns(\$edit_string);
196
197 while($sth->fetch()) {
198 my ($start, $end, $edit) = split " ", $edit_string;
199
200 my $slice = $self->sub_Slice($start, $end);
201 my $chr_proj = $slice->project("chromosome");
202 my $ref_seq = '-';
203 if(scalar @$chr_proj == 1) {
204 $ref_seq = $chr_proj->[0]->[2]->seq;
205 }
206
207
208 my $diff = {
209 'start' => $start,
210 'end' => $end,
211 'type' => 'substitution',
212 'seq' => $edit,
213 'ref' => $ref_seq,
214 };
215
216 push @results, $diff;
217 }
218
219 # get more complex differences via projections
220 ##############################################
221
222 # project the LRG slice to contig coordinates
223 my @segs = @{$self->project("contig")};
224
225 # if the LRG projects into more than one segment
226 if(scalar @segs > 1) {
227
228 my ($prev_end, $prev_chr_start, $prev_chr_end, $prev_was_chr);
229
230 foreach my $seg(@segs) {
231
232 # is this a novel LRG contig, or does it project to a chromosome?
233 my @chr_proj = @{$seg->[2]->project("chromosome")};
234
235 # if it is a normal contig
236 if(scalar @chr_proj) {
237
238 # check if there has been a deletion in LRG
239 if($prev_was_chr && $prev_end == $seg->[0] - 1) {
240
241 # check it's not just a break in contigs
242 unless(
243 ($chr_proj[0]->[2]->strand != $self->strand && $prev_chr_start == $chr_proj[0]->[2]->end + 1) ||
244 ($chr_proj[0]->[2]->strand != $self->strand && $prev_chr_end == $chr_proj[0]->[2]->start - 1)
245 ) {
246
247 # now get deleted slice coords, depends on the strand rel to LRG
248 my ($s, $e);
249
250 # opposite strand
251 if($chr_proj[0]->[2]->strand != $self->strand) {
252 ($s, $e) = ($prev_chr_start - 1, $chr_proj[0]->[2]->end + 1);
253 }
254
255 # same strand
256 else {
257 ($s, $e) = ($prev_chr_end + 1, $chr_proj[0]->[2]->start - 1);
258 }
259
260 if($s > $e) {
261 warn "Oops, trying to create a slice from $s to $e (could have been ", $prev_chr_start - 1, "-", $chr_proj[0]->[2]->end + 1, " or ", $prev_chr_end + 1, "-", $chr_proj[0]->[2]->start - 1, ")";
262 }
263
264 else {
265 # get a slice representing the sequence that was deleted
266 my $deleted_slice = $self->adaptor->fetch_by_region("chromosome", $chr_proj[0]->[2]->seq_region_name, $s, $e, $chr_proj[0]->[2]->strand);
267
268 my $diff = {
269 'start' => $seg->[0],
270 'end' => $prev_end,
271 'type' => 'deletion',
272 'seq' => '-',
273 'ref' => $deleted_slice->seq." ".$deleted_slice->start.'-'.$deleted_slice->end,
274 };
275
276 push @results, $diff;
277 }
278 }
279 }
280
281 $prev_was_chr = 1;
282
283 $prev_chr_start = $chr_proj[0]->[2]->start;
284 $prev_chr_end = $chr_proj[0]->[2]->end;
285 }
286
287 # if it is an LRG made-up contig for an insertion
288 else {
289 $prev_was_chr = 0;
290
291 my $diff = {
292 'start' => $seg->[0],
293 'end' => $seg->[1],
294 'type' => 'insertion',
295 'seq' => substr($self->seq, $seg->[0] - 1, $seg->[1] - $seg->[0] + 1),
296 'ref' => '-',
297 };
298
299 push @results, $diff;
300 }
301
302 $prev_end = $seg->[1];
303 }
304 }
305
306 # return results sorted by start, then end position
307 return [sort {$a->{start} <=> $b->{start} || $a->{end} <=> $b->{end}} @results];
308 }
309  
310 1;