You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

117 line
4.0 KiB

  1. /* The MIT License
  2. Copyright (c) 2017-2021 Genome Research Ltd.
  3. Author: Petr Danecek <pd3@sanger.ac.uk>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /*
  21. Buffer VCF records and perform operations on the buffer
  22. */
  23. #ifndef __VCFBUF_H__
  24. #define __VCFBUF_H__
  25. #include <htslib/vcf.h>
  26. typedef struct _vcfbuf_t vcfbuf_t;
  27. // Modes of operation
  28. typedef enum
  29. {
  30. VCFBUF_OVERLAP_WIN, // keep only overlapping variants in the window
  31. VCFBUF_RMDUP, // remove duplicate sites (completely)
  32. VCFBUF_NSITES, // leave at max this many sites in the window
  33. VCFBUF_NSITES_MODE, // one of: maxAF (keep sites with max AF), 1st (sites that come first), rand (pick randomly)
  34. VCFBUF_AF_TAG, // use this INFO tag with VCFBUF_NSITES
  35. // LD related options
  36. LD_RAND_MISSING, // randomize rather than ignore missing genotypes
  37. LD_FILTER1, // exclude the next record inserted by vcfbuf_push() from LD analysis
  38. LD_MAX_R2, // If set, vcfbuf_ld() will stop at the first record that exceeds the R2,
  39. LD_MAX_LD, // LD, or HD threshold. When multiple are set, the OR logic is applied
  40. LD_MAX_HD, //
  41. }
  42. vcfbuf_opt_t;
  43. #define vcfbuf_set_opt(buf,type,key,value) { type tmp = value; vcfbuf_set(buf, key, (void*)&tmp); }
  44. void vcfbuf_set(vcfbuf_t *buf, vcfbuf_opt_t key, void *value);
  45. /*
  46. * vcfbuf_init() - init buffer
  47. * @win: number of sites (>0) or bp (<0)
  48. */
  49. vcfbuf_t *vcfbuf_init(bcf_hdr_t *hdr, int win);
  50. void vcfbuf_destroy(vcfbuf_t *buf);
  51. /*
  52. * vcfbuf_push() - push a new site for analysis
  53. */
  54. bcf1_t *vcfbuf_push(vcfbuf_t *buf, bcf1_t *rec);
  55. /*
  56. * vcfbuf_peek() - return pointer to i-th record in the buffer but do not remove it from the buffer
  57. * @idx: 0-based index to buffered lines
  58. */
  59. bcf1_t *vcfbuf_peek(vcfbuf_t *buf, int idx);
  60. /*
  61. * vcfbuf_remove() - return pointer to i-th record in the buffer and remove it from the buffer
  62. * @idx: 0-based index to buffered lines
  63. */
  64. bcf1_t *vcfbuf_remove(vcfbuf_t *buf, int idx);
  65. bcf1_t *vcfbuf_flush(vcfbuf_t *buf, int flush_all);
  66. /*
  67. * vcfbuf_nsites() - return the number of sites in the buffer
  68. */
  69. int vcfbuf_nsites(vcfbuf_t *buf);
  70. /*
  71. * vcfbuf_ld() - find records with maximum LD values or the values in first record that exceeds thresholds
  72. * set by vcfbuf_set_opt(..,LD_MAX*,..)
  73. *
  74. * Returns 0 on success or -1 if no values were filled.
  75. *
  76. * @val: will be filled with the values
  77. * .. correlation coefficient r-squared
  78. * .. Lewontin's D' (PMID: 19433632)
  79. * .. Ragsdale's \hat{D} (doi:10.1093/molbev/msz265)
  80. * @rec: corresponding positions or NULL if the value(s) has not been set
  81. */
  82. #define VCFBUF_LD_N 3
  83. #define VCFBUF_LD_IDX_R2 0
  84. #define VCFBUF_LD_IDX_LD 1
  85. #define VCFBUF_LD_IDX_HD 2
  86. typedef struct
  87. {
  88. double val[VCFBUF_LD_N]; // r2, ld, hd
  89. bcf1_t *rec[VCFBUF_LD_N]; // record with max r2, ld, hd
  90. }
  91. vcfbuf_ld_t;
  92. int vcfbuf_ld(vcfbuf_t *buf, bcf1_t *rec, vcfbuf_ld_t *ld);
  93. #endif