forked from samtools/htslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcf_sweep.c
183 lines (151 loc) · 5.22 KB
/
vcf_sweep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* vcf_sweep.c -- forward/reverse sweep API.
Copyright (C) 2013 Genome Research Ltd.
Author: Petr Danecek <pd3@sanger.ac.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. */
#include <config.h>
#include "htslib/vcf_sweep.h"
#include "htslib/bgzf.h"
#define SW_FWD 0
#define SW_BWD 1
struct _bcf_sweep_t
{
htsFile *file;
bcf_hdr_t *hdr;
BGZF *fp;
int direction; // to tell if the direction has changed
int block_size; // the size of uncompressed data to hold in memory
bcf1_t *rec; // bcf buffer
int nrec, mrec; // number of used records; total size of the buffer
int lrid, lpos, lnals, lals_len, mlals; // to check uniqueness of a record
char *lals;
uint64_t *idx; // uncompressed offsets of VCF/BCF records
int iidx, nidx, midx; // i: current offset; n: used; m: allocated
int idx_done; // the index is built during the first pass
};
BGZF *hts_get_bgzfp(htsFile *fp);
int hts_useek(htsFile *file, long uoffset, int where);
long hts_utell(htsFile *file);
static inline int sw_rec_equal(bcf_sweep_t *sw, bcf1_t *rec)
{
if ( sw->lrid!=rec->rid ) return 0;
if ( sw->lpos!=rec->pos ) return 0;
if ( sw->lnals!=rec->n_allele ) return 0;
char *t = rec->d.allele[sw->lnals-1];
int len = t - rec->d.allele[0] + 1;
while ( *t ) { t++; len++; }
if ( sw->lals_len!=len ) return 0;
if ( memcmp(sw->lals,rec->d.allele[0],len) ) return 0;
return 1;
}
static void sw_rec_save(bcf_sweep_t *sw, bcf1_t *rec)
{
sw->lrid = rec->rid;
sw->lpos = rec->pos;
sw->lnals = rec->n_allele;
char *t = rec->d.allele[sw->lnals-1];
int len = t - rec->d.allele[0] + 1;
while ( *t ) { t++; len++; }
sw->lals_len = len;
hts_expand(char, len, sw->mlals, sw->lals);
memcpy(sw->lals, rec->d.allele[0], len);
}
static void sw_fill_buffer(bcf_sweep_t *sw)
{
if ( !sw->iidx ) return;
sw->iidx--;
int ret = hts_useek(sw->file, sw->idx[sw->iidx], 0);
assert( ret==0 );
sw->nrec = 0;
bcf1_t *rec = &sw->rec[sw->nrec];
while ( (ret=bcf_read1(sw->file, sw->hdr, rec))==0 )
{
bcf_unpack(rec, BCF_UN_STR);
// if not in the last block, stop at the saved record
if ( sw->iidx+1 < sw->nidx && sw_rec_equal(sw,rec) ) break;
sw->nrec++;
hts_expand0(bcf1_t, sw->nrec+1, sw->mrec, sw->rec);
rec = &sw->rec[sw->nrec];
}
sw_rec_save(sw, &sw->rec[0]);
}
bcf_sweep_t *bcf_sweep_init(const char *fname)
{
bcf_sweep_t *sw = (bcf_sweep_t*) calloc(1,sizeof(bcf_sweep_t));
sw->file = hts_open(fname, "r");
sw->fp = hts_get_bgzfp(sw->file);
if (sw->fp) bgzf_index_build_init(sw->fp);
sw->hdr = bcf_hdr_read(sw->file);
sw->mrec = 1;
sw->rec = (bcf1_t*) calloc(sw->mrec,(sizeof(bcf1_t)));
sw->block_size = 1024*1024*3;
sw->direction = SW_FWD;
return sw;
}
void bcf_sweep_destroy(bcf_sweep_t *sw)
{
int i;
for (i=0; i<sw->mrec; i++) bcf_empty1(&sw->rec[i]);
free(sw->idx);
free(sw->rec);
free(sw->lals);
bcf_hdr_destroy(sw->hdr);
hts_close(sw->file);
free(sw);
}
static void sw_seek(bcf_sweep_t *sw, int direction)
{
sw->direction = direction;
if ( direction==SW_FWD )
hts_useek(sw->file, sw->idx[0], 0);
else
{
sw->iidx = sw->nidx;
sw->nrec = 0;
}
}
bcf1_t *bcf_sweep_fwd(bcf_sweep_t *sw)
{
if ( sw->direction==SW_BWD ) sw_seek(sw, SW_FWD);
long pos = hts_utell(sw->file);
bcf1_t *rec = &sw->rec[0];
int ret = bcf_read1(sw->file, sw->hdr, rec);
if ( ret!=0 ) // last record, get ready for sweeping backwards
{
sw->idx_done = 1;
if (sw->fp) sw->fp->idx_build_otf = 0;
sw_seek(sw, SW_BWD);
return NULL;
}
if ( !sw->idx_done )
{
if ( !sw->nidx || pos - sw->idx[sw->nidx-1] > sw->block_size )
{
sw->nidx++;
hts_expand(uint64_t, sw->nidx, sw->midx, sw->idx);
sw->idx[sw->nidx-1] = pos;
}
}
return rec;
}
bcf1_t *bcf_sweep_bwd(bcf_sweep_t *sw)
{
if ( sw->direction==SW_FWD ) sw_seek(sw, SW_BWD);
if ( !sw->nrec ) sw_fill_buffer(sw);
if ( !sw->nrec ) return NULL;
return &sw->rec[ --sw->nrec ];
}
bcf_hdr_t *bcf_sweep_hdr(bcf_sweep_t *sw) { return sw->hdr; }