| 1 |
#ifndef __PLINK2_PVAR_H__ |
| 2 |
#define __PLINK2_PVAR_H__ |
| 3 |
|
| 4 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 5 |
// Christopher Chang. |
| 6 |
// |
| 7 |
// This library is free software: you can redistribute it and/or modify it |
| 8 |
// under the terms of the GNU Lesser General Public License as published by the |
| 9 |
// Free Software Foundation; either version 3 of the License, or (at your |
| 10 |
// option) any later version. |
| 11 |
// |
| 12 |
// This library 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 Lesser General Public License |
| 15 |
// for more details. |
| 16 |
// |
| 17 |
// You should have received a copy of the GNU Lesser General Public License |
| 18 |
// along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
#include "plink2_common.h" |
| 21 |
|
| 22 |
#ifdef __cplusplus |
| 23 |
namespace plink2 { |
| 24 |
#endif |
| 25 |
|
| 26 |
// Key .pvar properties: (i) .bim files are valid .pvar files; (ii) .vcf files, |
| 27 |
// with or without post-INFO columns removed, are valid .pvar files; (iii) |
| 28 |
// these files remain valid when {b}gzipped. |
| 29 |
// |
| 30 |
// File starts with an arbitrary (possibly zero) number of header lines marked |
| 31 |
// by a leading '#'. All lines which don't start with '#CHROM' are currently |
| 32 |
// ignored. The #CHROM line specifies the columns in the .pvar file; the |
| 33 |
// following column headers are recognized: |
| 34 |
// POS (bp coordinate) |
| 35 |
// ID (variant ID) |
| 36 |
// REF (reference allele) |
| 37 |
// ALT (alternate alleles, comma-separated) |
| 38 |
// QUAL (phred-scaled quality score for whether the locus is variable at all) |
| 39 |
// FILTER (PASS, ., or semicolon-separated list of failing filter codes) |
| 40 |
// INFO (semicolon-separated list of flags and key-value pairs, with types |
| 41 |
// declared in header) |
| 42 |
// FORMAT (terminates header line parsing, so any VCF sample data is ignored) |
| 43 |
// CM (centimorgan position) |
| 44 |
// The loader will error out if multiple #CHROM lines are in the header for |
| 45 |
// some bizarre reason. If no #CHROM line is present, it is assumed to be |
| 46 |
// "#CHROM ID CM POS ALT REF" for .bim compatibility, or |
| 47 |
// "#CHROM ID POS ALT REF" in the case where the first nonheader line has |
| 48 |
// exactly 5 columns. Multiple ALT alleles are prohibited in this .bim |
| 49 |
// compatibility mode. |
| 50 |
|
| 51 |
|
| 52 |
// allele_idx_offsets[] is a length-(variant_ct + 1) array of reference allele |
| 53 |
// indices in allele_storage[]. The alt1 allele index is the reference allele |
| 54 |
// index plus 1, etc. The number of alt alleles for (0-based) variant n is |
| 55 |
// (allele_idx_offsets[n+1] - allele_idx_offsets[n] - 1). |
| 56 |
|
| 57 |
// Memory for all the return arrays is allocated off the bottom of g_bigstack. |
| 58 |
// (I.e. it's assumed that you'll want to keep these arrays until your program |
| 59 |
// is about to terminate.) |
| 60 |
|
| 61 |
|
| 62 |
PglErr ReadChrsetHeaderLine(const char* chrset_iter, const char* file_descrip, MiscFlags misc_flags, uintptr_t line_idx, ChrInfo* cip); |
| 63 |
|
| 64 |
FLAGSET_DEF_START() |
| 65 |
kfVaridTemplateAllele0, |
| 66 |
kfVaridTemplateAlleleRefOr1 = (1 << 0), |
| 67 |
kfVaridTemplateAlleleAltOr2 = (1 << 1), |
| 68 |
kfVaridTemplateAlleleAsciiOrder = (1 << 2) |
| 69 |
FLAGSET_DEF_END(VaridTemplateAlleleFlags); |
| 70 |
|
| 71 |
typedef struct VaridTemplateStruct { |
| 72 |
NONCOPYABLE(VaridTemplateStruct); |
| 73 |
const char* missing_id_match; |
| 74 |
char* chr_output_name_buf; |
| 75 |
STD_ARRAY_DECL(const char*, 5, segs); |
| 76 |
STD_ARRAY_DECL(uint32_t, 5, seg_lens); |
| 77 |
STD_ARRAY_DECL(uint32_t, 4, insert_types); |
| 78 |
uint32_t insert_ct; |
| 79 |
uint32_t base_len; |
| 80 |
uint32_t chr_slen; |
| 81 |
VaridTemplateAlleleFlags allele_flags; |
| 82 |
|
| 83 |
// next two values (and the first two) are common between templates |
| 84 |
uint32_t new_id_max_allele_slen; |
| 85 |
uint32_t overflow_substitute_blen; // only nonzero if overflow -> missing |
| 86 |
} VaridTemplate; |
| 87 |
|
| 88 |
void VaridTemplateInit(const char* varid_template_str, const char* missing_id_match, char* chr_output_name_buf, uint32_t new_id_max_allele_slen, uint32_t overflow_substitute_blen, VaridTemplate* vtp); |
| 89 |
|
| 90 |
BoolErr VaridInitAll(unsigned char* arena_end, const char* varid_template_str, const char* varid_multi_template_str, const char* varid_multi_nonsnp_template_str, MiscFlags misc_flags, uint32_t new_variant_id_max_allele_slen, unsigned char** arena_basep, const char** missing_varid_matchp, char** chr_output_name_bufp, VaridTemplate** varid_templatepp, VaridTemplate** varid_multi_templatepp, VaridTemplate** varid_multi_nonsnp_templatepp, uint32_t* missing_varid_blenp, uint32_t* missing_varid_match_slenp); |
| 91 |
|
| 92 |
char* VaridTemplateWrite(const VaridTemplate* vtp, const char* ref_start, const char* alt1_start, uint32_t cur_bp, uint32_t ref_token_slen, uint32_t extra_alt_ct, uint32_t alt_token_slen, uint32_t* max_overflow_slenp, char* dst); |
| 93 |
|
| 94 |
// These functions assume info_token[-1] is safe to read |
| 95 |
// They may set info_token[info_slen] to \0, since they need to use strstr() |
| 96 |
// (todo: try memmem()... except it isn't available on 32-bit mingw?) |
| 97 |
uint32_t PrInInfo(uint32_t info_slen, char* info_token); |
| 98 |
|
| 99 |
char* InfoPrStart(uint32_t info_slen, char* info_token); |
| 100 |
|
| 101 |
// cip, max_variant_id_slen, and info_reload are in/out parameters. |
| 102 |
// Chromosome filtering is performed if cip requests it. |
| 103 |
PglErr LoadPvar(const char* pvarname, const char* var_filter_exceptions_flattened, const char* varid_template_str, const char* varid_multi_template_str, const char* varid_multi_nonsnp_template_str, const char* missing_varid_match, const char* require_info_flattened, const char* require_no_info_flattened, const CmpExpr* extract_if_info_exprp, const CmpExpr* exclude_if_info_exprp, MiscFlags misc_flags, PvarPsamFlags pvar_psam_flags, uint32_t xheader_needed, uint32_t qualfilter_needed, float var_min_qual, uint32_t splitpar_bound1, uint32_t splitpar_bound2, uint32_t new_variant_id_max_allele_slen, uint32_t snps_only, uint32_t split_chr_ok, uint32_t filter_min_allele_ct, uint32_t filter_max_allele_ct, char input_missing_geno_char, uint32_t max_thread_ct, ChrInfo* cip, uint32_t* max_variant_id_slen_ptr, uint32_t* info_reload_slen_ptr, UnsortedVar* vpos_sortstatus_ptr, char** xheader_ptr, uintptr_t** variant_include_ptr, uint32_t** variant_bps_ptr, char*** variant_ids_ptr, uintptr_t** allele_idx_offsets_ptr, const char*** allele_storage_ptr, uintptr_t** qual_present_ptr, float** quals_ptr, uintptr_t** filter_present_ptr, uintptr_t** filter_npass_ptr, char*** filter_storage_ptr, uintptr_t** nonref_flags_ptr, double** variant_cms_ptr, ChrIdx** chr_idxs_ptr, uint32_t* raw_variant_ct_ptr, uint32_t* variant_ct_ptr, uint32_t* max_allele_ct_ptr, uint32_t* max_allele_slen_ptr, uintptr_t* xheader_blen_ptr, InfoFlags* info_flags_ptr, uint32_t* max_filter_slen_ptr); |
| 104 |
|
| 105 |
PglErr LoadAlleleIdxOffsetsFromPvar(const char* pvarname, const char* file_descrip, uint32_t max_thread_ct, uint32_t* raw_variant_ctp, uint32_t* max_allele_slenp, uint32_t* max_observed_line_blenp, uintptr_t** allele_idx_offsets_ptr, uint32_t* max_allele_ctp); |
| 106 |
|
| 107 |
#ifdef __cplusplus |
| 108 |
} // namespace plink2 |
| 109 |
#endif |
| 110 |
|
| 111 |
#endif // __PLINK2_PVAR_H__ |