This repository has been archived by the owner on Jan 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconftest.py
205 lines (166 loc) · 5.44 KB
/
conftest.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
import glob
import importlib.resources
import os
import shutil
from unittest.mock import patch
import polars as pl
import pytest
import requests as req
from pgscatalog_utils.download.download_scorefile import download_scorefile
from pgscatalog_utils.match.preprocess import complement_valid_alleles
from pgscatalog_utils.scorefile.combine_scorefiles import combine_scorefiles
from pgscatalog_utils.scorefile.scorevariant import ScoreVariant
from tests.data import combine
pl.toggle_string_cache(True)
@pytest.fixture(scope="session")
def pgs_accessions():
return ["PGS001229", "PGS000922"]
@pytest.fixture(scope="session")
def mini_score_path(tmp_path_factory):
path = importlib.resources.files(combine) / "PGS001229_22.txt"
return str(path)
@pytest.fixture(scope="session")
def mini_scorefile(mini_score_path, tmp_path_factory):
# The mini scorefile overlaps well with cineca synthetic subset
out_path = tmp_path_factory.mktemp("scores") / "mini_score.txt"
args: list[str] = (
["combine_scorefiles", "-t", "GRCh37", "-s"]
+ [mini_score_path]
+ ["-o", str(out_path.resolve())]
)
with patch("sys.argv", args):
combine_scorefiles()
return str(out_path.resolve())
@pytest.fixture(scope="session")
def scorefiles(tmp_path_factory, pgs_accessions):
fn = tmp_path_factory.mktemp("scorefiles")
args: list[str] = [
"download_scorefiles",
"-b",
"GRCh37",
"-o",
str(fn.resolve()),
"-i",
] + pgs_accessions
with patch("sys.argv", args):
download_scorefile()
return glob.glob(os.path.join(fn.resolve(), "*.txt.gz"))
@pytest.fixture(scope="session")
def target_path(tmp_path_factory):
try:
bim = req.get(
"https://gitlab.ebi.ac.uk/nebfield/test-datasets/-/raw/master/pgsc_calc/cineca_synthetic_subset.bim",
timeout=5,
)
except (req.exceptions.ConnectionError, req.Timeout):
bim = []
if not bim:
pytest.skip("Couldn't get test data from network")
else:
fn = tmp_path_factory.mktemp("target") / "data.bim"
with open(fn, "wb") as f:
f.write(bim.content)
return str(fn.resolve())
@pytest.fixture(scope="session")
def chain_files(tmp_path_factory):
chain_dir = tmp_path_factory.mktemp("chain_dir")
shutil.copy2("tests/data/hg19ToHg38.over.chain.gz", chain_dir)
shutil.copy2("tests/data/hg38ToHg19.over.chain.gz", chain_dir)
return str(chain_dir.resolve())
@pytest.fixture(scope="session")
def hg38_coords():
rs11903757 = ScoreVariant(
**{
"rsid": "rs11903757",
"chr_name": "2",
"chr_position": 191722478,
"row_nr": 0,
"effect_weight": 1,
"accession": "test",
"effect_allele": "A",
}
)
rs6061231 = ScoreVariant(
**{
"rsid": "rs6061231",
"chr_name": "20",
"chr_position": 62381861,
"row_nr": 1,
"effect_weight": 1,
"accession": "test",
"effect_allele": "A",
}
)
return (x for x in [rs11903757, rs6061231])
@pytest.fixture(scope="session")
def hg19_coords():
# hg38_coords in GRCh37, from dbSNP
rs11903757 = ScoreVariant(
**{
"rsid": "rs11903757",
"chr_name": "2",
"chr_position": 192587204,
"row_nr": 0,
"effect_weight": 1,
"accession": "test",
"effect_allele": "A",
}
)
rs6061231 = ScoreVariant(
**{
"rsid": "rs6061231",
"chr_name": "20",
"chr_position": 60956917,
"row_nr": 1,
"effect_weight": 1,
"accession": "test",
"effect_allele": "A",
}
)
return (x for x in [rs11903757, rs6061231])
@pytest.fixture(scope="session")
def small_flipped_scorefile(small_scorefile):
# simulate a scorefile on the wrong strand
return (
complement_valid_alleles(small_scorefile, ["effect_allele", "other_allele"])
.drop(["effect_allele", "other_allele"])
.rename(
{"effect_allele_FLIP": "effect_allele", "other_allele_FLIP": "other_allele"}
)
.pipe(complement_valid_alleles, ["effect_allele", "other_allele"])
)
@pytest.fixture(scope="session")
def small_target():
return pl.DataFrame(
{
"#CHROM": [1, 2, 3],
"POS": [1, 2, 3],
"REF": ["A", "T", "T"],
"ALT": ["C", "A", "G"],
"ID": ["1:1:A:C", "2:2:T:A", "3:3:T:G"],
"is_multiallelic": [False, False, False],
}
)
@pytest.fixture(scope="session")
def small_scorefile():
df = pl.DataFrame(
{
"accession": ["test", "test", "test"],
"row_nr": [1, 2, 3],
"chr_name": [1, 2, 3],
"chr_position": [1, 2, 3],
"effect_allele": ["A", "A", "G"],
"other_allele": ["C", "T", "T"],
"effect_weight": [1, 2, 3],
"effect_type": ["additive", "additive", "additive"],
}
)
return complement_valid_alleles(df, ["effect_allele", "other_allele"])
@pytest.fixture(scope="session")
def small_scorefile_no_oa(small_scorefile):
return small_scorefile.with_column(pl.lit(None).alias("other_allele"))
def _get_timeout(url):
try:
return req.get(url, timeout=5)
except (req.exceptions.ConnectionError, req.Timeout):
return []