forked from samtools/htslib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
synced_bcf_reader.c
1226 lines (1107 loc) · 37.1 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-2014 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. */
#include <config.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <limits.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 MAX_CSI_COOR 0x7fffffff // maximum indexable coordinate of .csi
typedef struct
{
uint32_t start, end;
}
region1_t;
typedef struct _region_t
{
region1_t *regs;
int nregs, mregs, creg;
}
region_t;
#define BCF_SR_AUX(x) ((aux_t*)((x)->aux))
typedef struct
{
sr_sort_t sort;
}
aux_t;
static void _regions_add(bcf_sr_regions_t *reg, const char *chr, int start, int 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);
char *bcf_sr_strerror(int errnum)
{
switch (errnum)
{
case open_failed:
return strerror(errno); break;
case not_bgzf:
return "not compressed with bgzip"; break;
case idx_load_failed:
return "could not load index"; break;
case file_type_error:
return "unknown file type"; break;
case api_usage_error:
return "API usage error"; break;
case header_error:
return "could not parse header"; break;
case no_eof:
return "no BGZF EOF marker; file may be truncated"; break;
case no_memory:
return "Out of memory"; break;
case vcf_parse_error:
return "VCF parse error"; break;
case bcf_read_error:
return "BCF read error"; break;
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 = 1;
return 0;
case BCF_SR_PAIR_LOGIC:
va_start(args, opt);
BCF_SR_AUX(readers)->sort.pair = va_arg(args, int);
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 )
{
out = (int*) realloc(out, (nout+1)*sizeof(int));
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;
}
int bcf_sr_set_regions(bcf_srs_t *readers, const char *regions, int is_file)
{
assert( !readers->regions );
if ( readers->nreaders )
{
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 = 1;
return 0;
}
int bcf_sr_set_targets(bcf_srs_t *readers, const char *targets, int is_file, int alleles)
{
assert( !readers->targets );
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;
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)
{
htsFile* file_ptr = hts_open(fname, "r");
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 )
{
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 )
{
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,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);
}
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);
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--;
}
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:%d\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");
}
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, int start, int end)
{
if ( end>=MAX_CSI_COOR )
{
hts_log_error("The coordinate is out of csi index limit: %d", 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:%d-%d", 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
if ( bcf_sr_regions_next(files->regions)<0 ) return -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;
}
/*
* _reader_fill_buffer() - buffers all records with the same coordinate
*/
static void _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;
// No iterator (sequence not present in this file) and not streaming
if ( !reader->itr && !files->streaming ) return;
// 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]);
}
// 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]->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;
}
}
/*
* _readers_shift_buffer() - removes the first line and all subsequent lines with the same position
*/
static void _reader_shift_buffer(bcf_sr_t *reader)
{
int i;
for (i=2; i<=reader->nbuffer; i++)
if ( reader->buffer[i]->pos!=reader->buffer[1]->pos ) break;
if ( i<=reader->nbuffer )
{
// A record with a different position follows, swap it. Because of the reader's logic,
// only one such line can be present.
bcf1_t *tmp = reader->buffer[1]; reader->buffer[1] = reader->buffer[i]; reader->buffer[i] = tmp;
reader->nbuffer = 1;
}
else
reader->nbuffer = 0; // no other line
}
int _reader_next_line(bcf_srs_t *files)
{
int i, min_pos = INT_MAX;
const char *chr = NULL;
// 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
for (i=0; i<files->nreaders; i++)
{
_reader_fill_buffer(files, &files->readers[i]);
// Update the minimum coordinate
if ( !files->readers[i].nbuffer ) continue;
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]);
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==INT_MAX )
{
if ( !files->regions ) break;
continue;
}
// Skip this position if not present in targets
if ( files->targets )
{
int ret = bcf_sr_regions_overlap(files->targets, chr, min_pos, min_pos);
if ( (!files->targets_exclude && ret<0) || (files->targets_exclude && !ret) )
{
// Remove all lines with this position from the buffer
for (i=0; i<files->nreaders; i++)
if ( files->readers[i].nbuffer && files->readers[i].buffer[1]->pos==min_pos )
_reader_shift_buffer(&files->readers[i]);
min_pos = INT_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 _reader_next_line(files);
while (1)
{
int i, ret = _reader_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;
}
int bcf_sr_seek(bcf_srs_t *readers, const char *seq, int pos)
{
if ( !readers->regions ) return 0;
if ( !seq && !pos )
{
// seek to start
bcf_sr_seek_start(readers);
return 0;
}
bcf_sr_regions_overlap(readers->regions, seq, pos, pos);
int i, nret = 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 sorted by start,end. On input the coordinates
// are 1-based, stored 0-based, inclusive.
static void _regions_add(bcf_sr_regions_t *reg, const char *chr, int start, int 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];
// the regions may not be sorted on input: binary search
int i, min = 0, max = creg->nregs - 1;
while ( min<=max )
{
i = (max+min)/2;
if ( start < creg->regs[i].start ) max = i - 1;
else if ( start > creg->regs[i].start ) min = i + 1;
else break;
}
if ( min>max || creg->regs[i].start!=start || creg->regs[i].end!=end )
{
// no such region, insert a new one just after max
hts_expand(region1_t,creg->nregs+1,creg->mregs,creg->regs);
if ( ++max < creg->nregs )
memmove(&creg->regs[max+1],&creg->regs[max],(creg->nregs - max)*sizeof(region1_t));
creg->regs[max].start = start;
creg->regs[max].end = end;
creg->nregs++;
}
}
// File name or a list of genomic locations. If file name, NULL is returned.
static bcf_sr_regions_t *_regions_init_string(const char *str)
{
bcf_sr_regions_t *reg = (bcf_sr_regions_t *) calloc(1, sizeof(bcf_sr_regions_t));
reg->start = reg->end = -1;
reg->prev_start = reg->prev_seq = -1;
kstring_t tmp = {0,0,0};
const char *sp = str, *ep = str;
int from, to;
while ( 1 )
{
while ( *ep && *ep!=',' && *ep!=':' ) ep++;
tmp.l = 0;
kputsn(sp,ep-sp,&tmp);
if ( *ep==':' )
{
sp = ep+1;
from = hts_parse_decimal(sp,(char**)&ep,0);
if ( sp==ep )
{
hts_log_error("Could not parse the region(s): %s", str);
free(reg); free(tmp.s); return NULL;
}
if ( !*ep || *ep==',' )
{
_regions_add(reg, tmp.s, from, from);
sp = ep;
continue;
}
if ( *ep!='-' )
{
hts_log_error("Could not parse the region(s): %s", str);
free(reg); free(tmp.s); return NULL;
}
ep++;
sp = ep;
to = hts_parse_decimal(sp,(char**)&ep,0);
if ( *ep && *ep!=',' )
{
hts_log_error("Could not parse the region(s): %s", str);
free(reg); free(tmp.s); return NULL;
}
if ( sp==ep ) to = MAX_CSI_COOR-1;
_regions_add(reg, tmp.s, from, to);
if ( !*ep ) break;
sp = ep;
}
else
{
if ( tmp.l ) _regions_add(reg, tmp.s, -1, -1);
if ( !*ep ) break;
sp = ++ep;
}
}
free(tmp.s);
return reg;
}
// ichr,ifrom,ito are 0-based;
// returns -1 on error, 0 if the line is a comment line, 1 on success
static int _regions_parse_line(char *line, int ichr,int ifrom,int ito, char **chr,char **chr_end,int *from,int *to)
{
*chr_end = NULL;
if ( line[0]=='#' ) return 0;
int k,l; // index of the start and end column of the tab-delimited file
if ( ifrom <= ito )
k = ifrom, l = ito;
else
l = ifrom, k = ito;
int i;
char *se = line, *ss = NULL; // start and end
char *tmp;
for (i=0; i<=k && *se; i++)
{
ss = i==0 ? se++ : ++se;
while (*se && *se!='\t') se++;
}
if ( i<=k ) return -1;
if ( k==l )
{
*from = *to = hts_parse_decimal(ss, &tmp, 0);
if ( tmp==ss ) return -1;
}
else
{
if ( k==ifrom )
*from = hts_parse_decimal(ss, &tmp, 0);
else
*to = hts_parse_decimal(ss, &tmp, 0);
if ( ss==tmp ) return -1;
for (i=k; i<l && *se; i++)
{
ss = ++se;
while (*se && *se!='\t') se++;
}
if ( i<l ) return -1;
if ( k==ifrom )
*to = hts_parse_decimal(ss, &tmp, 0);
else
*from = hts_parse_decimal(ss, &tmp, 0);
if ( ss==tmp ) return -1;
}
ss = se = line;
for (i=0; i<=ichr && *se; i++)
{
if ( i>0 ) ss = ++se;
while (*se && *se!='\t') se++;
}
if ( i<=ichr ) return -1;
*chr_end = se;
*chr = ss;
return 1;
}
bcf_sr_regions_t *bcf_sr_regions_init(const char *regions, int is_file, int ichr, int ifrom, int ito)
{
bcf_sr_regions_t *reg;
if ( !is_file ) return _regions_init_string(regions);
reg = (bcf_sr_regions_t *) calloc(1, sizeof(bcf_sr_regions_t));
reg->start = reg->end = -1;
reg->prev_start = reg->prev_seq = -1;
reg->file = hts_open(regions, "rb");
if ( !reg->file )
{
hts_log_error("Could not open file: %s", regions);
free(reg);
return NULL;
}
reg->tbx = tbx_index_load(regions);
if ( !reg->tbx )
{
int len = strlen(regions);
int is_bed = strcasecmp(".bed",regions+len-4) ? 0 : 1;
if ( !is_bed && !strcasecmp(".bed.gz",regions+len-7) ) is_bed = 1;
if ( reg->file->format.format==vcf ) ito = 1;
// read the whole file, tabix index is not present
while ( hts_getline(reg->file, KS_SEP_LINE, ®->line) > 0 )
{
char *chr, *chr_end;
int from, to, ret;
ret = _regions_parse_line(reg->line.s, ichr,ifrom,abs(ito), &chr,&chr_end,&from,&to);
if ( ret < 0 )
{
if ( ito<0 )
ret = _regions_parse_line(reg->line.s, ichr,ifrom,ifrom, &chr,&chr_end,&from,&to);
if ( ret<0 )
{
hts_log_error("Could not parse the file %s, using the columns %d,%d[,%d]",
regions,ichr+1,ifrom+1,ito+1);
hts_close(reg->file); reg->file = NULL; free(reg);
return NULL;
}
}
if ( !ret ) continue;
if ( is_bed ) from++;
*chr_end = 0;
_regions_add(reg, chr, from, to);
*chr_end = '\t';
}
hts_close(reg->file); reg->file = NULL;
if ( !reg->nseqs ) { free(reg); return NULL; }
return reg;
}
reg->seq_names = (char**) tbx_seqnames(reg->tbx, ®->nseqs);
if ( !reg->seq_hash )
reg->seq_hash = khash_str2int_init();
int i;
for (i=0; i<reg->nseqs; i++)
{
khash_str2int_set(reg->seq_hash,reg->seq_names[i],i);
}
reg->fname = strdup(regions);
reg->is_bin = 1;