EnsEMBL/IdMapping/SyntenyRegion.pm untested

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 16 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 227 CC 32 SCAR 43.7
CC
32
Cov
0%
merge
210.0
score_location_relationship
90.0
new_fast
6.0
CRAP 79.2
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::IdMapping::SyntenyRegion - object representing syntenic regions
34  
35 =head1 SYNOPSIS
36  
37 # create a new SyntenyRegion from a source and a target gene
38 my $sr = Bio::EnsEMBL::IdMapping::SyntenyRegion->new_fast( [
39 $source_gene->start, $source_gene->end,
40 $source_gene->strand, $source_gene->seq_region_name,
41 $target_gene->start, $target_gene->end,
42 $target_gene->strand, $target_gene->seq_region_name,
43 $entry->score,
44 ] );
45  
46 # merge with another SyntenyRegion
47 my $merged_sr = $sr->merge($sr1);
48  
49 # score a gene pair against this SyntenyRegion
50 my $score =
51 $sr->score_location_relationship( $source_gene1, $target_gene1 );
52  
53 =head1 DESCRIPTION
54  
55 This object represents a synteny between a source and a target location.
56 SyntenyRegions are built from mapped genes, and the their score is
57 defined as the score of the gene mapping. For merged SyntenyRegions,
58 scores are combined.
59  
60 =head1 METHODS
61  
62 new_fast
63 source_start
64 source_end
65 source_strand
66 source_seq_region_name
67 target_start
68 target_end
69 target_strand
70 target_seq_region_name
71 score
72 merge
73 stretch
74 score_location_relationship
75 to_string
76  
77 =cut
78  
79 package Bio::EnsEMBL::IdMapping::SyntenyRegion;
80 $Bio::EnsEMBL::IdMapping::SyntenyRegion::VERSION = '114.0.0_50'; # TRIAL
81 $Bio::EnsEMBL::IdMapping::SyntenyRegion::VERSION = '114.0.050';
82 use strict;
83 use warnings;
84 no warnings 'uninitialized';
85  
86 use Bio::EnsEMBL::Utils::Exception qw(throw warning);
87  
88  
89 =head2 new_fast
90  
91 Arg[1] : Arrayref $array_ref - the arrayref to bless into the
92 SyntenyRegion object
93 Example : my $sr = Bio::EnsEMBL::IdMapping::SyntenyRegion->new_fast([
94 ]);
95 Description : Constructor. On instantiation, source and target regions are
96 reverse complemented so that source is always on forward strand.
97 Return type : a Bio::EnsEMBL::IdMapping::SyntenyRegion object
98 Exceptions : none
99 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
100 Status : At Risk
101 : under development
102  
103 =cut
104  
105 sub new_fast {
106 my $class = shift;
107 my $array_ref = shift;
108  
109 # reverse complement source and target so that source is always on forward
110 # strand; this will make merging and other comparison operations easier
111 # at later stages
112 if ($array_ref->[2] == -1) {
113 $array_ref->[2] = 1;
114 $array_ref->[6] = -1 * $array_ref->[6];
115 }
116
117 return bless $array_ref, $class;
118 }
119  
120  
121 =head2 source_start
122  
123 Arg[1] : (optional) Int - source location start coordinate
124 Description : Getter/setter for source location start coordinate.
125 Return type : Int
126 Exceptions : none
127 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
128 Status : At Risk
129 : under development
130  
131 =cut
132  
133 sub source_start {
134 my $self = shift;
135 $self->[0] = shift if (@_);
136 return $self->[0];
137 }
138  
139  
140 =head2 source_end
141  
142 Arg[1] : (optional) Int - source location end coordinate
143 Description : Getter/setter for source location end coordinate.
144 Return type : Int
145 Exceptions : none
146 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
147 Status : At Risk
148 : under development
149  
150 =cut
151  
152 sub source_end {
153 my $self = shift;
154 $self->[1] = shift if (@_);
155 return $self->[1];
156 }
157  
158  
159 =head2 source_strand
160  
161 Arg[1] : (optional) Int - source location strand
162 Description : Getter/setter for source location strand.
163 Return type : Int
164 Exceptions : none
165 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
166 Status : At Risk
167 : under development
168  
169 =cut
170  
171 sub source_strand {
172 my $self = shift;
173 $self->[2] = shift if (@_);
174 return $self->[2];
175 }
176  
177  
178 =head2 source_seq_region_name
179  
180 Arg[1] : (optional) String - source location seq_region name
181 Description : Getter/setter for source location seq_region name.
182 Return type : String
183 Exceptions : none
184 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
185 Status : At Risk
186 : under development
187  
188 =cut
189  
190 sub source_seq_region_name {
191 my $self = shift;
192 $self->[3] = shift if (@_);
193 return $self->[3];
194 }
195  
196  
197 =head2 target_start
198  
199 Arg[1] : (optional) Int - target location start coordinate
200 Description : Getter/setter for target location start coordinate.
201 Return type : Int
202 Exceptions : none
203 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
204 Status : At Risk
205 : under development
206  
207 =cut
208  
209 sub target_start {
210 my $self = shift;
211 $self->[4] = shift if (@_);
212 return $self->[4];
213 }
214  
215  
216 =head2 target_end
217  
218 Arg[1] : (optional) Int - target location end coordinate
219 Description : Getter/setter for target location end coordinate.
220 Return type : Int
221 Exceptions : none
222 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
223 Status : At Risk
224 : under development
225  
226 =cut
227  
228 sub target_end {
229 my $self = shift;
230 $self->[5] = shift if (@_);
231 return $self->[5];
232 }
233  
234  
235 =head2 target_strand
236  
237 Arg[1] : (optional) Int - target location strand
238 Description : Getter/setter for target location strand.
239 Return type : Int
240 Exceptions : none
241 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
242 Status : At Risk
243 : under development
244  
245 =cut
246  
247 sub target_strand {
248 my $self = shift;
249 $self->[6] = shift if (@_);
250 return $self->[6];
251 }
252  
253  
254 =head2 target_seq_region_name
255  
256 Arg[1] : (optional) String - target location seq_region name
257 Description : Getter/setter for target location seq_region name.
258 Return type : String
259 Exceptions : none
260 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
261 Status : At Risk
262 : under development
263  
264 =cut
265  
266 sub target_seq_region_name {
267 my $self = shift;
268 $self->[7] = shift if (@_);
269 return $self->[7];
270 }
271  
272  
273 =head2 score
274  
275 Arg[1] : (optional) Float - score
276 Description : Getter/setter for the score between source and target location.
277 Return type : Int
278 Exceptions : none
279 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
280 Status : At Risk
281 : under development
282  
283 =cut
284  
285 sub score {
286 my $self = shift;
287 $self->[8] = shift if (@_);
288 return $self->[8];
289 }
290  
291  
292 =head2 merge
293  
294 Arg[1] : Bio::EnsEMBL::IdMapping::SyntenyRegion $sr - another
295 SyntenyRegion
296 Example : $merged_sr = $sr->merge($other_sr);
297 Description : Merges two overlapping SyntenyRegions if they meet certain
298 criteria (see documentation in the code for details). Score is
299 calculated as a combined distance score. If the two
300 SyntenyRegions aren't mergeable, this method returns undef.
301 Return type : Bio::EnsEMBL::IdMapping::SyntenyRegion or undef
302 Exceptions : warns on bad scores
303 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
304 Status : At Risk
305 : under development
306  
307 =cut
308  
309 sub merge {
310 my ($self, $sr) = @_;
311  
312 # must be on same seq_region
313 if ($self->source_seq_region_name ne $sr->source_seq_region_name or
314 $self->target_seq_region_name ne $sr->target_seq_region_name) {
315 return 0;
316 }
317  
318 # target must be on same strand
319 return 0 unless ($self->target_strand == $sr->target_strand);
320  
321 # find the distance of source and target pair and compare
322 my $source_dist = $sr->source_start - $self->source_start;
323 my $target_dist;
324 if ($self->target_strand == 1) {
325 $target_dist = $sr->target_start - $self->target_start;
326 } else {
327 $target_dist = $self->target_end - $sr->target_end;
328 }
329  
330 # prevent division by zero error
331 if ($source_dist == 0 or $target_dist == 0) {
332 warn("WARNING: source_dist ($source_dist) and/or target_dist ($target_dist) is zero.\n");
333 return 0;
334 }
335  
336 # calculate a distance score
337 my $dist = $source_dist - $target_dist;
338 $dist = -$dist if ($dist < 0);
339 my $d1 = $dist/$source_dist;
340 $d1 = -$d1 if ($d1 < 0);
341 my $d2 = $dist/$target_dist;
342 $d2 = -$d2 if ($d2 < 0);
343 my $dist_score = 1 - $d1 - $d2;
344  
345 # distance score must be more than 50%
346 return 0 if ($dist_score < 0.5);
347  
348 my $new_score = $dist_score * ($sr->score + $self->score)/2;
349  
350 if ($new_score > 1) {
351 warn("WARNING: Bad merge score: $new_score\n");
352 }
353  
354 # extend SyntenyRegion to cover both sources and targets, set merged score
355 # and return
356 if ($sr->source_start < $self->source_start) {
357 $self->source_start($sr->source_start);
358 }
359 if ($sr->source_end > $self->source_end) {
360 $self->source_end($sr->source_end);
361 }
362
363 if ($sr->target_start < $self->target_start) {
364 $self->target_start($sr->target_start);
365 }
366 if ($sr->target_end > $self->target_end) {
367 $self->target_end($sr->target_end);
368 }
369  
370 $self->score($new_score);
371  
372 return $self;
373 }
374  
375  
376 =head2 stretch
377  
378 Arg[1] : Float $factor - stretching factor
379 Example : $stretched_sr = $sr->stretch(2);
380 Description : Extends this SyntenyRegion to span a $factor * $score more area.
381 Return type : Bio::EnsEMBL::IdMapping::SyntenyRegion
382 Exceptions : none
383 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
384 Status : At Risk
385 : under development
386  
387 =cut
388  
389 sub stretch {
390 my ($self, $factor) = @_;
391  
392 my $source_adjust = int(($self->source_end - $self->source_start + 1) *
393 $factor * $self->score);
394 $self->source_start($self->source_start - $source_adjust);
395 $self->source_end($self->source_end + $source_adjust);
396 #warn sprintf(" sss %d %d %d\n", $source_adjust, $self->source_start,
397 # $self->source_end);
398
399 my $target_adjust = int(($self->target_end - $self->target_start + 1) *
400 $factor * $self->score);
401 $self->target_start($self->target_start - $target_adjust);
402 $self->target_end($self->target_end + $target_adjust);
403  
404 return $self;
405 }
406  
407  
408 =head2 score_location_relationship
409  
410 Arg[1] : Bio::EnsEMBL::IdMapping::TinyGene $source_gene - source gene
411 Arg[2] : Bio::EnsEMBL::IdMapping::TinyGene $target_gene - target gene
412 Example : my $score = $sr->score_location_relationship($source_gene,
413 $target_gene);
414 Description : This function calculates how well the given source location
415 interpolates on given target location inside this SyntenyRegion.
416  
417 Scoring is done the following way: Source and target location
418 are normalized with respect to this Regions source and target.
419 Source range will then be somewhere close to 0.0-1.0 and target
420 range anything around that.
421  
422 The extend of the covered area between source and target range
423 is a measurement of how well they agree (smaller extend is
424 better). The extend (actually 2*extend) is reduced by the size
425 of the regions. This will result in 0.0 if they overlap
426 perfectly and bigger values if they dont.
427  
428 This is substracted from 1.0 to give the score. The score is
429 likely to be below zero, but is cut off at 0.0f.
430  
431 Finally, the score is multiplied with the score of the synteny
432 itself.
433 Return type : Float
434 Exceptions : warns if score out of range
435 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
436 Status : At Risk
437 : under development
438  
439 =cut
440  
441  
442  
443 sub score_location_relationship {
444 my ($self, $source_gene, $target_gene) = @_;
445  
446 # must be on same seq_region
447 if (($self->source_seq_region_name ne $source_gene->seq_region_name) or
448 ($self->target_seq_region_name ne $target_gene->seq_region_name)) {
449 return 0;
450 }
451  
452 # strand relationship must be the same (use logical XOR to find out)
453 if (($self->source_strand == $source_gene->strand) xor
454 ($self->target_strand == $target_gene->strand)) {
455 return 0;
456 }
457  
458 # normalise source location
459 my $source_rel_start = ($source_gene->start - $self->source_start) /
460 ($self->source_end - $self->source_start + 1);
461  
462 my $source_rel_end = ($source_gene->end - $self->source_start + 1) /
463 ($self->source_end - $self->source_start + 1);
464  
465 #warn " aaa ".$self->to_string."\n";
466 #warn sprintf(" bbb %.6f %.6f\n", $source_rel_start, $source_rel_end);
467  
468 # cut off if the source location is completely outside
469 return 0 if ($source_rel_start > 1.1 or $source_rel_end < -0.1);
470
471 # normalise target location
472 my ($target_rel_start, $target_rel_end);
473 my $t_length = $self->target_end - $self->target_start + 1;
474  
475 if ($self->target_strand == 1) {
476  
477 $target_rel_start = ($target_gene->start - $self->target_start) / $t_length;
478  
479 $target_rel_end = ($target_gene->end - $self->target_start + 1) / $t_length;
480  
481 } else {
482 $target_rel_start = ($self->target_end - $target_gene->end) / $t_length;
483 $target_rel_end = ($self->target_end - $target_gene->start + 1) / $t_length;
484 }
485  
486 my $added_range = (($target_rel_end > $source_rel_end) ? $target_rel_end :
487 $source_rel_end) -
488 (($target_rel_start < $source_rel_start) ? $target_rel_start :
489 $source_rel_start);
490  
491 my $score = $self->score * (1 - (2 * $added_range - $target_rel_end -
492 $source_rel_end + $target_rel_start + $source_rel_start));
493  
494 #warn " ccc ".sprintf("%.6f:%.6f:%.6f:%.6f:%.6f\n", $added_range,
495 # $source_rel_start, $source_rel_end, $target_rel_start, $target_rel_end);
496  
497 $score = 0 if ($score < 0);
498  
499 # sanity check
500 if ($score > 1) {
501 warn "Out of range score ($score) for ".$source_gene->id.":".
502 $target_gene->id."\n";
503 }
504  
505 return $score;
506 }
507  
508  
509 =head2 to_string
510  
511 Example : print LOG $sr->to_string, "\n";
512 Description : Returns a string representation of the SyntenyRegion object.
513 Useful for debugging and logging.
514 Return type : String
515 Exceptions : none
516 Caller : Bio::EnsEMBL::IdMapping::SyntenyFramework
517 Status : At Risk
518 : under development
519  
520 =cut
521  
522 sub to_string {
523 my $self = shift;
524 return sprintf("%s:%s-%s:%s %s:%s-%s:%s %.6f",
525 $self->source_seq_region_name,
526 $self->source_start,
527 $self->source_end,
528 $self->source_strand,
529 $self->target_seq_region_name,
530 $self->target_start,
531 $self->target_end,
532 $self->target_strand,
533 $self->score
534 );
535 }
536  
537  
538 1;
539