/
getAnnoFasta.pl
executable file
·257 lines (238 loc) · 7.01 KB
/
getAnnoFasta.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
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
#!/usr/bin/perl
# getAnnoFast.pl
# Creates fasta sequence files from the AUGUSTUS output.
#
# Mario Stanke, 10.05.2007
#
use strict;
use Getopt::Long;
my $usage = "getAnnoFasta.pl augustus.gff\n";
$usage .= " Makes a fasta file with protein sequences (augustus.aa)\n";
$usage .= " and one with coding sequences (augustus.codingseq)\n";
$usage .= " from the sequences provided in the comments of the AUGUSTUS output.\n";
$usage .= " These sequence comments are turned on with --protein=on and --codingseq=on, respectively\n";
$usage .= "Options:\n";
$usage .= " --seqfile=s Input a fasta file with the genomic sequences that AUGUSTUS was run on.\n";
$usage .= " When this option is given, an additional file with the individual\n";
$usage .= " coding exon sequences (augustus.cdsexons) is output.\n";
$usage .= " and a file with the complete mRNA including UTRs (augustus.mrna) is output.\n";
$usage .= " --chop_cds for incomplete transcripts: cut off bases before first codon.\n";
my ($seqname, $trid, $status, $haveCod, $haveAA, $haveCDS, $haveRNA, $seq, $seqfile, $chop_cds);
GetOptions('seqfile=s'=>\$seqfile, 'chop_cds!'=>\$chop_cds);
if ($#ARGV != 0) {
print $usage;
exit;
}
my $separator = ";";
my $augustusfilename = $ARGV[0];
open(AUG, "<$augustusfilename") || die "Couldn't open $augustusfilename\n";
my $stemfilename = $augustusfilename;
$stemfilename =~ s/(\.gff|\.gtf|.gff3|\.txt)//;
my %sequence = (); # Hash with the DNA sequence. The sequence names are the keys.
# Read in the sequence file in one chunk.
# And sort it in the sequence hash.
# Yes, this requires a lot of memory for large genomes.
if ($seqfile){
open (SEQ, "<$seqfile") or die ("Could not open sequence file $seqfile\n");
$/=">";
while (<SEQ>){
s/>$//;
next unless /\S+/;
/(.*)\n/;
$seqname = $1;
my $sequencepart = $'; #'
$seqname =~ s/\s.*//; # seqname only up to first white space
$sequencepart =~ s/\s//g;
$sequence{$seqname} = $sequencepart;
}
print "Read in " . (scalar keys %sequence) . " sequence(s) from $seqfile.\n";
}
$/="\n";
#
# Go through the augustus output transcript by transcript.
#
$haveCod = $haveAA = $haveCDS = $haveRNA = 0;
$status = 0;
my $exonUTRFormat = 0; # UTR implicitly given by exon features
my $UTRFormat = 0; # UTR explicitly given by *UTR features
my $cdsSeq = "";
my $aaSeq = "";
my %cdsnr = ();
my %cdsTx = (); #keys transcript id, values concatenated coding exon sequences
my %mrnaTx = (); #keys transcript id, values concatenated exon sequences (including UTR)
my %strandTx = (); #keys transcript id, values strands
my %frameTx = (); #keys transcript id, values frames
while(<AUG>) {
if ($seqfile && (/^(\S+)\t\S+\t(\S+)\t(\d+)\t(\d+)\t\S+\t(\S+)\t(\S+)\ttranscript_id "([^"]*)"; gene_id "([^"]*)";$/
|| /^(\S+)\t\S+\t(\S+)\t(\d+)\t(\d+)\t\S+\t(\S+)\t(\S+)\t.*Parent=([^;]+)/)){
my $feat = $2;
$seqname = $1;
my $start = $3;
my $end = $4;
my $strand = $5;
my $frame = $6;
$trid=$7;
$trid =~ s/\s$//;
next unless ($feat eq "CDS" || $feat =~ /UTR/ || $feat eq "exon");
# decide whether to use exon or UTR format for mRNA by whether we see UTR or exon first
$UTRFormat = 1 if (!$exonUTRFormat && $feat =~ /UTR/);
$exonUTRFormat = 1 if (!$UTRFormat && $feat eq "exon");
$cdsnr{$trid}++ if ($feat eq "CDS");
my $seqpart = lc(substr($sequence{$seqname}, $start-1, $end - $start + 1));
#print "$seqname $trid CDS $cdsnr{$trid} $start -> $end $seqpart\n";
if ($seqpart ne "") {
# add mRNA if applicable
if (($exonUTRFormat && $feat eq "exon") ||
($UTRFormat && ($feat eq "CDS" || $feat =~ /UTR/))) {
$mrnaTx{$trid} = "" if (!defined($mrnaTx{$trid}));
$mrnaTx{$trid} .= $seqpart;
}
if ($feat eq "CDS"){
if (!$haveCDS) {
open (CDSEXON, ">$stemfilename.cdsexons");
$haveCDS++;
}
$cdsTx{$trid} = "" if (!defined($cdsTx{$trid}));
$cdsTx{$trid} .= $seqpart;
push(@{$frameTx{$trid}}, $frame);
if ($strand eq '-') {
$seqpart = rc($seqpart);
$strandTx{$trid} = $strand;
}
print CDSEXON ">$trid.cds" . $cdsnr{$trid} . "\n$seqpart\n";
}
}
}
if (/^(\S+)\t.*\ttranscript_id "([^"]*)"; gene_id "([^"]*)";$/ ||
/^(\S+)\t.*Parent=([^;]+)/){
$seqname=$1;
$trid=$2;
$trid =~ s/\s$//;
$status=1;
} elsif (/coding sequence = \[(.*)/ && $status == 1){
if ($haveCod == 0) {
open (COD, ">$stemfilename.codingseq");
}
$haveCod++;
$seq = $1;
$seq =~ s/\]$//;
print COD ">$seqname.$trid\n$seq\n";
$status=2;
} elsif ($status == 2 && /^\# ([\w\]]*)$/){
$seq = $1;
$seq =~ s/\]$//;
print COD "$seq\n";
$status=2;
} elsif (/protein sequence = \[(.*)/ && $status >= 1){
if ($haveAA == 0) {
open (AA, ">$stemfilename.aa");
}
$haveAA++;
$seq = $1;
$seq =~ s/\]$//;
# print AA ">$seqname$separator$trid\n$seq\n";
# print AA ">$trid\n";
$aaSeq .= $seq;
if (!/\]/){
$status=3;
} else {
if ($aaSeq ne ""){
print AA ">$trid\n";
print AA getFa($aaSeq, 100);
}
$aaSeq = "";
$status=1;
}
} elsif ($status == 3 && /^\# (.*)/){
$seq = $1;
$seq =~ s/\]$//;
# print AA "$seq\n";
$aaSeq .= $seq;
if (!/\]/){
$status=3;
} else {
if ($aaSeq ne ""){
print AA ">$trid\n";
print AA getFa($aaSeq, 100);
}
$aaSeq = "";
$status=1;
}
}
}
#
# print coding sequences, if not already done (because included in output)
#
if (!$haveCod && scalar(keys %cdsTx)>0){
open (COD, ">$stemfilename.codingseq") or die ("Could not open $stemfilename.codingseq for writing.");
foreach my $trid (sort by_id keys %cdsTx){
print COD ">$trid\n";
my $codingseq = $cdsTx{$trid};
$codingseq = rc($codingseq) if ($strandTx{$trid} eq "-");
if($chop_cds){
if ($strandTx{$trid} eq "-"){
if($frameTx{$trid}[-1] != 0){
$codingseq = substr($codingseq, $frameTx{$trid}[-1]);
}
}else{
if($frameTx{$trid}[0] != 0){
$codingseq = substr($codingseq, $frameTx{$trid}[0]);
}
}
}
print COD getFa($codingseq);
}
}
#
# print mRNA sequences
#
if (scalar(keys %mrnaTx)>0){
open (MRNA, ">$stemfilename.mrna") or die ("Could not open $stemfilename.mrna for writing.");
foreach my $trid (sort by_id keys %mrnaTx){
print MRNA ">$trid\n";
my $mrnaseq = $mrnaTx{$trid};
$mrnaseq = rc($mrnaseq) if ($strandTx{$trid} eq "-");
print MRNA getFa($mrnaseq);
}
}
#
# sort by increasing transcript id
#
sub by_id{
$a =~ /g(\d+)\.t(\d+)/;
my ($ag,$at)=($1,$2);
$b =~ /g(\d+)\.t(\d+)/;
my ($bg,$bt)=($1,$2);
if ($ag>$bg){
return 1;
} elsif ($bg>$ag){
return -1;
} else {
return $at <=> $bt;
}
}
# reverse complement
sub rc{
my $s = shift;
$s = reverse $s;
$s =~ s/a/T/g;
$s =~ s/c/G/g;
$s =~ s/g/C/g;
$s =~ s/t/A/g;
$s = lc $s;
return $s;
}
sub getFa{
my $seq = shift;
my $cols = 100;
$cols = shift if (@_);
my $start = 0;
my $ret = "";
while (length($seq)-$start >= $cols) {
my $shortline = substr($seq, $start, $cols);
$ret .= "$shortline\n";
$start += $cols;
}
$ret .= substr($seq, $start, $cols) . "\n" if ($start<length($seq));
return $ret;
}