-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_functional_annotations_per_orthogroup.pl
323 lines (274 loc) · 8.25 KB
/
get_functional_annotations_per_orthogroup.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
my $dirname = dirname(__FILE__);
# usage: perl get_all_files.pl (change lines where the input files are called)
my ($line, $name, $nameout);
my $ogtable = "/home/projects/ku_00039/people/joeviz/orthofinder/run_allGAGA_final_annotations/toerda/Orthogroup_table_N0.tsv"; # Ortholog table from orthofinder
my $fannotdir = "/home/projects/ku_00039/people/joeviz/GAGA_annotations/Final_functional_annotation_wKEGG/"; # Functional annotation folder, it contains a folder for each ant genome (i.e.: GAGA-0001/) and it is generated in "get_all_functional_annotation.pl script"
# Load here functional annotations (tables and GOs)
system ("ls $fannotdir\/\*\/\*FunctionalAnnotation_summary.tsv > inputsums.txt");
#system ("ls $fannotdir\/GAGA-0001\/\*FunctionalAnnotation_summary.tsv > inputsums.txt"); # Only 1 genome to test
my %annotswiss; my %annotegg; my %annotipr; my %annotgos; my %annotkegs;
open (File , "<", "inputsums.txt");
while (<File>) {
chomp;
$line = $_;
next if ($line !~ /\S+/);
my $gagaid = "";
if ($line =~ /(GAGA\-\S\S\S\S)/ || $line =~ /(NCBI\-\S\S\S\S)/ || $line =~ /(OUT\-\S\S\S\S)/){
$gagaid = $1;
} else {die "Can't find GAGA id in $line\n";}
# Read summary table
open (Anofile, "<", $line);
while (<Anofile>){
chomp;
my $line2 = $_;
next if ($line2 !~ /\S+/);
next if ($line2 =~ /Protein id\tSwissprot/);
my @subl = split (/\t/, $line2);
my $prot = $subl[0];
if ($subl[1] =~ /\S+\/\S+\/(.*)/){
$annotswiss{$gagaid}{$prot} = $1;
}
if ($subl[2] =~ /\S+\/(.*)/){
$annotegg{$gagaid}{$prot} = $1;
} elsif ($subl[2] !~ /^NA$/) {
$annotegg{$gagaid}{$prot} = $subl[2];
}
if ($subl[6] =~ /(\S+)\/\S+\/(.*)/){
$annotipr{$gagaid}{$prot} = "$1\/$2";
}
}
close Anofile;
# Read GO
my $gofile = "$fannotdir\/\/$gagaid\/$gagaid\_final_annotation_repfilt_addreannot_noparpse_representative_FunctionalAnnotation_GO.tsv";
if (-f $gofile) {
# print "the file exists\n";
open (Anofile, "<", $gofile);
while (<Anofile>){
chomp;
my $line2 = $_;
next if ($line2 !~ /\S+/);
next if ($line2 =~ /^\#/);
my @subl = split (/\t/, $line2);
#my $prot = $subl[0];
my $prot = shift @subl;
# $annotgos{$gagaid}{$prot} = join('\t', @subl);
@{$annotgos{$gagaid}{$prot}} = @subl;
}
close Anofile;
} else {
die "Can't find $gagaid GO annotation $gofile file\n";
}
# Read KEGG
my $kegfile = "$fannotdir\/\/$gagaid\/$gagaid\_final_annotation_repfilt_addreannot_noparpse_representative_FunctionalAnnotation_KEGG.tsv";
if (-f $kegfile) {
# print "the file exists\n";
open (Anofile, "<", $kegfile);
while (<Anofile>){
chomp;
my $line2 = $_;
next if ($line2 !~ /\S+/);
next if ($line2 =~ /^\#/);
my @subl = split (/\t/, $line2);
#my $prot = $subl[0];
my $prot = shift @subl;
# $annotgos{$gagaid}{$prot} = join('\t', @subl);
@{$annotkegs{$gagaid}{$prot}} = @subl;
}
close Anofile;
} else {
die "Can't find $gagaid KEGG annotation $kegfile file\n";
}
}
# Open HOG table
open (Results, ">", "Orthogroups_functional_annotation_summary.tsv");
print Results "Orthogroup\tNumber of sequences\tNumber of species\tSwissprot\tNumber of seqs with that Swissprot\tEggNog\tNumber of seqs with that EggNog\tInterPro\tNumber of seqs with that InterPro\tNumber of GOs (at least 33\% sequences)\tTotal number of GOs\tNumber of KEGGs (at least 33\% sequences)\tTotal number of KEGGs\n";
open (ResultsGO, ">", "Orthogroups_functional_annotation_GO.annot");
open (ResultsGOtsv, ">", "Orthogroups_functional_annotation_GO.tsv");
open (ResultsGOall, ">", "Orthogroups_functional_annotation_GO_allnofilter.annot");
open (ResultsGOtsvall, ">", "Orthogroups_functional_annotation_GO_allnofilter.tsv");
open (ResultsKEGG, ">", "Orthogroups_functional_annotation_KEGG.annot");
open (ResultsKEGGtsv, ">", "Orthogroups_functional_annotation_KEGG.tsv");
open (ResultsKEGGall, ">", "Orthogroups_functional_annotation_KEGG_allnofilter.annot");
open (ResultsKEGGtsvall, ">", "Orthogroups_functional_annotation_KEGG_allnofilter.tsv");
my $header = "";
open (File , "<", $ogtable);
while (<File>) {
chomp;
$line = $_;
next if ($line !~ /\S+/);
if ($line =~ /Parent Clade\t(.*)/){
$header = $1;
next;
}
my @gagaids = split (/\t/, $header);
my @subl = split (/\t/, $line);
my $ogid = "";
if ($subl[0] =~ /\S+\.(HOG\S+)/){
$ogid = $1;
}
my %hoggo; my %hogkeg; my %hogsprot; my %hogegg; my %hogipr;
my $numsp = "0"; my $numseqs = "0";
my $discard = shift @subl; $discard = shift @subl; $discard = shift @subl;
my $col = 0;
foreach my $seqs (@subl){
if ($seqs !~ /\S+/){
$col++;
next;
}
my @arrayseqs = split (/\,/, $seqs);
foreach my $seq (@arrayseqs){
next if ($seq !~ /\S+/);
$seq =~ s/\s+//g;
$numseqs++;
my $gagaid = $gagaids[$col];
# Get GOs
if (exists $annotgos{$gagaid}{$seq}){
# my $golist = $annotgos{$gagaid}{$seq};
# my @goarray = split (/\t/, $golist);
my @goarray = @{$annotgos{$gagaid}{$seq}};
foreach my $eachgo (@goarray){
$hoggo{$eachgo}++;
}
}
# Get KEGGs
if (exists $annotkegs{$gagaid}{$seq}){
# my $keglist = $annotkegs{$gagaid}{$seq};
# my @kegarray = split (/\t/, $keglist);
my @kegarray = @{$annotkegs{$gagaid}{$seq}};
foreach my $eachkeg (@kegarray){
$hogkeg{$eachkeg}++;
}
}
# Get other annotations
if (exists $annotegg{$gagaid}{$seq}){
my $annolist = $annotegg{$gagaid}{$seq};
my @anoarray = split (/\t/, $annolist);
foreach my $eachano (@anoarray){
$hogegg{$eachano}++;
}
}
if (exists $annotipr{$gagaid}{$seq}){
my $annolist = $annotipr{$gagaid}{$seq};
my @anoarray = split (/\t/, $annolist);
foreach my $eachano (@anoarray){
$hogipr{$eachano}++;
}
}
if (exists $annotswiss{$gagaid}{$seq}){
my $annolist = $annotswiss{$gagaid}{$seq};
my @anoarray = split (/\t/, $annolist);
foreach my $eachano (@anoarray){
$hogsprot{$eachano}++;
}
}
}
if ($numseqs > 0){
$numsp++;
}
$col++;
}
# Print GOs
my $gonum = 0;
my $gonumall = 0;
if ( !keys %hoggo){
#Empty
} else {
print ResultsGOtsvall "$ogid\t";
foreach my $key (sort keys %hoggo){
my $times = $hoggo{$key};
$gonumall++;
print ResultsGOall "$ogid\t$key\n";
print ResultsGOtsvall "$key\t";
my $filter = $numseqs/3;
if ($times >= $filter){
if ($gonum == 0){
print ResultsGOtsv "$ogid\t";
}
$gonum++;
print ResultsGO "$ogid\t$key\n";
print ResultsGOtsv "$key\t";
}
}
}
if ($gonumall > 0){
print ResultsGOtsvall "\n";
}
if ($gonum > 0){
print ResultsGOtsv "\n";
}
# Print KEGGs
my $kegnum = 0;
my $kegnumall = 0;
if ( !keys %hogkeg){
#Empty
} else {
print ResultsKEGGtsvall "$ogid\t";
foreach my $key (sort keys %hogkeg){
my $times = $hogkeg{$key};
$kegnumall++;
print ResultsKEGGall "$ogid\t$key\n";
print ResultsKEGGtsvall "$key\t";
my $filter = $numseqs/3;
if ($times >= $filter){
if ($kegnum == 0){
print ResultsKEGGtsv "$ogid\t";
}
$kegnum++;
print ResultsKEGG "$ogid\t$key\n";
print ResultsKEGGtsv "$key\t";
}
}
}
if ($kegnumall > 0){
print ResultsKEGGtsvall "\n";
}
if ($kegnum > 0){
print ResultsKEGGtsv "\n";
}
my $sprotannot = "NA";
my $sprotnum = 0;
if ( !keys %hogsprot){
#Empty
} else {
foreach my $key (sort keys %hogsprot){
my $times = $hogsprot{$key};
if ($times > $sprotnum){
$sprotnum = $times;
$sprotannot = $key;
}
}
}
my $iprannot = "NA";
my $iprnum = 0;
if ( !keys %hogipr){
#Empty
} else {
foreach my $key (sort keys %hogipr){
my $times = $hogipr{$key};
if ($times > $iprnum){
$iprnum = $times;
$iprannot = $key;
}
}
}
my $eggannot = "NA";
my $eggnum = 0;
if ( !keys %hogegg){
#Empty
} else {
foreach my $key (sort keys %hogegg){
my $times = $hogegg{$key};
if ($times > $eggnum){
$eggnum = $times;
$eggannot = $key;
}
}
}
#print Results "Orthogroup\tNumber of sequences\tNumber of species\tSwissprot\tNumber of seqs with that Swissprot\tEggNog\tNumber of seqs with that EggNog\tInterPro\tNumber of seqs with that InterPro\tNumber of GOs\t";
print Results "$ogid\t$numseqs\t$numsp\t$sprotannot\t$sprotnum\t$eggannot\t$eggnum\t$iprannot\t$iprnum\t$gonum\t$gonumall\t$kegnum\t$kegnumall\n";
}
close File;