Debian Codegraph @

Python/tests/test_pgenlib.py

raw
1 #!/usr/bin/env python3
2 import pgenlib
3 import numpy as np
4 import csv
5 import os
6 import random
7 import string
8
9 def generate_unphased_biallelic_test_genotypes(nsample, nvariant):
10 geno_buf = np.zeros([nvariant, nsample], dtype=np.int8)
11 for vidx in range(nvariant):
12 approx_freq = 0.0
13 if random.randrange(2) == 0:
14 approx_freq = random.random() * 0.05
15 else:
16 approx_freq = random.random()
17 # This sampling procedure is expected to be inaccurate for larger
18 # values of approx_freq, but that doesn't matter.
19 one_attempts = int(2 * approx_freq * nvariant)
20 for i in range(one_attempts):
21 geno_buf[vidx][random.randrange(nsample)] = 1
22 two_attempts = int(approx_freq * approx_freq * nvariant)
23 for i in range(two_attempts):
24 geno_buf[vidx][random.randrange(nsample)] = 2
25
26 # Throw in some missing genotypes.
27 while True:
28 if random.randrange(2) == 0:
29 break
30 geno_buf[vidx][random.randrange(nsample)] = -9
31
32 if nvariant >= 3:
33 all_alt_vidx = random.randrange(nvariant)
34 geno_buf[all_alt_vidx].fill(2)
35 all_missing_vidx = random.randrange(nvariant - 1)
36 if all_missing_vidx >= all_alt_vidx:
37 all_missing_vidx = all_missing_vidx + 1
38 geno_buf[all_missing_vidx].fill(-9)
39
40 return geno_buf
41
42
43 def assert_alleles(want_geno, a0, a1):
44 if want_geno == -9:
45 assert (a0 == -9) and (a1 == -9)
46 elif want_geno == 0:
47 assert (a0 == 0) and (a1 == 0)
48 elif want_geno == 1:
49 assert (a0 == 0) and (a1 == 1)
50 else:
51 assert (want_geno == 2)
52 assert (a0 == 1) and (a1 == 1)
53
54
55 def check_unphased_biallelic_read_concordance(r, raw_nsample, nvariant, test_genotypes, sample_subset):
56 want_genotypes = test_genotypes
57 cur_nsample = raw_nsample
58 if sample_subset is not None:
59 cur_nsample = len(sample_subset)
60 want_genotypes = np.empty([nvariant, cur_nsample], dtype=np.int8)
61 for vidx in range(nvariant):
62 for i in range(cur_nsample):
63 want_genotypes[vidx][i] = test_genotypes[vidx][sample_subset[i]]
64 assert raw_nsample == r.get_raw_sample_ct()
65 assert nvariant == r.get_variant_ct()
66
67 got_genotypes = np.empty([cur_nsample], np.int8)
68 vidx = random.randrange(nvariant)
69 r.read(vidx, got_genotypes)
70 assert np.array_equal(got_genotypes, want_genotypes[vidx])
71 r.read(vidx, got_genotypes, allele_idx=0)
72 for i in range(cur_nsample):
73 assert ((got_genotypes[i] == -9) and (want_genotypes[vidx][i] == -9)) or (got_genotypes[i] == 2 - want_genotypes[vidx][i])
74
75 got_acodes = np.empty([2 * cur_nsample], np.int32)
76 vidx = random.randrange(nvariant)
77 r.read_alleles(vidx, got_acodes)
78 for i in range(cur_nsample):
79 assert_alleles(want_genotypes[vidx][i], got_acodes[2*i], got_acodes[2*i+1])
80
81 tmp_nvariant = 0
82 if nvariant > 258:
83 tmp_nvariant = random.randrange(258, nvariant)
84 else:
85 tmp_nvariant = random.randrange(1, nvariant + 1)
86 vidx_start = 0
87 if nvariant > tmp_nvariant:
88 vidx_start = random.randrange(nvariant - tmp_nvariant)
89 vidx_end = vidx_start + tmp_nvariant
90 vidx_list = np.asarray(sorted(random.sample(range(nvariant), k=tmp_nvariant)), np.uint32)
91 got_genotypes_2d = np.empty([tmp_nvariant, cur_nsample], np.int8)
92 got_acodes_2d = np.empty([tmp_nvariant, 2 * cur_nsample], np.int32)
93 got_genotypes_t = np.empty([cur_nsample, tmp_nvariant], np.int8)
94
95 r.read_range(vidx_start, vidx_end, got_genotypes_2d)
96 assert np.array_equal(got_genotypes_2d, want_genotypes[vidx_start:vidx_end])
97 r.read_range(vidx_start, vidx_end, got_genotypes_t, sample_maj=True)
98 assert np.array_equal(got_genotypes_2d, np.transpose(got_genotypes_t))
99
100 r.read_list(vidx_list, got_genotypes_2d)
101 for i in range(tmp_nvariant):
102 assert np.array_equal(got_genotypes_2d[i], want_genotypes[vidx_list[i]])
103 r.read_list(vidx_list, got_genotypes_t, sample_maj=True)
104 assert np.array_equal(got_genotypes_2d, np.transpose(got_genotypes_t))
105
106 # fewer variants since we're not testing transpose, and this is relatively
107 # slow
108 if nvariant > 10:
109 tmp_nvariant = random.randrange(5, 10)
110 else:
111 tmp_nvariant = random.randrange(1, nvariant + 1)
112 vidx_start = 0
113 if nvariant > tmp_nvariant:
114 vidx_start = random.randrange(nvariant - tmp_nvariant)
115 vidx_end = vidx_start + tmp_nvariant
116 vidx_list = np.asarray(sorted(random.sample(range(nvariant), k=tmp_nvariant)), np.uint32)
117 got_acodes_2d = np.empty([tmp_nvariant, 2 * cur_nsample], np.int32)
118 r.read_alleles_range(vidx_start, vidx_end, got_acodes_2d)
119 for i in range(tmp_nvariant):
120 for j in range(cur_nsample):
121 assert_alleles(want_genotypes[vidx_start+i][j], got_acodes_2d[i][2*j], got_acodes_2d[i][2*j+1])
122 r.read_alleles_list(vidx_list, got_acodes_2d)
123 for i in range(tmp_nvariant):
124 for j in range(cur_nsample):
125 assert_alleles(want_genotypes[vidx_list[i]][j], got_acodes_2d[i][2*j], got_acodes_2d[i][2*j+1])
126
127 got_counts = np.empty([4], np.uint32)
128 want_counts = np.empty([4], np.uint32)
129 for i in range(tmp_nvariant):
130 vidx = vidx_list[i]
131 r.count(vidx, got_counts)
132 want_counts.fill(0)
133 for j in range(cur_nsample):
134 want_geno = want_genotypes[vidx][j]
135 if want_geno == -9:
136 want_counts[3] += 1
137 else:
138 want_counts[want_geno] += 1
139 assert np.array_equal(got_counts, want_counts)
140 r.count(vidx, got_counts, allele_idx=0)
141 assert got_counts[0] == want_counts[2]
142 assert got_counts[1] == want_counts[1]
143 assert got_counts[2] == want_counts[0]
144 assert got_counts[3] == want_counts[3]
145
146
147 def unphased_biallelic_case(tmp_path, case_idx, nsample_min, nsample_limit, nvariant_min, nvariant_limit):
148 # 1. Generate a matrix of genotypes, where ~half have alt-freq < 0.05, at
149 # least one has alt-freq = 1, and at least one is all-missing.
150 # 2. Use a mix of append_biallelic() and append_biallelic_batch() to write
151 # these genotypes to a .pgen.
152 # 3. Verify that get_raw_sample_ct(), get_variant_ct(), read(),
153 # read_alleles(), read_range(), read_list(), read_alleles_range(),
154 # read_alleles_list(), count() return expected results. Make sure
155 # sample_maj=True cases are covered.
156 # 4. Call change_sample_subset(), and repeat step 3.
157 random.seed(case_idx)
158 nsample = random.randrange(nsample_min, nsample_limit)
159 nvariant = random.randrange(nvariant_min, nvariant_limit)
160 test_genotypes = generate_unphased_biallelic_test_genotypes(nsample, nvariant)
161 test_pgen_path = bytes(tmp_path / ("unphased_biallelic_" + str(case_idx) + ".pgen"))
162 with pgenlib.PgenWriter(test_pgen_path, nsample, variant_ct=nvariant, nonref_flags=False) as w:
163 for vidx in range(nvariant // 3):
164 w.append_biallelic(test_genotypes[vidx])
165 w.append_biallelic_batch(test_genotypes[nvariant // 3 : (2 * nvariant) // 3])
166 for vidx in range((2 * nvariant) // 3, nvariant):
167 w.append_biallelic(test_genotypes[vidx])
168 with pgenlib.PgenReader(test_pgen_path) as r:
169 check_unphased_biallelic_read_concordance(r, nsample, nvariant, test_genotypes, None)
170 sample_subset = sorted(random.sample(range(nsample), k=(nsample+1) // 2))
171 r.change_sample_subset(np.asarray(sample_subset, np.uint32))
172 check_unphased_biallelic_read_concordance(r, nsample, nvariant, test_genotypes, sample_subset)
173
174
175 def test_unphased_biallelic(tmp_path):
176 ncase = 5
177 # We want some cases to have dimensions > 256, so we can catch more
178 # transpose bugs.
179 for case_idx in range(0, ncase):
180 unphased_biallelic_case(tmp_path, case_idx, 260, 340, 260, 340)
181 # Also cover smaller possibilities.
182 for case_idx in range(ncase, 2*ncase):
183 unphased_biallelic_case(tmp_path, case_idx, 1, 100, 260, 340)
184 for case_idx in range(2*ncase, 3*ncase):
185 unphased_biallelic_case(tmp_path, case_idx, 260, 340, 1, 100)
186 for case_idx in range(3*ncase, 4*ncase):
187 unphased_biallelic_case(tmp_path, case_idx, 1, 100, 1, 100)
188
189
190 def generate_phased_multiallelic_test_acodes(nsample, nvariant, allele_ct_limit):
191 allele_codes = np.zeros([nvariant, 2*nsample], dtype=np.int32)
192 phasepresent_bools = np.zeros([nvariant, nsample], dtype=np.bool_)
193 allele_idx_offsets = np.zeros([nvariant+1], dtype=np.uintp)
194 phasepresent_bools[(nvariant // 3):nvariant].fill(True)
195 for vidx in range(nvariant):
196 approx_freq = 0.0
197 if random.randrange(2) == 0:
198 approx_freq = random.random() * 0.05
199 else:
200 approx_freq = random.random()
201 attempts = int(2 * approx_freq * nvariant)
202 for i in range(attempts):
203 allele_codes[vidx][random.randrange(2*nsample)] = random.randrange(1, allele_ct_limit)
204
205 # Throw in some missing genotypes.
206 while True:
207 if random.randrange(2) == 0:
208 break
209 sample_idx = random.randrange(nsample)
210 allele_codes[vidx][2*sample_idx] = -9
211 allele_codes[vidx][2*sample_idx+1] = -9
212 # in case it's in the all-phased range
213 phasepresent_bools[vidx][sample_idx] = False
214
215 if nvariant >= 2:
216 all_missing_vidx = random.randrange(nvariant)
217 allele_codes[all_missing_vidx].fill(-9)
218 phasepresent_bools[all_missing_vidx].fill(False)
219
220 # Now adjust for phase.
221 # First third are all-unphased, second third are all-phased, last third is
222 # random.
223 for vidx in range(nvariant // 3):
224 for i in range(nsample):
225 ac0 = allele_codes[vidx][2*i]
226 ac1 = allele_codes[vidx][2*i+1]
227 if (ac0 == ac1):
228 if ac0 != -9:
229 # Even in the all-unphased case,
230 # read_alleles_and_phasepresent() will mark homozygous
231 # genotypes as phased.
232 phasepresent_bools[vidx][i] = True
233 elif ac0 > ac1:
234 allele_codes[vidx][2*i], allele_codes[vidx][2*i+1] = ac1, ac0
235 for vidx in range((2 * nvariant) // 3, nvariant):
236 attempts = nsample // 2
237 for i in range(attempts):
238 sample_idx = random.randrange(nsample)
239 ac0 = allele_codes[vidx][2*sample_idx]
240 ac1 = allele_codes[vidx][2*sample_idx+1]
241 if ac0 != ac1:
242 phasepresent_bools[vidx][sample_idx] = False
243 if ac0 > ac1:
244 allele_codes[vidx][2*sample_idx] = ac1
245 allele_codes[vidx][2*sample_idx+1] = ac0
246 offset = 0
247 for vidx in range(nvariant):
248 max_allele_code_p1 = 1 + np.amax(allele_codes[vidx], initial=1)
249 if max_allele_code_p1 < allele_ct_limit:
250 if random.randrange(10) == 0:
251 max_allele_code_p1 += 1
252 offset += max_allele_code_p1
253 allele_idx_offsets[vidx+1] = offset
254
255 return allele_codes, phasepresent_bools, allele_idx_offsets
256
257
258 def make_allele_cts(allele_idx_offsets):
259 nvariant = len(allele_idx_offsets) - 1
260 allele_cts = np.empty([nvariant], np.uint32)
261 for i in range(nvariant):
262 allele_cts[i] = allele_idx_offsets[i+1] - allele_idx_offsets[i]
263 return allele_cts
264
265
266 def assert_geno(a0, a1, got_geno, aidx):
267 if got_geno == -9:
268 assert (a0 == -9) and (a1 == -9)
269 elif got_geno == 0:
270 assert (a0 != aidx) and (a1 != aidx)
271 elif got_geno == 1:
272 assert (a0 == aidx) != (a1 == aidx)
273 else:
274 assert (got_geno == 2)
275 assert (a0 == aidx) and (a1 == aidx)
276
277
278 def check_phased_multiallelic_read_concordance(r, raw_nsample, nvariant, test_acodes, test_phasepresent_bools, allele_idx_offsets, sample_subset):
279 want_acodes = test_acodes
280 want_phasepresent = test_phasepresent_bools
281 cur_nsample = raw_nsample
282 if sample_subset is not None:
283 cur_nsample = len(sample_subset)
284 want_acodes = np.empty([nvariant, 2*cur_nsample], dtype=np.int32)
285 want_phasepresent = np.empty([nvariant, cur_nsample], dtype=np.bool_)
286 for vidx in range(nvariant):
287 for i in range(cur_nsample):
288 src_i = sample_subset[i]
289 want_acodes[vidx][2*i] = test_acodes[vidx][2*src_i]
290 want_acodes[vidx][2*i+1] = test_acodes[vidx][2*src_i+1]
291 want_phasepresent[vidx][i] = test_phasepresent_bools[vidx][src_i]
292 assert raw_nsample == r.get_raw_sample_ct()
293 assert nvariant == r.get_variant_ct()
294 if r.hardcall_phase_present() == False:
295 # assert that there are no heterozygous genotypes with phasepresent
296 for vidx in range(nvariant):
297 for i in range(cur_nsample):
298 if want_acodes[vidx][2*i] != want_acodes[vidx][2*i+1]:
299 assert want_phasepresent[vidx][i] == False
300
301 got_genotypes = np.empty([cur_nsample], np.int8)
302 got_acodes = np.empty([2 * cur_nsample], np.int32)
303 got_phasepresent = np.empty([cur_nsample], np.bool_)
304 vidx = random.randrange(nvariant)
305 aidx = random.randrange(allele_idx_offsets[vidx+1] - allele_idx_offsets[vidx])
306 r.read(vidx, got_genotypes, aidx)
307 for i in range(cur_nsample):
308 assert_geno(want_acodes[vidx][2*i], want_acodes[vidx][2*i+1], got_genotypes[i], aidx)
309 r.read_alleles_and_phasepresent(vidx, got_acodes, got_phasepresent)
310 assert np.array_equal(got_acodes, want_acodes[vidx])
311 assert np.array_equal(got_phasepresent, want_phasepresent[vidx])
312 r.read_alleles(vidx, got_acodes)
313 assert np.array_equal(got_acodes, want_acodes[vidx])
314
315 tmp_nvariant = random.randrange(1, nvariant + 1)
316 vidx_start = 0
317 if nvariant > tmp_nvariant:
318 vidx_start = random.randrange(nvariant - tmp_nvariant)
319 vidx_end = vidx_start + tmp_nvariant
320 vidx_list = np.asarray(sorted(random.sample(range(nvariant), k=tmp_nvariant)), np.uint32)
321 got_acodes_2d = np.empty([tmp_nvariant, 2 * cur_nsample], np.int32)
322 got_phasepresent_2d = np.empty([tmp_nvariant, cur_nsample], np.bool_)
323
324 r.read_alleles_range(vidx_start, vidx_end, got_acodes_2d)
325 assert np.array_equal(got_acodes_2d, want_acodes[vidx_start:vidx_end])
326 r.read_alleles_and_phasepresent_range(vidx_start, vidx_end, got_acodes_2d, got_phasepresent_2d)
327 assert np.array_equal(got_acodes_2d, want_acodes[vidx_start:vidx_end])
328 assert np.array_equal(got_phasepresent_2d, want_phasepresent[vidx_start:vidx_end])
329 r.read_alleles_and_phasepresent_list(vidx_list, got_acodes_2d, got_phasepresent_2d)
330 for i in range(tmp_nvariant):
331 assert np.array_equal(got_acodes_2d[i], want_acodes[vidx_list[i]])
332 assert np.array_equal(got_phasepresent_2d[i], want_phasepresent[vidx_list[i]])
333 r.read_alleles_list(vidx_list, got_acodes_2d)
334 for i in range(tmp_nvariant):
335 assert np.array_equal(got_acodes_2d[i], want_acodes[vidx_list[i]])
336
337 vidx = random.randrange(nvariant)
338 allele_ct = allele_idx_offsets[vidx + 1] - allele_idx_offsets[vidx]
339 type_ct = int((allele_ct * (allele_ct + 1)) // 2)
340 want_counts = np.zeros([type_ct + 1], dtype=np.uint32)
341 for i in range(cur_nsample):
342 a0 = want_acodes[vidx][2*i]
343 a1 = want_acodes[vidx][2*i+1]
344 if a0 == -9:
345 want_counts[type_ct] += 1
346 elif a0 <= a1:
347 want_counts[a0 + ((a1 * (a1 + 1)) // 2)] += 1
348 else:
349 want_counts[a1 + ((a0 * (a0 + 1)) // 2)] += 1
350 got_counts = np.empty([type_ct + 1], np.uint32)
351 r.count(vidx, got_counts, allele_idx=None)
352 assert np.array_equal(got_counts, want_counts)
353
354
355 def phased_multiallelic_case(tmp_path, case_idx, nsample_min, nsample_limit, nvariant_min, nvariant_limit, allele_ct_max):
356 # 1. Generate allele_codes and phasepresent_bools, with some all-unphased
357 # and some all-phased variants.
358 # 2. Use a mix of append_alleles(), append_partially_phased(),
359 # append_alleles_batch(), and append_partially_phased_batch() to write
360 # these genotypes to a .pgen.
361 # 3. Write companion .pvar.
362 # 4. Verify that
363 # get_raw_sample_ct(),
364 # get_variant_ct()
365 # hardcall_phase_present()
366 # read()
367 # read_alleles[_and_phasepresent]()
368 # read_alleles[_and_phasepresent]_range()
369 # read_alleles[_and_phasepresent]_list()
370 # count()
371 # return expected results.
372 # 5. Call change_sample_subset(), and repeat step 4.
373 random.seed(case_idx)
374 nsample = random.randrange(nsample_min, nsample_limit)
375 nvariant = random.randrange(nvariant_min, nvariant_limit)
376 allele_ct_limit = random.randrange(2, allele_ct_max + 1)
377 test_acodes, test_phasepresent_bools, test_allele_idx_offsets = generate_phased_multiallelic_test_acodes(nsample, nvariant, allele_ct_limit)
378 test_pgen_path = bytes(tmp_path / ("phased_multiallelic_" + str(case_idx) + ".pgen"))
379 with pgenlib.PgenWriter(test_pgen_path, nsample, variant_ct=nvariant, nonref_flags=False, allele_ct_limit=allele_ct_limit, hardcall_phase_present=True) as w:
380 for vidx in range(nvariant // 6):
381 w.append_alleles(test_acodes[vidx], allele_ct = test_allele_idx_offsets[vidx+1] - test_allele_idx_offsets[vidx])
382 w.append_alleles_batch(test_acodes[nvariant // 6 : nvariant // 3], allele_cts = make_allele_cts(test_allele_idx_offsets[nvariant // 6 : (nvariant // 3) + 1]))
383 for vidx in range(nvariant // 3, nvariant // 2):
384 w.append_alleles(test_acodes[vidx], all_phased=True, allele_ct = test_allele_idx_offsets[vidx+1] - test_allele_idx_offsets[vidx])
385 w.append_alleles_batch(test_acodes[nvariant // 2 : (2 * nvariant) // 3], all_phased=True, allele_cts = make_allele_cts(test_allele_idx_offsets[nvariant // 2 : ((2 * nvariant) // 3) + 1]))
386 for vidx in range((2 * nvariant) // 3, (5 * nvariant) // 6):
387 w.append_partially_phased(test_acodes[vidx], test_phasepresent_bools[vidx], allele_ct = test_allele_idx_offsets[vidx+1] - test_allele_idx_offsets[vidx])
388 w.append_partially_phased_batch(test_acodes[(5 * nvariant) // 6 : nvariant], test_phasepresent_bools[(5 * nvariant) // 6 : nvariant], allele_cts = make_allele_cts(test_allele_idx_offsets[(5 * nvariant) // 6 : nvariant + 1]))
389 test_pvar_path = bytes(tmp_path / ("phased_multiallelic_" + str(case_idx) + ".pvar"))
390 with open(test_pvar_path, 'w', newline='') as pvarfile:
391 w = csv.writer(pvarfile, delimiter='\t')
392 w.writerow(['#CHROM', 'POS', 'ID', 'REF', 'ALT'])
393 for vidx in range(nvariant):
394 altstr = ",".join(['A'+str(aidx) for aidx in range(1, test_allele_idx_offsets[vidx+1] - test_allele_idx_offsets[vidx])])
395 w.writerow(['chr1', str(vidx + 1), 'var' + str(vidx + 1), 'A0', altstr])
396 with pgenlib.PvarReader(test_pvar_path) as pvar:
397 with pgenlib.PgenReader(test_pgen_path, pvar=pvar) as r:
398 check_phased_multiallelic_read_concordance(r, nsample, nvariant, test_acodes, test_phasepresent_bools, test_allele_idx_offsets, None)
399 sample_subset = sorted(random.sample(range(nsample), k=(nsample+1) // 2))
400 r.change_sample_subset(np.asarray(sample_subset, np.uint32))
401 check_phased_multiallelic_read_concordance(r, nsample, nvariant, test_acodes, test_phasepresent_bools, test_allele_idx_offsets, sample_subset)
402
403
404 def test_phased_multiallelic(tmp_path):
405 ncase = 500
406 for case_idx in range(0, ncase):
407 phased_multiallelic_case(tmp_path, case_idx, 1, 100, 1, 100, 2)
408 for case_idx in range(ncase, 2*ncase):
409 phased_multiallelic_case(tmp_path, case_idx, 1, 100, 1, 100, 255)
410
411 def check_bim_concordance(bimr, chrom_list, pos_list, id_list, ref_list, alt_list):
412 nvariant = bimr.get_variant_ct()
413 assert nvariant == len(chrom_list)
414
415 assert bimr.get_max_allele_ct() == 2
416
417 ncheck = 5
418 for _ in range(0, ncheck):
419 vidx = random.randrange(nvariant)
420 assert chrom_list[vidx].encode("utf-8") == bimr.get_variant_chrom(vidx)
421 assert pos_list[vidx] == bimr.get_variant_pos(vidx)
422 assert id_list[vidx].encode("utf-8") == bimr.get_variant_id(vidx)
423 assert ref_list[vidx].encode("utf-8") == bimr.get_allele_code(vidx, 0)
424 assert alt_list[vidx].encode("utf-8") == bimr.get_allele_code(vidx, 1)
425
426 def bim_case(tmp_path, case_idx, nvariant_min, nvariant_limit):
427 # A .bim file is supposed to be grandfathered in as a valid .pvar.
428 # This was broken in pvar_ffi_support before Jan 2025.
429 random.seed(case_idx)
430 nvariant = random.randrange(nvariant_min, nvariant_limit)
431
432 chrom_list = [random.choice(['chr1', 'chr21', 'PAR1', 'chrX', 'HLA-DRB1*13:01:01'])
433 for _ in range(nvariant)]
434 pos_list = [random.randrange(-2147483646, 2147483647)
435 for _ in range(nvariant)]
436 id_list = [''.join(random.choices(string.ascii_letters + string.digits,
437 k=random.randrange(1, 20)))
438 for _ in range(nvariant)]
439 ref_list = [random.choice(['A', 'C', 'ACT']) for _ in range(nvariant)]
440 alt_list = [random.choice(['G', 'T', '<DEL>']) for _ in range(nvariant)]
441
442 test_bim_path = bytes(tmp_path / ("test_" + str(case_idx) + ".bim"))
443 with open(test_bim_path, 'w', newline='') as bimfile:
444 w = csv.writer(bimfile, delimiter='\t')
445 for vidx in range(nvariant):
446 # CHROM ID CM POS ALT REF
447 w.writerow([chrom_list[vidx], id_list[vidx], str(pos_list[vidx]),
448 alt_list[vidx], ref_list[vidx]])
449 with pgenlib.PvarReader(test_bim_path) as bimr:
450 check_bim_concordance(bimr, chrom_list, pos_list, id_list, ref_list, alt_list)
451
452 def test_bim(tmp_path):
453 ncase = 5
454 for case_idx in range(0, ncase):
455 bim_case(tmp_path, case_idx, 1, 100)