-
Notifications
You must be signed in to change notification settings - Fork 6
/
bindb2.sql
executable file
·467 lines (357 loc) · 11.5 KB
/
bindb2.sql
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
.echo ON
ATTACH DATABASE 'metadatasrc.sqlite' AS meta;
INSERT INTO metadata
SELECT 'DBSCHEMA', db_schema
FROM meta.metadata
WHERE package_name = 'YEAST';
INSERT INTO metadata
SELECT 'ORGANISM', organism
FROM meta.metadata
WHERE package_name = 'YEAST';
INSERT INTO metadata
SELECT 'SPECIES', species
FROM meta.metadata
WHERE package_name = 'YEAST';
DETACH DATABASE meta;
ATTACH DATABASE 'keggsrc.sqlite' AS keggsrc;
CREATE TABLE kegg (
_id INTEGER NOT NULL, -- REFERENCES sgd
path_id CHAR(5) NOT NULL, -- KEGG pathway short ID
FOREIGN KEY (_id) REFERENCES sgd (_id)
);
INSERT INTO kegg
SELECT s._id, substr(p.pathway_id, -5, 5)
FROM sgd as s, keggsrc.pathway2gene as p
WHERE s.systematic_name=p.gene_or_orf_id;
CREATE INDEX k1 ON kegg(_id);
CREATE INDEX Fkegg ON kegg (_id);
CREATE TABLE ec (
_id INTEGER NOT NULL, -- REFERENCES sgd
ec_number VARCHAR(13) NOT NULL, -- EC number (no "EC:" prefix)
FOREIGN KEY (_id) REFERENCES sgd (_id)
);
INSERT INTO ec
SELECT s._id, e.ec_id
FROM sgd as s, keggsrc.sce_ec as e
WHERE s.systematic_name=e.gene_or_orf_id;
CREATE INDEX e1 ON ec(_id);
CREATE INDEX Fec ON ec (_id);
INSERT INTO metadata
SELECT * FROM keggsrc.metadata
WHERE name LIKE "KEGGSOURCE%";
DETACH DATABASE keggsrc;
ATTACH DATABASE 'gpsrc.sqlite' AS gp;
CREATE TABLE chrlengths (
chromosome VARCHAR(2) PRIMARY KEY,
length INTEGER NOT NULL
);
INSERT INTO chrlengths
SELECT chr, length FROM gp.chrlength_yeast;
ATTACH DATABASE 'gosrc.sqlite' AS gosrc;
CREATE TABLE go_bp_all (
_id INTEGER NOT NULL,
go_id CHAR(10) NOT NULL,
evidence CHAR(3) NOT NULL,
FOREIGN KEY (_id) REFERENCES sgd (_id)
);
INSERT INTO go_bp_all
SELECT g._id, t.go_id, g.evidence
FROM gosrc.go_bp_offspring as t,
go_bp as g
WHERE g.go_id=t.offspring_go_id;
INSERT INTO go_bp_all
SELECT _id, go_id, evidence
FROM go_bp;
CREATE INDEX goa1 on go_bp_all(_id);
CREATE TABLE go_mf_all (
_id INTEGER NOT NULL,
go_id CHAR(10) NOT NULL,
evidence CHAR(3) NOT NULL,
FOREIGN KEY (_id) REFERENCES sgd (_id)
);
INSERT INTO go_mf_all
SELECT g._id, t.go_id, g.evidence
FROM gosrc.go_mf_offspring as t,
go_mf as g
WHERE g.go_id=t.offspring_go_id;
INSERT INTO go_mf_all
SELECT _id, go_id, evidence
FROM go_mf;
CREATE INDEX goa2 on go_mf_all(_id);
CREATE TABLE go_cc_all (
_id INTEGER NOT NULL,
go_id CHAR(10) NOT NULL,
evidence CHAR(3) NOT NULL,
FOREIGN KEY (_id) REFERENCES sgd (_id)
);
INSERT INTO go_cc_all
SELECT g._id, t.go_id, g.evidence
FROM gosrc.go_cc_offspring as t,
go_cc as g
WHERE g.go_id=t.offspring_go_id;
INSERT INTO go_cc_all
SELECT _id, go_id, evidence
FROM go_cc;
CREATE INDEX goa3 on go_cc_all(_id);
INSERT INTO metadata
SELECT * FROM gosrc.metadata WHERE name LIKE "GOSOURCE%";
DETACH DATABASE gosrc;
ATTACH DATABASE 'chipsrc_yeastNCBI.sqlite' AS NCBIsrc;
INSERT INTO metadata
SELECT * FROM NCBIsrc.metadata
WHERE name LIKE "EGSOURCE%";
CREATE TABLE genes (
_id INTEGER NOT NULL, -- REFERENCES sgd
gene_id VARCHAR(15) NOT NULL, -- Entrez Gene ID
FOREIGN KEY (_id) REFERENCES sgd (_id)
);
INSERT INTO genes
SELECT DISTINCT s._id, n.gene_id
FROM (SELECT * FROM NCBIsrc.genes INNER JOIN NCBIsrc.locus_tags USING(_id)) AS n,
sgd as s
WHERE s.systematic_name=n.locus_tag
UNION
SELECT DISTINCT s._id, n.gene_id
FROM (SELECT * FROM NCBIsrc.genes INNER JOIN NCBIsrc.sgd_ids USING(_id)) AS n,
sgd as s
WHERE s.sgd_id=n.sgd_id;
CREATE INDEX eg1 on genes(_id);
--We also have to pass the ensembl IDs along from the NCBI yeast DB.
INSERT INTO metadata
SELECT * FROM NCBIsrc.metadata
WHERE name LIKE "ENSOURCE%";
CREATE TABLE ensembl (
_id INTEGER NOT NULL, -- REFERENCES sgd
ensid VARCHAR(20) NOT NULL, -- Ensembl gene ID
FOREIGN KEY (_id) REFERENCES sgd (_id)
);
INSERT INTO ensembl
SELECT DISTINCT s._id, n.ensid
FROM (SELECT * FROM NCBIsrc.ensembl INNER JOIN NCBIsrc.locus_tags USING(_id)) AS n,
sgd as s
WHERE s.systematic_name=n.locus_tag
UNION
SELECT DISTINCT s._id, n.ensid
FROM (SELECT * FROM NCBIsrc.ensembl INNER JOIN NCBIsrc.sgd_ids USING(_id)) AS n,
sgd as s
WHERE s.sgd_id=n.sgd_id;
CREATE INDEX ens1 on ensembl(_id);
--make a table to store this data as it was (from ncbi)
CREATE TABLE ncbi2ensembl (
_id INTEGER NOT NULL,
ensid VARCHAR(20) NOT NULL,
FOREIGN KEY (_id) REFERENCES sgd (_id)
);
INSERT INTO ncbi2ensembl
SELECT * FROM ensembl;
CREATE INDEX ncbi2ens__id ON ncbi2ensembl(_id);
--Now make and pop a table to hold the ncbi to ensembl mappings (from ensembl)
ATTACH DATABASE 'ensembl.sqlite' AS ens;
DELETE FROM metadata WHERE name LIKE 'ENS%';
INSERT INTO metadata
SELECT * FROM ens.metadata;
CREATE TABLE ensembl2ncbi (
_id INTEGER NOT NULL,
ensid VARCHAR(20) NOT NULL,
FOREIGN KEY (_id) REFERENCES sgd (_id)
);
-- Note insert below depends on the genes table having been added above...
-- Almost all the sgds have an EG id so this is acceptable.
INSERT INTO ensembl2ncbi
SELECT g._id, e.ensid
FROM genes as g CROSS JOIN ens.scerevisiae_gene_ensembl as e
WHERE e.gene_id=g.gene_id;
CREATE INDEX ens2ncbi__id ON ensembl2ncbi(_id);
--Insert everything
INSERT INTO ensembl
SELECT _id, ensid FROM ensembl2ncbi;
--THEN remove the duplicate rows like this:
DELETE FROM ensembl
WHERE rowid NOT IN (SELECT rowid
FROM ensembl
GROUP BY _id, ensid
HAVING min(rowid));
--Same trick also is used to map the uniprots over from NCBI
CREATE TABLE uniprot (
_id INTEGER NOT NULL, -- REFERENCES sgd
uniprot_id CHAR(5) NOT NULL, -- uniprot ID
FOREIGN KEY (_id) REFERENCES sgd (_id)
);
INSERT INTO uniprot
SELECT DISTINCT s._id, n.uniprot_id
FROM (SELECT * FROM NCBIsrc.uniprot INNER JOIN NCBIsrc.locus_tags USING(_id)) AS n,
sgd as s
WHERE s.systematic_name=n.locus_tag
UNION
SELECT DISTINCT s._id, n.uniprot_id
FROM (SELECT * FROM NCBIsrc.uniprot INNER JOIN NCBIsrc.sgd_ids USING(_id)) AS n,
sgd as s
WHERE s.sgd_id=n.sgd_id;
CREATE INDEX up1 on uniprot(_id);
CREATE TABLE refseq (
_id INTEGER NOT NULL, -- REFERENCES sgd
accession CHAR(5) NOT NULL, -- refseq accession
FOREIGN KEY (_id) REFERENCES sgd (_id)
);
INSERT INTO refseq
SELECT DISTINCT s._id, n.accession
FROM (SELECT * FROM NCBIsrc.refseq INNER JOIN NCBIsrc.locus_tags USING(_id)) AS n,
sgd as s
WHERE s.systematic_name=n.locus_tag
UNION
SELECT DISTINCT s._id, n.accession
FROM (SELECT * FROM NCBIsrc.refseq INNER JOIN NCBIsrc.sgd_ids USING(_id)) AS n,
sgd as s
WHERE s.sgd_id=n.sgd_id;
CREATE INDEX rs1 on refseq(_id);
DETACH DATABASE NCBIsrc;
BEGIN TRANSACTION;
INSERT INTO map_counts
SELECT 'CHRLENGTHS', count(*)
FROM chrlengths;
INSERT INTO map_counts
SELECT 'ENZYME', count(DISTINCT _id)
FROM ec;
INSERT INTO map_counts
SELECT 'ENZYME2GENE', count(DISTINCT ec_number)
FROM ec;
INSERT INTO map_counts
SELECT 'PATH', count(DISTINCT _id)
FROM kegg;
INSERT INTO map_counts
SELECT 'PATH2GENE', count(DISTINCT path_id)
FROM kegg;
INSERT INTO map_counts
SELECT 'GO2ALLGENES', COUNT(DISTINCT go_id) FROM (
SELECT go_id FROM sgd INNER JOIN go_bp_all USING(_id) WHERE systematic_name IS NOT NULL
UNION
SELECT go_id FROM sgd INNER JOIN go_cc_all USING(_id) WHERE systematic_name IS NOT NULL
UNION
SELECT go_id FROM sgd INNER JOIN go_mf_all USING(_id) WHERE systematic_name IS NOT NULL
);
INSERT INTO map_counts
SELECT 'ENSEMBL', count(DISTINCT _id)
FROM ensembl;
INSERT INTO map_counts
SELECT 'ENSEMBL2GENE', count(DISTINCT ensid)
FROM ensembl;
COMMIT TRANSACTION;
CREATE TABLE map_metadata (
map_name VARCHAR(80) NOT NULL,
source_name VARCHAR(80) NOT NULL,
source_url VARCHAR(255) NOT NULL,
source_date VARCHAR(20) NOT NULL
);
BEGIN TRANSACTION;
INSERT INTO map_metadata
SELECT 'ALIAS', m1.value, m2.value, m3.value
FROM metadata AS m1, metadata AS m2, metadata AS m3
WHERE m1.name='YGSOURCENAME' AND
m2.name='YGSOURCEURL' AND
m3.name='YGSOURCEDATE';
INSERT INTO map_metadata
SELECT 'COMMON2SYSTEMATIC', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'GENENAME', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'INTERPRO', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'PFAM', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'SMART', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'PMID', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'PMID2GENE', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'CHRLOC', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'CHRLOCEND', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'CHR', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'DESCRIPTION', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'GO', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'GO2GENE', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'GO2ALLGENES', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ALIAS';
INSERT INTO map_metadata
SELECT 'ENZYME', m1.value, m2.value, m3.value
FROM metadata AS m1, metadata AS m2, metadata AS m3
WHERE m1.name='KEGGSOURCENAME' AND
m2.name='KEGGSOURCEURL' AND
m3.name='KEGGSOURCEDATE';
INSERT INTO map_metadata
SELECT 'ENZYME2GENE', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ENZYME';
INSERT INTO map_metadata
SELECT 'PATH', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ENZYME';
INSERT INTO map_metadata
SELECT 'PATH2GENE', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ENZYME';
INSERT INTO map_metadata
SELECT 'GO2ALLGENES', m1.value, m2.value, m3.value
FROM metadata AS m1, metadata AS m2, metadata AS m3
WHERE m1.name='GOSOURCENAME' AND
m2.name='GOSOURCEURL' AND
m3.name='GOSOURCEDATE';
INSERT INTO map_metadata
SELECT 'ENTREZID', m1.value, m2.value, m3.value
FROM metadata AS m1, metadata AS m2, metadata AS m3
WHERE m1.name='EGSOURCENAME' AND
m2.name='EGSOURCEURL' AND
m3.name='EGSOURCEDATE';
INSERT INTO map_metadata
SELECT 'UNIPROT', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ENTREZID';
INSERT INTO map_metadata
SELECT 'REFSEQ', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ENTREZID';
INSERT INTO map_metadata
SELECT 'ENSEMBL', m1.value, m2.value, m3.value
FROM metadata AS m1, metadata AS m2, metadata AS m3
WHERE m1.name='ENSOURCENAME' AND
m2.name='ENSOURCEURL' AND
m3.name='ENSOURCEDATE';
INSERT INTO map_metadata
SELECT 'ENSEMBL2GENE', source_name, source_url, source_date
FROM map_metadata
WHERE map_name='ENSEMBL';
COMMIT TRANSACTION;
ANALYZE;