| 1 |
#ifndef __PGENLIB_MISC_H__ |
| 2 |
#define __PGENLIB_MISC_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 |
|
| 21 |
// Low-level C99/C++03/C++11 library for reading .pgen (PLINK 2.0 binary) files |
| 22 |
// (designed to produce good lowest-common-denominator binaries across |
| 23 |
// Windows/OS X/Linux). |
| 24 |
// |
| 25 |
// File format design: |
| 26 |
// - With the header loaded, it is possible to efficiently access a variant by |
| 27 |
// its index. Since records can now be variable-length, this sometimes |
| 28 |
// requires storage of record lengths. |
| 29 |
// - Due to the power of LD-based compression, we permit a variant record to |
| 30 |
// just store a list of differences from an earlier, fully stored variant. |
| 31 |
// However, only short-range dependence is permitted; sequential processing |
| 32 |
// of the file only requires caching of the most recent explicitly stored |
| 33 |
// variant. |
| 34 |
// - Like the plink1 format, this is balanced for relatively easy reading and |
| 35 |
// writing; in particular, the mode-0x10/0x11 header is not read-optimized, |
| 36 |
// it passes up some obvious compression opportunities which would make it |
| 37 |
// more difficult to write e.g. an efficient file merger. This isn't a big |
| 38 |
// deal if we don't have a huge number of one-sample .pgen files sharing a |
| 39 |
// single .bim file (or equivalent). (If they don't share the same .bim |
| 40 |
// file, .bim overhead > .pgen overhead.) If we ever do, we can define an |
| 41 |
// additional mode to handle that case more efficiently. |
| 42 |
// - Building blocks are arrays of 1-bit, 2-bit, 4-bit, 1-byte, 2-byte, 3-byte, |
| 43 |
// and 4-byte values. 3/5/6/7(/9...)-bit values don't play well with |
| 44 |
// bitwise operations, and when it's important, there's usually a natural way |
| 45 |
// to split them into power-of-2-bit components. |
| 46 |
// (unsigned integers known to be smaller than 2^24, but not known to be |
| 47 |
// smaller than 2^16, are stored as 3-byte values on disk and "decompressed" |
| 48 |
// to uint32_t during loading.) |
| 49 |
// - Missing value is usually all-1s. (Only exceptions right now: plink1 |
| 50 |
// backward compatibility mode; presence/absence of rare alts for variants |
| 51 |
// with >2 alt alleles is an array of 1-bit values, where absence = 0; and |
| 52 |
// presence/absence of phasing info is similar.) Time to move away from 01 |
| 53 |
// nonsense. |
| 54 |
// - Individual variant records are prohibited from being >= 4GiB, to reduce |
| 55 |
// integer overflow issues. (This may be reduced to 2GiB later, but I'll |
| 56 |
// attempt to handle the 2-4GiB range properly for now since it's conceivable |
| 57 |
// for multiallelic records in very large datasets to reach that size.) |
| 58 |
// - (later todo: include stuff like file creation command in .pvar header; |
| 59 |
// that doesn't really belong in a binary file.) |
| 60 |
// See the bottom of this header file, and the pgen_spec/ subdirectory of |
| 61 |
// plink-ng on GitHub, for details. |
| 62 |
|
| 63 |
// Additional parameter conventions: |
| 64 |
// - "nyparr" indicates a word-aligned, packed array of 2-bit values, while |
| 65 |
// "nypvec" is the vector-aligned equivalent. "nybblearr" marks the much |
| 66 |
// rarer case of a packed array of 4-bit values. |
| 67 |
// - "nypvec_01" indicates a packed, vector-aligned array of 2-bit values where |
| 68 |
// each value is zero or one. This data structure was used quite a bit by |
| 69 |
// plink 1.9 for operating on a subset of a 2-bit-genotype array. |
| 70 |
// - "genoarr"/"genovec" indicates a nyparr/nypvec containing genotype |
| 71 |
// information. |
| 72 |
// - "interleaved_vec" is plink 2.0's preferred alternative to nypvec_01: we |
| 73 |
// basically stack pairs of adjacent vectors on top of each other and unpack |
| 74 |
// on the fly, since that tends to be faster than having to access twice as |
| 75 |
// much memory. |
| 76 |
|
| 77 |
#include "plink2_bits.h" |
| 78 |
#ifndef NDEBUG |
| 79 |
# include <stdarg.h> // va_start() |
| 80 |
#endif |
| 81 |
|
| 82 |
// 10000 * major + 100 * minor + patch |
| 83 |
// Exception to CONSTI32, since we want the preprocessor to have access to this |
| 84 |
// value. Named with all caps as a consequence. |
| 85 |
#define PGENLIB_INTERNAL_VERNUM 2005 |
| 86 |
|
| 87 |
#ifdef __cplusplus |
| 88 |
namespace plink2 { |
| 89 |
#endif |
| 90 |
|
| 91 |
#ifdef NDEBUG |
| 92 |
HEADER_INLINE BoolErr PglInitLog(__attribute__((unused)) uintptr_t log_capacity) { |
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
HEADER_INLINE void PglLogprintf(__attribute__((unused)) const char* fmt, ...) { |
| 97 |
} |
| 98 |
|
| 99 |
HEADER_INLINE char* PglReturnLog() { |
| 100 |
return nullptr; |
| 101 |
} |
| 102 |
|
| 103 |
HEADER_INLINE void PglResetLog() { |
| 104 |
} |
| 105 |
#else |
| 106 |
extern char* g_pgl_errbuf; |
| 107 |
extern char* g_pgl_errbuf_write_iter; |
| 108 |
extern char* g_pgl_errbuf_end; |
| 109 |
|
| 110 |
HEADER_INLINE BoolErr PglInitLog(uintptr_t log_capacity) { |
| 111 |
if (g_pgl_errbuf) { |
| 112 |
if (log_capacity <= S_CAST(uintptr_t, g_pgl_errbuf_end - g_pgl_errbuf)) { |
| 113 |
// existing allocation is fine. no need to shrink |
| 114 |
g_pgl_errbuf_write_iter = g_pgl_errbuf; |
| 115 |
*g_pgl_errbuf_write_iter = '\0'; |
| 116 |
return 0; |
| 117 |
} |
| 118 |
free(g_pgl_errbuf); |
| 119 |
} |
| 120 |
// 128 extra bytes to make WordWrapMultiline() safe. |
| 121 |
g_pgl_errbuf = S_CAST(char*, malloc(log_capacity + 128)); |
| 122 |
if (!g_pgl_errbuf) { |
| 123 |
g_pgl_errbuf_write_iter = nullptr; |
| 124 |
g_pgl_errbuf_end = nullptr; |
| 125 |
// in the unlikely event this comes up, caller is free to ignore the error, |
| 126 |
// logging just won't happen. though, if malloc is failing, something else |
| 127 |
// is likely to fail... |
| 128 |
return 1; |
| 129 |
} |
| 130 |
g_pgl_errbuf_write_iter = g_pgl_errbuf; |
| 131 |
*g_pgl_errbuf_write_iter = '\0'; |
| 132 |
g_pgl_errbuf_end = &(g_pgl_errbuf[log_capacity]); |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
HEADER_INLINE void PglLogprintf(const char* fmt, ...) { |
| 137 |
// possible todo: log levels |
| 138 |
if (g_pgl_errbuf_write_iter != nullptr) { |
| 139 |
va_list args; |
| 140 |
va_start(args, fmt); |
| 141 |
const uintptr_t remaining_space = g_pgl_errbuf_end - g_pgl_errbuf_write_iter; |
| 142 |
const uintptr_t intended_slen = vsnprintf(g_pgl_errbuf_write_iter, remaining_space, fmt, args); |
| 143 |
if (intended_slen < remaining_space) { |
| 144 |
g_pgl_errbuf_write_iter = &(g_pgl_errbuf_write_iter[intended_slen]); |
| 145 |
} else { |
| 146 |
g_pgl_errbuf_write_iter = g_pgl_errbuf_end; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
HEADER_INLINE char* PglReturnLog() { |
| 152 |
return g_pgl_errbuf; |
| 153 |
} |
| 154 |
|
| 155 |
HEADER_INLINE void PglResetLog() { |
| 156 |
if (g_pgl_errbuf) { |
| 157 |
g_pgl_errbuf_write_iter = g_pgl_errbuf; |
| 158 |
g_pgl_errbuf_write_iter[0] = '\0'; |
| 159 |
} |
| 160 |
} |
| 161 |
#endif |
| 162 |
|
| 163 |
// other configuration-ish values needed by plink2_common subset |
| 164 |
typedef unsigned char AlleleCode; |
| 165 |
typedef uint16_t DoubleAlleleCode; |
| 166 |
static_assert(sizeof(DoubleAlleleCode) == 2 * sizeof(AlleleCode), "Inconsistent AlleleCode and DoubleAlleleCode definitions."); |
| 167 |
// Set this to 65534 if AlleleCode is uint16_t, 2^24 - 1 if uint32_t. |
| 168 |
CONSTI32(kPglMaxAltAlleleCt, 254); |
| 169 |
|
| 170 |
CONSTI32(kPglMaxAlleleCt, kPglMaxAltAlleleCt + 1); |
| 171 |
#define PGL_MAX_ALT_ALLELE_CT_STR "254" |
| 172 |
#define PGL_MAX_ALLELE_CT_STR "255" |
| 173 |
#ifdef __cplusplus |
| 174 |
# define kMissingAlleleCode S_CAST(plink2::AlleleCode, -1) |
| 175 |
# define kMissingDoubleAlleleCode S_CAST(plink2::DoubleAlleleCode, -1) |
| 176 |
#else |
| 177 |
# define kMissingAlleleCode S_CAST(AlleleCode, -1) |
| 178 |
# define kMissingDoubleAlleleCode S_CAST(DoubleAlleleCode, -1) |
| 179 |
#endif |
| 180 |
CONSTI32(kAlleleCodesPerVec, kBytesPerVec / sizeof(AlleleCode)); |
| 181 |
|
| 182 |
HEADER_INLINE uintptr_t AlleleCodeCtToVecCt(uintptr_t val) { |
| 183 |
return DivUp(val, kAlleleCodesPerVec); |
| 184 |
} |
| 185 |
|
| 186 |
HEADER_INLINE uintptr_t AlleleCodeCtToAlignedWordCt(uintptr_t val) { |
| 187 |
return kWordsPerVec * AlleleCodeCtToVecCt(val); |
| 188 |
} |
| 189 |
|
| 190 |
HEADER_INLINE AlleleCode* DowncastWToAC(uintptr_t* pp) { |
| 191 |
return R_CAST(AlleleCode*, pp); |
| 192 |
} |
| 193 |
|
| 194 |
HEADER_INLINE void AlignACToVec(AlleleCode** pp) { |
| 195 |
const uintptr_t addr = R_CAST(uintptr_t, *pp); |
| 196 |
*pp = R_CAST(AlleleCode*, RoundUpPow2(addr, kBytesPerVec)); |
| 197 |
} |
| 198 |
|
| 199 |
// returns a word with low bit in each pair set at each 00. |
| 200 |
HEADER_INLINE uintptr_t Word00(uintptr_t ww) { |
| 201 |
return (~(ww | (ww >> 1))) & kMask5555; |
| 202 |
} |
| 203 |
|
| 204 |
HEADER_INLINE uintptr_t Word01(uintptr_t ww) { |
| 205 |
return ww & (~(ww >> 1)) & kMask5555; |
| 206 |
} |
| 207 |
|
| 208 |
// returns a word with *low* bit in each pair set at each 10. |
| 209 |
HEADER_INLINE uintptr_t Word10(uintptr_t ww) { |
| 210 |
return (~ww) & (ww >> 1) & kMask5555; |
| 211 |
} |
| 212 |
|
| 213 |
HEADER_INLINE uintptr_t Word11(uintptr_t ww) { |
| 214 |
return ww & (ww >> 1) & kMask5555; |
| 215 |
} |
| 216 |
|
| 217 |
HEADER_INLINE Halfword Pack00ToHalfword(uintptr_t ww) { |
| 218 |
return PackWordToHalfwordMask5555(~(ww | (ww >> 1))); |
| 219 |
} |
| 220 |
|
| 221 |
HEADER_INLINE Halfword Pack01ToHalfword(uintptr_t ww) { |
| 222 |
return PackWordToHalfwordMask5555(ww & (~(ww >> 1))); |
| 223 |
} |
| 224 |
|
| 225 |
HEADER_INLINE Halfword Pack11ToHalfword(uintptr_t ww) { |
| 226 |
return PackWordToHalfwordMask5555(ww & (ww >> 1)); |
| 227 |
} |
| 228 |
|
| 229 |
#ifdef USE_SSE42 |
| 230 |
HEADER_INLINE uint32_t Popcount01Word(uintptr_t val) { |
| 231 |
return PopcountWord(val); |
| 232 |
} |
| 233 |
|
| 234 |
HEADER_INLINE uint32_t Popcount0001Word(uintptr_t val) { |
| 235 |
return PopcountWord(val); |
| 236 |
} |
| 237 |
#else |
| 238 |
HEADER_INLINE uint32_t Popcount01Word(uintptr_t val) { |
| 239 |
return NypsumWord(val); |
| 240 |
} |
| 241 |
|
| 242 |
HEADER_INLINE uint32_t Popcount0001Word(uintptr_t val) { |
| 243 |
# ifdef __LP64__ |
| 244 |
// (val * kMask1111) >> 60 can barely overflow, sigh |
| 245 |
const uintptr_t val0 = val & 1; |
| 246 |
return (((val - val0) * kMask1111) >> 60) + val0; |
| 247 |
# else |
| 248 |
return (val * kMask1111) >> 28; |
| 249 |
# endif |
| 250 |
} |
| 251 |
#endif |
| 252 |
|
| 253 |
// assumes subset_mask has trailing zeroes up to the next vector boundary |
| 254 |
void FillInterleavedMaskVec(const uintptr_t* __restrict subset_mask, uint32_t base_vec_ct, uintptr_t* interleaved_mask_vec); |
| 255 |
|
| 256 |
HEADER_INLINE void CopyNyparr(const uintptr_t* __restrict source_nyparr, uint32_t nyparr_entry_ct, uintptr_t* __restrict target_nyparr) { |
| 257 |
memcpy(target_nyparr, source_nyparr, NypCtToWordCt(nyparr_entry_ct) * kBytesPerWord); |
| 258 |
} |
| 259 |
|
| 260 |
// may want bit past the end of subset_mask (i.e. position |
| 261 |
// raw_nyparr_entry_ct) to always be allocated and unset. This removes the |
| 262 |
// need for some explicit end-of-bitarray checks. |
| 263 |
void CopyNyparrNonemptySubset(const uintptr_t* __restrict raw_nyparr, const uintptr_t* __restrict subset_mask, uint32_t raw_nyparr_entry_ct, uint32_t subset_entry_ct, uintptr_t* __restrict output_nyparr); |
| 264 |
|
| 265 |
// Copies a bit from raw_bitarr for each genoarr entry matching match_word. |
| 266 |
// (match_word must be a multiple of kMask5555.) |
| 267 |
void CopyGenomatchSubset(const uintptr_t* __restrict raw_bitarr, const uintptr_t* __restrict genoarr, uintptr_t match_word, uint32_t write_bit_idx_start, uint32_t bit_ct, void* __restrict output); |
| 268 |
|
| 269 |
void ExpandBytearrFromGenoarr(const void* __restrict compact_bitarr, const uintptr_t* __restrict genoarr, uintptr_t match_word, uint32_t genoword_ct, uint32_t expand_size, uint32_t read_start_bit, uintptr_t* __restrict target); |
| 270 |
|
| 271 |
|
| 272 |
// These functions are "unsafe" since they assume trailing bits of last |
| 273 |
// genovec/genoarr word are zeroed out. |
| 274 |
void GenovecCount12Unsafe(const uintptr_t* genovec, uint32_t sample_ct, uint32_t* __restrict raw_01_ctp, uint32_t* __restrict raw_10_ctp); |
| 275 |
|
| 276 |
void Count3FreqVec6(const void* geno_vvec, uint32_t vec_ct, uint32_t* __restrict even_ctp, uint32_t* __restrict odd_ctp, uint32_t* __restrict bothset_ctp); |
| 277 |
|
| 278 |
// vector-alignment preferred. |
| 279 |
void GenoarrCountFreqsUnsafe(const uintptr_t* genoarr, uint32_t sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts); |
| 280 |
|
| 281 |
// GenoarrCountFreqsUnsafe() wrapper that returns most common genotype, |
| 282 |
// breaking ties in favor of the lower value. |
| 283 |
uintptr_t MostCommonGenoUnsafe(const uintptr_t* genoarr, uint32_t sample_ct); |
| 284 |
|
| 285 |
void CountSubset3FreqVec6(const void* __restrict genoarr, const VecW* __restrict interleaved_mask_vvec, uint32_t vec_ct, uint32_t* __restrict even_ctp, uint32_t* __restrict odd_ctp, uint32_t* __restrict bothset_ctp); |
| 286 |
|
| 287 |
// genoarr vector-alignment preferred. |
| 288 |
void GenoarrCountSubsetFreqs(const uintptr_t* __restrict genoarr, const uintptr_t* __restrict sample_include_interleaved_vec, uint32_t raw_sample_ct, uint32_t sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts); |
| 289 |
|
| 290 |
// slower GenoarrCountSubsetFreqs() which does not require |
| 291 |
// sample_include_interleaved_vec to be precomputed |
| 292 |
void GenoarrCountSubsetFreqs2(const uintptr_t* __restrict genoarr, const uintptr_t* __restrict sample_include, uint32_t raw_sample_ct, uint32_t sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts); |
| 293 |
|
| 294 |
void GenoarrCountInvsubsetFreqs2(const uintptr_t* __restrict genoarr, const uintptr_t* __restrict sample_exclude, uint32_t raw_sample_ct, uint32_t sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts); |
| 295 |
|
| 296 |
void GenoarrCountSubsetIntersectFreqs(const uintptr_t* __restrict genoarr, const uintptr_t* __restrict subset1, const uintptr_t* __restrict subset2, uint32_t raw_sample_ct, STD_ARRAY_REF(uint32_t, 4) genocounts); |
| 297 |
|
| 298 |
void GenovecInvertUnsafe(uint32_t sample_ct, uintptr_t* genovec); |
| 299 |
|
| 300 |
HEADER_INLINE uintptr_t InvertGenoWordUnsafe(uintptr_t geno_word) { |
| 301 |
return (geno_word ^ ((~(geno_word << 1)) & kMaskAAAA)); |
| 302 |
} |
| 303 |
|
| 304 |
// too easy to forget to multiply by 2 |
| 305 |
HEADER_INLINE void ZeroTrailingNyps(uintptr_t nyp_ct, uintptr_t* bitarr) { |
| 306 |
ZeroTrailingBits(nyp_ct * 2, bitarr); |
| 307 |
} |
| 308 |
|
| 309 |
HEADER_INLINE void SetTrailingNyps(uintptr_t nyp_ct, uintptr_t* bitarr) { |
| 310 |
const uintptr_t trail_ct = nyp_ct % kBitsPerWordD2; |
| 311 |
if (trail_ct) { |
| 312 |
bitarr[nyp_ct / kBitsPerWordD2] |= (~k0LU) << (nyp_ct * 2); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
// GetVint31 and Vint32Append moved to plink2_base. |
| 317 |
|
| 318 |
// Input must be validated. |
| 319 |
HEADER_INLINE uint32_t GetVint32Unsafe(const unsigned char** buf_iterp) { |
| 320 |
uint32_t vint32 = *(*buf_iterp)++; |
| 321 |
if (vint32 <= 127) { |
| 322 |
return vint32; |
| 323 |
} |
| 324 |
vint32 &= 127; |
| 325 |
for (uint32_t shift = 7; ; shift += 7) { |
| 326 |
uint32_t uii = *(*buf_iterp)++; |
| 327 |
vint32 |= (uii & 127) << shift; |
| 328 |
if (uii <= 127) { |
| 329 |
return vint32; |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
HEADER_INLINE void SkipVintUnsafe(const unsigned char** buf_iterp) { |
| 335 |
uint32_t cur_byte; |
| 336 |
do { |
| 337 |
cur_byte = *(*buf_iterp)++; |
| 338 |
} while (cur_byte & 128); |
| 339 |
} |
| 340 |
|
| 341 |
// Does not update buf_iter. |
| 342 |
HEADER_INLINE uint32_t PeekVint31(const unsigned char* buf_iter, const unsigned char* buf_end) { |
| 343 |
if (likely(buf_end > buf_iter)) { |
| 344 |
uint32_t vint32 = *buf_iter++; |
| 345 |
if (vint32 <= 127) { |
| 346 |
return vint32; |
| 347 |
} |
| 348 |
vint32 &= 127; |
| 349 |
uint32_t shift = 7; |
| 350 |
while (likely(buf_end > buf_iter)) { |
| 351 |
uint32_t uii = *buf_iter++; |
| 352 |
vint32 |= (uii & 127) << shift; |
| 353 |
if (uii <= 127) { |
| 354 |
return vint32; |
| 355 |
} |
| 356 |
shift += 7; |
| 357 |
} |
| 358 |
} |
| 359 |
return 0x80000000U; |
| 360 |
} |
| 361 |
|
| 362 |
/* |
| 363 |
HEADER_INLINE void FPutVint31(uint32_t uii, FILE* ff) { |
| 364 |
// caller's responsibility to periodically check ferror |
| 365 |
while (uii > 127) { |
| 366 |
putc_unlocked((uii & 127) + 128, ff); |
| 367 |
uii >>= 7; |
| 368 |
} |
| 369 |
putc_unlocked(uii, ff); |
| 370 |
} |
| 371 |
*/ |
| 372 |
|
| 373 |
HEADER_INLINE BoolErr FSkipVint(FILE* ff) { |
| 374 |
while (1) { |
| 375 |
const uint32_t cur_byte = getc_unlocked(ff); |
| 376 |
if (cur_byte <= 127) { |
| 377 |
return 0; |
| 378 |
} |
| 379 |
if (unlikely(cur_byte > 255)) { |
| 380 |
return 1; |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
HEADER_INLINE uint64_t FGetVint63(FILE* ff) { |
| 386 |
// Can't be used when multiple threads are reading from ff. |
| 387 |
uint64_t vint64 = getc_unlocked(ff); |
| 388 |
if (vint64 <= 127) { |
| 389 |
return vint64; |
| 390 |
} |
| 391 |
if (unlikely(vint64 > 255)) { |
| 392 |
return (1LLU << 63); |
| 393 |
} |
| 394 |
vint64 &= 127; |
| 395 |
for (uint32_t shift = 7; ; shift += 7) { |
| 396 |
const uint64_t ullii = getc_unlocked(ff); |
| 397 |
vint64 |= (ullii & 127) << shift; |
| 398 |
if (ullii <= 127) { |
| 399 |
return vint64; |
| 400 |
} |
| 401 |
if (unlikely((ullii > 255) || (shift == 56))) { |
| 402 |
return (1LLU << 63); |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
// Need this for sparse multiallelic dosage. |
| 408 |
HEADER_INLINE unsigned char* Vint64Append(uint64_t ullii, unsigned char* buf) { |
| 409 |
while (ullii > 127) { |
| 410 |
*buf++ = (ullii & 127) + 128; |
| 411 |
ullii >>= 7; |
| 412 |
} |
| 413 |
*buf++ = ullii; |
| 414 |
return buf; |
| 415 |
} |
| 416 |
|
| 417 |
HEADER_INLINE uint64_t GetVint64Unsafe(const unsigned char** buf_iterp) { |
| 418 |
uint64_t vint64 = *(*buf_iterp)++; |
| 419 |
if (vint64 <= 127) { |
| 420 |
return vint64; |
| 421 |
} |
| 422 |
vint64 &= 127; |
| 423 |
for (uint32_t shift = 7; ; shift += 7) { |
| 424 |
uint64_t ullii = *(*buf_iterp)++; |
| 425 |
vint64 |= (ullii & 127) << shift; |
| 426 |
if (ullii <= 127) { |
| 427 |
return vint64; |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
HEADER_INLINE void FPutVint64(uint64_t ullii, FILE* ff) { |
| 433 |
// caller's responsibility to periodically check ferror |
| 434 |
while (ullii > 127) { |
| 435 |
putc_unlocked((ullii & 127) + 128, ff); |
| 436 |
ullii >>= 7; |
| 437 |
} |
| 438 |
putc_unlocked(ullii, ff); |
| 439 |
} |
| 440 |
|
| 441 |
// TODO: make this work properly with kCacheline == 128, then fix other |
| 442 |
// transpose functions, etc. |
| 443 |
|
| 444 |
// main batch size |
| 445 |
CONSTI32(kPglNypTransposeBatch, kNypsPerCacheline); |
| 446 |
|
| 447 |
// word width of each matrix row |
| 448 |
CONSTI32(kPglNypTransposeWords, kWordsPerCacheline); |
| 449 |
|
| 450 |
#ifdef __LP64__ |
| 451 |
CONSTI32(kPglNypTransposeBufbytes, (kPglNypTransposeBatch * kPglNypTransposeBatch) / 2); |
| 452 |
|
| 453 |
// buf0 and buf1 assumed to be vector-aligned. |
| 454 |
void TransposeNypblock64(const uintptr_t* read_iter, uint32_t read_ul_stride, uint32_t write_ul_stride, uint32_t read_batch_size, uint32_t write_batch_size, uintptr_t* __restrict write_iter, unsigned char* __restrict buf0, unsigned char* __restrict buf1); |
| 455 |
#else // !__LP64__ |
| 456 |
CONSTI32(kPglNypTransposeBufbytes, (kPglNypTransposeBatch * kPglNypTransposeBatch) / 2); |
| 457 |
|
| 458 |
void TransposeNypblock32(const uintptr_t* read_iter, uint32_t read_ul_stride, uint32_t write_ul_stride, uint32_t read_batch_size, uint32_t write_batch_size, uintptr_t* __restrict write_iter, unsigned char* __restrict buf0, unsigned char* __restrict buf1); |
| 459 |
#endif |
| 460 |
CONSTI32(kPglNypTransposeBufwords, kPglNypTransposeBufbytes / kBytesPerWord); |
| 461 |
|
| 462 |
// - single block is up to 256x256 (CACHELINE64) or 512x512 (CACHELINE128) |
| 463 |
// - vecaligned_buf must have size 32k (CACHELINE64) or 128k (CACHELINE128) |
| 464 |
// - does NOT zero out trailing bits, because main application is ind-major-bed |
| 465 |
// <-> plink2 format conversion, where the zeroing would be undone... |
| 466 |
// - important: write_iter must be allocated up to at least |
| 467 |
// RoundUpPow2(write_batch_size, 4) rows (may want to remove this |
| 468 |
// requirement) |
| 469 |
|
| 470 |
HEADER_INLINE void TransposeNypblock(const uintptr_t* read_iter, uint32_t read_ul_stride, uint32_t write_ul_stride, uint32_t read_batch_size, uint32_t write_batch_size, uintptr_t* write_iter, VecW* vecaligned_buf) { |
| 471 |
#ifdef __LP64__ |
| 472 |
// assert(!(write_ul_stride % 2)); |
| 473 |
TransposeNypblock64(read_iter, read_ul_stride, write_ul_stride, read_batch_size, write_batch_size, write_iter, DowncastToUc(vecaligned_buf), &(DowncastToUc(vecaligned_buf)[kPglNypTransposeBufbytes / 2])); |
| 474 |
#else |
| 475 |
TransposeNypblock32(read_iter, read_ul_stride, write_ul_stride, read_batch_size, write_batch_size, write_iter, DowncastToUc(vecaligned_buf), &(DowncastToUc(vecaligned_buf)[kPglNypTransposeBufbytes / 2])); |
| 476 |
#endif |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
// replaces each x with (32768 - x) |
| 481 |
// okay for dosage_main to be nullptr if dosage_ct == 0 |
| 482 |
void BiallelicDosage16Invert(uint32_t dosage_ct, uint16_t* dosage_main); |
| 483 |
|
| 484 |
// replaces each x with -x |
| 485 |
void BiallelicDphase16Invert(uint32_t dphase_ct, int16_t* dphase_delta); |
| 486 |
|
| 487 |
void PackWordsToHalfwordsInvmatch(const uintptr_t* __restrict genoarr, uintptr_t inv_match_word, uint32_t inword_ct, uintptr_t* __restrict dst); |
| 488 |
|
| 489 |
void PackWordsToHalfwordsMismatch(const uintptr_t* __restrict genoarr, uintptr_t mismatch_word, uint32_t inword_ct, uintptr_t* __restrict dst); |
| 490 |
|
| 491 |
// src and dst allowed to be identical; that's why src is not marked const |
| 492 |
// despite not being directly written to. |
| 493 |
void MaskWordsToHalfwordsInvmatch(const uintptr_t* __restrict genoarr, uintptr_t inv_match_word, uint32_t inword_ct, uintptr_t* src, uintptr_t* dst); |
| 494 |
|
| 495 |
// Unsafe since it assumes trailing genoarr bits are cleared. But if they are, |
| 496 |
// trailing missingness bits will be clear. |
| 497 |
HEADER_INLINE void GenoarrToMissingnessUnsafe(const uintptr_t* __restrict genoarr, uint32_t sample_ct, uintptr_t* __restrict missingness) { |
| 498 |
const uint32_t sample_ctl2 = NypCtToWordCt(sample_ct); |
| 499 |
PackWordsToHalfwordsInvmatch(genoarr, 0, sample_ctl2, missingness); |
| 500 |
if (sample_ctl2 % 2) { |
| 501 |
Halfword* __attribute__((may_alias)) missingness_alias = DowncastWToHW(missingness); |
| 502 |
missingness_alias[sample_ctl2] = 0; |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
HEADER_INLINE void GenoarrToNonmissing(const uintptr_t* __restrict genoarr, uint32_t sample_ct, uintptr_t* __restrict nonmissingness) { |
| 507 |
const uint32_t sample_ctl2 = NypCtToWordCt(sample_ct); |
| 508 |
PackWordsToHalfwordsMismatch(genoarr, ~k0LU, sample_ctl2, nonmissingness); |
| 509 |
ZeroTrailingBits(sample_ct, nonmissingness); |
| 510 |
} |
| 511 |
|
| 512 |
void SparseToMissingness(const uintptr_t* __restrict raregeno, const uint32_t* difflist_sample_ids, uint32_t sample_ct, uint32_t difflist_common_geno, uint32_t difflist_len, uintptr_t* __restrict missingness); |
| 513 |
|
| 514 |
// hom_buf gets set bits when genoarr value is 0 or 2. |
| 515 |
// ref2het_buf gets set bits when genoarr value is 0 or 1. |
| 516 |
// N.B. assumes trailing bits of loadbuf have been filled with 1s, not 0s |
| 517 |
// Also takes genoarr word count instead of sample count. |
| 518 |
void SplitHomRef2hetUnsafeW(const uintptr_t* genoarr, uint32_t inword_ct, uintptr_t* __restrict hom_buf, uintptr_t* __restrict ref2het_buf); |
| 519 |
|
| 520 |
void SplitHomRef2het(const uintptr_t* genoarr, uint32_t sample_ct, uintptr_t* __restrict hom_buf, uintptr_t* __restrict ref2het_buf); |
| 521 |
|
| 522 |
|
| 523 |
// Support for 1-bit-per-haplotype representation. |
| 524 |
// Ok for genoarr to have garbage trailing bits. |
| 525 |
BoolErr HapsplitMustPhased(const uintptr_t* genoarr, const uintptr_t* phasepresent, const uintptr_t* phaseinfo, uint32_t sample_ct, uint32_t phasepresent_ct, uintptr_t* hap_arr, uintptr_t* nm_arr); |
| 526 |
|
| 527 |
// Only 1 haplotype per genotype, rather than 2; het treated as missing. |
| 528 |
void HapsplitHaploid(const uintptr_t* __restrict genoarr, uint32_t sample_ct, uintptr_t* __restrict hap_arr, uintptr_t* __restrict nm_arr); |
| 529 |
|
| 530 |
|
| 531 |
// These functions use 16- or 256-element lookup tables to apply functions of |
| 532 |
// the form |
| 533 |
// f: {0,1,2,3} -> x |
| 534 |
// to genoarr, saving the output to result[]. |
| 535 |
// 256-element tables result in a substantially faster inner loop, but they are |
| 536 |
// more expensive to set up and consume a non-negligible fraction of L1 cache, |
| 537 |
// so they aren't always the right choice. |
| 538 |
// When lookup table rows are 16 bytes, they are assumed to be 16-byte aligned |
| 539 |
// in 64-bit builds. result[] is not assumed to be aligned. |
| 540 |
void GenoarrLookup256x1bx4(const uintptr_t* genoarr, const void* table256x1bx4, uint32_t sample_ct, void* __restrict result); |
| 541 |
|
| 542 |
void GenoarrLookup16x4bx2(const uintptr_t* genoarr, const void* table16x4bx2, uint32_t sample_ct, void* result); |
| 543 |
|
| 544 |
void GenoarrLookup256x2bx4(const uintptr_t* genoarr, const void* table256x2bx4, uint32_t sample_ct, void* result); |
| 545 |
|
| 546 |
void GenoarrLookup4x16b(const uintptr_t* genoarr, const void* table4x16b, uint32_t sample_ct, void* result); |
| 547 |
|
| 548 |
#define PAIR_TABLE16(a, b, c, d) \ |
| 549 |
{(a), (a), (b), (a), (c), (a), (d), (a), \ |
| 550 |
(a), (b), (b), (b), (c), (b), (d), (b), \ |
| 551 |
(a), (c), (b), (c), (c), (c), (d), (c), \ |
| 552 |
(a), (d), (b), (d), (c), (d), (d), (d)} |
| 553 |
|
| 554 |
void GenoarrLookup16x8bx2(const uintptr_t* genoarr, const void* table16x8bx2, uint32_t sample_ct, void* result); |
| 555 |
|
| 556 |
#define QUAD_TABLE256_INTERNAL2(a, b, c, d, f2, f3, f4) \ |
| 557 |
(a), (f2), (f3), (f4), \ |
| 558 |
(b), (f2), (f3), (f4), \ |
| 559 |
(c), (f2), (f3), (f4), \ |
| 560 |
(d), (f2), (f3), (f4) |
| 561 |
#define QUAD_TABLE256_INTERNAL3(a, b, c, d, f3, f4) \ |
| 562 |
QUAD_TABLE256_INTERNAL2((a), (b), (c), (d), (a), (f3), (f4)), \ |
| 563 |
QUAD_TABLE256_INTERNAL2((a), (b), (c), (d), (b), (f3), (f4)), \ |
| 564 |
QUAD_TABLE256_INTERNAL2((a), (b), (c), (d), (c), (f3), (f4)), \ |
| 565 |
QUAD_TABLE256_INTERNAL2((a), (b), (c), (d), (d), (f3), (f4)) |
| 566 |
#define QUAD_TABLE256_INTERNAL4(a, b, c, d, f4) \ |
| 567 |
QUAD_TABLE256_INTERNAL3((a), (b), (c), (d), (a), (f4)), \ |
| 568 |
QUAD_TABLE256_INTERNAL3((a), (b), (c), (d), (b), (f4)), \ |
| 569 |
QUAD_TABLE256_INTERNAL3((a), (b), (c), (d), (c), (f4)), \ |
| 570 |
QUAD_TABLE256_INTERNAL3((a), (b), (c), (d), (d), (f4)) |
| 571 |
#define QUAD_TABLE256(a, b, c, d) \ |
| 572 |
{QUAD_TABLE256_INTERNAL4((a), (b), (c), (d), (a)), \ |
| 573 |
QUAD_TABLE256_INTERNAL4((a), (b), (c), (d), (b)), \ |
| 574 |
QUAD_TABLE256_INTERNAL4((a), (b), (c), (d), (c)), \ |
| 575 |
QUAD_TABLE256_INTERNAL4((a), (b), (c), (d), (d))} |
| 576 |
|
| 577 |
void GenoarrLookup256x4bx4(const uintptr_t* genoarr, const void* table256x4bx4, uint32_t sample_ct, void* result); |
| 578 |
|
| 579 |
// Lookup table initialization functions. table[0][0], [1][0], [2][0], and |
| 580 |
// [3][0] must be initialized to f(0), f(1), f(2), and f(3) respectively. |
| 581 |
void InitLookup16x4bx2(void* table16x4bx2); |
| 582 |
|
| 583 |
void InitLookup16x8bx2(void* table16x8bx2); |
| 584 |
|
| 585 |
#ifdef USE_SHUFFLE8 |
| 586 |
// in bytes |
| 587 |
CONSTI32(kLookup256x1bx4Size, 1024 + 2 * kBytesPerVec); |
| 588 |
#else |
| 589 |
CONSTI32(kLookup256x1bx4Size, 1024); |
| 590 |
#endif |
| 591 |
|
| 592 |
void InitLookup256x1bx4(void* table256x1bx4); |
| 593 |
|
| 594 |
void InitLookup256x2bx4(void* table256x2bx4); |
| 595 |
|
| 596 |
void InitLookup256x4bx4(void* table256x4bx4); |
| 597 |
|
| 598 |
void PhaseLookup4b(const uintptr_t* genoarr, const uintptr_t* phasepresent, const uintptr_t* phaseinfo, const void* table56x4bx2, uint32_t sample_ct, void* result); |
| 599 |
|
| 600 |
// [0][0]..[3][0], [17][0], and [19][0] should contain the relevant values |
| 601 |
void InitPhaseLookup4b(void* table56x4bx2); |
| 602 |
|
| 603 |
void PhaseLookup8b(const uintptr_t* genoarr, const uintptr_t* phasepresent, const uintptr_t* phaseinfo, const void* table56x8bx2, uint32_t sample_ct, void* result); |
| 604 |
|
| 605 |
void InitPhaseLookup8b(void* table56x8bx2); |
| 606 |
|
| 607 |
// het-haploid prohibited. 64-entry table suffices: we use the same bits for |
| 608 |
// phasepresent and sex_male since they can't be true simultaneously. |
| 609 |
// phaseinfo is xor'd with bits 1 and 3 instead of 1 and 2. |
| 610 |
void PhaseXNohhLookup4b(const uintptr_t* genoarr, const uintptr_t* phasepresent, const uintptr_t* phaseinfo, const uintptr_t* sex_male, const void* table64x4bx2, uint32_t sample_ct, void* result); |
| 611 |
|
| 612 |
// [0][0]..[3][0], [16][0]..[19][0] |
| 613 |
void InitPhaseXNohhLookup4b(void* table64x4bx2); |
| 614 |
|
| 615 |
// uses same table as PhaseXNohhLookup |
| 616 |
void GenoarrSexLookup4b(const uintptr_t* genoarr, const uintptr_t* sex_male, const void* table64x4bx2, uint32_t sample_ct, void* result); |
| 617 |
|
| 618 |
void InitPhaseXNohhLookup8b(void* table64x8bx2); |
| 619 |
|
| 620 |
void GenoarrSexLookup8b(const uintptr_t* genoarr, const uintptr_t* sex_male, const void* table64x8bx2, uint32_t sample_ct, void* result); |
| 621 |
|
| 622 |
// Unlike PhaseLookup4b(), this allows the cur_phased bit to be set when the |
| 623 |
// genoarr entry is not 01 (het). |
| 624 |
void VcfPhaseLookup4b(const uintptr_t* genoarr, const uintptr_t* cur_phased, const uintptr_t* phaseinfo, const void* table246x4bx2, uint32_t sample_ct, void* __restrict result); |
| 625 |
|
| 626 |
// Precondition: |
| 627 |
// [0], [2], [4], [6] initialized with unphased entries |
| 628 |
// [32], [34], [36], [38] initialized with phased-unflipped entries |
| 629 |
// [162] initialized with phased-flipped case |
| 630 |
void InitVcfPhaseLookup4b(void* table246x4bx2); |
| 631 |
|
| 632 |
void VcfPhaseLookup2b(const uintptr_t* genoarr, const uintptr_t* cur_phased, const uintptr_t* phaseinfo, const void* table246x2bx2, uint32_t sample_ct, void* __restrict result); |
| 633 |
|
| 634 |
void InitVcfPhaseLookup2b(void* table246x2bx2); |
| 635 |
|
| 636 |
|
| 637 |
// Analogue of BitIter1x. |
| 638 |
HEADER_INLINE uint32_t GenoIter1x(const uintptr_t* __restrict genoarr, uintptr_t match_word, uintptr_t* __restrict widxp, uintptr_t* __restrict cur_bitsp) { |
| 639 |
uintptr_t cur_bits = *cur_bitsp; |
| 640 |
while (!cur_bits) { |
| 641 |
cur_bits = genoarr[++(*widxp)] ^ match_word; |
| 642 |
cur_bits = (~(cur_bits | (cur_bits >> 1))) & kMask5555; |
| 643 |
} |
| 644 |
*cur_bitsp = cur_bits & (cur_bits - 1); |
| 645 |
return ctzw(cur_bits); |
| 646 |
} |
| 647 |
|
| 648 |
// For every missing entry in genoarr, clear the corresponding subset and |
| 649 |
// sparse_vals entries. |
| 650 |
void ClearGenoarrMissing1bit8Unsafe(const uintptr_t* __restrict genoarr, uint32_t* subset_sizep, uintptr_t* __restrict subset, void* __restrict sparse_vals); |
| 651 |
|
| 652 |
void ClearGenoarrMissing1bit16Unsafe(const uintptr_t* __restrict genoarr, uint32_t* subset_sizep, uintptr_t* __restrict subset, void* __restrict sparse_vals); |
| 653 |
|
| 654 |
// See EasyasPi's answer to |
| 655 |
// https://stackoverflow.com/questions/25095741/how-can-i-multiply-64-bit-operands-and-get-128-bit-result-portably |
| 656 |
HEADER_INLINE uint64_t multiply64to128(uint64_t lhs, uint64_t rhs, uint64_t* high) { |
| 657 |
// GCC and Clang usually provide __uint128_t on 64-bit targets, although |
| 658 |
// Clang also defines it on WASM despite having to use builtins for most |
| 659 |
// purposes -- including multiplication. |
| 660 |
#if defined(__SIZEOF_INT128__) && !defined(__wasm__) |
| 661 |
__uint128_t product = S_CAST(__uint128_t, lhs) * S_CAST(__uint128_t, rhs); |
| 662 |
*high = S_CAST(uint64_t, product >> 64); |
| 663 |
return S_CAST(uint64_t, product & 0xffffffffffffffffLLU); |
| 664 |
#else |
| 665 |
// Fast yet simple grade school multiply that avoids 64-bit carries with the |
| 666 |
// properties of multiplying by 11 and takes advantage of UMAAL on ARMv6 to |
| 667 |
// only need 4 calculations. |
| 668 |
|
| 669 |
// First calculate all of the cross products. |
| 670 |
uint64_t lo_lo = (lhs & 0xffffffff) * (rhs & 0xffffffff); |
| 671 |
uint64_t hi_lo = (lhs >> 32) * (rhs & 0xffffffff); |
| 672 |
uint64_t lo_hi = (lhs & 0xffffffff) * (rhs >> 32); |
| 673 |
uint64_t hi_hi = (lhs >> 32) * (rhs >> 32); |
| 674 |
// Now add the products together. These will never overflow. |
| 675 |
uint64_t cross = (lo_lo >> 32) + (hi_lo & 0xffffffff) + lo_hi; |
| 676 |
uint64_t upper = (hi_lo >> 32) + (cross >> 32) + hi_hi; |
| 677 |
|
| 678 |
*high = upper; |
| 679 |
return (cross << 32) | (lo_lo & 0xffffffff); |
| 680 |
#endif |
| 681 |
} |
| 682 |
|
| 683 |
HEADER_INLINE double u127tod(uint64_t hi, uint64_t lo) { |
| 684 |
return u63tod(hi) * 18446744073709551616.0 + S_CAST(double, lo); |
| 685 |
} |
| 686 |
|
| 687 |
// plus_term0 * plus_term1 - minus_term0 * minus_term1 |
| 688 |
double u127prod_diff_d(uint64_t plus_term0, uint64_t plus_term1, uint64_t minus_term0, uint64_t minus_term1); |
| 689 |
|
| 690 |
double i127prod_diff_d(uint64_t plus_term0, uint64_t plus_term1, uint64_t minus_term0, uint64_t minus_term1); |
| 691 |
|
| 692 |
double MultiallelicDiploidMinimac3R2(const uint64_t* __restrict sums, const uint64_t* __restrict hap_ssqs_x2, uint32_t nm_sample_ct, uint32_t allele_ct, uint32_t extra_phased_het_ct); |
| 693 |
|
| 694 |
HEADER_INLINE double MultiallelicDiploidMachR2(const uint64_t* __restrict sums, const uint64_t* __restrict ssqs, uint32_t nm_sample_ct, uint32_t allele_ct) { |
| 695 |
return 2 * MultiallelicDiploidMinimac3R2(sums, ssqs, nm_sample_ct, allele_ct, 0); |
| 696 |
} |
| 697 |
|
| 698 |
// ----- end plink2_common subset ----- |
| 699 |
|
| 700 |
// other configuration-ish values |
| 701 |
// this part of the specification is set in stone. |
| 702 |
|
| 703 |
CONSTI32(kPglVblockSize, 65536); |
| 704 |
|
| 705 |
// kPglDifflistGroupSize defined in plink2_base |
| 706 |
|
| 707 |
// Currently chosen so that it plus kPglFwriteBlockSize + kCacheline - 2 is |
| 708 |
// < 2^32, so DivUp(kPglMaxBytesPerVariant + kPglFwriteBlockSize - 1, |
| 709 |
// kCacheline) doesn't overflow. |
| 710 |
static const uint32_t kPglMaxBytesPerVariant = 0xfffdffc0U; |
| 711 |
// CONSTI32(kPglMaxBytesPerDataTrack, 0x7ffff000); |
| 712 |
// static_assert(kMaxBytesPerIO >= (int32_t)kPglMaxBytesPerDataTrack, "pgenlib assumes a single variant data track always fits in one fread/fwrite operation."); |
| 713 |
|
| 714 |
FLAGSET_DEF_START() |
| 715 |
kfPgenGlobal0, |
| 716 |
kfPgenGlobalLdCompressionPresent = (1 << 0), |
| 717 |
kfPgenGlobalDifflistOrLdPresent = (1 << 1), |
| 718 |
|
| 719 |
// Only guaranteed to be set when present if phase or dosage also present. |
| 720 |
kfPgenGlobalMultiallelicHardcallFound = (1 << 2), |
| 721 |
|
| 722 |
kfPgenGlobalHardcallPhasePresent = (1 << 3), |
| 723 |
kfPgenGlobalDosagePresent = (1 << 4), |
| 724 |
kfPgenGlobalDosagePhasePresent = (1 << 5), |
| 725 |
kfPgenGlobalAllNonref = (1 << 6) |
| 726 |
FLAGSET_DEF_END(PgenGlobalFlags); |
| 727 |
|
| 728 |
// difflist/LD compression must not involve more than |
| 729 |
// raw_sample_ct / kPglMaxDifflistLenDivisor |
| 730 |
// entries. (however, returned difflists can have up to twice as many entries, |
| 731 |
// when a variant is LD-compressed and the reference variant is |
| 732 |
// difflist-compressed.) |
| 733 |
// This value can be considered set in stone. |
| 734 |
CONSTI32(kPglMaxDifflistLenDivisor, 8); |
| 735 |
|
| 736 |
// Threshold for using a deltalist to represent a bitarray on disk (currently |
| 737 |
// relevant for dosage data). This is a tunable parameter, but must be >= |
| 738 |
// kPglMaxDifflistLenDivisor. |
| 739 |
CONSTI32(kPglMaxDeltalistLenDivisor, 9); |
| 740 |
|
| 741 |
void PgrDifflistToGenovecUnsafe(const uintptr_t* __restrict raregeno, const uint32_t* difflist_sample_ids, uintptr_t difflist_common_geno, uint32_t sample_ct, uint32_t difflist_len, uintptr_t* __restrict genovec); |
| 742 |
|
| 743 |
// This covers all the possibilities. Todo: switch all functions exporting |
| 744 |
// multiallelic codes and/or phased dosage to use this struct. (Biallelic |
| 745 |
// phased hardcalls and unphased dosages are simple enough for this to be |
| 746 |
// overkill, though.) |
| 747 |
typedef struct PgenVariantStruct { |
| 748 |
uintptr_t* genovec; |
| 749 |
uintptr_t* patch_01_set; |
| 750 |
AlleleCode* patch_01_vals; |
| 751 |
uintptr_t* patch_10_set; |
| 752 |
AlleleCode* patch_10_vals; |
| 753 |
uintptr_t* phasepresent; |
| 754 |
uintptr_t* phaseinfo; |
| 755 |
uintptr_t* dosage_present; |
| 756 |
uint16_t* dosage_main; |
| 757 |
uintptr_t* multidosage_present; |
| 758 |
unsigned char* multidosage_cts; |
| 759 |
AlleleCode* multidosage_codes; |
| 760 |
uint16_t* multidosage_vals; |
| 761 |
uintptr_t* dphase_present; |
| 762 |
int16_t* dphase_delta; |
| 763 |
uintptr_t* multidphase_present; |
| 764 |
unsigned char* multidphase_cts; |
| 765 |
AlleleCode* multidphase_codes; |
| 766 |
int16_t* multidphase_delta; |
| 767 |
|
| 768 |
uint32_t patch_01_ct; |
| 769 |
uint32_t patch_10_ct; |
| 770 |
uint32_t phasepresent_ct; |
| 771 |
uint32_t dosage_ct; |
| 772 |
uint32_t multidosage_sample_ct; |
| 773 |
uint32_t dphase_ct; |
| 774 |
uint32_t multidphase_sample_ct; |
| 775 |
} PgenVariant; |
| 776 |
|
| 777 |
HEADER_INLINE uintptr_t GetAux1bAlleleEntryByteCt(uint32_t allele_ct, uint32_t rare10_ct) { |
| 778 |
assert(allele_ct >= 3); |
| 779 |
if (allele_ct == 3) { |
| 780 |
return DivUp(rare10_ct, 8); |
| 781 |
} |
| 782 |
if (allele_ct < 6) { |
| 783 |
return DivUp(rare10_ct, 2); |
| 784 |
} |
| 785 |
// one byte per entry for allele_ct <= 17, two bytes for 18..256 |
| 786 |
return ((allele_ct >= 18) + 1) * rare10_ct; |
| 787 |
// todo: allele_ct > 257 |
| 788 |
} |
| 789 |
|
| 790 |
extern const uint16_t kHcToAlleleCodes[1024]; |
| 791 |
|
| 792 |
// Permits missing codes, does not remap. |
| 793 |
void PglMultiallelicSparseToDenseMiss(const PgenVariant* pgvp, uint32_t sample_ct, AlleleCode* __restrict wide_codes); |
| 794 |
|
| 795 |
uintptr_t PglComputeMaxAlleleCt(const uintptr_t* allele_idx_offsets, uint32_t variant_ct); |
| 796 |
|
| 797 |
HEADER_INLINE AlleleCode GetAidx(const uintptr_t* allele_idx_offsets, uint32_t variant_uidx, uintptr_t allele_idx) { |
| 798 |
if (!allele_idx_offsets) { |
| 799 |
return allele_idx - 2 * variant_uidx; |
| 800 |
} |
| 801 |
return allele_idx - allele_idx_offsets[variant_uidx]; |
| 802 |
} |
| 803 |
|
| 804 |
uint32_t CountNybble(const void* nybblearr, uintptr_t nybble_word, uintptr_t nybble_ct); |
| 805 |
|
| 806 |
// The actual format: |
| 807 |
// 1. 2 magic bytes 0x6c 0x1b. |
| 808 |
// |
| 809 |
// 2. Mode byte. |
| 810 |
// 0x01 = plink1 variant-major. |
| 811 |
// 0x02 = plink2 basic variant-major. variant/sample counts in header, |
| 812 |
// 00 = hom ref, 01 = het, 10 = hom alt, 11 = missing. (vrtype 0) |
| 813 |
// 0x03 = plink2 basic unphased dosage (vrtype 0x40) |
| 814 |
// 0x04 = plink2 basic phased dosage (vrtype 0xc0) |
| 815 |
// These are designed to be easy to write. Note that the dosage formats |
| 816 |
// require hardcalls to be stored as well; however, you can just set them |
| 817 |
// to all-missing and then use |
| 818 |
// plink2 --hard-call-threshold <...> --make-pgen |
| 819 |
// to populate them. |
| 820 |
// |
| 821 |
// 0x10 = variable-type and/or variable-length records present. |
| 822 |
// 0x11 = mode 0x10, but with phase set information at the end of the |
| 823 |
// file. |
| 824 |
// 0x05..0x0f and 0x12..0x7f are reserved for possible use by future |
| 825 |
// versions of the PGEN specification, and 0 is off-limits (PLINK 1 |
| 826 |
// sample-major .bed). |
| 827 |
// 0x80..0xff can be safely used by developers for their own purposes. |
| 828 |
// |
| 829 |
// 3. If not plink1-format, |
| 830 |
// a. 4-byte # of variants; call this M. |
| 831 |
// b. 4-byte # of samples, call this N. |
| 832 |
// c. Additional 1-byte header 'control' value (PgenHeaderCtrl). May be |
| 833 |
// extended in the future. |
| 834 |
// bits 0-3: Indicates vrtype and variant record length storage widths. |
| 835 |
// If bit 3 is unset, bits 0-1 store (vrec_len_byte_ct - 1), while bit |
| 836 |
// 2 is set iff phase or dosage info is present (requiring 8 bits |
| 837 |
// instead of 4 bits for vrtypes). |
| 838 |
// If bit 3 is set, a specialized encoding is used which combines the |
| 839 |
// two pieces of information (reducing the overhead for files with few |
| 840 |
// samples). The following encodings are now defined (note that there |
| 841 |
// was a change of plans in Mar 2019): |
| 842 |
// 8: 1 bit per fused vrtype-length. Unset = vrtype 5, set = vrtype |
| 843 |
// 0. |
| 844 |
// 9: 2 bits, multiallelic. 0 = vrtype 5, 1 = vrtype 0, 2-3 = vrtype |
| 845 |
// 8 with that many more bytes than vrtype 0. Note that this is |
| 846 |
// limited to 16 ALT alleles. |
| 847 |
// 10: 2 bits, phased. 0 = vrtype 5, 1 = vrtype 0, 2-3 = vrtype 16 |
| 848 |
// with that many minus 1 bytes beyond vrtype 0. While this is |
| 849 |
// also aimed at the single-sample use case, it technically |
| 850 |
// supports up to 15 always-phased or 7 partially-phased samples. |
| 851 |
// 11: 4 bits, multiallelic + phased. 0 = vrtype 5, 1 = vrtype 0, 2-7 |
| 852 |
// = vrtype 8 with that many bytes beyond vrtype 0, 9 = vrtype 16 |
| 853 |
// phase info requiring just 1 byte, 10-15 = vrtype 24 with (x-7) |
| 854 |
// extra bytes required between multiallelic and phased tracks. |
| 855 |
// 12: 2 bits, dosage, must be single-sample. 0 = vrtype 5, 1 = |
| 856 |
// vrtype 0, 2 = vrtype 0x45 with 2 bytes, 3 = vrtype 0x40 with 3 |
| 857 |
// total bytes. |
| 858 |
// 13: reserved for single-sample multiallelic + dosage. |
| 859 |
// 14: 4 bits, phased + dosage, must be single-sample. 0 and 1 as |
| 860 |
// usual, 3 = vrtype 16 with 1 phaseinfo byte, 4 = vrtype 0x45 |
| 861 |
// with 2 bytes, 5 = vrtype 0x40 with 3 total bytes, 12 = vrtype |
| 862 |
// 0xc5 with 4 total bytes, 13 = vrtype 0xc0 with 5 total bytes, |
| 863 |
// 15 = vrtype 0xe0 with 6 total bytes |
| 864 |
// 15: reserved for single-sample multiallelic + phased dosage. |
| 865 |
// bits 4-5: allele count storage (00 = unstored, 01-11 = bytes per ct) |
| 866 |
// bits 6-7: nonref flags info (00 = unstored, 01 = all ref/alt, 10 = |
| 867 |
// never ref/alt, 11 = explicitly stored) |
| 868 |
// Bits 0-5 do not apply to the fixed-length modes (currently 0x02-0x04) |
| 869 |
// and should be zeroed out in that case. |
| 870 |
// |
| 871 |
// 4. If mode 0x10/0x11, |
| 872 |
// a. Array of 8-byte fpos values for the first variant in each vblock. |
| 873 |
// (Note that this suggests a way to support in-place insertions: some |
| 874 |
// unused space can be left between the vblocks.) |
| 875 |
// b. Sequence of header blocks, each containing information about |
| 876 |
// kPglVblockSize variants (except the last may be shorter). All values |
| 877 |
// are known-width, to allow e.g. plink2 --make-pgen/--pmerge to compress |
| 878 |
// all variant records first, then fseek to the beginning of the output |
| 879 |
// file and write the header. |
| 880 |
// i. array of 4-bit or 1-byte vrtypes. |
| 881 |
// ii. array of variant record lengths (each occupying vrec_len_byte_ct |
| 882 |
// bytes, or 2-4 bits). |
| 883 |
// iii. if bits 4-5 of {3c} aren't 00, array of alt allele counts. |
| 884 |
// iv. nonref flags info, if explicitly stored |
| 885 |
// (this representation allows more efficient random access) |
| 886 |
// If mode 0x02-0x04, and nonref flags info explicitly stored, just that |
| 887 |
// bitarray. |
| 888 |
// |
| 889 |
// 5. The variant records. See below for details. |
| 890 |
|
| 891 |
// Difflist format: |
| 892 |
// a. <difflist_len VINT> |
| 893 |
// If difflist_len is zero, that's it. Otherwise, the difflist is organized |
| 894 |
// into 64-element groups (the last group will usually be smaller), to make |
| 895 |
// extraction of e.g. a single sample less painful. Note that with 20k |
| 896 |
// samples, a difflist is space-saving even with MAF 5%: |
| 897 |
// ~1/400 hom alt + ~38/400 het = (~39/400) * 20k |
| 898 |
// = ~1950 sample IDs. |
| 899 |
// that's 31 groups, requiring about 2 + 62 + 30 + 488 + 1919 = 2501 bytes |
| 900 |
// (can be slightly higher since a few ID deltas may be larger than 127); |
| 901 |
// uncompressed storage requires 5000 bytes. |
| 902 |
// b. <array of group start sample IDs, each of sample_id_byte_ct> |
| 903 |
// c. <array of 1-byte <delta segment lengths minus 63>, with last entry |
| 904 |
// omitted> |
| 905 |
// d. <optional payload of fixed-width genotype values> |
| 906 |
// (in retrospect, it might have been better to position this after (e) |
| 907 |
// to avoid entanglement with the packed-bitarray definition, oh well...) |
| 908 |
// e. one "delta segment"/group: <array of <group size - 1> VINT values, |
| 909 |
// each indicating the difference between the current and previous sample |
| 910 |
// IDs; i.e. value is 1 for two consecutive samples> |
| 911 |
|
| 912 |
// Variant record type ('vrtype') coding: |
| 913 |
// bits 0-2: |
| 914 |
// 000 = Simple 2-bit encoding. |
| 915 |
// 100, 110, 111 = Simple difflist. Low two bits store the base value. (101 |
| 916 |
// isn't here since Hardy-Weinberg equilibrium prevents |
| 917 |
// all het ref/alt from arising much in practice, outside of |
| 918 |
// alignment/variant-calling technical artifacts that should |
| 919 |
// be removed.) |
| 920 |
// 010 = Differences-from-earlier-variant encoding ("LD compression"). The |
| 921 |
// last variant without this type of encoding is the base. |
| 922 |
// To simplify random access logic, the first variant in each vblock is |
| 923 |
// prohibited from using this encoding. |
| 924 |
// 011 = Inverted differences-from-earlier-variant encoding. (This covers |
| 925 |
// the case where a reference allele is 'wrong'.) When decoding, the |
| 926 |
// difflist should be processed first, then the entire genovec should |
| 927 |
// be flipped. |
| 928 |
// 001 = 1-bit + difflist representation. Suppose most calls are hom ref or |
| 929 |
// het (e.g. a 20% MAF variant with ~4% hom alt1, ~36% het ref/alt1, |
| 930 |
// ~64% hom ref), then the main datatrack has just the low bits of the |
| 931 |
// usual 2-bit codes. This is followed by a difflist containing the |
| 932 |
// hom alt1 and missing genotypes. |
| 933 |
// The main datatrack is preceded by a single byte indicating what |
| 934 |
// the two common values are: 2 low bits = <set value - unset value>, |
| 935 |
// next 2 bits = unset value (6 possibilities). Top 4 bits are |
| 936 |
// reserved. |
| 937 |
// 101 = All alleles are reference, no missing data. The main datatrack is |
| 938 |
// empty in this case. Although this saves only 1 byte per variant |
| 939 |
// over vrtype 100, this actually makes a huge difference for |
| 940 |
// single-sample files. |
| 941 |
// Since this was not defined until mid-2019, the standard plink2 |
| 942 |
// alpha-test binaries will not use this encoding. However, |
| 943 |
// alpha-2-final and later binaries interpret this encoding correctly. |
| 944 |
// If your workflow makes heavy use of single-sample .pgen files, you |
| 945 |
// can add -DFUTURE_ENCODER during compilation to unlock this feature. |
| 946 |
// |
| 947 |
// bit 3: multiallelic hardcalls present with alt2/alt3/... present? If yes, |
| 948 |
// auxiliary data track #1 disambiguates the 0b01 (ref/altx) and 0b10 |
| 949 |
// (altx/alty, x may equal y) hardcalls. This contains a format byte, |
| 950 |
// followed by a list of ref/altx patches, then a list of altx/alty |
| 951 |
// patches. All unpatched genotypes are ref/alt1 or alt1/alt1. |
| 952 |
// The bottom 4 bits of the format byte describe how the ref/altx patch |
| 953 |
// set is stored. |
| 954 |
// 0 = Starts with a bitarray with <total ref/altx count> bits (divide by 8 |
| 955 |
// and round up to get byte count of this component; any trailing bits in |
| 956 |
// the last byte must be 0), where each set bit corresponds to presence |
| 957 |
// of a rarealt (i.e. alt2/alt3/...). |
| 958 |
// ExpandBytearr(aux1_first_quarter, all_01, raw_sample_ctl, ct_01, 0, |
| 959 |
// patch_01); |
| 960 |
// can be used to convert this into a set of sample IDs, though we may |
| 961 |
// want to avoid an intermediate unpacking step in practice. Note that |
| 962 |
// when we're passing in sample_ct < raw_sample_ct and the main datatrack |
| 963 |
// is LD-compressed, we'd like ldbase_raw_genovec to be cached. |
| 964 |
// This is followed by a packed array of fixed-width <allele idx - 2> |
| 965 |
// values, where the width depends on the total number of alt alleles. |
| 966 |
// 2 alts: width ZERO. All set bits in the first bitarray correspond |
| 967 |
// to ref/alt2. |
| 968 |
// 3 alts: width 1 bit. Set bits correspond to ref/alt3, clear bits |
| 969 |
// correspond to ref/alt2. |
| 970 |
// 4-5 alts: width 2 bits. 0b00 corresponds to ref/alt2, 0b01 = |
| 971 |
// ref/alt3, 0b10 = ref/alt4, etc. |
| 972 |
// 6-17 alts: width 4 bits. |
| 973 |
// 18-257 alts: width 8 bits. |
| 974 |
// 258-65537 alts: width 16 bits. |
| 975 |
// 65538-16777215 alts: width 24 bits. Reasonable to prohibit more than |
| 976 |
// 2^24 - 1 = 16777215, since variant records are |
| 977 |
// limited to 4 GiB. I can imagine some |
| 978 |
// applications of >65534 in highly variable |
| 979 |
// regions, though, and it doesn't actually cost us |
| 980 |
// anything to define a way to represent it. (A |
| 981 |
// plink2 binary compiled with AlleleCode typedef'd |
| 982 |
// as uint32_t will run more slowly, of course, but |
| 983 |
// most binaries will not be compiled that way.) |
| 984 |
// 1 = Same as mode 0, except the initial bitarray is replaced by a difflist |
| 985 |
// with sample IDs. (We could make that piece somewhat smaller by |
| 986 |
// storing 0-based ref/altx indexes instead, but I'm pretty sure that |
| 987 |
// isn't worth the performance penalty of requiring all_01 and more |
| 988 |
// complicated unpacking. Though we'll need to peek at aux1[0] before |
| 989 |
// decompressing the main datatrack to exploit this.) |
| 990 |
// 15 = Empty patch set. Might remove this (storing this as mode 1 just |
| 991 |
// takes 1 more byte), but it may enable some relevant performance |
| 992 |
// optimizations. |
| 993 |
// 2-14 are reserved for future use. We don't define an efficient way to |
| 994 |
// represent a variant that e.g. has more alt2s than alt1s for now, since alt |
| 995 |
// alleles will usually be sorted in order of decreasing frequency, but maybe |
| 996 |
// this will matter in the future. |
| 997 |
// |
| 998 |
// The top 4 bits describe how the altx/alty patch set is stored. 0/1/15 |
| 999 |
// have the same meaning as they do for the ref/altx patch set; the only |
| 1000 |
// thing that changes is the format of the packed array of values at the end. |
| 1001 |
// 2 alts: width 1. This is treated as a special case. Set bits correspond |
| 1002 |
// to alt2/alt2, clear = alt1/alt2. |
| 1003 |
// 3-4 alts: width 2+2 bits. Each stores <allele idx - 1>, with the smaller |
| 1004 |
// number in the lower bits. E.g. alt1/alt2 is stored as 0b0100; |
| 1005 |
// alt3/alt3 is stored as 0b1010. |
| 1006 |
// 5-16 alts: width 4+4 bits. |
| 1007 |
// 17-256 alts: width 8+8 bits. |
| 1008 |
// 257-65536 alts: width 16+16 bits. |
| 1009 |
// 65537-16777215 alts: width 24+24 bits. |
| 1010 |
// |
| 1011 |
// bit 4: hardcall phased? If yes, auxiliary data track #2 contains phasing |
| 1012 |
// information for heterozygous calls. |
| 1013 |
// The first *bit* of the track indicates whether an explicit |
| 1014 |
// "phasepresent" bitarray is stored. If it's set, the next het_ct bits |
| 1015 |
// are 1-bit values, where 0 = no phasing info known, and 1 = phasing |
| 1016 |
// info present. If it's unset, phasing info is present for every het |
| 1017 |
// call. |
| 1018 |
// This is followed by a "phaseinfo" bitarray, where 0 = unswapped, |
| 1019 |
// 1 = swapped (e.g. "1|0" in VCF). |
| 1020 |
// This track is normally unpacked into fixed-size bitarrays when |
| 1021 |
// loaded, but a raw mode is also provided (which doesn't support |
| 1022 |
// subsetting). |
| 1023 |
// By default, entire chromosomes/contigs are assumed to be phased |
| 1024 |
// together. (Todo: support contiguous phase sets.) |
| 1025 |
// |
| 1026 |
// bits 5-6: |
| 1027 |
// 00 = no dosage data. |
| 1028 |
// 01 = dosage list. Auxiliary data track #3 contains a delta-encoded list |
| 1029 |
// of sample IDs (like a difflist, but with no genotypes). Track #4 |
| 1030 |
// contains a 16-bit (0..2^15; 65535 missing value is only permitted in |
| 1031 |
// unconditional-dosage case) value expressing the sum of all alt allele |
| 1032 |
// dosages. (Yes, making this the ref allele dosage would have been a |
| 1033 |
// bit cleaner, but it's too late now.) |
| 1034 |
// If the variant is multiallelic, nonzero alt2/alt3/... dosages are |
| 1035 |
// likely to be sparse. So, |
| 1036 |
// - track #5 contains a delta-encoded list describing which |
| 1037 |
// <sample_uidx x rarealt dosage> entries are nonzero, where rarealt |
| 1038 |
// index is in the lowest bits and sample_uidx can be computed via |
| 1039 |
// right-shift (to avoid integer-division headaches, especially |
| 1040 |
// important since indexes in this list can be larger than 2^32). |
| 1041 |
// We use sample_uidx here to make subsetting less painful. |
| 1042 |
// Since each nonzero dosage value requires 16 bits, and |
| 1043 |
// delta-encoding of a dense list adds less than 9 bits per entry, |
| 1044 |
// there isn't much point in supporting a dense bitarray mode here. |
| 1045 |
// To keep memory requirements sane for biobank-scale datasets when |
| 1046 |
// the number of alt alleles is very large, each sample is prohibited |
| 1047 |
// from having more than 255 nonzero allele dosages (this makes no |
| 1048 |
// difference when sizeof(AlleleCode) == 1, but it may matter later). |
| 1049 |
// - track #6 contains the rarealt nonzero dosage values. |
| 1050 |
// Note that this and the other dosage modes are in ADDITION to |
| 1051 |
// hardcalls. This increases filesize by up to 12.5%, but makes the |
| 1052 |
// reader substantially simpler; --hard-call-threshold logic is nicely |
| 1053 |
// compartmentalized. |
| 1054 |
// 10 = unconditional dosage (just track #4). |
| 1055 |
// 11 = dosage bitarray. In this case, auxiliary data track #3 contains an |
| 1056 |
// array of 1-bit values indicating which samples have dosages. If the |
| 1057 |
// variant is multiallelic, tracks #5 and 6 are as described above. |
| 1058 |
// bgen 1.2 format no longer permits fractional missingness, so no good |
| 1059 |
// reason for us to support it. |
| 1060 |
// Considered putting *all* dosage data at the end of the file (like I will |
| 1061 |
// do for phase set info); this could actually be worthwhile for |
| 1062 |
// unconditional dosages, but it doesn't work well when only some samples |
| 1063 |
// have dosage data. |
| 1064 |
// bit 7: some dosages explicitly phased? If yes, and dosages are not |
| 1065 |
// unconditionally present, auxiliary data track #7 is a bitarray of |
| 1066 |
// length dosage_ct indicating whether dphase_delta exists for that |
| 1067 |
// sample. Note that this is technically independent of bit 4; either |
| 1068 |
// can be set without the other. (However, in practice, bit 4 is almost |
| 1069 |
// always set when bit 7 is, since that enables more efficient storage |
| 1070 |
// of 0|0.99, 1|0.02, and similar pairs.) |
| 1071 |
// When phased dosages are present, track #8 contains values |
| 1072 |
// representing <(hap1 alt prob) - (hap2 alt prob)>, etc., where the |
| 1073 |
// underlying values are represented in [0..16384] (so the signed |
| 1074 |
// difference is in [-16384..16384]). Track #4 contains the |
| 1075 |
// corresponding sums; parity does NOT need to match (necessary to allow |
| 1076 |
// this since we treat omitted values as zero; and since we are allowing |
| 1077 |
// it, no point in making parity match in other situations either). In |
| 1078 |
// fixed-width case, -32768 should be stored in track #8 when the entire |
| 1079 |
// call is missing, while 0 and missing-phase are considered synonymous. |
| 1080 |
// In the biallelic case, if a hardcall is phased, a dosage is present, |
| 1081 |
// and no explicit dosage-phase is, we define it to mean the unique |
| 1082 |
// dphase_delta sequence with maximal absolute value, and --make-pgen |
| 1083 |
// takes advantage of it. This definition technically works for |
| 1084 |
// triallelic variants as well, but it breaks down with 4 alleles, so we |
| 1085 |
// prohibit hardcall-phase + dosage + no-dosage-phase with more than 2 |
| 1086 |
// alleles. |
| 1087 |
// In the multiallelic case, tracks #9 and #10 are analogous to #5 and |
| 1088 |
// #6. |
| 1089 |
// |
| 1090 |
// Representation of variable ploidy (MT) was considered, but rejected since |
| 1091 |
// dosages should be at least as appropriate for MT. |
| 1092 |
// Oxford/VCF-style storage of separate probabilities for every possible |
| 1093 |
// genotype (e.g. P(AA), P(AB), P(BB) instead of just 2P(AA) + P(AB) and |
| 1094 |
// 2P(BB) + P(AB)) is tentatively rejected due to (i) lack of relevance to |
| 1095 |
// PLINK's analysis functions and (ii) high storage cost where we can afford it |
| 1096 |
// least. In principle, this is subject to reevaluation if (i) changes, but |
| 1097 |
// given the poor interaction with phased dosages, it's probably better to just |
| 1098 |
// think of them as permanently outside PLINK's scope. |
| 1099 |
|
| 1100 |
// maximum prime < 2^32 is 4294967291; quadratic hashing guarantee breaks down |
| 1101 |
// past that divided by 2. |
| 1102 |
CONSTI32(kPglMaxVariantCt, 0x7ffffffd); |
| 1103 |
|
| 1104 |
CONSTI32(kPglMaxSampleCt, 0x7ffffffe); |
| 1105 |
|
| 1106 |
uint64_t PglHeaderBaseEndOffset(uint32_t variant_ct, uintptr_t vrec_len_byte_ct, uint32_t phase_or_dosage_present, uint32_t explicit_nonref_flags); |
| 1107 |
|
| 1108 |
// Current pgen-extension API assumes .pgen extension bodies fit comfortably in |
| 1109 |
// memory. |
| 1110 |
// It's easy to imagine a useful extension that breaks this assumption, e.g. |
| 1111 |
// storage of VCF FORMAT/GQ and FORMAT/DP. There's an escape hatch -- |
| 1112 |
// extensions are allowed to refer to additional files. We'll see whether that |
| 1113 |
// proves to be enough. |
| 1114 |
typedef struct PgenExtensionLlStruct { |
| 1115 |
struct PgenExtensionLlStruct* next; |
| 1116 |
uint64_t size; |
| 1117 |
unsigned char* contents; |
| 1118 |
uint8_t type_idx; |
| 1119 |
} PgenExtensionLl; |
| 1120 |
|
| 1121 |
#ifdef __cplusplus |
| 1122 |
} // namespace plink2 |
| 1123 |
#endif |
| 1124 |
|
| 1125 |
#endif // __PGENLIB_MISC_H__ |