| 1 |
#ifndef __PLINK2_MATRIX_CALC_H__ |
| 2 |
#define __PLINK2_MATRIX_CALC_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 "include/SFMT.h" |
| 23 |
|
| 24 |
#ifdef __cplusplus |
| 25 |
namespace plink2 { |
| 26 |
#endif |
| 27 |
|
| 28 |
FLAGSET_DEF_START() |
| 29 |
kfKing0, |
| 30 |
kfKingMatrixZs = (1 << 0), |
| 31 |
kfKingMatrixBin = (1 << 1), |
| 32 |
kfKingMatrixBin4 = (1 << 2), |
| 33 |
kfKingMatrixEncodemask = (kfKingMatrixZs | kfKingMatrixBin | kfKingMatrixBin4), |
| 34 |
kfKingMatrixSq = (1 << 3), |
| 35 |
kfKingMatrixSq0 = (1 << 4), |
| 36 |
kfKingMatrixTri = (1 << 5), |
| 37 |
kfKingMatrixShapemask = (kfKingMatrixSq0 | kfKingMatrixSq | kfKingMatrixTri), |
| 38 |
|
| 39 |
kfKingTableZs = (1 << 6), |
| 40 |
kfKingCounts = (1 << 7), |
| 41 |
kfKingRelCheck = (1 << 8), |
| 42 |
kfKingCutoffTable = (1 << 9), |
| 43 |
kfKingTableRequireXor = (1 << 10), |
| 44 |
|
| 45 |
kfKingColMaybefid = (1 << 11), |
| 46 |
kfKingColFid = (1 << 12), |
| 47 |
kfKingColId = (1 << 13), |
| 48 |
kfKingColMaybesid = (1 << 14), |
| 49 |
kfKingColSid = (1 << 15), |
| 50 |
kfKingColNsnp = (1 << 16), |
| 51 |
kfKingColHethet = (1 << 17), |
| 52 |
kfKingColIbs0 = (1 << 18), |
| 53 |
kfKingColIbs1 = (1 << 19), |
| 54 |
kfKingColKinship = (1 << 20), |
| 55 |
kfKingColDefault = (kfKingColMaybefid | kfKingColId | kfKingColMaybesid | kfKingColNsnp | kfKingColHethet | kfKingColIbs0 | kfKingColKinship), |
| 56 |
kfKingColAll = ((kfKingColKinship * 2) - kfKingColMaybefid) |
| 57 |
FLAGSET_DEF_END(KingFlags); |
| 58 |
|
| 59 |
FLAGSET_DEF_START() |
| 60 |
kfGrm0, |
| 61 |
kfGrmMatrixZs = (1 << 0), |
| 62 |
kfGrmMatrixBin = (1 << 1), |
| 63 |
kfGrmMatrixBin4 = (1 << 2), |
| 64 |
kfGrmMatrixEncodemask = (kfGrmMatrixZs | kfGrmMatrixBin | kfGrmMatrixBin4), |
| 65 |
kfGrmMatrixSq = (1 << 3), |
| 66 |
kfGrmMatrixSq0 = (1 << 4), |
| 67 |
kfGrmMatrixTri = (1 << 5), |
| 68 |
kfGrmMatrixShapemask = (kfGrmMatrixSq0 | kfGrmMatrixSq | kfGrmMatrixTri), |
| 69 |
kfGrmListNoGz = (1 << 6), |
| 70 |
kfGrmListZs = (1 << 7), |
| 71 |
kfGrmListmask = (kfGrmListNoGz | kfGrmListZs), |
| 72 |
kfGrmBin = (1 << 8), |
| 73 |
|
| 74 |
kfGrmMeanimpute = (1 << 9), |
| 75 |
kfGrmCov = (1 << 10), |
| 76 |
kfGrmNoIdHeader = (1 << 11), |
| 77 |
kfGrmNoIdHeaderIidOnly = (1 << 12) |
| 78 |
FLAGSET_DEF_END(GrmFlags); |
| 79 |
|
| 80 |
FLAGSET_DEF_START() |
| 81 |
kfPca0, |
| 82 |
kfPcaApprox = (1 << 0), |
| 83 |
kfPcaMeanimpute = (1 << 1), |
| 84 |
kfPcaAlleleWts = (1 << 2), |
| 85 |
kfPcaBiallelicVarWts = (1 << 3), |
| 86 |
kfPcaVarZs = (1 << 4), |
| 87 |
|
| 88 |
kfPcaScolMaybefid = (1 << 5), |
| 89 |
kfPcaScolFid = (1 << 6), |
| 90 |
kfPcaScolMaybesid = (1 << 7), |
| 91 |
kfPcaScolSid = (1 << 8), |
| 92 |
kfPcaScolDefault = (kfPcaScolMaybefid | kfPcaScolMaybesid), |
| 93 |
kfPcaScolAll = ((kfPcaScolSid * 2) - kfPcaScolMaybefid), |
| 94 |
|
| 95 |
kfPcaVcolChrom = (1 << 9), |
| 96 |
kfPcaVcolPos = (1 << 10), |
| 97 |
kfPcaVcolRef = (1 << 11), |
| 98 |
kfPcaVcolAlt1 = (1 << 12), |
| 99 |
kfPcaVcolAlt = (1 << 13), |
| 100 |
kfPcaVcolMaybeprovref = (1 << 14), |
| 101 |
kfPcaVcolProvref = (1 << 15), |
| 102 |
kfPcaVcolAx = (1 << 16), |
| 103 |
kfPcaVcolMaj = (1 << 17), |
| 104 |
kfPcaVcolNonmaj = (1 << 18), |
| 105 |
kfPcaVcolDefaultA = (kfPcaVcolChrom | kfPcaVcolRef | kfPcaVcolAlt | kfPcaVcolMaybeprovref), |
| 106 |
kfPcaVcolDefaultB = (kfPcaVcolChrom | kfPcaVcolMaybeprovref | kfPcaVcolMaj | kfPcaVcolNonmaj), |
| 107 |
kfPcaVcolAll = ((kfPcaVcolNonmaj * 2) - kfPcaVcolChrom) |
| 108 |
FLAGSET_DEF_END(PcaFlags); |
| 109 |
|
| 110 |
FLAGSET_DEF_START() |
| 111 |
kfScore0, |
| 112 |
kfScoreHeaderIgnore = (1 << 0), |
| 113 |
kfScoreHeaderRead = (1 << 1), |
| 114 |
kfScoreNoMeanimpute = (1 << 2), |
| 115 |
kfScoreDominant = (1 << 3), |
| 116 |
kfScoreRecessive = (1 << 4), |
| 117 |
kfScoreCenter = (1 << 5), |
| 118 |
kfScoreVarianceStandardize = (1 << 6), |
| 119 |
kfScoreSe = (1 << 7), |
| 120 |
kfScoreZs = (1 << 8), |
| 121 |
kfScoreIgnoreDupIds = (1 << 9), |
| 122 |
kfScoreListVariants = (1 << 10), |
| 123 |
kfScoreListVariantsZs = (1 << 11), |
| 124 |
kfScoreMultiInput = (1 << 12), |
| 125 |
|
| 126 |
kfScoreQsrHeader = (1 << 13), |
| 127 |
kfScoreQsrMin = (1 << 14), |
| 128 |
|
| 129 |
kfScoreColMaybefid = (1 << 15), |
| 130 |
kfScoreColFid = (1 << 16), |
| 131 |
kfScoreColMaybesid = (1 << 17), |
| 132 |
kfScoreColSid = (1 << 18), |
| 133 |
kfScoreColPheno1 = (1 << 19), |
| 134 |
kfScoreColPhenos = (1 << 20), |
| 135 |
kfScoreColNallele = (1 << 21), |
| 136 |
kfScoreColDenom = (1 << 22), |
| 137 |
kfScoreColDosageSum = (1 << 23), |
| 138 |
kfScoreColScoreAvgs = (1 << 24), |
| 139 |
kfScoreColScoreSums = (1 << 25), |
| 140 |
kfScoreListColDefault = (kfScoreColMaybefid | kfScoreColMaybesid | kfScoreColPhenos | kfScoreColScoreAvgs), |
| 141 |
kfScoreColDefault = (kfScoreListColDefault | kfScoreColNallele | kfScoreColDosageSum), |
| 142 |
kfScoreColAll = ((kfScoreColScoreSums * 2) - kfScoreColMaybefid) |
| 143 |
FLAGSET_DEF_END(ScoreFlags); |
| 144 |
|
| 145 |
FLAGSET_DEF_START() |
| 146 |
kfVscore0, |
| 147 |
kfVscoreZs = (1 << 0), |
| 148 |
kfVscoreSinglePrec = (1 << 1), |
| 149 |
kfVscoreBin = (1 << 2), |
| 150 |
kfVscoreBin4 = (1 << 3), |
| 151 |
|
| 152 |
kfVscoreColChrom = (1 << 4), |
| 153 |
kfVscoreColPos = (1 << 5), |
| 154 |
kfVscoreColRef = (1 << 6), |
| 155 |
kfVscoreColAlt1 = (1 << 7), |
| 156 |
kfVscoreColAlt = (1 << 8), |
| 157 |
kfVscoreColMaybeprovref = (1 << 9), |
| 158 |
kfVscoreColProvref = (1 << 10), |
| 159 |
kfVscoreColAltfreq = (1 << 11), |
| 160 |
kfVscoreColNmiss = (1 << 12), |
| 161 |
kfVscoreColNobs = (1 << 13), |
| 162 |
kfVscoreColDefault = (kfVscoreColChrom | kfVscoreColPos | kfVscoreColRef | kfVscoreColAlt | kfVscoreColMaybeprovref), |
| 163 |
kfVscoreColAll = ((kfVscoreColNobs * 2) - kfVscoreColChrom) |
| 164 |
FLAGSET_DEF_END(VscoreFlags); |
| 165 |
|
| 166 |
typedef struct ScoreInfoStruct { |
| 167 |
NONCOPYABLE(ScoreInfoStruct); |
| 168 |
ScoreFlags flags; |
| 169 |
uint32_t varid_col_p1; |
| 170 |
uint32_t allele_col_p1; |
| 171 |
char* input_fname; |
| 172 |
RangeList input_col_idx_range_list; |
| 173 |
|
| 174 |
// --q-score-range |
| 175 |
char* qsr_range_fname; |
| 176 |
char* qsr_data_fname; |
| 177 |
uint32_t qsr_varid_col_p1; |
| 178 |
uint32_t qsr_val_col_p1; |
| 179 |
} ScoreInfo; |
| 180 |
|
| 181 |
FLAGSET_DEF_START() |
| 182 |
kfPhenoSvd0, |
| 183 |
kfPhenoSvdForce = (1 << 0), |
| 184 |
|
| 185 |
kfPhenoSvdScolMaybefid = (1 << 1), |
| 186 |
kfPhenoSvdScolFid = (1 << 2), |
| 187 |
kfPhenoSvdScolMaybesid = (1 << 3), |
| 188 |
kfPhenoSvdScolSid = (1 << 4), |
| 189 |
kfPhenoSvdScolDefault = (kfPhenoSvdScolMaybefid | kfPhenoSvdScolMaybesid), |
| 190 |
kfPhenoSvdScolAll = ((kfPhenoSvdScolSid * 2) - kfPhenoSvdScolMaybefid), |
| 191 |
|
| 192 |
kfPhenoSvdPcolId = (1 << 5), |
| 193 |
kfPhenoSvdPcolSv = (1 << 6), |
| 194 |
kfPhenoSvdPcolDefault = (kfPhenoSvdPcolId | kfPhenoSvdPcolSv), |
| 195 |
kfPhenoSvdPcolAll = ((kfPhenoSvdPcolSv * 2) - kfPhenoSvdPcolId) |
| 196 |
FLAGSET_DEF_END(PhenoSvdFlags); |
| 197 |
|
| 198 |
typedef struct PhenoSvdInfoStruct { |
| 199 |
NONCOPYABLE(PhenoSvdInfoStruct); |
| 200 |
PhenoSvdFlags flags; |
| 201 |
uint32_t ct; |
| 202 |
double min_variance_explained; |
| 203 |
} PhenoSvdInfo; |
| 204 |
|
| 205 |
void InitScore(ScoreInfo* score_info_ptr); |
| 206 |
|
| 207 |
void CleanupScore(ScoreInfo* score_info_ptr); |
| 208 |
|
| 209 |
void InitPhenoSvd(PhenoSvdInfo* pheno_svd_info_ptr); |
| 210 |
|
| 211 |
CONSTI32(kMaxPc, 8000); |
| 212 |
|
| 213 |
PglErr KingCutoffBatchBinary(const SampleIdInfo* siip, uint32_t raw_sample_ct, double king_cutoff, uintptr_t* sample_include, char* king_cutoff_fprefix, uint32_t* sample_ct_ptr); |
| 214 |
|
| 215 |
PglErr KingCutoffBatchTable(const SampleIdInfo* siip, const char* kin0_fname, uint32_t raw_sample_ct, double king_cutoff, uintptr_t* sample_include, uint32_t* sample_ct_ptr); |
| 216 |
|
| 217 |
PglErr CalcKing(const SampleIdInfo* siip, const uintptr_t* variant_include_orig, const ChrInfo* cip, uint32_t raw_sample_ct, uint32_t orig_sample_ct, uint32_t raw_variant_ct, uint32_t variant_ct, double king_cutoff, double king_table_filter, KingFlags king_flags, uint32_t parallel_idx, uint32_t parallel_tot, uint32_t max_thread_ct, uintptr_t pgr_alloc_cacheline_ct, PgenFileInfo* pgfip, PgenReader* simple_pgrp, uintptr_t* sample_include, uint32_t* sample_ct_ptr, char* outname, char* outname_end); |
| 218 |
|
| 219 |
PglErr CalcKingTableSubset(const uintptr_t* orig_sample_include, const SampleIdInfo* siip, const uintptr_t* variant_include, const ChrInfo* cip, const char* subset_fname, const char* require_fnames, uint32_t raw_sample_ct, uint32_t orig_sample_ct, uint32_t raw_variant_ct, uint32_t variant_ct, double king_table_filter, double king_table_subset_thresh, uint32_t rel_check, KingFlags king_flags, uint32_t parallel_idx, uint32_t parallel_tot, uint32_t max_thread_ct, PgenReader* simple_pgrp, char* outname, char* outname_end); |
| 220 |
|
| 221 |
PglErr CalcGrm(const uintptr_t* orig_sample_include, const SampleIdInfo* siip, const uintptr_t* variant_include, const ChrInfo* cip, const uintptr_t* allele_idx_offsets, const double* allele_freqs, uint32_t raw_sample_ct, uint32_t sample_ct, uint32_t raw_variant_ct, uint32_t variant_ct, uint32_t max_allele_ct, GrmFlags grm_flags, uint32_t parallel_idx, uint32_t parallel_tot, uint32_t max_thread_ct, PgenReader* simple_pgrp, char* outname, char* outname_end, double** grm_ptr); |
| 222 |
|
| 223 |
#ifndef NOLAPACK |
| 224 |
PglErr CalcPca(const uintptr_t* sample_include, const SampleIdInfo* siip, const uintptr_t* variant_include, const ChrInfo* cip, const uint32_t* variant_bps, const char* const* variant_ids, const uintptr_t* allele_idx_offsets, const char* const* allele_storage, const AlleleCode* maj_alleles, const double* allele_freqs, uint32_t raw_sample_ct, uintptr_t pca_sample_ct, uint32_t raw_variant_ct, uint32_t variant_ct, uint32_t max_allele_ct, uint32_t max_allele_slen, uint32_t pc_ct, PcaFlags pca_flags, uint32_t max_thread_ct, PgenReader* simple_pgrp, sfmt_t* sfmtp, double* grm, char* outname, char* outname_end); |
| 225 |
#endif |
| 226 |
|
| 227 |
PglErr ScoreReport(const uintptr_t* sample_include, const SampleIdInfo* siip, const uintptr_t* sex_nm, const uintptr_t* sex_male, const PhenoCol* pheno_cols, const char* pheno_names, const uintptr_t* variant_include, const ChrInfo* cip, const char* const* variant_ids, const uintptr_t* allele_idx_offsets, const char* const* allele_storage, const double* allele_freqs, const ScoreInfo* score_info_ptr, const char* output_missing_pheno, uint32_t raw_sample_ct, uint32_t sample_ct, uint32_t nosex_ct, uint32_t pheno_ct, uintptr_t max_pheno_name_blen, uint32_t raw_variant_ct, uint32_t variant_ct, uint32_t max_variant_id_slen, uint32_t xchr_model, uint32_t max_thread_ct, PgenReader* simple_pgrp, char* outname, char* outname_end); |
| 228 |
|
| 229 |
PglErr Vscore(const uintptr_t* variant_include, const ChrInfo* cip, const uint32_t* variant_bps, const char* const* variant_ids, const uintptr_t* allele_idx_offsets, const char* const* allele_storage, const uintptr_t* sample_include, const SampleIdInfo* siip, const uintptr_t* sex_male, const double* allele_freqs, const char* in_fname, const RangeList* col_idx_range_listp, uint32_t raw_variant_ct, uint32_t variant_ct, uint32_t raw_sample_ct, uint32_t sample_ct, uint32_t nosex_ct, uint32_t max_allele_slen, VscoreFlags flags, uint32_t xchr_model, uint32_t max_thread_ct, uintptr_t pgr_alloc_cacheline_ct, PgenFileInfo* pgfip, char* outname, char* outname_end); |
| 230 |
|
| 231 |
#ifndef NOLAPACK |
| 232 |
PglErr PhenoSvd(const PhenoSvdInfo* psip, const uintptr_t* sample_include, const SampleIdInfo* siip, uint32_t raw_sample_ct, uint32_t orig_sample_ct, uint32_t max_thread_ct, char** pheno_names_ptr, uint32_t* pheno_ct_ptr, uintptr_t* max_pheno_name_blen_ptr, PhenoCol* pheno_cols, char* outname, char* outname_end); |
| 233 |
#endif |
| 234 |
|
| 235 |
#ifdef __cplusplus |
| 236 |
} // namespace plink2 |
| 237 |
#endif |
| 238 |
|
| 239 |
#endif // __PLINK2_MATRIX_CALC_H__ |