Debian Codegraph @

Python/python_api.txt

raw
1 Pgenlib Python API specification, v0.92
2 ("import numpy as np" assumed)
3
4 class PvarReader:
5 * PvarReader(bytes filename, omit_chrom = False, omit_pos = False)
6 Constructor, opens the .pvar or .bim file. To save memory, you can use
7 omit_chrom and/or omit_pos to skip loading of those columns.
8
9 * get_variant_ct()
10 Returns the number of variants in the .pvar file.
11
12 * get_variant_chrom(uint32_t variant_idx)
13 Returns a variant's chromosome code, given 0-based index.
14
15 * get_variant_pos(uint32_t variant_idx)
16 Returns a variant's POS (base-pair coordinate on a chromosome), given 0-based
17 index.
18
19 * get_variant_id(uint32_t variant_idx)
20 Returns a variant's ID, given 0-based index.
21
22 * get_allele_ct(uint32_t variant_idx)
23 Returns max(2, <number of alleles>) for a variant, given 0-based index.
24
25 * get_allele_code(uint32_t variant_idx, uint32_t allele_idx)
26 Returns the specified allele code for a variant, given 0-based variant index
27 and 0-based allele index (0=REF, 1=first ALT, etc.). Note that, for .bim
28 files, the *last* column is treated as REF, and the second-to-last is treated
29 as ALT.
30
31 * get_allele_idx_offsets()
32 Returns a np.uintp array of length (variant_ct+1), where
33 allele_idx_offsets[i+1] - allele_idx_offsets[i] is get_allele_ct(i).
34
35 * get_max_allele_ct()
36 Returns max(2, <highest number of alleles for any variant in the file>).
37
38 * close()
39 Closes the file, no further read operations can be performed.
40 - Not strictly necessary when e.g. a with-statement is used, but still a good
41 idea in longer scripts since otherwise object cleanup/file close may be
42 delayed.
43
44
45 class PgenReader:
46 * PgenReader(bytes filename, raw_sample_ct = None, variant_ct = None,
47 sample_subset = None, allele_idx_offsets = None, pvar = None)
48 Constructor, opens the .pgen or .bed file. Requires a filename (exception is
49 thrown if file doesn't exist or it's invalid).
50 - raw_sample_ct is required for a .bed file (otherwise an exception will be
51 thrown), and optional for a .pgen file. If it's provided for a .pgen file,
52 an exception is thrown if sample_ct does not match the value in the .pgen.
53 - variant_ct is always optional. An exception is thrown if it does not match
54 the value implied by the .bed file's size, or the explicitly stored value
55 in the .pgen.
56 - sample_subset is an optional numpy uint32 array of (0-based) indexes in
57 increasing order, telling the reader to only consider the specified samples
58 when loading or counting genotypes. For example, if your .fam file looks
59 something like
60 0 40184_187545456 0 0 1 -9
61 0 40195_187545457 0 0 1 -9
62 0 40206_187545458 0 0 1 -9
63 0 40217_187545459 0 0 2 -9
64 0 40228_187545460 0 0 1 -9
65 0 40239_187545461 0 0 1 -9
66 0 40250_187520807 0 0 1 -9
67 0 40261_187520806 0 0 1 -9
68 0 40272_187520805 0 0 2 -9
69 0 40283_187520804 0 0 1 -9
70 then a [1, 4, 5, 6] array specifies samples 40195_187545457,
71 40228_187545460, 40239_187545461, and 40250_187520807.
72 None indicates that all samples should be included. Otherwise, an
73 exception is thrown if any value is not an integer in 0..(raw_sample_ct -
74 1), or the values are not strictly increasing, or the array is empty.
75 (Empty array might be permitted in the future.)
76 - allele_idx_offsets is required when multiallelic variants are present.
77 When provided, it should be a np.uintp array of length (variant_ct+1),
78 where allele_idx_offsets[i+1] - allele_idx_offsets[i] is max(2, number of
79 alleles for 0-based variant i).
80 - If pvar is provided, variant_ct and allele_idx_offsets are scraped from it.
81 (You are not allowed to provide both pvar and
82 variant_ct/allele_idx_offsets directly.)
83
84 * get_raw_sample_ct()
85 Returns the number of samples in the .pgen file.
86
87 * get_variant_ct()
88 Returns the number of variants in the .pgen file.
89
90 * hardcall_phase_present()
91 Returns True iff phased hardcalls may be present.
92
93 * read(uint32_t variant_idx, np.ndarray[np.int{8,32,64}_t] geno_int_out,
94 uint32_t allele_idx = 1)
95 Takes a (0-based) variant index; fills geno_int_out, which must be a numpy
96 int8, int32, or int64 array with at least sample_ct entries, where sample_ct
97 (as distinguished from raw_sample_ct) is the size of the current sample
98 subset. These values are in {0, 1, 2, -9}; by default, 0/1/2 represents the
99 alternate allele count. Setting allele_idx = 0 causes reference allele
100 counts to be reported instead, etc.
101 - I tried the interface without the required outbuffer, and it could take
102 twice as long due to all the extra allocation/deallocation. The buffer can
103 be allocated with
104 buf = np.empty(sample_ct, np.int{8,32,64})
105 - An exception is thrown if variant_idx or allele_idx is invalid (or there's
106 e.g. a file I/O error, or the file isn't open)
107 - Note that pgenlib is unaware of chromosome boundaries. The caller is
108 responsible for chrX/chrY/chrM-specific corrections for now.
109
110 * read_dosages(uint32_t variant_idx,
111 np.ndarray[np.float{32,64}_t] floatarr_out,
112 uint32_t allele_idx = 1)
113 Takes a (0-based) variant index; fills floatarr_out, which must be a numpy
114 float32 or float64 array with at least sample_ct entries. Missing entries
115 are encoded as -9, everything else is a dosage in [0, 2] (more precisely,
116 they'll be a multiple of 2^{-14}).
117
118 * read_alleles(uint32_t variant_idx, np.ndarray[np.int32_t] allele_int32_out)
119 * read_alleles_and_phasepresent(uint32_t variant_idx,
120 np.ndarray[np.int32_t] allele_int32_out,
121 np.ndarray[np.uint8_t,cast=True] phasepresent_out)
122 Takes a (0-based) variant index.
123 - allele_int32_out must have space for at least (2 * sample_ct) allele
124 indexes, where elements 2n and (2n+1) correspond to sample n. Both indexes
125 are -9 if the genotype is missing. If the genotype is unphased, the lower
126 index appears first.
127 - For read_alleles_and_phasepresent(), phasepresent_out should be a numpy
128 bool_, int8, or uint8 array. Element n is set to true iff the genotype for
129 sample n has known phase. Most of these values will be true even when the
130 raw data is unphased, because homozygous genotypes always have known phase.
131 (Missing genotypes are considered to have unknown phase, of course.)
132 - If int32_t is problematically large, you probably want to work with a 2-bit
133 representation rather than int8_t. That's outside the scope of this API;
134 you may instead want to adapt the appropriate pgenlib.pyx function.
135
136 * read_range(uint32_t variant_idx_start, uint32_t variant_idx_end,
137 np.ndarray[np.int{8,32,64}_t,mode="c",ndim=2] geno_int_out,
138 uint32_t allele_idx = 1, bint sample_maj = 0)
139 read_list(np.ndarray[np.uint32_t] variant_idxs,
140 np.ndarray[np.int{8,32,64}_t,mode="c",ndim=2] geno_int_out,
141 uint32_t allele_idx = 1, bint sample_maj = 0)
142 read_alleles_range(uint32_t variant_idx_start, uint32_t variant_idx_end,
143 np.ndarray[np.int32_t,mode="c",ndim=2] allele_int32_out,
144 bint hap_maj = 0)
145 read_alleles_list(np.ndarray[np.uint32_t] variant_idxs,
146 np.ndarray[np.int32_t,mode="c",ndim=2] allele_int32_out,
147 bint hap_maj = 0)
148 read_alleles_and_phasepresent_range(uint32_t variant_idx_start,
149 uint32_t variant_idx_end,
150 np.ndarray[np.int32_t,mode="c",ndim=2] allele_int32_out,
151 np.ndarray[np.uint8_t,mode="c",cast=True,ndim=2] phasepresent_out,
152 bint hap_maj = 0)
153 read_alleles_and_phasepresent_list(np.ndarray[np.uint32_t] variant_idxs,
154 np.ndarray[np.int32_t,mode="c",ndim=2] allele_int32_out,
155 np.ndarray[np.uint8_t,mode="c",cast=True,ndim=2] phasepresent_out,
156 bint hap_maj = 0)
157 read_dosages_range(uint32_t variant_idx_start, uint32_t variant_idx_end,
158 np.ndarray[np.float{32,64}_t,mode="c",ndim=2] floatarr_out,
159 uint32_t allele_idx = 1, bint sample_maj = 0)
160 read_dosages_list(np.ndarray[np.uint32_t] variant_idxs,
161 np.ndarray[np.float{32,64}_t,mode="c",ndim=2] floatarr_out,
162 uint32_t allele_idx = 1, bint sample_maj = 0)
163 Read functions which handle multiple variants at once, saving data to 2D
164 arrays. By default, the return arrays are variant-major (# of columns is
165 based on sample_ct, # of rows is equal to the block size); set
166 sample_maj(/hap_maj) = True to make them sample(/haplotype)-major instead.
167
168 For the _range() functions, the interval is half-open (i.e. variant_idx_start
169 is included, variant_idx_end - 1 is included, variant_idx_end is not). For
170 the _list() functions, it's currently okay for the variant indexes to be
171 unsorted, or for duplicates to be present, but that may not remain true.
172
173 The sample/haplotype-major modes of read_dosages_{range,list} and
174 read_alleles_and_phasepresent_{range,list} are not implemented yet. The
175 haplotype-major modes of read_alleles_{range,list} don't yet support
176 multiallelic variants.
177
178 * count(uint32_t variant_idx, np.ndarray[np.uint32_t] genocount_uint32_out,
179 uint32_t allele_idx = 1)
180 Takes a (0-based) variant index, fills genocount_uint32_out (which must have
181 at least 4 slots). First element corresponds to the number of genotypes with
182 0 alt1 alleles, the second element corresponds to the # with one alt1 allele,
183 the third corresponds to homozygous-alt1, and the fourth corresponds to the
184 number of missing genotypes.
185 - Setting allele_idx = 0 replaces "alt1" with "ref" above.
186 - Setting allele_idx = None is functionally equivalent to allele_idx = 1 on
187 all-biallelic data, but the length of the array is no longer limited to 4
188 for multiallelic variants; instead it has 1 + v(v+1)/2 elements where v is
189 the number of alleles, in 0/0, 0/1, 1/1, 0/2, 1/2, 2/2, 0/3, ...,
190 (v-1)/(v-1), ./. order. (This matches VCF FORMAT:GL order.)
191 (Todo: check whether multi-variant analogues of read/read_alleles/count have
192 any value.)
193
194 * phased_samples() [not written yet]
195 If phase set data is present, or the entire dataset is unphased, returns a
196 numpy bool_ array where element n is true iff sample n has at least one phase
197 set. Throws an exception if phased variants are present, but no phase set
198 data track is present. (Note that the phase set data track is not
199 implemented yet.)
200
201 * change_sample_subset(sample_subset = None)
202 Changes the subset of samples to read.
203 - sample_subset format is the same as for the constructor. (This isn't
204 really optimized; it's assumed that you won't be calling this often. If
205 you ever need a higher-performance version of this, let me know.)
206
207 * close()
208 Closes the file, no further read operations can be performed.
209 - Not strictly necessary when e.g. a with-statement is used, but still a good
210 idea in longer scripts since otherwise object cleanup/file close may be
211 delayed.
212
213
214 class PgenWriter:
215 * PgenWriter(bytes filename, uint32_t sample_ct, variant_ct = None,
216 nonref_flags = True, allele_ct_limit = 2,
217 hardcall_phase_present = False, dosage_present = False,
218 dosage_phase_present = False, variant_ct_limit = None)
219 Constructor, creates a new .pgen file and writes a mostly-empty header (which
220 gets filled at the end).
221 - sample_ct must be positive, and less than ~2^31.
222 - Either variant_ct or variant_ct_limit must be provided; the value
223 must be positive, and less than ~2^31.
224 - nonref_flags should be True when the data is from an ordinary PLINK 1.x
225 .bed file (where the A2 allele is major rather than consistently
226 reference), False when the A2 allele *is* consistently reference, or a
227 numpy bool_ array of length variant_ct when this is mixed.
228 (Strictly speaking, None is also permitted--that delegates tracking of
229 nonref information to the .pvar file--but it's discouraged, since
230 .pgen+.bim+.fam is a useful data representation with direct plink2
231 support.)
232
233 * append_biallelic(genobytes)
234 Takes a numpy int8 array with sample_ct {0, 1, 2, -9} elements, and appends
235 it to the .pgen. Ok to use 3 in place of -9.
236
237 * append_alleles(allele_codes, all_phased = False, allele_ct = None)
238 append_partially_phased(allele_codes, np.ndarray[np.uint8_t,cast=True] phasepresent, allele_ct = None)
239 Takes a numpy int32 array with (2 * sample_ct) allele codes (0 = ref,
240 1 = alt1, -9 = missing), and appends it to the .pgen. -9s must occur in
241 pairs.
242 - With append_alleles(), all genotypes are treated as unphased by default.
243 Set all_phased to True to treat all genotypes as phased instead.
244 - With append_partially_phased(), phasepresent should be a numpy bool_ (or
245 equivalent int8_t/uint8_t; in that case, all values must be 0s and 1s)
246 array of length sample_ct. Entries for non-heterozygous calls are ignored.
247 - It's fine for an unphased het call to be stored in 1/0 instead of 0/1
248 order.
249 - The number of ALT alleles is inferred to be max(1, <max observed code>).
250 When this is inaccurate (i.e. the last ALT allele in the .pvar file is
251 unobserved), you must specify allele_ct for the variant to be encoded
252 properly.
253
254 * append_dosages(floatarr)
255 Takes a numpy float32 or float64 array of dosages, and appends it to the
256 .pgen.
257 - The writer must have been initialized with dosage_present = True.
258 - Regular dosages are expected to be in [0, 2] (you'll want to multiply
259 haploid dosages by 2).
260 - Except for small (2^{-16}) tolerances around 0 and 2, any out-of-range
261 value (including -9) is interpreted as missing.
262
263 * append_biallelic_batch(np.ndarray[np.int8_t,mode="c",ndim=2] genobytes_batch)
264 append_alleles_batch(np.ndarray[np.int32_t,mode="c",ndim=2] allele_codes_batch,
265 all_phased = False, allele_cts = None)
266 append_partially_phased_batch(np.ndarray[np.int32_t,mode="c",ndim=2] allele_codes_batch,
267 np.ndarray[np.{int8,uint8}_t,mode="c",ndim=2] phasepresent_batch,
268 allele_cts = None)
269 append_dosages_batch(np.ndarray[np.float{32,64}_t] floatarr_batch)
270 Multi-variant forms of append_biallelic(), append_alleles(),
271 append_partially_phased(), and append_dosages(). Input matrices must be
272 variant-major. When allele_cts is provided, it must be an np.uint32 array.
273
274 * close()
275 Backfills the header and closes the file; no further append operations can be
276 performed. Throws an exception if the number of written variants is not
277 equal to the initially-declared variant_ct.