Debian Codegraph @

plink2_data.h

raw
1 #ifndef __PLINK2_DATA_H__
2 #define __PLINK2_DATA_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
23 #ifdef __cplusplus
24 namespace plink2 {
25 #endif
26
27 FLAGSET_DEF_START()
28 kfMake0,
29 kfMakeBed = (1 << 0),
30 kfMakeBim = (1 << 1),
31 kfMakeFam = (1 << 2),
32 kfMakePgen = (1 << 3),
33 kfMakePvar = (1 << 4),
34 kfMakePsam = (1 << 5),
35 kfMakeBimZs = (1 << 6),
36 kfMakePlink2MSplitBase = (1 << 7), // three bits for multiallelic mode
37 kfMakePlink2MSplitAll = kfMakePlink2MSplitBase,
38 kfMakePlink2MSplitSnps = 2 * kfMakePlink2MSplitBase,
39 kfMakePlink2MJoin = (1 << 9),
40 kfMakePlink2MJoinBoth = kfMakePlink2MJoin,
41 kfMakePlink2MJoinSnps = kfMakePlink2MJoin + kfMakePlink2MSplitBase,
42 kfMakePlink2MJoinAny = kfMakePlink2MJoin + 2 * kfMakePlink2MSplitBase,
43 // don't support e.g. '+indels' for now due to lack of standardization re:
44 // handling of MNP/'other' classes
45 kfMakePlink2TrimAlts = (1 << 10),
46 kfMakePlink2MMask = kfMakePlink2TrimAlts - kfMakePlink2MSplitBase,
47 kfMakePlink2EraseAlt2Plus = (1 << 11),
48 kfMakePlink2VaridSemicolon = (1 << 12),
49 kfMakePlink2VaridDup = (1 << 13),
50
51 kfMakePlink2SetInvalidHaploidMissing = (1 << 14),
52 kfMakePlink2SetInvalidHaploidMissingKeepDosage = (1 << 15),
53 kfMakePlink2SetMixedMtMissing = (1 << 16),
54 kfMakePlink2SetMixedMtMissingKeepDosage = (1 << 17),
55 kfMakePgenFormatBase = (1 << 18), // two bits
56 kfMakePgenErasePhase = (1 << 20),
57 kfMakePgenEraseDosage = (1 << 21),
58 kfMakePgenFillMissingFromDosage = (1 << 22),
59 kfMakePgenWriterVer = (1 << 23)
60 FLAGSET_DEF_END(MakePlink2Flags);
61
62 FLAGSET_DEF_START()
63 kfImport0,
64 kfImportKeepAutoconv = (1 << 0),
65 kfImportKeepAutoconvVzs = (1 << 1),
66 kfImportDoubleId = (1 << 2),
67 kfImportVcfRequireGt = (1 << 3),
68 kfImportVcfRefNMissing = (1 << 4),
69 kfImportLaxChrX = (1 << 5),
70 kfImportPolyploidMissing = (1 << 6),
71 kfImportPolyploidExplicitError = (1 << 7),
72 kfImportVcfAllowNoNonvar = (1 << 8),
73 kfImportLaxBgen = (1 << 9)
74 FLAGSET_DEF_END(ImportFlags);
75
76 CONSTI32(kMaxInfoKeySlen, kMaxIdSlen);
77 #define MAX_INFO_KEY_SLEN_STR MAX_ID_SLEN_STR
78
79 // We only need to distinguish between the following INFO-value-type cases:
80 // Number=0 (flag), Number=<positive integer>, Number=., Number=A, and
81 // Number=R. (Number=G is being treated as Number=., since otherwise chrX is a
82 // nightmare; see https://github.com/samtools/hts-specs/issues/272 for some
83 // discussion.) We use negative numbers to represent the last 3 cases in
84 // InfoVtype.
85 CONSTI32(kInfoVtypeUnknown, -1);
86 CONSTI32(kInfoVtypeA, -2);
87 CONSTI32(kInfoVtypeR, -3);
88 // Used in plink2_data (though not plink2_merge) for INFO/PR flag.
89 CONSTI32(kInfoVtypeSkip, -4);
90
91 HEADER_INLINE uint32_t IsInfoVtypeARSkip(int32_t info_vtype) {
92 return (info_vtype <= kInfoVtypeA);
93 }
94
95 // Main fixed data structure when splitting/joining INFO is a hashmap of keys.
96 // Behavior when splitting:
97 // - Field order in the original variant is retained.
98 // - Number >= 0 and Number=. don't require any special handling, just copy the
99 // entire key=value pair (or lone key, in the Flag case).
100 // - Number=A and Number=R require splitting the value on ',' and verifying the
101 // comma count is correct, but is otherwise straightforward since alleles
102 // can't be permuted.
103 // - Number=G is being treated as Number=.
104 // When joining:
105 // - Field order is determined by header line order.
106 // - Number=./G and Number>0 just require a buffer of size ~info_reload_slen,
107 // and a boolean indicating whether no mismatch has been found.
108 // - Number=0 (Flag) requires a single boolean, we perform an or operation.
109 // - Number=A/R are the messy ones: we need to have enough space for
110 // max_write_allele_ct (or that minus 1) comma-separated values in the =A and
111 // =R cases.
112 typedef struct InfoVtypeStruct {
113 NONCOPYABLE(InfoVtypeStruct);
114 int32_t num;
115 char key[];
116 } InfoVtype;
117
118 PglErr WriteMapOrBim(const char* outname, 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* allele_presents, const AlleleCode* allele_permute, const double* variant_cms, uint32_t variant_ct, uint32_t max_allele_slen, char delim, char output_missing_geno_char, uint32_t output_zst, uint32_t thread_ct);
119
120 PglErr WritePsam(const char* outname, const uintptr_t* sample_include, const SampleIdInfo* siip, const ParentalIdInfo* parental_id_infop, const uintptr_t* sex_nm, const uintptr_t* sex_male, const PhenoCol* pheno_cols, const char* pheno_names, const uint32_t* new_sample_idx_to_old, const char* output_missing_pheno, uint32_t sample_ct, uint32_t pheno_ct, uintptr_t max_pheno_name_blen, PvarPsamFlags pvar_psam_flags, uint32_t psam_01);
121
122 PglErr PvarInfoOpenAndReloadHeader(const char* pvar_info_reload, uint32_t max_thread_ct, TextStream* pvar_reload_txsp, char** line_iterp, uint32_t* info_col_idx_ptr);
123
124 PglErr PvarInfoReload(uint32_t info_col_idx, uint32_t variant_uidx, TextStream* pvar_reload_txsp, char** line_iterp, uint32_t* trs_variant_uidx_ptr);
125
126 PglErr PvarInfoReloadAndWrite(uint32_t info_pr_flag_present, uint32_t info_col_idx, uint32_t variant_uidx, uint32_t is_pr, TextStream* pvar_reload_txsp, char** line_iterp, char** write_iter_ptr, uint32_t* rls_variant_uidx_ptr);
127
128 void AppendChrsetLine(const ChrInfo* cip, char** write_iter_ptr);
129
130 // uint32_t ChrLenLbound(const ChrInfo* cip, const uint32_t* variant_bps, const uintptr_t* allele_idx_offsets, const char* const* allele_storage, const uint32_t* new_variant_idx_to_old, uint32_t chr_fo_idx, uint32_t max_allele_slen, UnsortedVar vpos_sortstatus);
131
132 uint64_t FindContigHeaderLineLengthStr(const char* contig_name_end);
133
134 char* WriteRestOfHeaderLine(const char* hkvline_iter, const char* idval, const char* closing_gt, uint32_t id_slen, char* write_iter);
135
136 BoolErr FixAndWriteContigHeaderLine(const char* header_line_iter, const char* id_ptr, const char* closing_gt, uint32_t id_slen, uint32_t contig_len, char** writep_ptr);
137
138 // fileformat, fileDate, source
139 // assumes adequate buffer space
140 void AppendVcfHeaderStart(uint32_t v43, char** cswritepp);
141
142 PglErr WriteFam(const char* outname, const uintptr_t* sample_include, const PedigreeIdInfo* piip, const uintptr_t* sex_nm, const uintptr_t* sex_male, const PhenoCol* pheno_cols, const uint32_t* new_sample_idx_to_old, const char* legacy_output_missing_pheno, uint32_t sample_ct, uint32_t pheno_ct, char delim);
143
144 uint32_t DataFidColIsRequired(const uintptr_t* sample_include, const SampleIdInfo* siip, uint32_t sample_ct, uint32_t maybe_modifier);
145
146 uint32_t DataSidColIsRequired(const uintptr_t* sample_include, const char* sids, uint32_t sample_ct, uint32_t max_sid_blen, uint32_t maybe_modifier);
147
148 uint32_t DataParentalColsAreRequired(const uintptr_t* sample_include, const SampleIdInfo* siip, const ParentalIdInfo* parental_id_infop, uint32_t sample_ct, uint32_t maybe_modifier);
149
150 char* AppendPhenoStr(const PhenoCol* pheno_col, const char* output_missing_pheno, uint32_t omp_slen, uint32_t sample_uidx, char* write_iter);
151
152 char* AppendPhenoStrEx(const PhenoCol* pheno_col, const char* output_missing_pheno, uint32_t omp_slen, uint32_t sample_uidx, char ctrl_char, char* write_iter);
153
154 uint32_t CopyAndPermute8bit(const uintptr_t* __restrict sample_include, const uintptr_t* __restrict src_subset, const void* __restrict src_vals, const uint32_t* __restrict old_sample_idx_to_new, uint32_t sample_ct, uint32_t val_ct, uintptr_t* __restrict dst_subset, void* __restrict dst_vals);
155
156 uint32_t CopyAndPermute16bit(const uintptr_t* __restrict sample_include, const uintptr_t* __restrict src_subset, const void* __restrict src_vals, const uint32_t* __restrict old_sample_idx_to_new, uint32_t sample_ct, uint32_t val_ct, uintptr_t* __restrict dst_subset, void* __restrict dst_vals);
157
158 uint32_t Dense8bitToSparse(const uintptr_t* __restrict set, uint32_t sample_ctl, void* __restrict vals);
159
160 uint32_t Dense16bitToSparse(const uintptr_t* __restrict set, uint32_t sample_ctl, void* __restrict vals);
161
162 PglErr LoadAlleleAndGenoCounts(const uintptr_t* sample_include, const uintptr_t* founder_info, const uintptr_t* sex_nm, const uintptr_t* sex_male, const uintptr_t* variant_include, const ChrInfo* cip, const uintptr_t* allele_idx_offsets, uint32_t raw_sample_ct, uint32_t sample_ct, uint32_t founder_ct, uint32_t male_ct, uint32_t nosex_ct, uint32_t raw_variant_ct, uint32_t variant_ct, uint32_t first_hap_uidx, uint32_t is_minimac3_r2, uint32_t y_nosex_missing_stats, uint32_t max_thread_ct, uintptr_t pgr_alloc_cacheline_ct, PgenFileInfo* pgfip, uintptr_t* allele_presents, uint64_t* allele_ddosages, uint64_t* founder_allele_ddosages, uint32_t* variant_missing_hc_cts, uint32_t* variant_missing_dosage_cts, uint32_t* variant_hethap_cts, STD_ARRAY_PTR_DECL(uint32_t, 3, raw_geno_cts), STD_ARRAY_PTR_DECL(uint32_t, 3, founder_raw_geno_cts), STD_ARRAY_PTR_DECL(uint32_t, 3, x_male_geno_cts), STD_ARRAY_PTR_DECL(uint32_t, 3, founder_x_male_geno_cts), STD_ARRAY_PTR_DECL(uint32_t, 3, x_nosex_geno_cts), STD_ARRAY_PTR_DECL(uint32_t, 3, founder_x_nosex_geno_cts), double* imp_r2_vals);
163
164 void ApplyHardCallThresh(const uintptr_t* dosage_present, const Dosage* dosage_main, uint32_t dosage_ct, uint32_t hard_call_halfdist, uintptr_t* genovec);
165
166 uint32_t ApplyHardCallThreshPhased(const uintptr_t* dosage_present, const Dosage* dosage_main, uint32_t dosage_ct, uint32_t hard_call_halfdist, uintptr_t* genovec, uintptr_t* phasepresent, uintptr_t* phaseinfo, uintptr_t* dphase_present, SDosage* dphase_delta, SDosage* tmp_dphase_delta);
167
168 BoolErr FillInfoVtypeNum(const char* numstr, uint32_t num_slen, uint32_t* info_has_g_keyp, int32_t* info_vtype_num_ptr);
169
170 PglErr MakePlink2NoVsort(const uintptr_t* sample_include, const PedigreeIdInfo* piip, const uintptr_t* sex_nm, const uintptr_t* sex_male, const PhenoCol* pheno_cols, const char* pheno_names, const uint32_t* new_sample_idx_to_old, 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* allele_presents, const AlleleCode* allele_permute, const uintptr_t* pvar_qual_present, const float* pvar_quals, const uintptr_t* pvar_filter_present, const uintptr_t* pvar_filter_npass, const char* const* pvar_filter_storage, const char* pvar_info_reload, const double* variant_cms, const char* varid_template_str, __maybe_unused const char* varid_multi_template_str, __maybe_unused const char* varid_multi_nonsnp_template_str, const char* missing_varid_match, const char* output_missing_pheno, const char* legacy_output_missing_pheno, const uint32_t* contig_lens, const char* writer_ver, uintptr_t xheader_blen, InfoFlags info_flags, uint32_t raw_sample_ct, uint32_t sample_ct, uint32_t pheno_ct, uintptr_t max_pheno_name_blen, uint32_t raw_variant_ct, uint32_t variant_ct, uint32_t max_allele_ct, uint32_t max_allele_slen, uint32_t max_filter_slen, uint32_t info_reload_slen, char output_missing_geno_char, uint32_t max_thread_ct, uint32_t hard_call_thresh, uint32_t dosage_erase_thresh, uint32_t new_variant_id_max_allele_slen, MiscFlags misc_flags, MakePlink2Flags make_plink2_flags, PvarPsamFlags pvar_psam_flags, uintptr_t pgr_alloc_cacheline_ct, char* xheader, PgenFileInfo* pgfip, PgenReader* simple_pgrp, char* outname, char* outname_end);
171
172 PglErr MakePlink2Vsort(const uintptr_t* sample_include, const PedigreeIdInfo* piip, const uintptr_t* sex_nm, const uintptr_t* sex_male, const PhenoCol* pheno_cols, const char* pheno_names, const uint32_t* new_sample_idx_to_old, const uintptr_t* variant_include, const uint32_t* variant_bps, const char* const* variant_ids, const uintptr_t* allele_idx_offsets, const char* const* allele_storage, const uintptr_t* allele_presents, const AlleleCode* allele_permute, const uintptr_t* pvar_qual_present, const float* pvar_quals, const uintptr_t* pvar_filter_present, const uintptr_t* pvar_filter_npass, const char* const* pvar_filter_storage, const char* pvar_info_reload, const double* variant_cms, const char* output_missing_pheno, const char* legacy_output_missing_pheno, const uint32_t* contig_lens, const TwoColParams* update_chr_flag, const char* writer_ver, uintptr_t xheader_blen, InfoFlags info_flags, uint32_t raw_sample_ct, uint32_t sample_ct, uint32_t pheno_ct, uintptr_t max_pheno_name_blen, uint32_t raw_variant_ct, uint32_t variant_ct, uint32_t max_allele_ct, uint32_t max_variant_id_slen, uint32_t max_allele_slen, uint32_t max_filter_slen, uint32_t info_reload_slen, char output_missing_geno_char, uint32_t max_thread_ct, uint32_t hard_call_thresh, uint32_t dosage_erase_thresh, MiscFlags misc_flags, MakePlink2Flags make_plink2_flags, uint32_t use_nsort, PvarPsamFlags pvar_psam_flags, ChrInfo* cip, char* xheader, ChrIdx* chr_idxs, PgenReader* simple_pgrp, char* outname, char* outname_end);
173
174 PglErr SampleSortFileMap(const uintptr_t* sample_include, const SampleIdInfo* siip, const char* sample_sort_fname, uint32_t raw_sample_ct, uint32_t sample_ct, uint32_t** new_sample_idx_to_old_ptr);
175
176 PglErr LoadSampleMissingCts(const uintptr_t* sex_nm, const uintptr_t* sex_male, const uintptr_t* variant_include, const ChrInfo* cip, const char* calc_descrip_str, uint32_t raw_variant_ct, uint32_t variant_ct, uint32_t y_nosex_missing_stats, uint32_t raw_sample_ct, uint32_t max_thread_ct, uintptr_t pgr_alloc_cacheline_ct, PgenFileInfo* pgfip, uint32_t* sample_missing_hc_cts, uint32_t* sample_missing_dosage_cts, uint32_t* sample_hethap_cts);
177
178 #ifdef __cplusplus
179 } // namespace plink2
180 #endif
181
182 #endif // __PLINK2_DATA_H__