-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
306 lines (272 loc) · 9.37 KB
/
functions.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
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
from json import dumps, dump, load
import requests
from typing import Optional
from . import classes
def stringify(value_list):
line = ";".join([str(value) for value in value_list])
return line + "\n"
def build_basic_project_dict(
project_name: str,
metadata_keys: list,
metadata_values: list,
tree_calcs: list,
raw_matrices: list=list(),
hidden: list=list()):
"""
Create a Microreact project where the metadata table is constructed from lists of keys and values.
project_name: the name that will be shown for the project
metadata_keys: keys of the metadata fields as a list. The first one will become the id field
metadata_values: metadata values as a list of lists
trees: list with trees in Newick format (as strings)
Returns: a dict structure that is validated with MR's JSON schema and can be converted to JSON with json.dumps().
"""
project_meta = classes.Meta(name=project_name)
id_field_name = metadata_keys[0]
metadata_body = str()
metadata_body += stringify(metadata_keys) # Add keys as first line in body
for record in metadata_values:
metadata_body += stringify(record)
metadata_file = classes.File(type='data', body=metadata_body)
dataset = classes.Dataset(file=metadata_file.id, idFieldName=id_field_name)
files = [metadata_file]
trees = list()
tree_number = 1
for tree_calc in tree_calcs:
newick_file = classes.File(type='tree', body=tree_calc['result'])
files.append(newick_file)
tree = classes.Tree(
type='rc',
title=tree_calc['method'],
labelField=id_field_name,
file=newick_file.id,
highlightedId=None
)
trees.append(tree)
tree_number += 1
table = classes.Table(title='Metadata', columns=metadata_keys, file=metadata_file.id, hidden=hidden, dataset=dataset)
matrices = list()
matrix_idx = 0
for raw_matrix in raw_matrices:
matrix_idx += 1
matrix_file = classes.File(type='data', body=raw_matrix, name="matrix_" + str(matrix_idx))
files.append(matrix_file)
matrix=classes.Matrix(file=matrix_file.id, title="Matrix " + str(matrix_idx))
matrices.append(matrix)
project = classes.Project(
meta=project_meta,
datasets=[dataset],
files=files,
tables=[table],
trees=trees,
matrices=matrices
)
return project.to_dict()
def build_basic_project_dict_2(
project_name: str,
metadata_url: str,
columns: list,
tree_calcs: list=list(),
raw_matrices: list=list(),
hidden: list=list()
):
"""
Create a Microreact project where the metadata table is defined using an external URL.
project_name: the name that will be shown for the project
metadata_url: url containing af metadatalist. The first column will become the id field
columns: list of columns to read from metadata_url. Only these column will exist in the Microreact project,
and per default all columns will be visible.
tree_calcs: list with trees in Newick format (as strings)
hidden: an optional list of columns to be marked and hidden in the Microreact project.
Returns: a dict structure that is validated with MR's JSON schema and can be converted to JSON with json.dumps().
"""
project_meta = classes.Meta(name=project_name)
id_field_name = columns[0]
metadata_file = classes.File(type='data', url=metadata_url)
dataset = classes.Dataset(file=metadata_file.id, idFieldName=id_field_name)
files = [metadata_file]
trees = list()
tree_number = 1
for tree_calc in tree_calcs:
newick_file = classes.File(type='tree', body=tree_calc['result'])
files.append(newick_file)
tree = classes.Tree(
type='rc',
title=tree_calc['method'],
labelField=id_field_name,
file=newick_file.id,
highlightedId=None
)
trees.append(tree)
tree_number += 1
table = classes.Table(title='Metadata',
columns=columns,
file=metadata_file.id,
dataset=dataset.id,
hidden=hidden
)
matrices = list()
matrix_idx = 0
for raw_matrix in raw_matrices:
matrix_idx += 1
matrix_file = classes.File(type='data', body=raw_matrix, name="matrix_" + str(matrix_idx))
files.append(matrix_file)
matrix=classes.Matrix(file=matrix_file.id, title="Matrix " + str(matrix_idx))
matrices.append(matrix)
project = classes.Project(
meta=project_meta,
datasets=[dataset],
files=files,
tables=[table],
trees=trees,
matrices=matrices
)
return project.to_dict()
def new_project(
project_name: str,
metadata_keys: list,
metadata_values: list,
mr_access_token: str,
mr_base_url: str,
tree_calcs: list = list(),
hidden:list = list(),
raw_matrices: list[str] = list(),
public: bool=False,
verify: bool=True
):
project_dict = build_basic_project_dict(
project_name,
metadata_keys,
metadata_values,
tree_calcs,
hidden=hidden,
raw_matrices=raw_matrices
)
json_data = dumps(project_dict)
url = mr_base_url + '/api/projects/create/'
if not public:
url = url + '?access=private'
rest_response = requests.post(
url,
headers= {
'Content-Type': 'application/json; charset=utf-8',
'Access-Token': mr_access_token
},
data=json_data,
verify=verify
)
return rest_response
def new_project_2(
project_name: str,
metadata_url: str,
columns: list,
mr_access_token: str,
mr_base_url: str,
tree_calcs: list = list(),
hidden:list = list(),
raw_matrices: list[str] = list(),
public: bool=False,
verify: bool=True
):
print(f"Metadata URL: {metadata_url}")
project_dict = build_basic_project_dict_2(
project_name,
metadata_url,
columns,
tree_calcs=tree_calcs,
hidden=hidden,
raw_matrices=raw_matrices
)
json_data = dumps(project_dict, indent=4)
url = mr_base_url + '/api/projects/create/'
if not public:
url = url + '?access=private'
rest_response = requests.post(
url,
headers= {
'Content-Type': 'application/json; charset=utf-8',
'Access-Token': mr_access_token
},
data=json_data,
verify=verify
)
return rest_response
def new_project_file(
project_name: str,
tree_calcs: list,
metadata_keys: list,
metadata_values: list,
file_name: str
):
project_dict = build_basic_project_dict(project_name, metadata_keys, metadata_values, tree_calcs)
with open(file_name , "w") as f:
dump(project_dict, f)
print(f"Saved {file_name}")
def get_project_json(
project_id: str,
mr_access_token: str,
mr_base_url: str,
verify: bool=True
):
url = mr_base_url + '/api/projects/json?project=' + project_id
rest_response = requests.get(
url,
headers= {
'Content-Type': 'application/json; charset=utf-8',
'Access-Token': mr_access_token
},
verify=verify
)
return rest_response
def update_project(
project_id: str,
project_dict: dict,
mr_access_token: str,
mr_base_url: str,
verify: bool=True
):
json_data = dumps(project_dict)
url = mr_base_url + '/api/projects/update/'
print(project_id)
rest_response = requests.post(
url,
headers= {
'Content-Type': 'application/json; charset=utf-8',
'Access-Token': mr_access_token,
},
data=json_data,
params={'project': project_id},
verify=verify
)
return rest_response
def add_element(project_dict:dict, section_name:str, new_element_dict:dict):
section_elements = project_dict.pop(section_name)
new_element_id = new_element_dict['id']
assert new_element_id not in section_elements
section_elements[new_element_id] = new_element_dict
project_dict[section_name] = section_elements
# validate_json(project_dict) Output from /api/projects/json does not currently validate
return project_dict, new_element_id
def add_tree_fn(project_id, newick, mr_access_token, mr_base_url, verify):
rest_response = get_project_json(
project_id=project_id,
mr_access_token=mr_access_token,
mr_base_url=mr_base_url,
verify=verify
)
project_dict = rest_response.json()
"""We have to add something to both the files section and the trees section.
The newick data will go into the files section."""
# files section
new_file_dict = classes.File(type='tree', body=newick).to_dict()
project_dict, new_file_id = add_element(project_dict, 'files', new_file_dict)
# trees section
new_tree_dict = classes.Tree(file=new_file_id).to_dict()
project_dict, _new_tree_id = add_element(project_dict, 'trees', new_tree_dict)
rest_response = update_project(
project_id=project_id,
project_dict=project_dict,
mr_access_token=mr_access_token,
mr_base_url=mr_base_url,
verify = verify
)
return rest_response