forked from jmeneghin/perl-for-reysenbach-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblast_ncbi_meta_matchup.pl
More file actions
211 lines (207 loc) · 6.11 KB
/
Copy pathblast_ncbi_meta_matchup.pl
File metadata and controls
211 lines (207 loc) · 6.11 KB
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
#!/usr/bin/perl -w
###########################################################
### Matchup NCBI Blast Output to Available Meta Info ###
### Jennifer Meneghin ###
### July 27, 2010 ###
###########################################################
#---------------------------------------------------------------------------------------------------------------------------
#Deal with passed parameters
#---------------------------------------------------------------------------------------------------------------------------
#If no arguments are passed, show usage message and exit program.
if ($#ARGV == -1) {
&usage;
}
$in_file = "";
$out_file = "ncbi_kegg_matchup.txt";
$gi_list_file = "";
$ko_list_file = "";
$ko_file = "";
$column = 1;
%my_args = @ARGV;
for $i (sort keys %my_args) {
if ($i eq "-i") {
$in_file = $my_args{$i};
}
elsif ($i eq "-o") {
$out_file = $my_args{$i};
}
elsif ($i eq "-g") {
$gi_list_file = $my_args{$i};
}
elsif ($i eq "-l") {
$ko_list_file = $my_args{$i};
}
elsif ($i eq "-k") {
$ko_file = $my_args{$i};
}
elsif ($i eq "-c") {
if ($my_args{$i} =~ /\d+/) {
$column = $my_args{$i};
}
else {
print "\nUnrecognized column number: $my_args{$i}. Must be an integer.\n\n";
&usage;
}
}
else {
print "\nUnrecognized argument: $i\n\n";
&usage;
}
}
unless ( open(GIL, "$gi_list_file") ) {
print "\nCouldn't open file: $gi_list_file\n\n";
&usage;
}
unless ( open(KOL, "$ko_list_file") ) {
print "\nCouldn't open file: $ko_list_file\n\n";
&usage;
}
unless ( open(KO, "$ko_file") ) {
print "\nCouldn't open file: $ko_file\n\n";
&usage;
}
unless ( open(IN, "$in_file") ) {
print "\nGot a bad input file: $in_file\n\n";
&usage;
}
if (-e $out_file) {
print "\nCouldn't create $out_file, because it already exists\n\n";
&usage;
}
unless ( open(OUT, ">$out_file") ) {
print "\nGot a bad output file: $out_file\n\n";
&usage;
}
print "Parameters:\ninput file = $in_file\noutput file = $out_file\n\ngi list file = $gi_list_file\nko list file = $ko_list_file\nko file = $ko_file\ncolumn to match on = $column\n\n";
#---------------------------------------------------------------------------------------------------------------------------
#The main event
#---------------------------------------------------------------------------------------------------------------------------
%summary = ();
$cols = 0;
while (<IN>) {
chomp;
@fields = split(/\t/);
$genbank = $fields[($column-1)];
if ($genbank =~ /^gi/) {
# $genbank =~ s/^gi\|(\d+)\|ref.+$/$1/g;
$genbank =~ s/^gi\|(\d+)\|.+$/$1/g;
$summary{$genbank} = $_;
print "in: $genbank\n";
}
if ($#fields > $cols) {
$cols = $#fields;
}
}
close(IN);
%names = ();
%gis = ();
while (<GIL>) {
@fields = split(/\t/);
$gi = $fields[1];
$gi =~ s/^ncbi-gi:(\d+)$/$1/g;
if ($summary{$gi}) {
$names{$fields[0]} = $gi;
$gis{$gi} = $fields[0];
}
}
close(GIL);
%kos = ();
while (<KOL>) {
chomp;
@fields = split(/\t/);
$ko = $fields[1];
$ko =~ s/^ko:(.+)$/$1/g;
$name = $fields[0];
if ($names{$name}) {
if ($kos{$ko}) {
$kos{$ko} = $kos{$ko} . "|" . $names{$fields[0]};
}
else {
$kos{$ko} = $names{$fields[0]};
}
print "ko: $names{$fields[0]}\t$ko\n";
}
}
close(KOL);
$flag = 0;
for $i (0..$cols) {
print "\t";
print OUT "\t";
}
print "Name\tKEGG Information\n";
print OUT "Name\tKEGG Information\n";
while (<KO>) {
chomp;
if (/^ENTRY/) {
$knum = $_;
$knum =~ s/^ENTRY\s+(K\d+?)\s.+$/$1/g;
if ( $kos{$knum} ) {
$flag = 1;
if ( $kos{$knum} =~ /\|/) {
@genbanks = split(/\|/, $kos{$knum});
for $i (0..$#genbanks-1) {
print "$summary{$genbanks[$i]}\t$gis{$genbanks[$i]}\t$knum\n";
print OUT "$summary{$genbanks[$i]}\t$gis{$genbanks[$i]}\t$knum\n";
delete($summary{$genbanks[$i]});
}
print "$summary{$genbanks[$#genbanks]}\t$gis{$genbanks[$#genbanks]}\t$knum\t";
print OUT "$summary{$genbanks[$#genbanks]}\t$gis{$genbanks[$#genbanks]}\t$knum\t";
delete($summary{$genbanks[$#genbanks]});
}
else {
print "$summary{$kos{$knum}}\t$gis{$kos{$knum}}\t$knum\t";
print OUT "$summary{$kos{$knum}}\t$gis{$kos{$knum}}\t$knum\t";
delete($summary{$kos{$knum}});
}
}
else {
$flag = 0;
}
}
elsif (/^GENES/ || /^\/\/\//) {
$flag = 0;
}
elsif ($flag == 1) {
print "$_\n";
print OUT "$_\n";
$flag = 2;
}
elsif ($flag == 2) {
for $i (0..$cols) {
print "\t";
print OUT "\t";
}
print "\t\t$_\n";
print OUT "\t\t$_\n";
}
}
for $i (sort keys %summary) {
if ($gis{$i}) {
print "$summary{$i}\t$gis{$i}\tNo KEGG information found\n";
print OUT "$summary{$i}\t$gis{$i}\tNo KEGG information found\n";
}
else {
print "$summary{$i}\tNo Name Found\tNo KEGG information found\n";
print OUT "$summary{$i}\tNo Name Found\tNo KEGG information found\n";
}
}
close(KO);
close(OUT);
sub usage {
print "Usage: blast_ncbi_meta_matchup.pl\n\n";
print "Parameters:\n";
print "-i input_file\t\tA tabbed delimited file with NCBI IDs\n";
print "-k ko\t\t\tThe ko file downloaded from KEGG with pathway (and other) information for KO numbers\n";
print "-g genes_ncbi-gi.list\tThe genes_ncbi-gi.list file downloaded from KEGG that relates NCBI IDs to KEGG IDs\n";
print "-l genes_ko.list\tThe genes_ko.list file downloaded from KEGG that relates KEGG IDs to KEGG numbers\n";
print "-o output_file\t\tThe output file to create (optional. Default = ncbi_kegg_matchup.txt)\n";
print "-c integer\t\tThe column number of the NCBI ID (optional. Default = 1)\n\n";
print "This program takes a BLAST output file in short format, a summarized (by blast_summary.pl) BLAST output\n";
print "file in short format, or any other tab delimited file with a field for NCBI ID as it's input file,\n";
print "and returns this file with additional KEGG information found.\n\n";
print "Assumption: BLAST was performed on NCBI files. IDs must start with \"gi|\".\n\n";
print "Jennifer Meneghin\n";
print "July 27, 2010\n";
print "Updated August 12, 2010\n\n";
exit;
}