EnsEMBL/Utils/PolyA.pm untested

filter: Statementstmt 0.0% 0 / 129 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 21 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 215 CC 24 SCAR 50.5
CC
24
Cov
0%
_find_polyA
306.0
new
6.0
clip
6.0
CRAP 155.6
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::Utils::PolyA
34  
35 =head1 SYNOPSIS
36  
37 my $seq; # a Bio::Seq object
38 my $polyA = Bio::EnsEMBL::Utils::PolyA->new();
39  
40 # returns a new Bio::Seq object with the trimmed sequence
41 my $trimmed_seq = $polyA->clip($seq);
42  
43 # cat put Ns in the place of the polyA/polyT tail
44 my $masked_seq = $polyA->mask($seq);
45  
46 # can put in lower case the polyA/polyT using any flag:
47 my $softmasked_seq = $poly->mask( $seq, 'soft' );
48  
49 =head1 DESCRIPTION
50  
51 It reads a Bio::Seq object, it first finds out whether it has a
52 polyA or a polyT and then performs one operation in the seq string:
53 clipping, masking or softmasking. It then returns a new Bio::Seq
54 object with the new sequence.
55  
56 =head1 METHODS
57  
58 =cut
59  
60 package Bio::EnsEMBL::Utils::PolyA;
61 $Bio::EnsEMBL::Utils::PolyA::VERSION = '114.0.0_50'; # TRIAL
62 $Bio::EnsEMBL::Utils::PolyA::VERSION = '114.0.050';
63 use Bio::Seq;
64 use vars qw(@ISA);
65  
66 use strict;
67  
68  
69 =head2 new
70  
71 =cut
72  
73 sub new{
74 my ($class) = @_;
75 if (ref($class)){
76 $class = ref($class);
77 }
78 my $self = {};
79 bless($self,$class);
80
81 return $self;
82 }
83  
84  
85 ############################################################
86  
87 sub clip{
88 my ($self, $bioseq) = @_;
89  
90 # print STDERR "past a $bioseq\n";
91 my $seq = $bioseq->seq;
92 $self->_clip(1);
93 $self->_mask(0);
94 $self->_softmask(0);
95 my $new_seq = $self->_find_polyA($seq);
96 my $cdna = Bio::Seq->new();
97 if (length($new_seq) > 0){
98 $cdna->seq($new_seq);
99 }
100 else {
101 print "While clipping the the polyA tail, sequence ".$bioseq->display_id." totally disappeared.\n";
102 print "Returning undef\n";
103 return undef;
104 }
105 $cdna->display_id( $bioseq->display_id );
106 $cdna->desc( $bioseq->desc );
107
108 return $cdna;
109 }
110  
111 ############################################################
112  
113 sub mask{
114 my ($self, $bioseq, $flag ) = @_;
115
116 my $seq = $bioseq->seq;
117 $self->_clip(0);
118 if ( $flag ){
119 $self->_mask(0);
120 $self->_softmask(1);
121 }
122 else{
123 $self->_mask(1);
124 $self->_softmask(0);
125 }
126 my $new_seq = $self->_find_polyA($seq);
127 my $cdna = new Bio::Seq;
128 $cdna->seq($new_seq);
129 $cdna->display_id( $bioseq->display_id );
130 $cdna->desc( $bioseq->desc );
131
132 return $cdna;
133 }
134  
135 ############################################################
136  
137  
138  
139  
140 ############################################################
141  
142 sub _find_polyA{
143 my ($self, $seq) = @_;
144 my $new_seq;
145 my $length = length($seq);
146
147 # is it a polyA or polyT?
148 my $check_polyT = substr( $seq, 0, 6 );
149
150 my $check_polyA = substr( $seq, -6 );
151
152 my $t_count = $check_polyT =~ tr/Tt//;
153 my $a_count = $check_polyA =~ tr/Aa//;
154
155 #### polyA ####
156 if ( $a_count >= 5 && $a_count > $t_count ){
157
158 # we calculate the number of bases we want to chop
159 my $length_to_mask = 0;
160
161 # we start with 3 bases
162 my ($piece, $count ) = (3,0);
163
164 # count also the number of Ns, consider the Ns as potential As
165 my $n_count = 0;
166  
167 # take 3 by 3 bases from the end
168 while( $length_to_mask < $length ){
169 my $chunk = substr( $seq, ($length - ($length_to_mask + 3)), $piece);
170 $count = $chunk =~ tr/Aa//;
171 $n_count = $chunk =~ tr/Nn//;
172 if ( ($count + $n_count) >= 2*( $piece )/3 ){
173 $length_to_mask += 3;
174 }
175 else{
176 last;
177 }
178 }
179
180 if ( $length_to_mask > 0 ){
181 # do not mask the last base if it is not an A:
182 my $last_base = substr( $seq, ( $length - $length_to_mask ), 1);
183 my $previous_to_last = substr( $seq, ( $length - $length_to_mask - 1), 1);
184 if ( !( $last_base eq 'A' || $last_base eq 'a') ){
185 $length_to_mask--;
186 }
187 elsif( $previous_to_last eq 'A' || $previous_to_last eq 'a' ){
188 $length_to_mask++;
189 }
190 my $clipped_seq = substr( $seq, 0, $length - $length_to_mask );
191 my $mask;
192 if ( $self->_clip ){
193 $mask = "";
194 }
195 elsif( $self->_mask ){
196 $mask = "N" x ($length_to_mask);
197 }
198 elsif ( $self->_softmask ){
199 $mask = lc substr( $seq, ( $length - $length_to_mask ) );
200 }
201 $new_seq = $clipped_seq . $mask;
202 }
203 else{
204 $new_seq = $seq;
205 }
206 }
207 #### polyT ####
208 elsif( $t_count >=5 && $t_count > $a_count ){
209
210 # calculate the number of bases to chop
211 my $length_to_mask = -3;
212
213 # we start with 3 bases:
214 my ($piece, $count) = (3,3);
215
216 # count also the number of Ns, consider the Ns as potential As
217 my $n_count = 0;
218
219 # take 3 by 3 bases from the beginning
220 while ( $length_to_mask < $length ){
221 my $chunk = substr( $seq, $length_to_mask + 3, $piece );
222 #print STDERR "length to mask: $length_to_mask\n";
223 #print "chunk: $chunk\n";
224 $count = $chunk =~ tr/Tt//;
225 $n_count = $chunk =~ tr/Nn//;
226 if ( ($count+$n_count) >= 2*( $piece )/3 ){
227 $length_to_mask +=3;
228 }
229 else{
230 last;
231
232 }
233 }
234 if ( $length_to_mask >= 0 ){
235 # do not chop the last base if it is not a A:
236 #print STDERR "clipping sequence $seq\n";
237 my $last_base = substr( $seq, ( $length_to_mask + 3 - 1 ), 1 );
238 my $previous_to_last = substr( $seq, ( $length_to_mask + 3 ), 1 );
239 if ( !( $last_base eq 'T' || $last_base eq 't' ) ){
240 $length_to_mask--;
241 }
242 elsif( $previous_to_last eq 'T' || $previous_to_last eq 't' ){
243 $length_to_mask++;
244 }
245 my $clipped_seq = substr( $seq, $length_to_mask + 3);
246 my $mask;
247 if ( $self->_clip ){
248 $mask = "";
249 }
250 elsif( $self->_mask ){
251 $mask = "N" x ($length_to_mask+3);
252 }
253 elsif ($self->_softmask){
254 $mask = lc substr( $seq, 0, ($length_to_mask + 3) );
255 }
256 $new_seq = $mask.$clipped_seq;
257 }
258 else{
259 $new_seq = $seq;
260 }
261 }
262 else{
263 # we cannot be sure of what it is
264 # do not clip
265 $new_seq = $seq;
266 }
267  
268 return $new_seq;
269 }
270  
271 ############################################################
272  
273 sub _mask{
274 my ($self,$mask) = @_;
275 if (defined($mask)){
276 $self->{_mask} = $mask;
277 }
278 return $self->{_mask};
279 }
280  
281 ############################################################
282  
283 sub _clip{
284 my ($self,$clip) = @_;
285 if (defined($clip)){
286 $self->{_clip} = $clip;
287 }
288 return $self->{_clip};
289 }
290  
291 ############################################################
292  
293 sub _softmask{
294 my ($self,$softmask) = @_;
295 if (defined($softmask)){
296 $self->{_softmask} = $softmask;
297 }
298 return $self->{_softmask};
299 }
300  
301 ############################################################
302  
303  
304 sub has_polyA_track{
305 my ($self, $seq) = @_;
306 my $new_seq;
307 my $length = length($seq);
308
309 # is it a polyA or polyT?
310 my $check_polyT = substr( $seq, 0, 10 );
311
312 my $check_polyA = substr( $seq, -10 );
313
314 print STDERR "polyA: $check_polyA\n";
315  
316 my $t_count = $check_polyT =~ tr/Tt//;
317 my $a_count = $check_polyA =~ tr/Aa//;
318
319 ## testing with this short cut
320 if ( $a_count >=7 || $t_count >=7 ){
321 return 1;
322 }
323 else{
324 return 0;
325 }
326 }
327  
328  
329 ################
330 1;
331