Debian Codegraph @

pgenlibr/R/RcppExports.R

raw
1 # Generated by using Rcpp::compileAttributes() -> do not edit by hand
2 # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3
4 #' Opens a .pgen or PLINK 1 .bed file.
5 #'
6 #' @param filename .pgen/.bed file path.
7 #' @param pvar Object (see NewPvar()) corresponding to the .pgen's companion
8 #' .pvar; technically optional, but necessary for some functionality. In
9 #' particular, at multiallelic variants, all ALT alleles may be collapsed
10 #' together when .pvar information is not available.
11 #' @param raw_sample_ct Number of samples in file; required if it's a PLINK 1
12 #' .bed file, otherwise optional.
13 #' @param sample_subset List of 1-based positions of samples to load;
14 #' optional, all samples are loaded if this is not specified.
15 #' @return A pgen object, which can be queried for genotype/dosage data.
16 #' @export
17 NewPgen <- function(filename, pvar = NULL, raw_sample_ct = NULL, sample_subset = NULL) {
18 .Call(`_pgenlibr_NewPgen`, filename, pvar, raw_sample_ct, sample_subset)
19 }
20
21 #' Returns the number of samples in the file.
22 #'
23 #' @param pgen Object returned by NewPgen().
24 #' @return Number of samples.
25 #' @export
26 GetRawSampleCt <- function(pgen) {
27 .Call(`_pgenlibr_GetRawSampleCt`, pgen)
28 }
29
30 #' Returns the number of variants in the file.
31 #'
32 #' @param pvar_or_pgen Object returned by NewPvar() or NewPgen().
33 #' @return Number of variants.
34 #' @export
35 GetVariantCt <- function(pvar_or_pgen) {
36 .Call(`_pgenlibr_GetVariantCt`, pvar_or_pgen)
37 }
38
39 #' Returns the effective number of alleles for a variant. Note that if no
40 #' pvar was provided to the NewPgen() call, this function may return 2 even at
41 #' multiallelic variants, since the .pgen may not store allele-count
42 #' information.
43 #'
44 #' @param pvar_or_pgen Object returned by NewPvar() or NewPgen().
45 #' @param variant_num Variant index (1-based).
46 #' @return max(2, <number of alleles the variant_numth variant is known to
47 #' have>). Note that if no
48 #' @export
49 GetAlleleCt <- function(pvar_or_pgen, variant_num) {
50 .Call(`_pgenlibr_GetAlleleCt`, pvar_or_pgen, variant_num)
51 }
52
53 #' Returns the maximum GetAlleleCt() value across all variants in the file.
54 #'
55 #' @param pvar_or_pgen Object returned by NewPvar() or NewPgen().
56 #' @return Maximum GetAlleleCt() value across all variants.
57 #' @export
58 GetMaxAlleleCt <- function(pvar_or_pgen) {
59 .Call(`_pgenlibr_GetMaxAlleleCt`, pvar_or_pgen)
60 }
61
62 #' Returns whether explicitly phased hardcalls are present.
63 #'
64 #' @param pgen Object returned by NewPgen().
65 #' @return TRUE if the file contains at least one phased heterozygous
66 #' hardcall, FALSE otherwise.
67 #' @export
68 HardcallPhasePresent <- function(pgen) {
69 .Call(`_pgenlibr_HardcallPhasePresent`, pgen)
70 }
71
72 #' Returns a numeric buffer that Read() or ReadHardcalls() can load to.
73 #'
74 #' @param pgen Object returned by NewPgen().
75 #' @return Numeric vector with appropriate length for Read() and
76 #' ReadHardcalls().
77 #' @export
78 Buf <- function(pgen) {
79 .Call(`_pgenlibr_Buf`, pgen)
80 }
81
82 #' Returns an empty two-row numeric matrix that ReadAlleles() can load to.
83 #'
84 #' @param pgen Object returned by NewPgen().
85 #' @return Numeric matrix with two rows, and appropriate number of columns for
86 #' ReadAlleles().
87 #' @export
88 AlleleCodeBuf <- function(pgen) {
89 .Call(`_pgenlibr_AlleleCodeBuf`, pgen)
90 }
91
92 #' Returns an integer buffer that ReadHardcalls() can load to.
93 #'
94 #' @param pgen Object returned by NewPgen().
95 #' @return Integer vector with appropriate length for ReadHardcalls().
96 #' @export
97 IntBuf <- function(pgen) {
98 .Call(`_pgenlibr_IntBuf`, pgen)
99 }
100
101 #' Returns an empty two-row integer matrix that ReadAlleles() can load to.
102 #'
103 #' @param pgen Object returned by NewPgen().
104 #' @return Integer matrix with two rows, and appropriate number of columns for
105 #' ReadAlleles().
106 #' @export
107 IntAlleleCodeBuf <- function(pgen) {
108 .Call(`_pgenlibr_IntAlleleCodeBuf`, pgen)
109 }
110
111 #' Returns a bool buffer that ReadAlleles() can load phasing information to.
112 #'
113 #' @param pgen Object returned by NewPgen().
114 #' @return Logical vector with appropriate length for ReadAlleles().
115 #' @export
116 BoolBuf <- function(pgen) {
117 .Call(`_pgenlibr_BoolBuf`, pgen)
118 }
119
120 #' Loads the variant_numth variant, and then fills buf with \{0, 1, 2, NA\}
121 #' values indicating the number of copies of the first ALT (or user-specified)
122 #' allele each sample has.
123 #'
124 #' This function treats the data as diploid; you can divide by 2, and then
125 #' treat 0.5 as NA, if it's actually haploid.
126 #'
127 #' @param pgen Object returned by NewPgen().
128 #' @param buf Buffer returned by Buf() or IntBuf().
129 #' @param variant_num Variant index (1-based).
130 #' @param allele_num Allele index; 1 corresponds to REF, 2 to the first ALT
131 #' allele, 3 to the second ALT allele if it exists, etc. Optional, defaults
132 #' to 2.
133 #' @return No return value, called for buf-filling side-effect.
134 #' @export
135 ReadHardcalls <- function(pgen, buf, variant_num, allele_num = 2L) {
136 invisible(.Call(`_pgenlibr_ReadHardcalls`, pgen, buf, variant_num, allele_num))
137 }
138
139 #' Returns whether hardcalls for the variant_numth variant and given allele
140 #' are represented in a sparse manner that is supported by
141 #' ReadSparseHardcalls().
142 #'
143 #' @param pgen Object returned by NewPgen().
144 #' @param variant_num Variant index (1-based).
145 #' @param allele_num Allele index; 1 corresponds to REF, 2 to the first ALT
146 #' allele, 3 to the second ALT allele if it exists, etc. Optional, defaults
147 #' to 2.
148 #' @return True iff the (variant, allele) pair has a sparse representation
149 #' that can be returned by ReadSparseHardcalls().
150 #' @export
151 HasSparseHardcalls <- function(pgen, variant_num, allele_num = 2L) {
152 .Call(`_pgenlibr_HasSparseHardcalls`, pgen, variant_num, allele_num)
153 }
154
155 #' If HasSparseHardcalls() is true, returns a sparse representation for the
156 #' (variant, allele) pair. If HasSparseHardcalls() is false, the function
157 #' fails.
158 #'
159 #' @param pgen Object returned by NewPgen().
160 #' @param variant_num Variant index (1-based).
161 #' @param allele_num Allele index; 1 corresponds to REF, 2 to the first ALT
162 #' allele, 3 to the second ALT allele if it exists, etc. Optional, defaults
163 #' to 2.
164 #' @param return_ints Whether to make the "counts" component of the return
165 #' value an IntegerVector instead of a NumericVector; defaults to false.
166 #' @return Either an empty list, in which case buf is filled in the same
167 #' mannerObject where "sample_ids" is an increasing sequence of positive
168 #' integers listing which samples have the allele, and "counts" is a vector
169 #' listing the allele counts for those samples.
170 #' @export
171 ReadSparseHardcalls <- function(pgen, variant_num, allele_num = 2L, return_ints = FALSE) {
172 .Call(`_pgenlibr_ReadSparseHardcalls`, pgen, variant_num, allele_num, return_ints)
173 }
174
175 #' Loads the variant_numth variant, and then fills buf with numeric dosages
176 #' in [0, 2] indicating the dosages of the first ALT (or user-specified)
177 #' allele for each sample, with missing values represented by NA.
178 #'
179 #' This function treats the data as diploid; divide by 2 to obtain haploid
180 #' dosages.
181 #'
182 #' @param pgen Object returned by NewPgen().
183 #' @param buf Buffer returned by Buf().
184 #' @param variant_num Variant index (1-based).
185 #' @param allele_num Allele index; 1 corresponds to REF, 2 to the first ALT
186 #' allele, 3 to the second ALT allele if it exists, etc. Optional, defaults
187 #' to 2.
188 #' @return No return value, called for buf-filling side-effect.
189 #' @export
190 Read <- function(pgen, buf, variant_num, allele_num = 2L) {
191 invisible(.Call(`_pgenlibr_Read`, pgen, buf, variant_num, allele_num))
192 }
193
194 #' Loads the variant_numth variant, and then fills acbuf with integer allele
195 #' codes, where each column of the buffer corresponds to a sample. An allele
196 #' code of 0 corresponds to the REF allele, 1 to the first ALT, 2 to the
197 #' second ALT, etc. Missing hardcalls are represented by a pair of NA codes.
198 #'
199 #' This function treats the data as diploid. If it's really haploid, you may
200 #' want to compare the two rows, and then treat samples where the allele codes
201 #' differ as missing values.
202 #'
203 #' @param pgen Object returned by NewPgen().
204 #' @param acbuf Buffer returned by AlleleCodeBuf() or IntAlleleCodeBuf().
205 #' @param variant_num Variant index (1-based).
206 #' @param phasepresent_buf Buffer returned by BoolBuf(). Optional; if
207 #' provided, elements are set to true when the sample has known phase. Most
208 #' of these values will be TRUE even when the raw data is unphased, because
209 #' homozygous genotypes always have known phase. (Missing genotypes are
210 #' considered to have unknown phase.)
211 #' @return No return value, called for acbuf-filling side-effect.
212 #' @export
213 ReadAlleles <- function(pgen, acbuf, variant_num, phasepresent_buf = NULL) {
214 invisible(.Call(`_pgenlibr_ReadAlleles`, pgen, acbuf, variant_num, phasepresent_buf))
215 }
216
217 #' Load hardcalls for multiple variants as an integer matrix.
218 #'
219 #' This function treats the data as diploid; you can divide by 2, and then
220 #' treat 0.5 as NA, if it's actually haploid.
221 #'
222 #' @param pgen Object returned by NewPgen().
223 #' @param variant_subset Integer vector containing 1-based indexes of variants
224 #' to load.
225 #' @return Integer matrix, where rows correspond to samples, columns
226 #' correspond to variant_subset, and values are in \{0, 1, 2, NA\} indicating
227 #' the number of hardcall ALT allele copies. For multiallelic variants, all
228 #' ALT alleles are combined.
229 #' @export
230 ReadIntList <- function(pgen, variant_subset) {
231 .Call(`_pgenlibr_ReadIntList`, pgen, variant_subset)
232 }
233
234 #' Load dosages for multiple variants as a numeric matrix.
235 #'
236 #' This function treats the data as diploid; divide by 2 to obtain haploid
237 #' dosages.
238 #'
239 #' @param pgen Object returned by NewPgen().
240 #' @param variant_subset Integer vector containing 1-based indexes of variants
241 #' to load.
242 #' @param meanimpute Optional; if true, missing values are mean-imputed
243 #' instead of being represented by NA.
244 #' @return Numeric matrix, where rows correspond to samples, and columns
245 #' correspond to variant_subset. Values are in [0, 2] indicating ALT
246 #' allele dosages, or NA for missing dosages. For multiallelic variants, all
247 #' ALT alelles are combined.
248 #' @export
249 ReadList <- function(pgen, variant_subset, meanimpute = FALSE) {
250 .Call(`_pgenlibr_ReadList`, pgen, variant_subset, meanimpute)
251 }
252
253 #' Compute variant scores.
254 #'
255 #' This function treats the data as diploid; divide by 2 to obtain scores
256 #' based on a haploid dosage matrix.
257 #'
258 #' @param pgen Object returned by NewPgen().
259 #' @param weights Sample weights.
260 #' @param variant_subset Integer vector containing 1-based indexes of variants
261 #' to include in the dosage matrix. Optional; by default, all variants are
262 #' included.
263 #' @return Numeric vector, containing product of sample-weight vector and the
264 #' specified subset of the dosage matrix.
265 #' @export
266 VariantScores <- function(pgen, weights, variant_subset = NULL) {
267 .Call(`_pgenlibr_VariantScores`, pgen, weights, variant_subset)
268 }
269
270 #' Closes a pgen object, releasing resources.
271 #'
272 #' @param pgen Object returned by NewPgen().
273 #' @return No return value, called for side-effect.
274 #' @export
275 ClosePgen <- function(pgen) {
276 invisible(.Call(`_pgenlibr_ClosePgen`, pgen))
277 }
278
279 #' Loads variant positions, IDs, and allele codes from a .pvar or .bim file
280 #' (which can be compressed with gzip or Zstd).
281 #'
282 #' @param filename .pvar/.bim file path.
283 #' @param omit_chrom Whether to skip CHROM column.
284 #' @param omit_pos Whether to skip POS column.
285 #' @return A pvar object, which can be queried for variant IDs and allele
286 #' codes.
287 #' @export
288 NewPvar <- function(filename, omit_chrom = FALSE, omit_pos = FALSE) {
289 .Call(`_pgenlibr_NewPvar`, filename, omit_chrom, omit_pos)
290 }
291
292 #' Retrieve chromosome ID for given variant index.
293 #'
294 #' @param pvar Object returned by NewPvar().
295 #' @param variant_num Variant index (1-based).
296 #' @return Chromosome ID for the variant_numth variant.
297 #' @export
298 GetVariantChrom <- function(pvar, variant_num) {
299 .Call(`_pgenlibr_GetVariantChrom`, pvar, variant_num)
300 }
301
302 #' Retrieve POS (base-pair coordinate on a chromosome) for given variant
303 #' index.
304 #'
305 #' @param pvar Object returned by NewPvar().
306 #' @param variant_num Variant index (1-based).
307 #' @return POS for the variant_numth variant.
308 #' @export
309 GetVariantPos <- function(pvar, variant_num) {
310 .Call(`_pgenlibr_GetVariantPos`, pvar, variant_num)
311 }
312
313 #' Convert variant index to variant ID string.
314 #'
315 #' @param pvar Object returned by NewPvar().
316 #' @param variant_num Variant index (1-based).
317 #' @return The variant_numth variant ID string.
318 #' @export
319 GetVariantId <- function(pvar, variant_num) {
320 .Call(`_pgenlibr_GetVariantId`, pvar, variant_num)
321 }
322
323 #' Convert variant ID string to variant index(es).
324 #'
325 #' @param pvar Object returned by NewPvar().
326 #' @param id Variant ID to look up.
327 #' @return A list of all (1-based) variant indices with the given variant ID.
328 #' @export
329 GetVariantsById <- function(pvar, id) {
330 .Call(`_pgenlibr_GetVariantsById`, pvar, id)
331 }
332
333 #' Look up an allele code.
334 #'
335 #' @param pvar Object returned by NewPvar().
336 #' @param variant_num Variant index (1-based).
337 #' @param allele_num Allele index (1-based).
338 #' @return The allele_numth allele code for the variant_numth variant.
339 #' allele_num=1 corresponds to the REF allele, allele_num=2 corresponds to the
340 #' first ALT allele, allele_num=3 corresponds to the second ALT allele if it
341 #' exists and errors out otherwise, etc.
342 #' @export
343 GetAlleleCode <- function(pvar, variant_num, allele_num) {
344 .Call(`_pgenlibr_GetAlleleCode`, pvar, variant_num, allele_num)
345 }
346
347 #' Closes a pvar object, releasing memory.
348 #'
349 #' @param pvar Object returned by NewPvar().
350 #' @return No return value, called for side-effect.
351 #' @export
352 ClosePvar <- function(pvar) {
353 invisible(.Call(`_pgenlibr_ClosePvar`, pvar))
354 }
355