Debian Codegraph @

include/pgenlib_ffi_support.h

raw
1 #ifndef __PGENLIB_FFI_SUPPORT_H__
2 #define __PGENLIB_FFI_SUPPORT_H__
3
4 // This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell,
5 // Christopher Chang.
6 //
7 // This library is free software: you can redistribute it and/or modify it
8 // under the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation; either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with this library. If not, see <http://www.gnu.org/licenses/>.
19
20 #include "pgenlib_misc.h"
21
22 #ifdef __cplusplus
23 namespace plink2 {
24 #endif
25
26 // Could define a slightly-more-efficient version of this function which uses a
27 // missing code of 3 instead of -9. But let's play well with existing scripts
28 // first.
29 void GenoarrToBytesMinus9(const uintptr_t* genoarr, uint32_t sample_ct, int8_t* genobytes);
30
31 void GenoarrToInt32sMinus9(const uintptr_t* genoarr, uint32_t sample_ct, int32_t* geno_int32);
32
33 void GenoarrToInt64sMinus9(const uintptr_t* genoarr, uint32_t sample_ct, int64_t* geno_int64);
34
35 // May want to use STD_ARRAY_INIT_{START,END}... though it may not be worth the
36 // additional compilation headaches here.
37 extern const double kGenoDoublePairs[32];
38
39 HEADER_INLINE void GenoarrToDoublesMinus9(const uintptr_t* genoarr, uint32_t sample_ct, double* geno_double) {
40 GenoarrLookup16x8bx2(genoarr, kGenoDoublePairs, sample_ct, geno_double);
41 }
42
43 HEADER_INLINE void GenoarrToAlleleCodes(const uint64_t* geno_to_intcode_pair_table, const uintptr_t* genoarr, uint32_t sample_ct, int32_t* allele_codes) {
44 GenoarrLookup16x8bx2(genoarr, geno_to_intcode_pair_table, sample_ct, allele_codes);
45 }
46
47 extern const uint64_t kGenoToIntcodeDPairs[32];
48
49 // For FFI, allele_codes is always int32_t. Python/R programmers should not
50 // need to worry about whether pgenlib was compiled with 1-, 2-, or 4-byte
51 // AlleleCode.
52 //
53 // phasebytes can be nullptr; if it isn't, entry is 1 iff genotype is an
54 // explicitly phased het, OR genotype is homozygous
55 // phasepresent cannot be nullptr
56 void GenoarrPhasedToAlleleCodes(const uint64_t* geno_to_intcode_dpair_table, const uintptr_t* genoarr, const uintptr_t* phasepresent, const uintptr_t* phaseinfo, uint32_t sample_ct, uint32_t phasepresent_ct, unsigned char* phasebytes, int32_t* allele_codes);
57
58 HEADER_INLINE void GenoarrPhasedToAlleleCodesMinus9(const uintptr_t* genoarr, const uintptr_t* phasepresent, const uintptr_t* phaseinfo, uint32_t sample_ct, uint32_t phasepresent_ct, unsigned char* phasebytes, int32_t* allele_codes) {
59 GenoarrPhasedToAlleleCodes(kGenoToIntcodeDPairs, genoarr, phasepresent, phaseinfo, sample_ct, phasepresent_ct, phasebytes, allele_codes);
60 }
61
62 void GenoarrMPToAlleleCodes(const uint64_t* geno_to_intcode_dpair_table, const PgenVariant* pgv, uint32_t sample_ct, unsigned char* phasebytes, int32_t* allele_codes);
63
64 HEADER_INLINE void GenoarrMPToAlleleCodesMinus9(const PgenVariant* pgv, uint32_t sample_ct, unsigned char* phasebytes, int32_t* allele_codes) {
65 GenoarrMPToAlleleCodes(kGenoToIntcodeDPairs, pgv, sample_ct, phasebytes, allele_codes);
66 }
67
68 // assumes transposed genoarr, phaseinfo
69 void GenoarrPhasedToHapCodes(const uintptr_t* genoarr, const uintptr_t* phaseinfo, uint32_t variant_batch_size, int32_t* hap0_codes_iter, int32_t* hap1_codes_iter);
70
71 void Dosage16ToFloatsMinus9(const uintptr_t* genoarr, const uintptr_t* dosage_present, const uint16_t* dosage_main, uint32_t sample_ct, uint32_t dosage_ct, float* geno_float);
72
73 void Dosage16ToDoubles(const double* geno_double_pair_table, const uintptr_t* genoarr, const uintptr_t* dosage_present, const uint16_t* dosage_main, uint32_t sample_ct, uint32_t dosage_ct, double* geno_double);
74
75 // If all samples are missing, this errors out.
76 BoolErr Dosage16ToDoublesMeanimpute(const uintptr_t* genoarr, const uintptr_t* dosage_present, const uint16_t* dosage_main, uint32_t sample_ct, uint32_t dosage_ct, double* geno_double);
77
78 // Currently requires trailing bits of genoarr to be zeroed out.
79 double LinearCombinationMeanimpute(const double* weights, const uintptr_t* genoarr, const uintptr_t* dosage_present, const uint16_t* dosage_main, uint32_t sample_ct, uint32_t dosage_ct);
80
81 HEADER_INLINE void Dosage16ToDoublesMinus9(const uintptr_t* genoarr, const uintptr_t* dosage_present, const uint16_t* dosage_main, uint32_t sample_ct, uint32_t dosage_ct, double* geno_double) {
82 Dosage16ToDoubles(kGenoDoublePairs, genoarr, dosage_present, dosage_main, sample_ct, dosage_ct, geno_double);
83 }
84
85 // Does not zero out trailing bits of bitarr.
86 void BytesToBitsUnsafe(const uint8_t* boolbytes, uint32_t sample_ct, uintptr_t* bitarr);
87
88 // Bottom 2 bits are extracted from every byte. Conveniently, -9 and 3 are
89 // treated identically.
90 // Does not zero out trailing bits of genoarr.
91 void BytesToGenoarrUnsafe(const int8_t* genobytes, uint32_t sample_ct, uintptr_t* genoarr);
92
93 // - Assumes biallelic variant, does not validate that.
94 // - Low bit of each element of phasepresent_bytes is significant.
95 // - If phasepresent_bytes is nullptr, phasepresent is not updated. In this
96 // case, phaseinfo is updated iff it's not nullptr. It's okay for both
97 // phasepresent and phaseinfo to be nullptr here.
98 // - Otherwise, phasepresent and phaseinfo are always updated; neither can be
99 // nullptr.
100 // - Trailing bits of phasepresent/phaseinfo may not be zeroed out.
101 void AlleleCodesToGenoarrUnsafe(const int32_t* allele_codes, const unsigned char* phasepresent_bytes, uint32_t sample_ct, uintptr_t* genoarr, uintptr_t* phasepresent, uintptr_t* phaseinfo);
102
103 // Does not clear trailing bits of genovec, phasepresent, or phaseinfo.
104 // Returns min(2, 1 + max allele code) if allele_codes is valid, -1 if invalid.
105 int32_t ConvertMultiAlleleCodesUnsafe(const int32_t* allele_codes, const unsigned char* phasepresent_bytes, uint32_t sample_ct, uintptr_t* genoarr, uintptr_t* patch_01_set, AlleleCode* patch_01_vals, uintptr_t* patch_10_set, AlleleCode* patch_10_vals, uint32_t* patch_01_ctp, uint32_t* patch_10_ctp, uintptr_t* phasepresent, uintptr_t* phaseinfo);
106
107 void FloatsToDosage16(const float* floatarr, uint32_t sample_ct, uint32_t hard_call_halfdist, uintptr_t* genoarr, uintptr_t* dosage_present, uint16_t* dosage_main, uint32_t* dosage_ct_ptr);
108
109 void DoublesToDosage16(const double* doublearr, uint32_t sample_ct, uint32_t hard_call_halfdist, uintptr_t* genoarr, uintptr_t* dosage_present, uint16_t* dosage_main, uint32_t* dosage_ct_ptr);
110
111 #ifdef __cplusplus
112 } // namespace plink2
113 #endif
114
115 #endif // __PGENLIB_FFI_SUPPORT_H__