forked from samtools/htslib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
synced_bcf_reader.c
1463 lines (1326 loc) · 46.4 KB
/
synced_bcf_reader.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* synced_bcf_reader.c -- stream through multiple VCF files.
Copyright (C) 2012-2021 Genome Research Ltd.
Author: Petr Danecek <[email protected]>
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. */
#define HTS_BUILDING_LIBRARY // Enables HTSLIB_EXPORT, see htslib/hts_defs.h
#include <config.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#include "htslib/synced_bcf_reader.h"
#include "htslib/kseq.h"
#include "htslib/khash_str2int.h"
#include "htslib/bgzf.h"
#include "htslib/thread_pool.h"
#include "bcf_sr_sort.h"
#define REQUIRE_IDX_ 1
#define ALLOW_NO_IDX_ 2
// Maximum indexable coordinate of .csi, for default min_shift of 14.
// This comes out to about 17 Tbp. Limiting factor is the bin number,
// which is a uint32_t in CSI. The highest number of levels compatible
// with this is 10 (needs 31 bits).
#define MAX_CSI_COOR ((1LL << (14 + 30)) - 1)
typedef struct
{
hts_pos_t start, end; // records are marked for skipping have start>end
}
region1_t;
typedef struct bcf_sr_region_t
{
region1_t *regs; // regions will sorted and merged, redundant records marked for skipping have start>end
int nregs, mregs, creg; // creg: the current active region
}
region_t;
#define BCF_SR_AUX(x) ((aux_t*)((x)->aux))
typedef struct
{
sr_sort_t sort;
int regions_overlap, targets_overlap;
}
aux_t;
static int _regions_add(bcf_sr_regions_t *reg, const char *chr, hts_pos_t start, hts_pos_t end);
static bcf_sr_regions_t *_regions_init_string(const char *str);
static int _regions_match_alleles(bcf_sr_regions_t *reg, int als_idx, bcf1_t *rec);
static void _regions_sort_and_merge(bcf_sr_regions_t *reg);
static int _bcf_sr_regions_overlap(bcf_sr_regions_t *reg, const char *seq, hts_pos_t start, hts_pos_t end, int missed_reg_handler);
char *bcf_sr_strerror(int errnum)
{
switch (errnum)
{
case open_failed:
return strerror(errno);
case not_bgzf:
return "not compressed with bgzip";
case idx_load_failed:
return "could not load index";
case file_type_error:
return "unknown file type";
case api_usage_error:
return "API usage error";
case header_error:
return "could not parse header";
case no_eof:
return "no BGZF EOF marker; file may be truncated";
case no_memory:
return "Out of memory";
case vcf_parse_error:
return "VCF parse error";
case bcf_read_error:
return "BCF read error";
case noidx_error:
return "merge of unindexed files failed";
default: return "";
}
}
int bcf_sr_set_opt(bcf_srs_t *readers, bcf_sr_opt_t opt, ...)
{
va_list args;
switch (opt)
{
case BCF_SR_REQUIRE_IDX:
readers->require_index = REQUIRE_IDX_;
return 0;
case BCF_SR_ALLOW_NO_IDX:
readers->require_index = ALLOW_NO_IDX_;
return 0;
case BCF_SR_PAIR_LOGIC:
va_start(args, opt);
BCF_SR_AUX(readers)->sort.pair = va_arg(args, int);
return 0;
case BCF_SR_REGIONS_OVERLAP:
va_start(args, opt);
BCF_SR_AUX(readers)->regions_overlap = va_arg(args, int);
if ( readers->regions ) readers->regions->overlap = BCF_SR_AUX(readers)->regions_overlap;
return 0;
case BCF_SR_TARGETS_OVERLAP:
va_start(args, opt);
BCF_SR_AUX(readers)->targets_overlap = va_arg(args, int);
if ( readers->targets ) readers->targets->overlap = BCF_SR_AUX(readers)->targets_overlap;
return 0;
default:
break;
}
return 1;
}
static int *init_filters(bcf_hdr_t *hdr, const char *filters, int *nfilters)
{
kstring_t str = {0,0,0};
const char *tmp = filters, *prev = filters;
int nout = 0, *out = NULL;
while ( 1 )
{
if ( *tmp==',' || !*tmp )
{
int *otmp = (int*) realloc(out, (nout+1)*sizeof(int));
if (!otmp)
goto err;
out = otmp;
if ( tmp-prev==1 && *prev=='.' )
{
out[nout] = -1;
nout++;
}
else
{
str.l = 0;
kputsn(prev, tmp-prev, &str);
out[nout] = bcf_hdr_id2int(hdr, BCF_DT_ID, str.s);
if ( out[nout]>=0 ) nout++;
}
if ( !*tmp ) break;
prev = tmp+1;
}
tmp++;
}
if ( str.m ) free(str.s);
*nfilters = nout;
return out;
err:
if (str.m) free(str.s);
free(out);
return NULL;
}
int bcf_sr_set_regions(bcf_srs_t *readers, const char *regions, int is_file)
{
if ( readers->nreaders || readers->regions )
{
hts_log_error("Must call bcf_sr_set_regions() before bcf_sr_add_reader()");
return -1;
}
readers->regions = bcf_sr_regions_init(regions,is_file,0,1,-2);
if ( !readers->regions ) return -1;
readers->explicit_regs = 1;
readers->require_index = REQUIRE_IDX_;
readers->regions->overlap = BCF_SR_AUX(readers)->regions_overlap;
return 0;
}
int bcf_sr_set_targets(bcf_srs_t *readers, const char *targets, int is_file, int alleles)
{
if ( readers->nreaders || readers->targets )
{
hts_log_error("Must call bcf_sr_set_targets() before bcf_sr_add_reader()");
return -1;
}
if ( targets[0]=='^' )
{
readers->targets_exclude = 1;
targets++;
}
readers->targets = bcf_sr_regions_init(targets,is_file,0,1,-2);
if ( !readers->targets ) return -1;
readers->targets_als = alleles;
readers->targets->overlap = BCF_SR_AUX(readers)->targets_overlap;
return 0;
}
int bcf_sr_set_threads(bcf_srs_t *files, int n_threads)
{
if (!(files->n_threads = n_threads))
return 0;
files->p = calloc(1, sizeof(*files->p));
if (!files->p) {
files->errnum = no_memory;
return -1;
}
if (!(files->p->pool = hts_tpool_init(n_threads)))
return -1;
return 0;
}
void bcf_sr_destroy_threads(bcf_srs_t *files) {
if (!files->p)
return;
if (files->p->pool)
hts_tpool_destroy(files->p->pool);
free(files->p);
}
int bcf_sr_add_reader(bcf_srs_t *files, const char *fname)
{
char fmode[5];
strcpy(fmode, "r");
vcf_open_mode(fmode+1, fname, NULL);
htsFile* file_ptr = hts_open(fname, fmode);
if ( ! file_ptr ) {
files->errnum = open_failed;
return 0;
}
files->has_line = (int*) realloc(files->has_line, sizeof(int)*(files->nreaders+1));
files->has_line[files->nreaders] = 0;
files->readers = (bcf_sr_t*) realloc(files->readers, sizeof(bcf_sr_t)*(files->nreaders+1));
bcf_sr_t *reader = &files->readers[files->nreaders++];
memset(reader,0,sizeof(bcf_sr_t));
reader->file = file_ptr;
files->errnum = 0;
if ( reader->file->format.compression==bgzf )
{
BGZF *bgzf = hts_get_bgzfp(reader->file);
if ( bgzf && bgzf_check_EOF(bgzf) == 0 ) {
files->errnum = no_eof;
hts_log_warning("No BGZF EOF marker; file '%s' may be truncated", fname);
}
if (files->p)
bgzf_thread_pool(bgzf, files->p->pool, files->p->qsize);
}
if ( files->require_index==REQUIRE_IDX_ )
{
if ( reader->file->format.format==vcf )
{
if ( reader->file->format.compression!=bgzf )
{
files->errnum = not_bgzf;
return 0;
}
reader->tbx_idx = tbx_index_load(fname);
if ( !reader->tbx_idx )
{
files->errnum = idx_load_failed;
return 0;
}
reader->header = bcf_hdr_read(reader->file);
}
else if ( reader->file->format.format==bcf )
{
if ( reader->file->format.compression!=bgzf )
{
files->errnum = not_bgzf;
return 0;
}
reader->header = bcf_hdr_read(reader->file);
reader->bcf_idx = bcf_index_load(fname);
if ( !reader->bcf_idx )
{
files->errnum = idx_load_failed;
return 0;
}
}
else
{
files->errnum = file_type_error;
return 0;
}
}
else
{
if ( reader->file->format.format==bcf || reader->file->format.format==vcf )
{
reader->header = bcf_hdr_read(reader->file);
}
else
{
files->errnum = file_type_error;
return 0;
}
files->streaming = 1;
}
if ( files->streaming && files->nreaders>1 )
{
static int no_index_warned = 0;
if ( files->require_index==ALLOW_NO_IDX_ && !no_index_warned )
{
hts_log_warning("Using multiple unindexed files may produce errors, make sure chromosomes are in the same order!");
no_index_warned = 1;
}
if ( files->require_index!=ALLOW_NO_IDX_ )
{
files->errnum = api_usage_error;
hts_log_error("Must set require_index when the number of readers is greater than one");
return 0;
}
}
if ( files->streaming && files->regions )
{
files->errnum = api_usage_error;
hts_log_error("Cannot tabix-jump in streaming mode");
return 0;
}
if ( !reader->header )
{
files->errnum = header_error;
return 0;
}
reader->fname = strdup(fname);
if ( files->apply_filters )
reader->filter_ids = init_filters(reader->header, files->apply_filters, &reader->nfilter_ids);
// Update list of chromosomes
if ( !files->explicit_regs && !files->streaming )
{
int n = 0, i;
const char **names = reader->tbx_idx ? tbx_seqnames(reader->tbx_idx, &n) : bcf_hdr_seqnames(reader->header, &n);
for (i=0; i<n; i++)
{
if ( !files->regions )
files->regions = _regions_init_string(names[i]);
else
_regions_add(files->regions, names[i], -1, -1);
}
free(names);
_regions_sort_and_merge(files->regions);
}
if ( files->require_index==ALLOW_NO_IDX_ && files->nreaders > 1 )
{
bcf_hdr_t *hdr0 = files->readers[0].header;
bcf_hdr_t *hdr1 = reader->header;
if ( hdr0->n[BCF_DT_CTG]!=hdr1->n[BCF_DT_CTG] )
{
files->errnum = noidx_error;
hts_log_error("Different number of sequences in the header, refusing to stream multiple unindexed files");
return 0;
}
int i;
for (i=0; i<hdr0->n[BCF_DT_CTG]; i++)
{
if ( strcmp(bcf_hdr_id2name(hdr0,i),bcf_hdr_id2name(hdr1,i)) )
{
files->errnum = noidx_error;
hts_log_error("Sequences in the header appear in different order, refusing to stream multiple unindexed files");
return 0;
}
}
}
return 1;
}
bcf_srs_t *bcf_sr_init(void)
{
bcf_srs_t *files = (bcf_srs_t*) calloc(1,sizeof(bcf_srs_t));
files->aux = (aux_t*) calloc(1,sizeof(aux_t));
bcf_sr_sort_init(&BCF_SR_AUX(files)->sort);
bcf_sr_set_opt(files,BCF_SR_REGIONS_OVERLAP,1);
bcf_sr_set_opt(files,BCF_SR_TARGETS_OVERLAP,0);
return files;
}
static void bcf_sr_destroy1(bcf_sr_t *reader)
{
free(reader->fname);
if ( reader->tbx_idx ) tbx_destroy(reader->tbx_idx);
if ( reader->bcf_idx ) hts_idx_destroy(reader->bcf_idx);
bcf_hdr_destroy(reader->header);
hts_close(reader->file);
if ( reader->itr ) tbx_itr_destroy(reader->itr);
int j;
for (j=0; j<reader->mbuffer; j++)
bcf_destroy1(reader->buffer[j]);
free(reader->buffer);
free(reader->samples);
free(reader->filter_ids);
}
void bcf_sr_destroy(bcf_srs_t *files)
{
int i;
for (i=0; i<files->nreaders; i++)
bcf_sr_destroy1(&files->readers[i]);
free(files->has_line);
free(files->readers);
for (i=0; i<files->n_smpl; i++) free(files->samples[i]);
free(files->samples);
if (files->targets) bcf_sr_regions_destroy(files->targets);
if (files->regions) bcf_sr_regions_destroy(files->regions);
if (files->tmps.m) free(files->tmps.s);
if (files->n_threads) bcf_sr_destroy_threads(files);
bcf_sr_sort_destroy(&BCF_SR_AUX(files)->sort);
free(files->aux);
free(files);
}
void bcf_sr_remove_reader(bcf_srs_t *files, int i)
{
assert( !files->samples ); // not ready for this yet
bcf_sr_sort_remove_reader(files, &BCF_SR_AUX(files)->sort, i);
bcf_sr_destroy1(&files->readers[i]);
if ( i+1 < files->nreaders )
{
memmove(&files->readers[i], &files->readers[i+1], (files->nreaders-i-1)*sizeof(bcf_sr_t));
memmove(&files->has_line[i], &files->has_line[i+1], (files->nreaders-i-1)*sizeof(int));
}
files->nreaders--;
}
#if DEBUG_SYNCED_READER
void debug_buffer(FILE *fp, bcf_sr_t *reader)
{
int j;
for (j=0; j<=reader->nbuffer; j++)
{
bcf1_t *line = reader->buffer[j];
fprintf(fp,"\t%p\t%s%s\t%s:%"PRIhts_pos"\t%s ", (void*)line,reader->fname,j==0?"*":" ",reader->header->id[BCF_DT_CTG][line->rid].key,line->pos+1,line->n_allele?line->d.allele[0]:"");
int k;
for (k=1; k<line->n_allele; k++) fprintf(fp," %s", line->d.allele[k]);
fprintf(fp,"\n");
}
}
void debug_buffers(FILE *fp, bcf_srs_t *files)
{
int i;
for (i=0; i<files->nreaders; i++)
{
fprintf(fp, "has_line: %d\t%s\n", bcf_sr_has_line(files,i),files->readers[i].fname);
debug_buffer(fp, &files->readers[i]);
}
fprintf(fp,"\n");
}
#endif
static inline int has_filter(bcf_sr_t *reader, bcf1_t *line)
{
int i, j;
if ( !line->d.n_flt )
{
for (j=0; j<reader->nfilter_ids; j++)
if ( reader->filter_ids[j]<0 ) return 1;
return 0;
}
for (i=0; i<line->d.n_flt; i++)
{
for (j=0; j<reader->nfilter_ids; j++)
if ( line->d.flt[i]==reader->filter_ids[j] ) return 1;
}
return 0;
}
static int _reader_seek(bcf_sr_t *reader, const char *seq, hts_pos_t start, hts_pos_t end)
{
if ( end>=MAX_CSI_COOR )
{
hts_log_error("The coordinate is out of csi index limit: %"PRIhts_pos, end+1);
exit(1);
}
if ( reader->itr )
{
hts_itr_destroy(reader->itr);
reader->itr = NULL;
}
reader->nbuffer = 0;
if ( reader->tbx_idx )
{
int tid = tbx_name2id(reader->tbx_idx, seq);
if ( tid==-1 ) return -1; // the sequence not present in this file
reader->itr = tbx_itr_queryi(reader->tbx_idx,tid,start,end+1);
}
else
{
int tid = bcf_hdr_name2id(reader->header, seq);
if ( tid==-1 ) return -1; // the sequence not present in this file
reader->itr = bcf_itr_queryi(reader->bcf_idx,tid,start,end+1);
}
if (!reader->itr) {
hts_log_error("Could not seek: %s:%"PRIhts_pos"-%"PRIhts_pos, seq, start + 1, end + 1);
assert(0);
}
return 0;
}
/*
* _readers_next_region() - jumps to next region if necessary
* Returns 0 on success or -1 when there are no more regions left
*/
static int _readers_next_region(bcf_srs_t *files)
{
// Need to open new chromosome? Check number of lines in all readers' buffers
int i, eos = 0;
for (i=0; i<files->nreaders; i++)
if ( !files->readers[i].itr && !files->readers[i].nbuffer ) eos++;
if ( eos!=files->nreaders )
{
// Some of the readers still has buffered lines
return 0;
}
// No lines in the buffer, need to open new region or quit.
int prev_iseq = files->regions->iseq;
hts_pos_t prev_end = files->regions->end;
if ( bcf_sr_regions_next(files->regions)<0 ) return -1;
files->regions->prev_end = prev_iseq==files->regions->iseq ? prev_end : -1;
for (i=0; i<files->nreaders; i++)
_reader_seek(&files->readers[i],files->regions->seq_names[files->regions->iseq],files->regions->start,files->regions->end);
return 0;
}
static void _set_variant_boundaries(bcf1_t *rec, hts_pos_t *beg, hts_pos_t *end)
{
hts_pos_t off;
if ( rec->n_allele )
{
off = rec->rlen;
bcf_unpack(rec, BCF_UN_STR);
int i;
for (i=1; i<rec->n_allele; i++)
{
// Make symbolic alleles start at POS, although this is not strictly true for
// <DEL>,<INS> where POS should be the position BEFORE the deletion/insertion.
// However, since arbitrary symbolic alleles can be defined by the user, we
// will simplify the interpretation of --targets-overlap and --region-overlap.
int j = 0;
char *ref = rec->d.allele[0];
char *alt = rec->d.allele[i];
while ( ref[j] && alt[j] && ref[j]==alt[j] ) j++;
if ( off > j ) off = j;
if ( !off ) break;
}
}
else
off = 0;
*beg = rec->pos + off;
*end = rec->pos + rec->rlen - 1;
}
/*
* _reader_fill_buffer() - buffers all records with the same coordinate
*/
static int _reader_fill_buffer(bcf_srs_t *files, bcf_sr_t *reader)
{
// Return if the buffer is full: the coordinate of the last buffered record differs
if ( reader->nbuffer && reader->buffer[reader->nbuffer]->pos != reader->buffer[1]->pos ) return 0;
// No iterator (sequence not present in this file) and not streaming
if ( !reader->itr && !files->streaming ) return 0;
// Fill the buffer with records starting at the same position
int i, ret = 0;
while (1)
{
if ( reader->nbuffer+1 >= reader->mbuffer )
{
// Increase buffer size
reader->mbuffer += 8;
reader->buffer = (bcf1_t**) realloc(reader->buffer, sizeof(bcf1_t*)*reader->mbuffer);
for (i=8; i>0; i--) // initialize
{
reader->buffer[reader->mbuffer-i] = bcf_init1();
reader->buffer[reader->mbuffer-i]->max_unpack = files->max_unpack;
reader->buffer[reader->mbuffer-i]->pos = -1; // for rare cases when VCF starts from 1
}
}
if ( files->streaming )
{
if ( reader->file->format.format==vcf )
{
if ( (ret=hts_getline(reader->file, KS_SEP_LINE, &files->tmps)) < 0 ) break; // no more lines
ret = vcf_parse1(&files->tmps, reader->header, reader->buffer[reader->nbuffer+1]);
if ( ret<0 ) { files->errnum = vcf_parse_error; break; }
}
else if ( reader->file->format.format==bcf )
{
ret = bcf_read1(reader->file, reader->header, reader->buffer[reader->nbuffer+1]);
if ( ret < -1 ) files->errnum = bcf_read_error;
if ( ret < 0 ) break; // no more lines or an error
}
else
{
hts_log_error("Fixme: not ready for this");
exit(1);
}
}
else if ( reader->tbx_idx )
{
if ( (ret=tbx_itr_next(reader->file, reader->tbx_idx, reader->itr, &files->tmps)) < 0 ) break; // no more lines
ret = vcf_parse1(&files->tmps, reader->header, reader->buffer[reader->nbuffer+1]);
if ( ret<0 ) { files->errnum = vcf_parse_error; break; }
}
else
{
ret = bcf_itr_next(reader->file, reader->itr, reader->buffer[reader->nbuffer+1]);
if ( ret < -1 ) files->errnum = bcf_read_error;
if ( ret < 0 ) break; // no more lines or an error
bcf_subset_format(reader->header,reader->buffer[reader->nbuffer+1]);
}
// Prevent creation of duplicates from records overlapping multiple regions
// and recognize true variant overlaps vs record overlaps (e.g. TA>T vs A>-)
if ( files->regions )
{
hts_pos_t beg, end;
if ( BCF_SR_AUX(files)->regions_overlap==0 )
beg = end = reader->buffer[reader->nbuffer+1]->pos;
else if ( BCF_SR_AUX(files)->regions_overlap==1 )
{
beg = reader->buffer[reader->nbuffer+1]->pos;
end = reader->buffer[reader->nbuffer+1]->pos + reader->buffer[reader->nbuffer+1]->rlen - 1;
}
else if ( BCF_SR_AUX(files)->regions_overlap==2 )
_set_variant_boundaries(reader->buffer[reader->nbuffer+1], &beg,&end);
else
{
hts_log_error("This should never happen, just to keep clang compiler happy: %d",BCF_SR_AUX(files)->targets_overlap);
exit(1);
}
if ( beg <= files->regions->prev_end || end < files->regions->start || beg > files->regions->end ) continue;
}
// apply filter
if ( !reader->nfilter_ids )
bcf_unpack(reader->buffer[reader->nbuffer+1], BCF_UN_STR);
else
{
bcf_unpack(reader->buffer[reader->nbuffer+1], BCF_UN_STR|BCF_UN_FLT);
if ( !has_filter(reader, reader->buffer[reader->nbuffer+1]) ) continue;
}
reader->nbuffer++;
if ( reader->buffer[reader->nbuffer]->rid != reader->buffer[1]->rid ) break;
if ( reader->buffer[reader->nbuffer]->pos != reader->buffer[1]->pos ) break; // the buffer is full
}
if ( ret<0 )
{
// done for this region
tbx_itr_destroy(reader->itr);
reader->itr = NULL;
}
if ( files->require_index==ALLOW_NO_IDX_ && reader->buffer[reader->nbuffer]->rid < reader->buffer[1]->rid )
{
hts_log_error("Sequences out of order, cannot stream multiple unindexed files: %s", reader->fname);
exit(1);
}
return 0; // FIXME: Check for more errs in this function
}
/*
* _readers_shift_buffer() - removes the first line
*/
static void _reader_shift_buffer(bcf_sr_t *reader)
{
if ( !reader->nbuffer ) return;
int i;
bcf1_t *tmp = reader->buffer[1];
for (i=2; i<=reader->nbuffer; i++)
reader->buffer[i-1] = reader->buffer[i];
if ( reader->nbuffer > 1 )
reader->buffer[reader->nbuffer] = tmp;
reader->nbuffer--;
}
static int next_line(bcf_srs_t *files)
{
const char *chr = NULL;
hts_pos_t min_pos = HTS_POS_MAX;
// Loop until next suitable line is found or all readers have finished
while ( 1 )
{
// Get all readers ready for the next region.
if ( files->regions && _readers_next_region(files)<0 ) break;
// Fill buffers and find the minimum chromosome
int i, min_rid = INT32_MAX;
for (i=0; i<files->nreaders; i++)
{
_reader_fill_buffer(files, &files->readers[i]);
if ( files->require_index==ALLOW_NO_IDX_ )
{
if ( !files->readers[i].nbuffer ) continue;
if ( min_rid > files->readers[i].buffer[1]->rid ) min_rid = files->readers[i].buffer[1]->rid;
}
}
for (i=0; i<files->nreaders; i++)
{
if ( !files->readers[i].nbuffer ) continue;
if ( files->require_index==ALLOW_NO_IDX_ && min_rid != files->readers[i].buffer[1]->rid ) continue;
// Update the minimum coordinate
if ( min_pos > files->readers[i].buffer[1]->pos )
{
min_pos = files->readers[i].buffer[1]->pos;
chr = bcf_seqname(files->readers[i].header, files->readers[i].buffer[1]);
assert(chr);
bcf_sr_sort_set_active(&BCF_SR_AUX(files)->sort, i);
}
else if ( min_pos==files->readers[i].buffer[1]->pos )
bcf_sr_sort_add_active(&BCF_SR_AUX(files)->sort, i);
}
if ( min_pos==HTS_POS_MAX )
{
if ( !files->regions ) break;
continue;
}
// Skip this position if not present in targets
if ( files->targets )
{
int match = 0;
for (i=0; i<files->nreaders; i++)
{
if ( !files->readers[i].nbuffer || files->readers[i].buffer[1]->pos!=min_pos ) continue;
hts_pos_t beg, end;
if ( BCF_SR_AUX(files)->targets_overlap==0 )
beg = end = min_pos;
else if ( BCF_SR_AUX(files)->targets_overlap==1 )
{
beg = min_pos;
end = min_pos + files->readers[i].buffer[1]->rlen - 1;
}
else if ( BCF_SR_AUX(files)->targets_overlap==2 )
_set_variant_boundaries(files->readers[i].buffer[1], &beg,&end);
else
{
hts_log_error("This should never happen, just to keep clang compiler happy: %d",BCF_SR_AUX(files)->targets_overlap);
exit(1);
}
int overlap = bcf_sr_regions_overlap(files->targets, chr, beg, end)==0 ? 1 : 0;
if ( (!files->targets_exclude && !overlap) || (files->targets_exclude && overlap) )
_reader_shift_buffer(&files->readers[i]);
else
match = 1;
}
if ( !match )
{
min_pos = HTS_POS_MAX;
chr = NULL;
continue;
}
}
break; // done: chr and min_pos are set
}
if ( !chr ) return 0;
return bcf_sr_sort_next(files, &BCF_SR_AUX(files)->sort, chr, min_pos);
}
int bcf_sr_next_line(bcf_srs_t *files)
{
if ( !files->targets_als )
return next_line(files);
while (1)
{
int i, ret = next_line(files);
if ( !ret ) return ret;
for (i=0; i<files->nreaders; i++)
if ( files->has_line[i] ) break;
if ( _regions_match_alleles(files->targets, files->targets_als-1, files->readers[i].buffer[0]) ) return ret;
// Check if there are more duplicate lines in the buffers. If not, return this line as if it
// matched the targets, even if there is a type mismatch
for (i=0; i<files->nreaders; i++)
{
if ( !files->has_line[i] ) continue;
if ( files->readers[i].nbuffer==0 || files->readers[i].buffer[1]->pos!=files->readers[i].buffer[0]->pos ) continue;
break;
}
if ( i==files->nreaders ) return ret; // no more lines left, output even if target alleles are not of the same type
}
}
static void bcf_sr_seek_start(bcf_srs_t *readers)
{
bcf_sr_regions_t *reg = readers->regions;
int i;
for (i=0; i<reg->nseqs; i++)
reg->regs[i].creg = -1;
reg->iseq = 0;
reg->prev_seq = -1;
}
int bcf_sr_seek(bcf_srs_t *readers, const char *seq, hts_pos_t pos)
{
if ( !readers->regions ) return 0;
bcf_sr_sort_reset(&BCF_SR_AUX(readers)->sort);
if ( !seq && !pos )
{
// seek to start
bcf_sr_seek_start(readers);
return 0;
}
int i, nret = 0;
// Need to position both the readers and the regions. The latter is a bit of a mess
// because we can have in memory or external regions. The safe way is:
// - reset all regions as if they were not read from at all (bcf_sr_seek_start)
// - find the requested iseq (stored in the seq_hash)
// - position regions to the requested position (bcf_sr_regions_overlap)
bcf_sr_seek_start(readers);
if ( khash_str2int_get(readers->regions->seq_hash, seq, &i)>=0 ) readers->regions->iseq = i;
_bcf_sr_regions_overlap(readers->regions, seq, pos, pos, 0);
for (i=0; i<readers->nreaders; i++)
{
nret += _reader_seek(&readers->readers[i],seq,pos,MAX_CSI_COOR-1);
}
return nret;
}
int bcf_sr_set_samples(bcf_srs_t *files, const char *fname, int is_file)
{
int i, j, nsmpl, free_smpl = 0;
char **smpl = NULL;
void *exclude = (fname[0]=='^') ? khash_str2int_init() : NULL;
if ( exclude || strcmp("-",fname) ) // "-" stands for all samples
{
smpl = hts_readlist(fname, is_file, &nsmpl);
if ( !smpl )
{
hts_log_error("Could not read the file: \"%s\"", fname);
return 0;
}
if ( exclude )
{
for (i=0; i<nsmpl; i++)
khash_str2int_inc(exclude, smpl[i]);
}
free_smpl = 1;
}
if ( !smpl )
{
smpl = files->readers[0].header->samples; // intersection of all samples
nsmpl = bcf_hdr_nsamples(files->readers[0].header);
}
files->samples = NULL;
files->n_smpl = 0;
for (i=0; i<nsmpl; i++)
{
if ( exclude && khash_str2int_has_key(exclude,smpl[i]) ) continue;
int n_isec = 0;
for (j=0; j<files->nreaders; j++)
{
if ( bcf_hdr_id2int(files->readers[j].header, BCF_DT_SAMPLE, smpl[i])<0 ) break;
n_isec++;
}
if ( n_isec!=files->nreaders )
{
hts_log_warning("The sample \"%s\" was not found in %s, skipping",
smpl[i], files->readers[n_isec].fname);
continue;
}
files->samples = (char**) realloc(files->samples, (files->n_smpl+1)*sizeof(const char*));
files->samples[files->n_smpl++] = strdup(smpl[i]);
}
if ( exclude ) khash_str2int_destroy(exclude);
if ( free_smpl )
{
for (i=0; i<nsmpl; i++) free(smpl[i]);
free(smpl);
}
if ( !files->n_smpl )
{
if ( files->nreaders>1 )
hts_log_warning("No samples in common");
return 0;
}
for (i=0; i<files->nreaders; i++)
{
bcf_sr_t *reader = &files->readers[i];
reader->samples = (int*) malloc(sizeof(int)*files->n_smpl);
reader->n_smpl = files->n_smpl;
for (j=0; j<files->n_smpl; j++)
reader->samples[j] = bcf_hdr_id2int(reader->header, BCF_DT_SAMPLE, files->samples[j]);
}
return 1;
}
// Add a new region into a list. On input the coordinates are 1-based, inclusive, then stored 0-based,
// inclusive. Sorting and merging step needed afterwards: qsort(..,cmp_regions) and merge_regions().
static int _regions_add(bcf_sr_regions_t *reg, const char *chr, hts_pos_t start, hts_pos_t end)
{
if ( start==-1 && end==-1 )
{
start = 0; end = MAX_CSI_COOR-1;
}
else
{
start--; end--; // store 0-based coordinates
}
if ( !reg->seq_hash )
reg->seq_hash = khash_str2int_init();
int iseq;
if ( khash_str2int_get(reg->seq_hash, chr, &iseq)<0 )
{
// the chromosome block does not exist
iseq = reg->nseqs++;
reg->seq_names = (char**) realloc(reg->seq_names,sizeof(char*)*reg->nseqs);
reg->regs = (region_t*) realloc(reg->regs,sizeof(region_t)*reg->nseqs);
memset(®->regs[reg->nseqs-1],0,sizeof(region_t));
reg->seq_names[iseq] = strdup(chr);
reg->regs[iseq].creg = -1;
khash_str2int_set(reg->seq_hash,reg->seq_names[iseq],iseq);
}
region_t *creg = ®->regs[iseq];
hts_expand(region1_t,creg->nregs+1,creg->mregs,creg->regs);
creg->regs[creg->nregs].start = start;
creg->regs[creg->nregs].end = end;
creg->nregs++;
return 0; // FIXME: check for errs in this function
}
static int regions_cmp(const void *aptr, const void *bptr)
{
region1_t *a = (region1_t*)aptr;
region1_t *b = (region1_t*)bptr;
if ( a->start < b->start ) return -1;
if ( a->start > b->start ) return 1;
if ( a->end < b->end ) return -1;
if ( a->end > b->end ) return 1;
return 0;
}
static void regions_merge(region_t *reg)
{
int i = 0, j;
while ( i<reg->nregs )