| 1 |
// This file is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 2 |
// Christopher Chang. |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify it |
| 5 |
// under the terms of the GNU General Public License as published by the Free |
| 6 |
// Software Foundation, either version 3 of the License, or (at your option) |
| 7 |
// any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 |
// more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
|
| 18 |
#include "plink2_common.h" |
| 19 |
#include "plink2_compress_stream.h" |
| 20 |
|
| 21 |
// This covers formats that are fully supported by PLINK 1.x (no multiallelic |
| 22 |
// variants, dosages, or phase information). |
| 23 |
|
| 24 |
#ifdef __cplusplus |
| 25 |
namespace plink2 { |
| 26 |
#endif |
| 27 |
|
| 28 |
typedef struct TransposeToSmajReadCtxStruct { |
| 29 |
const uintptr_t* variant_include; |
| 30 |
const uintptr_t* allele_idx_offsets; |
| 31 |
const AlleleCode* allele_permute; |
| 32 |
const uintptr_t* sample_include; |
| 33 |
const uint32_t* sample_include_cumulative_popcounts; |
| 34 |
uint32_t sample_ct; |
| 35 |
|
| 36 |
PgenReader** pgr_ptrs; |
| 37 |
|
| 38 |
uint32_t* variant_uidx_starts; |
| 39 |
uint32_t cur_block_write_ct; |
| 40 |
|
| 41 |
uintptr_t* vmaj_readbuf; |
| 42 |
|
| 43 |
uint64_t err_info; |
| 44 |
} TransposeToSmajReadCtx; |
| 45 |
|
| 46 |
THREAD_FUNC_DECL TransposeToSmajReadThread(void* raw_arg) { |
| 47 |
ThreadGroupFuncArg* arg = S_CAST(ThreadGroupFuncArg*, raw_arg); |
| 48 |
const uintptr_t tidx = arg->tidx; |
| 49 |
TransposeToSmajReadCtx* ctx = S_CAST(TransposeToSmajReadCtx*, arg->sharedp->context); |
| 50 |
|
| 51 |
const uintptr_t* variant_include = ctx->variant_include; |
| 52 |
const uintptr_t* allele_idx_offsets = ctx->allele_idx_offsets; |
| 53 |
const AlleleCode* allele_permute = ctx->allele_permute; |
| 54 |
const uint32_t calc_thread_ct = GetThreadCt(arg->sharedp); |
| 55 |
const uintptr_t* sample_include = ctx->sample_include; |
| 56 |
const uint32_t read_sample_ct = ctx->sample_ct; |
| 57 |
const uintptr_t read_sample_ctaw2 = NypCtToAlignedWordCt(read_sample_ct); |
| 58 |
PgenReader* pgrp = ctx->pgr_ptrs[tidx]; |
| 59 |
PgrSampleSubsetIndex pssi; |
| 60 |
PgrSetSampleSubsetIndex(ctx->sample_include_cumulative_popcounts, pgrp, &pssi); |
| 61 |
uintptr_t prev_copy_ct = 0; |
| 62 |
uint64_t new_err_info = 0; |
| 63 |
do { |
| 64 |
const uintptr_t cur_block_copy_ct = ctx->cur_block_write_ct; |
| 65 |
const uint32_t cur_idx_end = ((tidx + 1) * cur_block_copy_ct) / calc_thread_ct; |
| 66 |
uintptr_t variant_uidx_base; |
| 67 |
uintptr_t cur_bits; |
| 68 |
BitIter1Start(variant_include, ctx->variant_uidx_starts[tidx], &variant_uidx_base, &cur_bits); |
| 69 |
const uint32_t cur_idx_start = (tidx * cur_block_copy_ct) / calc_thread_ct; |
| 70 |
uintptr_t* vmaj_readbuf_iter = &(ctx->vmaj_readbuf[(prev_copy_ct + cur_idx_start) * read_sample_ctaw2]); |
| 71 |
for (uint32_t cur_idx = cur_idx_start; cur_idx != cur_idx_end; ++cur_idx) { |
| 72 |
const uintptr_t variant_uidx = BitIter1(variant_include, &variant_uidx_base, &cur_bits); |
| 73 |
// todo: multiallelic case |
| 74 |
const PglErr reterr = PgrGet(sample_include, pssi, read_sample_ct, variant_uidx, pgrp, vmaj_readbuf_iter); |
| 75 |
if (unlikely(reterr)) { |
| 76 |
new_err_info = (S_CAST(uint64_t, variant_uidx) << 32) | S_CAST(uint32_t, reterr); |
| 77 |
goto TransposeToSmajReadThread_err; |
| 78 |
} |
| 79 |
if (allele_permute) { |
| 80 |
const uintptr_t allele_idx_offset_base = allele_idx_offsets? allele_idx_offsets[variant_uidx] : (2 * variant_uidx); |
| 81 |
if (allele_permute[allele_idx_offset_base]) { |
| 82 |
assert(allele_permute[allele_idx_offset_base] == 1); |
| 83 |
GenovecInvertUnsafe(read_sample_ct, vmaj_readbuf_iter); |
| 84 |
} |
| 85 |
// don't need ZeroTrailingNyps() |
| 86 |
} |
| 87 |
vmaj_readbuf_iter = &(vmaj_readbuf_iter[read_sample_ctaw2]); |
| 88 |
} |
| 89 |
prev_copy_ct += cur_block_copy_ct; |
| 90 |
while (0) { |
| 91 |
TransposeToSmajReadThread_err: |
| 92 |
UpdateU64IfSmaller(new_err_info, &ctx->err_info); |
| 93 |
} |
| 94 |
} while (!THREAD_BLOCK_FINISH(arg)); |
| 95 |
THREAD_RETURN; |
| 96 |
} |
| 97 |
|
| 98 |
typedef struct TransposeToPlink1SmajWriteCtxStruct { |
| 99 |
const uintptr_t* variant_include; |
| 100 |
uint32_t variant_ct; |
| 101 |
uint32_t sample_ct; |
| 102 |
|
| 103 |
const uintptr_t* vmaj_readbuf; |
| 104 |
|
| 105 |
VecW** thread_vecaligned_bufs; |
| 106 |
|
| 107 |
uint32_t sample_batch_size; |
| 108 |
|
| 109 |
uintptr_t* smaj_writebufs[2]; |
| 110 |
} TransposeToPlink1SmajWriteCtx; |
| 111 |
|
| 112 |
THREAD_FUNC_DECL TransposeToPlink1SmajWriteThread(void* raw_arg) { |
| 113 |
ThreadGroupFuncArg* arg = S_CAST(ThreadGroupFuncArg*, raw_arg); |
| 114 |
const uintptr_t tidx = arg->tidx; |
| 115 |
TransposeToPlink1SmajWriteCtx* ctx = S_CAST(TransposeToPlink1SmajWriteCtx*, arg->sharedp->context); |
| 116 |
|
| 117 |
const uint32_t variant_ct = ctx->variant_ct; |
| 118 |
const uintptr_t variant_batch_ct = DivUp(variant_ct, kPglNypTransposeBatch); |
| 119 |
const uintptr_t variant_batch_word_ct = variant_batch_ct * kPglNypTransposeWords; |
| 120 |
const uint32_t calc_thread_ct = GetThreadCt(arg->sharedp); |
| 121 |
const uintptr_t variant_batch_idx_start = (S_CAST(uint64_t, tidx) * variant_batch_ct) / calc_thread_ct; |
| 122 |
VecW* vecaligned_buf = ctx->thread_vecaligned_bufs[tidx]; |
| 123 |
uintptr_t variant_batch_idx_full_end = ((S_CAST(uint64_t, tidx) + 1) * variant_batch_ct) / calc_thread_ct; |
| 124 |
uint32_t variant_idx_end; |
| 125 |
if (tidx + 1 < calc_thread_ct) { |
| 126 |
variant_idx_end = variant_batch_idx_full_end * kPglNypTransposeBatch; |
| 127 |
} else { |
| 128 |
variant_idx_end = variant_ct; |
| 129 |
if (variant_ct % kPglNypTransposeBatch) { |
| 130 |
--variant_batch_idx_full_end; |
| 131 |
} |
| 132 |
} |
| 133 |
const uint32_t thread_variant_ct = variant_idx_end - variant_batch_idx_start * kPglNypTransposeBatch; |
| 134 |
const uint32_t read_sample_ct = ctx->sample_ct; |
| 135 |
const uintptr_t read_sample_ctaw2 = NypCtToAlignedWordCt(read_sample_ct); |
| 136 |
const uintptr_t* vmaj_readbuf = ctx->vmaj_readbuf; |
| 137 |
uint32_t sample_widx = 0; |
| 138 |
uint32_t parity = 0; |
| 139 |
do { |
| 140 |
const uint32_t sample_batch_size = ctx->sample_batch_size; |
| 141 |
const uintptr_t* vmaj_readbuf_iter = &(vmaj_readbuf[variant_batch_idx_start * kPglNypTransposeBatch * read_sample_ctaw2 + sample_widx]); |
| 142 |
uintptr_t* smaj_writebuf_start = &(ctx->smaj_writebufs[parity][variant_batch_idx_start * kPglNypTransposeWords]); |
| 143 |
uintptr_t* smaj_writebuf_iter = smaj_writebuf_start; |
| 144 |
uint32_t variant_batch_size = kPglNypTransposeBatch; |
| 145 |
for (uintptr_t variant_batch_idx = variant_batch_idx_start; ; ++variant_batch_idx) { |
| 146 |
if (variant_batch_idx >= variant_batch_idx_full_end) { |
| 147 |
if (variant_batch_idx * kPglNypTransposeBatch >= variant_idx_end) { |
| 148 |
break; |
| 149 |
} |
| 150 |
variant_batch_size = variant_idx_end - variant_batch_idx * kPglNypTransposeBatch; |
| 151 |
} |
| 152 |
TransposeNypblock(vmaj_readbuf_iter, read_sample_ctaw2, variant_batch_word_ct, variant_batch_size, sample_batch_size, smaj_writebuf_iter, vecaligned_buf); |
| 153 |
smaj_writebuf_iter = &(smaj_writebuf_iter[kPglNypTransposeWords]); |
| 154 |
vmaj_readbuf_iter = &(vmaj_readbuf_iter[variant_batch_size * read_sample_ctaw2]); |
| 155 |
} |
| 156 |
smaj_writebuf_iter = smaj_writebuf_start; |
| 157 |
for (uint32_t sample_idx = 0; sample_idx != sample_batch_size; ++sample_idx) { |
| 158 |
// could fold this into TransposeNypblock(), but I won't bother, |
| 159 |
// we're already saturating at ~3 threads |
| 160 |
PgrPlink2ToPlink1InplaceUnsafe(thread_variant_ct, smaj_writebuf_iter); |
| 161 |
ZeroTrailingNyps(thread_variant_ct, smaj_writebuf_iter); |
| 162 |
smaj_writebuf_iter = &(smaj_writebuf_iter[variant_batch_word_ct]); |
| 163 |
} |
| 164 |
parity = 1 - parity; |
| 165 |
sample_widx += sample_batch_size / kBitsPerWordD2; |
| 166 |
} while (!THREAD_BLOCK_FINISH(arg)); |
| 167 |
THREAD_RETURN; |
| 168 |
} |
| 169 |
|
| 170 |
PglErr ExportIndMajorBed(const uintptr_t* orig_sample_include, const uintptr_t* variant_include, const uintptr_t* allele_idx_offsets, const AlleleCode* allele_permute, uint32_t raw_sample_ct, uint32_t sample_ct, uint32_t raw_variant_ct, uint32_t variant_ct, uint32_t max_thread_ct, uintptr_t pgr_alloc_cacheline_ct, PgenFileInfo* pgfip, char* outname, char* outname_end) { |
| 171 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 172 |
FILE* outfile = nullptr; |
| 173 |
PglErr reterr = kPglRetSuccess; |
| 174 |
ThreadGroup read_tg; |
| 175 |
ThreadGroup write_tg; |
| 176 |
PreinitThreads(&read_tg); |
| 177 |
PreinitThreads(&write_tg); |
| 178 |
TransposeToSmajReadCtx read_ctx; |
| 179 |
TransposeToPlink1SmajWriteCtx write_ctx; |
| 180 |
{ |
| 181 |
// Possible special case: if the input file is a variant-major .bed, we do |
| 182 |
// not have enough memory to just load the whole file at once, and there |
| 183 |
// are more than ~20k samples, there can be a performance advantage to not |
| 184 |
// loading an entire variant at a time; we can use smaller fread calls and |
| 185 |
// reduce the number of (typically 4096 byte) disk blocks which need to be |
| 186 |
// read on each pass. But let's get .pgen -> sample-major humming first. |
| 187 |
snprintf(outname_end, kMaxOutfnameExtBlen, ".bed"); |
| 188 |
if (unlikely(fopen_checked(outname, FOPEN_WB, &outfile))) { |
| 189 |
goto ExportIndMajorBed_ret_OPEN_FAIL; |
| 190 |
} |
| 191 |
if (unlikely(!fwrite_unlocked("l\x1b", 3, 1, outfile))) { |
| 192 |
goto ExportIndMajorBed_ret_WRITE_FAIL; |
| 193 |
} |
| 194 |
if (variant_ct && sample_ct) { |
| 195 |
const uint32_t raw_sample_ctl = BitCtToWordCt(raw_sample_ct); |
| 196 |
uint32_t calc_thread_ct = (max_thread_ct > 2)? (max_thread_ct - 1) : max_thread_ct; |
| 197 |
// todo: if only 1 pass is needed, and no subsetting is happening, this |
| 198 |
// saturates at ~4 threads? |
| 199 |
STD_ARRAY_DECL(unsigned char*, 2, main_loadbufs); |
| 200 |
uint32_t read_block_size; |
| 201 |
// note that this is restricted to half of available workspace |
| 202 |
if (unlikely(PgenMtLoadInit(variant_include, sample_ct, variant_ct, bigstack_left() / 2, pgr_alloc_cacheline_ct, 0, 0, 0, pgfip, &calc_thread_ct, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &read_block_size, nullptr, main_loadbufs, &read_ctx.pgr_ptrs, &read_ctx.variant_uidx_starts))) { |
| 203 |
goto ExportIndMajorBed_ret_NOMEM; |
| 204 |
} |
| 205 |
if (unlikely(SetThreadCt(calc_thread_ct, &read_tg))) { |
| 206 |
goto ExportIndMajorBed_ret_NOMEM; |
| 207 |
} |
| 208 |
read_ctx.variant_include = variant_include; |
| 209 |
read_ctx.allele_idx_offsets = allele_idx_offsets; |
| 210 |
read_ctx.allele_permute = allele_permute; |
| 211 |
read_ctx.err_info = (~0LLU) << 32; |
| 212 |
SetThreadFuncAndData(TransposeToSmajReadThread, &read_ctx, &read_tg); |
| 213 |
|
| 214 |
const uintptr_t variant_cacheline_ct = NypCtToCachelineCt(variant_ct); |
| 215 |
uint32_t output_calc_thread_ct = MINV(calc_thread_ct, variant_cacheline_ct); |
| 216 |
// 4 still seems to be best in AVX2 case |
| 217 |
if (output_calc_thread_ct > 4) { |
| 218 |
output_calc_thread_ct = 4; |
| 219 |
} |
| 220 |
uintptr_t* sample_include; |
| 221 |
uint32_t* sample_include_cumulative_popcounts; |
| 222 |
if (unlikely(SetThreadCt(output_calc_thread_ct, &write_tg) || |
| 223 |
bigstack_alloc_w(raw_sample_ctl, &sample_include) || |
| 224 |
bigstack_alloc_u32(raw_sample_ctl, &sample_include_cumulative_popcounts) || |
| 225 |
bigstack_alloc_vp(output_calc_thread_ct, &write_ctx.thread_vecaligned_bufs))) { |
| 226 |
goto ExportIndMajorBed_ret_NOMEM; |
| 227 |
} |
| 228 |
for (uint32_t tidx = 0; tidx != output_calc_thread_ct; ++tidx) { |
| 229 |
write_ctx.thread_vecaligned_bufs[tidx] = S_CAST(VecW*, bigstack_alloc_raw(kPglNypTransposeBufbytes)); |
| 230 |
} |
| 231 |
// each of the two write buffers should use <= 1/8 of the remaining |
| 232 |
// workspace |
| 233 |
const uintptr_t writebuf_cachelines_avail = bigstack_left() / (kCacheline * 8); |
| 234 |
uint32_t sample_batch_size = kPglNypTransposeBatch; |
| 235 |
if (variant_cacheline_ct * kPglNypTransposeBatch > writebuf_cachelines_avail) { |
| 236 |
sample_batch_size = RoundDownPow2(writebuf_cachelines_avail / variant_cacheline_ct, kBitsPerWordD2); |
| 237 |
if (unlikely(!sample_batch_size)) { |
| 238 |
goto ExportIndMajorBed_ret_NOMEM; |
| 239 |
} |
| 240 |
} |
| 241 |
write_ctx.smaj_writebufs[0] = S_CAST(uintptr_t*, bigstack_alloc_raw(variant_cacheline_ct * kCacheline * sample_batch_size)); |
| 242 |
write_ctx.smaj_writebufs[1] = S_CAST(uintptr_t*, bigstack_alloc_raw(variant_cacheline_ct * kCacheline * sample_batch_size)); |
| 243 |
const uintptr_t readbuf_vecs_avail = (bigstack_left() / kCacheline) * kVecsPerCacheline; |
| 244 |
if (unlikely(readbuf_vecs_avail < variant_ct)) { |
| 245 |
goto ExportIndMajorBed_ret_NOMEM; |
| 246 |
} |
| 247 |
uintptr_t read_sample_ctv2 = readbuf_vecs_avail / variant_ct; |
| 248 |
uint32_t read_sample_ct; |
| 249 |
if (read_sample_ctv2 >= NypCtToVecCt(sample_ct)) { |
| 250 |
read_sample_ct = sample_ct; |
| 251 |
} else { |
| 252 |
read_sample_ct = read_sample_ctv2 * kNypsPerVec; |
| 253 |
} |
| 254 |
uintptr_t read_sample_ctaw2 = NypCtToAlignedWordCt(read_sample_ct); |
| 255 |
uintptr_t* vmaj_readbuf = S_CAST(uintptr_t*, bigstack_alloc_raw_rd(variant_ct * read_sample_ctaw2 * kBytesPerWord)); |
| 256 |
read_ctx.vmaj_readbuf = vmaj_readbuf; |
| 257 |
write_ctx.variant_include = variant_include; |
| 258 |
write_ctx.variant_ct = variant_ct; |
| 259 |
write_ctx.vmaj_readbuf = vmaj_readbuf; |
| 260 |
SetThreadFuncAndData(TransposeToPlink1SmajWriteThread, &write_ctx, &write_tg); |
| 261 |
uint32_t sample_uidx_start = AdvTo1Bit(orig_sample_include, 0); |
| 262 |
const uintptr_t variant_ct4 = NypCtToByteCt(variant_ct); |
| 263 |
const uintptr_t variant_ctaclw2 = variant_cacheline_ct * kWordsPerCacheline; |
| 264 |
const uint32_t pass_ct = 1 + (sample_ct - 1) / read_sample_ct; |
| 265 |
for (uint32_t pass_idx = 0; pass_idx != pass_ct; ++pass_idx) { |
| 266 |
memcpy(sample_include, orig_sample_include, raw_sample_ctl * sizeof(intptr_t)); |
| 267 |
if (sample_uidx_start) { |
| 268 |
ClearBitsNz(0, sample_uidx_start, sample_include); |
| 269 |
} |
| 270 |
uint32_t sample_uidx_end; |
| 271 |
if (pass_idx + 1 == pass_ct) { |
| 272 |
read_sample_ct = sample_ct - pass_idx * read_sample_ct; |
| 273 |
read_sample_ctaw2 = NypCtToAlignedWordCt(read_sample_ct); |
| 274 |
sample_uidx_end = raw_sample_ct; |
| 275 |
} else { |
| 276 |
sample_uidx_end = FindNth1BitFrom(orig_sample_include, sample_uidx_start + 1, read_sample_ct); |
| 277 |
ClearBitsNz(sample_uidx_end, raw_sample_ct, sample_include); |
| 278 |
} |
| 279 |
FillCumulativePopcounts(sample_include, raw_sample_ctl, sample_include_cumulative_popcounts); |
| 280 |
read_ctx.sample_include = sample_include; |
| 281 |
read_ctx.sample_include_cumulative_popcounts = sample_include_cumulative_popcounts; |
| 282 |
read_ctx.sample_ct = read_sample_ct; |
| 283 |
write_ctx.sample_ct = read_sample_ct; |
| 284 |
if (pass_idx) { |
| 285 |
pgfip->block_base = main_loadbufs[0]; |
| 286 |
// er, don't need SetBaseAndOffset0? |
| 287 |
PgrSetBaseAndOffset0(main_loadbufs[0], calc_thread_ct, read_ctx.pgr_ptrs); |
| 288 |
} |
| 289 |
uint32_t parity = 0; |
| 290 |
uint32_t read_block_idx = 0; |
| 291 |
ReinitThreads(&read_tg); |
| 292 |
uint32_t pct = 0; |
| 293 |
uint32_t next_print_idx = variant_ct / 100; |
| 294 |
putc_unlocked('\r', stdout); |
| 295 |
printf("--export ind-major-bed pass %u/%u: loading... 0%%", pass_idx + 1, pass_ct); |
| 296 |
fflush(stdout); |
| 297 |
for (uint32_t variant_idx = 0; ; ) { |
| 298 |
const uint32_t cur_block_write_ct = MultireadNonempty(variant_include, &read_tg, raw_variant_ct, read_block_size, pgfip, &read_block_idx, &reterr); |
| 299 |
if (unlikely(reterr)) { |
| 300 |
goto ExportIndMajorBed_ret_PGR_FAIL; |
| 301 |
} |
| 302 |
if (variant_idx) { |
| 303 |
JoinThreads(&read_tg); |
| 304 |
reterr = S_CAST(PglErr, read_ctx.err_info); |
| 305 |
if (unlikely(reterr)) { |
| 306 |
PgenErrPrintNV(reterr, read_ctx.err_info >> 32); |
| 307 |
goto ExportIndMajorBed_ret_1; |
| 308 |
} |
| 309 |
} |
| 310 |
if (!IsLastBlock(&read_tg)) { |
| 311 |
read_ctx.cur_block_write_ct = cur_block_write_ct; |
| 312 |
ComputeUidxStartPartition(variant_include, cur_block_write_ct, calc_thread_ct, read_block_idx * read_block_size, read_ctx.variant_uidx_starts); |
| 313 |
PgrCopyBaseAndOffset(pgfip, calc_thread_ct, read_ctx.pgr_ptrs); |
| 314 |
if (variant_idx + cur_block_write_ct == variant_ct) { |
| 315 |
DeclareLastThreadBlock(&read_tg); |
| 316 |
} |
| 317 |
if (unlikely(SpawnThreads(&read_tg))) { |
| 318 |
goto ExportIndMajorBed_ret_THREAD_CREATE_FAIL; |
| 319 |
} |
| 320 |
} |
| 321 |
parity = 1 - parity; |
| 322 |
if (variant_idx == variant_ct) { |
| 323 |
break; |
| 324 |
} |
| 325 |
if (variant_idx >= next_print_idx) { |
| 326 |
if (pct > 10) { |
| 327 |
putc_unlocked('\b', stdout); |
| 328 |
} |
| 329 |
pct = (variant_idx * 100LLU) / variant_ct; |
| 330 |
printf("\b\b%u%%", pct++); |
| 331 |
fflush(stdout); |
| 332 |
next_print_idx = (pct * S_CAST(uint64_t, variant_ct)) / 100; |
| 333 |
} |
| 334 |
|
| 335 |
++read_block_idx; |
| 336 |
variant_idx += cur_block_write_ct; |
| 337 |
pgfip->block_base = main_loadbufs[parity]; |
| 338 |
} |
| 339 |
// 2. Transpose and write. (Could parallelize some of the transposing |
| 340 |
// with the read loop, but since we can't write a single row until |
| 341 |
// the read loop is done, and both write speed and write buffer |
| 342 |
// space are bottlenecks, that can't be expected to help much.) |
| 343 |
ReinitThreads(&write_tg); |
| 344 |
write_ctx.sample_batch_size = sample_batch_size; |
| 345 |
parity = 0; |
| 346 |
if (pct > 10) { |
| 347 |
fputs("\b \b", stdout); |
| 348 |
} |
| 349 |
fputs("\b\b\b\b\b\b\b\b\b\b\b\b\bwriting... 0%", stdout); |
| 350 |
fflush(stdout); |
| 351 |
pct = 0; |
| 352 |
uint32_t flush_sample_idx = 0; |
| 353 |
next_print_idx = read_sample_ct / 100; |
| 354 |
for (uint32_t flush_sample_idx_end = 0; ; ) { |
| 355 |
if (!IsLastBlock(&write_tg)) { |
| 356 |
if (flush_sample_idx_end + sample_batch_size >= read_sample_ct) { |
| 357 |
DeclareLastThreadBlock(&write_tg); |
| 358 |
write_ctx.sample_batch_size = read_sample_ct - flush_sample_idx_end; |
| 359 |
} |
| 360 |
if (unlikely(SpawnThreads(&write_tg))) { |
| 361 |
goto ExportIndMajorBed_ret_THREAD_CREATE_FAIL; |
| 362 |
} |
| 363 |
} |
| 364 |
if (flush_sample_idx_end) { |
| 365 |
uintptr_t* smaj_writebuf_iter = write_ctx.smaj_writebufs[1 - parity]; |
| 366 |
for (; flush_sample_idx != flush_sample_idx_end; ++flush_sample_idx) { |
| 367 |
fwrite_unlocked(smaj_writebuf_iter, variant_ct4, 1, outfile); |
| 368 |
smaj_writebuf_iter = &(smaj_writebuf_iter[variant_ctaclw2]); |
| 369 |
} |
| 370 |
if (unlikely(ferror_unlocked(outfile))) { |
| 371 |
goto ExportIndMajorBed_ret_WRITE_FAIL; |
| 372 |
} |
| 373 |
if (flush_sample_idx_end == read_sample_ct) { |
| 374 |
break; |
| 375 |
} |
| 376 |
if (flush_sample_idx_end >= next_print_idx) { |
| 377 |
if (pct > 10) { |
| 378 |
putc_unlocked('\b', stdout); |
| 379 |
} |
| 380 |
pct = (flush_sample_idx_end * 100LLU) / read_sample_ct; |
| 381 |
printf("\b\b%u%%", pct++); |
| 382 |
fflush(stdout); |
| 383 |
next_print_idx = (pct * S_CAST(uint64_t, read_sample_ct)) / 100; |
| 384 |
} |
| 385 |
} |
| 386 |
JoinThreads(&write_tg); |
| 387 |
parity = 1 - parity; |
| 388 |
flush_sample_idx_end += sample_batch_size; |
| 389 |
if (flush_sample_idx_end > read_sample_ct) { |
| 390 |
flush_sample_idx_end = read_sample_ct; |
| 391 |
} |
| 392 |
} |
| 393 |
if (pct > 10) { |
| 394 |
fputs("\b \b", stdout); |
| 395 |
} |
| 396 |
sample_uidx_start = sample_uidx_end; |
| 397 |
} |
| 398 |
fputs("\b\bdone.\n", stdout); |
| 399 |
} |
| 400 |
if (unlikely(fclose_null(&outfile))) { |
| 401 |
goto ExportIndMajorBed_ret_WRITE_FAIL; |
| 402 |
} |
| 403 |
logprintfww("--export ind-major-bed: %s written.\n", outname); |
| 404 |
} |
| 405 |
while (0) { |
| 406 |
ExportIndMajorBed_ret_NOMEM: |
| 407 |
reterr = kPglRetNomem; |
| 408 |
break; |
| 409 |
ExportIndMajorBed_ret_OPEN_FAIL: |
| 410 |
reterr = kPglRetOpenFail; |
| 411 |
break; |
| 412 |
ExportIndMajorBed_ret_PGR_FAIL: |
| 413 |
PgenErrPrintN(reterr); |
| 414 |
break; |
| 415 |
ExportIndMajorBed_ret_WRITE_FAIL: |
| 416 |
reterr = kPglRetWriteFail; |
| 417 |
break; |
| 418 |
ExportIndMajorBed_ret_THREAD_CREATE_FAIL: |
| 419 |
reterr = kPglRetThreadCreateFail; |
| 420 |
break; |
| 421 |
} |
| 422 |
ExportIndMajorBed_ret_1: |
| 423 |
CleanupThreads(&write_tg); |
| 424 |
CleanupThreads(&read_tg); |
| 425 |
fclose_cond(outfile); |
| 426 |
pgfip->block_base = nullptr; |
| 427 |
BigstackReset(bigstack_mark); |
| 428 |
return reterr; |
| 429 |
} |
| 430 |
|
| 431 |
PglErr ExportTped(const char* outname, const uintptr_t* sample_include, const uint32_t* sample_include_cumulative_popcounts, const uintptr_t* variant_include, const ChrInfo* cip, const uint32_t* variant_bps, const char* const* variant_ids, const uintptr_t* allele_idx_offsets, const char* const* allele_storage, const double* variant_cms, uint32_t sample_ct, uint32_t variant_ct, uint32_t max_allele_slen, char exportf_delim, char lomg_char, PgenReader* simple_pgrp) { |
| 432 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 433 |
FILE* outfile = nullptr; |
| 434 |
PglErr reterr = kPglRetSuccess; |
| 435 |
{ |
| 436 |
if (unlikely(fopen_checked(outname, FOPEN_WB, &outfile))) { |
| 437 |
goto ExportTped_ret_OPEN_FAIL; |
| 438 |
} |
| 439 |
const uint32_t sample_ctl2 = NypCtToWordCt(sample_ct); |
| 440 |
const uint32_t max_chr_blen = 1 + GetMaxChrSlen(cip); |
| 441 |
const uintptr_t writebuf_size = RoundUpPow2(kMaxMediumLine + MAXV(2 * kMaxIdSlen + 32, MAXV(2 * max_allele_slen + 2, 4 * sample_ct)), kCacheline); |
| 442 |
char* chr_buf; |
| 443 |
char* writebuf; |
| 444 |
uintptr_t* genovec; |
| 445 |
char* genotext_buf; |
| 446 |
if (unlikely(bigstack_alloc_c(max_chr_blen, &chr_buf) || |
| 447 |
bigstack_alloc_c(writebuf_size, &writebuf) || |
| 448 |
bigstack_alloc_w(sample_ctl2, &genovec) || |
| 449 |
bigstack_alloc_c((6 * k1LU) * max_allele_slen + 10, &genotext_buf))) { |
| 450 |
goto ExportTped_ret_NOMEM; |
| 451 |
} |
| 452 |
char* writebuf_flush = &(writebuf[kMaxMediumLine]); |
| 453 |
PgrSampleSubsetIndex pssi; |
| 454 |
PgrSetSampleSubsetIndex(sample_include_cumulative_popcounts, simple_pgrp, &pssi); |
| 455 |
char* write_iter = writebuf; |
| 456 |
|
| 457 |
logprintfww5("--export tped to %s ... ", outname); |
| 458 |
fputs("0%", stdout); |
| 459 |
fflush(stdout); |
| 460 |
uint32_t geno_to_text32[4]; |
| 461 |
// delim, missing code, delim, missing code |
| 462 |
geno_to_text32[3] = (ctou32(exportf_delim) + 0x100 * ctou32(lomg_char)) * 0x10001; |
| 463 |
|
| 464 |
char* geno_to_str[4]; |
| 465 |
uint32_t geno_slen[4]; |
| 466 |
geno_to_str[3] = &(genotext_buf[(6 * k1LU) * max_allele_slen + 6]); |
| 467 |
geno_to_str[3][0] = exportf_delim; |
| 468 |
geno_to_str[3][1] = lomg_char; |
| 469 |
geno_to_str[3][2] = exportf_delim; |
| 470 |
geno_to_str[3][3] = lomg_char; |
| 471 |
geno_slen[3] = 4; |
| 472 |
|
| 473 |
const uint32_t sample_ctl2_m1 = sample_ctl2 - 1; |
| 474 |
uint32_t chr_fo_idx = UINT32_MAX; |
| 475 |
uint32_t chr_end = 0; |
| 476 |
uint32_t chr_blen = 0; |
| 477 |
uint32_t pct = 0; |
| 478 |
uint32_t next_print_variant_idx = variant_ct / 100; |
| 479 |
uintptr_t variant_uidx_base = 0; |
| 480 |
uintptr_t variant_include_bits = variant_include[0]; |
| 481 |
for (uint32_t variant_idx = 0; variant_idx != variant_ct; ++variant_idx) { |
| 482 |
const uint32_t variant_uidx = BitIter1(variant_include, &variant_uidx_base, &variant_include_bits); |
| 483 |
if (variant_uidx >= chr_end) { |
| 484 |
do { |
| 485 |
++chr_fo_idx; |
| 486 |
chr_end = cip->chr_fo_vidx_start[chr_fo_idx + 1]; |
| 487 |
} while (variant_uidx >= chr_end); |
| 488 |
const uint32_t chr_idx = cip->chr_file_order[chr_fo_idx]; |
| 489 |
char* chr_name_end = chrtoa(cip, chr_idx, chr_buf); |
| 490 |
*chr_name_end++ = exportf_delim; |
| 491 |
chr_blen = chr_name_end - chr_buf; |
| 492 |
} |
| 493 |
char* line_start = write_iter; |
| 494 |
write_iter = memcpya(write_iter, chr_buf, chr_blen); |
| 495 |
write_iter = strcpyax(write_iter, variant_ids[variant_uidx], exportf_delim); |
| 496 |
if (variant_cms) { |
| 497 |
write_iter = dtoa_g_p8(variant_cms[variant_uidx], write_iter); |
| 498 |
} else { |
| 499 |
*write_iter++ = '0'; |
| 500 |
} |
| 501 |
*write_iter++ = exportf_delim; |
| 502 |
write_iter = u32toa(variant_bps[variant_uidx], write_iter); |
| 503 |
uint64_t line_blen = write_iter - line_start; |
| 504 |
if (unlikely(fwrite_ck(writebuf_flush, outfile, &write_iter))) { |
| 505 |
goto ExportTped_ret_WRITE_FAIL; |
| 506 |
} |
| 507 |
|
| 508 |
uintptr_t allele_idx_offset_base = variant_uidx * 2; |
| 509 |
if (allele_idx_offsets) { |
| 510 |
allele_idx_offset_base = allele_idx_offsets[variant_uidx]; |
| 511 |
if (unlikely(allele_idx_offsets[variant_uidx + 1] != allele_idx_offset_base + 2)) { |
| 512 |
logputs("\n"); |
| 513 |
logerrprintfww("Error: %s cannot contain multiallelic variants.\n", outname); |
| 514 |
goto ExportTped_ret_INCONSISTENT_INPUT; |
| 515 |
} |
| 516 |
} |
| 517 |
reterr = PgrGet(sample_include, pssi, sample_ct, variant_uidx, simple_pgrp, genovec); |
| 518 |
if (unlikely(reterr)) { |
| 519 |
PgenErrPrintNV(reterr, variant_uidx); |
| 520 |
goto ExportTped_ret_1; |
| 521 |
} |
| 522 |
const char* ref_allele = allele_storage[allele_idx_offset_base]; |
| 523 |
const char* alt_allele = allele_storage[allele_idx_offset_base + 1]; |
| 524 |
const uint32_t ref_slen = strlen(ref_allele); |
| 525 |
const uint32_t alt_slen = strlen(alt_allele); |
| 526 |
uint32_t loop_len = kBitsPerWordD2; |
| 527 |
if ((ref_slen == 1) && (alt_slen == 1)) { |
| 528 |
// easy case: each genotype corresponds to 4 bytes |
| 529 |
// could set up a geno_pair_to_text64 array, but I think we're already |
| 530 |
// close enough to I/O-bound as-is |
| 531 |
geno_to_text32[0] = (ctou32(exportf_delim) + 0x100 * ctou32(ref_allele[0])) * 0x10001; |
| 532 |
// plink 1.x put the A1 allele first in the heterozygous case, so put |
| 533 |
// ALT first to approximate that |
| 534 |
geno_to_text32[1] = ctou32(exportf_delim) * 0x10001 + ctou32(alt_allele[0]) * 0x100 + ctou32(ref_allele[0]) * 0x1000000; |
| 535 |
|
| 536 |
geno_to_text32[2] = (ctou32(exportf_delim) + 0x100 * ctou32(alt_allele[0])) * 0x10001; |
| 537 |
unsigned char* write_iter_uc = R_CAST(unsigned char*, write_iter); |
| 538 |
for (uint32_t widx = 0; ; ++widx) { |
| 539 |
if (widx >= sample_ctl2_m1) { |
| 540 |
if (widx > sample_ctl2_m1) { |
| 541 |
break; |
| 542 |
} |
| 543 |
loop_len = ModNz(sample_ct, kBitsPerWordD2); |
| 544 |
} |
| 545 |
uintptr_t geno_word = genovec[widx]; |
| 546 |
for (uint32_t sample_idx_lowbits = 0; sample_idx_lowbits != loop_len; ++sample_idx_lowbits) { |
| 547 |
const uintptr_t cur_geno = geno_word & 3; |
| 548 |
AppendU32(geno_to_text32[cur_geno], &write_iter_uc); |
| 549 |
geno_word >>= 2; |
| 550 |
} |
| 551 |
} |
| 552 |
write_iter = R_CAST(char*, write_iter_uc); |
| 553 |
line_blen += sample_ct * (4 * k1LU); |
| 554 |
if (unlikely(fwrite_ck(writebuf_flush, outfile, &write_iter))) { |
| 555 |
goto ExportTped_ret_WRITE_FAIL; |
| 556 |
} |
| 557 |
} else { |
| 558 |
char* genotext_write_iter = genotext_buf; |
| 559 |
geno_to_str[0] = genotext_write_iter; |
| 560 |
*genotext_write_iter++ = exportf_delim; |
| 561 |
genotext_write_iter = memcpyax(genotext_write_iter, ref_allele, ref_slen, exportf_delim); |
| 562 |
genotext_write_iter = memcpya(genotext_write_iter, ref_allele, ref_slen); |
| 563 |
geno_slen[0] = 2 * ref_slen + 2; |
| 564 |
|
| 565 |
geno_to_str[1] = genotext_write_iter; |
| 566 |
*genotext_write_iter++ = exportf_delim; |
| 567 |
genotext_write_iter = memcpyax(genotext_write_iter, alt_allele, alt_slen, exportf_delim); |
| 568 |
genotext_write_iter = memcpya(genotext_write_iter, ref_allele, ref_slen); |
| 569 |
geno_slen[1] = ref_slen + alt_slen + 2; |
| 570 |
|
| 571 |
geno_to_str[2] = genotext_write_iter; |
| 572 |
*genotext_write_iter++ = exportf_delim; |
| 573 |
genotext_write_iter = memcpyax(genotext_write_iter, alt_allele, alt_slen, exportf_delim); |
| 574 |
memcpy(genotext_write_iter, alt_allele, alt_slen); |
| 575 |
geno_slen[2] = 2 * alt_slen + 2; |
| 576 |
|
| 577 |
for (uint32_t widx = 0; ; ++widx) { |
| 578 |
if (widx >= sample_ctl2_m1) { |
| 579 |
if (widx > sample_ctl2_m1) { |
| 580 |
break; |
| 581 |
} |
| 582 |
loop_len = ModNz(sample_ct, kBitsPerWordD2); |
| 583 |
} |
| 584 |
uintptr_t geno_word = genovec[widx]; |
| 585 |
for (uint32_t sample_idx_lowbits = 0; sample_idx_lowbits != loop_len; ++sample_idx_lowbits) { |
| 586 |
const uintptr_t cur_geno = geno_word & 3; |
| 587 |
const uintptr_t genotext_slen = geno_slen[cur_geno]; |
| 588 |
write_iter = memcpya(write_iter, geno_to_str[cur_geno], genotext_slen); |
| 589 |
line_blen += genotext_slen; |
| 590 |
if (unlikely(fwrite_ck(writebuf_flush, outfile, &write_iter))) { |
| 591 |
goto ExportTped_ret_WRITE_FAIL; |
| 592 |
} |
| 593 |
geno_word >>= 2; |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
AppendBinaryEoln(&write_iter); |
| 599 |
line_blen += strlen(EOLN_STR); |
| 600 |
if (unlikely(line_blen > kMaxLongLine)) { |
| 601 |
logputs("\n"); |
| 602 |
logerrputs("Error: --export tped would create an excessively long line.\n"); |
| 603 |
goto ExportTped_ret_INCONSISTENT_INPUT; |
| 604 |
} |
| 605 |
if (variant_idx >= next_print_variant_idx) { |
| 606 |
if (pct > 10) { |
| 607 |
putc_unlocked('\b', stdout); |
| 608 |
} |
| 609 |
pct = (variant_idx * 100LLU) / variant_ct; |
| 610 |
printf("\b\b%u%%", pct++); |
| 611 |
fflush(stdout); |
| 612 |
next_print_variant_idx = (pct * S_CAST(uint64_t, variant_ct)) / 100; |
| 613 |
} |
| 614 |
} |
| 615 |
if (unlikely(fclose_flush_null(writebuf_flush, write_iter, &outfile))) { |
| 616 |
goto ExportTped_ret_WRITE_FAIL; |
| 617 |
} |
| 618 |
if (pct > 10) { |
| 619 |
putc_unlocked('\b', stdout); |
| 620 |
} |
| 621 |
fputs("\b\b", stdout); |
| 622 |
logputs("done.\n"); |
| 623 |
} |
| 624 |
while (0) { |
| 625 |
ExportTped_ret_NOMEM: |
| 626 |
reterr = kPglRetNomem; |
| 627 |
break; |
| 628 |
ExportTped_ret_OPEN_FAIL: |
| 629 |
reterr = kPglRetOpenFail; |
| 630 |
break; |
| 631 |
ExportTped_ret_WRITE_FAIL: |
| 632 |
reterr = kPglRetWriteFail; |
| 633 |
break; |
| 634 |
ExportTped_ret_INCONSISTENT_INPUT: |
| 635 |
reterr = kPglRetInconsistentInput; |
| 636 |
break; |
| 637 |
} |
| 638 |
ExportTped_ret_1: |
| 639 |
fclose_cond(outfile); |
| 640 |
BigstackReset(bigstack_mark); |
| 641 |
return reterr; |
| 642 |
} |
| 643 |
|
| 644 |
PglErr ExportPed(const char* outname, const uintptr_t* orig_sample_include, const PedigreeIdInfo* piip, const uintptr_t* sex_nm, const uintptr_t* sex_male, const PhenoCol* pheno_cols, const uintptr_t* variant_include, const uintptr_t* allele_idx_offsets, const char* const* allele_storage, const char* legacy_output_missing_pheno, uint32_t raw_sample_ct, uint32_t sample_ct, uint32_t pheno_ct, uint32_t raw_variant_ct, uint32_t variant_ct, uint32_t max_allele_slen, uint32_t compound_genotypes, uint32_t max_thread_ct, uintptr_t pgr_alloc_cacheline_ct, char exportf_delim, char lomg_char, PgenFileInfo* pgfip) { |
| 645 |
// Similar to ExportIndMajorBed() and Export012Smaj(). |
| 646 |
// |
| 647 |
// Could be sped up by parallelizing the final rendering step (using |
| 648 |
// __atomic_fetch_add to distribute work in a manner similar to the bgzf |
| 649 |
// compressor), but that isn't realistically worth the effort since nobody |
| 650 |
// should be using this format when speed matters. |
| 651 |
// |
| 652 |
// ...more precisely, the general case isn't worth the effort. The |
| 653 |
// compound-genotypes and all-SNP ped subcases have better-behaved line |
| 654 |
// lengths, so they're substantially easier to accelerate. |
| 655 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 656 |
unsigned char* bigstack_end_mark = g_bigstack_end; |
| 657 |
FILE* outfile = nullptr; |
| 658 |
PglErr reterr = kPglRetSuccess; |
| 659 |
ThreadGroup read_tg; |
| 660 |
ThreadGroup transpose_tg; |
| 661 |
PreinitThreads(&read_tg); |
| 662 |
PreinitThreads(&transpose_tg); |
| 663 |
TransposeToSmajReadCtx read_ctx; |
| 664 |
TransposeToPlink1SmajWriteCtx transpose_ctx; |
| 665 |
{ |
| 666 |
if (unlikely(((4 * k1LU) - compound_genotypes) * variant_ct > kMaxLongLine - 4 * kMaxIdSlen - 32)) { |
| 667 |
logerrprintf("Error: --export %s would create an excessively long line.\n", compound_genotypes? "compound-genotypes" : "ped"); |
| 668 |
goto ExportPed_ret_INCONSISTENT_INPUT; |
| 669 |
} |
| 670 |
// Write header line; then fully load-and-transpose the first X samples, |
| 671 |
// flush them, load-and-transpose the next X, etc. |
| 672 |
if (unlikely(fopen_checked(outname, FOPEN_WB, &outfile))) { |
| 673 |
goto ExportPed_ret_OPEN_FAIL; |
| 674 |
} |
| 675 |
|
| 676 |
uint32_t multichar_allele_present = 0; |
| 677 |
if (max_allele_slen > 1) { |
| 678 |
// Still possible for multichar_allele_present to be false, since |
| 679 |
// max_allele_slen is not updated after variant filtering. |
| 680 |
uintptr_t variant_uidx_base = 0; |
| 681 |
uintptr_t variant_include_bits = variant_include[0]; |
| 682 |
for (uint32_t variant_idx = 0; variant_idx != variant_ct; ++variant_idx) { |
| 683 |
const uint32_t variant_uidx = BitIter1(variant_include, &variant_uidx_base, &variant_include_bits); |
| 684 |
uintptr_t allele_idx_offset_base = variant_uidx * 2; |
| 685 |
if (allele_idx_offsets) { |
| 686 |
allele_idx_offset_base = allele_idx_offsets[variant_uidx]; |
| 687 |
// Unnecessary to verify the variant is biallelic, we'll catch |
| 688 |
// violations later. |
| 689 |
} |
| 690 |
const char* ref_allele = allele_storage[allele_idx_offset_base]; |
| 691 |
const char* alt_allele = allele_storage[allele_idx_offset_base + 1]; |
| 692 |
if (ref_allele[1] || alt_allele[1]) { |
| 693 |
multichar_allele_present = 1; |
| 694 |
break; |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
// Preprocessing: precompute all possible genotype strings. (Yes, this can |
| 699 |
// eat a lot of memory, but ~100% of the time that's actually a problem, |
| 700 |
// the user has no business exporting a .ped anyway.) |
| 701 |
// |
| 702 |
// all-SNPs case: index into this array is of the form (variant_idx * 8) + |
| 703 |
// cur_geno_pair |
| 704 |
const char* geno_pair_matrix = nullptr; |
| 705 |
// variable-length case: index into this array is of the form |
| 706 |
// (variant_idx * 4) + cur_geno. |
| 707 |
const char** genotype_strs = nullptr; |
| 708 |
|
| 709 |
char* writebuf = g_textbuf; |
| 710 |
if (!multichar_allele_present) { |
| 711 |
// Inner loop writes kBitsPerWordD4 genotypes at a time, and we just move |
| 712 |
// the write pointer backward at the end if the last block is short. |
| 713 |
// So, to avoid possibly copying uninitialized memory, we initialize |
| 714 |
// geno_pair_matrix up to variant_idx_end/2 instead of just |
| 715 |
// (variant_ct+1)/2. |
| 716 |
const uint32_t variant_idx_end = RoundUpPow2(variant_ct, kBitsPerWordD4); |
| 717 |
const uint32_t geno_pair_alloc_ct = variant_idx_end / 2; |
| 718 |
char* geno_pair_matrix_start; |
| 719 |
if (unlikely(bigstack_alloc_c(geno_pair_alloc_ct * ((128 - 32 * compound_genotypes) * k1LU), &geno_pair_matrix_start))) { |
| 720 |
goto ExportPed_ret_NOMEM; |
| 721 |
} |
| 722 |
char geno_strs[2][4][4]; |
| 723 |
const uint32_t second_offset = 3 - compound_genotypes; |
| 724 |
for (uint32_t pair_part = 0; pair_part != 2; ++pair_part) { |
| 725 |
for (uint32_t geno_code = 0; geno_code != 4; ++geno_code) { |
| 726 |
geno_strs[pair_part][geno_code][0] = exportf_delim; |
| 727 |
geno_strs[pair_part][geno_code][1] = lomg_char; |
| 728 |
geno_strs[pair_part][geno_code][2] = exportf_delim; |
| 729 |
geno_strs[pair_part][geno_code][second_offset] = lomg_char; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
const uint32_t variant_ct_m1 = variant_ct - 1; |
| 734 |
char* geno_pair_matrix_iter = geno_pair_matrix_start; |
| 735 |
uintptr_t variant_uidx_base = 0; |
| 736 |
uintptr_t variant_include_bits = variant_include[0]; |
| 737 |
uint32_t pair_part_ct = 2; |
| 738 |
for (uint32_t variant_idx = 0; ; variant_idx += 2) { |
| 739 |
if (variant_idx >= variant_ct_m1) { |
| 740 |
if (variant_idx > variant_ct_m1) { |
| 741 |
if (variant_idx == variant_idx_end) { |
| 742 |
break; |
| 743 |
} |
| 744 |
pair_part_ct = 0; |
| 745 |
} else { |
| 746 |
pair_part_ct = 1; // last variant |
| 747 |
} |
| 748 |
} |
| 749 |
for (uint32_t pair_part = 0; pair_part != pair_part_ct; ++pair_part) { |
| 750 |
const uint32_t variant_uidx = BitIter1(variant_include, &variant_uidx_base, &variant_include_bits); |
| 751 |
uintptr_t allele_idx_offset_base = variant_uidx * 2; |
| 752 |
if (allele_idx_offsets) { |
| 753 |
allele_idx_offset_base = allele_idx_offsets[variant_uidx]; |
| 754 |
if (unlikely((allele_idx_offsets[variant_uidx + 1] != allele_idx_offset_base + 2))) { |
| 755 |
logputs("\n"); |
| 756 |
logerrprintfww("Error: %s cannot contain multiallelic variants.\n", outname); |
| 757 |
goto ExportPed_ret_INCONSISTENT_INPUT; |
| 758 |
} |
| 759 |
} |
| 760 |
const char ref_char = allele_storage[allele_idx_offset_base][0]; |
| 761 |
const char alt_char = allele_storage[allele_idx_offset_base + 1][0]; |
| 762 |
geno_strs[pair_part][0][1] = alt_char; |
| 763 |
geno_strs[pair_part][0][second_offset] = alt_char; |
| 764 |
geno_strs[pair_part][2][1] = alt_char; |
| 765 |
geno_strs[pair_part][2][second_offset] = ref_char; |
| 766 |
geno_strs[pair_part][3][1] = ref_char; |
| 767 |
geno_strs[pair_part][3][second_offset] = ref_char; |
| 768 |
} |
| 769 |
if (!compound_genotypes) { |
| 770 |
for (uint32_t geno1_code = 0; geno1_code != 4; ++geno1_code) { |
| 771 |
for (uint32_t geno0_code = 0; geno0_code != 4; ++geno0_code) { |
| 772 |
geno_pair_matrix_iter = memcpya_k(geno_pair_matrix_iter, geno_strs[0][geno0_code], 4); |
| 773 |
geno_pair_matrix_iter = memcpya_k(geno_pair_matrix_iter, geno_strs[1][geno1_code], 4); |
| 774 |
} |
| 775 |
} |
| 776 |
} else { |
| 777 |
for (uint32_t geno1_code = 0; geno1_code != 4; ++geno1_code) { |
| 778 |
for (uint32_t geno0_code = 0; geno0_code != 4; ++geno0_code) { |
| 779 |
geno_pair_matrix_iter = memcpya_k(geno_pair_matrix_iter, geno_strs[0][geno0_code], 3); |
| 780 |
geno_pair_matrix_iter = memcpya_k(geno_pair_matrix_iter, geno_strs[1][geno1_code], 3); |
| 781 |
} |
| 782 |
} |
| 783 |
} |
| 784 |
} |
| 785 |
geno_pair_matrix = geno_pair_matrix_start; |
| 786 |
} else { |
| 787 |
if (unlikely(compound_genotypes)) { |
| 788 |
logputs("\n"); |
| 789 |
logerrprintfww("Error: %s cannot contain multi-character allele codes.\n", outname); |
| 790 |
goto ExportPed_ret_INCONSISTENT_INPUT; |
| 791 |
} |
| 792 |
if (unlikely(bigstack_alloc_kcp(variant_ct * (4 * k1LU), &genotype_strs) || |
| 793 |
// (3n+1) strings, each string is null-terminated and has |
| 794 |
// length >= 4 |
| 795 |
(bigstack_left() < 5 + (15 * k1LU) * variant_ct))) { |
| 796 |
goto ExportPed_ret_NOMEM; |
| 797 |
} |
| 798 |
unsigned char* tmp_alloc_base = g_bigstack_base; |
| 799 |
unsigned char* tmp_alloc_end = &(bigstack_end_mark[-5]); |
| 800 |
tmp_alloc_end[0] = exportf_delim; |
| 801 |
tmp_alloc_end[1] = lomg_char; |
| 802 |
tmp_alloc_end[2] = exportf_delim; |
| 803 |
tmp_alloc_end[3] = lomg_char; |
| 804 |
tmp_alloc_end[4] = '\0'; |
| 805 |
const char* missing_geno_str = R_CAST(const char*, tmp_alloc_end); |
| 806 |
|
| 807 |
const char** genotype_strs_iter = genotype_strs; |
| 808 |
uintptr_t variant_uidx_base = 0; |
| 809 |
uintptr_t variant_include_bits = variant_include[0]; |
| 810 |
for (uint32_t variant_idx = 0; variant_idx != variant_ct; ++variant_idx) { |
| 811 |
const uint32_t variant_uidx = BitIter1(variant_include, &variant_uidx_base, &variant_include_bits); |
| 812 |
uintptr_t allele_idx_offset_base = variant_uidx * 2; |
| 813 |
if (allele_idx_offsets) { |
| 814 |
allele_idx_offset_base = allele_idx_offsets[variant_uidx]; |
| 815 |
// Unlike .bim export, .map export does NOT verify that all remaining |
| 816 |
// variants are biallelic. |
| 817 |
if (unlikely(allele_idx_offsets[variant_uidx + 1] != allele_idx_offset_base + 2)) { |
| 818 |
logputs("\n"); |
| 819 |
logerrprintfww("Error: %s cannot contain multiallelic variants.\n", outname); |
| 820 |
goto ExportPed_ret_INCONSISTENT_INPUT; |
| 821 |
} |
| 822 |
} |
| 823 |
const char* ref_allele = allele_storage[allele_idx_offset_base]; |
| 824 |
const char* alt_allele = allele_storage[allele_idx_offset_base + 1]; |
| 825 |
const uint32_t ref_slen = strlen(ref_allele); |
| 826 |
const uint32_t alt_slen = strlen(alt_allele); |
| 827 |
// Note that this is based on PLINK 1 .bed, not PLINK 2, 2-bit |
| 828 |
// encoding. i.e. 0 = alt-alt, 2 = alt-ref (rendered in that order |
| 829 |
// in the .ped). |
| 830 |
if (PtrWSubCk(tmp_alloc_base, 3 * (ref_slen + alt_slen) + 9, &tmp_alloc_end)) { |
| 831 |
goto ExportPed_ret_NOMEM; |
| 832 |
} |
| 833 |
char* geno0_start = R_CAST(char*, tmp_alloc_end); |
| 834 |
char* geno0_iter = geno0_start; |
| 835 |
*geno0_iter++ = exportf_delim; |
| 836 |
geno0_iter = memcpyax(geno0_iter, alt_allele, alt_slen, exportf_delim); |
| 837 |
char* geno2_start = memcpya(geno0_iter, alt_allele, alt_slen + 1); |
| 838 |
*genotype_strs_iter++ = geno0_start; |
| 839 |
|
| 840 |
*genotype_strs_iter++ = missing_geno_str; |
| 841 |
|
| 842 |
char* geno2_iter = geno2_start; |
| 843 |
*geno2_iter++ = exportf_delim; |
| 844 |
geno2_iter = memcpyax(geno2_iter, alt_allele, alt_slen, exportf_delim); |
| 845 |
char* geno3_start = memcpya(geno2_iter, ref_allele, ref_slen + 1); |
| 846 |
*genotype_strs_iter++ = geno2_start; |
| 847 |
|
| 848 |
char* geno3_iter = geno3_start; |
| 849 |
*geno3_iter++ = exportf_delim; |
| 850 |
geno3_iter = memcpyax(geno3_iter, ref_allele, ref_slen, exportf_delim); |
| 851 |
memcpy(geno3_iter, ref_allele, ref_slen + 1); |
| 852 |
*genotype_strs_iter++ = geno3_start; |
| 853 |
} |
| 854 |
BigstackEndSet(tmp_alloc_end); |
| 855 |
if (max_allele_slen >= kMaxMediumLine / kBitsPerWord) { |
| 856 |
// We only check writebuf_flush after each block of kBitsPerWordD2 |
| 857 |
// genotypes. |
| 858 |
if (unlikely(bigstack_alloc_c(kMaxMediumLine + (1 + max_allele_slen) * kBitsPerWord, &writebuf))) { |
| 859 |
goto ExportPed_ret_NOMEM; |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
char* write_iter = writebuf; |
| 865 |
char* writebuf_flush = &(writebuf[kMaxMediumLine]); |
| 866 |
|
| 867 |
uintptr_t* pheno_nm = nullptr; |
| 868 |
uintptr_t* pheno_cc = nullptr; |
| 869 |
double* pheno_qt = nullptr; |
| 870 |
// .ped files don't support categorical phenotypes |
| 871 |
const uint32_t pheno_idx = FirstCcOrQtPhenoIdx(pheno_cols, pheno_ct); |
| 872 |
if (pheno_idx != UINT32_MAX) { |
| 873 |
const PhenoDtype type_code = pheno_cols[pheno_idx].type_code; |
| 874 |
pheno_nm = pheno_cols[pheno_idx].nonmiss; |
| 875 |
if (type_code == kPhenoDtypeCc) { |
| 876 |
pheno_cc = pheno_cols[pheno_idx].data.cc; |
| 877 |
} else { |
| 878 |
pheno_qt = pheno_cols[pheno_idx].data.qt; |
| 879 |
} |
| 880 |
} |
| 881 |
const uint32_t lomp_slen = strlen(legacy_output_missing_pheno); |
| 882 |
|
| 883 |
// * Read phase: main thread reads raw bytes with MultireadNonempty(), |
| 884 |
// while other thread(s) decode to standard 2-bit genovecs |
| 885 |
// * Write phase: one thread transposes+permutes to sample-major .bed, and |
| 886 |
// one thread renders and writes the final .ped text. (Strictly |
| 887 |
// speaking, the rotation is unnecessary, but its cost is negligible and |
| 888 |
// we want to reuse TransposeToPlink1SmajWriteThread.) |
| 889 |
|
| 890 |
// todo: check when this saturates |
| 891 |
uint32_t calc_thread_ct = (max_thread_ct > 2)? (max_thread_ct - 1) : max_thread_ct; |
| 892 |
STD_ARRAY_DECL(unsigned char*, 2, main_loadbufs); |
| 893 |
uint32_t read_block_size; |
| 894 |
|
| 895 |
// note that this is restricted to half of available workspace |
| 896 |
if (unlikely(PgenMtLoadInit(variant_include, sample_ct, variant_ct, bigstack_left() / 2, pgr_alloc_cacheline_ct, 0, 0, 0, pgfip, &calc_thread_ct, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &read_block_size, nullptr, main_loadbufs, &read_ctx.pgr_ptrs, &read_ctx.variant_uidx_starts))) { |
| 897 |
goto ExportPed_ret_NOMEM; |
| 898 |
} |
| 899 |
if (unlikely(SetThreadCt(calc_thread_ct, &read_tg))) { |
| 900 |
goto ExportPed_ret_NOMEM; |
| 901 |
} |
| 902 |
read_ctx.variant_include = variant_include; |
| 903 |
read_ctx.allele_idx_offsets = nullptr; |
| 904 |
read_ctx.allele_permute = nullptr; |
| 905 |
read_ctx.err_info = (~0LLU) << 32; |
| 906 |
SetThreadFuncAndData(TransposeToSmajReadThread, &read_ctx, &read_tg); |
| 907 |
|
| 908 |
const uintptr_t variant_cacheline_ct = NypCtToCachelineCt(variant_ct); |
| 909 |
// transpose_calc_thread_ct == 1, since transposition is much cheaper than |
| 910 |
// the final render+write step |
| 911 |
const uint32_t raw_sample_ctl = BitCtToWordCt(raw_sample_ct); |
| 912 |
uintptr_t* sample_include; |
| 913 |
uint32_t* sample_include_cumulative_popcounts; |
| 914 |
if (unlikely(SetThreadCt(1, &transpose_tg) || |
| 915 |
bigstack_alloc_w(raw_sample_ctl, &sample_include) || |
| 916 |
bigstack_alloc_u32(raw_sample_ctl, &sample_include_cumulative_popcounts) || |
| 917 |
bigstack_alloc_vp(1, &transpose_ctx.thread_vecaligned_bufs))) { |
| 918 |
goto ExportPed_ret_NOMEM; |
| 919 |
} |
| 920 |
transpose_ctx.thread_vecaligned_bufs[0] = S_CAST(VecW*, bigstack_alloc_raw(kPglNypTransposeBufbytes)); |
| 921 |
// each of the two transpose-result buffers should use <= 1/8 of the |
| 922 |
// remaining workspace |
| 923 |
const uintptr_t indmaj_bed_cachelines_avail = bigstack_left() / (kCacheline * 8); |
| 924 |
uint32_t sample_batch_size = kPglNypTransposeBatch; |
| 925 |
if (variant_cacheline_ct * kPglNypTransposeBatch > indmaj_bed_cachelines_avail) { |
| 926 |
sample_batch_size = RoundDownPow2(indmaj_bed_cachelines_avail / variant_cacheline_ct, kBitsPerWordD2); |
| 927 |
if (unlikely(!sample_batch_size)) { |
| 928 |
goto ExportPed_ret_NOMEM; |
| 929 |
} |
| 930 |
} |
| 931 |
transpose_ctx.smaj_writebufs[0] = S_CAST(uintptr_t*, bigstack_alloc_raw(variant_cacheline_ct * kCacheline * sample_batch_size)); |
| 932 |
transpose_ctx.smaj_writebufs[1] = S_CAST(uintptr_t*, bigstack_alloc_raw(variant_cacheline_ct * kCacheline * sample_batch_size)); |
| 933 |
|
| 934 |
const uintptr_t readbuf_vecs_avail = (bigstack_left() / kCacheline) * kVecsPerCacheline; |
| 935 |
if (unlikely(readbuf_vecs_avail < variant_ct)) { |
| 936 |
goto ExportPed_ret_NOMEM; |
| 937 |
} |
| 938 |
uintptr_t read_sample_ctv2 = readbuf_vecs_avail / variant_ct; |
| 939 |
uint32_t read_sample_ct; |
| 940 |
if (read_sample_ctv2 >= NypCtToVecCt(sample_ct)) { |
| 941 |
read_sample_ct = sample_ct; |
| 942 |
} else { |
| 943 |
read_sample_ct = read_sample_ctv2 * kNypsPerVec; |
| 944 |
} |
| 945 |
uintptr_t read_sample_ctaw2 = NypCtToAlignedWordCt(read_sample_ct); |
| 946 |
uintptr_t* vmaj_readbuf = S_CAST(uintptr_t*, bigstack_alloc_raw_rd(variant_ct * read_sample_ctaw2 * kBytesPerWord)); |
| 947 |
read_ctx.vmaj_readbuf = vmaj_readbuf; |
| 948 |
transpose_ctx.variant_include = variant_include; |
| 949 |
transpose_ctx.variant_ct = variant_ct; |
| 950 |
transpose_ctx.vmaj_readbuf = vmaj_readbuf; |
| 951 |
SetThreadFuncAndData(TransposeToPlink1SmajWriteThread, &transpose_ctx, &transpose_tg); |
| 952 |
|
| 953 |
const char* sample_ids = piip->sii.sample_ids; |
| 954 |
const char* paternal_ids = piip->parental_id_info.paternal_ids; |
| 955 |
const char* maternal_ids = piip->parental_id_info.maternal_ids; |
| 956 |
const uintptr_t max_sample_id_blen = piip->sii.max_sample_id_blen; |
| 957 |
const uintptr_t max_paternal_id_blen = piip->parental_id_info.max_paternal_id_blen; |
| 958 |
const uintptr_t max_maternal_id_blen = piip->parental_id_info.max_maternal_id_blen; |
| 959 |
|
| 960 |
uint32_t sample_uidx_start = AdvTo1Bit(orig_sample_include, 0); |
| 961 |
const uint32_t variant_ctl2 = NypCtToWordCt(variant_ct); |
| 962 |
const uintptr_t variant_ctaclw2 = variant_cacheline_ct * kWordsPerCacheline; |
| 963 |
const uint32_t final_backtrack_byte_ct = (4 - compound_genotypes) * ((-variant_ct) & (kBitsPerWordD2 - 1)); |
| 964 |
const uint32_t pass_ct = 1 + (sample_ct - 1) / read_sample_ct; |
| 965 |
for (uint32_t pass_idx = 0; pass_idx != pass_ct; ++pass_idx) { |
| 966 |
memcpy(sample_include, orig_sample_include, raw_sample_ctl * sizeof(intptr_t)); |
| 967 |
if (sample_uidx_start) { |
| 968 |
ClearBitsNz(0, sample_uidx_start, sample_include); |
| 969 |
} |
| 970 |
uint32_t sample_uidx_end; |
| 971 |
if (pass_idx + 1 == pass_ct) { |
| 972 |
read_sample_ct = sample_ct - pass_idx * read_sample_ct; |
| 973 |
read_sample_ctaw2 = NypCtToAlignedWordCt(read_sample_ct); |
| 974 |
sample_uidx_end = raw_sample_ct; |
| 975 |
} else { |
| 976 |
sample_uidx_end = FindNth1BitFrom(orig_sample_include, sample_uidx_start + 1, read_sample_ct); |
| 977 |
ClearBitsNz(sample_uidx_end, raw_sample_ct, sample_include); |
| 978 |
} |
| 979 |
FillCumulativePopcounts(sample_include, raw_sample_ctl, sample_include_cumulative_popcounts); |
| 980 |
read_ctx.sample_include = sample_include; |
| 981 |
read_ctx.sample_include_cumulative_popcounts = sample_include_cumulative_popcounts; |
| 982 |
read_ctx.sample_ct = read_sample_ct; |
| 983 |
transpose_ctx.sample_ct = read_sample_ct; |
| 984 |
if (pass_idx) { |
| 985 |
pgfip->block_base = main_loadbufs[0]; |
| 986 |
// er, don't need SetBaseAndOffset0? |
| 987 |
PgrSetBaseAndOffset0(main_loadbufs[0], calc_thread_ct, read_ctx.pgr_ptrs); |
| 988 |
} |
| 989 |
uint32_t parity = 0; |
| 990 |
uint32_t read_block_idx = 0; |
| 991 |
ReinitThreads(&read_tg); |
| 992 |
uint32_t pct = 0; |
| 993 |
uint32_t next_print_idx = variant_ct / 100; |
| 994 |
putc_unlocked('\r', stdout); |
| 995 |
printf("--export %s pass %u/%u: loading... 0%%", compound_genotypes? "compound-genotypes" : "ped", pass_idx + 1, pass_ct); |
| 996 |
fflush(stdout); |
| 997 |
for (uint32_t variant_idx = 0; ; ) { |
| 998 |
const uint32_t cur_block_write_ct = MultireadNonempty(variant_include, &read_tg, raw_variant_ct, read_block_size, pgfip, &read_block_idx, &reterr); |
| 999 |
if (unlikely(reterr)) { |
| 1000 |
goto ExportPed_ret_PGR_FAIL; |
| 1001 |
} |
| 1002 |
if (variant_idx) { |
| 1003 |
JoinThreads(&read_tg); |
| 1004 |
reterr = S_CAST(PglErr, read_ctx.err_info); |
| 1005 |
if (unlikely(reterr)) { |
| 1006 |
PgenErrPrintNV(reterr, read_ctx.err_info >> 32); |
| 1007 |
goto ExportPed_ret_1; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
if (!IsLastBlock(&read_tg)) { |
| 1011 |
read_ctx.cur_block_write_ct = cur_block_write_ct; |
| 1012 |
ComputeUidxStartPartition(variant_include, cur_block_write_ct, calc_thread_ct, read_block_idx * read_block_size, read_ctx.variant_uidx_starts); |
| 1013 |
PgrCopyBaseAndOffset(pgfip, calc_thread_ct, read_ctx.pgr_ptrs); |
| 1014 |
if (variant_idx + cur_block_write_ct == variant_ct) { |
| 1015 |
DeclareLastThreadBlock(&read_tg); |
| 1016 |
} |
| 1017 |
if (unlikely(SpawnThreads(&read_tg))) { |
| 1018 |
goto ExportPed_ret_THREAD_CREATE_FAIL; |
| 1019 |
} |
| 1020 |
} |
| 1021 |
parity = 1 - parity; |
| 1022 |
if (variant_idx == variant_ct) { |
| 1023 |
break; |
| 1024 |
} |
| 1025 |
if (variant_idx >= next_print_idx) { |
| 1026 |
if (pct > 10) { |
| 1027 |
putc_unlocked('\b', stdout); |
| 1028 |
} |
| 1029 |
pct = (variant_idx * 100LLU) / variant_ct; |
| 1030 |
printf("\b\b%u%%", pct++); |
| 1031 |
fflush(stdout); |
| 1032 |
next_print_idx = (pct * S_CAST(uint64_t, variant_ct)) / 100; |
| 1033 |
} |
| 1034 |
|
| 1035 |
++read_block_idx; |
| 1036 |
variant_idx += cur_block_write_ct; |
| 1037 |
pgfip->block_base = main_loadbufs[parity]; |
| 1038 |
} |
| 1039 |
// 2. Transpose and write. |
| 1040 |
ReinitThreads(&transpose_tg); |
| 1041 |
transpose_ctx.sample_batch_size = sample_batch_size; |
| 1042 |
parity = 0; |
| 1043 |
if (pct > 10) { |
| 1044 |
fputs("\b \b", stdout); |
| 1045 |
} |
| 1046 |
fputs("\b\b\b\b\b\b\b\b\b\b\b\b\bwriting... 0%", stdout); |
| 1047 |
fflush(stdout); |
| 1048 |
pct = 0; |
| 1049 |
uintptr_t sample_uidx_base; |
| 1050 |
uintptr_t sample_include_bits; |
| 1051 |
BitIter1Start(sample_include, sample_uidx_start, &sample_uidx_base, &sample_include_bits); |
| 1052 |
uint32_t flush_sample_idx = 0; |
| 1053 |
next_print_idx = read_sample_ct / 100; |
| 1054 |
for (uint32_t flush_sample_idx_end = 0; ; ) { |
| 1055 |
if (!IsLastBlock(&transpose_tg)) { |
| 1056 |
if (flush_sample_idx_end + sample_batch_size >= read_sample_ct) { |
| 1057 |
DeclareLastThreadBlock(&transpose_tg); |
| 1058 |
transpose_ctx.sample_batch_size = read_sample_ct - flush_sample_idx_end; |
| 1059 |
} |
| 1060 |
if (unlikely(SpawnThreads(&transpose_tg))) { |
| 1061 |
goto ExportPed_ret_THREAD_CREATE_FAIL; |
| 1062 |
} |
| 1063 |
} |
| 1064 |
if (flush_sample_idx_end) { |
| 1065 |
uintptr_t* indmaj_bed_iter = transpose_ctx.smaj_writebufs[1 - parity]; |
| 1066 |
for (; flush_sample_idx != flush_sample_idx_end; ++flush_sample_idx) { |
| 1067 |
char* line_uncounted_start = write_iter; |
| 1068 |
const uintptr_t sample_uidx = BitIter1(sample_include, &sample_uidx_base, &sample_include_bits); |
| 1069 |
const char* cur_sample_fid = &(sample_ids[sample_uidx * max_sample_id_blen]); |
| 1070 |
const char* fid_end = AdvToDelim(cur_sample_fid, '\t'); |
| 1071 |
write_iter = memcpyax(write_iter, cur_sample_fid, fid_end - cur_sample_fid, exportf_delim); |
| 1072 |
write_iter = strcpyax(write_iter, &(fid_end[1]), exportf_delim); |
| 1073 |
write_iter = strcpyax(write_iter, &(paternal_ids[sample_uidx * max_paternal_id_blen]), exportf_delim); |
| 1074 |
write_iter = strcpyax(write_iter, &(maternal_ids[sample_uidx * max_maternal_id_blen]), exportf_delim); |
| 1075 |
*write_iter++ = Sexchar(sex_nm, sex_male, sample_uidx); |
| 1076 |
*write_iter++ = exportf_delim; |
| 1077 |
if ((!pheno_nm) || (!IsSet(pheno_nm, sample_uidx))) { |
| 1078 |
write_iter = memcpya(write_iter, legacy_output_missing_pheno, lomp_slen); |
| 1079 |
} else if (pheno_cc) { |
| 1080 |
*write_iter++ = '1' + IsSet(pheno_cc, sample_uidx); |
| 1081 |
} else { |
| 1082 |
write_iter = dtoa_g(pheno_qt[sample_uidx], write_iter); |
| 1083 |
} |
| 1084 |
if (geno_pair_matrix) { |
| 1085 |
const uint32_t render_block_wsize = kMaxMediumLine / (4 * kBitsPerWordD2); |
| 1086 |
const char* geno_pair_matrix_iter = geno_pair_matrix; |
| 1087 |
for (uint32_t widx = 0; ; ) { |
| 1088 |
uint32_t widx_stop = widx + render_block_wsize; |
| 1089 |
if (widx_stop > variant_ctl2) { |
| 1090 |
if (widx == variant_ctl2) { |
| 1091 |
break; |
| 1092 |
} |
| 1093 |
widx_stop = variant_ctl2; |
| 1094 |
} |
| 1095 |
if (fwrite_ck(writebuf_flush, outfile, &write_iter)) { |
| 1096 |
goto ExportPed_ret_WRITE_FAIL; |
| 1097 |
} |
| 1098 |
if (!compound_genotypes) { |
| 1099 |
for (; widx != widx_stop; ++widx) { |
| 1100 |
uintptr_t geno_word = indmaj_bed_iter[widx]; |
| 1101 |
for (uint32_t pair_idx = 0; pair_idx != kBitsPerWordD4; ++pair_idx) { |
| 1102 |
const uintptr_t geno_pair = geno_word & 15; |
| 1103 |
write_iter = memcpya_k(write_iter, &(geno_pair_matrix_iter[geno_pair * 8]), 8); |
| 1104 |
geno_pair_matrix_iter = &(geno_pair_matrix_iter[128]); |
| 1105 |
geno_word >>= 4; |
| 1106 |
} |
| 1107 |
} |
| 1108 |
} else { |
| 1109 |
for (; widx != widx_stop; ++widx) { |
| 1110 |
uintptr_t geno_word = indmaj_bed_iter[widx]; |
| 1111 |
for (uintptr_t pair_idx = 0; pair_idx != kBitsPerWordD4; ++pair_idx) { |
| 1112 |
const uintptr_t geno_pair = geno_word & 15; |
| 1113 |
write_iter = memcpya_k(write_iter, &(geno_pair_matrix_iter[geno_pair * 6]), 6); |
| 1114 |
geno_pair_matrix_iter = &(geno_pair_matrix_iter[96]); |
| 1115 |
geno_word >>= 4; |
| 1116 |
} |
| 1117 |
} |
| 1118 |
} |
| 1119 |
} |
| 1120 |
write_iter -= final_backtrack_byte_ct; |
| 1121 |
if (fwrite_ck(writebuf_flush, outfile, &write_iter)) { |
| 1122 |
goto ExportPed_ret_WRITE_FAIL; |
| 1123 |
} |
| 1124 |
AppendBinaryEoln(&write_iter); |
| 1125 |
} else { |
| 1126 |
uint64_t line_blen = write_iter - line_uncounted_start; |
| 1127 |
uint32_t variant_idx = 0; |
| 1128 |
for (uint32_t widx = 0; ; ++widx) { |
| 1129 |
uint32_t variant_idx_stop = variant_idx + kBitsPerWordD2; |
| 1130 |
if (variant_idx_stop > variant_ct) { |
| 1131 |
if (variant_idx == variant_ct) { |
| 1132 |
break; |
| 1133 |
} |
| 1134 |
variant_idx_stop = variant_ct; |
| 1135 |
} |
| 1136 |
|
| 1137 |
line_blen += write_iter - line_uncounted_start; |
| 1138 |
if (fwrite_ck(writebuf_flush, outfile, &write_iter)) { |
| 1139 |
goto ExportPed_ret_WRITE_FAIL; |
| 1140 |
} |
| 1141 |
line_uncounted_start = write_iter; |
| 1142 |
|
| 1143 |
uintptr_t geno_word = indmaj_bed_iter[widx]; |
| 1144 |
for (; variant_idx != variant_idx_stop; ++variant_idx) { |
| 1145 |
const uintptr_t cur_geno = geno_word & 3; |
| 1146 |
// no overflow risk, since we errored out earlier if |
| 1147 |
// variant_ct * 4 > kMaxLongLine - small constant |
| 1148 |
write_iter = strcpya(write_iter, genotype_strs[variant_idx * 4 + cur_geno]); |
| 1149 |
geno_word >>= 2; |
| 1150 |
} |
| 1151 |
} |
| 1152 |
line_blen += write_iter - line_uncounted_start; |
| 1153 |
if (fwrite_ck(writebuf_flush, outfile, &write_iter)) { |
| 1154 |
goto ExportPed_ret_WRITE_FAIL; |
| 1155 |
} |
| 1156 |
AppendBinaryEoln(&write_iter); |
| 1157 |
line_blen += strlen(EOLN_STR); |
| 1158 |
if (unlikely(line_blen > kMaxLongLine)) { |
| 1159 |
logputs("\n"); |
| 1160 |
logerrputs("Error: --export ped would create an excessively long line.\n"); |
| 1161 |
goto ExportPed_ret_INCONSISTENT_INPUT; |
| 1162 |
} |
| 1163 |
} |
| 1164 |
indmaj_bed_iter = &(indmaj_bed_iter[variant_ctaclw2]); |
| 1165 |
} |
| 1166 |
if (flush_sample_idx_end == read_sample_ct) { |
| 1167 |
break; |
| 1168 |
} |
| 1169 |
if (flush_sample_idx_end >= next_print_idx) { |
| 1170 |
if (pct > 10) { |
| 1171 |
putc_unlocked('\b', stdout); |
| 1172 |
} |
| 1173 |
pct = (flush_sample_idx_end * 100LLU) / read_sample_ct; |
| 1174 |
printf("\b\b%u%%", pct++); |
| 1175 |
fflush(stdout); |
| 1176 |
next_print_idx = (pct * S_CAST(uint64_t, read_sample_ct)) / 100; |
| 1177 |
} |
| 1178 |
} |
| 1179 |
JoinThreads(&transpose_tg); |
| 1180 |
parity = 1 - parity; |
| 1181 |
flush_sample_idx_end += sample_batch_size; |
| 1182 |
if (flush_sample_idx_end > read_sample_ct) { |
| 1183 |
flush_sample_idx_end = read_sample_ct; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
sample_uidx_start = sample_uidx_end; |
| 1187 |
if (pct > 10) { |
| 1188 |
fputs("\b \b", stdout); |
| 1189 |
} |
| 1190 |
} |
| 1191 |
|
| 1192 |
if (unlikely(fclose_flush_null(writebuf_flush, write_iter, &outfile))) { |
| 1193 |
goto ExportPed_ret_WRITE_FAIL; |
| 1194 |
} |
| 1195 |
fputs("\b\bdone.\n", stdout); |
| 1196 |
logprintfww("--export %s: %s written.\n", compound_genotypes? "compound-genotypes" : "ped", outname); |
| 1197 |
} |
| 1198 |
while (0) { |
| 1199 |
ExportPed_ret_NOMEM: |
| 1200 |
reterr = kPglRetNomem; |
| 1201 |
break; |
| 1202 |
ExportPed_ret_OPEN_FAIL: |
| 1203 |
reterr = kPglRetOpenFail; |
| 1204 |
break; |
| 1205 |
ExportPed_ret_PGR_FAIL: |
| 1206 |
PgenErrPrintN(reterr); |
| 1207 |
break; |
| 1208 |
ExportPed_ret_WRITE_FAIL: |
| 1209 |
reterr = kPglRetWriteFail; |
| 1210 |
break; |
| 1211 |
ExportPed_ret_INCONSISTENT_INPUT: |
| 1212 |
reterr = kPglRetInconsistentInput; |
| 1213 |
break; |
| 1214 |
ExportPed_ret_THREAD_CREATE_FAIL: |
| 1215 |
reterr = kPglRetThreadCreateFail; |
| 1216 |
break; |
| 1217 |
} |
| 1218 |
ExportPed_ret_1: |
| 1219 |
CleanupThreads(&transpose_tg); |
| 1220 |
CleanupThreads(&read_tg); |
| 1221 |
fclose_cond(outfile); |
| 1222 |
pgfip->block_base = nullptr; |
| 1223 |
BigstackDoubleReset(bigstack_mark, bigstack_end_mark); |
| 1224 |
return reterr; |
| 1225 |
} |
| 1226 |
|
| 1227 |
#ifdef __cplusplus |
| 1228 |
} // namespace plink2 |
| 1229 |
#endif |