-
Notifications
You must be signed in to change notification settings - Fork 2
/
ORF_finder.pm
executable file
·297 lines (243 loc) · 7.41 KB
/
ORF_finder.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/local/bin/perl
package ORF_finder;
use strict;
use Nuc_translator;
sub new {
shift;
## This object stores the longest ORF identified.
my $obj = { pep_seq =>0,
nt_seq => 0,
length => 0, #length of nt_seq
end5 => 0,
end3 => 0,
all_ORFS=>[], #container holds all ORFs found in order of decreasing length. Use orfs() method to retrieve them.
# flags to manipulate orf-finding behaviour:
ALLOW_5PRIME_PARTIALS=>0, #allow for lacking start codon in logest orf.
ALLOW_3PRIME_PARTIALS=>0, #allow for lacking stop codon in longest orf.
FORWARD_STRAND=>1, #default set to true (analyze forward strand)
REVERSE_STRAND=>1, #default set to true.
ALLOW_NON_MET_STARTS=>0 #allow for non-methionine start codons.
};
bless ($obj);
return ($obj);
}
## can include partial orfs at end of sequence.
sub allow_partials {
my $self = shift;
$self->{ALLOW_5PRIME_PARTIALS} = 1;
$self->{ALLOW_3PRIME_PARTIALS} = 1;
}
sub allow_5prime_partials {
my $self = shift;
$self->{ALLOW_5PRIME_PARTIALS} = 1;
}
sub allow_3prime_partials {
my $self = shift;
$self->{ALLOW_3PRIME_PARTIALS} = 1;
}
sub forward_strand_only {
my $self = shift;
$self->{REVERSE_STRAND} = 0;
}
sub reverse_strand_only {
my $self = shift;
$self->{FORWARD_STRAND} = 0;
}
sub allow_non_met_starts {
my $self = shift;
$self->{ALLOW_NON_MET_STARTS} = 1;
}
sub get_longest_orf {
my $self = shift;
my $input_sequence = shift;
unless (ref $self) {
$self = new Longest_orf();
}
unless ($input_sequence) {
print STDERR "I require a cDNA nucleotide sequence as my only parameter\n";
return;
}
unless (length ($input_sequence) >= 3) {
print STDERR "Sequence must code for at least a codon. Your seq_length is too short\n";
return;
}
my @orfList = $self->capture_all_ORFs($input_sequence);
$self->{all_ORFS} = \@orfList;
return ($self);
}
sub capture_all_ORFs {
my $self = shift;
my $input_sequence = shift;
unless ($input_sequence) {
print STDERR "I require a cDNA nucleotide sequence as my only parameter\n";
return;
}
unless (length ($input_sequence) >= 3) {
print STDERR "Sequence must code for at least a codon. Your seq_length is too short\n";
return;
}
$input_sequence = lc ($input_sequence);
my (@starts, @stops, @orfs);
if ($self->{FORWARD_STRAND}) {
## analyse forward position
@stops = $self->identify_putative_stops($input_sequence);
@starts = $self->identify_putative_starts($input_sequence,\@stops);
@orfs = &get_orfs (\@starts, \@stops, $input_sequence, '+');
}
if ($self->{REVERSE_STRAND}) {
## reverse complement sequence and do again
$input_sequence = &revcomp ($input_sequence);
@stops = $self->identify_putative_stops($input_sequence);
@starts = $self->identify_putative_starts($input_sequence, \@stops);
push (@orfs, &get_orfs (\@starts, \@stops, $input_sequence, '-'));
}
if (@orfs) {
## set in order of decreasing length
@orfs = reverse sort {$a->{length} <=> $b->{length}} @orfs;
my $longest_orf = $orfs[0];
my $start = $longest_orf->{start};
my $stop = $longest_orf->{stop};
my $seq = $longest_orf->{sequence};
my $length = length($seq);
my $protein = &get_protein ($seq);
$self->{end5} = $start; ## now coord is seq_based instead of array based.
$self->{end3} = $stop;
$self->{length} = $length;
$self->{nt_seq} = $seq;
$self->{pep_seq} = $protein;
return (@orfs);
}
}
sub orfs {
my $self = shift;
return (@{$self->{all_ORFS}});
}
#####################
# supporting methods
#####################
sub get_end5_end3 {
my $self = shift;
return ($self->{end5}, $self->{end3});
}
sub get_peptide_sequence {
my $self = shift;
return ($self->{pep_seq});
}
sub get_nucleotide_sequence {
my $self = shift;
return ($self->{nt_seq});
}
sub toString {
my $self = shift;
my ($end5, $end3) = $self->get_end5_end3();
my $protein = $self->get_peptide_sequence();
my $nt_seq = $self->get_nucleotide_sequence();
my $ret_string = "Coords: $end5, $end3\n"
. "Protein: $protein\n"
. "Nucleotides: $nt_seq\n";
return ($ret_string);
}
#################################
#Private methods:
sub get_orfs {
my ($starts_ref, $stops_ref, $seq, $direction) = @_;
my %last_delete_pos = ( 0=>-1,
1=>-1,
2=>-1); #store position of last chosen stop codon in spec reading frame.
my @orfs;
my $seq_length = length ($seq);
foreach my $start_pos (@{$starts_ref}) {
my $start_pos_frame = $start_pos % 3;
foreach my $stop_pos (@{$stops_ref}) {
if ( ($stop_pos > $start_pos) && #end3 > end5
( ($stop_pos - $start_pos) % 3 == 0) #must be in-frame
&& ($start_pos > $last_delete_pos{$start_pos_frame})) #only count each stop once.
{
$last_delete_pos{$start_pos_frame} = $stop_pos;
my ($start_pos_adj, $stop_pos_adj) = ( ($start_pos+1), ($stop_pos+1+2));
#print "Startposadj: $start_pos_adj\tStopPosadj: $stop_pos_adj\n";
# sequence based position rather than array-based
my ($start, $stop) = ($direction eq '+') ? ($start_pos_adj, $stop_pos_adj)
: (&revcomp_coord($start_pos_adj, $seq_length), &revcomp_coord($stop_pos_adj, $seq_length));
#print "Start: $start\tStop: $stop\n";
my $orfSeq = substr ($seq, $start_pos, ($stop_pos - $start_pos + 3)); #include the stop codon too.
my $protein = &get_protein($orfSeq);
my $orf = { sequence => $orfSeq,
protein => $protein,
start=>$start,
stop=>$stop,
length=>length($orfSeq),
orient=>$direction
};
push (@orfs, $orf);
last;
}
}
}
return (@orfs);
}
sub identify_putative_starts {
my ($self, $seq, $stops_aref) = @_;
my %starts;
my %stops;
foreach my $stop (@$stops_aref) {
$stops{$stop} = 1;
}
if ($self->{ALLOW_5PRIME_PARTIALS} || $self->{ALLOW_NON_MET_STARTS}) {
$starts{1} = 1 unless $stops{1};
$starts{2} = 1 unless $stops{2};
$starts{3} = 1 unless $stops{3};
}
if (!$self->{ALLOW_NON_MET_STARTS}) { #Look for ATG start codons.
my $start_pos = index ($seq, "atg");
while ($start_pos != -1) {
$starts{$start_pos} = 1;
#print "Start: $start_pos\n";
$start_pos = index ($seq, "atg", ($start_pos + 1));
}
} else {
# find all residues just subsequent to a stop codon, in-frame:
foreach my $stop (@$stops_aref) {
my $candidate_non_met_start = $stop +3;
unless ($stops{$candidate_non_met_start}) {
$starts{$candidate_non_met_start} = 1;
}
}
}
my @starts = sort {$a<=>$b} keys %starts;
return (@starts);
}
sub identify_putative_stops {
my ($self, $seq) = @_;
my %stops;
if ($self->{ALLOW_3PRIME_PARTIALS}) {
## count terminal 3 nts as possible ORF terminators.
my $seq_length = length ($seq);
$stops{$seq_length} = 1;
$seq_length--;
$stops{$seq_length} = 1;
$seq_length--;
$stops{$seq_length} = 1;
}
foreach my $stop_codon ("taa", "tga", "tag") {
my $stop_pos = index ($seq, $stop_codon);
while ($stop_pos != -1) {
$stops{$stop_pos} = 1;
#print "Stop: $stop_pos\n";
$stop_pos = index ($seq, $stop_codon, ($stop_pos + 1)); #include the stop codon too.
}
}
my @stops = sort {$a<=>$b} keys %stops;
return (@stops);
}
sub revcomp {
my ($seq) = @_;
my $reversed_seq = reverse ($seq);
$reversed_seq =~ tr/ACGTacgtyrkm/TGCAtgcarymk/;
return ($reversed_seq);
}
sub revcomp_coord {
my ($coord, $seq_length) = @_;
return ($seq_length - $coord + 1);
}
1;