-
Notifications
You must be signed in to change notification settings - Fork 109
/
gb2tab.rb
executable file
·317 lines (270 loc) · 5.61 KB
/
gb2tab.rb
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
#!/usr/bin/env ruby
#
# gb2tab.rb - convert GenBank into tab delimited data for MySQL
#
# Usage:
#
# % gb2tab.rb gb*.seq
#
# Copyright (C) 2001 KATAYAMA Toshiaki <k@bioruby.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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.
#
# $Id: gb2tab.rb,v 0.8 2002/03/27 20:54:50 katayama Exp $
#
require 'bio'
ARGV.each do |gbkfile|
gbk = open("#{gbkfile}")
ent = open("#{gbkfile}.ent.tab", "w")
ft = open("#{gbkfile}.ft.tab", "w")
ref = open("#{gbkfile}.ref.tab", "w")
seq = open("#{gbkfile}.seq.tab", "w")
while entry = gbk.gets(Bio::GenBank::DELIMITER)
gb = Bio::GenBank.new(entry)
### MAIN BODY
versions = gb.versions.inspect
kw = gb.keywords.inspect
seg = gb.segment.inspect
bc = gb.basecount.inspect
ary = [
gb.entry_id,
gb.nalen,
gb.strand,
gb.natype,
gb.circular,
gb.division,
gb.date,
gb.definition,
gb.accession,
versions,
kw,
seg,
gb.common_name,
gb.organism,
gb.taxonomy,
gb.comment,
bc,
gb.origin,
]
ent.puts ary.join("\t")
### FEATURES
num = 0
gb.features.each do |f|
num += 1
from, to = f.locations.span
if f.qualifiers.empty?
ary = [ gb.entry_id, num, f.feature, f.position, from, to, '', '' ]
ft.puts ary.join("\t")
else
f.each do |q|
ary = [
gb.entry_id,
num,
f.feature,
f.position,
from,
to,
q.qualifier,
q.value,
]
ft.puts ary.join("\t")
end
end
end
### REFERENCE
num = 0
gb.references.each do |r|
num += 1
ary = [
gb.entry_id,
num,
r.authors,
r.title,
r.journal,
r.medline,
r.pubmed,
]
ref.puts ary.join("\t")
end
### SEQUENCE
maxlen = 16 * 10 ** 6
num = 0
0.step(gb.nalen, maxlen) do |i|
num += 1
ary = [
gb.entry_id,
num,
gb.naseq[i, maxlen]
]
seq.puts ary.join("\t")
end
end
gbk.close
ent.close
ft.close
ref.close
seq.close
end
=begin
Example usage in zsh:
% gb2tab.rb *.seq
% for i in *.seq
> do
> hoge=`basename $i .seq`
> ruby -pe "gsub(/%HOGE%/,'$hoge')" gb2tab.sql | mysql
> done
gb2tab.sql:
CREATE DATABASE IF NOT EXISTS genbank;
USE genbank;
CREATE TABLE IF NOT EXISTS %HOGE% (
id varchar(16) NOT NULL PRIMARY KEY,
nalen integer,
strand varchar(5),
natype varchar(5),
circular varchar(10),
division varchar(5),
date varchar(10),
definition varchar(255),
accession varchar(30),
versions varchar(30),
keywords varchar(255),
segment varchar(255),
source varchar(255),
organism varchar(255),
taxonomy varchar(255),
comment text,
basecount varchar(255),
origin varchar(255),
KEY (nalen),
KEY (division),
KEY (accession),
KEY (organism),
KEY (taxonomy)
);
LOAD DATA LOCAL INFILE '%HOGE%.seq.ent.tab' INTO TABLE %HOGE%;
CREATE TABLE IF NOT EXISTS %HOGE%ft (
id varchar(16) NOT NULL,
num integer,
feature varchar(30),
position text,
span_min integer,
span_max integer,
qualifier varchar(30),
value text,
KEY (id),
KEY (num),
KEY (feature),
KEY (span_min),
KEY (span_max),
KEY (qualifier)
);
LOAD DATA LOCAL INFILE '%HOGE%.seq.ft.tab' INTO TABLE %HOGE%ft;
CREATE TABLE IF NOT EXISTS %HOGE%ref (
id varchar(16) NOT NULL,
num integer,
authors text,
title text,
journal text,
medline varchar(255),
pubmed varchar(255),
KEY (id),
KEY (medline),
KEY (pubmed)
);
LOAD DATA LOCAL INFILE '%HOGE%.seq.ref.tab' INTO TABLE %HOGE%ref;
CREATE TABLE IF NOT EXISTS %HOGE%seq (
id varchar(16) NOT NULL,
num integer,
naseq mediumtext,
KEY (id)
);
LOAD DATA LOCAL INFILE '%HOGE%.seq.seq.tab' INTO TABLE %HOGE%seq;
gbmerge.sql sample:
CREATE TABLE IF NOT EXISTS gb (
id varchar(16) NOT NULL PRIMARY KEY,
nalen integer,
strand varchar(5),
natype varchar(5),
circular varchar(10),
division varchar(5),
date varchar(10),
definition varchar(255),
accession varchar(30),
versions varchar(30),
keywords varchar(255),
segment varchar(255),
source varchar(255),
organism varchar(255),
taxonomy varchar(255),
comment text,
basecount varchar(255),
origin varchar(255),
KEY (nalen),
KEY (division),
KEY (accession),
KEY (organism),
KEY (taxonomy)
) TYPE=MERGE UNION=(
gbbct1,
gbbct2,
..., # list up all tables by yourself
gbvrt
);
CREATE TABLE IF NOT EXISTS gbft (
id varchar(16) NOT NULL,
num integer,
feature varchar(30),
position text,
span_min integer,
span_max integer,
qualifier varchar(30),
value text,
KEY (id),
KEY (num),
KEY (feature),
KEY (span_min),
KEY (span_max),
KEY (qualifier)
) TYPE=MERGE UNION=(
gbbct1ft,
gbbct2ft,
..., # list up all ft tables by yourself
gbvrtft
);
CREATE TABLE IF NOT EXISTS gbref (
id varchar(16) NOT NULL,
num integer,
authors text,
title text,
journal text,
medline varchar(255),
pubmed varchar(255),
KEY (id),
KEY (medline),
KEY (pubmed)
) TYPE=MERGE UNION=(
gbbct1ref,
gbbct2ref,
..., # list up all ref tables by yourself
gbvrtref
);
CREATE TABLE IF NOT EXISTS gbseq (
id varchar(16) NOT NULL,
num integer,
naseq mediumtext,
KEY (id)
) TYPE=MERGE UNION=(
gbbct1seq,
gbbct2seq,
..., # list up all seq tables by yourself
gbvrtseq
);
=end