EnsEMBL/SeqFeature.pm untested

filter: Statementstmt 0.0% 0 / 387 Branchbran 0.0% 0 / 228 Conditioncond 0.0% 0 / 96 Subroutinesub 0.0% 0 / 42 Podpod 0.0% 0 / 38 totaltotal 0.0% 0 / 791 CC 115 SCAR 42.4
CC
115
Cov
0%
_transform_to_RawContig
210.0
new
182.0
gffstring
132.0
CRAP 69.5
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::SeqFeature - Ensembl specific sequence feature.
34  
35 =head1 DESCRIPTION
36  
37 Do not use this module if you can avoid it. It has been replaced by
38 Bio::EnsEMBL::Feature. This module has a long history of usage but has
39 become very bloated, and quite unweildy. It was decided to replace
40 it completely with a smaller, light-weight feature class rather than
41 attempting to refactor this class, and maintain strict backwards
42 compatibility.
43  
44 Part of the complexity of this class was in its extensive
45 inheritance. As an example the following is a simplified inheritance
46 heirarchy that was present for Bio::EnsEMBL::DnaAlignFeature:
47  
48 Bio::EnsEMBL::DnaAlignFeature
49 Bio::EnsEMBL::BaseAlignFeature
50 Bio::EnsEMBL::FeaturePair
51 Bio::EnsEMBL::SeqFeature
52 Bio::SeqFeatureI
53 Bio::RangeI
54 Bio::Root::RootI
55  
56 The new Bio::EnsEMBL::Feature class is much shorter, and hopefully much
57 easier to understand and maintain.
58  
59 =head1 METHODS
60  
61 =cut
62  
63  
64 # Let the code begin...
65  
66  
67 package Bio::EnsEMBL::SeqFeature;
68 $Bio::EnsEMBL::SeqFeature::VERSION = '114.0.0_50'; # TRIAL
69 $Bio::EnsEMBL::SeqFeature::VERSION = '114.0.050';
70 use vars qw(@ISA);
71 use strict;
72  
73  
74 use Bio::EnsEMBL::Analysis;
75  
76 use Bio::EnsEMBL::Utils::Argument qw(rearrange);
77  
78 sub new {
79 my($caller,@args) = @_;
80  
81 my $self = {};
82  
83 if(ref $caller) {
84 bless $self, ref $caller;
85 } else {
86 bless $self, $caller;
87 }
88  
89 $self->{'_gsf_tag_hash'} = {};
90 $self->{'_gsf_sub_array'} = [];
91 $self->{'_parse_h'} = {};
92 $self->{'_is_splittable'} = 0;
93  
94 my ($start,$end,$strand,$frame,$score,$analysis,$seqname, $source_tag,
95 $primary_tag, $percent_id, $p_value, $phase, $end_phase) =
96  
97 &rearrange([qw(START
98 END
99 STRAND
100 FRAME
101 SCORE
102 ANALYSIS
103 SEQNAME
104 SOURCE_TAG
105 PRIMARY_TAG
106 PERCENT_ID
107 P_VALUE
108 PHASE
109 END_PHASE
110 )],@args);
111  
112 # $gff_string && $self->_from_gff_string($gff_string);
113  
114 if ( defined $analysis && $analysis ne "") { $self->analysis($analysis)};
115 if ( defined ($start) && $start ne "" ) { $self->start($start)};
116 if ( defined ($end ) && $end ne "" ) { $self->end($end)}
117 if ( defined $strand && $strand ne "") { $self->strand($strand)}
118 if ( defined $frame && $frame ne "") { $self->frame($frame)}
119 if ( defined $score && $score ne "") { $self->score($score)}
120 if ( defined $seqname && $seqname ne "") { $self->seqname($seqname)};
121 if ( defined $percent_id && $percent_id ne ""){ $self->percent_id($percent_id)};
122 if ( defined $p_value && $p_value ne "") { $self->p_value($p_value)};
123 if ( defined $phase && $phase ne "") { $self->phase($phase)};
124 if ( defined $end_phase && $end_phase ne "") { $self->end_phase($end_phase)};
125  
126 return $self; # success - we hope!
127  
128 }
129  
130  
131  
132  
133  
134  
135 =head2 start
136  
137 Title : start
138 Usage : $start = $feat->start
139 $feat->start(20)
140 Function: Get/set on the start coordinate of the feature
141 Returns : integer
142 Args : none
143  
144  
145 =cut
146  
147 sub start{
148 my ($self,$value) = @_;
149  
150 if (defined($value)) {
151 if ($value !~ /^\-?\d+/ ) {
152 $self->throw("$value is not a valid start");
153 }
154
155 $self->{'_gsf_start'} = $value
156 }
157  
158 return $self->{'_gsf_start'};
159  
160 }
161  
162 =head2 end
163  
164 Title : end
165 Usage : $end = $feat->end
166 $feat->end($end)
167 Function: get/set on the end coordinate of the feature
168 Returns : integer
169 Args : none
170  
171  
172 =cut
173  
174 sub end{
175 my ($self,$value) = @_;
176  
177 if (defined($value)) {
178 if( $value !~ /^\-?\d+/ ) {
179 $self->throw("[$value] is not a valid end");
180 }
181
182 $self->{'_gsf_end'} = $value;
183 }
184  
185 return $self->{'_gsf_end'};
186 }
187  
188 =head2 length
189  
190 Title : length
191 Usage :
192 Function:
193 Example :
194 Returns :
195 Args :
196  
197  
198 =cut
199  
200 sub length{
201 my ($self,@args) = @_;
202  
203 return $self->end - $self->start +1;
204 }
205  
206  
207 =head2 strand
208  
209 Title : strand
210 Usage : $strand = $feat->strand()
211 $feat->strand($strand)
212 Function: get/set on strand information, being 1,-1 or 0
213 Returns : -1,1 or 0
214 Args : none
215  
216  
217 =cut
218  
219 sub strand {
220 my ($self,$value) = @_;
221  
222 if (defined($value)) {
223 if( $value eq '+' ) { $value = 1; }
224 if( $value eq '-' ) { $value = -1; }
225 if( $value eq '.' ) { $value = 0; }
226  
227 if( $value != -1 && $value != 1 && $value != 0 ) {
228 $self->throw("$value is not a valid strand info");
229 }
230 $self->{'_gsf_strand'} = $value;
231 }
232  
233 return $self->{'_gsf_strand'};
234 }
235  
236  
237 =head2 move
238  
239 Arg [1] : int $start
240 Arg [2] : int $end
241 Arg [3] : (optional) int $strand
242 Example : $feature->move(100, 200, -1);
243 Description: Moves a feature to a different location. This is faster
244 then calling 3 seperate accesors in a large loop.
245 Returntype : none
246 Exceptions : none
247 Caller : BaseFeatureAdaptor
248  
249 =cut
250  
251 sub move {
252 my ($self, $start, $end, $strand) = @_;
253  
254 $self->{'_gsf_start'} = $start;
255 $self->{'_gsf_end'} = $end;
256 if(defined $strand) {
257 $self->{'_gsf_strand'} = $strand;
258 }
259 }
260  
261  
262 =head2 score
263  
264 Title : score
265 Usage : $score = $feat->score()
266 $feat->score($score)
267 Function: get/set on score information
268 Returns : float
269 Args : none if get, the new value if set
270  
271  
272 =cut
273  
274 sub score {
275 my ($self,$value) = @_;
276  
277 if(defined ($value) ) {
278 if( $value !~ /^[+-]?\d+\.?\d*(e-\d+)?/ ) {
279 $self->throw("'$value' is not a valid score");
280 }
281 $self->{'_gsf_score'} = $value;
282 }
283  
284 return $self->{'_gsf_score'};
285 }
286  
287 =head2 frame
288  
289 Title : frame
290 Usage : $frame = $feat->frame()
291 $feat->frame($frame)
292 Function: get/set on frame information
293 Returns : 0,1,2
294 Args : none if get, the new value if set
295  
296  
297 =cut
298  
299 sub frame {
300 my ($self,$value) = @_;
301  
302 if (defined($value)) {
303 if( $value != 1 && $value != 2 && $value != 3 ) {
304 $self->throw("'$value' is not a valid frame");
305 }
306 $self->{'_gsf_frame'} = $value;
307 }
308  
309 return $self->{'_gsf_frame'};
310 }
311  
312 =head2 primary_tag
313  
314 Title : primary_tag
315 Usage : $tag = $feat->primary_tag()
316 $feat->primary_tag('exon')
317 Function: get/set on the primary tag for a feature,
318 eg 'exon'
319 Returns : a string
320 Args : none
321  
322  
323 =cut
324  
325 sub primary_tag{
326 my ($self,$arg) = @_;
327
328 unless($self->analysis) {
329 return '';
330 }
331
332 return $self->analysis->gff_feature();
333 }
334  
335 =head2 source_tag
336  
337 Title : source_tag
338 Usage : $tag = $feat->source_tag()
339 $feat->source_tag('genscan');
340 Function: Returns the source tag for a feature,
341 eg, 'genscan'
342 Returns : a string
343 Args : none
344  
345  
346 =cut
347  
348 sub source_tag{
349 my ($self,$arg) = @_;
350  
351 unless($self->analysis) {
352 return "";
353 }
354  
355 return $self->analysis->gff_source();
356 }
357  
358  
359 =head2 analysis
360  
361 Title : analysis
362 Usage : $sf->analysis();
363 Function: Store details of the program/database
364 and versions used to create this feature.
365  
366 Example :
367 Returns :
368 Args :
369  
370  
371 =cut
372  
373 sub analysis {
374 my ($self,$value) = @_;
375  
376 if (defined($value)) {
377 unless(ref($value) && $value->isa('Bio::EnsEMBL::Analysis')) {
378 $self->throw("Analysis is not a Bio::EnsEMBL::Analysis object "
379 . "but a $value object");
380 }
381  
382 $self->{_analysis} = $value;
383 } else {
384 #if _analysis is not defined, create a new analysis object
385 unless(defined $self->{_analysis}) {
386 $self->{_analysis} = new Bio::EnsEMBL::Analysis();
387 }
388 }
389  
390 return $self->{_analysis};
391 }
392  
393 =head2 validate
394  
395 Title : validate
396 Usage : $sf->validate;
397 Function: Checks whether all the data is present in the
398 object.
399 Example :
400 Returns :
401 Args :
402  
403  
404 =cut
405  
406 sub validate {
407 my ($self) = @_;
408  
409 $self->vthrow("Seqname not defined in feature") unless defined($self->seqname);
410 $self->vthrow("start not defined in feature") unless defined($self->start);
411 $self->vthrow("end not defined in feature") unless defined($self->end);
412 $self->vthrow("strand not defined in feature") unless defined($self->strand);
413 $self->vthrow("score not defined in feature") unless defined($self->score);
414 $self->vthrow("analysis not defined in feature") unless defined($self->analysis);
415  
416 if ($self->end < $self->start) {
417 $self->vthrow("End coordinate < start coordinate");
418 }
419  
420 }
421  
422  
423  
424 sub vthrow {
425 my ($self,$message) = @_;
426  
427 print(STDERR "Error validating feature [$message]\n");
428 print(STDERR " Seqname : [" . $self->{_seqname} . "]\n");
429 print(STDERR " Start : [" . $self->{_gsf_start} . "]\n");
430 print(STDERR " End : [" . $self->{_gsf_end} . "]\n");
431 print(STDERR " Strand : [" .
432 ((defined ($self->{_gsf_strand})) ? $self->{_gsf_strand} : "undefined") . "]\n");
433  
434 print(STDERR " Score : [" . $self->{_gsf_score} . "]\n");
435  
436 print(STDERR " Analysis : [" . $self->{_analysis}->dbID . "]\n");
437  
438 $self->throw("Invalid feature - see dump on STDERR");
439 }
440  
441  
442 =head2 validate_prot_feature
443  
444 Title : validate_prot_feature
445 Usage :
446 Function:
447 Example :
448 Returns :
449 Args :
450  
451  
452 =cut
453  
454 # Shouldn't this go as "validate" into Pro_SeqFeature?
455 sub validate_prot_feature{
456 my ($self,$num) = @_;
457 $self->throw("Seqname not defined in feature") unless defined($self->seqname);
458 $self->throw("start not defined in feature") unless defined($self->start);
459 $self->throw("end not defined in feature") unless defined($self->end);
460 if ($num == 1) {
461 $self->throw("score not defined in feature") unless defined($self->score);
462 $self->throw("percent_id not defined in feature") unless defined($self->percent_id);
463 $self->throw("evalue not defined in feature") unless defined($self->p_value);
464 }
465 $self->throw("analysis not defined in feature") unless defined($self->analysis);
466 }
467  
468  
469  
470 # These methods are specified in the SeqFeatureI interface but we don't want
471 # people to store data in them. These are just here in order to keep
472 # existing code working
473  
474  
475 =head2 has_tag
476  
477 Title : has_tag
478 Usage : $value = $self->has_tag('some_tag')
479 Function: Returns the value of the tag (undef if
480 none)
481 Returns :
482 Args :
483  
484  
485 =cut
486  
487 sub has_tag{
488 my ($self,$tag) = (shift, shift);
489  
490 return exists $self->{'_gsf_tag_hash'}->{$tag};
491 }
492  
493 =head2 add_tag_value
494  
495 Title : add_tag_value
496 Usage : $self->add_tag_value('note',"this is a note");
497 Returns : nothing
498 Args : tag (string) and value (any scalar)
499  
500  
501 =cut
502  
503 sub add_tag_value{
504 my ($self,$tag,$value) = @_;
505  
506 if( !defined $self->{'_gsf_tag_hash'}->{$tag} ) {
507 $self->{'_gsf_tag_hash'}->{$tag} = [];
508 }
509  
510 push(@{$self->{'_gsf_tag_hash'}->{$tag}},$value);
511 }
512  
513 =head2 each_tag_value
514  
515 Title : each_tag_value
516 Usage :
517 Function:
518 Example :
519 Returns :
520 Args :
521  
522  
523 =cut
524  
525 sub each_tag_value {
526 my ($self,$tag) = @_;
527 if( ! exists $self->{'_gsf_tag_hash'}->{$tag} ) {
528 $self->throw("asking for tag value that does not exist $tag");
529 }
530  
531 return @{$self->{'_gsf_tag_hash'}->{$tag}};
532 }
533  
534  
535 =head2 all_tags
536  
537 Title : all_tags
538 Usage : @tags = $feat->all_tags()
539 Function: gives all tags for this feature
540 Returns : an array of strings
541 Args : none
542  
543  
544 =cut
545  
546 sub all_tags{
547 my ($self,@args) = @_;
548  
549 return keys %{$self->{'_gsf_tag_hash'}};
550 }
551  
552  
553  
554 =head2 seqname
555  
556 Arg [1] : string $seqname
557 Example : $seqname = $self->seqname();
558 Description: Obtains the seqname of this features sequence. This is set
559 automatically when a sequence with a name is attached, or may
560 be set manually.
561 Returntype : string
562 Exceptions : none
563 Caller : general, attach_seq
564  
565 =cut
566  
567 sub seqname{
568 my ($self,$seqname) = @_;
569  
570 my $seq = $self->contig();
571  
572 if(defined $seqname) {
573 $self->{_seqname} = $seqname;
574 } else {
575 if($seq && ref $seq && $seq->can('name')) {
576 $self->{_seqname} = $seq->name();
577 }
578 }
579  
580 return $self->{_seqname};
581 }
582  
583  
584 =head2 attach_seq
585  
586 Title : attach_seq
587 Usage : $sf->attach_seq($seq)
588 Function: Attaches a Bio::PrimarySeqI object to this feature. This
589 Bio::PrimarySeqI object is for the *entire* sequence: ie
590 from 1 to 10000
591 Example :
592 Returns :
593 Args :
594  
595  
596 =cut
597  
598 sub attach_seq{
599 my ($self, $seq) = @_;
600  
601 $self->contig($seq);
602 }
603  
604 =head2 seq
605  
606 Example : $tseq = $sf->seq()
607 Function: returns the sequence (if any ) for this feature truncated to the range spanning the feature
608 Returns : a Bio::PrimarySeq object (I reckon)
609  
610 =cut
611  
612 sub seq{
613 my ($self,$arg) = @_;
614  
615 if( defined $arg ) {
616 $self->throw("Calling SeqFeature::Generic->seq with an argument. " .
617 "You probably want attach_seq");
618 }
619  
620 if( ! exists $self->{'_gsf_seq'} ) {
621 return undef;
622 }
623  
624 # assumming our seq object is sensible, it should not have to yank
625 # the entire sequence out here.
626  
627 my $seq = $self->{'_gsf_seq'}->trunc($self->start(),$self->end());
628  
629  
630 if( $self->strand == -1 ) {
631  
632 # ok. this does not work well (?)
633 #print STDERR "Before revcom", $seq->str, "\n";
634 $seq = $seq->revcom;
635 #print STDERR "After revcom", $seq->str, "\n";
636 }
637  
638 return $seq;
639 }
640  
641 =head2 entire_seq
642  
643 Title : entire_seq
644 Usage : $whole_seq = $sf->entire_seq()
645 Function: gives the entire sequence that this seqfeature is attached to
646 Example :
647 Returns :
648 Args :
649  
650  
651 =cut
652  
653 sub entire_seq{
654 my ($self) = @_;
655  
656 return $self->contig;
657 }
658  
659  
660 =head2 sub_SeqFeature
661  
662 Title : sub_SeqFeature
663 Usage : @feats = $feat->sub_SeqFeature();
664 Function: Returns an array of sub Sequence Features
665 Returns : An array
666 Args : none
667  
668  
669 =cut
670  
671 sub sub_SeqFeature {
672 my ($self) = @_;
673  
674 if ( $self->{'_gsf_sub_array'} ) {
675 return @{ $self->{'_gsf_sub_array'} };
676 } else {
677 return ();
678 }
679 }
680  
681 =head2 add_sub_SeqFeature
682  
683 Title : add_sub_SeqFeature
684 Usage : $feat->add_sub_SeqFeature($subfeat);
685 $feat->add_sub_SeqFeature($subfeat,'EXPAND')
686 Function: adds a SeqFeature into the subSeqFeature array.
687 with no 'EXPAND' qualifer, subfeat will be tested
688 as to whether it lies inside the parent, and throw
689 an exception if not.
690  
691 If EXPAND is used, the parents start/end/strand will
692 be adjusted so that it grows to accommodate the new
693 subFeature
694 Returns : nothing
695 Args : An object which has the SeqFeatureI interface
696  
697  
698 =cut
699  
700 sub add_sub_SeqFeature{
701 my ($self,$feat,$expand) = @_;
702  
703 if( !$feat->isa('Bio::SeqFeatureI') ) {
704 $self->warn("$feat does not implement Bio::SeqFeatureI. Will add it anyway, but beware...");
705 }
706  
707 if( $expand eq 'EXPAND' ) {
708 # if this doesn't have start/end set - forget it!
709 if( !defined $self->start && !defined $self->end ) {
710 $self->start($feat->start());
711 $self->end($feat->end());
712 $self->strand($feat->strand);
713 } else {
714 my ($start,$end);
715 if( $feat->start < $self->start ) {
716 $start = $feat->start;
717 }
718  
719 if( $feat->end > $self->end ) {
720 $end = $feat->end;
721 }
722  
723 $self->start($start);
724 $self->end($end);
725  
726 }
727 } else {
728 if( !defined($feat->start()) || !defined($feat->end()) ||
729 !defined($self->start()) || !defined($self->end())) {
730 $self->throw( "This SeqFeature and the sub_SeqFeature must define".
731 " start and end.");
732 }
733 if($feat->start() > $feat->end() || $self->start() > $self->end()) {
734 $self->throw("This SeqFeature and the sub_SeqFeature must have " .
735 "start that is less than or equal to end.");
736 }
737 if($feat->start() < $self->start() || $feat->end() > $self->end() ) {
738 $self->throw("$feat is not contained within parent feature, " .
739 "and expansion is not valid");
740 }
741 }
742  
743 push(@{$self->{'_gsf_sub_array'}},$feat);
744  
745 }
746  
747 =head2 flush_sub_SeqFeature
748  
749 Title : flush_sub_SeqFeature
750 Usage : $sf->flush_sub_SeqFeature
751 Function: Removes all sub SeqFeature
752 (if you want to remove only a subset, take
753 an array of them all, flush them, and add
754 back only the guys you want)
755 Example :
756 Returns : none
757 Args : none
758  
759  
760 =cut
761  
762 sub flush_sub_SeqFeature {
763 my ($self) = @_;
764  
765 $self->{'_gsf_sub_array'} = []; # zap the array implicitly.
766 }
767  
768  
769 sub id {
770 my ($self,$value) = @_;
771  
772 if (defined($value)) {
773 $self->{_id} = $value;
774 }
775  
776 return $self->{_id};
777  
778 }
779  
780 =head2 percent_id
781  
782 Title : percent_id
783 Usage : $pid = $feat->percent_id()
784 $feat->percent_id($pid)
785 Function: get/set on percentage identity information
786 Returns : float
787 Args : none if get, the new value if set
788  
789 =cut
790  
791 sub percent_id {
792 my ($self,$value) = @_;
793  
794 if (defined($value))
795 {
796 $self->{_percent_id} = $value;
797 }
798  
799 return $self->{_percent_id};
800 }
801  
802 =head2 p_value
803  
804 Title : p_value
805 Usage : $p_val = $feat->p_value()
806 $feat->p_value($p_val)
807 Function: get/set on p value information
808 Returns : float
809 Args : none if get, the new value if set
810  
811 =cut
812  
813 sub p_value {
814 my ($self,$value) = @_;
815  
816 if (defined($value))
817 {
818 $self->{_p_value} = $value;
819 }
820  
821 return $self->{_p_value};
822 }
823  
824 =head2 phase
825  
826 Title : phase
827 Usage : $phase = $feat->phase()
828 $feat->phase($phase)
829 Function: get/set on start phase of predicted exon feature
830 Returns : [0,1,2]
831 Args : none if get, 0,1 or 2 if set.
832  
833 =cut
834  
835 sub phase {
836 my ($self, $value) = @_;
837  
838 if (defined($value) )
839 {
840 $self->throw("Valid values for Phase are [0,1,2]") if ($value < 0 || $value > 2);
841 $self->{_phase} = $value;
842 }
843  
844 return $self->{_phase};
845 }
846  
847 =head2 end_phase
848  
849 Title : end_phase
850 Usage : $end_phase = $feat->end_phase()
851 $feat->end_phase($end_phase)
852 Function: returns end_phase based on phase and length of feature
853 Returns : [0,1,2]
854 Args : none if get, 0,1 or 2 if set.
855  
856 =cut
857  
858 sub end_phase {
859 my ($self, $value) = @_;
860  
861 if (defined($value))
862 {
863 $self->throw("Valid values for Phase are [0,1,2]") if ($value < 0 || $value > 2);
864 $self->{_end_phase} = $value;
865 }
866  
867 return $self->{_end_phase};
868 }
869  
870  
871 sub gffstring {
872 my ($self) = @_;
873  
874 my $str;
875  
876 my $strand = "+";
877
878 if ((defined $self->strand)&&($self->strand == -1)) {
879 $strand = "-";
880 }
881
882 $str .= (defined $self->seqname) ? $self->seqname."\t" : "\t";
883 $str .= (defined $self->source_tag) ? $self->source_tag."\t" : "\t";
884 $str .= (defined $self->primary_tag) ? $self->primary_tag."\t" : "\t";
885 $str .= (defined $self->start) ? $self->start."\t" : "\t";
886 $str .= (defined $self->end) ? $self->end."\t" : "\t";
887 $str .= (defined $self->score) ? $self->score."\t" : "\t";
888 $str .= (defined $self->strand) ? $strand."\t" : ".\t";
889 $str .= (defined $self->phase) ? $self->phase."\t" : ".\t";
890 eval{
891 $str .= (defined $self->end_phase) ? $self->end_phase."\t" : ".\t";
892 };
893  
894 return $str;
895 }
896  
897  
898 =head2 external_db
899  
900 Title : external_db
901 Usage : $pid = $feat->external_db()
902 $feat->external_db($dbid)
903 Function: get/set for an external db accession number (e.g.: Interpro)
904 Returns :
905 Args : none if get, the new value if set
906  
907 =cut
908  
909 sub external_db {
910 my ($self,$value) = @_;
911  
912 if (defined($value))
913 {
914 $self->{'_external_db'} = $value;
915 }
916  
917 return $self->{'_external_db'};
918 }
919  
920  
921  
922 =head2 contig
923  
924 Arg [1] : Bio::PrimarySeqI $seq
925 Example : $seq = $self->contig;
926 Description: Accessor to attach/retrieve a sequence to/from a feature
927 Returntype : Bio::PrimarySeqI
928 Exceptions : none
929 Caller : general
930  
931 =cut
932  
933 sub contig {
934 my ($self, $arg) = @_;
935  
936 if ($arg) {
937 unless (defined $arg && ref $arg && $arg->isa("Bio::PrimarySeqI")) {
938 $self->throw("Must attach Bio::PrimarySeqI objects to SeqFeatures");
939 }
940
941 $self->{'_gsf_seq'} = $arg;
942
943 # attach to sub features if they want it
944
945 foreach my $sf ($self->sub_SeqFeature) {
946 if ($sf->can("attach_seq")) {
947 $sf->attach_seq($arg);
948 }
949 }
950 }
951 #print STDERR "contig is ".$self->{'_gsf_seq'}." with name ".$self->{'_gsf_seq'}->name."\n" unless(!$self->{'_gsf_seq'});
952 # my ($p, $f, $l) = caller;
953 # print STDERR "Caller = ".$f." ".$l."\n";
954 return $self->{'_gsf_seq'};
955 }
956  
957  
958  
959 sub is_splittable {
960 my ($self, $arg) = @_;
961  
962 if (defined $arg) {
963 $self->{'_is_splittable'} = $arg;
964 }
965 return $self->{'_is_splittable'};
966 }
967  
968  
969 sub transform {
970 my ($self, $slice) = @_;
971  
972 unless (defined $slice) {
973  
974 if ((defined $self->contig) &&
975 ($self->contig->isa("Bio::EnsEMBL::RawContig"))) {
976  
977 # we are already in rawcontig coords, nothing needs to be done
978 return $self;
979  
980 }
981 else {
982 # transform to raw_contig coords from Slice coords
983 return $self->_transform_to_RawContig();
984 }
985 }
986  
987 if (defined $self->contig) {
988  
989 if ($self->contig->isa("Bio::EnsEMBL::RawContig")) {
990  
991 # transform to slice coords from raw contig coords
992 return $self->_transform_to_Slice($slice);
993 }
994 elsif ($self->contig->isa( "Bio::EnsEMBL::Slice" ) or $self->contig->isa( "Bio::EnsEMBL::LRGSlice" )) {
995  
996 # transform to slice coords from other slice coords
997 return $self->_transform_between_Slices($slice);
998 }
999 else {
1000  
1001 # Unknown contig type
1002 $self->throw("Cannot transform unknown contig type @{[$self->contig]}");
1003 }
1004 }
1005 else {
1006  
1007 #Can't convert to slice coords without a contig to work with
1008 return $self->throw("Object's contig is not defined - cannot transform");
1009 }
1010  
1011 }
1012  
1013  
1014 sub _transform_to_Slice {
1015 my ($self, $slice) = @_;
1016  
1017 $self->throw("can't transform coordinates of $self without a contig defined")
1018 unless $self->contig;
1019  
1020 unless($self->contig->adaptor) {
1021 $self->throw("cannot transform coordinates of $self without adaptor " .
1022 "attached to contig");
1023 }
1024  
1025 my $dbh = $self->contig->adaptor->db;
1026  
1027 my $mapper =
1028 $dbh->get_AssemblyMapperAdaptor->fetch_by_type($slice->assembly_type);
1029 my $rca = $dbh->get_RawContigAdaptor;
1030  
1031 my @mapped = $mapper->map_coordinates_to_assembly(
1032 $self->contig->dbID,
1033 $self->start,
1034 $self->end,
1035 $self->strand
1036 );
1037  
1038 unless (@mapped) {
1039 $self->throw("couldn't map $self to Slice");
1040 }
1041  
1042 unless (@mapped == 1) {
1043 $self->throw("$self should only map to one chromosome - " .
1044 "something bad has happened ...");
1045 }
1046  
1047 if ($mapped[0]->isa("Bio::EnsEMBL::Mapper::Gap")) {
1048 $self->warn("feature lies on gap\n");
1049 return;
1050 }
1051  
1052 if( ! defined $slice->chr_name() ) {
1053 my $slice_adaptor = $slice->adaptor();
1054 %$slice = %{$slice_adaptor->fetch_by_chr_name( $mapped[0]->id() )};
1055 }
1056
1057 # mapped coords are on chromosome - need to convert to slice
1058 if($slice->strand == 1) {
1059 $self->start ($mapped[0]->start - $slice->chr_start + 1);
1060 $self->end ($mapped[0]->end - $slice->chr_start + 1);
1061 $self->strand ($mapped[0]->strand);
1062 } else {
1063 $self->start ($slice->chr_end - $mapped[0]->end + 1);
1064 $self->end ($slice->chr_end - $mapped[0]->start + 1);
1065 $self->strand ($mapped[0]->strand * -1);
1066 }
1067  
1068 $self->seqname($mapped[0]->id);
1069  
1070 #set the contig to the slice
1071 $self->contig($slice);
1072  
1073 return $self;
1074 }
1075  
1076  
1077 sub _transform_between_Slices {
1078 my ($self, $to_slice) = @_;
1079  
1080 my $from_slice = $self->contig;
1081  
1082 $self->throw("New contig [$to_slice] is not a Bio::EnsEMBL::Slice")
1083 unless ($to_slice->isa("Bio::EnsEMBL::Slice") or $to_slice->isa("Bio::EnsEMBL::LRGSlice") );
1084  
1085 if ((my $c1 = $from_slice->chr_name) ne (my $c2 = $to_slice->chr_name)) {
1086 $self->warn("Can't transform between chromosomes: $c1 and $c2");
1087 return;
1088 }
1089  
1090 my($start, $end, $strand);
1091  
1092 #first convert to assembly coords
1093 if($from_slice->strand == 1) {
1094 $start = $from_slice->chr_start + $self->start - 1;
1095 $end = $from_slice->chr_start + $self->end - 1;
1096 $strand = $self->strand;
1097 } else {
1098 $start = $from_slice->chr_end - $self->end + 1;
1099 $end = $from_slice->chr_end - $self->start + 1;
1100 $strand = $self->strand;
1101 }
1102  
1103 #now convert to the other slice's coords
1104 if($to_slice->strand == 1) {
1105 $self->start ($start - $to_slice->chr_start + 1);
1106 $self->end ($end - $to_slice->chr_start + 1);
1107 $self->strand($strand);
1108 } else {
1109 $self->start ($to_slice->chr_end - $end + 1);
1110 $self->end ($to_slice->chr_end - $start + 1);
1111 $self->strand($strand * -1);
1112 }
1113  
1114 $self->contig($to_slice);
1115  
1116 return $self;
1117 }
1118  
1119  
1120 sub _transform_to_RawContig {
1121 my($self) = @_;
1122  
1123 #print STDERR "transforming ".$self." to raw contig coords\n";
1124 $self->throw("can't transform coordinates of $self without a contig defined")
1125 unless $self->contig;
1126  
1127 my $slice = $self->contig;
1128  
1129 unless($slice->adaptor) {
1130 $self->throw("can't transform coordinates of $self without an adaptor " .
1131 "attached to the feature's slice");
1132 }
1133  
1134 my $dbh = $slice->adaptor->db;
1135  
1136 my $mapper =
1137 $dbh->get_AssemblyMapperAdaptor->fetch_by_type($slice->assembly_type);
1138 my $rca = $dbh->get_RawContigAdaptor;
1139  
1140 #first convert the features coordinates to assembly coordinates
1141 my($start, $end, $strand);
1142 if($slice->strand == 1) {
1143 $start = $slice->chr_start + $self->start - 1;
1144 $end = $slice->chr_start + $self->end - 1;
1145 $strand = $self->strand;
1146 } else {
1147 $start = $slice->chr_end - $self->end + 1;
1148 $end = $slice->chr_end - $self->start + 1;
1149 $strand = $self->strand * -1;
1150 }
1151  
1152 #convert the assembly coordinates to RawContig coordinates
1153 my @mapped = $mapper->map_coordinates_to_rawcontig(
1154 $slice->chr_name,
1155 $start,
1156 $end,
1157 $strand
1158 );
1159  
1160 unless (@mapped) {
1161 $self->throw("couldn't map $self");
1162 return $self;
1163 }
1164  
1165 if (@mapped == 1) {
1166  
1167 if ($mapped[0]->isa("Bio::EnsEMBL::Mapper::Gap")) {
1168 $self->warn("feature lies on gap\n");
1169 return;
1170 }
1171  
1172 my $rc = $rca->fetch_by_dbID($mapped[0]->id);
1173  
1174 $self->start ($mapped[0]->start);
1175 $self->end ($mapped[0]->end);
1176 $self->strand ($mapped[0]->strand);
1177 $self->seqname ($mapped[0]->id);
1178 #print STDERR "setting contig to be ".$mapped[0]->id."\n";
1179 $self->contig($rca->fetch_by_dbID($mapped[0]->id));
1180  
1181 return $self;
1182 }
1183 else {
1184  
1185 # more than one object returned from mapper
1186 # possibly more than one RawContig in region
1187  
1188 my (@gaps, @coords);
1189  
1190 foreach my $m (@mapped) {
1191  
1192 if ($m->isa("Bio::EnsEMBL::Mapper::Gap")) {
1193 push @gaps, $m;
1194 }
1195 elsif ($m->isa("Bio::EnsEMBL::Mapper::Coordinate")) {
1196 push @coords, $m;
1197 }
1198 }
1199  
1200 # case where only one RawContig maps
1201 if (@coords == 1) {
1202  
1203 $self->start ($coords[0]->start);
1204 $self->end ($coords[0]->end);
1205 $self->strand ($coords[0]->strand);
1206 $self->seqname($coords[0]->id);
1207 #print STDERR "2 setting contig to be ".$coords[0]->id."\n";
1208 $self->contig ($rca->fetch_by_dbID($coords[0]->id));
1209  
1210 $self->warn("Feature [$self] truncated as lies partially on a gap");
1211 return $self;
1212 }
1213  
1214 unless ($self->is_splittable) {
1215 $self->warn("Feature spans >1 raw contig - can't split\n");
1216 return;
1217 }
1218  
1219 my @out;
1220 my $obj = ref $self;
1221  
1222 SPLIT: foreach my $map (@mapped) {
1223  
1224 if ($map->isa("Bio::EnsEMBL::Mapper::Gap")) {
1225 $self->warn("piece of evidence lies on gap\n");
1226 next SPLIT;
1227 }
1228  
1229 my $feat = $obj->new;
1230  
1231 $feat->start ($map->start);
1232 $feat->end ($map->end);
1233 $feat->strand ($map->strand);
1234 #print STDERR "3 setting contig to be ".$mapped[0]->id."\n";
1235 $feat->contig ($rca->fetch_by_dbID($map->id));
1236 $feat->adaptor($self->adaptor) if $self->adaptor();
1237 $feat->display_label($self->display_label) if($self->can('display_label'));
1238 $feat->analysis($self->analysis);
1239 push @out, $feat;
1240 }
1241
1242 return @out;
1243 }
1244 }
1245  
1246  
1247 1;