| 1 |
#ifndef __PLINK2_COMMON_H__ |
| 2 |
#define __PLINK2_COMMON_H__ |
| 3 |
|
| 4 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 5 |
// Christopher Chang. |
| 6 |
// |
| 7 |
// This program 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 program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
|
| 21 |
// Resources needed across a variety of plink2 modules involving |
| 22 |
// plink2-specific constructs (e.g. chr_info_t, variable-length sample IDs, |
| 23 |
// 2-bit genotypes). More generic library code has been moved to plink2_base |
| 24 |
// and plink2_cmdline. |
| 25 |
|
| 26 |
#include "include/pgenlib_read.h" |
| 27 |
#include "plink2_decompress.h" |
| 28 |
|
| 29 |
#ifdef __cplusplus |
| 30 |
namespace plink2 { |
| 31 |
#endif |
| 32 |
|
| 33 |
// Uncomment this if the default contig-count limit of ~65k is too low, and you |
| 34 |
// want to rebuild this program to support ~980k instead. |
| 35 |
// #define HIGH_CONTIG_BUILD |
| 36 |
|
| 37 |
// Uncomment this if the default autosome-pair limit of 95 is too low, and you |
| 38 |
// want to rebuild this program to support 995 instead. |
| 39 |
// #define HIGH_AUTOSOME_NUM_BUILD |
| 40 |
|
| 41 |
#define PROG_NAME_STR "plink2" |
| 42 |
|
| 43 |
extern const double kSmallDoubles[4]; |
| 44 |
|
| 45 |
// Exclude 0x7fffffff, since the half-open interval containing it ends in |
| 46 |
// 0x80000000U or higher. |
| 47 |
CONSTI32(kMaxBp, 0x7ffffffe); |
| 48 |
|
| 49 |
// leave the door semi-open to 32-bit dosages (or 31? 24?) |
| 50 |
// note that u31tod()/u31tof() can't be used on 32-bit dosages |
| 51 |
typedef uint16_t Dosage; |
| 52 |
typedef int16_t SDosage; |
| 53 |
typedef uint32_t DosageProd; |
| 54 |
#define kDosageMax (1U << (8 * sizeof(Dosage) - 1)) |
| 55 |
CONSTI32(kDosageMid, kDosageMax / 2); |
| 56 |
CONSTI32(kDosage4th, kDosageMax / 4); |
| 57 |
CONSTI32(kDosageMissing, kDosageMax * 2 - 1); |
| 58 |
static const double kRecipDosageMax = 0.000030517578125; |
| 59 |
static const double kRecipDosageMid = 0.00006103515625; |
| 60 |
static const double kRecipDosageMidSq = 0.0000000037252902984619140625; |
| 61 |
static const float kRecipDosageMidf = 0.00006103515625; |
| 62 |
CONSTI32(kDosagePerVec, kBytesPerVec / sizeof(Dosage)); |
| 63 |
CONSTI32(kDosagePerCacheline, kCacheline / sizeof(Dosage)); |
| 64 |
|
| 65 |
HEADER_CINLINE uintptr_t DosageCtToVecCt(uintptr_t val) { |
| 66 |
return DivUp(val, kDosagePerVec); |
| 67 |
} |
| 68 |
|
| 69 |
HEADER_CINLINE uintptr_t DosageCtToAlignedWordCt(uintptr_t val) { |
| 70 |
return kWordsPerVec * DosageCtToVecCt(val); |
| 71 |
} |
| 72 |
|
| 73 |
// dosage_int = 0..2 value in 16384ths |
| 74 |
// returns distance from 0.5 or 1.5 in 16384ths, whichever is closer |
| 75 |
HEADER_INLINE uint32_t BiallelicDosageHalfdist(uint32_t dosage_int) { |
| 76 |
const uint32_t dosage_int_rem = dosage_int & (kDosageMid - 1); |
| 77 |
return abs_i32(S_CAST(int32_t, dosage_int_rem) - kDosage4th); |
| 78 |
} |
| 79 |
|
| 80 |
HEADER_INLINE uint32_t HaploidDosageHalfdist(uint32_t dosage_int) { |
| 81 |
return abs_i32(S_CAST(int32_t, dosage_int) - kDosage4th); |
| 82 |
} |
| 83 |
|
| 84 |
HEADER_INLINE uint32_t DosageHetdist(uint32_t dosage_int) { |
| 85 |
return abs_i32(S_CAST(int32_t, dosage_int) - kDosageMid); |
| 86 |
} |
| 87 |
|
| 88 |
HEADER_INLINE uint32_t DosageHomdist(uint32_t dosage_int) { |
| 89 |
// todo: compare vs. branchless |
| 90 |
return (dosage_int > kDosageMid)? (kDosageMax - dosage_int) : dosage_int; |
| 91 |
} |
| 92 |
|
| 93 |
// on 0..32768 scale |
| 94 |
HEADER_INLINE uint32_t DphaseHalfdist(uint32_t dphase_side) { |
| 95 |
return abs_i32(S_CAST(int32_t, dphase_side) - kDosageMid); |
| 96 |
} |
| 97 |
|
| 98 |
// this is a bit arbitrary |
| 99 |
CONSTI32(kMaxPhenoCt, 524287); |
| 100 |
#define MAX_PHENO_CT_STR "524287" |
| 101 |
|
| 102 |
// maximum number of usable cluster computers, this is arbitrary though it |
| 103 |
// shouldn't be larger than 2^32 - 1 |
| 104 |
// (actually, there's an overflow danger: <work units> * parallel_idx may not |
| 105 |
// fit in a uint64 if parallel_tot is too high.) |
| 106 |
CONSTI32(kParallelMax, 32768); |
| 107 |
|
| 108 |
// unnecessary to use e.g. (1LLU << 0), the FLAGSET64 macros should force the |
| 109 |
// integer type to 64-bit. |
| 110 |
FLAGSET64_DEF_START() |
| 111 |
kfMisc0, |
| 112 |
kfMiscAffection01 = (1 << 0), |
| 113 |
kfMiscProhibitExtraChr = (1 << 1), |
| 114 |
kfMiscRealRefAlleles = (1 << 2), |
| 115 |
kfMiscMajRef = (1 << 3), |
| 116 |
kfMiscMajRefForce = (1 << 4), |
| 117 |
kfMiscNonfounders = (1 << 5), |
| 118 |
kfMiscExcludePvarFilterFail = (1 << 6), |
| 119 |
kfMiscAutosomePar = (1 << 7), |
| 120 |
kfMiscAutosomeOnly = (1 << 8), |
| 121 |
kfMiscMergePar = (1 << 9), |
| 122 |
kfMiscPhenoColNums = (1 << 10), |
| 123 |
kfMiscCovarColNums = (1 << 11), |
| 124 |
kfMiscHweMidp = (1 << 12), |
| 125 |
kfMiscHweKeepFewhet = (1 << 13), |
| 126 |
kfMiscWriteSnplistZs = (1 << 14), |
| 127 |
kfMiscWriteSnplistAllowDups = (1 << 15), |
| 128 |
kfMiscGenoDosage = (1 << 16), |
| 129 |
kfMiscGenoHhMissing = (1 << 17), |
| 130 |
kfMiscMindDosage = (1 << 18), |
| 131 |
kfMiscMindHhMissing = (1 << 19), |
| 132 |
kfMiscGenotypingRateDosage = (1 << 20), |
| 133 |
kfMiscSetMissingVarIds = (1 << 21), |
| 134 |
kfMiscChrOverrideCmdline = (1 << 22), |
| 135 |
kfMiscChrOverrideFile = (1 << 23), |
| 136 |
kfMiscNewVarIdOverflowMissing = (1 << 24), |
| 137 |
kfMiscNewVarIdOverflowTruncate = (1 << 25), |
| 138 |
kfMiscRequirePheno = (1 << 26), |
| 139 |
kfMiscRequireCovar = (1 << 27), |
| 140 |
kfMiscCatPhenoFamily = (1 << 28), |
| 141 |
kfMiscRefAlleleForce = (1 << 29), |
| 142 |
kfMiscAltAlleleForce = (1 << 30), |
| 143 |
kfMiscMergeX = (1U << 31), |
| 144 |
kfMiscNoIdHeader = (1LLU << 32), |
| 145 |
kfMiscNoIdHeaderIidOnly = (1LLU << 33), |
| 146 |
kfMiscBiallelicOnly = (1LLU << 34), |
| 147 |
kfMiscBiallelicOnlyStrict = (1LLU << 35), |
| 148 |
kfMiscBiallelicOnlyList = (1LLU << 36), |
| 149 |
kfMiscStrictSid0 = (1LLU << 37), |
| 150 |
kfMiscAllowBadFreqs = (1LLU << 38), |
| 151 |
kfMiscIidSid = (1LLU << 39), |
| 152 |
kfMiscPhenoIidOnly = (1LLU << 40), |
| 153 |
kfMiscCovarIidOnly = (1LLU << 41), |
| 154 |
kfMiscAllowBadLd = (1LLU << 42), |
| 155 |
kfMiscErrorOnFreqCalc = (1LLU << 43), |
| 156 |
kfMiscNoCategorical = (1LLU << 44), |
| 157 |
kfMiscAcFounders = (1LLU << 45), |
| 158 |
kfMiscMakeFoundersFirst = (1LLU << 46), |
| 159 |
kfMiscMakeFoundersNotfirst = (1LLU << 47), |
| 160 |
kfMiscMakeFoundersRequire2Missing = (1LLU << 48), |
| 161 |
kfMiscYNosexMissingStats = (1LLU << 49), |
| 162 |
kfMiscNeg9PhenoReallyMissing = (1LLU << 50), |
| 163 |
kfMiscAlt1Allele = (1LLU << 51) |
| 164 |
FLAGSET64_DEF_END(MiscFlags); |
| 165 |
|
| 166 |
FLAGSET64_DEF_START() |
| 167 |
kfExportf0, |
| 168 |
kfExportf01 = (1 << 0), |
| 169 |
kfExportf12 = (1 << 1), |
| 170 |
kfExportfSpaces = (1 << 2), |
| 171 |
kfExportfRefFirst = (1 << 3), |
| 172 |
kfExportf23 = (1 << 4), |
| 173 |
kfExportfA = (1 << 5), |
| 174 |
kfExportfAv = (1 << 6), |
| 175 |
kfExportfAD = (1 << 7), |
| 176 |
kfExportfBcf42 = (1 << 8), |
| 177 |
kfExportfBcf43 = (1 << 9), |
| 178 |
kfExportfBcf = kfExportfBcf42 | kfExportfBcf43, |
| 179 |
kfExportfBeagle = (1 << 10), |
| 180 |
kfExportfBeagleNomap = (1 << 11), |
| 181 |
kfExportfBgen11 = (1 << 12), |
| 182 |
kfExportfBgen12 = (1 << 13), |
| 183 |
kfExportfBgen13 = (1 << 14), |
| 184 |
kfExportfBimbam = (1 << 15), |
| 185 |
kfExportfBimbam1chr = (1 << 16), |
| 186 |
kfExportfFastphase = (1 << 17), |
| 187 |
kfExportfFastphase1chr = (1 << 18), |
| 188 |
kfExportfHaps = (1 << 19), |
| 189 |
kfExportfHapsLegend = (1 << 20), |
| 190 |
kfExportfHv = (1 << 21), |
| 191 |
kfExportfHv1chr = (1 << 22), |
| 192 |
kfExportfIndMajorBed = (1 << 23), |
| 193 |
kfExportfLgen = (1 << 24), |
| 194 |
kfExportfLgenRef = (1 << 25), |
| 195 |
kfExportfList = (1 << 26), |
| 196 |
kfExportfRlist = (1 << 27), |
| 197 |
kfExportfOxGenV1 = (1 << 28), |
| 198 |
kfExportfOxGenV2 = (1 << 29), |
| 199 |
kfExportfOxGen = kfExportfOxGenV1 | kfExportfOxGenV2, |
| 200 |
kfExportfPed = (1 << 30), |
| 201 |
kfExportfCompound = (1U << 31), |
| 202 |
kfExportfPhylip = (1LLU << 32), |
| 203 |
kfExportfPhylipPhased = (1LLU << 33), |
| 204 |
kfExportfStructure = (1LLU << 34), |
| 205 |
kfExportfTped = (1LLU << 35), |
| 206 |
kfExportfVcf42 = (1LLU << 36), |
| 207 |
kfExportfVcf43 = (1LLU << 37), |
| 208 |
kfExportfVcf = kfExportfVcf42 | kfExportfVcf43, |
| 209 |
kfExportfTypemask = (2LLU * kfExportfVcf43) - kfExportf23, |
| 210 |
kfExportfIncludeAlt = (1LLU << 38), |
| 211 |
kfExportfBgz = (1LLU << 39), |
| 212 |
kfExportfOmitNonmaleY = (1LLU << 40), |
| 213 |
kfExportfSampleV2 = (1LLU << 41), |
| 214 |
kfExportfBgenOmitSampleIdBlock = (1LLU << 42), |
| 215 |
kfExportfPhylipUsedSites = (1LLU << 43) |
| 216 |
FLAGSET64_DEF_END(ExportfFlags); |
| 217 |
|
| 218 |
FLAGSET_DEF_START() |
| 219 |
kfInfo0, |
| 220 |
kfInfoPrFlagPresent = (1 << 0), |
| 221 |
kfInfoPrNonflagPresent = (1 << 1), |
| 222 |
kfInfoNonprPresent = (1 << 2), |
| 223 |
kfInfoAll = ((kfInfoNonprPresent * 2) - kfInfoPrFlagPresent), |
| 224 |
// this is set in the .bim case |
| 225 |
kfInfoPrNonrefDefault = (1 << 3), |
| 226 |
FLAGSET_DEF_END(InfoFlags); |
| 227 |
|
| 228 |
FLAGSET_DEF_START() |
| 229 |
kfPvarPsam0, |
| 230 |
kfPvarZs = (1 << 0), |
| 231 |
|
| 232 |
kfPvarColXheader = (1 << 1), |
| 233 |
kfPvarColVcfheader = (1 << 2), |
| 234 |
kfPvarColMaybequal = (1 << 3), |
| 235 |
kfPvarColQual = (1 << 4), |
| 236 |
kfPvarColMaybefilter = (1 << 5), |
| 237 |
kfPvarColFilter = (1 << 6), |
| 238 |
kfPvarColMaybeinfo = (1 << 7), |
| 239 |
kfPvarColInfo = (1 << 8), |
| 240 |
kfPvarColXinfo = (kfPvarColInfo * 2) - kfPvarColMaybeinfo, |
| 241 |
kfPvarColMaybecm = (1 << 9), |
| 242 |
kfPvarColCm = (1 << 10), |
| 243 |
kfPvarColDefault = (kfPvarColXheader | kfPvarColMaybequal | kfPvarColMaybefilter | kfPvarColMaybeinfo | kfPvarColMaybecm), |
| 244 |
kfPvarColAll = ((kfPvarColCm * 2) - kfPvarColXheader), |
| 245 |
kfPsamColMaybefid = (1 << 11), |
| 246 |
kfPsamColFid = (1 << 12), |
| 247 |
kfPsamColMaybesid = (1 << 13), |
| 248 |
kfPsamColSid = (1 << 14), |
| 249 |
kfPsamColMaybeparents = (1 << 15), |
| 250 |
kfPsamColParents = (1 << 16), |
| 251 |
kfPsamColSex = (1 << 17), |
| 252 |
kfPsamColPheno1 = (1 << 18), |
| 253 |
kfPsamColPhenos = (1 << 19), |
| 254 |
kfPsamColDefault = (kfPsamColMaybefid | kfPsamColMaybesid | kfPsamColMaybeparents | kfPsamColSex | kfPsamColPhenos), |
| 255 |
kfPsamColAll = ((kfPsamColPhenos * 2) - kfPsamColMaybefid) |
| 256 |
FLAGSET_DEF_END(PvarPsamFlags); |
| 257 |
|
| 258 |
// may want to rename FidPresent to FidMayBePresent |
| 259 |
FLAGSET_DEF_START() |
| 260 |
kfSampleId0, |
| 261 |
kfSampleIdFidPresent = (1 << 0), |
| 262 |
kfSampleIdParentsPresent = (1 << 1), |
| 263 |
kfSampleIdNoIdHeader = (1 << 2), |
| 264 |
kfSampleIdNoIdHeaderIidOnly = (1 << 3), |
| 265 |
kfSampleIdStrictSid0 = (1 << 4) |
| 266 |
FLAGSET_DEF_END(SampleIdFlags); |
| 267 |
|
| 268 |
// These structs are small enough and ownership of the pointed-to arrays is |
| 269 |
// generally clear enough that the noncopyable annotation is just intended to |
| 270 |
// be a tripwire. |
| 271 |
typedef struct SampleIdInfoStruct { |
| 272 |
NONCOPYABLE(SampleIdInfoStruct); |
| 273 |
char* sample_ids; |
| 274 |
char* sids; |
| 275 |
uintptr_t max_sample_id_blen; // FID+IID only, does not count SID |
| 276 |
uintptr_t max_sid_blen; |
| 277 |
SampleIdFlags flags; |
| 278 |
} SampleIdInfo; |
| 279 |
|
| 280 |
typedef struct ParentalIdInfoStruct { |
| 281 |
NONCOPYABLE(ParentalIdInfoStruct); |
| 282 |
char* paternal_ids; |
| 283 |
char* maternal_ids; |
| 284 |
uintptr_t max_paternal_id_blen; |
| 285 |
uintptr_t max_maternal_id_blen; |
| 286 |
} ParentalIdInfo; |
| 287 |
|
| 288 |
typedef struct PedigreeIdInfoStruct { |
| 289 |
SampleIdInfo sii; |
| 290 |
ParentalIdInfo parental_id_info; |
| 291 |
} PedigreeIdInfo; |
| 292 |
|
| 293 |
typedef struct APermStruct { |
| 294 |
uint32_t min; |
| 295 |
uint32_t max; |
| 296 |
double alpha; |
| 297 |
double beta; |
| 298 |
double init_interval; |
| 299 |
double interval_slope; |
| 300 |
} APerm; |
| 301 |
|
| 302 |
// (2^31 - 1000001) / 2 |
| 303 |
CONSTI32(kApermMax, 1073241823); |
| 304 |
|
| 305 |
typedef struct TwoColParamsStruct { |
| 306 |
NONCOPYABLE(TwoColParamsStruct); |
| 307 |
uint32_t colx; |
| 308 |
uint32_t colid; |
| 309 |
uint32_t skip_ct; |
| 310 |
char skipchar; |
| 311 |
char fname[]; |
| 312 |
} TwoColParams; |
| 313 |
|
| 314 |
void InitPedigreeIdInfo(MiscFlags misc_flags, PedigreeIdInfo* piip); |
| 315 |
|
| 316 |
// no CleanupPedigreeIdInfo function since LoadPsam() allocates the arrays in |
| 317 |
// bigstack. |
| 318 |
|
| 319 |
|
| 320 |
HEADER_INLINE BoolErr bigstack_alloc_ac(uintptr_t ct, AlleleCode** allele_arr_ptr) { |
| 321 |
*allele_arr_ptr = S_CAST(AlleleCode*, bigstack_alloc(ct * sizeof(AlleleCode))); |
| 322 |
return !(*allele_arr_ptr); |
| 323 |
} |
| 324 |
|
| 325 |
HEADER_INLINE BoolErr bigstack_alloc_kac(uintptr_t ct, const AlleleCode** allele_arr_ptr) { |
| 326 |
*allele_arr_ptr = S_CAST(const AlleleCode*, bigstack_alloc(ct * sizeof(AlleleCode))); |
| 327 |
return !(*allele_arr_ptr); |
| 328 |
} |
| 329 |
|
| 330 |
HEADER_INLINE BoolErr bigstack_alloc_dosage(uintptr_t ct, Dosage** dosage_arr_ptr) { |
| 331 |
*dosage_arr_ptr = S_CAST(Dosage*, bigstack_alloc(ct * sizeof(Dosage))); |
| 332 |
return !(*dosage_arr_ptr); |
| 333 |
} |
| 334 |
|
| 335 |
HEADER_INLINE BoolErr bigstack_alloc_dphase(uintptr_t ct, SDosage** dphase_arr_ptr) { |
| 336 |
*dphase_arr_ptr = S_CAST(SDosage*, bigstack_alloc(ct * sizeof(SDosage))); |
| 337 |
return !(*dphase_arr_ptr); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
HEADER_INLINE BoolErr bigstack_alloc_acp(uintptr_t ct, AlleleCode*** acp_arr_ptr) { |
| 342 |
*acp_arr_ptr = S_CAST(AlleleCode**, bigstack_alloc(ct * sizeof(intptr_t))); |
| 343 |
return !(*acp_arr_ptr); |
| 344 |
} |
| 345 |
|
| 346 |
HEADER_INLINE BoolErr bigstack_alloc_dosagep(uintptr_t ct, Dosage*** dosagep_arr_ptr) { |
| 347 |
*dosagep_arr_ptr = S_CAST(Dosage**, bigstack_alloc(ct * sizeof(intptr_t))); |
| 348 |
return !(*dosagep_arr_ptr); |
| 349 |
} |
| 350 |
|
| 351 |
HEADER_INLINE BoolErr bigstack_alloc_dphasep(uintptr_t ct, SDosage*** dphasep_arr_ptr) { |
| 352 |
*dphasep_arr_ptr = S_CAST(SDosage**, bigstack_alloc(ct * sizeof(intptr_t))); |
| 353 |
return !(*dphasep_arr_ptr); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
HEADER_INLINE BoolErr bigstack_allocv_ac(uintptr_t ct, AlleleCode** allele_arr_ptr) { |
| 358 |
*allele_arr_ptr = S_CAST(AlleleCode*, bigstack_allocv(ct * sizeof(AlleleCode))); |
| 359 |
return !(*allele_arr_ptr); |
| 360 |
} |
| 361 |
|
| 362 |
HEADER_INLINE BoolErr bigstack_allocv_dosage(uintptr_t ct, Dosage** dosage_arr_ptr) { |
| 363 |
*dosage_arr_ptr = S_CAST(Dosage*, bigstack_allocv(ct * sizeof(Dosage))); |
| 364 |
return !(*dosage_arr_ptr); |
| 365 |
} |
| 366 |
|
| 367 |
HEADER_INLINE BoolErr bigstack_allocv_dphase(uintptr_t ct, SDosage** dphase_arr_ptr) { |
| 368 |
*dphase_arr_ptr = S_CAST(SDosage*, bigstack_allocv(ct * sizeof(SDosage))); |
| 369 |
return !(*dphase_arr_ptr); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
HEADER_INLINE BoolErr bigstack_end_alloc_ac(uintptr_t ct, AlleleCode** allele_arr_ptr) { |
| 374 |
*allele_arr_ptr = S_CAST(AlleleCode*, bigstack_end_alloc(ct * sizeof(AlleleCode))); |
| 375 |
return !(*allele_arr_ptr); |
| 376 |
} |
| 377 |
|
| 378 |
HEADER_INLINE BoolErr bigstack_end_alloc_dosage(uintptr_t ct, Dosage** dosage_arr_ptr) { |
| 379 |
*dosage_arr_ptr = S_CAST(Dosage*, bigstack_end_alloc(ct * sizeof(Dosage))); |
| 380 |
return !(*dosage_arr_ptr); |
| 381 |
} |
| 382 |
|
| 383 |
HEADER_INLINE BoolErr bigstack_end_alloc_dphase(uintptr_t ct, SDosage** dphase_arr_ptr) { |
| 384 |
*dphase_arr_ptr = S_CAST(SDosage*, bigstack_end_alloc(ct * sizeof(SDosage))); |
| 385 |
return !(*dphase_arr_ptr); |
| 386 |
} |
| 387 |
|
| 388 |
BoolErr BigstackAllocPgv(uint32_t sample_ct, uint32_t multiallelic_needed, PgenGlobalFlags gflags, PgenVariant* pgvp); |
| 389 |
|
| 390 |
// remainder must be in [1, 16383]. |
| 391 |
char* PrintDosageDecimal(uint32_t remainder, char* start); |
| 392 |
|
| 393 |
// small_dosage must be in [0, kDosageMid * 10 - 1]. |
| 394 |
HEADER_INLINE char* PrintSmallDosage(uint32_t small_dosage, char* start) { |
| 395 |
*start++ = '0' + (small_dosage / kDosageMid); |
| 396 |
const uint32_t remainder = small_dosage % kDosageMid; |
| 397 |
if (!remainder) { |
| 398 |
return start; |
| 399 |
} |
| 400 |
return PrintDosageDecimal(remainder, start); |
| 401 |
} |
| 402 |
|
| 403 |
// 3 decimal places. dosage on /kDosageMax rather than /kDosageMid scale |
| 404 |
// (hence the extra 'd') |
| 405 |
char* ddosagetoa(uint64_t dosage, char* start); |
| 406 |
|
| 407 |
// remainder must be in [1, 32767]. |
| 408 |
char* PrintDdosageDecimal(uint32_t remainder, char* start); |
| 409 |
|
| 410 |
// 5 decimal places. Only used when it is important to be able to reconstruct |
| 411 |
// the exact original value. |
| 412 |
HEADER_INLINE char* ddosagetoa_full(uint64_t dosage, char* start) { |
| 413 |
start = u32toa(dosage / kDosageMax, start); |
| 414 |
const uint32_t remainder = dosage % kDosageMax; |
| 415 |
if (!remainder) { |
| 416 |
return start; |
| 417 |
} |
| 418 |
return PrintDdosageDecimal(remainder, start); |
| 419 |
} |
| 420 |
|
| 421 |
HEADER_INLINE void ZeroDosageArr(uintptr_t entry_ct, Dosage* dosage_arr) { |
| 422 |
memset(dosage_arr, 0, entry_ct * sizeof(Dosage)); |
| 423 |
} |
| 424 |
|
| 425 |
HEADER_INLINE void ZeroDphaseArr(uintptr_t entry_ct, SDosage* dphase_arr) { |
| 426 |
memset(dphase_arr, 0, entry_ct * sizeof(SDosage)); |
| 427 |
} |
| 428 |
|
| 429 |
HEADER_INLINE void SetAllDosageArr(uintptr_t entry_ct, Dosage* dosage_arr) { |
| 430 |
memset(dosage_arr, 255, entry_ct * sizeof(Dosage)); |
| 431 |
} |
| 432 |
|
| 433 |
// Assumes dense_dosage is allocated up to vector boundary. |
| 434 |
void PopulateDenseDosage(const uintptr_t* genoarr, const uintptr_t* dosage_present, const Dosage* dosage_main, uint32_t sample_ct, uint32_t dosage_ct, Dosage* dense_dosage); |
| 435 |
|
| 436 |
void PopulateDenseDosageNonemptySubset(const uintptr_t* sample_include, const uint32_t* sample_include_cumulative_popcounts, const uintptr_t* genoarr, const uintptr_t* dosage_present, const Dosage* dosage_main, uint32_t raw_sample_ct, uint32_t sample_ct, uint32_t dosage_ct, Dosage* dense_dosage, uintptr_t* workspace); |
| 437 |
|
| 438 |
void PopulateRescaledDosage(const uintptr_t* genoarr, const uintptr_t* dosage_present, const Dosage* dosage_main, double slope, double intercept, double missing_val, uint32_t sample_ct, uint32_t dosage_ct, double* expanded_dosages); |
| 439 |
|
| 440 |
void PopulateRescaledDosageF(const uintptr_t* genoarr, const uintptr_t* dosage_present, const Dosage* dosage_main, float slope, float intercept, float missing_val, uint32_t sample_ct, uint32_t dosage_ct, float* expanded_dosages); |
| 441 |
|
| 442 |
void PopulateDenseDphase(const uintptr_t* phasepresent, const uintptr_t* phaseinfo, const uintptr_t* dosage_present, const Dosage* dense_dosage_vec, const uintptr_t* dphase_present, const SDosage* dphase_delta, uint32_t sample_ct, uint32_t phasepresent_ct, uint32_t dosage_ct, uint32_t dphase_ct, SDosage* dense_dphase_delta); |
| 443 |
|
| 444 |
// assumes trailing bits of genoarr are zeroed out |
| 445 |
HEADER_INLINE uint32_t AtLeastOneHetUnsafe(const uintptr_t* genoarr, uint32_t sample_ct) { |
| 446 |
const uint32_t sample_ctl2 = NypCtToWordCt(sample_ct); |
| 447 |
for (uint32_t uii = 0; uii != sample_ctl2; ++uii) { |
| 448 |
const uintptr_t geno_word = genoarr[uii]; |
| 449 |
if (Word01(geno_word)) { |
| 450 |
return 1; |
| 451 |
} |
| 452 |
} |
| 453 |
return 0; |
| 454 |
} |
| 455 |
|
| 456 |
uint32_t AtLeastOneMultiallelicHet(const PgenVariant* pgvp, uint32_t sample_ct); |
| 457 |
|
| 458 |
void SetHetMissing(uintptr_t word_ct, uintptr_t* genovec); |
| 459 |
|
| 460 |
void SetHetMissingCleardosage(uintptr_t word_ct, uintptr_t* genovec, uint32_t* write_dosage_ct_ptr, uintptr_t* dosagepresent, Dosage* dosage_main); |
| 461 |
|
| 462 |
void SetHetMissingKeepdosage(uintptr_t word_ct, uintptr_t* genovec, uint32_t* write_dosage_ct_ptr, uintptr_t* dosagepresent, Dosage* dosage_main); |
| 463 |
|
| 464 |
uint32_t GenoarrCountMissingUnsafe(const uintptr_t* genoarr, uint32_t sample_ct); |
| 465 |
|
| 466 |
uint32_t GenoarrCountMissingSubset(const uintptr_t* genoarr, const uintptr_t* interleaved_vec, uint32_t sample_ct); |
| 467 |
|
| 468 |
uint32_t GenoarrCountMissingInvsubsetUnsafe(const uintptr_t* genoarr, const uintptr_t* exclude_mask, uint32_t sample_ct); |
| 469 |
|
| 470 |
// See also DataFidColIsRequired(), etc. in plink2_data.h, which checks whether |
| 471 |
// at least one remaining value is nonzero. |
| 472 |
HEADER_INLINE uint32_t FidColIsRequired(const SampleIdInfo* siip, uint32_t maybe_modifier) { |
| 473 |
return (maybe_modifier & 2) || ((maybe_modifier & 1) && (siip->flags & kfSampleIdFidPresent)); |
| 474 |
} |
| 475 |
|
| 476 |
HEADER_INLINE uint32_t SidColIsRequired(const char* sids, uint32_t maybe_modifier) { |
| 477 |
return (maybe_modifier & 2) || ((maybe_modifier & 1) && sids); |
| 478 |
} |
| 479 |
|
| 480 |
HEADER_INLINE uint32_t ParentalColsAreRequired(const PedigreeIdInfo* piip, uint32_t maybe_modifier) { |
| 481 |
return (maybe_modifier & 2) || ((maybe_modifier & 1) && (piip->sii.flags & kfSampleIdParentsPresent)); |
| 482 |
} |
| 483 |
|
| 484 |
void CollapsedSampleFmtidInit(const uintptr_t* sample_include, const SampleIdInfo* siip, uint32_t sample_ct, uint32_t include_fid, uint32_t include_sid, uintptr_t max_sample_fmtid_blen, char* collapsed_sample_fmtids_iter); |
| 485 |
|
| 486 |
HEADER_INLINE uintptr_t GetMaxSampleFmtidBlen(const SampleIdInfo* siip, uint32_t include_fid, uint32_t include_sid) { |
| 487 |
uintptr_t max_sid_blen = 0; |
| 488 |
if (include_sid) { |
| 489 |
max_sid_blen = siip->sids? siip->max_sid_blen : 2; |
| 490 |
} |
| 491 |
return siip->max_sample_id_blen + max_sid_blen - 2 * (!include_fid); |
| 492 |
} |
| 493 |
|
| 494 |
BoolErr CollapsedSampleFmtidInitAlloc(const uintptr_t* sample_include, const SampleIdInfo* siip, uint32_t sample_ct, uint32_t include_fid, uint32_t include_sid, char** collapsed_sample_fmtids_ptr, uintptr_t* max_sample_fmtid_blen_ptr); |
| 495 |
|
| 496 |
// Assumes sample_ct > 0. |
| 497 |
uint32_t OnlyOneFid(const uintptr_t* sample_include, const SampleIdInfo* siip, uint32_t sample_ct); |
| 498 |
|
| 499 |
uint32_t GetMajIdxMulti(const double* cur_allele_freqs, uint32_t cur_allele_ct); |
| 500 |
|
| 501 |
HEADER_INLINE uint32_t GetMajIdx(const double* cur_allele_freqs, uint32_t cur_allele_ct) { |
| 502 |
if (cur_allele_freqs[0] >= 0.5) { |
| 503 |
return 0; |
| 504 |
} |
| 505 |
if (cur_allele_ct == 2) { |
| 506 |
return 1; |
| 507 |
} |
| 508 |
return GetMajIdxMulti(cur_allele_freqs, cur_allele_ct); |
| 509 |
} |
| 510 |
|
| 511 |
HEADER_INLINE double GetNonmajFreq(const double* cur_allele_freqs, uint32_t allele_ct) { |
| 512 |
double tot_nonlast_freq = cur_allele_freqs[0]; |
| 513 |
double max_freq = tot_nonlast_freq; |
| 514 |
const uint32_t allele_ct_m1 = allele_ct - 1; |
| 515 |
for (uint32_t allele_idx = 1; allele_idx != allele_ct_m1; ++allele_idx) { |
| 516 |
const double cur_alt_freq = cur_allele_freqs[allele_idx]; |
| 517 |
tot_nonlast_freq += cur_alt_freq; |
| 518 |
if (cur_alt_freq > max_freq) { |
| 519 |
max_freq = cur_alt_freq; |
| 520 |
} |
| 521 |
} |
| 522 |
const double nonmajor_freq = 1.0 - max_freq; |
| 523 |
return MINV(nonmajor_freq, tot_nonlast_freq); |
| 524 |
} |
| 525 |
|
| 526 |
HEADER_INLINE double GetAlleleFreq(const double* cur_allele_freqs, uint32_t allele_idx, uint32_t allele_ct) { |
| 527 |
const uint32_t allele_ct_m1 = allele_ct - 1; |
| 528 |
if (allele_idx < allele_ct_m1) { |
| 529 |
return cur_allele_freqs[allele_idx]; |
| 530 |
} |
| 531 |
double last_freq = 1.0 - cur_allele_freqs[0]; |
| 532 |
for (uint32_t tmp_allele_idx = 1; tmp_allele_idx != allele_ct_m1; ++tmp_allele_idx) { |
| 533 |
last_freq -= cur_allele_freqs[tmp_allele_idx]; |
| 534 |
} |
| 535 |
return MAXV(last_freq, 0.0); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
// Functions with Xid in their name deal with both FID/IID (with a single tab |
| 540 |
// separator) and FID/IID/SID (two tabs) sample IDs. (Missing FID is |
| 541 |
// represented as FID='0'.) |
| 542 |
|
| 543 |
// With no header line, --keep/--remove and the like should interpret a single |
| 544 |
// token as IID (treating FID as 0), and two tokens as FID/IID. --no-id-header |
| 545 |
// does not support IID/SID output, so we don't need to worry about supporting |
| 546 |
// FidIidSidOrIidSid. |
| 547 |
// With a header line, all four {FID present/absent, SID present/absent} |
| 548 |
// combinations are allowed. |
| 549 |
// bugfix (26 Dec 2020): XidRead() now skips the SID column when appropriate, |
| 550 |
// and GetXidColCt() counts it. |
| 551 |
FLAGSET_DEF_START() |
| 552 |
kfXidMode0, |
| 553 |
|
| 554 |
kfXidModeFlagOneCoreTokenOk = (1 << 0), |
| 555 |
kfXidModeFlagNeverFid = (1 << 1), |
| 556 |
kfXidModeFlagSid = (1 << 2), |
| 557 |
|
| 558 |
kfXidModeFlagSkipSid = (1 << 3), |
| 559 |
|
| 560 |
kfXidModeFidIid = 0, |
| 561 |
kfXidModeFidIidOrIid = kfXidModeFlagOneCoreTokenOk, |
| 562 |
kfXidModeIid = (kfXidModeFlagOneCoreTokenOk | kfXidModeFlagNeverFid), |
| 563 |
kfXidModeFidIidSid = kfXidModeFlagSid, |
| 564 |
kfXidModeIidSid = (kfXidModeFlagNeverFid | kfXidModeFlagSid), |
| 565 |
kfXidModeCoreMask = kfXidModeFlagSkipSid - 1 |
| 566 |
FLAGSET_DEF_END(XidMode); |
| 567 |
|
| 568 |
// Assumes fixed-width. Includes skipped SID column if there is one. |
| 569 |
HEADER_INLINE uint32_t GetXidColCt(XidMode xid_mode) { |
| 570 |
const XidMode xid_mode_core = xid_mode & kfXidModeCoreMask; |
| 571 |
return 2 + ((xid_mode >> 3) & 1) + (xid_mode_core == kfXidModeFidIidSid) - (xid_mode_core == kfXidModeIid); |
| 572 |
} |
| 573 |
|
| 574 |
// sample_xid_map allocated on bottom, to play well with --indiv-sort |
| 575 |
PglErr SortedXidboxInitAlloc(const uintptr_t* sample_include, const SampleIdInfo* siip, uint32_t sample_ct, XidMode xid_mode, uint32_t use_nsort, char** sorted_xidbox_ptr, uint32_t** xid_map_ptr, uintptr_t* max_xid_blen_ptr); |
| 576 |
|
| 577 |
PglErr SortedXidboxInitAllocEnd(const uintptr_t* sample_include, const SampleIdInfo* siip, uint32_t sample_ct, XidMode xid_mode, uint32_t use_nsort, char** sorted_xidbox_ptr, uint32_t** xid_map_ptr, uintptr_t* max_xid_blen_ptr); |
| 578 |
|
| 579 |
// returns slen for ID, or 0 on guaranteed mismatch (longer than max_xid_blen) |
| 580 |
// or parse failure (*readpp set to nullptr in latter case). |
| 581 |
uint32_t XidRead(uintptr_t max_xid_blen, uint32_t comma_delim, XidMode xid_mode, const char** read_pp, char* __restrict idbuf); |
| 582 |
|
| 583 |
// returns 1 on missing token *or* if the sample ID is not present. cases can |
| 584 |
// be distinguished by checking whether *read_pp_new == nullptr: if it is, a |
| 585 |
// missing-tokens error should probably be reported. |
| 586 |
// sample_id_map == nullptr is permitted |
| 587 |
// *read_pp is now set to point to the end of IID/SID instead of the beginning |
| 588 |
// of the next token; this is a change from plink 1.9. |
| 589 |
HEADER_INLINE BoolErr SortedXidboxReadFind(const char* __restrict sorted_xidbox, const uint32_t* __restrict xid_map, uintptr_t max_xid_blen, uintptr_t xid_ct, uint32_t comma_delim, XidMode xid_mode, const char** read_pp, uint32_t* sample_uidx_ptr, char* __restrict idbuf) { |
| 590 |
const uint32_t slen_final = XidRead(max_xid_blen, comma_delim, xid_mode, read_pp, idbuf); |
| 591 |
idbuf[slen_final] = '\0'; // needed for some error messages |
| 592 |
if (!slen_final) { |
| 593 |
return 1; |
| 594 |
} |
| 595 |
return SortedIdboxFind(idbuf, sorted_xidbox, xid_map, slen_final, max_xid_blen, xid_ct, sample_uidx_ptr); |
| 596 |
} |
| 597 |
|
| 598 |
// Matches a sample ID *prefix*. Thus, if FID/IID/SID is loaded, but the input |
| 599 |
// file contains just FID/IID, and there are some FID/IID pairs which |
| 600 |
// correspond to multiple samples, this lets you iterate over all of them. |
| 601 |
// (Caller is responsible for looking up xid_map[] to perform xid_idx -> |
| 602 |
// sample_uidx conversions.) |
| 603 |
BoolErr SortedXidboxReadMultifind(const char* __restrict sorted_xidbox, uintptr_t max_xid_blen, uintptr_t xid_ct, uint32_t comma_delim, XidMode xid_mode, const char** read_pp, uint32_t* __restrict xid_idx_start_ptr, uint32_t* __restrict xid_idx_end_ptr, char* __restrict idbuf); |
| 604 |
|
| 605 |
FLAGSET_DEF_START() |
| 606 |
kfXidHeader0, |
| 607 |
|
| 608 |
kfXidHeaderFixedWidth = (1 << 0), |
| 609 |
kfXidHeaderIgnoreSid = (1 << 1), |
| 610 |
kfXidHeaderFixedWidthIgnoreSid = (kfXidHeaderFixedWidth | kfXidHeaderIgnoreSid) |
| 611 |
FLAGSET_DEF_END(XidHeaderFlags); |
| 612 |
|
| 613 |
// - May return kPglRetEof, or other TextStream errors. |
| 614 |
// - line_startp can be nullptr. If it isn't, it's set to the (lstripped) |
| 615 |
// beginning of the current line. |
| 616 |
// - line_idx must be zero unless initial lines were skipped. |
| 617 |
// - If no header line is present, xid_mode will be set to kfXidModeFidIid if |
| 618 |
// kfXidHeaderFixedWidth is set, and kfXidModeFidIidOrIid (which tolerates a |
| 619 |
// mix of single-token and multitoken lines) otherwise. |
| 620 |
// - TSTREAM_FAIL errors are not printed, but other errors are. |
| 621 |
PglErr LoadXidHeader(const char* flag_name, XidHeaderFlags xid_header_flags, uintptr_t* line_idx_ptr, TextStream* txsp, XidMode* xid_mode_ptr, char** line_startp); |
| 622 |
|
| 623 |
PglErr OpenAndLoadXidHeader(const char* fname, const char* flag_name, XidHeaderFlags xid_header_flags, uint32_t max_line_blen, TextStream* txsp, XidMode* xid_mode_ptr, uintptr_t* line_idx_ptr, char** line_startp); |
| 624 |
|
| 625 |
// header line expected to start with FID1, ID1, or IID1 |
| 626 |
PglErr LoadXidHeaderPair(const char* flag_name, uint32_t sid_over_fid, uintptr_t* line_idx_ptr, TextStream* txsp, XidMode* xid_mode_ptr, char** line_startp, char** line_iterp); |
| 627 |
|
| 628 |
// Assumes no duplicates. |
| 629 |
void InitXidHtable(const SampleIdInfo* siip, uint32_t sample_ct, uint32_t xid_htable_size, uint32_t* xid_htable, char* idbuf); |
| 630 |
|
| 631 |
BoolErr LookupXidHtable(const char* line_start, const SampleIdInfo* siip, const uint32_t* xid_htable, uint32_t xid_htable_size, uint32_t fid_present, uint32_t sid_present, uint32_t* sample_idxp, char* idbuf); |
| 632 |
|
| 633 |
PglErr CheckXidUniqueness(const uintptr_t* sample_include, const SampleIdInfo* siip, const char* err_suffix_str, uint32_t sample_ct); |
| 634 |
|
| 635 |
// could add MultifileIntersection mode if there's a user |
| 636 |
FLAGSET_DEF_START() |
| 637 |
kfLoadSampleIds0, |
| 638 |
kfLoadSampleIdsMultifile = (1 << 0), |
| 639 |
kfLoadSampleIdsFamOnly = (1 << 1) |
| 640 |
FLAGSET_DEF_END(LoadSampleIdsFlags); |
| 641 |
|
| 642 |
// Allocates (at the bottom of bigstack) and returns results in loaded_bitarr. |
| 643 |
PglErr LoadSampleIds(const char* fnames, const uintptr_t* sample_include, const SampleIdInfo* siip, const char* flag_name, uint32_t raw_sample_ct, uint32_t sample_ct, LoadSampleIdsFlags flags, uintptr_t** loaded_bitarrp, uint32_t* duplicate_ctp); |
| 644 |
|
| 645 |
// AppendXid is inline since it's reasonable to call it in loops writing |
| 646 |
// multi-GB files. AppendSpacedXid is not since it's only called when printing |
| 647 |
// error messages. |
| 648 |
HEADER_INLINE char* AppendXid(const char* sample_ids, const char* sids, uint32_t write_fid, uint32_t write_sid, uintptr_t max_sample_id_blen, uintptr_t max_sid_blen, uintptr_t sample_uidx, char* write_iter) { |
| 649 |
const char* cur_sample_id = &(sample_ids[max_sample_id_blen * sample_uidx]); |
| 650 |
if (!write_fid) { |
| 651 |
cur_sample_id = AdvPastDelim(cur_sample_id, '\t'); |
| 652 |
} |
| 653 |
write_iter = strcpya(write_iter, cur_sample_id); |
| 654 |
if (write_sid) { |
| 655 |
*write_iter++ = '\t'; |
| 656 |
if (sids) { |
| 657 |
write_iter = strcpya(write_iter, &(sids[max_sid_blen * sample_uidx])); |
| 658 |
} else { |
| 659 |
*write_iter++ = '0'; |
| 660 |
} |
| 661 |
} |
| 662 |
return write_iter; |
| 663 |
} |
| 664 |
|
| 665 |
char* AppendSpacedXid(const char* sample_ids, const char* sids, uint32_t write_fid, uint32_t write_sid, uintptr_t max_sample_id_blen, uintptr_t max_sid_blen, uintptr_t sample_uidx, char* write_iter); |
| 666 |
|
| 667 |
|
| 668 |
extern const unsigned char g_char_to_sex[256]; |
| 669 |
|
| 670 |
HEADER_INLINE uint32_t CharToSex(char cc) { |
| 671 |
return g_char_to_sex[S_CAST(unsigned char, cc)]; |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
#ifndef HIGH_CONTIG_BUILD |
| 676 |
// note that this is no longer divisible by 64 |
| 677 |
# ifndef HIGH_AUTOSOME_NUM_BUILD |
| 678 |
CONSTI32(kMaxContigs, 65274); |
| 679 |
# else |
| 680 |
CONSTI32(kMaxContigs, 64506); |
| 681 |
# endif |
| 682 |
CONSTI32(kMaxChrCodeDigits, 5); |
| 683 |
|
| 684 |
// change ChrIdx to uint32_t if (kMaxContigs + kChrOffsetCt) > 65536 |
| 685 |
typedef uint16_t ChrIdx; |
| 686 |
|
| 687 |
// GetHtableMinSize(kChrRawEnd) (use constexpr once sufficient compiler support |
| 688 |
// is available) |
| 689 |
// (not GetHtableFastSize since, an overwhelming majority of the time, we'll |
| 690 |
// have far fewer than 2^16 codes) |
| 691 |
CONSTI32(kChrHtableSize, 130560); |
| 692 |
#else |
| 693 |
// Keep this number in sync with the top-of-file comment, and the error message |
| 694 |
// in TryToAddChrName(). |
| 695 |
# ifndef HIGH_AUTOSOME_NUM_BUILD |
| 696 |
CONSTI32(kMaxContigs, 982778); |
| 697 |
# else |
| 698 |
CONSTI32(kMaxContigs, 982010); |
| 699 |
# endif |
| 700 |
CONSTI32(kMaxChrCodeDigits, 6); |
| 701 |
|
| 702 |
typedef uint32_t ChrIdx; |
| 703 |
|
| 704 |
CONSTI32(kChrHtableSize, 1965568); |
| 705 |
#endif |
| 706 |
|
| 707 |
static_assert((kMaxChrCodeDigits == 5) || (kMaxChrCodeDigits == 6), "u32toa_zchr() must be updated"); |
| 708 |
HEADER_INLINE char* u32toa_zchr(uint32_t uii, char* start) { |
| 709 |
if (kMaxChrCodeDigits == 5) { |
| 710 |
return u32toa_z5(uii, start); |
| 711 |
} else { |
| 712 |
return u32toa_z6(uii, start); |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
HEADER_INLINE BoolErr bigstack_alloc_chridx(uintptr_t ct, ChrIdx** chridx_arr_ptr) { |
| 717 |
*chridx_arr_ptr = S_CAST(ChrIdx*, bigstack_alloc(ct * sizeof(ChrIdx))); |
| 718 |
return !(*chridx_arr_ptr); |
| 719 |
} |
| 720 |
|
| 721 |
ENUM_U31_DEF_START() |
| 722 |
kChrOffsetX, |
| 723 |
kChrOffsetY, |
| 724 |
|
| 725 |
// old way of representing pseudo-autosomal regions. clumsy since this |
| 726 |
// required changing chromosome order |
| 727 |
kChrOffsetXY, |
| 728 |
|
| 729 |
kChrOffsetMT, |
| 730 |
|
| 731 |
// plink 2.x pseudo-autosomal regions. |
| 732 |
kChrOffsetPAR1, |
| 733 |
kChrOffsetPAR2, |
| 734 |
kChrOffsetCt |
| 735 |
ENUM_U31_DEF_END(XymtOffset); |
| 736 |
|
| 737 |
CONSTI32(kChrRawX, kMaxContigs + kChrOffsetX); |
| 738 |
CONSTI32(kChrRawY, kMaxContigs + kChrOffsetY); |
| 739 |
CONSTI32(kChrRawXY, kMaxContigs + kChrOffsetXY); |
| 740 |
CONSTI32(kChrRawMT, kMaxContigs + kChrOffsetMT); |
| 741 |
CONSTI32(kChrRawPAR1, kMaxContigs + kChrOffsetPAR1); |
| 742 |
CONSTI32(kChrRawPAR2, kMaxContigs + kChrOffsetPAR2); |
| 743 |
CONSTI32(kChrRawEnd, kMaxContigs + kChrOffsetCt); |
| 744 |
|
| 745 |
static_assert((!(kChrRawEnd % kBitsPerWord)), "kChrRawEnd expression must be updated."); |
| 746 |
CONSTI32(kChrMaskWords, kChrRawEnd / kBitsPerWord); |
| 747 |
|
| 748 |
#ifndef HIGH_AUTOSOME_NUM_BUILD |
| 749 |
// (note that n+1, n+2, n+3, and n+4 are reserved for X/Y/XY/MT) |
| 750 |
CONSTI32(kMaxChrTextnum, 95); |
| 751 |
|
| 752 |
# ifdef __LP64__ |
| 753 |
CONSTI32(kChrExcludeWords, 2); |
| 754 |
# else |
| 755 |
CONSTI32(kChrExcludeWords, 4); |
| 756 |
# endif |
| 757 |
|
| 758 |
// GetChrCodeRaw() needs to be modified if this changes |
| 759 |
CONSTI32(kMaxChrTextnumSlen, 2); |
| 760 |
#else // HIGH_AUTOSOME_NUM_BUILD |
| 761 |
CONSTI32(kMaxChrTextnum, 995); |
| 762 |
|
| 763 |
# ifdef __LP64__ |
| 764 |
CONSTI32(kChrExcludeWords, 16); |
| 765 |
# else |
| 766 |
CONSTI32(kChrExcludeWords, 32); |
| 767 |
# endif |
| 768 |
|
| 769 |
CONSTI32(kMaxChrTextnumSlen, 3); |
| 770 |
#endif |
| 771 |
static_assert(kChrExcludeWords * kBitsPerWord >= kMaxChrTextnum + 2 * kChrOffsetCt + 1, "kChrExcludeWords must be updated."); |
| 772 |
|
| 773 |
// AnotherFile is for .pvar merge (chrSets must be consistent across all .pvar |
| 774 |
// files in that case) |
| 775 |
ENUM_U31_DEF_START() |
| 776 |
kChrsetSourceDefault, |
| 777 |
kChrsetSourceCmdline, |
| 778 |
kChrsetSourceFile, |
| 779 |
kChrsetSourceAnotherFile |
| 780 |
ENUM_U31_DEF_END(ChrsetSource); |
| 781 |
|
| 782 |
FLAGSET_DEF_START() |
| 783 |
kfChrOutput0, |
| 784 |
kfChrOutputPrefix = (1 << 0), |
| 785 |
kfChrOutputM = (1 << 1), |
| 786 |
kfChrOutputMT = (1 << 2), |
| 787 |
kfChrOutput0M = (1 << 3) |
| 788 |
FLAGSET_DEF_END(ChrOutput); |
| 789 |
|
| 790 |
typedef struct ChrInfoStruct { |
| 791 |
// Main dynamic block intended to be allocated as a single aligned block of |
| 792 |
// memory on the heap freeable with vecaligned_free(), with chr_mask at the |
| 793 |
// base. |
| 794 |
NONCOPYABLE(ChrInfoStruct); |
| 795 |
|
| 796 |
uintptr_t* chr_mask; // which chromosomes aren't known to be absent? |
| 797 |
|
| 798 |
// This normally includes chrX. However, library function implementations |
| 799 |
// cannot assume that: e.g. --glm now temporarily clears the chrX bit when |
| 800 |
// all samples are female. |
| 801 |
// As of alpha 2, it also includes MT again (like plink 1.07, and unlike 1.9 |
| 802 |
// and 2.0a1), now that enough dosage functionality is in place. |
| 803 |
uintptr_t* haploid_mask; |
| 804 |
|
| 805 |
// order of chromosomes in input files |
| 806 |
// currently tolerates out-of-order chromosomes, as long as all variants for |
| 807 |
// any given chromosome are together |
| 808 |
uint32_t* chr_file_order; |
| 809 |
|
| 810 |
// if the second chromosome in the dataset is chr5, chr_file_order[1] == 5, |
| 811 |
// the raw variant indexes for chr5 are in [chr_fo_vidx_start[1], |
| 812 |
// chr_fo_vidx_start[2]). and chr_idx_to_foidx[5] == 1. |
| 813 |
uint32_t* chr_fo_vidx_start; |
| 814 |
uint32_t* chr_idx_to_foidx; |
| 815 |
|
| 816 |
// --allow-extra-chr support |
| 817 |
const char** nonstd_names; |
| 818 |
uint32_t* nonstd_id_htable; |
| 819 |
// end main dynamic block |
| 820 |
|
| 821 |
uint32_t chr_ct; // number of distinct chromosomes/contigs |
| 822 |
ChrsetSource chrset_source; |
| 823 |
|
| 824 |
uintptr_t chr_exclude[kChrExcludeWords]; |
| 825 |
|
| 826 |
// X, Y, XY...; UINT32_MAXM1 = not in chromosome set |
| 827 |
STD_ARRAY_DECL(uint32_t, kChrOffsetCt, xymt_codes); |
| 828 |
|
| 829 |
uint32_t max_numeric_code; |
| 830 |
uint32_t max_code; // no longer identical to max_numeric_code, with PARs |
| 831 |
|
| 832 |
uint32_t autosome_ct; |
| 833 |
|
| 834 |
// yet more --allow-extra-chr support |
| 835 |
uint32_t zero_extra_chrs; |
| 836 |
uint32_t name_ct; |
| 837 |
LlStr* incl_excl_name_stack; |
| 838 |
uint32_t is_include_stack; |
| 839 |
ChrOutput output_encoding; |
| 840 |
} ChrInfo; |
| 841 |
|
| 842 |
extern const char g_xymt_log_names[kChrOffsetCt][5]; |
| 843 |
|
| 844 |
PglErr InitChrInfo(ChrInfo* cip); |
| 845 |
|
| 846 |
void FinalizeChrset(MiscFlags misc_flags, ChrInfo* cip); |
| 847 |
|
| 848 |
HEADER_INLINE PglErr InitChrInfoHuman(ChrInfo* cip) { |
| 849 |
// convenience wrapper |
| 850 |
if (unlikely(InitChrInfo(cip))) { |
| 851 |
return kPglRetNomem; |
| 852 |
} |
| 853 |
FinalizeChrset(kfMisc0, cip); |
| 854 |
return kPglRetSuccess; |
| 855 |
} |
| 856 |
|
| 857 |
HEADER_INLINE uint32_t IsHumanChrset(const ChrInfo* cip) { |
| 858 |
return (cip->max_code == 28) && (cip->max_numeric_code == 26) && (cip->autosome_ct == 22); |
| 859 |
} |
| 860 |
|
| 861 |
void ForgetExtraChrNames(uint32_t reinitialize, ChrInfo* cip); |
| 862 |
|
| 863 |
// in the usual case where the number of chromosomes/contigs is much less than |
| 864 |
// kMaxContigs, this reduces chr_info's memory consumption and improves |
| 865 |
// locality. |
| 866 |
PglErr FinalizeChrInfo(ChrInfo* cip); |
| 867 |
|
| 868 |
void CleanupChrInfo(ChrInfo* cip); |
| 869 |
|
| 870 |
// assumes chr_idx is valid |
| 871 |
// note that chr_idx == 0 is always rendered as '0', never 'chr0' |
| 872 |
char* chrtoa(const ChrInfo* cip, uint32_t chr_idx, char* buf); |
| 873 |
|
| 874 |
uint32_t GetMaxChrSlen(const ChrInfo* cip); |
| 875 |
|
| 876 |
uint32_t IsHaploidChrPresent(const ChrInfo* cip); |
| 877 |
|
| 878 |
uint32_t IsAutosomalDiploidChrPresent(const ChrInfo* cip); |
| 879 |
|
| 880 |
// any character <= ' ' is considered a terminator |
| 881 |
// maps chrX -> kChrRawX, etc. |
| 882 |
uint32_t GetChrCodeRaw(const char* str_iter); |
| 883 |
|
| 884 |
// requires chr_name to be null-terminated |
| 885 |
// maps chrX -> xymt_codes[kChrOffsetX], etc. |
| 886 |
// error codes: |
| 887 |
// UINT32_MAX = --allow-extra-chr ok |
| 888 |
// UINT32_MAXM1 = total fail |
| 889 |
uint32_t GetChrCode(const char* chr_name, const ChrInfo* cip, uint32_t name_slen); |
| 890 |
|
| 891 |
// when the chromosome name isn't null-terminated |
| 892 |
// requires chr_name[name_slen] to be mutable |
| 893 |
uint32_t GetChrCodeCounted(const ChrInfo* cip, uint32_t name_slen, char* chr_name); |
| 894 |
|
| 895 |
HEADER_INLINE uint32_t GetVariantChrFoIdx(const ChrInfo* cip, uintptr_t variant_uidx) { |
| 896 |
return LowerBoundNonemptyU32(&(cip->chr_fo_vidx_start[1]), cip->chr_ct, variant_uidx + 1); |
| 897 |
} |
| 898 |
|
| 899 |
HEADER_INLINE uint32_t GetVariantChr(const ChrInfo* cip, uintptr_t variant_uidx) { |
| 900 |
return cip->chr_file_order[GetVariantChrFoIdx(cip, variant_uidx)]; |
| 901 |
} |
| 902 |
|
| 903 |
HEADER_INLINE uint32_t XymtExists(const ChrInfo* cip, uint32_t xymt_offset, uint32_t* xymt_code_ptr) { |
| 904 |
// too easy to forget IsSet(chr_mask) check if we don't use this |
| 905 |
const uint32_t xymt_code = cip->xymt_codes[xymt_offset]; |
| 906 |
*xymt_code_ptr = xymt_code; |
| 907 |
return (!IsI32Neg(xymt_code)) && IsSet(cip->chr_mask, xymt_code); |
| 908 |
} |
| 909 |
|
| 910 |
HEADER_INLINE void GetXymtStartAndEnd(const ChrInfo* cip, uint32_t xymt_offset, uint32_t* xymt_start_ptr, uint32_t* xymt_end_ptr) { |
| 911 |
uint32_t xymt_code; |
| 912 |
if (!XymtExists(cip, xymt_offset, &xymt_code)) { |
| 913 |
*xymt_start_ptr = 0; |
| 914 |
*xymt_end_ptr = 0; |
| 915 |
return; |
| 916 |
} |
| 917 |
const uint32_t chr_fo_idx = cip->chr_idx_to_foidx[xymt_code]; |
| 918 |
*xymt_start_ptr = cip->chr_fo_vidx_start[chr_fo_idx]; |
| 919 |
*xymt_end_ptr = cip->chr_fo_vidx_start[chr_fo_idx + 1]; |
| 920 |
} |
| 921 |
|
| 922 |
HEADER_INLINE void GetXymtCodeStartAndEndUnsafe(const ChrInfo* cip, uint32_t xymt_offset, uint32_t* xymt_code_ptr, uint32_t* xymt_start_ptr, uint32_t* xymt_end_ptr) { |
| 923 |
// assumes XymtExists was previously called, and is true |
| 924 |
const uint32_t xymt_code = cip->xymt_codes[xymt_offset]; |
| 925 |
*xymt_code_ptr = xymt_code; |
| 926 |
const uint32_t chr_fo_idx = cip->chr_idx_to_foidx[xymt_code]; |
| 927 |
*xymt_start_ptr = cip->chr_fo_vidx_start[chr_fo_idx]; |
| 928 |
*xymt_end_ptr = cip->chr_fo_vidx_start[chr_fo_idx + 1]; |
| 929 |
} |
| 930 |
|
| 931 |
// now assumes chr_name is null-terminated |
| 932 |
PglErr TryToAddChrName(const char* chr_name, const char* file_descrip, uintptr_t line_idx, uint32_t name_slen, uint32_t prohibit_extra_chrs, uint32_t* chr_idx_ptr, ChrInfo* cip); |
| 933 |
|
| 934 |
HEADER_INLINE PglErr GetOrAddChrCode(const char* chr_name, const char* file_descrip, uintptr_t line_idx, uint32_t name_slen, uint32_t prohibit_extra_chrs, ChrInfo* cip, uint32_t* chr_idx_ptr) { |
| 935 |
*chr_idx_ptr = GetChrCode(chr_name, cip, name_slen); |
| 936 |
if (!IsI32Neg(*chr_idx_ptr)) { |
| 937 |
return kPglRetSuccess; |
| 938 |
} |
| 939 |
return TryToAddChrName(chr_name, file_descrip, line_idx, name_slen, prohibit_extra_chrs, chr_idx_ptr, cip); |
| 940 |
} |
| 941 |
|
| 942 |
HEADER_INLINE PglErr GetOrAddChrCodeDestructive(const char* file_descrip, uintptr_t line_idx, uint32_t prohibit_extra_chrs, char* chr_name, char* chr_name_end, ChrInfo* cip, uint32_t* chr_idx_ptr) { |
| 943 |
*chr_name_end = '\0'; |
| 944 |
return GetOrAddChrCode(chr_name, file_descrip, line_idx, chr_name_end - chr_name, prohibit_extra_chrs, cip, chr_idx_ptr); |
| 945 |
} |
| 946 |
|
| 947 |
// Assumes sample_ct positive. Does not require trailing bits to be clear. |
| 948 |
uint32_t AllGenoEqual(const uintptr_t* genoarr, uint32_t sample_ct); |
| 949 |
|
| 950 |
// zeroes out samples not in the mask |
| 951 |
void InterleavedMaskZero(const uintptr_t* __restrict interleaved_mask, uintptr_t geno_vec_ct, uintptr_t* __restrict genovec); |
| 952 |
|
| 953 |
// sets samples outside the mask to missing (0b11) |
| 954 |
void InterleavedMaskMissing(const uintptr_t* __restrict interleaved_set, uintptr_t geno_vec_ct, uintptr_t* __restrict genovec); |
| 955 |
|
| 956 |
// sets samples in the mask to missing (0b11) |
| 957 |
void InterleavedSetMissing(const uintptr_t* __restrict interleaved_set, uintptr_t geno_vec_ct, uintptr_t* __restrict genovec); |
| 958 |
|
| 959 |
void InterleavedSetMissingCleardosage(const uintptr_t* __restrict orig_set, const uintptr_t* __restrict interleaved_set, uintptr_t geno_vec_ct, uintptr_t* __restrict genovec, uint32_t* __restrict write_dosage_ct_ptr, uintptr_t* __restrict dosagepresent, Dosage* dosage_main); |
| 960 |
|
| 961 |
void SetMaleHetMissing(const uintptr_t* __restrict sex_male_interleaved, uint32_t geno_vec_ct, uintptr_t* __restrict genovec); |
| 962 |
|
| 963 |
void EraseMaleDphases(const uintptr_t* __restrict sex_male, uint32_t* __restrict write_dphase_ct_ptr, uintptr_t* __restrict dphasepresent, SDosage* dphase_delta); |
| 964 |
|
| 965 |
void SetMaleHetMissingCleardosage(const uintptr_t* __restrict sex_male, const uintptr_t* __restrict sex_male_interleaved, uint32_t geno_vec_ct, uintptr_t* __restrict genovec, uint32_t* __restrict write_dosage_ct_ptr, uintptr_t* __restrict dosagepresent, Dosage* dosage_main); |
| 966 |
|
| 967 |
void SetMaleHetMissingKeepdosage(const uintptr_t* __restrict sex_male, const uintptr_t* __restrict sex_male_interleaved, uint32_t geno_word_ct, uintptr_t* __restrict genovec, uint32_t* __restrict write_dosage_ct_ptr, uintptr_t* __restrict dosagepresent, Dosage* dosage_main); |
| 968 |
|
| 969 |
// Clears each bit in bitarr which doesn't correspond to a genovec het. |
| 970 |
// Assumes that either trailing bits of bitarr are already zero, or trailing |
| 971 |
// bits of genovec are zero. |
| 972 |
HEADER_INLINE void MaskGenoarrHetsUnsafe(const uintptr_t* __restrict genoarr, uint32_t raw_sample_ctl2, uintptr_t* __restrict bitarr) { |
| 973 |
MaskWordsToHalfwordsInvmatch(genoarr, kMaskAAAA, raw_sample_ctl2, bitarr, bitarr); |
| 974 |
} |
| 975 |
|
| 976 |
void MaskGenoarrHetsMultiallelicUnsafe(const uintptr_t* __restrict genoarr, const uintptr_t* __restrict patch_10_set, const AlleleCode* __restrict patch_10_vals, uint32_t raw_sample_ctl2, uintptr_t* __restrict bitarr); |
| 977 |
|
| 978 |
// vertical popcount support |
| 979 |
// VcountScramble1() and friends in plink2_cmdline |
| 980 |
#ifdef __LP64__ |
| 981 |
# ifdef USE_AVX2 |
| 982 |
// 2->4: 0 2 ... 126 1 3 ... 127 |
| 983 |
// 4->8: 0 4 ... 124 2 6 ... 126 1 5 ... 125 3 7 ... 127 |
| 984 |
// 8->32: 0 16 ... 112 4 20 ... 116 ... 124 2 18 ... 114 6 22 ... 118 ... 126 1 17 ... |
| 985 |
HEADER_INLINE uint32_t VcountScramble2(uint32_t orig_idx) { |
| 986 |
return (orig_idx & (~127)) + ((orig_idx & 1) * 64) + ((orig_idx & 2) * 16) + ((orig_idx & 12) * 2) + ((orig_idx & 112) / 16); |
| 987 |
} |
| 988 |
# else |
| 989 |
HEADER_INLINE uint32_t VcountScramble2(uint32_t orig_idx) { |
| 990 |
return (orig_idx & (~63)) + ((orig_idx & 1) * 32) + ((orig_idx & 2) * 8) + (orig_idx & 12) + ((orig_idx & 48) / 16); |
| 991 |
} |
| 992 |
# endif |
| 993 |
#else |
| 994 |
// 2->4: 0 2 4 6 8 10 12 14 1 3 5 ... |
| 995 |
// 4->8: 0 4 8 12 2 6 10 14 1 5 9 ... |
| 996 |
// 8->32: 0 4 8 12 2 6 10 14 1 5 9 13 3 7 11 15 |
| 997 |
HEADER_INLINE uint32_t VcountScramble2(uint32_t orig_idx) { |
| 998 |
return (orig_idx & (~15)) + ((orig_idx & 1) * 8) + ((orig_idx & 2) * 2) + ((orig_idx & 12) / 4); |
| 999 |
} |
| 1000 |
#endif |
| 1001 |
|
| 1002 |
HEADER_INLINE void VcountIncr2To4(const VecW* acc2_iter, uint32_t acc2_vec_ct, VecW* acc4_iter) { |
| 1003 |
const VecW m2 = VCONST_W(kMask3333); |
| 1004 |
for (uint32_t vidx = 0; vidx != acc2_vec_ct; ++vidx) { |
| 1005 |
VecW loader = *acc2_iter++; |
| 1006 |
*acc4_iter += loader & m2; |
| 1007 |
++acc4_iter; |
| 1008 |
*acc4_iter += vecw_srli(loader, 2) & m2; |
| 1009 |
++acc4_iter; |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
HEADER_INLINE void Vcount0Incr2To4(uint32_t acc2_vec_ct, VecW* acc2_iter, VecW* acc4_iter) { |
| 1014 |
const VecW m2 = VCONST_W(kMask3333); |
| 1015 |
for (uint32_t vidx = 0; vidx != acc2_vec_ct; ++vidx) { |
| 1016 |
VecW loader = *acc2_iter; |
| 1017 |
*acc2_iter++ = vecw_setzero(); |
| 1018 |
*acc4_iter += loader & m2; |
| 1019 |
++acc4_iter; |
| 1020 |
*acc4_iter += vecw_srli(loader, 2) & m2; |
| 1021 |
++acc4_iter; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
// uint32_t chr_window_max(const uintptr_t* variant_include, const ChrInfo* cip, const uint32_t* variant_pos, uint32_t chr_fo_idx, uint32_t ct_max, uint32_t bp_max, uint32_t cur_window_max); |
| 1027 |
|
| 1028 |
HEADER_INLINE uint32_t CountChrVariantsUnsafe(const uintptr_t* variant_include, const ChrInfo* cip, uint32_t chr_idx) { |
| 1029 |
assert(IsSet(cip->chr_mask, chr_idx)); |
| 1030 |
const uint32_t chr_fo_idx = cip->chr_idx_to_foidx[chr_idx]; |
| 1031 |
const uint32_t start_vidx = cip->chr_fo_vidx_start[chr_fo_idx]; |
| 1032 |
const uint32_t end_vidx = cip->chr_fo_vidx_start[chr_fo_idx + 1]; |
| 1033 |
return PopcountBitRange(variant_include, start_vidx, end_vidx); |
| 1034 |
} |
| 1035 |
|
| 1036 |
HEADER_INLINE uint32_t ChrIsNonempty(const uintptr_t* variant_include, const ChrInfo* cip, uint32_t chr_idx) { |
| 1037 |
if (!IsSet(cip->chr_mask, chr_idx)) { |
| 1038 |
return 0; |
| 1039 |
} |
| 1040 |
const uint32_t chr_fo_idx = cip->chr_idx_to_foidx[chr_idx]; |
| 1041 |
const uint32_t start_vidx = cip->chr_fo_vidx_start[chr_fo_idx]; |
| 1042 |
const uint32_t end_vidx = cip->chr_fo_vidx_start[chr_fo_idx + 1]; |
| 1043 |
return !AllBitsAreZero(variant_include, start_vidx, end_vidx); |
| 1044 |
} |
| 1045 |
|
| 1046 |
HEADER_INLINE uint32_t XymtIsNonempty(const uintptr_t* variant_include, const ChrInfo* cip, uint32_t xymt_offset) { |
| 1047 |
const uint32_t xymt_code = cip->xymt_codes[xymt_offset]; |
| 1048 |
if (IsI32Neg(xymt_code) || (!IsSet(cip->chr_mask, xymt_code))) { |
| 1049 |
return 0; |
| 1050 |
} |
| 1051 |
return ChrIsNonempty(variant_include, cip, xymt_code); |
| 1052 |
} |
| 1053 |
|
| 1054 |
// assumes there's at least one variant on specified chromosome |
| 1055 |
uint32_t NotOnlyXymt(const uintptr_t* variant_include, const ChrInfo* cip, uint32_t raw_variant_ct, uint32_t xymt_offset); |
| 1056 |
|
| 1057 |
uint32_t CountNonAutosomalVariants(const uintptr_t* variant_include, const ChrInfo* cip, uint32_t count_x, uint32_t count_mt); |
| 1058 |
|
| 1059 |
void ExcludeNonAutosomalVariants(const ChrInfo* cip, uintptr_t* variant_include); |
| 1060 |
|
| 1061 |
// If calc_descrip == nullptr, no logging occurs when some variants are |
| 1062 |
// removed, and no error occurs if all variants are removed. (Might decouple |
| 1063 |
// these two later.) |
| 1064 |
PglErr ConditionalAllocateNonAutosomalVariants(const ChrInfo* cip, const char* calc_descrip, uint32_t raw_variant_ct, const uintptr_t** variant_include_ptr, uint32_t* variant_ct_ptr); |
| 1065 |
|
| 1066 |
void FillSubsetChrFoVidxStart(const uintptr_t* variant_include, const ChrInfo* cip, uint32_t* subset_chr_fo_vidx_start); |
| 1067 |
|
| 1068 |
HEADER_INLINE BoolErr AllocAndFillSubsetChrFoVidxStart(const uintptr_t* variant_include, const ChrInfo* cip, uint32_t** subset_chr_fo_vidx_start_ptr) { |
| 1069 |
const uint32_t chr_ct = cip->chr_ct; |
| 1070 |
if (unlikely(bigstack_alloc_u32(chr_ct + 1, subset_chr_fo_vidx_start_ptr))) { |
| 1071 |
return 1; |
| 1072 |
} |
| 1073 |
FillSubsetChrFoVidxStart(variant_include, cip, *subset_chr_fo_vidx_start_ptr); |
| 1074 |
return 0; |
| 1075 |
} |
| 1076 |
|
| 1077 |
/* |
| 1078 |
// newval does not need to be null-terminated |
| 1079 |
// assumes *allele_ptr is not initialized |
| 1080 |
// (stop using these in main plink2 binary?) |
| 1081 |
BoolErr allele_set(const char* newval, uint32_t allele_slen, char** allele_ptr); |
| 1082 |
|
| 1083 |
// *allele_ptr must be initialized; frees *allele_ptr if necessary |
| 1084 |
BoolErr allele_reset(const char* newval, uint32_t allele_slen, char** allele_ptr); |
| 1085 |
|
| 1086 |
void cleanup_allele_storage(uint32_t max_allele_slen, uintptr_t allele_storage_entry_ct, const char** allele_storage); |
| 1087 |
*/ |
| 1088 |
|
| 1089 |
CONSTI32(kMaxMissingPhenostrBlen, 32); |
| 1090 |
|
| 1091 |
// don't care about kfUnsortedVarChrom |
| 1092 |
FLAGSET_DEF_START() |
| 1093 |
kfUnsortedVar0, |
| 1094 |
kfUnsortedVarBp = (1 << 0), |
| 1095 |
kfUnsortedVarCm = (1 << 1), |
| 1096 |
kfUnsortedVarSplitChr = (1 << 2) |
| 1097 |
FLAGSET_DEF_END(UnsortedVar); |
| 1098 |
|
| 1099 |
FLAGSET_DEF_START() |
| 1100 |
kfFamCol0, |
| 1101 |
kfFamCol1 = (1 << 0), |
| 1102 |
kfFamCol34 = (1 << 1), |
| 1103 |
kfFamCol5 = (1 << 2), |
| 1104 |
kfFamCol6 = (1 << 3), |
| 1105 |
kfFamCol13456 = (kfFamCol1 | kfFamCol34 | kfFamCol5 | kfFamCol6) |
| 1106 |
FLAGSET_DEF_END(FamCol); |
| 1107 |
|
| 1108 |
HEADER_INLINE char Sexchar(const uintptr_t* sex_nm, const uintptr_t* sex_male, uintptr_t sample_uidx) { |
| 1109 |
if (IsSet(sex_nm, sample_uidx)) { |
| 1110 |
return '2' - IsSet(sex_male, sample_uidx); |
| 1111 |
} |
| 1112 |
return '0'; |
| 1113 |
} |
| 1114 |
|
| 1115 |
// kPhenoDtypeCc and kPhenoDtypeQt currently can't change |
| 1116 |
// kPhenoDtypeOther currently used for --glm local covariates |
| 1117 |
ENUM_U31_DEF_START() |
| 1118 |
kPhenoDtypeCc, |
| 1119 |
kPhenoDtypeQt, |
| 1120 |
kPhenoDtypeCat, |
| 1121 |
kPhenoDtypeOther |
| 1122 |
ENUM_U31_DEF_END(PhenoDtype); |
| 1123 |
|
| 1124 |
typedef union { |
| 1125 |
uintptr_t* cc; // bitvector |
| 1126 |
double* qt; |
| 1127 |
uint32_t* cat; // always 0 for missing, nonmiss[] check unnecessary |
| 1128 |
} PhenoData; |
| 1129 |
|
| 1130 |
typedef struct PhenoColStruct { |
| 1131 |
// * If categorical phenotype, [0] points to g_missing_catname, while [1], |
| 1132 |
// [2], etc. point to category names. These are part of the same |
| 1133 |
// allocation as nonmiss, so no separate free is needed. |
| 1134 |
// Otherwise, this is nullptr. |
| 1135 |
// * When .sample non-V2 categorical variables are imported, 'C' is added in |
| 1136 |
// front of the integers. |
| 1137 |
// * category_names[1..(n-1)] is guaranteed to be in natural-sorted order. |
| 1138 |
// (MergePsams() has slightly different behavior since its pheno_cols |
| 1139 |
// instance is only used for a one-time internal write.) |
| 1140 |
const char** category_names; |
| 1141 |
|
| 1142 |
uintptr_t* nonmiss; // bitvector |
| 1143 |
|
| 1144 |
// essentially a tagged union; part of the same allocation as nonmiss |
| 1145 |
PhenoData data; |
| 1146 |
PhenoDtype type_code; |
| 1147 |
|
| 1148 |
uint32_t nonnull_category_ct; |
| 1149 |
} PhenoCol; |
| 1150 |
|
| 1151 |
uint32_t IsCategoricalPhenostr(const char* phenostr_iter); |
| 1152 |
|
| 1153 |
uint32_t IsCategoricalPhenostrNocsv(const char* phenostr_iter); |
| 1154 |
|
| 1155 |
// returns 0xffffffffU if none exists |
| 1156 |
uint32_t FirstCcOrQtPhenoIdx(const PhenoCol* pheno_cols, uint32_t pheno_ct); |
| 1157 |
|
| 1158 |
// "_covar" since this doesn't handle case/control |
| 1159 |
uint32_t IsConstCovar(const PhenoCol* covar_col, const uintptr_t* sample_include, uint32_t raw_sample_ct); |
| 1160 |
|
| 1161 |
// returns number of nonempty categories, null included |
| 1162 |
uint32_t IdentifyRemainingCats(const uintptr_t* sample_include, const PhenoCol* covar_col, uint32_t sample_ct, uintptr_t* observed_cat_bitarr); |
| 1163 |
|
| 1164 |
// returns index of most common category |
| 1165 |
uint32_t IdentifyRemainingCatsAndMostCommon(const uintptr_t* sample_include, const PhenoCol* covar_col, uint32_t sample_ct, uintptr_t* observed_cat_bitarr, uint32_t* cat_obs_buf); |
| 1166 |
|
| 1167 |
uint32_t GetCatSamples(const uintptr_t* sample_include_base, const PhenoCol* cat_pheno_col, uint32_t raw_sample_ctl, uint32_t sample_ct, uint32_t cat_uidx, uintptr_t* cur_cat_samples); |
| 1168 |
|
| 1169 |
uint32_t RemoveExcludedCats(const uint32_t* data_cat, const uintptr_t* cat_keep_bitarr, uint32_t raw_sample_ct, uint32_t in_sample_ct, uintptr_t* sample_include); |
| 1170 |
|
| 1171 |
// pheno_names is also allocated on the heap, but it can be handled with a |
| 1172 |
// simple free_cond(). |
| 1173 |
void CleanupPhenoCols(uint32_t pheno_ct, PhenoCol* pheno_cols); |
| 1174 |
|
| 1175 |
PglErr ParseChrRanges(const char* const* argvk, const char* flagname_p, const char* errstr_append, uint32_t param_ct, uint32_t prohibit_extra_chrs, uint32_t xymt_subtract, char range_delim, ChrInfo* cip, uintptr_t* chr_mask); |
| 1176 |
|
| 1177 |
|
| 1178 |
uint32_t MultiallelicVariantPresent(const uintptr_t* variant_include, const uintptr_t* allele_idx_offsets, uint32_t variant_ct); |
| 1179 |
|
| 1180 |
uint32_t CountBiallelicVariants(const uintptr_t* variant_include, const uintptr_t* allele_idx_offsets, uint32_t variant_ct); |
| 1181 |
|
| 1182 |
uintptr_t CountAlleles(const uintptr_t* variant_include, const uintptr_t* allele_idx_offsets, uint32_t raw_variant_ct, uint32_t variant_ct); |
| 1183 |
|
| 1184 |
// This can be an overestimate (it won't return a value smaller than |
| 1185 |
// read_block_size). |
| 1186 |
uintptr_t GetMaxAltAlleleBlockSize(const uintptr_t* variant_include, const uintptr_t* allele_idx_offsets, uint32_t raw_variant_ct, uint32_t variant_ct, uint32_t read_block_size); |
| 1187 |
|
| 1188 |
// Mhc stands for "multiallelic hardcall" here, not major histocompatibility |
| 1189 |
// complex. Though multiallelic hardcalls are of course especially relevant |
| 1190 |
// for analysis of major histocompatibility complex genomic data... |
| 1191 |
uintptr_t GetMhcWordCt(uintptr_t sample_ct); |
| 1192 |
|
| 1193 |
// sample_ct not relevant if genovecs_ptr == nullptr |
| 1194 |
// only possible error is kPglRetNomem for now |
| 1195 |
// caller should reset pgfip->block_base to nullptr when it exits |
| 1196 |
PglErr PgenMtLoadInit(const uintptr_t* variant_include, uint32_t sample_ct, uint32_t variant_ct, uintptr_t bytes_avail, uintptr_t pgr_alloc_cacheline_ct, uintptr_t thread_xalloc_cacheline_ct, uintptr_t per_variant_xalloc_byte_ct, uintptr_t per_alt_allele_xalloc_byte_ct, PgenFileInfo* pgfip, uint32_t* calc_thread_ct_ptr, uintptr_t*** genovecs_ptr, uintptr_t*** mhc_ptr, uintptr_t*** phasepresent_ptr, uintptr_t*** phaseinfo_ptr, uintptr_t*** dosage_present_ptr, Dosage*** dosage_mains_ptr, uintptr_t*** dphase_present_ptr, SDosage*** dphase_delta_ptr, uint32_t* read_block_size_ptr, uintptr_t* max_alt_allele_block_size_ptr, STD_ARRAY_REF(unsigned char*, 2) main_loadbufs, PgenReader*** pgr_pps, uint32_t** read_variant_uidx_starts_ptr); |
| 1197 |
|
| 1198 |
// Returns number of variants in current block. Increases read_block_idx as |
| 1199 |
// necessary (to get to a nonempty block). |
| 1200 |
// reterr can be ReadFail but not MalformedInput, since this function just |
| 1201 |
// reads the raw bytes, it does not perform any unpacking. |
| 1202 |
uint32_t MultireadNonempty(const uintptr_t* variant_include, const ThreadGroup* tgp, uint32_t raw_variant_ct, uint32_t read_block_size, PgenFileInfo* pgfip, uint32_t* read_block_idxp, PglErr* reterrp); |
| 1203 |
|
| 1204 |
// Assumes mhc != nullptr, and is vector-aligned. |
| 1205 |
void ExpandMhc(uint32_t sample_ct, uintptr_t* mhc, uintptr_t** patch_01_set_ptr, AlleleCode** patch_01_vals_ptr, uintptr_t** patch_10_set_ptr, AlleleCode** patch_10_vals_ptr); |
| 1206 |
|
| 1207 |
HEADER_INLINE void SetPgvMhc(uint32_t sample_ct, uintptr_t* mhc, PgenVariant* pgvp) { |
| 1208 |
ExpandMhc(sample_ct, mhc, &(pgvp->patch_01_set), &(pgvp->patch_01_vals), &(pgvp->patch_10_set), &(pgvp->patch_10_vals)); |
| 1209 |
} |
| 1210 |
|
| 1211 |
HEADER_INLINE void SetPgvThreadMhcNull(uint32_t sample_ct, uint32_t tidx, uintptr_t** thread_mhc, PgenVariant* pgvp) { |
| 1212 |
if (!thread_mhc) { |
| 1213 |
pgvp->patch_01_set = nullptr; |
| 1214 |
pgvp->patch_01_vals = nullptr; |
| 1215 |
pgvp->patch_10_set = nullptr; |
| 1216 |
pgvp->patch_10_vals = nullptr; |
| 1217 |
} else { |
| 1218 |
ExpandMhc(sample_ct, thread_mhc[tidx], &(pgvp->patch_01_set), &(pgvp->patch_01_vals), &(pgvp->patch_10_set), &(pgvp->patch_10_vals)); |
| 1219 |
} |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
HEADER_INLINE BoolErr StoreStringAtBase(unsigned char* arena_top, const char* src, uintptr_t slen, unsigned char** arena_bottom_ptr, char** dst) { |
| 1224 |
if (unlikely(slen >= S_CAST(uintptr_t, arena_top - (*arena_bottom_ptr)))) { |
| 1225 |
return 1; |
| 1226 |
} |
| 1227 |
memcpyx(*arena_bottom_ptr, src, slen, '\0'); |
| 1228 |
*dst = R_CAST(char*, *arena_bottom_ptr); |
| 1229 |
*arena_bottom_ptr += slen + 1; |
| 1230 |
return 0; |
| 1231 |
} |
| 1232 |
|
| 1233 |
HEADER_INLINE BoolErr StoreStringAtEnd(unsigned char* arena_bottom, const char* src, uintptr_t slen, unsigned char** arena_top_ptr, char** dst) { |
| 1234 |
// minor todo: verify that the tiny amount of additional safety provided by |
| 1235 |
// PtrWSubCk has no/negligible performance cost |
| 1236 |
if (PtrWSubCk(arena_bottom, slen + 1, arena_top_ptr)) { |
| 1237 |
return 1; |
| 1238 |
} |
| 1239 |
memcpyx(*arena_top_ptr, src, slen, '\0'); |
| 1240 |
*dst = R_CAST(char*, *arena_top_ptr); |
| 1241 |
return 0; |
| 1242 |
} |
| 1243 |
|
| 1244 |
HEADER_INLINE BoolErr StoreStringAtEndK(unsigned char* arena_bottom, const char* src, uintptr_t slen, unsigned char** arena_top_ptr, const char** dst) { |
| 1245 |
if (PtrWSubCk(arena_bottom, slen + 1, arena_top_ptr)) { |
| 1246 |
return 1; |
| 1247 |
} |
| 1248 |
memcpyx(*arena_top_ptr, src, slen, '\0'); |
| 1249 |
*dst = R_CAST(char*, *arena_top_ptr); |
| 1250 |
return 0; |
| 1251 |
} |
| 1252 |
|
| 1253 |
HEADER_INLINE BoolErr StoreStringAndPrecharAtEnd(unsigned char* arena_bottom, const char* src, unsigned char prechar, uintptr_t slen, unsigned char** arena_top_ptr, char** dst) { |
| 1254 |
if (PtrWSubCk(arena_bottom, slen + 2, arena_top_ptr)) { |
| 1255 |
return 1; |
| 1256 |
} |
| 1257 |
**arena_top_ptr = prechar; |
| 1258 |
char* dst_write = 1 + R_CAST(char*, *arena_top_ptr); |
| 1259 |
memcpyx(dst_write, src, slen, '\0'); |
| 1260 |
*dst = dst_write; |
| 1261 |
return 0; |
| 1262 |
} |
| 1263 |
|
| 1264 |
// Memory layout: |
| 1265 |
// [ strset ][packed strings] |
| 1266 |
// ^ |
| 1267 |
// | |
| 1268 |
// arena_bottom |
| 1269 |
// strset pointers do not remain valid after resize. |
| 1270 |
|
| 1271 |
// returns 1 if resize needed, 2 if OOM |
| 1272 |
// uint32_t StrsetAdd(unsigned char* arena_top, const char* src, uint32_t slen, uint32_t strset_table_size, char** strset, uint32_t* str_ctp, unsigned char** arena_bottom_ptr); |
| 1273 |
|
| 1274 |
// uint32_t StrsetAddEnd(unsigned char* arena_bottom, const char* src, uint32_t slen, uint32_t strset_table_size, char** strset, uint32_t* str_ctp, unsigned char** arena_top_ptr); |
| 1275 |
|
| 1276 |
BoolErr StrsetAddResize(unsigned char* arena_top, const char* src, uint32_t slen, uint32_t strset_table_size_max, char** strset, uint32_t* strset_table_sizep, uint32_t* str_ctp, unsigned char** arena_bottom_ptr); |
| 1277 |
|
| 1278 |
BoolErr StrsetAddEndResize(unsigned char* arena_bottom, const char* src, uint32_t slen, uint32_t strset_table_size_max, char*** strsetp, uint32_t* strset_table_sizep, uint32_t* str_ctp, unsigned char** arena_top_ptr); |
| 1279 |
|
| 1280 |
// These use g_textbuf. |
| 1281 |
PglErr WriteSampleIdsOverride(const uintptr_t* sample_include, const SampleIdInfo* siip, const char* outname, uint32_t sample_ct, SampleIdFlags override_flags); |
| 1282 |
|
| 1283 |
HEADER_INLINE PglErr WriteSampleIds(const uintptr_t* sample_include, const SampleIdInfo* siip, const char* outname, uint32_t sample_ct) { |
| 1284 |
return WriteSampleIdsOverride(sample_include, siip, outname, sample_ct, siip->flags); |
| 1285 |
} |
| 1286 |
|
| 1287 |
// read_realpath must be a buffer of size >= kPglFnamesize bytes |
| 1288 |
uint32_t RealpathIdentical(const char* outname, const char* read_realpath, char* write_realpath_buf); |
| 1289 |
|
| 1290 |
// assumes rawval is in [1, 32767] |
| 1291 |
static_assert(kDosageMax == 32768, "PrintHaploidNonintDosage() needs to be updated."); |
| 1292 |
HEADER_INLINE char* PrintHaploidNonintDosage(uint32_t rawval, char* start) { |
| 1293 |
// Instead of constant 5-digit precision, we print fewer digits whenever that |
| 1294 |
// doesn't interfere with proper round-tripping. I.e. we search for the |
| 1295 |
// shortest string in |
| 1296 |
// ((n - 0.5)/32768, (n + 0.5)/32768). |
| 1297 |
assert(rawval - 1 < 32767); |
| 1298 |
*start++ = '0'; |
| 1299 |
return PrintDdosageDecimal(rawval, start); |
| 1300 |
} |
| 1301 |
|
| 1302 |
HEADER_INLINE char* PrintHaploidDosage(uint32_t rawval, char* start) { |
| 1303 |
*start++ = '0' + (rawval / kDosageMax); |
| 1304 |
const uint32_t remainder = rawval % kDosageMax; |
| 1305 |
if (!remainder) { |
| 1306 |
return start; |
| 1307 |
} |
| 1308 |
return PrintDdosageDecimal(remainder, start); |
| 1309 |
} |
| 1310 |
|
| 1311 |
char* PrintMultiallelicHcAsDs(uint32_t hc1, uint32_t hc2, uint32_t allele_ct, char* start); |
| 1312 |
|
| 1313 |
char* PrintMultiallelicHcAsHaploidDs(uint32_t hc1, uint32_t hc2, uint32_t allele_ct, char* start); |
| 1314 |
|
| 1315 |
|
| 1316 |
HEADER_INLINE void OutnameZstSet(const char* ext, uint32_t output_zst, char* outname_end) { |
| 1317 |
const uint32_t ext_slen = strlen(ext); |
| 1318 |
assert(ext_slen < kMaxOutfnameExtBlen - 4); |
| 1319 |
memcpy(outname_end, ext, ext_slen + 1); |
| 1320 |
if (output_zst) { |
| 1321 |
strcpy_k(&(outname_end[ext_slen]), ".zst"); |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
ENUM_U31_DEF_START() |
| 1326 |
kVfilterExtract, |
| 1327 |
kVfilterExtractIntersect, |
| 1328 |
kVfilterExclude |
| 1329 |
ENUM_U31_DEF_END(VfilterType); |
| 1330 |
|
| 1331 |
extern const char g_vft_names[3][18]; |
| 1332 |
|
| 1333 |
HEADER_INLINE BoolErr CleanupPgfi2(const char* file_descrip, PgenFileInfo* pgfip, PglErr* reterrp) { |
| 1334 |
if (CleanupPgfi(pgfip, reterrp)) { |
| 1335 |
logerrprintfww(kErrprintfFread, file_descrip, strerror(errno)); |
| 1336 |
return 1; |
| 1337 |
} |
| 1338 |
return 0; |
| 1339 |
} |
| 1340 |
|
| 1341 |
HEADER_INLINE BoolErr CleanupPgr2(const char* file_descrip, PgenReader* pgrp, PglErr* reterrp) { |
| 1342 |
if (CleanupPgr(pgrp, reterrp)) { |
| 1343 |
logerrprintfww(kErrprintfFread, file_descrip, strerror(errno)); |
| 1344 |
return 1; |
| 1345 |
} |
| 1346 |
return 0; |
| 1347 |
} |
| 1348 |
|
| 1349 |
void PgenErrPrintEx(const char* file_descrip, uint32_t prepend_lf, PglErr reterr, uint32_t variant_uidx); |
| 1350 |
|
| 1351 |
HEADER_INLINE void PgenErrPrintN(PglErr reterr) { |
| 1352 |
PgenErrPrintEx(".pgen file", 1, reterr, UINT32_MAX); |
| 1353 |
} |
| 1354 |
|
| 1355 |
HEADER_INLINE void PgenErrPrintNV(PglErr reterr, uint32_t variant_uidx) { |
| 1356 |
PgenErrPrintEx(".pgen file", 1, reterr, variant_uidx); |
| 1357 |
} |
| 1358 |
|
| 1359 |
HEADER_INLINE void PgenErrPrint(PglErr reterr) { |
| 1360 |
PgenErrPrintEx(".pgen file", 0, reterr, UINT32_MAX); |
| 1361 |
} |
| 1362 |
|
| 1363 |
HEADER_INLINE void PgenErrPrintV(PglErr reterr, uint32_t variant_uidx) { |
| 1364 |
PgenErrPrintEx(".pgen file", 0, reterr, variant_uidx); |
| 1365 |
} |
| 1366 |
|
| 1367 |
// This should be called on SpgwFinish() failure when kPgenWriteAndCopy mode |
| 1368 |
// may have been used; otherwise it's unnecessary. |
| 1369 |
void PgenWriteFinishErrPrint(PglErr reterr, char* outname, char* outname_end); |
| 1370 |
|
| 1371 |
// Given <outname>.tmp.pgen and <outname>.tmp.pgen.pgi, this generates |
| 1372 |
// <outname>.pgen and then deletes the two temporary files. |
| 1373 |
PglErr EmbedPgenIndex(char* outname, char* outname_end); |
| 1374 |
|
| 1375 |
// Before 3 Jan 2023, PLINK 2 usually assumed that in INFO= and FORMAT= VCF |
| 1376 |
// header lines, the ID key-value pair appeared first, then Number, then Type. |
| 1377 |
// However, the 2022 Byrska-Bishop et al. 1000 Genomes hg38 callset includes a |
| 1378 |
// a header line where Type appears before Number, the VCF specification does |
| 1379 |
// not clearly forbid this, and parsing of these header lines takes negligible |
| 1380 |
// time in practically all real workflows, so it's time to support arbitrary |
| 1381 |
// key ordering. |
| 1382 |
// |
| 1383 |
// Assumptions: |
| 1384 |
// - The line is \n-terminated. |
| 1385 |
// - It's safe to read a few bytes past the end of the line. |
| 1386 |
CXXCONST_CP HkvlineValEnd(const char* val_start); |
| 1387 |
|
| 1388 |
#ifdef __cplusplus |
| 1389 |
HEADER_INLINE char* HkvlineValEnd(char* val_start) { |
| 1390 |
return const_cast<char*>(HkvlineValEnd(const_cast<const char*>(val_start))); |
| 1391 |
} |
| 1392 |
#endif |
| 1393 |
|
| 1394 |
// returns 1 if key not found, 2 if line malformed |
| 1395 |
IntErr HkvlineFind2K(const char* hkvline_iter, const char* key, uint32_t key_slen, const char** val_ptr, uint32_t* val_slenp); |
| 1396 |
|
| 1397 |
HEADER_INLINE IntErr HkvlineFind2(const char* hkvline_iter, const char* key, uint32_t key_slen, char** val_ptr, uint32_t* val_slenp) { |
| 1398 |
return HkvlineFind2K(hkvline_iter, key, key_slen, K_CAST(const char**, val_ptr), val_slenp); |
| 1399 |
} |
| 1400 |
|
| 1401 |
HEADER_INLINE IntErr HkvlineFindK(const char* hkvline_iter, const char* key, const char** val_ptr, uint32_t* val_slenp) { |
| 1402 |
return HkvlineFind2K(hkvline_iter, key, strlen(key), val_ptr, val_slenp); |
| 1403 |
} |
| 1404 |
|
| 1405 |
HEADER_INLINE IntErr HkvlineFind(const char* hkvline_iter, const char* key, char** val_ptr, uint32_t* val_slenp) { |
| 1406 |
return HkvlineFind2(hkvline_iter, key, strlen(key), val_ptr, val_slenp); |
| 1407 |
} |
| 1408 |
|
| 1409 |
|
| 1410 |
// Input/output: |
| 1411 |
// - hkvline_iterp, initially pointing to the character after the '<'. On exit |
| 1412 |
// success, points to character after '>' if ID is only key; otherwise |
| 1413 |
// beginning of the second key if ID is first key, or stays in place if ID is |
| 1414 |
// positioned later. |
| 1415 |
// Goal is to avoid reparsing the ID entry when we need to look at later |
| 1416 |
// entries. |
| 1417 |
// Output: |
| 1418 |
// - idval_ptr and id_slenp, to be set to start of ID value and ID length, |
| 1419 |
// respectively. |
| 1420 |
// Works on contig=/FILTER= header lines, as well as FORMAT=/INFO=. |
| 1421 |
HEADER_INLINE BoolErr HkvlineIdK(const char** hkvline_iterp, const char** idval_ptr, uint32_t* id_slenp) { |
| 1422 |
const char* hkvline_iter = *hkvline_iterp; |
| 1423 |
if (HkvlineFindK(hkvline_iter, "ID", idval_ptr, id_slenp)) { |
| 1424 |
return 1; |
| 1425 |
} |
| 1426 |
if (*idval_ptr == &(hkvline_iter[1 + strlen("ID")])) { |
| 1427 |
*hkvline_iterp = &((*idval_ptr)[1 + (*id_slenp)]); |
| 1428 |
} |
| 1429 |
return 0; |
| 1430 |
} |
| 1431 |
|
| 1432 |
HEADER_INLINE BoolErr HkvlineId(char** hkvline_iterp, char** idval_ptr, uint32_t* id_slenp) { |
| 1433 |
return HkvlineIdK(K_CAST(const char**, hkvline_iterp), K_CAST(const char**, idval_ptr), id_slenp); |
| 1434 |
} |
| 1435 |
|
| 1436 |
// Convenience function for scraping Number and Type values simultaneously. |
| 1437 |
// (HkvlineFind works when only one of the two is needed.) |
| 1438 |
// Input: |
| 1439 |
// - hkvline_iter, as set by part 1 |
| 1440 |
// Output: |
| 1441 |
// - numstr_ptr and num_slenp, to be set to start of Number value string and |
| 1442 |
// string length, respectively |
| 1443 |
// - typestr_ptr and type_slenp, to be set to start of Type value and Type |
| 1444 |
// length, respectively |
| 1445 |
BoolErr HkvlineNumTypeK(const char* hkvline_iter, const char** numstr_ptr, uint32_t* num_slenp, const char** typestr_ptr, uint32_t* type_slenp); |
| 1446 |
|
| 1447 |
HEADER_INLINE BoolErr HkvlineNumType(char* hkvline_iter, char** numstr_ptr, uint32_t* num_slenp, char** typestr_ptr, uint32_t* type_slenp) { |
| 1448 |
return HkvlineNumTypeK(hkvline_iter, K_CAST(const char**, numstr_ptr), num_slenp, K_CAST(const char**, typestr_ptr), type_slenp); |
| 1449 |
} |
| 1450 |
|
| 1451 |
// In the unlikely event ID= isn't first, move it in front. |
| 1452 |
PglErr HkvlineForceIdFirst(uintptr_t workspace_size, char* hline_kv_start, void* workspace); |
| 1453 |
|
| 1454 |
|
| 1455 |
// 'maybe' bit should be at the bottom of shifted_flag_bits. |
| 1456 |
HEADER_INLINE uint32_t ProvrefCol(const uintptr_t* variant_include, const uintptr_t* nonref_flags, uint32_t shifted_flag_bits, uint32_t raw_variant_ct, uint32_t all_nonref) { |
| 1457 |
if (shifted_flag_bits & 2) { |
| 1458 |
return 1; |
| 1459 |
} |
| 1460 |
if (!(shifted_flag_bits & 1)) { |
| 1461 |
return 0; |
| 1462 |
} |
| 1463 |
if (!nonref_flags) { |
| 1464 |
return all_nonref; |
| 1465 |
} |
| 1466 |
return !IntersectionIsEmpty(variant_include, nonref_flags, BitCtToWordCt(raw_variant_ct)); |
| 1467 |
} |
| 1468 |
|
| 1469 |
PglErr NsortDedupAndWrite(const char* outname, uintptr_t str_ct, uint32_t max_slen, uint32_t output_zst, uint32_t max_thread_ct, const char** strptr_arr, uintptr_t* nwrite_ptr); |
| 1470 |
|
| 1471 |
PglErr AllocAndFillVariantLastAlidxs(const uintptr_t* allele_idx_offsets, uint32_t raw_variant_ct, uint32_t max_thread_ct, const uintptr_t** variant_last_alidxsp, const uint32_t** variant_last_alidxs_cumulative_popcountsp); |
| 1472 |
|
| 1473 |
|
| 1474 |
// Assumes IsSet(variant_include, variant_uidx) is true. |
| 1475 |
// [start_vidx, end_vidx) must be initialized to search bounds. (Could have |
| 1476 |
// wrapper that initializes them to variant_uidx's chromosome bounds.) |
| 1477 |
void GetBpWindow(const uintptr_t* variant_include, const uint32_t* variant_bps, uint32_t variant_uidx, uint32_t bp_radius, uint32_t* start_vidxp, uint32_t* end_vidxp); |
| 1478 |
|
| 1479 |
PgenGlobalFlags GflagsVfilter(const uintptr_t* variant_include, const unsigned char* vrtypes, uint32_t raw_variant_ct, PgenGlobalFlags input_gflags); |
| 1480 |
|
| 1481 |
uint32_t TriangleDivide(int64_t cur_prod_x2, int32_t modif); |
| 1482 |
|
| 1483 |
void ParallelBounds(uint32_t ct, int32_t start, uint32_t parallel_idx, uint32_t parallel_tot, int32_t* __restrict bound_start_ptr, int32_t* __restrict bound_end_ptr); |
| 1484 |
|
| 1485 |
// Unsafe since this may write up to 6 bytes past the end. |
| 1486 |
void AppendZerotabsUnsafe(uint32_t zerotab_ct, char** cswritep_ptr); |
| 1487 |
|
| 1488 |
// Unsafe since this may write up to word boundary past the end. |
| 1489 |
PglErr InitAllelePermuteUnsafe(const uintptr_t* allele_idx_offsets, uint32_t raw_variant_ct, uint32_t max_thread_ct, AlleleCode* allele_permute); |
| 1490 |
|
| 1491 |
HEADER_INLINE void PglLogputsb() { |
| 1492 |
char* pgl_errbuf = PglReturnLog(); |
| 1493 |
if (pgl_errbuf) { |
| 1494 |
WordWrapMultiline(pgl_errbuf); |
| 1495 |
logputs(pgl_errbuf); |
| 1496 |
PglResetLog(); |
| 1497 |
} |
| 1498 |
} |
| 1499 |
|
| 1500 |
#ifdef __cplusplus |
| 1501 |
} // namespace plink2 |
| 1502 |
#endif |
| 1503 |
|
| 1504 |
#endif // __PLINK2_COMMON_H__ |