| 1 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 2 |
// Christopher Chang. |
| 3 |
// |
| 4 |
// This library is free software: you can redistribute it and/or modify it |
| 5 |
// under the terms of the GNU Lesser General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3 of the License, or (at your |
| 7 |
// option) any later version. |
| 8 |
// |
| 9 |
// This library 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 Lesser General Public License |
| 12 |
// for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Lesser General Public License |
| 15 |
// along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
#include "pvar_ffi_support.h" |
| 18 |
#include "plink2_htable.h" |
| 19 |
#include "plink2_text.h" |
| 20 |
#include <errno.h> |
| 21 |
|
| 22 |
namespace plink2 { |
| 23 |
|
| 24 |
RefcountedWptr* CreateRefcountedWptr(uintptr_t size) { |
| 25 |
// "- sizeof(uintptr_t) + size * sizeof(uintptr_t)" because p[1] is intended |
| 26 |
// as a flexible array member |
| 27 |
RefcountedWptr* rwp = S_CAST(RefcountedWptr*, malloc(sizeof(RefcountedWptr))); |
| 28 |
if (!rwp) { |
| 29 |
return nullptr; |
| 30 |
} |
| 31 |
rwp->p = S_CAST(uintptr_t*, malloc(size * sizeof(intptr_t))); |
| 32 |
if (!rwp->p) { |
| 33 |
free(rwp); |
| 34 |
return nullptr; |
| 35 |
} |
| 36 |
rwp->ref_ct = 1; |
| 37 |
return rwp; |
| 38 |
} |
| 39 |
|
| 40 |
void CondReleaseRefcountedWptr(RefcountedWptr** rwpp) { |
| 41 |
RefcountedWptr* rwp = *rwpp; |
| 42 |
if (!rwp) { |
| 43 |
return; |
| 44 |
} |
| 45 |
--rwp->ref_ct; |
| 46 |
if (!rwp->ref_ct) { |
| 47 |
free(rwp->p); |
| 48 |
free(rwp); |
| 49 |
} |
| 50 |
*rwpp = nullptr; |
| 51 |
} |
| 52 |
|
| 53 |
void PreinitMinimalPvar(MinimalPvar* mpp) { |
| 54 |
mpp->chr_names = nullptr; |
| 55 |
mpp->chr_idxs = nullptr; |
| 56 |
mpp->variant_bps = nullptr; |
| 57 |
mpp->variant_ids = nullptr; |
| 58 |
mpp->allele_idx_offsetsp = nullptr; |
| 59 |
mpp->chr_ct = 0; |
| 60 |
mpp->variant_ct = 0; |
| 61 |
mpp->max_allele_ct = 0; |
| 62 |
} |
| 63 |
|
| 64 |
// note that \xxx character constants are interpreted in octal. |
| 65 |
// technically no need to represent 0-31, but 64 extra bytes of data is |
| 66 |
// probably cheaper than the code to subtract 32 everywhere. |
| 67 |
const char g_one_char_strs[] = "\0\0\1\0\2\0\3\0\4\0\5\0\6\0\7\0\10\0\11\0\12\0\13\0\14\0\15\0\16\0\17\0\20\0\21\0\22\0\23\0\24\0\25\0\26\0\27\0\30\0\31\0\32\0\33\0\34\0\35\0\36\0\37\0\40\0\41\0\42\0\43\0\44\0\45\0\46\0\47\0\50\0\51\0\52\0\53\0\54\0\55\0\56\0\57\0\60\0\61\0\62\0\63\0\64\0\65\0\66\0\67\0\70\0\71\0\72\0\73\0\74\0\75\0\76\0\77\0\100\0\101\0\102\0\103\0\104\0\105\0\106\0\107\0\110\0\111\0\112\0\113\0\114\0\115\0\116\0\117\0\120\0\121\0\122\0\123\0\124\0\125\0\126\0\127\0\130\0\131\0\132\0\133\0\134\0\135\0\136\0\137\0\140\0\141\0\142\0\143\0\144\0\145\0\146\0\147\0\150\0\151\0\152\0\153\0\154\0\155\0\156\0\157\0\160\0\161\0\162\0\163\0\164\0\165\0\166\0\167\0\170\0\171\0\172\0\173\0\174\0\175\0\176\0\177\0\200\0\201\0\202\0\203\0\204\0\205\0\206\0\207\0\210\0\211\0\212\0\213\0\214\0\215\0\216\0\217\0\220\0\221\0\222\0\223\0\224\0\225\0\226\0\227\0\230\0\231\0\232\0\233\0\234\0\235\0\236\0\237\0\240\0\241\0\242\0\243\0\244\0\245\0\246\0\247\0\250\0\251\0\252\0\253\0\254\0\255\0\256\0\257\0\260\0\261\0\262\0\263\0\264\0\265\0\266\0\267\0\270\0\271\0\272\0\273\0\274\0\275\0\276\0\277\0\300\0\301\0\302\0\303\0\304\0\305\0\306\0\307\0\310\0\311\0\312\0\313\0\314\0\315\0\316\0\317\0\320\0\321\0\322\0\323\0\324\0\325\0\326\0\327\0\330\0\331\0\332\0\333\0\334\0\335\0\336\0\337\0\340\0\341\0\342\0\343\0\344\0\345\0\346\0\347\0\350\0\351\0\352\0\353\0\354\0\355\0\356\0\357\0\360\0\361\0\362\0\363\0\364\0\365\0\366\0\367\0\370\0\371\0\372\0\373\0\374\0\375\0\376\0\377"; |
| 68 |
|
| 69 |
ENUM_U31_DEF_START() |
| 70 |
kPvarColPos = 0, |
| 71 |
kPvarColId, |
| 72 |
kPvarColRef, |
| 73 |
kPvarColAlt, |
| 74 |
kPvarColNull |
| 75 |
ENUM_U31_DEF_END(PvarColidx); |
| 76 |
|
| 77 |
FLAGSET_DEF_START() |
| 78 |
kfPvarColset0, |
| 79 |
kfPvarColsetPos = (1 << kPvarColPos), |
| 80 |
kfPvarColsetId = (1 << kPvarColId), |
| 81 |
kfPvarColsetRef = (1 << kPvarColRef), |
| 82 |
kfPvarColsetAlt = (1 << kPvarColAlt) |
| 83 |
FLAGSET_DEF_END(PvarColFlags); |
| 84 |
|
| 85 |
PglErr LoadMinimalPvarEx(const char* fname, LoadMinimalPvarFlags flags, MinimalPvar* mpp, char* errstr_buf) { |
| 86 |
// Simple, somewhat inefficient two-pass loader. Only looks at |
| 87 |
// CHROM/POS/ID/REF/ALT; the first two are optional. |
| 88 |
const char** chr_names_tmp = nullptr; |
| 89 |
uint32_t* chr_names_htable = nullptr; |
| 90 |
uint32_t chr_ct = 0; |
| 91 |
TextStream txs; |
| 92 |
PreinitTextStream(&txs); |
| 93 |
uintptr_t line_idx = 0; |
| 94 |
PglErr reterr = kPglRetSuccess; |
| 95 |
{ |
| 96 |
if (unlikely(TextStreamOpen(fname, &txs))) { |
| 97 |
goto LoadMinimalPvar_ret_FILE_FAIL; |
| 98 |
} |
| 99 |
const uint32_t chr_needed = ((flags & kfLoadMinimalPvarOmitChrom) == 0); |
| 100 |
const uint32_t pos_needed = ((flags & kfLoadMinimalPvarOmitPos) == 0); |
| 101 |
// [-1] = #CHROM (must be first column) |
| 102 |
// [0] = POS |
| 103 |
// [1] = ID |
| 104 |
// [2] = REF |
| 105 |
// [3] = ALT |
| 106 |
uint32_t col_skips[kPvarColNull]; |
| 107 |
uint32_t col_types[kPvarColNull]; |
| 108 |
uint32_t relevant_postchr_col_ct = 4; |
| 109 |
const char* line_iter; |
| 110 |
while (1) { |
| 111 |
++line_idx; |
| 112 |
line_iter = TextGet(&txs); |
| 113 |
if (unlikely(!line_iter)) { |
| 114 |
snprintf(errstr_buf, kPglErrstrBufBlen, "Error: No variants in %s.\n", fname); |
| 115 |
goto LoadMinimalPvar_ret_MALFORMED_INPUT; |
| 116 |
} |
| 117 |
if (*line_iter != '#') { |
| 118 |
// .bim: |
| 119 |
// 5 columns: #CHROM ID POS ALT REF |
| 120 |
// 6+ columns: #CHROM ID CM POS ALT REF |
| 121 |
const char* fifth_token = NextTokenMult(line_iter, 4); |
| 122 |
if (unlikely(!fifth_token)) { |
| 123 |
snprintf(errstr_buf, kPglErrstrBufBlen, "Error: No variants in %s.\n", fname); |
| 124 |
goto LoadMinimalPvar_ret_MALFORMED_INPUT; |
| 125 |
} |
| 126 |
// bugfix (12 Jan 2025): this was off by 1 |
| 127 |
col_skips[0] = 1; |
| 128 |
col_skips[2] = 1; |
| 129 |
col_skips[3] = 1; |
| 130 |
col_types[0] = kPvarColId; |
| 131 |
col_types[1] = kPvarColPos; |
| 132 |
col_types[2] = kPvarColAlt; |
| 133 |
col_types[3] = kPvarColRef; |
| 134 |
if (!NextToken(fifth_token)) { |
| 135 |
col_skips[1] = 1; |
| 136 |
} else { |
| 137 |
col_skips[1] = 2; |
| 138 |
} |
| 139 |
break; |
| 140 |
} |
| 141 |
if (tokequal_k(line_iter, "#CHROM")) { |
| 142 |
const char* token_end = &(line_iter[6]); |
| 143 |
uint32_t found_header_bitset = 0; |
| 144 |
relevant_postchr_col_ct = 0; |
| 145 |
for (uint32_t col_idx = 1; ; ++col_idx) { |
| 146 |
line_iter = FirstNonTspace(token_end); |
| 147 |
if (IsEolnKns(*line_iter)) { |
| 148 |
break; |
| 149 |
} |
| 150 |
token_end = CurTokenEnd(line_iter); |
| 151 |
const uint32_t token_slen = token_end - line_iter; |
| 152 |
uint32_t cur_col_type; |
| 153 |
if (token_slen <= 3) { |
| 154 |
if (token_slen == 3) { |
| 155 |
if (memequal_sk(line_iter, "POS")) { |
| 156 |
cur_col_type = kPvarColPos; |
| 157 |
} else if (memequal_sk(line_iter, "REF")) { |
| 158 |
cur_col_type = kPvarColRef; |
| 159 |
} else if (memequal_sk(line_iter, "ALT")) { |
| 160 |
cur_col_type = kPvarColAlt; |
| 161 |
} else { |
| 162 |
continue; |
| 163 |
} |
| 164 |
} else if (memequal_sk(line_iter, "ID")) { |
| 165 |
cur_col_type = kPvarColId; |
| 166 |
} else { |
| 167 |
continue; |
| 168 |
} |
| 169 |
} else if (strequal_k(line_iter, "FORMAT", token_slen)) { |
| 170 |
break; |
| 171 |
} else { |
| 172 |
continue; |
| 173 |
} |
| 174 |
const uint32_t cur_col_type_shifted = 1 << cur_col_type; |
| 175 |
if (unlikely(found_header_bitset & cur_col_type_shifted)) { |
| 176 |
// known token, so no overflow danger |
| 177 |
char* write_iter = strcpya_k(errstr_buf, "Error: Duplicate column header '"); |
| 178 |
write_iter = memcpya(write_iter, line_iter, token_slen); |
| 179 |
write_iter = strcpya_k(write_iter, "' on line "); |
| 180 |
write_iter = wtoa(line_idx, write_iter); |
| 181 |
write_iter = strcpya_k(write_iter, " of "); |
| 182 |
write_iter = strcpya(write_iter, fname); |
| 183 |
memcpy_k(write_iter, ".\n\0", 4); |
| 184 |
goto LoadMinimalPvar_ret_MALFORMED_INPUT; |
| 185 |
} |
| 186 |
found_header_bitset |= cur_col_type_shifted; |
| 187 |
col_skips[relevant_postchr_col_ct] = col_idx; |
| 188 |
col_types[relevant_postchr_col_ct++] = cur_col_type; |
| 189 |
} |
| 190 |
uint32_t required_cols = kfPvarColsetId | kfPvarColsetRef | kfPvarColsetAlt; |
| 191 |
if (pos_needed) { |
| 192 |
required_cols |= kfPvarColsetPos; |
| 193 |
} |
| 194 |
if (unlikely((found_header_bitset & required_cols) != required_cols)) { |
| 195 |
// PRIuPTR does not seem to be working as intended on CRAN |
| 196 |
// win-builder, so we can't have nice things. |
| 197 |
// snprintf(errstr_buf, kPglErrstrBufBlen, "Error: Missing column header(s) on line %" PRIuPTR " of %s. (ID, REF, ALT, and usually POS are required.)\n", line_idx, fname); |
| 198 |
char* write_iter = strcpya_k(errstr_buf, "Error: Missing column header(s) on line "); |
| 199 |
write_iter = wtoa(line_idx, write_iter); |
| 200 |
write_iter = strcpya_k(write_iter, " of "); |
| 201 |
write_iter = strcpya(write_iter, fname); |
| 202 |
strcpy_k(write_iter, ". (ID, REF, ALT, and usually POS are required.)\n"); |
| 203 |
goto LoadMinimalPvar_ret_MALFORMED_INPUT; |
| 204 |
} |
| 205 |
for (uint32_t rpc_col_idx = relevant_postchr_col_ct - 1; rpc_col_idx; --rpc_col_idx) { |
| 206 |
col_skips[rpc_col_idx] -= col_skips[rpc_col_idx - 1]; |
| 207 |
} |
| 208 |
// skip this line in main loop |
| 209 |
line_iter = &(TextLineEnd(&txs)[-1]); |
| 210 |
break; |
| 211 |
} |
| 212 |
} |
| 213 |
if (unlikely(TextStreamErrcode(&txs))) { |
| 214 |
goto LoadMinimalPvar_ret_FILE_FAIL; |
| 215 |
} |
| 216 |
|
| 217 |
if (chr_needed) { |
| 218 |
chr_names_tmp = S_CAST(const char**, malloc(kMaxChromosomes * sizeof(intptr_t))); |
| 219 |
chr_names_htable = S_CAST(uint32_t*, malloc(kChrHtableSize * sizeof(int32_t))); |
| 220 |
if (unlikely((!chr_names_tmp) || (!chr_names_htable))) { |
| 221 |
goto LoadMinimalPvar_ret_NOMEM; |
| 222 |
} |
| 223 |
SetAllU32Arr(kChrHtableSize, chr_names_htable); |
| 224 |
} |
| 225 |
uintptr_t variant_ct = 0; |
| 226 |
uintptr_t allele_ct = 0; |
| 227 |
uintptr_t string_byte_ct = 0; |
| 228 |
uint32_t max_extra_alt_ct = 0; |
| 229 |
while (1) { |
| 230 |
if (!IsEolnKns(*line_iter)) { |
| 231 |
if (chr_needed) { |
| 232 |
const char* chr_start = line_iter; |
| 233 |
const char* chr_end = CurTokenEnd(chr_start); |
| 234 |
const uint32_t chr_slen = chr_end - chr_start; |
| 235 |
if (unlikely(chr_slen > kMaxIdSlen)) { |
| 236 |
// snprintf(errstr_buf, kPglErrstrBufBlen, "Error: Line %" PRIuPTR " of %s has an excessively long chromosome/contig name.\n", line_idx, fname); |
| 237 |
char* write_iter = strcpya_k(errstr_buf, "Error: Line "); |
| 238 |
write_iter = wtoa(line_idx, write_iter); |
| 239 |
write_iter = strcpya_k(write_iter, " of "); |
| 240 |
write_iter = strcpya(write_iter, fname); |
| 241 |
strcpy_k(write_iter, " has an excessively long chromosome/contig name.\n"); |
| 242 |
goto LoadMinimalPvar_ret_MALFORMED_INPUT; |
| 243 |
} |
| 244 |
const uint32_t chr_idx = IdHtableFindNnt(chr_start, chr_names_tmp, chr_names_htable, chr_slen, kChrHtableSize); |
| 245 |
if (chr_idx == UINT32_MAX) { |
| 246 |
// update chromosome name list. Wait till next pass to save |
| 247 |
// per-variant info since we haven't allocated chr_idxs yet. |
| 248 |
if (unlikely(chr_ct == kMaxChromosomes)) { |
| 249 |
snprintf(errstr_buf, kPglErrstrBufBlen, "Error: Too many distinct chromosome/contig names in %s (limit %u).\n", fname, kMaxChromosomes); |
| 250 |
goto LoadMinimalPvar_ret_MALFORMED_INPUT; |
| 251 |
} |
| 252 |
// max ~65k, rather than tens or hundreds of millions. So we don't |
| 253 |
// bother optimizing out most of these allocations. (But maybe |
| 254 |
// worth compacting the allocations at the end of the first pass?) |
| 255 |
char* chr_name_copy = S_CAST(char*, malloc(chr_slen + 1)); |
| 256 |
if (unlikely(!chr_name_copy)) { |
| 257 |
goto LoadMinimalPvar_ret_NOMEM; |
| 258 |
} |
| 259 |
memcpyx(chr_name_copy, chr_start, chr_slen, '\0'); |
| 260 |
chr_names_tmp[chr_ct] = chr_name_copy; |
| 261 |
HtableAddNondup(chr_name_copy, chr_slen, kChrHtableSize, chr_ct, chr_names_htable); |
| 262 |
++chr_ct; |
| 263 |
} |
| 264 |
} |
| 265 |
const char* token_ptrs[kPvarColNull]; |
| 266 |
uint32_t token_slens[kPvarColNull]; |
| 267 |
line_iter = TokenLexK(line_iter, col_types, col_skips, relevant_postchr_col_ct, token_ptrs, token_slens); |
| 268 |
if (unlikely(!line_iter)) { |
| 269 |
goto LoadMinimalPvar_ret_MISSING_TOKENS; |
| 270 |
} |
| 271 |
// POS always ignored during this pass |
| 272 |
++variant_ct; |
| 273 |
// could enforce ID length limits |
| 274 |
string_byte_ct += token_slens[kPvarColId]; // +1 added later |
| 275 |
if (token_slens[1] > 1) { |
| 276 |
string_byte_ct += token_slens[kPvarColRef] + 1; |
| 277 |
} |
| 278 |
const uint32_t alt_slen = token_slens[kPvarColAlt]; |
| 279 |
if (alt_slen > 1) { // could also error out on isolated ',' |
| 280 |
const char* alt_start = token_ptrs[kPvarColAlt]; |
| 281 |
const uint32_t extra_alt_ct = CountByte(alt_start, ',', alt_slen); |
| 282 |
if (!extra_alt_ct) { |
| 283 |
string_byte_ct += alt_slen + 1; |
| 284 |
} else { |
| 285 |
if (extra_alt_ct > max_extra_alt_ct) { |
| 286 |
max_extra_alt_ct = extra_alt_ct; |
| 287 |
} |
| 288 |
allele_ct += extra_alt_ct; |
| 289 |
const char* alt_iter = alt_start; |
| 290 |
const char* alt_end = &(alt_start[alt_slen]); |
| 291 |
for (uint32_t alt_idx = 0; alt_idx != extra_alt_ct; ++alt_idx) { |
| 292 |
const char* cur_alt_end = AdvToDelim(alt_iter, ','); |
| 293 |
const uint32_t cur_allele_slen = cur_alt_end - alt_iter; |
| 294 |
if (cur_allele_slen != 1) { |
| 295 |
if (unlikely(!cur_allele_slen)) { |
| 296 |
goto LoadMinimalPvar_ret_EMPTY_ALLELE_CODE; |
| 297 |
} |
| 298 |
string_byte_ct += cur_allele_slen + 1; |
| 299 |
} |
| 300 |
alt_iter = &(cur_alt_end[1]); |
| 301 |
} |
| 302 |
const uint32_t cur_allele_slen = alt_end - alt_iter; |
| 303 |
if (cur_allele_slen != 1) { |
| 304 |
if (unlikely(!cur_allele_slen)) { |
| 305 |
goto LoadMinimalPvar_ret_EMPTY_ALLELE_CODE; |
| 306 |
} |
| 307 |
string_byte_ct += cur_allele_slen + 1; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
if (TextNextLineLstripK(&txs, &line_iter)) { |
| 313 |
if (unlikely(TextStreamErrcode(&txs))) { |
| 314 |
goto LoadMinimalPvar_ret_FILE_FAIL; |
| 315 |
} |
| 316 |
break; |
| 317 |
} |
| 318 |
++line_idx; |
| 319 |
} |
| 320 |
if (unlikely(!variant_ct)) { |
| 321 |
snprintf(errstr_buf, kPglErrstrBufBlen, "Error: No variants in %s.\n", fname); |
| 322 |
goto LoadMinimalPvar_ret_MALFORMED_INPUT; |
| 323 |
} |
| 324 |
if (unlikely(variant_ct > kPglMaxVariantCt)) { |
| 325 |
// snprintf(errstr_buf, kPglErrstrBufBlen, "Error: Too many variants in %s (%" PRIuPTR "; max 2^31 - 3).\n", fname, variant_ct); |
| 326 |
char* write_iter = strcpya_k(errstr_buf, "Error: Too many variants in "); |
| 327 |
write_iter = strcpya(write_iter, fname); |
| 328 |
write_iter = strcpya_k(write_iter, " ("); |
| 329 |
write_iter = wtoa(variant_ct, write_iter); |
| 330 |
strcpy_k(write_iter, "; max 2^31 - 3).\n"); |
| 331 |
goto LoadMinimalPvar_ret_MALFORMED_INPUT; |
| 332 |
} |
| 333 |
ChrIdx* chr_idxs = nullptr; |
| 334 |
if (chr_needed) { |
| 335 |
assert(!mpp->chr_names); |
| 336 |
mpp->chr_names = S_CAST(const char**, realloc(chr_names_tmp, chr_ct * sizeof(intptr_t))); |
| 337 |
if (unlikely(!mpp->chr_names)) { |
| 338 |
goto LoadMinimalPvar_ret_NOMEM; |
| 339 |
} |
| 340 |
mpp->chr_ct = chr_ct; |
| 341 |
chr_names_tmp = nullptr; |
| 342 |
chr_idxs = S_CAST(ChrIdx*, malloc(variant_ct * sizeof(ChrIdx))); |
| 343 |
if (unlikely(!chr_idxs)) { |
| 344 |
goto LoadMinimalPvar_ret_NOMEM; |
| 345 |
} |
| 346 |
assert(!mpp->chr_idxs); |
| 347 |
mpp->chr_idxs = chr_idxs; |
| 348 |
} |
| 349 |
int32_t* variant_bps; |
| 350 |
if (pos_needed) { |
| 351 |
assert(!mpp->variant_bps); |
| 352 |
variant_bps = S_CAST(int32_t*, malloc(variant_ct * sizeof(int32_t))); |
| 353 |
if (unlikely(!variant_bps)) { |
| 354 |
goto LoadMinimalPvar_ret_NOMEM; |
| 355 |
} |
| 356 |
mpp->variant_bps = variant_bps; |
| 357 |
} |
| 358 |
allele_ct += 2 * variant_ct; |
| 359 |
string_byte_ct += variant_ct; |
| 360 |
const char** variant_ids = S_CAST(const char**, malloc((variant_ct + allele_ct) * sizeof(intptr_t))); |
| 361 |
if (unlikely(!variant_ids)) { |
| 362 |
goto LoadMinimalPvar_ret_NOMEM; |
| 363 |
} |
| 364 |
assert(!mpp->variant_ids); |
| 365 |
mpp->variant_ids = variant_ids; |
| 366 |
char* string_space_iter = S_CAST(char*, malloc(string_byte_ct)); |
| 367 |
if (unlikely(!string_space_iter)) { |
| 368 |
variant_ids[0] = nullptr; |
| 369 |
goto LoadMinimalPvar_ret_NOMEM; |
| 370 |
} |
| 371 |
// ensure this is freed if we trigger one of the next few error conditions |
| 372 |
variant_ids[0] = string_space_iter; |
| 373 |
|
| 374 |
#ifndef NDEBUG |
| 375 |
char* string_space_end = &(string_space_iter[string_byte_ct]); |
| 376 |
#endif |
| 377 |
uintptr_t* allele_idx_offsets = nullptr; |
| 378 |
if (max_extra_alt_ct) { |
| 379 |
if (unlikely(max_extra_alt_ct >= kPglMaxAltAlleleCt)) { |
| 380 |
snprintf(errstr_buf, kPglErrstrBufBlen, "Error: Variant in %s has too many ALT alleles (%u; max " PGL_MAX_ALT_ALLELE_CT_STR ").\n", fname, max_extra_alt_ct + 1); |
| 381 |
reterr = kPglRetNotYetSupported; |
| 382 |
goto LoadMinimalPvar_ret_1; |
| 383 |
} |
| 384 |
mpp->allele_idx_offsetsp = CreateRefcountedWptr(variant_ct + 1); |
| 385 |
if (unlikely(!mpp->allele_idx_offsetsp)) { |
| 386 |
goto LoadMinimalPvar_ret_NOMEM; |
| 387 |
} |
| 388 |
allele_idx_offsets = mpp->allele_idx_offsetsp->p; |
| 389 |
} |
| 390 |
if (unlikely(TextRewind(&txs))) { |
| 391 |
goto LoadMinimalPvar_ret_FILE_FAIL; |
| 392 |
} |
| 393 |
const char** allele_storage = &(variant_ids[variant_ct]); |
| 394 |
mpp->allele_storage = allele_storage; |
| 395 |
mpp->variant_ct = variant_ct; |
| 396 |
mpp->max_allele_ct = max_extra_alt_ct + 2; |
| 397 |
uint32_t variant_idx = 0; |
| 398 |
uintptr_t allele_idx = 0; |
| 399 |
while (!TextNextLineLstripK(&txs, &line_iter)) { |
| 400 |
if (*line_iter == '#') { |
| 401 |
continue; |
| 402 |
} |
| 403 |
if (chr_needed) { |
| 404 |
const char* chr_start = line_iter; |
| 405 |
const char* chr_end = CurTokenEnd(chr_start); |
| 406 |
const uint32_t chr_slen = chr_end - chr_start; |
| 407 |
const uint32_t chr_idx = IdHtableFindNnt(chr_start, mpp->chr_names, chr_names_htable, chr_slen, kChrHtableSize); |
| 408 |
if (unlikely(chr_idx == UINT32_MAX)) { |
| 409 |
// should have been loaded during previous pass |
| 410 |
goto LoadMinimalPvar_ret_READ_FAIL; |
| 411 |
} |
| 412 |
chr_idxs[variant_idx] = chr_idx; |
| 413 |
} |
| 414 |
const char* token_ptrs[kPvarColNull]; |
| 415 |
uint32_t token_slens[kPvarColNull]; |
| 416 |
line_iter = TokenLexK(line_iter, col_types, col_skips, relevant_postchr_col_ct, token_ptrs, token_slens); |
| 417 |
if (unlikely(!line_iter)) { |
| 418 |
// previously validated |
| 419 |
goto LoadMinimalPvar_ret_READ_FAIL; |
| 420 |
} |
| 421 |
if (allele_idx_offsets) { |
| 422 |
allele_idx_offsets[variant_idx] = allele_idx; |
| 423 |
} |
| 424 |
|
| 425 |
if (pos_needed) { |
| 426 |
if (unlikely(ScanIntAbsDefcap(token_ptrs[kPvarColPos], &(variant_bps[variant_idx])))) { |
| 427 |
// snprintf(errstr_buf, kPglErrstrBufBlen, "Error: Invalid POS on line %" PRIuPTR " of %s.\n", line_idx, fname); |
| 428 |
char* write_iter = strcpya_k(errstr_buf, "Error: Invalid POS on line "); |
| 429 |
write_iter = wtoa(line_idx, write_iter); |
| 430 |
write_iter = strcpya_k(write_iter, " of "); |
| 431 |
write_iter = strcpya(write_iter, fname); |
| 432 |
memcpy_k(write_iter, ".\n\0", 4); |
| 433 |
goto LoadMinimalPvar_ret_MALFORMED_INPUT; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
const uint32_t variant_id_slen = token_slens[kPvarColId]; |
| 438 |
memcpyx(string_space_iter, token_ptrs[kPvarColId], variant_id_slen, '\0'); |
| 439 |
variant_ids[variant_idx] = string_space_iter; |
| 440 |
string_space_iter = &(string_space_iter[variant_id_slen + 1]); |
| 441 |
|
| 442 |
const uint32_t ref_slen = token_slens[kPvarColRef]; |
| 443 |
if (ref_slen == 1) { |
| 444 |
// don't bother with missing-code standardization for now |
| 445 |
allele_storage[allele_idx] = &(g_one_char_strs[2 * token_ptrs[kPvarColRef][0]]); |
| 446 |
} else { |
| 447 |
memcpyx(string_space_iter, token_ptrs[kPvarColRef], ref_slen, '\0'); |
| 448 |
allele_storage[allele_idx] = string_space_iter; |
| 449 |
string_space_iter = &(string_space_iter[ref_slen + 1]); |
| 450 |
} |
| 451 |
++allele_idx; |
| 452 |
|
| 453 |
const char* alt_start = token_ptrs[kPvarColAlt]; |
| 454 |
const uint32_t alt_slen = token_slens[kPvarColAlt]; |
| 455 |
if (alt_slen == 1) { |
| 456 |
allele_storage[allele_idx++] = &(g_one_char_strs[2 * alt_start[0]]); |
| 457 |
} else { |
| 458 |
const uint32_t extra_alt_ct = CountByte(alt_start, ',', alt_slen); |
| 459 |
if (!extra_alt_ct) { |
| 460 |
memcpyx(string_space_iter, alt_start, alt_slen, '\0'); |
| 461 |
allele_storage[allele_idx++] = string_space_iter; |
| 462 |
string_space_iter = &(string_space_iter[alt_slen + 1]); |
| 463 |
} else { |
| 464 |
const char* alt_iter = alt_start; |
| 465 |
const char* alt_end = &(alt_start[alt_slen]); |
| 466 |
for (uint32_t alt_idx = 0; alt_idx != extra_alt_ct; ++alt_idx) { |
| 467 |
const char* cur_alt_end = AdvToDelim(alt_iter, ','); |
| 468 |
const uint32_t cur_allele_slen = cur_alt_end - alt_iter; |
| 469 |
if (cur_allele_slen == 1) { |
| 470 |
allele_storage[allele_idx] = &(g_one_char_strs[2 * alt_iter[0]]); |
| 471 |
} else { |
| 472 |
memcpyx(string_space_iter, alt_iter, cur_allele_slen, '\0'); |
| 473 |
allele_storage[allele_idx] = string_space_iter; |
| 474 |
string_space_iter = &(string_space_iter[cur_allele_slen + 1]); |
| 475 |
} |
| 476 |
++allele_idx; |
| 477 |
alt_iter = &(cur_alt_end[1]); |
| 478 |
} |
| 479 |
const uint32_t cur_allele_slen = alt_end - alt_iter; |
| 480 |
if (cur_allele_slen == 1) { |
| 481 |
allele_storage[allele_idx] = &(g_one_char_strs[2 * alt_iter[0]]); |
| 482 |
} else { |
| 483 |
memcpyx(string_space_iter, alt_iter, cur_allele_slen, '\0'); |
| 484 |
allele_storage[allele_idx] = string_space_iter; |
| 485 |
string_space_iter = &(string_space_iter[cur_allele_slen + 1]); |
| 486 |
} |
| 487 |
++allele_idx; |
| 488 |
} |
| 489 |
} |
| 490 |
++variant_idx; |
| 491 |
} |
| 492 |
if (unlikely(TextStreamErrcode(&txs))) { |
| 493 |
goto LoadMinimalPvar_ret_FILE_FAIL; |
| 494 |
} |
| 495 |
if (unlikely(variant_idx != variant_ct)) { |
| 496 |
goto LoadMinimalPvar_ret_READ_FAIL; |
| 497 |
} |
| 498 |
#ifndef NDEBUG |
| 499 |
assert(string_space_iter == string_space_end); |
| 500 |
#endif |
| 501 |
if (allele_idx_offsets) { |
| 502 |
allele_idx_offsets[variant_idx] = allele_idx; |
| 503 |
} |
| 504 |
} |
| 505 |
while (0) { |
| 506 |
LoadMinimalPvar_ret_NOMEM: |
| 507 |
reterr = kPglRetNomem; |
| 508 |
break; |
| 509 |
LoadMinimalPvar_ret_READ_FAIL: |
| 510 |
errno = 0; |
| 511 |
reterr = kPglRetReadFail; |
| 512 |
break; |
| 513 |
LoadMinimalPvar_ret_FILE_FAIL: |
| 514 |
reterr = TextStreamErrcode(&txs); |
| 515 |
assert(reterr != kPglRetSuccess); |
| 516 |
{ |
| 517 |
const char* errmsg = TextStreamError(&txs); |
| 518 |
if (errmsg) { |
| 519 |
memcpy_k(errstr_buf, "Error: ", 7); |
| 520 |
strcpy(&(errstr_buf[7]), errmsg); |
| 521 |
} |
| 522 |
} |
| 523 |
break; |
| 524 |
LoadMinimalPvar_ret_MISSING_TOKENS: |
| 525 |
// snprintf(errstr_buf, kPglErrstrBufBlen, "Error: Fewer tokens than expected on line %" PRIuPTR " of %s.\n", line_idx, fname); |
| 526 |
{ |
| 527 |
char* write_iter = strcpya_k(errstr_buf, "Error: Fewer tokens than expected on line "); |
| 528 |
write_iter = wtoa(line_idx, write_iter); |
| 529 |
write_iter = strcpya_k(write_iter, " of "); |
| 530 |
write_iter = strcpya(write_iter, fname); |
| 531 |
memcpy_k(write_iter, ".\n\0", 4); |
| 532 |
} |
| 533 |
// fallthrough |
| 534 |
LoadMinimalPvar_ret_MALFORMED_INPUT: |
| 535 |
reterr = kPglRetMalformedInput; |
| 536 |
break; |
| 537 |
LoadMinimalPvar_ret_EMPTY_ALLELE_CODE: |
| 538 |
// snprintf(errstr_buf, kPglErrstrBufBlen, "Error: Empty allele code on line %" PRIuPTR " of %s.\n", line_idx, fname); |
| 539 |
{ |
| 540 |
char* write_iter = strcpya_k(errstr_buf, "Error: Empty allele code on line "); |
| 541 |
write_iter = wtoa(line_idx, write_iter); |
| 542 |
write_iter = strcpya_k(write_iter, " of "); |
| 543 |
write_iter = strcpya(write_iter, fname); |
| 544 |
memcpy_k(write_iter, ".\n\0", 4); |
| 545 |
} |
| 546 |
reterr = kPglRetMalformedInput; |
| 547 |
break; |
| 548 |
} |
| 549 |
LoadMinimalPvar_ret_1: |
| 550 |
free_cond(chr_names_htable); |
| 551 |
if (chr_names_tmp) { |
| 552 |
for (uint32_t chr_idx = 0; chr_idx != chr_ct; ++chr_idx) { |
| 553 |
free_const(chr_names_tmp[chr_idx]); |
| 554 |
} |
| 555 |
free(chr_names_tmp); |
| 556 |
} |
| 557 |
CleanupTextStream(&txs, &reterr); |
| 558 |
return reterr; |
| 559 |
} |
| 560 |
|
| 561 |
void CleanupMinimalPvar(MinimalPvar* mpp) { |
| 562 |
if (mpp->variant_ids) { |
| 563 |
if (mpp->variant_ids[0]) { |
| 564 |
free_const(mpp->variant_ids[0]); |
| 565 |
} |
| 566 |
CondReleaseRefcountedWptr(&mpp->allele_idx_offsetsp); |
| 567 |
free(mpp->variant_ids); |
| 568 |
mpp->variant_ids = nullptr; |
| 569 |
} |
| 570 |
if (mpp->chr_names) { |
| 571 |
for (uint32_t chr_idx = 0; chr_idx != mpp->chr_ct; ++chr_idx) { |
| 572 |
free_const(mpp->chr_names[chr_idx]); |
| 573 |
} |
| 574 |
free(mpp->chr_names); |
| 575 |
mpp->chr_names = nullptr; |
| 576 |
} |
| 577 |
if (mpp->chr_idxs) { |
| 578 |
free_const(mpp->chr_idxs); |
| 579 |
mpp->chr_idxs = nullptr; |
| 580 |
} |
| 581 |
if (mpp->variant_bps) { |
| 582 |
free_const(mpp->variant_bps); |
| 583 |
mpp->variant_bps = nullptr; |
| 584 |
} |
| 585 |
mpp->chr_ct = 0; |
| 586 |
mpp->variant_ct = 0; |
| 587 |
mpp->max_allele_ct = 0; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
} // namespace plink2 |