-
Notifications
You must be signed in to change notification settings - Fork 7
/
getkosByKOs.pl
executable file
·451 lines (385 loc) · 15.3 KB
/
getkosByKOs.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/usr/bin/env perl
#
# INGLÊS/ENGLISH
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# http://www.gnu.org/copyleft/gpl.html
#
#
# PORTUGUÊS/PORTUGUESE
# Este programa é distribuído na expectativa de ser útil aos seus
# usuários, porém NÃO TEM NENHUMA GARANTIA, EXPLÍCITAS OU IMPLÍCITAS,
# COMERCIAIS OU DE ATENDIMENTO A UMA DETERMINADA FINALIDADE. Consulte
# a Licença Pública Geral GNU para maiores detalhes.
# http://www.gnu.org/copyleft/gpl.html
#
# Copyright (C) 2012 Universidade de São Paulo
#
# Universidade de São Paulo
# Laboratório de Biologia do Desenvolvimento de Abelhas
# Núcleo de Bioinformática (LBDA-BioInfo)
#
# Daniel Guariz Pinheiro
# http://zulu.fmrp.usp.br/bioinfo
#
# $Id$
=head1 NAME
=head1 SYNOPSIS
=head1 ABSTRACT
=head1 DESCRIPTION
Arguments:
-h/--help Help
-l/--level Log level [Default: FATAL]
OFF
FATAL
ERROR
WARN
INFO
DEBUG
TRACE
ALL
=head1 AUTHOR
Daniel Guariz Pinheiro E<lt>[email protected]<gt>
Copyright (c) 2012 Universidade de São Paulo
=head1 LICENSE
GNU General Public License
http://www.gnu.org/copyleft/gpl.html
=cut
use strict;
use warnings;
use Readonly;
use Getopt::Long;
use File::Temp qw/ tempfile tempdir /;
use File::Basename;
use vars qw/$LOGGER/;
INIT {
use Log::Log4perl qw/:easy/;
Log::Log4perl->easy_init($FATAL);
$LOGGER = Log::Log4perl->get_logger($0);
}
my ($level, $KOid, $infile, $outprefix, $keep, $dbdir, $ignorehuman);
Usage("Too few arguments") if $#ARGV < 0;
GetOptions( "h|?|help" => sub { &Usage(); },
"l|level=s"=> \$level,
"K|KO=s"=>\$KOid,
"i|infile=s"=>\$infile,
"o|outprefix=s"=>\$outprefix,
"p|keep"=>\$keep,
"d|db=s"=>\$dbdir,
"ih|ignore_human"=>\$ignorehuman
) or &Usage();
if ($level) {
my %LEVEL = (
'OFF' =>$OFF,
'FATAL' =>$FATAL,
'ERROR' =>$ERROR,
'WARN' =>$WARN,
'INFO' =>$INFO,
'DEBUG' =>$DEBUG,
'TRACE' =>$TRACE,
'ALL' =>$ALL);
$LOGGER->logdie("Wrong log level ($level). Choose one of: ".join(', ', keys %LEVEL)) unless (exists $LEVEL{$level});
Log::Log4perl->easy_init($LEVEL{$level});
}
$outprefix||='./getkosByKOs';
if ($dbdir) {
$keep=1;
}
my %KO;
unless ($KOid) {
unless ($infile) {
$LOGGER->logdie("Missing KO info: id (-K) or input file (-i).");
} else {
open(IN, "<", $infile) or $LOGGER->logdie($!);
while(<IN>) {
chomp;
my ($id) = $_;
$KO{uc($id)} = undef;
}
close(IN);
}
} else {
$KO{uc($KOid)} = undef;
}
# Create a user agent object
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");
my $dir;
if ($keep) {
if ($dbdir) {
$dir=$dbdir;
} else {
$dir = dirname($outprefix).'/'.basename($outprefix);
}
mkdir($dir);
} else {
$dir = tempdir(basename($outprefix).'_XXXXX', DIR => dirname($outprefix), CLEANUP => (($keep) ? 0 : 1) );
}
foreach my $kid (keys %KO) {
my @content_line;
if ( ( ! -e "$dir/KO/$kid.txt") || ( -z "$dir/KO/$kid.txt") ) {
# Create a request
my $req = HTTP::Request->new(GET => 'http://rest.kegg.jp/get/ko:'.$kid);
$req->content_type('application/x-www-form-urlencoded');
$req->content('query=libwww-perl&mode=dist');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success()) {
my $content = $res->content();
@content_line = split(/\n/, $content);
mkdir("$dir/KO") unless (-d "$dir/KO");
open(KO, ">", "$dir/KO/$kid.txt") or $LOGGER->logdie($!);
print KO $content;
close(KO);
sleep(3);
} else {
print $kid,"\t",$res->status_line, "\n";
next;
}
} else {
open(KO, "<", "$dir/KO/$kid.txt") or $LOGGER->logdie($!);
while(<KO>) {
chomp;
push(@content_line, $_);
}
close(KO);
}
if (scalar(@content_line)) {
my $set_name = undef;
my $set_definition = undef;
my $set_pathway = undef;
foreach my $line ( @content_line ) {
if (($line =~ /^NAME/)||($set_name)) {
if (($set_name)&&($line=~/^[A-Z_]+\s+/)&&($line!~/^NAME/)) {
$set_name=undef;
} else {
$set_name = 1;
my ($kname) = $line=~/^(?:NAME\s+)?(.+)/;
$KO{$kid}->{'name'}.=$kname if ($kname);
}
}
if (($line =~ /^DEFINITION/)||($set_definition)) {
if (($set_definition)&&($line=~/^[A-Z_]+\s+/)&&($line!~/^DEFINITION/)) {
$set_definition=undef;
} else {
$set_definition = 1;
my ($kdesc) = $line=~/^(?:DEFINITION\s+)?(.+)/;
$KO{$kid}->{'definition'}.=$kdesc if ($kdesc);
}
}
if (($line =~ /^(?:MODULE|PATHWAY)/)||($set_pathway)) {
if (($set_pathway)&&($line=~/^[A-Z_]+\s+/)&&($line!~/^(?:MODULE|PATHWAY)/)) {
$set_pathway=undef;
last;
} else {
$set_pathway = 1;
if ($line=~/\s+((?:ko|map)\d{5})\s+.*/) {
my $ko = $1;
$KO{$kid}->{'ko'}->{$ko} = undef;
} elsif ($line=~/\s+((?:M)\d{5})\s+.*/) {
# Not used
my $mo = $1;
$KO{$kid}->{'M'}->{$mo} = undef;
}
}
}
}
}
}
my %pathway;
open(OUT, ">", $outprefix.'-KO-annotations.txt') or $LOGGER->logdie($!);
foreach my $kid (keys %KO) {
print OUT join("\t", $kid, map { $_||'' } @{ $KO{$kid} }{'name', 'definition'} ),"\n";
foreach my $pid (keys %{ $KO{$kid}->{'ko'} }) {
next if (exists $pathway{$pid});
my @content_line;
if ( ( ! -e "$dir/ko/$pid.txt") || ( -z "$dir/ko/$pid.txt") ) {
# Create a request
my $req = HTTP::Request->new(GET => 'http://rest.kegg.jp/get/'.$pid);
$req->content_type('application/x-www-form-urlencoded');
$req->content('query=libwww-perl&mode=dist');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success()) {
my $content = $res->content();
@content_line = split(/\n/, $content);
mkdir("$dir/ko") unless (-d "$dir/ko");
open(KO, ">", "$dir/ko/$pid.txt") or $LOGGER->logdie($!);
print KO $content;
close(KO);
sleep(3);
} else {
print $pid,"\t",$res->status_line, "\n";
next;
}
} else {
open(KO, "<", "$dir/ko/$pid.txt") or $LOGGER->logdie($!);
while(<KO>) {
chomp;
push(@content_line, $_);
}
close(KO);
}
if (scalar(@content_line)) {
my $set_name = undef;
my $set_orthology = undef;
my $set_module = undef;
my $set_class = undef;
foreach my $line ( @content_line ) {
if (($line =~ /^NAME/)||($set_name)) {
if (($set_name)&&($line=~/^\S+/)&&($line!~/^NAME/)) {
$set_name=undef;
} else {
$set_name = 1;
my ($kname) = $line=~/^(?:NAME\s+)?(.+)/;
$pathway{$pid}->{'name'}.=$kname if ($kname);
}
}
if ($ignorehuman) {
if (($line =~ /^CLASS/)||($set_class)) {
if (($set_class)&&($line=~/^[A-Z_]+\s+/)&&($line!~/^CLASS/)) {
$set_class=undef;
} else {
$set_class = 1;
my ($kname) = $line=~/^(?:CLASS\s+)?(.+)/;
my $set_human_diseases=undef;
foreach my $c (split(/; /, $kname)) {
if ($c eq 'Human Diseases') {
$set_human_diseases=1;
delete($pathway{$pid});
delete($KO{$kid}->{'ko'}->{$pid});
last;
}
}
if ($set_human_diseases) {
last;
}
}
}
}
if (($line =~ /^ORTHOLOGY/)||($set_orthology)) {
if (($set_orthology)&&($line=~/^[A-Z_]+\s+/)&&($line!~/^ORTHOLOGY/)) {
$set_orthology=undef;
last;
} else {
$set_orthology = 1;
if ($line=~/(K\d+)\s+(.*)/) {
my $K = $1;
my $name = $2;
$pathway{$pid}->{'orthology'}->{$K} = $name;
}
}
}
if (($line =~ /^MODULE/)||($set_module)) {
if (($set_module)&&($line=~/^[A-Z_]+\s+/)&&($line!~/^MODULE/)) {
$set_module=undef;
last;
} else {
$set_module = 1;
if ($line=~/(M\d+)\s+(.*)/) {
my $mid = $1;
my @m_content_line;
if ( ( ! -e "$dir/M/$mid.txt") || ( -z "$dir/M/$mid.txt") ) {
# Create a request
my $m_req = HTTP::Request->new(GET => 'http://rest.kegg.jp/get/'.$mid);
$m_req->content_type('application/x-www-form-urlencoded');
$m_req->content('query=libwww-perl&mode=dist');
# Pass request to the user agent and get a response back
my $m_res = $ua->request($m_req);
# Check the outcome of the response
if ($m_res->is_success()) {
my $m_content = $m_res->content();
@m_content_line = split(/\n/, $m_content);
mkdir("$dir/M") unless (-d "$dir/M");
open(MO, ">", "$dir/M/$mid.txt") or $LOGGER->logdie($!);
print MO $m_content;
close(MO);
sleep(3);
} else {
print $mid,"\t",$m_res->status_line, "\n";
next;
}
} else {
open(MO, "<", "$dir/M/$mid.txt") or $LOGGER->logdie($!);
while(<MO>) {
chomp;
push(@m_content_line, $_);
}
close(MO);
}
if (scalar(@m_content_line)) {
my $set_m_name = undef;
my $set_m_orthology = undef;
foreach my $m_line ( @m_content_line ) {
if (($line =~ /^NAME/)||($set_m_name)) {
if (($set_m_name)&&($m_line=~/^\S+/)&&($m_line!~/^NAME/)) {
$set_m_name=undef;
} else {
$set_m_name = 1;
my ($mname) = $m_line=~/^(?:NAME\s+)?(.+)/;
}
}
if (($m_line =~ /^ORTHOLOGY/)||($set_m_orthology)) {
if (($set_m_orthology)&&($m_line=~/^[A-Z_]+\s+/)&&($m_line!~/^ORTHOLOGY/)) {
$set_m_orthology=undef;
last;
} else {
$set_m_orthology = 1;
if ($m_line=~/(K\d+)\s+(.*)/) {
my $K = $1;
my $name = $2;
$pathway{$pid}->{'orthology'}->{$K} = $name;
}
}
}
}
}
}
}
}
}
}
}
}
close(OUT);
open(OUT, ">", $outprefix.'-ko-annotations.txt') or $LOGGER->logdie($!);
open(GMT, ">", $outprefix.'-ko-annotations.gmt') or $LOGGER->logdie($!);
foreach my $pid (keys %pathway) {
print OUT join("\t", $pid, $pathway{$pid}->{'name'} ),"\n";
print GMT $pid,"\t";
if (($pathway{$pid}->{'orthology'})&&(scalar(keys %{ $pathway{$pid}->{'orthology'} }))) {
print GMT join(";", map { $_||'' } keys %{ $pathway{$pid}->{'orthology'} } );
}
print GMT "\n";
}
close(OUT);
close(GMT);
# Subroutines
sub Usage {
my ($msg) = @_;
Readonly my $USAGE => <<"END_USAGE";
Daniel Guariz Pinheiro (dgpinheiro\@gmail.com)
(c)2012 Universidade de São Paulo
Usage
$0 [-h/--help] [-l/--level <LEVEL>]
Argument(s)
-h --help Help
-l --level Log level [Default: FATAL]
-K --KO KEGG Orthology ID (KO)
-i --infile Input file
-o --outprefix Output prefix
-p --keep Keep temporary directory [True if -d has value]
-d --db Database directory, i.e. with "KO/" (KEGG Orthology), "ko/" (KEGG Reference Pathway), and "M/" (KEGG Modules) directories
-ih --ignore_human Ignore KEGG pathways specific from "Human Diseases" category
END_USAGE
print STDERR "\nERR: $msg\n\n" if $msg;
print STDERR qq[$0 ] . q[$Revision$] . qq[\n];
print STDERR $USAGE;
exit(1);
}