IanYeo commited on
Commit
95ca0ab
·
1 Parent(s): d9c7900

Initial commit

Browse files
Files changed (1) hide show
  1. app.py +221 -0
app.py ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[1]:
5
+
6
+ import numpy as np
7
+ import pandas as pd
8
+ import regex as re
9
+ import streamlit as st
10
+
11
+
12
+ # In[2]:
13
+
14
+ cobie_file = pd.ExcelFile(path_to_cobie_file)
15
+
16
+ cobie_floor_df = cobie_file.parse(sheet_name = 'Floor', dtype={'ExtIdentifier':str, 'Name':str})
17
+ cobie_space_df = cobie_file.parse(sheet_name = 'Space', dtype={'ExtIdentifier':str, 'Name':str})
18
+ cobie_type_df = cobie_file.parse(sheet_name = 'Type', dtype={'ExtIdentifier':str, 'Description':str, 'Name':str})
19
+ cobie_system_df = cobie_file.parse(sheet_name = 'System', dtype={'ExtIdentifier':str, 'Description':str, 'Name':str})
20
+ cobie_component_df = cobie_file.parse(sheet_name = 'Component', dtype={'ExtIdentifier':str, 'Space':str, 'Description':str, 'Name':str})
21
+
22
+
23
+ def decodeIfc(txt):
24
+ # In regex "\" is hard to manage in Python... I use this workaround
25
+ txt = txt.replace('\\', 'µµµ')
26
+ txt = re.sub('µµµX2µµµ([0-9A-F]{4,})+µµµX0µµµ', decodeIfcX2, txt)
27
+ txt = re.sub('µµµSµµµ(.)', decodeIfcS, txt)
28
+ txt = re.sub('µµµXµµµ([0-9A-F]{2})', decodeIfcX, txt)
29
+ txt = txt.replace('µµµ','\\')
30
+ return txt
31
+
32
+ def decodeIfcX2(match):
33
+ # X2 encodes characters with multiple of 4 hexadecimal numbers.
34
+ return ''.join(list(map(lambda x : chr(int(x,16)), re.findall('([0-9A-F]{4})',match.group(1)))))
35
+
36
+ def decodeIfcS(match):
37
+ return chr(ord(match.group(1))+128)
38
+
39
+ def decodeIfcX(match):
40
+ # Sometimes, IFC files were made with old Mac... wich use MacRoman encoding.
41
+ num = int(match.group(1), 16)
42
+ if (num <= 127) | (num >= 160):
43
+ return chr(num)
44
+ else:
45
+ return bytes.fromhex(match.group(1)).decode("macroman")
46
+
47
+ def convert_unicode_string(row, column_name):
48
+ return decodeIfc(row[column_name])
49
+
50
+
51
+ columns_to_decode = ['Name', 'TypeName', 'Description']
52
+ for column_to_decode in columns_to_decode:
53
+
54
+ cobie_flat[column_to_decode] = cobie_flat.apply(
55
+ convert_unicode_string,
56
+ column_name=column_to_decode,
57
+ axis=1
58
+ )
59
+
60
+
61
+ # In[3]:
62
+
63
+ cobie_type_df.rename(columns={
64
+ 'Name':'TypeName',
65
+ 'ExtObject':'TypeExtObject',
66
+ 'ExtIdentifier':'TypeExtIdentifier',
67
+ }, inplace=True)
68
+
69
+ cobie_type_component = pd.merge(
70
+ cobie_component_df[[
71
+ 'Name','TypeName', 'Space',
72
+ 'ExtObject', 'ExtIdentifier', 'SerialNumber',
73
+ ]],
74
+ cobie_type_df[[
75
+ 'TypeName', 'Category', 'Description',
76
+ 'Manufacturer', 'ModelNumber',
77
+ 'TypeExtObject', 'TypeExtIdentifier',
78
+ ]],
79
+ on='TypeName',
80
+ how='left',
81
+ )
82
+
83
+
84
+ # In[4]:
85
+
86
+ cobie_system_df.rename(columns={
87
+ 'Name':'SystemName',
88
+ 'Description':'SystemDescription',
89
+ 'Category':'SystemCategory',
90
+ 'ComponentNames':'Name',
91
+ }, inplace=True)
92
+ system_all = cobie_system_df.explode(column='Name')
93
+
94
+ cobie_flat = pd.merge(
95
+ cobie_type_component,
96
+ system_all[[
97
+ 'SystemName', 'SystemDescription', 'SystemCategory',
98
+ 'Name',
99
+ ]],
100
+ on='Name',
101
+ how='left',
102
+ )
103
+
104
+ cobie_flat = cobie_flat[[
105
+ 'Name',
106
+ 'TypeName',
107
+ 'Description',
108
+ 'Category',
109
+ 'SystemName',
110
+ 'SystemDescription',
111
+ 'SystemCategory',
112
+ 'Space',
113
+ 'ExtObject',
114
+ 'ExtIdentifier',
115
+ 'SerialNumber',
116
+ 'Manufacturer',
117
+ 'ModelNumber',
118
+ 'TypeExtObject',
119
+ 'TypeExtIdentifier',
120
+
121
+ ]]
122
+
123
+
124
+
125
+ # In[5]:
126
+
127
+
128
+
129
+ # In[7]:
130
+
131
+
132
+
133
+ # In[8]:
134
+
135
+
136
+
137
+ # In[9]:
138
+
139
+
140
+ # In[10]:
141
+
142
+
143
+
144
+ # In[11]:
145
+
146
+
147
+
148
+ # In[12]:
149
+
150
+
151
+
152
+ # In[13]:
153
+
154
+
155
+
156
+
157
+ # In[ ]:
158
+
159
+ project_id_button = st.text_input("Project ID", key="project_id")
160
+
161
+ # In[ ]:
162
+
163
+ if project_id_button:
164
+
165
+ project_id = st.session_state.project_id
166
+ with st.spinner(text="In progress, this may take some time..."):
167
+ # db = sqlalchemy.create_engine(postgres_string)
168
+ # conn = psycopg2.connect(
169
+ # dbname="EIR-BEP",
170
+ # host="activodb.cnwtnluwiep3.eu-west-1.rds.amazonaws.com",
171
+ # user="ianyeo",
172
+ # password=password
173
+ # )
174
+ # st.write("Connected to database", str(conn))
175
+ project_responsibility_query = 'SELECT * from "projectResponsibility"'
176
+ project_specific_responsibility_query = f'SELECT * from "projectResponsibility" WHERE project_id = {project_id}'
177
+ raci_query = 'SELECT * from "raci"'
178
+ responsibility_query = 'SELECT * from "responsibility"'
179
+ project_responsibility_raci_query = 'SELECT * from "projectResponsibilityRACI"'
180
+ project_responsibility_raci_orgnanisation_query = 'SELECT * from "projectResponsibilityRACIorganisation"'
181
+ project_organisaion_query = 'SELECT po.organisation_id, org.name, org.organisation_codes FROM "projectorganisation" AS po JOIN "organisation" AS org ON po.organisation_id = org.id WHERE po.project_id = %(project_id)s'
182
+ test_query = '''
183
+ SELECT
184
+ precomputed.id
185
+ FROM rnc_rna_precomputed precomputed
186
+ JOIN rnc_taxonomy tax
187
+ ON
188
+ tax.id = precomputed.taxid
189
+ WHERE
190
+ tax.lineage LIKE 'cellular organisms; Bacteria; %%'
191
+ AND precomputed.is_active = true -- exclude sequences without active cross-references
192
+ AND rna_type = 'rRNA'
193
+ '''
194
+ test_query2 = '''
195
+ SELECT
196
+ upi, -- RNAcentral URS identifier
197
+ taxid, -- NCBI taxid
198
+ ac -- external accession
199
+ FROM xref
200
+ WHERE ac IN ('OTTHUMT00000106564.1', 'OTTHUMT00000416802.1')
201
+ '''
202
+ rows = run_query(project_specific_responsibility_query)
203
+ raci = pd.read_sql_query(raci_query, get_connection(), params={'project_id':project_id})
204
+ # test_data = pd.read_sql_query(test_query2, get_connection())
205
+
206
+ # project_responsibilities = pd.read_sql_query(project_responsibility_query, db, params={'project_id':project_id})
207
+ # responsibility = pd.read_sql_query(responsibility_query, db, params={'project_id':project_id})
208
+ # project_responsibility_raci = pd.read_sql_query(project_responsibility_raci_query, db, params={'project_id':project_id})
209
+ # project_organisaion = pd.read_sql_query(project_organisaion_query, db, params={'project_id':project_id})
210
+
211
+ # st.dataframe(project_organisaion)
212
+ st.dataframe(raci)
213
+
214
+ create_template_table = st.text_input("test", key="test")
215
+
216
+ if create_template_table:
217
+
218
+ download_table = st.session_state.test
219
+ # raci_table = create_raci_table(project_responsibilities, responsibility, project_responsibility_raci)
220
+
221
+