EnsEMBL/Analysis/PairAlign.pm untested

filter: Statementstmt 0.0% 0 / 134 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 45 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 251 CC 25 SCAR 38.0
CC
25
Cov
0%
convert_cDNA_feature
90.0
genomic2cDNA
56.0
cDNA2genomic
56.0
CRAP 44.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 PairAlign - Dna pairwise alignment module
34  
35 =head1 SYNOPSIS
36  
37 #To convert between coordinates:
38  
39 my $cdna_coord = $pair->genomic2cDNA($gen_coord);
40 my $gen_coord = $pair->cDNA2genomic($cdna_coord);
41  
42 =head1 DESCRIPTION
43  
44 Contains list of sub alignments making up a dna-dna alignment
45  
46 Creation:
47  
48 my $pair = new Bio::EnsEMBL::FeaturePair(
49 -start => $qstart,
50 -end => $qend,
51 -strand => $qstrand,
52 -hstart => $hstart,
53 -hend => $hend,
54 -hend => $hstrand,
55 );
56  
57 my $pairaln = new Bio::EnsEMBL::Analysis::PairAlign;
58 $pairaln->addFeaturePair($pair);
59  
60 Any number of pair alignments can be added to the PairAlign object
61  
62 =cut
63  
64 package Bio::EnsEMBL::Analysis::PairAlign;
65 $Bio::EnsEMBL::Analysis::PairAlign::VERSION = '114.0.0_50'; # TRIAL
66 $Bio::EnsEMBL::Analysis::PairAlign::VERSION = '114.0.050';
67 use vars qw(@ISA);
68 use strict;
69  
70  
71 sub new {
72 my($class,@args) = @_;
73 my $self = {};
74 bless $self, $class;
75  
76 $self->{'_homol'} = [];
77
78 return $self; # success - we hope!
79 }
80  
81 sub addFeaturePair {
82 my ($self,$pair) = @_;
83  
84 $self->throw("Not a Bio::EnsEMBL::FeaturePair object") unless ($pair->isa("Bio::EnsEMBL::FeaturePair"));
85  
86 push(@{$self->{'_pairs'}},$pair);
87
88 }
89  
90  
91 =head2 eachFeaturePair
92  
93 Title : eachFeaturePair
94 Example : my @pairs = $pair->eachFeaturePair
95 Returns : Array of Bio::SeqFeature::FeaturePair
96 Args : none
97 =cut
98  
99 sub eachFeaturePair {
100 my ($self) = @_;
101  
102 if (defined($self->{'_pairs'})) {
103 return @{$self->{'_pairs'}};
104 }
105 }
106  
107 sub get_hstrand {
108 my ($self) = @_;
109  
110 my @features = $self->eachFeaturePair;
111  
112 return $features[0]->hstrand;
113 }
114  
115 =head2 genomic2cDNA
116  
117 Title : genomic2cDNA
118 Usage : my $cdna_coord = $pair->genomic2cDNA($gen_coord)
119 Function: Converts a genomic coordinate to a cdna coordinate
120 Returns : int
121 Args : int
122 =cut
123  
124 sub genomic2cDNA {
125 my ($self,$coord) = @_;
126 my @pairs = $self->eachFeaturePair;
127
128 @pairs = sort {$a->start <=> $b->start} @pairs;
129
130 my $newcoord;
131
132 HOMOL: while (my $sf1 = shift(@pairs)) {
133 next HOMOL unless ($coord >= $sf1->start && $coord <= $sf1->end);
134
135 if ($sf1->strand == 1 && $sf1->hstrand == 1) {
136 $newcoord = $sf1->hstart + ($coord - $sf1->start);
137 last HOMOL;
138 } elsif ($sf1->strand == 1 && $sf1->hstrand == -1) {
139 $newcoord = $sf1->hend - ($coord - $sf1->start);
140 last HOMOL;
141 } elsif ($sf1->strand == -1 && $sf1->hstrand == 1) {
142 $newcoord = $sf1->hstart + ($sf1->end - $coord);
143 last HOMOL;
144 } elsif ($sf1->strand == -1 && $sf1->hstrand == -1) {
145 $newcoord = $sf1->hend - ($sf1->end - $coord);
146 last HOMOL;
147 } else {
148 $self->throw("ERROR: Wrong strand value in FeaturePair (" . $sf1->strand . "/" . $sf1->hstrand . "\n");
149 }
150 }
151
152 if (defined($newcoord)) {
153
154 return $newcoord;
155 } else {
156 $self->throw("Couldn't convert $coord");
157 }
158 }
159  
160 =head2 cDNA2genomic
161  
162 Title : cDNA2genomic
163 Usage : my $gen_coord = $pair->genomic2cDNA($cdna_coord)
164 Function: Converts a cdna coordinate to a genomic coordinate
165 Example :
166 Returns : int
167 Args : int
168  
169  
170 =cut
171  
172 sub cDNA2genomic {
173 my ($self,$coord) = @_;
174
175 my @pairs = $self->eachFeaturePair;
176
177 my $newcoord;
178
179 HOMOL: while (my $sf1 = shift(@pairs)) {
180 next HOMOL unless ($coord >= $sf1->hstart && $coord <= $sf1->hend);
181
182 if ($sf1->strand == 1 && $sf1->hstrand == 1) {
183 $newcoord = $sf1->start + ($coord - $sf1->hstart);
184 last HOMOL;
185 } elsif ($sf1->strand == 1 && $sf1->hstrand == -1) {
186 $newcoord = $sf1->start +($sf1->hend - $coord);
187 last HOMOL;
188 } elsif ($sf1->strand == -1 && $sf1->hstrand == 1) {
189 $newcoord = $sf1->end - ($coord - $sf1->hstart);
190 last HOMOL;
191 } elsif ($sf1->strand == -1 && $sf1->hstrand == -1) {
192 $newcoord = $sf1->end - ($sf1->hend - $coord);
193 last HOMOL;
194 } else {
195 $self->throw("ERROR: Wrong strand value in homol (" . $sf1->strand . "/" . $sf1->hstrand . "\n");
196 }
197 }
198
199 if (defined ($newcoord)) {
200 return $newcoord;
201 } else {
202 $self->throw("Couldn't convert $coord\n");
203 }
204 }
205  
206 sub find_Pair {
207 my ($self,$coord) = @_;
208
209 foreach my $p ($self->eachFeaturePair) {
210 if ($coord >= $p->hstart && $coord <= $p->hend) {
211 return $p;
212 }
213 }
214 }
215  
216 =head2 convert_cDNA_feature
217  
218 Title : convert_cDNA_feature
219 Usage : my @newfeatures = $self->convert_cDNA_feature($f);
220 Function: Converts a feature on the cDNA into an array of
221 features on the genomic (for features that span across introns);
222 Returns : Array of Bio::EnsEMBL::FeaturePair
223 Args : Bio::EnsEMBL::FeaturePair
224  
225 =cut
226  
227 sub convert_cDNA_feature {
228 my ($self,$feature) = @_;
229
230 my $foundstart = 0;
231 my $foundend = 0;
232
233 my @pairs = $self->eachFeaturePair;
234 my @newfeatures;
235
236 HOMOL: while (my $sf1 = shift(@pairs)) {
237 my $skip = 0;
238 #print STDERR "Looking at cDNA exon " . $sf1->hstart . "\t" . $sf1->hend . "\t" . $sf1->strand ."\n";
239
240 $skip = 1 unless ($feature->start >= $sf1->hstart
241 && $feature->start <= $sf1->hend);
242
243 if($skip){
244 #print STDERR "Skipping ".$sf1->hstart . "\t" . $sf1->hend . "\t" . $sf1->strand ."\n";
245 next HOMOL;
246 }
247 if ($feature->end >= $sf1->hstart && $feature->end <= $sf1->hend) {
248 $foundend = 1;
249 }
250
251 my $startcoord = $self->cDNA2genomic($feature->start);
252 my $endcoord;
253
254 if ($sf1->hstrand == 1) {
255 $endcoord = $sf1->end;
256 } else {
257 $endcoord = $sf1->start;
258 }
259
260 if ($foundend) {
261 $endcoord = $self->cDNA2genomic($feature->end);
262 }
263
264 #print STDERR "Making new genomic feature $startcoord\t$endcoord\n";
265
266 my $tmpf = new Bio::EnsEMBL::Feature(-seqname => $feature->seqname,
267 -start => $startcoord,
268 -end => $endcoord,
269 -strand => $feature->strand);
270 push(@newfeatures,$tmpf);
271 last;
272 }
273
274 # Now the rest of the pairs until we find the endcoord
275
276 while ((my $sf1 = shift(@pairs)) && ($foundend == 0)) {
277
278 if ($feature->end >= $sf1->hstart && $feature->end <= $sf1->hend) {
279 $foundend = 1;
280 }
281
282 my $startcoord;
283 my $endcoord;
284
285 if ($sf1->hstrand == 1) {
286 $startcoord = $sf1->start;
287 $endcoord = $sf1->end;
288 } else {
289 $startcoord = $sf1->end;
290 $endcoord = $sf1->start;
291 }
292
293 if ($foundend) {
294 $endcoord = $self->cDNA2genomic($feature->end);
295 }
296
297 # #print STDERR "Making new genomic feature $startcoord\t$endcoord\n";
298
299 my $tmpf = new Bio::EnsEMBL::Feature(-seqname => $feature->seqname,
300 -start => $startcoord,
301 -end => $endcoord,
302 -strand => $feature->strand);
303 push(@newfeatures,$tmpf);
304 }
305 #print STDERR "Have ".@newfeatures." features from ".$feature."\n";
306 return @newfeatures;
307 }
308  
309  
310 sub convert_FeaturePair {
311 my ($self,$pair) = @_;
312  
313 my $hstrand = $self->get_hstrand;
314
315 my $feat = $self->create_Feature($pair->start, $pair->end, $pair->strand,
316 $pair->slice);
317 my @newfeatures = $self->convert_cDNA_feature($feat);
318 my @newpairs;
319
320 my $hitpairaln = new Bio::EnsEMBL::Analysis::PairAlign;
321 $hitpairaln->addFeaturePair($pair);
322
323 foreach my $new (@newfeatures) {
324
325 # Now we want to convert these cDNA coords into hit coords
326
327 my $hstart1 = $self->genomic2cDNA($new->start);
328 my $hend1 = $self->genomic2cDNA($new->end);
329
330 my $hstart2 = $hitpairaln->genomic2cDNA($hstart1);
331 my $hend2 = $hitpairaln->genomic2cDNA($hend1);
332
333 # We can now put the final feature together
334
335 my $finalstrand = $hstrand * $pair->strand * $pair->hstrand;
336
337 if ($hstart2 > $hend2) {
338 my $tmp = $hstart2;
339 $hstart2 = $hend2;
340 $hend2 = $tmp;
341 }
342
343 my $finalpair = $self->create_FeaturePair($new->start, $new->end,
344 $new->strand,
345 $hstart2, $hend2,
346 $finalstrand, $pair->score);
347
348 push(@newpairs,$finalpair);
349
350 }
351
352 return @newpairs;
353 }
354  
355 sub create_FeaturePair {
356 my ($self, $start, $end, $strand, $hstart, $hend,
357 $hstrand, $score) = @_;
358
359 my $fp = Bio::EnsEMBL::FeaturePair->new(
360 -start => $start,
361 -end => $end,
362 -strand => $strand,
363 -hstart => $hstart,
364 -hend => $hend,
365 -hstrand => $hstrand,
366 -score => $score,
367 );
368  
369
370 return $fp;
371 }
372  
373 sub create_Feature{
374 my ($self, $start, $end, $strand, $slice) = @_;
375  
376 my $feat = new Bio::EnsEMBL::Feature(-start => $start,
377 -end => $end,
378 -strand => $strand,
379 -slice => $slice,
380 );
381 return $feat;
382 }
383  
384 1;
385  
386  
387  
388  
389  
390  
391  
392