| 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 "include/pgenlib_write.h" |
| 19 |
#include "plink2_compress_stream.h" |
| 20 |
#include "plink2_import.h" |
| 21 |
#include "plink2_psam.h" |
| 22 |
|
| 23 |
#include <unistd.h> // unlink() |
| 24 |
|
| 25 |
// This covers formats that are fully supported by PLINK 1.x (no multiallelic |
| 26 |
// variants, dosages, or phase information). |
| 27 |
|
| 28 |
#ifdef __cplusplus |
| 29 |
namespace plink2 { |
| 30 |
#endif |
| 31 |
|
| 32 |
// Counts number of variants (both raw and with chromosome filter applied), |
| 33 |
// checks whether a nonzero CM column exists, and allocates+returns the |
| 34 |
// chromosome-and-negative-bp-filter bitvector. |
| 35 |
// Assumes FinalizeChrset() has already been called. |
| 36 |
// Errors out on .bim file. |
| 37 |
// variant_include assumed to be initialized to nullptr. |
| 38 |
PglErr ScanMap(const char* mapname, MiscFlags misc_flags, ChrInfo* cip, uint32_t* raw_variant_ct_ptr, uint32_t* variant_ct_ptr, uint32_t* at_least_one_nzero_cm_ptr, uintptr_t** variant_include_ptr) { |
| 39 |
unsigned char* bigstack_end_mark = g_bigstack_end; |
| 40 |
uintptr_t line_idx = 0; |
| 41 |
PglErr reterr = kPglRetSuccess; |
| 42 |
TextStream map_txs; |
| 43 |
PreinitTextStream(&map_txs); |
| 44 |
{ |
| 45 |
// Even though no .map line should exceed kMaxMediumLine bytes (128 KiB) in |
| 46 |
// practice, TextStream actually doesn't permit a max_line_blen value |
| 47 |
// smaller than kDecompressMinBlen (1 MiB), and there is little reason to |
| 48 |
// use a value below kTextStreamBlenFast (11 MiB) in practice unless memory |
| 49 |
// is very tight. |
| 50 |
reterr = InitTextStreamEx(mapname, 1, kTextStreamBlenFast, kTextStreamBlenFast, 1, &map_txs); |
| 51 |
if (unlikely(reterr)) { |
| 52 |
goto ScanMap_ret_TSTREAM_FAIL; |
| 53 |
} |
| 54 |
uintptr_t raw_variant_ct_limit = CHAR_BIT * S_CAST(uintptr_t, g_bigstack_end - g_bigstack_base); |
| 55 |
if (raw_variant_ct_limit > kPglMaxVariantCt) { |
| 56 |
raw_variant_ct_limit = kPglMaxVariantCt; |
| 57 |
} |
| 58 |
uintptr_t* variant_include = R_CAST(uintptr_t*, g_bigstack_base); |
| 59 |
|
| 60 |
char* line_start; |
| 61 |
do { |
| 62 |
++line_idx; |
| 63 |
line_start = TextGet(&map_txs); |
| 64 |
if (unlikely(!line_start)) { |
| 65 |
if (!TextStreamErrcode2(&map_txs, &reterr)) { |
| 66 |
logerrputs("Error: Empty .map file.\n"); |
| 67 |
goto ScanMap_ret_DEGENERATE_DATA; |
| 68 |
} |
| 69 |
goto ScanMap_ret_TSTREAM_FAIL; |
| 70 |
} |
| 71 |
} while (*line_start == '#'); |
| 72 |
uint32_t map_cols = 3; |
| 73 |
{ |
| 74 |
const char* linebuf_iter = NextTokenMult(line_start, 2); |
| 75 |
if (unlikely(!linebuf_iter)) { |
| 76 |
goto ScanMap_ret_MISSING_TOKENS; |
| 77 |
} |
| 78 |
linebuf_iter = NextToken(linebuf_iter); |
| 79 |
if (linebuf_iter) { |
| 80 |
linebuf_iter = NextToken(linebuf_iter); |
| 81 |
if (likely(!linebuf_iter)) { |
| 82 |
map_cols = 4; |
| 83 |
} else { |
| 84 |
// do NOT permit >4 columns here. |
| 85 |
snprintf(g_logbuf, kLogbufSize, "Error: %s is not a .map file (too many columns).\n", mapname); |
| 86 |
goto ScanMap_ret_MALFORMED_INPUT_WW; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
const uint32_t prohibit_extra_chr = (misc_flags / kfMiscProhibitExtraChr) & 1; |
| 92 |
uint32_t at_least_one_nzero_cm = 0; |
| 93 |
uint32_t raw_variant_ct = 0; |
| 94 |
uint32_t variant_ct = 0; |
| 95 |
uintptr_t variant_include_word = 0; |
| 96 |
uint32_t variant_uidx_lowbits = 0; |
| 97 |
char* line_iter = line_start; |
| 98 |
while (1) { |
| 99 |
if (unlikely(raw_variant_ct == raw_variant_ct_limit)) { |
| 100 |
if (raw_variant_ct_limit == kPglMaxVariantCt) { |
| 101 |
logerrputs("Error: " PROG_NAME_STR " does not support more than 2^31 - 3 variants. We recommend using\nother software for very deep studies of small numbers of genomes.\n"); |
| 102 |
goto ScanMap_ret_MALFORMED_INPUT; |
| 103 |
} |
| 104 |
goto ScanMap_ret_NOMEM; |
| 105 |
} |
| 106 |
{ |
| 107 |
// chrom, id, (cm?), pos |
| 108 |
char* chr_code_end = CurTokenEnd(line_iter); |
| 109 |
char* variant_id_start = FirstNonTspace(chr_code_end); |
| 110 |
uint32_t cur_chr_code; |
| 111 |
reterr = GetOrAddChrCodeDestructive(".map file", line_idx, prohibit_extra_chr, line_iter, chr_code_end, cip, &cur_chr_code); |
| 112 |
if (unlikely(reterr)) { |
| 113 |
goto ScanMap_ret_1; |
| 114 |
} |
| 115 |
if (!IsSet(cip->chr_mask, cur_chr_code)) { |
| 116 |
line_iter = variant_id_start; |
| 117 |
goto ScanMap_skip_variant; |
| 118 |
} |
| 119 |
char* third_token_start = FirstNonTspace(FirstSpaceOrEoln(variant_id_start)); |
| 120 |
char* bp_start = third_token_start; |
| 121 |
if (map_cols == 4) { |
| 122 |
bp_start = FirstNonTspace(FirstSpaceOrEoln(third_token_start)); |
| 123 |
} |
| 124 |
if (IsEolnKns(*bp_start)) { |
| 125 |
goto ScanMap_ret_MISSING_TOKENS; |
| 126 |
} |
| 127 |
int32_t cur_bp; |
| 128 |
if (unlikely(ScanIntAbsDefcap(bp_start, &cur_bp))) { |
| 129 |
snprintf(g_logbuf, kLogbufSize, "Error: Invalid bp coordinate on line %" PRIuPTR " of %s.\n", line_idx, mapname); |
| 130 |
goto ScanMap_ret_MALFORMED_INPUT_WW; |
| 131 |
} |
| 132 |
line_iter = bp_start; |
| 133 |
if (cur_bp < 0) { |
| 134 |
goto ScanMap_skip_variant; |
| 135 |
} |
| 136 |
if ((map_cols == 4) && (!at_least_one_nzero_cm)) { |
| 137 |
double cur_cm; |
| 138 |
char* cm_end = ScantokDouble(third_token_start, &cur_cm); |
| 139 |
if (unlikely(!cm_end)) { |
| 140 |
snprintf(g_logbuf, kLogbufSize, "Error: Invalid centimorgan position on line %" PRIuPTR " of %s.\n", line_idx, mapname); |
| 141 |
goto ScanMap_ret_MALFORMED_INPUT_WW; |
| 142 |
} |
| 143 |
at_least_one_nzero_cm = (cur_cm != 0.0); |
| 144 |
} |
| 145 |
variant_include_word |= k1LU << variant_uidx_lowbits; |
| 146 |
++variant_ct; |
| 147 |
} |
| 148 |
ScanMap_skip_variant: |
| 149 |
if (++variant_uidx_lowbits == kBitsPerWord) { |
| 150 |
variant_include[raw_variant_ct / kBitsPerWord] = variant_include_word; |
| 151 |
variant_uidx_lowbits = 0; |
| 152 |
variant_include_word = 0; |
| 153 |
} |
| 154 |
++raw_variant_ct; |
| 155 |
do { |
| 156 |
++line_idx; |
| 157 |
line_iter = AdvPastDelim(line_iter, '\n'); |
| 158 |
if (!TextGetUnsafe2(&map_txs, &line_iter)) { |
| 159 |
if (!TextStreamErrcode2(&map_txs, &reterr)) { |
| 160 |
goto ScanMap_eof; |
| 161 |
} |
| 162 |
goto ScanMap_ret_TSTREAM_FAIL; |
| 163 |
} |
| 164 |
// bugfix (4 Aug 2024): original .map specification permits interior |
| 165 |
// comment lines |
| 166 |
} while (line_iter[0] == '#'); |
| 167 |
} |
| 168 |
ScanMap_eof: |
| 169 |
if (unlikely(!variant_ct)) { |
| 170 |
logerrputs("Error: All variants in .map file skipped due to chromosome filter or negative\nbp coordinates.\n"); |
| 171 |
goto ScanMap_ret_INCONSISTENT_INPUT; |
| 172 |
} |
| 173 |
*raw_variant_ct_ptr = raw_variant_ct; |
| 174 |
*variant_ct_ptr = variant_ct; |
| 175 |
*at_least_one_nzero_cm_ptr = at_least_one_nzero_cm; |
| 176 |
if (raw_variant_ct != variant_ct) { |
| 177 |
if (variant_uidx_lowbits) { |
| 178 |
variant_include[raw_variant_ct / kBitsPerWord] = variant_include_word; |
| 179 |
} |
| 180 |
BigstackFinalizeW(variant_include, DivUp(raw_variant_ct, kBitsPerWord)); |
| 181 |
*variant_include_ptr = variant_include; |
| 182 |
} |
| 183 |
} |
| 184 |
while (0) { |
| 185 |
ScanMap_ret_TSTREAM_FAIL: |
| 186 |
TextStreamErrPrint(mapname, &map_txs); |
| 187 |
break; |
| 188 |
ScanMap_ret_NOMEM: |
| 189 |
reterr = kPglRetNomem; |
| 190 |
break; |
| 191 |
ScanMap_ret_MISSING_TOKENS: |
| 192 |
snprintf(g_logbuf, kLogbufSize, "Error: Line %" PRIuPTR " of %s has fewer tokens than expected.\n", line_idx, mapname); |
| 193 |
ScanMap_ret_MALFORMED_INPUT_WW: |
| 194 |
WordWrapB(0); |
| 195 |
logerrputsb(); |
| 196 |
ScanMap_ret_MALFORMED_INPUT: |
| 197 |
reterr = kPglRetMalformedInput; |
| 198 |
break; |
| 199 |
ScanMap_ret_INCONSISTENT_INPUT: |
| 200 |
reterr = kPglRetInconsistentInput; |
| 201 |
break; |
| 202 |
ScanMap_ret_DEGENERATE_DATA: |
| 203 |
reterr = kPglRetDegenerateData; |
| 204 |
break; |
| 205 |
} |
| 206 |
ScanMap_ret_1: |
| 207 |
CleanupTextStream2(mapname, &map_txs, &reterr); |
| 208 |
BigstackEndReset(bigstack_end_mark); |
| 209 |
return reterr; |
| 210 |
} |
| 211 |
|
| 212 |
// Assumes ScanMap() was previously called on the .map. |
| 213 |
PglErr MapToPvar(const char* mapname, const ChrInfo* cip, const char* const* allele_storage, uint32_t variant_ct, uint32_t max_allele_slen, ImportFlags import_flags, uint32_t at_least_one_nzero_cm, char* outname, char* outname_end) { |
| 214 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 215 |
uintptr_t line_idx = 0; |
| 216 |
PglErr reterr = kPglRetSuccess; |
| 217 |
TextStream map_txs; |
| 218 |
PreinitTextStream(&map_txs); |
| 219 |
|
| 220 |
char* cswritep = nullptr; |
| 221 |
CompressStreamState css; |
| 222 |
PreinitCstream(&css); |
| 223 |
{ |
| 224 |
snprintf(outname_end, kMaxOutfnameExtBlen, ".pvar"); |
| 225 |
const uint32_t output_zst = (import_flags / kfImportKeepAutoconvVzs) & 1; |
| 226 |
if (output_zst) { |
| 227 |
snprintf(&(outname_end[5]), kMaxOutfnameExtBlen - 5, ".zst"); |
| 228 |
} |
| 229 |
const uintptr_t overflow_buf_size = kCompressStreamBlock + 2 * kMaxIdSlen + 32 + 2 * max_allele_slen; |
| 230 |
reterr = InitCstreamAlloc(outname, 0, output_zst, 1, overflow_buf_size, &css, &cswritep); |
| 231 |
if (unlikely(reterr)) { |
| 232 |
goto MapToPvar_ret_1; |
| 233 |
} |
| 234 |
|
| 235 |
reterr = InitTextStreamEx(mapname, 0, kTextStreamBlenFast, kTextStreamBlenFast, 1, &map_txs); |
| 236 |
if (unlikely(reterr)) { |
| 237 |
goto MapToPvar_ret_TSTREAM_REWIND_FAIL; |
| 238 |
} |
| 239 |
|
| 240 |
char* line_start; |
| 241 |
do { |
| 242 |
++line_idx; |
| 243 |
line_start = TextGet(&map_txs); |
| 244 |
if (unlikely(!line_start)) { |
| 245 |
goto MapToPvar_ret_TSTREAM_REWIND_FAIL; |
| 246 |
} |
| 247 |
} while (*line_start == '#'); |
| 248 |
uint32_t map_cols = 3; |
| 249 |
{ |
| 250 |
const char* linebuf_iter = NextTokenMult(line_start, 2); |
| 251 |
if (unlikely(!linebuf_iter)) { |
| 252 |
goto MapToPvar_ret_REWIND_FAIL; |
| 253 |
} |
| 254 |
linebuf_iter = NextToken(linebuf_iter); |
| 255 |
if (linebuf_iter) { |
| 256 |
linebuf_iter = NextToken(linebuf_iter); |
| 257 |
if (likely(!linebuf_iter)) { |
| 258 |
map_cols = 4; |
| 259 |
} else { |
| 260 |
goto MapToPvar_ret_REWIND_FAIL; |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
cswritep = strcpya_k(cswritep, "#CHROM\tPOS\tID\tREF\tALT"); |
| 265 |
if (at_least_one_nzero_cm) { |
| 266 |
cswritep = strcpya_k(cswritep, "\tCM"); |
| 267 |
} |
| 268 |
AppendBinaryEoln(&cswritep); |
| 269 |
|
| 270 |
char* line_iter = line_start; |
| 271 |
for (uint32_t variant_idx = 0; ; ) { |
| 272 |
{ |
| 273 |
// chrom, id, (cm?), pos |
| 274 |
char* chr_code_end = CurTokenEnd(line_iter); |
| 275 |
const uint32_t cur_chr_code = GetChrCodeCounted(cip, chr_code_end - line_iter, line_iter); |
| 276 |
if (!IsSet(cip->chr_mask, cur_chr_code)) { |
| 277 |
line_iter = chr_code_end; |
| 278 |
goto MapToPvar_skip_variant; |
| 279 |
} |
| 280 |
char* variant_id_start = FirstNonTspace(chr_code_end); |
| 281 |
char* variant_id_end = CurTokenEnd(variant_id_start); |
| 282 |
char* third_token_start = FirstNonTspace(variant_id_end); |
| 283 |
char* bp_start = third_token_start; |
| 284 |
if (map_cols == 4) { |
| 285 |
bp_start = FirstNonTspace(FirstSpaceOrEoln(third_token_start)); |
| 286 |
} |
| 287 |
if (IsEolnKns(*bp_start)) { |
| 288 |
goto MapToPvar_ret_REWIND_FAIL; |
| 289 |
} |
| 290 |
int32_t cur_bp; |
| 291 |
if (unlikely(ScanIntAbsDefcap(bp_start, &cur_bp))) { |
| 292 |
goto MapToPvar_ret_REWIND_FAIL; |
| 293 |
} |
| 294 |
line_iter = bp_start; |
| 295 |
if (cur_bp < 0) { |
| 296 |
goto MapToPvar_skip_variant; |
| 297 |
} |
| 298 |
cswritep = chrtoa(cip, cur_chr_code, cswritep); |
| 299 |
*cswritep++ = '\t'; |
| 300 |
cswritep = u32toa_x(cur_bp, '\t', cswritep); |
| 301 |
const uint32_t variant_id_slen = variant_id_end - variant_id_start; |
| 302 |
if (variant_id_slen > kMaxIdSlen) { |
| 303 |
logerrputs("Error: Variant names are limited to " MAX_ID_SLEN_STR " characters.\n"); |
| 304 |
goto MapToPvar_ret_MALFORMED_INPUT; |
| 305 |
} |
| 306 |
cswritep = memcpyax(cswritep, variant_id_start, variant_id_slen, '\t'); |
| 307 |
const char* ref_allele = allele_storage[variant_idx * 2]; |
| 308 |
if (ref_allele) { |
| 309 |
cswritep = strcpya(cswritep, ref_allele); |
| 310 |
} else { |
| 311 |
*cswritep++ = '.'; |
| 312 |
} |
| 313 |
*cswritep++ = '\t'; |
| 314 |
const char* alt_allele = allele_storage[variant_idx * 2 + 1]; |
| 315 |
if (alt_allele) { |
| 316 |
cswritep = strcpya(cswritep, alt_allele); |
| 317 |
} else { |
| 318 |
*cswritep++ = '.'; |
| 319 |
} |
| 320 |
if (at_least_one_nzero_cm) { |
| 321 |
double cur_cm; |
| 322 |
char* cm_end = ScantokDouble(third_token_start, &cur_cm); |
| 323 |
if (unlikely(!cm_end)) { |
| 324 |
logerrprintfww("Error: Invalid centimorgan position on line %" PRIuPTR " of %s.\n", line_idx, mapname); |
| 325 |
goto MapToPvar_ret_MALFORMED_INPUT; |
| 326 |
} |
| 327 |
*cswritep++ = '\t'; |
| 328 |
cswritep = dtoa_g_p8(cur_cm, cswritep); |
| 329 |
} |
| 330 |
AppendBinaryEoln(&cswritep); |
| 331 |
if (unlikely(Cswrite(&css, &cswritep))) { |
| 332 |
goto MapToPvar_ret_WRITE_FAIL; |
| 333 |
} |
| 334 |
++variant_idx; |
| 335 |
if (variant_idx == variant_ct) { |
| 336 |
break; |
| 337 |
} |
| 338 |
} |
| 339 |
MapToPvar_skip_variant: |
| 340 |
do { |
| 341 |
++line_idx; |
| 342 |
line_iter = AdvPastDelim(line_iter, '\n'); |
| 343 |
if (unlikely(!TextGetUnsafe2(&map_txs, &line_iter))) { |
| 344 |
TextStreamErrcode2(&map_txs, &reterr); |
| 345 |
goto MapToPvar_ret_TSTREAM_REWIND_FAIL; |
| 346 |
} |
| 347 |
} while (line_iter[0] == '#'); |
| 348 |
} |
| 349 |
} |
| 350 |
while (0) { |
| 351 |
MapToPvar_ret_TSTREAM_REWIND_FAIL: |
| 352 |
TextStreamErrPrintRewind(".map file", &map_txs, &reterr); |
| 353 |
break; |
| 354 |
MapToPvar_ret_REWIND_FAIL: |
| 355 |
logerrprintfww(kErrprintfRewind, ".map file"); |
| 356 |
reterr = kPglRetRewindFail; |
| 357 |
break; |
| 358 |
MapToPvar_ret_WRITE_FAIL: |
| 359 |
reterr = kPglRetWriteFail; |
| 360 |
break; |
| 361 |
MapToPvar_ret_MALFORMED_INPUT: |
| 362 |
reterr = kPglRetMalformedInput; |
| 363 |
break; |
| 364 |
} |
| 365 |
MapToPvar_ret_1: |
| 366 |
CswriteCloseCond(&css, cswritep); |
| 367 |
CleanupTextStream2(mapname, &map_txs, &reterr); |
| 368 |
BigstackReset(bigstack_mark); |
| 369 |
return reterr; |
| 370 |
} |
| 371 |
|
| 372 |
// could add an option to LoadPvar() to not require allele columns, but .map |
| 373 |
// is easy enough to write a separate loader for... |
| 374 |
CONSTI32(kLoadMapBlockSize, 65536); |
| 375 |
|
| 376 |
// assumes FinalizeChrset() has already been called. |
| 377 |
// .bim ok |
| 378 |
PglErr LoadMap(const char* mapname, MiscFlags misc_flags, ChrInfo* cip, uint32_t* max_variant_id_slen_ptr, ChrIdx** variant_chr_codes_ptr, uint32_t** variant_bps_ptr, char*** variant_ids_ptr, double** variant_cms_ptr, uint32_t* variant_ct_ptr) { |
| 379 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 380 |
unsigned char* bigstack_end_mark = g_bigstack_end; |
| 381 |
uintptr_t line_idx = 0; |
| 382 |
PglErr reterr = kPglRetSuccess; |
| 383 |
TextStream map_txs; |
| 384 |
PreinitTextStream(&map_txs); |
| 385 |
{ |
| 386 |
// Workspace used as follows: |
| 387 |
// |--linebuf--|--temp-->----|----<- variant IDs --| |
| 388 |
// 1/4 end |
| 389 |
// linebuf is overwritten with the main return arrays at the end. |
| 390 |
reterr = SizeAndInitTextStream(mapname, bigstack_left() / 4, 1, &map_txs); |
| 391 |
if (unlikely(reterr)) { |
| 392 |
goto LoadMap_ret_TSTREAM_FAIL; |
| 393 |
} |
| 394 |
char* line_start; |
| 395 |
do { |
| 396 |
++line_idx; |
| 397 |
line_start = TextGet(&map_txs); |
| 398 |
if (unlikely(!line_start)) { |
| 399 |
if (!TextStreamErrcode2(&map_txs, &reterr)) { |
| 400 |
logerrputs("Error: Empty .map file.\n"); |
| 401 |
goto LoadMap_ret_DEGENERATE_DATA; |
| 402 |
} |
| 403 |
goto LoadMap_ret_TSTREAM_FAIL; |
| 404 |
} |
| 405 |
} while (*line_start == '#'); |
| 406 |
uint32_t map_cols = 3; |
| 407 |
{ |
| 408 |
const char* linebuf_iter = NextTokenMult(line_start, 2); |
| 409 |
if (unlikely(!linebuf_iter)) { |
| 410 |
goto LoadMap_ret_MISSING_TOKENS; |
| 411 |
} |
| 412 |
linebuf_iter = NextToken(linebuf_iter); |
| 413 |
if (linebuf_iter) { |
| 414 |
linebuf_iter = NextToken(linebuf_iter); |
| 415 |
if (!linebuf_iter) { |
| 416 |
map_cols = 4; |
| 417 |
} else { |
| 418 |
linebuf_iter = NextToken(linebuf_iter); |
| 419 |
if (linebuf_iter) { |
| 420 |
if (unlikely(NextToken(linebuf_iter))) { |
| 421 |
// do NOT permit >6 columns, .bim is ok but .pvar is not |
| 422 |
// (pointless to support .pvar for legacy formats) |
| 423 |
snprintf(g_logbuf, kLogbufSize, "Error: %s is not a .map/.bim file (too many columns).\n", mapname); |
| 424 |
goto LoadMap_ret_MALFORMED_INPUT_WW; |
| 425 |
} |
| 426 |
map_cols = 4; |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
const uint32_t prohibit_extra_chr = (misc_flags / kfMiscProhibitExtraChr) & 1; |
| 433 |
uint32_t max_variant_id_slen = *max_variant_id_slen_ptr; |
| 434 |
unsigned char* tmp_alloc_base = g_bigstack_base; |
| 435 |
unsigned char* tmp_alloc_end = bigstack_end_mark; |
| 436 |
uint16_t* cur_chr_codes = nullptr; |
| 437 |
uint32_t* cur_bps = nullptr; |
| 438 |
char** cur_ids = nullptr; |
| 439 |
double* cur_cms = nullptr; |
| 440 |
double cur_cm = 0.0; |
| 441 |
uint32_t at_least_one_nzero_cm = 0; |
| 442 |
uint32_t variant_ct = 0; |
| 443 |
char* line_iter = line_start; |
| 444 |
while (1) { |
| 445 |
{ |
| 446 |
// chrom, id, (cm?), pos |
| 447 |
char* chr_code_end = CurTokenEnd(line_iter); |
| 448 |
char* linebuf_iter = FirstNonTspace(chr_code_end); |
| 449 |
if (unlikely(IsEolnKns(*linebuf_iter))) { |
| 450 |
goto LoadMap_ret_MISSING_TOKENS; |
| 451 |
} |
| 452 |
uint32_t cur_chr_code; |
| 453 |
reterr = GetOrAddChrCodeDestructive(".map file", line_idx, prohibit_extra_chr, line_iter, chr_code_end, cip, &cur_chr_code); |
| 454 |
if (unlikely(reterr)) { |
| 455 |
goto LoadMap_ret_1; |
| 456 |
} |
| 457 |
if (!IsSet(cip->chr_mask, cur_chr_code)) { |
| 458 |
line_iter = linebuf_iter; |
| 459 |
goto LoadMap_skip_variant; |
| 460 |
} |
| 461 |
char* token_end = CurTokenEnd(linebuf_iter); |
| 462 |
uint32_t id_slen = token_end - linebuf_iter; |
| 463 |
if (id_slen > max_variant_id_slen) { |
| 464 |
max_variant_id_slen = id_slen; |
| 465 |
} |
| 466 |
tmp_alloc_end -= id_slen + 1; |
| 467 |
if (unlikely(tmp_alloc_end < tmp_alloc_base)) { |
| 468 |
goto LoadMap_ret_NOMEM; |
| 469 |
} |
| 470 |
memcpyx(tmp_alloc_end, linebuf_iter, id_slen, '\0'); |
| 471 |
linebuf_iter = FirstNonTspace(token_end); |
| 472 |
if (unlikely(IsEolnKns(*linebuf_iter))) { |
| 473 |
goto LoadMap_ret_MISSING_TOKENS; |
| 474 |
} |
| 475 |
|
| 476 |
if (map_cols == 4) { |
| 477 |
char* cm_end = ScantokDouble(linebuf_iter, &cur_cm); |
| 478 |
if (unlikely(!cm_end)) { |
| 479 |
snprintf(g_logbuf, kLogbufSize, "Error: Invalid centimorgan position on line %" PRIuPTR " of %s.\n", line_idx, mapname); |
| 480 |
goto LoadMap_ret_MALFORMED_INPUT_WW; |
| 481 |
} |
| 482 |
at_least_one_nzero_cm = (cur_cm != 0.0); |
| 483 |
linebuf_iter = NextToken(cm_end); |
| 484 |
if (unlikely(!linebuf_iter)) { |
| 485 |
goto LoadMap_ret_MISSING_TOKENS; |
| 486 |
} |
| 487 |
} |
| 488 |
int32_t cur_bp; |
| 489 |
if (unlikely(ScanIntAbsDefcap(linebuf_iter, &cur_bp))) { |
| 490 |
snprintf(g_logbuf, kLogbufSize, "Error: Invalid bp coordinate on line %" PRIuPTR " of %s.\n", line_idx, mapname); |
| 491 |
goto LoadMap_ret_MALFORMED_INPUT_WW; |
| 492 |
} |
| 493 |
line_iter = linebuf_iter; |
| 494 |
if (cur_bp < 0) { |
| 495 |
goto LoadMap_skip_variant; |
| 496 |
} |
| 497 |
|
| 498 |
const uint32_t variant_idx_lowbits = variant_ct % kLoadMapBlockSize; |
| 499 |
if (!variant_idx_lowbits) { |
| 500 |
if (unlikely(S_CAST(uintptr_t, tmp_alloc_end - tmp_alloc_base) <= kLoadMapBlockSize * (sizeof(int16_t) + sizeof(int32_t) + sizeof(intptr_t) + sizeof(double)))) { |
| 501 |
goto LoadMap_ret_NOMEM; |
| 502 |
} |
| 503 |
cur_chr_codes = R_CAST(uint16_t*, tmp_alloc_base); |
| 504 |
tmp_alloc_base = R_CAST(unsigned char*, &(cur_chr_codes[kLoadMapBlockSize])); |
| 505 |
cur_bps = R_CAST(uint32_t*, tmp_alloc_base); |
| 506 |
tmp_alloc_base = R_CAST(unsigned char*, &(cur_bps[kLoadMapBlockSize])); |
| 507 |
cur_ids = R_CAST(char**, tmp_alloc_base); |
| 508 |
tmp_alloc_base = R_CAST(unsigned char*, &(cur_ids[kLoadMapBlockSize])); |
| 509 |
cur_cms = R_CAST(double*, tmp_alloc_base); |
| 510 |
tmp_alloc_base = R_CAST(unsigned char*, &(cur_cms[kLoadMapBlockSize])); |
| 511 |
} |
| 512 |
cur_chr_codes[variant_idx_lowbits] = cur_chr_code; |
| 513 |
cur_ids[variant_idx_lowbits] = R_CAST(char*, tmp_alloc_end); |
| 514 |
cur_cms[variant_idx_lowbits] = cur_cm; |
| 515 |
cur_bps[variant_idx_lowbits] = cur_bp; |
| 516 |
++variant_ct; |
| 517 |
} |
| 518 |
LoadMap_skip_variant: |
| 519 |
do { |
| 520 |
++line_idx; |
| 521 |
line_iter = AdvPastDelim(line_iter, '\n'); |
| 522 |
if (!TextGetUnsafe2(&map_txs, &line_iter)) { |
| 523 |
if (!TextStreamErrcode2(&map_txs, &reterr)) { |
| 524 |
goto LoadMap_eof; |
| 525 |
} |
| 526 |
goto LoadMap_ret_TSTREAM_FAIL; |
| 527 |
} |
| 528 |
} while (line_iter[0] == '#'); |
| 529 |
} |
| 530 |
LoadMap_eof: |
| 531 |
if (unlikely(max_variant_id_slen > kMaxIdSlen)) { |
| 532 |
logerrputs("Error: Variant names are limited to " MAX_ID_SLEN_STR " characters.\n"); |
| 533 |
goto LoadMap_ret_MALFORMED_INPUT; |
| 534 |
} |
| 535 |
|
| 536 |
if (unlikely(!variant_ct)) { |
| 537 |
logerrputs("Error: All variants in .map/.bim file skipped due to chromosome filter or\nnegative bp coordinates.\n"); |
| 538 |
goto LoadMap_ret_INCONSISTENT_INPUT; |
| 539 |
} |
| 540 |
// true requirement is weaker, but whatever |
| 541 |
g_bigstack_end = g_bigstack_base; |
| 542 |
g_bigstack_base = TextStreamMemStart(&map_txs); |
| 543 |
if (unlikely(CleanupTextStream2(mapname, &map_txs, &reterr))) { |
| 544 |
goto LoadMap_ret_1; |
| 545 |
} |
| 546 |
|
| 547 |
if (unlikely(bigstack_alloc_chridx(variant_ct, variant_chr_codes_ptr) || |
| 548 |
bigstack_alloc_u32(variant_ct, variant_bps_ptr) || |
| 549 |
bigstack_alloc_cp(variant_ct, variant_ids_ptr))) { |
| 550 |
goto LoadMap_ret_NOMEM; |
| 551 |
} |
| 552 |
ChrIdx* variant_chr_codes = *variant_chr_codes_ptr; |
| 553 |
uint32_t* variant_bps = *variant_bps_ptr; |
| 554 |
char** variant_ids = *variant_ids_ptr; |
| 555 |
double* variant_cms = nullptr; |
| 556 |
if (at_least_one_nzero_cm) { |
| 557 |
if (unlikely(bigstack_alloc_d(variant_ct, variant_cms_ptr))) { |
| 558 |
goto LoadMap_ret_NOMEM; |
| 559 |
} |
| 560 |
variant_cms = *variant_cms_ptr; |
| 561 |
} else { |
| 562 |
*variant_cms_ptr = nullptr; |
| 563 |
} |
| 564 |
*max_variant_id_slen_ptr = max_variant_id_slen; |
| 565 |
*variant_ct_ptr = variant_ct; |
| 566 |
const uint32_t full_block_ct = variant_ct / kLoadMapBlockSize; |
| 567 |
bigstack_mark = g_bigstack_base; |
| 568 |
unsigned char* read_iter = g_bigstack_end; // bugfix (30 May 2018) |
| 569 |
BigstackEndSet(tmp_alloc_end); |
| 570 |
bigstack_end_mark = g_bigstack_end; |
| 571 |
|
| 572 |
for (uint32_t block_idx = 0; block_idx != full_block_ct; ++block_idx) { |
| 573 |
memcpy(&(variant_chr_codes[block_idx * kLoadMapBlockSize]), read_iter, kLoadMapBlockSize * sizeof(ChrIdx)); |
| 574 |
read_iter = &(read_iter[kLoadMapBlockSize * sizeof(ChrIdx)]); |
| 575 |
memcpy(&(variant_bps[block_idx * kLoadMapBlockSize]), read_iter, kLoadMapBlockSize * sizeof(int32_t)); |
| 576 |
read_iter = &(read_iter[kLoadMapBlockSize * sizeof(int32_t)]); |
| 577 |
memcpy(&(variant_ids[block_idx * kLoadMapBlockSize]), read_iter, kLoadMapBlockSize * sizeof(intptr_t)); |
| 578 |
read_iter = &(read_iter[kLoadMapBlockSize * sizeof(intptr_t)]); |
| 579 |
if (at_least_one_nzero_cm) { |
| 580 |
memcpy(&(variant_cms[block_idx * kLoadMapBlockSize]), read_iter, kLoadMapBlockSize * sizeof(double)); |
| 581 |
} |
| 582 |
read_iter = &(read_iter[kLoadMapBlockSize * sizeof(double)]); |
| 583 |
} |
| 584 |
const uint32_t variant_ct_lowbits = variant_ct % kLoadMapBlockSize; |
| 585 |
memcpy(&(variant_chr_codes[full_block_ct * kLoadMapBlockSize]), read_iter, variant_ct_lowbits * sizeof(ChrIdx)); |
| 586 |
read_iter = &(read_iter[kLoadMapBlockSize * sizeof(int16_t)]); |
| 587 |
memcpy(&(variant_bps[full_block_ct * kLoadMapBlockSize]), read_iter, variant_ct_lowbits * sizeof(int32_t)); |
| 588 |
read_iter = &(read_iter[kLoadMapBlockSize * sizeof(int32_t)]); |
| 589 |
memcpy(&(variant_ids[full_block_ct * kLoadMapBlockSize]), read_iter, variant_ct_lowbits * sizeof(intptr_t)); |
| 590 |
if (at_least_one_nzero_cm) { |
| 591 |
read_iter = &(read_iter[kLoadMapBlockSize * sizeof(intptr_t)]); |
| 592 |
memcpy(&(variant_cms[full_block_ct * kLoadMapBlockSize]), read_iter, variant_ct_lowbits * sizeof(double)); |
| 593 |
} |
| 594 |
} |
| 595 |
while (0) { |
| 596 |
LoadMap_ret_TSTREAM_FAIL: |
| 597 |
TextStreamErrPrint(mapname, &map_txs); |
| 598 |
break; |
| 599 |
LoadMap_ret_NOMEM: |
| 600 |
reterr = kPglRetNomem; |
| 601 |
break; |
| 602 |
LoadMap_ret_MISSING_TOKENS: |
| 603 |
snprintf(g_logbuf, kLogbufSize, "Error: Line %" PRIuPTR " of %s has fewer tokens than expected.\n", line_idx, mapname); |
| 604 |
LoadMap_ret_MALFORMED_INPUT_WW: |
| 605 |
WordWrapB(0); |
| 606 |
logerrputsb(); |
| 607 |
LoadMap_ret_MALFORMED_INPUT: |
| 608 |
reterr = kPglRetMalformedInput; |
| 609 |
break; |
| 610 |
LoadMap_ret_INCONSISTENT_INPUT: |
| 611 |
reterr = kPglRetInconsistentInput; |
| 612 |
break; |
| 613 |
LoadMap_ret_DEGENERATE_DATA: |
| 614 |
reterr = kPglRetDegenerateData; |
| 615 |
break; |
| 616 |
} |
| 617 |
LoadMap_ret_1: |
| 618 |
CleanupTextStream2(mapname, &map_txs, &reterr); |
| 619 |
BigstackDoubleReset(bigstack_mark, bigstack_end_mark); |
| 620 |
return reterr; |
| 621 |
} |
| 622 |
|
| 623 |
// Ok for in_psamname to alias outname. |
| 624 |
PglErr RewritePsam(const char* in_psamname, const char* missing_catname, MiscFlags misc_flags, FamCol fam_cols, int32_t missing_pheno, uint32_t psam_01, uint32_t max_thread_ct, char* outname, char* outname_end, uint32_t* raw_sample_ctp) { |
| 625 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 626 |
PhenoCol* pheno_cols = nullptr; |
| 627 |
char* pheno_names = nullptr; |
| 628 |
uint32_t pheno_ct = 0; |
| 629 |
PglErr reterr = kPglRetSuccess; |
| 630 |
{ |
| 631 |
PedigreeIdInfo pii; |
| 632 |
InitPedigreeIdInfo(misc_flags, &pii); |
| 633 |
uint32_t raw_sample_ct = 0; |
| 634 |
uintptr_t* sample_include = nullptr; |
| 635 |
uintptr_t* sex_nm = nullptr; |
| 636 |
uintptr_t* sex_male = nullptr; |
| 637 |
uintptr_t* founder_info = nullptr; |
| 638 |
uintptr_t max_pheno_name_blen = 0; |
| 639 |
reterr = LoadPsam(in_psamname, nullptr, missing_catname, fam_cols, 0x7fffffff, missing_pheno, (misc_flags / kfMiscAffection01) & 1, (misc_flags / kfMiscNoCategorical) & 1, (misc_flags / kfMiscNeg9PhenoReallyMissing) & 1, max_thread_ct, &pii, &sample_include, &founder_info, &sex_nm, &sex_male, &pheno_cols, &pheno_names, &raw_sample_ct, &pheno_ct, &max_pheno_name_blen); |
| 640 |
if (unlikely(reterr)) { |
| 641 |
goto RewritePsam_ret_1; |
| 642 |
} |
| 643 |
if (raw_sample_ctp) { |
| 644 |
*raw_sample_ctp = raw_sample_ct; |
| 645 |
} |
| 646 |
|
| 647 |
snprintf(outname_end, kMaxOutfnameExtBlen, ".psam"); |
| 648 |
// Note that --output-missing-phenotype doesn't apply to autoconversion. |
| 649 |
reterr = WritePsam(outname, sample_include, &(pii.sii), (&pii.parental_id_info), sex_nm, sex_male, pheno_cols, pheno_names, nullptr, "NA", raw_sample_ct, pheno_ct, max_pheno_name_blen, kfPsamColDefault, psam_01); |
| 650 |
} |
| 651 |
RewritePsam_ret_1: |
| 652 |
CleanupPhenoCols(pheno_ct, pheno_cols); |
| 653 |
free_cond(pheno_names); |
| 654 |
BigstackReset(bigstack_mark); |
| 655 |
return reterr; |
| 656 |
} |
| 657 |
|
| 658 |
BoolErr TpedToPgenSnp(uint32_t sample_idx_start, uint32_t sample_idx_stop, char allele1_char, char allele2_char, char input_missing_geno_char, char** text_iter_ptr, uintptr_t* genovec) { |
| 659 |
char* text_iter = *text_iter_ptr; |
| 660 |
for (uint32_t sample_idx = sample_idx_start; sample_idx != sample_idx_stop; ++sample_idx) { |
| 661 |
const char first_delim = text_iter[0]; |
| 662 |
const char first_allele_char = text_iter[1]; |
| 663 |
const char second_delim = text_iter[2]; |
| 664 |
const char second_allele_char = text_iter[3]; |
| 665 |
text_iter = &(text_iter[4]); |
| 666 |
if (unlikely(((first_delim != '\t') && (first_delim != ' ')) || ((second_delim != '\t') && (second_delim != ' ')))) { |
| 667 |
return 1; |
| 668 |
} |
| 669 |
uintptr_t cur_geno; |
| 670 |
if (first_allele_char == allele1_char) { |
| 671 |
if (second_allele_char == allele1_char) { |
| 672 |
continue; |
| 673 |
} |
| 674 |
if (unlikely(second_allele_char != allele2_char)) { |
| 675 |
return 1; |
| 676 |
} |
| 677 |
cur_geno = 1; |
| 678 |
} else if (first_allele_char == allele2_char) { |
| 679 |
if (second_allele_char == allele1_char) { |
| 680 |
cur_geno = 1; |
| 681 |
} else if (likely(second_allele_char == allele2_char)) { |
| 682 |
cur_geno = 2; |
| 683 |
} else { |
| 684 |
return 1; |
| 685 |
} |
| 686 |
} else { |
| 687 |
if (unlikely(((first_allele_char != '.') && (first_allele_char != input_missing_geno_char)) || ((second_allele_char != '.') && (second_allele_char != input_missing_geno_char)))) { |
| 688 |
return 1; |
| 689 |
} |
| 690 |
cur_geno = 3; |
| 691 |
} |
| 692 |
genovec[sample_idx / kBitsPerWordD2] |= cur_geno << ((sample_idx % kBitsPerWordD2) * 2); |
| 693 |
} |
| 694 |
*text_iter_ptr = text_iter; |
| 695 |
return 0; |
| 696 |
} |
| 697 |
|
| 698 |
// psam_generated assumed to be initialized to 1. |
| 699 |
// Unlike plink 1.9, this does not support lines longer than 2 GiB. |
| 700 |
// It's possible to parallelize this more, but it isn't realistically worth the |
| 701 |
// effort, since there's so little reason to use this format over VCF for |
| 702 |
// larger datasets. |
| 703 |
PglErr TpedToPgen(const char* tpedname, const char* tfamname, const char* missing_catname, MiscFlags misc_flags, ImportFlags import_flags, FamCol fam_cols, int32_t missing_pheno, char input_missing_geno_char, uint32_t max_thread_ct, char* outname, char* outname_end, ChrInfo* cip, uint32_t* psam_generated_ptr) { |
| 704 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 705 |
TextStream tped_txs; |
| 706 |
PreinitTextStream(&tped_txs); |
| 707 |
uintptr_t line_idx = 0; |
| 708 |
|
| 709 |
char* pvar_cswritep = nullptr; |
| 710 |
CompressStreamState pvar_css; |
| 711 |
PreinitCstream(&pvar_css); |
| 712 |
STPgenWriter spgw; |
| 713 |
PglErr reterr = kPglRetSuccess; |
| 714 |
PreinitSpgw(&spgw); |
| 715 |
{ |
| 716 |
uint32_t tfam_sample_ct = 0; |
| 717 |
if (import_flags & kfImportKeepAutoconv) { |
| 718 |
// Only need to generate a .psam if this is a conversion-only run, or |
| 719 |
// --keep-autoconv was specified. Otherwise Plink2Core() can simply |
| 720 |
// interpret the .tfam as a psam file. |
| 721 |
reterr = RewritePsam(tfamname, missing_catname, misc_flags, fam_cols, missing_pheno, 0, max_thread_ct, outname, outname_end, &tfam_sample_ct); |
| 722 |
if (unlikely(reterr)) { |
| 723 |
goto TpedToPgen_ret_1; |
| 724 |
} |
| 725 |
} else { |
| 726 |
// Could scan the .tfam for the sake of performing a consistency check, |
| 727 |
// but that check will happen later anyway. |
| 728 |
*psam_generated_ptr = 0; |
| 729 |
} |
| 730 |
|
| 731 |
// First pass: determine variant_ct (applying chromosome filter), |
| 732 |
// sample_ct, and maximum line length; check whether CM column needs to be |
| 733 |
// in .pvar file. |
| 734 |
// (Tried making this single-pass, did not improve performance.) |
| 735 |
const uint32_t decompress_thread_ct = MAXV(1, max_thread_ct - 1); |
| 736 |
reterr = SizeAndInitTextStream(tpedname, bigstack_left(), decompress_thread_ct, &tped_txs); |
| 737 |
if (unlikely(reterr)) { |
| 738 |
goto TpedToPgen_ret_TSTREAM_FAIL; |
| 739 |
} |
| 740 |
++line_idx; |
| 741 |
char* tped_line_start = TextGet(&tped_txs); |
| 742 |
if (unlikely(!tped_line_start)) { |
| 743 |
if (TextStreamErrcode2(&tped_txs, &reterr)) { |
| 744 |
goto TpedToPgen_ret_TSTREAM_FAIL; |
| 745 |
} |
| 746 |
snprintf(g_logbuf, kLogbufSize, "Error: %s is empty.\n", tpedname); |
| 747 |
goto TpedToPgen_ret_DEGENERATE_DATA; |
| 748 |
} |
| 749 |
uint32_t sample_ct; |
| 750 |
{ |
| 751 |
const uint32_t token_ct = CountTokens(tped_line_start); |
| 752 |
if (unlikely(token_ct < 6)) { |
| 753 |
logerrputs("Error: Too few columns in .tped file.\n"); |
| 754 |
goto TpedToPgen_ret_MALFORMED_INPUT; |
| 755 |
} |
| 756 |
sample_ct = (token_ct - 4) / 2; |
| 757 |
if (tfam_sample_ct) { |
| 758 |
if (unlikely(tfam_sample_ct != sample_ct)) { |
| 759 |
logerrprintfww("Error: .tped file has %u sample%s, while .tfam file has %u.\n", sample_ct, (sample_ct == 1)? "" : "s", tfam_sample_ct); |
| 760 |
goto TpedToPgen_ret_INCONSISTENT_INPUT; |
| 761 |
} |
| 762 |
} else { |
| 763 |
if (unlikely((token_ct % 2) == 1)) { |
| 764 |
snprintf(g_logbuf, kLogbufSize, "Error: Unexpected number of columns in .tped file (%u; even number expected).\n", token_ct); |
| 765 |
goto TpedToPgen_ret_MALFORMED_INPUT_WW; |
| 766 |
} |
| 767 |
} |
| 768 |
logprintf("--tped: %u sample%s present.\n", sample_ct, (sample_ct == 1)? "" : "s"); |
| 769 |
} |
| 770 |
FinalizeChrset(misc_flags, cip); |
| 771 |
const uint32_t prohibit_extra_chr = (misc_flags / kfMiscProhibitExtraChr) & 1; |
| 772 |
uint32_t max_line_blen = TextLineEnd(&tped_txs) - tped_line_start; |
| 773 |
uint32_t variant_ct = 0; |
| 774 |
uint32_t at_least_one_nzero_cm = 0; |
| 775 |
while (1) { |
| 776 |
char* chr_code_end = CurTokenEnd(tped_line_start); |
| 777 |
|
| 778 |
// must do this before chromosome-code null-termination |
| 779 |
char* cm_start = FirstNonTspace(FirstSpaceOrEoln(FirstNonTspace(chr_code_end))); |
| 780 |
char* bp_start = FirstNonTspace(FirstSpaceOrEoln(cm_start)); |
| 781 |
if (unlikely(IsEolnKns(*bp_start))) { |
| 782 |
goto TpedToPgen_ret_MISSING_TOKENS; |
| 783 |
} |
| 784 |
|
| 785 |
uint32_t cur_chr_code; |
| 786 |
reterr = GetOrAddChrCodeDestructive(".tped file", line_idx, prohibit_extra_chr, tped_line_start, chr_code_end, cip, &cur_chr_code); |
| 787 |
if (unlikely(reterr)) { |
| 788 |
goto TpedToPgen_ret_1; |
| 789 |
} |
| 790 |
if (IsSet(cip->chr_mask, cur_chr_code)) { |
| 791 |
int32_t cur_bp; |
| 792 |
if (unlikely(ScanIntAbsDefcap(bp_start, &cur_bp))) { |
| 793 |
snprintf(g_logbuf, kLogbufSize, "Error: Invalid bp coordinate on line %" PRIuPTR " of %s.\n", line_idx, tpedname); |
| 794 |
goto TpedToPgen_ret_MALFORMED_INPUT_WW; |
| 795 |
} |
| 796 |
if (cur_bp >= 0) { |
| 797 |
if (variant_ct == kPglMaxVariantCt) { |
| 798 |
logerrputs("Error: " PROG_NAME_STR " does not support more than 2^31 - 3 variants. We recommend using\nother software for very deep studies of small numbers of genomes.\n"); |
| 799 |
goto TpedToPgen_ret_MALFORMED_INPUT; |
| 800 |
} |
| 801 |
++variant_ct; |
| 802 |
|
| 803 |
if (!at_least_one_nzero_cm) { |
| 804 |
double cur_cm; |
| 805 |
if (unlikely(!ScantokDouble(cm_start, &cur_cm))) { |
| 806 |
snprintf(g_logbuf, kLogbufSize, "Error: Invalid centimorgan position on line %" PRIuPTR " of %s.\n", line_idx, tpedname); |
| 807 |
goto TpedToPgen_ret_MALFORMED_INPUT_WW; |
| 808 |
} |
| 809 |
at_least_one_nzero_cm = (cur_cm != 0.0); |
| 810 |
} |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
++line_idx; |
| 815 |
tped_line_start = TextGet(&tped_txs); |
| 816 |
if (!tped_line_start) { |
| 817 |
break; |
| 818 |
} |
| 819 |
const uint32_t cur_line_blen = TextLineEnd(&tped_txs) - tped_line_start; |
| 820 |
if (max_line_blen < cur_line_blen) { |
| 821 |
max_line_blen = cur_line_blen; |
| 822 |
} |
| 823 |
} |
| 824 |
if (unlikely(TextStreamErrcode2(&tped_txs, &reterr))) { |
| 825 |
goto TpedToPgen_ret_TSTREAM_FAIL; |
| 826 |
} |
| 827 |
if (unlikely(!variant_ct)) { |
| 828 |
logerrputs("Error: All .tped variants excluded by chromosome filter or negative bp\ncoordinates.\n"); |
| 829 |
goto TpedToPgen_ret_INCONSISTENT_INPUT; |
| 830 |
} |
| 831 |
if (unlikely(CleanupTextStream2(tpedname, &tped_txs, &reterr))) { |
| 832 |
goto TpedToPgen_ret_1; |
| 833 |
} |
| 834 |
BigstackReset(bigstack_mark); |
| 835 |
const uint32_t variant_skip_ct = line_idx - 1 - variant_ct; |
| 836 |
if (variant_skip_ct) { |
| 837 |
logprintfww("--tped: %" PRIuPTR " variants scanned; %u excluded by chromosome filter or negative bp coordinates, %u remaining.\n", line_idx - 1, variant_skip_ct, variant_ct); |
| 838 |
} else { |
| 839 |
logprintf("--tped: %u variant%s scanned.\n", variant_ct, (variant_ct == 1)? "" : "s"); |
| 840 |
} |
| 841 |
reterr = InitTextStream(tpedname, MAXV(max_line_blen, kTextStreamBlenFast), decompress_thread_ct, &tped_txs); |
| 842 |
if (unlikely(reterr)) { |
| 843 |
goto TpedToPgen_ret_TSTREAM_REWIND_FAIL; |
| 844 |
} |
| 845 |
|
| 846 |
snprintf(outname_end, kMaxOutfnameExtBlen, ".pvar"); |
| 847 |
const uint32_t output_zst = (import_flags / kfImportKeepAutoconvVzs) & 1; |
| 848 |
if (output_zst) { |
| 849 |
snprintf(&(outname_end[5]), kMaxOutfnameExtBlen - 5, ".zst"); |
| 850 |
} |
| 851 |
const uintptr_t overflow_buf_size = kCompressStreamBlock + 64 + max_line_blen; |
| 852 |
reterr = InitCstreamAlloc(outname, 0, output_zst, 1, overflow_buf_size, &pvar_css, &pvar_cswritep); |
| 853 |
if (unlikely(reterr)) { |
| 854 |
goto TpedToPgen_ret_1; |
| 855 |
} |
| 856 |
pvar_cswritep = strcpya_k(pvar_cswritep, "#CHROM\tPOS\tID\tREF\tALT"); |
| 857 |
if (at_least_one_nzero_cm) { |
| 858 |
pvar_cswritep = strcpya_k(pvar_cswritep, "\tCM"); |
| 859 |
} |
| 860 |
AppendBinaryEoln(&pvar_cswritep); |
| 861 |
|
| 862 |
snprintf(outname_end, kMaxOutfnameExtBlen, ".pgen"); |
| 863 |
uintptr_t spgw_alloc_cacheline_ct; |
| 864 |
uint32_t max_vrec_len; |
| 865 |
reterr = SpgwInitPhase1(outname, nullptr, nullptr, variant_ct, sample_ct, 0, kPgenWriteBackwardSeek, kfPgenGlobal0, 2, &spgw, &spgw_alloc_cacheline_ct, &max_vrec_len); |
| 866 |
if (unlikely(reterr)) { |
| 867 |
if (reterr == kPglRetOpenFail) { |
| 868 |
logerrprintfww(kErrprintfFopen, outname, strerror(errno)); |
| 869 |
} |
| 870 |
goto TpedToPgen_ret_1; |
| 871 |
} |
| 872 |
unsigned char* spgw_alloc; |
| 873 |
if (unlikely(bigstack_alloc_uc(spgw_alloc_cacheline_ct * kCacheline, &spgw_alloc))) { |
| 874 |
goto TpedToPgen_ret_NOMEM; |
| 875 |
} |
| 876 |
SpgwInitPhase2(max_vrec_len, &spgw, spgw_alloc); |
| 877 |
|
| 878 |
const uint32_t sample_ctl2 = NypCtToWordCt(sample_ct); |
| 879 |
uintptr_t* genovec; |
| 880 |
if (unlikely(bigstack_alloc_w(sample_ctl2, &genovec))) { |
| 881 |
goto TpedToPgen_ret_NOMEM; |
| 882 |
} |
| 883 |
|
| 884 |
fputs("--tped: 0%", stdout); |
| 885 |
char* tped_line_iter = TextLineEnd(&tped_txs); |
| 886 |
uint32_t variant_idx = 0; |
| 887 |
uint32_t pct = 0; |
| 888 |
uint32_t next_print_idx = variant_ct / 100; |
| 889 |
for (line_idx = 1; ; ++line_idx) { |
| 890 |
reterr = TextGetUnsafe(&tped_txs, &tped_line_iter); |
| 891 |
if (unlikely(reterr)) { |
| 892 |
goto TpedToPgen_ret_TSTREAM_REWIND_FAIL; |
| 893 |
} |
| 894 |
char* chr_code_end = CurTokenEnd(tped_line_iter); |
| 895 |
const uint32_t cur_chr_code = GetChrCodeCounted(cip, chr_code_end - tped_line_iter, tped_line_iter); |
| 896 |
if (!IsSet(cip->chr_mask, cur_chr_code)) { |
| 897 |
tped_line_iter = AdvPastDelim(chr_code_end, '\n'); |
| 898 |
continue; |
| 899 |
} |
| 900 |
char* variant_id_start = FirstNonTspace(chr_code_end); |
| 901 |
char* variant_id_end = CurTokenEnd(variant_id_start); |
| 902 |
char* cm_start = FirstNonTspace(variant_id_end); |
| 903 |
char* cm_end = CurTokenEnd(cm_start); |
| 904 |
char* bp_start = FirstNonTspace(cm_end); |
| 905 |
// previously verified that bp token exists |
| 906 |
int32_t cur_bp; |
| 907 |
if (unlikely(ScanIntAbsDefcap(bp_start, &cur_bp))) { |
| 908 |
snprintf(g_logbuf, kLogbufSize, "Error: Invalid bp coordinate on line %" PRIuPTR " of %s.\n", line_idx, tpedname); |
| 909 |
goto TpedToPgen_ret_MALFORMED_INPUT_WW; |
| 910 |
} |
| 911 |
if (cur_bp < 0) { |
| 912 |
tped_line_iter = AdvPastDelim(bp_start, '\n'); |
| 913 |
continue; |
| 914 |
} |
| 915 |
pvar_cswritep = chrtoa(cip, cur_chr_code, pvar_cswritep); |
| 916 |
*pvar_cswritep++ = '\t'; |
| 917 |
pvar_cswritep = u32toa_x(cur_bp, '\t', pvar_cswritep); |
| 918 |
pvar_cswritep = memcpyax(pvar_cswritep, variant_id_start, variant_id_end - variant_id_start, '\t'); |
| 919 |
tped_line_iter = FirstNonTspace(CurTokenEnd(bp_start)); |
| 920 |
|
| 921 |
ZeroWArr(sample_ctl2, genovec); |
| 922 |
const char* allele1 = nullptr; |
| 923 |
const char* allele2 = nullptr; |
| 924 |
uint32_t allele1_slen = 0; |
| 925 |
uint32_t allele2_slen = 0; |
| 926 |
uint32_t sample_idx = 0; |
| 927 |
for (; sample_idx != sample_ct; ++sample_idx) { |
| 928 |
if (unlikely(IsEolnKns(*tped_line_iter))) { |
| 929 |
goto TpedToPgen_ret_MISSING_TOKENS; |
| 930 |
} |
| 931 |
char* first_allele_end = CurTokenEnd(tped_line_iter); |
| 932 |
const uint32_t first_allele_slen = first_allele_end - tped_line_iter; |
| 933 |
uint32_t missing_ct = 0; |
| 934 |
if ((first_allele_slen == allele1_slen) && memequal(tped_line_iter, allele1, allele1_slen)) { |
| 935 |
// do nothing |
| 936 |
} else if ((first_allele_slen == 1) && ((tped_line_iter[0] == '.') || (tped_line_iter[0] == input_missing_geno_char))) { |
| 937 |
missing_ct = 1; |
| 938 |
} else if (!allele1_slen) { |
| 939 |
allele1 = tped_line_iter; |
| 940 |
allele1_slen = first_allele_slen; |
| 941 |
} else { |
| 942 |
allele2 = tped_line_iter; |
| 943 |
allele2_slen = first_allele_slen; |
| 944 |
break; |
| 945 |
} |
| 946 |
char* second_allele_start = FirstNonTspace(first_allele_end); |
| 947 |
if (unlikely(IsEolnKns(*second_allele_start))) { |
| 948 |
goto TpedToPgen_ret_MISSING_TOKENS; |
| 949 |
} |
| 950 |
char* second_allele_end = CurTokenEnd(second_allele_start); |
| 951 |
const uint32_t second_allele_slen = second_allele_end - second_allele_start; |
| 952 |
if ((second_allele_slen == allele1_slen) && memequal(second_allele_start, allele1, allele1_slen)) { |
| 953 |
} else if ((second_allele_slen == 1) && ((second_allele_start[0] == '.') || (second_allele_start[0] == input_missing_geno_char))) { |
| 954 |
++missing_ct; |
| 955 |
} else if (allele1_slen) { |
| 956 |
// only way allele1_slen can be zero here is if first allele was |
| 957 |
// missing, in which case we'll immediately exit on half-missing |
| 958 |
allele2 = second_allele_start; |
| 959 |
allele2_slen = second_allele_slen; |
| 960 |
break; |
| 961 |
} |
| 962 |
if (missing_ct) { |
| 963 |
if (unlikely(missing_ct == 1)) { |
| 964 |
goto TpedToPgen_ret_HALF_MISSING; |
| 965 |
} |
| 966 |
genovec[sample_idx / kBitsPerWordD2] |= (3 * k1LU) << ((sample_idx % kBitsPerWordD2) * 2); |
| 967 |
} |
| 968 |
tped_line_iter = FirstNonTspace(second_allele_end); |
| 969 |
} |
| 970 |
// At this point, both allele codes are locked in. |
| 971 |
if ((allele1_slen == 1) && (allele2_slen == 1)) { |
| 972 |
char* eoln_ptr = AdvToDelim(tped_line_iter, '\n'); |
| 973 |
if (eoln_ptr[-1] == '\r') { |
| 974 |
// don't punish Windows eoln |
| 975 |
--eoln_ptr; |
| 976 |
} |
| 977 |
const uint32_t remaining_sample_ct = sample_ct - sample_idx; |
| 978 |
char* text_iter = &(tped_line_iter[-1]); |
| 979 |
if (eoln_ptr != &(text_iter[remaining_sample_ct * 4])) { |
| 980 |
goto TpedToPgen_general_case; |
| 981 |
} |
| 982 |
// Common-case optimization: if both allele codes are length-1, and the |
| 983 |
// line ends at the earliest possible byte, valid lines must strictly |
| 984 |
// alternate between delimiter-bytes and allele-code bytes. |
| 985 |
// We don't increment sample_idx or tped_line_iter until the loop |
| 986 |
// finishes, since if anything unexpected happens, we back up and |
| 987 |
// reparse the line the slow way to generate the correct error message. |
| 988 |
const char allele1_char = allele1[0]; |
| 989 |
const char allele2_char = allele2[0]; |
| 990 |
uint32_t sample_idx2 = sample_idx; |
| 991 |
#ifdef __LP64__ |
| 992 |
// Try to use vector instructions to process most of the rest of the |
| 993 |
// line, falling back on the scalar loop for the beginning and the end. |
| 994 |
// |
| 995 |
// We use movemask to efficiently convert between text-space and |
| 996 |
// genovec-space. Its return type fits kInt16PerVec (16 for AVX2, 8 |
| 997 |
// otherwise) 2-bit genotype values, so that's the natural chunk size. |
| 998 |
// (Yes, this is overkill for the immediate problem, but it's a |
| 999 |
// reasonable testbed for parsing ideas.) |
| 1000 |
const uint32_t sample_idx_interior_start = RoundUpPow2(sample_idx, kInt16PerVec); |
| 1001 |
const uint32_t sample_idx_interior_stop = RoundDownPow2(sample_ct, kInt16PerVec); |
| 1002 |
if (sample_idx_interior_stop > sample_idx_interior_start) { |
| 1003 |
if (TpedToPgenSnp(sample_idx2, sample_idx_interior_start, allele1_char, allele2_char, input_missing_geno_char, &text_iter, genovec)) { |
| 1004 |
goto TpedToPgen_general_case; |
| 1005 |
} |
| 1006 |
const uint32_t vidx_stop = sample_idx_interior_stop / kInt16PerVec; |
| 1007 |
|
| 1008 |
const VecUc vvec_tab = vecuc_set1('\t'); |
| 1009 |
const VecUc vvec_space = vecuc_set1(' '); |
| 1010 |
const VecUc vvec_ref = vecuc_set1(allele1_char); |
| 1011 |
const VecUc vvec_alt = vecuc_set1(allele2_char); |
| 1012 |
const VecUc vvec_dot = vecuc_set1('.'); |
| 1013 |
const VecUc vvec_missing = vecuc_set1(input_missing_geno_char); |
| 1014 |
const VecUc m8 = vecuc_set1_epi16(0x00FF); |
| 1015 |
// We bitwise-and all validity checks with ok_acc, and then perform |
| 1016 |
// a single check of ok_acc's contents at loop-end. |
| 1017 |
VecUc ok_acc = vecuc_set1(-1); |
| 1018 |
|
| 1019 |
Vec8thUint* genovec_alias = R_CAST(Vec8thUint*, genovec); |
| 1020 |
for (uint32_t vidx = sample_idx_interior_start / kInt16PerVec; vidx != vidx_stop; ++vidx) { |
| 1021 |
const VecUc text0 = vecuc_loadu(text_iter); |
| 1022 |
const VecUc text1 = vecuc_loadu(&(text_iter[kBytesPerVec])); |
| 1023 |
text_iter = &(text_iter[2 * kBytesPerVec]); |
| 1024 |
|
| 1025 |
const VecUc even_all = vecuc_gather_even(text0, text1, m8); |
| 1026 |
ok_acc = ok_acc & ((even_all == vvec_tab) | (even_all == vvec_space)); |
| 1027 |
const VecUc odd_all = vecuc_gather_odd(text0, text1); |
| 1028 |
|
| 1029 |
const VecUc cur_ref = (odd_all == vvec_ref); |
| 1030 |
const VecUc cur_alt = (odd_all == vvec_alt); |
| 1031 |
const VecUc cur_missing = (odd_all == vvec_dot) | (odd_all == vvec_missing); |
| 1032 |
// All allele codes must match REF, ALT, or missing. |
| 1033 |
ok_acc = ok_acc & (cur_ref | cur_alt | cur_missing); |
| 1034 |
|
| 1035 |
// Half-missing check could be done in vector-space instead (so we |
| 1036 |
// only have one movemask operation instead of two per loop, and we |
| 1037 |
// remove an if-statement), but that code is more complicated, and |
| 1038 |
// doesn't seem to be any faster. |
| 1039 |
|
| 1040 |
// todo: better ARM implementation |
| 1041 |
const Vec8thUint alt_bits = vecuc_movemask(cur_alt); |
| 1042 |
const Vec8thUint missing_bits = vecuc_movemask(cur_missing); |
| 1043 |
// Even missing bytes must match odd missing bits. |
| 1044 |
if ((missing_bits ^ (missing_bits >> 1)) & (kVec8thUintMax / 3)) { |
| 1045 |
// Don't jump directly to HALF_MISSING, since there may be |
| 1046 |
// another earlier error. |
| 1047 |
goto TpedToPgen_general_case; |
| 1048 |
} |
| 1049 |
genovec_alias[vidx] = ((alt_bits & (kVec8thUintMax / 3)) + ((alt_bits >> 1) & (kVec8thUintMax / 3))) | missing_bits; |
| 1050 |
} |
| 1051 |
if (!vec0255_is_all_set(ok_acc)) { |
| 1052 |
goto TpedToPgen_general_case; |
| 1053 |
} |
| 1054 |
sample_idx2 = sample_idx_interior_stop; |
| 1055 |
} |
| 1056 |
#endif |
| 1057 |
if (unlikely(TpedToPgenSnp(sample_idx2, sample_ct, allele1_char, allele2_char, input_missing_geno_char, &text_iter, genovec))) { |
| 1058 |
goto TpedToPgen_general_case; |
| 1059 |
} |
| 1060 |
tped_line_iter = eoln_ptr; |
| 1061 |
} else { |
| 1062 |
TpedToPgen_general_case: |
| 1063 |
for (; sample_idx != sample_ct; ++sample_idx) { |
| 1064 |
if (unlikely(IsEolnKns(*tped_line_iter))) { |
| 1065 |
goto TpedToPgen_ret_MISSING_TOKENS; |
| 1066 |
} |
| 1067 |
char* first_allele_end = CurTokenEnd(tped_line_iter); |
| 1068 |
const uint32_t first_allele_slen = first_allele_end - tped_line_iter; |
| 1069 |
uintptr_t allele2_ct = 0; |
| 1070 |
if ((first_allele_slen == allele1_slen) && memequal(tped_line_iter, allele1, allele1_slen)) { |
| 1071 |
// do nothing |
| 1072 |
} else if ((first_allele_slen == allele2_slen) && memequal(tped_line_iter, allele2, allele2_slen)) { |
| 1073 |
allele2_ct = 1; |
| 1074 |
} else if (likely((first_allele_slen == 1) && ((tped_line_iter[0] == '.') || (tped_line_iter[0] == input_missing_geno_char)))) { |
| 1075 |
allele2_ct = 4; |
| 1076 |
} else { |
| 1077 |
goto TpedToPgen_ret_MULTIALLELIC; |
| 1078 |
} |
| 1079 |
char* second_allele_start = FirstNonTspace(first_allele_end); |
| 1080 |
if (unlikely(IsEolnKns(*second_allele_start))) { |
| 1081 |
goto TpedToPgen_ret_MISSING_TOKENS; |
| 1082 |
} |
| 1083 |
char* second_allele_end = CurTokenEnd(second_allele_start); |
| 1084 |
const uint32_t second_allele_slen = second_allele_end - second_allele_start; |
| 1085 |
if ((second_allele_slen == allele1_slen) && memequal(second_allele_start, allele1, allele1_slen)) { |
| 1086 |
// do nothing |
| 1087 |
} else if ((second_allele_slen == allele2_slen) && memequal(second_allele_start, allele2, allele2_slen)) { |
| 1088 |
++allele2_ct; |
| 1089 |
} else if (likely((second_allele_slen == 1) && ((second_allele_start[0] == '.') || (second_allele_start[0] == input_missing_geno_char)))) { |
| 1090 |
allele2_ct += 4; |
| 1091 |
} else { |
| 1092 |
goto TpedToPgen_ret_MULTIALLELIC; |
| 1093 |
} |
| 1094 |
if (allele2_ct) { |
| 1095 |
if (allele2_ct >= 4) { |
| 1096 |
if (unlikely(allele2_ct != 8)) { |
| 1097 |
// Value of 4 or 5 corresponds to 0/. or 1/. |
| 1098 |
goto TpedToPgen_ret_HALF_MISSING; |
| 1099 |
} |
| 1100 |
// Turn this into a genotype code. |
| 1101 |
allele2_ct = 3; |
| 1102 |
} |
| 1103 |
genovec[sample_idx / kBitsPerWordD2] |= allele2_ct << ((sample_idx % kBitsPerWordD2) * 2); |
| 1104 |
} |
| 1105 |
|
| 1106 |
tped_line_iter = FirstNonTspace(second_allele_end); |
| 1107 |
} |
| 1108 |
} |
| 1109 |
// Count alleles, and swap allele2 to REF if it's more common than |
| 1110 |
// allele1. |
| 1111 |
// (This could be done slightly more efficiently with e.g. a function |
| 1112 |
// that only tracked 0b00 and 0b10 counts, but it isn't a bottleneck.) |
| 1113 |
STD_ARRAY_DECL(uint32_t, 4, genocounts); |
| 1114 |
GenoarrCountFreqsUnsafe(genovec, sample_ct, genocounts); |
| 1115 |
if (genocounts[2] > genocounts[0]) { |
| 1116 |
const char* tmp_allele = allele1; |
| 1117 |
allele1 = allele2; |
| 1118 |
allele2 = tmp_allele; |
| 1119 |
|
| 1120 |
const uint32_t tmp_slen = allele1_slen; |
| 1121 |
allele1_slen = allele2_slen; |
| 1122 |
allele2_slen = tmp_slen; |
| 1123 |
|
| 1124 |
GenovecInvertUnsafe(sample_ct, genovec); |
| 1125 |
ZeroTrailingNyps(sample_ct, genovec); |
| 1126 |
} |
| 1127 |
|
| 1128 |
if (allele1) { |
| 1129 |
pvar_cswritep = memcpya(pvar_cswritep, allele1, allele1_slen); |
| 1130 |
} else { |
| 1131 |
*pvar_cswritep++ = '.'; |
| 1132 |
} |
| 1133 |
*pvar_cswritep++ = '\t'; |
| 1134 |
if (allele2) { |
| 1135 |
pvar_cswritep = memcpya(pvar_cswritep, allele2, allele2_slen); |
| 1136 |
} else { |
| 1137 |
*pvar_cswritep++ = '.'; |
| 1138 |
} |
| 1139 |
|
| 1140 |
if (at_least_one_nzero_cm) { |
| 1141 |
double cur_cm; |
| 1142 |
ScanadvDouble(cm_start, &cur_cm); |
| 1143 |
*pvar_cswritep++ = '\t'; |
| 1144 |
pvar_cswritep = dtoa_g_p8(cur_cm, pvar_cswritep); |
| 1145 |
} |
| 1146 |
AppendBinaryEoln(&pvar_cswritep); |
| 1147 |
if (unlikely(Cswrite(&pvar_css, &pvar_cswritep))) { |
| 1148 |
goto TpedToPgen_ret_WRITE_FAIL; |
| 1149 |
} |
| 1150 |
|
| 1151 |
if (unlikely(SpgwAppendBiallelicGenovec(genovec, &spgw))) { |
| 1152 |
goto TpedToPgen_ret_WRITE_FAIL; |
| 1153 |
} |
| 1154 |
|
| 1155 |
++variant_idx; |
| 1156 |
if (variant_idx >= next_print_idx) { |
| 1157 |
if (variant_idx == variant_ct) { |
| 1158 |
break; |
| 1159 |
} |
| 1160 |
if (pct > 10) { |
| 1161 |
putc_unlocked('\b', stdout); |
| 1162 |
} |
| 1163 |
pct = (variant_idx * 100LLU) / variant_ct; |
| 1164 |
printf("\b\b%u%%", pct++); |
| 1165 |
fflush(stdout); |
| 1166 |
next_print_idx = (pct * S_CAST(uint64_t, variant_ct)) / 100; |
| 1167 |
} |
| 1168 |
tped_line_iter = AdvPastDelim(tped_line_iter, '\n'); |
| 1169 |
} |
| 1170 |
reterr = SpgwFinish(&spgw); |
| 1171 |
if (unlikely(reterr)) { |
| 1172 |
goto TpedToPgen_ret_1; |
| 1173 |
} |
| 1174 |
putc_unlocked('\r', stdout); |
| 1175 |
char* write_iter = strcpya_k(g_logbuf, "--tped: "); |
| 1176 |
const uint32_t outname_base_slen = outname_end - outname; |
| 1177 |
write_iter = memcpya(write_iter, outname, outname_base_slen + 5); |
| 1178 |
write_iter = strcpya_k(write_iter, " + "); |
| 1179 |
write_iter = memcpya(write_iter, outname, outname_base_slen); |
| 1180 |
write_iter = strcpya_k(write_iter, ".pvar"); |
| 1181 |
if (output_zst) { |
| 1182 |
write_iter = strcpya_k(write_iter, ".zst"); |
| 1183 |
} |
| 1184 |
if (import_flags & kfImportKeepAutoconv) { |
| 1185 |
write_iter = strcpya_k(write_iter, " + "); |
| 1186 |
write_iter = memcpya(write_iter, outname, outname_base_slen); |
| 1187 |
write_iter = strcpya_k(write_iter, ".psam"); |
| 1188 |
} |
| 1189 |
strcpy_k(write_iter, " written.\n"); |
| 1190 |
WordWrapB(0); |
| 1191 |
logputsb(); |
| 1192 |
} |
| 1193 |
while (0) { |
| 1194 |
TpedToPgen_ret_NOMEM: |
| 1195 |
reterr = kPglRetNomem; |
| 1196 |
break; |
| 1197 |
TpedToPgen_ret_TSTREAM_FAIL: |
| 1198 |
TextStreamErrPrint(tpedname, &tped_txs); |
| 1199 |
break; |
| 1200 |
TpedToPgen_ret_TSTREAM_REWIND_FAIL: |
| 1201 |
if ((reterr == kPglRetOpenFail) || (reterr == kPglRetEof)) { |
| 1202 |
reterr = kPglRetRewindFail; |
| 1203 |
} |
| 1204 |
TextStreamErrPrintRewind(tpedname, &tped_txs, &reterr); |
| 1205 |
break; |
| 1206 |
TpedToPgen_ret_WRITE_FAIL: |
| 1207 |
reterr = kPglRetWriteFail; |
| 1208 |
break; |
| 1209 |
TpedToPgen_ret_HALF_MISSING: |
| 1210 |
putc_unlocked('\n', stdout); |
| 1211 |
logerrprintfww("Error: Half-missing genotype on line %" PRIuPTR " of %s.\n", line_idx, tpedname); |
| 1212 |
reterr = kPglRetMalformedInput; |
| 1213 |
break; |
| 1214 |
TpedToPgen_ret_MULTIALLELIC: |
| 1215 |
putc_unlocked('\n', stdout); |
| 1216 |
logerrprintfww("Error: Multiallelic variant on line %" PRIuPTR " of %s. This violates the .tped specification; please reformat this as e.g. VCF.\n", line_idx, tpedname); |
| 1217 |
reterr = kPglRetMalformedInput; |
| 1218 |
break; |
| 1219 |
TpedToPgen_ret_MISSING_TOKENS: |
| 1220 |
putc_unlocked('\n', stdout); |
| 1221 |
snprintf(g_logbuf, kLogbufSize, "Error: Line %" PRIuPTR " of %s has fewer tokens than expected.\n", line_idx, tpedname); |
| 1222 |
TpedToPgen_ret_MALFORMED_INPUT_WW: |
| 1223 |
WordWrapB(0); |
| 1224 |
logerrputsb(); |
| 1225 |
TpedToPgen_ret_MALFORMED_INPUT: |
| 1226 |
reterr = kPglRetMalformedInput; |
| 1227 |
break; |
| 1228 |
TpedToPgen_ret_INCONSISTENT_INPUT: |
| 1229 |
reterr = kPglRetInconsistentInput; |
| 1230 |
break; |
| 1231 |
TpedToPgen_ret_DEGENERATE_DATA: |
| 1232 |
reterr = kPglRetDegenerateData; |
| 1233 |
break; |
| 1234 |
} |
| 1235 |
TpedToPgen_ret_1: |
| 1236 |
CleanupSpgw(&spgw, &reterr); |
| 1237 |
CswriteCloseCond(&pvar_css, pvar_cswritep); |
| 1238 |
CleanupTextStream2(tpedname, &tped_txs, &reterr); |
| 1239 |
BigstackReset(bigstack_mark); |
| 1240 |
return reterr; |
| 1241 |
} |
| 1242 |
|
| 1243 |
typedef struct Plink1SmajTransposeCtxStruct { |
| 1244 |
uint32_t sample_ct; |
| 1245 |
uint32_t loadbuf_ul_stride; |
| 1246 |
|
| 1247 |
uintptr_t* plink1_smaj_loadbuf_iter; |
| 1248 |
|
| 1249 |
const uintptr_t* allele_flips_iter; |
| 1250 |
|
| 1251 |
VecW** thread_vecaligned_bufs; |
| 1252 |
uintptr_t** thread_write_genovecs; |
| 1253 |
PgenWriterCommon** pwcs; |
| 1254 |
|
| 1255 |
uint32_t cur_block_write_ct; |
| 1256 |
} Plink1SmajTransposeCtx; |
| 1257 |
|
| 1258 |
void Plink1SmajTransposeMain(uint32_t tidx, Plink1SmajTransposeCtx* ctx) { |
| 1259 |
const uint32_t sample_ct = ctx->sample_ct; |
| 1260 |
const uint32_t sample_ctaw2 = NypCtToAlignedWordCt(sample_ct); |
| 1261 |
const uint32_t write_batch_ct_m1 = (sample_ct - 1) / kPglNypTransposeBatch; |
| 1262 |
PgenWriterCommon* pwcp = ctx->pwcs[tidx]; |
| 1263 |
VecW* vecaligned_buf = ctx->thread_vecaligned_bufs[tidx]; |
| 1264 |
uintptr_t* write_genovec = ctx->thread_write_genovecs[tidx]; |
| 1265 |
const uintptr_t cur_block_write_ct = ctx->cur_block_write_ct; |
| 1266 |
const uint32_t loadbuf_ul_stride = ctx->loadbuf_ul_stride; |
| 1267 |
uint32_t write_idx = tidx * kPglVblockSize; |
| 1268 |
uintptr_t* read_iter = &(ctx->plink1_smaj_loadbuf_iter[write_idx / kBitsPerWordD2]); |
| 1269 |
const uintptr_t* allele_flips_iter = ctx->allele_flips_iter; |
| 1270 |
if (allele_flips_iter) { |
| 1271 |
allele_flips_iter = &(allele_flips_iter[write_idx / kBitsPerWord]); |
| 1272 |
} |
| 1273 |
const uint32_t write_idx_end = MINV(write_idx + kPglVblockSize, cur_block_write_ct); |
| 1274 |
while (write_idx < write_idx_end) { |
| 1275 |
const uintptr_t* read_iter2 = read_iter; |
| 1276 |
// uintptr_t* write_iter = write_genovec; |
| 1277 |
const uint32_t vblock_size = MINV(kPglNypTransposeBatch, write_idx_end - write_idx); |
| 1278 |
uint32_t read_batch_size = kPglNypTransposeBatch; |
| 1279 |
for (uint32_t write_batch_idx = 0; ; ++write_batch_idx) { |
| 1280 |
if (write_batch_idx >= write_batch_ct_m1) { |
| 1281 |
if (write_batch_idx > write_batch_ct_m1) { |
| 1282 |
break; |
| 1283 |
} |
| 1284 |
read_batch_size = ModNz(sample_ct, kPglNypTransposeBatch); |
| 1285 |
} |
| 1286 |
TransposeNypblock(read_iter2, loadbuf_ul_stride, sample_ctaw2, read_batch_size, vblock_size, &(write_genovec[write_batch_idx * kPglNypTransposeWords]), vecaligned_buf); |
| 1287 |
read_iter2 = &(read_iter2[kPglNypTransposeBatch * loadbuf_ul_stride]); |
| 1288 |
} |
| 1289 |
if (!allele_flips_iter) { |
| 1290 |
for (uint32_t uii = 0; uii != vblock_size; ++uii) { |
| 1291 |
uintptr_t* cur_write_genovec = &(write_genovec[uii * sample_ctaw2]); |
| 1292 |
PgrPlink1ToPlink2InplaceUnsafe(sample_ct, cur_write_genovec); |
| 1293 |
ZeroTrailingNyps(sample_ct, cur_write_genovec); |
| 1294 |
PwcAppendBiallelicGenovec(cur_write_genovec, pwcp); |
| 1295 |
} |
| 1296 |
} else { |
| 1297 |
for (uint32_t uii = 0; uii != vblock_size; ++uii) { |
| 1298 |
uintptr_t* cur_write_genovec = &(write_genovec[uii * sample_ctaw2]); |
| 1299 |
PgrPlink1ToPlink2InplaceUnsafe(sample_ct, cur_write_genovec); |
| 1300 |
if (IsSet(allele_flips_iter, uii)) { |
| 1301 |
// could have a dedicated flip-and-invert function |
| 1302 |
GenovecInvertUnsafe(sample_ct, cur_write_genovec); |
| 1303 |
} |
| 1304 |
ZeroTrailingNyps(sample_ct, cur_write_genovec); |
| 1305 |
PwcAppendBiallelicGenovec(cur_write_genovec, pwcp); |
| 1306 |
} |
| 1307 |
} |
| 1308 |
write_idx += vblock_size; |
| 1309 |
read_iter = &(read_iter[kPglNypTransposeWords]); |
| 1310 |
if (allele_flips_iter) { |
| 1311 |
allele_flips_iter = &(allele_flips_iter[kPglNypTransposeWords / 2]); |
| 1312 |
} |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
THREAD_FUNC_DECL Plink1SmajTransposeThread(void* raw_arg) { |
| 1317 |
ThreadGroupFuncArg* arg = S_CAST(ThreadGroupFuncArg*, raw_arg); |
| 1318 |
const uint32_t tidx = arg->tidx; |
| 1319 |
Plink1SmajTransposeCtx* ctx = S_CAST(Plink1SmajTransposeCtx*, arg->sharedp->context); |
| 1320 |
do { |
| 1321 |
Plink1SmajTransposeMain(tidx, ctx); |
| 1322 |
} while (!THREAD_BLOCK_FINISH(arg)); |
| 1323 |
THREAD_RETURN; |
| 1324 |
} |
| 1325 |
|
| 1326 |
PglErr Plink1SampleMajorToPgen(const char* pgenname, const uintptr_t* allele_flips, uintptr_t variant_ct, uintptr_t sample_ct, uint32_t real_ref_alleles, uint32_t max_thread_ct, FILE* infile) { |
| 1327 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 1328 |
MTPgenWriter* mpgwp = nullptr; |
| 1329 |
ThreadGroup tg; |
| 1330 |
PreinitThreads(&tg); |
| 1331 |
Plink1SmajTransposeCtx ctx; |
| 1332 |
PglErr reterr = kPglRetSuccess; |
| 1333 |
{ |
| 1334 |
// If allele_flips is nullptr, we are being called by Plink2Core(). |
| 1335 |
// Otherwise, we are being called by PedmapToPgen(). |
| 1336 |
if (!allele_flips) { |
| 1337 |
// file size already validated by PgfiInitPhase1() |
| 1338 |
logprintfww("Sample-major .bed file detected. Transposing to %s .\n", pgenname); |
| 1339 |
} else { |
| 1340 |
logprintfww("Transposing sample-major .bed to %s , and setting major alleles to provisional-REF.\n", pgenname); |
| 1341 |
} |
| 1342 |
fputs("0%", stdout); |
| 1343 |
fflush(stdout); |
| 1344 |
if (unlikely((!variant_ct) || (!sample_ct))) { |
| 1345 |
logputs("\n"); |
| 1346 |
logerrputs("Error: Zero-variant/zero-sample .pgen writing is not supported.\n"); |
| 1347 |
goto Plink1SampleMajorToPgen_ret_INCONSISTENT_INPUT; |
| 1348 |
} |
| 1349 |
const uint32_t variant_ct4 = NypCtToByteCt(variant_ct); |
| 1350 |
unsigned char* raw_loadbuf = nullptr; |
| 1351 |
uint32_t raw_load_batch_size = 1; |
| 1352 |
if (variant_ct4 < 5120) { |
| 1353 |
// assuming 4K block size, fseek won't let us avoid reading many |
| 1354 |
// unnecessary disk blocks |
| 1355 |
raw_load_batch_size += 131071 / variant_ct4; |
| 1356 |
if (unlikely(bigstack_alloc_uc(raw_load_batch_size * variant_ct4, &raw_loadbuf))) { |
| 1357 |
goto Plink1SampleMajorToPgen_ret_NOMEM; |
| 1358 |
} |
| 1359 |
// bugfix (2 Jun 2022): forgot to skip header bytes |
| 1360 |
if (unlikely(fseeko(infile, 3, SEEK_SET))) { |
| 1361 |
goto Plink1SampleMajorToPgen_ret_READ_FAIL; |
| 1362 |
} |
| 1363 |
} |
| 1364 |
const uint32_t raw_load_batch_ct_m1 = (sample_ct - 1) / raw_load_batch_size; |
| 1365 |
if (!raw_load_batch_ct_m1) { |
| 1366 |
raw_load_batch_size = sample_ct; |
| 1367 |
} |
| 1368 |
const uint32_t raw_load_batch_ct = raw_load_batch_ct_m1 + 1; |
| 1369 |
uintptr_t alloc_base_cacheline_ct; |
| 1370 |
uint64_t mpgw_per_thread_cacheline_ct; |
| 1371 |
uint32_t vrec_len_byte_ct; |
| 1372 |
uint64_t vblock_cacheline_ct; |
| 1373 |
MpgwInitPhase1(nullptr, variant_ct, sample_ct, kfPgenGlobal0, &alloc_base_cacheline_ct, &mpgw_per_thread_cacheline_ct, &vrec_len_byte_ct, &vblock_cacheline_ct); |
| 1374 |
#ifndef __LP64__ |
| 1375 |
if (unlikely((mpgw_per_thread_cacheline_ct > (0x7fffffff / kCacheline)) || (vblock_cacheline_ct > (0x7fffffff / kCacheline)))) { |
| 1376 |
goto Plink1SampleMajorToPgen_ret_NOMEM; |
| 1377 |
} |
| 1378 |
#endif |
| 1379 |
|
| 1380 |
uint32_t calc_thread_ct = DivUp(variant_ct, kPglVblockSize); |
| 1381 |
if (calc_thread_ct >= max_thread_ct) { |
| 1382 |
calc_thread_ct = (max_thread_ct > 2)? (max_thread_ct - 1) : max_thread_ct; |
| 1383 |
} |
| 1384 |
// note that BIGSTACK_ALLOC_X() doesn't work here due to variable-size |
| 1385 |
// array at end |
| 1386 |
mpgwp = S_CAST(MTPgenWriter*, bigstack_alloc((calc_thread_ct + DivUp(sizeof(MTPgenWriter), kBytesPerWord)) * sizeof(intptr_t))); |
| 1387 |
if (unlikely(!mpgwp)) { |
| 1388 |
goto Plink1SampleMajorToPgen_ret_NOMEM; |
| 1389 |
} |
| 1390 |
PreinitMpgw(mpgwp); |
| 1391 |
if (unlikely(bigstack_alloc_vp(calc_thread_ct, &ctx.thread_vecaligned_bufs) || |
| 1392 |
bigstack_alloc_wp(calc_thread_ct, &ctx.thread_write_genovecs))) { |
| 1393 |
goto Plink1SampleMajorToPgen_ret_NOMEM; |
| 1394 |
} |
| 1395 |
ctx.pwcs = &(mpgwp->pwcs[0]); |
| 1396 |
uintptr_t cachelines_avail = bigstack_left() / kCacheline; |
| 1397 |
// inner loop transposes kPglNypTransposeBatch variants at a time |
| 1398 |
const uintptr_t transpose_thread_cacheline_ct = kPglNypTransposeBufbytes / kCacheline + NypCtToVecCt(sample_ct) * (kPglNypTransposeBatch / kVecsPerCacheline); |
| 1399 |
if (unlikely(cachelines_avail < calc_thread_ct * S_CAST(uint64_t, transpose_thread_cacheline_ct))) { |
| 1400 |
goto Plink1SampleMajorToPgen_ret_NOMEM; |
| 1401 |
} |
| 1402 |
for (uint32_t tidx = 0; tidx != calc_thread_ct; ++tidx) { |
| 1403 |
ctx.thread_vecaligned_bufs[tidx] = S_CAST(VecW*, bigstack_alloc_raw(kPglNypTransposeBufbytes)); |
| 1404 |
ctx.thread_write_genovecs[tidx] = S_CAST(uintptr_t*, bigstack_alloc_raw(NypCtToVecCt(sample_ct) * kBytesPerVec * kPglNypTransposeBatch)); |
| 1405 |
} |
| 1406 |
cachelines_avail = bigstack_left() / kCacheline; |
| 1407 |
// Main workflow: |
| 1408 |
// 1. Load next calc_thread_ct * load_multiplier * kPglVblockSize |
| 1409 |
// variants. |
| 1410 |
// calc_thread_ct is reduced as necessary to ensure the compression |
| 1411 |
// write buffers use <= 1/8 of total workspace. |
| 1412 |
// with calc_thread_ct determined, load_multiplier is then chosen to use |
| 1413 |
// as much of the remaining workspace as possible. |
| 1414 |
// 2. Repeat load_multiplier times: |
| 1415 |
// a. Spawn threads processing calc_thread_ct vblocks |
| 1416 |
// b. Join threads |
| 1417 |
// c. Flush results |
| 1418 |
// 3. Goto step 1 unless eof. (load_multiplier may be smaller on last |
| 1419 |
// iteration.) |
| 1420 |
// No double-buffering here since main bottleneck is how many variants we |
| 1421 |
// can load at once. |
| 1422 |
if ((cachelines_avail / 8) < alloc_base_cacheline_ct + mpgw_per_thread_cacheline_ct * calc_thread_ct) { |
| 1423 |
if ((cachelines_avail / 8) >= alloc_base_cacheline_ct + mpgw_per_thread_cacheline_ct) { |
| 1424 |
calc_thread_ct = ((cachelines_avail / 8) - alloc_base_cacheline_ct) / mpgw_per_thread_cacheline_ct; |
| 1425 |
} else if (likely((cachelines_avail / 3) >= alloc_base_cacheline_ct + mpgw_per_thread_cacheline_ct)) { |
| 1426 |
calc_thread_ct = 1; |
| 1427 |
} else { |
| 1428 |
// possible todo: simple single-threaded fallback |
| 1429 |
// report this value since it can plausibly come up |
| 1430 |
g_failed_alloc_attempt_size = (alloc_base_cacheline_ct + mpgw_per_thread_cacheline_ct) * kCacheline * 3; |
| 1431 |
goto Plink1SampleMajorToPgen_ret_NOMEM; |
| 1432 |
} |
| 1433 |
} |
| 1434 |
// todo: determine appropriate calc_thread_ct limit. (should not be less |
| 1435 |
// than 7-8.) |
| 1436 |
unsigned char* mpgw_alloc = S_CAST(unsigned char*, bigstack_alloc_raw((alloc_base_cacheline_ct + mpgw_per_thread_cacheline_ct * calc_thread_ct) * kCacheline)); |
| 1437 |
reterr = MpgwInitPhase2(pgenname, nullptr, variant_ct, sample_ct, kPgenWriteBackwardSeek, kfPgenGlobal0, 2 - real_ref_alleles, vrec_len_byte_ct, vblock_cacheline_ct, calc_thread_ct, mpgw_alloc, mpgwp); |
| 1438 |
if (unlikely(reterr)) { |
| 1439 |
if (reterr == kPglRetOpenFail) { |
| 1440 |
logputs("\n"); |
| 1441 |
logerrprintfww(kErrprintfFopen, pgenname, strerror(errno)); |
| 1442 |
} |
| 1443 |
goto Plink1SampleMajorToPgen_ret_1; |
| 1444 |
} |
| 1445 |
|
| 1446 |
cachelines_avail = bigstack_left() / kCacheline; |
| 1447 |
const uint64_t full_load_vecs_req = sample_ct * S_CAST(uint64_t, NypCtToAlignedWordCt(variant_ct)); |
| 1448 |
uintptr_t* plink1_smaj_loadbuf; |
| 1449 |
uint32_t load_multiplier; |
| 1450 |
uint32_t cur_vidx_ct; |
| 1451 |
if (full_load_vecs_req > cachelines_avail * kVecsPerCacheline) { |
| 1452 |
// each iteration requires ((kPglVblockSize / 4) * calc_thread_ct * |
| 1453 |
// sample_ct) bytes to be loaded |
| 1454 |
load_multiplier = cachelines_avail / ((kPglVblockSize / (4 * kCacheline)) * calc_thread_ct * S_CAST(uintptr_t, sample_ct)); |
| 1455 |
assert(load_multiplier); |
| 1456 |
cur_vidx_ct = load_multiplier * calc_thread_ct * kPglVblockSize; |
| 1457 |
plink1_smaj_loadbuf = S_CAST(uintptr_t*, bigstack_alloc_raw_rd((cur_vidx_ct / 4) * S_CAST(uintptr_t, sample_ct))); |
| 1458 |
// bugfix (18 Nov 2017): this may be larger than variant_ct |
| 1459 |
if (cur_vidx_ct > variant_ct) { |
| 1460 |
cur_vidx_ct = variant_ct; |
| 1461 |
load_multiplier = 1 + (cur_vidx_ct - 1) / (kPglVblockSize * calc_thread_ct); |
| 1462 |
} |
| 1463 |
} else { |
| 1464 |
load_multiplier = 1 + ((variant_ct - 1) / (calc_thread_ct * kPglVblockSize)); |
| 1465 |
cur_vidx_ct = variant_ct; |
| 1466 |
plink1_smaj_loadbuf = S_CAST(uintptr_t*, bigstack_alloc_raw_rd(full_load_vecs_req * kBytesPerVec)); |
| 1467 |
} |
| 1468 |
uint32_t cur_vidx_ct4 = NypCtToByteCt(cur_vidx_ct); |
| 1469 |
uint32_t cur_vidx_ctaw2 = NypCtToAlignedWordCt(cur_vidx_ct); |
| 1470 |
const uint32_t pass_ct = 1 + (variant_ct - 1) / cur_vidx_ct; |
| 1471 |
ctx.sample_ct = sample_ct; |
| 1472 |
ctx.loadbuf_ul_stride = NypCtToVecCt(cur_vidx_ct) * kWordsPerVec; |
| 1473 |
ctx.allele_flips_iter = nullptr; |
| 1474 |
if (unlikely(SetThreadCt0(calc_thread_ct - 1, &tg))) { |
| 1475 |
goto Plink1SampleMajorToPgen_ret_NOMEM; |
| 1476 |
} |
| 1477 |
SetThreadFuncAndData(Plink1SmajTransposeThread, &ctx, &tg); |
| 1478 |
uint32_t pass_idx1 = 0; |
| 1479 |
for (uint32_t cur_vidx_base = 0; ; ) { |
| 1480 |
uint32_t cur_raw_load_batch_size = raw_load_batch_size; |
| 1481 |
uintptr_t* smaj_loadbuf_iter = plink1_smaj_loadbuf; |
| 1482 |
putc_unlocked('\r', stdout); |
| 1483 |
++pass_idx1; |
| 1484 |
printf("Pass %u/%u: loading... 0%%", pass_idx1, pass_ct); |
| 1485 |
fflush(stdout); |
| 1486 |
uint32_t pct = 0; |
| 1487 |
uint32_t next_print_idx = raw_load_batch_ct / 100; |
| 1488 |
const uint64_t seek_addl_offset = 3 + cur_vidx_base / 4; |
| 1489 |
for (uint32_t raw_load_batch_idx = 0; ; ) { |
| 1490 |
if (raw_load_batch_size == 1) { |
| 1491 |
if (unlikely(fseeko(infile, seek_addl_offset + raw_load_batch_idx * S_CAST(uint64_t, variant_ct4), SEEK_SET))) { |
| 1492 |
goto Plink1SampleMajorToPgen_ret_READ_FAIL; |
| 1493 |
} |
| 1494 |
if (unlikely(!fread_unlocked(smaj_loadbuf_iter, cur_vidx_ct4, 1, infile))) { |
| 1495 |
goto Plink1SampleMajorToPgen_ret_READ_FAIL; |
| 1496 |
} |
| 1497 |
smaj_loadbuf_iter = &(smaj_loadbuf_iter[cur_vidx_ctaw2]); |
| 1498 |
} else { |
| 1499 |
if (unlikely(!fread_unlocked(raw_loadbuf, cur_raw_load_batch_size * variant_ct4, 1, infile))) { |
| 1500 |
goto Plink1SampleMajorToPgen_ret_READ_FAIL; |
| 1501 |
} |
| 1502 |
unsigned char* raw_loadbuf_iter = &(raw_loadbuf[cur_vidx_base / 4]); |
| 1503 |
for (uint32_t uii = 0; uii != cur_raw_load_batch_size; ++uii) { |
| 1504 |
memcpy(smaj_loadbuf_iter, raw_loadbuf_iter, cur_vidx_ct4); |
| 1505 |
raw_loadbuf_iter = &(raw_loadbuf_iter[variant_ct4]); |
| 1506 |
smaj_loadbuf_iter = &(smaj_loadbuf_iter[cur_vidx_ctaw2]); |
| 1507 |
} |
| 1508 |
} |
| 1509 |
++raw_load_batch_idx; |
| 1510 |
if (raw_load_batch_idx >= raw_load_batch_ct_m1) { |
| 1511 |
if (raw_load_batch_idx > raw_load_batch_ct_m1) { |
| 1512 |
break; |
| 1513 |
} |
| 1514 |
cur_raw_load_batch_size = sample_ct - raw_load_batch_idx * raw_load_batch_size; |
| 1515 |
} |
| 1516 |
if (raw_load_batch_idx >= next_print_idx) { |
| 1517 |
if (pct > 10) { |
| 1518 |
putc_unlocked('\b', stdout); |
| 1519 |
} |
| 1520 |
pct = (raw_load_batch_idx * 100LLU) / raw_load_batch_ct; |
| 1521 |
printf("\b\b%u%%", pct++); |
| 1522 |
fflush(stdout); |
| 1523 |
next_print_idx = (pct * S_CAST(uint64_t, raw_load_batch_ct)) / 100; |
| 1524 |
} |
| 1525 |
} |
| 1526 |
const uintptr_t last_tidx = calc_thread_ct - 1; |
| 1527 |
uint32_t load_idx = 0; |
| 1528 |
ctx.cur_block_write_ct = calc_thread_ct * kPglVblockSize; |
| 1529 |
putc_unlocked('\r', stdout); |
| 1530 |
printf("Pass %u/%u: transposing and compressing... 0%%", pass_idx1, pass_ct); |
| 1531 |
ReinitThreads(&tg); |
| 1532 |
pct = 0; |
| 1533 |
next_print_idx = load_idx / 100; |
| 1534 |
do { |
| 1535 |
if (load_idx >= next_print_idx) { |
| 1536 |
if (pct > 10) { |
| 1537 |
putc_unlocked('\b', stdout); |
| 1538 |
} |
| 1539 |
pct = (load_idx * 100LLU) / load_multiplier; |
| 1540 |
printf("\b\b%u%%", pct++); |
| 1541 |
fflush(stdout); |
| 1542 |
next_print_idx = (pct * S_CAST(uint64_t, load_multiplier)) / 100; |
| 1543 |
} |
| 1544 |
ctx.plink1_smaj_loadbuf_iter = &(plink1_smaj_loadbuf[load_idx * calc_thread_ct * (kPglVblockSize / kBitsPerWordD2)]); |
| 1545 |
if (allele_flips) { |
| 1546 |
ctx.allele_flips_iter = &(allele_flips[load_idx * calc_thread_ct * (kPglVblockSize / kBitsPerWord)]); |
| 1547 |
} |
| 1548 |
if (++load_idx == load_multiplier) { |
| 1549 |
DeclareLastThreadBlock(&tg); |
| 1550 |
ctx.cur_block_write_ct = cur_vidx_ct - (load_idx - 1) * calc_thread_ct * kPglVblockSize; |
| 1551 |
} |
| 1552 |
if (last_tidx) { |
| 1553 |
if (unlikely(SpawnThreads(&tg))) { |
| 1554 |
goto Plink1SampleMajorToPgen_ret_THREAD_CREATE_FAIL; |
| 1555 |
} |
| 1556 |
} |
| 1557 |
Plink1SmajTransposeMain(last_tidx, &ctx); |
| 1558 |
if (last_tidx) { |
| 1559 |
JoinThreads(&tg); |
| 1560 |
// Plink1SmajTransposeThread() never errors out |
| 1561 |
} |
| 1562 |
reterr = MpgwFlush(mpgwp); |
| 1563 |
if (unlikely(reterr)) { |
| 1564 |
goto Plink1SampleMajorToPgen_ret_WRITE_FAIL; |
| 1565 |
} |
| 1566 |
} while (!IsLastBlock(&tg)); |
| 1567 |
cur_vidx_base += cur_vidx_ct; |
| 1568 |
if (cur_vidx_base == variant_ct) { |
| 1569 |
if (pct > 10) { |
| 1570 |
putc_unlocked('\b', stdout); |
| 1571 |
} |
| 1572 |
break; |
| 1573 |
} |
| 1574 |
fputs("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b ", stdout); |
| 1575 |
// assumes PgfiInitPhase1() leaves file pointer at byte 3; otherwise, |
| 1576 |
// necessary to put this at top of main loop |
| 1577 |
if (unlikely(fseeko(infile, 3, SEEK_SET))) { |
| 1578 |
goto Plink1SampleMajorToPgen_ret_READ_FAIL; |
| 1579 |
} |
| 1580 |
if (variant_ct - cur_vidx_base <= cur_vidx_ct) { |
| 1581 |
cur_vidx_ct = variant_ct - cur_vidx_base; |
| 1582 |
cur_vidx_ct4 = NypCtToByteCt(cur_vidx_ct); |
| 1583 |
cur_vidx_ctaw2 = NypCtToAlignedWordCt(cur_vidx_ct); |
| 1584 |
ctx.loadbuf_ul_stride = NypCtToVecCt(cur_vidx_ct) * kWordsPerVec; |
| 1585 |
load_multiplier = 1 + (cur_vidx_ct - 1) / (kPglVblockSize * calc_thread_ct); |
| 1586 |
} |
| 1587 |
} |
| 1588 |
mpgwp = nullptr; |
| 1589 |
fputs("\b\bdone.\n", stdout); |
| 1590 |
logprintf("Transpose complete.\n"); |
| 1591 |
} |
| 1592 |
while (0) { |
| 1593 |
Plink1SampleMajorToPgen_ret_NOMEM: |
| 1594 |
reterr = kPglRetNomem; |
| 1595 |
break; |
| 1596 |
Plink1SampleMajorToPgen_ret_READ_FAIL: |
| 1597 |
if (feof_unlocked(infile)) { |
| 1598 |
errno = 0; |
| 1599 |
} |
| 1600 |
logputs("\n"); |
| 1601 |
logerrprintfww(kErrprintfFread, ".bed file", rstrerror(errno)); |
| 1602 |
reterr = kPglRetReadFail; |
| 1603 |
break; |
| 1604 |
Plink1SampleMajorToPgen_ret_WRITE_FAIL: |
| 1605 |
reterr = kPglRetWriteFail; |
| 1606 |
break; |
| 1607 |
Plink1SampleMajorToPgen_ret_INCONSISTENT_INPUT: |
| 1608 |
reterr = kPglRetInconsistentInput; |
| 1609 |
break; |
| 1610 |
Plink1SampleMajorToPgen_ret_THREAD_CREATE_FAIL: |
| 1611 |
reterr = kPglRetThreadCreateFail; |
| 1612 |
break; |
| 1613 |
} |
| 1614 |
Plink1SampleMajorToPgen_ret_1: |
| 1615 |
CleanupThreads(&tg); |
| 1616 |
CleanupMpgw(mpgwp, &reterr); |
| 1617 |
BigstackReset(bigstack_mark); |
| 1618 |
return reterr; |
| 1619 |
} |
| 1620 |
|
| 1621 |
PglErr PedmapToPgen(const char* pedname, const char* mapname, const char* missing_catname, MiscFlags misc_flags, ImportFlags import_flags, uint32_t psam_01, FamCol fam_cols, int32_t missing_pheno, char input_missing_geno_char, uint32_t max_thread_ct, char* outname, char* outname_end, ChrInfo* cip) { |
| 1622 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 1623 |
unsigned char* bigstack_end_mark = g_bigstack_end; |
| 1624 |
FILE* indmaj_bed_file = nullptr; |
| 1625 |
FILE* tmp_fam_file = nullptr; |
| 1626 |
TextStream ped_txs; |
| 1627 |
PreinitTextStream(&ped_txs); |
| 1628 |
uintptr_t line_idx = 0; |
| 1629 |
|
| 1630 |
char* pvar_cswritep = nullptr; |
| 1631 |
CompressStreamState pvar_css; |
| 1632 |
PreinitCstream(&pvar_css); |
| 1633 |
PglErr reterr = kPglRetSuccess; |
| 1634 |
{ |
| 1635 |
// 1. Scan .map file, count number of variants and check whether nonzero CM |
| 1636 |
// values exist. |
| 1637 |
// 2. Convert .ped to temporary sample-major .bed file, using the variant |
| 1638 |
// count to disambiguate the compound-genotypes case from the regular |
| 1639 |
// case. Write temporary .fam file as well, to be reread with same |
| 1640 |
// fam_cols setting. |
| 1641 |
// 3. RewritePsam() call. |
| 1642 |
// 4. Now that we know the alleles and their counts, populate allele_flips. |
| 1643 |
// 5. Rescan .map file, write .pvar; then free allele storage space. |
| 1644 |
// 6. Transpose sample-major .bed to .pgen, while flipping the necessary |
| 1645 |
// variants. |
| 1646 |
// 7. Delete temporary files. |
| 1647 |
FinalizeChrset(misc_flags, cip); |
| 1648 |
uintptr_t* variant_include = nullptr; |
| 1649 |
uint32_t raw_variant_ct; |
| 1650 |
uint32_t variant_ct; |
| 1651 |
uint32_t at_least_one_nzero_cm; |
| 1652 |
reterr = ScanMap(mapname, misc_flags, cip, &raw_variant_ct, &variant_ct, &at_least_one_nzero_cm, &variant_include); |
| 1653 |
if (unlikely(reterr)) { |
| 1654 |
goto PedmapToPgen_ret_1; |
| 1655 |
} |
| 1656 |
if (unlikely(raw_variant_ct > (kMaxLongLine / 4))) { |
| 1657 |
logerrputs("Error: Too many variants for .ped file converter.\n"); |
| 1658 |
goto PedmapToPgen_ret_MALFORMED_INPUT; |
| 1659 |
} |
| 1660 |
if (raw_variant_ct != variant_ct) { |
| 1661 |
logprintfww("--pedmap: %u variant%s in .map file; %u excluded by chromosome filter or negative bp coordinates, %u remaining.\n", raw_variant_ct, (raw_variant_ct == 1)? "" : "s", raw_variant_ct - variant_ct, variant_ct); |
| 1662 |
} else { |
| 1663 |
logprintf("--pedmap: %u variant%s in .map file.\n", variant_ct, (variant_ct == 1)? "" : "s"); |
| 1664 |
} |
| 1665 |
|
| 1666 |
const uint32_t variant_ctl = BitCtToWordCt(variant_ct); |
| 1667 |
const uint32_t variant_ctl2 = NypCtToWordCt(variant_ct); |
| 1668 |
const uint32_t variant_ct4 = NypCtToByteCt(variant_ct); |
| 1669 |
uintptr_t* allele_flips; |
| 1670 |
const char** allele_codes; |
| 1671 |
uint32_t* second_allele_plus_missing_cts; |
| 1672 |
uintptr_t* genovec; |
| 1673 |
if (unlikely(bigstack_calloc_w(variant_ctl, &allele_flips) || |
| 1674 |
bigstack_end_alloc_kcp(2 * variant_ct, &allele_codes) || |
| 1675 |
bigstack_end_calloc_u32(variant_ct, &second_allele_plus_missing_cts) || |
| 1676 |
bigstack_end_alloc_w(variant_ctl2, &genovec))) { |
| 1677 |
goto PedmapToPgen_ret_NOMEM; |
| 1678 |
} |
| 1679 |
const char* null_str = &(g_one_char_strs[0]); |
| 1680 |
for (uint32_t uii = 0; uii != 2 * variant_ct; ++uii) { |
| 1681 |
allele_codes[uii] = null_str; |
| 1682 |
} |
| 1683 |
snprintf(outname_end, kMaxOutfnameExtBlen, ".bed.smaj"); |
| 1684 |
if (unlikely(fopen_checked(outname, FOPEN_WB, &indmaj_bed_file))) { |
| 1685 |
goto PedmapToPgen_ret_OPEN_FAIL; |
| 1686 |
} |
| 1687 |
if (unlikely(!fwrite_unlocked("l\x1b", 3, 1, indmaj_bed_file))) { |
| 1688 |
goto PedmapToPgen_ret_WRITE_FAIL; |
| 1689 |
} |
| 1690 |
snprintf(outname_end, kMaxOutfnameExtBlen, ".fam.tmp"); |
| 1691 |
if (unlikely(fopen_checked(outname, FOPEN_WB, &tmp_fam_file))) { |
| 1692 |
goto PedmapToPgen_ret_OPEN_FAIL; |
| 1693 |
} |
| 1694 |
char* fam_writebuf = g_textbuf; |
| 1695 |
char* fam_writebuf_flush = &(fam_writebuf[kMaxMediumLine]); |
| 1696 |
char* fam_write_iter = fam_writebuf; |
| 1697 |
const uint32_t decompress_thread_ct = MAXV(1, max_thread_ct - 1); |
| 1698 |
reterr = SizeAndInitTextStream(pedname, bigstack_left() / 4, decompress_thread_ct, &ped_txs); |
| 1699 |
if (unlikely(reterr)) { |
| 1700 |
goto PedmapToPgen_ret_TSTREAM_FAIL; |
| 1701 |
} |
| 1702 |
unsigned char* tmp_alloc_base = g_bigstack_base; |
| 1703 |
unsigned char* tmp_alloc_end = g_bigstack_end; |
| 1704 |
const uint32_t fam_col_ct_m1 = ((fam_cols / kfFamCol1) & 1) + ((fam_cols / (kfFamCol34 / 2)) & 2) + ((fam_cols / kfFamCol5) & 1) + ((fam_cols / kfFamCol6) & 1); |
| 1705 |
char* ped_line_start; |
| 1706 |
do { |
| 1707 |
++line_idx; |
| 1708 |
ped_line_start = TextGet(&ped_txs); |
| 1709 |
if (unlikely(!ped_line_start)) { |
| 1710 |
if (TextStreamErrcode2(&ped_txs, &reterr)) { |
| 1711 |
goto PedmapToPgen_ret_TSTREAM_FAIL; |
| 1712 |
} |
| 1713 |
snprintf(g_logbuf, kLogbufSize, "Error: %s is empty.\n", pedname); |
| 1714 |
goto PedmapToPgen_ret_DEGENERATE_DATA; |
| 1715 |
} |
| 1716 |
} while (ped_line_start[0] == '#'); |
| 1717 |
uint32_t compound_genotypes = 0; |
| 1718 |
{ |
| 1719 |
const uint32_t token_ct = CountTokens(ped_line_start); |
| 1720 |
const uint32_t compound_genotypes_token_ct = fam_col_ct_m1 + 1 + raw_variant_ct; |
| 1721 |
if (token_ct == compound_genotypes_token_ct) { |
| 1722 |
compound_genotypes = 1; |
| 1723 |
} else { |
| 1724 |
const uint32_t regular_token_ct = compound_genotypes_token_ct + raw_variant_ct; |
| 1725 |
if (unlikely(token_ct != regular_token_ct)) { |
| 1726 |
logerrprintfww("Error: Unexpected number of columns in .ped file (%u or %u expected).\n", compound_genotypes_token_ct, regular_token_ct); |
| 1727 |
goto PedmapToPgen_ret_INCONSISTENT_INPUT; |
| 1728 |
} |
| 1729 |
} |
| 1730 |
} |
| 1731 |
uint32_t max_allele_slen = 1; |
| 1732 |
uint32_t sample_idx = 0; |
| 1733 |
while (1) { |
| 1734 |
char* fam_last = NextTokenMult0(ped_line_start, fam_col_ct_m1); |
| 1735 |
if (unlikely(!fam_last)) { |
| 1736 |
goto PedmapToPgen_ret_MISSING_TOKENS; |
| 1737 |
} |
| 1738 |
char* fam_end = CurTokenEnd(fam_last); |
| 1739 |
const uintptr_t fam_copy_byte_ct = fam_end - ped_line_start; |
| 1740 |
if (unlikely(fam_copy_byte_ct > kMaxLongLine - strlen(EOLN_STR))) { |
| 1741 |
logerrprintfww("Error: A leading column of line %" PRIuPTR " of %s is too long.\n", line_idx, pedname); |
| 1742 |
goto PedmapToPgen_ret_MALFORMED_INPUT; |
| 1743 |
} |
| 1744 |
fam_write_iter = memcpya(fam_write_iter, ped_line_start, fam_copy_byte_ct); |
| 1745 |
AppendBinaryEoln(&fam_write_iter); |
| 1746 |
if (unlikely(fwrite_ck(fam_writebuf_flush, tmp_fam_file, &fam_write_iter))) { |
| 1747 |
goto PedmapToPgen_ret_WRITE_FAIL; |
| 1748 |
} |
| 1749 |
char* ped_iter = fam_end; |
| 1750 |
uint32_t variant_idx = 0; |
| 1751 |
uint32_t variant_idx_lowbits_x2 = 0; |
| 1752 |
uintptr_t geno_word = 0; |
| 1753 |
if (!compound_genotypes) { |
| 1754 |
for (uint32_t raw_variant_idx = 0; ; ++raw_variant_idx) { |
| 1755 |
char* first_allele_start = FirstNonTspace(ped_iter); |
| 1756 |
char* first_allele_end = FirstSpaceOrEoln(first_allele_start); |
| 1757 |
char* second_allele_start = FirstNonTspace(first_allele_end); |
| 1758 |
if (unlikely(IsSpaceOrEoln(*second_allele_start))) { |
| 1759 |
goto PedmapToPgen_ret_MISSING_TOKENS; |
| 1760 |
} |
| 1761 |
ped_iter = CurTokenEnd(second_allele_start); |
| 1762 |
if (variant_include && (!IsSet(variant_include, raw_variant_idx))) { |
| 1763 |
continue; |
| 1764 |
} |
| 1765 |
const uint32_t first_allele_slen = first_allele_end - first_allele_start; |
| 1766 |
const uint32_t second_allele_slen = ped_iter - second_allele_start; |
| 1767 |
const uint32_t is_het = (first_allele_slen != second_allele_slen) || (!memequal(first_allele_start, second_allele_start, first_allele_slen)); |
| 1768 |
const uint32_t variant_idx_x2 = variant_idx * 2; |
| 1769 |
const uint32_t variant_idx_x2_p1 = variant_idx_x2 + 1; |
| 1770 |
const char* prov_ref_allele = allele_codes[variant_idx_x2]; |
| 1771 |
const char* prov_alt_allele = allele_codes[variant_idx_x2_p1]; |
| 1772 |
// Note that this uses PLINK 1 encoding. |
| 1773 |
uintptr_t cur_geno; |
| 1774 |
if (strequal_unsafe(prov_ref_allele, first_allele_start, first_allele_slen)) { |
| 1775 |
PedmapToPgen_ref_x: |
| 1776 |
cur_geno = 3 - is_het; |
| 1777 |
if (is_het) { |
| 1778 |
if (!strequal_unsafe(prov_alt_allele, second_allele_start, second_allele_slen)) { |
| 1779 |
if (unlikely(prov_alt_allele != null_str)) { |
| 1780 |
if ((second_allele_slen == 1) && ((second_allele_start[0] == '.') || (second_allele_start[0] == input_missing_geno_char))) { |
| 1781 |
goto PedmapToPgen_ret_HALF_MISSING; |
| 1782 |
} |
| 1783 |
goto PedmapToPgen_ret_MULTIALLELIC; |
| 1784 |
} |
| 1785 |
if (second_allele_slen == 1) { |
| 1786 |
allele_codes[variant_idx_x2_p1] = &(g_one_char_strs[ctou32(second_allele_start[0]) * 2]); |
| 1787 |
} else { |
| 1788 |
if (unlikely(StoreStringAtEndK(tmp_alloc_base, second_allele_start, second_allele_slen, &tmp_alloc_end, &(allele_codes[variant_idx_x2_p1])))) { |
| 1789 |
goto PedmapToPgen_ret_NOMEM; |
| 1790 |
} |
| 1791 |
if (second_allele_slen > max_allele_slen) { |
| 1792 |
max_allele_slen = second_allele_slen; |
| 1793 |
} |
| 1794 |
} |
| 1795 |
} |
| 1796 |
} |
| 1797 |
} else if (strequal_unsafe(prov_alt_allele, first_allele_start, first_allele_slen)) { |
| 1798 |
PedmapToPgen_alt_x: |
| 1799 |
cur_geno = is_het * 2; |
| 1800 |
if (is_het) { |
| 1801 |
if (unlikely(!strequal_unsafe(prov_ref_allele, second_allele_start, second_allele_slen))) { |
| 1802 |
if ((second_allele_slen == 1) && ((second_allele_start[0] == '.') || (second_allele_start[0] == input_missing_geno_char))) { |
| 1803 |
goto PedmapToPgen_ret_HALF_MISSING; |
| 1804 |
} |
| 1805 |
goto PedmapToPgen_ret_MULTIALLELIC; |
| 1806 |
} |
| 1807 |
} |
| 1808 |
} else if ((first_allele_slen == 1) && ((first_allele_start[0] == '.') || (first_allele_start[0] == input_missing_geno_char))) { |
| 1809 |
if (unlikely(is_het)) { |
| 1810 |
goto PedmapToPgen_ret_HALF_MISSING; |
| 1811 |
} |
| 1812 |
cur_geno = 1; |
| 1813 |
} else if (prov_ref_allele == null_str) { |
| 1814 |
if (first_allele_slen == 1) { |
| 1815 |
allele_codes[variant_idx_x2] = &(g_one_char_strs[ctou32(first_allele_start[0]) * 2]); |
| 1816 |
} else { |
| 1817 |
if (unlikely(StoreStringAtEndK(tmp_alloc_base, first_allele_start, first_allele_slen, &tmp_alloc_end, &(allele_codes[variant_idx_x2])))) { |
| 1818 |
goto PedmapToPgen_ret_NOMEM; |
| 1819 |
} |
| 1820 |
if (first_allele_slen > max_allele_slen) { |
| 1821 |
max_allele_slen = first_allele_slen; |
| 1822 |
} |
| 1823 |
} |
| 1824 |
goto PedmapToPgen_ref_x; |
| 1825 |
} else if (likely(prov_alt_allele == null_str)) { |
| 1826 |
if (first_allele_slen == 1) { |
| 1827 |
allele_codes[variant_idx_x2_p1] = &(g_one_char_strs[ctou32(first_allele_start[0]) * 2]); |
| 1828 |
} else { |
| 1829 |
if (unlikely(StoreStringAtEndK(tmp_alloc_base, first_allele_start, first_allele_slen, &tmp_alloc_end, &(allele_codes[variant_idx_x2_p1])))) { |
| 1830 |
goto PedmapToPgen_ret_NOMEM; |
| 1831 |
} |
| 1832 |
if (first_allele_slen > max_allele_slen) { |
| 1833 |
max_allele_slen = first_allele_slen; |
| 1834 |
} |
| 1835 |
} |
| 1836 |
goto PedmapToPgen_alt_x; |
| 1837 |
} else { |
| 1838 |
goto PedmapToPgen_ret_MULTIALLELIC; |
| 1839 |
} |
| 1840 |
second_allele_plus_missing_cts[variant_idx] += (4 - cur_geno) >> 1; |
| 1841 |
geno_word |= cur_geno << variant_idx_lowbits_x2; |
| 1842 |
variant_idx_lowbits_x2 += 2; |
| 1843 |
if (variant_idx_lowbits_x2 == kBitsPerWord) { |
| 1844 |
genovec[variant_idx / kBitsPerWordD2] = geno_word; |
| 1845 |
geno_word = 0; |
| 1846 |
variant_idx_lowbits_x2 = 0; |
| 1847 |
} |
| 1848 |
if (++variant_idx == variant_ct) { |
| 1849 |
break; |
| 1850 |
} |
| 1851 |
} |
| 1852 |
} else { |
| 1853 |
for (uint32_t raw_variant_idx = 0; ; ++raw_variant_idx) { |
| 1854 |
char* alleles_start = FirstNonTspace(ped_iter); |
| 1855 |
if (unlikely(IsSpaceOrEoln(*alleles_start))) { |
| 1856 |
goto PedmapToPgen_ret_MISSING_TOKENS; |
| 1857 |
} |
| 1858 |
ped_iter = CurTokenEnd(alleles_start); |
| 1859 |
if (unlikely(ped_iter != &(alleles_start[2]))) { |
| 1860 |
putc_unlocked('\n', stdout); |
| 1861 |
logerrprintfww("Error: --pedmap: .map file and number of tokens in first line of .ped file imply that the latter is in the compound-genotypes format, but line %" PRIuPTR " has a genotype that isn't length-2.\n", line_idx); |
| 1862 |
goto PedmapToPgen_ret_INCONSISTENT_INPUT; |
| 1863 |
} |
| 1864 |
if (variant_include && (!IsSet(variant_include, raw_variant_idx))) { |
| 1865 |
continue; |
| 1866 |
} |
| 1867 |
const char first_allele_char = alleles_start[0]; |
| 1868 |
const char second_allele_char = alleles_start[1]; |
| 1869 |
const uint32_t is_het = (first_allele_char != second_allele_char); |
| 1870 |
const uint32_t variant_idx_x2 = variant_idx * 2; |
| 1871 |
const uint32_t variant_idx_x2_p1 = variant_idx_x2 + 1; |
| 1872 |
const char prov_ref_char = allele_codes[variant_idx_x2][0]; |
| 1873 |
const char prov_alt_char = allele_codes[variant_idx_x2_p1][0]; |
| 1874 |
// Note that this uses PLINK 1 encoding. |
| 1875 |
uintptr_t cur_geno; |
| 1876 |
if (prov_ref_char == first_allele_char) { |
| 1877 |
PedmapToPgen_refchar_x: |
| 1878 |
cur_geno = 3 - is_het; |
| 1879 |
if (is_het) { |
| 1880 |
if (prov_alt_char != second_allele_char) { |
| 1881 |
if (unlikely(prov_alt_char)) { |
| 1882 |
if ((second_allele_char == '.') || (second_allele_char == input_missing_geno_char)) { |
| 1883 |
goto PedmapToPgen_ret_HALF_MISSING; |
| 1884 |
} |
| 1885 |
goto PedmapToPgen_ret_MULTIALLELIC; |
| 1886 |
} |
| 1887 |
allele_codes[variant_idx_x2_p1] = &(g_one_char_strs[ctou32(second_allele_char) * 2]); |
| 1888 |
} |
| 1889 |
} |
| 1890 |
} else if (prov_alt_char == first_allele_char) { |
| 1891 |
PedmapToPgen_altchar_x: |
| 1892 |
cur_geno = is_het * 2; |
| 1893 |
if (is_het) { |
| 1894 |
if (unlikely(prov_ref_char != second_allele_char)) { |
| 1895 |
if ((second_allele_char == '.') || (second_allele_char == input_missing_geno_char)) { |
| 1896 |
goto PedmapToPgen_ret_HALF_MISSING; |
| 1897 |
} |
| 1898 |
goto PedmapToPgen_ret_MULTIALLELIC; |
| 1899 |
} |
| 1900 |
} |
| 1901 |
} else if ((first_allele_char == '.') || (first_allele_char == input_missing_geno_char)) { |
| 1902 |
if (unlikely(is_het)) { |
| 1903 |
goto PedmapToPgen_ret_HALF_MISSING; |
| 1904 |
} |
| 1905 |
cur_geno = 1; |
| 1906 |
} else if (!prov_ref_char) { |
| 1907 |
allele_codes[variant_idx_x2] = &(g_one_char_strs[ctou32(first_allele_char) * 2]); |
| 1908 |
goto PedmapToPgen_refchar_x; |
| 1909 |
} else if (likely(!prov_alt_char)) { |
| 1910 |
allele_codes[variant_idx_x2_p1] = &(g_one_char_strs[ctou32(first_allele_char) * 2]); |
| 1911 |
goto PedmapToPgen_altchar_x; |
| 1912 |
} else { |
| 1913 |
goto PedmapToPgen_ret_MULTIALLELIC; |
| 1914 |
} |
| 1915 |
second_allele_plus_missing_cts[variant_idx] += (4 - cur_geno) >> 1; |
| 1916 |
geno_word |= cur_geno << variant_idx_lowbits_x2; |
| 1917 |
variant_idx_lowbits_x2 += 2; |
| 1918 |
if (variant_idx_lowbits_x2 == kBitsPerWord) { |
| 1919 |
genovec[variant_idx / kBitsPerWordD2] = geno_word; |
| 1920 |
geno_word = 0; |
| 1921 |
variant_idx_lowbits_x2 = 0; |
| 1922 |
} |
| 1923 |
if (++variant_idx == variant_ct) { |
| 1924 |
break; |
| 1925 |
} |
| 1926 |
} |
| 1927 |
} |
| 1928 |
if (variant_idx_lowbits_x2) { |
| 1929 |
genovec[variant_ct / kBitsPerWordD2] = geno_word; |
| 1930 |
} |
| 1931 |
if (unlikely(!fwrite_unlocked(genovec, variant_ct4, 1, indmaj_bed_file))) { |
| 1932 |
goto PedmapToPgen_ret_WRITE_FAIL; |
| 1933 |
} |
| 1934 |
++sample_idx; |
| 1935 |
if ((sample_idx % 100) == 0) { |
| 1936 |
printf("\r--pedmap: %u samples scanned.", sample_idx); |
| 1937 |
fflush(stdout); |
| 1938 |
} |
| 1939 |
do { |
| 1940 |
++line_idx; |
| 1941 |
ped_line_start = TextGet(&ped_txs); |
| 1942 |
if (!ped_line_start) { |
| 1943 |
goto PedmapToPgen_ped_eof; |
| 1944 |
} |
| 1945 |
} while (ped_line_start[0] == '#'); |
| 1946 |
if (unlikely(sample_idx == 0x7fffffff)) { |
| 1947 |
logerrputs("Error: " PROG_NAME_STR " does not support more than 2^31 - 2 samples.\n"); |
| 1948 |
goto PedmapToPgen_ret_MALFORMED_INPUT; |
| 1949 |
} |
| 1950 |
} |
| 1951 |
PedmapToPgen_ped_eof: |
| 1952 |
if (unlikely(TextStreamErrcode2(&ped_txs, &reterr))) { |
| 1953 |
goto PedmapToPgen_ret_TSTREAM_FAIL; |
| 1954 |
} |
| 1955 |
if (unlikely(fclose_flush_null(fam_writebuf_flush, fam_write_iter, &tmp_fam_file) || |
| 1956 |
fclose_null(&indmaj_bed_file))) { |
| 1957 |
goto PedmapToPgen_ret_WRITE_FAIL; |
| 1958 |
} |
| 1959 |
const uint32_t sample_ct = sample_idx; |
| 1960 |
*outname_end = '\0'; |
| 1961 |
putc_unlocked('\r', stdout); |
| 1962 |
logprintfww("--pedmap: %u sample%s present, genotypes extracted to %s.bed.smaj .\n", sample_ct, (sample_ct == 1)? "" : "s", outname); |
| 1963 |
*outname_end = '.'; |
| 1964 |
BigstackEndSet(tmp_alloc_end); |
| 1965 |
|
| 1966 |
reterr = RewritePsam(outname, missing_catname, misc_flags, fam_cols, missing_pheno, psam_01, max_thread_ct, outname, outname_end, nullptr); |
| 1967 |
if (unlikely(reterr)) { |
| 1968 |
goto PedmapToPgen_ret_1; |
| 1969 |
} |
| 1970 |
snprintf(outname_end, kMaxOutfnameExtBlen, ".fam.tmp"); |
| 1971 |
if (unlikely(unlink(outname))) { |
| 1972 |
goto PedmapToPgen_ret_WRITE_FAIL; |
| 1973 |
} |
| 1974 |
|
| 1975 |
for (uint32_t variant_idx = 0; variant_idx != variant_ct; ++variant_idx) { |
| 1976 |
if (second_allele_plus_missing_cts[variant_idx] > sample_ct) { |
| 1977 |
SetBit(variant_idx, allele_flips); |
| 1978 |
const char* tmp_allele_code = allele_codes[variant_idx * 2]; |
| 1979 |
allele_codes[variant_idx * 2] = allele_codes[variant_idx * 2 + 1]; |
| 1980 |
allele_codes[variant_idx * 2 + 1] = tmp_allele_code; |
| 1981 |
} else if (allele_codes[variant_idx * 2 + 1] == null_str) { |
| 1982 |
// Restore standard representation. |
| 1983 |
allele_codes[variant_idx * 2 + 1] = nullptr; |
| 1984 |
if (allele_codes[variant_idx * 2] == null_str) { |
| 1985 |
allele_codes[variant_idx * 2] = nullptr; |
| 1986 |
} |
| 1987 |
} |
| 1988 |
} |
| 1989 |
|
| 1990 |
reterr = MapToPvar(mapname, cip, allele_codes, variant_ct, max_allele_slen, import_flags, at_least_one_nzero_cm, outname, outname_end); |
| 1991 |
if (unlikely(reterr)) { |
| 1992 |
goto PedmapToPgen_ret_1; |
| 1993 |
} |
| 1994 |
BigstackEndReset(bigstack_end_mark); |
| 1995 |
|
| 1996 |
snprintf(outname_end, kMaxOutfnameExtBlen, ".bed.smaj"); |
| 1997 |
if (unlikely(fopen_checked(outname, FOPEN_RB, &indmaj_bed_file))) { |
| 1998 |
goto PedmapToPgen_ret_OPEN_FAIL; |
| 1999 |
} |
| 2000 |
|
| 2001 |
snprintf(outname_end, kMaxOutfnameExtBlen, ".pgen"); |
| 2002 |
reterr = Plink1SampleMajorToPgen(outname, allele_flips, variant_ct, sample_ct, 0, max_thread_ct, indmaj_bed_file); |
| 2003 |
if (unlikely(reterr)) { |
| 2004 |
goto PedmapToPgen_ret_1; |
| 2005 |
} |
| 2006 |
// can't do anything meaningful with an fclose error here |
| 2007 |
fclose(indmaj_bed_file); |
| 2008 |
indmaj_bed_file = nullptr; |
| 2009 |
snprintf(outname_end, kMaxOutfnameExtBlen, ".bed.smaj"); |
| 2010 |
if (unlikely(unlink(outname))) { |
| 2011 |
goto PedmapToPgen_ret_WRITE_FAIL; |
| 2012 |
} |
| 2013 |
|
| 2014 |
char* write_iter = strcpya_k(g_logbuf, "--pedmap: "); |
| 2015 |
const uint32_t outname_base_slen = outname_end - outname; |
| 2016 |
write_iter = memcpya(write_iter, outname, outname_base_slen); |
| 2017 |
write_iter = strcpya_k(write_iter, ".pgen + "); |
| 2018 |
write_iter = memcpya(write_iter, outname, outname_base_slen); |
| 2019 |
write_iter = strcpya_k(write_iter, ".pvar"); |
| 2020 |
if (import_flags & kfImportKeepAutoconvVzs) { |
| 2021 |
write_iter = strcpya_k(write_iter, ".zst"); |
| 2022 |
} |
| 2023 |
write_iter = strcpya_k(write_iter, " + "); |
| 2024 |
write_iter = memcpya(write_iter, outname, outname_base_slen); |
| 2025 |
strcpy_k(write_iter, ".psam written. .bed.smaj and .fam.tmp temporary files deleted.\n"); |
| 2026 |
WordWrapB(0); |
| 2027 |
logputsb(); |
| 2028 |
} |
| 2029 |
while (0) { |
| 2030 |
PedmapToPgen_ret_NOMEM: |
| 2031 |
reterr = kPglRetNomem; |
| 2032 |
break; |
| 2033 |
PedmapToPgen_ret_TSTREAM_FAIL: |
| 2034 |
TextStreamErrPrint(pedname, &ped_txs); |
| 2035 |
break; |
| 2036 |
PedmapToPgen_ret_OPEN_FAIL: |
| 2037 |
reterr = kPglRetOpenFail; |
| 2038 |
break; |
| 2039 |
PedmapToPgen_ret_WRITE_FAIL: |
| 2040 |
reterr = kPglRetWriteFail; |
| 2041 |
break; |
| 2042 |
PedmapToPgen_ret_MISSING_TOKENS: |
| 2043 |
putc_unlocked('\n', stdout); |
| 2044 |
logerrprintf("Error: Line %" PRIuPTR " of .ped file has fewer tokens than expected.\n", line_idx); |
| 2045 |
reterr = kPglRetMalformedInput; |
| 2046 |
break; |
| 2047 |
PedmapToPgen_ret_HALF_MISSING: |
| 2048 |
putc_unlocked('\n', stdout); |
| 2049 |
logerrprintfww("Error: Half-missing genotype on line %" PRIuPTR " of %s.\n", line_idx, pedname); |
| 2050 |
reterr = kPglRetMalformedInput; |
| 2051 |
break; |
| 2052 |
PedmapToPgen_ret_MULTIALLELIC: |
| 2053 |
putc_unlocked('\n', stdout); |
| 2054 |
logerrprintfww("Error: Multiallelic variant in %s. This violates the .ped specification; please reformat the file as e.g. VCF.\n", pedname); |
| 2055 |
reterr = kPglRetMalformedInput; |
| 2056 |
break; |
| 2057 |
PedmapToPgen_ret_MALFORMED_INPUT: |
| 2058 |
putc_unlocked('\n', stdout); |
| 2059 |
reterr = kPglRetMalformedInput; |
| 2060 |
break; |
| 2061 |
PedmapToPgen_ret_INCONSISTENT_INPUT: |
| 2062 |
putc_unlocked('\n', stdout); |
| 2063 |
reterr = kPglRetInconsistentInput; |
| 2064 |
break; |
| 2065 |
PedmapToPgen_ret_DEGENERATE_DATA: |
| 2066 |
reterr = kPglRetDegenerateData; |
| 2067 |
break; |
| 2068 |
} |
| 2069 |
PedmapToPgen_ret_1: |
| 2070 |
CswriteCloseCond(&pvar_css, pvar_cswritep); |
| 2071 |
CleanupTextStream2(pedname, &ped_txs, &reterr); |
| 2072 |
fclose_cond(tmp_fam_file); |
| 2073 |
fclose_cond(indmaj_bed_file); |
| 2074 |
BigstackDoubleReset(bigstack_mark, bigstack_end_mark); |
| 2075 |
return reterr; |
| 2076 |
} |
| 2077 |
|
| 2078 |
#ifdef __cplusplus |
| 2079 |
} // namespace plink2 |
| 2080 |
#endif |