| 1 |
#ifndef __PGENLIB_WRITE_H__ |
| 2 |
#define __PGENLIB_WRITE_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 |
// pgenlib_write contains writer-specific code. |
| 22 |
|
| 23 |
#include "pgenlib_misc.h" |
| 24 |
|
| 25 |
#ifdef __cplusplus |
| 26 |
namespace plink2 { |
| 27 |
#endif |
| 28 |
|
| 29 |
typedef struct PgenWriterCommonStruct { |
| 30 |
// was marked noncopyable, but, well, gcc 9 caught me cheating (memcpying the |
| 31 |
// whole struct) in the multithreaded writer implementation. So, copyable |
| 32 |
// now. |
| 33 |
|
| 34 |
// This must be the true variant count when writing a regular .pgen; but if |
| 35 |
// the index is being written to a separate .pgen.pgi file, it is okay for |
| 36 |
// this to just be an upper bound. |
| 37 |
uint32_t variant_ct_limit; |
| 38 |
|
| 39 |
uint32_t sample_ct; |
| 40 |
PgenGlobalFlags phase_dosage_gflags; // subset of gflags |
| 41 |
unsigned char nonref_flags_storage; |
| 42 |
|
| 43 |
// I'll cache this for now |
| 44 |
uintptr_t vrec_len_byte_ct; |
| 45 |
|
| 46 |
// There should be a single copy of these arrays shared by all threads. |
| 47 |
uint64_t* vblock_fpos; |
| 48 |
unsigned char* vrec_len_buf; |
| 49 |
uintptr_t* vrtype_buf; |
| 50 |
uintptr_t* explicit_nonref_flags; // usually nullptr |
| 51 |
|
| 52 |
// needed for multiallelic-phased case |
| 53 |
uintptr_t* genovec_hets_buf; |
| 54 |
|
| 55 |
PgenExtensionLl* header_exts; |
| 56 |
PgenExtensionLl* footer_exts; |
| 57 |
|
| 58 |
// should match ftello() return value in singlethreaded case, but be set to |
| 59 |
// zero in multithreaded case |
| 60 |
uint64_t vblock_fpos_offset; |
| 61 |
|
| 62 |
|
| 63 |
// The remainder of this data structure is unshared. |
| 64 |
STD_ARRAY_DECL(uint32_t, 4, ldbase_genocounts); |
| 65 |
|
| 66 |
// these must hold sample_ct entries |
| 67 |
// genovec_invert_buf also used as phaseinfo and dphase_present temporary |
| 68 |
// storage |
| 69 |
uintptr_t* genovec_invert_buf; |
| 70 |
uintptr_t* ldbase_genovec; |
| 71 |
|
| 72 |
// these must hold 2 * (sample_ct / kPglMaxDifflistLenDivisor) entries |
| 73 |
uintptr_t* ldbase_raregeno; |
| 74 |
uint32_t* ldbase_difflist_sample_ids; // 1 extra entry, == sample_ct |
| 75 |
|
| 76 |
// this must fit 64k variants in multithreaded case |
| 77 |
unsigned char* fwrite_buf; |
| 78 |
unsigned char* fwrite_bufp; |
| 79 |
|
| 80 |
uint32_t ldbase_common_geno; // UINT32_MAX if ldbase_genovec present |
| 81 |
uint32_t ldbase_difflist_len; |
| 82 |
|
| 83 |
uint32_t vidx; |
| 84 |
} PgenWriterCommon; |
| 85 |
|
| 86 |
// Given packed arrays of unphased biallelic genotypes in uncompressed plink2 |
| 87 |
// binary format (00 = hom ref, 01 = het ref/alt1, 10 = hom alt1, 11 = |
| 88 |
// missing), {S,M}TPgenWriter performs difflist (sparse variant), one bit |
| 89 |
// (mostly-two-value), and LD compression before writing to disk, and flushes |
| 90 |
// the header at the end. |
| 91 |
// |
| 92 |
// The flush requires a backward seek if write_mode is set to |
| 93 |
// kPgenWriteBackwardSeek during initialization. Otherwise, the (possibly |
| 94 |
// temporary) .pgen is written sequentially; in the kPgenWriteSeparateIndex |
| 95 |
// mode, the index is then saved to a separate .pgen.pgi file, while in the |
| 96 |
// kPgenWriteAndCopy mode, the real .pgen is only created at the end, by |
| 97 |
// writing the index and then appending the body of the first .pgen (which is |
| 98 |
// then deleted). |
| 99 |
// |
| 100 |
// Slightly over 128 KiB (kPglFwriteBlockSize) of stack space is currently |
| 101 |
// required. We use plain fwrite() instead of write() to reduce the amount of |
| 102 |
// necessary platform-specific logic; a 128 KiB write-block size keeps the |
| 103 |
// associated performance penalty to a minimum. |
| 104 |
// |
| 105 |
// MTPgenWriter has additional restrictions: |
| 106 |
// * You must process large blocks of variants at a time (64k per thread). |
| 107 |
// * You must know the true variant_ct (and, in the multiallelic case, the true |
| 108 |
// allele_idx_offsets) during initialization. |
| 109 |
// Thus, STPgenWriter is still worth using in many cases. |
| 110 |
|
| 111 |
ENUM_U31_DEF_START() |
| 112 |
kPgenWriteBackwardSeek, |
| 113 |
kPgenWriteSeparateIndex, |
| 114 |
kPgenWriteAndCopy |
| 115 |
ENUM_U31_DEF_END(PgenWriteMode); |
| 116 |
|
| 117 |
typedef struct STPgenWriterStruct { |
| 118 |
MOVABLE_BUT_NONCOPYABLE(STPgenWriterStruct); |
| 119 |
#ifdef __cplusplus |
| 120 |
PgenWriterCommon& GET_PRIVATE_pwc() { return pwc; } |
| 121 |
PgenWriterCommon const& GET_PRIVATE_pwc() const { return pwc; } |
| 122 |
FILE*& GET_PRIVATE_pgen_outfile() { return pgen_outfile; } |
| 123 |
FILE* const& GET_PRIVATE_pgen_outfile() const { return pgen_outfile; } |
| 124 |
FILE*& GET_PRIVATE_pgi_or_final_pgen_outfile() { return pgi_or_final_pgen_outfile; } |
| 125 |
FILE* const& GET_PRIVATE_pgi_or_final_pgen_outfile() const { return pgi_or_final_pgen_outfile; } |
| 126 |
char*& GET_PRIVATE_fname_buf() { return fname_buf; } |
| 127 |
char* const& GET_PRIVATE_fname_buf() const { return fname_buf; } |
| 128 |
private: |
| 129 |
#endif |
| 130 |
PgenWriterCommon pwc; |
| 131 |
FILE* pgen_outfile; |
| 132 |
FILE* pgi_or_final_pgen_outfile; |
| 133 |
// Initialized in kPgenWriteAndCopy mode. It is necessary on at least macOS |
| 134 |
// to close and reopen the initially-written .pgen; freopen() with |
| 135 |
// filename=nullptr can't be used to change from write-mode to read-mode. |
| 136 |
char* fname_buf; |
| 137 |
} STPgenWriter; |
| 138 |
|
| 139 |
typedef struct MTPgenWriterStruct { |
| 140 |
MOVABLE_BUT_NONCOPYABLE(MTPgenWriterStruct); |
| 141 |
FILE* pgen_outfile; |
| 142 |
FILE* pgi_or_final_pgen_outfile; |
| 143 |
char* fname_buf; |
| 144 |
uint32_t thread_ct; |
| 145 |
PgenWriterCommon* pwcs[]; |
| 146 |
} MTPgenWriter; |
| 147 |
|
| 148 |
HEADER_INLINE uint32_t SpgwGetVariantCt(STPgenWriter* spgwp) { |
| 149 |
PgenWriterCommon* pwcp = &GET_PRIVATE(*spgwp, pwc); |
| 150 |
return pwcp->variant_ct_limit; |
| 151 |
} |
| 152 |
|
| 153 |
HEADER_INLINE uint32_t SpgwGetSampleCt(STPgenWriter* spgwp) { |
| 154 |
PgenWriterCommon* pwcp = &GET_PRIVATE(*spgwp, pwc); |
| 155 |
return pwcp->sample_ct; |
| 156 |
} |
| 157 |
|
| 158 |
HEADER_INLINE uint32_t SpgwGetVidx(STPgenWriter* spgwp) { |
| 159 |
PgenWriterCommon* pwcp = &GET_PRIVATE(*spgwp, pwc); |
| 160 |
return pwcp->vidx; |
| 161 |
} |
| 162 |
|
| 163 |
void PreinitSpgw(STPgenWriter* spgwp); |
| 164 |
|
| 165 |
void PreinitMpgw(MTPgenWriter* mpgwp); |
| 166 |
|
| 167 |
// phase_dosage_gflags zero vs. nonzero is most important: this determines size |
| 168 |
// of header. Otherwise, setting more flags than necessary just increases |
| 169 |
// memory requirements. |
| 170 |
// |
| 171 |
// nonref_flags_storage values: |
| 172 |
// 0 = no info stored |
| 173 |
// 1 = always trusted |
| 174 |
// 2 = always untrusted |
| 175 |
// 3 = use explicit_nonref_flags |
| 176 |
// |
| 177 |
// Caller is responsible for printing open-fail error message. |
| 178 |
// |
| 179 |
// In the multiallelic case, allele_idx_offsets can be nullptr if |
| 180 |
// allele_ct_upper_bound is provided. |
| 181 |
// |
| 182 |
// If write_mode is kPgenWriteBackwardSeek, variant_ct_limit must be the actual |
| 183 |
// variant count. Otherwise, variant_ct_limit just needs to be an upper bound, |
| 184 |
// unless you're writing multiallelic variants and you don't specify |
| 185 |
// allele_ct_upper_bound. If you're using a plink2-style memory workspace, |
| 186 |
// variant_ct_limit := min(kPglMaxVariantCt, bigstack_left() / 8) is a |
| 187 |
// reasonable default: this tends to consume less than half of your remaining |
| 188 |
// workspace, without falling below the number of variants you can |
| 189 |
// realistically load on the system. |
| 190 |
// |
| 191 |
// The body of explicit_nonref_flags doesn't need to be filled until final |
| 192 |
// flush. |
| 193 |
// For header extensions, type_idx and size must be correct when this function |
| 194 |
// is called, but contents doesn't need to be filled until final flush. |
| 195 |
// For footer extensions, type_idx must be correct when this function is |
| 196 |
// called, but size and contents don't need to be filled until final flush. |
| 197 |
// Extension linked-lists must be ordered with lower type_idx first. |
| 198 |
PglErr SpgwInitPhase1Ex(const char* __restrict fname, const uintptr_t* __restrict allele_idx_offsets, uintptr_t* __restrict explicit_nonref_flags, PgenExtensionLl* header_exts, PgenExtensionLl* footer_exts, uint32_t variant_ct_limit, uint32_t sample_ct, uint32_t allele_ct_upper_bound, PgenWriteMode write_mode, PgenGlobalFlags phase_dosage_gflags, uint32_t nonref_flags_storage, STPgenWriter* spgwp, uintptr_t* alloc_cacheline_ct_ptr, uint32_t* max_vrec_len_ptr); |
| 199 |
|
| 200 |
HEADER_INLINE PglErr SpgwInitPhase1(const char* __restrict fname, const uintptr_t* __restrict allele_idx_offsets, uintptr_t* __restrict explicit_nonref_flags, uint32_t variant_ct_limit, uint32_t sample_ct, uint32_t allele_ct_upper_bound, PgenWriteMode write_mode, PgenGlobalFlags phase_dosage_gflags, uint32_t nonref_flags_storage, STPgenWriter* spgwp, uintptr_t* alloc_cacheline_ct_ptr, uint32_t* max_vrec_len_ptr) { |
| 201 |
return SpgwInitPhase1Ex(fname, allele_idx_offsets, explicit_nonref_flags, nullptr, nullptr, variant_ct_limit, sample_ct, allele_ct_upper_bound, write_mode, phase_dosage_gflags, nonref_flags_storage, spgwp, alloc_cacheline_ct_ptr, max_vrec_len_ptr); |
| 202 |
} |
| 203 |
|
| 204 |
void SpgwInitPhase2(uint32_t max_vrec_len, STPgenWriter* spgwp, unsigned char* spgw_alloc); |
| 205 |
|
| 206 |
// moderately likely that there isn't enough memory to use the maximum number |
| 207 |
// of threads, so this returns per-thread memory requirements before forcing |
| 208 |
// the caller to specify thread count |
| 209 |
// (eventually should write code which falls back on STPgenWriter |
| 210 |
// when there isn't enough memory for even a single 64k variant block, at least |
| 211 |
// for the most commonly used plink 2.0 functions) |
| 212 |
void MpgwInitPhase1(const uintptr_t* __restrict allele_idx_offsets, uint32_t variant_ct, uint32_t sample_ct, PgenGlobalFlags phase_dosage_gflags, uintptr_t* alloc_base_cacheline_ct_ptr, uint64_t* alloc_per_thread_cacheline_ct_ptr, uint32_t* vrec_len_byte_ct_ptr, uint64_t* vblock_cacheline_ct_ptr); |
| 213 |
|
| 214 |
// Caller is responsible for printing open-fail error message. |
| 215 |
PglErr MpgwInitPhase2Ex(const char* __restrict fname, uintptr_t* __restrict explicit_nonref_flags, PgenExtensionLl* header_exts, PgenExtensionLl* footer_exts, uint32_t variant_ct, uint32_t sample_ct, PgenWriteMode write_mode, PgenGlobalFlags phase_dosage_gflags, uint32_t nonref_flags_storage, uint32_t vrec_len_byte_ct, uintptr_t vblock_cacheline_ct, uint32_t thread_ct, unsigned char* mpgw_alloc, MTPgenWriter* mpgwp); |
| 216 |
|
| 217 |
HEADER_INLINE PglErr MpgwInitPhase2(const char* __restrict fname, uintptr_t* __restrict explicit_nonref_flags, uint32_t variant_ct, uint32_t sample_ct, PgenWriteMode write_mode, PgenGlobalFlags phase_dosage_gflags, uint32_t nonref_flags_storage, uint32_t vrec_len_byte_ct, uintptr_t vblock_cacheline_ct, uint32_t thread_ct, unsigned char* mpgw_alloc, MTPgenWriter* mpgwp) { |
| 218 |
return MpgwInitPhase2Ex(fname, explicit_nonref_flags, nullptr, nullptr, variant_ct, sample_ct, write_mode, phase_dosage_gflags, nonref_flags_storage, vrec_len_byte_ct, vblock_cacheline_ct, thread_ct, mpgw_alloc, mpgwp); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
// trailing bits of genovec must be zeroed out |
| 223 |
void PwcAppendBiallelicGenovec(const uintptr_t* __restrict genovec, PgenWriterCommon* pwcp); |
| 224 |
|
| 225 |
BoolErr SpgwFlush(STPgenWriter* spgwp); |
| 226 |
|
| 227 |
HEADER_INLINE PglErr SpgwAppendBiallelicGenovec(const uintptr_t* __restrict genovec, STPgenWriter* spgwp) { |
| 228 |
if (unlikely(SpgwFlush(spgwp))) { |
| 229 |
return kPglRetWriteFail; |
| 230 |
} |
| 231 |
PgenWriterCommon* pwcp = &GET_PRIVATE(*spgwp, pwc); |
| 232 |
PwcAppendBiallelicGenovec(genovec, pwcp); |
| 233 |
return kPglRetSuccess; |
| 234 |
} |
| 235 |
|
| 236 |
// trailing bits of raregeno must be zeroed out |
| 237 |
// all raregeno entries assumed to be unequal to difflist_common_geno; the |
| 238 |
// difflist should be compacted first if this isn't true |
| 239 |
// difflist_len must be <= 2 * (sample_ct / kPglMaxDifflistLenDivisor); |
| 240 |
// there's an assert checking this |
| 241 |
void PwcAppendBiallelicDifflistLimited(const uintptr_t* __restrict raregeno, const uint32_t* __restrict difflist_sample_ids, uint32_t difflist_common_geno, uint32_t difflist_len, PgenWriterCommon* pwcp); |
| 242 |
|
| 243 |
HEADER_INLINE PglErr SpgwAppendBiallelicDifflistLimited(const uintptr_t* __restrict raregeno, const uint32_t* __restrict difflist_sample_ids, uint32_t difflist_common_geno, uint32_t difflist_len, STPgenWriter* spgwp) { |
| 244 |
if (unlikely(SpgwFlush(spgwp))) { |
| 245 |
return kPglRetWriteFail; |
| 246 |
} |
| 247 |
PgenWriterCommon* pwcp = &GET_PRIVATE(*spgwp, pwc); |
| 248 |
PwcAppendBiallelicDifflistLimited(raregeno, difflist_sample_ids, difflist_common_geno, difflist_len, pwcp); |
| 249 |
return kPglRetSuccess; |
| 250 |
} |
| 251 |
|
| 252 |
// Two interfaces for appending multiallelic hardcalls: |
| 253 |
// 1. sparse: genovec, bitarray+values describing ref/altx hardcalls which |
| 254 |
// aren't ref/alt1, bitarray+values describing altx/alty hardcalls which |
| 255 |
// aren't alt1/alt1. |
| 256 |
// patch_01_vals[] contains one entry per relevant sample (only need altx |
| 257 |
// index), patch_10_vals[] contains two. |
| 258 |
// Ok if patch_01_ct == patch_10_ct == 0; in this case no aux1 track is |
| 259 |
// saved and bit 3 of vrtype is not set. (Note that multiallelic dosage may |
| 260 |
// still be present when vrtype bit 3 is unset.) |
| 261 |
// 2. generic dense: takes a length-2n array of AlleleCode allele codes. |
| 262 |
// Assumes [2k] <= [2k+1] for each k. Instead of providing direct API |
| 263 |
// functions for this, we just provide a dense -> sparse helper function. |
| 264 |
// |
| 265 |
// All arrays should be vector-aligned. |
| 266 |
BoolErr PwcAppendMultiallelicSparse(const uintptr_t* __restrict genovec, const uintptr_t* __restrict patch_01_set, const AlleleCode* __restrict patch_01_vals, const uintptr_t* __restrict patch_10_set, const AlleleCode* __restrict patch_10_vals, uint32_t allele_ct, uint32_t patch_01_ct, uint32_t patch_10_ct, PgenWriterCommon* pwcp); |
| 267 |
|
| 268 |
HEADER_INLINE PglErr SpgwAppendMultiallelicSparse(const uintptr_t* __restrict genovec, const uintptr_t* __restrict patch_01_set, const AlleleCode* __restrict patch_01_vals, const uintptr_t* __restrict patch_10_set, const AlleleCode* __restrict patch_10_vals, uint32_t allele_ct, uint32_t patch_01_ct, uint32_t patch_10_ct, STPgenWriter* spgwp) { |
| 269 |
if (unlikely(SpgwFlush(spgwp))) { |
| 270 |
return kPglRetWriteFail; |
| 271 |
} |
| 272 |
PgenWriterCommon* pwcp = &GET_PRIVATE(*spgwp, pwc); |
| 273 |
if (unlikely(PwcAppendMultiallelicSparse(genovec, patch_01_set, patch_01_vals, patch_10_set, patch_10_vals, allele_ct, patch_01_ct, patch_10_ct, pwcp))) { |
| 274 |
return kPglRetVarRecordTooLarge; |
| 275 |
} |
| 276 |
return kPglRetSuccess; |
| 277 |
} |
| 278 |
|
| 279 |
// This may not zero out trailing halfword of patch_{01,10}_set. |
| 280 |
void PglMultiallelicDenseToSparse(const AlleleCode* __restrict wide_codes, uint32_t sample_ct, uintptr_t* __restrict genoarr, uintptr_t* __restrict patch_01_set, AlleleCode* __restrict patch_01_vals, uintptr_t* __restrict patch_10_set, AlleleCode* __restrict patch_10_vals, uint32_t* __restrict patch_01_ct_ptr, uint32_t* __restrict patch_10_ct_ptr); |
| 281 |
|
| 282 |
// If remap is not nullptr, this simultaneously performs a rotation operation: |
| 283 |
// wide_codes[2n] and [2n+1] are set to remap[geno[n]] rather than geno[n], and |
| 284 |
// bit n of flipped (if flipped non-null) is set iff phase orientation is |
| 285 |
// flipped (i.e. wide_codes[2n] was larger than wide_codes[2n+1] before the |
| 286 |
// final reordering pass; caller needs to know this to properly update |
| 287 |
// phaseinfo, dphase_delta, multidphase_delta). |
| 288 |
// It currently assumes no present alleles are being mapped to 'missing'. |
| 289 |
void PglMultiallelicSparseToDense(const uintptr_t* __restrict genovec, const uintptr_t* __restrict patch_01_set, const AlleleCode* __restrict patch_01_vals, const uintptr_t* __restrict patch_10_set, const AlleleCode* __restrict patch_10_vals, const AlleleCode* __restrict remap, uint32_t sample_ct, uint32_t patch_01_ct, uint32_t patch_10_ct, uintptr_t* __restrict flipped, AlleleCode* __restrict wide_codes); |
| 290 |
|
| 291 |
// phasepresent == nullptr ok, that indicates that ALL heterozygous calls are |
| 292 |
// phased. Caller should use e.g. PwcAppendBiallelicGenovec() if it's known |
| 293 |
// in advance that no calls are phased. |
| 294 |
// Ok for phaseinfo to have bits set at non-het calls, NOT currently okay for |
| 295 |
// phasepresent |
| 296 |
void PwcAppendBiallelicGenovecHphase(const uintptr_t* __restrict genovec, const uintptr_t* __restrict phasepresent, const uintptr_t* __restrict phaseinfo, PgenWriterCommon* pwcp); |
| 297 |
|
| 298 |
// phasepresent == nullptr ok |
| 299 |
// ok for trailing bits of phaseinfo to not be zeroed out, NOT currently ok for |
| 300 |
// phasepresent |
| 301 |
HEADER_INLINE PglErr SpgwAppendBiallelicGenovecHphase(const uintptr_t* __restrict genovec, const uintptr_t* __restrict phasepresent, const uintptr_t* __restrict phaseinfo, STPgenWriter* spgwp) { |
| 302 |
if (unlikely(SpgwFlush(spgwp))) { |
| 303 |
return kPglRetWriteFail; |
| 304 |
} |
| 305 |
PgenWriterCommon* pwcp = &GET_PRIVATE(*spgwp, pwc); |
| 306 |
PwcAppendBiallelicGenovecHphase(genovec, phasepresent, phaseinfo, pwcp); |
| 307 |
return kPglRetSuccess; |
| 308 |
} |
| 309 |
|
| 310 |
BoolErr PwcAppendMultiallelicGenovecHphase(const uintptr_t* __restrict genovec, const uintptr_t* __restrict patch_01_set, const AlleleCode* __restrict patch_01_vals, const uintptr_t* __restrict patch_10_set, const AlleleCode* __restrict patch_10_vals, const uintptr_t* __restrict phasepresent, const uintptr_t* __restrict phaseinfo, uint32_t allele_ct, uint32_t patch_01_ct, uint32_t patch_10_ct, PgenWriterCommon* pwcp); |
| 311 |
|
| 312 |
HEADER_INLINE PglErr SpgwAppendMultiallelicGenovecHphase(const uintptr_t* __restrict genovec, const uintptr_t* __restrict patch_01_set, const AlleleCode* __restrict patch_01_vals, const uintptr_t* __restrict patch_10_set, const AlleleCode* __restrict patch_10_vals, const uintptr_t* __restrict phasepresent, const uintptr_t* __restrict phaseinfo, uint32_t allele_ct, uint32_t patch_01_ct, uint32_t patch_10_ct, STPgenWriter* spgwp) { |
| 313 |
if (unlikely(SpgwFlush(spgwp))) { |
| 314 |
return kPglRetWriteFail; |
| 315 |
} |
| 316 |
PgenWriterCommon* pwcp = &GET_PRIVATE(*spgwp, pwc); |
| 317 |
if (unlikely(PwcAppendMultiallelicGenovecHphase(genovec, patch_01_set, patch_01_vals, patch_10_set, patch_10_vals, phasepresent, phaseinfo, allele_ct, patch_01_ct, patch_10_ct, pwcp))) { |
| 318 |
return kPglRetVarRecordTooLarge; |
| 319 |
} |
| 320 |
return kPglRetSuccess; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
// dosage_main[] has length dosage_ct, not sample_ct |
| 325 |
// ok for trailing bits of dosage_present to not be zeroed out |
| 326 |
BoolErr PwcAppendBiallelicGenovecDosage16(const uintptr_t* __restrict genovec, const uintptr_t* __restrict dosage_present, const uint16_t* dosage_main, uint32_t dosage_ct, PgenWriterCommon* pwcp); |
| 327 |
|
| 328 |
HEADER_INLINE PglErr SpgwAppendBiallelicGenovecDosage16(const uintptr_t* __restrict genovec, const uintptr_t* __restrict dosage_present, const uint16_t* dosage_main, uint32_t dosage_ct, STPgenWriter* spgwp) { |
| 329 |
if (unlikely(SpgwFlush(spgwp))) { |
| 330 |
return kPglRetWriteFail; |
| 331 |
} |
| 332 |
PgenWriterCommon* pwcp = &GET_PRIVATE(*spgwp, pwc); |
| 333 |
if (unlikely(PwcAppendBiallelicGenovecDosage16(genovec, dosage_present, dosage_main, dosage_ct, pwcp))) { |
| 334 |
return kPglRetVarRecordTooLarge; |
| 335 |
} |
| 336 |
return kPglRetSuccess; |
| 337 |
} |
| 338 |
|
| 339 |
BoolErr PwcAppendBiallelicGenovecHphaseDosage16(const uintptr_t* __restrict genovec, const uintptr_t* __restrict phasepresent, const uintptr_t* __restrict phaseinfo, const uintptr_t* __restrict dosage_present, const uint16_t* dosage_main, uint32_t dosage_ct, PgenWriterCommon* pwcp); |
| 340 |
|
| 341 |
HEADER_INLINE PglErr SpgwAppendBiallelicGenovecHphaseDosage16(const uintptr_t* __restrict genovec, const uintptr_t* __restrict phasepresent, const uintptr_t* __restrict phaseinfo, const uintptr_t* __restrict dosage_present, const uint16_t* dosage_main, uint32_t dosage_ct, STPgenWriter* spgwp) { |
| 342 |
if (unlikely(SpgwFlush(spgwp))) { |
| 343 |
return kPglRetWriteFail; |
| 344 |
} |
| 345 |
PgenWriterCommon* pwcp = &GET_PRIVATE(*spgwp, pwc); |
| 346 |
if (unlikely(PwcAppendBiallelicGenovecHphaseDosage16(genovec, phasepresent, phaseinfo, dosage_present, dosage_main, dosage_ct, pwcp))) { |
| 347 |
return kPglRetVarRecordTooLarge; |
| 348 |
} |
| 349 |
return kPglRetSuccess; |
| 350 |
} |
| 351 |
|
| 352 |
// dosage_present cannot be null for nonzero dosage_ct |
| 353 |
// trailing bits of dosage_present MUST be zeroed out |
| 354 |
// could make dosage_main[] has length dosage_ct + dphase_ct instead of having |
| 355 |
// separate dphase_delta[]? |
| 356 |
BoolErr PwcAppendBiallelicGenovecDphase16(const uintptr_t* __restrict genovec, const uintptr_t* __restrict phasepresent, const uintptr_t* __restrict phaseinfo, const uintptr_t* __restrict dosage_present, const uintptr_t* __restrict dphase_present, const uint16_t* dosage_main, const int16_t* dphase_delta, uint32_t dosage_ct, uint32_t dphase_ct, PgenWriterCommon* pwcp); |
| 357 |
|
| 358 |
HEADER_INLINE PglErr SpgwAppendBiallelicGenovecDphase16(const uintptr_t* __restrict genovec, const uintptr_t* __restrict phasepresent, const uintptr_t* __restrict phaseinfo, const uintptr_t* __restrict dosage_present, const uintptr_t* dphase_present, const uint16_t* dosage_main, const int16_t* dphase_delta, uint32_t dosage_ct, uint32_t dphase_ct, STPgenWriter* spgwp) { |
| 359 |
if (unlikely(SpgwFlush(spgwp))) { |
| 360 |
return kPglRetWriteFail; |
| 361 |
} |
| 362 |
PgenWriterCommon* pwcp = &GET_PRIVATE(*spgwp, pwc); |
| 363 |
if (unlikely(PwcAppendBiallelicGenovecDphase16(genovec, phasepresent, phaseinfo, dosage_present, dphase_present, dosage_main, dphase_delta, dosage_ct, dphase_ct, pwcp))) { |
| 364 |
return kPglRetVarRecordTooLarge; |
| 365 |
} |
| 366 |
return kPglRetSuccess; |
| 367 |
} |
| 368 |
|
| 369 |
// Writes footer if present, backfills header, then closes the file. |
| 370 |
PglErr SpgwFinish(STPgenWriter* spgwp); |
| 371 |
|
| 372 |
// Last flush automatically writes footer if present, backfills header, and |
| 373 |
// closes the file. |
| 374 |
// (caller should set mpgwp = nullptr after that) |
| 375 |
PglErr MpgwFlush(MTPgenWriter* mpgwp); |
| 376 |
|
| 377 |
|
| 378 |
// these close the file if open, but do not free any memory |
| 379 |
// MpgwCleanup() handles mpgwp == nullptr, since it shouldn't be allocated on |
| 380 |
// the stack |
| 381 |
// error-return iff reterr was success and was changed to kPglRetWriteFail |
| 382 |
// (i.e. an error message should be printed), though this is not relevant for |
| 383 |
// plink2 |
| 384 |
BoolErr CleanupSpgw(STPgenWriter* spgwp, PglErr* reterrp); |
| 385 |
|
| 386 |
BoolErr CleanupMpgw(MTPgenWriter* mpgwp, PglErr* reterrp); |
| 387 |
|
| 388 |
#ifdef __cplusplus |
| 389 |
} // namespace plink2 |
| 390 |
#endif |
| 391 |
|
| 392 |
#endif // __PGENLIB_WRITE_H__ |