-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize_gff_in_sqlite3.py
More file actions
353 lines (303 loc) · 9.17 KB
/
normalize_gff_in_sqlite3.py
File metadata and controls
353 lines (303 loc) · 9.17 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
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
# import argparse
import os
import sqlite3
import sys
from itertools import combinations
def create_tbl(db, tbl, cl_list):
if os.path.exists(db):
con = sqlite3.connect(db)
cur = con.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS attributes(' +
'? TEXT, ' * len(cl_list) + ' TEXT);',
tuple(cl_list)
)
con.commit()
else:
sys.exit(f'aborting: database {db} does not exist')
def create_att_table(db, source_tbl):
"""1st normal form: all features contain only one piece of data per row"""
if os.path.exists(db):
con = sqlite3.connect(db)
cur = con.cursor()
cur.execute('SELECT name FROM sqlite_master WHERE type=?;', ('table',))
tbl_list = [tpl[0] for tpl in cur.fetchall()]
con.commit()
if 'attributes' in tbl_list:
print(f'attributes table already exists in {db}')
con.close()
return
atts = {
# "Alias" : '.',
# "Dbxref" : '.',
"ID" : '.',
# "Name" : '.',
# "Ontology_term" : '.',
"Parent" : '.'
# "derived_computed_cyto" : '.',
# "derived_experimental_cyto" : '.',
# "exonA" : '.',
# "exonB" : '.',
# "fullname" : '.',
# "gbunit" : '.',
# "read_count" : '.'
}
"""if Parent or ID missing then att values in row should be null"""
cur.execute('CREATE TABLE IF NOT EXISTS attributes(' +
' TEXT, '.join(atts.keys()) + ' TEXT, ' +
'type TEXT, start INT);'
)
con.commit()
cur.execute("SELECT COUNT(*) FROM " + source_tbl)
row_count = cur.fetchone()[0]
for i in range(row_count):
cur.execute("SELECT type, start, att FROM " + source_tbl +
" LIMIT 1 OFFSET " + str(i) + ";")
tp, strt, att = cur.fetchone()
att_list = att.rstrip(';').split(';')
# Updates atts with non-null values in current row
for tv in att_list:
tag, value = tv.split('=')
if tag in atts.keys():
atts[tag] = value
# need to add code if adding columns dynamically is desired
# 4 ?s are for 2 attributes (ID, Parent), type, and start
insert_cmd = "INSERT INTO attributes values("
insert_cmd += "?, " * len(atts) + "?, ?);"
cur.execute(insert_cmd, tuple(atts.values()) + (tp, strt))
if i%500 == 0:
con.commit()
print(i)
atts = {
# "Alias" : '.',
# "Dbxref" : '.',
"ID" : '.',
# "Name" : '.',
# "Ontology_term" : '.',
"Parent" : '.'
# "derived_computed_cyto" : '.',
# "derived_experimental_cyto" : '.',
# "exonA" : '.',
# "exonB" : '.',
# "fullname" : '.',
# "gbunit" : '.',
# "read_count" : '.'
}
con.commit()
remove_col(db, source_tbl, "att")
remove_duplicates(db, source_tbl)
else:
sys.exit(f'aborting: database {db} does not exist')
def remove_col(db, tbl, col):
if os.path.exists(db):
con = sqlite3.connect(db)
cur = con.cursor()
cur.execute("ALTER TABLE " + tbl + " DROP COLUMN " + col + ";")
con.commit()
else:
sys.exit(f'aborting: database {db} does not exist')
def remove_duplicates(db, tbl):
if os.path.exists(db):
con = sqlite3.connect(db)
cur = con.cursor()
cur.execute("CREATE TABLE Temp AS SELECT DISTINCT * FROM " + tbl + ";")
cur.execute("DROP TABLE " + tbl + ";")
cur.execute("ALTER TABLE Temp RENAME TO " + tbl + ";")
con.commit()
else:
sys.exit(f'aborting: database {db} does not exist')
def rank_col_uniqueness(db, tbl):
if os.path.exists(db):
con = sqlite3.connect(db)
cur = con.cursor()
# get list of column names in tbl
cur.execute('pragma table_info(' + tbl + ');')
table_info = cur.fetchall()
col_names = [row[1] for row in table_info]
# print(col_names)
sorted_col_names = []
uniq_vals = {}
for cl in col_names:
cur.execute('select count(*) from(select distinct ' + cl +
' from ' + tbl + ');'
)
uniq_vals[cl] = cur.fetchone()[0]
# print(uniq_vals)
# Insert column name into sorted_col_names based on how many
# unique values that column has.
# As each unique-value-count is generated, it's compared to all
# counts of columns with higher counts. (can be optimized)
col_name_len = len(sorted_col_names)
if col_name_len == 0: sorted_col_names.append(cl)
else:
for idx in range(col_name_len):
if uniq_vals[sorted_col_names[idx]] < uniq_vals[cl]:
sorted_col_names.insert(idx, cl)
break
elif idx == col_name_len - 1:
sorted_col_names.append(cl)
# print(uniq_vals)
# Sorted_col_names is now a list of column names where the
# unique-value-count corresponding to any name is >= the counts of
# column names which follow.
return sorted_col_names
else:
sys.exit(f'aborting: database {db} does not exist')
def identify_tbl_key(db, tbl):
if os.path.exists(db):
con = sqlite3.connect(db)
cur = con.cursor()
cur.execute(f'select count(*) from {tbl};')
row_count = cur.fetchone()[0]
# cl_names = rank_col_uniqueness(db, tbl)
cur.execute('pragma table_info(' + tbl + ');')
table_info = cur.fetchall()
cl_names = [row[1] for row in table_info]
cl_names_len = len(cl_names)
con.commit()
key = []
# for name in cl_names:
# key.append(name)
# cur.execute('select count(*) from(select distinct ' +
# ', '.join(key) + f' from {tbl})'
# )
# if cur.fetchone()[0] == row_count: break
# con.commit()
for i in range(1, cl_names_len + 1):
for cand_key in combinations(cl_names, i):
# print(', '.join(cand_key))
cur.execute('select count(*) from(select distinct ' +
', '.join(cand_key) + f' from {tbl})'
)
if cur.fetchone()[0] == row_count:
key = list(cand_key)
con.commit()
break
con.commit()
if key != []: break
return key
else:
sys.exit(f'aborting: database {db} does not exist')
def normal_forms(db, k, main_tbl=0):
if os.path.exists(db):
con = sqlite3.connect(db)
cur = con.cursor()
if main_tbl == 0: main_tbl = db.replace('.db', '')
match k:
case 1:
create_att_table(db, main_tbl)
case 2:
cur.execute('SELECT name FROM sqlite_master WHERE type=?;',
('table',)
)
tbl_list = [tpl[0] for tpl in cur.fetchall()]
con.commit()
for tbl in tbl_list:
print('')
print(tbl + ':', rank_col_uniqueness(db, tbl))
key = identify_tbl_key(db, tbl)
cur.execute('pragma table_info(' + tbl + ');')
table_info = cur.fetchall()
cl_names = [row[1] for row in table_info]
con.commit()
not_key = [col for col in cl_names if col not in key]
print(' '*(len(tbl)-len('key')) + 'key:', key)
print(' '*(len(tbl)-len('not_key')) + 'not_key:', not_key)
subkeys = {}
if len(not_key) != 1:
for col in not_key:
subkeys[col] = key
for i in range(1, len(key) + 1):
for subkey in combinations(key, i):
sql_cmd = ['select count(*) from(',
f'select count(distinct {col})',
f' from {tbl} group by ',
', '.join(subkey),
' having count(distinct ',
f'{col}) > 1',
');'
]
sql_cmd = ''.join(sql_cmd)
# print(sql_cmd)
cur.execute(sql_cmd)
con.commit()
if cur.fetchone()[0] == 0:
subkeys[col] = list(subkey)
break
if subkeys[col] != key:
print(col, ":", subkeys[col])
break
# print(' ' * (len(tbl + ': ') - len('key: ')) + 'key: ', key)
"""
Possibly NF2 schema
CREATE TABLE attributes(ID TEXT, Parent TEXT, type TEXT, start INT);
CREATE TABLE SCORES(
source TEXT,
start INT,
"end" INT,
score NUM
);
CREATE TABLE IF NOT EXISTS "dm1pct"(
source TEXT,
type TEXT,
start INT,
"end" INT,
phase TEXT
);
CREATE TABLE SEQID_STRAND(
seqid TEXT,
type TEXT,
start INT,
strand TEXT
);
"""
"""
most recent schema
CREATE TABLE IF NOT EXISTS "ATTRIBUTES_BACKUP"(ID TEXT, Parent TEXT, type TEXT, start INT);
CREATE TABLE IF NOT EXISTS "SCORES_BACKUP"(
source TEXT,
start INT,
"end" INT,
score NUM
);
CREATE TABLE IF NOT EXISTS "DM1PCT_BACKUP"(
source TEXT,
type TEXT,
start INT,
"end" INT,
phase TEXT
);
CREATE TABLE IF NOT EXISTS "SEQID_STRAND_BACKUP"(
seqid TEXT,
type TEXT,
start INT,
strand TEXT
);
CREATE TABLE attributes(ID TEXT, Parent TEXT, type TEXT, start INT);
CREATE TABLE IF NOT EXISTS "dm1pct"(
seqid TEXT,
source TEXT,
type TEXT,
start INT,
"end" INT,
score NUM,
strand TEXT,
phase TEXT
);
"""
case 3:
print(k)
case 4:
print(k)
case 5:
print(k)
case 6:
print(k)
else:
sys.exit(f'aborting: database {db} does not exist')
dbname = "dm1pct.db"
# main_table = "dm1pct"
# create_att_table(dbname, table)
normal_forms(dbname, 1)
normal_forms(dbname, 2)
# tbl = "attributes"
# print(rank_col_uniqueness(dbname, main_table))