1 /*
2     Copyright (C) 2017 Genome Research Ltd.
3 
4     Author: Petr Danecek <pd3@sanger.ac.uk>
5 
6     Permission is hereby granted, free of charge, to any person obtaining a copy
7     of this software and associated documentation files (the "Software"), to deal
8     in the Software without restriction, including without limitation the rights
9     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10     copies of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22     THE SOFTWARE.
23 */
24 
25 /*
26     Reorder duplicate lines so that compatible variant types are
27     returned together by bcf_sr_next_line()
28 
29     - readers grouped by variants. Even with many readers there will be
30       typically only several groups
31 
32 */
33 
34 #ifndef BCF_SR_SORT_H
35 #define BCF_SR_SORT_H
36 
37 #include "htslib/synced_bcf_reader.h"
38 #include "htslib/kbitset.h"
39 
40 typedef struct
41 {
42     int nrec, mrec;
43     bcf1_t **rec;
44 }
45 vcf_buf_t;
46 
47 typedef struct
48 {
49     char *str;      // "A>C" for biallelic records or "A>C,A>CC" for multiallelic records
50     int type;       // VCF_SNP, VCF_REF, etc.
51     int nalt;       // number of alternate alleles in this record
52     int nvcf, mvcf, *vcf;   // the list of readers with the same variants
53     bcf1_t **rec;           // list of VCF records in the readers
54     kbitset_t *mask;        // which groups contain the variant
55 }
56 var_t;
57 
58 typedef struct
59 {
60     char *key;              // only for debugging
61     int nvar, mvar, *var;   // the variants and their type
62     int nvcf;               // number of readers with the same variants
63 }
64 grp_t;
65 
66 typedef struct
67 {
68     int nvar, mvar, *var;   // list of compatible variants that can be output together
69     int cnt;                // number of readers in this group
70     kbitset_t *mask;        // which groups are populated in this set (replace with expandable bitmask)
71 }
72 varset_t;
73 
74 typedef struct
75 {
76     uint8_t score[256];
77     int nvar, mvar;
78     var_t *var;             // list of all variants from all readers
79     int nvset, mvset;
80     int mpmat, *pmat;       // pairing matrix, i-th vset and j-th group accessible as i*ngrp+j
81     int ngrp, mgrp;
82     int mcnt, *cnt;         // number of VCF covered by a varset
83     grp_t *grp;             // list of VCF representatives, each with a unique combination of duplicate lines
84     varset_t *vset;         // list of variant sets - combinations of compatible variants across multiple groups ready for output
85     vcf_buf_t *vcf_buf;     // records sorted in output order, for each VCF
86     bcf_srs_t *sr;
87     void *grp_str2int;
88     void *var_str2int;
89     kstring_t str;
90     int moff, noff, *off, mcharp;
91     char **charp;
92     const char *chr;
93     hts_pos_t pos;
94     int nsr, msr;
95     int pair;
96     int nactive, mactive, *active;  // list of readers with lines at the current pos
97 }
98 sr_sort_t;
99 
100 sr_sort_t *bcf_sr_sort_init(sr_sort_t *srt);
101 void bcf_sr_sort_reset(sr_sort_t *srt);
102 int bcf_sr_sort_next(bcf_srs_t *readers, sr_sort_t *srt, const char *chr, hts_pos_t pos);
103 int bcf_sr_sort_set_active(sr_sort_t *srt, int i);
104 int bcf_sr_sort_add_active(sr_sort_t *srt, int i);
105 void bcf_sr_sort_destroy(sr_sort_t *srt);
106 void bcf_sr_sort_remove_reader(bcf_srs_t *readers, sr_sort_t *srt, int i);
107 
108 #endif
109