Debian Codegraph @

plink2_psam.cc

raw
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 "plink2_psam.h"
18
19 #ifdef __cplusplus
20 namespace plink2 {
21 #endif
22
23 typedef struct PsamInfoLlStruct {
24 NONCOPYABLE(PsamInfoLlStruct);
25 // vardata[] starts with 8-byte phenotypes (we don't want to parse the same
26 // same numeric string twice), followed by NON-null-terminated sample_id, and
27 // then non-terminated paternal and maternal IDs.
28 struct PsamInfoLlStruct* next;
29 uint32_t sample_id_slen;
30 uint32_t sid_slen;
31 uint32_t paternal_id_slen;
32 uint32_t maternal_id_slen;
33 uint32_t sex_code; // 0 = unknown, 1 = male, 2 = female
34 unsigned char vardata[];
35 } PsamInfoLl;
36
37 typedef struct CatnameLl2Struct {
38 NONCOPYABLE(CatnameLl2Struct);
39 struct CatnameLl2Struct* htable_next;
40 struct CatnameLl2Struct* pheno_next;
41 uint32_t cat_idx; // 0 == "NONE", etc.
42 char str[];
43 } CatnameLl2;
44
45 PglErr LoadPsam(const char* psamname, const RangeList* pheno_range_list_ptr, const char* missing_catname, FamCol fam_cols, uint32_t pheno_ct_max, int32_t missing_pheno, uint32_t affection_01, uint32_t no_categorical, uint32_t neg9_pheno_really_missing, uint32_t max_thread_ct, PedigreeIdInfo* piip, uintptr_t** sample_include_ptr, uintptr_t** founder_info_ptr, uintptr_t** sex_nm_ptr, uintptr_t** sex_male_ptr, PhenoCol** pheno_cols_ptr, char** pheno_names_ptr, uint32_t* raw_sample_ct_ptr, uint32_t* pheno_ct_ptr, uintptr_t* max_pheno_name_blen_ptr) {
46 // outparameter pointers assumed to be initialized to nullptr
47 //
48 // pheno_ct_max should default to something like 0x7fffffff, not UINT32_MAX
49 //
50 // FidPresent flag and max_{sample,sid,paternal,maternal}_id_blen are in/out,
51 // for interoperation with --update-ids and --update-parents.
52 //
53 // permanent allocations are at stack end, not base, to work better with
54 // VariantIdDupflagHtableFind()
55
56 unsigned char* bigstack_mark = g_bigstack_base;
57 unsigned char* bigstack_end_mark = g_bigstack_end;
58
59 PhenoCol* pheno_cols = nullptr;
60 uintptr_t line_idx = 0;
61 uint32_t pheno_ct = 0;
62 PglErr reterr = kPglRetSuccess;
63 TextStream psam_txs;
64 PreinitTextStream(&psam_txs);
65 {
66 reterr = SizeAndInitTextStream(psamname, bigstack_left() / 4, MAXV(max_thread_ct - 1, 1), &psam_txs);
67 if (unlikely(reterr)) {
68 goto LoadPsam_ret_TSTREAM_FAIL;
69 }
70 const char* line_iter;
71 do {
72 ++line_idx;
73 line_iter = TextGet(&psam_txs);
74 if (unlikely(!line_iter)) {
75 if (!TextStreamErrcode2(&psam_txs, &reterr)) {
76 logerrprintfww("Error: No samples in %s.\n", psamname);
77 goto LoadPsam_ret_MALFORMED_INPUT;
78 }
79 goto LoadPsam_ret_TSTREAM_FAIL;
80 }
81 } while ((line_iter[0] == '#') && (!tokequal_k(&(line_iter[1]), "FID")) && (!tokequal_k(&(line_iter[1]), "IID")));
82 const uint32_t pheno_name_subset = pheno_range_list_ptr && pheno_range_list_ptr->names;
83 uint32_t* col_skips = nullptr;
84 uint32_t* col_types = nullptr;
85 uint32_t psam_cols_mask = 0;
86 LlStr* pheno_names_reverse_ll = nullptr;
87 uintptr_t max_pheno_name_blen = 0;
88 g_bigstack_end -= kMaxIdSlen;
89 unsigned char* tmp_bigstack_end = BigstackEndRoundedDown();
90 unsigned char* bigstack_mark2;
91 uint32_t fid_present = 1;
92 uint32_t relevant_postfid_col_ct;
93 if (line_iter[0] == '#') {
94 // parse header
95 // [-1] = #FID (if present, must be first column)
96 // [0] = IID (could also be first column)
97 // [1] = SID (if present, must immediately follow IID)
98 // [2] = PAT
99 // [3] = MAT
100 // [4] = SEX
101 // [5+] = phenotypes
102 relevant_postfid_col_ct = CountTokens(line_iter);
103 if (relevant_postfid_col_ct > pheno_ct_max + 5) {
104 relevant_postfid_col_ct = pheno_ct_max + 5;
105 }
106 if (unlikely(bigstack_alloc_u32(relevant_postfid_col_ct, &col_skips) ||
107 bigstack_alloc_u32(relevant_postfid_col_ct, &col_types))) {
108 goto LoadPsam_ret_NOMEM;
109 }
110 bigstack_mark2 = g_bigstack_base;
111 uint32_t rpf_col_idx = 0;
112 if (line_iter[1] == 'I') {
113 col_skips[0] = 0;
114 col_types[0] = 0;
115 ++rpf_col_idx;
116 psam_cols_mask = 1;
117 fid_present = 0;
118 }
119 uint32_t in_interval = 0;
120 char* cmdline_pheno_sorted_ids = nullptr;
121 uint32_t* cmdline_pheno_id_map = nullptr;
122 uintptr_t max_cmdline_pheno_id_blen = 0;
123 uintptr_t cmdline_pheno_name_ct = 0;
124 if (pheno_name_subset) {
125 max_cmdline_pheno_id_blen = pheno_range_list_ptr->name_max_blen;
126 cmdline_pheno_name_ct = pheno_range_list_ptr->name_ct;
127 uintptr_t* dummy_bitarr;
128 // don't bother freeing these before LoadPsam() is done
129 if (unlikely(bigstack_alloc_c(cmdline_pheno_name_ct * max_cmdline_pheno_id_blen, &cmdline_pheno_sorted_ids) ||
130 bigstack_alloc_u32(cmdline_pheno_name_ct, &cmdline_pheno_id_map) ||
131 bigstack_alloc_w(BitCtToWordCt(cmdline_pheno_name_ct), &dummy_bitarr))) {
132 goto LoadPsam_ret_NOMEM;
133 }
134 SetAllBits(cmdline_pheno_name_ct, dummy_bitarr);
135 reterr = CopySortStrboxSubsetNoalloc(dummy_bitarr, pheno_range_list_ptr->names, cmdline_pheno_name_ct, max_cmdline_pheno_id_blen, 0, 0, cmdline_pheno_sorted_ids, cmdline_pheno_id_map);
136 if (unlikely(reterr)) {
137 goto LoadPsam_ret_1;
138 }
139 char* dup_id = FindSortedStrboxDuplicate(cmdline_pheno_sorted_ids, cmdline_pheno_name_ct, max_cmdline_pheno_id_blen);
140 if (unlikely(dup_id)) {
141 snprintf(g_logbuf, kLogbufSize, "Error: Duplicate phentoype ID '%s'.\n", dup_id);
142 goto LoadPsam_ret_MALFORMED_INPUT_WW;
143 }
144 BigstackReset(dummy_bitarr);
145 }
146 const char* token_end = &(line_iter[4]);
147 unsigned char* ll_alloc_base = g_bigstack_base;
148 const char* linebuf_iter;
149 for (uint32_t col_idx = 1; ; ++col_idx) {
150 linebuf_iter = FirstNonTspace(token_end);
151 if (IsEolnKns(*linebuf_iter)) {
152 break;
153 }
154 token_end = CurTokenEnd(linebuf_iter);
155 const uint32_t token_slen = token_end - linebuf_iter;
156 if (token_slen == 3) {
157 uint32_t cur_col_type = UINT32_MAX;
158 if (memequal_sk(linebuf_iter, "IID")) {
159 cur_col_type = 0;
160 } else if (memequal_sk(linebuf_iter, "SID")) {
161 cur_col_type = 1;
162 } else if (memequal_sk(linebuf_iter, "PAT")) {
163 cur_col_type = 2;
164 } else if (memequal_sk(linebuf_iter, "MAT")) {
165 cur_col_type = 3;
166 } else if (memequal_sk(linebuf_iter, "SEX")) {
167 cur_col_type = 4;
168 } else if (unlikely(memequal_sk(linebuf_iter, "FID"))) {
169 snprintf(g_logbuf, kLogbufSize, "Error: 'FID' column header on line %" PRIuPTR " of %s is not at the beginning.\n", line_idx, psamname);
170 goto LoadPsam_ret_MALFORMED_INPUT_WW;
171 }
172 if (cur_col_type != UINT32_MAX) {
173 const uint32_t cur_col_type_shifted = 1 << cur_col_type;
174 if (unlikely(psam_cols_mask & cur_col_type_shifted)) {
175 // known token, so no overflow danger
176 char* write_iter = strcpya_k(g_logbuf, "Error: Duplicate column header '");
177 write_iter = memcpya_k(write_iter, linebuf_iter, 4);
178 write_iter = strcpya_k(write_iter, "' on line ");
179 write_iter = wtoa(line_idx, write_iter);
180 write_iter = strcpya_k(write_iter, " of ");
181 write_iter = strcpya(write_iter, psamname);
182 memcpy_k(write_iter, ".\n\0", 4);
183 goto LoadPsam_ret_MALFORMED_INPUT_WW;
184 }
185 psam_cols_mask |= cur_col_type_shifted;
186 col_skips[rpf_col_idx] = col_idx;
187 col_types[rpf_col_idx++] = cur_col_type;
188 continue;
189 }
190 }
191 if (pheno_ct < pheno_ct_max) {
192 if (pheno_name_subset) {
193 uint32_t cmdline_pos;
194 if (!SortedIdboxFind(linebuf_iter, cmdline_pheno_sorted_ids, cmdline_pheno_id_map, token_slen, max_cmdline_pheno_id_blen, cmdline_pheno_name_ct, &cmdline_pos)) {
195 // similar to string_range_list_to_bitarr()
196 if (pheno_range_list_ptr->starts_range[cmdline_pos]) {
197 if (unlikely(in_interval)) {
198 logerrputs("Error: Overlapping --pheno-name ranges.\n");
199 goto LoadPsam_ret_INCONSISTENT_INPUT;
200 }
201 in_interval = 1;
202 } else if (cmdline_pos && pheno_range_list_ptr->starts_range[cmdline_pos - 1]) {
203 if (unlikely(!in_interval)) {
204 snprintf(g_logbuf, kLogbufSize, "Error: --pheno-name range is inconsistent with %s.\n", psamname);
205 goto LoadPsam_ret_INCONSISTENT_INPUT_WW;
206 }
207 in_interval = 0;
208 }
209 } else if (!in_interval) {
210 continue;
211 }
212 }
213 const uint32_t tok_blen = token_slen + 1;
214 LlStr* ll_str_new = R_CAST(LlStr*, ll_alloc_base);
215 // just word-aligned, not cacheline-aligned
216 ll_alloc_base += RoundUpPow2(tok_blen + sizeof(LlStr), kBytesPerWord);
217 if (unlikely(ll_alloc_base > tmp_bigstack_end)) {
218 goto LoadPsam_ret_NOMEM;
219 }
220 ll_str_new->next = pheno_names_reverse_ll;
221 memcpyx(ll_str_new->str, linebuf_iter, token_slen, '\0');
222 if (tok_blen > max_pheno_name_blen) {
223 max_pheno_name_blen = tok_blen;
224 }
225 pheno_names_reverse_ll = ll_str_new;
226 col_skips[rpf_col_idx] = col_idx;
227 col_types[rpf_col_idx++] = pheno_ct + 5;
228 ++pheno_ct;
229 }
230 }
231 if (unlikely(max_pheno_name_blen > kMaxIdBlen)) {
232 logerrputs("Error: Phenotype/covariate names are limited to " MAX_ID_SLEN_STR " characters.\n");
233 goto LoadPsam_ret_MALFORMED_INPUT;
234 }
235 BigstackBaseSet(ll_alloc_base);
236 if (unlikely(!(psam_cols_mask & 1))) {
237 snprintf(g_logbuf, kLogbufSize, "Error: No IID column in %s.\n", psamname);
238 goto LoadPsam_ret_MALFORMED_INPUT_WW;
239 }
240 if (unlikely(col_types[0] != 0)) {
241 snprintf(g_logbuf, kLogbufSize, "Error: IID column is not first or second in %s.\n", psamname);
242 goto LoadPsam_ret_MALFORMED_INPUT_WW;
243 }
244 if (unlikely(in_interval)) {
245 snprintf(g_logbuf, kLogbufSize, "Error: --pheno-name range is inconsistent with %s.\n", psamname);
246 goto LoadPsam_ret_INCONSISTENT_INPUT_WW;
247 }
248 relevant_postfid_col_ct = rpf_col_idx;
249 for (rpf_col_idx = relevant_postfid_col_ct - 1; rpf_col_idx; --rpf_col_idx) {
250 col_skips[rpf_col_idx] -= col_skips[rpf_col_idx - 1];
251 }
252 if (unlikely((psam_cols_mask & 2) && ((col_types[1] != 1) || (col_skips[1] != 1)))) {
253 snprintf(g_logbuf, kLogbufSize, "Error: SID column does not immediately follow IID column in %s.\n", psamname);
254 goto LoadPsam_ret_MALFORMED_INPUT_WW;
255 }
256
257 line_iter = AdvPastDelim(linebuf_iter, '\n');
258 ++line_idx;
259 } else {
260 if (unlikely(pheno_name_subset)) {
261 logerrputs("Error: --pheno-name requires a --pheno or .psam file with a header.\n");
262 goto LoadPsam_ret_INCONSISTENT_INPUT;
263 }
264
265 pheno_ct = (fam_cols & kfFamCol6) && pheno_ct_max;
266 fid_present = (fam_cols / kfFamCol1) & 1;
267 relevant_postfid_col_ct = 1 + ((fam_cols / (kfFamCol34 / 2)) & 2) + ((fam_cols / kfFamCol5) & 1) + pheno_ct;
268 // these small allocations can't fail, since kMaxMediumLine <
269 // linebuf_size <= 1/3 of remaining space
270 col_skips = S_CAST(uint32_t*, bigstack_alloc_raw_rd(relevant_postfid_col_ct * sizeof(int32_t)));
271 col_types = S_CAST(uint32_t*, bigstack_alloc_raw_rd(relevant_postfid_col_ct * sizeof(int32_t)));
272 bigstack_mark2 = g_bigstack_base;
273 col_skips[0] = fid_present;
274 col_types[0] = 0;
275 // psam_cols_mask = 1; // may need this later
276 uint32_t rpf_col_idx = 1;
277 if (fam_cols & kfFamCol34) {
278 col_skips[rpf_col_idx] = 1;
279 col_types[rpf_col_idx++] = 2;
280 col_skips[rpf_col_idx] = 1;
281 col_types[rpf_col_idx++] = 3;
282 psam_cols_mask |= 12;
283 }
284 if (fam_cols & kfFamCol5) {
285 col_skips[rpf_col_idx] = 1;
286 col_types[rpf_col_idx++] = 4;
287 psam_cols_mask |= 0x10;
288 }
289 if (pheno_ct) {
290 col_skips[rpf_col_idx] = 1;
291 // col_types[rpf_col_idx++] = 6;
292 col_types[rpf_col_idx] = 5;
293 LlStr* ll_str_new = S_CAST(LlStr*, bigstack_alloc_raw_rd(7 + sizeof(LlStr)));
294 ll_str_new->next = pheno_names_reverse_ll;
295 strcpy_k(ll_str_new->str, "PHENO1");
296 max_pheno_name_blen = 7;
297 pheno_names_reverse_ll = ll_str_new;
298 }
299 }
300 if (psam_cols_mask & 12) {
301 piip->sii.flags |= kfSampleIdParentsPresent;
302 }
303 if (pheno_ct) {
304 char* pheno_names;
305 if (unlikely(pgl_malloc(pheno_ct * max_pheno_name_blen, &pheno_names))) {
306 goto LoadPsam_ret_NOMEM;
307 }
308 *pheno_names_ptr = pheno_names;
309 for (uint32_t pheno_idx = pheno_ct; pheno_idx; ) {
310 --pheno_idx;
311 strcpy(&(pheno_names[pheno_idx * max_pheno_name_blen]), pheno_names_reverse_ll->str);
312 pheno_names_reverse_ll = pheno_names_reverse_ll->next;
313 }
314 if (pheno_ct > 1) {
315 if (unlikely(pheno_ct > kMaxPhenoCt)) {
316 // yeah, yeah, this will never come up
317 logerrputs("Error: " PROG_NAME_STR " does not support more than " MAX_PHENO_CT_STR " phenotypes.\n");
318 goto LoadPsam_ret_MALFORMED_INPUT;
319 }
320 // verify there are no duplicates
321 uint32_t tmp_htable_size;
322 uint32_t* htable_tmp;
323 if (unlikely(HtableGoodSizeAlloc(pheno_ct, bigstack_left(), &htable_tmp, &tmp_htable_size))) {
324 goto LoadPsam_ret_NOMEM;
325 }
326 const uint32_t duplicate_idx = PopulateStrboxHtable(pheno_names, pheno_ct, max_pheno_name_blen, tmp_htable_size, htable_tmp);
327 if (unlikely(duplicate_idx)) {
328 const char* duplicate_pheno_name = &(pheno_names[duplicate_idx * max_pheno_name_blen]);
329 snprintf(g_logbuf, kLogbufSize, "Error: Duplicate phenotype/covariate name '%s' on line %" PRIuPTR " of %s.\n", duplicate_pheno_name, line_idx, psamname);
330 goto LoadPsam_ret_MALFORMED_INPUT_WW;
331 }
332 }
333 // free pheno_names_reverse_ll
334 BigstackReset(bigstack_mark2);
335 }
336
337 // make sure to error out properly in sample_ct == 0 case
338 PsamInfoLl* psam_info_reverse_ll = nullptr;
339 const uint32_t sids_present = (psam_cols_mask / 2) & 1;
340 const uint32_t paternal_ids_present = psam_cols_mask & 4;
341 const uint32_t maternal_ids_present = psam_cols_mask & 8;
342 const uint32_t sex_present = psam_cols_mask & 0x10;
343 const uint32_t col_type_end = 5 + pheno_ct;
344 const uint32_t pheno_ctl = BitCtToWordCt(pheno_ct);
345 const double missing_phenod = missing_pheno? S_CAST(double, missing_pheno) : HUGE_VAL;
346 const double pheno_ctrld = u31tod(1 - affection_01);
347 const double pheno_cased = pheno_ctrld + 1.0;
348 uintptr_t max_sample_id_blen = piip->sii.max_sample_id_blen;
349 uintptr_t max_sid_blen = piip->sii.max_sid_blen;
350 uintptr_t max_paternal_id_blen = piip->parental_id_info.max_paternal_id_blen;
351 uintptr_t max_maternal_id_blen = piip->parental_id_info.max_maternal_id_blen;
352 uint32_t raw_sample_ct = 0;
353 uint32_t categorical_pheno_ct = 0;
354
355 const char** token_ptrs;
356 uint32_t* token_slens;
357 uintptr_t* categorical_phenos;
358 uintptr_t* quantitative_phenos;
359 if (unlikely(bigstack_alloc_kcp(col_type_end, &token_ptrs) ||
360 bigstack_alloc_u32(col_type_end, &token_slens) ||
361 bigstack_calloc_w(pheno_ctl, &categorical_phenos) ||
362 bigstack_calloc_w(pheno_ctl, &quantitative_phenos))) {
363 goto LoadPsam_ret_NOMEM;
364 }
365 const uint32_t missing_catname_blen = strlen(missing_catname) + 1;
366 const uint32_t missing_catname_hval = Hashceil(missing_catname, missing_catname_blen - 1, kCatHtableSize);
367 unsigned char* tmp_bigstack_base = g_bigstack_base;
368 CatnameLl2** catname_htable = nullptr;
369 CatnameLl2** pheno_catname_last = nullptr;
370 uintptr_t* total_catname_blens = nullptr;
371 uint32_t fid_slen = 1;
372 for (; TextGetUnsafe2K(&psam_txs, &line_iter); line_iter = AdvPastDelim(line_iter, '\n'), ++line_idx) {
373 if (unlikely(line_iter[0] == '#')) {
374 snprintf(g_logbuf, kLogbufSize, "Error: Line %" PRIuPTR " of %s starts with a '#'. (This is only permitted before the first nonheader line, and if a #FID/IID header line is present it must denote the end of the header block.)\n", line_idx, psamname);
375 goto LoadPsam_ret_MALFORMED_INPUT_WW;
376 }
377 if (unlikely(raw_sample_ct == kPglMaxSampleCt)) {
378 logerrputs("Error: " PROG_NAME_STR " does not support more than 2^31 - 2 samples.\n");
379 goto LoadPsam_ret_MALFORMED_INPUT;
380 }
381 const char* line_start = line_iter;
382 line_iter = TokenLexK0(line_start, col_types, col_skips, relevant_postfid_col_ct, token_ptrs, token_slens);
383 if (unlikely(!line_iter)) {
384 goto LoadPsam_ret_MISSING_TOKENS;
385 }
386 const char* iid_ptr = token_ptrs[0];
387 const uint32_t iid_slen = token_slens[0];
388 if (fid_present) {
389 fid_slen = CurTokenEnd(line_start) - line_start;
390 }
391 const uint32_t sid_slen = sids_present? token_slens[1] : 0;
392 const uint32_t paternal_id_slen = paternal_ids_present? token_slens[2] : 1;
393 const uint32_t maternal_id_slen = maternal_ids_present? token_slens[3] : 1;
394 // phenotypes
395 if (!raw_sample_ct) {
396 for (uint32_t pheno_idx = 0; pheno_idx != pheno_ct; ++pheno_idx) {
397 if (IsCategoricalPhenostrNocsv(token_ptrs[pheno_idx + 5])) {
398 SetBit(pheno_idx, categorical_phenos);
399 }
400 }
401 categorical_pheno_ct = PopcountWords(categorical_phenos, pheno_ctl);
402 if (categorical_pheno_ct) {
403 // initialize hash table
404 const uint32_t cat_ul_byte_ct = categorical_pheno_ct * sizeof(intptr_t);
405 const uint32_t htable_byte_ct = kCatHtableSize * sizeof(uintptr_t);
406 const uintptr_t entry_byte_ct = RoundUpPow2(offsetof(CatnameLl2, str) + missing_catname_blen, sizeof(intptr_t));
407 if (unlikely(S_CAST(uintptr_t, tmp_bigstack_end - tmp_bigstack_base) < htable_byte_ct + categorical_pheno_ct * entry_byte_ct + 2 * cat_ul_byte_ct)) {
408 goto LoadPsam_ret_NOMEM;
409 }
410 pheno_catname_last = R_CAST(CatnameLl2**, tmp_bigstack_base);
411 tmp_bigstack_base += cat_ul_byte_ct;
412 total_catname_blens = R_CAST(uintptr_t*, tmp_bigstack_base);
413 tmp_bigstack_base += cat_ul_byte_ct;
414 ZeroWArr(categorical_pheno_ct, total_catname_blens);
415 catname_htable = R_CAST(CatnameLl2**, tmp_bigstack_base);
416 tmp_bigstack_base += htable_byte_ct;
417 for (uint32_t uii = 0; uii != kCatHtableSize; ++uii) {
418 catname_htable[uii] = nullptr;
419 }
420 uint32_t cur_hval = missing_catname_hval;
421 for (uint32_t cat_pheno_idx = 0; cat_pheno_idx != categorical_pheno_ct; ++cat_pheno_idx) {
422 CatnameLl2* new_entry = R_CAST(CatnameLl2*, tmp_bigstack_base);
423 tmp_bigstack_base += entry_byte_ct;
424 pheno_catname_last[cat_pheno_idx] = new_entry;
425 new_entry->cat_idx = 0;
426 new_entry->htable_next = nullptr;
427 new_entry->pheno_next = nullptr;
428 memcpy(new_entry->str, missing_catname, missing_catname_blen);
429 catname_htable[cur_hval++] = new_entry;
430 if (cur_hval == kCatHtableSize) {
431 cur_hval = 0;
432 }
433 }
434 }
435 }
436 // 1 extra byte for tab between FID and IID; this gets absorbed into
437 // the "+ sizeof(intptr_t)" at the end, since that would normally be
438 // "+ (sizeof(intptr_t) - 1)"
439 // bugfix: pheno_ct * sizeof(intptr_t) -> pheno_ct * 8
440 const uint32_t alloc_byte_ct = sizeof(PsamInfoLl) + sizeof(intptr_t) + RoundDownPow2(fid_slen + iid_slen + sid_slen + paternal_id_slen + maternal_id_slen + pheno_ct * 8, sizeof(intptr_t));
441 PsamInfoLl* new_psam_info = R_CAST(PsamInfoLl*, tmp_bigstack_base);
442 tmp_bigstack_base += alloc_byte_ct;
443 if (unlikely(tmp_bigstack_base > tmp_bigstack_end)) {
444 goto LoadPsam_ret_NOMEM;
445 }
446 new_psam_info->next = psam_info_reverse_ll;
447 char* sample_id_storage = R_CAST(char*, &(new_psam_info->vardata[pheno_ct * 8]));
448 char* ss_iter = sample_id_storage;
449 if (fid_present) {
450 ss_iter = memcpya(ss_iter, line_start, fid_slen);
451 } else {
452 *ss_iter++ = '0';
453 }
454 *ss_iter++ = '\t';
455 psam_info_reverse_ll = new_psam_info;
456 if (unlikely((iid_slen == 1) && (iid_ptr[0] == '0'))) {
457 snprintf(g_logbuf, kLogbufSize, "Error: Invalid IID '0' on line %" PRIuPTR " of %s.\n", line_idx, psamname);
458 goto LoadPsam_ret_MALFORMED_INPUT_WW;
459 }
460 ss_iter = memcpya(ss_iter, iid_ptr, iid_slen);
461 const uint32_t sample_id_slen = ss_iter - sample_id_storage;
462 if (sample_id_slen >= max_sample_id_blen) {
463 max_sample_id_blen = sample_id_slen + 1;
464 }
465 new_psam_info->sample_id_slen = sample_id_slen;
466 if (sids_present) {
467 ss_iter = memcpya(ss_iter, token_ptrs[1], sid_slen);
468 if (sid_slen >= max_sid_blen) {
469 max_sid_blen = sid_slen + 1;
470 }
471 // bugfix (3 Oct 2019): forgot this
472 new_psam_info->sid_slen = sid_slen;
473 }
474 if (paternal_ids_present) {
475 if (paternal_id_slen >= max_paternal_id_blen) {
476 max_paternal_id_blen = paternal_id_slen + 1;
477 }
478 ss_iter = memcpya(ss_iter, token_ptrs[2], paternal_id_slen);
479 } else {
480 *ss_iter++ = '0';
481 }
482 new_psam_info->paternal_id_slen = paternal_id_slen;
483 if (maternal_ids_present) {
484 if (maternal_id_slen >= max_maternal_id_blen) {
485 max_maternal_id_blen = maternal_id_slen + 1;
486 }
487 ss_iter = memcpya(ss_iter, token_ptrs[3], maternal_id_slen);
488 } else {
489 *ss_iter++ = '0';
490 }
491 new_psam_info->maternal_id_slen = maternal_id_slen;
492 uint32_t cur_sex_code = 0;
493 // don't accept "male"/"female", that's overkill
494 if (sex_present && (token_slens[4] == 1)) {
495 cur_sex_code = CharToSex(token_ptrs[4][0]);
496 }
497 new_psam_info->sex_code = cur_sex_code;
498 // phenotypes
499 unsigned char* pheno_data = new_psam_info->vardata;
500 uint32_t cat_pheno_idx = 0;
501 for (uint32_t pheno_idx = 0; pheno_idx != pheno_ct; ++pheno_idx) {
502 const uint32_t col_type_idx = pheno_idx + 5;
503 const char* cur_phenostr = token_ptrs[col_type_idx];
504 double dxx;
505 const char* cur_phenostr_end = ScanadvDouble(cur_phenostr, &dxx);
506 if (!cur_phenostr_end) {
507 // possible todo: defend against out-of-range numbers like 1e1000;
508 // right now they get treated as categorical variables...
509 const uint32_t slen = token_slens[col_type_idx];
510 if (no_categorical || IsNanStr(cur_phenostr, slen)) {
511 dxx = missing_phenod;
512 } else {
513 if (unlikely(!IsSet(categorical_phenos, pheno_idx))) {
514 assert(psam_info_reverse_ll->next);
515 const uint32_t is_second_relevant_line = !(psam_info_reverse_ll->next->next);
516 logerrprintfww("Error: '%s' entry on line %" PRIuPTR " of %s is categorical, while %s not.\n", &((*pheno_names_ptr)[pheno_idx * max_pheno_name_blen]), line_idx, psamname, is_second_relevant_line? "an earlier entry is" : "earlier entries are");
517 goto LoadPsam_ret_INCOMPATIBLE_PHENOSTRS;
518 }
519 if (unlikely(slen > kMaxIdSlen)) {
520 logerrputs("Error: Categorical phenotypes are limited to " MAX_ID_SLEN_STR " characters.\n");
521 goto LoadPsam_ret_MALFORMED_INPUT;
522 }
523 uint32_t hashval = Hashceil(cur_phenostr, slen, kCatHtableSize) + cat_pheno_idx;
524 if (hashval >= kCatHtableSize) {
525 hashval -= kCatHtableSize;
526 }
527 uintptr_t htable_idx = 0;
528 for (CatnameLl2** cur_entry_ptr = &(catname_htable[hashval]); ; ) {
529 CatnameLl2* cur_entry = *cur_entry_ptr;
530 if (!cur_entry) {
531 const uint32_t entry_byte_ct = RoundUpPow2(offsetof(CatnameLl2, str) + slen + 1, sizeof(intptr_t));
532 htable_idx = pheno_catname_last[cat_pheno_idx]->cat_idx + 1;
533 CatnameLl2* new_entry = R_CAST(CatnameLl2*, tmp_bigstack_base);
534 tmp_bigstack_base += entry_byte_ct;
535 if (unlikely(tmp_bigstack_base > tmp_bigstack_end)) {
536 goto LoadPsam_ret_NOMEM;
537 }
538 new_entry->htable_next = nullptr;
539 new_entry->pheno_next = nullptr;
540 pheno_catname_last[cat_pheno_idx]->pheno_next = new_entry;
541 pheno_catname_last[cat_pheno_idx] = new_entry;
542 *cur_entry_ptr = new_entry;
543 new_entry->cat_idx = htable_idx;
544 memcpyx(new_entry->str, cur_phenostr, slen, '\0');
545 total_catname_blens[cat_pheno_idx] += slen + 1;
546 break;
547 }
548 // safe since we guarantee kMaxIdSlen spare bytes at the end
549 // of bigstack
550 if (strequal_unsafe(cur_entry->str, cur_phenostr, slen)) {
551 htable_idx = cur_entry->cat_idx;
552 break;
553 }
554 cur_entry_ptr = &(cur_entry->htable_next);
555 }
556 // don't bother writing top 4 bytes in 32-bit build
557 memcpy(&(pheno_data[pheno_idx * 8]), &htable_idx, sizeof(intptr_t));
558 ++cat_pheno_idx;
559 continue;
560 }
561 } else {
562 if (unlikely(!IsSpaceOrEoln(*cur_phenostr_end))) {
563 cur_phenostr_end = CurTokenEnd(cur_phenostr_end);
564 *K_CAST(char*, cur_phenostr_end) = '\0';
565 snprintf(g_logbuf, kLogbufSize, "Error: Invalid numeric token '%s' on line %" PRIuPTR " of %s.\n", cur_phenostr, line_idx, psamname);
566 goto LoadPsam_ret_MALFORMED_INPUT_WW;
567 }
568 }
569 if (unlikely(IsSet(categorical_phenos, pheno_idx))) {
570 assert(psam_info_reverse_ll->next);
571 const uint32_t is_second_relevant_line = !(psam_info_reverse_ll->next->next);
572 logerrprintfww("Error: '%s' entry on line %" PRIuPTR " of %s is numeric/'NA', while %s categorical.\n", &((*pheno_names_ptr)[pheno_idx * max_pheno_name_blen]), line_idx, psamname, is_second_relevant_line? "an earlier entry is" : "earlier entries are");
573 goto LoadPsam_ret_INCOMPATIBLE_PHENOSTRS;
574 }
575 if (!IsSet(quantitative_phenos, pheno_idx)) {
576 if ((dxx != missing_phenod) && (dxx != pheno_ctrld) && (dxx != pheno_cased) && (dxx != 0.0)) {
577 SetBit(pheno_idx, quantitative_phenos);
578 }
579 }
580 memcpy(&(pheno_data[pheno_idx * 8]), &dxx, sizeof(double));
581 }
582 ++raw_sample_ct;
583 }
584 if (unlikely(TextStreamErrcode2(&psam_txs, &reterr))) {
585 goto LoadPsam_ret_TSTREAM_FAIL;
586 }
587 reterr = kPglRetSuccess;
588 if (unlikely((max_sample_id_blen > 2 * kMaxIdBlen) || (max_paternal_id_blen > kMaxIdBlen) || (max_maternal_id_blen > kMaxIdBlen))) {
589 logerrputs("Error: FIDs and IIDs are limited to " MAX_ID_SLEN_STR " characters.\n");
590 goto LoadPsam_ret_MALFORMED_INPUT;
591 }
592 if (unlikely(max_sid_blen > kMaxIdBlen)) {
593 logerrputs("Error: SIDs are limited to " MAX_ID_SLEN_STR " characters.\n");
594 goto LoadPsam_ret_MALFORMED_INPUT;
595 }
596 if (unlikely(!raw_sample_ct)) {
597 logerrprintfww("Error: No samples in %s.\n", psamname);
598 goto LoadPsam_ret_MALFORMED_INPUT;
599 }
600 BigstackBaseSet(tmp_bigstack_base);
601 const uintptr_t raw_sample_ctl = BitCtToWordCt(raw_sample_ct);
602 uint32_t** old_cat_idxs_to_new = nullptr;
603
604 if (pheno_ct) {
605 if (unlikely(pgl_malloc(pheno_ct * sizeof(PhenoCol), &pheno_cols))) {
606 goto LoadPsam_ret_NOMEM;
607 }
608 if (categorical_pheno_ct) {
609 if (unlikely(bigstack_calloc_u32p(pheno_ct, &old_cat_idxs_to_new))) {
610 goto LoadPsam_ret_NOMEM;
611 }
612 }
613 for (uint32_t pheno_idx = 0; pheno_idx != pheno_ct; ++pheno_idx) {
614 // ensure cleanup works if initialization fails in the middle
615 pheno_cols[pheno_idx].nonmiss = nullptr;
616 }
617 uint32_t cat_pheno_idx = 0;
618 PhenoCol* pheno_cols_iter = pheno_cols;
619 for (uint32_t pheno_idx = 0; pheno_idx != pheno_ct; ++pheno_idx) {
620 const uintptr_t nonmiss_vec_ct = BitCtToVecCt(raw_sample_ct);
621 const uint32_t is_categorical = IsSet(categorical_phenos, pheno_idx);
622 const uint32_t is_qt = IsSet(quantitative_phenos, pheno_idx);
623 uintptr_t data_vec_ct = 0;
624 uintptr_t catname_vec_ct = 0;
625 uintptr_t catname_storage_vec_ct = 0;
626 uint32_t nonnull_catname_ct = 0;
627 if (!is_categorical) {
628 pheno_cols_iter->category_names = nullptr;
629 pheno_cols_iter->type_code = S_CAST(PhenoDtype, is_qt);
630 pheno_cols_iter->nonnull_category_ct = 0;
631 if (is_qt) {
632 data_vec_ct = DblCtToVecCt(raw_sample_ct);
633 } else {
634 data_vec_ct = nonmiss_vec_ct;
635 }
636 } else {
637 nonnull_catname_ct = pheno_catname_last[cat_pheno_idx]->cat_idx;
638 if (unlikely(bigstack_alloc_u32(nonnull_catname_ct + 1, &(old_cat_idxs_to_new[pheno_idx])))) {
639 goto LoadPsam_ret_NOMEM;
640 }
641 data_vec_ct = Int32CtToVecCt(raw_sample_ct);
642 catname_vec_ct = WordCtToVecCt(nonnull_catname_ct + 1);
643 catname_storage_vec_ct = DivUp(total_catname_blens[cat_pheno_idx], kBytesPerVec);
644 pheno_cols_iter->type_code = kPhenoDtypeCat;
645 pheno_cols_iter->nonnull_category_ct = nonnull_catname_ct;
646 }
647 // pheno_cols_iter->nonmiss = nullptr;
648 uintptr_t* new_pheno_data_iter;
649 if (unlikely(vecaligned_malloc((nonmiss_vec_ct + data_vec_ct + catname_vec_ct + catname_storage_vec_ct) * kBytesPerVec, &new_pheno_data_iter))) {
650 goto LoadPsam_ret_NOMEM;
651 }
652 pheno_cols_iter->nonmiss = new_pheno_data_iter;
653 ZeroWArr(nonmiss_vec_ct * kWordsPerVec, new_pheno_data_iter);
654 new_pheno_data_iter = &(new_pheno_data_iter[nonmiss_vec_ct * kWordsPerVec]);
655 if (is_categorical) {
656 pheno_cols_iter->data.cat = R_CAST(uint32_t*, new_pheno_data_iter);
657 new_pheno_data_iter = &(new_pheno_data_iter[data_vec_ct * kWordsPerVec]);
658 const char** cur_name_ptrs = R_CAST(const char**, new_pheno_data_iter);
659 pheno_cols_iter->category_names = cur_name_ptrs;
660 *cur_name_ptrs++ = missing_catname;
661 char* name_storage_iter = R_CAST(char*, &(new_pheno_data_iter[catname_vec_ct * kWordsPerVec]));
662 uint32_t cur_hval = missing_catname_hval + cat_pheno_idx;
663 if (cur_hval >= kCatHtableSize) {
664 cur_hval -= kCatHtableSize;
665 }
666 // make this point to the "NONE" entry for the current phenotype,
667 // which starts the linked list
668 CatnameLl2* catname_entry_ptr = catname_htable[cur_hval];
669
670 for (uint32_t catname_idx = 0; catname_idx != nonnull_catname_ct; ++catname_idx) {
671 catname_entry_ptr = catname_entry_ptr->pheno_next;
672 char* cur_name_start = name_storage_iter;
673 name_storage_iter = strcpyax(name_storage_iter, catname_entry_ptr->str, '\0');
674 *cur_name_ptrs++ = cur_name_start;
675 }
676 if (unlikely(SortStrptrArrIndexed(nonnull_catname_ct + 1, 1, 0, 1, pheno_cols_iter->category_names, nullptr, old_cat_idxs_to_new[pheno_idx]))) {
677 goto LoadPsam_ret_NOMEM;
678 }
679 ++cat_pheno_idx;
680 } else if (!is_qt) {
681 pheno_cols_iter->data.cc = new_pheno_data_iter;
682 ZeroWArr(nonmiss_vec_ct * kWordsPerVec, new_pheno_data_iter);
683 } else {
684 pheno_cols_iter->data.qt = R_CAST(double*, new_pheno_data_iter);
685 }
686 ++pheno_cols_iter;
687 }
688 }
689 // real allocations start here
690 // could make these cacheline-aligned?
691 g_bigstack_end = bigstack_end_mark;
692 const uint32_t aligned_wct = BitCtToAlignedWordCt(raw_sample_ct);
693 if (unlikely(bigstack_end_alloc_c(raw_sample_ct * max_sample_id_blen, &(piip->sii.sample_ids)) ||
694 bigstack_end_alloc_c(raw_sample_ct * max_paternal_id_blen, &(piip->parental_id_info.paternal_ids)) ||
695 bigstack_end_alloc_c(raw_sample_ct * max_maternal_id_blen, &(piip->parental_id_info.maternal_ids)) ||
696 bigstack_end_alloc_w(raw_sample_ctl, sample_include_ptr) ||
697 bigstack_end_calloc_w(aligned_wct, founder_info_ptr) ||
698 bigstack_end_calloc_w(aligned_wct, sex_nm_ptr) ||
699 bigstack_end_calloc_w(aligned_wct, sex_male_ptr))) {
700 goto LoadPsam_ret_NOMEM;
701 }
702 if (max_sid_blen) {
703 if (unlikely(bigstack_end_alloc_c(raw_sample_ct * max_sid_blen, &(piip->sii.sids)))) {
704 goto LoadPsam_ret_NOMEM;
705 }
706 if (!sids_present) {
707 // Possible with --update-ids.
708 char* sids_iter = piip->sii.sids;
709 for (uint32_t sample_idx = 0; sample_idx != raw_sample_ct; ++sample_idx) {
710 strcpy_k(sids_iter, "0");
711 sids_iter = &(sids_iter[max_sid_blen]);
712 }
713 }
714 }
715 bigstack_end_mark = g_bigstack_end;
716 SetAllBits(raw_sample_ct, *sample_include_ptr);
717 // make FillInterleavedMaskVec() work by default
718 ZeroTrailingWords(raw_sample_ctl, *sample_include_ptr);
719 ZeroTrailingWords(raw_sample_ctl, *founder_info_ptr);
720 ZeroTrailingWords(raw_sample_ctl, *sex_male_ptr);
721 *raw_sample_ct_ptr = raw_sample_ct;
722 piip->sii.max_sample_id_blen = max_sample_id_blen;
723 piip->sii.max_sid_blen = max_sid_blen;
724 piip->parental_id_info.max_paternal_id_blen = max_paternal_id_blen;
725 piip->parental_id_info.max_maternal_id_blen = max_maternal_id_blen;
726 *max_pheno_name_blen_ptr = max_pheno_name_blen;
727 char* sample_ids = piip->sii.sample_ids;
728 char* sids = piip->sii.sids;
729 char* paternal_ids = piip->parental_id_info.paternal_ids;
730 char* maternal_ids = piip->parental_id_info.maternal_ids;
731 uintptr_t* founder_info = *founder_info_ptr;
732 uintptr_t* sex_nm = *sex_nm_ptr;
733 uintptr_t* sex_male = *sex_male_ptr;
734 const uint32_t check_neg8_to_10_pheno = (missing_pheno == -9) && (!neg9_pheno_really_missing);
735 uint32_t sample_uidx = raw_sample_ct;
736 uint32_t neg9_seen = 0;
737 uint32_t other_neg8_to_10_seen = 0;
738 while (sample_uidx) {
739 --sample_uidx;
740 unsigned char* cur_vardata = psam_info_reverse_ll->vardata;
741 for (uint32_t pheno_idx = 0; pheno_idx != pheno_ct; ++pheno_idx) {
742 if (IsSet(categorical_phenos, pheno_idx)) {
743 uint32_t cur_cat;
744 memcpy(&cur_cat, &(cur_vardata[pheno_idx * 8]), sizeof(int32_t));
745 pheno_cols[pheno_idx].data.cat[sample_uidx] = old_cat_idxs_to_new[pheno_idx][cur_cat];
746 if (cur_cat) {
747 SetBit(sample_uidx, pheno_cols[pheno_idx].nonmiss);
748 }
749 } else {
750 double dxx;
751 memcpy(&dxx, &(cur_vardata[pheno_idx * 8]), sizeof(double));
752 if (IsSet(quantitative_phenos, pheno_idx)) {
753 if (dxx != missing_phenod) {
754 SetBit(sample_uidx, pheno_cols[pheno_idx].nonmiss);
755 pheno_cols[pheno_idx].data.qt[sample_uidx] = dxx;
756 }
757 if (check_neg8_to_10_pheno) {
758 if ((dxx <= -8.0) && (dxx >= -10.0)) {
759 if (dxx == -9.0) {
760 neg9_seen = 1;
761 } else {
762 other_neg8_to_10_seen = 1;
763 }
764 }
765 }
766 } else {
767 if (dxx == pheno_cased) {
768 SetBit(sample_uidx, pheno_cols[pheno_idx].data.cc);
769 SetBit(sample_uidx, pheno_cols[pheno_idx].nonmiss);
770 } else if (dxx == pheno_ctrld) {
771 SetBit(sample_uidx, pheno_cols[pheno_idx].nonmiss);
772 }
773 }
774 }
775 }
776 const uint32_t sample_id_slen = psam_info_reverse_ll->sample_id_slen;
777 const uint32_t paternal_id_slen = psam_info_reverse_ll->paternal_id_slen;
778 const uint32_t maternal_id_slen = psam_info_reverse_ll->maternal_id_slen;
779 const uint32_t sex_code = psam_info_reverse_ll->sex_code;
780 char* cur_sample_id = R_CAST(char*, &(cur_vardata[pheno_ct * 8]));
781 memcpyx(&(sample_ids[sample_uidx * max_sample_id_blen]), cur_sample_id, sample_id_slen, '\0');
782 char* cur_paternal_id = &(cur_sample_id[sample_id_slen]);
783 if (sids_present) {
784 const uint32_t sid_slen = psam_info_reverse_ll->sid_slen;
785 char* cur_sid = cur_paternal_id;
786 memcpyx(&(sids[sample_uidx * max_sid_blen]), cur_sid, sid_slen, '\0');
787 cur_paternal_id = &(cur_sid[sid_slen]);
788 }
789 memcpyx(&(paternal_ids[sample_uidx * max_paternal_id_blen]), cur_paternal_id, paternal_id_slen, '\0');
790 char* cur_maternal_id = &(cur_paternal_id[paternal_id_slen]);
791 if ((paternal_id_slen == 1) && (maternal_id_slen == 1) && (cur_paternal_id[0] == '0') && (cur_maternal_id[0] == '0')) {
792 SetBit(sample_uidx, founder_info);
793 }
794 memcpyx(&(maternal_ids[sample_uidx * max_maternal_id_blen]), cur_maternal_id, maternal_id_slen, '\0');
795 if (sex_code) {
796 SetBit(sample_uidx, sex_nm);
797 if (sex_code == 1) {
798 SetBit(sample_uidx, sex_male);
799 }
800 }
801 psam_info_reverse_ll = psam_info_reverse_ll->next;
802 }
803 if (check_neg8_to_10_pheno && other_neg8_to_10_seen) {
804 if (unlikely(neg9_seen)) {
805 logerrputs("Error: Distinct phenotype values in [-8, -10] present, including -9, when -9 is\ntreated as missing.\n* Usually, this means -9 should be treated as an ordinary numeric value; use\n--no-input-missing-phenotype to specify this.\n* If -9 really does still mean missing, use --neg9-pheno-really-missing.\n");
806 goto LoadPsam_ret_INCONSISTENT_INPUT;
807 }
808 logerrputs("Warning: Phenotype value in [-8, -9) or (-9, -10] present, when -9 is treated\nas missing. Use --no-input-missing-phenotype to treat -9 as a numeric value\n(missing values can be indicated by 'NA'), or --neg9-pheno-really-missing to\nsuppress this warning.\n");
809 }
810 if (fid_present) {
811 piip->sii.flags |= kfSampleIdFidPresent;
812 }
813 // special case: if there's exactly one phenotype, it has the default name,
814 // and it's all-missing, discard it. This removes forced .fam-derived and
815 // similar phenotype columns, and is unlikely to break anything else.
816 if ((pheno_ct == 1) && (!strcmp(*pheno_names_ptr, "PHENO1")) && (!PopcountWords(pheno_cols[0].nonmiss, raw_sample_ctl))) {
817 free(*pheno_names_ptr);
818 *pheno_names_ptr = nullptr;
819 CleanupPhenoCols(1, pheno_cols);
820 *pheno_cols_ptr = nullptr;
821 *pheno_ct_ptr = 0;
822 } else {
823 *pheno_cols_ptr = pheno_cols;
824 *pheno_ct_ptr = pheno_ct;
825 }
826
827 reterr = CheckXidUniqueness(*sample_include_ptr, &(piip->sii), nullptr, raw_sample_ct);
828 if (unlikely(reterr)) {
829 goto LoadPsam_ret_1;
830 }
831 }
832 while (0) {
833 LoadPsam_ret_NOMEM:
834 reterr = kPglRetNomem;
835 break;
836 LoadPsam_ret_TSTREAM_FAIL:
837 TextStreamErrPrint(psamname, &psam_txs);
838 break;
839 LoadPsam_ret_INCONSISTENT_INPUT_WW:
840 WordWrapB(0);
841 logerrputsb();
842 LoadPsam_ret_INCONSISTENT_INPUT:
843 reterr = kPglRetInconsistentInput;
844 break;
845 LoadPsam_ret_MALFORMED_INPUT_WW:
846 WordWrapB(0);
847 logerrputsb();
848 LoadPsam_ret_MALFORMED_INPUT:
849 reterr = kPglRetMalformedInput;
850 break;
851 LoadPsam_ret_MISSING_TOKENS:
852 logerrprintfww("Error: Line %" PRIuPTR " of %s has fewer tokens than expected.\n", line_idx, psamname);
853 reterr = kPglRetMalformedInput;
854 break;
855 LoadPsam_ret_INCOMPATIBLE_PHENOSTRS:
856 logerrputs("(Unless --no-categorical is specified, case/control and quantitative phenotypes\nmust all be numeric/'NA'.\nCategorical phenotypes cannot be 'NA'--use e.g. 'NONE' to represent missing\ncategorical values instead--or start with a number.)\n");
857 reterr = kPglRetMalformedInput;
858 break;
859 }
860 LoadPsam_ret_1:
861 CleanupTextStream2(psamname, &psam_txs, &reterr);
862 BigstackDoubleReset(bigstack_mark, bigstack_end_mark);
863 if (reterr) {
864 if (*pheno_names_ptr) {
865 free(*pheno_names_ptr);
866 *pheno_names_ptr = nullptr;
867 }
868 CleanupPhenoCols(pheno_ct, pheno_cols);
869 *pheno_ct_ptr = 0;
870 *pheno_cols_ptr = nullptr;
871 }
872 return reterr;
873 }
874
875
876 typedef struct PhenoInfoLlStruct {
877 NONCOPYABLE(PhenoInfoLlStruct);
878 // for categorical phenotypes, phenodata entry should be reinterpreted as
879 // uint32_t
880 struct PhenoInfoLlStruct* next;
881 uint32_t sample_uidx;
882 double phenodata[];
883 } PhenoInfoLl;
884
885 // also for loading covariates. set affection_01 to 2 to prohibit case/control
886 // and make unnamed variables start with "COVAR" instead of "PHENO"
887 PglErr LoadPhenos(const char* pheno_fname, const RangeList* pheno_range_list_ptr, const uintptr_t* sample_include, const SampleIdInfo* siip, const char* missing_catname, uint32_t raw_sample_ct, uint32_t sample_ct, int32_t missing_pheno, uint32_t affection_01, uint32_t no_categorical, uint32_t iid_only, uint32_t numeric_ranges, uint32_t neg9_pheno_really_missing, uint32_t max_thread_ct, PhenoCol** pheno_cols_ptr, char** pheno_names_ptr, uint32_t* pheno_ct_ptr, uintptr_t* max_pheno_name_blen_ptr) {
888 unsigned char* bigstack_mark = g_bigstack_base;
889 unsigned char* bigstack_end_mark = g_bigstack_end;
890 char* pheno_names = nullptr;
891 uintptr_t line_idx = 0;
892 PglErr reterr = kPglRetSuccess;
893 TextStream pheno_txs;
894 PreinitTextStream(&pheno_txs);
895 {
896 if (!sample_ct) {
897 goto LoadPhenos_ret_1;
898 }
899 reterr = SizeAndInitTextStream(pheno_fname, bigstack_left() / 4, MAXV(max_thread_ct - 1, 1), &pheno_txs);
900 if (unlikely(reterr)) {
901 goto LoadPhenos_ret_TSTREAM_FAIL;
902 }
903 // We don't use LoadXidHeader() for now due to lack of comma-delimiter
904 // support.
905 const char* line_iter;
906 while (1) {
907 ++line_idx;
908 line_iter = TextGet(&pheno_txs);
909 if (!line_iter) {
910 if (likely(!TextStreamErrcode2(&pheno_txs, &reterr))) {
911 goto LoadPhenos_ret_1;
912 }
913 goto LoadPhenos_ret_TSTREAM_FAIL;
914 }
915 // Exit on first line with either:
916 // - No leading '#'.
917 // - #FID or #IID first token.
918 if (line_iter[0] != '#') {
919 break;
920 }
921 // For backward compatibility with PLINK 1.x --pheno/--covar, we don't
922 // require the leading '#'.
923 ++line_iter;
924 if ((memequal_sk(line_iter, "FID") || memequal_sk(line_iter, "IID")) && ((ctou32(line_iter[3]) <= 32) || (line_iter[3] == ','))) {
925 break;
926 }
927 }
928 const uint32_t old_pheno_ct = *pheno_ct_ptr;
929 const uintptr_t old_max_pheno_name_blen = *max_pheno_name_blen_ptr;
930 uintptr_t max_pheno_name_blen = old_max_pheno_name_blen;
931 uint32_t comma_delim = 0;
932 uint32_t* col_types = nullptr;
933 uint32_t* col_skips = nullptr;
934 XidMode xid_mode;
935 uint32_t new_pheno_ct;
936 uint32_t final_pheno_ct;
937 uintptr_t final_pheno_names_byte_ct;
938 if ((memequal_sk(line_iter, "FID") || memequal_sk(line_iter, "IID")) && ((ctou32(line_iter[3]) <= 32) || (line_iter[3] == ','))) {
939 // treat this as a header line
940 // autodetect CSV vs. space/tab-delimited
941 // (note that we don't permit CSVs without header lines)
942 xid_mode = (line_iter[0] == 'I')? kfXidModeFlagNeverFid : kfXidModeFidIid;
943 comma_delim = (line_iter[3] == ',');
944 const char* linebuf_iter = FirstNonTspace(&(line_iter[3 + comma_delim]));
945 if (unlikely(IsEolnKns(*linebuf_iter))) {
946 goto LoadPhenos_ret_MISSING_TOKENS;
947 }
948 const char* pheno_prestart;
949 if (xid_mode == kfXidModeFidIid) {
950 if (unlikely(iid_only)) {
951 snprintf(g_logbuf, kLogbufSize, "Error: \"--%s iid-only\" file has a FID column.\n", (affection_01 == 2)? "covar" : "pheno");
952 goto LoadPhenos_ret_INCONSISTENT_INPUT_2;
953 }
954 pheno_prestart = CommaOrTspaceTokenEnd(linebuf_iter, comma_delim);
955 const uintptr_t token_slen = pheno_prestart - linebuf_iter;
956 if (unlikely(!strequal_k(linebuf_iter, "IID", token_slen))) {
957 snprintf(g_logbuf, kLogbufSize, "Error: Second column header in %s must be 'IID'.\n", pheno_fname);
958 goto LoadPhenos_ret_MALFORMED_INPUT_WW;
959 }
960 linebuf_iter = CommaOrTspaceFirstToken(pheno_prestart, comma_delim);
961 } else {
962 pheno_prestart = &(line_iter[3]);
963 }
964 linebuf_iter = CommaOrTspaceFirstToken(pheno_prestart, comma_delim);
965 {
966 const char* token_end = CommaOrTspaceTokenEnd(linebuf_iter, comma_delim);
967 const uintptr_t token_slen = token_end - linebuf_iter;
968 if (strequal_k(linebuf_iter, "SID", token_slen)) {
969 if (siip->sids || (siip->flags & kfSampleIdStrictSid0)) {
970 xid_mode |= kfXidModeFlagSid;
971 } else {
972 xid_mode |= kfXidModeFlagSkipSid;
973 }
974 pheno_prestart = token_end;
975 linebuf_iter = CommaOrTspaceFirstToken(token_end, comma_delim);
976 }
977 }
978 if ((xid_mode & kfXidModeCoreMask) == kfXidModeFlagNeverFid) {
979 xid_mode |= kfXidModeFlagOneCoreTokenOk;
980 }
981 uint32_t pheno_col_ct = 0;
982 const char* pheno_start = linebuf_iter;
983 while (linebuf_iter) {
984 const char* token_end = CommaOrTspaceTokenEnd(linebuf_iter, comma_delim);
985 const uintptr_t token_slen = token_end - linebuf_iter;
986 if (max_pheno_name_blen <= token_slen) {
987 max_pheno_name_blen = token_slen + 1;
988 }
989 ++pheno_col_ct;
990 linebuf_iter = CommaOrTspaceFirstToken(token_end, comma_delim);
991 }
992 if (unlikely(max_pheno_name_blen > kMaxIdBlen)) {
993 logerrputs("Error: Phenotype/covariate names are limited to " MAX_ID_SLEN_STR " characters.\n");
994 goto LoadPhenos_ret_MALFORMED_INPUT;
995 }
996 if (pheno_range_list_ptr->names && pheno_col_ct) {
997 // bugfix (20 Oct 2017): forgot to make error message different in
998 // --covar case
999 uintptr_t* bitarr;
1000 if (numeric_ranges) {
1001 const uint32_t leading_col_ct = GetXidColCt(xid_mode);
1002 if (unlikely(bigstack_calloc_w(BitCtToWordCt(pheno_col_ct), &bitarr))) {
1003 goto LoadPhenos_ret_NOMEM;
1004 }
1005 if (unlikely(NumericRangeListToBitarr(pheno_range_list_ptr, pheno_col_ct, leading_col_ct + 1, 0, bitarr))) {
1006 snprintf(g_logbuf, kLogbufSize, "Error: Invalid --%s-col-nums argument for %s.\n", (affection_01 == 2)? "covar" : "pheno", pheno_fname);
1007 goto LoadPhenos_ret_INCONSISTENT_INPUT_WW;
1008 }
1009 } else {
1010 reterr = StringRangeListToBitarrAlloc(pheno_start, pheno_range_list_ptr, (affection_01 == 2)? "covar-name" : "pheno-name", pheno_fname, pheno_col_ct, 0, comma_delim, &bitarr);
1011 if (unlikely(reterr)) {
1012 goto LoadPhenos_ret_1;
1013 }
1014 }
1015 new_pheno_ct = PopcountWords(bitarr, BitCtToWordCt(pheno_col_ct));
1016 if (unlikely(bigstack_alloc_u32(new_pheno_ct, &col_types) ||
1017 bigstack_alloc_u32(new_pheno_ct, &col_skips))) {
1018 goto LoadPhenos_ret_NOMEM;
1019 }
1020 uintptr_t col_uidx_base = 0;
1021 uintptr_t cur_bits = bitarr[0];
1022 int32_t prev_col_uidx = -1;
1023 for (uint32_t col_idx = 0; col_idx != new_pheno_ct; ++col_idx) {
1024 const uint32_t col_uidx = BitIter1(bitarr, &col_uidx_base, &cur_bits);
1025 col_types[col_idx] = col_idx;
1026 col_skips[col_idx] = col_uidx - prev_col_uidx;
1027 prev_col_uidx = col_uidx;
1028 }
1029 } else {
1030 // usual case, load all phenotypes
1031 new_pheno_ct = pheno_col_ct;
1032 if (unlikely(bigstack_alloc_u32(new_pheno_ct, &col_types) ||
1033 bigstack_alloc_u32(new_pheno_ct, &col_skips))) {
1034 goto LoadPhenos_ret_NOMEM;
1035 }
1036 for (uint32_t col_idx = 0; col_idx != pheno_col_ct; ++col_idx) {
1037 col_types[col_idx] = col_idx;
1038 col_skips[col_idx] = 1;
1039 }
1040 }
1041 final_pheno_ct = new_pheno_ct + old_pheno_ct;
1042 final_pheno_names_byte_ct = final_pheno_ct * max_pheno_name_blen;
1043 if (unlikely(pgl_malloc(final_pheno_names_byte_ct, &pheno_names))) {
1044 goto LoadPhenos_ret_NOMEM;
1045 }
1046 linebuf_iter = pheno_prestart;
1047 char* pheno_names_iter = &(pheno_names[old_pheno_ct * max_pheno_name_blen]);
1048 for (uint32_t new_pheno_idx = 0; new_pheno_idx != new_pheno_ct; ++new_pheno_idx) {
1049 linebuf_iter = CommaOrTspaceNextTokenMult(linebuf_iter, col_skips[new_pheno_idx], comma_delim);
1050 if (unlikely(!linebuf_iter)) {
1051 goto LoadPhenos_ret_MISSING_TOKENS;
1052 }
1053 const char* token_end = CommaOrTspaceTokenEnd(linebuf_iter, comma_delim);
1054 const uint32_t name_slen = token_end - linebuf_iter;
1055 if (unlikely(IsReservedPhenoName(linebuf_iter, name_slen))) {
1056 char* write_iter = strcpya_k(g_logbuf, "Error: '");
1057 // length verified to be <= kMaxIdSlen
1058 write_iter = memcpya(write_iter, linebuf_iter, name_slen);
1059 snprintf(write_iter, kLogbufSize - kMaxIdSlen - 16, "' cannot be used as a phenotype/covariate name.\n");
1060 goto LoadPhenos_ret_MALFORMED_INPUT_2;
1061 }
1062 memcpyx(pheno_names_iter, linebuf_iter, name_slen, '\0');
1063 pheno_names_iter = &(pheno_names_iter[max_pheno_name_blen]);
1064 linebuf_iter = token_end;
1065 }
1066 line_iter = AdvPastDelim(linebuf_iter, '\n');
1067 ++line_idx;
1068 } else {
1069 // no header line
1070 xid_mode = iid_only? kfXidModeIid : kfXidModeFidIid;
1071 // don't support comma delimiter here, since we don't have guaranteed
1072 // leading FID/IID to distinguish it
1073 const uint32_t col_ct = CountTokens(line_iter);
1074 if (unlikely(col_ct < 3 - iid_only)) {
1075 // todo: tolerate col_ct == 2 with --allow-no-phenos
1076 goto LoadPhenos_ret_MISSING_TOKENS;
1077 }
1078 if (pheno_range_list_ptr->names) {
1079 if (unlikely(!numeric_ranges)) {
1080 snprintf(g_logbuf, kLogbufSize, "Error: Header line expected in %s, due to --pheno-name/--covar-name. (This line must start with '#FID', 'FID', '#IID', or 'IID'.)\n", pheno_fname);
1081 goto LoadPhenos_ret_INCONSISTENT_INPUT_WW;
1082 }
1083 const uint32_t bitarr_size = col_ct + iid_only - 2;
1084 uintptr_t* bitarr;
1085 if (unlikely(bigstack_calloc_w(BitCtToWordCt(bitarr_size), &bitarr))) {
1086 goto LoadPhenos_ret_NOMEM;
1087 }
1088 if (unlikely(NumericRangeListToBitarr(pheno_range_list_ptr, bitarr_size, 3 - iid_only, 0, bitarr))) {
1089 snprintf(g_logbuf, kLogbufSize, "Error: Invalid --%s-col-nums argument for %s.\n", (affection_01 == 2)? "covar" : "pheno", pheno_fname);
1090 goto LoadPhenos_ret_INCONSISTENT_INPUT_WW;
1091 }
1092 // this boilerplate may belong in its own function
1093 new_pheno_ct = PopcountWords(bitarr, BitCtToWordCt(col_ct));
1094 if (unlikely(bigstack_alloc_u32(new_pheno_ct, &col_types) ||
1095 bigstack_alloc_u32(new_pheno_ct, &col_skips))) {
1096 goto LoadPhenos_ret_NOMEM;
1097 }
1098 uintptr_t col_uidx_base = 0;
1099 uintptr_t cur_bits = bitarr[0];
1100 int32_t prev_col_uidx = -1;
1101 for (uint32_t col_idx = 0; col_idx != new_pheno_ct; ++col_idx) {
1102 const uint32_t col_uidx = BitIter1(bitarr, &col_uidx_base, &cur_bits);
1103 col_types[col_idx] = col_idx;
1104 col_skips[col_idx] = col_uidx - prev_col_uidx;
1105 prev_col_uidx = col_uidx;
1106 }
1107 } else {
1108 new_pheno_ct = col_ct + iid_only - 2;
1109 // bugfix (11 Mar 2018): forgot to initialize col_types here
1110 if (unlikely(bigstack_alloc_u32(new_pheno_ct, &col_types) ||
1111 bigstack_alloc_u32(new_pheno_ct, &col_skips))) {
1112 goto LoadPhenos_ret_NOMEM;
1113 }
1114 for (uint32_t col_idx = 0; col_idx != new_pheno_ct; ++col_idx) {
1115 col_types[col_idx] = col_idx;
1116 col_skips[col_idx] = 1;
1117 }
1118 }
1119 final_pheno_ct = new_pheno_ct + old_pheno_ct;
1120 // bugfix (29 Mar 2018): don't subtract 1 from final_pheno_ct here since
1121 // names are 1-based
1122 const uintptr_t max_new_name_blen = 6 + UintSlen(final_pheno_ct);
1123 if (max_new_name_blen > max_pheno_name_blen) {
1124 max_pheno_name_blen = max_new_name_blen;
1125 }
1126 final_pheno_names_byte_ct = final_pheno_ct * max_pheno_name_blen;
1127 if (unlikely(pgl_malloc(final_pheno_names_byte_ct, &pheno_names))) {
1128 goto LoadPhenos_ret_NOMEM;
1129 }
1130 const char* default_prefix = (affection_01 == 2)? "COVAR" : "PHENO";
1131 for (uint32_t pheno_idx = old_pheno_ct; pheno_idx != final_pheno_ct; ) {
1132 char* write_iter = memcpya_k(&(pheno_names[pheno_idx * max_pheno_name_blen]), default_prefix, 5);
1133 ++pheno_idx; // 1-based default names, not 0-based
1134 write_iter = u32toa(pheno_idx, write_iter);
1135 *write_iter = '\0';
1136 }
1137 }
1138 if (unlikely(final_pheno_ct > kMaxPhenoCt)) {
1139 // yeah, yeah, this will never come up
1140 logerrputs("Error: " PROG_NAME_STR " does not support more than " MAX_PHENO_CT_STR " phenotypes.\n");
1141 goto LoadPhenos_ret_INCONSISTENT_INPUT;
1142 }
1143 for (uint32_t old_pheno_idx = 0; old_pheno_idx != old_pheno_ct; ++old_pheno_idx) {
1144 strcpy(&(pheno_names[old_pheno_idx * max_pheno_name_blen]), &((*pheno_names_ptr)[old_pheno_idx * old_max_pheno_name_blen]));
1145 }
1146
1147 uint32_t tmp_htable_size;
1148 uint32_t* htable_tmp;
1149 if (unlikely(HtableGoodSizeAlloc(final_pheno_ct, bigstack_left(), &htable_tmp, &tmp_htable_size))) {
1150 goto LoadPhenos_ret_NOMEM;
1151 }
1152 // possible todo: implement something like --pheno-merge, allow conditional
1153 // duplication in that case
1154 const uint32_t duplicate_idx = PopulateStrboxHtable(pheno_names, final_pheno_ct, max_pheno_name_blen, tmp_htable_size, htable_tmp);
1155 if (unlikely(duplicate_idx)) {
1156 const char* duplicate_pheno_name = &(pheno_names[duplicate_idx * max_pheno_name_blen]);
1157 snprintf(g_logbuf, kLogbufSize, "Error: Duplicate phenotype/covariate ID '%s'.\n", duplicate_pheno_name);
1158 goto LoadPhenos_ret_MALFORMED_INPUT_WW;
1159 }
1160 BigstackReset(htable_tmp);
1161
1162 PhenoCol* new_pheno_cols = S_CAST(PhenoCol*, realloc(*pheno_cols_ptr, final_pheno_ct * sizeof(PhenoCol)));
1163 if (unlikely(!new_pheno_cols)) {
1164 goto LoadPhenos_ret_NOMEM;
1165 }
1166 // ensure cleanup works if initialization fails in the middle
1167 for (uint32_t pheno_idx = old_pheno_ct; pheno_idx != final_pheno_ct; ++pheno_idx) {
1168 new_pheno_cols[pheno_idx].nonmiss = nullptr;
1169 }
1170 *pheno_ct_ptr = final_pheno_ct;
1171 *pheno_cols_ptr = new_pheno_cols;
1172
1173 char* sorted_xidbox;
1174 uint32_t* xid_map;
1175 uintptr_t max_xid_blen;
1176 reterr = SortedXidboxInitAlloc(sample_include, siip, sample_ct, xid_mode, 0, &sorted_xidbox, &xid_map, &max_xid_blen);
1177 if (unlikely(reterr)) {
1178 goto LoadPhenos_ret_1;
1179 }
1180 const uintptr_t raw_sample_ctl = BitCtToWordCt(raw_sample_ct);
1181 char* id_buf;
1182 uintptr_t* already_seen;
1183 if (unlikely(bigstack_alloc_c(max_xid_blen, &id_buf) ||
1184 bigstack_calloc_w(raw_sample_ctl, &already_seen))) {
1185 goto LoadPhenos_ret_NOMEM;
1186 }
1187
1188 PhenoInfoLl* pheno_info_reverse_ll = nullptr;
1189 const uintptr_t pheno_info_alloc_byte_ct = sizeof(PhenoInfoLl) + new_pheno_ct * sizeof(double);
1190 const uint32_t new_pheno_ctl = BitCtToWordCt(new_pheno_ct);
1191 const double missing_phenod = missing_pheno? S_CAST(double, missing_pheno) : HUGE_VAL;
1192
1193 // affection_01 can be 2, so don't use u31tod()
1194 const double pheno_ctrld = S_CAST(int32_t, 1 - affection_01);
1195 const double pheno_cased = pheno_ctrld + 1.0;
1196 uint32_t categorical_pheno_ct = 0;
1197 const char** token_ptrs;
1198 uint32_t* token_slens;
1199 uintptr_t* categorical_phenos;
1200 uintptr_t* quantitative_phenos;
1201 if (unlikely(bigstack_alloc_kcp(new_pheno_ct, &token_ptrs) ||
1202 bigstack_alloc_u32(new_pheno_ct, &token_slens) ||
1203 bigstack_calloc_w(new_pheno_ctl, &categorical_phenos) ||
1204 bigstack_calloc_w(new_pheno_ctl, &quantitative_phenos))) {
1205 goto LoadPhenos_ret_NOMEM;
1206 }
1207 const uint32_t missing_catname_blen = strlen(missing_catname) + 1;
1208 const uint32_t missing_catname_hval = Hashceil(missing_catname, missing_catname_blen - 1, kCatHtableSize);
1209 unsigned char* bigstack_base_copy = g_bigstack_base;
1210 unsigned char* tmp_bigstack_end = g_bigstack_end;
1211 CatnameLl2** catname_htable = nullptr;
1212 CatnameLl2** pheno_catname_last = nullptr;
1213 uintptr_t* total_catname_blens = nullptr;
1214 for (; TextGetUnsafe2K(&pheno_txs, &line_iter); line_iter = AdvPastDelim(line_iter, '\n'), ++line_idx) {
1215 if (unlikely(line_iter[0] == '#')) {
1216 snprintf(g_logbuf, kLogbufSize, "Error: Line %" PRIuPTR " of %s starts with a '#'. (This is only permitted before the first nonheader line, and if a #FID/IID header line is present it must denote the end of the header block.)\n", line_idx, pheno_fname);
1217 goto LoadPhenos_ret_MALFORMED_INPUT_WW;
1218 }
1219 uint32_t xid_idx_start;
1220 uint32_t xid_idx_end;
1221 if (SortedXidboxReadMultifind(sorted_xidbox, max_xid_blen, sample_ct, comma_delim, xid_mode, &line_iter, &xid_idx_start, &xid_idx_end, id_buf)) {
1222 if (unlikely(!line_iter)) {
1223 goto LoadPhenos_ret_MISSING_TOKENS;
1224 }
1225 continue;
1226 }
1227 if (!comma_delim) {
1228 line_iter = TokenLexK(line_iter, col_types, col_skips, new_pheno_ct, token_ptrs, token_slens);
1229 } else {
1230 line_iter = CsvLexK(line_iter, col_types, col_skips, new_pheno_ct, token_ptrs, token_slens);
1231 }
1232 if (unlikely(!line_iter)) {
1233 goto LoadPhenos_ret_MISSING_TOKENS;
1234 }
1235 if ((!pheno_info_reverse_ll) && (!no_categorical)) {
1236 // first relevant line, detect categorical phenotypes...
1237 // bugfix (18 Apr 2024): ...unless --no-categorical
1238 for (uint32_t new_pheno_idx = 0; new_pheno_idx != new_pheno_ct; ++new_pheno_idx) {
1239 if (IsCategoricalPhenostr(token_ptrs[new_pheno_idx])) {
1240 SetBit(new_pheno_idx, categorical_phenos);
1241 } else if (affection_01 == 2) {
1242 SetBit(new_pheno_idx, quantitative_phenos);
1243 }
1244 }
1245 categorical_pheno_ct = PopcountWords(categorical_phenos, new_pheno_ctl);
1246 if (categorical_pheno_ct) {
1247 // initialize hash table
1248 const uint32_t cat_ul_byte_ct = categorical_pheno_ct * sizeof(intptr_t);
1249 const uint32_t htable_byte_ct = kCatHtableSize * sizeof(uintptr_t);
1250 const uintptr_t entry_byte_ct = RoundUpPow2(offsetof(CatnameLl2, str) + missing_catname_blen, sizeof(intptr_t));
1251
1252 if (unlikely(S_CAST(uintptr_t, tmp_bigstack_end - bigstack_base_copy) < htable_byte_ct + categorical_pheno_ct * entry_byte_ct + 2 * cat_ul_byte_ct)) {
1253 goto LoadPhenos_ret_NOMEM;
1254 }
1255 tmp_bigstack_end -= cat_ul_byte_ct;
1256 total_catname_blens = R_CAST(uintptr_t*, tmp_bigstack_end);
1257 tmp_bigstack_end -= cat_ul_byte_ct;
1258 pheno_catname_last = R_CAST(CatnameLl2**, tmp_bigstack_end);
1259 ZeroWArr(categorical_pheno_ct, total_catname_blens);
1260 tmp_bigstack_end -= htable_byte_ct;
1261 catname_htable = R_CAST(CatnameLl2**, tmp_bigstack_end);
1262 ZeroPtrArr(kCatHtableSize, catname_htable);
1263 uint32_t cur_hval = missing_catname_hval;
1264 for (uint32_t cat_pheno_idx = 0; cat_pheno_idx != categorical_pheno_ct; ++cat_pheno_idx) {
1265 tmp_bigstack_end -= entry_byte_ct;
1266 CatnameLl2* new_entry = R_CAST(CatnameLl2*, tmp_bigstack_end);
1267 pheno_catname_last[cat_pheno_idx] = new_entry;
1268 new_entry->cat_idx = 0;
1269 new_entry->htable_next = nullptr;
1270 new_entry->pheno_next = nullptr;
1271 memcpy(new_entry->str, missing_catname, missing_catname_blen);
1272 catname_htable[cur_hval++] = new_entry;
1273 if (cur_hval == kCatHtableSize) {
1274 cur_hval = 0;
1275 }
1276 }
1277 }
1278 }
1279 const uint32_t first_sample_uidx = xid_map[xid_idx_start];
1280 if (unlikely(IsSet(already_seen, first_sample_uidx))) {
1281 snprintf(g_logbuf, kLogbufSize, "Error: Duplicate sample ID in %s.\n", pheno_fname);
1282 goto LoadPhenos_ret_MALFORMED_INPUT_WW;
1283 }
1284 SetBit(first_sample_uidx, already_seen);
1285 if (unlikely(S_CAST(uintptr_t, tmp_bigstack_end - bigstack_base_copy) < (pheno_info_alloc_byte_ct * (xid_idx_end - xid_idx_start)))) {
1286 goto LoadPhenos_ret_NOMEM;
1287 }
1288 tmp_bigstack_end -= pheno_info_alloc_byte_ct;
1289 PhenoInfoLl* first_pheno_info = R_CAST(PhenoInfoLl*, tmp_bigstack_end);
1290 first_pheno_info->next = pheno_info_reverse_ll;
1291 first_pheno_info->sample_uidx = first_sample_uidx;
1292 double* first_pheno_data = first_pheno_info->phenodata;
1293 uint32_t cat_pheno_idx = 0;
1294 for (uint32_t new_pheno_idx = 0; new_pheno_idx != new_pheno_ct; ++new_pheno_idx) {
1295 const char* cur_phenostr = token_ptrs[new_pheno_idx];
1296 double dxx;
1297 const char* cur_phenostr_end = ScanadvDouble(cur_phenostr, &dxx);
1298 if (!cur_phenostr_end) {
1299 const uint32_t slen = token_slens[new_pheno_idx];
1300 if (no_categorical || IsNanStr(cur_phenostr, slen)) {
1301 // note that, in CSVs, empty string is interpreted as a missing
1302 // non-categorical phenotype; explicit "NONE" is needed to denote
1303 // a missing category
1304 dxx = missing_phenod;
1305 } else {
1306 if (unlikely(!IsSet(categorical_phenos, new_pheno_idx))) {
1307 assert(pheno_info_reverse_ll);
1308 const uint32_t is_second_relevant_line = !(pheno_info_reverse_ll->next);
1309 logerrprintfww("Error: '%s' entry on line %" PRIuPTR " of %s is categorical, while %s not.\n", &(pheno_names[(old_pheno_ct + new_pheno_idx) * max_pheno_name_blen]), line_idx, pheno_fname, is_second_relevant_line? "an earlier entry is" : "earlier entries are");
1310 goto LoadPhenos_ret_INCOMPATIBLE_PHENOSTRS;
1311 }
1312 uint32_t hashval;
1313 hashval = Hashceil(cur_phenostr, slen, kCatHtableSize) + cat_pheno_idx;
1314 if (hashval >= kCatHtableSize) {
1315 hashval -= kCatHtableSize;
1316 }
1317 uintptr_t htable_idx = 0;
1318 CatnameLl2** cur_entry_ptr = &(catname_htable[hashval]);
1319 while (1) {
1320 CatnameLl2* cur_entry = *cur_entry_ptr;
1321 if (!cur_entry) {
1322 const uint32_t entry_byte_ct = RoundUpPow2(offsetof(CatnameLl2, str) + slen + 1, sizeof(intptr_t));
1323 if (unlikely(S_CAST(uintptr_t, tmp_bigstack_end - bigstack_base_copy) < entry_byte_ct)) {
1324 goto LoadPhenos_ret_NOMEM;
1325 }
1326 tmp_bigstack_end -= entry_byte_ct;
1327 htable_idx = pheno_catname_last[cat_pheno_idx]->cat_idx + 1;
1328 CatnameLl2* new_entry = R_CAST(CatnameLl2*, tmp_bigstack_end);
1329 new_entry->htable_next = nullptr;
1330 new_entry->pheno_next = nullptr;
1331 pheno_catname_last[cat_pheno_idx]->pheno_next = new_entry;
1332 pheno_catname_last[cat_pheno_idx] = new_entry;
1333 *cur_entry_ptr = new_entry;
1334 new_entry->cat_idx = htable_idx;
1335 memcpyx(new_entry->str, cur_phenostr, slen, '\0');
1336 total_catname_blens[cat_pheno_idx] += slen + 1;
1337 break;
1338 }
1339 // safe since hash table entries are in the middle of bigstack
1340 if (strequal_unsafe(cur_entry->str, cur_phenostr, slen)) {
1341 htable_idx = cur_entry->cat_idx;
1342 break;
1343 }
1344 cur_entry_ptr = &(cur_entry->htable_next);
1345 }
1346 // don't bother writing top 4 bytes in 32-bit build
1347 memcpy(&(first_pheno_data[new_pheno_idx]), &htable_idx, sizeof(intptr_t));
1348 ++cat_pheno_idx;
1349 continue;
1350 }
1351 } else {
1352 if (unlikely(!IsCommaOrTspaceTokenEnd(*cur_phenostr_end, comma_delim))) {
1353 cur_phenostr_end = CommaOrTspaceTokenEnd(cur_phenostr_end, comma_delim);
1354 *K_CAST(char*, cur_phenostr_end) = '\0';
1355 snprintf(g_logbuf, kLogbufSize, "Error: Invalid numeric token '%s' on line %" PRIuPTR " of %s.\n", cur_phenostr, line_idx, pheno_fname);
1356 goto LoadPhenos_ret_MALFORMED_INPUT_WW;
1357 }
1358 }
1359 if (unlikely(IsSet(categorical_phenos, new_pheno_idx))) {
1360 assert(pheno_info_reverse_ll);
1361 const uint32_t is_second_relevant_line = !(pheno_info_reverse_ll->next);
1362 logerrprintfww("Error: '%s' entry on line %" PRIuPTR " of %s is numeric/'NA', while %s categorical.\n", &(pheno_names[(old_pheno_ct + new_pheno_idx) * max_pheno_name_blen]), line_idx, pheno_fname, is_second_relevant_line? "an earlier entry is" : "earlier entries are");
1363 goto LoadPhenos_ret_INCOMPATIBLE_PHENOSTRS;
1364 }
1365 if (!IsSet(quantitative_phenos, new_pheno_idx)) {
1366 if ((dxx != missing_phenod) && (dxx != pheno_ctrld) && (dxx != pheno_cased) && (dxx != 0.0)) {
1367 SetBit(new_pheno_idx, quantitative_phenos);
1368 }
1369 }
1370 first_pheno_data[new_pheno_idx] = dxx;
1371 }
1372 pheno_info_reverse_ll = first_pheno_info;
1373 for (uint32_t xid_idx = xid_idx_start + 1; xid_idx != xid_idx_end; ++xid_idx) {
1374 const uint32_t sample_uidx = xid_map[xid_idx];
1375 // if this is a duplicate, first ID in this group should also have been
1376 // caught as a duplicate
1377 assert(!IsSet(already_seen, sample_uidx));
1378 SetBit(sample_uidx, already_seen);
1379 tmp_bigstack_end -= pheno_info_alloc_byte_ct;
1380 PhenoInfoLl* cur_pheno_info = R_CAST(PhenoInfoLl*, tmp_bigstack_end);
1381 cur_pheno_info->next = pheno_info_reverse_ll;
1382 cur_pheno_info->sample_uidx = sample_uidx;
1383 memcpy(cur_pheno_info->phenodata, first_pheno_data, new_pheno_ct * sizeof(double));
1384 pheno_info_reverse_ll = cur_pheno_info;
1385 }
1386 }
1387 BigstackEndSet(tmp_bigstack_end);
1388 if (unlikely(TextStreamErrcode2(&pheno_txs, &reterr))) {
1389 goto LoadPhenos_ret_TSTREAM_FAIL;
1390 }
1391 reterr = kPglRetSuccess;
1392 if (unlikely(!pheno_info_reverse_ll)) {
1393 if (line_idx == 1) {
1394 snprintf(g_logbuf, kLogbufSize, "Error: %s is empty.\n", pheno_fname);
1395 goto LoadPhenos_ret_MALFORMED_INPUT_WW;
1396 }
1397 // could make this a warning, and automatically delete phenotypes?
1398 logerrprintf("Error: No entries in %s correspond to loaded sample IDs.\n", pheno_fname);
1399 goto LoadPhenos_ret_INCONSISTENT_INPUT;
1400 }
1401 if (new_pheno_ct) {
1402 uint32_t** old_cat_idxs_to_new = nullptr;
1403 if (categorical_pheno_ct) {
1404 if (bigstack_calloc_u32p(new_pheno_ct, &old_cat_idxs_to_new)) {
1405 goto LoadPhenos_ret_NOMEM;
1406 }
1407 }
1408 const uint32_t check_neg8_to_10_pheno = (missing_pheno == -9) && (!neg9_pheno_really_missing);
1409 const uintptr_t nonmiss_vec_ct = BitCtToVecCt(raw_sample_ct);
1410 uint32_t cat_pheno_idx = 0;
1411 uint32_t neg9_seen = 0;
1412 uint32_t other_neg8_to_10_seen = 0;
1413 PhenoCol* pheno_cols_iter = &(new_pheno_cols[old_pheno_ct]);
1414 for (uint32_t new_pheno_idx = 0; new_pheno_idx != new_pheno_ct; ++new_pheno_idx) {
1415 const uint32_t is_categorical = IsSet(categorical_phenos, new_pheno_idx);
1416 const uint32_t is_qt = IsSet(quantitative_phenos, new_pheno_idx);
1417 uintptr_t data_vec_ct = 0;
1418 uintptr_t catname_vec_ct = 0;
1419 uintptr_t catname_storage_vec_ct = 0;
1420 uint32_t nonnull_catname_ct = 0;
1421 if (!is_categorical) {
1422 pheno_cols_iter->category_names = nullptr;
1423 pheno_cols_iter->type_code = S_CAST(PhenoDtype, is_qt);
1424 pheno_cols_iter->nonnull_category_ct = 0;
1425 if (is_qt) {
1426 data_vec_ct = DblCtToVecCt(raw_sample_ct);
1427 } else {
1428 data_vec_ct = nonmiss_vec_ct;
1429 }
1430 } else {
1431 nonnull_catname_ct = pheno_catname_last[cat_pheno_idx]->cat_idx;
1432 if (unlikely(bigstack_alloc_u32(nonnull_catname_ct + 1, &(old_cat_idxs_to_new[new_pheno_idx])))) {
1433 goto LoadPhenos_ret_NOMEM;
1434 }
1435 data_vec_ct = Int32CtToVecCt(raw_sample_ct);
1436 catname_vec_ct = WordCtToVecCt(nonnull_catname_ct + 1);
1437 catname_storage_vec_ct = DivUp(total_catname_blens[cat_pheno_idx], kBytesPerVec);
1438 pheno_cols_iter->type_code = kPhenoDtypeCat;
1439 pheno_cols_iter->nonnull_category_ct = nonnull_catname_ct;
1440 }
1441 // pheno_cols_iter->nonmiss = nullptr;
1442 uintptr_t* new_pheno_data_iter;
1443 if (unlikely(vecaligned_malloc((nonmiss_vec_ct + data_vec_ct + catname_vec_ct + catname_storage_vec_ct) * kBytesPerVec, &new_pheno_data_iter))) {
1444 goto LoadPhenos_ret_NOMEM;
1445 }
1446 pheno_cols_iter->nonmiss = new_pheno_data_iter;
1447 ZeroWArr(nonmiss_vec_ct * kWordsPerVec, new_pheno_data_iter);
1448 new_pheno_data_iter = &(new_pheno_data_iter[nonmiss_vec_ct * kWordsPerVec]);
1449 if (is_categorical) {
1450 // allow nonmiss[] to be ignored in categorical case
1451 ZeroWArr(data_vec_ct, new_pheno_data_iter);
1452 pheno_cols_iter->data.cat = R_CAST(uint32_t*, new_pheno_data_iter);
1453 new_pheno_data_iter = &(new_pheno_data_iter[data_vec_ct * kWordsPerVec]);
1454 const char** cur_name_ptrs = R_CAST(const char**, new_pheno_data_iter);
1455 pheno_cols_iter->category_names = cur_name_ptrs;
1456 *cur_name_ptrs++ = missing_catname;
1457 char* name_storage_iter = R_CAST(char*, &(new_pheno_data_iter[catname_vec_ct * kWordsPerVec]));
1458 uint32_t cur_hval = missing_catname_hval + cat_pheno_idx;
1459 if (cur_hval >= kCatHtableSize) {
1460 cur_hval -= kCatHtableSize;
1461 }
1462 // make this point to the "NONE" entry for the current phenotype,
1463 // which starts the linked list
1464 CatnameLl2* catname_entry_ptr = catname_htable[cur_hval];
1465
1466 for (uint32_t catname_idx = 0; catname_idx != nonnull_catname_ct; ++catname_idx) {
1467 catname_entry_ptr = catname_entry_ptr->pheno_next;
1468 char* cur_name_start = name_storage_iter;
1469 name_storage_iter = strcpyax(name_storage_iter, catname_entry_ptr->str, '\0');
1470 *cur_name_ptrs++ = cur_name_start;
1471 }
1472 if (unlikely(SortStrptrArrIndexed(nonnull_catname_ct + 1, 1, 0, 1, pheno_cols_iter->category_names, nullptr, old_cat_idxs_to_new[new_pheno_idx]))) {
1473 goto LoadPhenos_ret_NOMEM;
1474 }
1475 ++cat_pheno_idx;
1476 } else if (!is_qt) {
1477 pheno_cols_iter->data.cc = new_pheno_data_iter;
1478 ZeroWArr(nonmiss_vec_ct * kWordsPerVec, new_pheno_data_iter);
1479 } else {
1480 pheno_cols_iter->data.qt = R_CAST(double*, new_pheno_data_iter);
1481 }
1482 ++pheno_cols_iter;
1483 }
1484 while (pheno_info_reverse_ll) {
1485 const uint32_t sample_uidx = pheno_info_reverse_ll->sample_uidx;
1486 double* pheno_data = pheno_info_reverse_ll->phenodata;
1487 pheno_cols_iter = &(new_pheno_cols[old_pheno_ct]);
1488 for (uint32_t new_pheno_idx = 0; new_pheno_idx != new_pheno_ct; ++new_pheno_idx) {
1489 if (IsSet(categorical_phenos, new_pheno_idx)) {
1490 uint32_t cur_cat;
1491 memcpy(&cur_cat, &(pheno_data[new_pheno_idx]), sizeof(int32_t));
1492 pheno_cols_iter->data.cat[sample_uidx] = old_cat_idxs_to_new[new_pheno_idx][cur_cat];
1493 if (cur_cat) {
1494 SetBit(sample_uidx, pheno_cols_iter->nonmiss);
1495 }
1496 } else {
1497 double dxx = pheno_data[new_pheno_idx];
1498 // bugfix (6 May 2017): forgot to accept 0 as missing value for
1499 // case/control
1500 if (IsSet(quantitative_phenos, new_pheno_idx)) {
1501 if (dxx != missing_phenod) {
1502 SetBit(sample_uidx, pheno_cols_iter->nonmiss);
1503 pheno_cols_iter->data.qt[sample_uidx] = dxx;
1504 }
1505 if (check_neg8_to_10_pheno) {
1506 if ((dxx <= -8.0) && (dxx >= -10.0)) {
1507 if (dxx == -9.0) {
1508 neg9_seen = 1;
1509 } else {
1510 other_neg8_to_10_seen = 1;
1511 }
1512 }
1513 }
1514 } else {
1515 if (dxx == pheno_cased) {
1516 SetBit(sample_uidx, pheno_cols_iter->data.cc);
1517 SetBit(sample_uidx, pheno_cols_iter->nonmiss);
1518 } else if (dxx == pheno_ctrld) {
1519 SetBit(sample_uidx, pheno_cols_iter->nonmiss);
1520 }
1521 }
1522 }
1523 ++pheno_cols_iter;
1524 }
1525 pheno_info_reverse_ll = pheno_info_reverse_ll->next;
1526 }
1527 if (check_neg8_to_10_pheno && other_neg8_to_10_seen) {
1528 if (unlikely(neg9_seen)) {
1529 logerrputs("Error: Distinct phenotype/covariate values in [-8, -10] present, including -9,\nwhen -9 is treated as missing. Use --no-input-missing-phenotype to treat -9 as\na numeric value (missing values can be indicated by 'NA'), or\n--neg9-pheno-really-missing to suppress this error.\n");
1530 goto LoadPhenos_ret_INCONSISTENT_INPUT;
1531 }
1532 logerrputs("Warning: Phenotype/covariate value in [-8, -9) or (-9, -10] present, when -9\nis treated as missing. Use --no-input-missing-phenotype to treat -9 as a\nnumeric value (missing values can be indicated by 'NA'), or\n--neg9-pheno-really-missing to suppress this warning.\n");
1533 }
1534 }
1535 *pheno_names_ptr = pheno_names;
1536 *max_pheno_name_blen_ptr = max_pheno_name_blen;
1537 }
1538 while (0) {
1539 LoadPhenos_ret_NOMEM:
1540 reterr = kPglRetNomem;
1541 break;
1542 LoadPhenos_ret_TSTREAM_FAIL:
1543 TextStreamErrPrint(pheno_fname, &pheno_txs);
1544 break;
1545 LoadPhenos_ret_MALFORMED_INPUT_WW:
1546 WordWrapB(0);
1547 LoadPhenos_ret_MALFORMED_INPUT_2:
1548 logerrputsb();
1549 LoadPhenos_ret_MALFORMED_INPUT:
1550 reterr = kPglRetMalformedInput;
1551 break;
1552 LoadPhenos_ret_MISSING_TOKENS:
1553 logerrprintfww("Error: Line %" PRIuPTR " of %s has fewer tokens than expected.\n", line_idx, pheno_fname);
1554 reterr = kPglRetMalformedInput;
1555 break;
1556 LoadPhenos_ret_INCOMPATIBLE_PHENOSTRS:
1557 logerrputs("(Unless --no-categorical is specified, case/control and quantitative phenotypes\nmust all be numeric/'NA'.\nCategorical phenotypes cannot be 'NA'--use e.g. 'NONE' to represent missing\ncategorical values instead--or start with a number.)\n");
1558 reterr = kPglRetMalformedInput;
1559 break;
1560 LoadPhenos_ret_INCONSISTENT_INPUT_WW:
1561 WordWrapB(0);
1562 LoadPhenos_ret_INCONSISTENT_INPUT_2:
1563 logerrputsb();
1564 LoadPhenos_ret_INCONSISTENT_INPUT:
1565 reterr = kPglRetInconsistentInput;
1566 break;
1567 }
1568 LoadPhenos_ret_1:
1569 CleanupTextStream2(pheno_fname, &pheno_txs, &reterr);
1570 BigstackDoubleReset(bigstack_mark, bigstack_end_mark);
1571 if (reterr) {
1572 free_cond(pheno_names);
1573 if (*pheno_names_ptr) {
1574 free(*pheno_names_ptr);
1575 *pheno_names_ptr = nullptr;
1576 }
1577 CleanupPhenoCols(*pheno_ct_ptr, *pheno_cols_ptr);
1578 *pheno_ct_ptr = 0;
1579 *pheno_cols_ptr = nullptr;
1580 }
1581 return reterr;
1582 }
1583
1584 PglErr IgnorePhenosOrCovars(const char* not_pheno_flattened, uint32_t is_covar, PhenoCol** pheno_cols_ptr, char** pheno_names_ptr, uint32_t* pheno_ct_ptr, uintptr_t* max_pheno_name_blen_ptr) {
1585 unsigned char* bigstack_mark = g_bigstack_base;
1586 PglErr reterr = kPglRetSuccess;
1587 {
1588 const uint32_t orig_pheno_ct = *pheno_ct_ptr;
1589 if (!orig_pheno_ct) {
1590 goto IgnorePhenosOrCovars_ret_NULL;
1591 }
1592 // probably time to write a function to directly construct a hash table
1593 // from the flattened list...
1594 char* not_pheno_strbox;
1595 uint32_t not_pheno_ct;
1596 uintptr_t not_pheno_max_blen;
1597 if (unlikely(MultistrToStrboxDedupAlloc(not_pheno_flattened, &not_pheno_strbox, &not_pheno_ct, &not_pheno_max_blen))) {
1598 goto IgnorePhenosOrCovars_ret_NOMEM;
1599 }
1600 uint32_t* id_htable;
1601 uint32_t id_htable_size;
1602 if (unlikely(HtableGoodSizeAlloc(not_pheno_ct, bigstack_left(), &id_htable, &id_htable_size))) {
1603 goto IgnorePhenosOrCovars_ret_NOMEM;
1604 }
1605 PopulateStrboxHtable(not_pheno_strbox, not_pheno_ct, not_pheno_max_blen, id_htable_size, id_htable);
1606
1607 const uint32_t orig_pheno_ctl = BitCtToWordCt(orig_pheno_ct);
1608 uintptr_t* pheno_delete;
1609 if (unlikely(bigstack_calloc_w(orig_pheno_ctl, &pheno_delete))) {
1610 goto IgnorePhenosOrCovars_ret_NOMEM;
1611 }
1612
1613 const uintptr_t max_pheno_name_blen = *max_pheno_name_blen_ptr;
1614 char* pheno_names = *pheno_names_ptr;
1615 char* pheno_name_iter = pheno_names;
1616 for (uint32_t pheno_idx = 0; pheno_idx != orig_pheno_ct; ++pheno_idx) {
1617 const uint32_t pheno_name_slen = strlen(pheno_name_iter);
1618 if (StrboxHtableFind(pheno_name_iter, not_pheno_strbox, id_htable, not_pheno_max_blen, pheno_name_slen, id_htable_size) != UINT32_MAX) {
1619 SetBit(pheno_idx, pheno_delete);
1620 }
1621 pheno_name_iter = &(pheno_name_iter[max_pheno_name_blen]);
1622 }
1623 const uint32_t delete_ct = PopcountWords(pheno_delete, orig_pheno_ctl);
1624 if (!delete_ct) {
1625 goto IgnorePhenosOrCovars_ret_NULL;
1626 }
1627 const uint32_t new_pheno_ct = orig_pheno_ct - delete_ct;
1628 if (new_pheno_ct) {
1629 PhenoCol* pheno_cols = *pheno_cols_ptr;
1630 uint32_t write_pheno_idx = AdvTo1Bit(pheno_delete, 0);
1631 for (uint32_t read_pheno_idx = write_pheno_idx; read_pheno_idx != orig_pheno_ct; ++read_pheno_idx) {
1632 if (IsSet(pheno_delete, read_pheno_idx)) {
1633 vecaligned_free_cond(pheno_cols[read_pheno_idx].nonmiss);
1634 } else {
1635 pheno_cols[write_pheno_idx] = pheno_cols[read_pheno_idx];
1636 memcpy(&(pheno_names[write_pheno_idx * max_pheno_name_blen]), &(pheno_names[read_pheno_idx * max_pheno_name_blen]), max_pheno_name_blen);
1637 ++write_pheno_idx;
1638 }
1639 }
1640 // possible todo: also try to shrink max_pheno_name_blen
1641 } else {
1642 free(*pheno_names_ptr);
1643 *pheno_names_ptr = nullptr;
1644 CleanupPhenoCols(orig_pheno_ct, *pheno_cols_ptr);
1645 *pheno_cols_ptr = nullptr;
1646 }
1647 *pheno_ct_ptr = new_pheno_ct;
1648 if (is_covar) {
1649 logprintf("--not-covar: %u covariate%s ignored, %u remaining.\n", delete_ct, (delete_ct == 1)? "" : "s", new_pheno_ct);
1650 } else {
1651 logprintf("--not-pheno: %u phenotype%s ignored, %u remaining.\n", delete_ct, (delete_ct == 1)? "" : "s", new_pheno_ct);
1652 }
1653 }
1654 while (0) {
1655 IgnorePhenosOrCovars_ret_NOMEM:
1656 reterr = kPglRetNomem;
1657 break;
1658 IgnorePhenosOrCovars_ret_NULL:
1659 logprintf("--not-%s: named %s not found. Doing nothing.\n", is_covar? "covar" : "pheno", is_covar? "covariate(s)" : "phenotype(s)");
1660 break;
1661 }
1662 BigstackReset(bigstack_mark);
1663 return reterr;
1664 }
1665
1666 #ifdef __cplusplus
1667 } // namespace plink2
1668 #endif