-
Notifications
You must be signed in to change notification settings - Fork 0
/
ORF_finder.pl
161 lines (92 loc) · 3.15 KB
/
ORF_finder.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
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/perl
use strict;
use warnings;
use Cwd qw(cwd);
my $dir = cwd;
my $orf_path = $ARGV[0];
my $input = $ARGV[1]; #input file to be in BED format;
my $chr_path = $ARGV[2]; #path to the folder that contains the chromosome fasta files;
my $OUTFILE = "$dir/output.txt";
my $tmp_fasta = "$dir/orf_tmp.fasta";
open (INPUT, $input) or die ("Unable to locate $input . \n");
open (OUTFILE, ">$OUTFILE") or die ("Unable to write to $OUTFILE.\n");
my %chr_dict;
sub main {
my @chr_list;
# READ in the input line:
while (<INPUT>) {
chomp;
my @input_entry = split /\t/;
my $chr = $input_entry[0];
my $start = $input_entry[1];
my $end = $input_entry[2];
#check if the chromosome sequence is already in the list:
if #exist:
(grep $_ eq $chr, @chr_list)
{fasta($chr,$start,$end);
orf($chr,$start,$end);}
else #not in the list yet:
{
my $file = "$chr_path/" . $chr . ".fa";
open (DICT, $file) or die ("Unable to open the $file\n");
my $chr_seq;
<DICT>;
while (my $seq1 = <DICT>) {
chomp $seq1;
$chr_seq = $chr_seq . $seq1;
}
$chr_dict{$chr} = $chr_seq; #add the chromosome sequence to the array of dictionaries;
close DICT;
push @chr_list, $chr; #push the chr name to the chr_list array;
{fasta($chr,$start,$end);
orf($chr,$start, $end);}
}
}
close INPUT;
close OUTFILE;
}
sub fasta {
my $chr_q = shift;
my $start_q = shift;
my $end_q = shift;
my $query_seq = substr $chr_dict{$chr_q}, ($start_q-1), ($end_q - $start_q + 1);
open (FASTA, ">$tmp_fasta") or die ("Unable to write to the $tmp_fasta\n");
print FASTA ($query_seq);
close FASTA;
}
sub orf {
my $chr_o = shift;
my $start_o = shift;
my $end_o = shift;
my $cmd = "$orf_path/ORFfinder";
my $ORF_out = "$dir/orf_output.txt";
my $stdErr = "$dir/error.txt";
my $fullCmd = "$cmd -in $tmp_fasta -ml 30 -out $ORF_out 2>$stdErr";
system($fullCmd);
if (-z $ORF_out) #if there is an empty output file produced, i.e. no ORF found;
{unlink $stdErr; #remove the standard error file;
unlink $ORF_out;
unlink $tmp_fasta;
print OUTFILE ($chr_o . "\t" . $start_o . "\t" . $end_o . "\t" . "No ORF" . "\n");}
else {
open (ORF, $ORF_out) or die ("No ORFfinder output found");
<ORF>;
my $orf_seq;
my @orf_array;
while (my $orf_line = <ORF>) {
chomp $orf_line;
if ((substr $orf_line, 0, 1) eq ">") {push @orf_array, $orf_seq; $orf_seq=();}
else {$orf_seq = $orf_seq . $orf_line;
if (eof(ORF)) {push @orf_array, $orf_seq;$orf_seq=();} }
}
close ORF;
my @length_array;
foreach my $data(@orf_array) {push @length_array, length($data);}
my $max = max @length_array;
print OUTFILE ($chr_o . "\t" . $start_o . "\t" . $end_o . "\t" . $max . "\n");
unlink $tmp_fasta;
unlink $ORF_out; #remove the ORF output file;
close $stdErr;
unlink $stdErr;
}}
main();