-
Notifications
You must be signed in to change notification settings - Fork 1
/
longevity2reporter.py
179 lines (149 loc) · 6.07 KB
/
longevity2reporter.py
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
import json
from cravat.cravat_report import CravatReport
import sys
import datetime
import re
import csv
import zipfile
from pathlib import Path
import sqlite3
from mako.template import Template
from mako import exceptions
class Reporter(CravatReport):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.savepath = kwargs["savepath"]
async def run(self, *args, **kwargs):
self.setup()
self.write_data()
self.end()
pass
def setup(self):
self.input_database_path = f'{self.savepath}_longevity.sqlite'
self.db_conn = sqlite3.connect(self.input_database_path)
self.db_cursor = self.db_conn.cursor()
outpath = f'{self.savepath}.longevity2.html'
self.outfile = open(outpath, 'w', encoding='utf-8')
self.template = Template(filename=str(Path(__file__).parent / "template.html"))
def write_table(self, name, json_fields = [], sort_field = "", sort_revers = False):
try:
sort_sql = ""
if sort_field != "":
sort_sql = " ORDER BY " + sort_field
if sort_revers:
sort_sql = sort_sql+" DESC"
else:
sort_sql = sort_sql+" ASC"
if name == "longevitymap":
res = {}
categories = (
'other', 'tumor-suppressor', 'inflammation', 'genome_maintenance', 'mitochondria', 'lipids',
'heat-shock', 'sirtuin', 'insulin', 'antioxidant', 'renin-angiotensin', 'mtor')
for category in categories:
res[category]=[]
try:
self.db_cursor.execute("SELECT * FROM "+name+sort_sql + ", category_name")
except:
print(f"No module just_{name}")
return res
rows = self.db_cursor.fetchall()
for category in categories:
for row in rows:
tmp = {}
if category == row[18]:
for i, item in enumerate(self.db_cursor.description):
if item[0] in json_fields:
lst = json.loads(row[i])
if len(lst) == 0:
tmp[item[0]] = ""
else:
tmp[item[0]] = lst
else:
tmp[item[0]] = row[i]
res[category].append(tmp)
return res
try:
self.db_cursor.execute("SELECT * FROM "+name+sort_sql)
except:
print(f"No module just_{name}")
res = []
return res
rows = self.db_cursor.fetchall()
res = []
for row in rows:
tmp = {}
for i, item in enumerate(self.db_cursor.description):
if item[0] in json_fields:
lst = json.loads(row[i])
if len(lst) == 0:
tmp[item[0]] = ""
else:
tmp[item[0]] = lst
else:
tmp[item[0]] = row[i]
res.append(tmp)
return res
except Exception as e:
print("Warning:", e)
def write_table_to_dict(self, name, key_field, json_fields = [], sort_field = "", sort_revers = False):
if key_field in json_fields:
raise ValueError("key_field should not be in json_fields")
sort_sql = ""
if sort_field != "":
sort_sql = " ORDER BY " + sort_field
if sort_revers:
sort_sql = sort_sql+" DESC"
else:
sort_sql = sort_sql+" ASC"
try:
self.db_cursor.execute("SELECT * FROM "+name+sort_sql)
except:
print(f"No module just_{name}")
res = []
return res
rows = self.db_cursor.fetchall()
res = dict()
col_value = None
for row in rows:
tmp = {}
for i, item in enumerate(self.db_cursor.description):
col_name = item[0]
if col_name in json_fields:
lst = json.loads(row[i])
if len(lst) == 0:
tmp[col_name] = ""
else:
tmp[col_name] = lst
else:
tmp[col_name] = row[i]
if col_name == key_field:
col_value = row[i]
if col_value is None:
raise ValueError("key_field is not in list of available fields")
res[col_value] = tmp
return res
def write_data(self):
# self.data = {"test1":[1,2,3], "test2":["aa", "bbb", "cccc"]}
data = {}
data["prs"] = self.write_table_to_dict("prs", "name")
data["longevitymap"] = self.write_table("longevitymap", ["conflicted_rows", "description"], "weight", False)
data["cancer"] = self.write_table("cancer", [], "id", True)
data["coronary"] = self.write_table("coronary", [], "weight", False)
data["drugs"] = self.write_table("drugs", [], "effect", True)
data["cardio"] = self.write_table("cardio", [], "id", True)
data["lipidmetabolism"] = self.write_table("lipid_metabolism", [], "weight", False)
data["thrombophilia"] = self.write_table("thrombophilia", [], "weight", False)
data["vo2max"] = self.write_table("vo2max", [], "weight", False)
try:
self.outfile.write(self.template.render(data=data))
except:
print(exceptions.text_error_template().render())
def end(self):
self.outfile.close()
return Path(self.outfile.name).resolve()
### Don't edit anything below here ###
def main():
reporter = Reporter(sys.argv)
reporter.run()
if __name__ == '__main__':
main()