| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
#include "plink_common.h" |
| 91 |
|
| 92 |
#include "plink_calc.h" |
| 93 |
#include "plink_cluster.h" |
| 94 |
#include "plink_matrix.h" |
| 95 |
#include "plink_misc.h" |
| 96 |
#include "plink_stats.h" |
| 97 |
#include "pigz.h" |
| 98 |
|
| 99 |
|
| 100 |
#define JACKKNIFE_VALS_REL 5 |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
#define MULTIPLEX_DIST 960 |
| 110 |
#define MULTIPLEX_2DIST (MULTIPLEX_DIST * 2) |
| 111 |
|
| 112 |
|
| 113 |
#define GENOME_MULTIPLEX 1152 |
| 114 |
#define GENOME_MULTIPLEX2 (GENOME_MULTIPLEX * 2) |
| 115 |
|
| 116 |
#define MAX_EM_ACCEL 100.0 |
| 117 |
|
| 118 |
void rel_init(Rel_info* relip) { |
| 119 |
relip->modifier = 0; |
| 120 |
relip->regress_rel_iters = ITERS_DEFAULT; |
| 121 |
relip->regress_rel_d = 0; |
| 122 |
relip->unrelated_herit_tol = 0.0000001; |
| 123 |
relip->unrelated_herit_covg = 0.45; |
| 124 |
relip->unrelated_herit_covr = 0.55; |
| 125 |
relip->cutoff = 0.025; |
| 126 |
relip->ibc_type = 0; |
| 127 |
relip->pc_ct = 20; |
| 128 |
relip->pca_cluster_names_flattened = nullptr; |
| 129 |
relip->pca_clusters_fname = nullptr; |
| 130 |
} |
| 131 |
|
| 132 |
void rel_cleanup(Rel_info* relip) { |
| 133 |
free_cond(relip->pca_cluster_names_flattened); |
| 134 |
free_cond(relip->pca_clusters_fname); |
| 135 |
} |
| 136 |
|
| 137 |
void update_rel_ibc(double* rel_ibc, uintptr_t* geno, double* set_allele_freqs, double* main_weights, int32_t ibc_type, uint32_t sample_ct, uint32_t window_size) { |
| 138 |
|
| 139 |
uint32_t uii; |
| 140 |
uint32_t ujj; |
| 141 |
uint32_t ukk; |
| 142 |
uint32_t umm; |
| 143 |
double twt; |
| 144 |
double* wtptr; |
| 145 |
double mult = 1.0; |
| 146 |
uintptr_t ulii; |
| 147 |
double weights[BITCT * 12]; |
| 148 |
double* weights1 = &(weights[64]); |
| 149 |
double* weights2 = &(weights[128]); |
| 150 |
double* weights3 = &(weights[256]); |
| 151 |
double* weights4 = &(weights[320]); |
| 152 |
#ifdef __LP64__ |
| 153 |
double* weights5 = &(weights[384]); |
| 154 |
double* weights6 = &(weights[448]); |
| 155 |
double* weights7 = &(weights[512]); |
| 156 |
double* weights8 = &(weights[640]); |
| 157 |
double* weights9 = &(weights[704]); |
| 158 |
#endif |
| 159 |
double wtarr[BITCT2 * 5]; |
| 160 |
double *wptr = weights; |
| 161 |
fill_double_zero(BITCT2 * 5, wtarr); |
| 162 |
for (uii = 0; uii < window_size; uii++) { |
| 163 |
if ((set_allele_freqs[uii] != 0.0) && (set_allele_freqs[uii] < (1.0 - EPSILON))) { |
| 164 |
if (ibc_type) { |
| 165 |
if (ibc_type == 2) { |
| 166 |
wtarr[uii * 8] = 2; |
| 167 |
wtarr[uii * 8 + 2] = 2.0 - 1.0 / (2 * set_allele_freqs[uii] * (1.0 - set_allele_freqs[uii])); |
| 168 |
wtarr[uii * 8 + 3] = 2; |
| 169 |
} else { |
| 170 |
twt = 2 * set_allele_freqs[uii]; |
| 171 |
if (ibc_type == 1) { |
| 172 |
mult = 1 / (twt * (1.0 - set_allele_freqs[uii])); |
| 173 |
} |
| 174 |
wtarr[uii * 8] = twt * twt * mult; |
| 175 |
wtarr[uii * 8 + 2] = (1.0 - twt) * (1.0 - twt) * mult; |
| 176 |
wtarr[uii * 8 + 3] = (2.0 - twt) * (2.0 - twt) * mult; |
| 177 |
} |
| 178 |
} else { |
| 179 |
twt = 1.0 - set_allele_freqs[uii]; |
| 180 |
mult = 1 / (set_allele_freqs[uii] * twt); |
| 181 |
wtarr[uii * 8] = 1.0 + set_allele_freqs[uii] * set_allele_freqs[uii] * mult; |
| 182 |
wtarr[uii * 8 + 3] = 1.0 + twt * twt * mult; |
| 183 |
} |
| 184 |
} else { |
| 185 |
if (ibc_type) { |
| 186 |
if (ibc_type == -1) { |
| 187 |
twt = 2 * set_allele_freqs[uii]; |
| 188 |
wtarr[uii * 8] = twt * twt; |
| 189 |
wtarr[uii * 8 + 2] = (1.0 - twt) * (1.0 - twt); |
| 190 |
wtarr[uii * 8 + 3] = (2.0 - twt) * (2.0 - twt); |
| 191 |
} else if (ibc_type == 1) { |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
wtarr[uii * 8 + 2] = INFINITY; |
| 196 |
if (set_allele_freqs[uii] == 0.0) { |
| 197 |
wtarr[uii * 8] = 0; |
| 198 |
wtarr[uii * 8 + 3] = INFINITY; |
| 199 |
} else { |
| 200 |
wtarr[uii * 8] = INFINITY; |
| 201 |
wtarr[uii * 8 + 3] = 0; |
| 202 |
} |
| 203 |
} else { |
| 204 |
|
| 205 |
wtarr[uii * 8] = 1; |
| 206 |
wtarr[uii * 8 + 2] = -INFINITY; |
| 207 |
wtarr[uii * 8 + 3] = 1; |
| 208 |
} |
| 209 |
} else { |
| 210 |
if (set_allele_freqs[uii] == 0.0) { |
| 211 |
wtarr[uii * 8] = 1; |
| 212 |
wtarr[uii * 8 + 3] = INFINITY; |
| 213 |
} else { |
| 214 |
wtarr[uii * 8] = INFINITY; |
| 215 |
wtarr[uii * 8 + 3] = 1; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
if (main_weights) { |
| 220 |
wtarr[uii * 8] *= main_weights[uii]; |
| 221 |
wtarr[uii * 8 + 2] *= main_weights[uii]; |
| 222 |
wtarr[uii * 8 + 3] *= main_weights[uii]; |
| 223 |
} |
| 224 |
} |
| 225 |
for (ukk = 0; ukk < (BITCT * 5) / 32; ukk++) { |
| 226 |
wtptr = &(wtarr[16 * ukk]); |
| 227 |
#ifdef __LP64__ |
| 228 |
if ((ukk == 2) || (ukk == 7)) { |
| 229 |
for (uii = 0; uii < 8; uii++) { |
| 230 |
twt = wtptr[uii + 8]; |
| 231 |
for (ujj = 0; ujj < 8; ujj++) { |
| 232 |
*wptr++ = twt + wtptr[ujj]; |
| 233 |
} |
| 234 |
wptr = &(wptr[8]); |
| 235 |
} |
| 236 |
} else { |
| 237 |
for (uii = 0; uii < 8; uii++) { |
| 238 |
twt = wtptr[uii + 8]; |
| 239 |
for (ujj = 0; ujj < 8; ujj++) { |
| 240 |
*wptr++ = twt + wtptr[ujj]; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
#else |
| 245 |
if (ukk == 2) { |
| 246 |
for (uii = 0; uii < 8; uii++) { |
| 247 |
twt = wtptr[uii + 8]; |
| 248 |
for (ujj = 0; ujj < 8; ujj++) { |
| 249 |
*wptr++ = twt + wtptr[ujj]; |
| 250 |
} |
| 251 |
wptr = &(wptr[8]); |
| 252 |
} |
| 253 |
} else { |
| 254 |
for (uii = 0; uii < 8; uii++) { |
| 255 |
twt = wtptr[uii + 8]; |
| 256 |
for (ujj = 0; ujj < 8; ujj++) { |
| 257 |
*wptr++ = twt + wtptr[ujj]; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
#endif |
| 262 |
} |
| 263 |
for (umm = 0; umm < sample_ct; umm++) { |
| 264 |
ulii = *geno++; |
| 265 |
#ifdef __LP64__ |
| 266 |
*rel_ibc += weights9[ulii >> 57] + weights8[(ulii >> 51) & 63] + weights7[(ulii >> 44) & 127] + weights6[(ulii >> 38) & 63] + weights5[(ulii >> 32) & 63] + weights4[(ulii >> 25) & 63] + weights3[(ulii >> 19) & 63] + weights2[(ulii >> 12) & 127] + weights1[(ulii >> 6) & 63] + weights[ulii & 63]; |
| 267 |
#else |
| 268 |
*rel_ibc += weights4[ulii >> 25] + weights3[(ulii >> 19) & 63] + weights2[(ulii >> 12) & 127] + weights1[(ulii >> 6) & 63] + weights[ulii & 63]; |
| 269 |
#endif |
| 270 |
rel_ibc++; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
void fill_subset_weights(double* subset_weights, double* main_weights) { |
| 275 |
uint32_t uii; |
| 276 |
uint32_t ujj; |
| 277 |
uint32_t ukk; |
| 278 |
uint32_t umm; |
| 279 |
uint32_t unn; |
| 280 |
uint32_t uoo; |
| 281 |
double wtarr[MULTIPLEX_DIST_EXP / 2]; |
| 282 |
double* wt; |
| 283 |
#ifdef __LP64__ |
| 284 |
double twt[5]; |
| 285 |
double twtf; |
| 286 |
__m128d* swpairs = (__m128d*)subset_weights; |
| 287 |
__m128d vpen; |
| 288 |
__m128d vfinal1; |
| 289 |
__m128d vfinal2; |
| 290 |
#else |
| 291 |
uint32_t upp; |
| 292 |
uint32_t uqq; |
| 293 |
double twt[7]; |
| 294 |
#endif |
| 295 |
memcpy(wtarr, main_weights, (MULTIPLEX_DIST_EXP / 2) * sizeof(double)); |
| 296 |
for (uoo = 0; uoo < 2; uoo++) { |
| 297 |
wt = &(wtarr[7 * uoo]); |
| 298 |
#ifdef __LP64__ |
| 299 |
vfinal1 = _mm_set_pd(wt[0], 0.0); |
| 300 |
vfinal2 = _mm_set_pd(wt[0] * 2, wt[0]); |
| 301 |
#endif |
| 302 |
twt[0] = 0; |
| 303 |
for (uii = 0; uii < 4; uii += 1) { |
| 304 |
|
| 305 |
|
| 306 |
if (uii & 1) { |
| 307 |
twt[0] += wt[6]; |
| 308 |
} |
| 309 |
twt[1] = twt[0]; |
| 310 |
for (ujj = 0; ujj < 4; ujj += 1) { |
| 311 |
if (ujj & 1) { |
| 312 |
twt[1] += wt[5]; |
| 313 |
} |
| 314 |
twt[2] = twt[1]; |
| 315 |
for (ukk = 0; ukk < 4; ukk += 1) { |
| 316 |
if (ukk & 1) { |
| 317 |
twt[2] += wt[4]; |
| 318 |
} |
| 319 |
twt[3] = twt[2]; |
| 320 |
for (umm = 0; umm < 4; umm++) { |
| 321 |
if (umm & 1) { |
| 322 |
twt[3] += wt[3]; |
| 323 |
} |
| 324 |
twt[4] = twt[3]; |
| 325 |
for (unn = 0; unn < 4; unn++) { |
| 326 |
if (unn & 1) { |
| 327 |
twt[4] += wt[2]; |
| 328 |
} |
| 329 |
#ifdef __LP64__ |
| 330 |
twtf = twt[4]; |
| 331 |
vpen = _mm_set1_pd(twtf); |
| 332 |
*swpairs++ = _mm_add_pd(vpen, vfinal1); |
| 333 |
*swpairs++ = _mm_add_pd(vpen, vfinal2); |
| 334 |
twtf += wt[1]; |
| 335 |
vpen = _mm_set1_pd(twtf); |
| 336 |
*swpairs++ = _mm_add_pd(vpen, vfinal1); |
| 337 |
*swpairs++ = _mm_add_pd(vpen, vfinal2); |
| 338 |
*swpairs = *(swpairs - 2); |
| 339 |
swpairs++; |
| 340 |
*swpairs = *(swpairs - 2); |
| 341 |
swpairs++; |
| 342 |
vpen = _mm_set1_pd(twtf + wt[1]); |
| 343 |
*swpairs++ = _mm_add_pd(vpen, vfinal1); |
| 344 |
*swpairs++ = _mm_add_pd(vpen, vfinal2); |
| 345 |
#else |
| 346 |
twt[5] = twt[4]; |
| 347 |
for (upp = 0; upp < 4; upp++) { |
| 348 |
if (upp & 1) { |
| 349 |
twt[5] += wt[1]; |
| 350 |
} |
| 351 |
twt[6] = twt[5]; |
| 352 |
for (uqq = 0; uqq < 4; uqq++) { |
| 353 |
if (uqq & 1) { |
| 354 |
twt[6] += wt[0]; |
| 355 |
} |
| 356 |
*subset_weights++ = twt[6]; |
| 357 |
} |
| 358 |
} |
| 359 |
#endif |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
#ifdef __LP64__ |
| 367 |
for (uoo = 0; uoo < 3; uoo++) { |
| 368 |
wt = &(wtarr[14 + 6 * uoo]); |
| 369 |
vfinal1 = _mm_set_pd(wt[0], 0.0); |
| 370 |
vfinal2 = _mm_set_pd(2 * wt[0], wt[0]); |
| 371 |
twt[0] = 0; |
| 372 |
for (uii = 0; uii < 4; uii += 1) { |
| 373 |
if (uii & 1) { |
| 374 |
twt[0] += wt[5]; |
| 375 |
} |
| 376 |
twt[1] = twt[0]; |
| 377 |
for (ujj = 0; ujj < 4; ujj += 1) { |
| 378 |
if (ujj & 1) { |
| 379 |
twt[1] += wt[4]; |
| 380 |
} |
| 381 |
twt[2] = twt[1]; |
| 382 |
for (ukk = 0; ukk < 4; ukk += 1) { |
| 383 |
if (ukk & 1) { |
| 384 |
twt[2] += wt[3]; |
| 385 |
} |
| 386 |
twt[3] = twt[2]; |
| 387 |
for (umm = 0; umm < 4; umm++) { |
| 388 |
if (umm & 1) { |
| 389 |
twt[3] += wt[2]; |
| 390 |
} |
| 391 |
twtf = twt[3]; |
| 392 |
vpen = _mm_set1_pd(twtf); |
| 393 |
*swpairs++ = _mm_add_pd(vpen, vfinal1); |
| 394 |
*swpairs++ = _mm_add_pd(vpen, vfinal2); |
| 395 |
twtf += wt[1]; |
| 396 |
vpen = _mm_set1_pd(twtf); |
| 397 |
*swpairs++ = _mm_add_pd(vpen, vfinal1); |
| 398 |
*swpairs++ = _mm_add_pd(vpen, vfinal2); |
| 399 |
*swpairs = *(swpairs - 2); |
| 400 |
swpairs++; |
| 401 |
*swpairs = *(swpairs - 2); |
| 402 |
swpairs++; |
| 403 |
vpen = _mm_set1_pd(twtf + wt[1]); |
| 404 |
*swpairs++ = _mm_add_pd(vpen, vfinal1); |
| 405 |
*swpairs++ = _mm_add_pd(vpen, vfinal2); |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
#endif |
| 412 |
} |
| 413 |
|
| 414 |
void fill_subset_weights_r(double* subset_weights, double* set_allele_freqs, double* main_weights, uint32_t var_std) { |
| 415 |
uint32_t uii; |
| 416 |
uint32_t ujj; |
| 417 |
uint32_t ukk; |
| 418 |
uint32_t umm; |
| 419 |
uint32_t unn; |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
double wtarr_raw[BITCT2 * 5 + 1]; |
| 424 |
double* wtarr = wtarr_raw; |
| 425 |
double twt; |
| 426 |
double twt2; |
| 427 |
double twt3; |
| 428 |
double twt4; |
| 429 |
double* wtptr; |
| 430 |
double mean; |
| 431 |
double mean_m1; |
| 432 |
double mean_m2; |
| 433 |
double mult = 1.0; |
| 434 |
double aux; |
| 435 |
#ifdef __LP64__ |
| 436 |
__m128d* swpairs = (__m128d*)subset_weights; |
| 437 |
__m128d vpen; |
| 438 |
__m128d vfinal1; |
| 439 |
__m128d vfinal2; |
| 440 |
__m128d vfinal3; |
| 441 |
__m128d vfinal4; |
| 442 |
#else |
| 443 |
uint32_t uoo; |
| 444 |
#endif |
| 445 |
if (((uintptr_t)wtarr) & 15) { |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
wtarr++; |
| 450 |
} |
| 451 |
for (uii = 0; uii < MULTIPLEX_REL / 3; uii += 1) { |
| 452 |
if (((set_allele_freqs[uii] != 0.0) && (set_allele_freqs[uii] < (1.0 - EPSILON))) || (!var_std)) { |
| 453 |
if (set_allele_freqs[uii] < 0.5) { |
| 454 |
mean = 2 * set_allele_freqs[uii]; |
| 455 |
mean_m1 = mean - 1.0; |
| 456 |
mean_m2 = mean - 2.0; |
| 457 |
if (var_std) { |
| 458 |
mult = 1 / (mean * (1.0 - set_allele_freqs[uii])); |
| 459 |
} |
| 460 |
aux = mean * mult; |
| 461 |
wtarr[uii * 8] = mean * aux; |
| 462 |
wtarr[uii * 8 + 1] = 0; |
| 463 |
wtarr[uii * 8 + 2] = mean_m1 * aux; |
| 464 |
wtarr[uii * 8 + 3] = mean_m2 * aux; |
| 465 |
wtarr[uii * 8 + 4] = mean_m1 * mean_m1 * mult; |
| 466 |
wtarr[uii * 8 + 5] = mean_m2 * mean_m1 * mult; |
| 467 |
wtarr[uii * 8 + 6] = mean_m2 * mean_m2 * mult; |
| 468 |
} else { |
| 469 |
mean = 2 * (1.0 - set_allele_freqs[uii]); |
| 470 |
mean_m1 = mean - 1.0; |
| 471 |
mean_m2 = mean - 2.0; |
| 472 |
if (var_std) { |
| 473 |
mult = 1 / (mean * set_allele_freqs[uii]); |
| 474 |
} |
| 475 |
aux = mean_m2 * mult; |
| 476 |
wtarr[uii * 8] = mean_m2 * aux; |
| 477 |
wtarr[uii * 8 + 1] = 0; |
| 478 |
wtarr[uii * 8 + 2] = mean_m1 * aux; |
| 479 |
wtarr[uii * 8 + 3] = mean * aux; |
| 480 |
wtarr[uii * 8 + 4] = mean_m1 * mean_m1 * mult; |
| 481 |
wtarr[uii * 8 + 5] = mean_m1 * mean * mult; |
| 482 |
wtarr[uii * 8 + 6] = mean * mean * mult; |
| 483 |
} |
| 484 |
} else { |
| 485 |
if (set_allele_freqs[uii] == 0.0) { |
| 486 |
wtarr[uii * 8] = 0; |
| 487 |
wtarr[uii * 8 + 1] = 0; |
| 488 |
wtarr[uii * 8 + 2] = -1; |
| 489 |
wtarr[uii * 8 + 3] = -2; |
| 490 |
wtarr[uii * 8 + 4] = INFINITY; |
| 491 |
wtarr[uii * 8 + 5] = INFINITY; |
| 492 |
wtarr[uii * 8 + 6] = INFINITY; |
| 493 |
} else { |
| 494 |
wtarr[uii * 8] = INFINITY; |
| 495 |
wtarr[uii * 8 + 1] = 0; |
| 496 |
wtarr[uii * 8 + 2] = INFINITY; |
| 497 |
wtarr[uii * 8 + 3] = -2; |
| 498 |
wtarr[uii * 8 + 4] = INFINITY; |
| 499 |
wtarr[uii * 8 + 5] = -1; |
| 500 |
wtarr[uii * 8 + 6] = 0; |
| 501 |
} |
| 502 |
} |
| 503 |
if (main_weights) { |
| 504 |
for (ujj = 0; ujj < 7; ujj++) { |
| 505 |
wtarr[uii * 8 + ujj] *= main_weights[uii]; |
| 506 |
} |
| 507 |
} |
| 508 |
wtarr[uii * 8 + 7] = 0; |
| 509 |
} |
| 510 |
for (unn = 0; unn < BITCT / 16; unn++) { |
| 511 |
wtptr = &(wtarr[40 * unn]); |
| 512 |
#ifdef __LP64__ |
| 513 |
vfinal1 = _mm_load_pd(wtptr); |
| 514 |
vfinal2 = _mm_load_pd(&(wtptr[2])); |
| 515 |
vfinal3 = _mm_load_pd(&(wtptr[4])); |
| 516 |
vfinal4 = _mm_load_pd(&(wtptr[6])); |
| 517 |
#endif |
| 518 |
for (uii = 0; uii < 8; uii++) { |
| 519 |
twt = wtptr[uii + 32]; |
| 520 |
for (ujj = 0; ujj < 8; ujj++) { |
| 521 |
twt2 = twt + wtptr[ujj + 24]; |
| 522 |
for (ukk = 0; ukk < 8; ukk++) { |
| 523 |
twt3 = twt2 + wtptr[ukk + 16]; |
| 524 |
for (umm = 0; umm < 8; umm++) { |
| 525 |
twt4 = twt3 + wtptr[umm + 8]; |
| 526 |
#ifdef __LP64__ |
| 527 |
vpen = _mm_set1_pd(twt4); |
| 528 |
*swpairs++ = _mm_add_pd(vpen, vfinal1); |
| 529 |
*swpairs++ = _mm_add_pd(vpen, vfinal2); |
| 530 |
*swpairs++ = _mm_add_pd(vpen, vfinal3); |
| 531 |
*swpairs++ = _mm_add_pd(vpen, vfinal4); |
| 532 |
#else |
| 533 |
for (uoo = 0; uoo < 8; uoo++) { |
| 534 |
*subset_weights++ = twt4 + wtptr[uoo]; |
| 535 |
} |
| 536 |
#endif |
| 537 |
} |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
void exclude_multi(uintptr_t* exclude_arr, int32_t* new_excl, uint32_t unfiltered_sample_ct, uintptr_t* exclude_ct_ptr) { |
| 545 |
uint32_t exclude_ct = *exclude_ct_ptr; |
| 546 |
int32_t* new_excl_end = &(new_excl[unfiltered_sample_ct - exclude_ct]); |
| 547 |
uint32_t sample_uidx = 0; |
| 548 |
uint32_t sample_uidx_stop; |
| 549 |
do { |
| 550 |
sample_uidx = next_unset_unsafe(exclude_arr, sample_uidx); |
| 551 |
sample_uidx_stop = next_set(exclude_arr, sample_uidx, unfiltered_sample_ct); |
| 552 |
do { |
| 553 |
if (*new_excl++ == -1) { |
| 554 |
SET_BIT(sample_uidx, exclude_arr); |
| 555 |
exclude_ct++; |
| 556 |
} |
| 557 |
} while (++sample_uidx < sample_uidx_stop); |
| 558 |
} while (new_excl < new_excl_end); |
| 559 |
*exclude_ct_ptr = exclude_ct; |
| 560 |
} |
| 561 |
|
| 562 |
void collapse_copy_phenod(double* target, double* pheno_d, uintptr_t* sample_exclude, uintptr_t unfiltered_sample_ct, uintptr_t sample_ct) { |
| 563 |
uintptr_t sample_uidx = 0; |
| 564 |
double* target_end = &(target[sample_ct]); |
| 565 |
uintptr_t delta; |
| 566 |
do { |
| 567 |
sample_uidx = next_unset_ul_unsafe(sample_exclude, sample_uidx); |
| 568 |
delta = next_set_ul(sample_exclude, sample_uidx, unfiltered_sample_ct) - sample_uidx; |
| 569 |
memcpy(target, &(pheno_d[sample_uidx]), delta * sizeof(double)); |
| 570 |
target = &(target[delta]); |
| 571 |
sample_uidx += delta; |
| 572 |
} while (target < target_end); |
| 573 |
} |
| 574 |
|
| 575 |
#ifdef __LP64__ |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
static inline uint32_t popcount_xor_1mask_multiword(__m128i** xor1p, __m128i* xor2, __m128i** maskp) { |
| 582 |
const __m128i m1 = {FIVEMASK, FIVEMASK}; |
| 583 |
const __m128i m2 = {0x3333333333333333LLU, 0x3333333333333333LLU}; |
| 584 |
const __m128i m4 = {0x0f0f0f0f0f0f0f0fLLU, 0x0f0f0f0f0f0f0f0fLLU}; |
| 585 |
const __m128i m8 = {0x00ff00ff00ff00ffLLU, 0x00ff00ff00ff00ffLLU}; |
| 586 |
__m128i count1, count2, half1, half2; |
| 587 |
__univec acc; |
| 588 |
__m128i* xor2_end = &(xor2[MULTIPLEX_2DIST / 128]); |
| 589 |
|
| 590 |
acc.vi = _mm_setzero_si128(); |
| 591 |
do { |
| 592 |
count1 = _mm_and_si128(_mm_xor_si128(*((*xor1p)++), *xor2++), *((*maskp)++)); |
| 593 |
count2 = _mm_and_si128(_mm_xor_si128(*((*xor1p)++), *xor2++), *((*maskp)++)); |
| 594 |
half1 = _mm_and_si128(_mm_xor_si128(*((*xor1p)++), *xor2++), *((*maskp)++)); |
| 595 |
half2 = _mm_and_si128(_mm_srli_epi64(half1, 1), m1); |
| 596 |
half1 = _mm_and_si128(half1, m1); |
| 597 |
count1 = _mm_sub_epi64(count1, _mm_and_si128(_mm_srli_epi64(count1, 1), m1)); |
| 598 |
count2 = _mm_sub_epi64(count2, _mm_and_si128(_mm_srli_epi64(count2, 1), m1)); |
| 599 |
count1 = _mm_add_epi64(count1, half1); |
| 600 |
count2 = _mm_add_epi64(count2, half2); |
| 601 |
count1 = _mm_add_epi64(_mm_and_si128(count1, m2), _mm_and_si128(_mm_srli_epi64(count1, 2), m2)); |
| 602 |
count1 = _mm_add_epi64(count1, _mm_add_epi64(_mm_and_si128(count2, m2), _mm_and_si128(_mm_srli_epi64(count2, 2), m2))); |
| 603 |
acc.vi = _mm_add_epi64(acc.vi, _mm_add_epi64(_mm_and_si128(count1, m4), _mm_and_si128(_mm_srli_epi64(count1, 4), m4))); |
| 604 |
} while (xor2 < xor2_end); |
| 605 |
#if MULTIPLEX_DIST > 960 |
| 606 |
acc.vi = _mm_add_epi64(_mm_and_si128(acc.vi, m8), _mm_and_si128(_mm_srli_epi64(acc.vi, 8), m8)); |
| 607 |
#else |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
acc.vi = _mm_and_si128(_mm_add_epi64(acc.vi, _mm_srli_epi64(acc.vi, 8)), m8); |
| 612 |
#endif |
| 613 |
return ((acc.u8[0] + acc.u8[1]) * 0x1000100010001LLU) >> 48; |
| 614 |
} |
| 615 |
|
| 616 |
static inline uint32_t popcount_xor_2mask_multiword(__m128i** xor1p, __m128i* xor2, __m128i** mask1p, __m128i* mask2) { |
| 617 |
const __m128i m1 = {FIVEMASK, FIVEMASK}; |
| 618 |
const __m128i m2 = {0x3333333333333333LLU, 0x3333333333333333LLU}; |
| 619 |
const __m128i m4 = {0x0f0f0f0f0f0f0f0fLLU, 0x0f0f0f0f0f0f0f0fLLU}; |
| 620 |
const __m128i m8 = {0x00ff00ff00ff00ffLLU, 0x00ff00ff00ff00ffLLU}; |
| 621 |
__m128i count1, count2, half1, half2; |
| 622 |
__univec acc; |
| 623 |
__m128i* xor2_end = &(xor2[MULTIPLEX_2DIST / 128]); |
| 624 |
|
| 625 |
acc.vi = _mm_setzero_si128(); |
| 626 |
do { |
| 627 |
count1 = _mm_and_si128(_mm_xor_si128(*((*xor1p)++), *xor2++), _mm_and_si128(*((*mask1p)++), *mask2++)); |
| 628 |
count2 = _mm_and_si128(_mm_xor_si128(*((*xor1p)++), *xor2++), _mm_and_si128(*((*mask1p)++), *mask2++)); |
| 629 |
half1 = _mm_and_si128(_mm_xor_si128(*((*xor1p)++), *xor2++), _mm_and_si128(*((*mask1p)++), *mask2++)); |
| 630 |
half2 = _mm_and_si128(_mm_srli_epi64(half1, 1), m1); |
| 631 |
half1 = _mm_and_si128(half1, m1); |
| 632 |
count1 = _mm_sub_epi64(count1, _mm_and_si128(_mm_srli_epi64(count1, 1), m1)); |
| 633 |
count2 = _mm_sub_epi64(count2, _mm_and_si128(_mm_srli_epi64(count2, 1), m1)); |
| 634 |
count1 = _mm_add_epi64(count1, half1); |
| 635 |
count2 = _mm_add_epi64(count2, half2); |
| 636 |
count1 = _mm_add_epi64(_mm_and_si128(count1, m2), _mm_and_si128(_mm_srli_epi64(count1, 2), m2)); |
| 637 |
count1 = _mm_add_epi64(count1, _mm_add_epi64(_mm_and_si128(count2, m2), _mm_and_si128(_mm_srli_epi64(count2, 2), m2))); |
| 638 |
acc.vi = _mm_add_epi64(acc.vi, _mm_add_epi64(_mm_and_si128(count1, m4), _mm_and_si128(_mm_srli_epi64(count1, 4), m4))); |
| 639 |
} while (xor2 < xor2_end); |
| 640 |
#if MULTIPLEX_DIST > 960 |
| 641 |
acc.vi = _mm_add_epi64(_mm_and_si128(acc.vi, m8), _mm_and_si128(_mm_srli_epi64(acc.vi, 8), m8)); |
| 642 |
#else |
| 643 |
acc.vi = _mm_and_si128(_mm_add_epi64(acc.vi, _mm_srli_epi64(acc.vi, 8)), m8); |
| 644 |
#endif |
| 645 |
return ((acc.u8[0] + acc.u8[1]) * 0x1000100010001LLU) >> 48; |
| 646 |
} |
| 647 |
#else |
| 648 |
static inline uint32_t popcount_xor_1mask_multiword(uintptr_t** xor1p, uintptr_t* xor2, uintptr_t** maskp) { |
| 649 |
uintptr_t* xor2_end = &(xor2[MULTIPLEX_DIST / 16]); |
| 650 |
uint32_t bit_count = 0; |
| 651 |
uintptr_t tmp_stor; |
| 652 |
uintptr_t loader; |
| 653 |
uintptr_t ulii; |
| 654 |
uintptr_t uljj; |
| 655 |
do { |
| 656 |
loader = ((*((*xor1p)++)) ^ *xor2++) & (*((*maskp)++)); |
| 657 |
ulii = loader - ((loader >> 1) & FIVEMASK); |
| 658 |
loader = ((*((*xor1p)++)) ^ *xor2++) & (*((*maskp)++)); |
| 659 |
uljj = loader - ((loader >> 1) & FIVEMASK); |
| 660 |
loader = ((*((*xor1p)++)) ^ *xor2++) & (*((*maskp)++)); |
| 661 |
ulii += (loader >> 1) & FIVEMASK; |
| 662 |
uljj += loader & FIVEMASK; |
| 663 |
ulii = (ulii & 0x33333333) + ((ulii >> 2) & 0x33333333); |
| 664 |
ulii += (uljj & 0x33333333) + ((uljj >> 2) & 0x33333333); |
| 665 |
tmp_stor = (ulii & 0x0f0f0f0f) + ((ulii >> 4) & 0x0f0f0f0f); |
| 666 |
|
| 667 |
loader = ((*((*xor1p)++)) ^ *xor2++) & (*((*maskp)++)); |
| 668 |
ulii = loader - ((loader >> 1) & FIVEMASK); |
| 669 |
loader = ((*((*xor1p)++)) ^ *xor2++) & (*((*maskp)++)); |
| 670 |
uljj = loader - ((loader >> 1) & FIVEMASK); |
| 671 |
loader = ((*((*xor1p)++)) ^ *xor2++) & (*((*maskp)++)); |
| 672 |
ulii += (loader >> 1) & FIVEMASK; |
| 673 |
uljj += loader & FIVEMASK; |
| 674 |
ulii = (ulii & 0x33333333) + ((ulii >> 2) & 0x33333333); |
| 675 |
ulii += (uljj & 0x33333333) + ((uljj >> 2) & 0x33333333); |
| 676 |
tmp_stor += (ulii & 0x0f0f0f0f) + ((ulii >> 4) & 0x0f0f0f0f); |
| 677 |
|
| 678 |
bit_count += (tmp_stor * 0x01010101) >> 24; |
| 679 |
} while (xor2 < xor2_end); |
| 680 |
return bit_count; |
| 681 |
} |
| 682 |
|
| 683 |
static inline uint32_t popcount_xor_2mask_multiword(uintptr_t** xor1p, uintptr_t* xor2, uintptr_t** mask1p, uintptr_t* mask2) { |
| 684 |
uintptr_t* xor2_end = &(xor2[MULTIPLEX_DIST / 16]); |
| 685 |
uint32_t bit_count = 0; |
| 686 |
uintptr_t tmp_stor; |
| 687 |
uintptr_t loader; |
| 688 |
uintptr_t ulii; |
| 689 |
uintptr_t uljj; |
| 690 |
do { |
| 691 |
loader = ((*((*xor1p)++)) ^ *xor2++) & ((*((*mask1p)++)) & *mask2++); |
| 692 |
ulii = loader - ((loader >> 1) & FIVEMASK); |
| 693 |
loader = ((*((*xor1p)++)) ^ *xor2++) & ((*((*mask1p)++)) & *mask2++); |
| 694 |
uljj = loader - ((loader >> 1) & FIVEMASK); |
| 695 |
loader = ((*((*xor1p)++)) ^ *xor2++) & ((*((*mask1p)++)) & *mask2++); |
| 696 |
loader -= ((loader >> 1) & FIVEMASK); |
| 697 |
ulii = (ulii & 0x33333333) + ((ulii >> 2) & 0x33333333); |
| 698 |
ulii += (uljj & 0x33333333) + ((uljj >> 2) & 0x33333333); |
| 699 |
ulii += (loader & 0x33333333) + ((loader >> 2) & 0x33333333); |
| 700 |
tmp_stor = (ulii & 0x0f0f0f0f) + ((ulii >> 4) & 0x0f0f0f0f); |
| 701 |
|
| 702 |
loader = ((*((*xor1p)++)) ^ *xor2++) & ((*((*mask1p)++)) & *mask2++); |
| 703 |
ulii = loader - ((loader >> 1) & FIVEMASK); |
| 704 |
loader = ((*((*xor1p)++)) ^ *xor2++) & ((*((*mask1p)++)) & *mask2++); |
| 705 |
uljj = loader - ((loader >> 1) & FIVEMASK); |
| 706 |
loader = ((*((*xor1p)++)) ^ *xor2++) & ((*((*mask1p)++)) & *mask2++); |
| 707 |
loader -= ((loader >> 1) & FIVEMASK); |
| 708 |
ulii = (ulii & 0x33333333) + ((ulii >> 2) & 0x33333333); |
| 709 |
ulii += (uljj & 0x33333333) + ((uljj >> 2) & 0x33333333); |
| 710 |
ulii += (loader & 0x33333333) + ((loader >> 2) & 0x33333333); |
| 711 |
tmp_stor += (ulii & 0x0f0f0f0f) + ((ulii >> 4) & 0x0f0f0f0f); |
| 712 |
|
| 713 |
bit_count += (tmp_stor * 0x01010101) >> 24; |
| 714 |
} while (xor2 < xor2_end); |
| 715 |
return bit_count; |
| 716 |
} |
| 717 |
#endif |
| 718 |
|
| 719 |
|
| 720 |
double* g_rel_dists = nullptr; |
| 721 |
uint32_t* g_sample_missing_unwt = nullptr; |
| 722 |
uint32_t* g_missing_dbl_excluded = nullptr; |
| 723 |
double* g_dists = nullptr; |
| 724 |
|
| 725 |
static uint32_t g_thread_start[MAX_THREADS_P1]; |
| 726 |
static int32_t* g_idists; |
| 727 |
static uintptr_t* g_pheno_nm = nullptr; |
| 728 |
static uintptr_t* g_pheno_c = nullptr; |
| 729 |
static unsigned char* g_geno = nullptr; |
| 730 |
static double* g_subset_weights; |
| 731 |
static uint32_t* g_subset_weights_i; |
| 732 |
static double g_reg_tot_xy; |
| 733 |
static double g_reg_tot_x; |
| 734 |
static double g_reg_tot_y; |
| 735 |
static double g_reg_tot_xx; |
| 736 |
static double g_reg_tot_yy; |
| 737 |
static uint32_t g_ctrl_ct; |
| 738 |
static uint32_t g_case_ct; |
| 739 |
static uintptr_t g_sample_ct; |
| 740 |
static uintptr_t g_jackknife_iters; |
| 741 |
static uint32_t g_jackknife_d; |
| 742 |
static double g_calc_result[MAX_THREADS_P1][9]; |
| 743 |
static uintptr_t* g_masks; |
| 744 |
static uintptr_t* g_mmasks; |
| 745 |
static uint32_t* g_missing_tot_weights; |
| 746 |
static uint32_t* g_sample_missing; |
| 747 |
static double* g_jackknife_precomp = nullptr; |
| 748 |
static uint32_t* g_genome_main = nullptr; |
| 749 |
static uintptr_t g_marker_window[GENOME_MULTIPLEX * 2]; |
| 750 |
static double* g_pheno_packed; |
| 751 |
|
| 752 |
|
| 753 |
static uintptr_t* g_perm_rows; |
| 754 |
|
| 755 |
static uintptr_t* g_perm_col_buf; |
| 756 |
static double* g_ibs_test_partial_sums; |
| 757 |
static double* g_perm_results; |
| 758 |
static uintptr_t g_perm_ct; |
| 759 |
static double g_half_marker_ct_recip; |
| 760 |
static uint32_t g_load_dists; |
| 761 |
static unsigned char* g_generic_buf; |
| 762 |
|
| 763 |
void ibs_test_init_col_buf(uintptr_t row_idx, uintptr_t perm_ct, uintptr_t* perm_rows, uintptr_t* perm_col_buf) { |
| 764 |
uintptr_t perm_idx = 0; |
| 765 |
uintptr_t block_size = BITCT; |
| 766 |
uintptr_t rshift_ct = row_idx & (BITCT - 1); |
| 767 |
uintptr_t* gpr_ptr; |
| 768 |
uintptr_t ulii; |
| 769 |
uintptr_t block_pos; |
| 770 |
gpr_ptr = &(perm_rows[(row_idx / BITCT) * perm_ct]); |
| 771 |
do { |
| 772 |
if (perm_idx + BITCT > perm_ct) { |
| 773 |
block_size = perm_ct - perm_idx; |
| 774 |
} |
| 775 |
ulii = 0; |
| 776 |
block_pos = 0; |
| 777 |
do { |
| 778 |
ulii |= (((*gpr_ptr++) >> rshift_ct) & ONELU) << block_pos; |
| 779 |
} while (++block_pos < block_size); |
| 780 |
perm_idx += block_size; |
| 781 |
*perm_col_buf++ = ulii; |
| 782 |
} while (perm_idx < perm_ct); |
| 783 |
} |
| 784 |
|
| 785 |
double fill_psbuf(uintptr_t block_size, double half_marker_ct_recip, uint32_t load_dists, uintptr_t* pheno_nm, uintptr_t* pheno_c, double* dists, uintptr_t* col_uidxp, double* psbuf, double* ssq0p) { |
| 786 |
|
| 787 |
double tot = 0.0; |
| 788 |
uintptr_t col_idx = 0; |
| 789 |
uintptr_t col_uidx = *col_uidxp; |
| 790 |
uint32_t sub_block_size = 8; |
| 791 |
uint32_t sub_block_idx; |
| 792 |
double subtot; |
| 793 |
uintptr_t ulii; |
| 794 |
double increment[8]; |
| 795 |
double ssq[2]; |
| 796 |
double dxx; |
| 797 |
ssq[0] = 0.0; |
| 798 |
ssq[1] = 0.0; |
| 799 |
do { |
| 800 |
if (col_idx + 8 > block_size) { |
| 801 |
sub_block_size = block_size - col_idx; |
| 802 |
} |
| 803 |
sub_block_idx = 0; |
| 804 |
subtot = 0.0; |
| 805 |
do { |
| 806 |
next_set_ul_unsafe_ck(pheno_nm, &col_uidx); |
| 807 |
if (load_dists) { |
| 808 |
dxx = dists[col_uidx]; |
| 809 |
} else { |
| 810 |
dxx = 1.0 - dists[col_uidx] * half_marker_ct_recip; |
| 811 |
} |
| 812 |
increment[sub_block_idx] = subtot - dxx; |
| 813 |
subtot += dxx; |
| 814 |
ssq[IS_SET(pheno_c, col_uidx)] += dxx * dxx; |
| 815 |
col_uidx++; |
| 816 |
} while (++sub_block_idx < sub_block_size); |
| 817 |
tot += subtot; |
| 818 |
while (sub_block_idx < 8) { |
| 819 |
increment[sub_block_idx++] = subtot; |
| 820 |
} |
| 821 |
ulii = 0; |
| 822 |
dxx = subtot; |
| 823 |
goto fill_psbuf_loop_start; |
| 824 |
|
| 825 |
do { |
| 826 |
dxx += increment[CTZLU(++ulii)]; |
| 827 |
fill_psbuf_loop_start: |
| 828 |
*psbuf++ = dxx; |
| 829 |
} while (ulii < 255); |
| 830 |
col_idx += sub_block_size; |
| 831 |
} while (col_idx < block_size); |
| 832 |
*col_uidxp = col_uidx; |
| 833 |
ssq0p[0] += ssq[0]; |
| 834 |
ssq0p[1] += ssq[1]; |
| 835 |
return tot; |
| 836 |
} |
| 837 |
|
| 838 |
void ibs_test_process_perms(uintptr_t* perm_row_start, uintptr_t perm_ct, uint32_t sub_block_ct, double block_tot, double* psbuf, uintptr_t* perm_col_buf, double* perm_results) { |
| 839 |
uintptr_t perm_idx = 0; |
| 840 |
uintptr_t block_size = BITCT; |
| 841 |
double dxx; |
| 842 |
uintptr_t block_pos; |
| 843 |
uintptr_t col_bits; |
| 844 |
uintptr_t ulii; |
| 845 |
uint32_t sub_block_idx; |
| 846 |
do { |
| 847 |
col_bits = *perm_col_buf++; |
| 848 |
if (perm_idx + BITCT > perm_ct) { |
| 849 |
block_size = perm_ct - perm_idx; |
| 850 |
} |
| 851 |
block_pos = 0; |
| 852 |
if (sub_block_ct == BITCT / 8) { |
| 853 |
do { |
| 854 |
sub_block_idx = 0; |
| 855 |
ulii = *perm_row_start++; |
| 856 |
#ifdef __LP64__ |
| 857 |
dxx = psbuf[(uint8_t)ulii] + psbuf[256 + ((uint8_t)(ulii >> 8))] + psbuf[512 + ((uint8_t)(ulii >> 16))] + psbuf[768 + ((uint8_t)(ulii >> 24))] + psbuf[1024 + ((uint8_t)(ulii >> 32))] + psbuf[1280 + ((uint8_t)(ulii >> 40))] + psbuf[1536 + ((uint8_t)(ulii >> 48))] + psbuf[1792 + (ulii >> 56)]; |
| 858 |
#else |
| 859 |
dxx = psbuf[(uint8_t)ulii] + psbuf[256 + ((uint8_t)(ulii >> 8))] + psbuf[512 + ((uint8_t)(ulii >> 16))] + psbuf[768 + (ulii >> 24)]; |
| 860 |
#endif |
| 861 |
if (col_bits & 1) { |
| 862 |
perm_results[2 * block_pos + 1] += dxx; |
| 863 |
} else { |
| 864 |
perm_results[2 * block_pos] += dxx; |
| 865 |
perm_results[2 * block_pos + 1] += (block_tot - dxx); |
| 866 |
} |
| 867 |
col_bits >>= 1; |
| 868 |
} while (++block_pos < block_size); |
| 869 |
} else { |
| 870 |
do { |
| 871 |
sub_block_idx = 0; |
| 872 |
ulii = *perm_row_start++; |
| 873 |
dxx = psbuf[(uint8_t)ulii]; |
| 874 |
while (++sub_block_idx < sub_block_ct) { |
| 875 |
dxx += psbuf[256 * sub_block_idx + ((uint8_t)(ulii >> (8 * sub_block_idx)))]; |
| 876 |
} |
| 877 |
if (col_bits & 1) { |
| 878 |
perm_results[2 * block_pos + 1] += dxx; |
| 879 |
} else { |
| 880 |
perm_results[2 * block_pos] += dxx; |
| 881 |
perm_results[2 * block_pos + 1] += (block_tot - dxx); |
| 882 |
} |
| 883 |
col_bits >>= 1; |
| 884 |
} while (++block_pos < block_size); |
| 885 |
} |
| 886 |
perm_idx += block_size; |
| 887 |
perm_results = &(perm_results[2 * block_size]); |
| 888 |
} while (perm_idx < perm_ct); |
| 889 |
} |
| 890 |
|
| 891 |
void ibs_test_range(uint32_t tidx, uintptr_t* perm_col_buf, double* perm_results) { |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
double* psptr = &(g_ibs_test_partial_sums[tidx * (32 * BITCT)]); |
| 896 |
double dist_tot = 0.0; |
| 897 |
uintptr_t row_uidx = 0; |
| 898 |
uintptr_t pct = 0; |
| 899 |
uintptr_t pct_div = 1 + ((g_thread_start[1] * (g_thread_start[1] - 1)) / 100); |
| 900 |
uintptr_t pct_next = pct_div; |
| 901 |
uintptr_t perm_ct = g_perm_ct; |
| 902 |
uintptr_t row_bound1 = g_thread_start[tidx]; |
| 903 |
uintptr_t row_bound2 = g_thread_start[tidx + 1]; |
| 904 |
uintptr_t* pheno_nm = g_pheno_nm; |
| 905 |
uintptr_t* pheno_c = g_pheno_c; |
| 906 |
uintptr_t* perm_rows = g_perm_rows; |
| 907 |
double* dists = g_dists; |
| 908 |
double half_marker_ct_recip = g_half_marker_ct_recip; |
| 909 |
uint32_t load_dists = g_load_dists; |
| 910 |
double ssq[3]; |
| 911 |
double* dptr; |
| 912 |
double block_tot; |
| 913 |
uintptr_t row_idx; |
| 914 |
uintptr_t col_idx; |
| 915 |
uintptr_t col_uidx; |
| 916 |
uintptr_t block_size; |
| 917 |
uintptr_t ulii; |
| 918 |
uint32_t row_set; |
| 919 |
ssq[0] = 0.0; |
| 920 |
ssq[1] = 0.0; |
| 921 |
ssq[2] = 0.0; |
| 922 |
for (row_idx = 0; row_idx < row_bound1; row_uidx++, row_idx++) { |
| 923 |
next_set_ul_unsafe_ck(pheno_nm, &row_uidx); |
| 924 |
} |
| 925 |
for (; row_idx < row_bound2; row_uidx++, row_idx++) { |
| 926 |
next_set_ul_unsafe_ck(pheno_nm, &row_uidx); |
| 927 |
dptr = &(dists[(row_uidx * (row_uidx - 1)) / 2]); |
| 928 |
col_idx = 0; |
| 929 |
col_uidx = 0; |
| 930 |
row_set = IS_SET(pheno_c, row_uidx); |
| 931 |
ibs_test_init_col_buf(row_idx, perm_ct, perm_rows, perm_col_buf); |
| 932 |
do { |
| 933 |
if (col_idx + BITCT > row_idx) { |
| 934 |
block_size = row_idx - col_idx; |
| 935 |
} else { |
| 936 |
block_size = BITCT; |
| 937 |
} |
| 938 |
block_tot = fill_psbuf(block_size, half_marker_ct_recip, load_dists, pheno_nm, pheno_c, dptr, &col_uidx, psptr, &(ssq[row_set])); |
| 939 |
dist_tot += block_tot; |
| 940 |
ibs_test_process_perms(&(perm_rows[(col_idx / BITCT) * perm_ct]), perm_ct, (block_size + 7) / 8, block_tot, psptr, perm_col_buf, perm_results); |
| 941 |
col_idx += block_size; |
| 942 |
} while (col_idx < row_idx); |
| 943 |
if (!tidx) { |
| 944 |
|
| 945 |
|
| 946 |
ulii = row_idx * (row_idx + 1); |
| 947 |
if (ulii >= pct_next) { |
| 948 |
if (pct >= 10) { |
| 949 |
putc_unlocked('\b', stdout); |
| 950 |
} |
| 951 |
pct = ulii / pct_div; |
| 952 |
printf("\b\b%" PRIuPTR "%%", pct); |
| 953 |
fflush(stdout); |
| 954 |
pct_next = pct_div * (pct + 1); |
| 955 |
} |
| 956 |
} |
| 957 |
} |
| 958 |
g_calc_result[tidx][0] = dist_tot; |
| 959 |
g_calc_result[tidx][1] = ssq[0]; |
| 960 |
g_calc_result[tidx][2] = ssq[1]; |
| 961 |
g_calc_result[tidx][3] = ssq[2]; |
| 962 |
} |
| 963 |
|
| 964 |
THREAD_RET_TYPE ibs_test_thread(void* arg) { |
| 965 |
uintptr_t tidx = (uintptr_t)arg; |
| 966 |
uintptr_t perm_ctcl = (g_perm_ct + (CACHELINE * 8 - 1)) / (CACHELINE * 8); |
| 967 |
uintptr_t perm_ctcld = (g_perm_ct + (CACHELINE_DBL - 1)) / CACHELINE_DBL; |
| 968 |
ibs_test_range((uint32_t)tidx, &(g_perm_col_buf[tidx * perm_ctcl * (CACHELINE / sizeof(intptr_t))]), &(g_perm_results[tidx * perm_ctcld * 2 * CACHELINE_DBL])); |
| 969 |
THREAD_RETURN; |
| 970 |
} |
| 971 |
|
| 972 |
void incr_dists_i(uint32_t* idists, uintptr_t* geno, uintptr_t* masks, uint32_t start_idx, uint32_t end_idx) { |
| 973 |
#ifdef __LP64__ |
| 974 |
__m128i* glptr; |
| 975 |
__m128i* glptr2; |
| 976 |
__m128i* mptr; |
| 977 |
__m128i* mcptr_start; |
| 978 |
uintptr_t* lptr; |
| 979 |
#else |
| 980 |
uintptr_t* glptr; |
| 981 |
uintptr_t* glptr2; |
| 982 |
uintptr_t* mptr; |
| 983 |
uintptr_t* mcptr_start; |
| 984 |
#endif |
| 985 |
uint32_t uii; |
| 986 |
int32_t jj; |
| 987 |
uintptr_t mask_fixed; |
| 988 |
for (uii = start_idx; uii < end_idx; uii++) { |
| 989 |
jj = uii * (MULTIPLEX_2DIST / BITCT); |
| 990 |
#ifdef __LP64__ |
| 991 |
glptr = (__m128i*)geno; |
| 992 |
glptr2 = (__m128i*)(&(geno[jj])); |
| 993 |
lptr = &(masks[jj]); |
| 994 |
mcptr_start = (__m128i*)lptr; |
| 995 |
mask_fixed = *lptr++; |
| 996 |
for (jj = 0; jj < MULTIPLEX_2DIST / BITCT - 1; jj++) { |
| 997 |
mask_fixed &= *lptr++; |
| 998 |
} |
| 999 |
mptr = (__m128i*)masks; |
| 1000 |
#else |
| 1001 |
glptr = geno; |
| 1002 |
glptr2 = &(geno[jj]); |
| 1003 |
mcptr_start = &(masks[jj]); |
| 1004 |
mptr = mcptr_start; |
| 1005 |
mask_fixed = *mptr++; |
| 1006 |
for (jj = 0; jj < MULTIPLEX_2DIST / BITCT - 1; jj++) { |
| 1007 |
mask_fixed &= *mptr++; |
| 1008 |
} |
| 1009 |
mptr = masks; |
| 1010 |
#endif |
| 1011 |
if (~mask_fixed) { |
| 1012 |
while (glptr < glptr2) { |
| 1013 |
*idists += popcount_xor_2mask_multiword(&glptr, glptr2, &mptr, mcptr_start); |
| 1014 |
idists++; |
| 1015 |
} |
| 1016 |
} else { |
| 1017 |
while (glptr < glptr2) { |
| 1018 |
*idists += popcount_xor_1mask_multiword(&glptr, glptr2, &mptr); |
| 1019 |
idists++; |
| 1020 |
} |
| 1021 |
} |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
void incr_wt_dist_missing(uint32_t* mtw, uint32_t* subset_weights_i, uintptr_t* mmasks, uint32_t start_idx, uint32_t end_idx) { |
| 1026 |
uintptr_t* glptr; |
| 1027 |
uintptr_t ulii; |
| 1028 |
uintptr_t uljj; |
| 1029 |
uint32_t uii; |
| 1030 |
uint32_t ujj; |
| 1031 |
for (uii = start_idx; uii < end_idx; uii++) { |
| 1032 |
glptr = mmasks; |
| 1033 |
ulii = mmasks[uii]; |
| 1034 |
if (ulii) { |
| 1035 |
for (ujj = 0; ujj < uii; ujj++) { |
| 1036 |
uljj = (*glptr++) & ulii; |
| 1037 |
while (uljj) { |
| 1038 |
mtw[ujj] += subset_weights_i[CTZLU(uljj)]; |
| 1039 |
uljj &= uljj - 1; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |
| 1043 |
mtw = &(mtw[uii]); |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
void incr_dists_rm(uint32_t* idists, uintptr_t* mmasks, uint32_t start_idx, uint32_t end_idx) { |
| 1048 |
|
| 1049 |
uintptr_t* mlptr; |
| 1050 |
uintptr_t ulii; |
| 1051 |
uintptr_t uljj; |
| 1052 |
uint32_t uii; |
| 1053 |
uint32_t ujj; |
| 1054 |
for (uii = start_idx; uii < end_idx; uii++) { |
| 1055 |
mlptr = mmasks; |
| 1056 |
ulii = mmasks[uii]; |
| 1057 |
if (ulii) { |
| 1058 |
for (ujj = 0; ujj < uii; ujj++) { |
| 1059 |
uljj = (*mlptr++) & ulii; |
| 1060 |
if (uljj) { |
| 1061 |
idists[ujj] += popcount_long(uljj); |
| 1062 |
} |
| 1063 |
} |
| 1064 |
} |
| 1065 |
idists = &(idists[uii]); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
THREAD_RET_TYPE calc_ibs_thread(void* arg) { |
| 1070 |
uintptr_t tidx = (intptr_t)arg; |
| 1071 |
uintptr_t ulii = g_thread_start[tidx]; |
| 1072 |
uintptr_t uljj = g_thread_start[0]; |
| 1073 |
uintptr_t offset = (((uint64_t)ulii) * (ulii - 1) - ((uint64_t)uljj) * (uljj - 1)) / 2; |
| 1074 |
uint32_t* weighted_missing_ptr = g_missing_tot_weights; |
| 1075 |
uint32_t* flat_missing_ptr = g_missing_dbl_excluded; |
| 1076 |
uint32_t* idists_ptr = (uint32_t*)(&(g_idists[offset])); |
| 1077 |
uintptr_t* geno_ptr = (uintptr_t*)g_geno; |
| 1078 |
uintptr_t* masks_ptr = g_masks; |
| 1079 |
uintptr_t* mmasks_ptr = g_mmasks; |
| 1080 |
uint32_t end_idx = g_thread_start[tidx + 1]; |
| 1081 |
uint32_t is_last_block; |
| 1082 |
if (weighted_missing_ptr) { |
| 1083 |
weighted_missing_ptr = &(weighted_missing_ptr[offset]); |
| 1084 |
} |
| 1085 |
if (flat_missing_ptr) { |
| 1086 |
flat_missing_ptr = &(flat_missing_ptr[offset]); |
| 1087 |
} |
| 1088 |
while (1) { |
| 1089 |
is_last_block = g_is_last_thread_block; |
| 1090 |
if (weighted_missing_ptr) { |
| 1091 |
|
| 1092 |
incr_wt_dist_missing(weighted_missing_ptr, g_subset_weights_i, mmasks_ptr, ulii, end_idx); |
| 1093 |
} |
| 1094 |
if (flat_missing_ptr) { |
| 1095 |
incr_dists_rm(flat_missing_ptr, mmasks_ptr, ulii, end_idx); |
| 1096 |
} |
| 1097 |
if (is_last_block || ((g_thread_spawn_ct % (MULTIPLEX_DIST / BITCT)) == ((MULTIPLEX_DIST / BITCT) - 1))) { |
| 1098 |
incr_dists_i(idists_ptr, geno_ptr, masks_ptr, ulii, end_idx); |
| 1099 |
} |
| 1100 |
if ((!tidx) || is_last_block) { |
| 1101 |
THREAD_RETURN; |
| 1102 |
} |
| 1103 |
THREAD_BLOCK_FINISH(tidx); |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
void incr_genome(uint32_t* genome_main, uintptr_t* geno, uintptr_t* masks, uintptr_t sample_ct, uint32_t start_idx, uint32_t end_idx) { |
| 1108 |
#ifdef __LP64__ |
| 1109 |
const __m128i m1 = {FIVEMASK, FIVEMASK}; |
| 1110 |
const __m128i m2 = {0x3333333333333333LLU, 0x3333333333333333LLU}; |
| 1111 |
const __m128i m4 = {0x0f0f0f0f0f0f0f0fLLU, 0x0f0f0f0f0f0f0f0fLLU}; |
| 1112 |
const __m128i m8 = {0x00ff00ff00ff00ffLLU, 0x00ff00ff00ff00ffLLU}; |
| 1113 |
__m128i xor_buf[GENOME_MULTIPLEX / BITCT]; |
| 1114 |
__m128i* xor_buf_end = &(xor_buf[GENOME_MULTIPLEX / BITCT]); |
| 1115 |
__m128i* maskptr; |
| 1116 |
__m128i* maskptr_fixed; |
| 1117 |
__m128i* maskptr_fixed_tmp; |
| 1118 |
__m128i* xor_ptr; |
| 1119 |
__m128i* glptr_fixed_tmp; |
| 1120 |
__m128i loader; |
| 1121 |
__m128i loader2; |
| 1122 |
__m128i loader3; |
| 1123 |
__m128i count_ibs1; |
| 1124 |
__m128i count_ibs0; |
| 1125 |
__m128i count2_ibs1; |
| 1126 |
__m128i count2_ibs0; |
| 1127 |
__univec acc_ibs1; |
| 1128 |
__univec acc_ibs0; |
| 1129 |
uintptr_t* lptr; |
| 1130 |
__m128i* glptr; |
| 1131 |
__m128i* glptr_fixed; |
| 1132 |
__m128i* glptr_end; |
| 1133 |
#else |
| 1134 |
uintptr_t* glptr; |
| 1135 |
uintptr_t* glptr_fixed; |
| 1136 |
uintptr_t* glptr_end; |
| 1137 |
uintptr_t* maskptr; |
| 1138 |
uintptr_t* maskptr_fixed; |
| 1139 |
uintptr_t* maskptr_fixed_tmp; |
| 1140 |
uintptr_t* glptr_fixed_tmp; |
| 1141 |
uintptr_t xor_buf[GENOME_MULTIPLEX2 / BITCT]; |
| 1142 |
uintptr_t* xor_buf_end = &(xor_buf[GENOME_MULTIPLEX2 / BITCT]); |
| 1143 |
uintptr_t* xor_ptr; |
| 1144 |
uint32_t bit_count_ibs1 = 0; |
| 1145 |
uint32_t bit_count_ibs0 = 0; |
| 1146 |
uintptr_t bitfield_ibs1; |
| 1147 |
uintptr_t bitfield_ibs0; |
| 1148 |
uintptr_t loader; |
| 1149 |
uintptr_t loader2; |
| 1150 |
uintptr_t tmp_stor_ibs1; |
| 1151 |
uintptr_t tmp_stor_ibs0; |
| 1152 |
#endif |
| 1153 |
uintptr_t* glptr_back; |
| 1154 |
uintptr_t ibs_incr; |
| 1155 |
uint32_t uii; |
| 1156 |
uint32_t ujj; |
| 1157 |
int32_t offset; |
| 1158 |
uintptr_t uland; |
| 1159 |
uintptr_t ulval; |
| 1160 |
uintptr_t next_ppc_marker_hybrid; |
| 1161 |
uintptr_t mask_fixed_test; |
| 1162 |
uintptr_t* marker_window_ptr; |
| 1163 |
int32_t lowct2 = g_ctrl_ct * 2; |
| 1164 |
int32_t highct2 = g_case_ct * 2; |
| 1165 |
#ifdef __LP64__ |
| 1166 |
glptr_end = (__m128i*)(&(geno[sample_ct * (GENOME_MULTIPLEX2 / BITCT)])); |
| 1167 |
#else |
| 1168 |
glptr_end = &(geno[sample_ct * (GENOME_MULTIPLEX2 / BITCT)]); |
| 1169 |
#endif |
| 1170 |
for (uii = start_idx; uii < end_idx; uii++) { |
| 1171 |
ujj = uii * (GENOME_MULTIPLEX2 / BITCT); |
| 1172 |
#ifdef __LP64__ |
| 1173 |
glptr_fixed = (__m128i*)(&(geno[ujj])); |
| 1174 |
glptr = (__m128i*)(&(geno[ujj + (GENOME_MULTIPLEX2 / BITCT)])); |
| 1175 |
lptr = &(masks[ujj]); |
| 1176 |
maskptr = (__m128i*)(&(masks[ujj + (GENOME_MULTIPLEX2 / BITCT)])); |
| 1177 |
maskptr_fixed = (__m128i*)lptr; |
| 1178 |
mask_fixed_test = *lptr++; |
| 1179 |
for (ujj = 0; ujj < GENOME_MULTIPLEX2 / BITCT - 1; ujj++) { |
| 1180 |
mask_fixed_test &= *lptr++; |
| 1181 |
} |
| 1182 |
#else |
| 1183 |
glptr_fixed = &(geno[ujj]); |
| 1184 |
glptr = &(geno[ujj + (GENOME_MULTIPLEX2 / BITCT)]); |
| 1185 |
maskptr_fixed = &(masks[ujj]); |
| 1186 |
maskptr = maskptr_fixed; |
| 1187 |
mask_fixed_test = *maskptr++; |
| 1188 |
for (ujj = 0; ujj < GENOME_MULTIPLEX2 / BITCT - 1; ujj++) { |
| 1189 |
mask_fixed_test &= *maskptr++; |
| 1190 |
} |
| 1191 |
#endif |
| 1192 |
if (~mask_fixed_test) { |
| 1193 |
while (glptr < glptr_end) { |
| 1194 |
xor_ptr = xor_buf; |
| 1195 |
glptr_back = (uintptr_t*)glptr; |
| 1196 |
glptr_fixed_tmp = glptr_fixed; |
| 1197 |
maskptr_fixed_tmp = maskptr_fixed; |
| 1198 |
#ifdef __LP64__ |
| 1199 |
acc_ibs1.vi = _mm_setzero_si128(); |
| 1200 |
acc_ibs0.vi = _mm_setzero_si128(); |
| 1201 |
do { |
| 1202 |
loader = _mm_and_si128(_mm_xor_si128(*glptr++, *glptr_fixed_tmp++), _mm_and_si128(*maskptr++, *maskptr_fixed_tmp++)); |
| 1203 |
loader2 = _mm_srli_epi64(loader, 1); |
| 1204 |
count_ibs1 = _mm_and_si128(_mm_xor_si128(loader, loader2), m1); |
| 1205 |
count_ibs0 = _mm_and_si128(_mm_and_si128(loader, loader2), m1); |
| 1206 |
*xor_ptr++ = count_ibs0; |
| 1207 |
|
| 1208 |
loader = _mm_and_si128(_mm_xor_si128(*glptr++, *glptr_fixed_tmp++), _mm_and_si128(*maskptr++, *maskptr_fixed_tmp++)); |
| 1209 |
loader2 = _mm_srli_epi64(loader, 1); |
| 1210 |
count_ibs1 = _mm_add_epi64(count_ibs1, _mm_and_si128(_mm_xor_si128(loader, loader2), m1)); |
| 1211 |
loader3 = _mm_and_si128(_mm_and_si128(loader, loader2), m1); |
| 1212 |
*xor_ptr++ = loader3; |
| 1213 |
count_ibs0 = _mm_add_epi64(count_ibs0, loader3); |
| 1214 |
|
| 1215 |
loader = _mm_and_si128(_mm_xor_si128(*glptr++, *glptr_fixed_tmp++), _mm_and_si128(*maskptr++, *maskptr_fixed_tmp++)); |
| 1216 |
loader2 = _mm_srli_epi64(loader, 1); |
| 1217 |
count_ibs1 = _mm_add_epi64(count_ibs1, _mm_and_si128(_mm_xor_si128(loader, loader2), m1)); |
| 1218 |
loader3 = _mm_and_si128(_mm_and_si128(loader, loader2), m1); |
| 1219 |
*xor_ptr++ = loader3; |
| 1220 |
count_ibs0 = _mm_add_epi64(count_ibs0, loader3); |
| 1221 |
|
| 1222 |
loader = _mm_and_si128(_mm_xor_si128(*glptr++, *glptr_fixed_tmp++), _mm_and_si128(*maskptr++, *maskptr_fixed_tmp++)); |
| 1223 |
loader2 = _mm_srli_epi64(loader, 1); |
| 1224 |
count2_ibs1 = _mm_and_si128(_mm_xor_si128(loader, loader2), m1); |
| 1225 |
count2_ibs0 = _mm_and_si128(_mm_and_si128(loader, loader2), m1); |
| 1226 |
*xor_ptr++ = count2_ibs0; |
| 1227 |
|
| 1228 |
loader = _mm_and_si128(_mm_xor_si128(*glptr++, *glptr_fixed_tmp++), _mm_and_si128(*maskptr++, *maskptr_fixed_tmp++)); |
| 1229 |
loader2 = _mm_srli_epi64(loader, 1); |
| 1230 |
count2_ibs1 = _mm_add_epi64(count2_ibs1, _mm_and_si128(_mm_xor_si128(loader, loader2), m1)); |
| 1231 |
loader3 = _mm_and_si128(_mm_and_si128(loader, loader2), m1); |
| 1232 |
*xor_ptr++ = loader3; |
| 1233 |
count2_ibs0 = _mm_add_epi64(count2_ibs0, loader3); |
| 1234 |
|
| 1235 |
loader = _mm_and_si128(_mm_xor_si128(*glptr++, *glptr_fixed_tmp++), _mm_and_si128(*maskptr++, *maskptr_fixed_tmp++)); |
| 1236 |
loader2 = _mm_srli_epi64(loader, 1); |
| 1237 |
count2_ibs1 = _mm_add_epi64(count2_ibs1, _mm_and_si128(_mm_xor_si128(loader, loader2), m1)); |
| 1238 |
loader3 = _mm_and_si128(_mm_and_si128(loader, loader2), m1); |
| 1239 |
*xor_ptr++ = loader3; |
| 1240 |
count2_ibs0 = _mm_add_epi64(count2_ibs0, loader3); |
| 1241 |
|
| 1242 |
count_ibs1 = _mm_add_epi64(_mm_and_si128(count_ibs1, m2), _mm_and_si128(_mm_srli_epi64(count_ibs1, 2), m2)); |
| 1243 |
count_ibs0 = _mm_add_epi64(_mm_and_si128(count_ibs0, m2), _mm_and_si128(_mm_srli_epi64(count_ibs0, 2), m2)); |
| 1244 |
count_ibs1 = _mm_add_epi64(count_ibs1, _mm_add_epi64(_mm_and_si128(count2_ibs1, m2), _mm_and_si128(_mm_srli_epi64(count2_ibs1, 2), m2))); |
| 1245 |
count_ibs0 = _mm_add_epi64(count_ibs0, _mm_add_epi64(_mm_and_si128(count2_ibs0, m2), _mm_and_si128(_mm_srli_epi64(count2_ibs0, 2), m2))); |
| 1246 |
acc_ibs1.vi = _mm_add_epi64(acc_ibs1.vi, _mm_add_epi64(_mm_and_si128(count_ibs1, m4), _mm_and_si128(_mm_srli_epi64(count_ibs1, 4), m4))); |
| 1247 |
acc_ibs0.vi = _mm_add_epi64(acc_ibs0.vi, _mm_add_epi64(_mm_and_si128(count_ibs0, m4), _mm_and_si128(_mm_srli_epi64(count_ibs0, 4), m4))); |
| 1248 |
} while (xor_ptr < xor_buf_end); |
| 1249 |
#if GENOME_MULTIPLEX > 1920 |
| 1250 |
acc_ibs1.vi = _mm_add_epi64(_mm_and_si128(acc_ibs1.vi, m8), _mm_and_si128(_mm_srli_epi64(acc_ibs1.vi, 8), m8)); |
| 1251 |
acc_ibs0.vi = _mm_add_epi64(_mm_and_si128(acc_ibs0.vi, m8), _mm_and_si128(_mm_srli_epi64(acc_ibs0.vi, 8), m8)); |
| 1252 |
#else |
| 1253 |
acc_ibs1.vi = _mm_and_si128(_mm_add_epi64(acc_ibs1.vi, _mm_srli_epi64(acc_ibs1.vi, 8)), m8); |
| 1254 |
acc_ibs0.vi = _mm_and_si128(_mm_add_epi64(acc_ibs0.vi, _mm_srli_epi64(acc_ibs0.vi, 8)), m8); |
| 1255 |
#endif |
| 1256 |
*genome_main += ((acc_ibs1.u8[0] + acc_ibs1.u8[1]) * 0x1000100010001LLU) >> 48; |
| 1257 |
genome_main++; |
| 1258 |
*genome_main += ((acc_ibs0.u8[0] + acc_ibs0.u8[1]) * 0x1000100010001LLU) >> 48; |
| 1259 |
#else |
| 1260 |
bit_count_ibs1 = 0; |
| 1261 |
bit_count_ibs0 = 0; |
| 1262 |
do { |
| 1263 |
loader = ((*glptr++) ^ (*glptr_fixed_tmp++)) & ((*maskptr++) & (*maskptr_fixed_tmp++)); |
| 1264 |
loader2 = loader >> 1; |
| 1265 |
bitfield_ibs1 = (loader ^ loader2) & FIVEMASK; |
| 1266 |
bitfield_ibs0 = (loader & loader2) & FIVEMASK; |
| 1267 |
*xor_ptr++ = bitfield_ibs0; |
| 1268 |
loader = ((*glptr++) ^ (*glptr_fixed_tmp++)) & ((*maskptr++) & (*maskptr_fixed_tmp++)); |
| 1269 |
loader2 = loader >> 1; |
| 1270 |
bitfield_ibs1 += (loader ^ loader2) & FIVEMASK; |
| 1271 |
loader2 = (loader & loader2) & FIVEMASK; |
| 1272 |
bitfield_ibs0 += loader2; |
| 1273 |
*xor_ptr++ = loader2; |
| 1274 |
loader = ((*glptr++) ^ (*glptr_fixed_tmp++)) & ((*maskptr++) & (*maskptr_fixed_tmp++)); |
| 1275 |
loader2 = loader >> 1; |
| 1276 |
bitfield_ibs1 += (loader ^ loader2) & FIVEMASK; |
| 1277 |
loader2 = (loader & loader2) & FIVEMASK; |
| 1278 |
bitfield_ibs0 += loader2; |
| 1279 |
*xor_ptr++ = loader2; |
| 1280 |
bitfield_ibs1 = (bitfield_ibs1 & 0x33333333) + ((bitfield_ibs1 >> 2) & 0x33333333); |
| 1281 |
bitfield_ibs0 = (bitfield_ibs0 & 0x33333333) + ((bitfield_ibs0 >> 2) & 0x33333333); |
| 1282 |
tmp_stor_ibs1 = (bitfield_ibs1 + (bitfield_ibs1 >> 4)) & 0x0f0f0f0f; |
| 1283 |
tmp_stor_ibs0 = (bitfield_ibs0 + (bitfield_ibs0 >> 4)) & 0x0f0f0f0f; |
| 1284 |
|
| 1285 |
bit_count_ibs1 += (tmp_stor_ibs1 * 0x01010101) >> 24; |
| 1286 |
bit_count_ibs0 += (tmp_stor_ibs0 * 0x01010101) >> 24; |
| 1287 |
} while (xor_ptr < xor_buf_end); |
| 1288 |
*genome_main += bit_count_ibs1; |
| 1289 |
genome_main++; |
| 1290 |
*genome_main += bit_count_ibs0; |
| 1291 |
#endif |
| 1292 |
genome_main++; |
| 1293 |
next_ppc_marker_hybrid = *genome_main - lowct2; |
| 1294 |
if (next_ppc_marker_hybrid < GENOME_MULTIPLEX2) { |
| 1295 |
ibs_incr = 0; |
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
do { |
| 1309 |
offset = next_ppc_marker_hybrid / BITCT; |
| 1310 |
marker_window_ptr = &(g_marker_window[offset * BITCT]); |
| 1311 |
next_ppc_marker_hybrid = ~ZEROLU << (next_ppc_marker_hybrid & (BITCT - 1)); |
| 1312 |
incr_genome_2mask_loop: |
| 1313 |
uland = glptr_back[offset] & (((uintptr_t*)glptr_fixed)[offset]); |
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
ulval = (((uland & (uland << 1)) & AAAAMASK) | (((uintptr_t*)xor_buf)[offset])); |
| 1327 |
do { |
| 1328 |
ulval &= next_ppc_marker_hybrid; |
| 1329 |
if (ulval) { |
| 1330 |
ujj = CTZLU(ulval); |
| 1331 |
next_ppc_marker_hybrid = marker_window_ptr[ujj]; |
| 1332 |
ibs_incr += (ONELU << ((ujj & 1) * BITCT2)); |
| 1333 |
} else if (offset < ((GENOME_MULTIPLEX2 - BITCT) / BITCT)) { |
| 1334 |
offset++; |
| 1335 |
next_ppc_marker_hybrid = ~ZEROLU; |
| 1336 |
marker_window_ptr = &(marker_window_ptr[BITCT]); |
| 1337 |
goto incr_genome_2mask_loop; |
| 1338 |
} else { |
| 1339 |
*genome_main = highct2; |
| 1340 |
goto incr_genome_2mask_exit; |
| 1341 |
} |
| 1342 |
} while (next_ppc_marker_hybrid & (ONELU << (BITCT - 1))); |
| 1343 |
} while (next_ppc_marker_hybrid < GENOME_MULTIPLEX2); |
| 1344 |
*genome_main = next_ppc_marker_hybrid + lowct2; |
| 1345 |
incr_genome_2mask_exit: |
| 1346 |
genome_main++; |
| 1347 |
*genome_main += ibs_incr & ((~ZEROLU) >> BITCT2); |
| 1348 |
genome_main++; |
| 1349 |
*genome_main += ibs_incr >> BITCT2; |
| 1350 |
genome_main++; |
| 1351 |
} else { |
| 1352 |
genome_main = &(genome_main[3]); |
| 1353 |
} |
| 1354 |
} |
| 1355 |
} else { |
| 1356 |
while (glptr < glptr_end) { |
| 1357 |
xor_ptr = xor_buf; |
| 1358 |
glptr_back = (uintptr_t*)glptr; |
| 1359 |
glptr_fixed_tmp = glptr_fixed; |
| 1360 |
#ifdef __LP64__ |
| 1361 |
acc_ibs1.vi = _mm_setzero_si128(); |
| 1362 |
acc_ibs0.vi = _mm_setzero_si128(); |
| 1363 |
do { |
| 1364 |
loader = _mm_and_si128(_mm_xor_si128(*glptr++, *glptr_fixed_tmp++), *maskptr++); |
| 1365 |
loader2 = _mm_srli_epi64(loader, 1); |
| 1366 |
count_ibs1 = _mm_and_si128(_mm_xor_si128(loader, loader2), m1); |
| 1367 |
count_ibs0 = _mm_and_si128(_mm_and_si128(loader, loader2), m1); |
| 1368 |
*xor_ptr++ = count_ibs0; |
| 1369 |
loader = _mm_and_si128(_mm_xor_si128(*glptr++, *glptr_fixed_tmp++), *maskptr++); |
| 1370 |
loader2 = _mm_srli_epi64(loader, 1); |
| 1371 |
count_ibs1 = _mm_add_epi64(count_ibs1, _mm_and_si128(_mm_xor_si128(loader, loader2), m1)); |
| 1372 |
loader3 = _mm_and_si128(_mm_and_si128(loader, loader2), m1); |
| 1373 |
*xor_ptr++ = loader3; |
| 1374 |
count_ibs0 = _mm_add_epi64(count_ibs0, loader3); |
| 1375 |
loader = _mm_and_si128(_mm_xor_si128(*glptr++, *glptr_fixed_tmp++), *maskptr++); |
| 1376 |
loader2 = _mm_srli_epi64(loader, 1); |
| 1377 |
count_ibs1 = _mm_add_epi64(count_ibs1, _mm_and_si128(_mm_xor_si128(loader, loader2), m1)); |
| 1378 |
loader3 = _mm_and_si128(_mm_and_si128(loader, loader2), m1); |
| 1379 |
*xor_ptr++ = loader3; |
| 1380 |
count_ibs0 = _mm_add_epi64(count_ibs0, loader3); |
| 1381 |
|
| 1382 |
loader = _mm_and_si128(_mm_xor_si128(*glptr++, *glptr_fixed_tmp++), *maskptr++); |
| 1383 |
loader2 = _mm_srli_epi64(loader, 1); |
| 1384 |
count2_ibs1 = _mm_and_si128(_mm_xor_si128(loader, loader2), m1); |
| 1385 |
count2_ibs0 = _mm_and_si128(_mm_and_si128(loader, loader2), m1); |
| 1386 |
*xor_ptr++ = count2_ibs0; |
| 1387 |
loader = _mm_and_si128(_mm_xor_si128(*glptr++, *glptr_fixed_tmp++), *maskptr++); |
| 1388 |
loader2 = _mm_srli_epi64(loader, 1); |
| 1389 |
count2_ibs1 = _mm_add_epi64(count2_ibs1, _mm_and_si128(_mm_xor_si128(loader, loader2), m1)); |
| 1390 |
loader3 = _mm_and_si128(_mm_and_si128(loader, loader2), m1); |
| 1391 |
*xor_ptr++ = loader3; |
| 1392 |
count2_ibs0 = _mm_add_epi64(count2_ibs0, loader3); |
| 1393 |
loader = _mm_and_si128(_mm_xor_si128(*glptr++, *glptr_fixed_tmp++), *maskptr++); |
| 1394 |
loader2 = _mm_srli_epi64(loader, 1); |
| 1395 |
count2_ibs1 = _mm_add_epi64(count2_ibs1, _mm_and_si128(_mm_xor_si128(loader, loader2), m1)); |
| 1396 |
loader3 = _mm_and_si128(_mm_and_si128(loader, loader2), m1); |
| 1397 |
*xor_ptr++ = loader3; |
| 1398 |
count2_ibs0 = _mm_add_epi64(count2_ibs0, loader3); |
| 1399 |
|
| 1400 |
count_ibs1 = _mm_add_epi64(_mm_and_si128(count_ibs1, m2), _mm_and_si128(_mm_srli_epi64(count_ibs1, 2), m2)); |
| 1401 |
count_ibs0 = _mm_add_epi64(_mm_and_si128(count_ibs0, m2), _mm_and_si128(_mm_srli_epi64(count_ibs0, 2), m2)); |
| 1402 |
count_ibs1 = _mm_add_epi64(count_ibs1, _mm_add_epi64(_mm_and_si128(count2_ibs1, m2), _mm_and_si128(_mm_srli_epi64(count2_ibs1, 2), m2))); |
| 1403 |
count_ibs0 = _mm_add_epi64(count_ibs0, _mm_add_epi64(_mm_and_si128(count2_ibs0, m2), _mm_and_si128(_mm_srli_epi64(count2_ibs0, 2), m2))); |
| 1404 |
acc_ibs1.vi = _mm_add_epi64(acc_ibs1.vi, _mm_add_epi64(_mm_and_si128(count_ibs1, m4), _mm_and_si128(_mm_srli_epi64(count_ibs1, 4), m4))); |
| 1405 |
acc_ibs0.vi = _mm_add_epi64(acc_ibs0.vi, _mm_add_epi64(_mm_and_si128(count_ibs0, m4), _mm_and_si128(_mm_srli_epi64(count_ibs0, 4), m4))); |
| 1406 |
} while (xor_ptr < xor_buf_end); |
| 1407 |
#if GENOME_MULTIPLEX > 1920 |
| 1408 |
acc_ibs1.vi = _mm_add_epi64(_mm_and_si128(acc_ibs1.vi, m8), _mm_and_si128(_mm_srli_epi64(acc_ibs1.vi, 8), m8)); |
| 1409 |
acc_ibs0.vi = _mm_add_epi64(_mm_and_si128(acc_ibs0.vi, m8), _mm_and_si128(_mm_srli_epi64(acc_ibs0.vi, 8), m8)); |
| 1410 |
#else |
| 1411 |
acc_ibs1.vi = _mm_and_si128(_mm_add_epi64(acc_ibs1.vi, _mm_srli_epi64(acc_ibs1.vi, 8)), m8); |
| 1412 |
acc_ibs0.vi = _mm_and_si128(_mm_add_epi64(acc_ibs0.vi, _mm_srli_epi64(acc_ibs0.vi, 8)), m8); |
| 1413 |
#endif |
| 1414 |
*genome_main += ((acc_ibs1.u8[0] + acc_ibs1.u8[1]) * 0x1000100010001LLU) >> 48; |
| 1415 |
genome_main++; |
| 1416 |
*genome_main += ((acc_ibs0.u8[0] + acc_ibs0.u8[1]) * 0x1000100010001LLU) >> 48; |
| 1417 |
#else |
| 1418 |
bit_count_ibs1 = 0; |
| 1419 |
bit_count_ibs0 = 0; |
| 1420 |
do { |
| 1421 |
loader = ((*glptr++) ^ (*glptr_fixed_tmp++)) & (*maskptr++); |
| 1422 |
loader2 = loader >> 1; |
| 1423 |
bitfield_ibs1 = (loader ^ loader2) & FIVEMASK; |
| 1424 |
bitfield_ibs0 = (loader & loader2) & FIVEMASK; |
| 1425 |
*xor_ptr++ = bitfield_ibs0; |
| 1426 |
loader = ((*glptr++) ^ (*glptr_fixed_tmp++)) & (*maskptr++); |
| 1427 |
loader2 = loader >> 1; |
| 1428 |
bitfield_ibs1 += (loader ^ loader2) & FIVEMASK; |
| 1429 |
loader2 = (loader & loader2) & FIVEMASK; |
| 1430 |
bitfield_ibs0 += loader2; |
| 1431 |
*xor_ptr++ = loader2; |
| 1432 |
loader = ((*glptr++) ^ (*glptr_fixed_tmp++)) & (*maskptr++); |
| 1433 |
loader2 = loader >> 1; |
| 1434 |
bitfield_ibs1 += (loader ^ loader2) & FIVEMASK; |
| 1435 |
loader2 = (loader & loader2) & FIVEMASK; |
| 1436 |
bitfield_ibs0 += loader2; |
| 1437 |
*xor_ptr++ = loader2; |
| 1438 |
bitfield_ibs1 = (bitfield_ibs1 & 0x33333333) + ((bitfield_ibs1 >> 2) & 0x33333333); |
| 1439 |
bitfield_ibs0 = (bitfield_ibs0 & 0x33333333) + ((bitfield_ibs0 >> 2) & 0x33333333); |
| 1440 |
tmp_stor_ibs1 = (bitfield_ibs1 + (bitfield_ibs1 >> 4)) & 0x0f0f0f0f; |
| 1441 |
tmp_stor_ibs0 = (bitfield_ibs0 + (bitfield_ibs0 >> 4)) & 0x0f0f0f0f; |
| 1442 |
|
| 1443 |
bit_count_ibs1 += (tmp_stor_ibs1 * 0x01010101) >> 24; |
| 1444 |
bit_count_ibs0 += (tmp_stor_ibs0 * 0x01010101) >> 24; |
| 1445 |
} while (xor_ptr < xor_buf_end); |
| 1446 |
*genome_main += bit_count_ibs1; |
| 1447 |
genome_main++; |
| 1448 |
*genome_main += bit_count_ibs0; |
| 1449 |
#endif |
| 1450 |
genome_main++; |
| 1451 |
next_ppc_marker_hybrid = *genome_main - lowct2; |
| 1452 |
if (next_ppc_marker_hybrid < GENOME_MULTIPLEX2) { |
| 1453 |
ibs_incr = 0; |
| 1454 |
do { |
| 1455 |
offset = next_ppc_marker_hybrid / BITCT; |
| 1456 |
marker_window_ptr = &(g_marker_window[offset * BITCT]); |
| 1457 |
next_ppc_marker_hybrid = (~ZEROLU) << (next_ppc_marker_hybrid & (BITCT - 1)); |
| 1458 |
incr_genome_1mask_loop: |
| 1459 |
uland = glptr_back[offset] & (((uintptr_t*)glptr_fixed)[offset]); |
| 1460 |
ulval = ((uland & (uland << 1)) & AAAAMASK) | (((uintptr_t*)xor_buf)[offset]); |
| 1461 |
do { |
| 1462 |
ulval &= next_ppc_marker_hybrid; |
| 1463 |
if (ulval) { |
| 1464 |
ujj = CTZLU(ulval); |
| 1465 |
next_ppc_marker_hybrid = marker_window_ptr[ujj]; |
| 1466 |
ibs_incr += (ONELU << ((ujj & 1) * BITCT2)); |
| 1467 |
} else if (offset < ((GENOME_MULTIPLEX2 - BITCT) / BITCT)) { |
| 1468 |
offset++; |
| 1469 |
next_ppc_marker_hybrid = ~ZEROLU; |
| 1470 |
marker_window_ptr = &(marker_window_ptr[BITCT]); |
| 1471 |
goto incr_genome_1mask_loop; |
| 1472 |
} else { |
| 1473 |
*genome_main = highct2; |
| 1474 |
goto incr_genome_1mask_exit; |
| 1475 |
} |
| 1476 |
} while (next_ppc_marker_hybrid & (ONELU << (BITCT - 1))); |
| 1477 |
} while (next_ppc_marker_hybrid < GENOME_MULTIPLEX2); |
| 1478 |
*genome_main = next_ppc_marker_hybrid + lowct2; |
| 1479 |
incr_genome_1mask_exit: |
| 1480 |
genome_main++; |
| 1481 |
*genome_main += ibs_incr & ((~ZEROLU) >> BITCT2); |
| 1482 |
genome_main++; |
| 1483 |
*genome_main += ibs_incr >> BITCT2; |
| 1484 |
genome_main++; |
| 1485 |
} else { |
| 1486 |
genome_main = &(genome_main[3]); |
| 1487 |
} |
| 1488 |
} |
| 1489 |
} |
| 1490 |
} |
| 1491 |
} |
| 1492 |
|
| 1493 |
void incr_dists_rm_inv(uint32_t* idists, uintptr_t* mmasks, uintptr_t sample_ct_m1, uint32_t start_idx, uint32_t end_idx) { |
| 1494 |
|
| 1495 |
uintptr_t* glptr; |
| 1496 |
uintptr_t ulii; |
| 1497 |
uintptr_t uljj; |
| 1498 |
uint32_t uii; |
| 1499 |
uint32_t ujj; |
| 1500 |
uint32_t ukk; |
| 1501 |
for (uii = start_idx; uii < end_idx; uii++) { |
| 1502 |
ulii = mmasks[uii]; |
| 1503 |
ukk = sample_ct_m1 - uii; |
| 1504 |
if (ulii) { |
| 1505 |
glptr = &(mmasks[uii + 1]); |
| 1506 |
for (ujj = 0; ujj < ukk; ujj++) { |
| 1507 |
uljj = (*glptr++) & ulii; |
| 1508 |
if (uljj) { |
| 1509 |
idists[ujj] += popcount_long(uljj); |
| 1510 |
} |
| 1511 |
} |
| 1512 |
} |
| 1513 |
idists = &(idists[ukk]); |
| 1514 |
} |
| 1515 |
} |
| 1516 |
|
| 1517 |
THREAD_RET_TYPE calc_genome_thread(void* arg) { |
| 1518 |
uintptr_t tidx = (uintptr_t)arg; |
| 1519 |
uintptr_t sample_ct = g_sample_ct; |
| 1520 |
uintptr_t sample_ct_m1 = sample_ct - 1; |
| 1521 |
uintptr_t ulii = g_thread_start[tidx]; |
| 1522 |
uintptr_t uljj = g_thread_start[0]; |
| 1523 |
|
| 1524 |
|
| 1525 |
uintptr_t offsetm = ((uint64_t)sample_ct) * (ulii - uljj) - ((((uint64_t)ulii) * (ulii + 1) - ((uint64_t)uljj) * (uljj + 1)) / 2); |
| 1526 |
uintptr_t offset = offsetm * 5; |
| 1527 |
uint32_t* missing_ptr = &(g_missing_dbl_excluded[offsetm]); |
| 1528 |
uint32_t* genome_main_ptr = &(g_genome_main[offset]); |
| 1529 |
uintptr_t* geno_ptr = (uintptr_t*)g_geno; |
| 1530 |
uintptr_t* masks_ptr = g_masks; |
| 1531 |
uintptr_t* mmasks_ptr = g_mmasks; |
| 1532 |
uint32_t end_idx = g_thread_start[tidx + 1]; |
| 1533 |
uint32_t is_last_block; |
| 1534 |
while (1) { |
| 1535 |
is_last_block = g_is_last_thread_block; |
| 1536 |
incr_dists_rm_inv(missing_ptr, mmasks_ptr, sample_ct_m1, ulii, end_idx); |
| 1537 |
if (is_last_block || ((g_thread_spawn_ct % (GENOME_MULTIPLEX / BITCT)) == (GENOME_MULTIPLEX / BITCT) - 1)) { |
| 1538 |
incr_genome(genome_main_ptr, geno_ptr, masks_ptr, sample_ct, ulii, end_idx); |
| 1539 |
} |
| 1540 |
if ((!tidx) || is_last_block) { |
| 1541 |
THREAD_RETURN; |
| 1542 |
} |
| 1543 |
THREAD_BLOCK_FINISH(tidx); |
| 1544 |
} |
| 1545 |
} |
| 1546 |
|
| 1547 |
void incr_dists(double* dists, uintptr_t* geno, uintptr_t* masks, double* weights, uint32_t start_idx, uint32_t end_idx) { |
| 1548 |
uintptr_t* glptr; |
| 1549 |
uintptr_t ulii; |
| 1550 |
uintptr_t mask_fixed; |
| 1551 |
uintptr_t uljj; |
| 1552 |
uintptr_t* mptr; |
| 1553 |
double* weights1 = &(weights[16384]); |
| 1554 |
#ifdef __LP64__ |
| 1555 |
double* weights2 = &(weights[32768]); |
| 1556 |
double* weights3 = &(weights[36864]); |
| 1557 |
double* weights4 = &(weights[40960]); |
| 1558 |
#endif |
| 1559 |
uint32_t uii; |
| 1560 |
uint32_t ujj; |
| 1561 |
for (uii = start_idx; uii < end_idx; uii++) { |
| 1562 |
glptr = geno; |
| 1563 |
ulii = geno[uii]; |
| 1564 |
mptr = masks; |
| 1565 |
mask_fixed = masks[uii]; |
| 1566 |
#ifdef __LP64__ |
| 1567 |
if (mask_fixed == ~ZEROLU) { |
| 1568 |
for (ujj = 0; ujj < uii; ujj++) { |
| 1569 |
uljj = (*glptr++ ^ ulii) & (*mptr++); |
| 1570 |
*dists += weights4[uljj >> 52] + weights3[(uljj >> 40) & 4095] + weights2[(uljj >> 28) & 4095] + weights1[(uljj >> 14) & 16383] + weights[uljj & 16383]; |
| 1571 |
dists++; |
| 1572 |
} |
| 1573 |
} else { |
| 1574 |
for (ujj = 0; ujj < uii; ujj++) { |
| 1575 |
uljj = (*glptr++ ^ ulii) & (mask_fixed & (*mptr++)); |
| 1576 |
*dists += weights4[uljj >> 52] + weights3[(uljj >> 40) & 4095] + weights2[(uljj >> 28) & 4095] + weights1[(uljj >> 14) & 16383] + weights[uljj & 16383]; |
| 1577 |
dists++; |
| 1578 |
} |
| 1579 |
} |
| 1580 |
#else |
| 1581 |
if (mask_fixed == 0x0fffffff) { |
| 1582 |
for (ujj = 0; ujj < uii; ujj++) { |
| 1583 |
uljj = (*glptr++ ^ ulii) & (*mptr++); |
| 1584 |
*dists += weights1[uljj >> 14] + weights[uljj & 16383]; |
| 1585 |
dists++; |
| 1586 |
} |
| 1587 |
} else { |
| 1588 |
for (ujj = 0; ujj < uii; ujj++) { |
| 1589 |
uljj = (*glptr++ ^ ulii) & (mask_fixed & (*mptr++)); |
| 1590 |
*dists += weights1[uljj >> 14] + weights[uljj & 16383]; |
| 1591 |
dists++; |
| 1592 |
} |
| 1593 |
} |
| 1594 |
#endif |
| 1595 |
} |
| 1596 |
} |
| 1597 |
|
| 1598 |
THREAD_RET_TYPE calc_wdist_thread(void* arg) { |
| 1599 |
uintptr_t tidx = (uintptr_t)arg; |
| 1600 |
uintptr_t ulii = g_thread_start[tidx]; |
| 1601 |
uintptr_t uljj = g_thread_start[0]; |
| 1602 |
uintptr_t offset = (((uint64_t)ulii) * (ulii - 1) - ((uint64_t)uljj) * (uljj - 1)) / 2; |
| 1603 |
double* dists_ptr = &(g_dists[offset]); |
| 1604 |
uintptr_t* geno_ptr = (uintptr_t*)g_geno; |
| 1605 |
uintptr_t* masks_ptr = g_masks; |
| 1606 |
uintptr_t* mmasks_ptr = g_mmasks; |
| 1607 |
double* subset_weights_ptr = g_subset_weights; |
| 1608 |
uint32_t* subset_weights_i_ptr = g_subset_weights_i; |
| 1609 |
uint32_t* weighted_missing_ptr = &(g_missing_tot_weights[offset]); |
| 1610 |
uint32_t end_idx = g_thread_start[tidx + 1]; |
| 1611 |
uint32_t is_last_block; |
| 1612 |
while (1) { |
| 1613 |
is_last_block = g_is_last_thread_block; |
| 1614 |
incr_dists(dists_ptr, geno_ptr, masks_ptr, subset_weights_ptr, ulii, end_idx); |
| 1615 |
if (is_last_block || (g_thread_spawn_ct & 1)) { |
| 1616 |
|
| 1617 |
incr_wt_dist_missing(weighted_missing_ptr, subset_weights_i_ptr, mmasks_ptr, ulii, end_idx); |
| 1618 |
} |
| 1619 |
if ((!tidx) || is_last_block) { |
| 1620 |
THREAD_RETURN; |
| 1621 |
} |
| 1622 |
THREAD_BLOCK_FINISH(tidx); |
| 1623 |
} |
| 1624 |
} |
| 1625 |
|
| 1626 |
void incr_dists_r(double* dists, uintptr_t* geno, uintptr_t* masks, uint32_t tidx, double* weights) { |
| 1627 |
uintptr_t* glptr; |
| 1628 |
uintptr_t* maskptr; |
| 1629 |
uintptr_t ulii; |
| 1630 |
uintptr_t uljj; |
| 1631 |
uintptr_t basemask; |
| 1632 |
double* weights1 = &(weights[32768]); |
| 1633 |
#ifdef __LP64__ |
| 1634 |
double* weights2 = &(weights[65536]); |
| 1635 |
double* weights3 = &(weights[98304]); |
| 1636 |
#endif |
| 1637 |
uint32_t uii; |
| 1638 |
uint32_t ujj; |
| 1639 |
for (uii = g_thread_start[tidx]; uii < g_thread_start[tidx + 1]; uii++) { |
| 1640 |
glptr = geno; |
| 1641 |
ulii = geno[uii]; |
| 1642 |
maskptr = masks; |
| 1643 |
basemask = masks[uii]; |
| 1644 |
if (!basemask) { |
| 1645 |
for (ujj = 0; ujj < uii; ujj++) { |
| 1646 |
uljj = ((*glptr++) + ulii) | (*maskptr++); |
| 1647 |
#ifdef __LP64__ |
| 1648 |
*dists += weights[(uint16_t)uljj] + weights1[(uint16_t)(uljj >> 16)] + weights2[(uint16_t)(uljj >> 32)] + weights3[uljj >> 48]; |
| 1649 |
#else |
| 1650 |
*dists += weights[(uint16_t)uljj] + weights1[uljj >> 16]; |
| 1651 |
#endif |
| 1652 |
dists++; |
| 1653 |
} |
| 1654 |
} else { |
| 1655 |
for (ujj = 0; ujj < uii; ujj++) { |
| 1656 |
uljj = ((*glptr++) + ulii) | ((*maskptr++) | basemask); |
| 1657 |
#ifdef __LP64__ |
| 1658 |
*dists += weights[(uint16_t)uljj] + weights1[(uint16_t)(uljj >> 16)] + weights2[(uint16_t)(uljj >> 32)] + weights3[uljj >> 48]; |
| 1659 |
#else |
| 1660 |
*dists += weights[(uint16_t)uljj] + weights1[uljj >> 16]; |
| 1661 |
#endif |
| 1662 |
dists++; |
| 1663 |
} |
| 1664 |
} |
| 1665 |
} |
| 1666 |
} |
| 1667 |
|
| 1668 |
THREAD_RET_TYPE calc_rel_thread(void* arg) { |
| 1669 |
uintptr_t tidx = (uintptr_t)arg; |
| 1670 |
uintptr_t ulii = g_thread_start[tidx]; |
| 1671 |
uintptr_t uljj = g_thread_start[0]; |
| 1672 |
uintptr_t offset = (((uint64_t)ulii) * (ulii - 1) - ((uint64_t)uljj) * (uljj - 1)) / 2; |
| 1673 |
double* rel_ptr = &(g_rel_dists[offset]); |
| 1674 |
uintptr_t* geno_ptr = (uintptr_t*)g_geno; |
| 1675 |
uintptr_t* masks_ptr = g_masks; |
| 1676 |
uintptr_t* mmasks_ptr = g_mmasks; |
| 1677 |
uint32_t* missing_ptr = &(g_missing_dbl_excluded[offset]); |
| 1678 |
double* subset_weights_ptr = g_subset_weights; |
| 1679 |
uint32_t end_idx = g_thread_start[tidx + 1]; |
| 1680 |
uint32_t is_last_block; |
| 1681 |
while (1) { |
| 1682 |
is_last_block = g_is_last_thread_block; |
| 1683 |
incr_dists_r(rel_ptr, geno_ptr, masks_ptr, (uint32_t)tidx, subset_weights_ptr); |
| 1684 |
if (is_last_block || ((g_thread_spawn_ct % 3) == 2)) { |
| 1685 |
incr_dists_rm(missing_ptr, mmasks_ptr, ulii, end_idx); |
| 1686 |
} |
| 1687 |
if ((!tidx) || is_last_block) { |
| 1688 |
THREAD_RETURN; |
| 1689 |
} |
| 1690 |
THREAD_BLOCK_FINISH(tidx); |
| 1691 |
} |
| 1692 |
} |
| 1693 |
|
| 1694 |
THREAD_RET_TYPE calc_wt_rel_thread(void* arg) { |
| 1695 |
|
| 1696 |
uintptr_t tidx = (uintptr_t)arg; |
| 1697 |
uintptr_t ulii = g_thread_start[tidx]; |
| 1698 |
uintptr_t uljj = g_thread_start[0]; |
| 1699 |
uintptr_t offset = (((uint64_t)ulii) * (ulii - 1) - ((uint64_t)uljj) * (uljj - 1)) / 2; |
| 1700 |
double* rel_ptr = &(g_rel_dists[offset]); |
| 1701 |
uintptr_t* geno_ptr = (uintptr_t*)g_geno; |
| 1702 |
uintptr_t* masks_ptr = g_masks; |
| 1703 |
uintptr_t* mmasks_ptr = g_mmasks; |
| 1704 |
uint32_t* missing_ptr = &(g_missing_dbl_excluded[offset]); |
| 1705 |
double* subset_weights_ptr = g_subset_weights; |
| 1706 |
uint32_t end_idx = g_thread_start[tidx + 1]; |
| 1707 |
uint32_t is_last_block; |
| 1708 |
while (1) { |
| 1709 |
is_last_block = g_is_last_thread_block; |
| 1710 |
incr_dists_r(rel_ptr, geno_ptr, masks_ptr, (uint32_t)tidx, subset_weights_ptr); |
| 1711 |
if (is_last_block || ((g_thread_spawn_ct % 3) == 2)) { |
| 1712 |
incr_dists_rm(missing_ptr, mmasks_ptr, ulii, end_idx); |
| 1713 |
} |
| 1714 |
if ((!tidx) || is_last_block) { |
| 1715 |
THREAD_RETURN; |
| 1716 |
} |
| 1717 |
THREAD_BLOCK_FINISH(tidx); |
| 1718 |
} |
| 1719 |
} |
| 1720 |
|
| 1721 |
THREAD_RET_TYPE calc_missing_thread(void* arg) { |
| 1722 |
uintptr_t tidx = (uintptr_t)arg; |
| 1723 |
uintptr_t ulii = g_thread_start[tidx]; |
| 1724 |
uintptr_t uljj = g_thread_start[0]; |
| 1725 |
uintptr_t offset = (((uint64_t)ulii) * (ulii - 1) - ((uint64_t)uljj) * (uljj - 1)) / 2; |
| 1726 |
uintptr_t* mmasks_ptr = g_mmasks; |
| 1727 |
uint32_t* missing_ptr = &(g_missing_dbl_excluded[offset]); |
| 1728 |
uint32_t end_idx = g_thread_start[tidx + 1]; |
| 1729 |
while (1) { |
| 1730 |
incr_dists_rm(missing_ptr, mmasks_ptr, ulii, end_idx); |
| 1731 |
if ((!tidx) || g_is_last_thread_block) { |
| 1732 |
THREAD_RETURN; |
| 1733 |
} |
| 1734 |
THREAD_BLOCK_FINISH(tidx); |
| 1735 |
} |
| 1736 |
} |
| 1737 |
|
| 1738 |
void groupdist_jack(uint32_t jackknife_d, uint32_t case_ct, uint32_t ctrl_ct, double reg_tot_x, double reg_tot_xy, double reg_tot_y, uint32_t* uibuf, double* jackknife_precomp, double* dists, uintptr_t* pheno_c, double* returns) { |
| 1739 |
uint32_t* uiptr = uibuf; |
| 1740 |
uint32_t* uiptr2 = &(uibuf[jackknife_d]); |
| 1741 |
double neg_tot_uu = 0.0; |
| 1742 |
double neg_tot_au = 0.0; |
| 1743 |
double neg_tot_aa = 0.0; |
| 1744 |
uint32_t neg_a = 0; |
| 1745 |
uint32_t neg_u = 0; |
| 1746 |
double* dptr; |
| 1747 |
uint32_t* uiptr3; |
| 1748 |
uintptr_t sample_idx; |
| 1749 |
uint32_t uii; |
| 1750 |
while (uiptr < uiptr2) { |
| 1751 |
dptr = &(jackknife_precomp[(*uiptr++) * JACKKNIFE_VALS_GROUPDIST]); |
| 1752 |
neg_tot_uu += *dptr++; |
| 1753 |
neg_tot_au += *dptr++; |
| 1754 |
neg_tot_aa += *dptr++; |
| 1755 |
} |
| 1756 |
uiptr = uibuf; |
| 1757 |
while (uiptr < uiptr2) { |
| 1758 |
sample_idx = *uiptr; |
| 1759 |
uiptr3 = uibuf; |
| 1760 |
dptr = &(dists[(sample_idx * (sample_idx - 1)) / 2]); |
| 1761 |
if (IS_SET(pheno_c, sample_idx)) { |
| 1762 |
neg_a++; |
| 1763 |
while (uiptr3 < uiptr) { |
| 1764 |
uii = *uiptr3++; |
| 1765 |
if (IS_SET(pheno_c, uii)) { |
| 1766 |
neg_tot_aa -= dptr[uii]; |
| 1767 |
} else { |
| 1768 |
neg_tot_au -= dptr[uii]; |
| 1769 |
} |
| 1770 |
} |
| 1771 |
} else { |
| 1772 |
neg_u++; |
| 1773 |
while (uiptr3 < uiptr) { |
| 1774 |
uii = *uiptr3++; |
| 1775 |
if (IS_SET(pheno_c, uii)) { |
| 1776 |
neg_tot_au -= dptr[uii]; |
| 1777 |
} else { |
| 1778 |
neg_tot_uu -= dptr[uii]; |
| 1779 |
} |
| 1780 |
} |
| 1781 |
} |
| 1782 |
uiptr++; |
| 1783 |
} |
| 1784 |
returns[0] = (reg_tot_x - neg_tot_aa) / (double)(((intptr_t)(case_ct - neg_a) * (case_ct - neg_a - 1)) / 2); |
| 1785 |
returns[1] = (reg_tot_xy - neg_tot_au) / (double)((intptr_t)(case_ct - neg_a) * (ctrl_ct - neg_u)); |
| 1786 |
returns[2] = (reg_tot_y - neg_tot_uu) / (double)(((intptr_t)(ctrl_ct - neg_u) * (ctrl_ct - neg_u - 1)) / 2); |
| 1787 |
} |
| 1788 |
|
| 1789 |
void small_remap(uint32_t* uibuf, uint32_t ct, uint32_t dd) { |
| 1790 |
uint32_t* uibuf_end = &(uibuf[dd]); |
| 1791 |
uint32_t missings = 0; |
| 1792 |
uint32_t curpos = 0; |
| 1793 |
do { |
| 1794 |
if (!IS_SET(g_pheno_nm, curpos)) { |
| 1795 |
missings++; |
| 1796 |
} else if (*uibuf == curpos - missings) { |
| 1797 |
*uibuf++ = curpos; |
| 1798 |
} |
| 1799 |
curpos++; |
| 1800 |
} while (uibuf < uibuf_end); |
| 1801 |
} |
| 1802 |
|
| 1803 |
void pick_d(unsigned char* cbuf, uint32_t ct, uint32_t dd, sfmt_t* sfmtp) { |
| 1804 |
uint32_t uii; |
| 1805 |
uint32_t ujj; |
| 1806 |
uint32_t ukk; |
| 1807 |
memset(cbuf, 0, ct); |
| 1808 |
#ifdef __LP64__ |
| 1809 |
ukk = (uint32_t)(0x100000000LLU % ct); |
| 1810 |
#else |
| 1811 |
ukk = 2 * (0x80000000U % ct); |
| 1812 |
#endif |
| 1813 |
for (uii = 0; uii < dd; uii++) { |
| 1814 |
do { |
| 1815 |
do { |
| 1816 |
ujj = sfmt_genrand_uint32(sfmtp); |
| 1817 |
} while (ujj < ukk); |
| 1818 |
ujj %= ct; |
| 1819 |
} while (cbuf[ujj]); |
| 1820 |
cbuf[ujj] = 1; |
| 1821 |
} |
| 1822 |
} |
| 1823 |
|
| 1824 |
void pick_d_small(unsigned char* tmp_cbuf, uint32_t* uibuf, uint32_t ct, uint32_t dd, sfmt_t* sfmtp) { |
| 1825 |
uint32_t uii; |
| 1826 |
pick_d(tmp_cbuf, ct, dd, sfmtp); |
| 1827 |
for (uii = 0; uii < ct; uii++) { |
| 1828 |
if (tmp_cbuf[uii]) { |
| 1829 |
*uibuf++ = uii; |
| 1830 |
} |
| 1831 |
} |
| 1832 |
*uibuf = ct; |
| 1833 |
} |
| 1834 |
|
| 1835 |
THREAD_RET_TYPE groupdist_jack_thread(void* arg) { |
| 1836 |
uintptr_t tidx = (uintptr_t)arg; |
| 1837 |
uintptr_t sample_ct = g_sample_ct; |
| 1838 |
uint32_t case_ct = g_case_ct; |
| 1839 |
uint32_t ctrl_ct = g_ctrl_ct; |
| 1840 |
uint32_t jackknife_d = g_jackknife_d; |
| 1841 |
uint32_t* uibuf = (uint32_t*)(&(g_geno[tidx * round_up_pow2(case_ct + ctrl_ct + (jackknife_d + 1) * sizeof(int32_t), CACHELINE)])); |
| 1842 |
unsigned char* cbuf = &(g_geno[tidx * round_up_pow2(case_ct + ctrl_ct + (jackknife_d + 1) * sizeof(int32_t), CACHELINE) + (jackknife_d + 1) * sizeof(int32_t)]); |
| 1843 |
uintptr_t jackknife_iters = g_jackknife_iters; |
| 1844 |
uintptr_t uljj = jackknife_iters / 100; |
| 1845 |
double reg_tot_x = g_reg_tot_x; |
| 1846 |
double reg_tot_xy = g_reg_tot_xy; |
| 1847 |
double reg_tot_y = g_reg_tot_y; |
| 1848 |
double* jackknife_precomp = g_jackknife_precomp; |
| 1849 |
double* dists = g_dists; |
| 1850 |
uintptr_t* pheno_c = g_pheno_c; |
| 1851 |
sfmt_t* sfmtp = g_sfmtp_arr[tidx]; |
| 1852 |
double returns[3]; |
| 1853 |
double results[9]; |
| 1854 |
uintptr_t ulii; |
| 1855 |
fill_double_zero(9, results); |
| 1856 |
for (ulii = 0; ulii < jackknife_iters; ulii++) { |
| 1857 |
pick_d_small(cbuf, uibuf, case_ct + ctrl_ct, jackknife_d, sfmtp); |
| 1858 |
if (case_ct + ctrl_ct < sample_ct) { |
| 1859 |
small_remap(uibuf, case_ct + ctrl_ct, jackknife_d); |
| 1860 |
} |
| 1861 |
groupdist_jack(jackknife_d, case_ct, ctrl_ct, reg_tot_x, reg_tot_xy, reg_tot_y, uibuf, jackknife_precomp, dists, pheno_c, returns); |
| 1862 |
results[0] += returns[0]; |
| 1863 |
results[1] += returns[1]; |
| 1864 |
results[2] += returns[2]; |
| 1865 |
results[3] += returns[0] * returns[0]; |
| 1866 |
results[4] += returns[1] * returns[1]; |
| 1867 |
results[5] += returns[2] * returns[2]; |
| 1868 |
results[6] += returns[0] * returns[1]; |
| 1869 |
results[7] += returns[0] * returns[2]; |
| 1870 |
results[8] += returns[1] * returns[2]; |
| 1871 |
if ((!tidx) && (ulii >= uljj)) { |
| 1872 |
uljj = (ulii * 100LLU) / jackknife_iters; |
| 1873 |
printf("\r%" PRIuPTR "%%", uljj); |
| 1874 |
fflush(stdout); |
| 1875 |
uljj = ((uljj + 1LLU) * jackknife_iters) / 100; |
| 1876 |
} |
| 1877 |
} |
| 1878 |
|
| 1879 |
for (ulii = 0; ulii < 9; ulii++) { |
| 1880 |
g_calc_result[tidx][ulii] = results[ulii]; |
| 1881 |
} |
| 1882 |
THREAD_RETURN; |
| 1883 |
} |
| 1884 |
|
| 1885 |
double regress_rel_jack(uint32_t jackknife_d, uint32_t sample_ct, double reg_tot_xy, double reg_tot_x, double reg_tot_y, double reg_tot_xx, double reg_tot_yy, uint32_t* uibuf, double* jackknife_precomp, double* rel_dists, double* pheno_packed, double* ret2_ptr) { |
| 1886 |
uint32_t* uiptr = uibuf; |
| 1887 |
uint32_t* uiptr2 = &(uibuf[jackknife_d]); |
| 1888 |
uint32_t uii; |
| 1889 |
uint32_t ujj; |
| 1890 |
uint32_t ukk; |
| 1891 |
double* dptr; |
| 1892 |
double* dptr2; |
| 1893 |
double neg_tot_xy = 0.0; |
| 1894 |
double neg_tot_x = 0.0; |
| 1895 |
double neg_tot_y = 0.0; |
| 1896 |
double neg_tot_xx = 0.0; |
| 1897 |
double neg_tot_yy = 0.0; |
| 1898 |
double dxx; |
| 1899 |
double dxx1; |
| 1900 |
double dyy; |
| 1901 |
while (uiptr < uiptr2) { |
| 1902 |
dptr2 = &(jackknife_precomp[(*uiptr++) * JACKKNIFE_VALS_REL]); |
| 1903 |
neg_tot_xy += *dptr2++; |
| 1904 |
neg_tot_x += *dptr2++; |
| 1905 |
neg_tot_y += *dptr2++; |
| 1906 |
neg_tot_xx += *dptr2++; |
| 1907 |
neg_tot_yy += *dptr2++; |
| 1908 |
} |
| 1909 |
uiptr = uibuf; |
| 1910 |
for (uii = 1; uii < jackknife_d; uii++) { |
| 1911 |
ujj = *(++uiptr); |
| 1912 |
dxx1 = pheno_packed[ujj]; |
| 1913 |
uiptr2 = uibuf; |
| 1914 |
dptr = &(rel_dists[((uintptr_t)ujj * (ujj - 1)) / 2]); |
| 1915 |
while (uiptr2 < uiptr) { |
| 1916 |
ukk = *uiptr2++; |
| 1917 |
dxx = (dxx1 + pheno_packed[ukk]) * 0.5; |
| 1918 |
dyy = dptr[ukk]; |
| 1919 |
neg_tot_xy -= dxx * dyy; |
| 1920 |
neg_tot_x -= dxx; |
| 1921 |
neg_tot_y -= dyy; |
| 1922 |
neg_tot_xx -= dxx * dxx; |
| 1923 |
neg_tot_yy -= dyy * dyy; |
| 1924 |
} |
| 1925 |
} |
| 1926 |
dxx = reg_tot_y - neg_tot_y; |
| 1927 |
dyy = ((int32_t)sample_ct) - ((int32_t)jackknife_d); |
| 1928 |
dyy = dyy * (dyy - 1.0) * 0.5; |
| 1929 |
*ret2_ptr = ((reg_tot_xy - neg_tot_xy) - dxx * (reg_tot_x - neg_tot_x) / dyy) / ((reg_tot_yy - neg_tot_yy) - dxx * dxx / dyy); |
| 1930 |
dxx = reg_tot_x - neg_tot_x; |
| 1931 |
return ((reg_tot_xy - neg_tot_xy) - dxx * (reg_tot_y - neg_tot_y) / dyy) / ((reg_tot_xx - neg_tot_xx) - dxx * dxx / dyy); |
| 1932 |
} |
| 1933 |
|
| 1934 |
THREAD_RET_TYPE regress_rel_jack_thread(void* arg) { |
| 1935 |
uintptr_t tidx = (uintptr_t)arg; |
| 1936 |
uintptr_t sample_ct = g_sample_ct; |
| 1937 |
uintptr_t jackknife_iters = g_jackknife_iters; |
| 1938 |
double reg_tot_xy = g_reg_tot_xy; |
| 1939 |
double reg_tot_x = g_reg_tot_x; |
| 1940 |
double reg_tot_y = g_reg_tot_y; |
| 1941 |
double reg_tot_xx = g_reg_tot_xx; |
| 1942 |
double reg_tot_yy = g_reg_tot_yy; |
| 1943 |
uint32_t jackknife_d = g_jackknife_d; |
| 1944 |
uint32_t* uibuf = (uint32_t*)(&(g_geno[tidx * round_up_pow2(sample_ct + (jackknife_d + 1) * sizeof(int32_t), CACHELINE)])); |
| 1945 |
unsigned char* cbuf = &(g_geno[tidx * round_up_pow2(sample_ct + (jackknife_d + 1) * sizeof(int32_t), CACHELINE) + (jackknife_d + 1) * sizeof(int32_t)]); |
| 1946 |
double* jackknife_precomp = g_jackknife_precomp; |
| 1947 |
double* rel_dists = g_rel_dists; |
| 1948 |
double* pheno_packed = g_pheno_packed; |
| 1949 |
uintptr_t uljj = jackknife_iters / 100; |
| 1950 |
double sum = 0.0; |
| 1951 |
double sum_sq = 0.0; |
| 1952 |
double sum2 = 0; |
| 1953 |
double sum2_sq = 0.0; |
| 1954 |
sfmt_t* sfmtp = g_sfmtp_arr[tidx]; |
| 1955 |
uint64_t ulii; |
| 1956 |
double dxx; |
| 1957 |
double ret2; |
| 1958 |
for (ulii = 0; ulii < jackknife_iters; ulii++) { |
| 1959 |
pick_d_small(cbuf, uibuf, sample_ct, jackknife_d, sfmtp); |
| 1960 |
dxx = regress_rel_jack(jackknife_d, sample_ct, reg_tot_xy, reg_tot_x, reg_tot_y, reg_tot_xx, reg_tot_yy, uibuf, jackknife_precomp, rel_dists, pheno_packed, &ret2); |
| 1961 |
sum += dxx; |
| 1962 |
sum_sq += dxx * dxx; |
| 1963 |
sum2 += ret2; |
| 1964 |
sum2_sq += ret2 * ret2; |
| 1965 |
if ((!tidx) && (ulii >= uljj)) { |
| 1966 |
uljj = (ulii * 100LLU) / jackknife_iters; |
| 1967 |
printf("\r%" PRIuPTR "%%", uljj); |
| 1968 |
fflush(stdout); |
| 1969 |
uljj = ((uljj + 1LLU) * jackknife_iters) / 100; |
| 1970 |
} |
| 1971 |
} |
| 1972 |
g_calc_result[tidx][0] = sum; |
| 1973 |
g_calc_result[tidx][1] = sum_sq; |
| 1974 |
g_calc_result[tidx][2] = sum2; |
| 1975 |
g_calc_result[tidx][3] = sum2_sq; |
| 1976 |
THREAD_RETURN; |
| 1977 |
} |
| 1978 |
|
| 1979 |
void print_pheno_stdev(double* pheno_d, uint32_t sample_ct) { |
| 1980 |
double reg_tot_x = 0.0; |
| 1981 |
double reg_tot_xx = 0.0; |
| 1982 |
double dxx; |
| 1983 |
uint32_t uii; |
| 1984 |
for (uii = 0; uii < sample_ct; uii++) { |
| 1985 |
dxx = pheno_d[uii]; |
| 1986 |
reg_tot_x += dxx; |
| 1987 |
reg_tot_xx += dxx * dxx; |
| 1988 |
} |
| 1989 |
LOGPRINTF("Phenotype stdev: %g\n", sqrt((reg_tot_xx - reg_tot_x * reg_tot_x / sample_ct) / (sample_ct - 1))); |
| 1990 |
} |
| 1991 |
|
| 1992 |
uint32_t set_default_jackknife_d(uint32_t ct) { |
| 1993 |
uint32_t dd = (uint32_t)pow((double)ct, 0.600000000001); |
| 1994 |
LOGPRINTF("Setting d=%u for jackknife.\n", dd); |
| 1995 |
return dd; |
| 1996 |
} |
| 1997 |
|
| 1998 |
int32_t regress_rel_main(uintptr_t unfiltered_sample_ct, uintptr_t* sample_exclude, uintptr_t sample_ct, Rel_info* relip, pthread_t* threads, double* pheno_d) { |
| 1999 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 2000 |
uintptr_t regress_rel_iters = relip->regress_rel_iters; |
| 2001 |
double reg_tot_xy = 0; |
| 2002 |
double reg_tot_x = 0; |
| 2003 |
double reg_tot_y = 0; |
| 2004 |
double reg_tot_xx = 0; |
| 2005 |
double reg_tot_yy = 0; |
| 2006 |
uint32_t regress_rel_d = relip->regress_rel_d; |
| 2007 |
double* rel_ptr; |
| 2008 |
double* pheno_ptr; |
| 2009 |
double* pheno_ptr2; |
| 2010 |
double* jp_fixed_ptr; |
| 2011 |
double* jp_moving_ptr; |
| 2012 |
uint32_t uii; |
| 2013 |
uintptr_t ulii; |
| 2014 |
uintptr_t trimatrix_size; |
| 2015 |
double trimatrix_size_recip; |
| 2016 |
double half_avg_pheno; |
| 2017 |
double dxx; |
| 2018 |
double dyy; |
| 2019 |
double dxxyy; |
| 2020 |
double dxxsq; |
| 2021 |
double dyysq; |
| 2022 |
g_sample_ct = sample_ct; |
| 2023 |
if (bigstack_alloc_d(sample_ct, &g_pheno_packed)) { |
| 2024 |
return RET_NOMEM; |
| 2025 |
} |
| 2026 |
collapse_copy_phenod(g_pheno_packed, pheno_d, sample_exclude, unfiltered_sample_ct, sample_ct); |
| 2027 |
print_pheno_stdev(g_pheno_packed, sample_ct); |
| 2028 |
trimatrix_size = ((uintptr_t)sample_ct * (sample_ct - 1)) / 2; |
| 2029 |
rel_ptr = g_rel_dists; |
| 2030 |
pheno_ptr = g_pheno_packed; |
| 2031 |
if (bigstack_calloc_d(sample_ct * JACKKNIFE_VALS_REL, &g_jackknife_precomp)) { |
| 2032 |
return RET_NOMEM; |
| 2033 |
} |
| 2034 |
for (uii = 1; uii < sample_ct; uii++) { |
| 2035 |
half_avg_pheno = *(++pheno_ptr); |
| 2036 |
pheno_ptr2 = g_pheno_packed; |
| 2037 |
jp_fixed_ptr = &(g_jackknife_precomp[uii * JACKKNIFE_VALS_REL]); |
| 2038 |
jp_moving_ptr = g_jackknife_precomp; |
| 2039 |
while (pheno_ptr2 < pheno_ptr) { |
| 2040 |
dxx = (half_avg_pheno + (*pheno_ptr2++)) * 0.5; |
| 2041 |
dyy = (*rel_ptr++); |
| 2042 |
dxxyy = dxx * dyy; |
| 2043 |
dxxsq = dxx * dxx; |
| 2044 |
dyysq = dyy * dyy; |
| 2045 |
reg_tot_xy += dxxyy; |
| 2046 |
jp_fixed_ptr[0] += dxxyy; |
| 2047 |
*jp_moving_ptr += dxxyy; |
| 2048 |
jp_moving_ptr++; |
| 2049 |
reg_tot_x += dxx; |
| 2050 |
jp_fixed_ptr[1] += dxx; |
| 2051 |
*jp_moving_ptr += dxx; |
| 2052 |
jp_moving_ptr++; |
| 2053 |
reg_tot_y += dyy; |
| 2054 |
jp_fixed_ptr[2] += dyy; |
| 2055 |
*jp_moving_ptr += dyy; |
| 2056 |
jp_moving_ptr++; |
| 2057 |
reg_tot_xx += dxxsq; |
| 2058 |
jp_fixed_ptr[3] += dxxsq; |
| 2059 |
*jp_moving_ptr += dxxsq; |
| 2060 |
jp_moving_ptr++; |
| 2061 |
reg_tot_yy += dyysq; |
| 2062 |
jp_fixed_ptr[4] += dyysq; |
| 2063 |
*jp_moving_ptr += dyysq; |
| 2064 |
jp_moving_ptr++; |
| 2065 |
} |
| 2066 |
} |
| 2067 |
g_reg_tot_xy = reg_tot_xy; |
| 2068 |
g_reg_tot_x = reg_tot_x; |
| 2069 |
g_reg_tot_y = reg_tot_y; |
| 2070 |
g_reg_tot_xx = reg_tot_xx; |
| 2071 |
g_reg_tot_yy = reg_tot_yy; |
| 2072 |
trimatrix_size_recip = 1.0 / (double)trimatrix_size; |
| 2073 |
LOGPRINTF("Regression slope (y = genomic relationship, x = avg phenotype): %g\n", (reg_tot_xy - reg_tot_x * reg_tot_y * trimatrix_size_recip) / (reg_tot_xx - reg_tot_x * reg_tot_x * trimatrix_size_recip)); |
| 2074 |
LOGPRINTF(" (y = avg phenotype, x = genomic relationship): %g\n", (reg_tot_xy - reg_tot_x * reg_tot_y * trimatrix_size_recip) / (reg_tot_yy - reg_tot_y * reg_tot_y * trimatrix_size_recip)); |
| 2075 |
g_jackknife_iters = (regress_rel_iters + g_thread_ct - 1) / g_thread_ct; |
| 2076 |
if (regress_rel_d) { |
| 2077 |
g_jackknife_d = regress_rel_d; |
| 2078 |
} else { |
| 2079 |
g_jackknife_d = set_default_jackknife_d(sample_ct); |
| 2080 |
} |
| 2081 |
if (bigstack_alloc_uc(g_thread_ct * round_up_pow2(sample_ct * (g_jackknife_d + 1) * sizeof(int32_t), CACHELINE), &g_geno)) { |
| 2082 |
return RET_NOMEM; |
| 2083 |
} |
| 2084 |
if (bigstack_init_sfmtp(g_thread_ct)) { |
| 2085 |
return RET_NOMEM; |
| 2086 |
} |
| 2087 |
if (spawn_threads(threads, ®ress_rel_jack_thread, g_thread_ct)) { |
| 2088 |
return RET_THREAD_CREATE_FAIL; |
| 2089 |
} |
| 2090 |
ulii = 0; |
| 2091 |
regress_rel_jack_thread((void*)ulii); |
| 2092 |
dxx = g_calc_result[0][0]; |
| 2093 |
dxxsq = g_calc_result[0][1]; |
| 2094 |
|
| 2095 |
dyy = g_calc_result[0][2]; |
| 2096 |
dyysq = g_calc_result[0][3]; |
| 2097 |
|
| 2098 |
join_threads(threads, g_thread_ct); |
| 2099 |
for (uii = 0; uii < g_thread_ct - 1; uii++) { |
| 2100 |
dxx += g_calc_result[uii + 1][0]; |
| 2101 |
dxxsq += g_calc_result[uii + 1][1]; |
| 2102 |
dyy += g_calc_result[uii + 1][2]; |
| 2103 |
dyysq += g_calc_result[uii + 1][3]; |
| 2104 |
} |
| 2105 |
ulii = g_jackknife_iters * g_thread_ct; |
| 2106 |
putc_unlocked('\r', stdout); |
| 2107 |
LOGPRINTF("Jackknife s.e. (y = genomic relationship): %g\n", sqrt(((sample_ct - g_jackknife_d) / (double)g_jackknife_d) * (dxxsq - dxx * dxx / (double)ulii) / ((double)ulii - 1))); |
| 2108 |
LOGPRINTF(" (y = phenotype): %g\n", sqrt(((sample_ct - g_jackknife_d) / (double)g_jackknife_d) * (dyysq - dyy * dyy / (double)ulii) / ((double)ulii - 1))); |
| 2109 |
bigstack_reset(bigstack_mark); |
| 2110 |
return 0; |
| 2111 |
} |
| 2112 |
|
| 2113 |
|
| 2114 |
|
| 2115 |
void matrix_const_mult_add(uint32_t sample_ct, double* matrix, double mult_val, double add_val) { |
| 2116 |
uint32_t uii; |
| 2117 |
uint32_t loop_end = sample_ct - 1; |
| 2118 |
uint32_t ujj; |
| 2119 |
double* dptr = matrix; |
| 2120 |
#ifdef __LP64__ |
| 2121 |
__m128d* vptr; |
| 2122 |
__m128d v_mult_val = _mm_set1_pd(mult_val); |
| 2123 |
#endif |
| 2124 |
for (uii = 0; uii < loop_end; uii++) { |
| 2125 |
*dptr = (*dptr) * mult_val + add_val; |
| 2126 |
dptr++; |
| 2127 |
#ifdef __LP64__ |
| 2128 |
if ((uintptr_t)dptr & 8) { |
| 2129 |
*dptr *= mult_val; |
| 2130 |
dptr++; |
| 2131 |
ujj = 1; |
| 2132 |
} else { |
| 2133 |
ujj = 0; |
| 2134 |
} |
| 2135 |
vptr = (__m128d*)dptr; |
| 2136 |
while (ujj < loop_end) { |
| 2137 |
*vptr = _mm_mul_pd(*vptr, v_mult_val); |
| 2138 |
vptr++; |
| 2139 |
ujj += 2; |
| 2140 |
} |
| 2141 |
dptr = (double*)vptr; |
| 2142 |
if (ujj < sample_ct) { |
| 2143 |
*dptr *= mult_val; |
| 2144 |
dptr++; |
| 2145 |
} |
| 2146 |
#else |
| 2147 |
for (ujj = 0; ujj < sample_ct; ujj++) { |
| 2148 |
*dptr *= mult_val; |
| 2149 |
dptr++; |
| 2150 |
} |
| 2151 |
#endif |
| 2152 |
} |
| 2153 |
*dptr = (*dptr) * mult_val + add_val; |
| 2154 |
} |
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
void matrix_row_sum_ur(uintptr_t sample_ct, double* sums, double* matrix) { |
| 2160 |
uintptr_t sample_idx; |
| 2161 |
double* dptr; |
| 2162 |
double acc; |
| 2163 |
double* sptr_end; |
| 2164 |
double* sptr; |
| 2165 |
fill_double_zero(sample_ct, sums); |
| 2166 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 2167 |
dptr = &(matrix[sample_idx * sample_ct]); |
| 2168 |
acc = 0.0; |
| 2169 |
sptr_end = &(sums[sample_idx]); |
| 2170 |
sptr = sums; |
| 2171 |
while (sptr < sptr_end) { |
| 2172 |
acc += *dptr; |
| 2173 |
*sptr += *dptr++; |
| 2174 |
sptr++; |
| 2175 |
} |
| 2176 |
*sptr += acc + *dptr; |
| 2177 |
} |
| 2178 |
} |
| 2179 |
|
| 2180 |
#ifndef NOLAPACK |
| 2181 |
static inline void collapse_copy_phenod_incl(double* target, double* pheno_d, uintptr_t* sample_include, uintptr_t unfiltered_sample_ct, uintptr_t sample_ct) { |
| 2182 |
uintptr_t sample_uidx = 0; |
| 2183 |
double* target_end = &(target[sample_ct]); |
| 2184 |
uintptr_t delta; |
| 2185 |
do { |
| 2186 |
sample_uidx = next_set_ul_unsafe(sample_include, sample_uidx); |
| 2187 |
delta = next_unset_ul(sample_include, sample_uidx, unfiltered_sample_ct) - sample_uidx; |
| 2188 |
memcpy(target, &(pheno_d[sample_uidx]), delta * sizeof(double)); |
| 2189 |
target = &(target[delta]); |
| 2190 |
sample_uidx += delta; |
| 2191 |
} while (target < target_end); |
| 2192 |
} |
| 2193 |
|
| 2194 |
|
| 2195 |
|
| 2196 |
|
| 2197 |
|
| 2198 |
|
| 2199 |
|
| 2200 |
|
| 2201 |
void reml_em_one_trait(double* wkbase, double* pheno, double* covg_ref, double* covr_ref, double tol, uint32_t is_strict) { |
| 2202 |
double ll_change; |
| 2203 |
uintptr_t sample_ct = g_sample_ct; |
| 2204 |
int64_t mat_offset = sample_ct; |
| 2205 |
double* rel_dists; |
| 2206 |
MATRIX_INVERT_BUF1_TYPE* irow; |
| 2207 |
__CLPK_integer lwork; |
| 2208 |
double* row; |
| 2209 |
double* row2; |
| 2210 |
double* work; |
| 2211 |
double* dptr; |
| 2212 |
double* dptr2; |
| 2213 |
double* matrix_pvg; |
| 2214 |
__CLPK_integer sample_ct_li = sample_ct; |
| 2215 |
double dxx; |
| 2216 |
double dyy; |
| 2217 |
double max_jump; |
| 2218 |
double dzz; |
| 2219 |
double dlg; |
| 2220 |
double dle; |
| 2221 |
double covg_cur_change = 1.0; |
| 2222 |
double covr_cur_change = 1.0; |
| 2223 |
double covg_last_change; |
| 2224 |
double covr_last_change; |
| 2225 |
double sample_ct_recip = 1 / ((double)((intptr_t)sample_ct)); |
| 2226 |
uintptr_t sample_idx; |
| 2227 |
int32_t jj; |
| 2228 |
#ifdef _WIN32 |
| 2229 |
char blas_char; |
| 2230 |
int32_t sample_ct_i32 = sample_ct; |
| 2231 |
#endif |
| 2232 |
mat_offset = round_up_pow2_ull(mat_offset * mat_offset, CACHELINE); |
| 2233 |
rel_dists = &(wkbase[mat_offset]); |
| 2234 |
row = &(wkbase[mat_offset * 3]); |
| 2235 |
irow = (MATRIX_INVERT_BUF1_TYPE*)row; |
| 2236 |
row2 = &(row[sample_ct]); |
| 2237 |
work = &(wkbase[mat_offset * 2]); |
| 2238 |
lwork = mat_offset; |
| 2239 |
matrix_pvg = work; |
| 2240 |
if (!lwork) { |
| 2241 |
lwork = CACHELINE_DBL; |
| 2242 |
} |
| 2243 |
fill_double_zero(mat_offset, matrix_pvg); |
| 2244 |
fill_double_zero(sample_ct, row2); |
| 2245 |
printf(" "); |
| 2246 |
do { |
| 2247 |
memcpy(wkbase, rel_dists, mat_offset * sizeof(double)); |
| 2248 |
matrix_const_mult_add(sample_ct, wkbase, *covg_ref, *covr_ref); |
| 2249 |
invert_matrix(sample_ct_li, wkbase, irow, work); |
| 2250 |
matrix_row_sum_ur(sample_ct, row, wkbase); |
| 2251 |
dxx = 0.0; |
| 2252 |
dptr = row; |
| 2253 |
dptr2 = &(row[sample_ct]); |
| 2254 |
while (dptr < dptr2) { |
| 2255 |
dxx += *dptr++; |
| 2256 |
} |
| 2257 |
dxx = -1 / dxx; |
| 2258 |
#ifdef _WIN32 |
| 2259 |
jj = 1; |
| 2260 |
dger_(&sample_ct_i32, &sample_ct_i32, &dxx, row, &jj, row, &jj, wkbase, &sample_ct_i32); |
| 2261 |
|
| 2262 |
dyy = 1.0; |
| 2263 |
dzz = 0.0; |
| 2264 |
col_major_matrix_multiply(sample_ct_i32, sample_ct_i32, sample_ct_i32, wkbase, rel_dists, matrix_pvg); |
| 2265 |
|
| 2266 |
|
| 2267 |
dlg = 0.0; |
| 2268 |
dle = 0.0; |
| 2269 |
jj = sample_ct + 1; |
| 2270 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 2271 |
dlg -= matrix_pvg[sample_idx * jj]; |
| 2272 |
dle -= wkbase[sample_idx * jj]; |
| 2273 |
} |
| 2274 |
blas_char = 'U'; |
| 2275 |
jj = 1; |
| 2276 |
dsymv_(&blas_char, &sample_ct_i32, &dyy, wkbase, &sample_ct_i32, pheno, &jj, &dzz, row2, &jj); |
| 2277 |
dsymv_(&blas_char, &sample_ct_i32, &dyy, matrix_pvg, &sample_ct_i32, row2, &jj, &dzz, row, &jj); |
| 2278 |
dlg += ddot_(&sample_ct_i32, pheno, &jj, row, &jj); |
| 2279 |
dsymv_(&blas_char, &sample_ct_i32, &dyy, wkbase, &sample_ct_i32, row2, &jj, &dzz, row, &jj); |
| 2280 |
dle += ddot_(&sample_ct_i32, pheno, &jj, row, &jj); |
| 2281 |
#else |
| 2282 |
cblas_dger(CblasColMajor, sample_ct, sample_ct, dxx, row, 1, row, 1, wkbase, sample_ct); |
| 2283 |
|
| 2284 |
col_major_matrix_multiply(sample_ct, sample_ct, sample_ct, wkbase, rel_dists, matrix_pvg); |
| 2285 |
|
| 2286 |
dlg = 0.0; |
| 2287 |
dle = 0.0; |
| 2288 |
jj = sample_ct + 1; |
| 2289 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 2290 |
dlg -= matrix_pvg[sample_idx * jj]; |
| 2291 |
dle -= wkbase[sample_idx * jj]; |
| 2292 |
} |
| 2293 |
cblas_dsymv(CblasColMajor, CblasUpper, sample_ct, 1.0, wkbase, sample_ct, pheno, 1, 0.0, row2, 1); |
| 2294 |
cblas_dsymv(CblasColMajor, CblasUpper, sample_ct, 1.0, matrix_pvg, sample_ct, row2, 1, 0.0, row, 1); |
| 2295 |
dlg += cblas_ddot(sample_ct, pheno, 1, row, 1); |
| 2296 |
cblas_dsymv(CblasColMajor, CblasUpper, sample_ct, 1.0, wkbase, sample_ct, row2, 1, 0.0, row, 1); |
| 2297 |
dle += cblas_ddot(sample_ct, pheno, 1, row, 1); |
| 2298 |
#endif |
| 2299 |
covg_last_change = covg_cur_change; |
| 2300 |
covr_last_change = covr_cur_change; |
| 2301 |
covg_cur_change = (*covg_ref) * (*covg_ref) * dlg * sample_ct_recip; |
| 2302 |
covr_cur_change = (*covr_ref) * (*covr_ref) * dle * sample_ct_recip; |
| 2303 |
if (is_strict) { |
| 2304 |
max_jump = 1.0; |
| 2305 |
} else { |
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
dxx = atan2(covg_last_change, covr_last_change) - atan2(covg_cur_change, covr_cur_change); |
| 2311 |
if (dxx < 0.0) { |
| 2312 |
dxx = -dxx; |
| 2313 |
} |
| 2314 |
if (dxx > PI) { |
| 2315 |
dxx = 2 * PI - dxx; |
| 2316 |
} |
| 2317 |
dyy = sqrt((covg_cur_change * covg_cur_change + covr_cur_change * covr_cur_change) / (covg_last_change * covg_last_change + covr_last_change * covr_last_change)); |
| 2318 |
if (covg_cur_change < 0.0) { |
| 2319 |
max_jump = *covg_ref * (-0.5) / covg_cur_change; |
| 2320 |
} else { |
| 2321 |
max_jump = (1.0 - *covg_ref) * 0.5 / covg_cur_change; |
| 2322 |
} |
| 2323 |
if (covr_cur_change < 0.0) { |
| 2324 |
dzz = *covr_ref * (-0.5) / covr_cur_change; |
| 2325 |
} else { |
| 2326 |
dzz = (1.0 - *covr_ref) * 0.5 / covr_cur_change; |
| 2327 |
} |
| 2328 |
if (dzz < max_jump) { |
| 2329 |
max_jump = dzz; |
| 2330 |
} |
| 2331 |
dzz = (PI / 4) / dxx; |
| 2332 |
if (dzz < max_jump) { |
| 2333 |
max_jump = dzz; |
| 2334 |
} |
| 2335 |
if (dyy < 1.0) { |
| 2336 |
dzz = 1 / (1.0 - dyy); |
| 2337 |
} |
| 2338 |
if (dzz < max_jump) { |
| 2339 |
max_jump = dzz; |
| 2340 |
} |
| 2341 |
if (max_jump < 1.0) { |
| 2342 |
max_jump = 1.0; |
| 2343 |
} else if (max_jump > MAX_EM_ACCEL) { |
| 2344 |
max_jump = MAX_EM_ACCEL; |
| 2345 |
} |
| 2346 |
} |
| 2347 |
*covg_ref += covg_cur_change * max_jump; |
| 2348 |
*covr_ref += covr_cur_change * max_jump; |
| 2349 |
ll_change = (covg_cur_change * dlg) + (covr_cur_change * dle); |
| 2350 |
printf("\b\b\b\b\b\b \rcovg: %g covr: %g EM step log likelihood change: %g", *covg_ref, *covr_ref, ll_change); |
| 2351 |
fflush(stdout); |
| 2352 |
} while (ll_change > tol); |
| 2353 |
putc_unlocked('\n', stdout); |
| 2354 |
snprintf(g_logbuf, LOGBUFLEN, "covg: %g covr: %g\n", *covg_ref, *covr_ref); |
| 2355 |
logstr(g_logbuf); |
| 2356 |
} |
| 2357 |
|
| 2358 |
void mean_zero_var_one_in_place(uint32_t sample_ct, double* pheno_d) { |
| 2359 |
double sum = 0.0; |
| 2360 |
double ssq = 0.0; |
| 2361 |
double* dptr = pheno_d; |
| 2362 |
double dxx; |
| 2363 |
double sample_ctd; |
| 2364 |
double mean; |
| 2365 |
double stdev_recip; |
| 2366 |
uint32_t sample_idx; |
| 2367 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 2368 |
dxx = *dptr++; |
| 2369 |
sum += dxx; |
| 2370 |
ssq += dxx * dxx; |
| 2371 |
} |
| 2372 |
sample_ctd = (double)((int32_t)sample_ct); |
| 2373 |
mean = sum / sample_ctd; |
| 2374 |
|
| 2375 |
|
| 2376 |
stdev_recip = sqrt(sample_ctd / (ssq - sum * mean)); |
| 2377 |
dptr = pheno_d; |
| 2378 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 2379 |
*dptr = ((*dptr) - mean) * stdev_recip; |
| 2380 |
dptr++; |
| 2381 |
} |
| 2382 |
} |
| 2383 |
|
| 2384 |
int32_t calc_unrelated_herit(uint64_t calculation_type, Rel_info* relip, uintptr_t unfiltered_sample_ct, uintptr_t* sample_exclude, uintptr_t sample_ct, double* pheno_d, double* rel_ibc) { |
| 2385 |
uint32_t is_strict = (relip->modifier / REL_UNRELATED_HERITABILITY_STRICT) & 1; |
| 2386 |
int32_t ibc_type = relip->ibc_type; |
| 2387 |
double unrelated_herit_covg = relip->unrelated_herit_covg; |
| 2388 |
double unrelated_herit_covr = relip->unrelated_herit_covr; |
| 2389 |
double unrelated_herit_tol = relip->unrelated_herit_tol; |
| 2390 |
uintptr_t ulii; |
| 2391 |
uintptr_t uljj; |
| 2392 |
double* pheno_ptr; |
| 2393 |
double* ibc_ptr; |
| 2394 |
double* rel_base; |
| 2395 |
g_sample_ct = sample_ct; |
| 2396 |
g_missing_dbl_excluded = nullptr; |
| 2397 |
ulii = sample_ct; |
| 2398 |
ulii = round_up_pow2(ulii * ulii, CACHELINE_DBL); |
| 2399 |
rel_base = &(g_rel_dists[ulii]); |
| 2400 |
ulii = ulii * 3 + round_up_pow2(sample_ct, CACHELINE_DBL) * 3; |
| 2401 |
|
| 2402 |
bigstack_reset(g_rel_dists); |
| 2403 |
if (bigstack_alloc_d(ulii, &g_rel_dists)) { |
| 2404 |
return RET_NOMEM; |
| 2405 |
} |
| 2406 |
pheno_ptr = &(g_rel_dists[ulii - round_up_pow2(sample_ct, CACHELINE_DBL)]); |
| 2407 |
collapse_copy_phenod(pheno_ptr, pheno_d, sample_exclude, unfiltered_sample_ct, sample_ct); |
| 2408 |
mean_zero_var_one_in_place(sample_ct, pheno_ptr); |
| 2409 |
if (calculation_type & CALC_IBC) { |
| 2410 |
ibc_ptr = &(rel_ibc[ibc_type * sample_ct]); |
| 2411 |
} else { |
| 2412 |
ibc_ptr = rel_ibc; |
| 2413 |
} |
| 2414 |
for (ulii = 0; ulii < sample_ct; ulii++) { |
| 2415 |
memcpy(&(rel_base[ulii * sample_ct]), &(g_rel_dists[(ulii * (ulii - 1)) / 2]), ulii * sizeof(double)); |
| 2416 |
rel_base[ulii * (sample_ct + 1)] = *ibc_ptr++; |
| 2417 |
for (uljj = ulii + 1; uljj < sample_ct; uljj++) { |
| 2418 |
rel_base[ulii * sample_ct + uljj] = g_rel_dists[(uljj * (uljj - 1)) / 2 + ulii]; |
| 2419 |
} |
| 2420 |
} |
| 2421 |
reml_em_one_trait(g_rel_dists, pheno_ptr, &unrelated_herit_covg, &unrelated_herit_covr, unrelated_herit_tol, is_strict); |
| 2422 |
LOGPRINTF("h^2 estimate: %g\n", unrelated_herit_covg); |
| 2423 |
return 0; |
| 2424 |
} |
| 2425 |
|
| 2426 |
int32_t unrelated_herit_batch(uint32_t load_grm_bin, char* grmname, char* phenoname, uint32_t mpheno_col, char* phenoname_str, int32_t missing_pheno, Rel_info* relip) { |
| 2427 |
char* grmname_end = (char*)memchr(grmname, 0, FNAMESIZE); |
| 2428 |
FILE* infile = nullptr; |
| 2429 |
FILE* grm_binfile = nullptr; |
| 2430 |
gzFile grm_gzfile = nullptr; |
| 2431 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 2432 |
unsigned char* bigstack_end_mark = g_bigstack_end; |
| 2433 |
uintptr_t max_sample_id_len = 4; |
| 2434 |
uintptr_t unfiltered_sample_ct = 0; |
| 2435 |
uintptr_t sample_uidx = 0; |
| 2436 |
uintptr_t line_idx = 0; |
| 2437 |
double unrelated_herit_tol = relip->unrelated_herit_tol; |
| 2438 |
double unrelated_herit_covg = relip->unrelated_herit_covg; |
| 2439 |
double unrelated_herit_covr = relip->unrelated_herit_covr; |
| 2440 |
uint32_t is_strict = (relip->modifier / REL_UNRELATED_HERITABILITY_STRICT) & 1; |
| 2441 |
uintptr_t* pheno_c = nullptr; |
| 2442 |
double* pheno_d = nullptr; |
| 2443 |
uintptr_t cur_sample_id_len; |
| 2444 |
uintptr_t unfiltered_sample_ctl; |
| 2445 |
uintptr_t* pheno_nm; |
| 2446 |
uintptr_t pheno_nm_ct; |
| 2447 |
uintptr_t sample_uidx2; |
| 2448 |
char* sorted_ids; |
| 2449 |
uint32_t* id_map; |
| 2450 |
char* bufptr; |
| 2451 |
char* bufptr2; |
| 2452 |
double* matrix_wkbase; |
| 2453 |
double* pheno_ptr; |
| 2454 |
double* rel_base; |
| 2455 |
double* row_ptr; |
| 2456 |
uint64_t fpos; |
| 2457 |
uintptr_t ulii; |
| 2458 |
uintptr_t uljj; |
| 2459 |
double dxx; |
| 2460 |
float fxx; |
| 2461 |
int32_t retval; |
| 2462 |
|
| 2463 |
|
| 2464 |
|
| 2465 |
|
| 2466 |
memcpy(grmname_end, ".grm.id", 8); |
| 2467 |
if (fopen_checked(grmname, "r", &infile)) { |
| 2468 |
goto unrelated_herit_batch_ret_OPEN_FAIL; |
| 2469 |
} |
| 2470 |
g_textbuf[MAXLINELEN - 1] = ' '; |
| 2471 |
while (fgets(g_textbuf, MAXLINELEN, infile)) { |
| 2472 |
line_idx++; |
| 2473 |
if (!g_textbuf[MAXLINELEN - 1]) { |
| 2474 |
LOGPREPRINTFWW("Error: Line %" PRIuPTR " of %s is pathologically long.\n", line_idx, grmname); |
| 2475 |
goto unrelated_herit_batch_ret_INVALID_FORMAT_2; |
| 2476 |
} |
| 2477 |
bufptr = skip_initial_spaces(g_textbuf); |
| 2478 |
if (is_eoln_kns(*bufptr)) { |
| 2479 |
continue; |
| 2480 |
} |
| 2481 |
bufptr2 = token_endnn(bufptr); |
| 2482 |
cur_sample_id_len = bufptr2 - bufptr; |
| 2483 |
bufptr2 = skip_initial_spaces(bufptr2); |
| 2484 |
if (is_eoln_kns(*bufptr2)) { |
| 2485 |
LOGPREPRINTFWW("Error: Line %" PRIuPTR " of %s has fewer tokens than expected.\n", line_idx, grmname); |
| 2486 |
goto unrelated_herit_batch_ret_INVALID_FORMAT_2; |
| 2487 |
} |
| 2488 |
cur_sample_id_len += strlen_se(bufptr2) + 2; |
| 2489 |
if (cur_sample_id_len > max_sample_id_len) { |
| 2490 |
max_sample_id_len = cur_sample_id_len; |
| 2491 |
} |
| 2492 |
unfiltered_sample_ct++; |
| 2493 |
} |
| 2494 |
if (!feof(infile)) { |
| 2495 |
goto unrelated_herit_batch_ret_READ_FAIL; |
| 2496 |
} |
| 2497 |
if (unfiltered_sample_ct < 2) { |
| 2498 |
logerrprint("Error: Less than two samples in .grm.id file.\n"); |
| 2499 |
goto unrelated_herit_batch_ret_INVALID_FORMAT; |
| 2500 |
} |
| 2501 |
rewind(infile); |
| 2502 |
unfiltered_sample_ctl = BITCT_TO_WORDCT(unfiltered_sample_ct); |
| 2503 |
if (bigstack_calloc_ul(unfiltered_sample_ctl, &pheno_nm) || |
| 2504 |
bigstack_end_alloc_c(unfiltered_sample_ct * max_sample_id_len, &sorted_ids) || |
| 2505 |
bigstack_end_alloc_ui(unfiltered_sample_ct, &id_map)) { |
| 2506 |
goto unrelated_herit_batch_ret_NOMEM; |
| 2507 |
} |
| 2508 |
while (fgets(g_textbuf, MAXLINELEN, infile)) { |
| 2509 |
bufptr = skip_initial_spaces(g_textbuf); |
| 2510 |
if (is_eoln_kns(*bufptr)) { |
| 2511 |
continue; |
| 2512 |
} |
| 2513 |
bufptr2 = token_endnn(bufptr); |
| 2514 |
cur_sample_id_len = bufptr2 - bufptr; |
| 2515 |
memcpy(&(sorted_ids[sample_uidx * max_sample_id_len]), bufptr, cur_sample_id_len); |
| 2516 |
sorted_ids[sample_uidx * max_sample_id_len + (cur_sample_id_len++)] = '\t'; |
| 2517 |
bufptr2 = skip_initial_spaces(bufptr2); |
| 2518 |
ulii = strlen_se(bufptr2); |
| 2519 |
memcpy(&(sorted_ids[sample_uidx * max_sample_id_len + cur_sample_id_len]), bufptr2, ulii); |
| 2520 |
sorted_ids[(sample_uidx++) * max_sample_id_len + cur_sample_id_len + ulii] = '\0'; |
| 2521 |
} |
| 2522 |
if (!feof(infile)) { |
| 2523 |
goto unrelated_herit_batch_ret_READ_FAIL; |
| 2524 |
} |
| 2525 |
for (sample_uidx = 0; sample_uidx < unfiltered_sample_ct; sample_uidx++) { |
| 2526 |
id_map[sample_uidx] = sample_uidx; |
| 2527 |
} |
| 2528 |
if (qsort_ext(sorted_ids, unfiltered_sample_ct, max_sample_id_len, strcmp_deref, (char*)id_map, sizeof(int32_t))) { |
| 2529 |
goto unrelated_herit_batch_ret_NOMEM; |
| 2530 |
} |
| 2531 |
|
| 2532 |
fclose_null(&infile); |
| 2533 |
if (fopen_checked(phenoname, "r", &infile)) { |
| 2534 |
goto unrelated_herit_batch_ret_OPEN_FAIL; |
| 2535 |
} |
| 2536 |
retval = load_pheno(infile, unfiltered_sample_ct, 0, sorted_ids, max_sample_id_len, id_map, missing_pheno, 0, mpheno_col, phenoname_str, pheno_nm, &pheno_c, &pheno_d, nullptr, 0); |
| 2537 |
bigstack_end_reset(bigstack_end_mark); |
| 2538 |
fclose_null(&infile); |
| 2539 |
if (retval) { |
| 2540 |
goto unrelated_herit_batch_ret_1; |
| 2541 |
} |
| 2542 |
if (!pheno_d) { |
| 2543 |
logerrprint("Error: --unrelated-heritability requires scalar phenotype.\n"); |
| 2544 |
goto unrelated_herit_batch_ret_INVALID_CMDLINE; |
| 2545 |
} |
| 2546 |
pheno_nm_ct = popcount_longs(pheno_nm, unfiltered_sample_ctl); |
| 2547 |
if (pheno_nm_ct < 2) { |
| 2548 |
logerrprint("Error: Less than two phenotypes present.\n"); |
| 2549 |
goto unrelated_herit_batch_ret_INVALID_FORMAT; |
| 2550 |
} |
| 2551 |
ulii = round_up_pow2(pheno_nm_ct * pheno_nm_ct, CACHELINE_DBL); |
| 2552 |
uljj = ulii * 3 + round_up_pow2(pheno_nm_ct, CACHELINE_DBL) * 3; |
| 2553 |
if (bigstack_alloc_d(ulii, &matrix_wkbase)) { |
| 2554 |
goto unrelated_herit_batch_ret_NOMEM; |
| 2555 |
} |
| 2556 |
g_sample_ct = pheno_nm_ct; |
| 2557 |
pheno_ptr = &(matrix_wkbase[uljj - round_up_pow2(pheno_nm_ct, CACHELINE_DBL)]); |
| 2558 |
collapse_copy_phenod_incl(pheno_ptr, pheno_d, pheno_nm, unfiltered_sample_ct, pheno_nm_ct); |
| 2559 |
rel_base = &(matrix_wkbase[ulii]); |
| 2560 |
mean_zero_var_one_in_place(pheno_nm_ct, pheno_ptr); |
| 2561 |
sample_uidx = 0; |
| 2562 |
if (load_grm_bin) { |
| 2563 |
memcpy(grmname_end, ".grm.bin", 9); |
| 2564 |
if (fopen_checked(grmname, FOPEN_RB, &grm_binfile)) { |
| 2565 |
goto unrelated_herit_batch_ret_OPEN_FAIL; |
| 2566 |
} |
| 2567 |
if (fseeko(grm_binfile, 0, SEEK_END)) { |
| 2568 |
goto unrelated_herit_batch_ret_READ_FAIL; |
| 2569 |
} |
| 2570 |
|
| 2571 |
fpos = ((uint64_t)unfiltered_sample_ct) * (unfiltered_sample_ct + 1) * 2; |
| 2572 |
if (((int64_t)fpos) != ftello(grm_binfile)) { |
| 2573 |
LOGPREPRINTFWW("Error: --grm-bin expects size of %s to be %" PRIu64 " bytes.\n", grmname, fpos); |
| 2574 |
goto unrelated_herit_batch_ret_INVALID_FORMAT_2; |
| 2575 |
} |
| 2576 |
rewind(grm_binfile); |
| 2577 |
for (ulii = 0; ulii < pheno_nm_ct; ulii++) { |
| 2578 |
if (IS_SET(pheno_nm, sample_uidx)) { |
| 2579 |
fpos = sample_uidx * (sample_uidx + 1) * (sizeof(float) / 2); |
| 2580 |
} else { |
| 2581 |
sample_uidx = next_set_ul_unsafe(pheno_nm, sample_uidx); |
| 2582 |
fpos = sample_uidx * (sample_uidx + 1) * (sizeof(float) / 2); |
| 2583 |
if (fseeko(grm_binfile, fpos, SEEK_SET)) { |
| 2584 |
goto unrelated_herit_batch_ret_READ_FAIL; |
| 2585 |
} |
| 2586 |
} |
| 2587 |
row_ptr = &(rel_base[ulii * pheno_nm_ct]); |
| 2588 |
sample_uidx2 = 0; |
| 2589 |
for (uljj = 0; uljj <= ulii; uljj++) { |
| 2590 |
if (!IS_SET(pheno_nm, sample_uidx2)) { |
| 2591 |
sample_uidx2 = next_set_ul_unsafe(pheno_nm, sample_uidx2); |
| 2592 |
if (fseeko(grm_binfile, fpos + (sample_uidx2 * sizeof(float)), SEEK_SET)) { |
| 2593 |
goto unrelated_herit_batch_ret_READ_FAIL; |
| 2594 |
} |
| 2595 |
} |
| 2596 |
if (fread(&fxx, sizeof(float), 1, grm_binfile) != sizeof(float)) { |
| 2597 |
goto unrelated_herit_batch_ret_READ_FAIL; |
| 2598 |
} |
| 2599 |
*row_ptr++ = (double)fxx; |
| 2600 |
sample_uidx2++; |
| 2601 |
} |
| 2602 |
sample_uidx++; |
| 2603 |
} |
| 2604 |
fclose_null(&grm_binfile); |
| 2605 |
} else { |
| 2606 |
memcpy(grmname_end, ".grm.gz", 8); |
| 2607 |
retval = gzopen_read_checked(grmname, &grm_gzfile); |
| 2608 |
if (retval) { |
| 2609 |
goto unrelated_herit_batch_ret_1; |
| 2610 |
} |
| 2611 |
ulii = 0; |
| 2612 |
for (sample_uidx = 0; sample_uidx < pheno_nm_ct; sample_uidx++) { |
| 2613 |
if (!IS_SET(pheno_nm, sample_uidx)) { |
| 2614 |
for (sample_uidx2 = 0; sample_uidx2 <= sample_uidx; sample_uidx2++) { |
| 2615 |
if (!gzgets(grm_gzfile, g_textbuf, MAXLINELEN)) { |
| 2616 |
goto unrelated_herit_batch_ret_READ_FAIL; |
| 2617 |
} |
| 2618 |
if (!g_textbuf[MAXLINELEN - 1]) { |
| 2619 |
goto unrelated_herit_batch_ret_INVALID_FORMAT_3; |
| 2620 |
} |
| 2621 |
} |
| 2622 |
} else { |
| 2623 |
row_ptr = &(rel_base[ulii * pheno_nm_ct]); |
| 2624 |
for (sample_uidx2 = 0; sample_uidx2 <= sample_uidx; sample_uidx2++) { |
| 2625 |
if (!gzgets(grm_gzfile, g_textbuf, MAXLINELEN)) { |
| 2626 |
goto unrelated_herit_batch_ret_READ_FAIL; |
| 2627 |
} |
| 2628 |
if (!g_textbuf[MAXLINELEN - 1]) { |
| 2629 |
goto unrelated_herit_batch_ret_INVALID_FORMAT_3; |
| 2630 |
} |
| 2631 |
if (IS_SET(pheno_nm, sample_uidx2)) { |
| 2632 |
bufptr = next_token_mult(g_textbuf, 3); |
| 2633 |
if (no_more_tokens_kns(bufptr)) { |
| 2634 |
goto unrelated_herit_batch_ret_INVALID_FORMAT_3; |
| 2635 |
} |
| 2636 |
if (scan_double(bufptr, &dxx)) { |
| 2637 |
goto unrelated_herit_batch_ret_INVALID_FORMAT_3; |
| 2638 |
} |
| 2639 |
*row_ptr++ = dxx; |
| 2640 |
} |
| 2641 |
} |
| 2642 |
ulii++; |
| 2643 |
} |
| 2644 |
} |
| 2645 |
gzclose(grm_gzfile); |
| 2646 |
grm_gzfile = nullptr; |
| 2647 |
} |
| 2648 |
|
| 2649 |
for (ulii = 0; ulii < pheno_nm_ct; ulii++) { |
| 2650 |
row_ptr = &(rel_base[ulii * pheno_nm_ct + ulii + 1]); |
| 2651 |
for (uljj = ulii + 1; uljj < pheno_nm_ct; uljj++) { |
| 2652 |
*row_ptr++ = rel_base[uljj * pheno_nm_ct + ulii]; |
| 2653 |
} |
| 2654 |
} |
| 2655 |
LOGPRINTF("--unrelated-heritability: %" PRIuPTR " phenotypes loaded.\n", pheno_nm_ct); |
| 2656 |
reml_em_one_trait(matrix_wkbase, pheno_ptr, &unrelated_herit_covg, &unrelated_herit_covr, unrelated_herit_tol, is_strict); |
| 2657 |
LOGPRINTF("h^2 estimate: %g\n", unrelated_herit_covg); |
| 2658 |
while (0) { |
| 2659 |
unrelated_herit_batch_ret_NOMEM: |
| 2660 |
retval = RET_NOMEM; |
| 2661 |
break; |
| 2662 |
unrelated_herit_batch_ret_OPEN_FAIL: |
| 2663 |
retval = RET_OPEN_FAIL; |
| 2664 |
break; |
| 2665 |
unrelated_herit_batch_ret_READ_FAIL: |
| 2666 |
retval = RET_READ_FAIL; |
| 2667 |
break; |
| 2668 |
unrelated_herit_batch_ret_INVALID_CMDLINE: |
| 2669 |
retval = RET_INVALID_CMDLINE; |
| 2670 |
break; |
| 2671 |
unrelated_herit_batch_ret_INVALID_FORMAT_3: |
| 2672 |
logerrprint("Error: Invalid .grm.gz file.\n"); |
| 2673 |
retval = RET_INVALID_FORMAT; |
| 2674 |
break; |
| 2675 |
unrelated_herit_batch_ret_INVALID_FORMAT_2: |
| 2676 |
logerrprintb(); |
| 2677 |
unrelated_herit_batch_ret_INVALID_FORMAT: |
| 2678 |
retval = RET_INVALID_FORMAT; |
| 2679 |
break; |
| 2680 |
} |
| 2681 |
unrelated_herit_batch_ret_1: |
| 2682 |
aligned_free_cond(pheno_c); |
| 2683 |
fclose_cond(infile); |
| 2684 |
fclose_cond(grm_binfile); |
| 2685 |
gzclose_cond(grm_gzfile); |
| 2686 |
bigstack_double_reset(bigstack_mark, bigstack_end_mark); |
| 2687 |
return retval; |
| 2688 |
} |
| 2689 |
#endif |
| 2690 |
|
| 2691 |
int32_t ibs_test_calc(pthread_t* threads, char* read_dists_fname, uintptr_t unfiltered_sample_ct, uintptr_t* sample_exclude, uintptr_t sample_ct, uintptr_t perm_ct, uintptr_t pheno_nm_ct, uintptr_t pheno_ctrl_ct, uintptr_t* pheno_nm, uintptr_t* pheno_c) { |
| 2692 |
|
| 2693 |
|
| 2694 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 2695 |
uintptr_t unfiltered_sample_ctl = BITCT_TO_WORDCT(unfiltered_sample_ct); |
| 2696 |
uintptr_t pheno_nm_ctl = BITCT_TO_WORDCT(pheno_nm_ct); |
| 2697 |
uintptr_t perm_ctcl = 1 + (perm_ct / CACHELINE_BIT); |
| 2698 |
uintptr_t perm_ctclm = perm_ctcl * (CACHELINE / sizeof(intptr_t)); |
| 2699 |
uintptr_t perm_ctcld = 1 + (perm_ct / CACHELINE_DBL); |
| 2700 |
uintptr_t perm_ctcldm = perm_ctcld * CACHELINE_DBL; |
| 2701 |
uintptr_t case_ct = pheno_nm_ct - pheno_ctrl_ct; |
| 2702 |
uint32_t tidx = 1; |
| 2703 |
int32_t retval = 0; |
| 2704 |
int32_t perm_test[6]; |
| 2705 |
uintptr_t* perm_rows; |
| 2706 |
double* perm_results; |
| 2707 |
double ctrl_ctrl_ct; |
| 2708 |
double ctrl_case_ct; |
| 2709 |
double case_case_ct; |
| 2710 |
double tot_sum; |
| 2711 |
double ctrl_ctrl_tot; |
| 2712 |
double ctrl_case_tot; |
| 2713 |
double case_case_tot; |
| 2714 |
double ctrl_ctrl_ssq; |
| 2715 |
double ctrl_case_ssq; |
| 2716 |
double case_case_ssq; |
| 2717 |
double tot_mean; |
| 2718 |
double ingroups_mean; |
| 2719 |
double ctrl_ctrl_mean; |
| 2720 |
double ctrl_case_mean; |
| 2721 |
double case_case_mean; |
| 2722 |
double ctrl_ctrl_var; |
| 2723 |
double ctrl_case_var; |
| 2724 |
double case_case_var; |
| 2725 |
double ctrl_ctrl_tot1; |
| 2726 |
double ctrl_case_tot1; |
| 2727 |
double case_case_tot1; |
| 2728 |
double case_case_minus_ctrl_ctrl; |
| 2729 |
double case_case_minus_ctrl_case; |
| 2730 |
double ctrl_ctrl_minus_ctrl_case; |
| 2731 |
double between_ssq; |
| 2732 |
double total_ssq; |
| 2733 |
double perm_ct_recip; |
| 2734 |
uintptr_t ulii; |
| 2735 |
#ifdef __LP64__ |
| 2736 |
__m128d* rvptr1; |
| 2737 |
__m128d* rvptr2; |
| 2738 |
#else |
| 2739 |
double* rptr1; |
| 2740 |
double* rptr2; |
| 2741 |
#endif |
| 2742 |
uintptr_t perm_idx; |
| 2743 |
g_load_dists = read_dists_fname? 1 : 0; |
| 2744 |
g_sample_ct = sample_ct; |
| 2745 |
perm_ct += 1; |
| 2746 |
if (pheno_ctrl_ct < 2) { |
| 2747 |
logerrprint("Warning: Skipping --ibs-test due to too few controls (minimum 2).\n"); |
| 2748 |
goto ibs_test_calc_ret_1; |
| 2749 |
} else if (case_ct < 2) { |
| 2750 |
logerrprint("Warning: Skipping --ibs-test due to too few cases (minimum 2).\n"); |
| 2751 |
goto ibs_test_calc_ret_1; |
| 2752 |
} |
| 2753 |
for (ulii = 0; ulii < 6; ulii++) { |
| 2754 |
perm_test[ulii] = 0; |
| 2755 |
} |
| 2756 |
ctrl_ctrl_ct = (pheno_ctrl_ct * (pheno_ctrl_ct - 1)) / 2; |
| 2757 |
ctrl_case_ct = pheno_ctrl_ct * case_ct; |
| 2758 |
case_case_ct = (case_ct * (case_ct - 1)) / 2; |
| 2759 |
g_perm_ct = perm_ct; |
| 2760 |
|
| 2761 |
if (bigstack_alloc_ul(unfiltered_sample_ctl, &g_pheno_nm) || |
| 2762 |
bigstack_alloc_ul(unfiltered_sample_ctl, &g_pheno_c)) { |
| 2763 |
goto ibs_test_calc_ret_NOMEM; |
| 2764 |
} |
| 2765 |
copy_bitarr_subset_excl(pheno_nm, sample_exclude, unfiltered_sample_ct, sample_ct, g_pheno_nm); |
| 2766 |
copy_bitarr_subset_excl(pheno_c, sample_exclude, unfiltered_sample_ct, sample_ct, g_pheno_c); |
| 2767 |
if (bigstack_alloc_d(g_thread_ct * 32 * BITCT, &g_ibs_test_partial_sums) || |
| 2768 |
bigstack_alloc_ul(perm_ct * pheno_nm_ctl, &perm_rows) || |
| 2769 |
bigstack_alloc_ul(perm_ctclm * g_thread_ct, &g_perm_col_buf) || |
| 2770 |
bigstack_calloc_d(2 * perm_ctcldm * g_thread_ct, &perm_results)) { |
| 2771 |
goto ibs_test_calc_ret_NOMEM; |
| 2772 |
} |
| 2773 |
g_perm_results = perm_results; |
| 2774 |
g_perm_rows = perm_rows; |
| 2775 |
|
| 2776 |
|
| 2777 |
copy_bitarr_subset(g_pheno_c, g_pheno_nm, unfiltered_sample_ct, pheno_nm_ct, perm_rows); |
| 2778 |
for (ulii = pheno_nm_ctl - 1; ulii; ulii--) { |
| 2779 |
perm_rows[ulii * perm_ct] = perm_rows[ulii]; |
| 2780 |
} |
| 2781 |
|
| 2782 |
printf("--ibs-test (%" PRIuPTR " permutations): [generating permutations]", perm_ct - 1); |
| 2783 |
fflush(stdout); |
| 2784 |
|
| 2785 |
|
| 2786 |
generate_perm1_interleaved(pheno_nm_ct, case_ct, 1, perm_ct, perm_rows); |
| 2787 |
fputs("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b \b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b0%", stdout); |
| 2788 |
fflush(stdout); |
| 2789 |
triangle_fill(pheno_nm_ct, g_thread_ct, 0, 1, 1, 1, g_thread_start); |
| 2790 |
if (spawn_threads(threads, &ibs_test_thread, g_thread_ct)) { |
| 2791 |
goto ibs_test_calc_ret_THREAD_CREATE_FAIL; |
| 2792 |
} |
| 2793 |
ulii = 0; |
| 2794 |
ibs_test_thread((void*)ulii); |
| 2795 |
join_threads(threads, g_thread_ct); |
| 2796 |
|
| 2797 |
tot_sum = g_calc_result[0][0]; |
| 2798 |
ctrl_ctrl_ssq = g_calc_result[0][1]; |
| 2799 |
ctrl_case_ssq = g_calc_result[0][2]; |
| 2800 |
case_case_ssq = g_calc_result[0][3]; |
| 2801 |
for (; tidx < g_thread_ct; tidx++) { |
| 2802 |
tot_sum += g_calc_result[tidx][0]; |
| 2803 |
ctrl_ctrl_ssq += g_calc_result[tidx][1]; |
| 2804 |
ctrl_case_ssq += g_calc_result[tidx][2]; |
| 2805 |
case_case_ssq += g_calc_result[tidx][3]; |
| 2806 |
#ifdef __LP64__ |
| 2807 |
rvptr1 = (__m128d*)perm_results; |
| 2808 |
rvptr2 = (__m128d*)(&(perm_results[2 * perm_ctcldm * tidx])); |
| 2809 |
for (perm_idx = 0; perm_idx < perm_ct; perm_idx++) { |
| 2810 |
*rvptr1 = _mm_add_pd(*rvptr1, *rvptr2++); |
| 2811 |
rvptr1++; |
| 2812 |
} |
| 2813 |
#else |
| 2814 |
rptr1 = perm_results; |
| 2815 |
rptr2 = &(perm_results[2 * perm_ctcldm * tidx]); |
| 2816 |
for (perm_idx = 0; perm_idx < perm_ct; perm_idx++) { |
| 2817 |
*rptr1++ += *rptr2++; |
| 2818 |
*rptr1++ += *rptr2++; |
| 2819 |
} |
| 2820 |
#endif |
| 2821 |
} |
| 2822 |
ctrl_ctrl_tot = perm_results[0]; |
| 2823 |
ctrl_case_tot = perm_results[1]; |
| 2824 |
case_case_tot = tot_sum - ctrl_ctrl_tot - ctrl_case_tot; |
| 2825 |
|
| 2826 |
tot_mean = tot_sum / (ctrl_ctrl_ct + ctrl_case_ct + case_case_ct); |
| 2827 |
ingroups_mean = (ctrl_ctrl_tot + case_case_tot) / (ctrl_ctrl_ct + case_case_ct); |
| 2828 |
ctrl_ctrl_mean = ctrl_ctrl_tot / ctrl_ctrl_ct; |
| 2829 |
ctrl_case_mean = ctrl_case_tot / ctrl_case_ct; |
| 2830 |
case_case_mean = case_case_tot / case_case_ct; |
| 2831 |
|
| 2832 |
ctrl_ctrl_var = ctrl_ctrl_ssq - ctrl_ctrl_tot * ctrl_ctrl_mean; |
| 2833 |
ctrl_case_var = ctrl_case_ssq - ctrl_case_tot * ctrl_case_mean; |
| 2834 |
case_case_var = case_case_ssq - case_case_tot * case_case_mean; |
| 2835 |
|
| 2836 |
total_ssq = ctrl_ctrl_var + ctrl_case_var + case_case_var; |
| 2837 |
between_ssq = ctrl_case_ct * (ctrl_case_mean - tot_mean) * (ctrl_case_mean - tot_mean) + (ctrl_ctrl_ct + case_case_ct) * (ingroups_mean - tot_mean) * (ingroups_mean - tot_mean); |
| 2838 |
|
| 2839 |
case_case_minus_ctrl_ctrl = case_case_tot - ctrl_ctrl_tot; |
| 2840 |
case_case_minus_ctrl_case = case_case_tot - ctrl_case_tot; |
| 2841 |
ctrl_ctrl_minus_ctrl_case = ctrl_ctrl_tot - ctrl_case_tot; |
| 2842 |
|
| 2843 |
for (ulii = 1; ulii < perm_ct; ulii++) { |
| 2844 |
ctrl_ctrl_tot1 = perm_results[ulii * 2]; |
| 2845 |
ctrl_case_tot1 = perm_results[ulii * 2 + 1]; |
| 2846 |
case_case_tot1 = tot_sum - ctrl_ctrl_tot1 - ctrl_case_tot1; |
| 2847 |
if (ctrl_case_tot1 < ctrl_case_tot) { |
| 2848 |
perm_test[0] += 1; |
| 2849 |
} |
| 2850 |
if (case_case_tot1 - ctrl_ctrl_tot1 < case_case_minus_ctrl_ctrl) { |
| 2851 |
perm_test[1] += 1; |
| 2852 |
} |
| 2853 |
if (case_case_tot1 < case_case_tot) { |
| 2854 |
perm_test[2] += 1; |
| 2855 |
} |
| 2856 |
if (ctrl_ctrl_tot1 < ctrl_ctrl_tot) { |
| 2857 |
perm_test[3] += 1; |
| 2858 |
} |
| 2859 |
if (case_case_tot1 - ctrl_case_tot1 < case_case_minus_ctrl_case) { |
| 2860 |
perm_test[4] += 1; |
| 2861 |
} |
| 2862 |
if (ctrl_ctrl_tot1 - ctrl_case_tot1 < ctrl_ctrl_minus_ctrl_case) { |
| 2863 |
perm_test[5] += 1; |
| 2864 |
} |
| 2865 |
} |
| 2866 |
|
| 2867 |
fputs("\r \r", stdout); |
| 2868 |
logprint("--ibs-test results:\n"); |
| 2869 |
LOGPRINTF(" Between-group IBS (mean, SD) = %g, %g\n", ctrl_case_mean, sqrt(ctrl_case_var / (ctrl_case_ct - 1))); |
| 2870 |
LOGPRINTF(" In-group (case) IBS (mean, SD) = %g, %g\n", case_case_mean, sqrt(case_case_var / (case_case_ct - 1))); |
| 2871 |
LOGPRINTF(" In-group (ctrl) IBS (mean, SD) = %g, %g\n", ctrl_ctrl_mean, sqrt(ctrl_ctrl_var / (ctrl_ctrl_ct - 1))); |
| 2872 |
LOGPRINTF(" Approximate proportion of variance between group = %g\n", between_ssq / total_ssq); |
| 2873 |
perm_ct_recip = 1.0 / ((double)perm_ct); |
| 2874 |
fputs(" IBS group-difference empirical p-values:\n", stdout); |
| 2875 |
LOGPRINTF(" T1: Case/control less similar p = %g\n", (perm_test[0]) * perm_ct_recip); |
| 2876 |
LOGPRINTF(" T2: Case/control more similar p = %g\n\n", (perm_ct - perm_test[0]) * perm_ct_recip); |
| 2877 |
LOGPRINTF(" T3: Case/case less similar than ctrl/ctrl p = %g\n", (perm_test[1]) * perm_ct_recip); |
| 2878 |
LOGPRINTF(" T4: Case/case more similar than ctrl/ctrl p = %g\n\n", (perm_ct - perm_test[1]) * perm_ct_recip); |
| 2879 |
LOGPRINTF(" T5: Case/case less similar p = %g\n", (perm_test[2]) * perm_ct_recip); |
| 2880 |
LOGPRINTF(" T6: Case/case more similar p = %g\n\n", (perm_ct - perm_test[2]) * perm_ct_recip); |
| 2881 |
LOGPRINTF(" T7: Control/control less similar p = %g\n", (perm_test[3]) * perm_ct_recip); |
| 2882 |
LOGPRINTF(" T8: Control/control more similar p = %g\n\n", (perm_ct - perm_test[3]) * perm_ct_recip); |
| 2883 |
LOGPRINTF(" T9: Case/case less similar than case/ctrl p = %g\n", (perm_test[4]) * perm_ct_recip); |
| 2884 |
LOGPRINTF(" T10: Case/case more similar than case/ctrl p = %g\n\n", (perm_ct - perm_test[4]) * perm_ct_recip); |
| 2885 |
LOGPRINTF(" T11: Ctrl/ctrl less similar than case/ctrl p = %g\n", (perm_test[5]) * perm_ct_recip); |
| 2886 |
LOGPRINTF(" T12: Ctrl/ctrl more similar than case/ctrl p = %g\n", (perm_ct - perm_test[5]) * perm_ct_recip); |
| 2887 |
|
| 2888 |
while (0) { |
| 2889 |
ibs_test_calc_ret_NOMEM: |
| 2890 |
retval = RET_NOMEM; |
| 2891 |
break; |
| 2892 |
ibs_test_calc_ret_THREAD_CREATE_FAIL: |
| 2893 |
retval = RET_THREAD_CREATE_FAIL; |
| 2894 |
break; |
| 2895 |
} |
| 2896 |
ibs_test_calc_ret_1: |
| 2897 |
bigstack_reset(bigstack_mark); |
| 2898 |
g_pheno_nm = nullptr; |
| 2899 |
g_pheno_c = nullptr; |
| 2900 |
return retval; |
| 2901 |
} |
| 2902 |
|
| 2903 |
int32_t groupdist_calc(pthread_t* threads, uint32_t unfiltered_sample_ct, uintptr_t* sample_exclude, uintptr_t sample_ct, uintptr_t groupdist_iters, uint32_t groupdist_d, uint32_t pheno_nm_ct, uint32_t pheno_ctrl_ct, uintptr_t* pheno_nm, uintptr_t* pheno_c) { |
| 2904 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 2905 |
uintptr_t unfiltered_sample_ctl = BITCT_TO_WORDCT(unfiltered_sample_ct); |
| 2906 |
double* dist_ptr = g_dists; |
| 2907 |
double dhh_ssq = 0.0; |
| 2908 |
double dhl_ssq = 0.0; |
| 2909 |
double dll_ssq = 0.0; |
| 2910 |
int32_t retval = 0; |
| 2911 |
uintptr_t* pheno_nm_local; |
| 2912 |
uintptr_t* pheno_c_local; |
| 2913 |
double* jackknife_precomp; |
| 2914 |
int32_t ll_size; |
| 2915 |
int32_t lh_size; |
| 2916 |
int32_t hh_size; |
| 2917 |
double* ll_pool; |
| 2918 |
double* lh_pool; |
| 2919 |
double* hh_pool; |
| 2920 |
double* ll_poolp; |
| 2921 |
double* lh_poolp; |
| 2922 |
double* hh_poolp; |
| 2923 |
uintptr_t ulii; |
| 2924 |
uint32_t uii; |
| 2925 |
uint32_t ujj; |
| 2926 |
uint32_t sample_idx; |
| 2927 |
double ll_med; |
| 2928 |
double lh_med; |
| 2929 |
double hh_med; |
| 2930 |
double dll_sd; |
| 2931 |
double dhl_sd; |
| 2932 |
double dhh_sd; |
| 2933 |
double dxx; |
| 2934 |
double dyy; |
| 2935 |
double dzz; |
| 2936 |
double dww; |
| 2937 |
uint32_t is_case; |
| 2938 |
if (pheno_ctrl_ct < 2) { |
| 2939 |
logerrprint("Warning: Skipping --groupdist due to too few controls (minimum 2).\n"); |
| 2940 |
goto groupdist_calc_ret_1; |
| 2941 |
} |
| 2942 |
g_case_ct = pheno_nm_ct - pheno_ctrl_ct; |
| 2943 |
if (g_case_ct < 2) { |
| 2944 |
logerrprint("Warning: Skipping --groupdist due to too few cases (minimum 2).\n"); |
| 2945 |
goto groupdist_calc_ret_1; |
| 2946 |
} |
| 2947 |
g_ctrl_ct = pheno_ctrl_ct; |
| 2948 |
g_sample_ct = sample_ct; |
| 2949 |
|
| 2950 |
if (bigstack_alloc_ul(unfiltered_sample_ctl, &pheno_nm_local) || |
| 2951 |
bigstack_alloc_ul(unfiltered_sample_ctl, &pheno_c_local)) { |
| 2952 |
goto groupdist_calc_ret_NOMEM; |
| 2953 |
} |
| 2954 |
g_pheno_nm = pheno_nm_local; |
| 2955 |
g_pheno_c = pheno_c_local; |
| 2956 |
copy_bitarr_subset_excl(pheno_nm, sample_exclude, unfiltered_sample_ct, sample_ct, pheno_nm_local); |
| 2957 |
copy_bitarr_subset_excl(pheno_c, sample_exclude, unfiltered_sample_ct, sample_ct, pheno_c_local); |
| 2958 |
ll_size = ((uintptr_t)g_ctrl_ct * (g_ctrl_ct - 1)) / 2; |
| 2959 |
lh_size = g_ctrl_ct * g_case_ct; |
| 2960 |
hh_size = ((uintptr_t)g_case_ct * (g_case_ct - 1)) / 2; |
| 2961 |
g_reg_tot_y = 0.0; |
| 2962 |
g_reg_tot_xy = 0.0; |
| 2963 |
g_reg_tot_x = 0.0; |
| 2964 |
if (groupdist_d) { |
| 2965 |
g_jackknife_d = groupdist_d; |
| 2966 |
} else { |
| 2967 |
g_jackknife_d = set_default_jackknife_d(g_case_ct + g_ctrl_ct); |
| 2968 |
} |
| 2969 |
if (bigstack_alloc_d(ll_size, &ll_pool) || |
| 2970 |
bigstack_alloc_d(lh_size, &lh_pool) || |
| 2971 |
bigstack_alloc_d(hh_size, &hh_pool) || |
| 2972 |
bigstack_alloc_uc(g_thread_ct * round_up_pow2(g_case_ct + g_ctrl_ct + (g_jackknife_d + 1) * sizeof(int32_t), CACHELINE), &g_geno)) { |
| 2973 |
goto groupdist_calc_ret_NOMEM; |
| 2974 |
} |
| 2975 |
ll_poolp = ll_pool; |
| 2976 |
lh_poolp = lh_pool; |
| 2977 |
hh_poolp = hh_pool; |
| 2978 |
for (sample_idx = 1; sample_idx < sample_ct; sample_idx++) { |
| 2979 |
if (IS_SET(pheno_nm_local, sample_idx)) { |
| 2980 |
if (IS_SET(pheno_c_local, sample_idx)) { |
| 2981 |
for (uii = 0; uii < sample_idx; uii++) { |
| 2982 |
if (IS_SET(pheno_nm_local, uii)) { |
| 2983 |
dxx = *dist_ptr; |
| 2984 |
if (IS_SET(pheno_c_local, uii)) { |
| 2985 |
*hh_poolp++ = dxx; |
| 2986 |
g_reg_tot_x += dxx; |
| 2987 |
dhh_ssq += dxx * dxx; |
| 2988 |
} else { |
| 2989 |
*lh_poolp++ = dxx; |
| 2990 |
g_reg_tot_xy += dxx; |
| 2991 |
dhl_ssq += dxx * dxx; |
| 2992 |
} |
| 2993 |
} |
| 2994 |
dist_ptr++; |
| 2995 |
} |
| 2996 |
} else { |
| 2997 |
for (uii = 0; uii < sample_idx; uii++) { |
| 2998 |
if (IS_SET(pheno_nm_local, uii)) { |
| 2999 |
dxx = *dist_ptr; |
| 3000 |
if (IS_SET(pheno_c_local, uii)) { |
| 3001 |
*lh_poolp++ = dxx; |
| 3002 |
g_reg_tot_xy += dxx; |
| 3003 |
dhl_ssq += dxx * dxx; |
| 3004 |
} else { |
| 3005 |
*ll_poolp++ = dxx; |
| 3006 |
g_reg_tot_y += dxx; |
| 3007 |
dll_ssq += dxx * dxx; |
| 3008 |
} |
| 3009 |
} |
| 3010 |
dist_ptr++; |
| 3011 |
} |
| 3012 |
} |
| 3013 |
} else { |
| 3014 |
dist_ptr += sample_idx; |
| 3015 |
} |
| 3016 |
} |
| 3017 |
ll_med = destructive_get_dmedian(ll_size, ll_pool); |
| 3018 |
lh_med = destructive_get_dmedian(lh_size, lh_pool); |
| 3019 |
hh_med = destructive_get_dmedian(hh_size, hh_pool); |
| 3020 |
logprint("Case/control distance analysis:\n"); |
| 3021 |
if (g_case_ct < 2) { |
| 3022 |
dxx = 0.0; |
| 3023 |
dhh_sd = 0.0; |
| 3024 |
} else { |
| 3025 |
dww = (double)(((uintptr_t)g_case_ct * (g_case_ct - 1)) / 2); |
| 3026 |
dxx = g_reg_tot_x / dww; |
| 3027 |
dhh_sd = sqrt((dhh_ssq / dww - dxx * dxx) / (dww - 1.0)); |
| 3028 |
} |
| 3029 |
if (!(g_case_ct && g_ctrl_ct)) { |
| 3030 |
dyy = 0.0; |
| 3031 |
dhl_sd = 0.0; |
| 3032 |
} else { |
| 3033 |
dww = (double)((uintptr_t)g_case_ct * g_ctrl_ct); |
| 3034 |
dyy = g_reg_tot_xy / dww; |
| 3035 |
dhl_sd = sqrt((dhl_ssq / dww - dyy * dyy) / (dww - 1.0)); |
| 3036 |
} |
| 3037 |
if (g_ctrl_ct < 2) { |
| 3038 |
dzz = 0.0; |
| 3039 |
dll_sd = 0.0; |
| 3040 |
} else { |
| 3041 |
dww = (double)(((uintptr_t)g_ctrl_ct * (g_ctrl_ct - 1)) / 2); |
| 3042 |
dzz = g_reg_tot_y / dww; |
| 3043 |
dll_sd = sqrt((dll_ssq / dww - dzz * dzz) / (dww - 1.0)); |
| 3044 |
} |
| 3045 |
LOGPRINTF(" Mean (sd), median dists between 2x affected : %g (%g), %g\n", dxx, dhh_sd, hh_med); |
| 3046 |
LOGPRINTF(" Mean (sd), median dists between aff. and unaff. : %g (%g), %g\n", dyy, dhl_sd, lh_med); |
| 3047 |
LOGPRINTF(" Mean (sd), median dists between 2x unaffected : %g (%g), %g\n\n", dzz, dll_sd, ll_med); |
| 3048 |
if (2 * g_jackknife_d >= (g_case_ct + g_ctrl_ct)) { |
| 3049 |
logprint("Delete-d jackknife skipped because d is too large.\n"); |
| 3050 |
} else { |
| 3051 |
if (bigstack_calloc_d(sample_ct * JACKKNIFE_VALS_GROUPDIST, &jackknife_precomp)) { |
| 3052 |
goto groupdist_calc_ret_NOMEM; |
| 3053 |
} |
| 3054 |
g_jackknife_precomp = jackknife_precomp; |
| 3055 |
if (bigstack_init_sfmtp(g_thread_ct)) { |
| 3056 |
goto groupdist_calc_ret_NOMEM; |
| 3057 |
} |
| 3058 |
|
| 3059 |
|
| 3060 |
dist_ptr = g_dists; |
| 3061 |
for (sample_idx = 1; sample_idx < sample_ct; sample_idx++) { |
| 3062 |
if (IS_SET(pheno_nm_local, sample_idx)) { |
| 3063 |
uii = 0; |
| 3064 |
is_case = IS_SET(pheno_c_local, sample_idx); |
| 3065 |
dyy = 0; |
| 3066 |
dzz = 0; |
| 3067 |
do { |
| 3068 |
if (IS_SET(pheno_nm_local, uii)) { |
| 3069 |
dxx = dist_ptr[uii]; |
| 3070 |
if (IS_SET(pheno_c_local, uii)) { |
| 3071 |
jackknife_precomp[(uii * JACKKNIFE_VALS_GROUPDIST) + is_case + 1] += dxx; |
| 3072 |
dzz += dxx; |
| 3073 |
} else { |
| 3074 |
jackknife_precomp[(uii * JACKKNIFE_VALS_GROUPDIST) + is_case] += dxx; |
| 3075 |
dyy += dxx; |
| 3076 |
} |
| 3077 |
} |
| 3078 |
} while ((++uii) < sample_idx); |
| 3079 |
jackknife_precomp[(sample_idx * JACKKNIFE_VALS_GROUPDIST) + is_case] += dyy; |
| 3080 |
jackknife_precomp[(sample_idx * JACKKNIFE_VALS_GROUPDIST) + is_case + 1] += dzz; |
| 3081 |
} |
| 3082 |
dist_ptr = &(dist_ptr[sample_idx]); |
| 3083 |
} |
| 3084 |
|
| 3085 |
g_jackknife_iters = (groupdist_iters + g_thread_ct - 1) / g_thread_ct; |
| 3086 |
if (spawn_threads(threads, &groupdist_jack_thread, g_thread_ct)) { |
| 3087 |
goto groupdist_calc_ret_THREAD_CREATE_FAIL; |
| 3088 |
} |
| 3089 |
ulii = 0; |
| 3090 |
groupdist_jack_thread((void*)ulii); |
| 3091 |
join_threads(threads, g_thread_ct); |
| 3092 |
for (uii = 1; uii < g_thread_ct; uii++) { |
| 3093 |
for (ujj = 0; ujj < 9; ujj++) { |
| 3094 |
g_calc_result[0][ujj] += g_calc_result[uii][ujj]; |
| 3095 |
} |
| 3096 |
} |
| 3097 |
dxx = 1.0 / ((double)(g_jackknife_iters * g_thread_ct)); |
| 3098 |
|
| 3099 |
|
| 3100 |
|
| 3101 |
|
| 3102 |
|
| 3103 |
|
| 3104 |
|
| 3105 |
|
| 3106 |
|
| 3107 |
for (uii = 0; uii < 9; uii++) { |
| 3108 |
g_calc_result[0][uii] *= dxx; |
| 3109 |
} |
| 3110 |
putc_unlocked('\r', stdout); |
| 3111 |
dxx = g_calc_result[0][0] - g_calc_result[0][1]; |
| 3112 |
LOGPRINTF(" AA mean - AU mean avg difference (s.e.): %g (%g)\n", dxx, sqrt(((g_case_ct + g_ctrl_ct - g_jackknife_d) / ((double)g_jackknife_d)) * (g_calc_result[0][3] + g_calc_result[0][4] - 2 * g_calc_result[0][6] - dxx * dxx))); |
| 3113 |
dxx = g_calc_result[0][0] - g_calc_result[0][2]; |
| 3114 |
LOGPRINTF(" AA mean - UU mean avg difference (s.e.): %g (%g)\n", dxx, sqrt(((g_case_ct + g_ctrl_ct - g_jackknife_d) / ((double)g_jackknife_d)) * (g_calc_result[0][3] + g_calc_result[0][5] - 2 * g_calc_result[0][7] - dxx * dxx))); |
| 3115 |
dxx = g_calc_result[0][1] - g_calc_result[0][2]; |
| 3116 |
LOGPRINTF(" AU mean - UU mean avg difference (s.e.): %g (%g)\n", dxx, sqrt(((g_case_ct + g_ctrl_ct - g_jackknife_d) / ((double)g_jackknife_d)) * (g_calc_result[0][4] + g_calc_result[0][5] - 2 * g_calc_result[0][8] - dxx * dxx))); |
| 3117 |
} |
| 3118 |
while (0) { |
| 3119 |
groupdist_calc_ret_NOMEM: |
| 3120 |
retval = RET_NOMEM; |
| 3121 |
break; |
| 3122 |
groupdist_calc_ret_THREAD_CREATE_FAIL: |
| 3123 |
retval = RET_THREAD_CREATE_FAIL; |
| 3124 |
break; |
| 3125 |
} |
| 3126 |
groupdist_calc_ret_1: |
| 3127 |
bigstack_reset(bigstack_mark); |
| 3128 |
g_pheno_nm = nullptr; |
| 3129 |
g_pheno_c = nullptr; |
| 3130 |
return retval; |
| 3131 |
} |
| 3132 |
|
| 3133 |
void normalize_phenos(double* new_phenos, uint32_t sample_ct, uintptr_t* sample_exclude, uintptr_t* sex_nm, uintptr_t* sex_male, uint32_t sex_exclude) { |
| 3134 |
uint32_t incl_males = sex_exclude & 1; |
| 3135 |
uint32_t incl_females = sex_exclude & 2; |
| 3136 |
uint32_t incl_unknown = sex_exclude & 4; |
| 3137 |
uint32_t sample_uidx = 0xffffffffU; |
| 3138 |
double pheno_tot = 0.0; |
| 3139 |
double pheno_sq_tot = 0.0; |
| 3140 |
uint32_t pheno_ct = 0; |
| 3141 |
uint32_t sample_idx = 0xffffffffU; |
| 3142 |
double dxx; |
| 3143 |
double mean; |
| 3144 |
double stdev_recip; |
| 3145 |
|
| 3146 |
while ((++sample_idx) < sample_ct) { |
| 3147 |
sample_uidx++; |
| 3148 |
next_unset_unsafe_ck(sample_exclude, &sample_uidx); |
| 3149 |
if (IS_SET(sex_nm, sample_uidx)) { |
| 3150 |
if (IS_SET(sex_male, sample_uidx)) { |
| 3151 |
if (!incl_males) { |
| 3152 |
continue; |
| 3153 |
} |
| 3154 |
} else if (!incl_females) { |
| 3155 |
continue; |
| 3156 |
} |
| 3157 |
} else if (!incl_unknown) { |
| 3158 |
continue; |
| 3159 |
} |
| 3160 |
dxx = new_phenos[sample_idx]; |
| 3161 |
pheno_tot += dxx; |
| 3162 |
pheno_sq_tot += dxx * dxx; |
| 3163 |
pheno_ct++; |
| 3164 |
} |
| 3165 |
if (!pheno_ct) { |
| 3166 |
return; |
| 3167 |
} |
| 3168 |
mean = pheno_tot / pheno_ct; |
| 3169 |
if (pheno_ct == 1) { |
| 3170 |
stdev_recip = 0; |
| 3171 |
} else { |
| 3172 |
stdev_recip = sqrt((double)(pheno_ct - 1) / (pheno_sq_tot - pheno_tot * mean)); |
| 3173 |
} |
| 3174 |
sample_uidx = 0xffffffffU; |
| 3175 |
sample_idx = 0xffffffffU; |
| 3176 |
while ((++sample_idx) < sample_ct) { |
| 3177 |
sample_uidx++; |
| 3178 |
next_unset_unsafe_ck(sample_exclude, &sample_uidx); |
| 3179 |
if (IS_SET(sex_nm, sample_uidx)) { |
| 3180 |
if (IS_SET(sex_male, sample_uidx)) { |
| 3181 |
if (!incl_males) { |
| 3182 |
continue; |
| 3183 |
} |
| 3184 |
} else if (!incl_females) { |
| 3185 |
continue; |
| 3186 |
} |
| 3187 |
} else if (!incl_unknown) { |
| 3188 |
continue; |
| 3189 |
} |
| 3190 |
new_phenos[sample_idx] = (new_phenos[sample_idx] - mean) * stdev_recip; |
| 3191 |
} |
| 3192 |
} |
| 3193 |
|
| 3194 |
static uintptr_t g_cg_sample1idx; |
| 3195 |
static uintptr_t g_cg_sample2idx; |
| 3196 |
static uintptr_t g_cg_sample1uidx; |
| 3197 |
static uintptr_t g_cg_sample2uidx; |
| 3198 |
static uintptr_t g_cg_tstc; |
| 3199 |
static uint32_t g_cg_marker_ct; |
| 3200 |
static uintptr_t g_cg_max_sample_id_len; |
| 3201 |
static uint32_t g_cg_max_sample_fid_len; |
| 3202 |
static uint32_t g_cg_max_sample_iid_len; |
| 3203 |
static uintptr_t g_cg_max_paternal_id_len; |
| 3204 |
static uintptr_t g_cg_max_maternal_id_len; |
| 3205 |
static uintptr_t* g_cg_sample_exclude; |
| 3206 |
static uintptr_t* g_cg_pheno_nm; |
| 3207 |
static uintptr_t* g_cg_pheno_c; |
| 3208 |
static uintptr_t* g_cg_founder_info; |
| 3209 |
static char* g_cg_sample_ids; |
| 3210 |
static char* g_cg_paternal_ids; |
| 3211 |
static char* g_cg_maternal_ids; |
| 3212 |
static uint32_t g_cg_genome_modifier; |
| 3213 |
static Pedigree_rel_info g_cg_pri; |
| 3214 |
static uint32_t g_cg_family_id_fixed; |
| 3215 |
static uint32_t g_cg_is_founder_fixed; |
| 3216 |
static uint32_t g_cg_rel_space_id_fixed; |
| 3217 |
static int64_t g_cg_llfct; |
| 3218 |
static char* g_cg_sptr_start; |
| 3219 |
static char* g_cg_fam1; |
| 3220 |
static char* g_cg_fam2; |
| 3221 |
static char* g_cg_sample1; |
| 3222 |
static char* g_cg_pat1; |
| 3223 |
static char* g_cg_mat1; |
| 3224 |
static int64_t g_cg_cur_line; |
| 3225 |
static int64_t g_cg_tot_lines; |
| 3226 |
static uint32_t g_pct; |
| 3227 |
static uintptr_t g_cg_gmcell; |
| 3228 |
static uintptr_t g_cg_mdecell; |
| 3229 |
static double g_cg_e00; |
| 3230 |
static double g_cg_e01; |
| 3231 |
static double g_cg_e02; |
| 3232 |
static double g_cg_e11; |
| 3233 |
static double g_cg_e12; |
| 3234 |
static double g_cg_min_pi_hat; |
| 3235 |
static double g_cg_max_pi_hat; |
| 3236 |
|
| 3237 |
static uintptr_t g_dw_sample1idx; |
| 3238 |
static uintptr_t g_dw_sample2idx; |
| 3239 |
static uintptr_t g_dw_min_sample; |
| 3240 |
static uintptr_t g_dw_max_sample1idx; |
| 3241 |
static uint64_t g_dw_start_offset; |
| 3242 |
static uint64_t g_dw_hundredth; |
| 3243 |
static double* g_dw_dists; |
| 3244 |
static double* g_dw_dist_ptr; |
| 3245 |
static unsigned char* g_dw_membuf; |
| 3246 |
static double g_dw_half_marker_ct_recip; |
| 3247 |
|
| 3248 |
int32_t write_ids(char* outname, uintptr_t unfiltered_sample_ct, uintptr_t* sample_exclude, char* sample_ids, uintptr_t max_sample_id_len) { |
| 3249 |
uintptr_t sample_uidx = 0; |
| 3250 |
FILE* outfile; |
| 3251 |
if (fopen_checked(outname, "w", &outfile)) { |
| 3252 |
return RET_OPEN_FAIL; |
| 3253 |
} |
| 3254 |
while (1) { |
| 3255 |
next_unset_ul_ck(sample_exclude, unfiltered_sample_ct, &sample_uidx); |
| 3256 |
if (sample_uidx == unfiltered_sample_ct) { |
| 3257 |
break; |
| 3258 |
} |
| 3259 |
fputs(&(sample_ids[sample_uidx * max_sample_id_len]), outfile); |
| 3260 |
if (putc_checked('\n', outfile)) { |
| 3261 |
return RET_WRITE_FAIL; |
| 3262 |
} |
| 3263 |
sample_uidx++; |
| 3264 |
} |
| 3265 |
if (fclose_null(&outfile)) { |
| 3266 |
return RET_WRITE_FAIL; |
| 3267 |
} |
| 3268 |
return 0; |
| 3269 |
} |
| 3270 |
|
| 3271 |
int32_t distance_d_write_ids(char* outname, char* outname_end, uint32_t dist_calc_type, uintptr_t unfiltered_sample_ct, uintptr_t* sample_exclude, char* sample_ids, uintptr_t max_sample_id_len) { |
| 3272 |
int32_t retval; |
| 3273 |
if (dist_calc_type & DISTANCE_ALCT) { |
| 3274 |
strcpy(outname_end, ".dist.id"); |
| 3275 |
retval = write_ids(outname, unfiltered_sample_ct, sample_exclude, sample_ids, max_sample_id_len); |
| 3276 |
if (retval) { |
| 3277 |
return retval; |
| 3278 |
} |
| 3279 |
} |
| 3280 |
if (dist_calc_type & DISTANCE_IBS) { |
| 3281 |
strcpy(outname_end, ".mibs.id"); |
| 3282 |
retval = write_ids(outname, unfiltered_sample_ct, sample_exclude, sample_ids, max_sample_id_len); |
| 3283 |
if (retval) { |
| 3284 |
return retval; |
| 3285 |
} |
| 3286 |
} |
| 3287 |
if (dist_calc_type & DISTANCE_1_MINUS_IBS) { |
| 3288 |
strcpy(outname_end, ".mdist.id"); |
| 3289 |
retval = write_ids(outname, unfiltered_sample_ct, sample_exclude, sample_ids, max_sample_id_len); |
| 3290 |
if (retval) { |
| 3291 |
return retval; |
| 3292 |
} |
| 3293 |
} |
| 3294 |
return 0; |
| 3295 |
} |
| 3296 |
|
| 3297 |
int32_t distance_open(FILE** outfile_ptr, FILE** outfile2_ptr, FILE** outfile3_ptr, char* outname, char* outname_end, const char* varsuffix, const char* mode, int32_t dist_calc_type, int32_t parallel_idx, int32_t parallel_tot) { |
| 3298 |
if (dist_calc_type & DISTANCE_ALCT) { |
| 3299 |
if (parallel_tot > 1) { |
| 3300 |
snprintf(outname_end, MAX_POST_EXT, ".dist%s.%d", varsuffix, parallel_idx + 1); |
| 3301 |
} else { |
| 3302 |
snprintf(outname_end, MAX_POST_EXT, ".dist%s", varsuffix); |
| 3303 |
} |
| 3304 |
strcpy(g_textbuf, outname_end); |
| 3305 |
if (fopen_checked(outname, mode, outfile_ptr)) { |
| 3306 |
return 1; |
| 3307 |
} |
| 3308 |
} |
| 3309 |
if (dist_calc_type & DISTANCE_IBS) { |
| 3310 |
if (parallel_tot > 1) { |
| 3311 |
snprintf(outname_end, MAX_POST_EXT, ".mibs%s.%d", varsuffix, parallel_idx + 1); |
| 3312 |
} else { |
| 3313 |
snprintf(outname_end, MAX_POST_EXT, ".mibs%s", varsuffix); |
| 3314 |
} |
| 3315 |
strcpy(&(g_textbuf[MAX_POST_EXT]), outname_end); |
| 3316 |
if (fopen_checked(outname, mode, outfile2_ptr)) { |
| 3317 |
return 1; |
| 3318 |
} |
| 3319 |
} |
| 3320 |
if (dist_calc_type & DISTANCE_1_MINUS_IBS) { |
| 3321 |
if (parallel_tot > 1) { |
| 3322 |
snprintf(outname_end, MAX_POST_EXT, ".mdist%s.%d", varsuffix, parallel_idx + 1); |
| 3323 |
} else { |
| 3324 |
snprintf(outname_end, MAX_POST_EXT, ".mdist%s", varsuffix); |
| 3325 |
} |
| 3326 |
strcpy(&(g_textbuf[MAX_POST_EXT * 2]), outname_end); |
| 3327 |
if (fopen_checked(outname, mode, outfile3_ptr)) { |
| 3328 |
return 1; |
| 3329 |
} |
| 3330 |
} |
| 3331 |
return 0; |
| 3332 |
} |
| 3333 |
|
| 3334 |
void distance_print_done(int32_t format_code, char* outname, char* outname_end) { |
| 3335 |
putc_unlocked('\r', stdout); |
| 3336 |
if (!format_code) { |
| 3337 |
strcpy(outname_end, g_textbuf); |
| 3338 |
snprintf(g_logbuf, LOGBUFLEN, "Distances (allele counts) written to %s .\n", outname); |
| 3339 |
} else if (format_code == 1) { |
| 3340 |
strcpy(outname_end, &(g_textbuf[MAX_POST_EXT])); |
| 3341 |
snprintf(g_logbuf, LOGBUFLEN, "IBS matrix written to %s .\n", outname); |
| 3342 |
} else if (format_code == 2) { |
| 3343 |
strcpy(outname_end, &(g_textbuf[MAX_POST_EXT * 2])); |
| 3344 |
snprintf(g_logbuf, LOGBUFLEN, "Distances (proportions) written to %s .\n", outname); |
| 3345 |
} |
| 3346 |
wordwrapb(0); |
| 3347 |
logprintb(); |
| 3348 |
} |
| 3349 |
|
| 3350 |
uint32_t distance_d_write_tri_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 3351 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 3352 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 3353 |
uint64_t start_offset = g_dw_start_offset; |
| 3354 |
uint64_t hundredth = g_dw_hundredth; |
| 3355 |
uintptr_t max_sample1idx = g_dw_max_sample1idx; |
| 3356 |
double* dist_ptr = g_dw_dist_ptr; |
| 3357 |
uintptr_t sample1idx = g_dw_sample1idx; |
| 3358 |
uintptr_t sample2idx = g_dw_sample2idx; |
| 3359 |
uint32_t pct = g_pct; |
| 3360 |
while (sample1idx < max_sample1idx) { |
| 3361 |
while (sample2idx + 1 < sample1idx) { |
| 3362 |
sptr_cur = dtoa_gx(*dist_ptr++, '\t', sptr_cur); |
| 3363 |
sample2idx++; |
| 3364 |
if (sptr_cur >= readbuf_end) { |
| 3365 |
goto distance_d_write_tri_emitn_ret; |
| 3366 |
} |
| 3367 |
} |
| 3368 |
if (sample2idx + 1 == sample1idx) { |
| 3369 |
sptr_cur = dtoa_gx(*dist_ptr++, '\n', sptr_cur); |
| 3370 |
} |
| 3371 |
if ((((uint64_t)sample1idx) * (sample1idx + 1) / 2 - start_offset) >= hundredth * pct) { |
| 3372 |
pct = (((uint64_t)sample1idx) * (sample1idx + 1) / 2 - start_offset) / hundredth; |
| 3373 |
printf("\rWriting... %u%%", pct++); |
| 3374 |
fflush(stdout); |
| 3375 |
} |
| 3376 |
sample1idx++; |
| 3377 |
sample2idx = 0; |
| 3378 |
} |
| 3379 |
distance_d_write_tri_emitn_ret: |
| 3380 |
g_dw_dist_ptr = dist_ptr; |
| 3381 |
g_dw_sample1idx = sample1idx; |
| 3382 |
g_dw_sample2idx = sample2idx; |
| 3383 |
g_pct = pct; |
| 3384 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 3385 |
} |
| 3386 |
|
| 3387 |
uint32_t distance_d_write_sq0_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 3388 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 3389 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 3390 |
uintptr_t sample_ct = g_sample_ct; |
| 3391 |
uintptr_t max_sample1idx = g_dw_max_sample1idx; |
| 3392 |
uintptr_t min_sample = g_dw_min_sample; |
| 3393 |
double* dist_ptr = g_dw_dist_ptr; |
| 3394 |
unsigned char* membuf1 = &(g_dw_membuf[1]); |
| 3395 |
uintptr_t sample1idx = g_dw_sample1idx; |
| 3396 |
uintptr_t sample2idx = g_dw_sample2idx; |
| 3397 |
uint32_t pct = g_pct; |
| 3398 |
uintptr_t ulii; |
| 3399 |
while (sample1idx < max_sample1idx) { |
| 3400 |
while (sample2idx < sample1idx) { |
| 3401 |
sptr_cur = dtoa_gx(*dist_ptr++, '\t', sptr_cur); |
| 3402 |
sample2idx++; |
| 3403 |
if (sptr_cur >= readbuf_end) { |
| 3404 |
goto distance_d_write_sq0_emitn_ret; |
| 3405 |
} |
| 3406 |
} |
| 3407 |
ulii = 1 + (((uintptr_t)(readbuf_end - sptr_cur)) / 2); |
| 3408 |
if (ulii < (sample_ct - sample2idx)) { |
| 3409 |
sptr_cur = memcpya(sptr_cur, membuf1, 2 * ulii); |
| 3410 |
sample2idx += ulii; |
| 3411 |
goto distance_d_write_sq0_emitn_ret; |
| 3412 |
} |
| 3413 |
ulii = sample_ct - sample2idx; |
| 3414 |
sptr_cur = memcpyax(sptr_cur, membuf1, 2 * ulii - 1, '\n'); |
| 3415 |
sample1idx++; |
| 3416 |
if ((sample1idx - min_sample) * 100LLU >= ((uint64_t)pct) * (max_sample1idx - min_sample)) { |
| 3417 |
pct = ((sample1idx - min_sample) * 100LLU) / (max_sample1idx - min_sample); |
| 3418 |
printf("\rWriting... %u%%", pct++); |
| 3419 |
fflush(stdout); |
| 3420 |
} |
| 3421 |
sample2idx = 0; |
| 3422 |
} |
| 3423 |
distance_d_write_sq0_emitn_ret: |
| 3424 |
g_dw_dist_ptr = dist_ptr; |
| 3425 |
g_dw_sample1idx = sample1idx; |
| 3426 |
g_dw_sample2idx = sample2idx; |
| 3427 |
g_pct = pct; |
| 3428 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 3429 |
} |
| 3430 |
|
| 3431 |
uint32_t distance_d_write_sq_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 3432 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 3433 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 3434 |
uintptr_t sample_ct = g_sample_ct; |
| 3435 |
uintptr_t max_sample1idx = g_dw_max_sample1idx; |
| 3436 |
uintptr_t min_sample = g_dw_min_sample; |
| 3437 |
double* dists = g_dw_dists; |
| 3438 |
double* dist_ptr = g_dw_dist_ptr; |
| 3439 |
uintptr_t sample1idx = g_dw_sample1idx; |
| 3440 |
uintptr_t sample2idx = g_dw_sample2idx; |
| 3441 |
uint32_t pct = g_pct; |
| 3442 |
while (sample1idx < max_sample1idx) { |
| 3443 |
while (sample2idx < sample1idx) { |
| 3444 |
sptr_cur = dtoa_gx(*dist_ptr++, '\t', sptr_cur); |
| 3445 |
sample2idx++; |
| 3446 |
if (sptr_cur >= readbuf_end) { |
| 3447 |
goto distance_d_write_sq_emitn_ret; |
| 3448 |
} |
| 3449 |
} |
| 3450 |
if (sample2idx == sample1idx) { |
| 3451 |
*sptr_cur++ = '0'; |
| 3452 |
sample2idx++; |
| 3453 |
} |
| 3454 |
while (sample2idx < sample_ct) { |
| 3455 |
*sptr_cur++ = '\t'; |
| 3456 |
sptr_cur = dtoa_g(dists[((sample2idx * (sample2idx - 1)) / 2) + sample1idx], sptr_cur); |
| 3457 |
sample2idx++; |
| 3458 |
if (sptr_cur >= readbuf_end) { |
| 3459 |
goto distance_d_write_sq_emitn_ret; |
| 3460 |
} |
| 3461 |
} |
| 3462 |
*sptr_cur++ = '\n'; |
| 3463 |
sample1idx++; |
| 3464 |
if ((sample1idx - min_sample) * 100LLU >= ((uint64_t)pct) * (max_sample1idx - min_sample)) { |
| 3465 |
pct = ((sample1idx - min_sample) * 100LLU) / (max_sample1idx - min_sample); |
| 3466 |
printf("\rWriting... %u%%", pct++); |
| 3467 |
fflush(stdout); |
| 3468 |
} |
| 3469 |
sample2idx = 0; |
| 3470 |
} |
| 3471 |
distance_d_write_sq_emitn_ret: |
| 3472 |
g_dw_dist_ptr = dist_ptr; |
| 3473 |
g_dw_sample1idx = sample1idx; |
| 3474 |
g_dw_sample2idx = sample2idx; |
| 3475 |
g_pct = pct; |
| 3476 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 3477 |
} |
| 3478 |
|
| 3479 |
uint32_t distance_d_write_ibs_tri_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 3480 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 3481 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 3482 |
uint64_t start_offset = g_dw_start_offset; |
| 3483 |
uint64_t hundredth = g_dw_hundredth; |
| 3484 |
double half_marker_ct_recip = g_dw_half_marker_ct_recip; |
| 3485 |
uintptr_t max_sample1idx = g_dw_max_sample1idx; |
| 3486 |
double* dist_ptr = g_dw_dist_ptr; |
| 3487 |
uintptr_t sample1idx = g_dw_sample1idx; |
| 3488 |
uintptr_t sample2idx = g_dw_sample2idx; |
| 3489 |
uint32_t pct = g_pct; |
| 3490 |
while (sample1idx < max_sample1idx) { |
| 3491 |
while (sample2idx < sample1idx) { |
| 3492 |
sptr_cur = dtoa_gx(1.0 - (*dist_ptr++) * half_marker_ct_recip, '\t', sptr_cur); |
| 3493 |
sample2idx++; |
| 3494 |
if (sptr_cur >= readbuf_end) { |
| 3495 |
goto distance_d_write_ibs_tri_emitn_ret; |
| 3496 |
} |
| 3497 |
} |
| 3498 |
if (sample2idx == sample1idx) { |
| 3499 |
sptr_cur = memcpya(sptr_cur, "1\n", 2); |
| 3500 |
} |
| 3501 |
sample1idx++; |
| 3502 |
if ((((uint64_t)sample1idx) * (sample1idx + 1) / 2 - start_offset) >= hundredth * pct) { |
| 3503 |
pct = (((uint64_t)sample1idx) * (sample1idx + 1) / 2 - start_offset) / hundredth; |
| 3504 |
printf("\rWriting... %u%%", pct++); |
| 3505 |
fflush(stdout); |
| 3506 |
} |
| 3507 |
sample2idx = 0; |
| 3508 |
} |
| 3509 |
distance_d_write_ibs_tri_emitn_ret: |
| 3510 |
g_dw_dist_ptr = dist_ptr; |
| 3511 |
g_dw_sample1idx = sample1idx; |
| 3512 |
g_dw_sample2idx = sample2idx; |
| 3513 |
g_pct = pct; |
| 3514 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 3515 |
} |
| 3516 |
|
| 3517 |
uint32_t distance_d_write_ibs_sq0_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 3518 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 3519 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 3520 |
double half_marker_ct_recip = g_dw_half_marker_ct_recip; |
| 3521 |
uintptr_t sample_ct = g_sample_ct; |
| 3522 |
uintptr_t max_sample1idx = g_dw_max_sample1idx; |
| 3523 |
uintptr_t min_sample = g_dw_min_sample; |
| 3524 |
double* dist_ptr = g_dw_dist_ptr; |
| 3525 |
unsigned char* membuf = g_dw_membuf; |
| 3526 |
uintptr_t sample1idx = g_dw_sample1idx; |
| 3527 |
uintptr_t sample2idx = g_dw_sample2idx; |
| 3528 |
uint32_t pct = g_pct; |
| 3529 |
uintptr_t ulii; |
| 3530 |
while (sample1idx < max_sample1idx) { |
| 3531 |
while (sample2idx < sample1idx) { |
| 3532 |
sptr_cur = dtoa_gx(1.0 - (*dist_ptr++) * half_marker_ct_recip, '\t', sptr_cur); |
| 3533 |
sample2idx++; |
| 3534 |
if (sptr_cur >= readbuf_end) { |
| 3535 |
goto distance_d_write_ibs_sq0_emitn_ret; |
| 3536 |
} |
| 3537 |
} |
| 3538 |
if (sample2idx == sample1idx) { |
| 3539 |
*sptr_cur++ = '1'; |
| 3540 |
sample2idx++; |
| 3541 |
if (sptr_cur >= readbuf_end) { |
| 3542 |
goto distance_d_write_ibs_sq0_emitn_ret; |
| 3543 |
} |
| 3544 |
} |
| 3545 |
ulii = (1 + ((uintptr_t)(readbuf_end - sptr_cur))) / 2; |
| 3546 |
if (ulii < (sample_ct - sample2idx)) { |
| 3547 |
sptr_cur = memcpya(sptr_cur, membuf, 2 * ulii); |
| 3548 |
sample2idx += ulii; |
| 3549 |
goto distance_d_write_ibs_sq0_emitn_ret; |
| 3550 |
} |
| 3551 |
ulii = sample_ct - sample2idx; |
| 3552 |
sptr_cur = memcpyax(sptr_cur, membuf, 2 * ulii, '\n'); |
| 3553 |
sample1idx++; |
| 3554 |
if ((sample1idx - min_sample) * 100LLU >= ((uint64_t)pct) * (max_sample1idx - min_sample)) { |
| 3555 |
pct = ((sample1idx - min_sample) * 100LLU) / (max_sample1idx - min_sample); |
| 3556 |
printf("\rWriting... %u%%", pct++); |
| 3557 |
fflush(stdout); |
| 3558 |
} |
| 3559 |
sample2idx = 0; |
| 3560 |
} |
| 3561 |
distance_d_write_ibs_sq0_emitn_ret: |
| 3562 |
g_dw_dist_ptr = dist_ptr; |
| 3563 |
g_dw_sample1idx = sample1idx; |
| 3564 |
g_dw_sample2idx = sample2idx; |
| 3565 |
g_pct = pct; |
| 3566 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 3567 |
} |
| 3568 |
|
| 3569 |
uint32_t distance_d_write_ibs_sq_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 3570 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 3571 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 3572 |
double half_marker_ct_recip = g_dw_half_marker_ct_recip; |
| 3573 |
uintptr_t sample_ct = g_sample_ct; |
| 3574 |
uintptr_t max_sample1idx = g_dw_max_sample1idx; |
| 3575 |
uintptr_t min_sample = g_dw_min_sample; |
| 3576 |
double* dists = g_dw_dists; |
| 3577 |
double* dist_ptr = g_dw_dist_ptr; |
| 3578 |
uintptr_t sample1idx = g_dw_sample1idx; |
| 3579 |
uintptr_t sample2idx = g_dw_sample2idx; |
| 3580 |
uint32_t pct = g_pct; |
| 3581 |
while (sample1idx < max_sample1idx) { |
| 3582 |
while (sample2idx < sample1idx) { |
| 3583 |
sptr_cur = dtoa_gx(1.0 - (*dist_ptr++) * half_marker_ct_recip, '\t', sptr_cur); |
| 3584 |
sample2idx++; |
| 3585 |
if (sptr_cur >= readbuf_end) { |
| 3586 |
goto distance_d_write_ibs_sq_emitn_ret; |
| 3587 |
} |
| 3588 |
} |
| 3589 |
if (sample2idx == sample1idx) { |
| 3590 |
*sptr_cur++ = '1'; |
| 3591 |
sample2idx++; |
| 3592 |
} |
| 3593 |
while (sample2idx < sample_ct) { |
| 3594 |
*sptr_cur++ = '\t'; |
| 3595 |
sptr_cur = dtoa_g(1.0 - (dists[((sample2idx * (sample2idx - 1)) / 2) + sample1idx]) * half_marker_ct_recip, sptr_cur); |
| 3596 |
sample2idx++; |
| 3597 |
if (sptr_cur >= readbuf_end) { |
| 3598 |
goto distance_d_write_ibs_sq_emitn_ret; |
| 3599 |
} |
| 3600 |
} |
| 3601 |
*sptr_cur++ = '\n'; |
| 3602 |
sample1idx++; |
| 3603 |
if ((sample1idx - min_sample) * 100LLU >= ((uint64_t)pct) * (max_sample1idx - min_sample)) { |
| 3604 |
pct = ((sample1idx - min_sample) * 100LLU) / (max_sample1idx - min_sample); |
| 3605 |
printf("\rWriting... %u%%", pct++); |
| 3606 |
fflush(stdout); |
| 3607 |
} |
| 3608 |
sample2idx = 0; |
| 3609 |
} |
| 3610 |
distance_d_write_ibs_sq_emitn_ret: |
| 3611 |
g_dw_dist_ptr = dist_ptr; |
| 3612 |
g_dw_sample1idx = sample1idx; |
| 3613 |
g_dw_sample2idx = sample2idx; |
| 3614 |
g_pct = pct; |
| 3615 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 3616 |
} |
| 3617 |
|
| 3618 |
uint32_t distance_d_write_1mibs_tri_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 3619 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 3620 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 3621 |
uint64_t start_offset = g_dw_start_offset; |
| 3622 |
uint64_t hundredth = g_dw_hundredth; |
| 3623 |
double half_marker_ct_recip = g_dw_half_marker_ct_recip; |
| 3624 |
uintptr_t max_sample1idx = g_dw_max_sample1idx; |
| 3625 |
double* dist_ptr = g_dw_dist_ptr; |
| 3626 |
uintptr_t sample1idx = g_dw_sample1idx; |
| 3627 |
uintptr_t sample2idx = g_dw_sample2idx; |
| 3628 |
uint32_t pct = g_pct; |
| 3629 |
while (sample1idx < max_sample1idx) { |
| 3630 |
while (sample2idx + 1 < sample1idx) { |
| 3631 |
sptr_cur = dtoa_gx((*dist_ptr++) * half_marker_ct_recip, '\t', sptr_cur); |
| 3632 |
sample2idx++; |
| 3633 |
if (sptr_cur >= readbuf_end) { |
| 3634 |
goto distance_d_write_1mibs_tri_emitn_ret; |
| 3635 |
} |
| 3636 |
} |
| 3637 |
if (sample2idx + 1 == sample1idx) { |
| 3638 |
sptr_cur = dtoa_gx((*dist_ptr++) * half_marker_ct_recip, '\n', sptr_cur); |
| 3639 |
} |
| 3640 |
if ((((uint64_t)sample1idx) * (sample1idx + 1) / 2 - start_offset) >= hundredth * pct) { |
| 3641 |
pct = (((uint64_t)sample1idx) * (sample1idx + 1) / 2 - start_offset) / hundredth; |
| 3642 |
printf("\rWriting... %u%%", pct++); |
| 3643 |
fflush(stdout); |
| 3644 |
} |
| 3645 |
sample1idx++; |
| 3646 |
sample2idx = 0; |
| 3647 |
} |
| 3648 |
distance_d_write_1mibs_tri_emitn_ret: |
| 3649 |
g_dw_dist_ptr = dist_ptr; |
| 3650 |
g_dw_sample1idx = sample1idx; |
| 3651 |
g_dw_sample2idx = sample2idx; |
| 3652 |
g_pct = pct; |
| 3653 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 3654 |
} |
| 3655 |
|
| 3656 |
uint32_t distance_d_write_1mibs_sq0_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 3657 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 3658 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 3659 |
double half_marker_ct_recip = g_dw_half_marker_ct_recip; |
| 3660 |
uintptr_t sample_ct = g_sample_ct; |
| 3661 |
uintptr_t max_sample1idx = g_dw_max_sample1idx; |
| 3662 |
uintptr_t min_sample = g_dw_min_sample; |
| 3663 |
double* dist_ptr = g_dw_dist_ptr; |
| 3664 |
unsigned char* membuf1 = &(g_dw_membuf[1]); |
| 3665 |
uintptr_t sample1idx = g_dw_sample1idx; |
| 3666 |
uintptr_t sample2idx = g_dw_sample2idx; |
| 3667 |
uint32_t pct = g_pct; |
| 3668 |
uintptr_t ulii; |
| 3669 |
while (sample1idx < max_sample1idx) { |
| 3670 |
while (sample2idx < sample1idx) { |
| 3671 |
sptr_cur = dtoa_gx((*dist_ptr++) * half_marker_ct_recip, '\t', sptr_cur); |
| 3672 |
sample2idx++; |
| 3673 |
if (sptr_cur >= readbuf_end) { |
| 3674 |
goto distance_d_write_1mibs_sq0_emitn_ret; |
| 3675 |
} |
| 3676 |
} |
| 3677 |
ulii = 1 + (((uintptr_t)(readbuf_end - sptr_cur)) / 2); |
| 3678 |
if (ulii < (sample_ct - sample2idx)) { |
| 3679 |
sptr_cur = memcpya(sptr_cur, membuf1, 2 * ulii); |
| 3680 |
sample2idx += ulii; |
| 3681 |
goto distance_d_write_1mibs_sq0_emitn_ret; |
| 3682 |
} |
| 3683 |
ulii = sample_ct - sample2idx; |
| 3684 |
sptr_cur = memcpyax(sptr_cur, membuf1, 2 * ulii - 1, '\n'); |
| 3685 |
sample1idx++; |
| 3686 |
if ((sample1idx - min_sample) * 100LLU >= ((uint64_t)pct) * (max_sample1idx - min_sample)) { |
| 3687 |
pct = ((sample1idx - min_sample) * 100LLU) / (max_sample1idx - min_sample); |
| 3688 |
printf("\rWriting... %u%%", pct++); |
| 3689 |
fflush(stdout); |
| 3690 |
} |
| 3691 |
sample2idx = 0; |
| 3692 |
} |
| 3693 |
distance_d_write_1mibs_sq0_emitn_ret: |
| 3694 |
g_dw_dist_ptr = dist_ptr; |
| 3695 |
g_dw_sample1idx = sample1idx; |
| 3696 |
g_dw_sample2idx = sample2idx; |
| 3697 |
g_pct = pct; |
| 3698 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 3699 |
} |
| 3700 |
|
| 3701 |
uint32_t distance_d_write_1mibs_sq_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 3702 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 3703 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 3704 |
double half_marker_ct_recip = g_dw_half_marker_ct_recip; |
| 3705 |
uintptr_t sample_ct = g_sample_ct; |
| 3706 |
uintptr_t max_sample1idx = g_dw_max_sample1idx; |
| 3707 |
uintptr_t min_sample = g_dw_min_sample; |
| 3708 |
double* dists = g_dw_dists; |
| 3709 |
double* dist_ptr = g_dw_dist_ptr; |
| 3710 |
uintptr_t sample1idx = g_dw_sample1idx; |
| 3711 |
uintptr_t sample2idx = g_dw_sample2idx; |
| 3712 |
uint32_t pct = g_pct; |
| 3713 |
while (sample1idx < max_sample1idx) { |
| 3714 |
while (sample2idx < sample1idx) { |
| 3715 |
sptr_cur = dtoa_gx((*dist_ptr++) * half_marker_ct_recip, '\t', sptr_cur); |
| 3716 |
sample2idx++; |
| 3717 |
if (sptr_cur >= readbuf_end) { |
| 3718 |
goto distance_d_write_1mibs_sq_emitn_ret; |
| 3719 |
} |
| 3720 |
} |
| 3721 |
if (sample2idx == sample1idx) { |
| 3722 |
*sptr_cur++ = '0'; |
| 3723 |
sample2idx++; |
| 3724 |
} |
| 3725 |
while (sample2idx < sample_ct) { |
| 3726 |
*sptr_cur++ = '\t'; |
| 3727 |
sptr_cur = dtoa_g((dists[((sample2idx * (sample2idx - 1)) / 2) + sample1idx]) * half_marker_ct_recip, sptr_cur); |
| 3728 |
sample2idx++; |
| 3729 |
if (sptr_cur >= readbuf_end) { |
| 3730 |
goto distance_d_write_1mibs_sq_emitn_ret; |
| 3731 |
} |
| 3732 |
} |
| 3733 |
*sptr_cur++ = '\n'; |
| 3734 |
sample1idx++; |
| 3735 |
if ((sample1idx - min_sample) * 100LLU >= ((uint64_t)pct) * (max_sample1idx - min_sample)) { |
| 3736 |
pct = ((sample1idx - min_sample) * 100LLU) / (max_sample1idx - min_sample); |
| 3737 |
printf("\rWriting... %u%%", pct++); |
| 3738 |
fflush(stdout); |
| 3739 |
} |
| 3740 |
sample2idx = 0; |
| 3741 |
} |
| 3742 |
distance_d_write_1mibs_sq_emitn_ret: |
| 3743 |
g_dw_dist_ptr = dist_ptr; |
| 3744 |
g_dw_sample1idx = sample1idx; |
| 3745 |
g_dw_sample2idx = sample2idx; |
| 3746 |
g_pct = pct; |
| 3747 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 3748 |
} |
| 3749 |
|
| 3750 |
int32_t distance_d_write(FILE** outfile_ptr, FILE** outfile2_ptr, FILE** outfile3_ptr, int32_t dist_calc_type, char* outname, char* outname_end, double* dists, double half_marker_ct_recip, uint32_t sample_ct, int32_t first_sample_idx, int32_t end_sample_idx, int32_t parallel_idx, int32_t parallel_tot, unsigned char* membuf) { |
| 3751 |
|
| 3752 |
printf("distance_d_write\n"); |
| 3753 |
uint32_t bin4 = dist_calc_type & DISTANCE_BIN4; |
| 3754 |
int32_t shape = dist_calc_type & DISTANCE_SHAPEMASK; |
| 3755 |
int32_t write_alcts = dist_calc_type & DISTANCE_ALCT; |
| 3756 |
int32_t write_ibs_matrix = dist_calc_type & DISTANCE_IBS; |
| 3757 |
int32_t write_1mibs_matrix = dist_calc_type & DISTANCE_1_MINUS_IBS; |
| 3758 |
int32_t retval = 0; |
| 3759 |
unsigned char overflow_buf[262144]; |
| 3760 |
double dxx; |
| 3761 |
double dyy; |
| 3762 |
double* dist_ptr; |
| 3763 |
uintptr_t* glptr; |
| 3764 |
uintptr_t sample_idx_ct; |
| 3765 |
uintptr_t ulii; |
| 3766 |
uintptr_t uljj; |
| 3767 |
float fxx; |
| 3768 |
float fyy; |
| 3769 |
uint32_t uii; |
| 3770 |
int32_t ii; |
| 3771 |
int32_t jj; |
| 3772 |
g_sample_ct = sample_ct; |
| 3773 |
g_dw_start_offset = ((int64_t)first_sample_idx * (first_sample_idx - 1)) / 2; |
| 3774 |
sample_idx_ct = (uintptr_t)(((int64_t)end_sample_idx * (end_sample_idx - 1)) / 2 - g_dw_start_offset); |
| 3775 |
g_dw_hundredth = 1 + (sample_idx_ct / 100); |
| 3776 |
if (first_sample_idx == 1) { |
| 3777 |
first_sample_idx = 0; |
| 3778 |
} |
| 3779 |
g_pct = 1; |
| 3780 |
if (dist_calc_type & (DISTANCE_BIN | DISTANCE_BIN4)) { |
| 3781 |
if (distance_open(outfile_ptr, outfile2_ptr, outfile3_ptr, outname, outname_end, ".bin", FOPEN_WB, dist_calc_type, parallel_idx, parallel_tot)) { |
| 3782 |
goto distance_d_write_ret_OPEN_FAIL; |
| 3783 |
} |
| 3784 |
if (shape == DISTANCE_TRI) { |
| 3785 |
if (write_alcts) { |
| 3786 |
fputs("Writing...", stdout); |
| 3787 |
fflush(stdout); |
| 3788 |
if (!bin4) { |
| 3789 |
if (fwrite_checked(dists, sample_idx_ct * sizeof(double), *outfile_ptr)) { |
| 3790 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3791 |
} |
| 3792 |
} else { |
| 3793 |
for (ulii = 0; ulii < sample_idx_ct; ulii++) { |
| 3794 |
fxx = dists[ulii]; |
| 3795 |
if (fwrite_checked(&fxx, sizeof(float), *outfile_ptr)) { |
| 3796 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3797 |
} |
| 3798 |
} |
| 3799 |
} |
| 3800 |
distance_print_done(0, outname, outname_end); |
| 3801 |
} |
| 3802 |
if (write_ibs_matrix) { |
| 3803 |
dist_ptr = dists; |
| 3804 |
ulii = 0; |
| 3805 |
do { |
| 3806 |
uljj = (((uint64_t)sample_idx_ct) * g_pct) / 100LL; |
| 3807 |
if (!bin4) { |
| 3808 |
for (; ulii < uljj; ulii++) { |
| 3809 |
dxx = 1.0 - (*dist_ptr++) * half_marker_ct_recip; |
| 3810 |
if (fwrite_checked(&dxx, sizeof(double), *outfile2_ptr)) { |
| 3811 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3812 |
} |
| 3813 |
} |
| 3814 |
} else { |
| 3815 |
for (; ulii < uljj; ulii++) { |
| 3816 |
fxx = (float)(1.0 - (*dist_ptr++) * half_marker_ct_recip); |
| 3817 |
if (fwrite_checked(&fxx, sizeof(float), *outfile2_ptr)) { |
| 3818 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3819 |
} |
| 3820 |
} |
| 3821 |
} |
| 3822 |
printf("\rWriting... %d%%", g_pct++); |
| 3823 |
fflush(stdout); |
| 3824 |
} while (g_pct <= 100); |
| 3825 |
distance_print_done(1, outname, outname_end); |
| 3826 |
} |
| 3827 |
if (write_1mibs_matrix) { |
| 3828 |
dist_ptr = dists; |
| 3829 |
ulii = 0; |
| 3830 |
do { |
| 3831 |
uljj = (((uint64_t)sample_idx_ct) * g_pct) / 100LL; |
| 3832 |
if (!bin4) { |
| 3833 |
for (; ulii < uljj; ulii++) { |
| 3834 |
dxx = (*dist_ptr++) * half_marker_ct_recip; |
| 3835 |
if (fwrite_checked(&dxx, sizeof(double), *outfile3_ptr)) { |
| 3836 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3837 |
} |
| 3838 |
} |
| 3839 |
} else { |
| 3840 |
for (; ulii < uljj; ulii++) { |
| 3841 |
fxx = (float)((*dist_ptr++) * half_marker_ct_recip); |
| 3842 |
if (fwrite_checked(&fxx, sizeof(float), *outfile3_ptr)) { |
| 3843 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3844 |
} |
| 3845 |
} |
| 3846 |
} |
| 3847 |
printf("\rWriting... %d%%", g_pct++); |
| 3848 |
fflush(stdout); |
| 3849 |
} while (g_pct <= 100); |
| 3850 |
distance_print_done(2, outname, outname_end); |
| 3851 |
} |
| 3852 |
} else if (!bin4) { |
| 3853 |
if (shape == DISTANCE_SQ0) { |
| 3854 |
fill_double_zero(sample_ct, (double*)membuf); |
| 3855 |
} |
| 3856 |
if (write_alcts) { |
| 3857 |
dxx = 0.0; |
| 3858 |
dist_ptr = dists; |
| 3859 |
for (ii = first_sample_idx; ii < end_sample_idx; ii++) { |
| 3860 |
if (fwrite_checked(dist_ptr, ii * sizeof(double), *outfile_ptr)) { |
| 3861 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3862 |
} |
| 3863 |
dist_ptr = &(dist_ptr[(uint32_t)ii]); |
| 3864 |
if (shape == DISTANCE_SQ0) { |
| 3865 |
if (fwrite_checked(membuf, (sample_ct - ii) * sizeof(double), *outfile_ptr)) { |
| 3866 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3867 |
} |
| 3868 |
} else { |
| 3869 |
|
| 3870 |
if (fwrite_checked(&dxx, sizeof(double), *outfile_ptr)) { |
| 3871 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3872 |
} |
| 3873 |
for (ulii = ii + 1; ulii < sample_ct; ulii++) { |
| 3874 |
if (fwrite_checked(&(dists[(ulii * (ulii - 1)) / 2 + ii]), sizeof(double), *outfile_ptr)) { |
| 3875 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3876 |
} |
| 3877 |
} |
| 3878 |
} |
| 3879 |
if ((ii - first_sample_idx) * 100LL >= (int64_t)g_pct * (end_sample_idx - first_sample_idx)) { |
| 3880 |
g_pct = ((ii - first_sample_idx) * 100LL) / (end_sample_idx - first_sample_idx); |
| 3881 |
printf("\rWriting... %d%%", g_pct++); |
| 3882 |
fflush(stdout); |
| 3883 |
} |
| 3884 |
} |
| 3885 |
if (fclose_null(outfile_ptr)) { |
| 3886 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3887 |
} |
| 3888 |
distance_print_done(0, outname, outname_end); |
| 3889 |
g_pct = 1; |
| 3890 |
} |
| 3891 |
if (write_ibs_matrix) { |
| 3892 |
dist_ptr = dists; |
| 3893 |
dyy = 1.0; |
| 3894 |
for (ii = first_sample_idx; ii < end_sample_idx; ii++) { |
| 3895 |
for (jj = 0; jj < ii; jj++) { |
| 3896 |
dxx = 1.0 - (*dist_ptr++) * half_marker_ct_recip; |
| 3897 |
if (fwrite_checked(&dxx, sizeof(double), *outfile2_ptr)) { |
| 3898 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3899 |
} |
| 3900 |
} |
| 3901 |
if (shape == DISTANCE_SQ0) { |
| 3902 |
if (fwrite_checked(membuf, (sample_ct - ii) * sizeof(double), *outfile2_ptr)) { |
| 3903 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3904 |
} |
| 3905 |
} else { |
| 3906 |
|
| 3907 |
if (fwrite_checked(&dyy, sizeof(double), *outfile2_ptr)) { |
| 3908 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3909 |
} |
| 3910 |
for (ulii = ii + 1; ulii < sample_ct; ulii++) { |
| 3911 |
dxx = 1.0 - dists[(ulii * (ulii - 1)) / 2 + ii] * half_marker_ct_recip; |
| 3912 |
if (fwrite_checked(&dxx, sizeof(double), *outfile2_ptr)) { |
| 3913 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3914 |
} |
| 3915 |
} |
| 3916 |
} |
| 3917 |
if ((ii - first_sample_idx) * 100LL >= (int64_t)g_pct * (end_sample_idx - first_sample_idx)) { |
| 3918 |
g_pct = ((ii - first_sample_idx) * 100LL) / (end_sample_idx - first_sample_idx); |
| 3919 |
printf("\rWriting... %d%%", g_pct++); |
| 3920 |
fflush(stdout); |
| 3921 |
} |
| 3922 |
} |
| 3923 |
if (fclose_null(outfile2_ptr)) { |
| 3924 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3925 |
} |
| 3926 |
distance_print_done(1, outname, outname_end); |
| 3927 |
g_pct = 1; |
| 3928 |
} |
| 3929 |
if (write_1mibs_matrix) { |
| 3930 |
dist_ptr = dists; |
| 3931 |
dyy = 0.0; |
| 3932 |
for (ii = first_sample_idx; ii < end_sample_idx; ii++) { |
| 3933 |
for (jj = 0; jj < ii; jj++) { |
| 3934 |
dxx = (*dist_ptr++) * half_marker_ct_recip; |
| 3935 |
if (fwrite_checked(&dxx, sizeof(double), *outfile3_ptr)) { |
| 3936 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3937 |
} |
| 3938 |
} |
| 3939 |
if (shape == DISTANCE_SQ0) { |
| 3940 |
if (fwrite_checked(membuf, (sample_ct - ii) * sizeof(double), *outfile3_ptr)) { |
| 3941 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3942 |
} |
| 3943 |
} else { |
| 3944 |
|
| 3945 |
if (fwrite_checked(&dyy, sizeof(double), *outfile3_ptr)) { |
| 3946 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3947 |
} |
| 3948 |
for (ulii = ii + 1; ulii < sample_ct; ulii++) { |
| 3949 |
dxx = dists[(ulii * (ulii - 1)) / 2 + ii] * half_marker_ct_recip; |
| 3950 |
if (fwrite_checked(&dxx, sizeof(double), *outfile3_ptr)) { |
| 3951 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3952 |
} |
| 3953 |
} |
| 3954 |
} |
| 3955 |
if ((ii - first_sample_idx) * 100LL >= (int64_t)g_pct * (end_sample_idx - first_sample_idx)) { |
| 3956 |
g_pct = ((ii - first_sample_idx) * 100LL) / (end_sample_idx - first_sample_idx); |
| 3957 |
printf("\rWriting... %d%%", g_pct++); |
| 3958 |
fflush(stdout); |
| 3959 |
} |
| 3960 |
} |
| 3961 |
if (fclose_null(outfile3_ptr)) { |
| 3962 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3963 |
} |
| 3964 |
distance_print_done(2, outname, outname_end); |
| 3965 |
} |
| 3966 |
} else { |
| 3967 |
if (shape == DISTANCE_SQ0) { |
| 3968 |
fill_float_zero(sample_ct, (float*)membuf); |
| 3969 |
} |
| 3970 |
if (write_alcts) { |
| 3971 |
fxx = 0.0; |
| 3972 |
dist_ptr = dists; |
| 3973 |
for (ii = first_sample_idx; ii < end_sample_idx; ii++) { |
| 3974 |
for (uii = 0; uii < (uint32_t)ii; uii++) { |
| 3975 |
fxx = (float)dist_ptr[uii]; |
| 3976 |
fwrite(&fxx, 4, 1, *outfile_ptr); |
| 3977 |
} |
| 3978 |
dist_ptr = &(dist_ptr[(uint32_t)ii]); |
| 3979 |
if (shape == DISTANCE_SQ0) { |
| 3980 |
if (fwrite_checked(membuf, (sample_ct - ii) * sizeof(float), *outfile_ptr)) { |
| 3981 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3982 |
} |
| 3983 |
} else { |
| 3984 |
|
| 3985 |
if (fwrite_checked(&fxx, sizeof(float), *outfile_ptr)) { |
| 3986 |
goto distance_d_write_ret_WRITE_FAIL; |
| 3987 |
} |
| 3988 |
for (ulii = ii + 1; ulii < sample_ct; ulii++) { |
| 3989 |
fyy = (float)dists[(ulii * (ulii - 1)) / 2 + ii]; |
| 3990 |
fwrite(&fyy, 4, 1, *outfile_ptr); |
| 3991 |
} |
| 3992 |
} |
| 3993 |
if ((ii - first_sample_idx) * 100LL >= (int64_t)g_pct * (end_sample_idx - first_sample_idx)) { |
| 3994 |
g_pct = ((ii - first_sample_idx) * 100LL) / (end_sample_idx - first_sample_idx); |
| 3995 |
printf("\rWriting... %d%%", g_pct++); |
| 3996 |
fflush(stdout); |
| 3997 |
} |
| 3998 |
} |
| 3999 |
if (fclose_null(outfile_ptr)) { |
| 4000 |
goto distance_d_write_ret_WRITE_FAIL; |
| 4001 |
} |
| 4002 |
distance_print_done(0, outname, outname_end); |
| 4003 |
g_pct = 1; |
| 4004 |
} |
| 4005 |
if (write_ibs_matrix) { |
| 4006 |
dist_ptr = dists; |
| 4007 |
fyy = 1.0; |
| 4008 |
for (ii = first_sample_idx; ii < end_sample_idx; ii++) { |
| 4009 |
for (jj = 0; jj < ii; jj++) { |
| 4010 |
fxx = (float)(1.0 - (*dist_ptr++) * half_marker_ct_recip); |
| 4011 |
if (fwrite_checked(&fxx, sizeof(float), *outfile2_ptr)) { |
| 4012 |
goto distance_d_write_ret_WRITE_FAIL; |
| 4013 |
} |
| 4014 |
} |
| 4015 |
if (shape == DISTANCE_SQ0) { |
| 4016 |
if (fwrite_checked(membuf, (sample_ct - ii) * sizeof(float), *outfile2_ptr)) { |
| 4017 |
goto distance_d_write_ret_WRITE_FAIL; |
| 4018 |
} |
| 4019 |
} else { |
| 4020 |
|
| 4021 |
if (fwrite_checked(&fyy, sizeof(float), *outfile2_ptr)) { |
| 4022 |
goto distance_d_write_ret_WRITE_FAIL; |
| 4023 |
} |
| 4024 |
for (ulii = ii + 1; ulii < sample_ct; ulii++) { |
| 4025 |
fxx = (float)(1.0 - dists[(ulii * (ulii - 1)) / 2 + ii] * half_marker_ct_recip); |
| 4026 |
fwrite(&fxx, 4, 1, *outfile2_ptr); |
| 4027 |
} |
| 4028 |
} |
| 4029 |
if ((ii - first_sample_idx) * 100LL >= (int64_t)g_pct * (end_sample_idx - first_sample_idx)) { |
| 4030 |
g_pct = ((ii - first_sample_idx) * 100LL) / (end_sample_idx - first_sample_idx); |
| 4031 |
printf("\rWriting... %d%%", g_pct++); |
| 4032 |
fflush(stdout); |
| 4033 |
} |
| 4034 |
} |
| 4035 |
if (fclose_null(outfile2_ptr)) { |
| 4036 |
goto distance_d_write_ret_WRITE_FAIL; |
| 4037 |
} |
| 4038 |
distance_print_done(1, outname, outname_end); |
| 4039 |
g_pct = 1; |
| 4040 |
} |
| 4041 |
if (write_1mibs_matrix) { |
| 4042 |
dist_ptr = dists; |
| 4043 |
fyy = 0.0; |
| 4044 |
for (ii = first_sample_idx; ii < end_sample_idx; ii++) { |
| 4045 |
for (jj = 0; jj < ii; jj++) { |
| 4046 |
fxx = (float)((*dist_ptr++) * half_marker_ct_recip); |
| 4047 |
if (fwrite_checked(&fxx, sizeof(float), *outfile3_ptr)) { |
| 4048 |
goto distance_d_write_ret_WRITE_FAIL; |
| 4049 |
} |
| 4050 |
} |
| 4051 |
if (shape == DISTANCE_SQ0) { |
| 4052 |
if (fwrite_checked(membuf, (sample_ct - ii) * sizeof(float), *outfile3_ptr)) { |
| 4053 |
goto distance_d_write_ret_WRITE_FAIL; |
| 4054 |
} |
| 4055 |
} else { |
| 4056 |
|
| 4057 |
if (fwrite_checked(&fyy, sizeof(float), *outfile3_ptr)) { |
| 4058 |
goto distance_d_write_ret_WRITE_FAIL; |
| 4059 |
} |
| 4060 |
for (ulii = ii + 1; ulii < sample_ct; ulii++) { |
| 4061 |
fxx = (float)(dists[(ulii * (ulii - 1)) / 2 + ii] * half_marker_ct_recip); |
| 4062 |
fwrite(&fxx, 4, 1, *outfile3_ptr); |
| 4063 |
} |
| 4064 |
} |
| 4065 |
if ((ii - first_sample_idx) * 100LL >= (int64_t)g_pct * (end_sample_idx - first_sample_idx)) { |
| 4066 |
g_pct = ((ii - first_sample_idx) * 100LL) / (end_sample_idx - first_sample_idx); |
| 4067 |
printf("\rWriting... %d%%", g_pct++); |
| 4068 |
fflush(stdout); |
| 4069 |
} |
| 4070 |
} |
| 4071 |
if (fclose_null(outfile3_ptr)) { |
| 4072 |
goto distance_d_write_ret_WRITE_FAIL; |
| 4073 |
} |
| 4074 |
distance_print_done(2, outname, outname_end); |
| 4075 |
} |
| 4076 |
} |
| 4077 |
} else { |
| 4078 |
if (shape == DISTANCE_SQ0) { |
| 4079 |
|
| 4080 |
#ifdef __LP64__ |
| 4081 |
ulii = 0x3009300930093009LLU; |
| 4082 |
#else |
| 4083 |
ulii = 0x30093009; |
| 4084 |
#endif |
| 4085 |
ii = (sample_ct * 2 + sizeof(intptr_t) - 2) / sizeof(intptr_t); |
| 4086 |
glptr = (uintptr_t*)membuf; |
| 4087 |
for (jj = 0; jj < ii; jj++) { |
| 4088 |
*glptr++ = ulii; |
| 4089 |
} |
| 4090 |
g_dw_membuf = membuf; |
| 4091 |
} |
| 4092 |
g_dw_min_sample = first_sample_idx; |
| 4093 |
g_dw_max_sample1idx = end_sample_idx; |
| 4094 |
g_dw_dists = dists; |
| 4095 |
g_dw_half_marker_ct_recip = half_marker_ct_recip; |
| 4096 |
if (write_alcts) { |
| 4097 |
g_dw_sample1idx = first_sample_idx; |
| 4098 |
g_dw_sample2idx = 0; |
| 4099 |
g_dw_dist_ptr = dists; |
| 4100 |
if (dist_calc_type & DISTANCE_GZ) { |
| 4101 |
if (parallel_tot > 1) { |
| 4102 |
snprintf(outname_end, MAX_POST_EXT, ".dist.%u.gz", parallel_idx + 1); |
| 4103 |
} else { |
| 4104 |
snprintf(outname_end, MAX_POST_EXT, ".dist.gz"); |
| 4105 |
} |
| 4106 |
if (shape == DISTANCE_SQ) { |
| 4107 |
parallel_compress(outname, overflow_buf, 0, distance_d_write_sq_emitn); |
| 4108 |
} else if (shape == DISTANCE_SQ0) { |
| 4109 |
parallel_compress(outname, overflow_buf, 0, distance_d_write_sq0_emitn); |
| 4110 |
} else { |
| 4111 |
parallel_compress(outname, overflow_buf, 0, distance_d_write_tri_emitn); |
| 4112 |
} |
| 4113 |
} else { |
| 4114 |
if (parallel_tot > 1) { |
| 4115 |
snprintf(outname_end, MAX_POST_EXT, ".dist.%u", parallel_idx + 1); |
| 4116 |
} else { |
| 4117 |
snprintf(outname_end, MAX_POST_EXT, ".dist"); |
| 4118 |
} |
| 4119 |
if (shape == DISTANCE_SQ) { |
| 4120 |
retval = write_uncompressed(outname, overflow_buf, 0, distance_d_write_sq_emitn); |
| 4121 |
} else if (shape == DISTANCE_SQ0) { |
| 4122 |
retval = write_uncompressed(outname, overflow_buf, 0, distance_d_write_sq0_emitn); |
| 4123 |
} else { |
| 4124 |
retval = write_uncompressed(outname, overflow_buf, 0, distance_d_write_tri_emitn); |
| 4125 |
} |
| 4126 |
if (retval) { |
| 4127 |
goto distance_d_write_ret_1; |
| 4128 |
} |
| 4129 |
} |
| 4130 |
putc_unlocked('\r', stdout); |
| 4131 |
LOGPRINTFWW("Distances (allele counts) written to %s .\n", outname); |
| 4132 |
g_pct = 1; |
| 4133 |
} |
| 4134 |
if (write_1mibs_matrix) { |
| 4135 |
g_dw_sample1idx = first_sample_idx; |
| 4136 |
g_dw_sample2idx = 0; |
| 4137 |
g_dw_dist_ptr = dists; |
| 4138 |
if (dist_calc_type & DISTANCE_GZ) { |
| 4139 |
if (parallel_tot > 1) { |
| 4140 |
snprintf(outname_end, MAX_POST_EXT, ".mdist.%u.gz", parallel_idx + 1); |
| 4141 |
} else { |
| 4142 |
snprintf(outname_end, MAX_POST_EXT, ".mdist.gz"); |
| 4143 |
} |
| 4144 |
if (shape == DISTANCE_SQ) { |
| 4145 |
parallel_compress(outname, overflow_buf, 0, distance_d_write_1mibs_sq_emitn); |
| 4146 |
} else if (shape == DISTANCE_SQ0) { |
| 4147 |
parallel_compress(outname, overflow_buf, 0, distance_d_write_1mibs_sq0_emitn); |
| 4148 |
} else { |
| 4149 |
parallel_compress(outname, overflow_buf, 0, distance_d_write_1mibs_tri_emitn); |
| 4150 |
} |
| 4151 |
} else { |
| 4152 |
if (parallel_tot > 1) { |
| 4153 |
snprintf(outname_end, MAX_POST_EXT, ".mdist.%u", parallel_idx + 1); |
| 4154 |
} else { |
| 4155 |
snprintf(outname_end, MAX_POST_EXT, ".mdist"); |
| 4156 |
} |
| 4157 |
if (shape == DISTANCE_SQ) { |
| 4158 |
retval = write_uncompressed(outname, overflow_buf, 0, distance_d_write_1mibs_sq_emitn); |
| 4159 |
} else if (shape == DISTANCE_SQ0) { |
| 4160 |
retval = write_uncompressed(outname, overflow_buf, 0, distance_d_write_1mibs_sq0_emitn); |
| 4161 |
} else { |
| 4162 |
retval = write_uncompressed(outname, overflow_buf, 0, distance_d_write_1mibs_tri_emitn); |
| 4163 |
} |
| 4164 |
if (retval) { |
| 4165 |
goto distance_d_write_ret_1; |
| 4166 |
} |
| 4167 |
} |
| 4168 |
putc_unlocked('\r', stdout); |
| 4169 |
LOGPRINTFWW("Distances (proportions) written to %s .\n", outname); |
| 4170 |
g_pct = 1; |
| 4171 |
} |
| 4172 |
if (write_ibs_matrix) { |
| 4173 |
g_dw_sample1idx = first_sample_idx; |
| 4174 |
g_dw_sample2idx = 0; |
| 4175 |
g_dw_dist_ptr = dists; |
| 4176 |
g_dw_start_offset = ((int64_t)first_sample_idx * (first_sample_idx + 1)) / 2; |
| 4177 |
g_dw_hundredth = 1 + ((((int64_t)end_sample_idx * (end_sample_idx + 1)) / 2 - g_dw_start_offset) / 100); |
| 4178 |
if (dist_calc_type & DISTANCE_GZ) { |
| 4179 |
if (parallel_tot > 1) { |
| 4180 |
snprintf(outname_end, MAX_POST_EXT, ".mibs.%u.gz", parallel_idx + 1); |
| 4181 |
} else { |
| 4182 |
snprintf(outname_end, MAX_POST_EXT, ".mibs.gz"); |
| 4183 |
} |
| 4184 |
if (shape == DISTANCE_SQ) { |
| 4185 |
parallel_compress(outname, overflow_buf, 0, distance_d_write_ibs_sq_emitn); |
| 4186 |
} else if (shape == DISTANCE_SQ0) { |
| 4187 |
parallel_compress(outname, overflow_buf, 0, distance_d_write_ibs_sq0_emitn); |
| 4188 |
} else { |
| 4189 |
parallel_compress(outname, overflow_buf, 0, distance_d_write_ibs_tri_emitn); |
| 4190 |
} |
| 4191 |
} else { |
| 4192 |
if (parallel_tot > 1) { |
| 4193 |
snprintf(outname_end, MAX_POST_EXT, ".mibs.%u", parallel_idx + 1); |
| 4194 |
} else { |
| 4195 |
snprintf(outname_end, MAX_POST_EXT, ".mibs"); |
| 4196 |
} |
| 4197 |
if (shape == DISTANCE_SQ) { |
| 4198 |
retval = write_uncompressed(outname, overflow_buf, 0, distance_d_write_ibs_sq_emitn); |
| 4199 |
} else if (shape == DISTANCE_SQ0) { |
| 4200 |
retval = write_uncompressed(outname, overflow_buf, 0, distance_d_write_ibs_sq0_emitn); |
| 4201 |
} else { |
| 4202 |
retval = write_uncompressed(outname, overflow_buf, 0, distance_d_write_ibs_tri_emitn); |
| 4203 |
} |
| 4204 |
if (retval) { |
| 4205 |
goto distance_d_write_ret_1; |
| 4206 |
} |
| 4207 |
} |
| 4208 |
putc_unlocked('\r', stdout); |
| 4209 |
LOGPRINTFWW("IBS matrix written to %s .\n", outname); |
| 4210 |
} |
| 4211 |
} |
| 4212 |
while (0) { |
| 4213 |
distance_d_write_ret_OPEN_FAIL: |
| 4214 |
retval = RET_OPEN_FAIL; |
| 4215 |
break; |
| 4216 |
distance_d_write_ret_WRITE_FAIL: |
| 4217 |
retval = RET_WRITE_FAIL; |
| 4218 |
break; |
| 4219 |
} |
| 4220 |
distance_d_write_ret_1: |
| 4221 |
return retval; |
| 4222 |
} |
| 4223 |
|
| 4224 |
uint32_t calc_genome_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 4225 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 4226 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 4227 |
uintptr_t* sample_exclude = g_cg_sample_exclude; |
| 4228 |
uint32_t* sample_missing_unwt = g_sample_missing_unwt; |
| 4229 |
uint32_t* missing_dbl_excluded = g_missing_dbl_excluded; |
| 4230 |
uint32_t* genome_main = g_genome_main; |
| 4231 |
uintptr_t sample_ct = g_sample_ct; |
| 4232 |
uint32_t is_rel_check = g_cg_genome_modifier & GENOME_REL_CHECK; |
| 4233 |
uint32_t is_unbounded = g_cg_genome_modifier & GENOME_IBD_UNBOUNDED; |
| 4234 |
uint32_t is_nudge = g_cg_genome_modifier & GENOME_NUDGE; |
| 4235 |
uint32_t filter_pi_hat = g_cg_genome_modifier & GENOME_FILTER_PI_HAT; |
| 4236 |
uint32_t output_full = g_cg_genome_modifier & GENOME_OUTPUT_FULL; |
| 4237 |
uintptr_t sample1idx = g_cg_sample1idx; |
| 4238 |
uintptr_t sample2idx = g_cg_sample2idx; |
| 4239 |
uintptr_t sample1uidx = g_cg_sample1uidx; |
| 4240 |
uintptr_t sample2uidx = g_cg_sample2uidx; |
| 4241 |
uintptr_t tstc = g_cg_tstc; |
| 4242 |
uint32_t marker_ct = g_cg_marker_ct; |
| 4243 |
uintptr_t max_sample_id_len = g_cg_max_sample_id_len; |
| 4244 |
uint32_t max_sample_fid_len = g_cg_max_sample_fid_len; |
| 4245 |
uint32_t max_sample_iid_len = g_cg_max_sample_iid_len; |
| 4246 |
uintptr_t max_paternal_id_len = g_cg_max_paternal_id_len; |
| 4247 |
uintptr_t max_maternal_id_len = g_cg_max_maternal_id_len; |
| 4248 |
uintptr_t* pheno_c = g_cg_pheno_c; |
| 4249 |
uintptr_t* founder_info = g_cg_founder_info; |
| 4250 |
char* sample_ids = g_cg_sample_ids; |
| 4251 |
char* paternal_ids = g_cg_paternal_ids; |
| 4252 |
char* maternal_ids = g_cg_maternal_ids; |
| 4253 |
uint32_t family_id_fixed = g_cg_family_id_fixed; |
| 4254 |
uint32_t is_founder_fixed = g_cg_is_founder_fixed; |
| 4255 |
uint32_t rel_space_id_fixed = g_cg_rel_space_id_fixed; |
| 4256 |
int64_t llfct = g_cg_llfct; |
| 4257 |
char* sptr_start = g_cg_sptr_start; |
| 4258 |
char* fam1 = g_cg_fam1; |
| 4259 |
char* fam2 = g_cg_fam2; |
| 4260 |
char* sample1 = g_cg_sample1; |
| 4261 |
char* pat1 = g_cg_pat1; |
| 4262 |
char* mat1 = g_cg_mat1; |
| 4263 |
int64_t cur_line = g_cg_cur_line; |
| 4264 |
int64_t tot_lines = g_cg_tot_lines; |
| 4265 |
uint32_t pct = g_pct; |
| 4266 |
uintptr_t gmcell = g_cg_gmcell; |
| 4267 |
uintptr_t mdecell = g_cg_mdecell; |
| 4268 |
double e00 = g_cg_e00; |
| 4269 |
double e01 = g_cg_e01; |
| 4270 |
double e02 = g_cg_e02; |
| 4271 |
double e11 = g_cg_e11; |
| 4272 |
double e12 = g_cg_e12; |
| 4273 |
char* pat2; |
| 4274 |
char* mat2; |
| 4275 |
char* cptr; |
| 4276 |
char* sample2; |
| 4277 |
char* sptr_cur_start; |
| 4278 |
uint32_t founder_ct; |
| 4279 |
uint32_t uii; |
| 4280 |
uint32_t ujj; |
| 4281 |
uint32_t ukk; |
| 4282 |
uint32_t umm; |
| 4283 |
int32_t nn; |
| 4284 |
int32_t oo; |
| 4285 |
double dxx; |
| 4286 |
double dyy; |
| 4287 |
double dxx1; |
| 4288 |
double dxx2; |
| 4289 |
if (!sample2idx) { |
| 4290 |
|
| 4291 |
if (!sample1idx) { |
| 4292 |
sptr_cur += sprintf(sptr_cur, g_textbuf, " FID1", " IID1", " FID2", " IID2"); |
| 4293 |
} |
| 4294 |
sample1idx = g_thread_start[0]; |
| 4295 |
sample2idx = sample1idx + 1; |
| 4296 |
g_textbuf[0] = ' '; |
| 4297 |
} |
| 4298 |
while (sample1idx < tstc) { |
| 4299 |
if (sample2idx == sample1idx + 1) { |
| 4300 |
cptr = &(sample_ids[sample1uidx * max_sample_id_len]); |
| 4301 |
uii = strlen_se(cptr); |
| 4302 |
memcpyx(fam1, cptr, uii, '\0'); |
| 4303 |
sample1 = next_token(cptr); |
| 4304 |
pat1 = &(paternal_ids[sample1uidx * max_paternal_id_len]); |
| 4305 |
mat1 = &(maternal_ids[sample1uidx * max_maternal_id_len]); |
| 4306 |
is_founder_fixed = IS_SET(founder_info, sample1uidx); |
| 4307 |
rel_space_id_fixed = g_cg_pri.family_rel_nf_idxs[sample1uidx]; |
| 4308 |
family_id_fixed = g_cg_pri.family_idxs[sample1uidx]; |
| 4309 |
founder_ct = g_cg_pri.family_founder_cts[family_id_fixed]; |
| 4310 |
llfct = ((int64_t)founder_ct * (founder_ct - 1)) - 2 * g_cg_pri.family_rel_space_offsets[family_id_fixed]; |
| 4311 |
sptr_start = fw_strcpyn(max_sample_fid_len - 1, uii, fam1, &(g_textbuf[1])); |
| 4312 |
*sptr_start++ = ' '; |
| 4313 |
sptr_start = fw_strcpy(max_sample_iid_len - 1, sample1, sptr_start); |
| 4314 |
*sptr_start++ = ' '; |
| 4315 |
} |
| 4316 |
while (sample2idx < sample_ct) { |
| 4317 |
next_unset_ul_unsafe_ck(sample_exclude, &sample2uidx); |
| 4318 |
sptr_cur_start = sptr_cur; |
| 4319 |
sptr_cur = memcpya(sptr_cur, g_textbuf, sptr_start - g_textbuf); |
| 4320 |
cptr = &(sample_ids[sample2uidx * max_sample_id_len]); |
| 4321 |
uii = strlen_se(cptr); |
| 4322 |
memcpyx(fam2, cptr, uii, '\0'); |
| 4323 |
sample2 = skip_initial_spaces(&(cptr[uii + 1])); |
| 4324 |
pat2 = &(paternal_ids[sample2uidx * max_paternal_id_len]); |
| 4325 |
mat2 = &(maternal_ids[sample2uidx * max_maternal_id_len]); |
| 4326 |
sptr_cur = fw_strcpyn(max_sample_fid_len - 1, uii, fam2, sptr_cur); |
| 4327 |
*sptr_cur++ = ' '; |
| 4328 |
sptr_cur = fw_strcpy(max_sample_iid_len - 1, sample2, sptr_cur); |
| 4329 |
*sptr_cur++ = ' '; |
| 4330 |
if (!strcmp(fam1, fam2)) { |
| 4331 |
|
| 4332 |
|
| 4333 |
|
| 4334 |
|
| 4335 |
while (1) { |
| 4336 |
if (!(is_founder_fixed || IS_SET(founder_info, sample2uidx))) { |
| 4337 |
if ((!strcmp(pat1, pat2)) && (!strcmp(mat1, mat2))) { |
| 4338 |
sptr_cur = memcpyl3a(sptr_cur, "FS "); |
| 4339 |
break; |
| 4340 |
} else if ((!strcmp(pat1, pat2)) || (!strcmp(mat1, mat2))) { |
| 4341 |
sptr_cur = memcpyl3a(sptr_cur, "HS "); |
| 4342 |
break; |
| 4343 |
} |
| 4344 |
} |
| 4345 |
if ((!strcmp(pat1, sample2)) || (!strcmp(mat1, sample2)) || (!strcmp(pat2, sample1)) || (!strcmp(mat2, sample1))) { |
| 4346 |
sptr_cur = memcpyl3a(sptr_cur, "PO "); |
| 4347 |
break; |
| 4348 |
} |
| 4349 |
sptr_cur = memcpyl3a(sptr_cur, "OT "); |
| 4350 |
break; |
| 4351 |
} |
| 4352 |
|
| 4353 |
|
| 4354 |
oo = IS_SET(founder_info, sample2uidx); |
| 4355 |
if (is_founder_fixed && oo) { |
| 4356 |
dxx = 0.0; |
| 4357 |
} else { |
| 4358 |
|
| 4359 |
nn = g_cg_pri.family_rel_nf_idxs[sample2uidx]; |
| 4360 |
if (is_founder_fixed || ((nn > (int32_t)rel_space_id_fixed) && (!oo))) { |
| 4361 |
dxx = g_cg_pri.rel_space[rel_space_id_fixed + ((int64_t)nn * (nn - 1) - llfct) / 2]; |
| 4362 |
} else { |
| 4363 |
dxx = g_cg_pri.rel_space[nn + ((int64_t)rel_space_id_fixed * (rel_space_id_fixed - 1) - llfct) / 2]; |
| 4364 |
} |
| 4365 |
} |
| 4366 |
sptr_cur = width_force(5, sptr_cur, dtoa_g(dxx, sptr_cur)); |
| 4367 |
} else if (!is_rel_check) { |
| 4368 |
sptr_cur = memcpya(sptr_cur, "UN NA", 8); |
| 4369 |
} else { |
| 4370 |
sptr_cur = sptr_cur_start; |
| 4371 |
goto calc_genome_emitn_skip_line; |
| 4372 |
} |
| 4373 |
nn = marker_ct - sample_missing_unwt[sample1idx] - sample_missing_unwt[sample2idx] + missing_dbl_excluded[mdecell]; |
| 4374 |
oo = nn - genome_main[gmcell] - genome_main[gmcell + 1]; |
| 4375 |
dxx = ((double)((int32_t)genome_main[gmcell + 1])) / (e00 * nn); |
| 4376 |
dyy = (((double)((int32_t)genome_main[gmcell])) - dxx * e01 * nn) / (e11 * nn); |
| 4377 |
dxx1 = ((double)oo - nn * (dxx * e02 + dyy * e12)) / ((double)nn); |
| 4378 |
if (!is_unbounded) { |
| 4379 |
if (dxx > 1) { |
| 4380 |
dxx = 1; |
| 4381 |
dyy = 0; |
| 4382 |
dxx1 = 0; |
| 4383 |
} else if (dyy > 1) { |
| 4384 |
dyy = 1; |
| 4385 |
dxx = 0; |
| 4386 |
dxx1 = 0; |
| 4387 |
} else if (dxx1 > 1) { |
| 4388 |
dxx1 = 1; |
| 4389 |
dyy = 0; |
| 4390 |
dxx = 0; |
| 4391 |
} else if (dxx < 0) { |
| 4392 |
dxx2 = 1.0 / (dyy + dxx1); |
| 4393 |
dyy *= dxx2; |
| 4394 |
dxx1 *= dxx2; |
| 4395 |
dxx = 0; |
| 4396 |
} |
| 4397 |
if (dyy < 0) { |
| 4398 |
dxx2 = 1.0 / (dxx + dxx1); |
| 4399 |
dxx *= dxx2; |
| 4400 |
dxx1 *= dxx2; |
| 4401 |
dyy = 0; |
| 4402 |
} |
| 4403 |
if (dxx1 < 0) { |
| 4404 |
dxx2 = 1.0 / (dxx + dyy); |
| 4405 |
dxx *= dxx2; |
| 4406 |
dyy *= dxx2; |
| 4407 |
dxx1 = 0; |
| 4408 |
} |
| 4409 |
} |
| 4410 |
dxx2 = dyy * 0.5 + dxx1; |
| 4411 |
if (filter_pi_hat && ((dxx2 < g_cg_min_pi_hat) || (dxx2 > g_cg_max_pi_hat))) { |
| 4412 |
sptr_cur = sptr_cur_start; |
| 4413 |
goto calc_genome_emitn_skip_line; |
| 4414 |
} |
| 4415 |
if (is_nudge && (dxx2 * dxx2 < dxx1)) { |
| 4416 |
dxx = (1 - dxx2) * (1 - dxx2); |
| 4417 |
dyy = 2 * dxx2 * (1 - dxx2); |
| 4418 |
dxx1 = dxx2 * dxx2; |
| 4419 |
} |
| 4420 |
*sptr_cur++ = ' '; |
| 4421 |
sptr_cur = dtoa_f_w7p4x(dxx, ' ', sptr_cur); |
| 4422 |
sptr_cur = dtoa_f_w7p4x(dyy, ' ', sptr_cur); |
| 4423 |
sptr_cur = dtoa_f_w7p4x(dxx1, ' ', sptr_cur); |
| 4424 |
sptr_cur = dtoa_f_w7p4(dxx2, sptr_cur); |
| 4425 |
|
| 4426 |
if (pheno_c) { |
| 4427 |
uii = IS_SET(g_cg_pheno_nm, sample1uidx); |
| 4428 |
ujj = IS_SET(g_cg_pheno_nm, sample2uidx); |
| 4429 |
ukk = IS_SET(pheno_c, sample1uidx); |
| 4430 |
umm = IS_SET(pheno_c, sample2uidx); |
| 4431 |
if (((!uii) || (!ukk)) && ((!ujj) || (!umm))) { |
| 4432 |
memcpy(sptr_cur, " -1 ", 5); |
| 4433 |
} else if (uii && ujj && ukk && umm) { |
| 4434 |
memcpy(sptr_cur, " 1 ", 5); |
| 4435 |
} else { |
| 4436 |
memcpy(sptr_cur, " 0 ", 5); |
| 4437 |
} |
| 4438 |
} else { |
| 4439 |
memcpy(sptr_cur, " NA ", 5); |
| 4440 |
} |
| 4441 |
sptr_cur += 5; |
| 4442 |
dxx = (double)genome_main[gmcell + 4]; |
| 4443 |
dyy = (double)genome_main[gmcell + 3]; |
| 4444 |
dxx1 = 1.0 / ((double)(genome_main[gmcell + 4] + genome_main[gmcell + 3])); |
| 4445 |
dxx2 = normdist((dxx * dxx1 - 0.666666) / (sqrt(0.2222222 * dxx1))); |
| 4446 |
sptr_cur = dtoa_f_w9p6x(1.0 - (genome_main[gmcell] + 2 * genome_main[gmcell + 1]) / ((double)(2 * nn)), ' ', sptr_cur); |
| 4447 |
if (dxx2 != dxx2) { |
| 4448 |
sptr_cur = memcpya(sptr_cur, " NA ", 8); |
| 4449 |
} else { |
| 4450 |
sptr_cur = dtoa_f_w7p4x(dxx2, ' ', sptr_cur); |
| 4451 |
} |
| 4452 |
if (genome_main[gmcell + 3]) { |
| 4453 |
sptr_cur = dtoa_f_w7p4(dxx / dyy, sptr_cur); |
| 4454 |
} else { |
| 4455 |
sptr_cur = memcpya(sptr_cur, " NA", 7); |
| 4456 |
} |
| 4457 |
if (output_full) { |
| 4458 |
*sptr_cur++ = ' '; |
| 4459 |
sptr_cur = uint32toa_w7x(genome_main[gmcell + 1], ' ', sptr_cur); |
| 4460 |
sptr_cur = uint32toa_w7x(genome_main[gmcell], ' ', sptr_cur); |
| 4461 |
sptr_cur = uint32toa_w7x(oo, ' ', sptr_cur); |
| 4462 |
sptr_cur = dtoa_f_w7p4x(dyy, ' ', sptr_cur); |
| 4463 |
sptr_cur = dtoa_f_w7p4(dxx, sptr_cur); |
| 4464 |
} |
| 4465 |
*sptr_cur++ = '\n'; |
| 4466 |
calc_genome_emitn_skip_line: |
| 4467 |
gmcell += 5; |
| 4468 |
mdecell++; |
| 4469 |
sample2idx++; |
| 4470 |
sample2uidx++; |
| 4471 |
if (sptr_cur >= readbuf_end) { |
| 4472 |
goto calc_genome_emitn_ret; |
| 4473 |
} |
| 4474 |
} |
| 4475 |
cur_line += sample_ct - sample1idx - 1; |
| 4476 |
sample1idx++; |
| 4477 |
sample1uidx++; |
| 4478 |
next_unset_ul_unsafe_ck(sample_exclude, &sample1uidx); |
| 4479 |
sample2idx = sample1idx + 1; |
| 4480 |
sample2uidx = sample1uidx + 1; |
| 4481 |
} |
| 4482 |
calc_genome_emitn_ret: |
| 4483 |
if (cur_line * 100 >= tot_lines * pct) { |
| 4484 |
pct = (cur_line * 100) / tot_lines; |
| 4485 |
printf("\rWriting... %u%%", pct++); |
| 4486 |
fflush(stdout); |
| 4487 |
} |
| 4488 |
g_cg_sample1idx = sample1idx; |
| 4489 |
g_cg_sample2idx = sample2idx; |
| 4490 |
g_cg_sample1uidx = sample1uidx; |
| 4491 |
g_cg_sample2uidx = sample2uidx; |
| 4492 |
g_cg_family_id_fixed = family_id_fixed; |
| 4493 |
g_cg_is_founder_fixed = is_founder_fixed; |
| 4494 |
g_cg_rel_space_id_fixed = rel_space_id_fixed; |
| 4495 |
g_cg_llfct = llfct; |
| 4496 |
g_cg_sptr_start = sptr_start; |
| 4497 |
g_cg_sample1 = sample1; |
| 4498 |
g_cg_pat1 = pat1; |
| 4499 |
g_cg_mat1 = mat1; |
| 4500 |
g_cg_cur_line = cur_line; |
| 4501 |
g_cg_tot_lines = tot_lines; |
| 4502 |
g_pct = pct; |
| 4503 |
g_cg_gmcell = gmcell; |
| 4504 |
g_cg_mdecell = mdecell; |
| 4505 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 4506 |
} |
| 4507 |
|
| 4508 |
int32_t calc_genome(pthread_t* threads, FILE* bedfile, uintptr_t bed_offset, uint32_t marker_ct, uintptr_t unfiltered_marker_ct, uintptr_t* marker_exclude, Chrom_info* chrom_info_ptr, uint32_t* marker_pos, double* set_allele_freqs, uint32_t* nchrobs, uintptr_t unfiltered_sample_ct, uintptr_t* sample_exclude, uintptr_t sample_ct, char* sample_ids, uint32_t plink_maxfid, uint32_t plink_maxiid, uintptr_t max_sample_id_len, char* paternal_ids, uintptr_t max_paternal_id_len, char* maternal_ids, uintptr_t max_maternal_id_len, uintptr_t* founder_info, uint32_t parallel_idx, uint32_t parallel_tot, char* outname, char* outname_end, int32_t nonfounders, uint64_t calculation_type, uint32_t genome_modifier, uint32_t ppc_gap, double min_pi_hat, double max_pi_hat, uintptr_t* pheno_nm, uintptr_t* pheno_c, Pedigree_rel_info pri, uint32_t skip_write) { |
| 4509 |
FILE* outfile = nullptr; |
| 4510 |
int32_t retval = 0; |
| 4511 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 4512 |
uintptr_t unfiltered_sample_ct4 = (unfiltered_sample_ct + 3) / 4; |
| 4513 |
unsigned char* loadbuf = nullptr; |
| 4514 |
int32_t ibd_prect = 0; |
| 4515 |
int64_t cur_line = 0; |
| 4516 |
double e00 = 0; |
| 4517 |
double e01 = 0; |
| 4518 |
double e02 = 0; |
| 4519 |
double e11 = 0; |
| 4520 |
double e12 = 0; |
| 4521 |
uint32_t mp_lead_unfiltered_idx = 0; |
| 4522 |
uint32_t mp_lead_idx = 0; |
| 4523 |
uint32_t chrom_fo_idx = 0; |
| 4524 |
uint32_t dist_thread_ct = g_thread_ct; |
| 4525 |
char wbuf[16]; |
| 4526 |
int32_t missing_ct_buf[BITCT]; |
| 4527 |
double set_allele_freq_buf[GENOME_MULTIPLEX]; |
| 4528 |
uint32_t nchrobs_buf[GENOME_MULTIPLEX]; |
| 4529 |
unsigned char* overflow_buf; |
| 4530 |
unsigned char* gptr; |
| 4531 |
char* cptr; |
| 4532 |
uintptr_t* geno; |
| 4533 |
uintptr_t* masks; |
| 4534 |
uintptr_t* mmasks; |
| 4535 |
uintptr_t* glptr; |
| 4536 |
uintptr_t* glptr2; |
| 4537 |
uintptr_t* glptr3; |
| 4538 |
uint32_t* missing_dbl_excluded; |
| 4539 |
uint32_t* sample_missing_unwt; |
| 4540 |
uint32_t* genome_main; |
| 4541 |
uint32_t* giptr; |
| 4542 |
uint32_t* giptr2; |
| 4543 |
uint32_t* giptr3; |
| 4544 |
uintptr_t ulii; |
| 4545 |
uintptr_t uljj; |
| 4546 |
uintptr_t ulkk; |
| 4547 |
uint32_t is_last_block; |
| 4548 |
uint32_t uii; |
| 4549 |
uint32_t ujj; |
| 4550 |
uint32_t ukk; |
| 4551 |
uint32_t umm; |
| 4552 |
uint32_t unn; |
| 4553 |
uint32_t uoo; |
| 4554 |
uint32_t upp; |
| 4555 |
uint32_t uqq; |
| 4556 |
int32_t missing_ct_all; |
| 4557 |
double dpp; |
| 4558 |
double dqq; |
| 4559 |
double dpp_sq; |
| 4560 |
double dqq_sq; |
| 4561 |
double dxx; |
| 4562 |
double dyy; |
| 4563 |
double dxx_recip; |
| 4564 |
double dyy_recip; |
| 4565 |
double dxx1; |
| 4566 |
double dxx2; |
| 4567 |
double dyy1; |
| 4568 |
double dyy2; |
| 4569 |
int64_t tot_cells; |
| 4570 |
double num_alleles; |
| 4571 |
double num_allelesf2; |
| 4572 |
double num_allelesf3; |
| 4573 |
uint32_t chrom_end; |
| 4574 |
uint32_t is_x; |
| 4575 |
uint32_t is_y; |
| 4576 |
uint32_t is_mt; |
| 4577 |
uint32_t is_haploid; |
| 4578 |
uintptr_t marker_uidx; |
| 4579 |
uintptr_t sample_uidx; |
| 4580 |
uintptr_t sample_idx; |
| 4581 |
uint32_t marker_idx; |
| 4582 |
uint32_t pct; |
| 4583 |
|
| 4584 |
if (is_set(chrom_info_ptr->haploid_mask, 0)) { |
| 4585 |
logerrprint("Error: --genome cannot be used on haploid genomes.\n"); |
| 4586 |
goto calc_genome_ret_INVALID_CMDLINE; |
| 4587 |
} |
| 4588 |
#ifdef __LP64__ |
| 4589 |
|
| 4590 |
|
| 4591 |
if (sample_ct > 119304647) { |
| 4592 |
logerrprint("Error: --genome does not support > 119304647 samples.\n"); |
| 4593 |
retval = RET_CALC_NOT_YET_SUPPORTED; |
| 4594 |
goto calc_genome_ret_1; |
| 4595 |
} |
| 4596 |
#endif |
| 4597 |
g_sample_ct = sample_ct; |
| 4598 |
if (dist_thread_ct > sample_ct / 32) { |
| 4599 |
dist_thread_ct = sample_ct / 32; |
| 4600 |
if (!dist_thread_ct) { |
| 4601 |
dist_thread_ct = 1; |
| 4602 |
} |
| 4603 |
} |
| 4604 |
g_cg_max_sample_fid_len = plink_maxfid; |
| 4605 |
g_cg_max_sample_iid_len = plink_maxiid; |
| 4606 |
g_cg_min_pi_hat = min_pi_hat; |
| 4607 |
g_cg_max_pi_hat = max_pi_hat; |
| 4608 |
|
| 4609 |
triangle_fill(sample_ct, dist_thread_ct, parallel_tot - parallel_idx - 1, parallel_tot, 1, 1, g_thread_start); |
| 4610 |
|
| 4611 |
for (uii = 0; uii <= dist_thread_ct / 2; uii++) { |
| 4612 |
ujj = g_thread_start[uii]; |
| 4613 |
g_thread_start[uii] = sample_ct - g_thread_start[dist_thread_ct - uii]; |
| 4614 |
g_thread_start[dist_thread_ct - uii] = sample_ct - ujj; |
| 4615 |
} |
| 4616 |
|
| 4617 |
if (!parallel_idx) { |
| 4618 |
cur_line = 1; |
| 4619 |
} |
| 4620 |
g_cg_tstc = g_thread_start[dist_thread_ct]; |
| 4621 |
|
| 4622 |
|
| 4623 |
|
| 4624 |
|
| 4625 |
|
| 4626 |
|
| 4627 |
tot_cells = (int64_t)sample_ct * (g_cg_tstc - g_thread_start[0]) - ((int64_t)g_cg_tstc * (g_cg_tstc + 1) - (int64_t)g_thread_start[0] * (g_thread_start[0] + 1)) / 2; |
| 4628 |
g_cg_tot_lines = cur_line + tot_cells; |
| 4629 |
if (bigstack_alloc_ui(tot_cells, &missing_dbl_excluded) || |
| 4630 |
bigstack_alloc_ui(sample_ct, &sample_missing_unwt) || |
| 4631 |
bigstack_alloc_ui(tot_cells * 5, &genome_main) || |
| 4632 |
bigstack_alloc_uc(GENOME_MULTIPLEX * unfiltered_sample_ct4, &loadbuf) || |
| 4633 |
bigstack_alloc_ul(sample_ct * (GENOME_MULTIPLEX / BITCT2), &geno) || |
| 4634 |
bigstack_alloc_ul(sample_ct * (GENOME_MULTIPLEX / BITCT2), &masks) || |
| 4635 |
bigstack_alloc_ul(sample_ct, &mmasks) || |
| 4636 |
bigstack_alloc_c(plink_maxfid + 1, &g_cg_fam1) || |
| 4637 |
bigstack_alloc_c(plink_maxfid + 1, &g_cg_fam2) || |
| 4638 |
bigstack_alloc_uc(262144, &overflow_buf)) { |
| 4639 |
goto calc_genome_ret_NOMEM; |
| 4640 |
} |
| 4641 |
|
| 4642 |
g_geno = (unsigned char*)geno; |
| 4643 |
g_masks = masks; |
| 4644 |
g_mmasks = mmasks; |
| 4645 |
g_missing_dbl_excluded = missing_dbl_excluded; |
| 4646 |
g_sample_missing_unwt = sample_missing_unwt; |
| 4647 |
g_genome_main = genome_main; |
| 4648 |
fill_uint_zero(tot_cells, missing_dbl_excluded); |
| 4649 |
fill_uint_zero(sample_ct, sample_missing_unwt); |
| 4650 |
fill_uint_zero(tot_cells * 5, genome_main); |
| 4651 |
if (!IS_SET(marker_exclude, 0)) { |
| 4652 |
if (fseeko(bedfile, bed_offset, SEEK_SET)) { |
| 4653 |
retval = RET_READ_FAIL; |
| 4654 |
goto calc_genome_ret_1; |
| 4655 |
} |
| 4656 |
} |
| 4657 |
marker_uidx = 0; |
| 4658 |
marker_idx = 0; |
| 4659 |
refresh_chrom_info(chrom_info_ptr, marker_uidx, &chrom_end, &chrom_fo_idx, &is_x, &is_y, &is_mt, &is_haploid); |
| 4660 |
|
| 4661 |
ukk = count_non_autosomal_markers(chrom_info_ptr, marker_exclude, 1, 1); |
| 4662 |
if (ukk) { |
| 4663 |
LOGPRINTF("Excluding %u variant%s on non-autosomes from IBD calculation.\n", ukk, (ukk == 1)? "" : "s"); |
| 4664 |
marker_ct -= ukk; |
| 4665 |
} |
| 4666 |
do { |
| 4667 |
ukk = marker_ct - marker_idx; |
| 4668 |
if (ukk > GENOME_MULTIPLEX) { |
| 4669 |
ukk = GENOME_MULTIPLEX; |
| 4670 |
} |
| 4671 |
glptr2 = g_marker_window; |
| 4672 |
for (ujj = 0; ujj < ukk; ujj++) { |
| 4673 |
if (IS_SET(marker_exclude, marker_uidx)) { |
| 4674 |
marker_uidx = next_unset_ul_unsafe(marker_exclude, marker_uidx); |
| 4675 |
if (fseeko(bedfile, bed_offset + ((uint64_t)marker_uidx) * unfiltered_sample_ct4, SEEK_SET)) { |
| 4676 |
retval = RET_READ_FAIL; |
| 4677 |
goto calc_genome_ret_1; |
| 4678 |
} |
| 4679 |
} |
| 4680 |
if (marker_uidx >= chrom_end) { |
| 4681 |
while (1) { |
| 4682 |
chrom_fo_idx++; |
| 4683 |
refresh_chrom_info(chrom_info_ptr, marker_uidx, &chrom_end, &chrom_fo_idx, &is_x, &is_y, &is_mt, &is_haploid); |
| 4684 |
if ((!is_haploid) && (!is_mt)) { |
| 4685 |
break; |
| 4686 |
} |
| 4687 |
marker_uidx = next_unset_ul_unsafe(marker_exclude, chrom_end); |
| 4688 |
if (fseeko(bedfile, bed_offset + ((uint64_t)marker_uidx) * unfiltered_sample_ct4, SEEK_SET)) { |
| 4689 |
retval = RET_READ_FAIL; |
| 4690 |
goto calc_genome_ret_1; |
| 4691 |
} |
| 4692 |
} |
| 4693 |
} |
| 4694 |
if (fread(&(loadbuf[ujj * unfiltered_sample_ct4]), 1, unfiltered_sample_ct4, bedfile) < unfiltered_sample_ct4) { |
| 4695 |
retval = RET_READ_FAIL; |
| 4696 |
goto calc_genome_ret_1; |
| 4697 |
} |
| 4698 |
set_allele_freq_buf[ujj] = set_allele_freqs[marker_uidx]; |
| 4699 |
if (nchrobs) { |
| 4700 |
nchrobs_buf[ujj] = nchrobs[marker_uidx]; |
| 4701 |
} |
| 4702 |
|
| 4703 |
|
| 4704 |
|
| 4705 |
|
| 4706 |
|
| 4707 |
|
| 4708 |
|
| 4709 |
|
| 4710 |
|
| 4711 |
|
| 4712 |
|
| 4713 |
|
| 4714 |
|
| 4715 |
if (mp_lead_unfiltered_idx < marker_uidx) { |
| 4716 |
mp_lead_unfiltered_idx = marker_uidx; |
| 4717 |
mp_lead_idx = marker_idx + ujj; |
| 4718 |
} |
| 4719 |
if (mp_lead_unfiltered_idx < chrom_end) { |
| 4720 |
uii = ppc_gap + marker_pos[marker_uidx]; |
| 4721 |
if (marker_pos[mp_lead_unfiltered_idx] <= uii) { |
| 4722 |
do { |
| 4723 |
if (!IS_SET(marker_exclude, mp_lead_unfiltered_idx)) { |
| 4724 |
mp_lead_idx++; |
| 4725 |
} |
| 4726 |
mp_lead_unfiltered_idx++; |
| 4727 |
} while ((mp_lead_unfiltered_idx < chrom_end) && (IS_SET(marker_exclude, mp_lead_unfiltered_idx) || (marker_pos[mp_lead_unfiltered_idx] <= uii))); |
| 4728 |
} |
| 4729 |
} |
| 4730 |
if (mp_lead_unfiltered_idx < unfiltered_marker_ct) { |
| 4731 |
ulii = 2 * (mp_lead_idx - marker_idx); |
| 4732 |
if (ulii < BITCT + (2 * (ujj & (~(BITCT2 - 1))))) { |
| 4733 |
ulii = (~ZEROLU) << (ulii & (BITCT - 1)); |
| 4734 |
} |
| 4735 |
} else { |
| 4736 |
ulii = 2 * (unfiltered_marker_ct + GENOME_MULTIPLEX); |
| 4737 |
} |
| 4738 |
|
| 4739 |
*glptr2++ = ulii; |
| 4740 |
*glptr2++ = ulii; |
| 4741 |
marker_uidx++; |
| 4742 |
} |
| 4743 |
if (ukk < GENOME_MULTIPLEX) { |
| 4744 |
memset(&(loadbuf[ukk * unfiltered_sample_ct4]), 0, (GENOME_MULTIPLEX - ukk) * unfiltered_sample_ct4); |
| 4745 |
fill_ulong_zero(sample_ct * (GENOME_MULTIPLEX / BITCT2), geno); |
| 4746 |
fill_ulong_zero(sample_ct * (GENOME_MULTIPLEX / BITCT2), masks); |
| 4747 |
for (umm = ukk * 2; umm < GENOME_MULTIPLEX2; umm++) { |
| 4748 |
*glptr2++ = GENOME_MULTIPLEX2; |
| 4749 |
} |
| 4750 |
} |
| 4751 |
g_case_ct = marker_idx + ukk; |
| 4752 |
is_last_block = (g_case_ct == marker_ct); |
| 4753 |
for (ujj = 0; ujj < ukk; ujj += BITCT) { |
| 4754 |
glptr = &(geno[ujj / BITCT2]); |
| 4755 |
glptr2 = &(masks[ujj / BITCT2]); |
| 4756 |
glptr3 = mmasks; |
| 4757 |
giptr = sample_missing_unwt; |
| 4758 |
sample_uidx = 0; |
| 4759 |
fill_int_zero(BITCT, missing_ct_buf); |
| 4760 |
missing_ct_all = 0; |
| 4761 |
for (sample_idx = 0; sample_idx < sample_ct; sample_uidx++, sample_idx++) { |
| 4762 |
|
| 4763 |
next_unset_ul_unsafe_ck(sample_exclude, &sample_uidx); |
| 4764 |
uoo = (sample_uidx % 4) * 2; |
| 4765 |
ulii = 0; |
| 4766 |
ulkk = 0; |
| 4767 |
gptr = &(loadbuf[sample_uidx / 4 + ujj * unfiltered_sample_ct4]); |
| 4768 |
uqq = (nonfounders || IS_SET(founder_info, sample_uidx)); |
| 4769 |
if (uqq) { |
| 4770 |
for (upp = 0; upp < BITCT2; upp++) { |
| 4771 |
uljj = (gptr[upp * unfiltered_sample_ct4] >> uoo) & 3; |
| 4772 |
ulii |= uljj << (upp * 2); |
| 4773 |
if (uljj == 1) { |
| 4774 |
missing_ct_buf[upp] += 1; |
| 4775 |
ulkk |= ONELU << upp; |
| 4776 |
*giptr += 1; |
| 4777 |
} |
| 4778 |
} |
| 4779 |
} else { |
| 4780 |
missing_ct_all++; |
| 4781 |
for (upp = 0; upp < BITCT2; upp++) { |
| 4782 |
uljj = (gptr[upp * unfiltered_sample_ct4] >> uoo) & 3; |
| 4783 |
ulii |= uljj << (upp * 2); |
| 4784 |
if (uljj == 1) { |
| 4785 |
ulkk |= ONELU << upp; |
| 4786 |
*giptr += 1; |
| 4787 |
} |
| 4788 |
} |
| 4789 |
} |
| 4790 |
ulii ^= FIVEMASK; |
| 4791 |
*glptr++ = ulii; |
| 4792 |
ulii = (ulii | (ulii >> 1)) & FIVEMASK; |
| 4793 |
*glptr2++ = ulii * 3; |
| 4794 |
*glptr3 = ulkk; |
| 4795 |
|
| 4796 |
ulii = 0; |
| 4797 |
ulkk = 0; |
| 4798 |
gptr = &(loadbuf[sample_uidx / 4 + (ujj + BITCT2) * unfiltered_sample_ct4]); |
| 4799 |
if (uqq) { |
| 4800 |
for (upp = 0; upp < BITCT2; upp++) { |
| 4801 |
uljj = (gptr[upp * unfiltered_sample_ct4] >> uoo) & 3; |
| 4802 |
ulii |= uljj << (upp * 2); |
| 4803 |
if (uljj == 1) { |
| 4804 |
missing_ct_buf[upp + BITCT2] += 1; |
| 4805 |
ulkk |= ONELU << upp; |
| 4806 |
*giptr += 1; |
| 4807 |
} |
| 4808 |
} |
| 4809 |
} else { |
| 4810 |
for (upp = 0; upp < BITCT2; upp++) { |
| 4811 |
uljj = (gptr[upp * unfiltered_sample_ct4] >> uoo) & 3; |
| 4812 |
ulii |= uljj << (upp * 2); |
| 4813 |
if (uljj == 1) { |
| 4814 |
ulkk |= ONELU << upp; |
| 4815 |
*giptr += 1; |
| 4816 |
} |
| 4817 |
} |
| 4818 |
} |
| 4819 |
ulii ^= FIVEMASK; |
| 4820 |
*glptr = ulii; |
| 4821 |
ulii = (ulii | (ulii >> 1)) & FIVEMASK; |
| 4822 |
*glptr2 = ulii * 3; |
| 4823 |
*glptr3++ |= ulkk << BITCT2; |
| 4824 |
glptr = &(glptr[(GENOME_MULTIPLEX2 / BITCT) - 1]); |
| 4825 |
glptr2 = &(glptr2[(GENOME_MULTIPLEX2 / BITCT) - 1]); |
| 4826 |
giptr++; |
| 4827 |
} |
| 4828 |
unn = ukk - ujj; |
| 4829 |
if (unn > BITCT) { |
| 4830 |
unn = BITCT; |
| 4831 |
} |
| 4832 |
for (umm = 0; umm < unn; umm++) { |
| 4833 |
dpp = set_allele_freq_buf[ujj + umm]; |
| 4834 |
dqq = 1.0 - dpp; |
| 4835 |
if ((!nchrobs) || (nchrobs_buf[ujj + umm] == 0xffffffffU)) { |
| 4836 |
num_alleles = (double)(2 * (sample_ct - missing_ct_buf[umm] - missing_ct_all)); |
| 4837 |
} else { |
| 4838 |
num_alleles = (double)nchrobs_buf[ujj + umm]; |
| 4839 |
} |
| 4840 |
if ((num_alleles > 3) && (dpp > 0.0) && (dqq > 0.0)) { |
| 4841 |
|
| 4842 |
|
| 4843 |
num_allelesf2 = num_alleles * num_alleles / ((num_alleles - 1) * (num_alleles - 2)); |
| 4844 |
num_allelesf3 = num_allelesf2 * num_alleles / (num_alleles - 3); |
| 4845 |
dxx = dpp * num_alleles; |
| 4846 |
dyy = dqq * num_alleles; |
| 4847 |
dxx_recip = 1.0 / dxx; |
| 4848 |
dyy_recip = 1.0 / dyy; |
| 4849 |
dpp_sq = dpp * dpp; |
| 4850 |
dqq_sq = dqq * dqq; |
| 4851 |
dxx1 = (dxx - 1) * dxx_recip; |
| 4852 |
dxx2 = dxx1 * (dxx - 2) * dxx_recip; |
| 4853 |
dyy1 = (dyy - 1) * dyy_recip; |
| 4854 |
dyy2 = dyy1 * (dyy - 2) * dyy_recip; |
| 4855 |
e00 += 2 * dpp_sq * dqq_sq * dxx1 * dyy1 * num_allelesf3; |
| 4856 |
e01 += 4 * dpp * dqq * num_allelesf3 * (dpp_sq * dxx2 + dqq_sq * dyy2); |
| 4857 |
e02 += num_allelesf3 * (dqq_sq * dqq_sq * dyy2 * (dyy - 3) * dyy_recip + dpp_sq * dpp_sq * dxx2 * (dxx - 3) * dxx_recip + 4 * dpp_sq * dqq_sq * dxx1 * dyy1); |
| 4858 |
e11 += 2 * dpp * dqq * num_allelesf2 * (dpp * dxx1 + dqq * dyy1); |
| 4859 |
e12 += num_allelesf2 * (dpp_sq * dpp * dxx2 + dqq_sq * dqq * dyy2 + dpp_sq * dqq * dxx1 + dpp * dqq_sq * dyy1); |
| 4860 |
ibd_prect++; |
| 4861 |
} |
| 4862 |
} |
| 4863 |
uii = is_last_block && (ujj + BITCT >= ukk); |
| 4864 |
if (spawn_threads2(threads, &calc_genome_thread, dist_thread_ct, uii)) { |
| 4865 |
goto calc_genome_ret_THREAD_CREATE_FAIL; |
| 4866 |
} |
| 4867 |
ulii = 0; |
| 4868 |
calc_genome_thread((void*)ulii); |
| 4869 |
join_threads2(threads, dist_thread_ct, uii); |
| 4870 |
} |
| 4871 |
|
| 4872 |
marker_idx = g_case_ct; |
| 4873 |
g_ctrl_ct = g_case_ct; |
| 4874 |
printf("\r%u markers complete.", marker_idx); |
| 4875 |
fflush(stdout); |
| 4876 |
} while (!is_last_block); |
| 4877 |
fputs("\rIBD calculations complete. \n", stdout); |
| 4878 |
logstr("IBD calculations complete.\n"); |
| 4879 |
if (skip_write) { |
| 4880 |
goto calc_genome_ret_1; |
| 4881 |
} |
| 4882 |
dxx = 1.0 / ((double)ibd_prect); |
| 4883 |
g_cg_e00 = e00 * dxx; |
| 4884 |
g_cg_e01 = e01 * dxx; |
| 4885 |
g_cg_e02 = e02 * dxx; |
| 4886 |
g_cg_e11 = e11 * dxx; |
| 4887 |
g_cg_e12 = e12 * dxx; |
| 4888 |
|
| 4889 |
if (calculation_type & CALC_PLINK1_IBS_MATRIX) { |
| 4890 |
strcpy(outname_end, ".mibs"); |
| 4891 |
if (fopen_checked(outname, "w", &outfile)) { |
| 4892 |
goto calc_genome_ret_OPEN_FAIL; |
| 4893 |
} |
| 4894 |
giptr = genome_main; |
| 4895 |
giptr2 = missing_dbl_excluded; |
| 4896 |
pct = 1; |
| 4897 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 4898 |
giptr3 = sample_missing_unwt; |
| 4899 |
uii = marker_ct - giptr3[sample_idx]; |
| 4900 |
uljj = sample_idx - 1; |
| 4901 |
for (ulii = 0; ulii < sample_idx; ulii++) { |
| 4902 |
cptr = dtoa_gx(1.0 - ((double)(genome_main[uljj * 5] + 2 * genome_main[uljj * 5 + 1])) / ((double)(2 * (uii - (*giptr3++) + missing_dbl_excluded[uljj]))), ' ', wbuf); |
| 4903 |
fwrite(wbuf, 1, cptr - wbuf, outfile); |
| 4904 |
uljj += sample_ct - ulii - 2; |
| 4905 |
} |
| 4906 |
putc_unlocked('1', outfile); |
| 4907 |
putc_unlocked(' ', outfile); |
| 4908 |
giptr3++; |
| 4909 |
for (ujj = sample_idx + 1; ujj < sample_ct; ujj++) { |
| 4910 |
cptr = dtoa_gx(1.0 - ((double)((*giptr) + 2 * giptr[1])) / ((double)(2 * (uii - (*giptr3++) + (*giptr2++)))), ' ', wbuf); |
| 4911 |
fwrite(wbuf, 1, cptr - wbuf, outfile); |
| 4912 |
giptr = &(giptr[5]); |
| 4913 |
} |
| 4914 |
if (putc_checked('\n', outfile)) { |
| 4915 |
goto calc_genome_ret_WRITE_FAIL; |
| 4916 |
} |
| 4917 |
if (sample_idx * 100 >= (pct * sample_ct)) { |
| 4918 |
pct = (sample_idx * 100) / sample_ct; |
| 4919 |
printf("\rWriting... %d%%", pct++); |
| 4920 |
fflush(stdout); |
| 4921 |
} |
| 4922 |
} |
| 4923 |
if (fclose_null(&outfile)) { |
| 4924 |
goto calc_genome_ret_WRITE_FAIL; |
| 4925 |
} |
| 4926 |
putc_unlocked('\r', stdout); |
| 4927 |
strcpy(outname_end, ".mibs.id"); |
| 4928 |
retval = write_ids(outname, unfiltered_sample_ct, sample_exclude, sample_ids, max_sample_id_len); |
| 4929 |
if (retval) { |
| 4930 |
goto calc_genome_ret_1; |
| 4931 |
} |
| 4932 |
*outname_end = '\0'; |
| 4933 |
LOGPRINTFWW("IBS matrix written to %s.mibs , and IDs written to %s.mibs.id .\n", outname, outname); |
| 4934 |
} |
| 4935 |
|
| 4936 |
if (calculation_type & CALC_PLINK1_DISTANCE_MATRIX) { |
| 4937 |
strcpy(outname_end, ".mdist"); |
| 4938 |
if (fopen_checked(outname, "w", &outfile)) { |
| 4939 |
goto calc_genome_ret_OPEN_FAIL; |
| 4940 |
} |
| 4941 |
giptr = genome_main; |
| 4942 |
giptr2 = missing_dbl_excluded; |
| 4943 |
ukk = 1; |
| 4944 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 4945 |
giptr3 = sample_missing_unwt; |
| 4946 |
uii = marker_ct - giptr3[sample_idx]; |
| 4947 |
uljj = sample_idx - 1; |
| 4948 |
for (ulii = 0; ulii < sample_idx; ulii++) { |
| 4949 |
cptr = dtoa_gx(((double)(genome_main[uljj * 5] + 2 * genome_main[uljj * 5 + 1])) / ((double)(2 * (uii - (*giptr3++) + missing_dbl_excluded[uljj]))), ' ', wbuf); |
| 4950 |
fwrite(wbuf, 1, cptr - wbuf, outfile); |
| 4951 |
uljj += sample_ct - ulii - 2; |
| 4952 |
} |
| 4953 |
putc_unlocked('0', outfile); |
| 4954 |
putc_unlocked(' ', outfile); |
| 4955 |
giptr3++; |
| 4956 |
for (ujj = sample_idx + 1; ujj < sample_ct; ujj++) { |
| 4957 |
cptr = dtoa_gx(((double)((*giptr) + 2 * giptr[1])) / ((double)(2 * (uii - (*giptr3++) + (*giptr2++)))), ' ', wbuf); |
| 4958 |
fwrite(wbuf, 1, cptr - wbuf, outfile); |
| 4959 |
giptr = &(giptr[5]); |
| 4960 |
} |
| 4961 |
if (putc_checked('\n', outfile)) { |
| 4962 |
goto calc_genome_ret_WRITE_FAIL; |
| 4963 |
} |
| 4964 |
if (sample_idx * 100 >= (ukk * sample_ct)) { |
| 4965 |
ukk = (sample_idx * 100) / sample_ct; |
| 4966 |
printf("\rWriting... %d%%", ukk++); |
| 4967 |
fflush(stdout); |
| 4968 |
} |
| 4969 |
} |
| 4970 |
if (fclose_null(&outfile)) { |
| 4971 |
goto calc_genome_ret_WRITE_FAIL; |
| 4972 |
} |
| 4973 |
putc_unlocked('\r', stdout); |
| 4974 |
strcpy(outname_end, ".mdist.id"); |
| 4975 |
retval = write_ids(outname, unfiltered_sample_ct, sample_exclude, sample_ids, max_sample_id_len); |
| 4976 |
if (retval) { |
| 4977 |
goto calc_genome_ret_1; |
| 4978 |
} |
| 4979 |
*outname_end = '\0'; |
| 4980 |
LOGPRINTFWW("Distances (proportions) written to %s.mdist , and IDs written to %s.mdist.id .\n", outname, outname); |
| 4981 |
} |
| 4982 |
|
| 4983 |
if (!parallel_idx) { |
| 4984 |
if (genome_modifier & GENOME_OUTPUT_FULL) { |
| 4985 |
sprintf(g_textbuf, "%%%us%%%us%%%us%%%us RT EZ Z0 Z1 Z2 PI_HAT PHE DST PPC RATIO IBS0 IBS1 IBS2 HOMHOM HETHET\n", plink_maxfid, plink_maxiid, plink_maxfid, plink_maxiid); |
| 4986 |
} else { |
| 4987 |
sprintf(g_textbuf, "%%%us%%%us%%%us%%%us RT EZ Z0 Z1 Z2 PI_HAT PHE DST PPC RATIO\n", plink_maxfid, plink_maxiid, plink_maxfid, plink_maxiid); |
| 4988 |
} |
| 4989 |
} |
| 4990 |
g_pct = 1; |
| 4991 |
g_cg_gmcell = 0; |
| 4992 |
g_cg_mdecell = 0; |
| 4993 |
g_cg_marker_ct = marker_ct; |
| 4994 |
g_cg_max_sample_id_len = max_sample_id_len; |
| 4995 |
g_cg_max_paternal_id_len = max_paternal_id_len; |
| 4996 |
g_cg_max_maternal_id_len = max_maternal_id_len; |
| 4997 |
g_cg_pheno_nm = pheno_nm; |
| 4998 |
g_cg_pheno_c = pheno_c; |
| 4999 |
g_cg_founder_info = founder_info; |
| 5000 |
g_cg_sample_ids = sample_ids; |
| 5001 |
g_cg_paternal_ids = paternal_ids; |
| 5002 |
g_cg_maternal_ids = maternal_ids; |
| 5003 |
g_cg_genome_modifier = genome_modifier; |
| 5004 |
g_cg_pri = pri; |
| 5005 |
g_cg_cur_line = 0; |
| 5006 |
|
| 5007 |
|
| 5008 |
|
| 5009 |
g_cg_sample1idx = parallel_idx; |
| 5010 |
g_cg_sample2idx = 0; |
| 5011 |
g_cg_sample_exclude = sample_exclude; |
| 5012 |
g_cg_sample1uidx = jump_forward_unset_unsafe(sample_exclude, 0, g_thread_start[0] + 1); |
| 5013 |
g_cg_sample2uidx = g_cg_sample1uidx + 1; |
| 5014 |
if (genome_modifier & GENOME_OUTPUT_GZ) { |
| 5015 |
if (parallel_tot > 1) { |
| 5016 |
snprintf(outname_end, MAX_POST_EXT, ".genome.%d.gz", parallel_idx + 1); |
| 5017 |
} else { |
| 5018 |
strcpy(outname_end, ".genome.gz"); |
| 5019 |
} |
| 5020 |
parallel_compress(outname, overflow_buf, 0, calc_genome_emitn); |
| 5021 |
} else { |
| 5022 |
if (parallel_tot > 1) { |
| 5023 |
snprintf(outname_end, MAX_POST_EXT, ".genome.%d", parallel_idx + 1); |
| 5024 |
} else { |
| 5025 |
strcpy(outname_end, ".genome"); |
| 5026 |
} |
| 5027 |
retval = write_uncompressed(outname, overflow_buf, 0, calc_genome_emitn); |
| 5028 |
if (retval) { |
| 5029 |
goto calc_genome_ret_1; |
| 5030 |
} |
| 5031 |
} |
| 5032 |
putc_unlocked('\r', stdout); |
| 5033 |
LOGPRINTFWW("Finished writing %s .\n", outname); |
| 5034 |
while (0) { |
| 5035 |
calc_genome_ret_NOMEM: |
| 5036 |
retval = RET_NOMEM; |
| 5037 |
break; |
| 5038 |
calc_genome_ret_OPEN_FAIL: |
| 5039 |
retval = RET_OPEN_FAIL; |
| 5040 |
break; |
| 5041 |
calc_genome_ret_WRITE_FAIL: |
| 5042 |
retval = RET_WRITE_FAIL; |
| 5043 |
break; |
| 5044 |
calc_genome_ret_INVALID_CMDLINE: |
| 5045 |
retval = RET_INVALID_CMDLINE; |
| 5046 |
break; |
| 5047 |
calc_genome_ret_THREAD_CREATE_FAIL: |
| 5048 |
retval = RET_THREAD_CREATE_FAIL; |
| 5049 |
break; |
| 5050 |
} |
| 5051 |
calc_genome_ret_1: |
| 5052 |
fclose_cond(outfile); |
| 5053 |
if ((!retval) && (calculation_type & (CALC_CLUSTER | CALC_NEIGHBOR))) { |
| 5054 |
bigstack_reset(loadbuf); |
| 5055 |
} else { |
| 5056 |
bigstack_reset(bigstack_mark); |
| 5057 |
} |
| 5058 |
return retval; |
| 5059 |
} |
| 5060 |
|
| 5061 |
static inline void rel_cut_arr_dec(int32_t* rel_ct_arr_elem, uint32_t* exactly_one_rel_ct_ptr) { |
| 5062 |
int32_t rcae = *rel_ct_arr_elem - 1; |
| 5063 |
*rel_ct_arr_elem = rcae; |
| 5064 |
if (rcae < 2) { |
| 5065 |
if (rcae) { |
| 5066 |
*exactly_one_rel_ct_ptr += 1; |
| 5067 |
} else { |
| 5068 |
*exactly_one_rel_ct_ptr -= 1; |
| 5069 |
} |
| 5070 |
} |
| 5071 |
} |
| 5072 |
|
| 5073 |
int32_t do_rel_cutoff(uint64_t calculation_type, double rel_cutoff, double* rel_ibc, uintptr_t* sample_exclude, uintptr_t* sample_exclude_ct_ptr, char* outname, char* outname_end, uintptr_t unfiltered_sample_ct, char* sample_ids, uintptr_t max_sample_id_len) { |
| 5074 |
uint32_t samples_excluded = 0; |
| 5075 |
uint32_t exactly_one_rel_ct = 0; |
| 5076 |
uintptr_t sample_ct = unfiltered_sample_ct - (*sample_exclude_ct_ptr); |
| 5077 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 5078 |
double* rel_dists = g_rel_dists; |
| 5079 |
double* dist_ptr = rel_dists; |
| 5080 |
double* dptr2; |
| 5081 |
double* dptr3; |
| 5082 |
double* dptr4; |
| 5083 |
uint32_t* giptr; |
| 5084 |
uint32_t* giptr2; |
| 5085 |
|
| 5086 |
int32_t* rel_ct_arr; |
| 5087 |
uintptr_t sample_idx; |
| 5088 |
uint32_t uii; |
| 5089 |
uintptr_t ulii; |
| 5090 |
int32_t kk; |
| 5091 |
int32_t mm; |
| 5092 |
int32_t retval; |
| 5093 |
|
| 5094 |
|
| 5095 |
|
| 5096 |
|
| 5097 |
|
| 5098 |
|
| 5099 |
|
| 5100 |
|
| 5101 |
|
| 5102 |
if (bigstack_calloc_i(sample_ct, &rel_ct_arr)) { |
| 5103 |
return RET_NOMEM; |
| 5104 |
} |
| 5105 |
for (sample_idx = 1; sample_idx < sample_ct; sample_idx++) { |
| 5106 |
for (uii = 0; uii < sample_idx; uii++) { |
| 5107 |
if (*dist_ptr++ > rel_cutoff) { |
| 5108 |
rel_ct_arr[sample_idx] += 1; |
| 5109 |
rel_ct_arr[uii] += 1; |
| 5110 |
} |
| 5111 |
} |
| 5112 |
} |
| 5113 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 5114 |
if (rel_ct_arr[sample_idx] == 1) { |
| 5115 |
exactly_one_rel_ct++; |
| 5116 |
} |
| 5117 |
} |
| 5118 |
while (1) { |
| 5119 |
kk = 0; |
| 5120 |
if (exactly_one_rel_ct) { |
| 5121 |
|
| 5122 |
|
| 5123 |
while (rel_ct_arr[kk] != 1) { |
| 5124 |
kk++; |
| 5125 |
} |
| 5126 |
|
| 5127 |
dist_ptr = &(rel_dists[((intptr_t)kk * (kk - 1)) / 2]); |
| 5128 |
for (mm = 0; mm < kk; mm++) { |
| 5129 |
if (*dist_ptr > rel_cutoff) { |
| 5130 |
*dist_ptr = 0.0; |
| 5131 |
break; |
| 5132 |
} |
| 5133 |
dist_ptr++; |
| 5134 |
} |
| 5135 |
if (mm == kk) { |
| 5136 |
do { |
| 5137 |
mm++; |
| 5138 |
dist_ptr = &(rel_dists[tri_coord_no_diag((uint32_t)kk, (uint32_t)mm)]); |
| 5139 |
} while (*dist_ptr <= rel_cutoff); |
| 5140 |
*dist_ptr = 0.0; |
| 5141 |
} |
| 5142 |
rel_ct_arr[kk] = 0; |
| 5143 |
exactly_one_rel_ct--; |
| 5144 |
if (rel_ct_arr[mm] == 1) { |
| 5145 |
|
| 5146 |
exactly_one_rel_ct--; |
| 5147 |
rel_ct_arr[mm] = -1; |
| 5148 |
samples_excluded++; |
| 5149 |
continue; |
| 5150 |
} |
| 5151 |
} else { |
| 5152 |
|
| 5153 |
|
| 5154 |
|
| 5155 |
mm = -1; |
| 5156 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 5157 |
if (rel_ct_arr[sample_idx] > kk) { |
| 5158 |
kk = rel_ct_arr[sample_idx]; |
| 5159 |
mm = sample_idx; |
| 5160 |
} |
| 5161 |
} |
| 5162 |
|
| 5163 |
if (mm == -1) { |
| 5164 |
break; |
| 5165 |
} |
| 5166 |
} |
| 5167 |
dist_ptr = &(rel_dists[((intptr_t)mm * (mm - 1)) / 2]); |
| 5168 |
for (kk = 0; kk < mm; kk++) { |
| 5169 |
if (*dist_ptr > rel_cutoff) { |
| 5170 |
*dist_ptr = 0.0; |
| 5171 |
rel_cut_arr_dec(&(rel_ct_arr[kk]), &exactly_one_rel_ct); |
| 5172 |
} |
| 5173 |
dist_ptr++; |
| 5174 |
} |
| 5175 |
for (ulii = mm + 1; ulii < sample_ct; ulii++) { |
| 5176 |
dist_ptr = &(rel_dists[tri_coord_no_diag((uint32_t)mm, ulii)]); |
| 5177 |
if (*dist_ptr > rel_cutoff) { |
| 5178 |
*dist_ptr = 0.0; |
| 5179 |
rel_cut_arr_dec(&(rel_ct_arr[ulii]), &exactly_one_rel_ct); |
| 5180 |
} |
| 5181 |
} |
| 5182 |
rel_ct_arr[mm] = -1; |
| 5183 |
samples_excluded++; |
| 5184 |
} |
| 5185 |
exclude_multi(sample_exclude, rel_ct_arr, unfiltered_sample_ct, sample_exclude_ct_ptr); |
| 5186 |
if (samples_excluded) { |
| 5187 |
dist_ptr = rel_dists; |
| 5188 |
dptr2 = rel_dists; |
| 5189 |
dptr3 = rel_ibc; |
| 5190 |
dptr4 = rel_ibc; |
| 5191 |
giptr = g_missing_dbl_excluded; |
| 5192 |
giptr2 = g_missing_dbl_excluded; |
| 5193 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 5194 |
if (rel_ct_arr[sample_idx] != -1) { |
| 5195 |
if (calculation_type & CALC_IBC) { |
| 5196 |
dptr3[sample_ct] = dptr4[sample_ct]; |
| 5197 |
dptr3[sample_ct * 2] = dptr4[sample_ct * 2]; |
| 5198 |
} |
| 5199 |
*dptr3 = *dptr4++; |
| 5200 |
dptr3++; |
| 5201 |
for (uii = 0; uii < sample_idx; uii++) { |
| 5202 |
if (rel_ct_arr[uii] != -1) { |
| 5203 |
*dist_ptr = *dptr2++; |
| 5204 |
dist_ptr++; |
| 5205 |
*giptr = *giptr2++; |
| 5206 |
giptr++; |
| 5207 |
} else { |
| 5208 |
dptr2++; |
| 5209 |
giptr2++; |
| 5210 |
} |
| 5211 |
} |
| 5212 |
} else { |
| 5213 |
dptr4++; |
| 5214 |
dptr2 = &(dptr2[sample_idx]); |
| 5215 |
giptr2 = &(giptr2[sample_idx]); |
| 5216 |
} |
| 5217 |
} |
| 5218 |
sample_ct -= samples_excluded; |
| 5219 |
if (calculation_type & CALC_IBC) { |
| 5220 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 5221 |
*dptr3++ = *dptr4++; |
| 5222 |
} |
| 5223 |
dptr4 = &(dptr4[samples_excluded]); |
| 5224 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 5225 |
*dptr3++ = *dptr4++; |
| 5226 |
} |
| 5227 |
} |
| 5228 |
giptr = g_sample_missing_unwt; |
| 5229 |
giptr2 = g_sample_missing_unwt; |
| 5230 |
for (sample_idx = 0; sample_idx < sample_ct + samples_excluded; sample_idx++) { |
| 5231 |
if (rel_ct_arr[sample_idx] != -1) { |
| 5232 |
*giptr = *giptr2++; |
| 5233 |
giptr++; |
| 5234 |
} else { |
| 5235 |
giptr2++; |
| 5236 |
} |
| 5237 |
} |
| 5238 |
} |
| 5239 |
LOGPRINTF("%u %s excluded by --rel-cutoff.\n", samples_excluded, species_str(samples_excluded)); |
| 5240 |
if (!(calculation_type & (CALC_RELATIONSHIP | CALC_GDISTANCE_MASK))) { |
| 5241 |
strcpy(outname_end, ".rel.id"); |
| 5242 |
retval = write_ids(outname, unfiltered_sample_ct, sample_exclude, sample_ids, max_sample_id_len); |
| 5243 |
if (retval) { |
| 5244 |
return retval; |
| 5245 |
} |
| 5246 |
LOGPRINTFWW("Remaining sample IDs written to %s .\n", outname); |
| 5247 |
} |
| 5248 |
bigstack_reset(bigstack_mark); |
| 5249 |
return 0; |
| 5250 |
} |
| 5251 |
|
| 5252 |
uint32_t g_rcb_row; |
| 5253 |
uint32_t g_rcb_col; |
| 5254 |
uint32_t g_rcb_new_row; |
| 5255 |
uint32_t g_rcb_new_col; |
| 5256 |
uint32_t g_rcb_sample_ct; |
| 5257 |
uint64_t g_rcb_progress; |
| 5258 |
uint64_t g_rcb_hundredth; |
| 5259 |
FILE* g_rcb_in_binfile; |
| 5260 |
FILE* g_rcb_in_bin_nfile; |
| 5261 |
gzFile g_rcb_cur_gzfile; |
| 5262 |
int32_t* g_rcb_rel_ct_arr; |
| 5263 |
|
| 5264 |
uint32_t rel_cutoff_batch_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 5265 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 5266 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 5267 |
gzFile cur_gzfile = g_rcb_cur_gzfile; |
| 5268 |
uint32_t sample_ct = g_rcb_sample_ct; |
| 5269 |
uint32_t row = g_rcb_row; |
| 5270 |
uint32_t col = g_rcb_col; |
| 5271 |
uint32_t new_row = g_rcb_new_row; |
| 5272 |
uint32_t new_col = g_rcb_new_col; |
| 5273 |
uint64_t progress = g_rcb_progress; |
| 5274 |
uint64_t hundredth = g_rcb_hundredth; |
| 5275 |
uint32_t pct = g_pct; |
| 5276 |
int32_t* rel_ct_arr = g_rcb_rel_ct_arr; |
| 5277 |
char wbuf[16]; |
| 5278 |
char* cptr; |
| 5279 |
uint32_t wbuf_ct; |
| 5280 |
uint32_t uii; |
| 5281 |
while (row < sample_ct) { |
| 5282 |
if (rel_ct_arr[row] == -1) { |
| 5283 |
for (uii = 0; uii <= row; uii++) { |
| 5284 |
gzgets(cur_gzfile, g_textbuf, MAXLINELEN); |
| 5285 |
} |
| 5286 |
} else { |
| 5287 |
cptr = uint32toa_x(new_row, '\t', wbuf); |
| 5288 |
wbuf_ct = (uintptr_t)(cptr - wbuf); |
| 5289 |
while (col <= row) { |
| 5290 |
gzgets(cur_gzfile, g_textbuf, MAXLINELEN); |
| 5291 |
if (rel_ct_arr[col++] != -1) { |
| 5292 |
cptr = next_token_mult(g_textbuf, 2); |
| 5293 |
uii = strlen(cptr); |
| 5294 |
sptr_cur = memcpya(sptr_cur, wbuf, wbuf_ct); |
| 5295 |
sptr_cur = uint32toa_x(++new_col, '\t', sptr_cur); |
| 5296 |
sptr_cur = memcpya(sptr_cur, cptr, uii); |
| 5297 |
if (sptr_cur >= readbuf_end) { |
| 5298 |
goto rel_cutoff_batch_emitn_ret; |
| 5299 |
} |
| 5300 |
} |
| 5301 |
} |
| 5302 |
new_row++; |
| 5303 |
new_col = 0; |
| 5304 |
} |
| 5305 |
row++; |
| 5306 |
progress += row; |
| 5307 |
if (progress >= pct * hundredth) { |
| 5308 |
if (pct > 10) { |
| 5309 |
putc_unlocked('\b', stdout); |
| 5310 |
} |
| 5311 |
pct = 1 + (progress / hundredth); |
| 5312 |
printf("\b\b%u%%", pct - 1); |
| 5313 |
fflush(stdout); |
| 5314 |
} |
| 5315 |
col = 0; |
| 5316 |
} |
| 5317 |
rel_cutoff_batch_emitn_ret: |
| 5318 |
g_rcb_row = row; |
| 5319 |
g_rcb_col = col; |
| 5320 |
g_rcb_new_row = new_row; |
| 5321 |
g_rcb_new_col = new_col; |
| 5322 |
g_rcb_progress = progress; |
| 5323 |
g_pct = pct; |
| 5324 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 5325 |
} |
| 5326 |
|
| 5327 |
uint32_t rel_cutoff_batch_rbin_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 5328 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 5329 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 5330 |
FILE* in_binfile = g_rcb_in_binfile; |
| 5331 |
FILE* in_bin_nfile = g_rcb_in_bin_nfile; |
| 5332 |
uint32_t sample_ct = g_rcb_sample_ct; |
| 5333 |
uint32_t row = g_rcb_row; |
| 5334 |
uint32_t col = g_rcb_col; |
| 5335 |
uint32_t new_row = g_rcb_new_row; |
| 5336 |
uint32_t new_col = g_rcb_new_col; |
| 5337 |
uint64_t progress = g_rcb_progress; |
| 5338 |
uint64_t hundredth = g_rcb_hundredth; |
| 5339 |
uint32_t pct = g_pct; |
| 5340 |
int32_t* rel_ct_arr = g_rcb_rel_ct_arr; |
| 5341 |
char wbuf[16]; |
| 5342 |
char* cptr; |
| 5343 |
uint32_t wbuf_ct; |
| 5344 |
uint32_t uii; |
| 5345 |
float fxx; |
| 5346 |
float fyy; |
| 5347 |
while (row < sample_ct) { |
| 5348 |
if (rel_ct_arr[row] == -1) { |
| 5349 |
fseeko(in_binfile, (row + 1) * sizeof(float), SEEK_CUR); |
| 5350 |
fseeko(in_bin_nfile, (row + 1) * sizeof(float), SEEK_CUR); |
| 5351 |
} else { |
| 5352 |
cptr = uint32toa_x(new_row, '\t', wbuf); |
| 5353 |
wbuf_ct = (uintptr_t)(cptr - wbuf); |
| 5354 |
while (col <= row) { |
| 5355 |
if (rel_ct_arr[col] == -1) { |
| 5356 |
uii = col; |
| 5357 |
while (++col <= row) { |
| 5358 |
if (rel_ct_arr[col] != -1) { |
| 5359 |
break; |
| 5360 |
} |
| 5361 |
} |
| 5362 |
fseeko(in_binfile, (col - uii) * sizeof(float), SEEK_CUR); |
| 5363 |
fseeko(in_bin_nfile, (col - uii) * sizeof(float), SEEK_CUR); |
| 5364 |
if (col > row) { |
| 5365 |
break; |
| 5366 |
} |
| 5367 |
} |
| 5368 |
sptr_cur = memcpya(sptr_cur, wbuf, wbuf_ct); |
| 5369 |
sptr_cur = uint32toa_x(++new_col, '\t', sptr_cur); |
| 5370 |
if ((fread(&fxx, sizeof(float), 1, in_bin_nfile) != sizeof(float)) || (fread(&fyy, sizeof(float), 1, in_binfile) != sizeof(float))) { |
| 5371 |
|
| 5372 |
putc_unlocked('\n', stdout); |
| 5373 |
fflush(stdout); |
| 5374 |
fputs("Error: File read failure.\n", stderr); |
| 5375 |
exit(RET_READ_FAIL); |
| 5376 |
} |
| 5377 |
sptr_cur = uint32toa_x((int32_t)fxx, '\t', sptr_cur); |
| 5378 |
sptr_cur = ftoa_ex(fyy, '\n', sptr_cur); |
| 5379 |
col++; |
| 5380 |
if (sptr_cur >= readbuf_end) { |
| 5381 |
goto rel_cutoff_batch_rbin_emitn_ret; |
| 5382 |
} |
| 5383 |
} |
| 5384 |
new_row++; |
| 5385 |
new_col = 0; |
| 5386 |
} |
| 5387 |
row++; |
| 5388 |
progress += row; |
| 5389 |
if (progress >= pct * hundredth) { |
| 5390 |
if (pct > 10) { |
| 5391 |
putc_unlocked('\b', stdout); |
| 5392 |
} |
| 5393 |
pct = 1 + (progress / hundredth); |
| 5394 |
printf("\b\b%u%%", pct - 1); |
| 5395 |
fflush(stdout); |
| 5396 |
} |
| 5397 |
col = 0; |
| 5398 |
} |
| 5399 |
rel_cutoff_batch_rbin_emitn_ret: |
| 5400 |
g_rcb_row = row; |
| 5401 |
g_rcb_col = col; |
| 5402 |
g_rcb_new_row = new_row; |
| 5403 |
g_rcb_new_col = new_col; |
| 5404 |
g_rcb_progress = progress; |
| 5405 |
g_pct = pct; |
| 5406 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 5407 |
} |
| 5408 |
|
| 5409 |
int32_t rel_cutoff_batch(uint32_t load_grm_bin, char* grmname, char* outname, char* outname_end, Rel_info* relip) { |
| 5410 |
|
| 5411 |
char* grmname_end = (char*)memchr(grmname, 0, FNAMESIZE); |
| 5412 |
uintptr_t sample_ct = 0; |
| 5413 |
uintptr_t line_idx = 0; |
| 5414 |
double rel_cutoff = relip->cutoff; |
| 5415 |
FILE* idfile = nullptr; |
| 5416 |
FILE* outfile = nullptr; |
| 5417 |
FILE* out_bin_nfile = nullptr; |
| 5418 |
FILE* in_binfile = nullptr; |
| 5419 |
FILE* in_bin_nfile = nullptr; |
| 5420 |
gzFile cur_gzfile = nullptr; |
| 5421 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 5422 |
uint32_t samples_excluded = 0; |
| 5423 |
uint32_t exactly_one_rel_ct = 0; |
| 5424 |
uint32_t rel_calc_type = relip->modifier & REL_CALC_MASK; |
| 5425 |
uintptr_t* compact_rel_table; |
| 5426 |
uintptr_t* rtptr; |
| 5427 |
unsigned char* overflow_buf; |
| 5428 |
char* bufptr; |
| 5429 |
uint64_t ullii; |
| 5430 |
uint64_t ulljj; |
| 5431 |
uint64_t progress; |
| 5432 |
uint64_t hundredth; |
| 5433 |
uintptr_t tot_words; |
| 5434 |
uintptr_t words_left; |
| 5435 |
uintptr_t wl_floor; |
| 5436 |
uintptr_t cur_word; |
| 5437 |
uintptr_t ulii; |
| 5438 |
uintptr_t uljj; |
| 5439 |
uintptr_t ulkk; |
| 5440 |
uint32_t inword_idx; |
| 5441 |
uint32_t inword_bound; |
| 5442 |
uint32_t uii; |
| 5443 |
uint32_t row; |
| 5444 |
uint32_t col; |
| 5445 |
uint32_t pct; |
| 5446 |
uintptr_t sample_idx; |
| 5447 |
int32_t* rel_ct_arr; |
| 5448 |
float rel_cutoff_f; |
| 5449 |
float fxx; |
| 5450 |
float fyy; |
| 5451 |
double dxx; |
| 5452 |
int32_t retval; |
| 5453 |
int32_t kk; |
| 5454 |
int32_t mm; |
| 5455 |
int32_t cur_prune; |
| 5456 |
ulii = (uintptr_t)(grmname_end - grmname); |
| 5457 |
if ((ulii == (uintptr_t)(outname_end - outname)) && (!memcmp(grmname, outname, ulii))) { |
| 5458 |
LOGERRPRINTF("Error: --rel-cutoff input and output ID filenames cannot match.%s\n", strcmp(outname, PROG_NAME_STR)? "" : " (Use --out.)"); |
| 5459 |
goto rel_cutoff_batch_ret_INVALID_CMDLINE; |
| 5460 |
} |
| 5461 |
memcpy(grmname_end, ".grm.id", 8); |
| 5462 |
if (fopen_checked(grmname, "r", &idfile)) { |
| 5463 |
goto rel_cutoff_batch_ret_OPEN_FAIL; |
| 5464 |
} |
| 5465 |
g_textbuf[MAXLINELEN - 1] = ' '; |
| 5466 |
while (fgets(g_textbuf, MAXLINELEN, idfile)) { |
| 5467 |
line_idx++; |
| 5468 |
if (!g_textbuf[MAXLINELEN - 1]) { |
| 5469 |
LOGPREPRINTFWW("Error: Line %" PRIuPTR " of %s is pathologically long.\n", line_idx, grmname); |
| 5470 |
goto rel_cutoff_batch_ret_INVALID_FORMAT_2; |
| 5471 |
} |
| 5472 |
if (is_eoln_kns(*(skip_initial_spaces(g_textbuf)))) { |
| 5473 |
continue; |
| 5474 |
} |
| 5475 |
sample_ct++; |
| 5476 |
} |
| 5477 |
if (!feof(idfile)) { |
| 5478 |
goto rel_cutoff_batch_ret_READ_FAIL; |
| 5479 |
} |
| 5480 |
fclose_null(&idfile); |
| 5481 |
ullii = sample_ct; |
| 5482 |
ullii = BITCT_TO_WORDCT((ullii * (ullii - 1)) / 2); |
| 5483 |
#ifndef __LP64__ |
| 5484 |
if (ullii >= 0x20000000) { |
| 5485 |
goto rel_cutoff_batch_ret_NOMEM; |
| 5486 |
} |
| 5487 |
#endif |
| 5488 |
tot_words = ullii; |
| 5489 |
if (bigstack_calloc_ul(tot_words, &compact_rel_table) || |
| 5490 |
bigstack_calloc_i(sample_ct, &rel_ct_arr) || |
| 5491 |
bigstack_alloc_uc(262144, &overflow_buf)) { |
| 5492 |
goto rel_cutoff_batch_ret_NOMEM; |
| 5493 |
} |
| 5494 |
|
| 5495 |
fputs("Reading... 0%", stdout); |
| 5496 |
fflush(stdout); |
| 5497 |
words_left = tot_words; |
| 5498 |
rtptr = compact_rel_table; |
| 5499 |
row = 0; |
| 5500 |
col = 0; |
| 5501 |
if (load_grm_bin) { |
| 5502 |
memcpy(grmname_end, ".grm.bin", 9); |
| 5503 |
if (fopen_checked(grmname, FOPEN_RB, &in_binfile)) { |
| 5504 |
goto rel_cutoff_batch_ret_OPEN_FAIL; |
| 5505 |
} |
| 5506 |
rel_cutoff_f = (float)rel_cutoff; |
| 5507 |
for (pct = 1; pct <= 100; pct++) { |
| 5508 |
wl_floor = (((uint64_t)tot_words) * (100 - pct)) / 100; |
| 5509 |
while (words_left > wl_floor) { |
| 5510 |
cur_word = 0; |
| 5511 |
if (--words_left) { |
| 5512 |
inword_bound = BITCT; |
| 5513 |
} else { |
| 5514 |
|
| 5515 |
uii = sample_ct & (BITCT * 2 - 1); |
| 5516 |
inword_bound = ((uii * (uii - 1)) / 2) & (BITCT - 1); |
| 5517 |
if (!inword_bound) { |
| 5518 |
inword_bound = BITCT; |
| 5519 |
} |
| 5520 |
} |
| 5521 |
for (inword_idx = 0; inword_idx < inword_bound; inword_idx++) { |
| 5522 |
if (!fread(&fxx, 4, 1, in_binfile)) { |
| 5523 |
goto rel_cutoff_batch_ret_READ_FAIL; |
| 5524 |
} |
| 5525 |
if (row == col) { |
| 5526 |
row++; |
| 5527 |
col = 0; |
| 5528 |
if (!fread(&fxx, 4, 1, in_binfile)) { |
| 5529 |
goto rel_cutoff_batch_ret_READ_FAIL; |
| 5530 |
} |
| 5531 |
} |
| 5532 |
if (fxx > rel_cutoff_f) { |
| 5533 |
rel_ct_arr[row] += 1; |
| 5534 |
rel_ct_arr[col] += 1; |
| 5535 |
cur_word |= (ONELU << inword_idx); |
| 5536 |
} |
| 5537 |
col++; |
| 5538 |
} |
| 5539 |
*rtptr++ = cur_word; |
| 5540 |
} |
| 5541 |
if (pct < 100) { |
| 5542 |
if (pct > 10) { |
| 5543 |
putc_unlocked('\b', stdout); |
| 5544 |
} |
| 5545 |
printf("\b\b%u%%", pct); |
| 5546 |
fflush(stdout); |
| 5547 |
} |
| 5548 |
} |
| 5549 |
fclose_null(&in_binfile); |
| 5550 |
} else { |
| 5551 |
memcpy(grmname_end, ".grm.gz", 8); |
| 5552 |
retval = gzopen_read_checked(grmname, &cur_gzfile); |
| 5553 |
if (retval) { |
| 5554 |
goto rel_cutoff_batch_ret_1; |
| 5555 |
} |
| 5556 |
for (pct = 1; pct <= 100; pct++) { |
| 5557 |
wl_floor = (((uint64_t)tot_words) * (100 - pct)) / 100; |
| 5558 |
while (words_left > wl_floor) { |
| 5559 |
cur_word = 0; |
| 5560 |
if (--words_left) { |
| 5561 |
inword_bound = BITCT; |
| 5562 |
} else { |
| 5563 |
|
| 5564 |
uii = sample_ct & (BITCT * 2 - 1); |
| 5565 |
inword_bound = ((uii * (uii - 1)) / 2) & (BITCT - 1); |
| 5566 |
if (!inword_bound) { |
| 5567 |
inword_bound = BITCT; |
| 5568 |
} |
| 5569 |
} |
| 5570 |
for (inword_idx = 0; inword_idx < inword_bound; inword_idx++) { |
| 5571 |
if (!gzgets(cur_gzfile, g_textbuf, MAXLINELEN)) { |
| 5572 |
goto rel_cutoff_batch_ret_READ_FAIL; |
| 5573 |
} |
| 5574 |
if (row == col) { |
| 5575 |
row++; |
| 5576 |
col = 0; |
| 5577 |
if (!gzgets(cur_gzfile, g_textbuf, MAXLINELEN)) { |
| 5578 |
goto rel_cutoff_batch_ret_READ_FAIL; |
| 5579 |
} |
| 5580 |
} |
| 5581 |
bufptr = next_token_mult(g_textbuf, 3); |
| 5582 |
if (no_more_tokens_kns(bufptr)) { |
| 5583 |
goto rel_cutoff_batch_ret_INVALID_FORMAT_GENERIC; |
| 5584 |
} |
| 5585 |
if (scan_double(bufptr, &dxx)) { |
| 5586 |
goto rel_cutoff_batch_ret_INVALID_FORMAT_GENERIC; |
| 5587 |
} |
| 5588 |
if (dxx > rel_cutoff) { |
| 5589 |
rel_ct_arr[row] += 1; |
| 5590 |
rel_ct_arr[col] += 1; |
| 5591 |
cur_word |= (ONELU << inword_idx); |
| 5592 |
} |
| 5593 |
col++; |
| 5594 |
} |
| 5595 |
*rtptr++ = cur_word; |
| 5596 |
} |
| 5597 |
if (pct < 100) { |
| 5598 |
if (pct > 10) { |
| 5599 |
putc_unlocked('\b', stdout); |
| 5600 |
} |
| 5601 |
printf("\b\b%u%%", pct); |
| 5602 |
fflush(stdout); |
| 5603 |
} |
| 5604 |
} |
| 5605 |
if (!gzgets(cur_gzfile, g_textbuf, MAXLINELEN)) { |
| 5606 |
goto rel_cutoff_batch_ret_READ_FAIL; |
| 5607 |
} |
| 5608 |
if (gzgets(cur_gzfile, g_textbuf, MAXLINELEN)) { |
| 5609 |
goto rel_cutoff_batch_ret_INVALID_FORMAT_GENERIC; |
| 5610 |
} |
| 5611 |
gzclose(cur_gzfile); |
| 5612 |
cur_gzfile = nullptr; |
| 5613 |
} |
| 5614 |
putc_unlocked('\r', stdout); |
| 5615 |
LOGPRINTFWW("%s read complete. Pruning.\n", grmname); |
| 5616 |
|
| 5617 |
|
| 5618 |
|
| 5619 |
|
| 5620 |
|
| 5621 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 5622 |
if (rel_ct_arr[sample_idx] == 1) { |
| 5623 |
exactly_one_rel_ct++; |
| 5624 |
} |
| 5625 |
} |
| 5626 |
|
| 5627 |
while (1) { |
| 5628 |
kk = 0; |
| 5629 |
cur_prune = -1; |
| 5630 |
if (exactly_one_rel_ct) { |
| 5631 |
while (rel_ct_arr[kk] != 1) { |
| 5632 |
kk++; |
| 5633 |
} |
| 5634 |
ullii = (((int64_t)kk) * (kk - 1)) / 2; |
| 5635 |
ulii = ullii / BITCT; |
| 5636 |
rtptr = &(compact_rel_table[ulii]); |
| 5637 |
inword_idx = ullii & (BITCT - 1); |
| 5638 |
ulljj = ullii + kk; |
| 5639 |
uljj = ulljj / BITCT; |
| 5640 |
inword_bound = ulljj & (BITCT - 1); |
| 5641 |
if (uljj == ulii) { |
| 5642 |
uljj = (ONELU << inword_bound) - (ONELU << inword_idx); |
| 5643 |
ulkk = (*rtptr) & uljj; |
| 5644 |
if (ulkk) { |
| 5645 |
*rtptr &= ~uljj; |
| 5646 |
cur_prune = CTZLU(ulkk) - inword_idx; |
| 5647 |
} |
| 5648 |
} else { |
| 5649 |
ulkk = (*rtptr) & (~((ONELU << inword_idx) - ONELU)); |
| 5650 |
if (ulkk) { |
| 5651 |
*rtptr &= (ONELU << inword_idx) - ONELU; |
| 5652 |
cur_prune = CTZLU(ulkk) - inword_idx; |
| 5653 |
} else { |
| 5654 |
col = BITCT - inword_idx; |
| 5655 |
row = col + (uljj - ulii - 1) * BITCT; |
| 5656 |
while (col < row) { |
| 5657 |
ulkk = *(++rtptr); |
| 5658 |
if (ulkk) { |
| 5659 |
*rtptr = 0; |
| 5660 |
cur_prune = CTZLU(ulkk) + col; |
| 5661 |
break; |
| 5662 |
} |
| 5663 |
col += BITCT; |
| 5664 |
} |
| 5665 |
if (cur_prune == -1) { |
| 5666 |
ulkk = (*(++rtptr)) & ((ONELU << inword_bound) - ONELU); |
| 5667 |
if (ulkk) { |
| 5668 |
*rtptr &= (~((ONELU << inword_bound) - ONELU)); |
| 5669 |
cur_prune = CTZLU(ulkk) + col; |
| 5670 |
} |
| 5671 |
} |
| 5672 |
} |
| 5673 |
} |
| 5674 |
if (cur_prune == -1) { |
| 5675 |
mm = kk + 1; |
| 5676 |
while (1) { |
| 5677 |
ullii = ((((int64_t)mm) * (mm - 1)) / 2) + kk; |
| 5678 |
ulii = ullii / BITCT; |
| 5679 |
inword_idx = ullii & (BITCT - 1); |
| 5680 |
if (compact_rel_table[ulii] & (ONELU << inword_idx)) { |
| 5681 |
compact_rel_table[ulii] &= ~(ONELU << inword_idx); |
| 5682 |
cur_prune = mm; |
| 5683 |
break; |
| 5684 |
} |
| 5685 |
mm++; |
| 5686 |
} |
| 5687 |
} |
| 5688 |
rel_ct_arr[kk] = 0; |
| 5689 |
exactly_one_rel_ct--; |
| 5690 |
if (rel_ct_arr[cur_prune] == 1) { |
| 5691 |
exactly_one_rel_ct--; |
| 5692 |
rel_ct_arr[cur_prune] = -1; |
| 5693 |
samples_excluded++; |
| 5694 |
continue; |
| 5695 |
} |
| 5696 |
} else { |
| 5697 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 5698 |
if (rel_ct_arr[sample_idx] > kk) { |
| 5699 |
kk = rel_ct_arr[sample_idx]; |
| 5700 |
cur_prune = sample_idx; |
| 5701 |
} |
| 5702 |
} |
| 5703 |
if (cur_prune == -1) { |
| 5704 |
break; |
| 5705 |
} |
| 5706 |
} |
| 5707 |
|
| 5708 |
ullii = (((int64_t)cur_prune) * (cur_prune - 1)) / 2; |
| 5709 |
ulii = ullii / BITCT; |
| 5710 |
rtptr = &(compact_rel_table[ulii]); |
| 5711 |
inword_idx = ullii & (BITCT - 1); |
| 5712 |
ulljj = ullii + cur_prune; |
| 5713 |
uljj = ulljj / BITCT; |
| 5714 |
inword_bound = ulljj & (BITCT - 1); |
| 5715 |
if (uljj == ulii) { |
| 5716 |
uljj = (ONELU << inword_bound) - (ONELU << inword_idx); |
| 5717 |
ulkk = (*rtptr) & uljj; |
| 5718 |
if (ulkk) { |
| 5719 |
do { |
| 5720 |
uii = CTZLU(ulkk) - inword_idx; |
| 5721 |
rel_cut_arr_dec(&(rel_ct_arr[uii]), &exactly_one_rel_ct); |
| 5722 |
ulkk &= ulkk - 1; |
| 5723 |
} while (ulkk); |
| 5724 |
*rtptr &= ~uljj; |
| 5725 |
} |
| 5726 |
} else { |
| 5727 |
ulkk = (*rtptr) & (~((ONELU << inword_idx) - ONELU)); |
| 5728 |
if (ulkk) { |
| 5729 |
do { |
| 5730 |
uii = CTZLU(ulkk) - inword_idx; |
| 5731 |
rel_cut_arr_dec(&(rel_ct_arr[uii]), &exactly_one_rel_ct); |
| 5732 |
ulkk &= ulkk - 1; |
| 5733 |
} while (ulkk); |
| 5734 |
*rtptr &= (ONELU << inword_idx) - ONELU; |
| 5735 |
} |
| 5736 |
col = BITCT - inword_idx; |
| 5737 |
row = col + (uljj - ulii - 1) * BITCT; |
| 5738 |
while (col < row) { |
| 5739 |
ulkk = *(++rtptr); |
| 5740 |
if (ulkk) { |
| 5741 |
do { |
| 5742 |
uii = CTZLU(ulkk) + col; |
| 5743 |
rel_cut_arr_dec(&(rel_ct_arr[uii]), &exactly_one_rel_ct); |
| 5744 |
ulkk &= ulkk - 1; |
| 5745 |
} while (ulkk); |
| 5746 |
*rtptr = 0; |
| 5747 |
} |
| 5748 |
col += BITCT; |
| 5749 |
} |
| 5750 |
ulkk = (*(++rtptr)) & ((ONELU << inword_bound) - ONELU); |
| 5751 |
if (ulkk) { |
| 5752 |
do { |
| 5753 |
uii = CTZLU(ulkk) + col; |
| 5754 |
rel_cut_arr_dec(&(rel_ct_arr[uii]), &exactly_one_rel_ct); |
| 5755 |
ulkk &= ulkk - 1; |
| 5756 |
} while (ulkk); |
| 5757 |
*rtptr &= (~((ONELU << inword_bound) - ONELU)); |
| 5758 |
} |
| 5759 |
} |
| 5760 |
|
| 5761 |
for (uljj = cur_prune + 1; uljj < sample_ct; uljj++) { |
| 5762 |
ullii = ((((uint64_t)uljj) * (uljj - 1)) / 2) + cur_prune; |
| 5763 |
ulii = ullii / BITCT; |
| 5764 |
rtptr = &(compact_rel_table[ulii]); |
| 5765 |
inword_idx = ullii & (BITCT - 1); |
| 5766 |
if ((*rtptr) & (ONELU << inword_idx)) { |
| 5767 |
rel_cut_arr_dec(&(rel_ct_arr[uljj]), &exactly_one_rel_ct); |
| 5768 |
*rtptr &= ~(ONELU << inword_idx); |
| 5769 |
} |
| 5770 |
} |
| 5771 |
rel_ct_arr[cur_prune] = -1; |
| 5772 |
samples_excluded++; |
| 5773 |
} |
| 5774 |
|
| 5775 |
memcpy(grmname_end, ".grm.id", 8); |
| 5776 |
if (fopen_checked(grmname, "r", &idfile)) { |
| 5777 |
goto rel_cutoff_batch_ret_OPEN_FAIL; |
| 5778 |
} |
| 5779 |
|
| 5780 |
memcpy(outname_end, ".grm.id", 8); |
| 5781 |
if (fopen_checked(outname, "w", &outfile)) { |
| 5782 |
goto rel_cutoff_batch_ret_OPEN_FAIL; |
| 5783 |
} |
| 5784 |
|
| 5785 |
for (sample_idx = 0; sample_idx < sample_ct;) { |
| 5786 |
if (fgets(g_textbuf, MAXLINELEN, idfile) == nullptr) { |
| 5787 |
goto rel_cutoff_batch_ret_READ_FAIL; |
| 5788 |
} |
| 5789 |
if (is_eoln_kns(*(skip_initial_spaces(g_textbuf)))) { |
| 5790 |
continue; |
| 5791 |
} |
| 5792 |
if (rel_ct_arr[sample_idx] != -1) { |
| 5793 |
if (fputs_checked(g_textbuf, outfile)) { |
| 5794 |
goto rel_cutoff_batch_ret_WRITE_FAIL; |
| 5795 |
} |
| 5796 |
} |
| 5797 |
sample_idx++; |
| 5798 |
} |
| 5799 |
|
| 5800 |
fclose_null(&idfile); |
| 5801 |
fclose_null(&outfile); |
| 5802 |
|
| 5803 |
LOGPRINTF("%u %s excluded by --rel-cutoff.\n", samples_excluded, species_str(samples_excluded)); |
| 5804 |
LOGPRINTFWW("Remaining sample IDs written to %s .\n", outname); |
| 5805 |
if (rel_calc_type & (REL_CALC_GRM | REL_CALC_GRM_BIN)) { |
| 5806 |
if (load_grm_bin) { |
| 5807 |
memcpy(grmname_end, ".grm.bin", 9); |
| 5808 |
if (fopen_checked(grmname, FOPEN_RB, &in_binfile)) { |
| 5809 |
goto rel_cutoff_batch_ret_OPEN_FAIL; |
| 5810 |
} |
| 5811 |
g_rcb_in_binfile = in_binfile; |
| 5812 |
memcpy(grmname_end, ".grm.N.bin", 11); |
| 5813 |
if (fopen_checked(grmname, FOPEN_RB, &in_bin_nfile)) { |
| 5814 |
goto rel_cutoff_batch_ret_OPEN_FAIL; |
| 5815 |
} |
| 5816 |
g_rcb_in_bin_nfile = in_bin_nfile; |
| 5817 |
} else { |
| 5818 |
memcpy(grmname_end, ".grm.gz", 8); |
| 5819 |
retval = gzopen_read_checked(grmname, &cur_gzfile); |
| 5820 |
if (retval) { |
| 5821 |
goto rel_cutoff_batch_ret_1; |
| 5822 |
} |
| 5823 |
g_rcb_cur_gzfile = cur_gzfile; |
| 5824 |
} |
| 5825 |
fputs("Rewriting matrix... 0%", stdout); |
| 5826 |
fflush(stdout); |
| 5827 |
if (rel_calc_type & REL_CALC_GRM) { |
| 5828 |
g_pct = 1; |
| 5829 |
g_rcb_row = 0; |
| 5830 |
g_rcb_col = 0; |
| 5831 |
g_rcb_new_row = 1; |
| 5832 |
g_rcb_new_col = 0; |
| 5833 |
g_rcb_progress = 0; |
| 5834 |
g_rcb_hundredth = 1 + ((((uint64_t)sample_ct) * (sample_ct - 1)) / 200); |
| 5835 |
g_rcb_sample_ct = sample_ct; |
| 5836 |
g_rcb_rel_ct_arr = rel_ct_arr; |
| 5837 |
if (load_grm_bin) { |
| 5838 |
if (rel_calc_type & REL_CALC_GZ) { |
| 5839 |
memcpy(outname_end, ".grm.gz", 8); |
| 5840 |
parallel_compress(outname, overflow_buf, 0, rel_cutoff_batch_rbin_emitn); |
| 5841 |
} else { |
| 5842 |
memcpy(outname_end, ".grm", 5); |
| 5843 |
retval = write_uncompressed(outname, overflow_buf, 0, rel_cutoff_batch_rbin_emitn); |
| 5844 |
if (retval) { |
| 5845 |
goto rel_cutoff_batch_ret_1; |
| 5846 |
} |
| 5847 |
} |
| 5848 |
} else { |
| 5849 |
if (rel_calc_type & REL_CALC_GZ) { |
| 5850 |
memcpy(outname_end, ".grm.gz", 8); |
| 5851 |
parallel_compress(outname, overflow_buf, 0, rel_cutoff_batch_emitn); |
| 5852 |
} else { |
| 5853 |
memcpy(outname_end, ".grm", 5); |
| 5854 |
retval = write_uncompressed(outname, overflow_buf, 0, rel_cutoff_batch_emitn); |
| 5855 |
if (retval) { |
| 5856 |
goto rel_cutoff_batch_ret_1; |
| 5857 |
} |
| 5858 |
} |
| 5859 |
} |
| 5860 |
} else { |
| 5861 |
pct = 1; |
| 5862 |
row = 0; |
| 5863 |
col = 0; |
| 5864 |
progress = 0; |
| 5865 |
hundredth = 1 + ((((uint64_t)sample_ct) * (sample_ct - 1)) / 200); |
| 5866 |
memcpy(outname_end, ".grm.N.bin", 11); |
| 5867 |
if (fopen_checked(outname, FOPEN_WB, &out_bin_nfile)) { |
| 5868 |
goto rel_cutoff_batch_ret_OPEN_FAIL; |
| 5869 |
} |
| 5870 |
memcpy(outname_end, ".grm.bin", 9); |
| 5871 |
if (fopen_checked(outname, FOPEN_WB, &outfile)) { |
| 5872 |
goto rel_cutoff_batch_ret_OPEN_FAIL; |
| 5873 |
} |
| 5874 |
while (row < sample_ct) { |
| 5875 |
if (rel_ct_arr[row] == -1) { |
| 5876 |
if (load_grm_bin) { |
| 5877 |
fseeko(in_binfile, (row + 1) * sizeof(float), SEEK_CUR); |
| 5878 |
fseeko(in_bin_nfile, (row + 1) * sizeof(float), SEEK_CUR); |
| 5879 |
} else { |
| 5880 |
for (uii = 0; uii <= row; uii++) { |
| 5881 |
gzgets(cur_gzfile, g_textbuf, MAXLINELEN); |
| 5882 |
} |
| 5883 |
} |
| 5884 |
} else { |
| 5885 |
if (load_grm_bin) { |
| 5886 |
while (col <= row) { |
| 5887 |
if (rel_ct_arr[col] == -1) { |
| 5888 |
uii = col; |
| 5889 |
while (++col <= row) { |
| 5890 |
if (rel_ct_arr[col] != -1) { |
| 5891 |
break; |
| 5892 |
} |
| 5893 |
} |
| 5894 |
fseeko(in_bin_nfile, (col - uii) * sizeof(float), SEEK_CUR); |
| 5895 |
fseeko(in_bin_nfile, (col - uii) * sizeof(float), SEEK_CUR); |
| 5896 |
if (col > row) { |
| 5897 |
break; |
| 5898 |
} |
| 5899 |
} |
| 5900 |
if ((fread(&fxx, sizeof(float), 1, in_bin_nfile) != sizeof(float)) || (fread(&fyy, sizeof(float), 1, in_binfile) != sizeof(float))) { |
| 5901 |
goto rel_cutoff_batch_ret_READ_FAIL; |
| 5902 |
} |
| 5903 |
fwrite(&fxx, 4, 1, out_bin_nfile); |
| 5904 |
fwrite(&fyy, 4, 1, outfile); |
| 5905 |
col++; |
| 5906 |
} |
| 5907 |
} else { |
| 5908 |
while (col <= row) { |
| 5909 |
gzgets(cur_gzfile, g_textbuf, MAXLINELEN); |
| 5910 |
if (rel_ct_arr[col++] != -1) { |
| 5911 |
bufptr = next_token_mult(g_textbuf, 2); |
| 5912 |
if (scan_float(bufptr, &fxx)) { |
| 5913 |
goto rel_cutoff_batch_ret_INVALID_FORMAT_GENERIC; |
| 5914 |
} |
| 5915 |
bufptr = next_token(bufptr); |
| 5916 |
if (scan_float(bufptr, &fyy)) { |
| 5917 |
goto rel_cutoff_batch_ret_INVALID_FORMAT_GENERIC; |
| 5918 |
} |
| 5919 |
fwrite(&fxx, 4, 1, out_bin_nfile); |
| 5920 |
fwrite(&fyy, 4, 1, outfile); |
| 5921 |
} |
| 5922 |
} |
| 5923 |
} |
| 5924 |
} |
| 5925 |
row++; |
| 5926 |
progress += row; |
| 5927 |
if (progress >= pct * hundredth) { |
| 5928 |
if (pct > 10) { |
| 5929 |
putc_unlocked('\b', stdout); |
| 5930 |
} |
| 5931 |
pct = 1 + (progress / hundredth); |
| 5932 |
printf("\b\b%u%%", pct - 1); |
| 5933 |
fflush(stdout); |
| 5934 |
} |
| 5935 |
col = 0; |
| 5936 |
} |
| 5937 |
} |
| 5938 |
putc_unlocked('\r', stdout); |
| 5939 |
LOGPRINTFWW("Pruned relationship matrix written to %s .\n", outname); |
| 5940 |
} |
| 5941 |
retval = 0; |
| 5942 |
while (0) { |
| 5943 |
rel_cutoff_batch_ret_NOMEM: |
| 5944 |
retval = RET_NOMEM; |
| 5945 |
break; |
| 5946 |
rel_cutoff_batch_ret_OPEN_FAIL: |
| 5947 |
retval = RET_OPEN_FAIL; |
| 5948 |
break; |
| 5949 |
rel_cutoff_batch_ret_READ_FAIL: |
| 5950 |
retval = RET_READ_FAIL; |
| 5951 |
break; |
| 5952 |
rel_cutoff_batch_ret_WRITE_FAIL: |
| 5953 |
retval = RET_WRITE_FAIL; |
| 5954 |
break; |
| 5955 |
rel_cutoff_batch_ret_INVALID_FORMAT_GENERIC: |
| 5956 |
logprint("\n"); |
| 5957 |
logerrprint("Error: Improperly formatted .grm.gz file.\n"); |
| 5958 |
retval = RET_INVALID_FORMAT; |
| 5959 |
break; |
| 5960 |
rel_cutoff_batch_ret_INVALID_FORMAT_2: |
| 5961 |
logerrprintb(); |
| 5962 |
retval = RET_INVALID_FORMAT; |
| 5963 |
break; |
| 5964 |
rel_cutoff_batch_ret_INVALID_CMDLINE: |
| 5965 |
retval = RET_INVALID_CMDLINE; |
| 5966 |
break; |
| 5967 |
} |
| 5968 |
rel_cutoff_batch_ret_1: |
| 5969 |
fclose_cond(idfile); |
| 5970 |
fclose_cond(in_binfile); |
| 5971 |
fclose_cond(in_bin_nfile); |
| 5972 |
fclose_cond(outfile); |
| 5973 |
fclose_cond(out_bin_nfile); |
| 5974 |
gzclose_cond(cur_gzfile); |
| 5975 |
bigstack_reset(bigstack_mark); |
| 5976 |
return retval; |
| 5977 |
} |
| 5978 |
|
| 5979 |
static uint32_t g_cr_marker_ct; |
| 5980 |
static uintptr_t g_cr_sample1idx; |
| 5981 |
static uintptr_t g_cr_sample2idx; |
| 5982 |
static uintptr_t g_cr_min_sample; |
| 5983 |
static uintptr_t g_cr_max_sample1idx; |
| 5984 |
static uint64_t g_cr_start_offset; |
| 5985 |
static uint64_t g_cr_hundredth; |
| 5986 |
static uint32_t* g_cr_mdeptr; |
| 5987 |
static double* g_cr_dist_ptr; |
| 5988 |
static double* g_cr_ibc_ptr; |
| 5989 |
|
| 5990 |
uint32_t calc_rel_tri_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 5991 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 5992 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 5993 |
uint64_t start_offset = g_cr_start_offset; |
| 5994 |
uint64_t hundredth = g_cr_hundredth; |
| 5995 |
double* dist_ptr = g_cr_dist_ptr; |
| 5996 |
double* ibc_ptr = g_cr_ibc_ptr; |
| 5997 |
uintptr_t max_sample1idx = g_cr_max_sample1idx; |
| 5998 |
uintptr_t sample1idx = g_cr_sample1idx; |
| 5999 |
uintptr_t sample2idx = g_cr_sample2idx; |
| 6000 |
uint32_t pct = g_pct; |
| 6001 |
while (sample1idx < max_sample1idx) { |
| 6002 |
while (sample2idx < sample1idx) { |
| 6003 |
sptr_cur = dtoa_gx(*dist_ptr++, '\t', sptr_cur); |
| 6004 |
sample2idx++; |
| 6005 |
if (sptr_cur >= readbuf_end) { |
| 6006 |
goto calc_rel_tri_emitn_ret; |
| 6007 |
} |
| 6008 |
} |
| 6009 |
sptr_cur = dtoa_gx(*ibc_ptr++, '\n', sptr_cur); |
| 6010 |
sample1idx++; |
| 6011 |
if ((((uint64_t)sample1idx) * (sample1idx + 1) / 2 - start_offset) >= hundredth * pct) { |
| 6012 |
pct = (((uint64_t)sample1idx) * (sample1idx + 1) / 2 - start_offset) / hundredth; |
| 6013 |
printf("\rWriting... %u%%", pct++); |
| 6014 |
fflush(stdout); |
| 6015 |
} |
| 6016 |
sample2idx = 0; |
| 6017 |
} |
| 6018 |
calc_rel_tri_emitn_ret: |
| 6019 |
g_cr_sample1idx = sample1idx; |
| 6020 |
g_cr_sample2idx = sample2idx; |
| 6021 |
g_cr_dist_ptr = dist_ptr; |
| 6022 |
g_cr_ibc_ptr = ibc_ptr; |
| 6023 |
g_pct = pct; |
| 6024 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 6025 |
} |
| 6026 |
|
| 6027 |
uint32_t calc_rel_sq0_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 6028 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 6029 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 6030 |
unsigned char* zbuf = g_geno; |
| 6031 |
double* dist_ptr = g_cr_dist_ptr; |
| 6032 |
double* ibc_ptr = g_cr_ibc_ptr; |
| 6033 |
uintptr_t sample_ct = g_sample_ct; |
| 6034 |
uintptr_t max_sample1idx = g_cr_max_sample1idx; |
| 6035 |
uintptr_t min_sample = g_cr_min_sample; |
| 6036 |
uintptr_t sample1idx = g_cr_sample1idx; |
| 6037 |
uintptr_t sample2idx = g_cr_sample2idx; |
| 6038 |
uint32_t pct = g_pct; |
| 6039 |
uintptr_t ulii; |
| 6040 |
while (sample1idx < max_sample1idx) { |
| 6041 |
while (sample2idx < sample1idx) { |
| 6042 |
sptr_cur = dtoa_gx(*dist_ptr++, '\t', sptr_cur); |
| 6043 |
sample2idx++; |
| 6044 |
if (sptr_cur >= readbuf_end) { |
| 6045 |
goto calc_rel_sq0_emitn_ret; |
| 6046 |
} |
| 6047 |
} |
| 6048 |
if (sample2idx == sample1idx) { |
| 6049 |
sptr_cur = dtoa_g(*ibc_ptr++, sptr_cur); |
| 6050 |
sample2idx++; |
| 6051 |
} |
| 6052 |
if (sptr_cur >= readbuf_end) { |
| 6053 |
goto calc_rel_sq0_emitn_ret; |
| 6054 |
} else { |
| 6055 |
ulii = (1 + (uintptr_t)(readbuf_end - sptr_cur)) / 2; |
| 6056 |
if (ulii < (sample_ct - sample2idx)) { |
| 6057 |
sptr_cur = memcpya(sptr_cur, zbuf, 2 * ulii); |
| 6058 |
sample2idx += ulii; |
| 6059 |
goto calc_rel_sq0_emitn_ret; |
| 6060 |
} |
| 6061 |
ulii = 2 * (sample_ct - sample2idx); |
| 6062 |
sptr_cur = memcpya(sptr_cur, zbuf, ulii); |
| 6063 |
} |
| 6064 |
*sptr_cur++ = '\n'; |
| 6065 |
sample1idx++; |
| 6066 |
if ((sample1idx - min_sample) * 100LLU >= ((uint64_t)pct) * (max_sample1idx - min_sample)) { |
| 6067 |
pct = ((sample1idx - min_sample) * 100LLU) / (max_sample1idx - min_sample); |
| 6068 |
printf("\rWriting... %u%%", pct++); |
| 6069 |
fflush(stdout); |
| 6070 |
} |
| 6071 |
sample2idx = 0; |
| 6072 |
} |
| 6073 |
calc_rel_sq0_emitn_ret: |
| 6074 |
g_cr_sample1idx = sample1idx; |
| 6075 |
g_cr_sample2idx = sample2idx; |
| 6076 |
g_cr_dist_ptr = dist_ptr; |
| 6077 |
g_cr_ibc_ptr = ibc_ptr; |
| 6078 |
g_pct = pct; |
| 6079 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 6080 |
} |
| 6081 |
|
| 6082 |
uint32_t calc_rel_sq_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 6083 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 6084 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 6085 |
double* rel_dists = g_rel_dists; |
| 6086 |
double* dist_ptr = g_cr_dist_ptr; |
| 6087 |
double* ibc_ptr = g_cr_ibc_ptr; |
| 6088 |
uintptr_t sample_ct = g_sample_ct; |
| 6089 |
uintptr_t max_sample1idx = g_cr_max_sample1idx; |
| 6090 |
uintptr_t min_sample = g_cr_min_sample; |
| 6091 |
uintptr_t sample1idx = g_cr_sample1idx; |
| 6092 |
uintptr_t sample2idx = g_cr_sample2idx; |
| 6093 |
uint32_t pct = g_pct; |
| 6094 |
while (sample1idx < max_sample1idx) { |
| 6095 |
while (sample2idx < sample1idx) { |
| 6096 |
sptr_cur = dtoa_gx(*dist_ptr++, '\t', sptr_cur); |
| 6097 |
sample2idx++; |
| 6098 |
if (sptr_cur >= readbuf_end) { |
| 6099 |
goto calc_rel_sq_emitn_ret; |
| 6100 |
} |
| 6101 |
} |
| 6102 |
if (sample2idx == sample1idx) { |
| 6103 |
sptr_cur = dtoa_g(*ibc_ptr++, sptr_cur); |
| 6104 |
sample2idx++; |
| 6105 |
} |
| 6106 |
while (sample2idx < sample_ct) { |
| 6107 |
*sptr_cur++ = '\t'; |
| 6108 |
sptr_cur = dtoa_g(rel_dists[tri_coord_no_diag(sample1idx, sample2idx)], sptr_cur); |
| 6109 |
sample2idx++; |
| 6110 |
if (sptr_cur >= readbuf_end) { |
| 6111 |
goto calc_rel_sq_emitn_ret; |
| 6112 |
} |
| 6113 |
} |
| 6114 |
*sptr_cur++ = '\n'; |
| 6115 |
sample1idx++; |
| 6116 |
if ((sample1idx - min_sample) * 100LLU >= ((uint64_t)pct) * (max_sample1idx - min_sample)) { |
| 6117 |
pct = ((sample1idx - min_sample) * 100LLU) / (max_sample1idx - min_sample); |
| 6118 |
printf("\rWriting... %u%%", pct++); |
| 6119 |
fflush(stdout); |
| 6120 |
} |
| 6121 |
sample2idx = 0; |
| 6122 |
} |
| 6123 |
calc_rel_sq_emitn_ret: |
| 6124 |
g_cr_sample1idx = sample1idx; |
| 6125 |
g_cr_sample2idx = sample2idx; |
| 6126 |
g_cr_dist_ptr = dist_ptr; |
| 6127 |
g_cr_ibc_ptr = ibc_ptr; |
| 6128 |
g_pct = pct; |
| 6129 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 6130 |
} |
| 6131 |
|
| 6132 |
uint32_t calc_rel_grm_emitn(uint32_t overflow_ct, unsigned char* readbuf) { |
| 6133 |
char* sptr_cur = (char*)(&(readbuf[overflow_ct])); |
| 6134 |
char* readbuf_end = (char*)(&(readbuf[PIGZ_BLOCK_SIZE])); |
| 6135 |
uint64_t start_offset = g_cr_start_offset; |
| 6136 |
uint64_t hundredth = g_cr_hundredth; |
| 6137 |
uint32_t* sample_missing_unwt = g_sample_missing_unwt; |
| 6138 |
uint32_t* mdeptr = g_cr_mdeptr; |
| 6139 |
double* dist_ptr = g_cr_dist_ptr; |
| 6140 |
double* ibc_ptr = g_cr_ibc_ptr; |
| 6141 |
uintptr_t max_sample1idx = g_cr_max_sample1idx; |
| 6142 |
uintptr_t sample1idx = g_cr_sample1idx; |
| 6143 |
uintptr_t sample2idx = g_cr_sample2idx; |
| 6144 |
uint32_t marker_ct = g_cr_marker_ct; |
| 6145 |
uint32_t pct = g_pct; |
| 6146 |
char wbuf[16]; |
| 6147 |
char* wbuf_end; |
| 6148 |
uint32_t wbuf_len; |
| 6149 |
uint32_t uii; |
| 6150 |
while (sample1idx < max_sample1idx) { |
| 6151 |
uii = marker_ct - sample_missing_unwt[sample1idx]; |
| 6152 |
wbuf_end = uint32toa_x(sample1idx + 1, '\t', wbuf); |
| 6153 |
wbuf_len = (uintptr_t)(wbuf_end - wbuf); |
| 6154 |
while (sample2idx < sample1idx) { |
| 6155 |
sptr_cur = memcpya(sptr_cur, wbuf, wbuf_len); |
| 6156 |
sptr_cur = uint32toa_x(sample2idx + 1, '\t', sptr_cur); |
| 6157 |
sptr_cur = uint32toa_x((uii - sample_missing_unwt[sample2idx]) + (*mdeptr++), '\t', sptr_cur); |
| 6158 |
sptr_cur = dtoa_ex(*dist_ptr++, '\n', sptr_cur); |
| 6159 |
sample2idx++; |
| 6160 |
if (sptr_cur >= readbuf_end) { |
| 6161 |
goto calc_rel_grm_emitn_ret; |
| 6162 |
} |
| 6163 |
} |
| 6164 |
sptr_cur = memcpya(sptr_cur, wbuf, wbuf_len); |
| 6165 |
sptr_cur = uint32toa_x(++sample1idx, '\t', sptr_cur); |
| 6166 |
sptr_cur = uint32toa_x(uii, '\t', sptr_cur); |
| 6167 |
sptr_cur = dtoa_ex(*ibc_ptr++, '\n', sptr_cur); |
| 6168 |
if ((((uint64_t)sample1idx) * (sample1idx + 1) / 2 - start_offset) >= hundredth * pct) { |
| 6169 |
pct = (((uint64_t)sample1idx) * (sample1idx + 1) / 2 - start_offset) / hundredth; |
| 6170 |
printf("\rWriting... %u%%", pct++); |
| 6171 |
fflush(stdout); |
| 6172 |
} |
| 6173 |
sample2idx = 0; |
| 6174 |
} |
| 6175 |
calc_rel_grm_emitn_ret: |
| 6176 |
g_cr_sample1idx = sample1idx; |
| 6177 |
g_cr_sample2idx = sample2idx; |
| 6178 |
g_cr_dist_ptr = dist_ptr; |
| 6179 |
g_cr_ibc_ptr = ibc_ptr; |
| 6180 |
g_cr_mdeptr = mdeptr; |
| 6181 |
g_pct = pct; |
| 6182 |
return (uintptr_t)(((unsigned char*)sptr_cur) - readbuf); |
| 6183 |
} |
| 6184 |
|
| 6185 |
uint32_t block_load(FILE* bedfile, int32_t bed_offset, uintptr_t* marker_exclude, uint32_t marker_ct, uint32_t block_max_size, uintptr_t unfiltered_sample_ct4, unsigned char* readbuf, uintptr_t* marker_uidx_ptr, uintptr_t* marker_idx_ptr, uint32_t* block_size_ptr) { |
| 6186 |
uintptr_t marker_uidx = *marker_uidx_ptr; |
| 6187 |
uintptr_t marker_idx = *marker_idx_ptr; |
| 6188 |
uint32_t markers_read = 0; |
| 6189 |
if (block_max_size > marker_ct - marker_idx) { |
| 6190 |
block_max_size = marker_ct - marker_idx; |
| 6191 |
} |
| 6192 |
while (markers_read < block_max_size) { |
| 6193 |
if (IS_SET(marker_exclude, marker_uidx)) { |
| 6194 |
marker_uidx = next_unset_ul_unsafe(marker_exclude, marker_uidx); |
| 6195 |
if (fseeko(bedfile, bed_offset + ((uint64_t)marker_uidx) * unfiltered_sample_ct4, SEEK_SET)) { |
| 6196 |
return RET_READ_FAIL; |
| 6197 |
} |
| 6198 |
} |
| 6199 |
if (fread(&(readbuf[markers_read * unfiltered_sample_ct4]), 1, unfiltered_sample_ct4, bedfile) < unfiltered_sample_ct4) { |
| 6200 |
return RET_READ_FAIL; |
| 6201 |
} |
| 6202 |
markers_read++; |
| 6203 |
marker_idx++; |
| 6204 |
marker_uidx++; |
| 6205 |
} |
| 6206 |
|
| 6207 |
*marker_uidx_ptr = marker_uidx; |
| 6208 |
*marker_idx_ptr = marker_idx; |
| 6209 |
*block_size_ptr = markers_read; |
| 6210 |
return 0; |
| 6211 |
} |
| 6212 |
|
| 6213 |
void copy_set_allele_freqs(uintptr_t marker_uidx, uintptr_t* marker_exclude, uint32_t block_max_size, uintptr_t marker_idx, uint32_t marker_ct, uintptr_t* marker_reverse, double* set_allele_freqs, double* set_allele_freq_buf) { |
| 6214 |
uint32_t markers_read = 0; |
| 6215 |
if (block_max_size > marker_ct - marker_idx) { |
| 6216 |
block_max_size = marker_ct - marker_idx; |
| 6217 |
} |
| 6218 |
while (markers_read < block_max_size) { |
| 6219 |
next_unset_ul_unsafe_ck(marker_exclude, &marker_uidx); |
| 6220 |
if ((!marker_reverse) || (!IS_SET(marker_reverse, marker_uidx))) { |
| 6221 |
set_allele_freq_buf[markers_read] = set_allele_freqs[marker_uidx]; |
| 6222 |
} else { |
| 6223 |
set_allele_freq_buf[markers_read] = 1.0 - set_allele_freqs[marker_uidx]; |
| 6224 |
} |
| 6225 |
markers_read++; |
| 6226 |
marker_idx++; |
| 6227 |
marker_uidx++; |
| 6228 |
} |
| 6229 |
} |
| 6230 |
|
| 6231 |
int32_t load_distance_wts(char* distance_wts_fname, uintptr_t unfiltered_marker_ct, char* marker_ids, uintptr_t max_marker_id_len, uint32_t noheader, uint32_t conditional_alloc_exclude, uintptr_t** marker_exclude_ptr, uint32_t* marker_ct_ptr, double** main_weights_ptr) { |
| 6232 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 6233 |
unsigned char* bigstack_end_mark = g_bigstack_end; |
| 6234 |
FILE* infile = nullptr; |
| 6235 |
uintptr_t unfiltered_marker_ctl = BITCT_TO_WORDCT(unfiltered_marker_ct); |
| 6236 |
uintptr_t line_idx = 0; |
| 6237 |
|
| 6238 |
|
| 6239 |
|
| 6240 |
uint32_t zcount = 0; |
| 6241 |
|
| 6242 |
int32_t retval = 0; |
| 6243 |
uintptr_t* marker_include; |
| 6244 |
double* main_weights_tmp; |
| 6245 |
double* dptr; |
| 6246 |
char* bufptr; |
| 6247 |
uint32_t* marker_id_htable; |
| 6248 |
double dxx; |
| 6249 |
uint32_t marker_id_htable_size; |
| 6250 |
uint32_t marker_uidx; |
| 6251 |
uint32_t marker_idx; |
| 6252 |
uint32_t idlen; |
| 6253 |
uint32_t marker_ct; |
| 6254 |
if (bigstack_end_calloc_ul(unfiltered_marker_ctl, &marker_include) || |
| 6255 |
bigstack_end_alloc_d(unfiltered_marker_ct, &main_weights_tmp)) { |
| 6256 |
goto load_distance_wts_ret_NOMEM; |
| 6257 |
} |
| 6258 |
retval = alloc_and_populate_id_htable(unfiltered_marker_ct, *marker_exclude_ptr, *marker_ct_ptr, marker_ids, max_marker_id_len, 0, &marker_id_htable_size, &marker_id_htable); |
| 6259 |
if (retval) { |
| 6260 |
goto load_distance_wts_ret_1; |
| 6261 |
} |
| 6262 |
if (fopen_checked(distance_wts_fname, "r", &infile)) { |
| 6263 |
goto load_distance_wts_ret_OPEN_FAIL; |
| 6264 |
} |
| 6265 |
g_textbuf[MAXLINELEN - 1] = ' '; |
| 6266 |
while (fgets(g_textbuf, MAXLINELEN, infile)) { |
| 6267 |
line_idx++; |
| 6268 |
if (!g_textbuf[MAXLINELEN - 1]) { |
| 6269 |
LOGPREPRINTFWW("Error: Line %" PRIuPTR " of %s is pathologically long.\n", line_idx, distance_wts_fname); |
| 6270 |
goto load_distance_wts_ret_INVALID_FORMAT_2; |
| 6271 |
} |
| 6272 |
bufptr = skip_initial_spaces(g_textbuf); |
| 6273 |
if (is_eoln_kns(*bufptr)) { |
| 6274 |
continue; |
| 6275 |
} |
| 6276 |
if (!noheader) { |
| 6277 |
noheader = 1; |
| 6278 |
continue; |
| 6279 |
} |
| 6280 |
|
| 6281 |
idlen = strlen_se(bufptr); |
| 6282 |
marker_uidx = id_htable_find(bufptr, idlen, marker_id_htable, marker_id_htable_size, marker_ids, max_marker_id_len); |
| 6283 |
if (marker_uidx == 0xffffffffU) { |
| 6284 |
continue; |
| 6285 |
} |
| 6286 |
if (is_set(marker_include, marker_uidx)) { |
| 6287 |
bufptr[idlen] = '\0'; |
| 6288 |
LOGPREPRINTFWW("Error: Duplicate variant ID '%s' in --distance-wts file.\n", bufptr); |
| 6289 |
goto load_distance_wts_ret_INVALID_FORMAT_2; |
| 6290 |
} |
| 6291 |
set_bit(marker_uidx, marker_include); |
| 6292 |
bufptr = skip_initial_spaces(&(bufptr[idlen])); |
| 6293 |
if (is_eoln_kns(*bufptr)) { |
| 6294 |
snprintf(g_logbuf, LOGBUFLEN, "Error: Line %" PRIuPTR " of --distance-wts file has fewer tokens than expected.\n", line_idx); |
| 6295 |
goto load_distance_wts_ret_INVALID_FORMAT_2; |
| 6296 |
} |
| 6297 |
if (scan_double(bufptr, &dxx)) { |
| 6298 |
goto load_distance_wts_ret_INVALID_WEIGHT; |
| 6299 |
} |
| 6300 |
if (!((dxx >= 0.0) && (dxx != INFINITY))) { |
| 6301 |
goto load_distance_wts_ret_INVALID_WEIGHT; |
| 6302 |
} |
| 6303 |
if (dxx == 0.0) { |
| 6304 |
zcount++; |
| 6305 |
} |
| 6306 |
main_weights_tmp[marker_uidx] = dxx; |
| 6307 |
} |
| 6308 |
if (!feof(infile)) { |
| 6309 |
goto load_distance_wts_ret_READ_FAIL; |
| 6310 |
} |
| 6311 |
bigstack_reset(bigstack_mark); |
| 6312 |
marker_ct = popcount_longs(marker_include, unfiltered_marker_ctl) - zcount; |
| 6313 |
if (!marker_ct) { |
| 6314 |
logerrprint("Error: No valid nonzero entries in --distance-wts file.\n"); |
| 6315 |
goto load_distance_wts_ret_INVALID_FORMAT; |
| 6316 |
} |
| 6317 |
if ((marker_ct != (*marker_ct_ptr))) { |
| 6318 |
if (conditional_alloc_exclude) { |
| 6319 |
if (bigstack_alloc_ul(unfiltered_marker_ctl, marker_exclude_ptr)) { |
| 6320 |
goto load_distance_wts_ret_NOMEM; |
| 6321 |
} |
| 6322 |
} |
| 6323 |
bitarr_invert_copy(marker_include, unfiltered_marker_ct, *marker_exclude_ptr); |
| 6324 |
*marker_ct_ptr = marker_ct; |
| 6325 |
} |
| 6326 |
if (bigstack_alloc_d(marker_ct, main_weights_ptr)) { |
| 6327 |
goto load_distance_wts_ret_NOMEM; |
| 6328 |
} |
| 6329 |
dptr = *main_weights_ptr; |
| 6330 |
*marker_ct_ptr = marker_ct; |
| 6331 |
for (marker_uidx = 0, marker_idx = 0; marker_idx < marker_ct; marker_uidx++) { |
| 6332 |
next_set_unsafe_ck(marker_include, &marker_uidx); |
| 6333 |
dxx = main_weights_tmp[marker_uidx]; |
| 6334 |
if (dxx != 0.0) { |
| 6335 |
*dptr++ = dxx; |
| 6336 |
marker_idx++; |
| 6337 |
} |
| 6338 |
} |
| 6339 |
while (0) { |
| 6340 |
load_distance_wts_ret_NOMEM: |
| 6341 |
retval = RET_NOMEM; |
| 6342 |
break; |
| 6343 |
load_distance_wts_ret_OPEN_FAIL: |
| 6344 |
retval = RET_OPEN_FAIL; |
| 6345 |
break; |
| 6346 |
load_distance_wts_ret_READ_FAIL: |
| 6347 |
retval = RET_READ_FAIL; |
| 6348 |
break; |
| 6349 |
load_distance_wts_ret_INVALID_WEIGHT: |
| 6350 |
snprintf(g_logbuf, LOGBUFLEN, "Error: Invalid weight on line %" PRIuPTR " of --distance-wts file.\n", line_idx); |
| 6351 |
load_distance_wts_ret_INVALID_FORMAT_2: |
| 6352 |
logerrprintb(); |
| 6353 |
load_distance_wts_ret_INVALID_FORMAT: |
| 6354 |
retval = RET_INVALID_FORMAT; |
| 6355 |
break; |
| 6356 |
} |
| 6357 |
load_distance_wts_ret_1: |
| 6358 |
bigstack_end_reset(bigstack_end_mark); |
| 6359 |
fclose_cond(infile); |
| 6360 |
return retval; |
| 6361 |
} |
| 6362 |
|
| 6363 |
int32_t calc_rel(pthread_t* threads, uint32_t parallel_idx, uint32_t parallel_tot, uint64_t calculation_type, Rel_info* relip, FILE* bedfile, uintptr_t bed_offset, char* outname, char* outname_end, char* distance_wts_fname, uint32_t distance_wts_noheader, uintptr_t unfiltered_marker_ct, uintptr_t* marker_exclude_orig, uintptr_t* marker_reverse, uint32_t marker_ct, char* marker_ids, uintptr_t max_marker_id_len, uintptr_t unfiltered_sample_ct, uintptr_t* sample_exclude, uintptr_t* sample_exclude_ct_ptr, char* sample_ids, uintptr_t max_sample_id_len, double* set_allele_freqs, double** rel_ibc_ptr, Chrom_info* chrom_info_ptr) { |
| 6364 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 6365 |
uintptr_t unfiltered_sample_ct4 = (unfiltered_sample_ct + 3) / 4; |
| 6366 |
uintptr_t sample_ct = unfiltered_sample_ct - (*sample_exclude_ct_ptr); |
| 6367 |
uintptr_t marker_uidx = 0; |
| 6368 |
uintptr_t marker_idx = 0; |
| 6369 |
FILE* outfile = nullptr; |
| 6370 |
FILE* out_bin_nfile = nullptr; |
| 6371 |
uintptr_t* marker_exclude = marker_exclude_orig; |
| 6372 |
uint32_t rel_calc_type = relip->modifier & REL_CALC_MASK; |
| 6373 |
int32_t ibc_type = relip->ibc_type; |
| 6374 |
int32_t retval = 0; |
| 6375 |
uint32_t dist_thread_ct = g_thread_ct; |
| 6376 |
uint32_t rel_req = relationship_req(calculation_type); |
| 6377 |
uint32_t all_missing_warning = 0; |
| 6378 |
int64_t llxx = 0; |
| 6379 |
double rel_cutoff = relip->cutoff; |
| 6380 |
double* dist_ptr = nullptr; |
| 6381 |
double* dptr3 = nullptr; |
| 6382 |
double* dptr4 = nullptr; |
| 6383 |
double* rel_dists = nullptr; |
| 6384 |
double* main_weights = nullptr; |
| 6385 |
double* main_weights_ptr = nullptr; |
| 6386 |
double* dptr2; |
| 6387 |
double set_allele_freq_buf[MULTIPLEX_DIST]; |
| 6388 |
uint64_t start_offset; |
| 6389 |
uint64_t hundredth; |
| 6390 |
unsigned char* overflow_buf; |
| 6391 |
char* wptr; |
| 6392 |
char* fam_id; |
| 6393 |
char* sample_id; |
| 6394 |
uintptr_t* geno; |
| 6395 |
uintptr_t* masks; |
| 6396 |
uintptr_t* mmasks; |
| 6397 |
double* subset_weights; |
| 6398 |
double* rel_ibc; |
| 6399 |
uint32_t* mdeptr; |
| 6400 |
uint32_t* sample_missing_unwt; |
| 6401 |
uint32_t cur_markers_loaded; |
| 6402 |
uint32_t win_marker_idx; |
| 6403 |
uint32_t is_last_block; |
| 6404 |
uintptr_t sample_uidx; |
| 6405 |
uintptr_t sample_idx; |
| 6406 |
uintptr_t ulii; |
| 6407 |
uintptr_t uljj; |
| 6408 |
float fxx; |
| 6409 |
uint32_t uii; |
| 6410 |
uint32_t ujj; |
| 6411 |
uint32_t ukk; |
| 6412 |
uint32_t umm; |
| 6413 |
uint32_t unn; |
| 6414 |
uint32_t rel_shape; |
| 6415 |
uint32_t min_sample; |
| 6416 |
uint32_t max_parallel_sample; |
| 6417 |
uint32_t pct; |
| 6418 |
unsigned char* gptr; |
| 6419 |
unsigned char* gptr2; |
| 6420 |
uint32_t* giptr; |
| 6421 |
uint32_t* giptr2; |
| 6422 |
uintptr_t* glptr2; |
| 6423 |
if (distance_wts_fname) { |
| 6424 |
logerrprint("Error: --make-{rel,grm-gz,grm-bin} + --distance-wts is currently under\ndevelopment.\n"); |
| 6425 |
goto calc_rel_ret_1; |
| 6426 |
} |
| 6427 |
|
| 6428 |
|
| 6429 |
|
| 6430 |
if (dist_thread_ct > sample_ct / 64) { |
| 6431 |
dist_thread_ct = sample_ct / 64; |
| 6432 |
if (!dist_thread_ct) { |
| 6433 |
dist_thread_ct = 1; |
| 6434 |
} |
| 6435 |
} |
| 6436 |
|
| 6437 |
if (bigstack_calloc_ui(sample_ct, &sample_missing_unwt)) { |
| 6438 |
goto calc_rel_ret_NOMEM; |
| 6439 |
} |
| 6440 |
g_sample_missing_unwt = sample_missing_unwt; |
| 6441 |
g_sample_ct = sample_ct; |
| 6442 |
if (dist_thread_ct > sample_ct / 2) { |
| 6443 |
dist_thread_ct = sample_ct / 2; |
| 6444 |
} |
| 6445 |
triangle_fill(sample_ct, dist_thread_ct, parallel_idx, parallel_tot, 1, 1, g_thread_start); |
| 6446 |
if (calculation_type & CALC_IBC) { |
| 6447 |
uii = sample_ct * 3; |
| 6448 |
} else { |
| 6449 |
uii = sample_ct; |
| 6450 |
} |
| 6451 |
if (bigstack_calloc_d(uii, rel_ibc_ptr)) { |
| 6452 |
goto calc_rel_ret_NOMEM; |
| 6453 |
} |
| 6454 |
rel_ibc = *rel_ibc_ptr; |
| 6455 |
if (rel_req) { |
| 6456 |
llxx = g_thread_start[dist_thread_ct]; |
| 6457 |
llxx = ((llxx * (llxx - 1)) - (int64_t)g_thread_start[0] * (g_thread_start[0] - 1)) / 2; |
| 6458 |
if (!(calculation_type & (CALC_PCA | CALC_UNRELATED_HERITABILITY))) { |
| 6459 |
|
| 6460 |
|
| 6461 |
|
| 6462 |
|
| 6463 |
if (bigstack_calloc_ui(llxx, &g_missing_dbl_excluded)) { |
| 6464 |
goto calc_rel_ret_NOMEM; |
| 6465 |
} |
| 6466 |
} |
| 6467 |
if (bigstack_calloc_d(llxx, &rel_dists)) { |
| 6468 |
goto calc_rel_ret_NOMEM; |
| 6469 |
} |
| 6470 |
g_rel_dists = rel_dists; |
| 6471 |
} |
| 6472 |
bigstack_mark = g_bigstack_base; |
| 6473 |
|
| 6474 |
if (rel_req && (!g_missing_dbl_excluded)) { |
| 6475 |
if (bigstack_calloc_ui(llxx, &g_missing_dbl_excluded)) { |
| 6476 |
goto calc_rel_ret_NOMEM; |
| 6477 |
} |
| 6478 |
} |
| 6479 |
if (fseeko(bedfile, bed_offset, SEEK_SET)) { |
| 6480 |
goto calc_rel_ret_READ_FAIL; |
| 6481 |
} |
| 6482 |
if (bigstack_alloc_ul(sample_ct, &geno) || |
| 6483 |
bigstack_alloc_ul(sample_ct, &mmasks) || |
| 6484 |
bigstack_alloc_uc(MULTIPLEX_REL * unfiltered_sample_ct4, &gptr) || |
| 6485 |
bigstack_alloc_ul(sample_ct, &masks) || |
| 6486 |
bigstack_alloc_d(2048 * BITCT, &subset_weights) || |
| 6487 |
bigstack_alloc_uc(262144, &overflow_buf)) { |
| 6488 |
goto calc_rel_ret_NOMEM; |
| 6489 |
} |
| 6490 |
g_geno = (unsigned char*)geno; |
| 6491 |
g_masks = masks; |
| 6492 |
g_mmasks = mmasks; |
| 6493 |
g_subset_weights = subset_weights; |
| 6494 |
|
| 6495 |
|
| 6496 |
retval = conditional_allocate_non_autosomal_markers(chrom_info_ptr, unfiltered_marker_ct, marker_exclude_orig, marker_ct, 1, 1, "relationship matrix calc", &marker_exclude, &uii); |
| 6497 |
if (retval) { |
| 6498 |
goto calc_rel_ret_1; |
| 6499 |
} |
| 6500 |
marker_ct -= uii; |
| 6501 |
|
| 6502 |
if (distance_wts_fname) { |
| 6503 |
retval = load_distance_wts(distance_wts_fname, unfiltered_marker_ct, marker_ids, max_marker_id_len, distance_wts_noheader, (marker_exclude == marker_exclude_orig), &marker_exclude, &marker_ct, &main_weights); |
| 6504 |
if (retval) { |
| 6505 |
goto calc_rel_ret_1; |
| 6506 |
} |
| 6507 |
} |
| 6508 |
|
| 6509 |
|
| 6510 |
|
| 6511 |
|
| 6512 |
|
| 6513 |
|
| 6514 |
do { |
| 6515 |
copy_set_allele_freqs(marker_uidx, marker_exclude, MULTIPLEX_REL, marker_idx, marker_ct, marker_reverse, set_allele_freqs, set_allele_freq_buf); |
| 6516 |
if (main_weights) { |
| 6517 |
main_weights_ptr = &(main_weights[marker_idx]); |
| 6518 |
} |
| 6519 |
retval = block_load(bedfile, bed_offset, marker_exclude, marker_ct, MULTIPLEX_REL, unfiltered_sample_ct4, gptr, &marker_uidx, &marker_idx, &cur_markers_loaded); |
| 6520 |
if (retval) { |
| 6521 |
goto calc_rel_ret_1; |
| 6522 |
} |
| 6523 |
if (cur_markers_loaded < MULTIPLEX_REL) { |
| 6524 |
memset(&(gptr[cur_markers_loaded * unfiltered_sample_ct4]), 0, (MULTIPLEX_REL - cur_markers_loaded) * unfiltered_sample_ct4); |
| 6525 |
fill_double_zero(MULTIPLEX_REL - cur_markers_loaded, &(set_allele_freq_buf[cur_markers_loaded])); |
| 6526 |
} |
| 6527 |
fill_ulong_zero(sample_ct, mmasks); |
| 6528 |
|
| 6529 |
is_last_block = (marker_idx == marker_ct); |
| 6530 |
for (win_marker_idx = 0; win_marker_idx < cur_markers_loaded; win_marker_idx += MULTIPLEX_REL / 3) { |
| 6531 |
fill_ulong_zero(sample_ct, masks); |
| 6532 |
sample_idx = 0; |
| 6533 |
glptr2 = geno; |
| 6534 |
for (sample_uidx = 0; sample_idx < sample_ct; sample_uidx++, sample_idx++) { |
| 6535 |
next_unset_ul_unsafe_ck(sample_exclude, &sample_uidx); |
| 6536 |
ulii = 0; |
| 6537 |
gptr2 = &(gptr[sample_uidx / 4 + win_marker_idx * unfiltered_sample_ct4]); |
| 6538 |
uii = (sample_uidx % 4) * 2; |
| 6539 |
umm = 0; |
| 6540 |
unn = 0; |
| 6541 |
for (ukk = 0; ukk < (BITCT / 16); ukk++) { |
| 6542 |
for (ujj = 0; ujj < 5; ujj++) { |
| 6543 |
uljj = (gptr2[umm * unfiltered_sample_ct4] >> uii) & 3; |
| 6544 |
if (uljj == 1) { |
| 6545 |
masks[sample_idx] |= (7 * ONELU) << unn; |
| 6546 |
mmasks[sample_idx] |= ONELU << (win_marker_idx + umm); |
| 6547 |
sample_missing_unwt[sample_idx] += 1; |
| 6548 |
} |
| 6549 |
ulii |= uljj << unn; |
| 6550 |
umm++; |
| 6551 |
unn += 3; |
| 6552 |
} |
| 6553 |
unn++; |
| 6554 |
} |
| 6555 |
*glptr2++ = ulii; |
| 6556 |
} |
| 6557 |
ujj = is_last_block && (cur_markers_loaded - win_marker_idx <= MULTIPLEX_REL / 3); |
| 6558 |
if (!ujj) { |
| 6559 |
ukk = MULTIPLEX_REL / 3; |
| 6560 |
} else { |
| 6561 |
ukk = cur_markers_loaded - win_marker_idx; |
| 6562 |
} |
| 6563 |
if (calculation_type & CALC_IBC) { |
| 6564 |
for (uii = 0; uii < 3; uii++) { |
| 6565 |
update_rel_ibc(&(rel_ibc[uii * sample_ct]), geno, &(set_allele_freq_buf[win_marker_idx]), main_weights_ptr? (&(main_weights_ptr[win_marker_idx])) : nullptr, uii, sample_ct, ukk); |
| 6566 |
} |
| 6567 |
} else { |
| 6568 |
update_rel_ibc(rel_ibc, geno, &(set_allele_freq_buf[win_marker_idx]), main_weights_ptr? (&(main_weights_ptr[win_marker_idx])) : nullptr, ibc_type, sample_ct, ukk); |
| 6569 |
} |
| 6570 |
if (rel_req) { |
| 6571 |
fill_subset_weights_r(subset_weights, &(set_allele_freq_buf[win_marker_idx]), main_weights_ptr? (&(main_weights_ptr[win_marker_idx])) : nullptr, (ibc_type != -1)); |
| 6572 |
ulii = 0; |
| 6573 |
if (!main_weights_ptr) { |
| 6574 |
if (spawn_threads2(threads, &calc_rel_thread, dist_thread_ct, ujj)) { |
| 6575 |
goto calc_rel_ret_THREAD_CREATE_FAIL; |
| 6576 |
} |
| 6577 |
calc_rel_thread((void*)ulii); |
| 6578 |
} else { |
| 6579 |
if (spawn_threads2(threads, &calc_wt_rel_thread, dist_thread_ct, ujj)) { |
| 6580 |
goto calc_rel_ret_THREAD_CREATE_FAIL; |
| 6581 |
} |
| 6582 |
calc_wt_rel_thread((void*)ulii); |
| 6583 |
} |
| 6584 |
join_threads2(threads, dist_thread_ct, ujj); |
| 6585 |
} |
| 6586 |
} |
| 6587 |
printf("\r%" PRIuPTR " markers complete.", marker_idx); |
| 6588 |
fflush(stdout); |
| 6589 |
} while (!is_last_block); |
| 6590 |
if (rel_req) { |
| 6591 |
putc_unlocked('\r', stdout); |
| 6592 |
logprint("Relationship matrix calculation complete.\n"); |
| 6593 |
dist_ptr = rel_dists; |
| 6594 |
} else { |
| 6595 |
putc_unlocked('\n', stdout); |
| 6596 |
} |
| 6597 |
dptr2 = rel_ibc; |
| 6598 |
if (calculation_type & CALC_IBC) { |
| 6599 |
dptr3 = &(rel_ibc[sample_ct]); |
| 6600 |
dptr4 = &(rel_ibc[sample_ct * 2]); |
| 6601 |
} |
| 6602 |
giptr2 = g_missing_dbl_excluded; |
| 6603 |
min_sample = g_thread_start[0]; |
| 6604 |
max_parallel_sample = g_thread_start[dist_thread_ct]; |
| 6605 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 6606 |
uii = marker_ct - sample_missing_unwt[sample_idx]; |
| 6607 |
if ((!all_missing_warning) && (!uii)) { |
| 6608 |
all_missing_warning = 1; |
| 6609 |
} |
| 6610 |
if ((sample_idx >= min_sample) && (sample_idx < max_parallel_sample)) { |
| 6611 |
if (rel_req) { |
| 6612 |
giptr = sample_missing_unwt; |
| 6613 |
for (ujj = 0; ujj < sample_idx; ujj++) { |
| 6614 |
*dist_ptr /= uii - (*giptr++) + (*giptr2++); |
| 6615 |
dist_ptr++; |
| 6616 |
} |
| 6617 |
} |
| 6618 |
} |
| 6619 |
if (calculation_type & CALC_IBC) { |
| 6620 |
*dptr2 /= uii; |
| 6621 |
dptr2++; |
| 6622 |
*dptr3 /= uii; |
| 6623 |
dptr3++; |
| 6624 |
*dptr4 /= uii; |
| 6625 |
dptr4++; |
| 6626 |
} else { |
| 6627 |
*dptr2 /= uii; |
| 6628 |
dptr2++; |
| 6629 |
} |
| 6630 |
} |
| 6631 |
if (calculation_type & CALC_REL_CUTOFF) { |
| 6632 |
retval = do_rel_cutoff(calculation_type, rel_cutoff, rel_ibc, sample_exclude, sample_exclude_ct_ptr, outname, outname_end, unfiltered_sample_ct, sample_ids, max_sample_id_len); |
| 6633 |
if (retval) { |
| 6634 |
goto calc_rel_ret_1; |
| 6635 |
} |
| 6636 |
sample_ct = unfiltered_sample_ct - *sample_exclude_ct_ptr; |
| 6637 |
g_sample_ct = sample_ct; |
| 6638 |
} |
| 6639 |
|
| 6640 |
if (calculation_type & CALC_IBC) { |
| 6641 |
strcpy(outname_end, ".ibc"); |
| 6642 |
if (fopen_checked(outname, "w", &outfile)) { |
| 6643 |
goto calc_rel_ret_OPEN_FAIL; |
| 6644 |
} |
| 6645 |
dptr2 = rel_ibc; |
| 6646 |
dptr3 = &(rel_ibc[sample_ct]); |
| 6647 |
dptr4 = &(rel_ibc[sample_ct * 2]); |
| 6648 |
if (fputs_checked("FID\tIID\tNOMISS\tFhat1\tFhat2\tFhat3\n", outfile)) { |
| 6649 |
goto calc_rel_ret_WRITE_FAIL; |
| 6650 |
} |
| 6651 |
char* textbuf = g_textbuf; |
| 6652 |
sample_uidx = 0; |
| 6653 |
for (sample_idx = 0; sample_idx < sample_ct; sample_uidx++, sample_idx++) { |
| 6654 |
next_unset_ul_unsafe_ck(sample_exclude, &sample_uidx); |
| 6655 |
fam_id = &(sample_ids[sample_uidx * max_sample_id_len]); |
| 6656 |
sample_id = (char*)memchr(fam_id, '\t', max_sample_id_len); |
| 6657 |
wptr = memcpyax(textbuf, fam_id, (uintptr_t)(sample_id - fam_id), '\t'); |
| 6658 |
wptr = strcpyax(wptr, &(sample_id[1]), '\t'); |
| 6659 |
wptr = uint32toa_x(marker_ct - sample_missing_unwt[sample_idx], '\t', wptr); |
| 6660 |
wptr = dtoa_gx(*dptr3++ - 1.0, '\t', wptr); |
| 6661 |
wptr = dtoa_gx(*dptr4++ - 1.0, '\t', wptr); |
| 6662 |
wptr = dtoa_gx(*dptr2++ - 1.0, '\n', wptr); |
| 6663 |
|
| 6664 |
if (fwrite_checked(textbuf, wptr - textbuf, outfile)) { |
| 6665 |
goto calc_rel_ret_WRITE_FAIL; |
| 6666 |
} |
| 6667 |
} |
| 6668 |
if (fclose_null(&outfile)) { |
| 6669 |
goto calc_rel_ret_WRITE_FAIL; |
| 6670 |
} |
| 6671 |
LOGPRINTFWW("%s written.\n", outname); |
| 6672 |
} |
| 6673 |
if (calculation_type & CALC_RELATIONSHIP) { |
| 6674 |
rel_shape = rel_calc_type & REL_CALC_SHAPEMASK; |
| 6675 |
if (parallel_tot == 1) { |
| 6676 |
|
| 6677 |
max_parallel_sample = sample_ct; |
| 6678 |
} else { |
| 6679 |
|
| 6680 |
max_parallel_sample = g_thread_start[dist_thread_ct]; |
| 6681 |
} |
| 6682 |
min_sample = g_thread_start[0]; |
| 6683 |
if (min_sample == 1) { |
| 6684 |
min_sample = 0; |
| 6685 |
} |
| 6686 |
if (calculation_type & CALC_IBC) { |
| 6687 |
dptr2 = &(rel_ibc[((uint32_t)ibc_type) * sample_ct + min_sample]); |
| 6688 |
} else { |
| 6689 |
dptr2 = &(rel_ibc[min_sample]); |
| 6690 |
} |
| 6691 |
start_offset = ((uint64_t)min_sample * (min_sample - 1)) / 2; |
| 6692 |
hundredth = 1 + (((((uint64_t)max_parallel_sample * (max_parallel_sample + 1)) / 2) - start_offset) / 100); |
| 6693 |
if (rel_calc_type & REL_CALC_BIN) { |
| 6694 |
pct = 1; |
| 6695 |
if (rel_shape == REL_CALC_SQ0) { |
| 6696 |
fill_double_zero(sample_ct - 1, (double*)geno); |
| 6697 |
} |
| 6698 |
strcpy(outname_end, ".rel.bin"); |
| 6699 |
if (parallel_tot > 1) { |
| 6700 |
snprintf(&(outname_end[8]), MAX_POST_EXT - 8, ".%u", parallel_idx + 1); |
| 6701 |
} |
| 6702 |
if (fopen_checked(outname, FOPEN_WB, &outfile)) { |
| 6703 |
goto calc_rel_ret_OPEN_FAIL; |
| 6704 |
} |
| 6705 |
for (sample_idx = min_sample; sample_idx < max_parallel_sample; sample_idx++) { |
| 6706 |
if (fwrite_checked(&(rel_dists[((int64_t)sample_idx * (sample_idx - 1)) / 2 - start_offset]), sample_idx * sizeof(double), outfile)) { |
| 6707 |
goto calc_rel_ret_WRITE_FAIL; |
| 6708 |
} |
| 6709 |
if (fwrite_checked(dptr2++, sizeof(double), outfile)) { |
| 6710 |
goto calc_rel_ret_WRITE_FAIL; |
| 6711 |
} |
| 6712 |
if (rel_shape == REL_CALC_TRI) { |
| 6713 |
if ((((uint64_t)sample_idx + 1) * (sample_idx + 2) / 2 - start_offset) >= hundredth * pct) { |
| 6714 |
pct = (((uint64_t)sample_idx + 1) * (sample_idx + 2) / 2 - start_offset) / hundredth; |
| 6715 |
printf("\rWriting... %u%%", pct++); |
| 6716 |
fflush(stdout); |
| 6717 |
} |
| 6718 |
} else { |
| 6719 |
if (rel_shape == REL_CALC_SQ0) { |
| 6720 |
if (fwrite_checked(geno, (sample_ct - sample_idx - 1) * sizeof(double), outfile)) { |
| 6721 |
goto calc_rel_ret_WRITE_FAIL; |
| 6722 |
} |
| 6723 |
} else { |
| 6724 |
for (uii = sample_idx + 1; uii < sample_ct; uii++) { |
| 6725 |
if (fwrite_checked(&(rel_dists[((uintptr_t)uii * (uii - 1) / 2) + sample_idx - start_offset]), sizeof(double), outfile)) { |
| 6726 |
goto calc_rel_ret_WRITE_FAIL; |
| 6727 |
} |
| 6728 |
} |
| 6729 |
} |
| 6730 |
if ((sample_idx + 1 - min_sample) * 100 >= pct * (max_parallel_sample - min_sample)) { |
| 6731 |
pct = ((sample_idx + 1 - min_sample) * 100) / (max_parallel_sample - min_sample); |
| 6732 |
printf("\rWriting... %u%%", pct++); |
| 6733 |
fflush(stdout); |
| 6734 |
} |
| 6735 |
} |
| 6736 |
} |
| 6737 |
if (fclose_null(&outfile)) { |
| 6738 |
goto calc_rel_ret_WRITE_FAIL; |
| 6739 |
} |
| 6740 |
} else if (rel_calc_type & REL_CALC_GRM_BIN) { |
| 6741 |
pct = 1; |
| 6742 |
memcpy(outname_end, ".grm.N.bin", 11); |
| 6743 |
if (parallel_tot > 1) { |
| 6744 |
outname_end[10] = '.'; |
| 6745 |
uint32toa_x(parallel_idx + 1, '\0', &(outname_end[11])); |
| 6746 |
} |
| 6747 |
if (fopen_checked(outname, FOPEN_WB, &out_bin_nfile)) { |
| 6748 |
goto calc_rel_ret_OPEN_FAIL; |
| 6749 |
} |
| 6750 |
memcpy(outname_end, ".grm.bin", 9); |
| 6751 |
if (parallel_tot > 1) { |
| 6752 |
outname_end[8] = '.'; |
| 6753 |
uint32toa_x(parallel_idx + 1, '\0', &(outname_end[9])); |
| 6754 |
} |
| 6755 |
if (fopen_checked(outname, FOPEN_WB, &outfile)) { |
| 6756 |
goto calc_rel_ret_OPEN_FAIL; |
| 6757 |
} |
| 6758 |
mdeptr = g_missing_dbl_excluded; |
| 6759 |
for (sample_idx = min_sample; sample_idx < max_parallel_sample; sample_idx++) { |
| 6760 |
dptr3 = &(rel_dists[((int64_t)sample_idx * (sample_idx - 1)) / 2 - start_offset]); |
| 6761 |
for (ulii = 0; ulii < sample_idx; ulii++) { |
| 6762 |
fxx = (float)(*dptr3++); |
| 6763 |
fwrite(&fxx, sizeof(float), 1, outfile); |
| 6764 |
} |
| 6765 |
fxx = (float)(*dptr2++); |
| 6766 |
if (fwrite_checked(&fxx, sizeof(float), outfile)) { |
| 6767 |
goto calc_rel_ret_WRITE_FAIL; |
| 6768 |
} |
| 6769 |
uii = marker_ct - sample_missing_unwt[sample_idx]; |
| 6770 |
for (ujj = 0; ujj < sample_idx; ujj++) { |
| 6771 |
fxx = (float)((int32_t)(uii - sample_missing_unwt[ujj] + (*mdeptr++))); |
| 6772 |
fwrite(&fxx, sizeof(float), 1, out_bin_nfile); |
| 6773 |
} |
| 6774 |
fxx = (float)((int32_t)uii); |
| 6775 |
if (fwrite_checked(&fxx, sizeof(float), out_bin_nfile)) { |
| 6776 |
goto calc_rel_ret_WRITE_FAIL; |
| 6777 |
} |
| 6778 |
if ((((uint64_t)sample_idx + 1) * (sample_idx + 2) / 2 - start_offset) >= hundredth * pct) { |
| 6779 |
pct = (((uint64_t)sample_idx + 1) * (sample_idx + 2) / 2 - start_offset) / hundredth; |
| 6780 |
printf("\rWriting... %u%%", pct++); |
| 6781 |
fflush(stdout); |
| 6782 |
} |
| 6783 |
} |
| 6784 |
} else if (rel_calc_type & REL_CALC_BIN4) { |
| 6785 |
|
| 6786 |
pct = 1; |
| 6787 |
if (rel_shape == REL_CALC_SQ0) { |
| 6788 |
fill_float_zero(sample_ct - 1, (float*)geno); |
| 6789 |
} |
| 6790 |
|
| 6791 |
strcpy(outname_end, ".rel.bin"); |
| 6792 |
if (parallel_tot > 1) { |
| 6793 |
snprintf(&(outname_end[8]), MAX_POST_EXT - 8, ".%u", parallel_idx + 1); |
| 6794 |
} |
| 6795 |
if (fopen_checked(outname, FOPEN_WB, &outfile)) { |
| 6796 |
goto calc_rel_ret_OPEN_FAIL; |
| 6797 |
} |
| 6798 |
for (sample_idx = min_sample; sample_idx < max_parallel_sample; sample_idx++) { |
| 6799 |
dptr3 = &(rel_dists[((int64_t)sample_idx * (sample_idx - 1)) / 2 - start_offset]); |
| 6800 |
for (ulii = 0; ulii < sample_idx; ulii++) { |
| 6801 |
fxx = (float)(*dptr3++); |
| 6802 |
fwrite(&fxx, sizeof(float), 1, outfile); |
| 6803 |
} |
| 6804 |
fxx = (float)(*dptr2++); |
| 6805 |
if (fwrite_checked(&fxx, sizeof(float), outfile)) { |
| 6806 |
goto calc_rel_ret_WRITE_FAIL; |
| 6807 |
} |
| 6808 |
if (rel_shape == REL_CALC_TRI) { |
| 6809 |
if ((((uint64_t)sample_idx + 1) * (sample_idx + 2) / 2 - start_offset) >= hundredth * pct) { |
| 6810 |
pct = (((uint64_t)sample_idx + 1) * (sample_idx + 2) / 2 - start_offset) / hundredth; |
| 6811 |
printf("\rWriting... %u%%", pct++); |
| 6812 |
fflush(stdout); |
| 6813 |
} |
| 6814 |
} else { |
| 6815 |
if (rel_shape == REL_CALC_SQ0) { |
| 6816 |
if (fwrite_checked(geno, (sample_ct - sample_idx - 1) * sizeof(float), outfile)) { |
| 6817 |
goto calc_rel_ret_WRITE_FAIL; |
| 6818 |
} |
| 6819 |
} else { |
| 6820 |
for (ulii = sample_idx + 1; ulii < sample_ct; ulii++) { |
| 6821 |
fxx = (float)(rel_dists[(ulii * (ulii - 1) / 2) + sample_idx - start_offset]); |
| 6822 |
fwrite(&fxx, 4, 1, outfile); |
| 6823 |
} |
| 6824 |
} |
| 6825 |
if ((sample_idx + 1 - min_sample) * 100 >= pct * (max_parallel_sample - min_sample)) { |
| 6826 |
pct = ((sample_idx + 1 - min_sample) * 100) / (max_parallel_sample - min_sample); |
| 6827 |
printf("\rWriting... %u%%", pct++); |
| 6828 |
fflush(stdout); |
| 6829 |
} |
| 6830 |
} |
| 6831 |
} |
| 6832 |
if (fclose_null(&outfile)) { |
| 6833 |
goto calc_rel_ret_WRITE_FAIL; |
| 6834 |
} |
| 6835 |
} else { |
| 6836 |
g_cr_marker_ct = marker_ct; |
| 6837 |
g_pct = 1; |
| 6838 |
g_cr_start_offset = start_offset; |
| 6839 |
g_cr_hundredth = hundredth; |
| 6840 |
g_cr_sample1idx = min_sample; |
| 6841 |
g_cr_sample2idx = 0; |
| 6842 |
g_cr_max_sample1idx = max_parallel_sample; |
| 6843 |
g_cr_mdeptr = g_missing_dbl_excluded; |
| 6844 |
g_cr_dist_ptr = rel_dists; |
| 6845 |
g_cr_ibc_ptr = dptr2; |
| 6846 |
if (rel_calc_type & REL_CALC_GRM) { |
| 6847 |
if (rel_calc_type & REL_CALC_GZ) { |
| 6848 |
if (parallel_tot > 1) { |
| 6849 |
snprintf(outname_end, MAX_POST_EXT, ".grm.%u.gz", parallel_idx + 1); |
| 6850 |
} else { |
| 6851 |
strcpy(outname_end, ".grm.gz"); |
| 6852 |
} |
| 6853 |
parallel_compress(outname, overflow_buf, 0, calc_rel_grm_emitn); |
| 6854 |
} else { |
| 6855 |
strcpy(outname_end, ".grm"); |
| 6856 |
if (parallel_tot > 1) { |
| 6857 |
snprintf(&(outname_end[4]), MAX_POST_EXT - 4, ".%u", parallel_idx + 1); |
| 6858 |
} |
| 6859 |
retval = write_uncompressed(outname, overflow_buf, 0, calc_rel_grm_emitn); |
| 6860 |
if (retval) { |
| 6861 |
goto calc_rel_ret_1; |
| 6862 |
} |
| 6863 |
} |
| 6864 |
} else { |
| 6865 |
if (rel_calc_type & REL_CALC_GZ) { |
| 6866 |
if (parallel_tot > 1) { |
| 6867 |
snprintf(outname_end, MAX_POST_EXT, ".rel.%u.gz", parallel_idx + 1); |
| 6868 |
} else { |
| 6869 |
strcpy(outname_end, ".rel.gz"); |
| 6870 |
} |
| 6871 |
} else { |
| 6872 |
strcpy(outname_end, ".rel"); |
| 6873 |
if (parallel_tot > 1) { |
| 6874 |
snprintf(&(outname_end[4]), MAX_POST_EXT - 4, ".%u", parallel_idx + 1); |
| 6875 |
} |
| 6876 |
} |
| 6877 |
if (rel_shape == REL_CALC_TRI) { |
| 6878 |
if (rel_calc_type & REL_CALC_GZ) { |
| 6879 |
parallel_compress(outname, overflow_buf, 0, calc_rel_tri_emitn); |
| 6880 |
} else { |
| 6881 |
retval = write_uncompressed(outname, overflow_buf, 0, calc_rel_tri_emitn); |
| 6882 |
if (retval) { |
| 6883 |
goto calc_rel_ret_1; |
| 6884 |
} |
| 6885 |
} |
| 6886 |
} else if (rel_shape == REL_CALC_SQ0) { |
| 6887 |
|
| 6888 |
|
| 6889 |
|
| 6890 |
|
| 6891 |
|
| 6892 |
|
| 6893 |
#ifdef __LP64__ |
| 6894 |
ulii = 0x3009300930093009LLU; |
| 6895 |
#else |
| 6896 |
ulii = 0x30093009LU; |
| 6897 |
#endif |
| 6898 |
uii = (sample_ct * 2 + sizeof(intptr_t) - 4) / sizeof(intptr_t); |
| 6899 |
glptr2 = geno; |
| 6900 |
for (ujj = 0; ujj < uii; ujj++) { |
| 6901 |
*glptr2++ = ulii; |
| 6902 |
} |
| 6903 |
g_cr_min_sample = min_sample; |
| 6904 |
if (rel_calc_type & REL_CALC_GZ) { |
| 6905 |
parallel_compress(outname, overflow_buf, 0, calc_rel_sq0_emitn); |
| 6906 |
} else { |
| 6907 |
retval = write_uncompressed(outname, overflow_buf, 0, calc_rel_sq0_emitn); |
| 6908 |
if (retval) { |
| 6909 |
goto calc_rel_ret_1; |
| 6910 |
} |
| 6911 |
} |
| 6912 |
} else { |
| 6913 |
g_cr_min_sample = min_sample; |
| 6914 |
if (rel_calc_type & REL_CALC_GZ) { |
| 6915 |
parallel_compress(outname, overflow_buf, 0, calc_rel_sq_emitn); |
| 6916 |
} else { |
| 6917 |
retval = write_uncompressed(outname, overflow_buf, 0, calc_rel_sq_emitn); |
| 6918 |
if (retval) { |
| 6919 |
goto calc_rel_ret_1; |
| 6920 |
} |
| 6921 |
} |
| 6922 |
} |
| 6923 |
} |
| 6924 |
} |
| 6925 |
putc_unlocked('\r', stdout); |
| 6926 |
if (!parallel_idx) { |
| 6927 |
wptr = strcpya(g_logbuf, "Relationship matrix "); |
| 6928 |
if (parallel_tot > 1) { |
| 6929 |
wptr = strcpya(wptr, "component "); |
| 6930 |
} |
| 6931 |
wptr = strcpya(wptr, "written to "); |
| 6932 |
wptr = strcpya(wptr, outname); |
| 6933 |
strcpy(&(outname_end[4]), ".id"); |
| 6934 |
retval = write_ids(outname, unfiltered_sample_ct, sample_exclude, sample_ids, max_sample_id_len); |
| 6935 |
if (retval) { |
| 6936 |
goto calc_rel_ret_1; |
| 6937 |
} |
| 6938 |
snprintf(wptr, LOGBUFLEN - (wptr - g_logbuf), " , and IDs written to %s .\n", outname); |
| 6939 |
} else { |
| 6940 |
snprintf(g_logbuf, LOGBUFLEN, "Relationship matrix component written to %s .\n", outname); |
| 6941 |
} |
| 6942 |
wordwrapb(0); |
| 6943 |
logprintb(); |
| 6944 |
} |
| 6945 |
if (all_missing_warning) { |
| 6946 |
|
| 6947 |
|
| 6948 |
|
| 6949 |
if (calculation_type & (CALC_REGRESS_REL | CALC_PCA | CALC_UNRELATED_HERITABILITY)) { |
| 6950 |
logerrprint("Error: Sample(s) present with no genotype data. Use --mind to filter them out.\n"); |
| 6951 |
retval = RET_INVALID_FORMAT; |
| 6952 |
} else { |
| 6953 |
logerrprint("Warning: Sample(s) present with no genotype data. Use --mind to filter them\nout.\n"); |
| 6954 |
} |
| 6955 |
} |
| 6956 |
while (0) { |
| 6957 |
calc_rel_ret_NOMEM: |
| 6958 |
retval = RET_NOMEM; |
| 6959 |
break; |
| 6960 |
calc_rel_ret_OPEN_FAIL: |
| 6961 |
retval = RET_OPEN_FAIL; |
| 6962 |
break; |
| 6963 |
calc_rel_ret_READ_FAIL: |
| 6964 |
retval = RET_READ_FAIL; |
| 6965 |
break; |
| 6966 |
calc_rel_ret_WRITE_FAIL: |
| 6967 |
retval = RET_WRITE_FAIL; |
| 6968 |
break; |
| 6969 |
calc_rel_ret_THREAD_CREATE_FAIL: |
| 6970 |
retval = RET_THREAD_CREATE_FAIL; |
| 6971 |
break; |
| 6972 |
} |
| 6973 |
calc_rel_ret_1: |
| 6974 |
bigstack_reset(bigstack_mark); |
| 6975 |
fclose_cond(outfile); |
| 6976 |
fclose_cond(out_bin_nfile); |
| 6977 |
return retval; |
| 6978 |
} |
| 6979 |
|
| 6980 |
#ifndef NOLAPACK |
| 6981 |
int32_t calc_pca(FILE* bedfile, uintptr_t bed_offset, char* outname, char* outname_end, uint64_t calculation_type, Rel_info* relip, 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, uintptr_t unfiltered_sample_ct, uintptr_t* sample_exclude, uintptr_t sample_ct, uintptr_t* pca_sample_exclude, uintptr_t pca_sample_ct, char* sample_ids, uintptr_t max_sample_id_len, double* set_allele_freqs, Chrom_info* chrom_info_ptr, double* rel_ibc) { |
| 6982 |
|
| 6983 |
|
| 6984 |
|
| 6985 |
|
| 6986 |
FILE* outfile = nullptr; |
| 6987 |
uintptr_t unfiltered_sample_ct4 = (unfiltered_sample_ct + 3) / 4; |
| 6988 |
uintptr_t unfiltered_sample_ctl = BITCT_TO_WORDCT(unfiltered_sample_ct); |
| 6989 |
uintptr_t unfiltered_sample_ctl2 = QUATERCT_TO_WORDCT(unfiltered_sample_ct); |
| 6990 |
uintptr_t pca_sample_ctl2 = QUATERCT_TO_WORDCT(pca_sample_ct); |
| 6991 |
uintptr_t proj_sample_ct = sample_ct - pca_sample_ct; |
| 6992 |
uintptr_t proj_sample_ctl2 = QUATERCT_TO_WORDCT(proj_sample_ct); |
| 6993 |
uintptr_t final_mask = get_final_mask(pca_sample_ct); |
| 6994 |
double nz = 0.0; |
| 6995 |
double zz = -1.0; |
| 6996 |
uintptr_t* loadbuf_proj = nullptr; |
| 6997 |
uintptr_t* sample_exclude_proj = nullptr; |
| 6998 |
double* proj_sample_loadings = nullptr; |
| 6999 |
double* proj_allhom_wts = nullptr; |
| 7000 |
uint32_t* proj_missing_cts = nullptr; |
| 7001 |
uint32_t write_headers = relip->modifier & REL_PCA_HEADER; |
| 7002 |
uint32_t pc_ct = relip->pc_ct; |
| 7003 |
uint32_t var_wts = relip->modifier & REL_PCA_VAR_WTS; |
| 7004 |
uint32_t chrom_ct = chrom_info_ptr->chrom_ct; |
| 7005 |
int32_t ibc_type = relip->ibc_type; |
| 7006 |
int32_t mt_code = chrom_info_ptr->xymt_codes[MT_OFFSET]; |
| 7007 |
int32_t retval = 0; |
| 7008 |
__CLPK_integer info = 0; |
| 7009 |
__CLPK_integer lwork = -1; |
| 7010 |
__CLPK_integer liwork = -1; |
| 7011 |
char jobz = 'V'; |
| 7012 |
char range = 'I'; |
| 7013 |
char uplo = 'U'; |
| 7014 |
char delimiter = (relip->modifier & REL_PCA_TABS)? '\t' : ' '; |
| 7015 |
double var_wt_incr[4]; |
| 7016 |
double* rel_dists; |
| 7017 |
double* main_matrix; |
| 7018 |
double* out_w; |
| 7019 |
double* out_z; |
| 7020 |
double* work; |
| 7021 |
double* sample_loadings; |
| 7022 |
double* cur_var_wts; |
| 7023 |
double* pc_sums; |
| 7024 |
double* eigval_inv_sqrts; |
| 7025 |
double* dptr; |
| 7026 |
double* dptr2; |
| 7027 |
uintptr_t* loadbuf_raw; |
| 7028 |
uintptr_t* loadbuf; |
| 7029 |
uintptr_t* ulptr; |
| 7030 |
char* cptr; |
| 7031 |
char* wptr_start; |
| 7032 |
char* wptr; |
| 7033 |
double optim_lwork; |
| 7034 |
double dxx; |
| 7035 |
double dyy; |
| 7036 |
uintptr_t chrom_end; |
| 7037 |
uintptr_t marker_uidx; |
| 7038 |
uintptr_t sample_uidx; |
| 7039 |
uintptr_t sample_idx; |
| 7040 |
uintptr_t ulii; |
| 7041 |
__CLPK_integer mdim; |
| 7042 |
__CLPK_integer i1; |
| 7043 |
__CLPK_integer i2; |
| 7044 |
__CLPK_integer out_m; |
| 7045 |
__CLPK_integer ldz; |
| 7046 |
__CLPK_integer optim_liwork; |
| 7047 |
__CLPK_integer* iwork; |
| 7048 |
__CLPK_integer* isuppz; |
| 7049 |
uint32_t chrom_fo_idx; |
| 7050 |
uint32_t pc_idx; |
| 7051 |
uint32_t uii; |
| 7052 |
uint32_t ujj; |
| 7053 |
int32_t chrom_idx; |
| 7054 |
|
| 7055 |
g_missing_dbl_excluded = nullptr; |
| 7056 |
marker_ct -= count_non_autosomal_markers(chrom_info_ptr, marker_exclude, 1, 1); |
| 7057 |
if ((pc_ct > pca_sample_ct) || (pc_ct > marker_ct)) { |
| 7058 |
if (pca_sample_ct <= marker_ct) { |
| 7059 |
pc_ct = pca_sample_ct; |
| 7060 |
snprintf(g_logbuf, LOGBUFLEN, "Warning: calculating %u PCs, since there are only %u samples.\n", pc_ct, pc_ct); |
| 7061 |
} else { |
| 7062 |
pc_ct = marker_ct; |
| 7063 |
snprintf(g_logbuf, LOGBUFLEN, "Warning: calculating %u PCs, since there are only %u autosomal markers.\n", pc_ct, pc_ct); |
| 7064 |
} |
| 7065 |
logerrprintb(); |
| 7066 |
} |
| 7067 |
ulii = (pca_sample_ct * (pca_sample_ct + 1)) / 2; |
| 7068 |
if (ulii < sample_ct * pc_ct) { |
| 7069 |
ulii = sample_ct * pc_ct; |
| 7070 |
} |
| 7071 |
rel_dists = g_rel_dists; |
| 7072 |
bigstack_reset(rel_dists); |
| 7073 |
if (bigstack_alloc_d(ulii, &rel_dists) || |
| 7074 |
bigstack_alloc_d(pca_sample_ct * pca_sample_ct, &main_matrix)) { |
| 7075 |
goto calc_pca_ret_NOMEM; |
| 7076 |
} |
| 7077 |
ulii = ((pca_sample_ct - 1) * (pca_sample_ct - 2)) >> 1; |
| 7078 |
for (sample_idx = pca_sample_ct - 1; sample_idx;) { |
| 7079 |
memcpy(&(main_matrix[sample_idx * pca_sample_ct]), &(rel_dists[ulii]), sample_idx * sizeof(double)); |
| 7080 |
ulii -= (--sample_idx); |
| 7081 |
} |
| 7082 |
if (calculation_type & CALC_IBC) { |
| 7083 |
dptr = &(rel_ibc[ibc_type * pca_sample_ct]); |
| 7084 |
} else { |
| 7085 |
dptr = rel_ibc; |
| 7086 |
} |
| 7087 |
ulii = pca_sample_ct + 1; |
| 7088 |
for (sample_idx = 0; sample_idx < pca_sample_ct; sample_idx++) { |
| 7089 |
main_matrix[sample_idx * ulii] = *dptr++; |
| 7090 |
} |
| 7091 |
ulii = pca_sample_ct; |
| 7092 |
fputs("[extracting eigenvalues and eigenvectors]", stdout); |
| 7093 |
fflush(stdout); |
| 7094 |
mdim = ulii; |
| 7095 |
i2 = mdim; |
| 7096 |
i1 = i2 + 1 - pc_ct; |
| 7097 |
if (bigstack_calloc_d(pc_ct, &out_w) || |
| 7098 |
bigstack_calloc_d(pc_ct * ulii, &out_z)) { |
| 7099 |
goto calc_pca_ret_NOMEM; |
| 7100 |
} |
| 7101 |
isuppz = (__CLPK_integer*)bigstack_alloc(2 * pc_ct * sizeof(__CLPK_integer)); |
| 7102 |
if (!isuppz) { |
| 7103 |
goto calc_pca_ret_NOMEM; |
| 7104 |
} |
| 7105 |
fill_int_zero(2 * pc_ct * (sizeof(__CLPK_integer) / sizeof(int32_t)), (int32_t*)isuppz); |
| 7106 |
ldz = mdim; |
| 7107 |
|
| 7108 |
dsyevr_(&jobz, &range, &uplo, &mdim, main_matrix, &mdim, &nz, &nz, &i1, &i2, &zz, &out_m, out_w, out_z, &ldz, isuppz, &optim_lwork, &lwork, &optim_liwork, &liwork, &info); |
| 7109 |
lwork = (int32_t)optim_lwork; |
| 7110 |
if (bigstack_calloc_d(lwork, &work)) { |
| 7111 |
goto calc_pca_ret_NOMEM; |
| 7112 |
} |
| 7113 |
liwork = optim_liwork; |
| 7114 |
iwork = (__CLPK_integer*)bigstack_alloc(liwork * sizeof(__CLPK_integer)); |
| 7115 |
if (!iwork) { |
| 7116 |
goto calc_pca_ret_NOMEM; |
| 7117 |
} |
| 7118 |
fill_int_zero(liwork * (sizeof(__CLPK_integer) / sizeof(int32_t)), (int32_t*)iwork); |
| 7119 |
dsyevr_(&jobz, &range, &uplo, &mdim, main_matrix, &mdim, &nz, &nz, &i1, &i2, &zz, &out_m, out_w, out_z, &ldz, isuppz, work, &lwork, iwork, &liwork, &info); |
| 7120 |
if ((info != 0) || (out_w[0] != out_w[0])) { |
| 7121 |
logprint("\n"); |
| 7122 |
logerrprint("Error: Failed to extract eigenvector(s) from GRM.\n"); |
| 7123 |
goto calc_pca_ret_DEGENERATE_DATA; |
| 7124 |
} |
| 7125 |
|
| 7126 |
|
| 7127 |
|
| 7128 |
|
| 7129 |
sample_loadings = rel_dists; |
| 7130 |
dptr = sample_loadings; |
| 7131 |
|
| 7132 |
for (sample_idx = 0; sample_idx < pca_sample_ct; sample_idx++) { |
| 7133 |
dptr2 = &(out_z[sample_idx]); |
| 7134 |
for (pc_idx = 0; pc_idx < pc_ct; pc_idx++) { |
| 7135 |
*dptr++ = dptr2[pc_idx * pca_sample_ct]; |
| 7136 |
} |
| 7137 |
} |
| 7138 |
bigstack_reset(out_z); |
| 7139 |
if (var_wts || proj_sample_ct) { |
| 7140 |
if (bigstack_alloc_ul(unfiltered_sample_ctl2, &loadbuf_raw) || |
| 7141 |
bigstack_alloc_ul(pca_sample_ctl2, &loadbuf) || |
| 7142 |
bigstack_alloc_d(pc_ct, &cur_var_wts) || |
| 7143 |
bigstack_calloc_d(pc_ct, &pc_sums) || |
| 7144 |
bigstack_alloc_d(pc_ct, &eigval_inv_sqrts)) { |
| 7145 |
goto calc_pca_ret_NOMEM; |
| 7146 |
} |
| 7147 |
loadbuf_raw[unfiltered_sample_ctl2 - 1] = 0; |
| 7148 |
loadbuf[pca_sample_ctl2 - 1] = 0; |
| 7149 |
dptr = sample_loadings; |
| 7150 |
for (sample_idx = 0; sample_idx < pca_sample_ct; sample_idx++) { |
| 7151 |
for (pc_idx = 0; pc_idx < pc_ct; pc_idx++) { |
| 7152 |
pc_sums[pc_idx] += *dptr++; |
| 7153 |
} |
| 7154 |
} |
| 7155 |
for (pc_idx = 0; pc_idx < pc_ct; pc_idx++) { |
| 7156 |
eigval_inv_sqrts[pc_idx] = sqrt(1 / out_w[pc_idx]); |
| 7157 |
} |
| 7158 |
if (proj_sample_ct) { |
| 7159 |
if (bigstack_alloc_ul(unfiltered_sample_ctl, &sample_exclude_proj) || |
| 7160 |
bigstack_alloc_ul(proj_sample_ctl2, &loadbuf_proj) || |
| 7161 |
bigstack_calloc_d(proj_sample_ct * pc_ct, &proj_sample_loadings) || |
| 7162 |
bigstack_calloc_d(pc_ct, &proj_allhom_wts) || |
| 7163 |
bigstack_calloc_ui(proj_sample_ct, &proj_missing_cts)) { |
| 7164 |
goto calc_pca_ret_NOMEM; |
| 7165 |
} |
| 7166 |
memcpy(sample_exclude_proj, sample_exclude, unfiltered_sample_ctl * sizeof(intptr_t)); |
| 7167 |
bitvec_ornot(pca_sample_exclude, unfiltered_sample_ctl, sample_exclude_proj); |
| 7168 |
zero_trailing_bits(unfiltered_sample_ct, sample_exclude_proj); |
| 7169 |
loadbuf_proj[proj_sample_ctl2 - 1] = 0; |
| 7170 |
} |
| 7171 |
char* textbuf_mid = &(g_textbuf[MAXLINELEN]); |
| 7172 |
if (var_wts) { |
| 7173 |
memcpy(outname_end, ".eigenvec.var", 14); |
| 7174 |
if (fopen_checked(outname, "w", &outfile)) { |
| 7175 |
goto calc_pca_ret_OPEN_FAIL; |
| 7176 |
} |
| 7177 |
if (write_headers) { |
| 7178 |
wptr = memcpyl3a(g_textbuf, "CHR"); |
| 7179 |
*wptr++ = delimiter; |
| 7180 |
wptr = memcpyl3a(wptr, "VAR"); |
| 7181 |
|
| 7182 |
*wptr++ = delimiter; |
| 7183 |
wptr = strcpya(wptr, "A1"); |
| 7184 |
*wptr++ = delimiter; |
| 7185 |
wptr = strcpya(wptr, "A2"); |
| 7186 |
for (pc_idx = 1; pc_idx <= pc_ct; pc_idx++) { |
| 7187 |
*wptr++ = delimiter; |
| 7188 |
wptr = memcpya(wptr, "PC", 2); |
| 7189 |
wptr = uint32toa(pc_idx, wptr); |
| 7190 |
} |
| 7191 |
*wptr++ = '\n'; |
| 7192 |
if (fwrite_checked(g_textbuf, wptr - g_textbuf, outfile)) { |
| 7193 |
goto calc_pca_ret_WRITE_FAIL; |
| 7194 |
} |
| 7195 |
} |
| 7196 |
} |
| 7197 |
var_wt_incr[0] = 0; |
| 7198 |
for (chrom_fo_idx = 0; chrom_fo_idx < chrom_ct; chrom_fo_idx++) { |
| 7199 |
chrom_idx = chrom_info_ptr->chrom_file_order[chrom_fo_idx]; |
| 7200 |
if (is_set(chrom_info_ptr->haploid_mask, chrom_idx) || (chrom_idx == mt_code)) { |
| 7201 |
continue; |
| 7202 |
} |
| 7203 |
marker_uidx = chrom_info_ptr->chrom_fo_vidx_start[chrom_fo_idx]; |
| 7204 |
chrom_end = chrom_info_ptr->chrom_fo_vidx_start[chrom_fo_idx + 1]; |
| 7205 |
wptr_start = chrom_name_write(chrom_info_ptr, chrom_idx, g_textbuf); |
| 7206 |
*wptr_start++ = delimiter; |
| 7207 |
if (marker_uidx < chrom_end) { |
| 7208 |
if (fseeko(bedfile, bed_offset + ((uint64_t)marker_uidx) * unfiltered_sample_ct4, SEEK_SET)) { |
| 7209 |
goto calc_pca_ret_READ_FAIL; |
| 7210 |
} |
| 7211 |
do { |
| 7212 |
if (IS_SET(marker_exclude, marker_uidx)) { |
| 7213 |
marker_uidx = next_unset_ul(marker_exclude, marker_uidx, chrom_end); |
| 7214 |
if (marker_uidx == chrom_end) { |
| 7215 |
break; |
| 7216 |
} |
| 7217 |
if (fseeko(bedfile, bed_offset + ((uint64_t)marker_uidx) * unfiltered_sample_ct4, SEEK_SET)) { |
| 7218 |
goto calc_pca_ret_READ_FAIL; |
| 7219 |
} |
| 7220 |
} |
| 7221 |
|
| 7222 |
|
| 7223 |
|
| 7224 |
if (load_and_collapse(unfiltered_sample_ct, pca_sample_ct, pca_sample_exclude, final_mask, IS_SET(marker_reverse, marker_uidx), bedfile, loadbuf_raw, loadbuf)) { |
| 7225 |
goto calc_pca_ret_READ_FAIL; |
| 7226 |
} |
| 7227 |
|
| 7228 |
|
| 7229 |
|
| 7230 |
fill_double_zero(pc_ct, cur_var_wts); |
| 7231 |
dxx = set_allele_freqs[marker_uidx]; |
| 7232 |
dyy = sqrt(1 / (2 * dxx * (1.0 - dxx))); |
| 7233 |
ulptr = loadbuf; |
| 7234 |
|
| 7235 |
var_wt_incr[1] = dyy; |
| 7236 |
var_wt_incr[2] = 2 * (1.0 - dxx) * dyy; |
| 7237 |
var_wt_incr[3] = 2 * dyy; |
| 7238 |
sample_uidx = 0; |
| 7239 |
do { |
| 7240 |
ulii = ~(*ulptr++); |
| 7241 |
if (sample_uidx + BITCT2 > pca_sample_ct) { |
| 7242 |
ulii &= (ONELU << ((pca_sample_ct & (BITCT2 - 1)) * 2)) - ONELU; |
| 7243 |
} |
| 7244 |
while (ulii) { |
| 7245 |
uii = CTZLU(ulii) & (BITCT - 2); |
| 7246 |
ujj = (ulii >> uii) & 3; |
| 7247 |
sample_idx = sample_uidx + (uii / 2); |
| 7248 |
dxx = var_wt_incr[ujj]; |
| 7249 |
dptr = cur_var_wts; |
| 7250 |
dptr2 = &(sample_loadings[sample_idx * pc_ct]); |
| 7251 |
for (pc_idx = 0; pc_idx < pc_ct; pc_idx++) { |
| 7252 |
*dptr += dxx * (*dptr2++); |
| 7253 |
dptr++; |
| 7254 |
} |
| 7255 |
ulii &= ~((3 * ONELU) << uii); |
| 7256 |
} |
| 7257 |
sample_uidx += BITCT2; |
| 7258 |
} while (sample_uidx < pca_sample_ct); |
| 7259 |
for (pc_idx = 0; pc_idx < pc_ct; pc_idx++) { |
| 7260 |
cur_var_wts[pc_idx] -= pc_sums[pc_idx] * var_wt_incr[2]; |
| 7261 |
cur_var_wts[pc_idx] *= eigval_inv_sqrts[pc_idx]; |
| 7262 |
} |
| 7263 |
if (proj_sample_ct) { |
| 7264 |
copy_quaterarr_nonempty_subset_excl(loadbuf_raw, sample_exclude_proj, unfiltered_sample_ct, proj_sample_ct, loadbuf_proj); |
| 7265 |
if (IS_SET(marker_reverse, marker_uidx)) { |
| 7266 |
reverse_loadbuf(proj_sample_ct, (unsigned char*)loadbuf_proj); |
| 7267 |
} |
| 7268 |
ulptr = loadbuf_proj; |
| 7269 |
sample_uidx = 0; |
| 7270 |
do { |
| 7271 |
ulii = ~(*ulptr++); |
| 7272 |
if (sample_uidx + BITCT2 > proj_sample_ct) { |
| 7273 |
ulii &= (ONELU << ((proj_sample_ct & (BITCT2 - 1)) * 2)) - ONELU; |
| 7274 |
} |
| 7275 |
while (ulii) { |
| 7276 |
uii = CTZLU(ulii) & (BITCT - 2); |
| 7277 |
ujj = (ulii >> uii) & 3; |
| 7278 |
sample_idx = sample_uidx + (uii / 2); |
| 7279 |
dxx = var_wt_incr[ujj]; |
| 7280 |
dptr = &(proj_sample_loadings[sample_idx * pc_ct]); |
| 7281 |
dptr2 = cur_var_wts; |
| 7282 |
for (pc_idx = 0; pc_idx < pc_ct; pc_idx++) { |
| 7283 |
*dptr += dxx * cur_var_wts[pc_idx]; |
| 7284 |
dptr++; |
| 7285 |
} |
| 7286 |
if (ujj == 2) { |
| 7287 |
proj_missing_cts[sample_idx] += 1; |
| 7288 |
} |
| 7289 |
ulii &= ~((3 * ONELU) << uii); |
| 7290 |
} |
| 7291 |
sample_uidx += BITCT2; |
| 7292 |
} while (sample_uidx < proj_sample_ct); |
| 7293 |
dptr = proj_allhom_wts; |
| 7294 |
dxx = var_wt_incr[2]; |
| 7295 |
for (pc_idx = 0; pc_idx < pc_ct; pc_idx++) { |
| 7296 |
*dptr += dxx * cur_var_wts[pc_idx]; |
| 7297 |
dptr++; |
| 7298 |
} |
| 7299 |
} |
| 7300 |
if (var_wts) { |
| 7301 |
wptr = strcpya(wptr_start, &(marker_ids[marker_uidx * max_marker_id_len])); |
| 7302 |
*wptr++ = delimiter; |
| 7303 |
if (fwrite_checked(g_textbuf, wptr - g_textbuf, outfile)) { |
| 7304 |
goto calc_pca_ret_WRITE_FAIL; |
| 7305 |
} |
| 7306 |
fputs(marker_allele_ptrs[marker_uidx * 2], outfile); |
| 7307 |
putc_unlocked(delimiter, outfile); |
| 7308 |
if (fputs_checked(marker_allele_ptrs[marker_uidx * 2 + 1], outfile)) { |
| 7309 |
goto calc_pca_ret_WRITE_FAIL; |
| 7310 |
} |
| 7311 |
wptr = textbuf_mid; |
| 7312 |
dptr = &(cur_var_wts[pc_ct]); |
| 7313 |
dptr2 = cur_var_wts; |
| 7314 |
do { |
| 7315 |
*wptr++ = delimiter; |
| 7316 |
wptr = dtoa_g(*(--dptr), wptr); |
| 7317 |
} while (dptr > dptr2); |
| 7318 |
*wptr++ = '\n'; |
| 7319 |
if (fwrite_checked(textbuf_mid, wptr - textbuf_mid, outfile)) { |
| 7320 |
goto calc_pca_ret_WRITE_FAIL; |
| 7321 |
} |
| 7322 |
} |
| 7323 |
} while (++marker_uidx < chrom_end); |
| 7324 |
} |
| 7325 |
} |
| 7326 |
if (var_wts) { |
| 7327 |
if (fclose_null(&outfile)) { |
| 7328 |
goto calc_pca_ret_WRITE_FAIL; |
| 7329 |
} |
| 7330 |
} |
| 7331 |
if (proj_sample_ct) { |
| 7332 |
dptr = proj_sample_loadings; |
| 7333 |
for (sample_idx = 0; sample_idx < proj_sample_ct; sample_idx++) { |
| 7334 |
dxx = 1.0 / ((double)((int32_t)(marker_ct - proj_missing_cts[sample_idx]))); |
| 7335 |
for (pc_idx = 0; pc_idx < pc_ct; pc_idx++) { |
| 7336 |
*dptr = ((*dptr) - proj_allhom_wts[pc_idx]) * eigval_inv_sqrts[pc_idx] * dxx; |
| 7337 |
dptr++; |
| 7338 |
} |
| 7339 |
} |
| 7340 |
|
| 7341 |
sample_uidx = unfiltered_sample_ct - 1; |
| 7342 |
sample_idx = sample_ct - 1; |
| 7343 |
ulii = pca_sample_ct; |
| 7344 |
while (1) { |
| 7345 |
if (IS_SET(sample_exclude, sample_uidx)) { |
| 7346 |
sample_uidx = prev_unset_unsafe(sample_exclude, sample_uidx); |
| 7347 |
} |
| 7348 |
if (IS_SET(sample_exclude_proj, sample_uidx)) { |
| 7349 |
memcpy(&(sample_loadings[sample_idx * pc_ct]), &(sample_loadings[(--ulii) * pc_ct]), pc_ct * sizeof(double)); |
| 7350 |
} else { |
| 7351 |
memcpy(&(sample_loadings[sample_idx * pc_ct]), &(proj_sample_loadings[(--proj_sample_ct) * pc_ct]), pc_ct * sizeof(double)); |
| 7352 |
if (!proj_sample_ct) { |
| 7353 |
break; |
| 7354 |
} |
| 7355 |
} |
| 7356 |
sample_uidx--; |
| 7357 |
sample_idx--; |
| 7358 |
} |
| 7359 |
} |
| 7360 |
} |
| 7361 |
|
| 7362 |
memcpy(outname_end, ".eigenval", 10); |
| 7363 |
if (fopen_checked(outname, "w", &outfile)) { |
| 7364 |
goto calc_pca_ret_OPEN_FAIL; |
| 7365 |
} |
| 7366 |
wptr = g_textbuf; |
| 7367 |
pc_idx = pc_ct; |
| 7368 |
do { |
| 7369 |
pc_idx--; |
| 7370 |
wptr = dtoa_gx(out_w[pc_idx], '\n', wptr); |
| 7371 |
} while (pc_idx); |
| 7372 |
if (fwrite_checked(g_textbuf, wptr - g_textbuf, outfile)) { |
| 7373 |
goto calc_pca_ret_WRITE_FAIL; |
| 7374 |
} |
| 7375 |
if (fclose_null(&outfile)) { |
| 7376 |
goto calc_pca_ret_WRITE_FAIL; |
| 7377 |
} |
| 7378 |
memcpy(outname_end, ".eigenvec", 10); |
| 7379 |
if (fopen_checked(outname, "w", &outfile)) { |
| 7380 |
goto calc_pca_ret_OPEN_FAIL; |
| 7381 |
} |
| 7382 |
if (write_headers) { |
| 7383 |
wptr = memcpyl3a(g_textbuf, "FID"); |
| 7384 |
*wptr++ = delimiter; |
| 7385 |
wptr = memcpyl3a(wptr, "IID"); |
| 7386 |
for (pc_idx = 1; pc_idx <= pc_ct; pc_idx++) { |
| 7387 |
*wptr++ = delimiter; |
| 7388 |
wptr = memcpya(wptr, "PC", 2); |
| 7389 |
wptr = uint32toa(pc_idx, wptr); |
| 7390 |
} |
| 7391 |
*wptr++ = '\n'; |
| 7392 |
if (fwrite_checked(g_textbuf, wptr - g_textbuf, outfile)) { |
| 7393 |
goto calc_pca_ret_WRITE_FAIL; |
| 7394 |
} |
| 7395 |
} |
| 7396 |
for (sample_uidx = 0, sample_idx = 0; sample_idx < sample_ct; sample_idx++, sample_uidx++) { |
| 7397 |
next_unset_ul_unsafe_ck(sample_exclude, &sample_uidx); |
| 7398 |
cptr = &(sample_ids[sample_uidx * max_sample_id_len]); |
| 7399 |
if (delimiter == '\t') { |
| 7400 |
wptr = strcpya(g_textbuf, cptr); |
| 7401 |
} else { |
| 7402 |
wptr_start = (char*)memchr(cptr, '\t', max_sample_id_len); |
| 7403 |
wptr = memcpya(g_textbuf, cptr, wptr_start - cptr); |
| 7404 |
*wptr++ = ' '; |
| 7405 |
wptr = strcpya(wptr, &(wptr_start[1])); |
| 7406 |
} |
| 7407 |
dptr = &(sample_loadings[(sample_idx + 1) * pc_ct]); |
| 7408 |
dptr2 = &(sample_loadings[sample_idx * pc_ct]); |
| 7409 |
do { |
| 7410 |
*wptr++ = delimiter; |
| 7411 |
wptr = dtoa_g(*(--dptr), wptr); |
| 7412 |
} while (dptr > dptr2); |
| 7413 |
*wptr++ = '\n'; |
| 7414 |
if (fwrite_checked(g_textbuf, wptr - g_textbuf, outfile)) { |
| 7415 |
goto calc_pca_ret_WRITE_FAIL; |
| 7416 |
} |
| 7417 |
} |
| 7418 |
if (fclose_null(&outfile)) { |
| 7419 |
goto calc_pca_ret_WRITE_FAIL; |
| 7420 |
} |
| 7421 |
*outname_end = '\0'; |
| 7422 |
putc_unlocked('\r', stdout); |
| 7423 |
if (var_wts) { |
| 7424 |
LOGPRINTFWW("--pca: Results saved to %s.eigenval , %s.eigenvec , and %s.eigenvec.var .\n", outname, outname, outname); |
| 7425 |
} else { |
| 7426 |
LOGPRINTFWW("--pca: Results saved to %s.eigenval and %s.eigenvec .\n", outname, outname); |
| 7427 |
} |
| 7428 |
while (0) { |
| 7429 |
calc_pca_ret_NOMEM: |
| 7430 |
retval = RET_NOMEM; |
| 7431 |
break; |
| 7432 |
calc_pca_ret_OPEN_FAIL: |
| 7433 |
retval = RET_OPEN_FAIL; |
| 7434 |
break; |
| 7435 |
calc_pca_ret_READ_FAIL: |
| 7436 |
retval = RET_READ_FAIL; |
| 7437 |
break; |
| 7438 |
calc_pca_ret_WRITE_FAIL: |
| 7439 |
retval = RET_WRITE_FAIL; |
| 7440 |
break; |
| 7441 |
calc_pca_ret_DEGENERATE_DATA: |
| 7442 |
retval = RET_DEGENERATE_DATA; |
| 7443 |
break; |
| 7444 |
} |
| 7445 |
return retval; |
| 7446 |
} |
| 7447 |
#endif |
| 7448 |
|
| 7449 |
int32_t calc_ibm(pthread_t* threads, FILE* bedfile, uintptr_t bed_offset, uintptr_t unfiltered_marker_ct, uintptr_t* marker_exclude_orig, uint32_t marker_ct, uintptr_t unfiltered_sample_ct, uintptr_t* sample_exclude, uintptr_t sample_ct, Chrom_info* chrom_info_ptr) { |
| 7450 |
uintptr_t unfiltered_sample_ct4 = (unfiltered_sample_ct + 3) / 4; |
| 7451 |
uintptr_t marker_uidx = 0; |
| 7452 |
uintptr_t marker_idx = 0; |
| 7453 |
uint32_t dist_thread_ct = g_thread_ct; |
| 7454 |
int32_t retval = 0; |
| 7455 |
uintptr_t* marker_exclude = marker_exclude_orig; |
| 7456 |
uint32_t* giptr = nullptr; |
| 7457 |
unsigned char* bigstack_mark; |
| 7458 |
unsigned char* bedbuf; |
| 7459 |
unsigned char* gptr; |
| 7460 |
uint32_t* sample_missing_unwt; |
| 7461 |
uintptr_t* mmasks; |
| 7462 |
uintptr_t sample_uidx; |
| 7463 |
uintptr_t ulii; |
| 7464 |
uint32_t is_last_block; |
| 7465 |
uint32_t uii; |
| 7466 |
uint32_t ujj; |
| 7467 |
uint32_t ukk; |
| 7468 |
uint32_t umm; |
| 7469 |
uint32_t unn; |
| 7470 |
uintptr_t* glptr; |
| 7471 |
int64_t llxx; |
| 7472 |
g_sample_ct = sample_ct; |
| 7473 |
if (dist_thread_ct > sample_ct / 32) { |
| 7474 |
dist_thread_ct = sample_ct / 32; |
| 7475 |
if (!dist_thread_ct) { |
| 7476 |
dist_thread_ct = 1; |
| 7477 |
} |
| 7478 |
} |
| 7479 |
triangle_fill(sample_ct, dist_thread_ct, 0, 1, 1, 1, g_thread_start); |
| 7480 |
llxx = g_thread_start[dist_thread_ct]; |
| 7481 |
llxx = (llxx * (llxx - 1)) / 2; |
| 7482 |
if (bigstack_calloc_ui(llxx, &g_missing_dbl_excluded) || |
| 7483 |
bigstack_calloc_ui(sample_ct, &sample_missing_unwt)) { |
| 7484 |
goto calc_ibm_ret_NOMEM; |
| 7485 |
} |
| 7486 |
g_sample_missing_unwt = sample_missing_unwt; |
| 7487 |
bigstack_mark = g_bigstack_base; |
| 7488 |
|
| 7489 |
if (bigstack_alloc_ul(sample_ct, &mmasks) || |
| 7490 |
bigstack_alloc_uc(MULTIPLEX_DIST * unfiltered_sample_ct4, &bedbuf)) { |
| 7491 |
goto calc_ibm_ret_NOMEM; |
| 7492 |
} |
| 7493 |
g_mmasks = mmasks; |
| 7494 |
fseeko(bedfile, bed_offset, SEEK_SET); |
| 7495 |
retval = conditional_allocate_non_autosomal_markers(chrom_info_ptr, unfiltered_marker_ct, marker_exclude_orig, marker_ct, 1, 1, "IBM calculation", &marker_exclude, &uii); |
| 7496 |
if (retval) { |
| 7497 |
goto calc_ibm_ret_1; |
| 7498 |
} |
| 7499 |
marker_ct -= uii; |
| 7500 |
do { |
| 7501 |
retval = block_load(bedfile, bed_offset, marker_exclude, marker_ct, MULTIPLEX_DIST, unfiltered_sample_ct4, bedbuf, &marker_uidx, &marker_idx, &ujj); |
| 7502 |
if (retval) { |
| 7503 |
goto calc_ibm_ret_1; |
| 7504 |
} |
| 7505 |
if (ujj < MULTIPLEX_DIST) { |
| 7506 |
memset(&(bedbuf[ujj * unfiltered_sample_ct4]), 0, (MULTIPLEX_DIST - ujj) * unfiltered_sample_ct4); |
| 7507 |
} |
| 7508 |
is_last_block = (marker_idx == marker_ct); |
| 7509 |
for (ukk = 0; ukk < ujj; ukk += BITCT) { |
| 7510 |
glptr = mmasks; |
| 7511 |
giptr = sample_missing_unwt; |
| 7512 |
for (sample_uidx = 0; sample_uidx < unfiltered_sample_ct; sample_uidx++) { |
| 7513 |
if (!IS_SET(sample_exclude, sample_uidx)) { |
| 7514 |
unn = (sample_uidx % 4) * 2; |
| 7515 |
ulii = 0; |
| 7516 |
gptr = &(bedbuf[sample_uidx / 4 + ukk * unfiltered_sample_ct4]); |
| 7517 |
for (umm = 0; umm < BITCT2; umm++) { |
| 7518 |
if (((gptr[umm * unfiltered_sample_ct4] >> unn) & 3) == 1) { |
| 7519 |
ulii |= ONELU << umm; |
| 7520 |
*giptr += 1; |
| 7521 |
} |
| 7522 |
} |
| 7523 |
*glptr = ulii; |
| 7524 |
ulii = 0; |
| 7525 |
gptr = &(bedbuf[sample_uidx / 4 + (ukk + BITCT2) * unfiltered_sample_ct4]); |
| 7526 |
for (umm = 0; umm < BITCT2; umm++) { |
| 7527 |
if (((gptr[umm * unfiltered_sample_ct4] >> unn) & 3) == 1) { |
| 7528 |
ulii |= ONELU << umm; |
| 7529 |
*giptr += 1; |
| 7530 |
} |
| 7531 |
} |
| 7532 |
*glptr++ |= ulii << BITCT2; |
| 7533 |
giptr++; |
| 7534 |
} |
| 7535 |
} |
| 7536 |
|
| 7537 |
uii = is_last_block && (ukk + BITCT >= ujj); |
| 7538 |
if (spawn_threads2(threads, &calc_missing_thread, dist_thread_ct, uii)) { |
| 7539 |
goto calc_ibm_ret_THREAD_CREATE_FAIL; |
| 7540 |
} |
| 7541 |
ulii = 0; |
| 7542 |
calc_missing_thread((void*)ulii); |
| 7543 |
join_threads2(threads, dist_thread_ct, uii); |
| 7544 |
} |
| 7545 |
|
| 7546 |
printf("\r%" PRIuPTR " markers complete.", marker_idx); |
| 7547 |
fflush(stdout); |
| 7548 |
} while (!is_last_block); |
| 7549 |
putc_unlocked('\r', stdout); |
| 7550 |
bigstack_reset(bigstack_mark); |
| 7551 |
while (0) { |
| 7552 |
calc_ibm_ret_NOMEM: |
| 7553 |
retval = RET_NOMEM; |
| 7554 |
break; |
| 7555 |
calc_ibm_ret_THREAD_CREATE_FAIL: |
| 7556 |
retval = RET_THREAD_CREATE_FAIL; |
| 7557 |
break; |
| 7558 |
} |
| 7559 |
calc_ibm_ret_1: |
| 7560 |
|
| 7561 |
return retval; |
| 7562 |
} |
| 7563 |
|
| 7564 |
int32_t calc_distance(pthread_t* threads, uint32_t parallel_idx, uint32_t parallel_tot, FILE* bedfile, uintptr_t bed_offset, char* outname, char* outname_end, char* read_dists_fname, char* distance_wts_fname, double distance_exp, uint64_t calculation_type, uint32_t dist_calc_type, uintptr_t unfiltered_marker_ct, uintptr_t* marker_exclude_orig, uint32_t marker_ct, char* marker_ids, uintptr_t max_marker_id_len, double* set_allele_freqs, uintptr_t unfiltered_sample_ct, uintptr_t* sample_exclude, uintptr_t sample_ct, char* sample_ids, uintptr_t max_sample_id_len, Chrom_info* chrom_info_ptr) { |
| 7565 |
|
| 7566 |
|
| 7567 |
FILE* outfile = nullptr; |
| 7568 |
FILE* outfile2 = nullptr; |
| 7569 |
FILE* outfile3 = nullptr; |
| 7570 |
uintptr_t unfiltered_sample_ct4 = (unfiltered_sample_ct + 3) / 4; |
| 7571 |
uint64_t dists_alloc = 0; |
| 7572 |
uint32_t unweighted_and_flat = ((calculation_type & (CALC_PLINK1_DISTANCE_MATRIX | CALC_PLINK1_IBS_MATRIX)) || (dist_calc_type & DISTANCE_FLAT_MISSING)) && (!distance_wts_fname) && (distance_exp == 0.0); |
| 7573 |
uint32_t missing_wt_needed = ((calculation_type & CALC_DISTANCE) || ((!read_dists_fname) && (calculation_type & (CALC_IBS_TEST | CALC_GROUPDIST | CALC_REGRESS_DISTANCE)))) && (!unweighted_and_flat); |
| 7574 |
uint32_t unwt_needed = 0; |
| 7575 |
uint32_t marker_weight_sum = 0; |
| 7576 |
int32_t retval = 0; |
| 7577 |
uintptr_t* marker_exclude = marker_exclude_orig; |
| 7578 |
uint32_t* dist_missing_wts_i = nullptr; |
| 7579 |
uint32_t* sample_missing = nullptr; |
| 7580 |
uint32_t* sample_missing_unwt = nullptr; |
| 7581 |
uint32_t* giptr = nullptr; |
| 7582 |
uint32_t* giptr2 = nullptr; |
| 7583 |
char* writebuf = nullptr; |
| 7584 |
double* main_weights = nullptr; |
| 7585 |
double* subset_weights = nullptr; |
| 7586 |
uint32_t dist_thread_ct = g_thread_ct; |
| 7587 |
double set_allele_freq_buf[MULTIPLEX_DIST]; |
| 7588 |
uint32_t wtbuf[MULTIPLEX_DIST]; |
| 7589 |
uintptr_t* geno; |
| 7590 |
uintptr_t* masks; |
| 7591 |
uintptr_t* mmasks; |
| 7592 |
char* wptr; |
| 7593 |
unsigned char* bigstack_mark; |
| 7594 |
unsigned char* bedbuf; |
| 7595 |
unsigned char* gptr; |
| 7596 |
uintptr_t sample_uidx; |
| 7597 |
uintptr_t sample_idx; |
| 7598 |
uintptr_t marker_uidx; |
| 7599 |
uintptr_t marker_idx; |
| 7600 |
uintptr_t ulii; |
| 7601 |
uintptr_t uljj; |
| 7602 |
uintptr_t ulkk; |
| 7603 |
uint32_t is_last_block; |
| 7604 |
uint32_t uii; |
| 7605 |
uint32_t ujj; |
| 7606 |
uint32_t ukk; |
| 7607 |
uint32_t umm; |
| 7608 |
uint32_t unn; |
| 7609 |
uint32_t pct; |
| 7610 |
uint32_t tstc; |
| 7611 |
int32_t* iptr; |
| 7612 |
uint32_t* giptr3; |
| 7613 |
uintptr_t* glptr; |
| 7614 |
uintptr_t* glptr2; |
| 7615 |
uintptr_t* glptr3; |
| 7616 |
double* dist_missing_wts; |
| 7617 |
double* dptr2; |
| 7618 |
double marker_weight_sum_d; |
| 7619 |
double dxx; |
| 7620 |
double dyy; |
| 7621 |
uint32_t multiplex; |
| 7622 |
int64_t llxx; |
| 7623 |
g_sample_ct = sample_ct; |
| 7624 |
if (dist_thread_ct > sample_ct / 32) { |
| 7625 |
dist_thread_ct = sample_ct / 32; |
| 7626 |
if (!dist_thread_ct) { |
| 7627 |
dist_thread_ct = 1; |
| 7628 |
} |
| 7629 |
} |
| 7630 |
triangle_fill(sample_ct, dist_thread_ct, parallel_idx, parallel_tot, 1, 1, g_thread_start); |
| 7631 |
llxx = g_thread_start[dist_thread_ct]; |
| 7632 |
llxx = ((llxx * (llxx - 1)) - (int64_t)g_thread_start[0] * (g_thread_start[0] - 1)) / 2; |
| 7633 |
dists_alloc = llxx * sizeof(double); |
| 7634 |
#ifndef __LP64__ |
| 7635 |
if (dists_alloc > 0x7fffffff) { |
| 7636 |
goto calc_distance_ret_NOMEM; |
| 7637 |
} |
| 7638 |
#endif |
| 7639 |
if ((calculation_type & (CALC_PLINK1_DISTANCE_MATRIX | CALC_PLINK1_IBS_MATRIX)) || (dist_calc_type & DISTANCE_FLAT_MISSING)) { |
| 7640 |
if (bigstack_calloc_ui(llxx, &g_missing_dbl_excluded) || |
| 7641 |
bigstack_calloc_ui(sample_ct, &sample_missing_unwt)) { |
| 7642 |
goto calc_distance_ret_NOMEM; |
| 7643 |
} |
| 7644 |
g_sample_missing_unwt = sample_missing_unwt; |
| 7645 |
unwt_needed = 1; |
| 7646 |
} else { |
| 7647 |
|
| 7648 |
g_missing_dbl_excluded = nullptr; |
| 7649 |
} |
| 7650 |
|
| 7651 |
|
| 7652 |
if (bigstack_alloc_d(dists_alloc + CACHELINE_DBL, &g_dists)) { |
| 7653 |
goto calc_distance_ret_NOMEM; |
| 7654 |
} |
| 7655 |
|
| 7656 |
bigstack_mark = g_bigstack_base; |
| 7657 |
if (missing_wt_needed) { |
| 7658 |
if (bigstack_calloc_ui(llxx, &g_missing_tot_weights) || |
| 7659 |
bigstack_calloc_ui(sample_ct, &sample_missing)) { |
| 7660 |
goto calc_distance_ret_NOMEM; |
| 7661 |
} |
| 7662 |
g_sample_missing = sample_missing; |
| 7663 |
} else { |
| 7664 |
g_missing_tot_weights = nullptr; |
| 7665 |
} |
| 7666 |
|
| 7667 |
ujj = distance_wts_fname || (distance_exp != 0.0); |
| 7668 |
if (!ujj) { |
| 7669 |
g_idists = (int32_t*)((char*)bigstack_mark - round_up_pow2(llxx * sizeof(int32_t), CACHELINE)); |
| 7670 |
fill_int_zero(llxx, g_idists); |
| 7671 |
} else { |
| 7672 |
fill_double_zero(llxx, g_dists); |
| 7673 |
} |
| 7674 |
|
| 7675 |
retval = conditional_allocate_non_autosomal_markers(chrom_info_ptr, unfiltered_marker_ct, marker_exclude_orig, marker_ct, 1, 1, "distance matrix calc", &marker_exclude, &uii); |
| 7676 |
if (retval) { |
| 7677 |
goto calc_distance_ret_1; |
| 7678 |
} |
| 7679 |
marker_ct -= uii; |
| 7680 |
|
| 7681 |
if (distance_wts_fname) { |
| 7682 |
retval = load_distance_wts(distance_wts_fname, unfiltered_marker_ct, marker_ids, max_marker_id_len, dist_calc_type & DISTANCE_WTS_NOHEADER, (marker_exclude == marker_exclude_orig), &marker_exclude, &marker_ct, &main_weights); |
| 7683 |
if (retval) { |
| 7684 |
goto calc_distance_ret_1; |
| 7685 |
} |
| 7686 |
} |
| 7687 |
|
| 7688 |
|
| 7689 |
if (bigstack_alloc_ul(ujj? sample_ct : (sample_ct * (MULTIPLEX_2DIST / BITCT)), &masks) || |
| 7690 |
bigstack_alloc_ul(sample_ct, &mmasks)) { |
| 7691 |
goto calc_distance_ret_NOMEM; |
| 7692 |
} |
| 7693 |
|
| 7694 |
|
| 7695 |
if (distance_exp != 0.0) { |
| 7696 |
if (bigstack_alloc_d(marker_ct, &main_weights)) { |
| 7697 |
goto calc_distance_ret_NOMEM; |
| 7698 |
} |
| 7699 |
for (marker_uidx = 0, marker_idx = 0; marker_idx < marker_ct; marker_uidx++, marker_idx++) { |
| 7700 |
next_unset_ul_unsafe_ck(marker_exclude, &marker_uidx); |
| 7701 |
dxx = set_allele_freqs[marker_uidx]; |
| 7702 |
dyy = 2 * dxx * (1.0 - dxx); |
| 7703 |
if (dyy != 0.0) { |
| 7704 |
dyy = pow(dyy, -distance_exp); |
| 7705 |
} |
| 7706 |
main_weights[marker_idx] = dyy; |
| 7707 |
} |
| 7708 |
} |
| 7709 |
|
| 7710 |
|
| 7711 |
|
| 7712 |
if (missing_wt_needed) { |
| 7713 |
|
| 7714 |
|
| 7715 |
if (bigstack_alloc_ui(CACHELINE_INT32, &dist_missing_wts_i) || |
| 7716 |
bigstack_alloc_d(marker_ct, &dist_missing_wts)) { |
| 7717 |
goto calc_distance_ret_NOMEM; |
| 7718 |
} |
| 7719 |
dyy = 0.0; |
| 7720 |
for (marker_uidx = 0, marker_idx = 0; marker_idx < marker_ct; marker_uidx++, marker_idx++) { |
| 7721 |
next_unset_ul_unsafe_ck(marker_exclude, &marker_uidx); |
| 7722 |
|
| 7723 |
|
| 7724 |
|
| 7725 |
|
| 7726 |
|
| 7727 |
|
| 7728 |
|
| 7729 |
|
| 7730 |
|
| 7731 |
|
| 7732 |
|
| 7733 |
|
| 7734 |
|
| 7735 |
|
| 7736 |
|
| 7737 |
|
| 7738 |
|
| 7739 |
|
| 7740 |
dxx = set_allele_freqs[marker_uidx]; |
| 7741 |
if ((dxx != 0.0) && (dxx != 1.0)) { |
| 7742 |
dxx = dxx * (1.0 - dxx) * (dxx * dxx - dxx + 1); |
| 7743 |
if (main_weights) { |
| 7744 |
dxx *= main_weights[marker_idx]; |
| 7745 |
} |
| 7746 |
} |
| 7747 |
dist_missing_wts[marker_idx] = dxx; |
| 7748 |
dyy += dxx; |
| 7749 |
} |
| 7750 |
|
| 7751 |
|
| 7752 |
|
| 7753 |
|
| 7754 |
|
| 7755 |
dyy = (4294967296.0 - ((double)((intptr_t)marker_ct))) / dyy; |
| 7756 |
for (marker_idx = 0; marker_idx < marker_ct; marker_idx++) { |
| 7757 |
uii = (uint32_t)(dist_missing_wts[marker_idx] * dyy + 0.5); |
| 7758 |
marker_weight_sum += uii; |
| 7759 |
dist_missing_wts_i[marker_idx] = uii; |
| 7760 |
} |
| 7761 |
} |
| 7762 |
marker_weight_sum_d = (double)marker_weight_sum; |
| 7763 |
|
| 7764 |
if (!main_weights) { |
| 7765 |
multiplex = MULTIPLEX_DIST; |
| 7766 |
bigstack_alloc_ul(sample_ct * (MULTIPLEX_2DIST / BITCT), &geno); |
| 7767 |
} else { |
| 7768 |
multiplex = MULTIPLEX_DIST_EXP; |
| 7769 |
bigstack_alloc_ul(sample_ct, &geno); |
| 7770 |
} |
| 7771 |
if (!geno) { |
| 7772 |
goto calc_distance_ret_NOMEM; |
| 7773 |
} |
| 7774 |
g_geno = (unsigned char*)geno; |
| 7775 |
g_masks = masks; |
| 7776 |
g_mmasks = mmasks; |
| 7777 |
|
| 7778 |
if (bigstack_alloc_uc(multiplex * unfiltered_sample_ct4, &bedbuf)) { |
| 7779 |
goto calc_distance_ret_NOMEM; |
| 7780 |
} |
| 7781 |
if (main_weights) { |
| 7782 |
#ifdef __LP64__ |
| 7783 |
if (bigstack_alloc_d(45056, &subset_weights)) { |
| 7784 |
goto calc_distance_ret_NOMEM; |
| 7785 |
} |
| 7786 |
#else |
| 7787 |
if (bigstack_alloc_d(32768, &subset_weights)) { |
| 7788 |
goto calc_distance_ret_NOMEM; |
| 7789 |
} |
| 7790 |
g_subset_weights_i = wtbuf; |
| 7791 |
#endif |
| 7792 |
g_subset_weights = subset_weights; |
| 7793 |
} |
| 7794 |
fseeko(bedfile, bed_offset, SEEK_SET); |
| 7795 |
marker_uidx = 0; |
| 7796 |
marker_idx = 0; |
| 7797 |
do { |
| 7798 |
for (ujj = 0; ujj < multiplex; ujj++) { |
| 7799 |
set_allele_freq_buf[ujj] = 0.5; |
| 7800 |
} |
| 7801 |
fill_int_zero(multiplex, (int32_t*)wtbuf); |
| 7802 |
|
| 7803 |
|
| 7804 |
|
| 7805 |
|
| 7806 |
|
| 7807 |
|
| 7808 |
|
| 7809 |
|
| 7810 |
|
| 7811 |
|
| 7812 |
|
| 7813 |
|
| 7814 |
|
| 7815 |
|
| 7816 |
|
| 7817 |
|
| 7818 |
|
| 7819 |
|
| 7820 |
|
| 7821 |
|
| 7822 |
|
| 7823 |
|
| 7824 |
|
| 7825 |
|
| 7826 |
|
| 7827 |
|
| 7828 |
|
| 7829 |
|
| 7830 |
|
| 7831 |
|
| 7832 |
|
| 7833 |
|
| 7834 |
|
| 7835 |
|
| 7836 |
|
| 7837 |
|
| 7838 |
|
| 7839 |
|
| 7840 |
|
| 7841 |
|
| 7842 |
|
| 7843 |
|
| 7844 |
|
| 7845 |
|
| 7846 |
copy_set_allele_freqs(marker_uidx, marker_exclude, multiplex, marker_idx, marker_ct, nullptr, set_allele_freqs, set_allele_freq_buf); |
| 7847 |
if (missing_wt_needed) { |
| 7848 |
uii = marker_ct - marker_idx; |
| 7849 |
if (uii > multiplex) { |
| 7850 |
uii = multiplex; |
| 7851 |
} |
| 7852 |
memcpy(wtbuf, &(dist_missing_wts_i[marker_idx]), uii * sizeof(int32_t)); |
| 7853 |
} |
| 7854 |
retval = block_load(bedfile, bed_offset, marker_exclude, marker_ct, multiplex, unfiltered_sample_ct4, bedbuf, &marker_uidx, &marker_idx, &ujj); |
| 7855 |
if (retval) { |
| 7856 |
goto calc_distance_ret_1; |
| 7857 |
} |
| 7858 |
if (ujj < multiplex) { |
| 7859 |
memset(&(bedbuf[ujj * unfiltered_sample_ct4]), 0, (multiplex - ujj) * unfiltered_sample_ct4); |
| 7860 |
if (!main_weights) { |
| 7861 |
fill_ulong_zero(sample_ct * (MULTIPLEX_2DIST / BITCT), geno); |
| 7862 |
fill_ulong_zero(sample_ct * (MULTIPLEX_2DIST / BITCT), masks); |
| 7863 |
} else { |
| 7864 |
fill_ulong_zero(sample_ct, geno); |
| 7865 |
fill_ulong_zero(sample_ct, masks); |
| 7866 |
} |
| 7867 |
} |
| 7868 |
is_last_block = (marker_idx == marker_ct); |
| 7869 |
if (!main_weights) { |
| 7870 |
for (ukk = 0; ukk < ujj; ukk += BITCT) { |
| 7871 |
glptr = &(geno[ukk / BITCT2]); |
| 7872 |
glptr2 = &(masks[ukk / BITCT2]); |
| 7873 |
glptr3 = mmasks; |
| 7874 |
if (missing_wt_needed) { |
| 7875 |
giptr = sample_missing; |
| 7876 |
} |
| 7877 |
if (unwt_needed) { |
| 7878 |
giptr2 = sample_missing_unwt; |
| 7879 |
} |
| 7880 |
for (sample_uidx = 0; sample_uidx < unfiltered_sample_ct; sample_uidx++) { |
| 7881 |
if (!IS_SET(sample_exclude, sample_uidx)) { |
| 7882 |
unn = (sample_uidx % 4) * 2; |
| 7883 |
ulii = 0; |
| 7884 |
ulkk = 0; |
| 7885 |
gptr = &(bedbuf[sample_uidx / 4 + ukk * unfiltered_sample_ct4]); |
| 7886 |
for (umm = 0; umm < BITCT2; umm++) { |
| 7887 |
uljj = (gptr[umm * unfiltered_sample_ct4] >> unn) & 3; |
| 7888 |
ulii |= uljj << (umm * 2); |
| 7889 |
if (uljj == 1) { |
| 7890 |
ulkk |= ONELU << umm; |
| 7891 |
if (missing_wt_needed) { |
| 7892 |
*giptr += wtbuf[umm + ukk]; |
| 7893 |
} |
| 7894 |
if (unwt_needed) { |
| 7895 |
*giptr2 += 1; |
| 7896 |
} |
| 7897 |
} |
| 7898 |
} |
| 7899 |
|
| 7900 |
|
| 7901 |
ulii ^= FIVEMASK; |
| 7902 |
*glptr++ = ulii; |
| 7903 |
ulii = (ulii | (ulii >> 1)) & FIVEMASK; |
| 7904 |
*glptr2++ = ulii * 3; |
| 7905 |
*glptr3 = ulkk; |
| 7906 |
ulii = 0; |
| 7907 |
ulkk = 0; |
| 7908 |
gptr = &(bedbuf[sample_uidx / 4 + (ukk + BITCT2) * unfiltered_sample_ct4]); |
| 7909 |
for (umm = 0; umm < BITCT2; umm++) { |
| 7910 |
uljj = (gptr[umm * unfiltered_sample_ct4] >> unn) & 3; |
| 7911 |
ulii |= uljj << (umm * 2); |
| 7912 |
if (uljj == 1) { |
| 7913 |
ulkk |= ONELU << umm; |
| 7914 |
if (missing_wt_needed) { |
| 7915 |
*giptr += wtbuf[umm + ukk + BITCT2]; |
| 7916 |
} |
| 7917 |
if (unwt_needed) { |
| 7918 |
*giptr2 += 1; |
| 7919 |
} |
| 7920 |
} |
| 7921 |
} |
| 7922 |
ulii ^= FIVEMASK; |
| 7923 |
*glptr = ulii; |
| 7924 |
ulii = (ulii | (ulii >> 1)) & FIVEMASK; |
| 7925 |
*glptr2 = ulii * 3; |
| 7926 |
*glptr3++ |= ulkk << BITCT2; |
| 7927 |
glptr = &(glptr[(MULTIPLEX_2DIST / BITCT) - 1]); |
| 7928 |
glptr2 = &(glptr2[(MULTIPLEX_2DIST / BITCT) - 1]); |
| 7929 |
if (missing_wt_needed) { |
| 7930 |
giptr++; |
| 7931 |
} |
| 7932 |
if (unwt_needed) { |
| 7933 |
giptr2++; |
| 7934 |
} |
| 7935 |
} |
| 7936 |
} |
| 7937 |
|
| 7938 |
if (missing_wt_needed) { |
| 7939 |
g_subset_weights_i = &(wtbuf[ukk]); |
| 7940 |
} |
| 7941 |
uii = is_last_block && (ukk + BITCT >= ujj); |
| 7942 |
if (spawn_threads2(threads, &calc_ibs_thread, dist_thread_ct, uii)) { |
| 7943 |
goto calc_distance_ret_THREAD_CREATE_FAIL; |
| 7944 |
} |
| 7945 |
ulii = 0; |
| 7946 |
calc_ibs_thread((void*)ulii); |
| 7947 |
join_threads2(threads, dist_thread_ct, uii); |
| 7948 |
} |
| 7949 |
} else { |
| 7950 |
fill_ulong_zero(sample_ct, mmasks); |
| 7951 |
for (ukk = 0; ukk < ujj; ukk += MULTIPLEX_DIST_EXP / 2) { |
| 7952 |
glptr = geno; |
| 7953 |
glptr2 = masks; |
| 7954 |
glptr3 = mmasks; |
| 7955 |
giptr3 = sample_missing; |
| 7956 |
for (sample_uidx = 0; sample_uidx < unfiltered_sample_ct; sample_uidx++) { |
| 7957 |
if (!IS_SET(sample_exclude, sample_uidx)) { |
| 7958 |
unn = (sample_uidx % 4) * 2; |
| 7959 |
ulii = 0; |
| 7960 |
ulkk = 0; |
| 7961 |
gptr = &(bedbuf[sample_uidx / 4 + ukk * unfiltered_sample_ct4]); |
| 7962 |
for (umm = 0; umm < MULTIPLEX_DIST_EXP / 2; umm++) { |
| 7963 |
uljj = (gptr[umm * unfiltered_sample_ct4] >> unn) & 3; |
| 7964 |
ulii |= uljj << (umm * 2); |
| 7965 |
if (uljj == 1) { |
| 7966 |
ulkk |= ONELU << umm; |
| 7967 |
*giptr3 += wtbuf[umm + ukk]; |
| 7968 |
} |
| 7969 |
} |
| 7970 |
#ifdef __LP64__ |
| 7971 |
ulii ^= FIVEMASK; |
| 7972 |
*glptr++ = ulii; |
| 7973 |
ulii = (ulii | (ulii >> 1)) & FIVEMASK; |
| 7974 |
#else |
| 7975 |
|
| 7976 |
|
| 7977 |
ulii ^= 0x05555555; |
| 7978 |
*glptr++ = ulii; |
| 7979 |
ulii = (ulii | (ulii >> 1)) & 0x05555555; |
| 7980 |
#endif |
| 7981 |
*glptr2++ = ulii * 3; |
| 7982 |
*glptr3++ |= ulkk << ukk; |
| 7983 |
giptr3++; |
| 7984 |
} |
| 7985 |
} |
| 7986 |
fill_subset_weights(subset_weights, &(main_weights[marker_idx - ujj + ukk])); |
| 7987 |
g_subset_weights_i = &(wtbuf[ukk]); |
| 7988 |
uii = is_last_block && (ukk + (MULTIPLEX_DIST_EXP / 3) >= ujj); |
| 7989 |
if (spawn_threads2(threads, &calc_wdist_thread, dist_thread_ct, uii)) { |
| 7990 |
goto calc_distance_ret_THREAD_CREATE_FAIL; |
| 7991 |
} |
| 7992 |
ulii = 0; |
| 7993 |
calc_wdist_thread((void*)ulii); |
| 7994 |
join_threads2(threads, dist_thread_ct, uii); |
| 7995 |
} |
| 7996 |
} |
| 7997 |
printf("\r%" PRIuPTR " markers complete.", marker_idx); |
| 7998 |
fflush(stdout); |
| 7999 |
} while (!is_last_block); |
| 8000 |
putc_unlocked('\r', stdout); |
| 8001 |
logprint("Distance matrix calculation complete.\n"); |
| 8002 |
if (calculation_type & (CALC_DISTANCE | CALC_IBS_TEST)) { |
| 8003 |
|
| 8004 |
|
| 8005 |
if (main_weights) { |
| 8006 |
dyy = 0.0; |
| 8007 |
marker_uidx = 0; |
| 8008 |
for (marker_idx = 0; marker_idx < marker_ct; marker_idx++) { |
| 8009 |
dyy += main_weights[marker_idx]; |
| 8010 |
} |
| 8011 |
g_half_marker_ct_recip = 0.5 / dyy; |
| 8012 |
} else { |
| 8013 |
g_half_marker_ct_recip = 0.5 / (double)marker_ct; |
| 8014 |
} |
| 8015 |
} |
| 8016 |
bigstack_reset(masks); |
| 8017 |
if (calculation_type & (CALC_PLINK1_DISTANCE_MATRIX | CALC_PLINK1_IBS_MATRIX)) { |
| 8018 |
if (bigstack_alloc_c(16 * sample_ct, &writebuf)) { |
| 8019 |
goto calc_distance_ret_NOMEM; |
| 8020 |
} |
| 8021 |
} |
| 8022 |
if (calculation_type & CALC_PLINK1_DISTANCE_MATRIX) { |
| 8023 |
strcpy(outname_end, ".mdist"); |
| 8024 |
if (fopen_checked(outname, "w", &outfile)) { |
| 8025 |
goto calc_distance_ret_OPEN_FAIL; |
| 8026 |
} |
| 8027 |
iptr = g_idists; |
| 8028 |
giptr = g_missing_dbl_excluded; |
| 8029 |
pct = 1; |
| 8030 |
|
| 8031 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 8032 |
giptr2 = sample_missing_unwt; |
| 8033 |
uii = marker_ct - giptr2[sample_idx]; |
| 8034 |
wptr = writebuf; |
| 8035 |
for (ujj = 0; ujj < sample_idx; ujj++) { |
| 8036 |
wptr = dtoa_gx(((double)(*iptr++)) / (2 * (uii - (*giptr2++) + (*giptr++))), ' ', wptr); |
| 8037 |
} |
| 8038 |
wptr = memcpya(wptr, "0 ", 2); |
| 8039 |
giptr2++; |
| 8040 |
for (ulii = sample_idx + 1; ulii < sample_ct; ulii++) { |
| 8041 |
uljj = tri_coord_no_diag(sample_idx, ulii); |
| 8042 |
wptr = dtoa_gx(((double)g_idists[uljj]) / (2 * (uii - (*giptr2++) + g_missing_dbl_excluded[uljj])), ' ', wptr); |
| 8043 |
} |
| 8044 |
*wptr++ = '\n'; |
| 8045 |
if (fwrite_checked(writebuf, wptr - writebuf, outfile)) { |
| 8046 |
goto calc_distance_ret_WRITE_FAIL; |
| 8047 |
} |
| 8048 |
if (sample_idx * 100LLU >= ((uint64_t)pct * sample_ct)) { |
| 8049 |
pct = (sample_idx * 100LLU) / sample_ct; |
| 8050 |
printf("\rWriting... %u%%", pct++); |
| 8051 |
fflush(stdout); |
| 8052 |
} |
| 8053 |
} |
| 8054 |
if (fclose_null(&outfile)) { |
| 8055 |
goto calc_distance_ret_WRITE_FAIL; |
| 8056 |
} |
| 8057 |
putc_unlocked('\r', stdout); |
| 8058 |
if (!parallel_idx) { |
| 8059 |
wptr = strcpya(g_logbuf, "Distances (proportions) written to "); |
| 8060 |
wptr = strcpya(wptr, outname); |
| 8061 |
strcpy(outname_end, ".mdist.id"); |
| 8062 |
retval = write_ids(outname, unfiltered_sample_ct, sample_exclude, sample_ids, max_sample_id_len); |
| 8063 |
if (retval) { |
| 8064 |
goto calc_distance_ret_1; |
| 8065 |
} |
| 8066 |
snprintf(wptr, LOGBUFLEN - (wptr - g_logbuf), " , and IDs to %s .\n", outname); |
| 8067 |
} else { |
| 8068 |
snprintf(g_logbuf, LOGBUFLEN, "Distances (proportions) written to %s .\n", outname); |
| 8069 |
} |
| 8070 |
wordwrapb(0); |
| 8071 |
logprintb(); |
| 8072 |
} |
| 8073 |
if (calculation_type & CALC_PLINK1_IBS_MATRIX) { |
| 8074 |
strcpy(outname_end, ".mibs"); |
| 8075 |
if (fopen_checked(outname, "w", &outfile)) { |
| 8076 |
goto calc_distance_ret_OPEN_FAIL; |
| 8077 |
} |
| 8078 |
iptr = g_idists; |
| 8079 |
giptr = g_missing_dbl_excluded; |
| 8080 |
pct = 1; |
| 8081 |
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) { |
| 8082 |
giptr2 = sample_missing_unwt; |
| 8083 |
uii = marker_ct - giptr2[sample_idx]; |
| 8084 |
wptr = writebuf; |
| 8085 |
for (ujj = 0; ujj < sample_idx; ujj++) { |
| 8086 |
wptr = dtoa_gx(1.0 - (((double)(*iptr++)) / (2 * (uii - (*giptr2++) + (*giptr++)))), ' ', wptr); |
| 8087 |
} |
| 8088 |
wptr = memcpya(wptr, "1 ", 2); |
| 8089 |
giptr2++; |
| 8090 |
for (ulii = sample_idx + 1; ulii < sample_ct; ulii++) { |
| 8091 |
uljj = tri_coord_no_diag(sample_idx, ulii); |
| 8092 |
wptr = dtoa_gx(1.0 - (((double)g_idists[uljj]) / (2 * (uii - (*giptr2++) + g_missing_dbl_excluded[uljj]))), ' ', wptr); |
| 8093 |
} |
| 8094 |
*wptr++ = '\n'; |
| 8095 |
if (fwrite_checked(writebuf, wptr - writebuf, outfile)) { |
| 8096 |
goto calc_distance_ret_WRITE_FAIL; |
| 8097 |
} |
| 8098 |
if (sample_idx * 100 >= (pct * sample_ct)) { |
| 8099 |
pct = (sample_idx * 100) / sample_ct; |
| 8100 |
printf("\rWriting... %u%%", pct++); |
| 8101 |
fflush(stdout); |
| 8102 |
} |
| 8103 |
} |
| 8104 |
if (fclose_null(&outfile)) { |
| 8105 |
goto calc_distance_ret_WRITE_FAIL; |
| 8106 |
} |
| 8107 |
putc_unlocked('\r', stdout); |
| 8108 |
strcpy(outname_end, ".mibs.id"); |
| 8109 |
retval = write_ids(outname, unfiltered_sample_ct, sample_exclude, sample_ids, max_sample_id_len); |
| 8110 |
if (retval) { |
| 8111 |
goto calc_distance_ret_1; |
| 8112 |
} |
| 8113 |
outname_end[5] = '\0'; |
| 8114 |
LOGPRINTFWW("IBS matrix written to %s , and IDs to %s.id .\n", outname, outname); |
| 8115 |
} while (!is_last_block); |
| 8116 |
tstc = g_thread_start[dist_thread_ct]; |
| 8117 |
if (missing_wt_needed) { |
| 8118 |
giptr = g_missing_tot_weights; |
| 8119 |
dptr2 = g_dists; |
| 8120 |
if (!main_weights) { |
| 8121 |
iptr = g_idists; |
| 8122 |
for (sample_idx = g_thread_start[0]; sample_idx < tstc; sample_idx++) { |
| 8123 |
giptr2 = sample_missing; |
| 8124 |
uii = giptr2[sample_idx]; |
| 8125 |
for (ujj = 0; ujj < sample_idx; ujj++) { |
| 8126 |
*dptr2++ = (marker_weight_sum_d / ((marker_weight_sum - uii - (*giptr2++)) + (*giptr++))) * (*iptr++); |
| 8127 |
} |
| 8128 |
} |
| 8129 |
} else { |
| 8130 |
for (sample_idx = g_thread_start[0]; sample_idx < tstc; sample_idx++) { |
| 8131 |
giptr2 = sample_missing; |
| 8132 |
uii = giptr2[sample_idx]; |
| 8133 |
for (ujj = 0; ujj < sample_idx; ujj++) { |
| 8134 |
*dptr2 *= (marker_weight_sum_d / ((marker_weight_sum - uii - (*giptr2++)) + (*giptr++))); |
| 8135 |
dptr2++; |
| 8136 |
} |
| 8137 |
} |
| 8138 |
} |
| 8139 |
} else if (dist_calc_type & DISTANCE_FLAT_MISSING) { |
| 8140 |
dptr2 = g_dists; |
| 8141 |
giptr = g_missing_dbl_excluded; |
| 8142 |
if (!main_weights) { |
| 8143 |
iptr = g_idists; |
| 8144 |
if (dist_calc_type & DISTANCE_CLUSTER) { |
| 8145 |
|
| 8146 |
for (sample_idx = g_thread_start[0]; sample_idx < tstc; sample_idx++) { |
| 8147 |
giptr2 = sample_missing_unwt; |
| 8148 |
uii = marker_ct - giptr2[sample_idx]; |
| 8149 |
for (ujj = 0; ujj < sample_idx; ujj++) { |
| 8150 |
*dptr2++ = 1.0 - (((double)(*iptr++)) / (2 * (uii - (*giptr2++) + (*giptr++)))); |
| 8151 |
} |
| 8152 |
} |
| 8153 |
} else { |
| 8154 |
for (sample_idx = g_thread_start[0]; sample_idx < tstc; sample_idx++) { |
| 8155 |
giptr2 = sample_missing_unwt; |
| 8156 |
uii = marker_ct - giptr2[sample_idx]; |
| 8157 |
for (ujj = 0; ujj < sample_idx; ujj++) { |
| 8158 |
*dptr2++ = (((double)marker_ct) / (uii - (*giptr2++) + (*giptr++))) * (*iptr++); |
| 8159 |
} |
| 8160 |
} |
| 8161 |
} |
| 8162 |
} else { |
| 8163 |
for (sample_idx = g_thread_start[0]; sample_idx < tstc; sample_idx++) { |
| 8164 |
giptr2 = sample_missing_unwt; |
| 8165 |
uii = marker_ct - giptr2[sample_idx]; |
| 8166 |
for (ujj = 0; ujj < sample_idx; ujj++) { |
| 8167 |
*dptr2 *= ((double)marker_ct) / (uii - (*giptr2++) + (*giptr++)); |
| 8168 |
dptr2++; |
| 8169 |
} |
| 8170 |
} |
| 8171 |
} |
| 8172 |
} |
| 8173 |
|
| 8174 |
if (calculation_type & CALC_DISTANCE) { |
| 8175 |
if (!parallel_idx) { |
| 8176 |
retval = distance_d_write_ids(outname, outname_end, dist_calc_type, unfiltered_sample_ct, sample_exclude, sample_ids, max_sample_id_len); |
| 8177 |
if (retval) { |
| 8178 |
goto calc_distance_ret_1; |
| 8179 |
} |
| 8180 |
LOGPRINTFWW("IDs written to %s .\n", outname); |
| 8181 |
} |
| 8182 |
retval = distance_d_write(&outfile, &outfile2, &outfile3, dist_calc_type, outname, outname_end, g_dists, g_half_marker_ct_recip, sample_ct, g_thread_start[0], g_thread_start[dist_thread_ct], parallel_idx, parallel_tot, (unsigned char*)geno); |
| 8183 |
if (retval) { |
| 8184 |
goto calc_distance_ret_1; |
| 8185 |
} |
| 8186 |
} |
| 8187 |
bigstack_reset(bigstack_mark); |
| 8188 |
|
| 8189 |
while (0) { |
| 8190 |
calc_distance_ret_NOMEM: |
| 8191 |
retval = RET_NOMEM; |
| 8192 |
break; |
| 8193 |
calc_distance_ret_OPEN_FAIL: |
| 8194 |
retval = RET_OPEN_FAIL; |
| 8195 |
break; |
| 8196 |
calc_distance_ret_WRITE_FAIL: |
| 8197 |
retval = RET_WRITE_FAIL; |
| 8198 |
break; |
| 8199 |
calc_distance_ret_THREAD_CREATE_FAIL: |
| 8200 |
retval = RET_THREAD_CREATE_FAIL; |
| 8201 |
break; |
| 8202 |
} |
| 8203 |
calc_distance_ret_1: |
| 8204 |
fclose_cond(outfile); |
| 8205 |
fclose_cond(outfile2); |
| 8206 |
fclose_cond(outfile3); |
| 8207 |
return retval; |
| 8208 |
} |
| 8209 |
|
| 8210 |
void cluster_dist_divide(uintptr_t sample_ct, uintptr_t cluster_ct, uint32_t* cluster_starts, double* cluster_sdistances) { |
| 8211 |
uintptr_t tcoord; |
| 8212 |
uintptr_t ulii; |
| 8213 |
uintptr_t uljj; |
| 8214 |
uint32_t uii; |
| 8215 |
double dxx; |
| 8216 |
for (ulii = 0; ulii < cluster_ct; ulii++) { |
| 8217 |
uii = cluster_starts[ulii + 1] - cluster_starts[ulii]; |
| 8218 |
if (uii > 1) { |
| 8219 |
dxx = 1.0 / ((double)((int32_t)uii)); |
| 8220 |
uljj = (ulii * (ulii + 1)) / 2; |
| 8221 |
for (tcoord = (ulii * (ulii - 1)) / 2; tcoord < uljj; tcoord++) { |
| 8222 |
cluster_sdistances[tcoord] *= dxx; |
| 8223 |
} |
| 8224 |
for (uljj = ulii + 1; uljj < sample_ct; uljj++) { |
| 8225 |
cluster_sdistances[tri_coord_no_diag(ulii, uljj)] *= dxx; |
| 8226 |
} |
| 8227 |
} |
| 8228 |
} |
| 8229 |
} |
| 8230 |
|
| 8231 |
void cluster_dist_multiply(uintptr_t sample_ct, uintptr_t cluster_ct, uint32_t* cluster_starts, double* cluster_sdistances) { |
| 8232 |
uintptr_t tcoord; |
| 8233 |
uintptr_t ulii; |
| 8234 |
uintptr_t uljj; |
| 8235 |
uint32_t uii; |
| 8236 |
double dxx; |
| 8237 |
for (ulii = 0; ulii < cluster_ct; ulii++) { |
| 8238 |
uii = cluster_starts[ulii + 1] - cluster_starts[ulii]; |
| 8239 |
if (uii > 1) { |
| 8240 |
dxx = ((double)((int32_t)uii)); |
| 8241 |
uljj = (ulii * (ulii + 1)) / 2; |
| 8242 |
for (tcoord = (ulii * (ulii - 1)) / 2; tcoord < uljj; tcoord++) { |
| 8243 |
cluster_sdistances[tcoord] *= dxx; |
| 8244 |
} |
| 8245 |
for (uljj = ulii + 1; uljj < sample_ct; uljj++) { |
| 8246 |
cluster_sdistances[tri_coord_no_diag(ulii, uljj)] *= dxx; |
| 8247 |
} |
| 8248 |
} |
| 8249 |
} |
| 8250 |
} |
| 8251 |
|
| 8252 |
int32_t calc_cluster_neighbor(pthread_t* threads, FILE* bedfile, uintptr_t bed_offset, uint32_t marker_ct, uintptr_t unfiltered_marker_ct, uintptr_t* marker_exclude, Chrom_info* chrom_info_ptr, double* set_allele_freqs, uintptr_t unfiltered_sample_ct, uintptr_t* sample_exclude, uintptr_t sample_ct, char* sample_ids, uint32_t plink_maxfid, uint32_t plink_maxiid, uintptr_t max_sample_id_len, char* read_dists_fname, char* read_dists_id_fname, char* read_genome_fname, char* outname, char* outname_end, uint64_t calculation_type, uintptr_t cluster_ct, uint32_t* cluster_map, uint32_t* cluster_starts, Cluster_info* cp, int32_t missing_pheno, uint32_t neighbor_n1, uint32_t neighbor_n2, uint32_t ppc_gap, uintptr_t* pheno_c, double* mds_plot_dmatrix_copy, uintptr_t* cluster_merge_prevented, double* cluster_sorted_ibs, unsigned char* bigstack_mark_precluster, unsigned char* bigstack_mark_postcluster) { |
| 8253 |
|
| 8254 |
|
| 8255 |
|
| 8256 |
|
| 8257 |
|
| 8258 |
|
| 8259 |
|
| 8260 |
|
| 8261 |
|
| 8262 |
|
| 8263 |
|
| 8264 |
|
| 8265 |
|
| 8266 |
FILE* outfile = nullptr; |
| 8267 |
uint32_t* cluster_sorted_ibs_indices = nullptr; |
| 8268 |
#ifdef __LP64__ |
| 8269 |
|
| 8270 |
#endif |
| 8271 |
uint32_t* sample_to_cluster = nullptr; |
| 8272 |
double* neighbor_quantiles = nullptr; |
| 8273 |
double* neighbor_quantile_means = nullptr; |
| 8274 |
double* neighbor_quantile_stdev_recips = nullptr; |
| 8275 |
uint32_t* neighbor_qindices = nullptr; |
| 8276 |
uint32_t* ppc_fail_counts = nullptr; |
| 8277 |
uint32_t* cur_cluster_sizes = nullptr; |
| 8278 |
uint32_t* cur_cluster_case_cts = nullptr; |
| 8279 |
uint32_t* cur_cluster_remap = nullptr; |
| 8280 |
uint32_t* cluster_index = nullptr; |
| 8281 |
uintptr_t* collapsed_pheno_c = nullptr; |
| 8282 |
uint32_t* sample_idx_to_uidx = nullptr; |
| 8283 |
uint32_t* late_clidx_to_sample_uidx = nullptr; |
| 8284 |
uintptr_t* ibs_ties = nullptr; |
| 8285 |
uint32_t* genome_main = g_genome_main; |
| 8286 |
double* dptr = nullptr; |
| 8287 |
double min_ppc = cp->ppc; |
| 8288 |
double min_ibm = cp->min_ibm; |
| 8289 |
double min_zx = 0.0; |
| 8290 |
uint32_t ibm_constraint = (min_ibm != 0.0); |
| 8291 |
uintptr_t unfiltered_sample_ctl = BITCT_TO_WORDCT(unfiltered_sample_ct); |
| 8292 |
uintptr_t sample_ctl = BITCT_TO_WORDCT(sample_ct); |
| 8293 |
double sample_ct_recip = 1.0 / ((double)((intptr_t)sample_ct)); |
| 8294 |
uint32_t do_neighbor = (calculation_type / CALC_NEIGHBOR) & 1; |
| 8295 |
uint32_t is_group_avg = (cp->modifier / CLUSTER_GROUP_AVG) & 1; |
| 8296 |
uint32_t is_mds_cluster = cp->modifier & CLUSTER_MDS; |
| 8297 |
uint32_t is_old_tiebreaks = cp->modifier & CLUSTER_OLD_TIEBREAKS; |
| 8298 |
uint32_t mds_fill_nonclust = (!is_mds_cluster) && mds_plot_dmatrix_copy; |
| 8299 |
uint32_t cluster_missing = cp->modifier & CLUSTER_MISSING; |
| 8300 |
uint32_t use_genome_dists = (!g_dists) && (!read_dists_fname) && (!cluster_missing); |
| 8301 |
uint32_t cur_cluster_ct = sample_ct; |
| 8302 |
uintptr_t neighbor_row_ct = neighbor_n2 - neighbor_n1 + 1; |
| 8303 |
uint32_t mc_warning = 0; |
| 8304 |
uint32_t mcc_warning = 0; |
| 8305 |
uint32_t ppc_warning = 0; |
| 8306 |
uint32_t ibm_warning = 0; |
| 8307 |
uintptr_t tcoord = 0; |
| 8308 |
uintptr_t clidx1 = 0; |
| 8309 |
uintptr_t clidx2 = 0; |
| 8310 |
uint32_t case_ct = 0xffffffffU; |
| 8311 |
uint32_t ctrl_ct = 0xffffffffU; |
| 8312 |
double dyy = 0.0; |
| 8313 |
uint32_t pct = 1; |
| 8314 |
int32_t retval = 0; |
| 8315 |
uintptr_t initial_triangle_size; |
| 8316 |
uintptr_t heap_size; |
| 8317 |
double* dptr2; |
| 8318 |
uint32_t* sample_missing_unwt; |
| 8319 |
uint32_t* missing_dbl_excluded; |
| 8320 |
uint32_t* sample_missing_ptr; |
| 8321 |
uint32_t* dbl_exclude_ptr; |
| 8322 |
uint32_t* merge_sequence; |
| 8323 |
uint32_t* uiptr; |
| 8324 |
uint32_t* uiptr2; |
| 8325 |
uint32_t merge_ct; |
| 8326 |
char* wptr_start; |
| 8327 |
char* fam_id; |
| 8328 |
char* sample_id; |
| 8329 |
char* wptr; |
| 8330 |
double dxx; |
| 8331 |
double dxx1; |
| 8332 |
uintptr_t ulii; |
| 8333 |
uintptr_t uljj; |
| 8334 |
uintptr_t sample_idx1; |
| 8335 |
uintptr_t sample_idx2; |
| 8336 |
uint32_t uii; |
| 8337 |
uint32_t ujj; |
| 8338 |
uint32_t ukk; |
| 8339 |
uint32_t umm; |
| 8340 |
g_sample_ct = sample_ct; |
| 8341 |
|
| 8342 |
if (sample_ct <= cp->min_ct) { |
| 8343 |
|
| 8344 |
|
| 8345 |
logerrprint("Error: --K parameter too large (>= sample count).\n"); |
| 8346 |
goto calc_cluster_neighbor_ret_INVALID_CMDLINE; |
| 8347 |
} |
| 8348 |
if (cluster_ct) { |
| 8349 |
cur_cluster_ct += cluster_ct - cluster_starts[cluster_ct]; |
| 8350 |
if (cur_cluster_ct <= cp->min_ct) { |
| 8351 |
logerrprint("Error: --K parameter too large (>= initial cluster count).\n"); |
| 8352 |
goto calc_cluster_neighbor_ret_INVALID_CMDLINE; |
| 8353 |
} |
| 8354 |
sample_to_cluster = (uint32_t*)malloc(sample_ct * sizeof(int32_t)); |
| 8355 |
if (!sample_to_cluster) { |
| 8356 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 8357 |
} |
| 8358 |
if (cluster_starts[cluster_ct] < sample_ct) { |
| 8359 |
late_clidx_to_sample_uidx = (uint32_t*)malloc((sample_ct - cluster_starts[cluster_ct]) * sizeof(int32_t)); |
| 8360 |
if (!late_clidx_to_sample_uidx) { |
| 8361 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 8362 |
} |
| 8363 |
} |
| 8364 |
retval = fill_sample_to_cluster(unfiltered_sample_ct, sample_exclude, sample_ct, cluster_ct, cluster_map, cluster_starts, sample_to_cluster, late_clidx_to_sample_uidx); |
| 8365 |
if (retval) { |
| 8366 |
goto calc_cluster_neighbor_ret_1; |
| 8367 |
} |
| 8368 |
} |
| 8369 |
initial_triangle_size = (((uintptr_t)cur_cluster_ct) * (cur_cluster_ct - 1)) >> 1; |
| 8370 |
heap_size = initial_triangle_size; |
| 8371 |
if (!cluster_sorted_ibs) { |
| 8372 |
|
| 8373 |
if (g_dists) { |
| 8374 |
cluster_sorted_ibs = g_dists; |
| 8375 |
} else { |
| 8376 |
if (bigstack_calloc_d(initial_triangle_size, &cluster_sorted_ibs)) { |
| 8377 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 8378 |
} |
| 8379 |
} |
| 8380 |
bigstack_mark_precluster = g_bigstack_base; |
| 8381 |
} |
| 8382 |
if (do_neighbor) { |
| 8383 |
if (neighbor_n2 >= sample_ct) { |
| 8384 |
logerrprint("Error: Second --neighbour parameter too large (>= population size).\n"); |
| 8385 |
goto calc_cluster_neighbor_ret_INVALID_CMDLINE; |
| 8386 |
} |
| 8387 |
ulii = neighbor_n2 * sample_ct; |
| 8388 |
neighbor_quantiles = (double*)malloc(ulii * sizeof(double)); |
| 8389 |
if (!neighbor_quantiles) { |
| 8390 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 8391 |
} |
| 8392 |
neighbor_qindices = (uint32_t*)malloc(ulii * sizeof(int32_t)); |
| 8393 |
if (!neighbor_qindices) { |
| 8394 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 8395 |
} |
| 8396 |
neighbor_quantile_means = (double*)malloc(neighbor_row_ct * sizeof(double)); |
| 8397 |
if (!neighbor_quantile_means) { |
| 8398 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 8399 |
} |
| 8400 |
neighbor_quantile_stdev_recips = (double*)malloc(neighbor_row_ct * sizeof(double)); |
| 8401 |
if (!neighbor_quantile_stdev_recips) { |
| 8402 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 8403 |
} |
| 8404 |
fill_double_zero(ulii, neighbor_quantiles); |
| 8405 |
} |
| 8406 |
fill_ulong_zero(BITCT_TO_WORDCT(initial_triangle_size), cluster_merge_prevented); |
| 8407 |
if ((min_ppc != 0.0) || genome_main || read_genome_fname) { |
| 8408 |
if (do_neighbor && (min_ppc != 0.0)) { |
| 8409 |
ppc_fail_counts = (uint32_t*)malloc(sample_ct * sizeof(int32_t)); |
| 8410 |
if (!ppc_fail_counts) { |
| 8411 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 8412 |
} |
| 8413 |
fill_uint_zero(sample_ct, ppc_fail_counts); |
| 8414 |
} |
| 8415 |
if (read_genome_fname) { |
| 8416 |
retval = read_genome(read_genome_fname, unfiltered_sample_ct, sample_exclude, sample_ct, sample_ids, max_sample_id_len, cluster_merge_prevented, use_genome_dists? cluster_sorted_ibs : nullptr, neighbor_n2, neighbor_quantiles, neighbor_qindices, ppc_fail_counts, min_ppc, cluster_ct && (!is_group_avg), cluster_ct, cluster_starts, sample_to_cluster); |
| 8417 |
if (retval) { |
| 8418 |
goto calc_cluster_neighbor_ret_1; |
| 8419 |
} |
| 8420 |
} else { |
| 8421 |
|
| 8422 |
|
| 8423 |
|
| 8424 |
|
| 8425 |
|
| 8426 |
if (min_ppc != 0.0) { |
| 8427 |
min_zx = ltqnorm(min_ppc) * sqrt(0.2222222); |
| 8428 |
} |
| 8429 |
ulii = 0; |
| 8430 |
dbl_exclude_ptr = g_missing_dbl_excluded; |
| 8431 |
sample_missing_unwt = g_sample_missing_unwt; |
| 8432 |
for (sample_idx1 = 0; sample_idx1 < sample_ct; sample_idx1++) { |
| 8433 |
sample_missing_ptr = &(sample_missing_unwt[sample_idx1]); |
| 8434 |
uii = marker_ct - (*sample_missing_ptr++); |
| 8435 |
for (sample_idx2 = sample_idx1 + 1; sample_idx2 < sample_ct; sample_idx2++) { |
| 8436 |
if (cluster_ct) { |
| 8437 |
clidx1 = sample_to_cluster[sample_idx1]; |
| 8438 |
clidx2 = sample_to_cluster[sample_idx2]; |
| 8439 |
if (clidx1 < clidx2) { |
| 8440 |
tcoord = tri_coord_no_diag(clidx1, clidx2); |
| 8441 |
} else { |
| 8442 |
tcoord = tri_coord_no_diag(clidx2, clidx1); |
| 8443 |
} |
| 8444 |
} |
| 8445 |
if (use_genome_dists) { |
| 8446 |
dxx = 1.0 - (((double)(genome_main[ulii] + 2 * genome_main[ulii + 1])) / ((double)(2 * (uii - (*sample_missing_ptr++) + (*dbl_exclude_ptr++))))); |
| 8447 |
if (do_neighbor) { |
| 8448 |
update_neighbor(sample_ct, neighbor_n2, sample_idx1, sample_idx2, dxx, neighbor_quantiles, neighbor_qindices); |
| 8449 |
} |
| 8450 |
if (cluster_ct) { |
| 8451 |
if (clidx1 != clidx2) { |
| 8452 |
if (!is_group_avg) { |
| 8453 |
if (dxx < cluster_sorted_ibs[tcoord]) { |
| 8454 |
cluster_sorted_ibs[tcoord] = dxx; |
| 8455 |
} |
| 8456 |
} else { |
| 8457 |
cluster_sorted_ibs[tcoord] += dxx; |
| 8458 |
} |
| 8459 |
} |
| 8460 |
} else { |
| 8461 |
cluster_sorted_ibs[tri_coord_no_diag(sample_idx1, sample_idx2)] = dxx; |
| 8462 |
} |
| 8463 |
if (mds_plot_dmatrix_copy) { |
| 8464 |
mds_plot_dmatrix_copy[tri_coord_no_diag(sample_idx1, sample_idx2)] = dxx; |
| 8465 |
} |
| 8466 |
} |
| 8467 |
if (min_ppc != 0.0) { |
| 8468 |
dxx = (double)genome_main[ulii + 4]; |
| 8469 |
dxx1 = 1.0 / ((double)(genome_main[ulii + 4] + genome_main[ulii + 3])); |
| 8470 |
if (((dxx * dxx1 - 0.666666) / sqrt(dxx1)) < min_zx) { |
| 8471 |
if (do_neighbor) { |
| 8472 |
ppc_fail_counts[sample_idx1] += 1; |
| 8473 |
ppc_fail_counts[sample_idx2] += 1; |
| 8474 |
} |
| 8475 |
if (!cluster_ct) { |
| 8476 |
set_bit_ul(tri_coord_no_diag(sample_idx1, sample_idx2), cluster_merge_prevented); |
| 8477 |
} else { |
| 8478 |
if (clidx1 != clidx2) { |
| 8479 |
SET_BIT(tcoord, cluster_merge_prevented); |
| 8480 |
} else if (!ppc_warning) { |
| 8481 |
logerrprint("Warning: Initial cluster assignment violates PPC test constraint.\n"); |
| 8482 |
ppc_warning = 1; |
| 8483 |
} |
| 8484 |
} |
| 8485 |
} |
| 8486 |
} |
| 8487 |
ulii += 5; |
| 8488 |
} |
| 8489 |
} |
| 8490 |
bigstack_reset(genome_main); |
| 8491 |
} |
| 8492 |
} else if (((!cluster_missing) || do_neighbor) && (!read_dists_fname)) { |
| 8493 |
|
| 8494 |
|
| 8495 |
if (!g_dists) { |
| 8496 |
retval = calc_distance(threads, 0, 1, bedfile, bed_offset, outname, outname_end, nullptr, nullptr, 0.0, 0, DISTANCE_FLAT_MISSING | DISTANCE_CLUSTER, unfiltered_marker_ct, marker_exclude, marker_ct, nullptr, 0, set_allele_freqs, unfiltered_sample_ct, sample_exclude, sample_ct, sample_ids, max_sample_id_len, chrom_info_ptr); |
| 8497 |
if (retval) { |
| 8498 |
goto calc_cluster_neighbor_ret_1; |
| 8499 |
} |
| 8500 |
if (!cluster_ct) { |
| 8501 |
cluster_sorted_ibs = g_dists; |
| 8502 |
} |
| 8503 |
} |
| 8504 |
if (do_neighbor) { |
| 8505 |
dptr = g_dists; |
| 8506 |
for (sample_idx1 = 1; sample_idx1 < sample_ct; sample_idx1++) { |
| 8507 |
for (sample_idx2 = 0; sample_idx2 < sample_idx1; sample_idx2++) { |
| 8508 |
update_neighbor(sample_ct, neighbor_n2, sample_idx1, sample_idx2, *dptr++, neighbor_quantiles, neighbor_qindices); |
| 8509 |
} |
| 8510 |
} |
| 8511 |
} |
| 8512 |
if (mds_fill_nonclust) { |
| 8513 |
memcpy(mds_plot_dmatrix_copy, g_dists, (sample_ct * (sample_ct - 1)) * (sizeof(double) / 2)); |
| 8514 |
} |
| 8515 |
if (cluster_ct) { |
| 8516 |
dptr = g_dists; |
| 8517 |
if (!is_group_avg) { |
| 8518 |
for (sample_idx1 = 1; sample_idx1 < sample_ct; sample_idx1++) { |
| 8519 |
clidx1 = sample_to_cluster[sample_idx1]; |
| 8520 |
for (sample_idx2 = 0; sample_idx2 < sample_idx1; sample_idx2++) { |
| 8521 |
clidx2 = sample_to_cluster[sample_idx2]; |
| 8522 |
if (clidx1 != clidx2) { |
| 8523 |
dxx = *dptr++; |
| 8524 |
if (clidx1 < clidx2) { |
| 8525 |
dptr2 = &(cluster_sorted_ibs[tri_coord_no_diag(clidx1, clidx2)]); |
| 8526 |
} else { |
| 8527 |
dptr2 = &(cluster_sorted_ibs[tri_coord_no_diag(clidx2, clidx1)]); |
| 8528 |
} |
| 8529 |
if (dxx < (*dptr2)) { |
| 8530 |
*dptr2 = dxx; |
| 8531 |
} |
| 8532 |
} else { |
| 8533 |
dptr++; |
| 8534 |
} |
| 8535 |
} |
| 8536 |
} |
| 8537 |
} else { |
| 8538 |
for (sample_idx1 = 1; sample_idx1 < sample_ct; sample_idx1++) { |
| 8539 |
clidx1 = sample_to_cluster[sample_idx1]; |
| 8540 |
for (sample_idx2 = 0; sample_idx2 < sample_idx1; sample_idx2++) { |
| 8541 |
clidx2 = sample_to_cluster[sample_idx2]; |
| 8542 |
if (clidx1 < clidx2) { |
| 8543 |
cluster_sorted_ibs[tri_coord_no_diag(clidx1, clidx2)] += *dptr++; |
| 8544 |
} else if (clidx1 > clidx2) { |
| 8545 |
cluster_sorted_ibs[tri_coord_no_diag(clidx2, clidx1)] += *dptr++; |
| 8546 |
} else { |
| 8547 |
dptr++; |
| 8548 |
} |
| 8549 |
} |
| 8550 |
} |
| 8551 |
} |
| 8552 |
bigstack_reset(g_dists); |
| 8553 |
} |
| 8554 |
} |
| 8555 |
if (read_dists_fname) { |
| 8556 |
retval = read_dists(read_dists_fname, read_dists_id_fname, unfiltered_sample_ct, sample_exclude, sample_ct, sample_ids, max_sample_id_len, cluster_ct, cluster_starts, sample_to_cluster, (calculation_type & (CALC_IBS_TEST | CALC_GROUPDIST | CALC_REGRESS_DISTANCE))? 1 : 0, cluster_ct && (!is_group_avg), cluster_sorted_ibs, neighbor_n2, neighbor_quantiles, neighbor_qindices); |
| 8557 |
if (retval) { |
| 8558 |
goto calc_cluster_neighbor_ret_1; |
| 8559 |
} |
| 8560 |
} |
| 8561 |
sample_idx_to_uidx = (uint32_t*)malloc(sample_ct * sizeof(int32_t)); |
| 8562 |
if (!sample_idx_to_uidx) { |
| 8563 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 8564 |
} |
| 8565 |
fill_idx_to_uidx(sample_exclude, unfiltered_sample_ct, sample_ct, sample_idx_to_uidx); |
| 8566 |
if (do_neighbor) { |
| 8567 |
memcpy(outname_end, ".nearest", 9); |
| 8568 |
if (fopen_checked(outname, "w", &outfile)) { |
| 8569 |
goto calc_cluster_neighbor_ret_OPEN_FAIL; |
| 8570 |
} |
| 8571 |
if (fputs_checked(" FID IID NN MIN_DST Z FID2 IID2 ", outfile)) { |
| 8572 |
goto calc_cluster_neighbor_ret_WRITE_FAIL; |
| 8573 |
} |
| 8574 |
dptr = &(neighbor_quantiles[(neighbor_n1 - 1) * sample_ct]); |
| 8575 |
for (sample_idx1 = neighbor_n1 - 1; sample_idx1 < neighbor_n2; sample_idx1++) { |
| 8576 |
dxx = 0.0; |
| 8577 |
dxx1 = 0.0; |
| 8578 |
for (sample_idx2 = 0; sample_idx2 < sample_ct; sample_idx2++) { |
| 8579 |
dyy = *dptr++; |
| 8580 |
dxx += dyy; |
| 8581 |
dxx1 += dyy * dyy; |
| 8582 |
} |
| 8583 |
dyy = dxx * sample_ct_recip; |
| 8584 |
neighbor_quantile_means[sample_idx1 + 1 - neighbor_n1] = dyy; |
| 8585 |
neighbor_quantile_stdev_recips[sample_idx1 + 1 - neighbor_n1] = sqrt(((double)((intptr_t)(sample_ct - 1))) / (dxx1 - dxx * dyy)); |
| 8586 |
} |
| 8587 |
if (min_ppc != 0.0) { |
| 8588 |
fputs(" PROP_DIFF ", outfile); |
| 8589 |
} |
| 8590 |
putc_unlocked('\n', outfile); |
| 8591 |
dxx1 = 1.0 / ((double)((intptr_t)(sample_ct - 1))); |
| 8592 |
for (sample_idx1 = 0; sample_idx1 < sample_ct; sample_idx1++) { |
| 8593 |
fam_id = &(sample_ids[sample_idx_to_uidx[sample_idx1] * max_sample_id_len]); |
| 8594 |
sample_id = (char*)memchr(fam_id, '\t', max_sample_id_len); |
| 8595 |
wptr_start = fw_strcpyn(12, (uint32_t)(sample_id - fam_id), fam_id, g_textbuf); |
| 8596 |
*wptr_start++ = ' '; |
| 8597 |
wptr_start = fw_strcpy(12, &(sample_id[1]), wptr_start); |
| 8598 |
*wptr_start++ = ' '; |
| 8599 |
if (min_ppc != 0.0) { |
| 8600 |
dyy = ((double)((int32_t)ppc_fail_counts[sample_idx1])) * dxx1; |
| 8601 |
} |
| 8602 |
for (ulii = 0; ulii < neighbor_row_ct; ulii++) { |
| 8603 |
wptr = uint32toa_w6x(ulii + neighbor_n1, ' ', wptr_start); |
| 8604 |
sample_idx2 = sample_idx1 + ulii * sample_ct; |
| 8605 |
dxx = neighbor_quantiles[sample_idx2]; |
| 8606 |
wptr = dtoa_g_wxp4x(dxx, 12, ' ', wptr); |
| 8607 |
wptr = dtoa_g_wxp4x((dxx - neighbor_quantile_means[ulii]) * neighbor_quantile_stdev_recips[ulii], 12, ' ', wptr); |
| 8608 |
sample_idx2 = neighbor_qindices[sample_idx2]; |
| 8609 |
fam_id = &(sample_ids[sample_idx_to_uidx[sample_idx2] * max_sample_id_len]); |
| 8610 |
sample_id = (char*)memchr(fam_id, '\t', max_sample_id_len); |
| 8611 |
wptr = fw_strcpyn(12, (uint32_t)(sample_id - fam_id), fam_id, wptr); |
| 8612 |
*wptr++ = ' '; |
| 8613 |
wptr = fw_strcpy(12, &(sample_id[1]), wptr); |
| 8614 |
*wptr++ = ' '; |
| 8615 |
if (min_ppc != 0.0) { |
| 8616 |
wptr = dtoa_g_wxp4x(dyy, 12, ' ', wptr); |
| 8617 |
} |
| 8618 |
*wptr++ = '\n'; |
| 8619 |
if (fwrite_checked(g_textbuf, wptr - g_textbuf, outfile)) { |
| 8620 |
goto calc_cluster_neighbor_ret_WRITE_FAIL; |
| 8621 |
} |
| 8622 |
} |
| 8623 |
} |
| 8624 |
if (fclose_null(&outfile)) { |
| 8625 |
goto calc_cluster_neighbor_ret_WRITE_FAIL; |
| 8626 |
} |
| 8627 |
LOGPRINTFWW("--neighbour report written to %s .\n", outname); |
| 8628 |
if (!(calculation_type & CALC_CLUSTER)) { |
| 8629 |
goto calc_cluster_neighbor_ret_1; |
| 8630 |
} |
| 8631 |
} |
| 8632 |
if (cluster_missing || ibm_constraint) { |
| 8633 |
if (!g_missing_dbl_excluded) { |
| 8634 |
retval = calc_ibm(threads, bedfile, bed_offset, unfiltered_marker_ct, marker_exclude, marker_ct, unfiltered_sample_ct, sample_exclude, sample_ct, chrom_info_ptr); |
| 8635 |
if (retval) { |
| 8636 |
goto calc_cluster_neighbor_ret_1; |
| 8637 |
} |
| 8638 |
if (!cluster_missing) { |
| 8639 |
logprint("IBM matrix calculated."); |
| 8640 |
fputs(" ", stdout); |
| 8641 |
logprint("\n"); |
| 8642 |
} |
| 8643 |
|
| 8644 |
bigstack_reset(g_missing_dbl_excluded); |
| 8645 |
} |
| 8646 |
dxx1 = 1.0 / ((double)((int32_t)marker_ct)); |
| 8647 |
if (cluster_missing) { |
| 8648 |
memcpy(outname_end, ".mdist.missing", 15); |
| 8649 |
if (fopen_checked(outname, "w", &outfile)) { |
| 8650 |
goto calc_cluster_neighbor_ret_OPEN_FAIL; |
| 8651 |
} |
| 8652 |
fputs("Writing IBM matrix... 0% \b\b\b\b", stdout); |
| 8653 |
fflush(stdout); |
| 8654 |
} |
| 8655 |
sample_missing_unwt = g_sample_missing_unwt; |
| 8656 |
missing_dbl_excluded = g_missing_dbl_excluded; |
| 8657 |
for (sample_idx1 = 0; sample_idx1 < sample_ct; sample_idx1++) { |
| 8658 |
sample_missing_ptr = sample_missing_unwt; |
| 8659 |
uii = sample_missing_ptr[sample_idx1]; |
| 8660 |
if (!cluster_ct) { |
| 8661 |
dptr = &(cluster_sorted_ibs[(sample_idx1 * (sample_idx1 - 1)) >> 1]); |
| 8662 |
} else if (mds_fill_nonclust) { |
| 8663 |
dptr = &(mds_plot_dmatrix_copy[(sample_idx1 * (sample_idx1 - 1)) >> 1]); |
| 8664 |
} |
| 8665 |
ulii = sample_ct - 2; |
| 8666 |
if (!genome_main) { |
| 8667 |
dbl_exclude_ptr = &(missing_dbl_excluded[(sample_idx1 * (sample_idx1 - 1)) >> 1]); |
| 8668 |
if (!cluster_ct) { |
| 8669 |
for (sample_idx2 = 0; sample_idx2 < sample_idx1; sample_idx2++) { |
| 8670 |
dxx = 1.0 - ((double)((int32_t)(uii + (*sample_missing_ptr++) - 2 * (*dbl_exclude_ptr++)))) * dxx1; |
| 8671 |
if (cluster_missing) { |
| 8672 |
*dptr++ = dxx; |
| 8673 |
wptr = dtoa_gx(dxx, ' ', g_textbuf); |
| 8674 |
fwrite(g_textbuf, 1, wptr - g_textbuf, outfile); |
| 8675 |
} |
| 8676 |
if (dxx < min_ibm) { |
| 8677 |
set_bit_ul(tri_coord_no_diag(sample_idx2, sample_idx1), cluster_merge_prevented); |
| 8678 |
} |
| 8679 |
} |
| 8680 |
} else { |
| 8681 |
clidx1 = sample_to_cluster[sample_idx1]; |
| 8682 |
for (sample_idx2 = 0; sample_idx2 < sample_idx1; sample_idx2++) { |
| 8683 |
dxx = 1.0 - ((double)((int32_t)(uii + (*sample_missing_ptr++) - 2 * (*dbl_exclude_ptr++)))) * dxx1; |
| 8684 |
clidx2 = sample_to_cluster[sample_idx2]; |
| 8685 |
if (cluster_missing) { |
| 8686 |
if (mds_fill_nonclust) { |
| 8687 |
*dptr++ = dxx; |
| 8688 |
} |
| 8689 |
if (clidx1 != clidx2) { |
| 8690 |
if (clidx1 < clidx2) { |
| 8691 |
dptr2 = &(cluster_sorted_ibs[tri_coord_no_diag(clidx1, clidx2)]); |
| 8692 |
} else { |
| 8693 |
dptr2 = &(cluster_sorted_ibs[tri_coord_no_diag(clidx2, clidx1)]); |
| 8694 |
} |
| 8695 |
if (!is_group_avg) { |
| 8696 |
if (dxx < (*dptr2)) { |
| 8697 |
*dptr2 = dxx; |
| 8698 |
} |
| 8699 |
} else { |
| 8700 |
*dptr2 += dxx; |
| 8701 |
} |
| 8702 |
} |
| 8703 |
wptr = dtoa_gx(dxx, ' ', g_textbuf); |
| 8704 |
fwrite(g_textbuf, 1, wptr - g_textbuf, outfile); |
| 8705 |
} |
| 8706 |
if (dxx < min_ibm) { |
| 8707 |
if (clidx1 < clidx2) { |
| 8708 |
set_bit_ul(tri_coord_no_diag(clidx1, clidx2), cluster_merge_prevented); |
| 8709 |
} else if (clidx1 > clidx2) { |
| 8710 |
set_bit_ul(tri_coord_no_diag(clidx2, clidx1), cluster_merge_prevented); |
| 8711 |
} else { |
| 8712 |
ibm_warning = 1; |
| 8713 |
} |
| 8714 |
} |
| 8715 |
} |
| 8716 |
} |
| 8717 |
} else { |
| 8718 |
dbl_exclude_ptr = &(missing_dbl_excluded[sample_idx1 - 1]); |
| 8719 |
if (!cluster_ct) { |
| 8720 |
for (sample_idx2 = 0; sample_idx2 < sample_idx1; sample_idx2++) { |
| 8721 |
dxx = 1.0 - ((double)((int32_t)(uii + (*sample_missing_ptr++) - 2 * (*dbl_exclude_ptr)))) * dxx1; |
| 8722 |
dbl_exclude_ptr = &(dbl_exclude_ptr[ulii - sample_idx2]); |
| 8723 |
if (cluster_missing) { |
| 8724 |
*dptr++ = dxx; |
| 8725 |
wptr = dtoa_gx(dxx, ' ', g_textbuf); |
| 8726 |
fwrite(g_textbuf, 1, wptr - g_textbuf, outfile); |
| 8727 |
} |
| 8728 |
if (dxx < min_ibm) { |
| 8729 |
set_bit_ul(tri_coord_no_diag(sample_idx2, sample_idx1), cluster_merge_prevented); |
| 8730 |
} |
| 8731 |
} |
| 8732 |
} else { |
| 8733 |
clidx1 = sample_to_cluster[sample_idx1]; |
| 8734 |
for (sample_idx2 = 0; sample_idx2 < sample_idx1; sample_idx2++) { |
| 8735 |
dxx = 1.0 - ((double)((int32_t)(uii + (*sample_missing_ptr++) - 2 * (*dbl_exclude_ptr)))) * dxx1; |
| 8736 |
dbl_exclude_ptr = &(dbl_exclude_ptr[ulii - sample_idx2]); |
| 8737 |
clidx2 = sample_to_cluster[sample_idx2]; |
| 8738 |
if (cluster_missing) { |
| 8739 |
if (mds_fill_nonclust) { |
| 8740 |
*dptr++ = dxx; |
| 8741 |
} |
| 8742 |
if (clidx1 != clidx2) { |
| 8743 |
if (clidx1 < clidx2) { |
| 8744 |
dptr2 = &(cluster_sorted_ibs[tri_coord_no_diag(clidx1, clidx2)]); |
| 8745 |
} else { |
| 8746 |
dptr2 = &(cluster_sorted_ibs[tri_coord_no_diag(clidx2, clidx1)]); |
| 8747 |
} |
| 8748 |
if (!is_group_avg) { |
| 8749 |
if (dxx < (*dptr2)) { |
| 8750 |
*dptr2 = dxx; |
| 8751 |
} |
| 8752 |
} else { |
| 8753 |
*dptr2 += dxx; |
| 8754 |
} |
| 8755 |
} |
| 8756 |
wptr = dtoa_gx(dxx, ' ', g_textbuf); |
| 8757 |
fwrite(g_textbuf, 1, wptr - g_textbuf, outfile); |
| 8758 |
} |
| 8759 |
if (dxx < min_ibm) { |
| 8760 |
if (clidx1 < clidx2) { |
| 8761 |
set_bit_ul(tri_coord_no_diag(clidx1, clidx2), cluster_merge_prevented); |
| 8762 |
} else if (clidx1 > clidx2) { |
| 8763 |
set_bit_ul(tri_coord_no_diag(clidx2, clidx1), cluster_merge_prevented); |
| 8764 |
} else { |
| 8765 |
ibm_warning = 1; |
| 8766 |
} |
| 8767 |
} |
| 8768 |
} |
| 8769 |
} |
| 8770 |
} |
| 8771 |
if (cluster_missing) { |
| 8772 |
if (putc_checked('1', outfile)) { |
| 8773 |
goto calc_cluster_neighbor_ret_WRITE_FAIL; |
| 8774 |
} |
| 8775 |
putc_unlocked(' ', outfile); |
| 8776 |
if (!genome_main) { |
| 8777 |
for (sample_idx2 = sample_idx1 + 1; sample_idx2 < sample_ct; sample_idx2++) { |
| 8778 |
dxx = 1.0 - ((double)((int32_t)(uii + (*(++sample_missing_ptr)) - 2 * missing_dbl_excluded[((sample_idx2 * (sample_idx2 - 1)) >> 1) + sample_idx1]))) * dxx1; |
| 8779 |
wptr = dtoa_gx(dxx, ' ', g_textbuf); |
| 8780 |
fwrite(g_textbuf, 1, wptr - g_textbuf, outfile); |
| 8781 |
} |
| 8782 |
} else { |
| 8783 |
|
| 8784 |
|
| 8785 |
|
| 8786 |
|
| 8787 |
|
| 8788 |
dbl_exclude_ptr = &(missing_dbl_excluded[sample_ct * sample_idx1 - ((sample_idx1 * (sample_idx1 + 1)) >> 1)]); |
| 8789 |
for (sample_idx2 = sample_idx1 + 1; sample_idx2 < sample_ct; sample_idx2++) { |
| 8790 |
dxx = 1.0 - ((double)((int32_t)(uii + (*(++sample_missing_ptr)) - 2 * (*dbl_exclude_ptr++)))) * dxx1; |
| 8791 |
wptr = dtoa_gx(dxx, ' ', g_textbuf); |
| 8792 |
fwrite(g_textbuf, 1, wptr - g_textbuf, outfile); |
| 8793 |
} |
| 8794 |
} |
| 8795 |
if (putc_checked('\n', outfile)) { |
| 8796 |
goto calc_cluster_neighbor_ret_WRITE_FAIL; |
| 8797 |
} |
| 8798 |
if ((sample_idx1 + 1) * 100LLU >= sample_ct * ((uint64_t)pct)) { |
| 8799 |
if (pct > 10) { |
| 8800 |
putc_unlocked('\b', stdout); |
| 8801 |
} |
| 8802 |
pct = ((sample_idx1 + 1) * 100LLU) / sample_ct; |
| 8803 |
printf("\b\b%u%%", pct++); |
| 8804 |
fflush(stdout); |
| 8805 |
} |
| 8806 |
} |
| 8807 |
} |
| 8808 |
if (cluster_missing) { |
| 8809 |
if (fclose_null(&outfile)) { |
| 8810 |
goto calc_cluster_neighbor_ret_WRITE_FAIL; |
| 8811 |
} |
| 8812 |
putc_unlocked('\r', stdout); |
| 8813 |
LOGPRINTFWW("IBM matrix written to %s .\n", outname); |
| 8814 |
if (ibm_warning) { |
| 8815 |
logerrprint("Warning: Initial cluster assignment violates IBM constraint.\n"); |
| 8816 |
} |
| 8817 |
if (mds_fill_nonclust && (!cluster_ct)) { |
| 8818 |
memcpy(mds_plot_dmatrix_copy, cluster_sorted_ibs, initial_triangle_size * sizeof(double)); |
| 8819 |
} |
| 8820 |
} |
| 8821 |
} |
| 8822 |
if (is_group_avg) { |
| 8823 |
cluster_dist_divide(sample_ct, cluster_ct, cluster_starts, cluster_sorted_ibs); |
| 8824 |
} |
| 8825 |
if (mds_plot_dmatrix_copy && is_mds_cluster) { |
| 8826 |
memcpy(mds_plot_dmatrix_copy, cluster_sorted_ibs, initial_triangle_size * sizeof(double)); |
| 8827 |
} |
| 8828 |
if (pheno_c) { |
| 8829 |
case_ct = popcount_longs(pheno_c, unfiltered_sample_ctl); |
| 8830 |
ctrl_ct = sample_ct - case_ct; |
| 8831 |
if ((cp->modifier & CLUSTER_CC) || (cp->max_cases < case_ct) || (cp->max_ctrls < ctrl_ct)) { |
| 8832 |
if (bigstack_alloc_ul(sample_ctl, &collapsed_pheno_c)) { |
| 8833 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 8834 |
} |
| 8835 |
cur_cluster_case_cts = (uint32_t*)malloc(cur_cluster_ct * sizeof(int32_t)); |
| 8836 |
if (!cur_cluster_case_cts) { |
| 8837 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 8838 |
} |
| 8839 |
copy_bitarr_subset_excl(pheno_c, sample_exclude, unfiltered_sample_ct, sample_ct, collapsed_pheno_c); |
| 8840 |
fill_uint_zero(cur_cluster_ct, cur_cluster_case_cts); |
| 8841 |
if (!cluster_ct) { |
| 8842 |
for (sample_idx1 = 0; sample_idx1 < sample_ct; sample_idx1++) { |
| 8843 |
if (IS_SET(collapsed_pheno_c, sample_idx1)) { |
| 8844 |
cur_cluster_case_cts[sample_idx1] += 1; |
| 8845 |
} |
| 8846 |
} |
| 8847 |
} else { |
| 8848 |
for (sample_idx1 = 0; sample_idx1 < sample_ct; sample_idx1++) { |
| 8849 |
if (IS_SET(collapsed_pheno_c, sample_idx1)) { |
| 8850 |
cur_cluster_case_cts[sample_to_cluster[sample_idx1]] += 1; |
| 8851 |
} |
| 8852 |
} |
| 8853 |
} |
| 8854 |
bigstack_reset(collapsed_pheno_c); |
| 8855 |
} |
| 8856 |
} |
| 8857 |
if (cur_cluster_case_cts || is_group_avg || (cp->max_size < sample_ct)) { |
| 8858 |
cur_cluster_sizes = (uint32_t*)malloc(cur_cluster_ct * sizeof(int32_t)); |
| 8859 |
if (!cur_cluster_sizes) { |
| 8860 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 8861 |
} |
| 8862 |
uii = cp->max_size; |
| 8863 |
for (clidx1 = 0; clidx1 < cluster_ct; clidx1++) { |
| 8864 |
ujj = cluster_starts[clidx1 + 1] - cluster_starts[clidx1]; |
| 8865 |
cur_cluster_sizes[clidx1] = ujj; |
| 8866 |
if (ujj >= uii) { |
| 8867 |
if (ujj != uii) { |
| 8868 |
mc_warning = 1; |
| 8869 |
} |
| 8870 |
fill_bits((clidx1 * (clidx1 - 1)) >> 1, clidx1, cluster_merge_prevented); |
| 8871 |
for (clidx2 = clidx1 + 1; clidx2 < cur_cluster_ct; clidx2++) { |
| 8872 |
set_bit_ul(tri_coord_no_diag(clidx1, clidx2), cluster_merge_prevented); |
| 8873 |
} |
| 8874 |
} else if (ujj > uii / 2) { |
| 8875 |
ujj = uii - ujj; |
| 8876 |
for (clidx2 = 0; clidx2 < clidx1; clidx2++) { |
| 8877 |
if (cluster_starts[clidx2 + 1] - cluster_starts[clidx2] > ujj) { |
| 8878 |
set_bit_ul(tri_coord_no_diag(clidx2, clidx1), cluster_merge_prevented); |
| 8879 |
} |
| 8880 |
} |
| 8881 |
for (clidx2 = clidx1 + 1; clidx2 < cluster_ct; clidx2++) { |
| 8882 |
if (cluster_starts[clidx2 + 1] - cluster_starts[clidx2] > ujj) { |
| 8883 |
set_bit_ul(tri_coord_no_diag(clidx1, clidx2), cluster_merge_prevented); |
| 8884 |
} |
| 8885 |
} |
| 8886 |
} |
| 8887 |
} |
| 8888 |
if (mc_warning) { |
| 8889 |
logerrprint("Warning: Initial cluster assignment violates --mc restriction.\n"); |
| 8890 |
} |
| 8891 |
for (clidx1 = cluster_ct; clidx1 < cur_cluster_ct; clidx1++) { |
| 8892 |
cur_cluster_sizes[clidx1] = 1; |
| 8893 |
} |
| 8894 |
if ((cp->max_cases < case_ct) || (cp->max_ctrls < ctrl_ct)) { |
| 8895 |
uii = cp->max_cases; |
| 8896 |
if (uii < case_ct) { |
| 8897 |
for (clidx1 = 0; clidx1 < cluster_ct; clidx1++) { |
| 8898 |
ujj = cur_cluster_case_cts[clidx1]; |
| 8899 |
if (ujj > uii) { |
| 8900 |
mcc_warning = 1; |
| 8901 |
fill_bits((clidx1 * (clidx1 - 1)) >> 1, clidx1, cluster_merge_prevented); |
| 8902 |
for (clidx2 = clidx1 + 1; clidx2 < cur_cluster_ct; clidx2++) { |
| 8903 |
set_bit_ul(tri_coord_no_diag(clidx1, clidx2), cluster_merge_prevented); |
| 8904 |
} |
| 8905 |
} else if (ujj > uii / 2) { |
| 8906 |
ujj = uii - ujj; |
| 8907 |
for (clidx2 = 0; clidx2 < clidx1; clidx2++) { |
| 8908 |
if (cur_cluster_case_cts[clidx2] > ujj) { |
| 8909 |
set_bit_ul(tri_coord_no_diag(clidx2, clidx1), cluster_merge_prevented); |
| 8910 |
} |
| 8911 |
} |
| 8912 |
for (clidx2 = clidx1 + 1; clidx2 < cluster_ct; clidx2++) { |
| 8913 |
if (cur_cluster_case_cts[clidx2] > ujj) { |
| 8914 |
set_bit_ul(tri_coord_no_diag(clidx1, clidx2), cluster_merge_prevented); |
| 8915 |
} |
| 8916 |
} |
| 8917 |
if (!ujj) { |
| 8918 |
for (clidx2 = cluster_ct; clidx2 < cur_cluster_ct; clidx2++) { |
| 8919 |
if (cur_cluster_case_cts[clidx2]) { |
| 8920 |
set_bit_ul(tri_coord_no_diag(clidx1, clidx2), cluster_merge_prevented); |
| 8921 |
} |
| 8922 |
} |
| 8923 |
} |
| 8924 |
} |
| 8925 |
} |
| 8926 |
if (uii == 1) { |
| 8927 |
for (clidx1 = cluster_ct; clidx1 < cur_cluster_ct; clidx1++) { |
| 8928 |
if (cur_cluster_case_cts[clidx1]) { |
| 8929 |
for (clidx2 = clidx1 + 1; clidx2 < cur_cluster_ct; clidx2++) { |
| 8930 |
if (cur_cluster_case_cts[clidx2]) { |
| 8931 |
set_bit_ul(tri_coord_no_diag(clidx1, clidx2), cluster_merge_prevented); |
| 8932 |
} |
| 8933 |
} |
| 8934 |
} |
| 8935 |
} |
| 8936 |
} |
| 8937 |
} |
| 8938 |
uii = cp->max_ctrls; |
| 8939 |
if (uii < ctrl_ct) { |
| 8940 |
for (clidx1 = 0; clidx1 < cluster_ct; clidx1++) { |
| 8941 |
ujj = cur_cluster_sizes[clidx1] - cur_cluster_case_cts[clidx1]; |
| 8942 |
if (ujj > uii) { |
| 8943 |
mcc_warning = 1; |
| 8944 |
fill_bits((clidx1 * (clidx1 - 1)) >> 1, clidx1, cluster_merge_prevented); |
| 8945 |
for (clidx2 = clidx1 + 1; clidx2 < cur_cluster_ct; clidx2++) { |
| 8946 |
set_bit_ul(tri_coord_no_diag(clidx1, clidx2), cluster_merge_prevented); |
| 8947 |
} |
| 8948 |
} else if (ujj > uii / 2) { |
| 8949 |
ujj = uii - ujj; |
| 8950 |
for (clidx2 = 0; clidx2 < clidx1; clidx2++) { |
| 8951 |
if (cur_cluster_sizes[clidx2] - cur_cluster_case_cts[clidx2] > ujj) { |
| 8952 |
set_bit_ul(tri_coord_no_diag(clidx2, clidx1), cluster_merge_prevented); |
| 8953 |
} |
| 8954 |
} |
| 8955 |
for (clidx2 = clidx1 + 1; clidx2 < cluster_ct; clidx2++) { |
| 8956 |
if (cur_cluster_sizes[clidx2] - cur_cluster_case_cts[clidx2] > ujj) { |
| 8957 |
set_bit_ul(tri_coord_no_diag(clidx1, clidx2), cluster_merge_prevented); |
| 8958 |
} |
| 8959 |
} |
| 8960 |
if (!ujj) { |
| 8961 |
for (clidx2 = cluster_ct; clidx2 < cur_cluster_ct; clidx2++) { |
| 8962 |
if (!cur_cluster_case_cts[clidx2]) { |
| 8963 |
set_bit_ul(tri_coord_no_diag(clidx1, clidx2), cluster_merge_prevented); |
| 8964 |
} |
| 8965 |
} |
| 8966 |
} |
| 8967 |
} |
| 8968 |
} |
| 8969 |
if (uii == 1) { |
| 8970 |
for (clidx1 = cluster_ct; clidx1 < cur_cluster_ct; clidx1++) { |
| 8971 |
if (!cur_cluster_case_cts[clidx1]) { |
| 8972 |
for (clidx2 = clidx1 + 1; clidx2 < cur_cluster_ct; clidx2++) { |
| 8973 |
if (!cur_cluster_case_cts[clidx2]) { |
| 8974 |
set_bit_ul(tri_coord_no_diag(clidx1, clidx2), cluster_merge_prevented); |
| 8975 |
} |
| 8976 |
} |
| 8977 |
} |
| 8978 |
} |
| 8979 |
} |
| 8980 |
} |
| 8981 |
if (mcc_warning) { |
| 8982 |
logerrprint("Warning: Initial cluster assignment violates --mcc restriction.\n"); |
| 8983 |
} |
| 8984 |
} |
| 8985 |
} |
| 8986 |
|
| 8987 |
bigstack_reset(bigstack_mark_precluster); |
| 8988 |
if (cp->match_fname || cp->qmatch_fname) { |
| 8989 |
retval = cluster_enforce_match(cp, missing_pheno, unfiltered_sample_ct, sample_exclude, sample_ct, sample_ids, max_sample_id_len, cluster_ct, cluster_starts, sample_to_cluster, cluster_merge_prevented); |
| 8990 |
if (retval) { |
| 8991 |
goto calc_cluster_neighbor_ret_1; |
| 8992 |
} |
| 8993 |
} |
| 8994 |
|
| 8995 |
tcoord = next_set_ul(cluster_merge_prevented, 0, initial_triangle_size); |
| 8996 |
logprint("Clustering..."); |
| 8997 |
printf(" [sorting IB%c values]", cluster_missing? 'M' : 'S'); |
| 8998 |
fflush(stdout); |
| 8999 |
#ifdef __LP64__ |
| 9000 |
if (cur_cluster_ct <= 65536) { |
| 9001 |
#endif |
| 9002 |
|
| 9003 |
|
| 9004 |
|
| 9005 |
|
| 9006 |
|
| 9007 |
|
| 9008 |
|
| 9009 |
|
| 9010 |
|
| 9011 |
|
| 9012 |
|
| 9013 |
|
| 9014 |
|
| 9015 |
|
| 9016 |
|
| 9017 |
|
| 9018 |
|
| 9019 |
|
| 9020 |
|
| 9021 |
|
| 9022 |
|
| 9023 |
|
| 9024 |
if (tcoord == initial_triangle_size) { |
| 9025 |
umm = cur_cluster_ct; |
| 9026 |
} else { |
| 9027 |
|
| 9028 |
|
| 9029 |
|
| 9030 |
#ifdef __LP64__ |
| 9031 |
umm = (int32_t)sqrt((intptr_t)(tcoord * 2)); |
| 9032 |
#else |
| 9033 |
umm = (int32_t)sqrt(2 * ((double)((intptr_t)tcoord))); |
| 9034 |
#endif |
| 9035 |
if (tcoord >= (umm * (umm + 1)) / 2) { |
| 9036 |
umm++; |
| 9037 |
} |
| 9038 |
heap_size -= popcount_longs_nzbase(cluster_merge_prevented, tcoord / BITCT, BITCT_TO_WORDCT(initial_triangle_size)); |
| 9039 |
} |
| 9040 |
if (!heap_size) { |
| 9041 |
logerrprint("Error: No cluster merges possible.\n"); |
| 9042 |
goto calc_cluster_neighbor_ret_INVALID_CMDLINE; |
| 9043 |
} |
| 9044 |
bigstack_reset(cluster_sorted_ibs); |
| 9045 |
if (bigstack_alloc_ui((3 + is_group_avg) * heap_size + CACHELINE_INT32, &cluster_sorted_ibs_indices)) { |
| 9046 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 9047 |
} |
| 9048 |
ulii = initial_triangle_size - 1; |
| 9049 |
do { |
| 9050 |
cluster_sorted_ibs_indices[ulii * 3 + CACHELINE_INT32] = cluster_sorted_ibs_indices[ulii * 2]; |
| 9051 |
cluster_sorted_ibs_indices[ulii * 3 + CACHELINE_INT32 + 1] = cluster_sorted_ibs_indices[ulii * 2 + 1]; |
| 9052 |
} while (ulii--); |
| 9053 |
|
| 9054 |
uiptr = &(cluster_sorted_ibs_indices[CACHELINE_INT32 + 2]); |
| 9055 |
for (uii = 1; uii < umm; uii++) { |
| 9056 |
ukk = uii << 16; |
| 9057 |
for (ujj = 0; ujj < uii; ujj++) { |
| 9058 |
*uiptr = ukk | ujj; |
| 9059 |
uiptr = &(uiptr[3]); |
| 9060 |
} |
| 9061 |
} |
| 9062 |
if (uii < cur_cluster_ct) { |
| 9063 |
umm = tcoord - ((uii * (uii - 1)) / 2); |
| 9064 |
ukk = uii << 16; |
| 9065 |
for (ujj = 0; ujj < umm; ujj++) { |
| 9066 |
*uiptr = ukk | ujj; |
| 9067 |
uiptr = &(uiptr[3]); |
| 9068 |
} |
| 9069 |
umm = cur_cluster_ct; |
| 9070 |
uiptr = &(uiptr[-2]); |
| 9071 |
do { |
| 9072 |
uiptr2 = &(cluster_sorted_ibs_indices[CACHELINE_INT32 + ((uintptr_t)((uii * (uii - 1)) / 2)) * 3]); |
| 9073 |
ukk = uii << 16; |
| 9074 |
do { |
| 9075 |
if (!IS_SET(cluster_merge_prevented, tcoord)) { |
| 9076 |
*uiptr++ = uiptr2[3 * ujj]; |
| 9077 |
*uiptr++ = uiptr2[3 * ujj + 1]; |
| 9078 |
*uiptr++ = ukk | ujj; |
| 9079 |
} |
| 9080 |
tcoord++; |
| 9081 |
} while ((++ujj) < uii); |
| 9082 |
ujj = 0; |
| 9083 |
} while ((++uii) < umm); |
| 9084 |
} |
| 9085 |
qsort(&(cluster_sorted_ibs_indices[CACHELINE_INT32]), heap_size, 12, double_cmp_decr); |
| 9086 |
if (!is_group_avg) { |
| 9087 |
if (is_old_tiebreaks) { |
| 9088 |
ulii = 1 + (heap_size / BITCT); |
| 9089 |
if (bigstack_calloc_ul(ulii, &ibs_ties)) { |
| 9090 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 9091 |
} |
| 9092 |
|
| 9093 |
uljj = heap_size - 1; |
| 9094 |
for (ulii = 0; ulii < uljj; ulii++) { |
| 9095 |
if ((cluster_sorted_ibs_indices[ulii * 3 + CACHELINE_INT32] == cluster_sorted_ibs_indices[ulii * 3 + 3 + CACHELINE_INT32]) && (cluster_sorted_ibs_indices[ulii * 3 + 1 + CACHELINE_INT32] == cluster_sorted_ibs_indices[ulii * 3 + 4 + CACHELINE_INT32])) { |
| 9096 |
SET_BIT(ulii, ibs_ties); |
| 9097 |
} |
| 9098 |
} |
| 9099 |
} |
| 9100 |
for (ulii = 0; ulii < heap_size; ulii++) { |
| 9101 |
cluster_sorted_ibs_indices[ulii] = cluster_sorted_ibs_indices[CACHELINE_INT32 + 2 + (ulii * 3)]; |
| 9102 |
} |
| 9103 |
if (ulii < cur_cluster_ct) { |
| 9104 |
|
| 9105 |
ulii = cur_cluster_ct; |
| 9106 |
} |
| 9107 |
bigstack_reset((&(cluster_sorted_ibs_indices[round_up_pow2(ulii, CACHELINE_INT32)]))); |
| 9108 |
} else { |
| 9109 |
uiptr = &(cluster_sorted_ibs_indices[CACHELINE_INT32 + 3 * heap_size]); |
| 9110 |
for (ulii = 0; ulii < heap_size; ulii++) { |
| 9111 |
*uiptr++ = cluster_sorted_ibs_indices[CACHELINE_INT32 + 2 + (ulii * 3)]; |
| 9112 |
} |
| 9113 |
uiptr = cluster_sorted_ibs_indices; |
| 9114 |
for (ulii = 0; ulii < heap_size; ulii++) { |
| 9115 |
*uiptr++ = cluster_sorted_ibs_indices[CACHELINE_INT32 + ulii * 3]; |
| 9116 |
*uiptr++ = cluster_sorted_ibs_indices[CACHELINE_INT32 + ulii * 3 + 1]; |
| 9117 |
} |
| 9118 |
bigstack_shrink_top(cluster_sorted_ibs, heap_size * sizeof(double)); |
| 9119 |
memcpy(g_bigstack_base, &(cluster_sorted_ibs_indices[CACHELINE_INT32 + 3 * heap_size]), heap_size * sizeof(int32_t)); |
| 9120 |
ulii = heap_size; |
| 9121 |
if (ulii < cur_cluster_ct) { |
| 9122 |
ulii = cur_cluster_ct; |
| 9123 |
} |
| 9124 |
bigstack_alloc_ui(ulii, &cluster_sorted_ibs_indices); |
| 9125 |
} |
| 9126 |
if (bigstack_calloc_ui(initial_triangle_size, &cluster_index)) { |
| 9127 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 9128 |
} |
| 9129 |
ujj = heap_size; |
| 9130 |
if (!is_group_avg) { |
| 9131 |
for (uii = 0; uii < ujj; uii++) { |
| 9132 |
ukk = cluster_sorted_ibs_indices[uii]; |
| 9133 |
cluster_index[tri_coord_no_diag_32(ukk & 65535, ukk >> 16)] = uii; |
| 9134 |
} |
| 9135 |
} else { |
| 9136 |
for (uii = 0; uii < ujj; uii++) { |
| 9137 |
ukk = cluster_sorted_ibs_indices[uii]; |
| 9138 |
cluster_index[tri_coord_no_diag_32(ukk & 65535, ukk >> 16)] = uii + 1; |
| 9139 |
} |
| 9140 |
} |
| 9141 |
#ifdef __LP64__ |
| 9142 |
} else { |
| 9143 |
logerrprint("Error: --cluster cannot handle >65536 initial clusters yet.\n"); |
| 9144 |
retval = RET_CALC_NOT_YET_SUPPORTED; |
| 9145 |
goto calc_cluster_neighbor_ret_1; |
| 9146 |
} |
| 9147 |
#endif |
| 9148 |
|
| 9149 |
if (bigstack_alloc_ui(2 * (sample_ct - cp->min_ct), &merge_sequence)) { |
| 9150 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 9151 |
} |
| 9152 |
cur_cluster_remap = (uint32_t*)malloc(cur_cluster_ct * sizeof(int32_t)); |
| 9153 |
if (!cur_cluster_remap) { |
| 9154 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 9155 |
} |
| 9156 |
for (clidx1 = 0; clidx1 < cur_cluster_ct; clidx1++) { |
| 9157 |
cur_cluster_remap[clidx1] = clidx1; |
| 9158 |
} |
| 9159 |
|
| 9160 |
if (!is_group_avg) { |
| 9161 |
merge_ct = cluster_main(cur_cluster_ct, cluster_merge_prevented, heap_size, cluster_sorted_ibs_indices, cluster_index, cur_cluster_sizes, sample_ct, cur_cluster_case_cts, case_ct, ctrl_ct, cur_cluster_remap, cp, ibs_ties, merge_sequence); |
| 9162 |
} else { |
| 9163 |
merge_ct = cluster_group_avg_main(cur_cluster_ct, cluster_merge_prevented, heap_size + 1, &(cluster_sorted_ibs[-1]), &(cluster_sorted_ibs_indices[-1]), cluster_index, cur_cluster_sizes, sample_ct, cur_cluster_case_cts, case_ct, ctrl_ct, cur_cluster_remap, cp, merge_sequence); |
| 9164 |
} |
| 9165 |
fputs("\rClustering...", stdout); |
| 9166 |
logprint(" done."); |
| 9167 |
fputs(" ", stdout); |
| 9168 |
logprint("\n"); |
| 9169 |
retval = write_cluster_solution(outname, outname_end, sample_to_cluster, sample_ct, cluster_map, cluster_starts, late_clidx_to_sample_uidx, cluster_ct, cur_cluster_ct, sample_ids, max_sample_id_len, pheno_c, sample_idx_to_uidx, cp, cur_cluster_remap, cluster_sorted_ibs_indices, merge_ct, merge_sequence); |
| 9170 |
if (retval) { |
| 9171 |
goto calc_cluster_neighbor_ret_1; |
| 9172 |
} |
| 9173 |
|
| 9174 |
#ifndef NOLAPACK |
| 9175 |
if (cp->mds_dim_ct) { |
| 9176 |
if (!mds_plot_dmatrix_copy) { |
| 9177 |
|
| 9178 |
bigstack_reset(bigstack_mark_postcluster); |
| 9179 |
if (!is_mds_cluster) { |
| 9180 |
ulii = sample_ct; |
| 9181 |
} else { |
| 9182 |
ulii = cur_cluster_ct; |
| 9183 |
} |
| 9184 |
if (bigstack_alloc_d((ulii * (ulii - 1)) / 2, &mds_plot_dmatrix_copy)) { |
| 9185 |
goto calc_cluster_neighbor_ret_NOMEM; |
| 9186 |
} |
| 9187 |
if (is_mds_cluster || (!read_dists_fname)) { |
| 9188 |
fill_double_zero((ulii * (ulii - 1)) / 2, mds_plot_dmatrix_copy); |
| 9189 |
} |
| 9190 |
if (read_dists_fname) { |
| 9191 |
retval = read_dists(read_dists_fname, read_dists_id_fname, unfiltered_sample_ct, sample_exclude, sample_ct, sample_ids, max_sample_id_len, is_mds_cluster? cluster_ct : 0, is_mds_cluster? cluster_starts : nullptr, is_mds_cluster? sample_to_cluster : nullptr, 2, 0, mds_plot_dmatrix_copy, 0, nullptr, nullptr); |
| 9192 |
} else { |
| 9193 |
retval = read_genome(read_genome_fname, unfiltered_sample_ct, sample_exclude, sample_ct, sample_ids, max_sample_id_len, nullptr, mds_plot_dmatrix_copy, 0, nullptr, nullptr, nullptr, 0.0, 0, is_mds_cluster? cluster_ct : 0, is_mds_cluster? cluster_starts : nullptr, is_mds_cluster? sample_to_cluster : nullptr); |
| 9194 |
} |
| 9195 |
if (retval) { |
| 9196 |
goto calc_cluster_neighbor_ret_1; |
| 9197 |
} |
| 9198 |
} else { |
| 9199 |
bigstack_reset(cluster_merge_prevented); |
| 9200 |
if (is_mds_cluster) { |
| 9201 |
cluster_dist_multiply(sample_ct, cluster_ct, cluster_starts, mds_plot_dmatrix_copy); |
| 9202 |
} |
| 9203 |
} |
| 9204 |
if (cp->modifier & CLUSTER_MDS_EIGENDECOMP) { |
| 9205 |
retval = mds_plot_eigendecomp(outname, outname_end, sample_exclude, sample_ct, sample_idx_to_uidx, sample_ids, plink_maxfid, plink_maxiid, max_sample_id_len, cur_cluster_ct, merge_ct, sample_to_cluster, cur_cluster_remap, cp->mds_dim_ct, is_mds_cluster, cp->modifier & CLUSTER_MDS_EIGVALS, mds_plot_dmatrix_copy); |
| 9206 |
} else { |
| 9207 |
retval = mds_plot(outname, outname_end, sample_exclude, sample_ct, sample_idx_to_uidx, sample_ids, plink_maxfid, plink_maxiid, max_sample_id_len, cur_cluster_ct, merge_ct, sample_to_cluster, cur_cluster_remap, cp->mds_dim_ct, is_mds_cluster, cp->modifier & CLUSTER_MDS_EIGVALS, mds_plot_dmatrix_copy); |
| 9208 |
} |
| 9209 |
if (retval) { |
| 9210 |
goto calc_cluster_neighbor_ret_1; |
| 9211 |
} |
| 9212 |
} |
| 9213 |
#endif |
| 9214 |
|
| 9215 |
while (0) { |
| 9216 |
calc_cluster_neighbor_ret_NOMEM: |
| 9217 |
retval = RET_NOMEM; |
| 9218 |
break; |
| 9219 |
calc_cluster_neighbor_ret_OPEN_FAIL: |
| 9220 |
retval = RET_OPEN_FAIL; |
| 9221 |
break; |
| 9222 |
calc_cluster_neighbor_ret_WRITE_FAIL: |
| 9223 |
retval = RET_WRITE_FAIL; |
| 9224 |
break; |
| 9225 |
calc_cluster_neighbor_ret_INVALID_CMDLINE: |
| 9226 |
retval = RET_INVALID_CMDLINE; |
| 9227 |
break; |
| 9228 |
} |
| 9229 |
calc_cluster_neighbor_ret_1: |
| 9230 |
bigstack_reset(bigstack_mark_postcluster); |
| 9231 |
free_cond(sample_to_cluster); |
| 9232 |
free_cond(neighbor_quantiles); |
| 9233 |
free_cond(neighbor_qindices); |
| 9234 |
free_cond(neighbor_quantile_means); |
| 9235 |
free_cond(neighbor_quantile_stdev_recips); |
| 9236 |
free_cond(ppc_fail_counts); |
| 9237 |
free_cond(cur_cluster_case_cts); |
| 9238 |
free_cond(cur_cluster_sizes); |
| 9239 |
free_cond(sample_idx_to_uidx); |
| 9240 |
free_cond(late_clidx_to_sample_uidx); |
| 9241 |
free_cond(cur_cluster_remap); |
| 9242 |
fclose_cond(outfile); |
| 9243 |
return retval; |
| 9244 |
} |
| 9245 |
|
| 9246 |
double regress_jack(uint32_t jackknife_d, uint32_t sample_ct, double reg_tot_xy, double reg_tot_x, double reg_tot_y, double reg_tot_xx, double reg_tot_yy, uint32_t* uibuf, double* jackknife_precomp, double* dists, double* pheno_packed, double* ret2_ptr) { |
| 9247 |
uint32_t* uiptr = uibuf; |
| 9248 |
uint32_t* uiptr2 = &(uibuf[jackknife_d]); |
| 9249 |
uint32_t uii; |
| 9250 |
uint32_t ujj; |
| 9251 |
uint32_t ukk; |
| 9252 |
double* dptr; |
| 9253 |
double* dptr2; |
| 9254 |
double neg_tot_xy = 0.0; |
| 9255 |
double neg_tot_x = 0.0; |
| 9256 |
double neg_tot_y = 0.0; |
| 9257 |
double neg_tot_xx = 0.0; |
| 9258 |
double neg_tot_yy = 0.0; |
| 9259 |
double dxx; |
| 9260 |
double dxx1; |
| 9261 |
double dyy; |
| 9262 |
while (uiptr < uiptr2) { |
| 9263 |
dptr2 = &(jackknife_precomp[(*uiptr++) * JACKKNIFE_VALS_DIST]); |
| 9264 |
neg_tot_xy += *dptr2++; |
| 9265 |
neg_tot_x += *dptr2++; |
| 9266 |
neg_tot_y += *dptr2++; |
| 9267 |
neg_tot_xx += *dptr2++; |
| 9268 |
neg_tot_yy += *dptr2++; |
| 9269 |
} |
| 9270 |
uiptr = uibuf; |
| 9271 |
for (uii = 1; uii < jackknife_d; uii++) { |
| 9272 |
ujj = *(++uiptr); |
| 9273 |
dxx1 = pheno_packed[ujj]; |
| 9274 |
uiptr2 = uibuf; |
| 9275 |
dptr = &(dists[(((uintptr_t)ujj) * (ujj - 1)) / 2]); |
| 9276 |
while (uiptr2 < uiptr) { |
| 9277 |
ukk = *uiptr2++; |
| 9278 |
dxx = (dxx1 + pheno_packed[ukk]) * 0.5; |
| 9279 |
dyy = dptr[ukk]; |
| 9280 |
neg_tot_xy -= dxx * dyy; |
| 9281 |
neg_tot_x -= dxx; |
| 9282 |
neg_tot_y -= dyy; |
| 9283 |
neg_tot_xx -= dxx * dxx; |
| 9284 |
neg_tot_yy -= dyy * dyy; |
| 9285 |
} |
| 9286 |
} |
| 9287 |
dxx = reg_tot_y - neg_tot_y; |
| 9288 |
dyy = ((int32_t)sample_ct) - ((int32_t)jackknife_d); |
| 9289 |
dyy = dyy * (dyy - 1.0) * 0.5; |
| 9290 |
*ret2_ptr = ((reg_tot_xy - neg_tot_xy) - dxx * (reg_tot_x - neg_tot_x) / dyy) / ((reg_tot_yy - neg_tot_yy) - dxx * dxx / dyy); |
| 9291 |
dxx = reg_tot_x - neg_tot_x; |
| 9292 |
return ((reg_tot_xy - neg_tot_xy) - dxx * (reg_tot_y - neg_tot_y) / dyy) / ((reg_tot_xx - neg_tot_xx) - dxx * dxx / dyy); |
| 9293 |
} |
| 9294 |
|
| 9295 |
THREAD_RET_TYPE regress_jack_thread(void* arg) { |
| 9296 |
uintptr_t tidx = (uintptr_t)arg; |
| 9297 |
uintptr_t jackknife_iters = g_jackknife_iters; |
| 9298 |
uintptr_t sample_ct = g_sample_ct; |
| 9299 |
uint32_t jackknife_d = g_jackknife_d; |
| 9300 |
uint32_t* uibuf = (uint32_t*)(&(g_generic_buf[tidx * round_up_pow2(sample_ct + (jackknife_d + 1) * sizeof(int32_t), CACHELINE)])); |
| 9301 |
unsigned char* cbuf = &(g_generic_buf[tidx * round_up_pow2(sample_ct + (jackknife_d + 1) * sizeof(int32_t), CACHELINE) + (jackknife_d + 1) * sizeof(int32_t)]); |
| 9302 |
uintptr_t uljj = jackknife_iters / 100; |
| 9303 |
double* jackknife_precomp = g_jackknife_precomp; |
| 9304 |
double* dists = g_dists; |
| 9305 |
double* pheno_packed = g_pheno_packed; |
| 9306 |
double reg_tot_xy = g_reg_tot_xy; |
| 9307 |
double reg_tot_x = g_reg_tot_x; |
| 9308 |
double reg_tot_y = g_reg_tot_y; |
| 9309 |
double reg_tot_xx = g_reg_tot_xx; |
| 9310 |
double reg_tot_yy = g_reg_tot_yy; |
| 9311 |
double sum = 0.0; |
| 9312 |
double sum_sq = 0.0; |
| 9313 |
double sum2 = 0; |
| 9314 |
double sum2_sq = 0.0; |
| 9315 |
sfmt_t* sfmtp = g_sfmtp_arr[tidx]; |
| 9316 |
uintptr_t ulii; |
| 9317 |
double dxx; |
| 9318 |
double ret2; |
| 9319 |
for (ulii = 0; ulii < jackknife_iters; ulii++) { |
| 9320 |
pick_d_small(cbuf, uibuf, sample_ct, jackknife_d, sfmtp); |
| 9321 |
dxx = regress_jack(jackknife_d, sample_ct, reg_tot_xy, reg_tot_x, reg_tot_y, reg_tot_xx, reg_tot_yy, uibuf, jackknife_precomp, dists, pheno_packed, &ret2); |
| 9322 |
sum += dxx; |
| 9323 |
sum_sq += dxx * dxx; |
| 9324 |
sum2 += ret2; |
| 9325 |
sum2_sq += ret2 * ret2; |
| 9326 |
if ((!tidx) && (ulii >= uljj)) { |
| 9327 |
uljj = (ulii * 100LLU) / jackknife_iters; |
| 9328 |
printf("\r%" PRIuPTR "%%", uljj); |
| 9329 |
fflush(stdout); |
| 9330 |
uljj = ((uljj + 1LLU) * jackknife_iters) / 100; |
| 9331 |
} |
| 9332 |
} |
| 9333 |
g_calc_result[tidx][0] = sum; |
| 9334 |
g_calc_result[tidx][1] = sum_sq; |
| 9335 |
g_calc_result[tidx][2] = sum2; |
| 9336 |
g_calc_result[tidx][3] = sum2_sq; |
| 9337 |
THREAD_RETURN; |
| 9338 |
} |
| 9339 |
|
| 9340 |
int32_t regress_distance(pthread_t* threads, uint64_t calculation_type, double* pheno_d, uintptr_t unfiltered_sample_ct, uintptr_t* sample_exclude, uintptr_t sample_ct, uint32_t thread_ct, uintptr_t regress_iters, uint32_t regress_d) { |
| 9341 |
unsigned char* bigstack_mark = g_bigstack_base; |
| 9342 |
double reg_tot_xy = 0; |
| 9343 |
double reg_tot_x = 0; |
| 9344 |
double reg_tot_y = 0; |
| 9345 |
double reg_tot_xx = 0; |
| 9346 |
double reg_tot_yy = 0; |
| 9347 |
int32_t retval = 0; |
| 9348 |
uintptr_t ulii; |
| 9349 |
uint32_t uii; |
| 9350 |
double* pheno_packed; |
| 9351 |
double* jackknife_precomp; |
| 9352 |
double* dist_ptr; |
| 9353 |
double* dptr2; |
| 9354 |
double* dptr3; |
| 9355 |
double* dptr4; |
| 9356 |
double* dptr5; |
| 9357 |
double dxx; |
| 9358 |
double dyy; |
| 9359 |
double dzz; |
| 9360 |
double dww; |
| 9361 |
double dvv; |
| 9362 |
double duu; |
| 9363 |
|
| 9364 |
g_sample_ct = sample_ct; |
| 9365 |
|
| 9366 |
|
| 9367 |
pheno_packed = (double*)alloc_and_init_collapsed_arr((char*)pheno_d, sizeof(double), unfiltered_sample_ct, sample_exclude, sample_ct, 1); |
| 9368 |
g_pheno_packed = pheno_packed; |
| 9369 |
if (!(calculation_type & CALC_REGRESS_REL)) { |
| 9370 |
print_pheno_stdev(pheno_packed, sample_ct); |
| 9371 |
} |
| 9372 |
ulii = sample_ct; |
| 9373 |
ulii = ulii * (ulii - 1) / 2; |
| 9374 |
dptr4 = g_dists; |
| 9375 |
dist_ptr = pheno_packed; |
| 9376 |
|
| 9377 |
|
| 9378 |
|
| 9379 |
|
| 9380 |
|
| 9381 |
|
| 9382 |
|
| 9383 |
if (bigstack_calloc_d(sample_ct * JACKKNIFE_VALS_DIST, &jackknife_precomp)) { |
| 9384 |
goto regress_distance_ret_NOMEM; |
| 9385 |
} |
| 9386 |
g_jackknife_precomp = jackknife_precomp; |
| 9387 |
if (bigstack_init_sfmtp(g_thread_ct)) { |
| 9388 |
goto regress_distance_ret_NOMEM; |
| 9389 |
} |
| 9390 |
for (uii = 1; uii < sample_ct; uii++) { |
| 9391 |
dzz = *(++dist_ptr); |
| 9392 |
dptr2 = pheno_packed; |
| 9393 |
dptr3 = &(jackknife_precomp[uii * JACKKNIFE_VALS_DIST]); |
| 9394 |
dptr5 = jackknife_precomp; |
| 9395 |
do { |
| 9396 |
dxx = (dzz + *dptr2++) * 0.5; |
| 9397 |
dyy = (*dptr4++); |
| 9398 |
dww = dxx * dyy; |
| 9399 |
dvv = dxx * dxx; |
| 9400 |
duu = dyy * dyy; |
| 9401 |
reg_tot_xy += dww; |
| 9402 |
*dptr3 += dww; |
| 9403 |
*dptr5 += dww; |
| 9404 |
dptr5++; |
| 9405 |
reg_tot_x += dxx; |
| 9406 |
dptr3[1] += dxx; |
| 9407 |
*dptr5 += dxx; |
| 9408 |
dptr5++; |
| 9409 |
reg_tot_y += dyy; |
| 9410 |
dptr3[2] += dyy; |
| 9411 |
*dptr5 += dyy; |
| 9412 |
dptr5++; |
| 9413 |
reg_tot_xx += dvv; |
| 9414 |
dptr3[3] += dvv; |
| 9415 |
*dptr5 += dvv; |
| 9416 |
dptr5++; |
| 9417 |
reg_tot_yy += duu; |
| 9418 |
dptr3[4] += duu; |
| 9419 |
*dptr5 += duu; |
| 9420 |
dptr5++; |
| 9421 |
} while (dptr2 < dist_ptr); |
| 9422 |
} |
| 9423 |
|
| 9424 |
g_reg_tot_xy = reg_tot_xy; |
| 9425 |
g_reg_tot_x = reg_tot_x; |
| 9426 |
g_reg_tot_y = reg_tot_y; |
| 9427 |
g_reg_tot_xx = reg_tot_xx; |
| 9428 |
g_reg_tot_yy = reg_tot_yy; |
| 9429 |
|
| 9430 |
dxx = ulii; |
| 9431 |
LOGPRINTF("Regression slope (y = genomic distance, x = avg phenotype): %g\n", (reg_tot_xy - reg_tot_x * reg_tot_y / dxx) / (reg_tot_xx - reg_tot_x * reg_tot_x / dxx)); |
| 9432 |
LOGPRINTF("Regression slope (y = avg phenotype, x = genomic distance): %g\n", (reg_tot_xy - reg_tot_x * reg_tot_y / dxx) / (reg_tot_yy - reg_tot_y * reg_tot_y / dxx)); |
| 9433 |
|
| 9434 |
g_jackknife_iters = (regress_iters + thread_ct - 1) / thread_ct; |
| 9435 |
if (regress_d) { |
| 9436 |
g_jackknife_d = regress_d; |
| 9437 |
} else { |
| 9438 |
g_jackknife_d = set_default_jackknife_d(sample_ct); |
| 9439 |
} |
| 9440 |
if (bigstack_alloc_uc(thread_ct * round_up_pow2(sample_ct + (g_jackknife_d + 1) * sizeof(int32_t), CACHELINE), &g_generic_buf)) { |
| 9441 |
goto regress_distance_ret_NOMEM; |
| 9442 |
} |
| 9443 |
if (spawn_threads(threads, ®ress_jack_thread, thread_ct)) { |
| 9444 |
goto regress_distance_ret_THREAD_CREATE_FAIL; |
| 9445 |
} |
| 9446 |
ulii = 0; |
| 9447 |
regress_jack_thread((void*)ulii); |
| 9448 |
dyy = g_calc_result[0][0]; |
| 9449 |
dzz = g_calc_result[0][1]; |
| 9450 |
dww = g_calc_result[0][2]; |
| 9451 |
dvv = g_calc_result[0][3]; |
| 9452 |
join_threads(threads, thread_ct); |
| 9453 |
for (uii = 0; uii < thread_ct - 1; uii++) { |
| 9454 |
dyy += g_calc_result[uii + 1][0]; |
| 9455 |
dzz += g_calc_result[uii + 1][1]; |
| 9456 |
dww += g_calc_result[uii + 1][2]; |
| 9457 |
dvv += g_calc_result[uii + 1][3]; |
| 9458 |
} |
| 9459 |
regress_iters = g_jackknife_iters * thread_ct; |
| 9460 |
putc_unlocked('\r', stdout); |
| 9461 |
LOGPRINTF("Jackknife s.e.: %g\n", sqrt(((sample_ct - g_jackknife_d) / ((double)g_jackknife_d)) * (dzz - dyy * dyy / regress_iters) / (regress_iters - 1))); |
| 9462 |
LOGPRINTF("Jackknife s.e. (y = avg phenotype): %g\n", sqrt(((sample_ct - g_jackknife_d) / ((double)g_jackknife_d)) * (dvv - dww * dww / regress_iters) / (regress_iters - 1))); |
| 9463 |
while (0) { |
| 9464 |
regress_distance_ret_NOMEM: |
| 9465 |
retval = RET_NOMEM; |
| 9466 |
break; |
| 9467 |
regress_distance_ret_THREAD_CREATE_FAIL: |
| 9468 |
retval = RET_THREAD_CREATE_FAIL; |
| 9469 |
} |
| 9470 |
bigstack_reset(bigstack_mark); |
| 9471 |
return retval; |
| 9472 |
} |