| 1 | #line 1 "inc/Bio/GapSeq.pm - /home/dave/lib//Bio/GapSeq.pm" | ||
| 2 | # | ||
| 3 | # BioPerl module for FAST::Bio::Seq::GapSeq | ||
| 4 | # | ||
| 5 | # Cared for by | ||
| 6 | # | ||
| 7 | # Copyright | ||
| 8 | # | ||
| 9 | # You may distribute this module under the same terms as perl itself | ||
| 10 | |||
| 11 | # POD documentation - main docs before the code | ||
| 12 | |||
| 13 | #line 56 | ||
| 14 | |||
| 15 | |||
| 16 | # Let the code begin... | ||
| 17 | |||
| 18 | package FAST::Bio::GapSeq; | ||
| 19 | use vars qw($AUTOLOAD @ISA); | ||
| 20 | use strict; | ||
| 21 | |||
| 22 | # Object preamble - inherits from FAST::Bio::Root::Object | ||
| 23 | |||
| 24 | use FAST::Bio::Seq; | ||
| 25 | use FAST::Bio::GapSeqI; | ||
| 26 | |||
| 27 | @ISA = qw(FAST::Bio::Seq FAST::Bio::GapSeqI); | ||
| 28 | |||
| 29 | |||
| 30 | #line 83 | ||
| 31 | |||
| 32 | sub new { | ||
| 33 | # standard new call.. | ||
| 34 | my($caller,@args) = @_; | ||
| 35 | my $self = $caller->SUPER::new(@args); | ||
| 36 | |||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | #line 102 | ||
| 41 | |||
| 42 | sub copy { | ||
| 43 | ## not the standard constructor | ||
| 44 | my($caller,$seq_obj) = @_; | ||
| 45 | my $self = $caller->SUPER::new(); | ||
| 46 | unless (UNIVERSAL::isa($seq_obj,"FAST::Bio::Seq")) { | ||
| 47 | $self->throw("Argument to copy constructor must be a FAST::Bio::Seq.\n"); | ||
| 48 | } | ||
| 49 | |||
| 50 | $self->alphabet($seq_obj->alphabet); | ||
| 51 | $self->seq($seq_obj->seq); | ||
| 52 | $self->display_id($seq_obj->display_id); | ||
| 53 | $self->accession_number($seq_obj->accession_number); | ||
| 54 | $self->primary_id($seq_obj->primary_id); | ||
| 55 | $self->desc($seq_obj->desc); | ||
| 56 | return $self; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | #line 135 | ||
| 61 | |||
| 62 | |||
| 63 | sub translate { | ||
| 64 | my($self) = shift; | ||
| 65 | my($stop, $unknown, $frame, $tableid, $fullCDS, $throw) = @_; | ||
| 66 | my($i, $len, $output) = (0,0,''); | ||
| 67 | my $aa; | ||
| 68 | #DHA: IN THIS VERSION WE WILL ASSUME THAT GAP IS '-' | ||
| 69 | my $ingap = '-'; | ||
| 70 | my $outgap = $ingap; | ||
| 71 | |||
| 72 | ## User can pass in symbol for stop and unknown codons | ||
| 73 | unless(defined($stop) and $stop ne '') { $stop = "*"; } | ||
| 74 | unless(defined($unknown) and $unknown ne '') { $unknown = "X"; } | ||
| 75 | unless(defined($frame) and $frame ne '') { $frame = 0; } | ||
| 76 | |||
| 77 | ## the codon table ID | ||
| 78 | unless(defined($tableid) and $tableid ne '') { $tableid = 1; } | ||
| 79 | |||
| 80 | ##Error if monomer is "Amino" | ||
| 81 | $self->throw("Can't translate an amino acid sequence.") if | ||
| 82 | ($self->alphabet eq 'protein'); | ||
| 83 | |||
| 84 | ##Error if frame is not 0, 1 or 2 | ||
| 85 | $self->throw("Valid values for frame are 0, 1, 2, not [$frame].") unless | ||
| 86 | ($frame == 0 or $frame == 1 or $frame == 2); | ||
| 87 | |||
| 88 | #thows a warning if ID is invalid | ||
| 89 | my $codonTable = FAST::Bio::Tools::CodonTable->new( -id => $tableid); | ||
| 90 | my ($seq) = $self->seq(); | ||
| 91 | |||
| 92 | |||
| 93 | #DHA: | ||
| 94 | # FRAME IS DETERMINED BY THE FIRST NON-GAP CHARACTERS INSTEAD OF THE | ||
| 95 | # HEAD OF THE SEQUENCE. | ||
| 96 | |||
| 97 | ##DHA: why was this here? $seq .= 'n'; | ||
| 98 | my $length = length $seq; | ||
| 99 | my $codon = ''; | ||
| 100 | my $frame_copy = $frame; | ||
| 101 | my $frame_not_dealt_with = 1; | ||
| 102 | if ($frame == 0) { | ||
| 103 | $frame_not_dealt_with = 0; | ||
| 104 | } | ||
| 105 | |||
| 106 | CHAR: for ($i = 0 ; $i < $length ; $i++) { | ||
| 107 | my $char = substr($seq, $i, 1); | ||
| 108 | if ($char eq $ingap) { | ||
| 109 | $output .= $outgap; | ||
| 110 | } | ||
| 111 | elsif ($frame_not_dealt_with == 1) { | ||
| 112 | $output .= $outgap; | ||
| 113 | $frame_copy--; | ||
| 114 | if ($frame_copy != 0) { | ||
| 115 | next CHAR; | ||
| 116 | } | ||
| 117 | else { | ||
| 118 | $frame_not_dealt_with = 0; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | $codon .= $char; | ||
| 123 | if (length $codon != 3){ | ||
| 124 | $output .= $outgap; | ||
| 125 | next CHAR; | ||
| 126 | } | ||
| 127 | else { ## COMPLETE CODON, TRANSLATE | ||
| 128 | my $aa = $codonTable->translate($codon); | ||
| 129 | if ($aa eq '*') { | ||
| 130 | $output .= $stop; | ||
| 131 | } | ||
| 132 | elsif ($aa eq 'X') { | ||
| 133 | $output .= $unknown; | ||
| 134 | } | ||
| 135 | else { | ||
| 136 | $output .= $aa ; | ||
| 137 | } | ||
| 138 | $codon = ''; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | ## DHA: FOR THE FOLLOWING TESTS, USE UNGAPPED SEQUENCE | ||
| 143 | my $ungapped_output = $output; | ||
| 144 | $ungapped_output =~ s/$outgap//g; | ||
| 145 | # only if we are expecting to translate a complete coding region | ||
| 146 | if ($fullCDS) { | ||
| 147 | my $id = $self->display_id; | ||
| 148 | #remove the stop character | ||
| 149 | if( substr($ungapped_output,-1,1) eq $stop ) { | ||
| 150 | chop $ungapped_output; | ||
| 151 | } else { | ||
| 152 | $throw && $self->throw("Seq [$id]: Not using a valid terminator codon!"); | ||
| 153 | $self->warn("Seq [$id]: Not using a valid terminator codon!"); | ||
| 154 | } | ||
| 155 | # test if there are terminator characters inside the protein sequence! | ||
| 156 | if ($ungapped_output =~ /\*/) { | ||
| 157 | $throw && $self->throw("Seq [$id]: Terminator codon inside CDS!"); | ||
| 158 | $self->warn("Seq [$id]: Terminator codon inside CDS!"); | ||
| 159 | } | ||
| 160 | # if the initiator codon is not ATG, the amino acid needs to changed into M | ||
| 161 | if ( substr($ungapped_output,0,1) ne 'M' ) { | ||
| 162 | if ($codonTable->is_start_codon(substr($seq, 0, 3)) ) { | ||
| 163 | $ungapped_output = 'M'. substr($ungapped_output,1); | ||
| 164 | } | ||
| 165 | elsif ($throw) { | ||
| 166 | $self->warn("Seq [$id]: Not using a valid initiator codon!"); | ||
| 167 | } else { | ||
| 168 | $self->throw("Seq [$id]: Not using a valid initiator codon!"); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | my $seqclass; | ||
| 174 | if($self->can_call_new()) { | ||
| 175 | $seqclass = ref($self); | ||
| 176 | } else { | ||
| 177 | $seqclass = 'FAST::Bio::PrimarySeq'; | ||
| 178 | $self->_attempt_to_load_Seq(); | ||
| 179 | } | ||
| 180 | my $out = $seqclass->new( '-seq' => $output, | ||
| 181 | '-display_id' => $self->display_id, | ||
| 182 | '-accession_number' => $self->accession_number, | ||
| 183 | # is there anything wrong with retaining the | ||
| 184 | # description? | ||
| 185 | '-desc' => $self->desc(), | ||
| 186 | '-alphabet' => 'protein' | ||
| 187 | ); | ||
| 188 | return $out; | ||
| 189 | |||
| 190 | } | ||
| 191 | |||
| 192 | 1; |