-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdf-collect-mgrast-proteins.pl
executable file
·275 lines (240 loc) · 7.04 KB
/
sdf-collect-mgrast-proteins.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#! /usr/bin/env perl
use strict vars;
use Carp;
use Data::Dumper;
use Digest::MD5 qw(md5_hex);
use gjoseqlib;
my $usage = "Usage: $0 path\n\n";
my $path = shift @ARGV || '/bigdata/PUP/MEGAHIT';
my @dirs = grep { chomp; /^mgm\d/ && -d "$path/$_" } `ls $path`;
my $caller = 'prodigal';
my $outdir = "MGRAST";
my $assembler = 'MEGAHIT';
my @tabs;
for my $sample (@dirs) {
my $fna = "$path/$sample/final.contigs.fa";
my $gff = "$path/$sample/final.contigs.gene_calls.faa.coords.gff";
-s $fna and -s $gff or next;
my $tabF = process_called_genes_in_mgrast_sample($fna, $gff, $caller, $sample, $outdir);
collect_mg_features($sample, $assembler, 'NObin', $caller, $tabF, 'mgrast');
}
sub collect_mg_features {
my ($sdf, $assembler, $binner, $caller, $input_file, $output_prefix) = @_;
open(IN, "<$input_file") or die "Could not open $input_file";
open(TAB, ">>$output_prefix.tab") or die "Could not open $output_prefix.tab";
open(FNA, ">>$output_prefix.fna") or die "Could not open $output_prefix.fna";
open(FAA, ">>$output_prefix.faa") or die "Could not open $output_prefix.faa";
while (<IN>) {
chomp;
my @cols = split /\t/;
my $id = join('~', $sdf, $assembler, $binner, @cols[0..3]);
my ($nt, $aa, $nt_md5, $aa_md5) = @cols[10, 11, 12, 13];
print FNA ">$id $nt_md5\n$nt\n";
print FAA ">$id $aa_md5\n$aa\n";
print TAB join("\t", $id, $sdf, $assembler, $binner, @cols)."\n";
# last;
}
close(IN);
close(FAA);
close(FNA);
close(TAB);
print "Added $input_file to table: $output_prefix.tab\n";
}
sub process_called_genes_in_mgrast_sample {
my ($fna, $gff, $caller, $sample, $outdir) = @_;
$outdir ||= '.';
my @seqs = gjoseqlib::read_fasta($fna);
my %ctgs = map { $_->[0] => $_->[2] } @seqs;
my @lines = `grep -v "^#" $gff`;
print STDERR join("\t", $sample, scalar@lines)."\n";
my $tabF = "$outdir/$sample.$caller.tab";
open(TAB, ">$tabF") or die "Could not open $tabF";
my $i;
for (@lines) {
# http://useast.ensembl.org/info/website/upload/gff.html
my ($contig, $caller_v, $feature, $start, $end, $score, $strand, $frame) = split /\t/;
next unless $feature eq 'CDS' && $start && $end;
($contig) = split(/\s+/, $contig);
$start += $frame if $strand eq '+';
$end -= $frame if $strand eq '-';
my $cds_id = "CDS.". ++$i;
my ($b, $e) = $strand eq '+' ? ($start, $end) : ($end, $start);
my $loc = "$contig\_$b\_$e";
my $nt = extract_seq(\%ctgs, $loc);
my $gc = calc_gc($nt);
my $aa = translate($nt);
my $nt_md5 = md5_hex(uc $nt);
my $aa_md5 = md5_hex(uc $aa);
print TAB join("\t", 'tb.0', $contig, $caller, $cds_id, $feature, $start, $end, $strand, $score, $gc, $nt, $aa, $nt_md5, $aa_md5)."\n";
}
close(TAB);
return $tabF;
}
# ------------ SEED routines -------------
sub extract_seq {
my($contigs, $loc) = @_;
my($contig, $beg, $end, $contig_seq);
my($plus, $minus);
$plus = $minus = 0;
my $strand = "";
my @loc = split(/,/,$loc);
my @seq = ();
foreach $loc (@loc)
{
if ($loc =~ /^\S+_(\d+)_(\d+)$/)
{
if ($1 < $2)
{
$plus++;
}
elsif ($2 < $1)
{
$minus++;
}
}
}
if ($plus > $minus)
{
$strand = "+";
}
elsif ($plus < $minus)
{
$strand = "-";
}
foreach $loc (@loc)
{
if ($loc =~ /^(\S+)_(\d+)_(\d+)$/)
{
($contig, $beg, $end) = ($1, $2, $3);
my $len = length($contigs->{$contig});
if (!$len)
{
carp "Undefined or zero-length contig $contig";
return "";
}
if (($beg > $len) || ($end > $len))
{
carp "Region $loc out of bounds (contig len=$len)";
}
else
{
if (($beg < $end) || (($beg == $end) && ($strand eq "+")))
{
push(@seq, substr($contigs->{$contig},$beg-1,($end+1-$beg)));
}
else
{
$strand = "-";
push(@seq, complement_DNA_seq(substr($contigs->{$contig},$end-1,($beg+1-$end))));
}
}
}
}
return join("",@seq);
}
sub complement_DNA_seq {
my $seq = reverse shift;
$seq =~ tr[ACGTUKMRSWYBDHVNacgtukmrswybdhvn]
[TGCAAMKYSWRVHDBNtgcaamkyswrvhdbn];
return $seq;
}
sub calc_gc {
my ($nt) = @_;
return undef unless $nt;
my $len = length $nt;
my $cnt = $nt =~ tr/GCgc//;
return sprintf "%.3f", $cnt/$len;
}
# remove trailing *
sub translate {
my( $dna,$code,$start ) = @_;
my( $i,$j,$ln );
my( $x,$y );
my( $prot );
if (! defined($code)) {
$code = &standard_genetic_code;
}
$ln = length($dna);
$prot = "X" x ($ln/3);
$dna =~ tr/a-z/A-Z/;
for ($i=0,$j=0; ($i < ($ln-2)); $i += 3,$j++) {
$x = substr($dna,$i,3);
if ($y = $code->{$x}) {
substr($prot,$j,1) = $y;
}
}
if (($start) && ($ln >= 3) && (substr($dna,0,3) =~ /^[GT]TG$/)) {
substr($prot,0,1) = 'M';
}
# remove trailing *
$prot =~ s/\*$//;
return $prot;
}
sub standard_genetic_code {
my $code = {};
$code->{"AAA"} = "K";
$code->{"AAC"} = "N";
$code->{"AAG"} = "K";
$code->{"AAT"} = "N";
$code->{"ACA"} = "T";
$code->{"ACC"} = "T";
$code->{"ACG"} = "T";
$code->{"ACT"} = "T";
$code->{"AGA"} = "R";
$code->{"AGC"} = "S";
$code->{"AGG"} = "R";
$code->{"AGT"} = "S";
$code->{"ATA"} = "I";
$code->{"ATC"} = "I";
$code->{"ATG"} = "M";
$code->{"ATT"} = "I";
$code->{"CAA"} = "Q";
$code->{"CAC"} = "H";
$code->{"CAG"} = "Q";
$code->{"CAT"} = "H";
$code->{"CCA"} = "P";
$code->{"CCC"} = "P";
$code->{"CCG"} = "P";
$code->{"CCT"} = "P";
$code->{"CGA"} = "R";
$code->{"CGC"} = "R";
$code->{"CGG"} = "R";
$code->{"CGT"} = "R";
$code->{"CTA"} = "L";
$code->{"CTC"} = "L";
$code->{"CTG"} = "L";
$code->{"CTT"} = "L";
$code->{"GAA"} = "E";
$code->{"GAC"} = "D";
$code->{"GAG"} = "E";
$code->{"GAT"} = "D";
$code->{"GCA"} = "A";
$code->{"GCC"} = "A";
$code->{"GCG"} = "A";
$code->{"GCT"} = "A";
$code->{"GGA"} = "G";
$code->{"GGC"} = "G";
$code->{"GGG"} = "G";
$code->{"GGT"} = "G";
$code->{"GTA"} = "V";
$code->{"GTC"} = "V";
$code->{"GTG"} = "V";
$code->{"GTT"} = "V";
$code->{"TAA"} = "*";
$code->{"TAC"} = "Y";
$code->{"TAG"} = "*";
$code->{"TAT"} = "Y";
$code->{"TCA"} = "S";
$code->{"TCC"} = "S";
$code->{"TCG"} = "S";
$code->{"TCT"} = "S";
$code->{"TGA"} = "*";
$code->{"TGC"} = "C";
$code->{"TGG"} = "W";
$code->{"TGT"} = "C";
$code->{"TTA"} = "L";
$code->{"TTC"} = "F";
$code->{"TTG"} = "L";
$code->{"TTT"} = "F";
return $code;
}