rust_htslib/bcf/
mod.rs

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
// Copyright 2014 Johannes Köster.
// Licensed under the MIT license (http://opensource.org/licenses/MIT)
// This file may not be copied, modified, or distributed
// except according to those terms.

//! Module for working with VCF and BCF files.
//!
//! # Performance Remarks
//!
//! Note that BCF corresponds to the in-memory representation of BCF/VCF records in Htslib
//! itself. Thus, it comes without a runtime penalty for parsing, in contrast to reading VCF
//! files.
//!
//! # Example (reading)
//!
//!   - Obtaining 0-based locus index of the VCF record.
//!   - Obtaining alleles of the VCF record.
//!   - calculate alt-allele dosage in a mutli-sample VCF / BCF
//!
//! ```
//! use crate::rust_htslib::bcf::{Reader, Read};
//! use std::convert::TryFrom;
//!
//! let path = &"test/test_string.vcf";
//! let mut bcf = Reader::from_path(path).expect("Error opening file.");
//! // iterate through each row of the vcf body.
//! for (i, record_result) in bcf.records().enumerate() {
//!     let mut record = record_result.expect("Fail to read record");
//!     let mut s = String::new();
//!      for allele in record.alleles() {
//!          for c in allele {
//!              s.push(char::from(*c))
//!          }
//!          s.push(' ')
//!      }
//!     // 0-based position and the list of alleles
//!     println!("Locus: {}, Alleles: {}", record.pos(), s);
//!     // number of sample in the vcf
//!     let sample_count = usize::try_from(record.sample_count()).unwrap();
//!
//!     // Counting ref, alt and missing alleles for each sample
//!     let mut n_ref = vec![0; sample_count];
//!     let mut n_alt = vec![0; sample_count];
//!     let mut n_missing = vec![0; sample_count];
//!     let gts = record.genotypes().expect("Error reading genotypes");
//!     for sample_index in 0..sample_count {
//!         // for each sample
//!         for gta in gts.get(sample_index).iter() {
//!             // for each allele
//!             match gta.index() {
//!                 Some(0) => n_ref[sample_index] += 1,  // reference allele
//!                 Some(_) => n_alt[sample_index] += 1,  // alt allele
//!                 None => n_missing[sample_index] += 1, // missing allele
//!             }
//!         }
//!     }
//! }
//! ```
//!
//! # Example (writing)
//!
//!   - Setting up a VCF writer from scratch (including a simple header)
//!   - Creating a VCF record and writing it to the VCF file
//!
//! ```
//! use rust_htslib::bcf::{Format, Writer};
//! use rust_htslib::bcf::header::Header;
//! use rust_htslib::bcf::record::GenotypeAllele;
//!
//! // Create minimal VCF header with a single contig and a single sample
//! let mut header = Header::new();
//! let header_contig_line = r#"##contig=<ID=1,length=10>"#;
//! header.push_record(header_contig_line.as_bytes());
//! let header_gt_line = r#"##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">"#;
//! header.push_record(header_gt_line.as_bytes());
//! header.push_sample("test_sample".as_bytes());
//!
//! // Write uncompressed VCF to stdout with above header and get an empty record
//! let mut vcf = Writer::from_stdout(&header, true, Format::Vcf).unwrap();
//! let mut record = vcf.empty_record();
//!
//! // Set chrom and pos to 1 and 7, respectively - note the 0-based positions
//! let rid = vcf.header().name2rid(b"1").unwrap();
//! record.set_rid(Some(rid));
//! record.set_pos(6);
//!
//! // Set record genotype to 0|1 - note first allele is always unphased
//! let alleles = &[GenotypeAllele::Unphased(0), GenotypeAllele::Phased(1)];
//! record.push_genotypes(alleles).unwrap();
//!
//! // Write record
//! vcf.write(&record).unwrap()
//! ```
//!
//! This will print the following VCF to stdout:
//!
//! ```lang-none
//! ##fileformat=VCFv4.2
//! ##FILTER=<ID=PASS,Description="All filters passed">
//! ##contig=<ID=1,length=10>
//! ##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
//! #CHROM  POS     ID      REF     ALT     QUAL    FILTER  INFO    FORMAT  test_sample
//! 1       7       .       .       .       0       .       .       GT      0|1
//! ```

use std::ffi;
use std::path::Path;
use std::rc::Rc;
use std::str;

use url::Url;

pub mod buffer;
pub mod header;
pub mod index;
pub mod record;

use crate::bcf::header::{HeaderView, SampleSubset};
use crate::errors::{Error, Result};
use crate::htslib;

pub use crate::bcf::header::{Header, HeaderRecord};
pub use crate::bcf::record::Record;

/// A trait for a BCF reader with a read method.
pub trait Read: Sized {
    /// Read the next record.
    ///
    /// # Arguments
    /// * record - an empty record, that can be created with `bcf::Reader::empty_record`.
    ///
    /// # Returns
    /// None if end of file was reached, otherwise Some will contain
    /// a result with an error in case of failure.
    fn read(&mut self, record: &mut record::Record) -> Option<Result<()>>;

    /// Return an iterator over all records of the VCF/BCF file.
    fn records(&mut self) -> Records<'_, Self>;

    /// Return the header.
    fn header(&self) -> &HeaderView;

    /// Return empty record.  Can be reused multiple times.
    fn empty_record(&self) -> Record;

    /// Activate multi-threaded BCF/VCF read support in htslib. This should permit faster
    /// reading of large VCF files.
    ///
    /// Setting `nthreads` to `0` does not change the current state.  Note that it is not
    /// possible to set the number of background threads below `1` once it has been set.
    ///
    /// # Arguments
    ///
    /// * `n_threads` - number of extra background writer threads to use, must be `> 0`.
    fn set_threads(&mut self, n_threads: usize) -> Result<()>;
}

/// A VCF/BCF reader.
#[derive(Debug)]
pub struct Reader {
    inner: *mut htslib::htsFile,
    header: Rc<HeaderView>,
}

unsafe impl Send for Reader {}
/// # Safety
///
/// Implementation for `Reader::set_threads()` and `Writer::set_threads`.
pub unsafe fn set_threads(hts_file: *mut htslib::htsFile, n_threads: usize) -> Result<()> {
    assert!(n_threads > 0, "n_threads must be > 0");

    let r = htslib::hts_set_threads(hts_file, n_threads as i32);
    if r != 0 {
        Err(Error::SetThreads)
    } else {
        Ok(())
    }
}

impl Reader {
    /// Create a new reader from a given path.
    pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self> {
        match path.as_ref().to_str() {
            Some(p) if !path.as_ref().exists() => Err(Error::FileNotFound { path: p.into() }),
            Some(p) => Self::new(p.as_bytes()),
            _ => Err(Error::NonUnicodePath),
        }
    }

    /// Create a new reader from a given URL.
    pub fn from_url(url: &Url) -> Result<Self> {
        Self::new(url.as_str().as_bytes())
    }

    /// Create a new reader from standard input.
    pub fn from_stdin() -> Result<Self> {
        Self::new(b"-")
    }

    fn new(path: &[u8]) -> Result<Self> {
        let htsfile = bcf_open(path, b"r")?;
        let header = unsafe { htslib::bcf_hdr_read(htsfile) };
        Ok(Reader {
            inner: htsfile,
            header: Rc::new(HeaderView::new(header)),
        })
    }
}

impl Read for Reader {
    fn read(&mut self, record: &mut record::Record) -> Option<Result<()>> {
        match unsafe { htslib::bcf_read(self.inner, self.header.inner, record.inner) } {
            0 => {
                unsafe {
                    // Always unpack record.
                    htslib::bcf_unpack(record.inner_mut(), htslib::BCF_UN_ALL as i32);
                }
                record.set_header(Rc::clone(&self.header));
                Some(Ok(()))
            }
            -1 => None,
            _ => Some(Err(Error::BcfInvalidRecord)),
        }
    }

    fn records(&mut self) -> Records<'_, Self> {
        Records { reader: self }
    }

    fn set_threads(&mut self, n_threads: usize) -> Result<()> {
        unsafe { set_threads(self.inner, n_threads) }
    }

    fn header(&self) -> &HeaderView {
        &self.header
    }

    /// Return empty record.  Can be reused multiple times.
    fn empty_record(&self) -> Record {
        self.header.empty_record()
    }
}

impl Drop for Reader {
    fn drop(&mut self) {
        unsafe {
            htslib::hts_close(self.inner);
        }
    }
}

/// An indexed VCF/BCF reader.
#[derive(Debug)]
pub struct IndexedReader {
    /// The synced VCF/BCF reader to use internally.
    inner: *mut htslib::bcf_srs_t,
    /// The header.
    header: Rc<HeaderView>,

    /// The position of the previous fetch, if any.
    current_region: Option<(u32, u64, Option<u64>)>,
}

unsafe impl Send for IndexedReader {}

impl IndexedReader {
    /// Create a new `IndexedReader` from path.
    ///
    /// # Arguments
    ///
    /// * `path` - the path to open.
    pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref();
        match path.to_str() {
            Some(p) if path.exists() => {
                Self::new(&ffi::CString::new(p).map_err(|_| Error::NonUnicodePath)?)
            }
            Some(p) => Err(Error::FileNotFound { path: p.into() }),
            None => Err(Error::NonUnicodePath),
        }
    }

    /// Create a new `IndexedReader` from an URL.
    pub fn from_url(url: &Url) -> Result<Self> {
        Self::new(&ffi::CString::new(url.as_str()).unwrap())
    }

    /// Create a new `IndexedReader`.
    ///
    /// # Arguments
    ///
    /// * `path` - the path. Use "-" for stdin.
    fn new(path: &ffi::CStr) -> Result<Self> {
        // Create reader and require existence of index file.
        let ser_reader = unsafe { htslib::bcf_sr_init() };
        unsafe {
            htslib::bcf_sr_set_opt(ser_reader, 0);
        } // 0: BCF_SR_REQUIRE_IDX
          // Attach a file with the path from the arguments.
        if unsafe { htslib::bcf_sr_add_reader(ser_reader, path.as_ptr()) } >= 0 {
            let header = Rc::new(HeaderView::new(unsafe {
                htslib::bcf_hdr_dup((*(*ser_reader).readers.offset(0)).header)
            }));
            Ok(IndexedReader {
                inner: ser_reader,
                header,
                current_region: None,
            })
        } else {
            Err(Error::BcfOpen {
                target: path.to_str().unwrap().to_owned(),
            })
        }
    }

    /// Jump to the given region.
    ///
    /// # Arguments
    ///
    /// * `rid` - numeric ID of the reference to jump to; use `HeaderView::name2rid` for resolving
    ///           contig name to ID.
    /// * `start` - `0`-based **inclusive** start coordinate of region on reference.
    /// * `end` - Optional `0`-based **inclusive** end coordinate of region on reference. If `None`
    /// is given, records are fetched from `start` until the end of the contig.
    ///
    /// # Note
    /// The entire contig can be fetched by setting `start` to `0` and `end` to `None`.
    pub fn fetch(&mut self, rid: u32, start: u64, end: Option<u64>) -> Result<()> {
        let contig = self.header.rid2name(rid)?;
        let contig = ffi::CString::new(contig).unwrap();
        if unsafe { htslib::bcf_sr_seek(self.inner, contig.as_ptr(), start as i64) } != 0 {
            Err(Error::GenomicSeek {
                contig: contig.to_str().unwrap().to_owned(),
                start,
            })
        } else {
            self.current_region = Some((rid, start, end));
            Ok(())
        }
    }
}

impl Read for IndexedReader {
    fn read(&mut self, record: &mut record::Record) -> Option<Result<()>> {
        match unsafe { htslib::bcf_sr_next_line(self.inner) } {
            0 => {
                if unsafe { (*self.inner).errnum } != 0 {
                    Some(Err(Error::BcfInvalidRecord))
                } else {
                    None
                }
            }
            i => {
                assert!(i > 0, "Must not be negative");
                // Note that the sync BCF reader has a different interface than the others
                // as it keeps its own buffer already for each record.  An alternative here
                // would be to replace the `inner` value by an enum that can be a pointer
                // into a synced reader or an owning popinter to an allocated record.
                unsafe {
                    htslib::bcf_copy(
                        record.inner,
                        *(*(*self.inner).readers.offset(0)).buffer.offset(0),
                    );
                }

                unsafe {
                    // Always unpack record.
                    htslib::bcf_unpack(record.inner_mut(), htslib::BCF_UN_ALL as i32);
                }

                record.set_header(Rc::clone(&self.header));

                match self.current_region {
                    Some((rid, _start, end)) => {
                        let endpos = match end {
                            Some(e) => e,
                            None => u64::MAX,
                        };
                        if Some(rid) == record.rid() && record.pos() as u64 <= endpos {
                            Some(Ok(()))
                        } else {
                            None
                        }
                    }
                    None => Some(Ok(())),
                }
            }
        }
    }

    fn records(&mut self) -> Records<'_, Self> {
        Records { reader: self }
    }

    fn set_threads(&mut self, n_threads: usize) -> Result<()> {
        assert!(n_threads > 0, "n_threads must be > 0");

        let r = unsafe { htslib::bcf_sr_set_threads(self.inner, n_threads as i32) };
        if r != 0 {
            Err(Error::SetThreads)
        } else {
            Ok(())
        }
    }

    fn header(&self) -> &HeaderView {
        &self.header
    }

    fn empty_record(&self) -> Record {
        Record::new(Rc::clone(&self.header))
    }
}

impl Drop for IndexedReader {
    fn drop(&mut self) {
        unsafe { htslib::bcf_sr_destroy(self.inner) };
    }
}

/// This module contains the `SyncedReader` class and related code.
pub mod synced {

    use super::*;

    /// This module contains bitmask constants for `SyncedReader`.
    pub mod pairing {
        /// Allow different alleles, as long as they all are SNPs.
        pub const SNPS: u32 = crate::htslib::BCF_SR_PAIR_SNPS;
        /// The same as above, but with indels.
        pub const INDELS: u32 = crate::htslib::BCF_SR_PAIR_INDELS;
        /// Any combination of alleles can be returned by `bcf_sr_next_line()`.
        pub const ANY: u32 = crate::htslib::BCF_SR_PAIR_ANY;
        /// At least some of multiallelic ALTs must match.  Implied by all the others with the exception of `EXACT`.
        pub const SOME: u32 = crate::htslib::BCF_SR_PAIR_SOME;
        /// Allow REF-only records with SNPs.
        pub const SNP_REF: u32 = crate::htslib::BCF_SR_PAIR_SNP_REF;
        /// Allow REF-only records with indels.
        pub const INDEL_REF: u32 = crate::htslib::BCF_SR_PAIR_INDEL_REF;
        /// Require the exact same set of alleles in all files.
        pub const EXACT: u32 = crate::htslib::BCF_SR_PAIR_EXACT;
        /// `SNPS | INDELS`.
        pub const BOTH: u32 = crate::htslib::BCF_SR_PAIR_BOTH;
        /// `SNPS | INDELS | SNP_REF | INDEL_REF`.
        pub const BOTH_REF: u32 = crate::htslib::BCF_SR_PAIR_BOTH_REF;
    }

    /// A wrapper for `bcf_srs_t`; allows joint traversal of multiple VCF and/or BCF files.
    #[derive(Debug)]
    pub struct SyncedReader {
        /// Internal handle for the synced reader.
        inner: *mut crate::htslib::bcf_srs_t,

        /// RC's of `HeaderView`s of the readers.
        headers: Vec<Rc<HeaderView>>,

        /// The position of the previous fetch, if any.
        current_region: Option<(u32, u64, u64)>,
    }

    // TODO: add interface for setting threads, ensure that the pool is freed properly
    impl SyncedReader {
        pub fn new() -> Result<Self> {
            let inner = unsafe { crate::htslib::bcf_sr_init() };
            if inner.is_null() {
                return Err(Error::BcfAllocationError);
            }

            Ok(SyncedReader {
                inner,
                headers: Vec::new(),
                current_region: None,
            })
        }

        /// Enable or disable requiring of index
        pub fn set_require_index(&mut self, do_require: bool) {
            unsafe {
                (*self.inner).require_index = if do_require { 1 } else { 0 };
            }
        }

        /// Set the given bitmask of values from `sr_pairing` module.
        pub fn set_pairing(&mut self, bitmask: u32) {
            unsafe {
                // TODO: 1 actually is BCF_SR_PAIR_LOGIC but is not available here?
                crate::htslib::bcf_sr_set_opt(self.inner, 1, bitmask);
            }
        }

        /// Add new reader with the path to the file.
        pub fn add_reader<P: AsRef<Path>>(&mut self, path: P) -> Result<()> {
            match path.as_ref().to_str() {
                Some(p) if path.as_ref().exists() => {
                    let p_cstring = ffi::CString::new(p).unwrap();
                    let res =
                        unsafe { crate::htslib::bcf_sr_add_reader(self.inner, p_cstring.as_ptr()) };

                    if res == 0 {
                        return Err(Error::BcfOpen {
                            target: p.to_owned(),
                        });
                    }

                    let i = (self.reader_count() - 1) as isize;
                    let header = Rc::new(HeaderView::new(unsafe {
                        crate::htslib::bcf_hdr_dup((*(*self.inner).readers.offset(i)).header)
                    }));
                    self.headers.push(header);
                    Ok(())
                }
                _ => Err(Error::NonUnicodePath),
            }
        }

        /// Remove reader with the given index.
        pub fn remove_reader(&mut self, idx: u32) {
            if idx >= self.reader_count() {
                panic!("Invalid reader!");
            } else {
                unsafe {
                    crate::htslib::bcf_sr_remove_reader(self.inner, idx as i32);
                }
                self.headers.remove(idx as usize);
            }
        }

        /// Return number of open files/readers.
        pub fn reader_count(&self) -> u32 {
            unsafe { (*self.inner).nreaders as u32 }
        }

        /// Read next line and return number of readers that have the given line (0 if end of all files is reached).
        pub fn read_next(&mut self) -> Result<u32> {
            let num = unsafe { crate::htslib::bcf_sr_next_line(self.inner) as u32 };

            if num == 0 {
                if unsafe { (*self.inner).errnum } != 0 {
                    return Err(Error::BcfInvalidRecord);
                }
                Ok(0)
            } else {
                assert!(num > 0, "num returned by htslib must not be negative");
                match self.current_region {
                    Some((rid, _start, end)) => {
                        for idx in 0..self.reader_count() {
                            if !self.has_line(idx) {
                                continue;
                            }
                            unsafe {
                                let record = *(*(*self.inner).readers.offset(idx as isize))
                                    .buffer
                                    .offset(0);
                                if (*record).rid != (rid as i32) || (*record).pos >= (end as i64) {
                                    return Ok(0);
                                }
                            }
                        }
                        Ok(num)
                    }
                    None => Ok(num),
                }
            }
        }

        /// Return whether the given reader has the line.
        pub fn has_line(&self, idx: u32) -> bool {
            if idx >= self.reader_count() {
                panic!("Invalid reader!");
            } else {
                unsafe { (*(*self.inner).has_line.offset(idx as isize)) != 0 }
            }
        }

        /// Return record from the given reader, if any.
        pub fn record(&self, idx: u32) -> Option<Record> {
            if self.has_line(idx) {
                let record = Record::new(self.headers[idx as usize].clone());
                unsafe {
                    crate::htslib::bcf_copy(
                        record.inner,
                        *(*(*self.inner).readers.offset(idx as isize))
                            .buffer
                            .offset(0),
                    );
                }
                Some(record)
            } else {
                None
            }
        }

        /// Return header from the given reader.
        pub fn header(&self, idx: u32) -> &HeaderView {
            // TODO: is the mutability here correct?
            if idx >= self.reader_count() {
                panic!("Invalid reader!");
            } else {
                &self.headers[idx as usize]
            }
        }

        /// Jump to the given region.
        ///
        /// # Arguments
        ///
        /// * `rid` - numeric ID of the reference to jump to; use `HeaderView::name2rid` for resolving
        ///           contig name to ID.
        /// * `start` - `0`-based start coordinate of region on reference.
        /// * `end` - `0`-based end coordinate of region on reference.
        pub fn fetch(&mut self, rid: u32, start: u64, end: u64) -> Result<()> {
            let contig = {
                let contig = self.header(0).rid2name(rid).unwrap(); //.clone();
                ffi::CString::new(contig).unwrap()
            };
            if unsafe { htslib::bcf_sr_seek(self.inner, contig.as_ptr(), start as i64) } != 0 {
                Err(Error::GenomicSeek {
                    contig: contig.to_str().unwrap().to_owned(),
                    start,
                })
            } else {
                self.current_region = Some((rid, start, end));
                Ok(())
            }
        }
    }

    impl Drop for SyncedReader {
        fn drop(&mut self) {
            unsafe { crate::htslib::bcf_sr_destroy(self.inner) };
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Format {
    Vcf,
    Bcf,
}

/// A VCF/BCF writer.
#[derive(Debug)]
pub struct Writer {
    inner: *mut htslib::htsFile,
    header: Rc<HeaderView>,
    subset: Option<SampleSubset>,
}

unsafe impl Send for Writer {}

impl Writer {
    /// Create a new writer that writes to the given path.
    ///
    /// # Arguments
    ///
    /// * `path` - the path
    /// * `header` - header definition to use
    /// * `uncompressed` - disable compression
    /// * `vcf` - write VCF instead of BCF
    pub fn from_path<P: AsRef<Path>>(
        path: P,
        header: &Header,
        uncompressed: bool,
        format: Format,
    ) -> Result<Self> {
        if let Some(p) = path.as_ref().to_str() {
            Ok(Self::new(p.as_bytes(), header, uncompressed, format)?)
        } else {
            Err(Error::NonUnicodePath)
        }
    }

    /// Create a new writer from a URL.
    ///
    /// # Arguments
    ///
    /// * `url` - the URL
    /// * `header` - header definition to use
    /// * `uncompressed` - disable compression
    /// * `vcf` - write VCF instead of BCF
    pub fn from_url(
        url: &Url,
        header: &Header,
        uncompressed: bool,
        format: Format,
    ) -> Result<Self> {
        Self::new(url.as_str().as_bytes(), header, uncompressed, format)
    }

    /// Create a new writer to stdout.
    ///
    /// # Arguments
    ///
    /// * `header` - header definition to use
    /// * `uncompressed` - disable compression
    /// * `vcf` - write VCF instead of BCF
    pub fn from_stdout(header: &Header, uncompressed: bool, format: Format) -> Result<Self> {
        Self::new(b"-", header, uncompressed, format)
    }

    fn new(path: &[u8], header: &Header, uncompressed: bool, format: Format) -> Result<Self> {
        let mode: &[u8] = match (uncompressed, format) {
            (true, Format::Vcf) => b"w",
            (false, Format::Vcf) => b"wz",
            (true, Format::Bcf) => b"wbu",
            (false, Format::Bcf) => b"wb",
        };

        let htsfile = bcf_open(path, mode)?;
        unsafe { htslib::bcf_hdr_write(htsfile, header.inner) };
        Ok(Writer {
            inner: htsfile,
            header: Rc::new(HeaderView::new(unsafe {
                htslib::bcf_hdr_dup(header.inner)
            })),
            subset: header.subset.clone(),
        })
    }

    /// Obtain reference to the lightweight `HeaderView` of the BCF header.
    pub fn header(&self) -> &HeaderView {
        &self.header
    }

    /// Create empty record for writing to this writer.
    ///
    /// This record can then be reused multiple times.
    pub fn empty_record(&self) -> Record {
        record::Record::new(Rc::clone(&self.header))
    }

    /// Translate record to header of this writer.
    ///
    /// # Arguments
    ///
    /// - `record` - The `Record` to translate.
    pub fn translate(&mut self, record: &mut record::Record) {
        unsafe {
            htslib::bcf_translate(self.header.inner, record.header().inner, record.inner);
        }
        record.set_header(Rc::clone(&self.header));
    }

    /// Subset samples of record to match header of this writer.
    ///
    /// # Arguments
    ///
    /// - `record` - The `Record` to modify.
    pub fn subset(&mut self, record: &mut record::Record) {
        if let Some(ref mut subset) = self.subset {
            unsafe {
                htslib::bcf_subset(
                    self.header.inner,
                    record.inner,
                    subset.len() as i32,
                    subset.as_mut_ptr(),
                );
            }
        }
    }

    /// Write `record` to the Writer.
    ///
    /// # Arguments
    ///
    /// - `record` - The `Record` to write.
    pub fn write(&mut self, record: &record::Record) -> Result<()> {
        if unsafe { htslib::bcf_write(self.inner, self.header.inner, record.inner) } == -1 {
            Err(Error::WriteRecord)
        } else {
            Ok(())
        }
    }

    /// Activate multi-threaded BCF write support in htslib. This should permit faster
    /// writing of large BCF files.
    ///
    /// # Arguments
    ///
    /// * `n_threads` - number of extra background writer threads to use, must be `> 0`.
    pub fn set_threads(&mut self, n_threads: usize) -> Result<()> {
        unsafe { set_threads(self.inner, n_threads) }
    }
}

impl Drop for Writer {
    fn drop(&mut self) {
        unsafe {
            htslib::hts_close(self.inner);
        }
    }
}

#[derive(Debug)]
pub struct Records<'a, R: Read> {
    reader: &'a mut R,
}

impl<'a, R: Read> Iterator for Records<'a, R> {
    type Item = Result<record::Record>;

    fn next(&mut self) -> Option<Result<record::Record>> {
        let mut record = self.reader.empty_record();
        match self.reader.read(&mut record) {
            Some(Err(e)) => Some(Err(e)),
            Some(Ok(_)) => Some(Ok(record)),
            None => None,
        }
    }
}

/// Wrapper for opening a BCF file.
fn bcf_open(target: &[u8], mode: &[u8]) -> Result<*mut htslib::htsFile> {
    let p = ffi::CString::new(target).unwrap();
    let c_str = ffi::CString::new(mode).unwrap();
    let ret = unsafe { htslib::hts_open(p.as_ptr(), c_str.as_ptr()) };

    if ret.is_null() {
        return Err(Error::BcfOpen {
            target: str::from_utf8(target).unwrap().to_owned(),
        });
    }

    unsafe {
        if !(mode.contains(&b'w')
            || (*ret).format.category == htslib::htsFormatCategory_variant_data)
        {
            return Err(Error::BcfOpen {
                target: str::from_utf8(target).unwrap().to_owned(),
            });
        }
    }
    Ok(ret)
}

#[cfg(test)]
mod tests {
    use super::record::Buffer;
    use super::*;
    use crate::bcf::header::Id;
    use crate::bcf::record::GenotypeAllele;
    use crate::bcf::record::Numeric;
    use crate::bcf::Reader;
    use std::convert::TryFrom;
    use std::fs::File;
    use std::io::prelude::Read as IoRead;
    use std::path::Path;
    use std::str;

    fn _test_read<P: AsRef<Path>>(path: &P) {
        let mut bcf = Reader::from_path(path).expect("Error opening file.");
        assert_eq!(bcf.header.samples(), [b"NA12878.subsample-0.25-0"]);

        for (i, rec) in bcf.records().enumerate() {
            let record = rec.expect("Error reading record.");
            assert_eq!(record.sample_count(), 1);

            assert_eq!(record.rid().expect("Error reading rid."), 0);
            assert_eq!(record.pos(), 10021 + i as i64);
            assert!((record.qual() - 0f32).abs() < std::f32::EPSILON);
            let mut buffer = Buffer::new();
            assert!(
                (record
                    .info_shared_buffer(b"MQ0F", &mut buffer)
                    .float()
                    .expect("Error reading info.")
                    .expect("Missing tag")[0]
                    - 1.0)
                    .abs()
                    < std::f32::EPSILON
            );
            if i == 59 {
                assert!(
                    (record
                        .info_shared_buffer(b"SGB", &mut buffer)
                        .float()
                        .expect("Error reading info.")
                        .expect("Missing tag")[0]
                        - -0.379885)
                        .abs()
                        < std::f32::EPSILON
                );
            }
            // the artificial "not observed" allele is present in each record.
            assert_eq!(record.alleles().iter().last().unwrap(), b"<X>");

            let fmt = record.format(b"PL");
            let pl = fmt.integer().expect("Error reading format.");
            assert_eq!(pl.len(), 1);
            if i == 59 {
                assert_eq!(pl[0].len(), 6);
            } else {
                assert_eq!(pl[0].len(), 3);
            }
        }
    }

    #[test]
    fn test_read() {
        _test_read(&"test/test.bcf");
    }

    #[test]
    fn test_reader_set_threads() {
        let path = &"test/test.bcf";
        let mut bcf = Reader::from_path(path).expect("Error opening file.");
        bcf.set_threads(2).unwrap();
    }

    #[test]
    fn test_writer_set_threads() {
        let path = &"test/test.bcf";
        let tmp = tempfile::Builder::new()
            .prefix("rust-htslib")
            .tempdir()
            .expect("Cannot create temp dir");
        let bcfpath = tmp.path().join("test.bcf");
        let bcf = Reader::from_path(path).expect("Error opening file.");
        let header = Header::from_template_subset(&bcf.header, &[b"NA12878.subsample-0.25-0"])
            .expect("Error subsetting samples.");
        let mut writer =
            Writer::from_path(&bcfpath, &header, false, Format::Bcf).expect("Error opening file.");
        writer.set_threads(2).unwrap();
    }

    #[test]
    fn test_fetch() {
        let mut bcf = IndexedReader::from_path(&"test/test.bcf").expect("Error opening file.");
        bcf.set_threads(2).unwrap();
        let rid = bcf
            .header()
            .name2rid(b"1")
            .expect("Translating from contig '1' to ID failed.");
        bcf.fetch(rid, 10_033, Some(10_060))
            .expect("Fetching failed");
        assert_eq!(bcf.records().count(), 28);
    }

    #[test]
    fn test_fetch_all() {
        let mut bcf = IndexedReader::from_path(&"test/test.bcf").expect("Error opening file.");
        bcf.set_threads(2).unwrap();
        let rid = bcf
            .header()
            .name2rid(b"1")
            .expect("Translating from contig '1' to ID failed.");
        bcf.fetch(rid, 0, None).expect("Fetching failed");
        assert_eq!(bcf.records().count(), 62);
    }

    #[test]
    fn test_fetch_open_ended() {
        let mut bcf = IndexedReader::from_path(&"test/test.bcf").expect("Error opening file.");
        bcf.set_threads(2).unwrap();
        let rid = bcf
            .header()
            .name2rid(b"1")
            .expect("Translating from contig '1' to ID failed.");
        bcf.fetch(rid, 10077, None).expect("Fetching failed");
        assert_eq!(bcf.records().count(), 6);
    }

    #[test]
    fn test_fetch_start_greater_than_last_vcf_pos() {
        let mut bcf = IndexedReader::from_path(&"test/test.bcf").expect("Error opening file.");
        bcf.set_threads(2).unwrap();
        let rid = bcf
            .header()
            .name2rid(b"1")
            .expect("Translating from contig '1' to ID failed.");
        bcf.fetch(rid, 20077, None).expect("Fetching failed");
        assert_eq!(bcf.records().count(), 0);
    }

    #[test]
    fn test_write() {
        let mut bcf = Reader::from_path(&"test/test_multi.bcf").expect("Error opening file.");
        let tmp = tempfile::Builder::new()
            .prefix("rust-htslib")
            .tempdir()
            .expect("Cannot create temp dir");
        let bcfpath = tmp.path().join("test.bcf");
        println!("{:?}", bcfpath);
        {
            let header = Header::from_template_subset(&bcf.header, &[b"NA12878.subsample-0.25-0"])
                .expect("Error subsetting samples.");
            let mut writer = Writer::from_path(&bcfpath, &header, false, Format::Bcf)
                .expect("Error opening file.");
            for rec in bcf.records() {
                let mut record = rec.expect("Error reading record.");
                writer.translate(&mut record);
                writer.subset(&mut record);
                record.trim_alleles().expect("Error trimming alleles.");
                writer.write(&record).expect("Error writing record");
            }
        }
        {
            _test_read(&bcfpath);
        }
        tmp.close().expect("Failed to delete temp dir");
    }

    #[test]
    fn test_strings() {
        let mut vcf = Reader::from_path(&"test/test_string.vcf").expect("Error opening file.");
        let fs1 = [
            &b"LongString1"[..],
            &b"LongString2"[..],
            &b"."[..],
            &b"LongString4"[..],
            &b"evenlength"[..],
            &b"ss6"[..],
        ];
        let mut buffer = Buffer::new();
        for (i, rec) in vcf.records().enumerate() {
            println!("record {}", i);
            let record = rec.expect("Error reading record.");
            assert_eq!(
                record
                    .info_shared_buffer(b"S1", &mut buffer)
                    .string()
                    .expect("Error reading string.")
                    .expect("Missing tag")[0],
                format!("string{}", i + 1).as_bytes()
            );
            let fs1_str_vec = record
                .format_shared_buffer(b"FS1", &mut buffer)
                .string()
                .expect("Error reading string.");
            assert_eq!(fs1_str_vec.len(), 2);
            println!("{}", String::from_utf8_lossy(fs1_str_vec[0]));
            assert_eq!(
                record
                    .format(b"FS1")
                    .string()
                    .expect("Error reading string.")[0],
                fs1[i]
            );
        }
    }

    #[test]
    fn test_missing() {
        let mut vcf = Reader::from_path(&"test/test_missing.vcf").expect("Error opening file.");
        let fn4 = [
            &[
                i32::missing(),
                i32::missing(),
                i32::missing(),
                i32::missing(),
            ][..],
            &[i32::missing()][..],
        ];
        let f1 = [false, true];
        let mut buffer = Buffer::new();
        for (i, rec) in vcf.records().enumerate() {
            let record = rec.expect("Error reading record.");
            assert_eq!(
                record
                    .info_shared_buffer(b"F1", &mut buffer)
                    .float()
                    .expect("Error reading float.")
                    .expect("Missing tag")[0]
                    .is_nan(),
                f1[i]
            );
            assert_eq!(
                record
                    .format(b"FN4")
                    .integer()
                    .expect("Error reading integer.")[1],
                fn4[i]
            );
            assert!(
                record.format(b"FF4").float().expect("Error reading float.")[1]
                    .iter()
                    .all(|&v| v.is_missing())
            );
        }
    }

    #[test]
    fn test_genotypes() {
        let mut vcf = Reader::from_path(&"test/test_string.vcf").expect("Error opening file.");
        let expected = ["./1", "1|1", "0/1", "0|1", "1|.", "1/1"];
        for (rec, exp_gt) in vcf.records().zip(expected.iter()) {
            let rec = rec.expect("Error reading record.");
            let genotypes = rec.genotypes().expect("Error reading genotypes");
            assert_eq!(&format!("{}", genotypes.get(0)), exp_gt);
        }
    }

    #[test]
    fn test_header_ids() {
        let vcf = Reader::from_path(&"test/test_string.vcf").expect("Error opening file.");
        let header = &vcf.header();
        use crate::bcf::header::Id;

        assert_eq!(header.id_to_name(Id(4)), b"GT");
        assert_eq!(header.name_to_id(b"GT").unwrap(), Id(4));
        assert!(header.name_to_id(b"XX").is_err());
    }

    #[test]
    fn test_header_samples() {
        let vcf = Reader::from_path(&"test/test_string.vcf").expect("Error opening file.");
        let header = &vcf.header();

        assert_eq!(header.id_to_sample(Id(0)), b"one");
        assert_eq!(header.id_to_sample(Id(1)), b"two");
        assert_eq!(header.sample_to_id(b"one").unwrap(), Id(0));
        assert_eq!(header.sample_to_id(b"two").unwrap(), Id(1));
        assert!(header.sample_to_id(b"three").is_err());
    }

    #[test]
    fn test_header_contigs() {
        let vcf = Reader::from_path(&"test/test_multi.bcf").expect("Error opening file.");
        let header = &vcf.header();

        assert_eq!(header.contig_count(), 86);

        // test existing contig names and IDs
        assert_eq!(header.rid2name(0).unwrap(), b"1");
        assert_eq!(header.name2rid(b"1").unwrap(), 0);

        assert_eq!(header.rid2name(85).unwrap(), b"hs37d5");
        assert_eq!(header.name2rid(b"hs37d5").unwrap(), 85);

        // test nonexistent contig names and IDs
        assert!(header.name2rid(b"nonexistent_contig").is_err());
        assert!(header.rid2name(100).is_err());
    }

    #[test]
    fn test_header_records() {
        let vcf = Reader::from_path(&"test/test_string.vcf").expect("Error opening file.");
        let records = vcf.header().header_records();
        assert_eq!(records.len(), 10);

        match records[1] {
            HeaderRecord::Filter {
                ref key,
                ref values,
            } => {
                assert_eq!(key, "FILTER");
                assert_eq!(values["ID"], "PASS");
            }
            _ => {
                panic!("Invalid HeaderRecord");
            }
        }
    }

    #[test]
    fn test_header_info_types() {
        let vcf = Reader::from_path(&"test/test.bcf").unwrap();
        let header = vcf.header();
        let truth = vec![
            (
                // INFO=<ID=INDEL,Number=0,Type=Flag>
                "INDEL",
                header::TagType::Flag,
                header::TagLength::Fixed(0),
            ),
            (
                // INFO=<ID=DP,Number=1,Type=Integer>
                "DP",
                header::TagType::Integer,
                header::TagLength::Fixed(1),
            ),
            (
                // INFO=<ID=QS,Number=R,Type=Float>
                "QS",
                header::TagType::Float,
                header::TagLength::Alleles,
            ),
            (
                // INFO=<ID=I16,Number=16,Type=Float>
                "I16",
                header::TagType::Float,
                header::TagLength::Fixed(16),
            ),
        ];
        for (ref_name, ref_type, ref_length) in truth {
            let (tag_type, tag_length) = header.info_type(ref_name.as_bytes()).unwrap();
            assert_eq!(tag_type, ref_type);
            assert_eq!(tag_length, ref_length);
        }

        let vcf = Reader::from_path(&"test/test_svlen.vcf").unwrap();
        let header = vcf.header();
        let truth = vec![
            (
                // INFO=<ID=IMPRECISE,Number=0,Type=Flag>
                "IMPRECISE",
                header::TagType::Flag,
                header::TagLength::Fixed(0),
            ),
            (
                // INFO=<ID=SVTYPE,Number=1,Type=String>
                "SVTYPE",
                header::TagType::String,
                header::TagLength::Fixed(1),
            ),
            (
                // INFO=<ID=SVLEN,Number=.,Type=Integer>
                "SVLEN",
                header::TagType::Integer,
                header::TagLength::Variable,
            ),
            (
                // INFO<ID=CIGAR,Number=A,Type=String>
                "CIGAR",
                header::TagType::String,
                header::TagLength::AltAlleles,
            ),
        ];
        for (ref_name, ref_type, ref_length) in truth {
            let (tag_type, tag_length) = header.info_type(ref_name.as_bytes()).unwrap();
            assert_eq!(tag_type, ref_type);
            assert_eq!(tag_length, ref_length);
        }

        assert!(header.info_type(b"NOT_THERE").is_err());
    }

    #[test]
    fn test_remove_alleles() {
        let mut bcf = Reader::from_path(&"test/test_multi.bcf").unwrap();
        for res in bcf.records() {
            let mut record = res.unwrap();
            if record.pos() == 10080 {
                record.remove_alleles(&[false, false, true]).unwrap();
                assert_eq!(record.alleles(), [b"A", b"C"]);
            }
        }
    }

    // Helper function reading full file into string.
    fn read_all<P: AsRef<Path>>(path: P) -> String {
        let mut file = File::open(path.as_ref())
            .unwrap_or_else(|_| panic!("Unable to open the file: {:?}", path.as_ref()));
        let mut contents = String::new();
        file.read_to_string(&mut contents)
            .unwrap_or_else(|_| panic!("Unable to read the file: {:?}", path.as_ref()));
        contents
    }

    // Open `test_various.vcf`, add a record from scratch to it and write it out again.
    //
    // This exercises the full functionality of updating information in a `record::Record`.
    #[test]
    fn test_write_various() {
        // Open reader, then create writer.
        let tmp = tempfile::Builder::new()
            .prefix("rust-htslib")
            .tempdir()
            .expect("Cannot create temp dir");
        let out_path = tmp.path().join("test_various.out.vcf");

        let vcf = Reader::from_path(&"test/test_various.vcf").expect("Error opening file.");
        // The writer goes into its own block so we can ensure that the file is closed and
        // all data is written below.
        {
            let mut writer = Writer::from_path(
                &out_path,
                &Header::from_template(&vcf.header()),
                true,
                Format::Vcf,
            )
            .expect("Error opening file.");

            // Setup empty record, filled below.
            let mut record = writer.empty_record();

            record.set_rid(Some(0));
            assert_eq!(record.rid().unwrap(), 0);

            record.set_pos(12);
            assert_eq!(record.pos(), 12);

            assert_eq!(str::from_utf8(record.id().as_ref()).unwrap(), ".");
            record.set_id(b"to_be_cleared").unwrap();
            assert_eq!(
                str::from_utf8(record.id().as_ref()).unwrap(),
                "to_be_cleared"
            );
            record.clear_id().unwrap();
            assert_eq!(str::from_utf8(record.id().as_ref()).unwrap(), ".");
            record.set_id(b"first_id").unwrap();
            record.push_id(b"second_id").unwrap();
            record.push_id(b"first_id").unwrap();

            assert!(record.filters().next().is_none());
            record.set_filters(&["q10".as_bytes()]).unwrap();
            record.push_filter("s50".as_bytes()).unwrap();
            record.remove_filter("q10".as_bytes(), true).unwrap();
            record.push_filter("q10".as_bytes()).unwrap();

            record.set_alleles(&[b"C", b"T", b"G"]).unwrap();

            record.set_qual(10.0);

            record.push_info_integer(b"N1", &[32]).unwrap();
            record.push_info_float(b"F1", &[33.0]).unwrap();
            record.push_info_string(b"S1", &[b"fourtytwo"]).unwrap();
            record.push_info_flag(b"X1").unwrap();

            record
                .push_genotypes(&[
                    GenotypeAllele::Unphased(0),
                    GenotypeAllele::Unphased(1),
                    GenotypeAllele::Unphased(1),
                    GenotypeAllele::Phased(1),
                ])
                .unwrap();

            record
                .push_format_string(b"FS1", &[&b"yes"[..], &b"no"[..]])
                .unwrap();
            record.push_format_integer(b"FF1", &[43, 11]).unwrap();
            record.push_format_float(b"FN1", &[42.0, 10.0]).unwrap();
            record
                .push_format_char(b"CH1", &[b"A"[0], b"B"[0]])
                .unwrap();

            // Finally, write out the record.
            writer.write(&record).unwrap();
        }

        // Now, compare expected and real output.
        let expected = read_all("test/test_various.out.vcf");
        let actual = read_all(&out_path);
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_remove_headers() {
        let vcf = Reader::from_path(&"test/test_headers.vcf").expect("Error opening file.");
        let tmp = tempfile::Builder::new()
            .prefix("rust-htslib")
            .tempdir()
            .expect("Cannot create temp dir");
        let vcfpath = tmp.path().join("test.vcf");
        let mut header = Header::from_template(&vcf.header);
        header
            .remove_contig(b"contig2")
            .remove_info(b"INFO2")
            .remove_format(b"FORMAT2")
            .remove_filter(b"FILTER2")
            .remove_structured(b"Foo2")
            .remove_generic(b"Bar2");
        {
            let mut _writer = Writer::from_path(&vcfpath, &header, true, Format::Vcf)
                .expect("Error opening output file.");
            // Note that we don't need to write anything, we are just looking at the header.
        }

        let expected = read_all("test/test_headers.out.vcf");
        let actual = read_all(&vcfpath);
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_synced_reader() {
        let mut reader = synced::SyncedReader::new().unwrap();
        reader.set_require_index(true);
        reader.set_pairing(synced::pairing::SNPS);

        assert_eq!(reader.reader_count(), 0);
        reader.add_reader(&"test/test_left.vcf.gz").unwrap();
        reader.add_reader(&"test/test_right.vcf.gz").unwrap();
        assert_eq!(reader.reader_count(), 2);

        let res1 = reader.read_next();
        assert_eq!(res1.unwrap(), 2);
        assert!(reader.has_line(0));
        assert!(reader.has_line(1));

        let res2 = reader.read_next();
        assert_eq!(res2.unwrap(), 1);
        assert!(reader.has_line(0));
        assert!(!reader.has_line(1));

        let res3 = reader.read_next();
        assert_eq!(res3.unwrap(), 1);
        assert!(!reader.has_line(0));
        assert!(reader.has_line(1));

        let res4 = reader.read_next();
        assert_eq!(res4.unwrap(), 0);
    }

    #[test]
    fn test_synced_reader_fetch() {
        let mut reader = synced::SyncedReader::new().unwrap();
        reader.set_require_index(true);
        reader.set_pairing(synced::pairing::SNPS);

        assert_eq!(reader.reader_count(), 0);
        reader.add_reader(&"test/test_left.vcf.gz").unwrap();
        reader.add_reader(&"test/test_right.vcf.gz").unwrap();
        assert_eq!(reader.reader_count(), 2);

        reader.fetch(0, 0, 1000).unwrap();
        let res1 = reader.read_next();
        assert_eq!(res1.unwrap(), 2);
        assert!(reader.has_line(0));
        assert!(reader.has_line(1));

        let res2 = reader.read_next();
        assert_eq!(res2.unwrap(), 1);
        assert!(reader.has_line(0));
        assert!(!reader.has_line(1));

        let res3 = reader.read_next();
        assert_eq!(res3.unwrap(), 1);
        assert!(!reader.has_line(0));
        assert!(reader.has_line(1));

        let res4 = reader.read_next();
        assert_eq!(res4.unwrap(), 0);
    }

    #[test]
    fn test_svlen() {
        let mut reader = Reader::from_path("test/test_svlen.vcf").unwrap();

        let mut record = reader.empty_record();
        reader.read(&mut record).unwrap().unwrap();

        assert_eq!(
            *record.info(b"SVLEN").integer().unwrap().unwrap(),
            &[-127][..]
        );
    }

    #[test]
    fn test_fails_on_bam() {
        let reader = Reader::from_path("test/test.bam");
        assert!(reader.is_err());
    }

    #[test]
    fn test_fails_on_non_existiant() {
        let reader = Reader::from_path("test/no_such_file");
        assert!(reader.is_err());
    }

    #[test]
    fn test_multi_string_info_tag() {
        let mut reader = Reader::from_path("test/test-info-multi-string.vcf").unwrap();
        let mut rec = reader.empty_record();
        let _ = reader.read(&mut rec);

        assert_eq!(
            rec.info_shared_buffer(b"ANN", Buffer::new())
                .string()
                .unwrap()
                .unwrap()
                .len(),
            14
        );
    }

    #[test]
    fn test_multi_string_info_tag_number_a() {
        let mut reader = Reader::from_path("test/test-info-multi-string-number=A.vcf").unwrap();
        let mut rec = reader.empty_record();
        let _ = reader.read(&mut rec);

        assert_eq!(
            rec.info_shared_buffer(b"X", Buffer::new())
                .string()
                .unwrap()
                .unwrap()
                .len(),
            2
        );
    }

    #[test]
    fn test_genotype_allele_conversion() {
        let allele = GenotypeAllele::Unphased(1);
        let converted: i32 = allele.into();
        let expected = 4;
        assert_eq!(converted, expected);
        let reverse_conversion = GenotypeAllele::from(expected);
        assert_eq!(allele, reverse_conversion);
    }

    #[test]
    fn test_genotype_missing_allele_conversion() {
        let allele = GenotypeAllele::PhasedMissing;
        let converted: i32 = allele.into();
        let expected = 1;
        assert_eq!(converted, expected);
        let reverse_conversion = GenotypeAllele::from(expected);
        assert_eq!(allele, reverse_conversion);
    }

    #[test]
    fn test_alt_allele_dosage() {
        let path = &"test/test_string.vcf";
        let mut bcf = Reader::from_path(path).expect("Error opening file.");
        let _header = bcf.header();
        // FORMAT fields of first record of the vcf should look like:
        // GT:FS1:FN1	./1:LongString1:1	1/1:ss1:2
        let first_record = bcf.records().next().unwrap().expect("Fail to read record");
        let sample_count = usize::try_from(first_record.sample_count()).unwrap();
        assert_eq!(sample_count, 2);
        let mut n_ref = vec![0; sample_count];
        let mut n_alt = vec![0; sample_count];
        let mut n_missing = vec![0; sample_count];
        let gts = first_record.genotypes().expect("Error reading genotypes");
        for sample_index in 0..sample_count {
            // for each sample
            for gta in gts.get(sample_index).iter() {
                // for each allele
                match gta.index() {
                    Some(0) => n_ref[sample_index] += 1,  // reference allele
                    Some(_) => n_alt[sample_index] += 1,  // alt allele
                    None => n_missing[sample_index] += 1, // missing allele
                }
            }
        }
        assert_eq!(n_ref, [0, 0]);
        assert_eq!(n_alt, [1, 2]);
        assert_eq!(n_missing, [1, 0]);
    }

    #[test]
    fn test_obs_cornercase() {
        let mut reader = Reader::from_path("test/obs-cornercase.vcf").unwrap();
        let first_record = reader
            .records()
            .next()
            .unwrap()
            .expect("Fail to read record");

        assert_eq!(
            *first_record.info(b"EVENT").string().unwrap().unwrap(),
            [b"gridss33fb_1085"]
        );
        assert_eq!(
            *first_record.info(b"MATEID").string().unwrap().unwrap(),
            [b"gridss33fb_1085h"]
        );
    }

    #[test]
    fn test_trailing_omitted_format_fields() {
        let mut reader = Reader::from_path("test/test_trailing_omitted_format.vcf").unwrap();
        let first_record = reader
            .records()
            .next()
            .unwrap()
            .expect("Fail to read record");

        let expected: Vec<&[u8]> = Vec::new();
        assert_eq!(*first_record.format(b"STR").string().unwrap(), expected,);
        assert_eq!(
            *first_record.format(b"INT").integer().unwrap(),
            vec![&[i32::missing()]],
        );
        assert!(first_record.format(b"FLT").float().unwrap()[0][0].is_nan(),);
    }

    // #[test]
    // fn test_buffer_lifetime() {
    //     let mut reader = Reader::from_path("test/obs-cornercase.vcf").unwrap();
    //     let first_record = reader
    //         .records()
    //         .next()
    //         .unwrap()
    //         .expect("Fail to read record");

    //     fn get_value<'a, 'b>(record: &'a Record) -> &'b [u8] {
    //         // FIXME: this should not be possible, because the slice outlives the buffer.
    //         let buffer: BufferBacked<'b, _, _> = record.info(b"EVENT").string().unwrap().unwrap();
    //         let value: &'b [u8] = buffer[0];
    //         value
    //     }

    //     let buffered = first_record.info(b"EVENT").string().unwrap().unwrap();
    //     assert_eq!(get_value(&first_record), buffered[0]);
    // }
}