| 1 | /* The MIT License |
| 2 | |
| 3 | Copyright (c) 2017-2024 Genome Research Ltd. |
| 4 | |
| 5 | Author: Petr Danecek <[email protected]> |
| 6 | |
| 7 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | of this software and associated documentation files (the "Software"), to deal |
| 9 | in the Software without restriction, including without limitation the rights |
| 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | copies of the Software, and to permit persons to whom the Software is |
| 12 | furnished to do so, subject to the following conditions: |
| 13 | |
| 14 | The above copyright notice and this permission notice shall be included in |
| 15 | all copies or substantial portions of the Software. |
| 16 | |
| 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | THE SOFTWARE. |
| 24 | |
| 25 | */ |
| 26 | |
| 27 | /* |
| 28 | Buffer VCF records and perform operations on the buffer |
| 29 | */ |
| 30 | |
| 31 | #ifndef __VCFBUF_H__ |
| 32 | #define __VCFBUF_H__ |
| 33 | |
| 34 | #include <htslib/vcf.h> |
| 35 | |
| 36 | typedef struct _vcfbuf_t vcfbuf_t; |
| 37 | |
| 38 | // Modes of operation |
| 39 | typedef enum |
| 40 | { |
| 41 | VCFBUF_DUMMY, // int {0,1}, the caller maintains the buffer via push/peek/flush, nothing is removed by vcfbuf |
| 42 | |
| 43 | // pruning |
| 44 | PRUNE_NSITES, // int, leave max this many sites in the window |
| 45 | PRUNE_NSITES_MODE, // char *, maxAF (keep sites with max AF), 1st (sites that come first), rand (pick randomly) |
| 46 | PRUNE_AF_TAG, // char *, use this INFO/AF tag with VCFBUF_NSITES |
| 47 | |
| 48 | // duplicates and overlaps |
| 49 | MARK, // w: char *, resolve overlaps by preferentially removing sites according to EXPR: |
| 50 | // min(QUAL) .. remove sites with lowest QUAL until overlaps are resolved |
| 51 | // overlap .. select all overlapping sites |
| 52 | // dup .. select duplicate sites |
| 53 | // r: use as |
| 54 | // while ( (rec=vcfbuf_flush(buf,flush_all)) ) |
| 55 | // { |
| 56 | // int is_marked = vcfbuf_get_val(buf,int,MARK); |
| 57 | // if ( is_marked ) do_something(rec); |
| 58 | // } |
| 59 | MARK_MISSING_EXPR, // char *, what to do when missing value are encountered with min(QUAL) |
| 60 | // 0 .. set to 0 (the default) |
| 61 | // DP .. scale max quality in the window proportionally to INFO/DP |
| 62 | |
| 63 | // LD related options |
| 64 | LD_RAND_MISSING, // randomize rather than ignore missing genotypes |
| 65 | LD_FILTER1, // exclude the next record inserted by vcfbuf_push() from LD analysis |
| 66 | LD_MAX_R2, // If set, vcfbuf_ld() will stop at the first record that exceeds the R2, |
| 67 | LD_MAX_LD, // LD, or HD threshold. When multiple are set, the OR logic is applied |
| 68 | LD_MAX_HD, // |
| 69 | } |
| 70 | vcfbuf_opt_t; |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * vcfbuf_set() - set various options, see the vcfbuf_opt_t keys for the complete list |
| 75 | * |
| 76 | * Returns 0 if the call succeeded, or negative number on error. |
| 77 | */ |
| 78 | int vcfbuf_set(vcfbuf_t *buf, vcfbuf_opt_t key, ...); // returns 0 on success |
| 79 | |
| 80 | /** |
| 81 | * vcfbuf_get() - get various options, see the vcfbuf_opt_t keys |
| 82 | * vcfbuf_get_val() - wrapper for `vcfbuf_get()` to return typed value |
| 83 | * |
| 84 | * The former returns pointer to the memory area populated by the requested setting, |
| 85 | * its type can be inferred from the vcfbuf_opt_t documentation. |
| 86 | */ |
| 87 | void *vcfbuf_get(vcfbuf_t *buf, vcfbuf_opt_t key, ...); |
| 88 | #define vcfbuf_get_val(buf,type,key) (*(type*)vcfbuf_get(buf, key)) |
| 89 | |
| 90 | |
| 91 | /* |
| 92 | * vcfbuf_init() - init buffer |
| 93 | * @win: number of sites (>0), bp (<0) |
| 94 | */ |
| 95 | vcfbuf_t *vcfbuf_init(bcf_hdr_t *hdr, int win); |
| 96 | void vcfbuf_destroy(vcfbuf_t *buf); |
| 97 | |
| 98 | /* |
| 99 | * vcfbuf_push() - push a new site for analysis. |
| 100 | * |
| 101 | * Note that vcfbuf_flush() or vcfbuf_peek() must be called before next site is pushed. |
| 102 | */ |
| 103 | bcf1_t *vcfbuf_push(vcfbuf_t *buf, bcf1_t *rec); |
| 104 | |
| 105 | /* |
| 106 | * vcfbuf_peek() - return pointer to i-th record in the buffer but do not remove it from the buffer |
| 107 | * @idx: 0-based index to buffered lines |
| 108 | */ |
| 109 | bcf1_t *vcfbuf_peek(vcfbuf_t *buf, int idx); |
| 110 | |
| 111 | /* |
| 112 | * vcfbuf_remove() - return pointer to i-th record in the buffer and remove it from the buffer |
| 113 | * @idx: 0-based index to buffered lines |
| 114 | */ |
| 115 | bcf1_t *vcfbuf_remove(vcfbuf_t *buf, int idx); |
| 116 | |
| 117 | /* |
| 118 | * vcfbuf_flush() - returns the next record or NULL, depending on the mode of operation and |
| 119 | * the content of the buffer |
| 120 | * |
| 121 | * @flush_all: 1 if no more vcfbuf_push() calls will follow, 0 otherwise |
| 122 | */ |
| 123 | bcf1_t *vcfbuf_flush(vcfbuf_t *buf, int flush_all); |
| 124 | |
| 125 | /* |
| 126 | * vcfbuf_nsites() - return the number of sites in the buffer |
| 127 | */ |
| 128 | int vcfbuf_nsites(vcfbuf_t *buf); |
| 129 | |
| 130 | /* |
| 131 | * vcfbuf_ld() - find records with maximum LD values or the values in first record that exceeds thresholds |
| 132 | * set by vcfbuf_set_opt(..,LD_MAX*,..) |
| 133 | * |
| 134 | * Returns 0 on success or -1 if no values were filled. |
| 135 | * |
| 136 | * @val: will be filled with the values |
| 137 | * .. correlation coefficient r-squared |
| 138 | * .. Lewontin's D' (PMID: 19433632) |
| 139 | * .. Ragsdale's \hat{D} (doi:10.1093/molbev/msz265) |
| 140 | * @rec: corresponding positions or NULL if the value(s) has not been set |
| 141 | */ |
| 142 | #define VCFBUF_LD_N 3 |
| 143 | #define VCFBUF_LD_IDX_R2 0 |
| 144 | #define VCFBUF_LD_IDX_LD 1 |
| 145 | #define VCFBUF_LD_IDX_HD 2 |
| 146 | typedef struct |
| 147 | { |
| 148 | double val[VCFBUF_LD_N]; // r2, ld, hd |
| 149 | bcf1_t *rec[VCFBUF_LD_N]; // record with max r2, ld, hd |
| 150 | } |
| 151 | vcfbuf_ld_t; |
| 152 | int vcfbuf_ld(vcfbuf_t *buf, bcf1_t *rec, vcfbuf_ld_t *ld); |
| 153 | |
| 154 | #endif |
| 155 |