drprg 0.1.1

Drug resistance prediction with reference graphs
Documentation
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
use std::cmp::{max, min};
use std::collections::{HashMap, HashSet};
use std::convert::TryFrom;
use std::fs;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Seek, Write};
use std::path::{Path, PathBuf};

use anyhow::{anyhow, Context, Result};
use bstr::ByteSlice;
use chrono::Utc;
use clap::Parser;
use log::{debug, info, warn};
use noodles::core::Position;
use noodles::fasta::fai;
use noodles::fasta::record::{Definition, Sequence};
use noodles::fasta::repository::adapters::IndexedReader;
use noodles::fasta::repository::Adapter;
use noodles::gff::record::Strand;
use noodles::{fasta, gff};
use rayon::prelude::*;
use rust_htslib::bcf;
use rust_htslib::bcf::Read;
use thiserror::Error;

use drprg::{
    find_prg_index_in, index_fasta, index_vcf, revcomp, Bcftools, GffExt, MakePrg,
    Pandora,
};
use drprg::{MultipleSeqAligner, PathExt};

use crate::cli::check_path_exists;
use crate::config::Config;
use crate::expert::{ExpertRules, RuleExt};
use crate::panel::{Panel, PanelError, PanelExt, PanelRecord};
use crate::Runner;

static META: &str = "##";
const VERSION: &str = env!("CARGO_PKG_VERSION");
static DEFAULT_KMER_SIZE: u32 = 15;
static DEFAULT_WINDOW_SIZE: u32 = 11;
static DEFAULT_PADDING: u32 = 100;
static DEFAULT_MIN_MATCH_LEN: u32 = 5;
static DEFAULT_MAX_NESTING: u32 = 5;

lazy_static! {
    static ref CURRENT_DATE: String = format!("{}", Utc::now().format("%Y%m%d"));
}

#[derive(Parser, Debug, Default)]
pub struct Build {
    /// Path to pandora executable. Will try in src/ext or $PATH if not given
    #[clap(
        short = 'p',
        long = "pandora",
        value_parser,
        hide_short_help = true,
        value_name = "FILE"
    )]
    pandora_exec: Option<PathBuf>,
    /// Path to make_prg executable. Will try in src/ext or $PATH if not given
    #[clap(
        short = 'm',
        long = "makeprg",
        value_parser,
        hide_short_help = true,
        value_name = "FILE"
    )]
    makeprg_exec: Option<PathBuf>,
    /// Path to MAFFT executable. Will try in src/ext or $PATH if not given
    #[clap(
        short = 'M',
        long = "mafft",
        value_parser,
        hide_short_help = true,
        value_name = "FILE"
    )]
    mafft_exec: Option<PathBuf>,
    /// Path to bcftools executable. Will try in src/ext or $PATH if not given
    #[clap(
        short = 'B',
        long = "bcftools",
        value_parser,
        hide_short_help = true,
        value_name = "FILE"
    )]
    bcftools_exec: Option<PathBuf>,
    /// Annotation file that will be used to gather information about genes in catalogue
    #[clap(short = 'a', long = "gff", value_parser = check_path_exists, value_name = "FILE", help_heading="Input/Output")]
    gff_file: PathBuf,
    /// Panel/catalogue to build index for
    #[clap(short = 'i', long = "panel", value_parser = check_path_exists, value_name = "FILE", help_heading="Input/Output")]
    panel_file: PathBuf,
    /// Reference genome in FASTA format (must be indexed with samtools faidx)
    #[clap(short = 'f', long = "fasta", value_parser = check_path_exists, value_name = "FILE", help_heading="Input/Output")]
    reference_file: PathBuf,
    /// An indexed VCF to build the index PRG from. If not provided, then a prebuilt PRG must be
    /// given. See `--prebuilt-prg`
    #[clap(short = 'b', long = "vcf", value_parser = check_path_exists, value_name = "FILE", required_unless_present("prebuilt_prg"), help_heading="Input/Output")]
    input_vcf: Option<PathBuf>,
    /// Number of bases of padding to add to start and end of each gene
    #[clap(
        short = 'P',
        long = "padding",
        default_value_t = DEFAULT_PADDING,
        value_name = "INT"
    )]
    padding: u32,
    /// Directory to place output
    #[clap(
        short,
        long,
        default_value = ".",
        value_parser,
        value_name = "DIR",
        help_heading = "Input/Output"
    )]
    outdir: PathBuf,
    /// A prebuilt PRG to use.
    ///
    /// Only build the panel VCF and reference sequences - not the PRG. This directory MUST
    /// contain a PRG file named `dr.prg`, along, with a directory called `msas/` that contains an
    /// MSA fasta file for each gene `<gene>.fa`. There can optionally also be a pandora index file, but
    /// if not, the indexing will be performed by drprg. Note: the PRG is expected to contain the
    /// reference sequence for each gene according to the annotation and reference genome given
    /// (along with padding) and must be in the forward strand orientation.
    #[clap(short = 'd', long, value_parser = check_path_exists, value_name = "DIR", help_heading="Input/Output")]
    prebuilt_prg: Option<PathBuf>,
    /// "Expert rules" to be applied in addition to the catalogue.
    ///
    /// CSV file with blanket rules that describe resistance (or susceptibility). The columns are
    /// <variant type>,<gene>,<start>,<end>,<drug(s)>. See the docs for a detailed explanation.
    #[clap(short, long, value_parser = check_path_exists, value_name = "FILE", help_heading="Input/Output")]
    rules: Option<PathBuf>,
    /// Minimum number of consecutive characters which must be identical for a match in make_prg
    #[clap(
        short = 'l',
        long = "match-len",
        default_value_t = DEFAULT_MIN_MATCH_LEN,
        hide_short_help = true,
        value_name = "INT"
    )]
    match_len: u32,
    /// Maximum nesting level when constructing the reference graph with make_prg
    #[clap(
        short = 'N',
        long = "max-nesting",
        default_value_t = DEFAULT_MAX_NESTING,
        hide_short_help = true,
        value_name = "INT"
    )]
    max_nesting: u32,
    /// Kmer size to use for pandora
    #[clap(
        short = 'k',
        long,
        default_value_t = DEFAULT_KMER_SIZE,
        hide_short_help = true,
        value_name = "INT"
    )]
    pandora_k: u32,
    /// Window size to use for pandora
    #[clap(
        short = 'w',
        long,
        default_value_t = DEFAULT_WINDOW_SIZE,
        hide_short_help = true,
        value_name = "INT"
    )]
    pandora_w: u32,
    /// Don't index --fasta if an index doesn't exist
    #[clap(short = 'I', long = "no-fai")]
    dont_index_ref: bool,
    /// Don't index --vcf if an index doesn't exist
    #[clap(short = 'C', long = "no-csi")]
    dont_index_vcf: bool,
    /// Version to use for the index
    #[clap(long, default_value_t = CURRENT_DATE.to_string())]
    version: String,
}

impl Build {
    fn load_panel(&self) -> Result<Panel, anyhow::Error> {
        Panel::from_csv(&self.panel_file)
    }

    /// A utility function that checks if a VCF index exists and optionally indexes it
    pub fn check_vcf_index_exists(
        vcf_path: &Path,
        create_index: bool,
    ) -> Result<(), BuildError> {
        for ext in [".csi", ".tbi"] {
            let p = vcf_path.add_extension(ext.as_ref());
            if p.exists() {
                return Ok(());
            }
        }
        if create_index {
            info!("No index found for {:?}. Creating one...", vcf_path);
            index_vcf(vcf_path).map_err(|src| {
                BuildError::IndexError(format!(
                    "Failed to create an index file for {vcf_path:?} due to {src:?}",
                ))
            })
        } else {
            Err(BuildError::IndexError(format!(
                "No index exists for {vcf_path:?} and creation was not requested",
            )))
        }
    }

    /// A utility function that checks if a VCF index exists and optionally indexes it
    pub fn check_fasta_index_exists(
        fasta_path: &Path,
        create_index: bool,
    ) -> Result<(), BuildError> {
        let p = fasta_path.add_extension(".fai".as_ref());
        if p.exists() {
            Ok(())
        } else if create_index {
            info!("No index found for {:?}. Creating one...", fasta_path);
            match index_fasta(fasta_path) {
                Err(src) => Err(BuildError::IndexError(format!(
                    "Failed to create an index file for {fasta_path:?} due to {src:?}",
                ))),
                Ok(_) => Ok(()),
            }
        } else {
            Err(BuildError::IndexError(format!(
                "No index exists for {fasta_path:?} and creation was not requested",
            )))
        }
    }

    fn create_vcf_header(
        &self,
        annotations: &HashMap<String, gff::Record>,
    ) -> bcf::Header {
        let mut header = bcf::Header::new();
        header.push_record(format!("{META}source=drprgV{VERSION}").as_bytes());
        // add contigs to vcf header
        for (gene, gff_record) in annotations {
            let length: u64 = ((usize::from(gff_record.end()) + 1)
                - usize::from(gff_record.start())
                + (&self.padding * 2) as usize) as u64;
            header.push_record(&vcf_contig_field(gene, length));
        }
        for entry in PanelRecord::vcf_header_entries() {
            header.push_record(entry);
        }
        header
    }

    fn rules_path(&self) -> PathBuf {
        self.outdir.join("rules.csv")
    }
    fn prg_path(&self) -> PathBuf {
        self.outdir.join("dr.prg")
    }
    fn msa_dir(&self) -> PathBuf {
        self.outdir.join("msas")
    }
    fn prg_index_path(&self) -> PathBuf {
        let ext = format!("k{}.w{}.idx", self.pandora_k, self.pandora_w);
        self.prg_path().add_extension(ext.as_str().as_ref())
    }
    fn prg_index_kmer_prgs_path(&self) -> PathBuf {
        self.outdir.join("kmer_prgs")
    }
    fn reference_index_file(&self) -> PathBuf {
        self.reference_file.add_extension(".fai".as_ref())
    }
    fn organise_prebuilt_prg(&self) -> Result<(), BuildError> {
        let prebuilt_dir = match &self.prebuilt_prg {
            Some(d) => d.canonicalize().unwrap(), // we know it exists so safe to unwrap
            _ => return Ok(()),
        };
        let prg = prebuilt_dir.join(self.prg_path().file_name().unwrap());
        if !prg.exists() {
            return Err(BuildError::MissingFile(prg));
        }
        let msa_dir = prebuilt_dir.join(self.msa_dir().file_name().unwrap());
        if !msa_dir.exists() {
            return Err(BuildError::MissingFile(msa_dir));
        }
        let prg_index = find_prg_index_in(&prebuilt_dir).ok_or_else(|| {
            BuildError::MissingFile(prebuilt_dir.join("dr.prg.kX.wY.idx"))
        })?;
        let prg_index_kmer_prgs =
            prebuilt_dir.join(self.prg_index_kmer_prgs_path().file_name().unwrap());
        let index_exists = prg_index.exists() && prg_index_kmer_prgs.exists();

        if self.outdir == prebuilt_dir {
            Ok(())
        } else {
            let copyopts = fs_extra::dir::CopyOptions {
                overwrite: true,
                skip_exist: false,
                buffer_size: 64000,
                copy_inside: true,
                content_only: false,
                depth: 0,
            };
            let mut to_copy = vec![prg, msa_dir];
            if index_exists {
                to_copy.push(prg_index);
                to_copy.push(prg_index_kmer_prgs);
            }
            match fs_extra::copy_items(&to_copy, &self.outdir, &copyopts) {
                Ok(_) => Ok(()),
                Err(e) => Err(BuildError::CopyError(format!(
                    "Failed to copy prebuilt PRG files with error message {e}"
                ))),
            }
        }
    }
}

impl Runner for Build {
    fn run(&mut self) -> Result<()> {
        if !self.outdir.exists() {
            info!("Outdir doesn't exist...creating...");
            std::fs::create_dir(&self.outdir)
                .context(format!("Failed to create {:?}", &self.outdir))?;
        }
        self.outdir = self
            .outdir
            .canonicalize()
            .context("Failed to canonicalize outdir")?;

        if self.prebuilt_prg.is_some() {
            self.organise_prebuilt_prg()
                .context("Failed to organise prebuilt PRG files")?;
        } else {
            Build::check_vcf_index_exists(
                self.input_vcf.as_ref().unwrap().as_path(),
                !self.dont_index_vcf,
            )?;
        }

        Build::check_fasta_index_exists(&self.reference_file, !self.dont_index_ref)?;

        let premsa_dir = self.outdir.join("premsa");
        let msa_dir = self.msa_dir();
        info!("Building panel index...");

        info!("Loading the panel...");
        let panel: Panel = self.load_panel()?;
        let mut genes: HashSet<String> = panel.keys().map(|k| k.to_owned()).collect();
        info!("Loaded panel");

        if let Some(p) = &self.rules {
            let rules =
                ExpertRules::from_csv(p).context("Failed to read expert rules")?;
            for g in rules.keys() {
                let _ = genes.insert(g.to_owned());
            }
            fs::copy(p, self.rules_path())
                .context("Failed to copy expert rules file")?;
        }

        info!("Loading genome annotation for panel genes...");
        debug!("Panel genes: {:?}", genes);
        let gff_reader = File::open(&self.gff_file)
            .map(BufReader::new)
            .map(gff::Reader::new)?;
        let annotations = load_annotations_for_genes(gff_reader, &genes);
        if genes.len() != annotations.len() {
            let annotation_genes: HashSet<String> =
                annotations.keys().cloned().collect();
            let diff = genes.difference(&annotation_genes);
            warn!("Couldn't load annotations for genes {:?}", diff);
        }
        info!("Loaded annotations");

        let panel_vcf_path = self.outdir.join("panel.bcf");
        let gene_refs_path = self.outdir.join("genes.fa");
        debug!("Creating VCF header...");
        let vcf_header = self.create_vcf_header(&annotations);
        debug!("VCF header created");
        let unsorted_panel_vcf_path = panel_vcf_path.with_extension("unsorted.bcf");
        {
            let mut vcf_writer = bcf::Writer::from_path(
                &unsorted_panel_vcf_path,
                &vcf_header,
                false,
                bcf::Format::Bcf,
            )?;
            debug!("Loading the reference genome index...");

            let fa_reader = File::open(&self.reference_file)
                .map(BufReader::new)
                .map(fasta::Reader::new)?;
            let index = File::open(self.reference_index_file())
                .map(BufReader::new)
                .map(fai::Reader::new)?
                .read_index()?;
            let mut faidx = IndexedReader::new(fa_reader, index);
            debug!("Loaded the reference genome index");
            let mut fa_writer =
                File::create(&gene_refs_path).map(BufWriter::new).map(|f| {
                    fasta::Writer::builder(f)
                        .set_line_base_count(usize::MAX)
                        .build()
                })?;
            if !premsa_dir.exists() {
                debug!("Pre-MSA directory doesn't exist...creating...");
                std::fs::create_dir(&premsa_dir)
                    .context(format!("Failed to create {:?}", &premsa_dir))?;
            } else {
                debug!("Existing pre-MSA directory found...removing...");
                std::fs::remove_dir_all(&premsa_dir)
                    .and_then(|_| std::fs::create_dir(&premsa_dir))
                    .context(format!(
                        "Failed to remove and recreate {:?}",
                        &premsa_dir
                    ))?;
            }

            info!("Converting the panel to a VCF...");
            for (gene, gff_record) in &annotations {
                let seq =
                    extract_gene_from_index(gff_record, &mut faidx, self.padding)?;
                let definition =
                    Definition::new(gene, Some(format!("padding={}", self.padding)));
                let fa_record =
                    fasta::Record::new(definition, Sequence::from(seq.to_owned()));
                fa_writer.write_record(&fa_record)?;

                let premsa_path = premsa_dir.join(format!("{gene}.fa"));
                let mut premsa_writer =
                    File::create(premsa_path).map(BufWriter::new).map(|f| {
                        fasta::Writer::builder(f)
                            .set_line_base_count(usize::MAX)
                            .build()
                    })?;
                premsa_writer.write_record(&fa_record)?;

                let panel_records_for_gene = &panel[gene];
                for panel_record in panel_records_for_gene {
                    let mut vcf_record = vcf_writer.empty_record();
                    match panel_record.to_vcf(&mut vcf_record, &seq, self.padding) {
                        Err(PanelError::PosOutOfRange(pos, gene)) => {
                            warn!(
                                "Position {} is out of range for gene {} [Skipping]",
                                pos, gene
                            );
                            continue;
                        }
                        Err(e) => return Err(anyhow!(e)),
                        _ => (),
                    }

                    let strand = gff_record.strand();
                    vcf_record
                        .push_info_string(b"ST", &[strand.as_ref().as_bytes()])
                        .context(format!(
                            "Couldn't set INFO field ST for {}",
                            panel_record.name()
                        ))?;
                    vcf_writer.write(&vcf_record)?;
                }
            }
        }
        debug!("Indexing the genes reference fasta...");
        index_fasta(&gene_refs_path)
            .context("Failed to index genes reference fasta")?;

        debug!("Sorting the original panel VCF...");
        let bcftools = Bcftools::from_path(&self.bcftools_exec)?;
        bcftools
            .sort(&unsorted_panel_vcf_path, &panel_vcf_path)
            .context("Failed to sort the panel VCF")?;
        if let Err(e) = std::fs::remove_file(unsorted_panel_vcf_path) {
            warn!(
                "Failed to remove the unsorted/original panel VCF with error:\n{}",
                e.to_string()
            );
        }
        debug!("Panel VCF sorted and original removed");
        info!(
            "Panel successfully converted to a VCF at {:?}",
            panel_vcf_path
        );

        info!("Indexing the panel VCF...");
        index_vcf(&panel_vcf_path).context("Failed to index the panel VCF")?;
        info!("Panel VCF successfully indexed");

        if self.prebuilt_prg.is_none() {
            info!("Applying input VCF variants to reference (pre-MSA)");
            // we can unwrap as it MUST be some if prebuilt prg is none
            let input_vcf_path = self.input_vcf.as_ref().unwrap();
            let input_vcf = bcf::Reader::from_path(input_vcf_path)
                .context("Failed to open input VCF file")?;
            let samples = input_vcf.header().samples();

            let consensus_dir = self.outdir.join("consensus");
            if !consensus_dir.exists() {
                debug!("Consensus directory doesn't exist...creating...");
                std::fs::create_dir(&consensus_dir)
                    .context(format!("Failed to create {:?}", &consensus_dir))?;
            } else {
                debug!("Existing consensus directory found...removing...");
                std::fs::remove_dir_all(&consensus_dir)
                    .and_then(|_| std::fs::create_dir(&consensus_dir))
                    .context(format!(
                        "Failed to remove and recreate {:?}",
                        &consensus_dir
                    ))?;
            }

            debug!("Writing consensus sequence reference file...");
            let consensus_ref_path = consensus_dir.join("ref.fa");
            let mut fa_reader = File::open(&gene_refs_path)
                .map(BufReader::new)
                .map(fasta::Reader::new)?;
            {
                let mut fa_writer = File::create(&consensus_ref_path)
                    .map(BufWriter::new)
                    .map(|f| {
                        fasta::Writer::builder(f)
                            .set_line_base_count(usize::MAX)
                            .build()
                    })?;
                for result in fa_reader.records() {
                    let record = result?;
                    let gene = record.definition().name();
                    // safe to unwrap as we built this fasta from the annotations
                    let gff_record = annotations.get(gene).unwrap();
                    let seq = match gff_record.strand() {
                        Strand::Reverse => Ok(revcomp(record.sequence().as_ref())),
                        Strand::Forward => Ok(record.sequence().as_ref().to_vec()),
                        _ => Err(BuildError::MissingStrand(gene.to_string())),
                    }?;
                    let out_record = fasta::Record::new(
                        Definition::new(gene, None),
                        Sequence::from(seq),
                    );
                    fa_writer
                        .write_record(&out_record)
                        .context("Failed to write consensus reference sequence")?;
                }
            }

            samples.into_par_iter().try_for_each(|sample| {
                let s = sample.to_str_lossy();
                let consensus_path = consensus_dir.join(format!("{s}.fa"));
                let args = &[
                    "-H",
                    "A",
                    "-s",
                    &s,
                    "-f",
                    &consensus_ref_path.to_string_lossy(),
                ];
                bcftools.consensus(input_vcf_path, &consensus_path, args)
            })?;
            debug!(
                "Generated consensus sequences for all {} sample(s) in the input VCF",
                input_vcf.header().sample_count()
            );
            std::fs::remove_file(&consensus_ref_path)
                .context("Failed to remove consensus reference file")?;

            {
                debug!("Combining consensus sequences into pre-MSAs...");
                for sample in input_vcf.header().samples() {
                    let s = sample.to_str_lossy().to_string();
                    let sample_consensus_path = consensus_dir.join(format!("{s}.fa"));
                    let mut sample_consensus_fasta = File::open(sample_consensus_path)
                        .map(BufReader::new)
                        .map(fasta::Reader::new)?;
                    for result in sample_consensus_fasta.records() {
                        let consensus_record = result.context(format!("Couldn't parse fasta record from bcftools consensus for sample {}", &s))?;
                        let gene = consensus_record.name();
                        let is_reverse =
                            annotations.get(gene).unwrap().strand() == Strand::Reverse;
                        let gene_pre_msa_path = premsa_dir.join(format!("{gene}.fa"));
                        {
                            let mut premsa_writer = File::options()
                                .append(true)
                                .open(gene_pre_msa_path)
                                .map(BufWriter::new)
                                .map(|f| {
                                    fasta::Writer::builder(f)
                                        .set_line_base_count(usize::MAX)
                                        .build()
                                })?;
                            let def = Definition::new(&s, None);
                            let seq = if is_reverse {
                                revcomp(consensus_record.sequence().as_ref())
                            } else {
                                consensus_record.sequence().as_ref().to_vec()
                            };
                            let out_record =
                                fasta::Record::new(def, Sequence::from(seq));
                            premsa_writer.write_record(&out_record)?;
                        }
                    }
                }
            }

            info!("Generating multiple sequence alignments and reference graphs for all genes and their variants...");
            let mafft = MultipleSeqAligner::from_path(&self.mafft_exec)?;

            if !msa_dir.exists() {
                debug!("MSA directory doesn't exist...creating...");
                std::fs::create_dir(&msa_dir)
                    .context(format!("Failed to create {:?}", &msa_dir))?;
            } else {
                info!("Existing MSA directory found...removing...");
                std::fs::remove_dir_all(&msa_dir)
                    .and_then(|_| std::fs::create_dir(&msa_dir))
                    .context(format!("Failed to remove and recreate {:?}", &msa_dir))?;
            }

            genes.par_iter().try_for_each(|gene| {
                let premsa_path = premsa_dir.join(format!("{gene}.fa"));
                let msa_path = msa_dir.join(format!("{gene}.fa"));
                debug!("Running MSA for {}", gene);
                mafft.run_with(
                    &premsa_path,
                    &msa_path,
                    ["--auto", "--thread", "-1"],
                    true,
                )
            })?;
            info!("Successfully generated MSAs");

            info!("Building reference graphs for genes...");
            let makeprg = MakePrg::from_path(&self.makeprg_exec)?;
            let make_prg_args = &[
                "-t",
                &rayon::current_num_threads().to_string(),
                "-L",
                &self.match_len.to_string(),
                "-N",
                &self.max_nesting.to_string(),
            ];
            makeprg.from_msas_with(&msa_dir, &self.prg_path(), make_prg_args)?;
            info!("Successfully created panel reference graph");
        }
        info!("Indexing reference graph with pandora...");
        let pandora = Pandora::from_path(&self.pandora_exec)?;
        let pandora_args = &[
            "-t",
            &rayon::current_num_threads().to_string(),
            "-k",
            &self.pandora_k.to_string(),
            "-w",
            &self.pandora_w.to_string(),
        ];
        if self.prebuilt_prg.is_some() && self.prg_index_path().exists() {
            info!("Existing pandora index found...skipping...");
        } else {
            pandora.index_with(&self.prg_path(), pandora_args)?;
            info!("Reference graph indexed successfully");
        }
        info!("Panel index built");

        let config_file = &self.outdir.join(".config.toml");
        {
            let mut f = File::create(config_file).map(BufWriter::new)?;
            let config: Config = Config {
                min_match_len: self.match_len,
                max_nesting: self.max_nesting,
                k: self.pandora_k,
                w: self.pandora_w,
                padding: self.padding,
                version: self.version.to_owned(),
            };
            let toml = toml::to_string(&config)?;
            writeln!(f, "{toml}")?;
        }

        debug!("Cleaning up temporary files...");
        if premsa_dir.exists() {
            std::fs::remove_dir_all(&premsa_dir)
                .context(format!("Failed to remove {:?}", &msa_dir))?;
        }
        Ok(())
    }
}

/// A collection of custom errors relating to the build component of this package
#[derive(Error, Debug, PartialEq, Eq)]
pub enum BuildError {
    /// Contig is missing from the FASTA index
    #[error("Contig {0} is missing from the reference genome index")]
    MissingContig(String),
    /// Couldn't fetch a gene
    #[error("Couldn't fetch contig {0} with start {1} and end {2}")]
    FetchError(String, u64, u64),
    /// GFF record doesn't have a strand
    #[error("No strand information in GFF for {0}")]
    MissingStrand(String),
    /// An expected file is missing
    #[error("Missing expected file {0:?}")]
    MissingFile(PathBuf),
    /// File copying error
    #[error("{0}")]
    CopyError(String),
    /// An issue with an index file
    #[error("{0}")]
    IndexError(String),
}

fn load_annotations_for_genes<R>(
    mut reader: gff::Reader<R>,
    genes: &HashSet<String>,
) -> HashMap<String, gff::Record>
where
    R: BufRead,
{
    let mut records = reader.records();
    let mut annotations: HashMap<String, gff::Record> = HashMap::new();

    while let Some(Ok(record)) = records.next() {
        if record.ty() != "gene" {
            continue;
        }

        if let Some(entry) = record
            .attributes()
            .iter()
            .find(|&entry| entry.key() == "Name")
        {
            if genes.contains(entry.value()) {
                annotations.insert(entry.value().to_string(), record.to_owned());
            }
        }
    }
    annotations
}

fn extract_gene_from_index<R>(
    record: &gff::Record,
    faidx: &mut IndexedReader<R>,
    padding: u32,
) -> Result<Vec<u8>, BuildError>
where
    R: BufRead + Seek,
{
    let idx_record = faidx.get(record.reference_sequence_name());
    let contig = match &idx_record {
        Some(Ok(contig)) => contig,
        _ => {
            return Err(BuildError::MissingContig(
                record.reference_sequence_name().to_string(),
            ))
        }
    };
    let contig_len = contig.sequence().len() as u64;

    // GFF3 start is 1-based, inclusive. We want to make it 0-based, inclusive
    let start: u64 = max(
        usize::from(record.start()) as isize - padding as isize - 1,
        0,
    ) as u64;
    // GFF3 end is 1-based, inclusive. We want to make it 0-based, exclusive
    let end: u64 = min(
        usize::from(record.end()) as u64 + padding as u64,
        contig_len,
    );
    if start > end {
        return Err(BuildError::FetchError(
            record.reference_sequence_name().to_string(),
            start,
            end,
        ));
    }

    // noodles sequence slice/positions are 1-based
    let fetch_slice = Position::try_from((start + 1) as usize).unwrap()
        ..=Position::try_from(end as usize).unwrap();
    match contig.sequence().slice(fetch_slice) {
        None => Err(BuildError::FetchError(
            record.reference_sequence_name().to_string(),
            start,
            end,
        )),
        Some(seq) => {
            let is_rev = match record.strand() {
                Strand::Reverse => true,
                Strand::Forward => false,
                _ => {
                    return Err(BuildError::MissingStrand(
                        record.name().unwrap_or("No Name").to_string(),
                    ))
                }
            };
            if is_rev {
                Ok(revcomp(seq.as_ref()))
            } else {
                Ok(Vec::from(seq.as_ref()))
            }
        }
    }
}

fn vcf_contig_field(id: &str, length: u64) -> Vec<u8> {
    format!("{META}contig=<ID={id},length={length}>")
        .as_bytes()
        .to_owned()
}

#[cfg(test)]
mod tests {
    use std::io::{Cursor, Read, Write};
    use std::iter::FromIterator;
    use std::str::FromStr;

    use crate::panel::{Residue, Variant};

    use super::*;

    const PANEL: &str = "tests/cases/panel.tsv";
    const ANNOTATION: &str = "tests/cases/ann.gff3";
    const VCF: &str = "tests/cases/build/input.bcf";
    const REF: &str = "tests/cases/ref.fa";

    #[test]
    fn load_annotations_when_no_genes_in_common_returns_empty() {
        const GFF: &[u8] = b"NC_000962.3\tRefSeq\tgene\t1\t1524\t.\t+\t.\tID=gene-Rv0001;Dbxref=GeneID:885041;Name=dnaA;experiment=DESCRIPTION:Mutation analysis%2C gene expression[PMID: 10375628];gbkey=Gene;gene=dnaA;gene_biotype=protein_coding;locus_tag=Rv0001\n";
        let reader = gff::Reader::new(GFF);
        let genes = HashSet::from_iter(vec!["geneX".to_string()]);

        let actual = load_annotations_for_genes(reader, &genes);
        assert!(actual.is_empty())
    }

    #[test]
    fn load_annotations_for_genes_one_gene_in_common() {
        const GFF: &[u8] = b"NC_000962.3\tRefSeq\tgene\t1\t1524\t.\t+\t.\tID=gene-Rv0001;Dbxref=GeneID:885041;Name=dnaA;experiment=DESCRIPTION:Mutation analysis%2C gene expression[PMID: 10375628];gbkey=Gene;gene=dnaA;gene_biotype=protein_coding;locus_tag=Rv0001\n";
        let reader = gff::Reader::new(GFF);
        let genes = HashSet::from_iter(vec!["geneX".to_string(), "dnaA".to_string()]);

        let actual = load_annotations_for_genes(reader, &genes);
        let (actual_gene, actual_record) = actual.iter().next().unwrap();
        assert_eq!(actual_gene, "dnaA");
        assert_eq!(actual_record.end(), Position::new(1524).unwrap())
    }

    #[test]
    fn load_annotations_for_genes_is_cds_returns_empty() {
        const GFF: &[u8] = b"NC_000962.3\tRefSeq\tCDS\t1\t1524\t.\t+\t.\tID=gene-Rv0001;Dbxref=GeneID:885041;Name=dnaA;experiment=DESCRIPTION:Mutation analysis%2C gene expression[PMID: 10375628];gbkey=Gene;gene=dnaA;gene_biotype=protein_coding;locus_tag=Rv0001\n";
        let reader = gff::Reader::new(GFF);
        let genes = HashSet::from_iter(vec!["geneX".to_string(), "dnaA".to_string()]);

        let actual = load_annotations_for_genes(reader, &genes);
        assert!(actual.is_empty())
    }

    #[test]
    fn extract_gene_from_index_gene_not_in_index() {
        let padding = 0;
        const GFF: &[u8] = b"NC_000962.3\tRefSeq\tCDS\t1\t1524\t.\t+\t0\tID=gene-Rv0001;Dbxref=GeneID:885041;Name=dnaA;experiment=DESCRIPTION:Mutation analysis%2C gene expression[PMID: 10375628];gbkey=Gene;gene=dnaA;gene_biotype=protein_coding;locus_tag=Rv0001\n";
        let mut reader = gff::Reader::new(GFF);
        let record = reader.records().next().unwrap().unwrap();
        const FASTA_FILE: &[u8] = b">chr1\nGTAGGCTGAAAA\nCCCC";
        const FAI_FILE: &[u8] = b"chr1\t16\t6\t12\t13";

        let fa_reader = fasta::Reader::new(Cursor::new(FASTA_FILE));
        let index = fai::Reader::new(FAI_FILE).read_index().unwrap();
        let mut faidx = IndexedReader::new(fa_reader, index);

        let actual = extract_gene_from_index(&record, &mut faidx, padding).unwrap_err();
        let expected = BuildError::MissingContig("NC_000962.3".to_string());
        assert_eq!(actual, expected)
    }

    #[test]
    fn extract_gene_from_index_interval_out_of_bounds() {
        let padding = 0;
        const GFF: &[u8] = b"chr1\tRefSeq\tCDS\t100\t1524\t.\t+\t0\tID=gene-Rv0001;Dbxref=GeneID:885041;Name=dnaA;experiment=DESCRIPTION:Mutation analysis%2C gene expression[PMID: 10375628];gbkey=Gene;gene=dnaA;gene_biotype=protein_coding;locus_tag=Rv0001\n";
        let mut reader = gff::Reader::new(GFF);
        let record = reader.records().next().unwrap().unwrap();
        const FASTA_FILE: &[u8] = b">chr1\nGTAGGCTGAAAA\nCCCC";
        const FAI_FILE: &[u8] = b"chr1\t16\t6\t12\t13";
        let fa_reader = fasta::Reader::new(Cursor::new(FASTA_FILE));
        let index = fai::Reader::new(FAI_FILE).read_index().unwrap();
        let mut faidx = IndexedReader::new(fa_reader, index);

        let actual = extract_gene_from_index(&record, &mut faidx, padding).unwrap_err();
        let expected = BuildError::FetchError("chr1".to_string(), 99, 16);
        assert_eq!(actual, expected)
    }

    #[test]
    fn extract_gene_from_index_first_base() {
        let padding = 0;
        const GFF: &[u8] = b"chr1\tRefSeq\tCDS\t1\t1\t.\t+\t0\tID=gene-Rv0001;Dbxref=GeneID:885041;Name=dnaA;experiment=DESCRIPTION:Mutation analysis%2C gene expression[PMID: 10375628];gbkey=Gene;gene=dnaA;gene_biotype=protein_coding;locus_tag=Rv0001\n";
        let mut reader = gff::Reader::new(GFF);
        let record = reader.records().next().unwrap().unwrap();
        const FASTA_FILE: &[u8] = b">chr1\nGTAGGCTGAAAA\nCCCC";
        const FAI_FILE: &[u8] = b"chr1\t16\t6\t12\t13";
        let fa_reader = fasta::Reader::new(Cursor::new(FASTA_FILE));
        let index = fai::Reader::new(FAI_FILE).read_index().unwrap();
        let mut faidx = IndexedReader::new(fa_reader, index);

        let actual = extract_gene_from_index(&record, &mut faidx, padding).unwrap();
        let expected = b"G";
        assert_eq!(actual, expected)
    }

    #[test]
    fn extract_gene_from_index_too_much_padding_left_wraps_to_start() {
        let padding = 2;
        const GFF: &[u8] = b"chr1\tRefSeq\tCDS\t1\t1\t.\t+\t0\tID=gene-Rv0001;Dbxref=GeneID:885041;Name=dnaA;experiment=DESCRIPTION:Mutation analysis%2C gene expression[PMID: 10375628];gbkey=Gene;gene=dnaA;gene_biotype=protein_coding;locus_tag=Rv0001\n";
        let mut reader = gff::Reader::new(GFF);
        let record = reader.records().next().unwrap().unwrap();
        const FASTA_FILE: &[u8] = b">chr1\nGTAGGCTGAAAA\nCCCC";
        const FAI_FILE: &[u8] = b"chr1\t16\t6\t12\t13";
        let fa_reader = fasta::Reader::new(Cursor::new(FASTA_FILE));
        let index = fai::Reader::new(FAI_FILE).read_index().unwrap();
        let mut faidx = IndexedReader::new(fa_reader, index);

        let actual = extract_gene_from_index(&record, &mut faidx, padding).unwrap();
        let expected = b"GTA";
        assert_eq!(actual, expected)
    }

    #[test]
    fn extract_gene_from_index_too_much_padding_right_wraps_to_end() {
        let padding = 4;
        const GFF: &[u8] = b"chr1\tRefSeq\tCDS\t16\t16\t.\t+\t0\tID=gene-Rv0001;Dbxref=GeneID:885041;Name=dnaA;experiment=DESCRIPTION:Mutation analysis%2C gene expression[PMID: 10375628];gbkey=Gene;gene=dnaA;gene_biotype=protein_coding;locus_tag=Rv0001\n";
        let mut reader = gff::Reader::new(GFF);
        let record = reader.records().next().unwrap().unwrap();
        const FASTA_FILE: &[u8] = b">chr1\nGTAGGCTGAAAA\nCCCC";
        const FAI_FILE: &[u8] = b"chr1\t16\t6\t12\t13";
        let fa_reader = fasta::Reader::new(Cursor::new(FASTA_FILE));
        let index = fai::Reader::new(FAI_FILE).read_index().unwrap();
        let mut faidx = IndexedReader::new(fa_reader, index);

        let actual = extract_gene_from_index(&record, &mut faidx, padding).unwrap();
        let expected = b"ACCCC";
        assert_eq!(actual, expected)
    }

    #[test]
    fn extract_gene_from_index_no_padding_start_and_end_exactly_the_same_as_gene() {
        let padding = 0;
        const GFF: &[u8] = b"chr1\tRefSeq\tCDS\t1\t16\t.\t+\t0\tID=gene-Rv0001;Dbxref=GeneID:885041;Name=dnaA;experiment=DESCRIPTION:Mutation analysis%2C gene expression[PMID: 10375628];gbkey=Gene;gene=dnaA;gene_biotype=protein_coding;locus_tag=Rv0001\n";
        let mut reader = gff::Reader::new(GFF);
        let record = reader.records().next().unwrap().unwrap();
        const FASTA_FILE: &[u8] = b">chr1\nGTAGGCTGAAAA\nCCCC";
        const FAI_FILE: &[u8] = b"chr1\t16\t6\t12\t13";
        let fa_reader = fasta::Reader::new(Cursor::new(FASTA_FILE));
        let index = fai::Reader::new(FAI_FILE).read_index().unwrap();
        let mut faidx = IndexedReader::new(fa_reader, index);

        let actual = extract_gene_from_index(&record, &mut faidx, padding).unwrap();
        let expected = b"GTAGGCTGAAAACCCC";
        assert_eq!(actual, expected)
    }

    #[test]
    fn extract_gene_from_index_on_reverse_strand() {
        let padding = 0;
        const GFF: &[u8] = b"chr1\tRefSeq\tCDS\t1\t16\t.\t-\t0\tID=gene-Rv0001;Dbxref=GeneID:885041;Name=dnaA;experiment=DESCRIPTION:Mutation analysis%2C gene expression[PMID: 10375628];gbkey=Gene;gene=dnaA;gene_biotype=protein_coding;locus_tag=Rv0001\n";
        let mut reader = gff::Reader::new(GFF);
        let record = reader.records().next().unwrap().unwrap();
        const FASTA_FILE: &[u8] = b">chr1\nGTAGGCTGAAAA\nCCCC";
        const FAI_FILE: &[u8] = b"chr1\t16\t6\t12\t13";
        let fa_reader = fasta::Reader::new(Cursor::new(FASTA_FILE));
        let index = fai::Reader::new(FAI_FILE).read_index().unwrap();
        let mut faidx = IndexedReader::new(fa_reader, index);

        let actual = extract_gene_from_index(&record, &mut faidx, padding).unwrap();
        let expected = revcomp(b"GTAGGCTGAAAACCCC");
        assert_eq!(actual, expected)
    }

    #[test]
    fn extract_gene_from_index_no_strand() {
        let padding = 0;
        const GFF: &[u8] = b"chr1\tRefSeq\tCDS\t1\t16\t.\t.\t0\tID=gene-Rv0001;Dbxref=GeneID:885041;Name=dnaA;experiment=DESCRIPTION:Mutation analysis%2C gene expression[PMID: 10375628];gbkey=Gene;gene=dnaA;gene_biotype=protein_coding;locus_tag=Rv0001\n";
        let mut reader = gff::Reader::new(GFF);
        let record = reader.records().next().unwrap().unwrap();
        const FASTA_FILE: &[u8] = b">chr1\nGTAGGCTGAAAA\nCCCC";
        const FAI_FILE: &[u8] = b"chr1\t16\t6\t12\t13";
        let fa_reader = fasta::Reader::new(Cursor::new(FASTA_FILE));
        let index = fai::Reader::new(FAI_FILE).read_index().unwrap();
        let mut faidx = IndexedReader::new(fa_reader, index);

        let actual = extract_gene_from_index(&record, &mut faidx, padding).unwrap_err();
        let expected = BuildError::MissingStrand("dnaA".to_string());
        assert_eq!(actual, expected)
    }

    #[test]
    fn extract_gene_from_index_no_padding_start_same_as_gene_end_minus_one_from_gene_length(
    ) {
        let padding = 0;
        const GFF: &[u8] = b"chr1\tRefSeq\tCDS\t1\t15\t.\t+\t0\tID=gene-Rv0001;Dbxref=GeneID:885041;Name=dnaA;experiment=DESCRIPTION:Mutation analysis%2C gene expression[PMID: 10375628];gbkey=Gene;gene=dnaA;gene_biotype=protein_coding;locus_tag=Rv0001\n";
        let mut reader = gff::Reader::new(GFF);
        let record = reader.records().next().unwrap().unwrap();
        const FASTA_FILE: &[u8] = b">chr1\nGTAGGCTGAAAA\nCCCC";
        const FAI_FILE: &[u8] = b"chr1\t16\t6\t12\t13";
        let fa_reader = fasta::Reader::new(Cursor::new(FASTA_FILE));
        let index = fai::Reader::new(FAI_FILE).read_index().unwrap();
        let mut faidx = IndexedReader::new(fa_reader, index);

        let actual = extract_gene_from_index(&record, &mut faidx, padding).unwrap();
        let expected = b"GTAGGCTGAAAACCC";
        assert_eq!(actual, expected)
    }

    #[test]
    fn extract_gene_from_index_no_padding_start_plus_one_from_gene_start_end_same_as_gene(
    ) {
        let padding = 0;
        const GFF: &[u8] = b"chr1\tRefSeq\tCDS\t2\t16\t.\t+\t0\tID=gene-Rv0001;Dbxref=GeneID:885041;Name=dnaA;experiment=DESCRIPTION:Mutation analysis%2C gene expression[PMID: 10375628];gbkey=Gene;gene=dnaA;gene_biotype=protein_coding;locus_tag=Rv0001\n";
        let mut reader = gff::Reader::new(GFF);
        let record = reader.records().next().unwrap().unwrap();
        const FASTA_FILE: &[u8] = b">chr1\nGTAGGCTGAAAA\nCCCC";
        const FAI_FILE: &[u8] = b"chr1\t16\t6\t12\t13";
        let fa_reader = fasta::Reader::new(Cursor::new(FASTA_FILE));
        let index = fai::Reader::new(FAI_FILE).read_index().unwrap();
        let mut faidx = IndexedReader::new(fa_reader, index);

        let actual = extract_gene_from_index(&record, &mut faidx, padding).unwrap();
        let expected = b"TAGGCTGAAAACCCC";
        assert_eq!(actual, expected)
    }

    #[test]
    fn test_vcf_contig_field() {
        let id = "chr1";
        let length = 33;

        let actual = vcf_contig_field(id, length);
        let expected = b"##contig=<ID=chr1,length=33>";

        assert_eq!(actual, expected)
    }

    #[test]
    fn load_panel_single_record() {
        let gene = "pncA";
        let variant = "G6T";
        let residue = "DNA";
        let drugs = "Drug1";
        let contents: &str = &[gene, variant, residue, drugs].join("\t");
        let mut file = tempfile::NamedTempFile::new().unwrap();
        file.write_all(contents.as_bytes()).unwrap();
        let builder = Build {
            panel_file: PathBuf::from(file.path()),
            ..Default::default()
        };

        let actual = builder.load_panel().unwrap();
        let expected = HashMap::from_iter(vec![(
            gene.to_string(),
            HashSet::from_iter(vec![PanelRecord {
                gene: gene.to_string(),
                variant: Variant::from_str(variant).unwrap(),
                residue: Residue::from_str(residue).unwrap(),
                drugs: HashSet::from_iter(vec![drugs.to_string()]),
            }]),
        )]);

        assert_eq!(actual, expected)
    }

    #[test]
    fn load_panel_duplicate_record() {
        let gene = "pncA";
        let variant = "G6T";
        let residue = "DNA";
        let drugs = "Drug1";
        let contents: &str = &[gene, variant, residue, drugs].join("\t");
        let mut file = tempfile::NamedTempFile::new().unwrap();
        file.write_all(contents.as_bytes()).unwrap();
        file.write_all(contents.as_bytes()).unwrap();
        let builder = Build {
            panel_file: PathBuf::from(file.path()),
            ..Default::default()
        };

        let actual = builder.load_panel().unwrap();
        let expected = HashMap::from_iter(vec![(
            gene.to_string(),
            HashSet::from_iter(vec![PanelRecord {
                gene: gene.to_string(),
                variant: Variant::from_str(variant).unwrap(),
                residue: Residue::from_str(residue).unwrap(),
                drugs: HashSet::from_iter(vec![drugs.to_string()]),
            }]),
        )]);

        assert_eq!(actual, expected)
    }

    #[test]
    fn load_panel_wrong_delimiter() {
        let gene = "pncA";
        let variant = "G6T";
        let residue = "DNA";
        let drugs = "Drug1";
        let contents: &str = &[gene, variant, residue, drugs].join(",");
        let mut file = tempfile::NamedTempFile::new().unwrap();
        file.write_all(contents.as_bytes()).unwrap();
        let builder = Build {
            panel_file: PathBuf::from(file.path()),
            ..Default::default()
        };

        let actual = builder.load_panel();
        assert!(actual.is_err())
    }

    #[test]
    fn load_panel_has_header() {
        let header: &str = &["gene", "variant", "residue", "drugs"].join("\t");
        let gene = "pncA";
        let variant = "G6T";
        let residue = "DNA";
        let drugs = "Drug1";
        let contents: &str = &[gene, variant, residue, drugs].join("\t");
        let mut file = tempfile::NamedTempFile::new().unwrap();
        file.write_all(header.as_bytes()).unwrap();
        file.write_all(contents.as_bytes()).unwrap();
        let builder = Build {
            panel_file: PathBuf::from(file.path()),
            ..Default::default()
        };

        let actual = builder.load_panel();
        assert!(actual.is_err())
    }

    #[test]
    fn load_panel_path_doesnt_exist() {
        let builder = Build {
            panel_file: PathBuf::from("foobar"),
            ..Default::default()
        };

        let actual = builder.load_panel();
        assert!(actual.is_err())
    }

    #[test]
    fn create_vcf_header() {
        let builder = Build::default();
        const GFF: &[u8] = b"NC_000962.3\tRefSeq\tgene\t1\t1524\t.\t+\t.\tID=gene-Rv0001;Name=dnaA;gene=dnaA\n";
        let reader = gff::Reader::new(GFF);
        let genes = HashSet::from_iter(vec!["dnaA".to_string()]);
        let annotations = load_annotations_for_genes(reader, &genes);
        let tmp = tempfile::NamedTempFile::new().unwrap();

        let header = builder.create_vcf_header(&annotations);

        let writer =
            bcf::Writer::from_path(tmp.path(), &header, false, bcf::Format::Vcf)
                .unwrap();
        let view = writer.header();

        assert_eq!(view.contig_count(), 1);
        assert_eq!(view.rid2name(0).unwrap(), b"dnaA")
    }

    #[test]
    fn build_runner() {
        let outdir = tempfile::tempdir().unwrap();
        let mut builder = Build {
            pandora_exec: Some(PathBuf::from("src/ext/pandora")),
            makeprg_exec: Some(PathBuf::from("src/ext/make_prg")),
            mafft_exec: Some(PathBuf::from("src/ext/mafft/bin/mafft")),
            bcftools_exec: Some(PathBuf::from("src/ext/bcftools")),
            gff_file: ANNOTATION.parse().unwrap(),
            panel_file: PANEL.parse().unwrap(),
            input_vcf: Some(VCF.parse().unwrap()),
            reference_file: REF.parse().unwrap(),
            padding: 100,
            outdir: PathBuf::from(outdir.path()),
            match_len: 5,
            max_nesting: 7,
            pandora_w: 14,
            pandora_k: 15,
            ..Build::default()
        };
        let result = builder.run();
        assert!(result.is_ok());

        let mut file1 = File::open("tests/cases/expected/dr.prg").unwrap();
        let mut file2 =
            File::open(format!("{}/dr.prg", outdir.path().to_string_lossy())).unwrap();

        let mut contents = String::new();
        file1.read_to_string(&mut contents).unwrap();
        let mut other = String::new();
        file2.read_to_string(&mut other).unwrap();

        let mut sorted1 = contents.as_bytes().to_owned();
        sorted1.sort_unstable();

        let mut sorted2 = other.as_bytes().to_owned();
        sorted2.sort_unstable();

        assert_eq!(sorted1, sorted2);
    }

    #[test]
    fn build_runner_outdir_doesnt_exist() {
        let outdir = std::env::temp_dir().join("foooo");
        if outdir.exists() {
            std::fs::remove_dir_all(&outdir).unwrap();
        }
        let mut builder = Build {
            pandora_exec: Some(PathBuf::from("src/ext/pandora")),
            makeprg_exec: Some(PathBuf::from("src/ext/make_prg")),
            mafft_exec: Some(PathBuf::from("src/ext/mafft/bin/mafft")),
            bcftools_exec: Some(PathBuf::from("src/ext/bcftools")),
            gff_file: ANNOTATION.parse().unwrap(),
            panel_file: PANEL.parse().unwrap(),
            input_vcf: Some(VCF.parse().unwrap()),
            reference_file: REF.parse().unwrap(),
            padding: 100,
            outdir: outdir.to_owned(),
            match_len: 5,
            max_nesting: 7,
            pandora_w: 14,
            pandora_k: 15,
            ..Build::default()
        };
        let result = builder.run();
        assert!(result.is_ok());

        let mut file1 = File::open("tests/cases/expected/dr.prg").unwrap();
        let mut file2 =
            File::open(format!("{}/dr.prg", outdir.to_string_lossy())).unwrap();

        let mut contents = String::new();
        file1.read_to_string(&mut contents).unwrap();
        let mut other = String::new();
        file2.read_to_string(&mut other).unwrap();

        let mut sorted1 = contents.as_bytes().to_owned();
        sorted1.sort_unstable();

        let mut sorted2 = other.as_bytes().to_owned();
        sorted2.sort_unstable();

        assert_eq!(sorted1, sorted2);
    }

    #[test]
    fn check_vcf_and_index_exists_it_doesnt() {
        let p = PathBuf::from("tests/cases/predict/in.vcf");
        let actual = Build::check_vcf_index_exists(&p, false);
        assert!(actual.is_err())
    }

    #[test]
    fn check_vcf_and_index_exists_it_does() {
        let path = PathBuf::from("tests/cases/build/input.bcf");
        let actual = Build::check_vcf_index_exists(&path, false);
        assert!(actual.is_ok())
    }
}