| 1 |
#ifndef __PLINK2_GLM_SHARED_H__ |
| 2 |
#define __PLINK2_GLM_SHARED_H__ |
| 3 |
|
| 4 |
// This file is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 5 |
// Christopher Chang. |
| 6 |
// |
| 7 |
// This program is free software: you can redistribute it and/or modify it |
| 8 |
// under the terms of the GNU General Public License as published by the Free |
| 9 |
// Software Foundation, either version 3 of the License, or (at your option) |
| 10 |
// any later version. |
| 11 |
// |
| 12 |
// This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 |
// more details. |
| 16 |
// |
| 17 |
// You should have received a copy of the GNU General Public License |
| 18 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
|
| 21 |
#include "plink2_common.h" |
| 22 |
#include "plink2_matrix.h" |
| 23 |
|
| 24 |
#ifdef __cplusplus |
| 25 |
namespace plink2 { |
| 26 |
#endif |
| 27 |
|
| 28 |
FLAGSET_DEF_START() |
| 29 |
kfGlm0, |
| 30 |
kfGlmZs = (1 << 0), |
| 31 |
|
| 32 |
kfGlmOmitRef = (1 << 1), |
| 33 |
|
| 34 |
// mutually exclusive |
| 35 |
kfGlmSex = (1 << 2), |
| 36 |
kfGlmNoXSex = (1 << 3), |
| 37 |
|
| 38 |
kfGlmLog10 = (1 << 4), |
| 39 |
|
| 40 |
// mutually exclusive |
| 41 |
kfGlmGenotypic = (1 << 5), |
| 42 |
kfGlmHethom = (1 << 6), |
| 43 |
kfGlmDominant = (1 << 7), |
| 44 |
kfGlmRecessive = (1 << 8), |
| 45 |
kfGlmHetonly = (1 << 9), |
| 46 |
|
| 47 |
kfGlmInteraction = (1 << 10), |
| 48 |
kfGlmHideCovar = (1 << 11), |
| 49 |
kfGlmIntercept = (1 << 12), |
| 50 |
kfGlmSkipInvalidPheno = (1 << 13), |
| 51 |
kfGlmNoFirth = (1 << 14), |
| 52 |
kfGlmFirth = (1 << 15), |
| 53 |
kfGlmPerm = (1 << 16), |
| 54 |
kfGlmPermCount = (1 << 17), |
| 55 |
kfGlmConditionDominant = (1 << 18), |
| 56 |
kfGlmConditionRecessive = (1 << 19), |
| 57 |
kfGlmConditionMultiallelic = (1 << 20), |
| 58 |
kfGlmLocalOmitLast = (1 << 21), |
| 59 |
kfGlmTestsAll = (1 << 22), |
| 60 |
kfGlmPhenoIds = (1 << 23), |
| 61 |
kfGlmLocalHaps = (1 << 24), |
| 62 |
kfGlmLocalCats1based = (1 << 25), |
| 63 |
kfGlmFirthResidualize = (1 << 26), |
| 64 |
kfGlmCcResidualize = (1 << 27), |
| 65 |
kfGlmQtResidualize = (1 << 28), |
| 66 |
kfGlmResidualizeMask = (kfGlmFirthResidualize | kfGlmCcResidualize | kfGlmQtResidualize), |
| 67 |
kfGlmSinglePrecCc = (1 << 29), |
| 68 |
kfGlmAllowNoCovars = (1 << 30) |
| 69 |
FLAGSET_DEF_END(GlmFlags); |
| 70 |
|
| 71 |
FLAGSET_DEF_START() |
| 72 |
kfGlmCol0, |
| 73 |
kfGlmColChrom = (1 << 0), |
| 74 |
kfGlmColPos = (1 << 1), |
| 75 |
kfGlmColRef = (1 << 2), |
| 76 |
kfGlmColAlt1 = (1 << 3), |
| 77 |
kfGlmColAlt = (1 << 4), |
| 78 |
kfGlmColMaybeprovref = (1 << 5), |
| 79 |
kfGlmColProvref = (1 << 6), |
| 80 |
kfGlmColOmitted = (1 << 7), |
| 81 |
kfGlmColA1count = (1 << 8), |
| 82 |
kfGlmColTotallele = (1 << 9), |
| 83 |
kfGlmColA1countcc = (1 << 10), |
| 84 |
kfGlmColTotallelecc = (1 << 11), |
| 85 |
kfGlmColGcountcc = (1 << 12), |
| 86 |
kfGlmColA1freq = (1 << 13), |
| 87 |
kfGlmColA1freqcc = (1 << 14), |
| 88 |
kfGlmColMachR2 = (1 << 15), |
| 89 |
kfGlmColFirthYn = (1 << 16), |
| 90 |
kfGlmColTest = (1 << 17), |
| 91 |
kfGlmColNobs = (1 << 18), |
| 92 |
|
| 93 |
// if beta specified, ignore orbeta |
| 94 |
kfGlmColBeta = (1 << 19), |
| 95 |
kfGlmColOrbeta = (1 << 20), |
| 96 |
|
| 97 |
kfGlmColSe = (1 << 21), |
| 98 |
kfGlmColCi = (1 << 22), |
| 99 |
kfGlmColTz = (1 << 23), |
| 100 |
kfGlmColP = (1 << 24), |
| 101 |
kfGlmColErr = (1 << 25), |
| 102 |
|
| 103 |
kfGlmColAx = (1 << 26), |
| 104 |
kfGlmColDefault = (kfGlmColChrom | kfGlmColPos | kfGlmColRef | kfGlmColAlt | kfGlmColProvref | kfGlmColOmitted | kfGlmColA1freq | kfGlmColFirthYn | kfGlmColTest | kfGlmColNobs | kfGlmColOrbeta | kfGlmColSe | kfGlmColCi | kfGlmColTz | kfGlmColP | kfGlmColErr), |
| 105 |
kfGlmColGwasSsfReq = (kfGlmColChrom | kfGlmColPos | kfGlmColRef | kfGlmColAlt | kfGlmColOmitted | kfGlmColA1freq | kfGlmColTest | kfGlmColNobs | kfGlmColSe | kfGlmColCi | kfGlmColTz | kfGlmColP) |
| 106 |
FLAGSET_DEF_END(GlmColFlags); |
| 107 |
|
| 108 |
typedef struct GlmInfoStruct { |
| 109 |
NONCOPYABLE(GlmInfoStruct); |
| 110 |
GlmFlags flags; |
| 111 |
GlmColFlags cols; |
| 112 |
uint32_t mperm_ct; |
| 113 |
uint32_t local_cat_ct; |
| 114 |
uint32_t local_header_line_ct; |
| 115 |
uint32_t local_chrom_col; |
| 116 |
uint32_t local_bp_col; |
| 117 |
uint32_t local_first_covar_col; |
| 118 |
double max_corr; |
| 119 |
char* condition_varname; |
| 120 |
char* condition_list_fname; |
| 121 |
RangeList parameters_range_list; |
| 122 |
RangeList tests_range_list; |
| 123 |
} GlmInfo; |
| 124 |
|
| 125 |
// Useful precomputed values for linear and logistic regression, for variants |
| 126 |
// with no missing genotypes. |
| 127 |
typedef struct { |
| 128 |
double* xtx_image; // (covar_ct + domdev_present + 2)^2, genotype cols empty |
| 129 |
double* covarx_dotprod_inv; // (covar_ct + 1) x (covar_ct + 1), reflected |
| 130 |
double* corr_inv; // covar_ct x covar_ct, reflected |
| 131 |
double* corr_image; // (covar_ct + domdev_present_p1)^2, genotype cols empty |
| 132 |
double* corr_inv_sqrts; // covar_ct x 1 |
| 133 |
double* xt_y_image; // (covar_ct + domdev_present + 2) x 1 |
| 134 |
} RegressionNmPrecomp; |
| 135 |
|
| 136 |
typedef struct GlmCtxStruct { |
| 137 |
const uintptr_t* variant_include; |
| 138 |
const ChrInfo* cip; |
| 139 |
const uintptr_t* allele_idx_offsets; |
| 140 |
const AlleleCode* omitted_alleles; |
| 141 |
const uint32_t* subset_chr_fo_vidx_start; |
| 142 |
const uintptr_t* sample_include; |
| 143 |
const uintptr_t* sample_include_x; |
| 144 |
const uintptr_t* sample_include_y; |
| 145 |
uint32_t* sample_include_cumulative_popcounts; |
| 146 |
const uint32_t* sample_include_x_cumulative_popcounts; |
| 147 |
const uint32_t* sample_include_y_cumulative_popcounts; |
| 148 |
const uintptr_t* sex_male_collapsed; |
| 149 |
uintptr_t* parameter_subset; |
| 150 |
uintptr_t* parameter_subset_x; |
| 151 |
uintptr_t* parameter_subset_y; |
| 152 |
uintptr_t* joint_test_params; |
| 153 |
uintptr_t* joint_test_params_x; |
| 154 |
uintptr_t* joint_test_params_y; |
| 155 |
uint32_t variant_ct; |
| 156 |
uint32_t sample_ct; |
| 157 |
uint32_t sample_ct_x; |
| 158 |
uint32_t sample_ct_y; |
| 159 |
uint32_t max_extra_allele_ct; |
| 160 |
uint32_t covar_ct; |
| 161 |
uint32_t local_covar_ct; |
| 162 |
uint32_t covar_ct_x; |
| 163 |
uint32_t covar_ct_y; |
| 164 |
uint32_t constraint_ct; |
| 165 |
uint32_t constraint_ct_x; |
| 166 |
uint32_t constraint_ct_y; |
| 167 |
uint32_t tests_flag; |
| 168 |
GlmFlags glm_flags; |
| 169 |
uint32_t is_xchr_model_1; |
| 170 |
double max_corr; |
| 171 |
double vif_thresh; |
| 172 |
uintptr_t max_reported_test_ct; |
| 173 |
|
| 174 |
#ifndef NDEBUG |
| 175 |
// temporary debug |
| 176 |
const char* outname; |
| 177 |
#endif |
| 178 |
|
| 179 |
RegressionNmPrecomp* nm_precomp; |
| 180 |
RegressionNmPrecomp* nm_precomp_x; |
| 181 |
RegressionNmPrecomp* nm_precomp_y; |
| 182 |
|
| 183 |
uint32_t cur_block_variant_ct; |
| 184 |
|
| 185 |
PgenReader** pgr_ptrs; |
| 186 |
uintptr_t** genovecs; |
| 187 |
uintptr_t** thread_mhc; |
| 188 |
uintptr_t** dosage_presents; |
| 189 |
Dosage** dosage_mains; |
| 190 |
uint32_t* read_variant_uidx_starts; |
| 191 |
|
| 192 |
unsigned char** workspace_bufs; |
| 193 |
|
| 194 |
double* block_beta_se; |
| 195 |
|
| 196 |
uint64_t err_info; |
| 197 |
} GlmCtx; |
| 198 |
|
| 199 |
// may want to try to identify a specific linear dependency in rank-deficient |
| 200 |
// case |
| 201 |
ENUM_U31_DEF_START() |
| 202 |
kGlmErrcodeNone, |
| 203 |
kGlmErrcodeSampleCtLtePredictorCt, |
| 204 |
kGlmErrcodeConstOmittedAllele, |
| 205 |
kGlmErrcodeConstAllele, |
| 206 |
kGlmErrcodeCorrTooHigh, // 2 predictor args |
| 207 |
kGlmErrcodeVifInfinite, |
| 208 |
kGlmErrcodeVifTooHigh, // 1 predictor arg |
| 209 |
kGlmErrcodeSeparation, // 1 allele arg |
| 210 |
kGlmErrcodeRankDeficient, |
| 211 |
kGlmErrcodeLogisticConvergeFail, |
| 212 |
kGlmErrcodeFirthConvergeFail, |
| 213 |
kGlmErrcodeInvalidResult, |
| 214 |
// no codes for logistic-unfinished and firth-unfinished for now since we |
| 215 |
// still report results there |
| 216 |
|
| 217 |
// only during initial scan |
| 218 |
kGlmErrcodeUnstableScale |
| 219 |
ENUM_U31_DEF_END(GlmErrcode); |
| 220 |
|
| 221 |
#if __cplusplus >= 201103L |
| 222 |
// see IntErr in plink2_base.h |
| 223 |
struct GlmErr { |
| 224 |
GlmErr() {} |
| 225 |
|
| 226 |
GlmErr(uint64_t source) : value_(source) {} |
| 227 |
|
| 228 |
explicit operator uint64_t() const { |
| 229 |
return value_; |
| 230 |
} |
| 231 |
|
| 232 |
explicit operator uint32_t() const { |
| 233 |
return static_cast<uint32_t>(value_); |
| 234 |
} |
| 235 |
|
| 236 |
explicit operator bool() const { |
| 237 |
return (value_ != 0); |
| 238 |
} |
| 239 |
|
| 240 |
private: |
| 241 |
uint64_t value_; |
| 242 |
}; |
| 243 |
#else |
| 244 |
typedef uint64_t GlmErr; |
| 245 |
#endif |
| 246 |
|
| 247 |
// The error code, along with up to two predictor/allele-index arguments, must |
| 248 |
// fit in 8 bytes for now. Since kMaxPhenoCt is larger than 2^16, we need a |
| 249 |
// custom encoding. |
| 250 |
// Current encoding has GlmErrcode in bits 0..7, the first argument in bits |
| 251 |
// 8..31, the second argument in bits 32..55, and the top 8 bits are guaranteed |
| 252 |
// to be zero. |
| 253 |
HEADER_INLINE GlmErr SetGlmErr0(GlmErrcode errcode) { |
| 254 |
return errcode; |
| 255 |
} |
| 256 |
|
| 257 |
HEADER_INLINE GlmErr SetGlmErr1(GlmErrcode errcode, uint32_t arg1) { |
| 258 |
return errcode | (arg1 << 8); |
| 259 |
} |
| 260 |
|
| 261 |
HEADER_INLINE GlmErr SetGlmErr2(GlmErrcode errcode, uint32_t arg1, uint32_t arg2) { |
| 262 |
return S_CAST(uint64_t, errcode) | (arg1 << 8) | (S_CAST(uint64_t, arg2) << 32); |
| 263 |
} |
| 264 |
|
| 265 |
HEADER_INLINE GlmErrcode GetGlmErrCode(GlmErr glm_err) { |
| 266 |
return S_CAST(GlmErrcode, S_CAST(uint64_t, glm_err) & 255); |
| 267 |
} |
| 268 |
|
| 269 |
HEADER_INLINE uint32_t GetGlmErrArg1(GlmErr glm_err) { |
| 270 |
return S_CAST(uint32_t, glm_err) >> 8; |
| 271 |
} |
| 272 |
|
| 273 |
HEADER_INLINE uint32_t GetGlmErrArg2(GlmErr glm_err) { |
| 274 |
return S_CAST(uint64_t, glm_err) >> 32; |
| 275 |
} |
| 276 |
|
| 277 |
char* AppendGlmErrstr(GlmErr glm_err, char* write_iter); |
| 278 |
|
| 279 |
GlmErr CheckMaxCorrAndVif(const double* predictor_dotprods, uint32_t first_predictor_idx, uint32_t predictor_ct, uintptr_t sample_ct, double max_corr, double vif_thresh, double* dbl_2d_buf, double* corr_buf, double* inverse_corr_buf, MatrixInvertBuf1* matrix_invert_buf1); |
| 280 |
|
| 281 |
GlmErr CheckMaxCorrAndVifNm(const double* predictor_dotprods, const double* corr_inv, uint32_t predictor_ct, uint32_t geno_pred_ct, double sample_ct_recip, double sample_ct_m1_recip, double max_corr, double vif_thresh, double* __restrict semicomputed_corr_matrix, double* __restrict semicomputed_inv_corr_sqrts, double* __restrict corr_row_buf, double* __restrict inverse_corr_diag, double* __restrict ainv_b_buf); |
| 282 |
|
| 283 |
PglErr GlmFillAndTestCovars(const uintptr_t* sample_include, const uintptr_t* covar_include, const PhenoCol* covar_cols, const char* covar_names, uintptr_t sample_ct, uintptr_t covar_ct, uint32_t local_covar_ct, uint32_t covar_max_nonnull_cat_ct, uintptr_t extra_cat_ct, uintptr_t max_covar_name_blen, double max_corr, double vif_thresh, double* covar_dotprod, double* corr_buf, double* inverse_corr_buf, double* covars_cmaj, const char** cur_covar_names, GlmErr* glm_err_ptr); |
| 284 |
|
| 285 |
BoolErr InitNmPrecomp(const double* covars_cmaj, const double* covar_dotprod, const double* corr_buf, const double* corr_inv_tri, uint32_t sample_ct, uint32_t is_qt, uintptr_t new_covar_ct, uintptr_t xtx_state, RegressionNmPrecomp* nm_precomp); |
| 286 |
|
| 287 |
uint32_t DosageIsConstant(uint64_t dosage_sum, uint64_t dosage_ssq, uint32_t nm_sample_ct); |
| 288 |
|
| 289 |
uint32_t GetBiallelicReportedTestCt(const uintptr_t* parameter_subset, GlmFlags glm_flags, uint32_t covar_ct, uint32_t tests_flag); |
| 290 |
|
| 291 |
PglErr ReadLocalCovarBlock(const GlmCtx* common, const uint32_t* local_sample_uidx_order, const uintptr_t* local_variant_include, uint32_t variant_uidx_start, uint32_t variant_uidx_end, uint32_t cur_block_variant_ct, uint32_t local_sample_ct, uint32_t local_cat_ct, TextStream* local_covar_txsp, uint32_t* local_line_idx_ptr, uint32_t* local_xy_ptr, float* local_covars_vcmaj_f_iter, double* local_covars_vcmaj_d_iter, uint32_t* local_sample_idx_order); |
| 292 |
|
| 293 |
PglErr ReadRfmix2Block(const GlmCtx* common, const uint32_t* variant_bps, const uint32_t* local_sample_uidx_order, const float* prev_local_covar_row_f, const double* prev_local_covar_row_d, uint32_t variant_uidx_start, uint32_t variant_uidx_end, uint32_t cur_block_variant_ct, uint32_t local_sample_ct, uint32_t local_cat_ct, uint32_t local_chrom_col, uint32_t local_bp_col, uint32_t local_first_covar_col, TextStream* local_covar_txsp, const char** local_line_iterp, uint32_t* local_line_idx_ptr, uint32_t* local_prev_chr_code_ptr, uint32_t* local_chr_code_ptr, uint32_t* local_bp_ptr, uint32_t* local_skip_chr_ptr, float* local_covars_vcmaj_f_iter, double* local_covars_vcmaj_d_iter, uint32_t* local_sample_idx_order); |
| 294 |
|
| 295 |
extern const double kSmallDoublePairs[32]; |
| 296 |
|
| 297 |
extern const double kSmallInvDoublePairs[32]; |
| 298 |
|
| 299 |
extern const double kSmallInvDoubles[4]; |
| 300 |
|
| 301 |
uint32_t GenoarrToDoublesRemoveMissing(const uintptr_t* genoarr, const double* __restrict table, uint32_t sample_ct, double* __restrict dst); |
| 302 |
|
| 303 |
BoolErr LinearHypothesisChisq(const double* coef, const double* constraints_con_major, const double* cov_matrix, uintptr_t constraint_ct, uintptr_t predictor_ct, uintptr_t cov_stride, double* chisq_ptr, double* tmphxs_buf, double* h_transpose_buf, double* inner_buf, MatrixInvertBuf1* mi_buf, double* outer_buf); |
| 304 |
|
| 305 |
#ifdef __cplusplus |
| 306 |
} // namespace plink2 |
| 307 |
#endif |
| 308 |
|
| 309 |
#endif // __PLINK2_GLM_SHARED_H__ |