| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include "plink_common.h" |
| 19 |
|
| 20 |
#include "plink_lasso.h" |
| 21 |
#include "plink_matrix.h" |
| 22 |
|
| 23 |
|
| 24 |
#define DEFAULT_WARM_START_ITERS 1000 |
| 25 |
|
| 26 |
#define NLAMBDA 100 |
| 27 |
#define DELTA_THRESHOLD 0.0001 |
| 28 |
|
| 29 |
|
| 30 |
int32_t transpose_covar(const uintptr_t* sample_exclude, const uintptr_t* pheno_nm, const uintptr_t* covar_nm, const double* readptr, uint32_t sample_ct, uintptr_t sample_valid_ct, uintptr_t covar_ct, double sqrt_n_recip, double sample_valid_ct_recip, double sample_valid_ctm1d, double* writeptr_start) { |
| 31 |
double sum = 0.0; |
| 32 |
double ssq = 0.0; |
| 33 |
double* writeptr = writeptr_start; |
| 34 |
uint32_t sample_uidx = 0; |
| 35 |
for (uint32_t sample_idx = 0; sample_idx < sample_ct; ++sample_uidx, ++sample_idx) { |
| 36 |
next_unset_unsafe_ck(sample_exclude, &sample_uidx); |
| 37 |
if (IS_SET(pheno_nm, sample_uidx)) { |
| 38 |
const double dxx = readptr[sample_idx * covar_ct]; |
| 39 |
sum += dxx; |
| 40 |
ssq += dxx * dxx; |
| 41 |
*writeptr++ = dxx; |
| 42 |
} |
| 43 |
} |
| 44 |
if (ssq * ((double)sample_valid_ct) == sum * sum) { |
| 45 |
return -1; |
| 46 |
} |
| 47 |
const double subtract_by = sum * sample_valid_ct_recip; |
| 48 |
const double multiply_by = sqrt_n_recip * sqrt(sample_valid_ctm1d / (ssq - sum * subtract_by)); |
| 49 |
writeptr = writeptr_start; |
| 50 |
for (uint32_t sample_idx = 0; sample_idx < sample_valid_ct; ++sample_idx) { |
| 51 |
*writeptr = ((*writeptr) - subtract_by) * multiply_by; |
| 52 |
writeptr++; |
| 53 |
} |
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
int32_t lasso_bigmem(FILE* bedfile, uintptr_t bed_offset, uintptr_t* marker_exclude, uintptr_t marker_ct, uintptr_t* marker_reverse, Chrom_info* chrom_info_ptr, uintptr_t unfiltered_sample_ct, const uintptr_t* sample_exclude, uintptr_t* pheno_nm2, double lasso_h2, double lasso_minlambda, uint32_t select_covars, uintptr_t* select_covars_bitfield, double* pheno_d_collapsed, uintptr_t covar_ct, char* covar_names, uintptr_t max_covar_name_len, uintptr_t* covar_nm, double* covar_d, uint32_t hh_or_mt_exists, uint32_t sample_ct, uintptr_t sample_valid_ct, uintptr_t* sample_include2, uintptr_t* sample_male_include2, uintptr_t* loadbuf_raw, uintptr_t* loadbuf_collapsed, double* rand_matrix, double* misc_arr, double* residuals, uintptr_t* polymorphic_markers, uintptr_t* polymorphic_marker_ct_ptr, uint64_t* iter_tot_ptr, double** xhat_ptr) { |
| 58 |
uintptr_t unfiltered_sample_ct4 = (unfiltered_sample_ct + 3) / 4; |
| 59 |
double* data_arr = (double*)g_bigstack_base; |
| 60 |
double sqrt_n_recip = sqrt(1.0 / ((double)((intptr_t)sample_valid_ct))); |
| 61 |
double lambda_max = 0.0; |
| 62 |
double err_cur = 0.0; |
| 63 |
uint64_t iter_tot = 0; |
| 64 |
uintptr_t sample_valid_ctl2 = QUATERCT_TO_WORDCT(sample_valid_ct); |
| 65 |
uintptr_t polymorphic_marker_ct = 0; |
| 66 |
uintptr_t unselected_covar_ct = 0; |
| 67 |
uintptr_t final_mask = get_final_mask(sample_valid_ct); |
| 68 |
double lambda_min = lasso_minlambda; |
| 69 |
uint32_t chrom_fo_idx = 0xffffffffU; |
| 70 |
uint32_t chrom_end = 0; |
| 71 |
uint32_t is_x = 0; |
| 72 |
uint32_t is_y = 0; |
| 73 |
uint32_t min_ploidy_1 = 0; |
| 74 |
int32_t retval = 0; |
| 75 |
double cur_mapping[4]; |
| 76 |
double* xhat; |
| 77 |
double* prod_matrix; |
| 78 |
double* dptr; |
| 79 |
double* dptr2; |
| 80 |
uintptr_t* ulptr_end_init; |
| 81 |
uintptr_t* ulptr_end; |
| 82 |
uintptr_t* active_set; |
| 83 |
uintptr_t* ulptr; |
| 84 |
double sige; |
| 85 |
double loghi; |
| 86 |
double loglo; |
| 87 |
double logdelta; |
| 88 |
double lambda; |
| 89 |
double xjold; |
| 90 |
double err_last; |
| 91 |
double dxx; |
| 92 |
double dyy; |
| 93 |
double zz; |
| 94 |
uintptr_t cur_word; |
| 95 |
uintptr_t cur_genotype; |
| 96 |
uintptr_t ulii; |
| 97 |
uintptr_t iter; |
| 98 |
uintptr_t col_ct; |
| 99 |
uintptr_t col_ctl; |
| 100 |
uintptr_t col_idx; |
| 101 |
uintptr_t col_nz_ct; |
| 102 |
uintptr_t col_to_z; |
| 103 |
uintptr_t col_uidx; |
| 104 |
uintptr_t covar_idx; |
| 105 |
uintptr_t sample_idx; |
| 106 |
uintptr_t sample_idx_stop; |
| 107 |
uintptr_t marker_idx; |
| 108 |
uint32_t lambi; |
| 109 |
uint32_t marker_uidx; |
| 110 |
uint32_t homrar_ct; |
| 111 |
uint32_t missing_ct; |
| 112 |
uint32_t het_ct; |
| 113 |
uint32_t homset_ct; |
| 114 |
uint32_t uii; |
| 115 |
|
| 116 |
cur_mapping[1] = 0; |
| 117 |
if (fseeko(bedfile, bed_offset, SEEK_SET)) { |
| 118 |
goto lasso_bigmem_ret_READ_FAIL; |
| 119 |
} |
| 120 |
ulptr_end_init = &(loadbuf_collapsed[sample_valid_ct / BITCT2]); |
| 121 |
fputs("--lasso: Populating data matrix...", stdout); |
| 122 |
fflush(stdout); |
| 123 |
if (covar_ct) { |
| 124 |
dxx = 1.0 / ((double)((intptr_t)sample_valid_ct)); |
| 125 |
dyy = (double)((intptr_t)(sample_valid_ct - 1)); |
| 126 |
if (!select_covars_bitfield) { |
| 127 |
for (covar_idx = 0; covar_idx < covar_ct; covar_idx++) { |
| 128 |
if (transpose_covar(sample_exclude, pheno_nm2, covar_nm, &(covar_d[covar_idx]), sample_ct, sample_valid_ct, covar_ct, sqrt_n_recip, dxx, dyy, &(data_arr[covar_idx * sample_valid_ct]))) { |
| 129 |
goto lasso_bigmem_ret_CONST_COVAR; |
| 130 |
} |
| 131 |
} |
| 132 |
if (!select_covars) { |
| 133 |
unselected_covar_ct = covar_ct; |
| 134 |
} |
| 135 |
} else { |
| 136 |
ulii = 0; |
| 137 |
for (covar_idx = 0; covar_idx < covar_ct; covar_idx++) { |
| 138 |
if (IS_SET(select_covars_bitfield, covar_idx)) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
if (transpose_covar(sample_exclude, pheno_nm2, covar_nm, &(covar_d[covar_idx]), sample_ct, sample_valid_ct, covar_ct, sqrt_n_recip, dxx, dyy, &(data_arr[ulii * sample_valid_ct]))) { |
| 142 |
goto lasso_bigmem_ret_CONST_COVAR; |
| 143 |
} |
| 144 |
ulii++; |
| 145 |
} |
| 146 |
unselected_covar_ct = ulii; |
| 147 |
for (covar_idx = 0; covar_idx < covar_ct; covar_idx++) { |
| 148 |
if (!IS_SET(select_covars_bitfield, covar_idx)) { |
| 149 |
continue; |
| 150 |
} |
| 151 |
if (transpose_covar(sample_exclude, pheno_nm2, covar_nm, &(covar_d[covar_idx]), sample_ct, sample_valid_ct, covar_ct, sqrt_n_recip, dxx, dyy, &(data_arr[ulii * sample_valid_ct]))) { |
| 152 |
goto lasso_bigmem_ret_CONST_COVAR; |
| 153 |
} |
| 154 |
ulii++; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
dptr = &(data_arr[covar_ct * sample_valid_ct]); |
| 159 |
for (marker_uidx = 0, marker_idx = 0; marker_idx < marker_ct; marker_uidx++, marker_idx++) { |
| 160 |
if (IS_SET(marker_exclude, marker_uidx)) { |
| 161 |
marker_uidx = next_unset_unsafe(marker_exclude, marker_uidx); |
| 162 |
if (fseeko(bedfile, bed_offset + ((uint64_t)marker_uidx) * unfiltered_sample_ct4, SEEK_SET)) { |
| 163 |
goto lasso_bigmem_ret_READ_FAIL; |
| 164 |
} |
| 165 |
} |
| 166 |
if (marker_uidx >= chrom_end) { |
| 167 |
chrom_fo_idx++; |
| 168 |
refresh_chrom_info(chrom_info_ptr, marker_uidx, &chrom_end, &chrom_fo_idx, &is_x, &is_y, &uii, &min_ploidy_1); |
| 169 |
} |
| 170 |
min_ploidy_1 |= uii; |
| 171 |
if (load_and_collapse_incl(unfiltered_sample_ct, sample_valid_ct, pheno_nm2, final_mask, IS_SET(marker_reverse, marker_uidx), bedfile, loadbuf_raw, loadbuf_collapsed)) { |
| 172 |
goto lasso_bigmem_ret_READ_FAIL; |
| 173 |
} |
| 174 |
if (min_ploidy_1) { |
| 175 |
haploid_fix(hh_or_mt_exists, sample_include2, sample_male_include2, sample_valid_ct, is_x, is_y, (unsigned char*)loadbuf_collapsed); |
| 176 |
} |
| 177 |
genovec_3freq(loadbuf_collapsed, sample_include2, sample_valid_ctl2, &missing_ct, &het_ct, &homset_ct); |
| 178 |
uii = sample_valid_ct - missing_ct; |
| 179 |
homrar_ct = uii - het_ct - homset_ct; |
| 180 |
if (!(((!homrar_ct) && ((!het_ct) || (!homset_ct))) || ((!het_ct) && (!homset_ct)))) { |
| 181 |
|
| 182 |
SET_BIT(marker_uidx, polymorphic_markers); |
| 183 |
dyy = (double)(2 * homrar_ct + het_ct); |
| 184 |
dxx = dyy / ((double)((int32_t)uii)); |
| 185 |
dyy = sqrt_n_recip * sqrt(((double)((int32_t)(uii - 1))) / (4 * ((double)((int32_t)homrar_ct)) + ((double)((int32_t)het_ct)) - dyy * dxx)); |
| 186 |
cur_mapping[0] = (2 - dxx) * dyy; |
| 187 |
cur_mapping[2] = (1 - dxx) * dyy; |
| 188 |
cur_mapping[3] = (-dxx) * dyy; |
| 189 |
sample_idx = 0; |
| 190 |
sample_idx_stop = BITCT2; |
| 191 |
ulptr = loadbuf_collapsed; |
| 192 |
ulptr_end = ulptr_end_init; |
| 193 |
while (1) { |
| 194 |
while (ulptr < ulptr_end) { |
| 195 |
cur_word = *ulptr++; |
| 196 |
for (; sample_idx < sample_idx_stop; sample_idx++, cur_word >>= 2) { |
| 197 |
cur_genotype = cur_word & 3; |
| 198 |
*dptr++ = cur_mapping[cur_genotype]; |
| 199 |
} |
| 200 |
sample_idx_stop += BITCT2; |
| 201 |
} |
| 202 |
if (sample_idx == sample_valid_ct) { |
| 203 |
break; |
| 204 |
} |
| 205 |
ulptr_end++; |
| 206 |
sample_idx_stop = sample_valid_ct; |
| 207 |
} |
| 208 |
polymorphic_marker_ct++; |
| 209 |
} |
| 210 |
} |
| 211 |
*polymorphic_marker_ct_ptr = polymorphic_marker_ct; |
| 212 |
if (!polymorphic_marker_ct) { |
| 213 |
putc_unlocked('\n', stdout); |
| 214 |
logerrprint("Warning: Skipping --lasso since no polymorphic loci are present.\n"); |
| 215 |
return 0; |
| 216 |
} |
| 217 |
col_ct = covar_ct + polymorphic_marker_ct; |
| 218 |
col_ctl = BITCT_TO_WORDCT(col_ct); |
| 219 |
bigstack_shrink_top(data_arr, col_ct * sample_valid_ct * sizeof(double)); |
| 220 |
sige = sqrt(1.0 - lasso_h2 + 1.0 / ((double)((intptr_t)sample_valid_ct))); |
| 221 |
zz = sige * sqrt_n_recip; |
| 222 |
if (rand_matrix) { |
| 223 |
bigstack_alloc_d(DEFAULT_WARM_START_ITERS * DEFAULT_WARM_START_ITERS, &prod_matrix); |
| 224 |
fputs("\r--lasso: Initializing warm start matrix...", stdout); |
| 225 |
fflush(stdout); |
| 226 |
fill_double_zero(DEFAULT_WARM_START_ITERS, misc_arr); |
| 227 |
for (col_idx = 0; col_idx < col_ct;) { |
| 228 |
ulii = col_idx + DEFAULT_WARM_START_ITERS; |
| 229 |
if (ulii > col_ct) { |
| 230 |
ulii = col_ct; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
col_major_matrix_multiply(DEFAULT_WARM_START_ITERS, ulii - col_idx, sample_valid_ct, rand_matrix, &(data_arr[col_idx * sample_valid_ct]), prod_matrix); |
| 235 |
dptr = prod_matrix; |
| 236 |
for (; col_idx < ulii; col_idx++) { |
| 237 |
for (uii = 0; uii < DEFAULT_WARM_START_ITERS; uii++) { |
| 238 |
dxx = fabs(*dptr++); |
| 239 |
if (dxx > misc_arr[uii]) { |
| 240 |
misc_arr[uii] = dxx; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
lambda_min = destructive_get_dmedian(DEFAULT_WARM_START_ITERS, misc_arr) * zz; |
| 246 |
logstr("--lasso:"); |
| 247 |
LOGPRINTF(" using min lambda = %g.\n", lambda_min); |
| 248 |
bigstack_reset(prod_matrix); |
| 249 |
} |
| 250 |
bigstack_alloc_d(col_ct, &xhat); |
| 251 |
bigstack_alloc_ul(col_ctl, &active_set); |
| 252 |
*xhat_ptr = xhat; |
| 253 |
dptr = data_arr; |
| 254 |
for (col_idx = 0; col_idx < col_ct; col_idx++) { |
| 255 |
dxx = 0; |
| 256 |
dptr2 = pheno_d_collapsed; |
| 257 |
for (sample_idx = 0; sample_idx < sample_valid_ct; sample_idx++) { |
| 258 |
dxx += (*dptr++) * (*dptr2++); |
| 259 |
} |
| 260 |
dxx = fabs(dxx); |
| 261 |
if (dxx > lambda_max) { |
| 262 |
lambda_max = dxx; |
| 263 |
} |
| 264 |
} |
| 265 |
if (lambda_min >= lambda_max) { |
| 266 |
logprint("\n"); |
| 267 |
logerrprint("Error: min lambda >= max lambda.\n"); |
| 268 |
goto lasso_bigmem_ret_INVALID_CMDLINE; |
| 269 |
} |
| 270 |
loghi = log(lambda_max); |
| 271 |
loglo = log(lambda_min); |
| 272 |
logdelta = (loghi - loglo) / (NLAMBDA - 1.0); |
| 273 |
dptr = data_arr; |
| 274 |
for (col_idx = 0; col_idx < col_ct; col_idx++) { |
| 275 |
dxx = 0.0; |
| 276 |
dptr2 = pheno_d_collapsed; |
| 277 |
for (sample_idx = 0; sample_idx < sample_valid_ct; sample_idx++) { |
| 278 |
dxx += (*dptr++) * (*dptr2++); |
| 279 |
} |
| 280 |
xhat[col_idx] = dxx; |
| 281 |
} |
| 282 |
fputs("\r--lasso: Executing coordinate descent... ", stdout); |
| 283 |
for (lambi = 0; lambi < NLAMBDA; lambi++) { |
| 284 |
if (lambi > 10) { |
| 285 |
fputs("\b\b\b", stdout); |
| 286 |
} else if (lambi) { |
| 287 |
fputs("\b\b", stdout); |
| 288 |
} |
| 289 |
printf("%u%%", lambi); |
| 290 |
fflush(stdout); |
| 291 |
lambda = exp(loghi - logdelta * ((double)((int32_t)lambi))); |
| 292 |
memcpy(residuals, pheno_d_collapsed, sample_valid_ct * sizeof(double)); |
| 293 |
for (col_idx = 0; col_idx < col_ct; col_idx++) { |
| 294 |
dptr = residuals; |
| 295 |
dptr2 = &(data_arr[col_idx * sample_valid_ct]); |
| 296 |
dxx = -xhat[col_idx]; |
| 297 |
for (sample_idx = 0; sample_idx < sample_valid_ct; sample_idx++) { |
| 298 |
*dptr += (*dptr2++) * dxx; |
| 299 |
dptr++; |
| 300 |
} |
| 301 |
} |
| 302 |
iter = 0; |
| 303 |
fill_all_bits(col_ct, active_set); |
| 304 |
col_nz_ct = col_ct; |
| 305 |
while (1) { |
| 306 |
col_uidx = 0; |
| 307 |
col_to_z = 0; |
| 308 |
for (marker_idx = 0; marker_idx < col_nz_ct; marker_idx++, col_uidx++) { |
| 309 |
col_uidx = next_set_unsafe(active_set, col_uidx); |
| 310 |
xjold = xhat[col_uidx]; |
| 311 |
dxx = xjold; |
| 312 |
dptr = &(data_arr[col_uidx * sample_valid_ct]); |
| 313 |
dptr2 = residuals; |
| 314 |
for (sample_idx = 0; sample_idx < sample_valid_ct; sample_idx++) { |
| 315 |
dxx += (*dptr++) * (*dptr2++); |
| 316 |
} |
| 317 |
if (col_uidx >= unselected_covar_ct) { |
| 318 |
if (dxx > 0.0) { |
| 319 |
dxx = MAXV(dxx - lambda, 0.0); |
| 320 |
} else { |
| 321 |
dxx = MINV(dxx + lambda, 0.0); |
| 322 |
} |
| 323 |
} |
| 324 |
xhat[col_uidx] = dxx; |
| 325 |
if (dxx == 0.0) { |
| 326 |
CLEAR_BIT(col_uidx, active_set); |
| 327 |
col_to_z++; |
| 328 |
} |
| 329 |
dptr = residuals; |
| 330 |
dptr2 = &(data_arr[col_uidx * sample_valid_ct]); |
| 331 |
dxx -= xjold; |
| 332 |
for (sample_idx = 0; sample_idx < sample_valid_ct; sample_idx++) { |
| 333 |
*dptr -= (*dptr2++) * dxx; |
| 334 |
dptr++; |
| 335 |
} |
| 336 |
} |
| 337 |
col_nz_ct -= col_to_z; |
| 338 |
err_last = err_cur; |
| 339 |
err_cur = 0.0; |
| 340 |
col_uidx = 0; |
| 341 |
for (marker_idx = 0; marker_idx < col_nz_ct; marker_idx++, col_uidx++) { |
| 342 |
col_uidx = next_set_unsafe(active_set, col_uidx); |
| 343 |
err_cur += fabs(xhat[col_uidx]); |
| 344 |
} |
| 345 |
err_cur *= lambda; |
| 346 |
dptr = residuals; |
| 347 |
for (sample_idx = 0; sample_idx < sample_valid_ct; sample_idx++) { |
| 348 |
dxx = *dptr++; |
| 349 |
err_cur += dxx * dxx; |
| 350 |
} |
| 351 |
if (iter++) { |
| 352 |
if (((1.0 - (MINV(err_last, err_cur) / MAXV(err_last, err_cur))) < DELTA_THRESHOLD) || (err_cur != err_cur)) { |
| 353 |
iter_tot += iter; |
| 354 |
break; |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
*iter_tot_ptr = iter_tot; |
| 360 |
while (0) { |
| 361 |
lasso_bigmem_ret_READ_FAIL: |
| 362 |
retval = RET_READ_FAIL; |
| 363 |
break; |
| 364 |
lasso_bigmem_ret_CONST_COVAR: |
| 365 |
logerrprint("Error: --lasso covariate is constant.\n"); |
| 366 |
lasso_bigmem_ret_INVALID_CMDLINE: |
| 367 |
retval = RET_INVALID_CMDLINE; |
| 368 |
break; |
| 369 |
} |
| 370 |
return retval; |
| 371 |
} |
| 372 |
|
| 373 |
uint32_t load_and_normalize(FILE* bedfile, uintptr_t* loadbuf_raw, uintptr_t unfiltered_sample_ct, uintptr_t* loadbuf_collapsed, uintptr_t sample_valid_ct, uintptr_t* pheno_nm2, uintptr_t final_mask, uint32_t do_reverse, uint32_t min_ploidy_1, uint32_t hh_or_mt_exists, uintptr_t* sample_include2, uintptr_t* sample_male_include2, uint32_t is_x, uint32_t is_y, double sqrt_n_recip, double* data_window_ptr) { |
| 374 |
uintptr_t sample_valid_ctl2 = QUATERCT_TO_WORDCT(sample_valid_ct); |
| 375 |
uintptr_t sample_idx = 0; |
| 376 |
uintptr_t sample_idx_stop = BITCT2; |
| 377 |
uintptr_t* ulptr_end_init = &(loadbuf_collapsed[sample_valid_ct / BITCT2]); |
| 378 |
double cur_mapping[4]; |
| 379 |
uintptr_t* ulptr = loadbuf_collapsed; |
| 380 |
uintptr_t* ulptr_end = ulptr_end_init; |
| 381 |
uintptr_t cur_word; |
| 382 |
uintptr_t cur_genotype; |
| 383 |
double dxx; |
| 384 |
double dyy; |
| 385 |
uint32_t homrar_ct; |
| 386 |
uint32_t missing_ct; |
| 387 |
uint32_t het_ct; |
| 388 |
uint32_t homset_ct; |
| 389 |
uint32_t uii; |
| 390 |
if (load_and_collapse_incl(unfiltered_sample_ct, sample_valid_ct, pheno_nm2, final_mask, do_reverse, bedfile, loadbuf_raw, loadbuf_collapsed)) { |
| 391 |
return 2; |
| 392 |
} |
| 393 |
if (min_ploidy_1) { |
| 394 |
haploid_fix(hh_or_mt_exists, sample_include2, sample_male_include2, sample_valid_ct, is_x, is_y, (unsigned char*)loadbuf_collapsed); |
| 395 |
} |
| 396 |
genovec_3freq(loadbuf_collapsed, sample_include2, sample_valid_ctl2, &missing_ct, &het_ct, &homset_ct); |
| 397 |
uii = sample_valid_ct - missing_ct; |
| 398 |
homrar_ct = uii - het_ct - homset_ct; |
| 399 |
if (((!homrar_ct) && ((!het_ct) || (!homset_ct))) || ((!het_ct) && (!homset_ct))) { |
| 400 |
return 1; |
| 401 |
} |
| 402 |
dyy = (double)(2 * homrar_ct + het_ct); |
| 403 |
dxx = dyy / ((double)((int32_t)uii)); |
| 404 |
dyy = sqrt_n_recip * sqrt(((double)((int32_t)(uii - 1))) / (4 * ((double)((int32_t)homrar_ct)) + ((double)((int32_t)het_ct)) - dyy * dxx)); |
| 405 |
cur_mapping[0] = (2 - dxx) * dyy; |
| 406 |
cur_mapping[1] = 0.0; |
| 407 |
cur_mapping[2] = (1 - dxx) * dyy; |
| 408 |
cur_mapping[3] = (-dxx) * dyy; |
| 409 |
while (1) { |
| 410 |
while (ulptr < ulptr_end) { |
| 411 |
cur_word = *ulptr++; |
| 412 |
for (; sample_idx < sample_idx_stop; sample_idx++, cur_word >>= 2) { |
| 413 |
cur_genotype = cur_word & 3; |
| 414 |
*data_window_ptr++ = cur_mapping[cur_genotype]; |
| 415 |
} |
| 416 |
sample_idx_stop += BITCT2; |
| 417 |
} |
| 418 |
if (sample_idx == sample_valid_ct) { |
| 419 |
return 0; |
| 420 |
} |
| 421 |
ulptr_end++; |
| 422 |
sample_idx_stop = sample_valid_ct; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
#define LASSO_LAMBDA_BLOCK_SIZE 64 |
| 429 |
|
| 430 |
int32_t lasso_lambda(const uintptr_t* marker_exclude, const uintptr_t* marker_reverse, Chrom_info* chrom_info_ptr, const uintptr_t* sample_exclude, uintptr_t* sex_male, uintptr_t* pheno_nm, const uintptr_t* covar_nm, uintptr_t bed_offset, uintptr_t unfiltered_marker_ct, uintptr_t marker_ct, uintptr_t unfiltered_sample_ct, uint32_t sample_ct, uintptr_t pheno_nm_ct, uint32_t hh_or_mt_exists, uint32_t lasso_lambda_iters, double lasso_h2, FILE* bedfile, char* outname, char* outname_end, double* lasso_minlambda_ptr) { |
| 431 |
|
| 432 |
|
| 433 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 434 |
FILE* outfile = nullptr; |
| 435 |
int32_t retval = 0; |
| 436 |
{ |
| 437 |
const uintptr_t unfiltered_sample_ctl = BITCT_TO_WORDCT(unfiltered_sample_ct); |
| 438 |
uintptr_t* pheno_nm2; |
| 439 |
uintptr_t sample_valid_ct; |
| 440 |
if (!covar_nm) { |
| 441 |
sample_valid_ct = pheno_nm_ct; |
| 442 |
pheno_nm2 = pheno_nm; |
| 443 |
} else { |
| 444 |
if (bigstack_calloc_ul(unfiltered_sample_ctl, &pheno_nm2)) { |
| 445 |
goto lasso_lambda_ret_NOMEM; |
| 446 |
} |
| 447 |
uintptr_t sample_uidx = 0; |
| 448 |
for (uintptr_t sample_idx = 0; sample_idx < sample_ct; ++sample_uidx, ++sample_idx) { |
| 449 |
next_unset_ul_unsafe_ck(sample_exclude, &sample_uidx); |
| 450 |
if (IS_SET(pheno_nm, sample_uidx) && IS_SET(covar_nm, sample_idx)) { |
| 451 |
SET_BIT(sample_uidx, pheno_nm2); |
| 452 |
} |
| 453 |
} |
| 454 |
sample_valid_ct = popcount_longs(pheno_nm2, unfiltered_sample_ctl); |
| 455 |
} |
| 456 |
const uintptr_t final_mask = get_final_mask(sample_valid_ct); |
| 457 |
double* rand_matrix; |
| 458 |
double* max_empirical_lambdas; |
| 459 |
double* data_window; |
| 460 |
double* prod_matrix; |
| 461 |
if (bigstack_alloc_d(sample_valid_ct * LASSO_LAMBDA_BLOCK_SIZE, &rand_matrix) || |
| 462 |
bigstack_alloc_d(lasso_lambda_iters, &max_empirical_lambdas) || |
| 463 |
bigstack_alloc_d(sample_valid_ct * LASSO_LAMBDA_BLOCK_SIZE, &data_window) || |
| 464 |
bigstack_alloc_d(LASSO_LAMBDA_BLOCK_SIZE * LASSO_LAMBDA_BLOCK_SIZE, &prod_matrix)) { |
| 465 |
goto lasso_lambda_ret_NOMEM; |
| 466 |
} |
| 467 |
fill_double_zero(lasso_lambda_iters, max_empirical_lambdas); |
| 468 |
|
| 469 |
const uintptr_t sample_valid_ctv2 = QUATERCT_TO_ALIGNED_WORDCT(sample_valid_ct); |
| 470 |
const uintptr_t unfiltered_sample_ctv2 = QUATERCT_TO_ALIGNED_WORDCT(unfiltered_sample_ct); |
| 471 |
const double sqrt_n_recip = sqrt(1.0 / ((double)((intptr_t)sample_valid_ct))); |
| 472 |
uintptr_t* sample_include2; |
| 473 |
uintptr_t* loadbuf_raw; |
| 474 |
uintptr_t* loadbuf_collapsed; |
| 475 |
double* residuals; |
| 476 |
if (bigstack_alloc_ul(sample_valid_ctv2, &sample_include2) || |
| 477 |
bigstack_alloc_ul(unfiltered_sample_ctv2, &loadbuf_raw) || |
| 478 |
bigstack_alloc_ul(sample_valid_ctv2, &loadbuf_collapsed) || |
| 479 |
bigstack_alloc_d(sample_valid_ct, &residuals)) { |
| 480 |
goto lasso_lambda_ret_NOMEM; |
| 481 |
} |
| 482 |
fill_quatervec_55(sample_valid_ct, sample_include2); |
| 483 |
if ((chrom_info_ptr->xymt_codes[MT_OFFSET] != -2) && is_set(chrom_info_ptr->chrom_mask, chrom_info_ptr->xymt_codes[MT_OFFSET])) { |
| 484 |
hh_or_mt_exists |= NXMHH_EXISTS; |
| 485 |
} |
| 486 |
uintptr_t* sample_male_include2; |
| 487 |
if (alloc_collapsed_haploid_filters(pheno_nm2, sex_male, unfiltered_sample_ct, sample_valid_ct, hh_or_mt_exists, 1, &sample_include2, &sample_male_include2)) { |
| 488 |
goto lasso_lambda_ret_NOMEM; |
| 489 |
} |
| 490 |
|
| 491 |
const uintptr_t unfiltered_sample_ct4 = (unfiltered_sample_ct + 3) / 4; |
| 492 |
const uint32_t block_ct = (lasso_lambda_iters + LASSO_LAMBDA_BLOCK_SIZE - 1) / LASSO_LAMBDA_BLOCK_SIZE; |
| 493 |
double* max_empirical_lambdas_iter = max_empirical_lambdas; |
| 494 |
for (uint32_t block_idx = 0; block_idx < block_ct; ++block_idx) { |
| 495 |
printf("\r--lasso-lambda: %u iterations complete.", block_idx * LASSO_LAMBDA_BLOCK_SIZE); |
| 496 |
fflush(stdout); |
| 497 |
uint32_t cur_block_size = LASSO_LAMBDA_BLOCK_SIZE; |
| 498 |
if (block_idx == block_ct - 1) { |
| 499 |
cur_block_size = 1 + ((lasso_lambda_iters - 1) % LASSO_LAMBDA_BLOCK_SIZE); |
| 500 |
} |
| 501 |
|
| 502 |
const uintptr_t rand_matrix_size = cur_block_size * sample_valid_ct; |
| 503 |
|
| 504 |
for (uintptr_t rand_matrix_pos = 0; rand_matrix_pos < rand_matrix_size; rand_matrix_pos += 2) { |
| 505 |
rand_matrix[rand_matrix_pos] = rand_normal(&(rand_matrix[rand_matrix_pos + 1])); |
| 506 |
} |
| 507 |
|
| 508 |
uint32_t chrom_fo_idx = 0xffffffffU; |
| 509 |
uint32_t chrom_end = 0; |
| 510 |
uintptr_t marker_idx = 0; |
| 511 |
uint32_t marker_uidx = 0; |
| 512 |
uint32_t is_x = 0; |
| 513 |
uint32_t is_y = 0; |
| 514 |
double* dptr = data_window; |
| 515 |
uint32_t partial_marker_idx = 0; |
| 516 |
if (fseeko(bedfile, bed_offset, SEEK_SET)) { |
| 517 |
goto lasso_lambda_ret_READ_FAIL; |
| 518 |
} |
| 519 |
for (; marker_idx < marker_ct; marker_uidx++, marker_idx++) { |
| 520 |
|
| 521 |
if (IS_SET(marker_exclude, marker_uidx)) { |
| 522 |
marker_uidx = next_unset_unsafe(marker_exclude, marker_uidx); |
| 523 |
if (fseeko(bedfile, bed_offset + ((uint64_t)marker_uidx) * unfiltered_sample_ct4, SEEK_SET)) { |
| 524 |
goto lasso_lambda_ret_READ_FAIL; |
| 525 |
} |
| 526 |
} |
| 527 |
uint32_t min_ploidy_1 = 0; |
| 528 |
uint32_t uii; |
| 529 |
if (marker_uidx >= chrom_end) { |
| 530 |
chrom_fo_idx++; |
| 531 |
refresh_chrom_info(chrom_info_ptr, marker_uidx, &chrom_end, &chrom_fo_idx, &is_x, &is_y, &uii, &min_ploidy_1); |
| 532 |
} |
| 533 |
uii = load_and_normalize(bedfile, loadbuf_raw, unfiltered_sample_ct, loadbuf_collapsed, sample_valid_ct, pheno_nm2, final_mask, IS_SET(marker_reverse, marker_uidx), min_ploidy_1, hh_or_mt_exists, sample_include2, sample_male_include2, is_x, is_y, sqrt_n_recip, dptr); |
| 534 |
if (uii == 2) { |
| 535 |
goto lasso_lambda_ret_READ_FAIL; |
| 536 |
} |
| 537 |
if (uii == 1) { |
| 538 |
continue; |
| 539 |
} |
| 540 |
partial_marker_idx++; |
| 541 |
if (partial_marker_idx == LASSO_LAMBDA_BLOCK_SIZE) { |
| 542 |
col_major_matrix_multiply(cur_block_size, LASSO_LAMBDA_BLOCK_SIZE, sample_valid_ct, rand_matrix, data_window, prod_matrix); |
| 543 |
dptr = prod_matrix; |
| 544 |
for (uintptr_t col_idx = 0; col_idx < LASSO_LAMBDA_BLOCK_SIZE; col_idx++) { |
| 545 |
for (uintptr_t ulii = 0; ulii < cur_block_size; ulii++) { |
| 546 |
double dxx = fabs(*dptr++); |
| 547 |
if (dxx > max_empirical_lambdas_iter[ulii]) { |
| 548 |
max_empirical_lambdas_iter[ulii] = dxx; |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
partial_marker_idx = 0; |
| 553 |
dptr = data_window; |
| 554 |
} else { |
| 555 |
dptr = &(dptr[sample_valid_ct]); |
| 556 |
} |
| 557 |
} |
| 558 |
if (partial_marker_idx) { |
| 559 |
col_major_matrix_multiply(cur_block_size, partial_marker_idx, sample_valid_ct, rand_matrix, data_window, prod_matrix); |
| 560 |
dptr = prod_matrix; |
| 561 |
for (uintptr_t col_idx = 0; col_idx < partial_marker_idx; col_idx++) { |
| 562 |
for (uintptr_t ulii = 0; ulii < cur_block_size; ulii++) { |
| 563 |
double dxx = fabs(*dptr++); |
| 564 |
if (dxx > max_empirical_lambdas_iter[ulii]) { |
| 565 |
max_empirical_lambdas_iter[ulii] = dxx; |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
} |
| 570 |
max_empirical_lambdas_iter = &(max_empirical_lambdas_iter[LASSO_LAMBDA_BLOCK_SIZE]); |
| 571 |
} |
| 572 |
const double sige = sqrt(1.0 - lasso_h2 + 1.0 / ((double)((intptr_t)sample_valid_ct))); |
| 573 |
const double zz = sige * sqrt_n_recip; |
| 574 |
for (uint32_t iter_idx = 0; iter_idx < lasso_lambda_iters; ++iter_idx) { |
| 575 |
max_empirical_lambdas[iter_idx] *= zz; |
| 576 |
} |
| 577 |
#ifdef __cplusplus |
| 578 |
std::sort(max_empirical_lambdas, &(max_empirical_lambdas[lasso_lambda_iters])); |
| 579 |
#else |
| 580 |
qsort(max_empirical_lambdas, lasso_lambda_iters, sizeof(double), double_cmp); |
| 581 |
#endif |
| 582 |
|
| 583 |
double lambda_min = get_dmedian(max_empirical_lambdas, lasso_lambda_iters); |
| 584 |
putc_unlocked('\r', stdout); |
| 585 |
LOGPRINTF("--lasso-lambda (%u iteration%s): min lambda = %g.\n", lasso_lambda_iters, (lasso_lambda_iters == 1)? "" : "s", lambda_min); |
| 586 |
*lasso_minlambda_ptr = lambda_min; |
| 587 |
memcpy(outname_end, ".lambdamin", 11); |
| 588 |
if (fopen_checked(outname, "w", &outfile)) { |
| 589 |
goto lasso_lambda_ret_OPEN_FAIL; |
| 590 |
} |
| 591 |
char* wptr = g_textbuf; |
| 592 |
char* wptr_flush = &(wptr[MAXLINELEN]); |
| 593 |
for (uint32_t iter_idx = 0; iter_idx < lasso_lambda_iters; ++iter_idx) { |
| 594 |
wptr = dtoa_g(max_empirical_lambdas[iter_idx], wptr); |
| 595 |
*wptr++ = '\n'; |
| 596 |
if (wptr >= wptr_flush) { |
| 597 |
if (fwrite_checked(g_textbuf, wptr - g_textbuf, outfile)) { |
| 598 |
goto lasso_lambda_ret_WRITE_FAIL; |
| 599 |
} |
| 600 |
wptr = g_textbuf; |
| 601 |
} |
| 602 |
} |
| 603 |
if (wptr != wptr_flush) { |
| 604 |
if (fwrite_checked(g_textbuf, wptr - g_textbuf, outfile)) { |
| 605 |
goto lasso_lambda_ret_WRITE_FAIL; |
| 606 |
} |
| 607 |
} |
| 608 |
if (fclose_null(&outfile)) { |
| 609 |
goto lasso_lambda_ret_WRITE_FAIL; |
| 610 |
} |
| 611 |
LOGPRINTFWW("Lambda distribution written to %s .\n", outname); |
| 612 |
} |
| 613 |
while (0) { |
| 614 |
lasso_lambda_ret_NOMEM: |
| 615 |
retval = RET_NOMEM; |
| 616 |
break; |
| 617 |
lasso_lambda_ret_OPEN_FAIL: |
| 618 |
retval = RET_OPEN_FAIL; |
| 619 |
break; |
| 620 |
lasso_lambda_ret_READ_FAIL: |
| 621 |
retval = RET_READ_FAIL; |
| 622 |
break; |
| 623 |
lasso_lambda_ret_WRITE_FAIL: |
| 624 |
retval = RET_WRITE_FAIL; |
| 625 |
break; |
| 626 |
} |
| 627 |
fclose_cond(outfile); |
| 628 |
bigstack_reset(bigstack_mark); |
| 629 |
return retval; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
int32_t lasso_smallmem(pthread_t* threads, FILE* bedfile, uintptr_t bed_offset, uintptr_t* marker_exclude, uintptr_t marker_ct, uintptr_t* marker_reverse, Chrom_info* chrom_info_ptr, uintptr_t unfiltered_sample_ct, const uintptr_t* sample_exclude, uintptr_t* pheno_nm2, double lasso_h2, double lasso_minlambda, uint32_t select_covars, uintptr_t* select_covars_bitfield, double* pheno_d_collapsed, uintptr_t covar_ct, char* covar_names, uintptr_t max_covar_name_len, uintptr_t* covar_nm, double* covar_d, uint32_t hh_or_mt_exists, uint32_t sample_ct, uintptr_t sample_valid_ct, uintptr_t* sample_include2, uintptr_t* sample_male_include2, uintptr_t* loadbuf_raw, uintptr_t* loadbuf_collapsed, double* rand_matrix, double* misc_arr, double* residuals, uintptr_t* polymorphic_markers, uintptr_t* polymorphic_marker_ct_ptr, uint64_t* iter_tot_ptr, double** xhat_ptr) { |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
uintptr_t unfiltered_sample_ct4 = (unfiltered_sample_ct + 3) / 4; |
| 640 |
double* covar_data_arr = nullptr; |
| 641 |
double sqrt_n_recip = sqrt(1.0 / ((double)((intptr_t)sample_valid_ct))); |
| 642 |
double lambda_max = 0.0; |
| 643 |
double err_cur = 0.0; |
| 644 |
double lambda_min = lasso_minlambda; |
| 645 |
uint64_t iter_tot = 0; |
| 646 |
uintptr_t unselected_covar_ct = 0; |
| 647 |
uintptr_t final_mask = get_final_mask(sample_valid_ct); |
| 648 |
uintptr_t polymorphic_marker_ct = 0; |
| 649 |
uint32_t chrom_fo_idx = 0xffffffffU; |
| 650 |
uint32_t chrom_end = 0; |
| 651 |
uint32_t is_x = 0; |
| 652 |
uint32_t is_y = 0; |
| 653 |
uint32_t min_ploidy_1 = 0; |
| 654 |
uint32_t partial_marker_idx = 0; |
| 655 |
int32_t retval = 0; |
| 656 |
double* prod_matrix = nullptr; |
| 657 |
double* data_window; |
| 658 |
double* xhat; |
| 659 |
double* dptr; |
| 660 |
double* dptr2; |
| 661 |
uintptr_t* active_set; |
| 662 |
double sige; |
| 663 |
double loghi; |
| 664 |
double loglo; |
| 665 |
double logdelta; |
| 666 |
double lambda; |
| 667 |
double xjold; |
| 668 |
double err_last; |
| 669 |
double dxx; |
| 670 |
double dyy; |
| 671 |
double zz; |
| 672 |
uintptr_t ulii; |
| 673 |
uintptr_t iter; |
| 674 |
uintptr_t col_ct; |
| 675 |
uintptr_t col_ctl; |
| 676 |
uintptr_t col_idx; |
| 677 |
uintptr_t col_nz_ct; |
| 678 |
uintptr_t col_to_z; |
| 679 |
uintptr_t col_uidx; |
| 680 |
uintptr_t covar_idx; |
| 681 |
uintptr_t sample_idx; |
| 682 |
uintptr_t marker_idx; |
| 683 |
uint32_t lambi; |
| 684 |
uint32_t marker_uidx; |
| 685 |
uint32_t last_marker_uidx; |
| 686 |
uint32_t uii; |
| 687 |
|
| 688 |
if (fseeko(bedfile, bed_offset, SEEK_SET)) { |
| 689 |
goto lasso_smallmem_ret_READ_FAIL; |
| 690 |
} |
| 691 |
fputs("Using memory-conserving LASSO implementation.\n", stdout); |
| 692 |
if (covar_ct) { |
| 693 |
if (bigstack_alloc_d(covar_ct * sample_valid_ct, &covar_data_arr)) { |
| 694 |
goto lasso_smallmem_ret_NOMEM; |
| 695 |
} |
| 696 |
dxx = 1.0 / ((double)((intptr_t)sample_valid_ct)); |
| 697 |
dyy = (double)((intptr_t)(sample_valid_ct - 1)); |
| 698 |
if (!select_covars_bitfield) { |
| 699 |
for (covar_idx = 0; covar_idx < covar_ct; covar_idx++) { |
| 700 |
if (transpose_covar(sample_exclude, pheno_nm2, covar_nm, &(covar_d[covar_idx]), sample_ct, sample_valid_ct, covar_ct, sqrt_n_recip, dxx, dyy, &(covar_data_arr[covar_idx * sample_valid_ct]))) { |
| 701 |
goto lasso_smallmem_ret_CONST_COVAR; |
| 702 |
} |
| 703 |
} |
| 704 |
if (!select_covars) { |
| 705 |
unselected_covar_ct = covar_ct; |
| 706 |
} |
| 707 |
} else { |
| 708 |
ulii = 0; |
| 709 |
for (covar_idx = 0; covar_idx < covar_ct; covar_idx++) { |
| 710 |
if (IS_SET(select_covars_bitfield, covar_idx)) { |
| 711 |
continue; |
| 712 |
} |
| 713 |
if (transpose_covar(sample_exclude, pheno_nm2, covar_nm, &(covar_d[covar_idx]), sample_ct, sample_valid_ct, covar_ct, sqrt_n_recip, dxx, dyy, &(covar_data_arr[ulii * sample_valid_ct]))) { |
| 714 |
goto lasso_smallmem_ret_CONST_COVAR; |
| 715 |
} |
| 716 |
ulii++; |
| 717 |
} |
| 718 |
unselected_covar_ct = ulii; |
| 719 |
for (covar_idx = 0; covar_idx < covar_ct; covar_idx++) { |
| 720 |
if (!IS_SET(select_covars_bitfield, covar_idx)) { |
| 721 |
continue; |
| 722 |
} |
| 723 |
if (transpose_covar(sample_exclude, pheno_nm2, covar_nm, &(covar_d[covar_idx]), sample_ct, sample_valid_ct, covar_ct, sqrt_n_recip, dxx, dyy, &(covar_data_arr[ulii * sample_valid_ct]))) { |
| 724 |
goto lasso_smallmem_ret_CONST_COVAR; |
| 725 |
} |
| 726 |
ulii++; |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
sige = sqrt(1.0 - lasso_h2 + 1.0 / ((double)((intptr_t)sample_valid_ct))); |
| 731 |
zz = sige * sqrt_n_recip; |
| 732 |
|
| 733 |
|
| 734 |
if (bigstack_alloc_d(covar_ct + marker_ct, &xhat)) { |
| 735 |
goto lasso_smallmem_ret_NOMEM; |
| 736 |
} |
| 737 |
if (rand_matrix) { |
| 738 |
if (bigstack_alloc_d(sample_valid_ct * DEFAULT_WARM_START_ITERS, &data_window) || |
| 739 |
bigstack_alloc_d(DEFAULT_WARM_START_ITERS * DEFAULT_WARM_START_ITERS, &prod_matrix)) { |
| 740 |
goto lasso_smallmem_ret_NOMEM; |
| 741 |
} |
| 742 |
fputs("\r--lasso: Initializing warm start matrix...", stdout); |
| 743 |
fflush(stdout); |
| 744 |
fill_double_zero(DEFAULT_WARM_START_ITERS, misc_arr); |
| 745 |
} else { |
| 746 |
if (bigstack_alloc_d(sample_valid_ct, &data_window)) { |
| 747 |
goto lasso_smallmem_ret_NOMEM; |
| 748 |
} |
| 749 |
} |
| 750 |
dptr = data_window; |
| 751 |
for (marker_uidx = 0, marker_idx = 0; marker_idx < marker_ct; marker_uidx++, marker_idx++) { |
| 752 |
|
| 753 |
|
| 754 |
if (IS_SET(marker_exclude, marker_uidx)) { |
| 755 |
marker_uidx = next_unset_unsafe(marker_exclude, marker_uidx); |
| 756 |
if (fseeko(bedfile, bed_offset + ((uint64_t)marker_uidx) * unfiltered_sample_ct4, SEEK_SET)) { |
| 757 |
goto lasso_smallmem_ret_READ_FAIL; |
| 758 |
} |
| 759 |
} |
| 760 |
if (marker_uidx >= chrom_end) { |
| 761 |
chrom_fo_idx++; |
| 762 |
refresh_chrom_info(chrom_info_ptr, marker_uidx, &chrom_end, &chrom_fo_idx, &is_x, &is_y, &uii, &min_ploidy_1); |
| 763 |
} |
| 764 |
uii = load_and_normalize(bedfile, loadbuf_raw, unfiltered_sample_ct, loadbuf_collapsed, sample_valid_ct, pheno_nm2, final_mask, IS_SET(marker_reverse, marker_uidx), min_ploidy_1, hh_or_mt_exists, sample_include2, sample_male_include2, is_x, is_y, sqrt_n_recip, dptr); |
| 765 |
if (uii == 2) { |
| 766 |
goto lasso_smallmem_ret_READ_FAIL; |
| 767 |
} |
| 768 |
if (uii == 1) { |
| 769 |
continue; |
| 770 |
} |
| 771 |
SET_BIT(marker_uidx, polymorphic_markers); |
| 772 |
dxx = 0.0; |
| 773 |
for (sample_idx = 0; sample_idx < sample_valid_ct; sample_idx++) { |
| 774 |
dxx += dptr[sample_idx] * pheno_d_collapsed[sample_idx]; |
| 775 |
} |
| 776 |
xhat[covar_ct + polymorphic_marker_ct + partial_marker_idx] = dxx; |
| 777 |
partial_marker_idx++; |
| 778 |
dxx = fabs(dxx); |
| 779 |
if (dxx > lambda_max) { |
| 780 |
lambda_max = dxx; |
| 781 |
} |
| 782 |
if (partial_marker_idx == DEFAULT_WARM_START_ITERS) { |
| 783 |
if (rand_matrix) { |
| 784 |
col_major_matrix_multiply(DEFAULT_WARM_START_ITERS, DEFAULT_WARM_START_ITERS, sample_valid_ct, rand_matrix, data_window, prod_matrix); |
| 785 |
dptr = prod_matrix; |
| 786 |
for (col_idx = 0; col_idx < DEFAULT_WARM_START_ITERS; col_idx++) { |
| 787 |
for (ulii = 0; ulii < DEFAULT_WARM_START_ITERS; ulii++) { |
| 788 |
dxx = fabs(*dptr++); |
| 789 |
if (dxx > misc_arr[ulii]) { |
| 790 |
misc_arr[ulii] = dxx; |
| 791 |
} |
| 792 |
} |
| 793 |
} |
| 794 |
} |
| 795 |
polymorphic_marker_ct += DEFAULT_WARM_START_ITERS; |
| 796 |
partial_marker_idx = 0; |
| 797 |
dptr = data_window; |
| 798 |
} else if (!rand_matrix) { |
| 799 |
dptr = data_window; |
| 800 |
} else { |
| 801 |
dptr = &(dptr[sample_valid_ct]); |
| 802 |
} |
| 803 |
} |
| 804 |
if (!(polymorphic_marker_ct + partial_marker_idx)) { |
| 805 |
logerrprint("Warning: Skipping --lasso since no polymorphic markers are present.\n"); |
| 806 |
return 0; |
| 807 |
} |
| 808 |
if (rand_matrix) { |
| 809 |
if (partial_marker_idx) { |
| 810 |
col_major_matrix_multiply(DEFAULT_WARM_START_ITERS, partial_marker_idx, sample_valid_ct, rand_matrix, data_window, prod_matrix); |
| 811 |
dptr = prod_matrix; |
| 812 |
for (col_idx = 0; col_idx < partial_marker_idx; col_idx++) { |
| 813 |
for (ulii = 0; ulii < DEFAULT_WARM_START_ITERS; ulii++) { |
| 814 |
dxx = fabs(*dptr++); |
| 815 |
if (dxx > misc_arr[ulii]) { |
| 816 |
misc_arr[ulii] = dxx; |
| 817 |
} |
| 818 |
} |
| 819 |
} |
| 820 |
} |
| 821 |
lambda_min = destructive_get_dmedian(DEFAULT_WARM_START_ITERS, misc_arr) * zz; |
| 822 |
logstr("--lasso:"); |
| 823 |
LOGPRINTF(" using min lambda = %g.\n", lambda_min); |
| 824 |
} |
| 825 |
polymorphic_marker_ct += partial_marker_idx; |
| 826 |
*polymorphic_marker_ct_ptr = polymorphic_marker_ct; |
| 827 |
col_ct = covar_ct + polymorphic_marker_ct; |
| 828 |
bigstack_reset(data_window); |
| 829 |
bigstack_shrink_top(xhat, col_ct * sizeof(double)); |
| 830 |
col_ctl = BITCT_TO_WORDCT(col_ct); |
| 831 |
*xhat_ptr = xhat; |
| 832 |
if (lambda_min >= lambda_max) { |
| 833 |
logprint("\n"); |
| 834 |
logerrprint("Error: min lambda >= max lambda.\n"); |
| 835 |
goto lasso_smallmem_ret_INVALID_CMDLINE; |
| 836 |
} |
| 837 |
if (bigstack_alloc_ul(col_ctl, &active_set) || |
| 838 |
bigstack_alloc_d(sample_valid_ct, &data_window)) { |
| 839 |
goto lasso_smallmem_ret_NOMEM; |
| 840 |
} |
| 841 |
loghi = log(lambda_max); |
| 842 |
loglo = log(lambda_min); |
| 843 |
logdelta = (loghi - loglo) / (NLAMBDA - 1.0); |
| 844 |
fputs("\r--lasso: Executing coordinate descent... ", stdout); |
| 845 |
for (lambi = 0; lambi < NLAMBDA; lambi++) { |
| 846 |
if (lambi > 10) { |
| 847 |
fputs("\b\b\b", stdout); |
| 848 |
} else if (lambi) { |
| 849 |
fputs("\b\b", stdout); |
| 850 |
} |
| 851 |
printf("%u%%", lambi); |
| 852 |
fflush(stdout); |
| 853 |
lambda = exp(loghi - logdelta * ((double)((int32_t)lambi))); |
| 854 |
memcpy(residuals, pheno_d_collapsed, sample_valid_ct * sizeof(double)); |
| 855 |
for (col_idx = 0; col_idx < covar_ct; col_idx++) { |
| 856 |
dptr = residuals; |
| 857 |
dptr2 = &(covar_data_arr[col_idx * sample_valid_ct]); |
| 858 |
dxx = -xhat[col_idx]; |
| 859 |
for (sample_idx = 0; sample_idx < sample_valid_ct; sample_idx++) { |
| 860 |
*dptr += (*dptr2++) * dxx; |
| 861 |
dptr++; |
| 862 |
} |
| 863 |
} |
| 864 |
if (fseeko(bedfile, bed_offset, SEEK_SET)) { |
| 865 |
goto lasso_smallmem_ret_READ_FAIL; |
| 866 |
} |
| 867 |
for (marker_uidx = 0, marker_idx = 0, chrom_end = 0, chrom_fo_idx = 0xffffffffU; marker_idx < polymorphic_marker_ct; marker_uidx++, marker_idx++) { |
| 868 |
if (!IS_SET(polymorphic_markers, marker_uidx)) { |
| 869 |
marker_uidx = next_set_unsafe(polymorphic_markers, marker_uidx); |
| 870 |
if (fseeko(bedfile, bed_offset + ((uint64_t)marker_uidx) * unfiltered_sample_ct4, SEEK_SET)) { |
| 871 |
goto lasso_smallmem_ret_READ_FAIL; |
| 872 |
} |
| 873 |
} |
| 874 |
if (marker_uidx >= chrom_end) { |
| 875 |
chrom_fo_idx++; |
| 876 |
refresh_chrom_info(chrom_info_ptr, marker_uidx, &chrom_end, &chrom_fo_idx, &is_x, &is_y, &uii, &min_ploidy_1); |
| 877 |
} |
| 878 |
if (load_and_normalize(bedfile, loadbuf_raw, unfiltered_sample_ct, loadbuf_collapsed, sample_valid_ct, pheno_nm2, final_mask, IS_SET(marker_reverse, marker_uidx), min_ploidy_1, hh_or_mt_exists, sample_include2, sample_male_include2, is_x, is_y, sqrt_n_recip, data_window)) { |
| 879 |
goto lasso_smallmem_ret_READ_FAIL; |
| 880 |
} |
| 881 |
dptr = residuals; |
| 882 |
dptr2 = data_window; |
| 883 |
dxx = -xhat[marker_idx + covar_ct]; |
| 884 |
for (sample_idx = 0; sample_idx < sample_valid_ct; sample_idx++) { |
| 885 |
*dptr += (*dptr2++) * dxx; |
| 886 |
dptr++; |
| 887 |
} |
| 888 |
} |
| 889 |
iter = 0; |
| 890 |
fill_all_bits(col_ct, active_set); |
| 891 |
col_nz_ct = col_ct; |
| 892 |
while (1) { |
| 893 |
col_uidx = 0; |
| 894 |
col_to_z = 0; |
| 895 |
for (marker_uidx = 0, last_marker_uidx = 0xfffffffeU, chrom_end = 0, chrom_fo_idx = 0xffffffffU, marker_idx = 0; marker_idx < col_nz_ct; col_uidx++) { |
| 896 |
if (col_uidx < covar_ct) { |
| 897 |
if (!(IS_SET(active_set, col_uidx))) { |
| 898 |
continue; |
| 899 |
} |
| 900 |
dptr = &(covar_data_arr[col_uidx * sample_valid_ct]); |
| 901 |
} else { |
| 902 |
marker_uidx = next_set_unsafe(polymorphic_markers, marker_uidx); |
| 903 |
if (!(IS_SET(active_set, col_uidx))) { |
| 904 |
marker_uidx++; |
| 905 |
continue; |
| 906 |
} |
| 907 |
if (marker_uidx != last_marker_uidx + 1) { |
| 908 |
if (fseeko(bedfile, bed_offset + ((uint64_t)marker_uidx) * unfiltered_sample_ct4, SEEK_SET)) { |
| 909 |
goto lasso_smallmem_ret_READ_FAIL; |
| 910 |
} |
| 911 |
} |
| 912 |
if (marker_uidx >= chrom_end) { |
| 913 |
chrom_fo_idx++; |
| 914 |
refresh_chrom_info(chrom_info_ptr, marker_uidx, &chrom_end, &chrom_fo_idx, &is_x, &is_y, &uii, &min_ploidy_1); |
| 915 |
} |
| 916 |
if (load_and_normalize(bedfile, loadbuf_raw, unfiltered_sample_ct, loadbuf_collapsed, sample_valid_ct, pheno_nm2, final_mask, IS_SET(marker_reverse, marker_uidx), min_ploidy_1, hh_or_mt_exists, sample_include2, sample_male_include2, is_x, is_y, sqrt_n_recip, data_window)) { |
| 917 |
goto lasso_smallmem_ret_READ_FAIL; |
| 918 |
} |
| 919 |
dptr = data_window; |
| 920 |
} |
| 921 |
marker_idx++; |
| 922 |
xjold = xhat[col_uidx]; |
| 923 |
dxx = xjold; |
| 924 |
dptr2 = residuals; |
| 925 |
for (sample_idx = 0; sample_idx < sample_valid_ct; sample_idx++) { |
| 926 |
dxx += (*dptr++) * (*dptr2++); |
| 927 |
} |
| 928 |
if (col_uidx >= unselected_covar_ct) { |
| 929 |
if (dxx > 0.0) { |
| 930 |
dxx = MAXV(dxx - lambda, 0.0); |
| 931 |
} else { |
| 932 |
dxx = MINV(dxx + lambda, 0.0); |
| 933 |
} |
| 934 |
} |
| 935 |
xhat[col_uidx] = dxx; |
| 936 |
if (dxx == 0.0) { |
| 937 |
CLEAR_BIT(col_uidx, active_set); |
| 938 |
col_to_z++; |
| 939 |
} |
| 940 |
dptr = residuals; |
| 941 |
if (col_uidx < covar_ct) { |
| 942 |
dptr2 = &(covar_data_arr[col_uidx * sample_valid_ct]); |
| 943 |
} else { |
| 944 |
dptr2 = data_window; |
| 945 |
last_marker_uidx = marker_uidx; |
| 946 |
marker_uidx++; |
| 947 |
} |
| 948 |
dxx -= xjold; |
| 949 |
for (sample_idx = 0; sample_idx < sample_valid_ct; sample_idx++) { |
| 950 |
*dptr -= (*dptr2++) * dxx; |
| 951 |
dptr++; |
| 952 |
} |
| 953 |
} |
| 954 |
col_nz_ct -= col_to_z; |
| 955 |
err_last = err_cur; |
| 956 |
err_cur = 0.0; |
| 957 |
for (col_uidx = 0, marker_idx = 0; marker_idx < col_nz_ct; marker_idx++, col_uidx++) { |
| 958 |
col_uidx = next_set_unsafe(active_set, col_uidx); |
| 959 |
err_cur += fabs(xhat[col_uidx]); |
| 960 |
} |
| 961 |
err_cur *= lambda; |
| 962 |
dptr = residuals; |
| 963 |
for (sample_idx = 0; sample_idx < sample_valid_ct; sample_idx++) { |
| 964 |
dxx = *dptr++; |
| 965 |
err_cur += dxx * dxx; |
| 966 |
} |
| 967 |
if (iter++) { |
| 968 |
if (((1.0 - (MINV(err_last, err_cur) / MAXV(err_last, err_cur))) < DELTA_THRESHOLD) || (err_cur != err_cur)) { |
| 969 |
iter_tot += iter; |
| 970 |
break; |
| 971 |
} |
| 972 |
} |
| 973 |
} |
| 974 |
} |
| 975 |
*iter_tot_ptr = iter_tot; |
| 976 |
while (0) { |
| 977 |
lasso_smallmem_ret_NOMEM: |
| 978 |
retval = RET_NOMEM; |
| 979 |
break; |
| 980 |
lasso_smallmem_ret_READ_FAIL: |
| 981 |
retval = RET_READ_FAIL; |
| 982 |
break; |
| 983 |
lasso_smallmem_ret_CONST_COVAR: |
| 984 |
logerrprint("Error: --lasso covariate is constant.\n"); |
| 985 |
lasso_smallmem_ret_INVALID_CMDLINE: |
| 986 |
retval = RET_INVALID_CMDLINE; |
| 987 |
break; |
| 988 |
} |
| 989 |
return retval; |
| 990 |
} |
| 991 |
|
| 992 |
int32_t lasso(pthread_t* threads, FILE* bedfile, uintptr_t bed_offset, char* outname, char* outname_end, uintptr_t unfiltered_marker_ct, uintptr_t* marker_exclude, uintptr_t marker_ct, char* marker_ids, uintptr_t max_marker_id_len, char** marker_allele_ptrs, uintptr_t* marker_reverse, Chrom_info* chrom_info_ptr, uintptr_t unfiltered_sample_ct, uint32_t sample_ct, uintptr_t pheno_nm_ct, double lasso_h2, double lasso_minlambda, Range_list* select_covars_range_list_ptr, uint64_t misc_flags, const uintptr_t* sample_exclude, uintptr_t* pheno_nm, uintptr_t* pheno_c, double* pheno_d, uintptr_t covar_ct, char* covar_names, uintptr_t max_covar_name_len, uintptr_t* covar_nm, double* covar_d, uintptr_t* sex_male, uint32_t hh_or_mt_exists) { |
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 998 |
FILE* outfile = nullptr; |
| 999 |
const uintptr_t unfiltered_sample_ctl = BITCT_TO_WORDCT(unfiltered_sample_ct); |
| 1000 |
const uintptr_t unfiltered_sample_ctv2 = QUATERCT_TO_ALIGNED_WORDCT(unfiltered_sample_ct); |
| 1001 |
const uintptr_t unfiltered_marker_ctl = BITCT_TO_WORDCT(unfiltered_marker_ct); |
| 1002 |
uintptr_t polymorphic_marker_ct = 0; |
| 1003 |
uint64_t iter_tot = 0; |
| 1004 |
double* xhat = nullptr; |
| 1005 |
double* rand_matrix = nullptr; |
| 1006 |
double* misc_arr = nullptr; |
| 1007 |
uintptr_t* sample_male_include2 = nullptr; |
| 1008 |
uintptr_t* select_covars_bitfield = nullptr; |
| 1009 |
char* wptr_start = nullptr; |
| 1010 |
const uint32_t report_zeroes = (misc_flags / MISC_LASSO_REPORT_ZEROES) & 1; |
| 1011 |
const uint32_t select_covars = (misc_flags / MISC_LASSO_SELECT_COVARS) & 1; |
| 1012 |
uint32_t chrom_fo_idx = 0xffffffffU; |
| 1013 |
uint32_t chrom_end = 0; |
| 1014 |
uint32_t is_x = 0; |
| 1015 |
uint32_t is_y = 0; |
| 1016 |
uint32_t min_ploidy_1 = 0; |
| 1017 |
int32_t retval = 0; |
| 1018 |
double* pheno_d_collapsed; |
| 1019 |
double* residuals; |
| 1020 |
double* dptr; |
| 1021 |
double* dptr2; |
| 1022 |
double* dptr3; |
| 1023 |
uintptr_t* pheno_nm2; |
| 1024 |
uintptr_t* sample_include2; |
| 1025 |
uintptr_t* loadbuf_raw; |
| 1026 |
uintptr_t* loadbuf_collapsed; |
| 1027 |
uintptr_t* polymorphic_markers; |
| 1028 |
char* wptr; |
| 1029 |
char* wptr2; |
| 1030 |
double sqrt_n_recip; |
| 1031 |
double dxx; |
| 1032 |
double dyy; |
| 1033 |
double dzz; |
| 1034 |
uint64_t ullii; |
| 1035 |
uint64_t ulljj; |
| 1036 |
uintptr_t sample_valid_ct; |
| 1037 |
uintptr_t sample_valid_ctv2; |
| 1038 |
uintptr_t marker_idx; |
| 1039 |
uintptr_t marker_idx2; |
| 1040 |
uintptr_t ulii; |
| 1041 |
uintptr_t uljj; |
| 1042 |
uintptr_t sample_uidx; |
| 1043 |
uintptr_t sample_uidx_stop; |
| 1044 |
uintptr_t sample_idx; |
| 1045 |
uint32_t marker_uidx; |
| 1046 |
uint32_t uii; |
| 1047 |
if (!covar_ct) { |
| 1048 |
sample_valid_ct = pheno_nm_ct; |
| 1049 |
pheno_nm2 = pheno_nm; |
| 1050 |
} else { |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
if (bigstack_calloc_ul(unfiltered_sample_ctl, &pheno_nm2)) { |
| 1056 |
goto lasso_ret_NOMEM; |
| 1057 |
} |
| 1058 |
for (sample_uidx = 0, sample_idx = 0; sample_idx < sample_ct; sample_uidx++, sample_idx++) { |
| 1059 |
next_unset_ul_unsafe_ck(sample_exclude, &sample_uidx); |
| 1060 |
if (IS_SET(pheno_nm, sample_uidx) && IS_SET(covar_nm, sample_idx)) { |
| 1061 |
SET_BIT(sample_uidx, pheno_nm2); |
| 1062 |
} |
| 1063 |
} |
| 1064 |
sample_valid_ct = popcount_longs(pheno_nm2, unfiltered_sample_ctl); |
| 1065 |
} |
| 1066 |
if (sample_valid_ct < 2) { |
| 1067 |
if (pheno_nm_ct < 2) { |
| 1068 |
logerrprint("Warning: Skipping --lasso since less than two phenotypes are present.\n"); |
| 1069 |
} else { |
| 1070 |
logerrprint("Warning: Skipping --lasso since too many samples have missing covariates.\n"); |
| 1071 |
} |
| 1072 |
goto lasso_ret_1; |
| 1073 |
} |
| 1074 |
sample_valid_ctv2 = QUATERCT_TO_ALIGNED_WORDCT(sample_valid_ct); |
| 1075 |
sqrt_n_recip = sqrt(1.0 / ((double)((intptr_t)sample_valid_ct))); |
| 1076 |
if (bigstack_alloc_ul(sample_valid_ctv2, &sample_include2) || |
| 1077 |
bigstack_alloc_ul(unfiltered_sample_ctv2, &loadbuf_raw) || |
| 1078 |
bigstack_alloc_ul(sample_valid_ctv2, &loadbuf_collapsed) || |
| 1079 |
bigstack_alloc_ul(unfiltered_marker_ctl, &polymorphic_markers) || |
| 1080 |
bigstack_alloc_d(sample_valid_ct, &pheno_d_collapsed) || |
| 1081 |
bigstack_alloc_d(sample_valid_ct, &residuals)) { |
| 1082 |
goto lasso_ret_NOMEM; |
| 1083 |
} |
| 1084 |
if (lasso_minlambda == -1) { |
| 1085 |
if (bigstack_alloc_d(sample_valid_ct * DEFAULT_WARM_START_ITERS, &rand_matrix) || |
| 1086 |
bigstack_alloc_d(DEFAULT_WARM_START_ITERS, &misc_arr)) { |
| 1087 |
goto lasso_ret_NOMEM; |
| 1088 |
} |
| 1089 |
} |
| 1090 |
dxx = 0; |
| 1091 |
dyy = 0; |
| 1092 |
dptr = pheno_d_collapsed; |
| 1093 |
sample_uidx = 0; |
| 1094 |
sample_idx = 0; |
| 1095 |
if (pheno_d) { |
| 1096 |
do { |
| 1097 |
sample_uidx = next_set_ul_unsafe(pheno_nm2, sample_uidx); |
| 1098 |
sample_uidx_stop = next_unset_ul(pheno_nm2, sample_uidx, unfiltered_sample_ct); |
| 1099 |
sample_idx += sample_uidx_stop - sample_uidx; |
| 1100 |
dptr2 = &(pheno_d[sample_uidx]); |
| 1101 |
sample_uidx = sample_uidx_stop; |
| 1102 |
dptr3 = &(pheno_d[sample_uidx_stop]); |
| 1103 |
do { |
| 1104 |
dzz = *dptr2++; |
| 1105 |
*dptr++ = dzz; |
| 1106 |
dxx += dzz; |
| 1107 |
dyy += dzz * dzz; |
| 1108 |
} while (dptr2 < dptr3); |
| 1109 |
} while (sample_idx < sample_valid_ct); |
| 1110 |
} else { |
| 1111 |
do { |
| 1112 |
sample_uidx = next_set_ul_unsafe(pheno_nm2, sample_uidx); |
| 1113 |
sample_uidx_stop = next_unset_ul(pheno_nm2, sample_uidx, unfiltered_sample_ct); |
| 1114 |
sample_idx += sample_uidx_stop - sample_uidx; |
| 1115 |
do { |
| 1116 |
dzz = ((int32_t)IS_SET(pheno_c, sample_uidx)); |
| 1117 |
*dptr++ = dzz; |
| 1118 |
dxx += dzz; |
| 1119 |
} while ((++sample_uidx) < sample_uidx_stop); |
| 1120 |
} while (sample_idx < sample_valid_ct); |
| 1121 |
dyy = dxx; |
| 1122 |
} |
| 1123 |
if (dyy * ((double)((intptr_t)sample_valid_ct)) == dxx * dxx) { |
| 1124 |
logerrprint("Warning: Skipping --lasso since phenotype is constant.\n"); |
| 1125 |
goto lasso_ret_1; |
| 1126 |
} |
| 1127 |
dzz = dxx / ((double)((intptr_t)sample_valid_ct)); |
| 1128 |
dyy = sqrt_n_recip * sqrt(((double)((intptr_t)(sample_valid_ct - 1))) / (dyy - dxx * dzz)); |
| 1129 |
dptr = pheno_d_collapsed; |
| 1130 |
for (sample_idx = 0; sample_idx < sample_valid_ct; sample_idx++) { |
| 1131 |
*dptr = ((*dptr) - dzz) * dyy; |
| 1132 |
dptr++; |
| 1133 |
} |
| 1134 |
fill_quatervec_55(sample_valid_ct, sample_include2); |
| 1135 |
fill_ulong_zero(unfiltered_marker_ctl, polymorphic_markers); |
| 1136 |
if ((chrom_info_ptr->xymt_codes[MT_OFFSET] != -2) && is_set(chrom_info_ptr->chrom_mask, chrom_info_ptr->xymt_codes[MT_OFFSET])) { |
| 1137 |
hh_or_mt_exists |= NXMHH_EXISTS; |
| 1138 |
} |
| 1139 |
if (alloc_collapsed_haploid_filters(pheno_nm2, sex_male, unfiltered_sample_ct, sample_valid_ct, hh_or_mt_exists, 1, &sample_include2, &sample_male_include2)) { |
| 1140 |
goto lasso_ret_NOMEM; |
| 1141 |
} |
| 1142 |
if (select_covars && select_covars_range_list_ptr->name_ct) { |
| 1143 |
if (!covar_ct) { |
| 1144 |
logerrprint("Error: No covariates loaded for --lasso-select-covars.\n"); |
| 1145 |
goto lasso_ret_INVALID_CMDLINE; |
| 1146 |
} |
| 1147 |
retval = string_range_list_to_bitarr_alloc(covar_names, covar_ct, max_covar_name_len, select_covars_range_list_ptr, "lasso-select-covars", "--covar file", &select_covars_bitfield); |
| 1148 |
if (retval) { |
| 1149 |
goto lasso_ret_1; |
| 1150 |
} |
| 1151 |
} |
| 1152 |
uii = marker_ct + covar_ct; |
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
ullii = round_up_pow2(((uint64_t)uii) * sizeof(double), CACHELINE) + round_up_pow2((uii + 7) / 8, CACHELINE); |
| 1164 |
|
| 1165 |
if (rand_matrix) { |
| 1166 |
uljj = (sample_valid_ct * DEFAULT_WARM_START_ITERS) - 1; |
| 1167 |
for (ulii = 0; ulii < uljj; ulii += 2) { |
| 1168 |
rand_matrix[ulii] = rand_normal(&(rand_matrix[ulii + 1])); |
| 1169 |
} |
| 1170 |
ulljj = round_up_pow2(DEFAULT_WARM_START_ITERS * DEFAULT_WARM_START_ITERS * sizeof(double), CACHELINE); |
| 1171 |
if (ullii < ulljj) { |
| 1172 |
ullii = ulljj; |
| 1173 |
} |
| 1174 |
} |
| 1175 |
ullii += round_up_pow2(((uint64_t)uii) * sample_valid_ct * sizeof(double), CACHELINE); |
| 1176 |
if (ullii <= bigstack_left()) { |
| 1177 |
retval = lasso_bigmem(bedfile, bed_offset, marker_exclude, marker_ct, marker_reverse, chrom_info_ptr, unfiltered_sample_ct, sample_exclude, pheno_nm2, lasso_h2, lasso_minlambda, select_covars, select_covars_bitfield, pheno_d_collapsed, covar_ct, covar_names, max_covar_name_len, covar_nm, covar_d, hh_or_mt_exists, sample_ct, sample_valid_ct, sample_include2, sample_male_include2, loadbuf_raw, loadbuf_collapsed, rand_matrix, misc_arr, residuals, polymorphic_markers, &polymorphic_marker_ct, &iter_tot, &xhat); |
| 1178 |
} else { |
| 1179 |
retval = lasso_smallmem(threads, bedfile, bed_offset, marker_exclude, marker_ct, marker_reverse, chrom_info_ptr, unfiltered_sample_ct, sample_exclude, pheno_nm2, lasso_h2, lasso_minlambda, select_covars, select_covars_bitfield, pheno_d_collapsed, covar_ct, covar_names, max_covar_name_len, covar_nm, covar_d, hh_or_mt_exists, sample_ct, sample_valid_ct, sample_include2, sample_male_include2, loadbuf_raw, loadbuf_collapsed, rand_matrix, misc_arr, residuals, polymorphic_markers, &polymorphic_marker_ct, &iter_tot, &xhat); |
| 1180 |
} |
| 1181 |
if (retval || (!polymorphic_marker_ct)) { |
| 1182 |
goto lasso_ret_1; |
| 1183 |
} |
| 1184 |
memcpy(outname_end, ".lasso", 7); |
| 1185 |
if (fopen_checked(outname, "w", &outfile)) { |
| 1186 |
goto lasso_ret_OPEN_FAIL; |
| 1187 |
} |
| 1188 |
if (fputs_checked("CHR\tSNP\tA1\tEFFECT\n", outfile)) { |
| 1189 |
goto lasso_ret_WRITE_FAIL; |
| 1190 |
} |
| 1191 |
g_textbuf[MAXLINELEN] = '\t'; |
| 1192 |
if (select_covars) { |
| 1193 |
if (select_covars_bitfield) { |
| 1194 |
marker_idx = covar_ct - popcount_longs(select_covars_bitfield, BITCT_TO_WORDCT(covar_ct)); |
| 1195 |
} else { |
| 1196 |
marker_idx = 0; |
| 1197 |
} |
| 1198 |
for (marker_uidx = 0; marker_idx < covar_ct; marker_uidx++, marker_idx++) { |
| 1199 |
if (select_covars_bitfield) { |
| 1200 |
next_set_unsafe_ck(select_covars_bitfield, &marker_uidx); |
| 1201 |
} |
| 1202 |
dxx = xhat[marker_idx]; |
| 1203 |
if ((!report_zeroes) && (dxx == 0)) { |
| 1204 |
continue; |
| 1205 |
} |
| 1206 |
wptr = memcpya(g_textbuf, "COV\t", 4); |
| 1207 |
wptr = strcpyax(wptr, &(covar_names[marker_uidx * max_covar_name_len]), '\t'); |
| 1208 |
wptr = memcpyl3a(wptr, "NA\t"); |
| 1209 |
wptr = dtoa_gx(dxx, '\n', wptr); |
| 1210 |
if (fwrite_checked(g_textbuf, wptr - g_textbuf, outfile)) { |
| 1211 |
goto lasso_ret_WRITE_FAIL; |
| 1212 |
} |
| 1213 |
} |
| 1214 |
} |
| 1215 |
for (marker_uidx = 0, marker_idx = 0, marker_idx2 = 0; marker_idx < marker_ct; marker_uidx++, marker_idx++) { |
| 1216 |
next_unset_unsafe_ck(marker_exclude, &marker_uidx); |
| 1217 |
if (marker_uidx >= chrom_end) { |
| 1218 |
chrom_fo_idx++; |
| 1219 |
refresh_chrom_info(chrom_info_ptr, marker_uidx, &chrom_end, &chrom_fo_idx, &is_x, &is_y, &uii, &min_ploidy_1); |
| 1220 |
uii = chrom_info_ptr->chrom_file_order[chrom_fo_idx]; |
| 1221 |
wptr_start = chrom_name_write(chrom_info_ptr, uii, g_textbuf); |
| 1222 |
*wptr_start++ = '\t'; |
| 1223 |
} |
| 1224 |
wptr = strcpyax(wptr_start, &(marker_ids[marker_uidx * max_marker_id_len]), '\t'); |
| 1225 |
if (IS_SET(polymorphic_markers, marker_uidx)) { |
| 1226 |
dxx = xhat[covar_ct + (marker_idx2++)]; |
| 1227 |
if ((!report_zeroes) && (dxx == 0)) { |
| 1228 |
continue; |
| 1229 |
} |
| 1230 |
wptr2 = dtoa_gx(dxx, '\n', &(g_textbuf[MAXLINELEN + 1])); |
| 1231 |
} else { |
| 1232 |
if (!report_zeroes) { |
| 1233 |
continue; |
| 1234 |
} |
| 1235 |
wptr2 = memcpyl3a(&(g_textbuf[MAXLINELEN + 1]), "NA\n"); |
| 1236 |
} |
| 1237 |
if (fwrite_checked(g_textbuf, wptr - g_textbuf, outfile)) { |
| 1238 |
goto lasso_ret_WRITE_FAIL; |
| 1239 |
} |
| 1240 |
fputs(marker_allele_ptrs[2 * marker_uidx], outfile); |
| 1241 |
if (fwrite_checked(&(g_textbuf[MAXLINELEN]), (uintptr_t)(wptr2 - (&(g_textbuf[MAXLINELEN]))), outfile)) { |
| 1242 |
goto lasso_ret_WRITE_FAIL; |
| 1243 |
} |
| 1244 |
} |
| 1245 |
if (fclose_null(&outfile)) { |
| 1246 |
goto lasso_ret_WRITE_FAIL; |
| 1247 |
} |
| 1248 |
putc_unlocked('\r', stdout); |
| 1249 |
LOGPRINTFWW("--lasso report written to %s. Total iterations: %" PRIu64 ".\n", outname, iter_tot); |
| 1250 |
|
| 1251 |
while (0) { |
| 1252 |
lasso_ret_NOMEM: |
| 1253 |
retval = RET_NOMEM; |
| 1254 |
break; |
| 1255 |
lasso_ret_OPEN_FAIL: |
| 1256 |
retval = RET_OPEN_FAIL; |
| 1257 |
break; |
| 1258 |
lasso_ret_WRITE_FAIL: |
| 1259 |
retval = RET_WRITE_FAIL; |
| 1260 |
break; |
| 1261 |
lasso_ret_INVALID_CMDLINE: |
| 1262 |
retval = RET_INVALID_CMDLINE; |
| 1263 |
break; |
| 1264 |
} |
| 1265 |
lasso_ret_1: |
| 1266 |
bigstack_reset(bigstack_mark); |
| 1267 |
fclose_cond(outfile); |
| 1268 |
return retval; |
| 1269 |
} |