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
|
import csv
import sys
import html
import json
import requests
from lxml import etree
from pathlib import Path
from lxml.html import parse
from functools import reduce
from link_checker import check_page
def corrs_base_data():
return [
{
"dataset": "HC_M2_0606_P",
"trait_id": "1435464_at",
"corr_dataset": "HC_M2_0606_P",
},
{
"dataset": "HC_M2_0606_P",
"trait_id": "1457545_at",
"corr_dataset": "HC_M2_0606_R",
},
{
"dataset": "HC_M2_0606_P",
"trait_id": "1442370_at",
"corr_dataset": "BXDPublish",
}
]
def sample_vals():
return '{"C57BL/6J":"10.835","DBA/2J":"11.142","B6D2F1":"11.126","D2B6F1":"11.143","BXD1":"10.811","BXD2":"11.503","BXD5":"10.766","BXD6":"10.986","BXD8":"11.050","BXD9":"10.822","BXD11":"10.670","BXD12":"10.946","BXD13":"10.890","BXD14":"x","BXD15":"10.884","BXD16":"11.222","BXD18":"x","BXD19":"10.968","BXD20":"10.962","BXD21":"10.906","BXD22":"11.080","BXD23":"11.046","BXD24":"11.146","BXD24a":"x","BXD25":"x","BXD27":"11.078","BXD28":"11.034","BXD29":"10.808","BXD30":"x","BXD31":"11.087","BXD32":"11.029","BXD33":"10.662","BXD34":"11.482","BXD35":"x","BXD36":"x","BXD37":"x","BXD38":"10.836","BXD39":"10.926","BXD40":"10.638","BXD41":"x","BXD42":"10.974","BXD43":"10.828","BXD44":"10.900","BXD45":"11.358","BXD48":"11.042","BXD48a":"10.975","BXD49":"x","BXD50":"11.228","BXD51":"11.126","BXD52":"x","BXD53":"x","BXD54":"x","BXD55":"11.580","BXD56":"x","BXD59":"x","BXD60":"10.829","BXD61":"11.152","BXD62":"11.156","BXD63":"10.942","BXD64":"10.506","BXD65":"11.126","BXD65a":"11.272","BXD65b":"11.157","BXD66":"11.071","BXD67":"11.080","BXD68":"10.997","BXD69":"11.096","BXD70":"11.152","BXD71":"x","BXD72":"x","BXD73":"11.262","BXD73a":"11.444","BXD73b":"x","BXD74":"10.974","BXD75":"11.150","BXD76":"10.920","BXD77":"10.928","BXD78":"x","BXD79":"11.371","BXD81":"x","BXD83":"10.946","BXD84":"11.181","BXD85":"10.992","BXD86":"10.770","BXD87":"11.200","BXD88":"x","BXD89":"10.930","BXD90":"11.183","BXD91":"x","BXD93":"11.056","BXD94":"10.737","BXD95":"x","BXD98":"10.986","BXD99":"10.892","BXD100":"x","BXD101":"x","BXD102":"x","BXD104":"x","BXD105":"x","BXD106":"x","BXD107":"x","BXD108":"x","BXD109":"x","BXD110":"x","BXD111":"x","BXD112":"x","BXD113":"x","BXD114":"x","BXD115":"x","BXD116":"x","BXD117":"x","BXD119":"x","BXD120":"x","BXD121":"x","BXD122":"x","BXD123":"x","BXD124":"x","BXD125":"x","BXD126":"x","BXD127":"x","BXD128":"x","BXD128a":"x","BXD130":"x","BXD131":"x","BXD132":"x","BXD133":"x","BXD134":"x","BXD135":"x","BXD136":"x","BXD137":"x","BXD138":"x","BXD139":"x","BXD141":"x","BXD142":"x","BXD144":"x","BXD145":"x","BXD146":"x","BXD147":"x","BXD148":"x","BXD149":"x","BXD150":"x","BXD151":"x","BXD152":"x","BXD153":"x","BXD154":"x","BXD155":"x","BXD156":"x","BXD157":"x","BXD160":"x","BXD161":"x","BXD162":"x","BXD165":"x","BXD168":"x","BXD169":"x","BXD170":"x","BXD171":"x","BXD172":"x","BXD173":"x","BXD174":"x","BXD175":"x","BXD176":"x","BXD177":"x","BXD178":"x","BXD180":"x","BXD181":"x","BXD183":"x","BXD184":"x","BXD186":"x","BXD187":"x","BXD188":"x","BXD189":"x","BXD190":"x","BXD191":"x","BXD192":"x","BXD193":"x","BXD194":"x","BXD195":"x","BXD196":"x","BXD197":"x","BXD198":"x","BXD199":"x","BXD200":"x","BXD201":"x","BXD202":"x","BXD203":"x","BXD204":"x","BXD205":"x","BXD206":"x","BXD207":"x","BXD208":"x","BXD209":"x","BXD210":"x","BXD211":"x","BXD212":"x","BXD213":"x","BXD214":"x","BXD215":"x","BXD216":"x","BXD217":"x","BXD218":"x","BXD219":"x","BXD220":"x"}'
def do_request(url, data):
response = requests.post(
url,
data={
"dataset": "HC_M2_0606_P",
"trait_id": "1435464_at",
"corr_dataset": "HC_M2_0606_P",
"corr_sample_method": "pearson",
"corr_return_results": "100",
"corr_samples_group": "samples_primary",
"sample_vals": sample_vals(),
"location_type": "gene",
**data,
})
while response.text.find('<meta http-equiv="refresh" content="5">') >= 0:
response = requests.get(response.url)
pass
return response
def check_sample_correlations(baseurl, base_data):
data = {
**base_data,
"corr_type": "sample",
"corr_sample_method": "pearson",
"location_type": "gene",
"corr_return_results": "200"
}
top_n_message = "The top 200 correlations ranked by the Genetic Correlation"
result = do_request(f"{baseurl}/corr_compute", data)
assert result.status_code == 200
assert (result.text.find(f"Values of record {base_data['trait_id']}") >= 0), result.text
assert (result.text.find(top_n_message) >= 0), result.text
def check_tissue_correlations(baseurl, base_data):
data = {
**base_data,
"corr_type": "tissue",
"location_type": "gene",
}
result = do_request(f"{baseurl}/corr_compute", data)
assert result.status_code == 200
if (data["trait_id"] == "1442370_at"
and data["corr_dataset"] in ("BXDPublish",)):
top_n_message = (
"It is not possible to compute the 'Tissue' correlations between "
f"trait '{data['trait_id']}' and the data")
else:
top_n_message = "The top 100 correlations ranked by the Tissue Correlation"
assert (result.text.find(f"Values of record {base_data['trait_id']}") >= 0), result.text
assert (html.unescape(result.text).find(top_n_message) >= 0), (
f"NOT FOUND: {top_n_message}")
def check_lit_correlations(baseurl, base_data):
data = {
**base_data,
"corr_type": "lit",
"corr_return_results": "200"
}
result = do_request(f"{baseurl}/corr_compute", data)
assert result.status_code == 200
if (data["trait_id"] == "1442370_at"
and data["corr_dataset"] in ("BXDPublish",)):
top_n_message = (
"It is not possible to compute the 'Literature' correlations "
f"between trait '{data['trait_id']}' and the data")
else:
top_n_message = "The top 200 correlations ranked by the Literature Correlation"
assert (result.text.find(f"Values of record {base_data['trait_id']}") >= 0), result.text
assert (html.unescape(result.text).find(top_n_message) >= 0), (
f"NOT FOUND: {top_n_message}")
def check_correlations(args_obj, parser):
print("")
print("Checking the correlations...")
corr_type_fns = {
"sample": check_sample_correlations,
"tissue": check_tissue_correlations,
"lit": check_lit_correlations
}
host = args_obj.host
failure = False
for corr_type, corr_type_fn in corr_type_fns.items():
for corr_base in corrs_base_data():
try:
print(f"\tChecking {corr_type} correlations...", end="")
corr_type_fn(host, corr_base)
print(" ok")
except AssertionError as asserterr:
print (f" fail: {asserterr.args[0]}")
failure = True
if failure:
print("FAIL!")
sys.exit(1)
print("OK")
def thread(value, *functions):
return reduce(lambda result, func: func(result), functions, value)
def parse_results_from_html(raw_html):
doc = etree.HTML(raw_html)
scripts = doc.xpath('//script')
for script in scripts:
script_content = thread(
script.xpath('.//child::text()'),
lambda val: "".join(val).strip())
if script_content.find("var tableJson") >= 0:
return json.loads(thread(
script_content,
lambda val: val[len("var tableJson = "):].strip()))
return []
def parse_expected(filepath):
with open(filepath, encoding="utf-8") as infl:
reader = csv.DictReader(infl, dialect=csv.unix_dialect)
for line in reader:
yield line
def collect_failures(actual, expected, keys):
# assert len(actual) == len(expected), (
# f"Expected {len(expected)} results but instead got {len(actual)} "
# "results")
def __equal(act_row, exp_row):
__eq = tuple()
for act_key, exp_key, title in keys:
act_val, exp_val = (act_row[act_key], exp_row[exp_key])
if act_val == exp_val:
# __eq = __eq + ("PASSED",)
continue
__eq = __eq + ((
f"Different '{title}' values: expected: '{exp_val}' but got "
f"'{act_val}'"),)
continue
return __eq
return tuple(
__equal(act_row, exp_row)
for act_row, exp_row in zip(actual, expected))
def check_correctness(host):
pearsons_keys = (
("trait_id", "Record ID", "Trait/Record ID"),
("sample_r", "Sample r ?", "Sample r value"),
("num_overlap", "N Cases", "N Cases"),
("sample_p", "Sample p(r) ?", "Sample p value"),
("symbol", "Symbol", "Symbol"),
("description", "Description", "Description"),
("location", "Location Chr and Mb", "Location Chr and Mb"),
("mean", "Mean Expr", "Mean"),
("lrs_location", "Max LRS Location Chr and Mb", "Max LRS Location Chr and Mb"),
("lit_corr", "Lit Corr ?", "Literature Correlation"),
("tissue_corr", "Tissue r ?", "Tissue Correlation r"),
("tissue_pvalue", "Tissue p(r) ?", "Tissue Correlation p value"))
spearmans_keys = (
("trait_id", "Record ID", "Trait/Record ID"),
("sample_r", "Sample rho ?", "Sample rho value"),
("num_overlap", "N Cases", "N Cases"),
("sample_p", "Sample p(rho) ?", "Sample p(rho) value"),
("symbol", "Symbol", "Symbol"),
("description", "Description", "Description"),
("location", "Location Chr and Mb", "Location Chr and Mb"),
("mean", "Mean Expr", "Mean"),
("lrs_location", "Max LRS Location Chr and Mb", "Max LRS Location Chr and Mb"),
("lit_corr", "Lit Corr ?", "Literature Correlation"),
("tissue_corr", "Tissue rho ?", "Tissue Correlation rho"),
("tissue_pvalue", "Tissue p(rho) ?", "Tissue Correlation p(rho) value"))
failures = {}
tests = [
("Trait '1435464_at' (Dataset 'HC_M2_0606_P'): Sample Correlation, Pearson, 500 results",
{"dataset": "HC_M2_0606_P", "trait_id": "1435464_at",
"corr_dataset": "HC_M2_0606_P", "corr_type": "sample",
"corr_sample_method": "pearson", "location_type": "gene",
"corr_samples_group": "samples_primary", "sample_vals": sample_vals(),
"corr_return_results": "500"},
"HC_M2_0606_P___1435464_at___sample_r___pearson__results.csv",
pearsons_keys),
("Trait '1435464_at' (Dataset 'HC_M2_0606_P'): Sample Correlation, Spearmans, 500 results",
{"dataset": "HC_M2_0606_P", "trait_id": "1435464_at",
"corr_dataset": "HC_M2_0606_P", "corr_type": "sample",
"corr_sample_method": "spearman", "location_type": "gene",
"corr_samples_group": "samples_primary", "sample_vals": sample_vals(),
"corr_return_results": "500"},
"HC_M2_0606_P___1435464_at___sample_r___spearman___no_filters__results.csv",
spearmans_keys),
("Trait '1435464_at' (Dataset 'HC_M2_0606_P'): Tissue Correlation, Pearsons, 500 results",
{"dataset": "HC_M2_0606_P", "trait_id": "1435464_at",
"corr_dataset": "HC_M2_0606_P", "corr_type": "tissue",
"corr_sample_method": "pearson", "location_type": "gene",
"corr_samples_group": "samples_primary", "sample_vals": sample_vals(),
"corr_return_results": "500"},
"HC_M2_0606_P___1435464_at___tissue_r___pearson__results.csv",
pearsons_keys),
("Trait '1435464_at' (Dataset 'HC_M2_0606_P'): Tissue Correlation, Spearmans, 500 results",
{"dataset": "HC_M2_0606_P", "trait_id": "1435464_at",
"corr_dataset": "HC_M2_0606_P", "corr_type": "tissue",
"corr_sample_method": "spearman", "location_type": "gene",
"corr_samples_group": "samples_primary", "sample_vals": sample_vals(),
"corr_return_results": "500"},
"HC_M2_0606_P___1435464_at___tissue_r___spearman__results.csv",
spearmans_keys)]
for test_title, test_data, expected_file, method_keys in tests:
print(f"Test: {test_title} ...", end="\t")
response = requests.post(f"{host}/corr_compute", data=test_data)
while response.text.find('<meta http-equiv="refresh" content="5">') >= 0:
response = requests.get(response.url)
pass
results = parse_results_from_html(response.text)
if len(results) == 0:
failures = {
**failures,
test_title: (("No results found.",),)}
continue
filepath = Path.cwd().parent.joinpath(
f"test/requests/correlation_results_text_files/{expected_file}")
failures = {
**failures,
test_title: collect_failures(
results, tuple(parse_expected(filepath)), method_keys)
}
if len(failures) > 0:
print("\n\nFAILURES: ")
for test_title, failures in failures.items():
print(f"\nTest: {test_title}")
for result, result_failures in enumerate(failures):
for failure in result_failures:
print(f"\tResult {result}: {failure}")
print("")
return False
return True
def check_correlations_correctness(args_obj, parser):
print("")
print("Checking the correctness of the correlations...")
if not check_correctness(args_obj.host):
sys.exit(1)
|