forked from josephhughes/Sequence-manipulation
-
Notifications
You must be signed in to change notification settings - Fork 2
/
longestORF.pl
executable file
·150 lines (120 loc) · 4.18 KB
/
longestORF.pl
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
#!/usr/bin/perl
# longorf.pl v0208020920
# (c) Dan Kortschak 2002
use vars qw($USAGE);
use strict;
use Getopt::Long;
use Bio::SeqIO;
$USAGE = "longorf [--help] [--notstrict] [--verbose] [--graph] [--width printwidth] [--format seqformat] --input seqfile\n";
my ($sequencefile,$sequenceformat,$notstrict,$graph,$verb,$printwidth,$help) =
(undef, 'fasta', undef, undef,undef,50, undef);
&GetOptions('input|i=s' => \$sequencefile,
'format|f=s' => \$sequenceformat,
'notstrict|n' => \$notstrict,
'width|w=s' => \$printwidth,
'graph|g' => \$graph,
'verbose|v' => \$verb,
'help|h' => \$help,
);
if ($help) {
exec('perldoc', $0);
die;
}
if (!defined $sequencefile) {
die($USAGE . "\nPlease specify an input filename.\n");
}
sub longestORF {
my $best=0;
my ($bests,$beste,$beststrand)=(-1,-1,0);
my $bestorf="";
my $relaxed=$_[1];
my $dna=Bio::Seq->new(-seq => $_[0]);
my %strand=('+'=>$dna->seq,
'-'=>$dna->revcom->seq);
foreach my $direction (keys %strand) {
my @starts=();
my @ends=();
if ($relaxed) {
for (my $frame=0;$frame<3;$frame++) {
unless ($strand{$direction}=~m/^.{$frame}(taa|tga|tag)/i) {
push @starts,$frame+1;
}
}
}
while ($strand{$direction}=~m/(atg)/gi) {
push @starts,pos($strand{$direction})-2;
}
while ($strand{$direction}=~m/(taa|tga|tag)/gi) {
push @ends,pos($strand{$direction})-2;
}
push @ends,($dna->length-2,$dna->length-1,$dna->length);
for my $s (@starts) {
for my $e (@ends) {
if ($e%3==$s%3 and $e>$s) {
if ($e-$s>$best) {
$best=$e-$s;
($bests,$beste,$beststrand)=($s,$e,$direction);
$bestorf=Bio::Seq->new(-seq=>$strand{$direction})->subseq($s,$e);
}
last
} else {
next
}
}
}
}
return ($best,$bests,$beste,$beststrand,$bestorf);
}
my $seqio = new Bio::SeqIO('-format' => $sequenceformat,
'-file' => $sequencefile );
my ($length,$start,$end,$direction,$sequence);
my $count=0;
my @lengths;
my $totallength=0;
while (my $dna = $seqio->next_seq) {
$count++;
($length,$start,$end,$direction,$sequence)=longestORF($dna->seq,$notstrict);
if ($verb) {
print $dna->display_id," ",$dna->desc,": ";
print "$length, $start, $end ($direction)\n$sequence\n\n",Bio::Seq->new(-seq=>$sequence)->translate->seq,"\n\n--\n\n";
}
$totallength+=$length;
$lengths[$length/3]++;
}
print "Average ORF length: ", $totallength/$count,"\n\n";
print "Length distribution is:\n";
if ($graph) {
my $length;
my $maxlength=0;
for ($length=0;$length<@lengths;$length++) {
$lengths[$length]=0 unless $lengths[$length];
$maxlength=$lengths[$length] if ($lengths[$length]>$maxlength);
}
for ($length=0;$length<@lengths;$length++) {
print $length*3,"\t",$lengths[$length],"\t|";
print "#"x(($lengths[$length])*$printwidth/$maxlength);
print "\n";
}
} else {
for ($length=0;$length<@lengths;$length++) {
print $length*3,"\t",($lengths[$length]or"0"),"\n";
}
}
__END__
=head1 NAME
longorf.pl - perl script to find the longest ORF of a sequence
=head1 SYNOPSIS
% longorf.pl [-h] [-n] [-v] [-g] [-w printwidth] [-f seqformat] -i seqfile
=head1 DESCRIPTION
This script will examine a set of nucleotide sequences and determine
the longest ORF in each sequence. ORFs may start at the canonical ATG
or at the beginning of the sequence if the notstrict option is chosen.
The script will output a list of the longest ORF lengths, starts, ends
and strands with the ORF and amino acid sequence if the verbose option
is chosen. A histogram of the longest ORFs in the input set may be
printed by choosing the graph option.
=head1 FEEDBACK
This script is not supported by anyone, but requests can be made to the
author.
=head1 AUTHOR - Dan Kortschak <[email protected]>
=cut