libsfasta 0.3.4

Better FASTA sequence compression and querying
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
// Easy, high-performance conversion functions
use crossbeam::queue::ArrayQueue;
use crossbeam::thread;
use crossbeam::utils::Backoff;

use std::fs::{metadata, File};
use std::io::{BufReader, Read, Seek, SeekFrom, Write};
use std::num::NonZeroU64;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Instant;

use crate::compression_stream_buffer::{CompressionStreamBuffer, CompressionStreamBufferConfig};
use crate::datatypes::*;
use crate::dual_level_index::*;
use crate::formats::*;
use crate::utils::*;
use crate::CompressionType;

pub struct Converter {
    masking: bool,
    index: bool,
    threads: usize,
    pub block_size: usize,
    seqlocs_chunk_size: usize,
    quality_scores: bool,
    compression_type: CompressionType,
    compression_level: Option<i8>,
    dict: Option<Vec<u8>>,
}

impl Default for Converter {
    fn default() -> Self {
        Converter {
            threads: 4,
            block_size: 4 * 1024 * 1024,    // 1Mb
            seqlocs_chunk_size: 128 * 1024, // 128k
            index: true,
            masking: false,
            quality_scores: false,
            compression_type: CompressionType::ZSTD,
            dict: None,
            compression_level: None,
        }
    }
}

impl Converter {
    // Builder configuration functions...
    pub fn with_dict(mut self, dict: Vec<u8>) -> Self {
        self.dict = Some(dict);
        self
    }

    pub fn with_masking(mut self) -> Self {
        self.masking = true;
        self
    }

    pub fn with_index(mut self) -> Self {
        self.index = true;
        self
    }

    pub fn without_index(mut self) -> Self {
        self.index = false;
        self
    }

    pub fn with_scores(mut self) -> Self {
        self.quality_scores = true;
        self
    }

    pub fn with_threads(mut self, threads: usize) -> Self {
        assert!(
            threads < u16::MAX as usize,
            "Maximum number of supported threads is u16::MAX"
        );
        self.threads = threads;
        self
    }

    pub fn with_block_size(mut self, block_size: usize) -> Self {
        assert!(
            block_size < u32::MAX as usize,
            "Block size must be less than u32::MAX (~4Gb)"
        );

        self.block_size = block_size;
        self
    }

    pub fn with_seqlocs_chunk_size(mut self, chunk_size: usize) -> Self {
        assert!(
            chunk_size < u32::MAX as usize,
            "Chunk size must be less than u32::MAX (~4Gb)"
        );

        self.seqlocs_chunk_size = chunk_size;

        self
    }

    pub fn with_compression_type(mut self, ct: CompressionType) -> Self {
        self.compression_type = ct;
        self
    }

    pub fn with_compression_level(mut self, level: i8) -> Self {
        self.compression_level = Some(level);
        self
    }

    /// Write the headers for the SFASTA tool, return the location of th headers (so they can be updated at the end)
    pub fn write_headers<W>(&self, mut out_fh: &mut Box<W>, sfasta: &Sfasta) -> u64
    where
        W: Write + Seek,
    {
        let bincode_config = bincode::config::standard().with_fixed_int_encoding();

        out_fh
            .write_all("sfasta".as_bytes())
            .expect("Unable to write 'sfasta' to output");

        // Write the directory, parameters, and metadata structs out...

        // Write the version
        bincode::encode_into_std_write(sfasta.version, &mut out_fh, bincode_config)
            .expect("Unable to write directory to file");

        // Write the directory
        let directory_location = out_fh
            .stream_position()
            .expect("Unable to work with seek API");

        let dir: DirectoryOnDisk = sfasta.directory.clone().into();
        bincode::encode_into_std_write(dir, &mut out_fh, bincode_config)
            .expect("Unable to write directory to file");

        // Write the parameters
        bincode::encode_into_std_write(&sfasta.parameters, &mut out_fh, bincode_config)
            .expect("Unable to write Parameters to file");

        // Write the metadata
        bincode::encode_into_std_write(&sfasta.metadata, &mut out_fh, bincode_config)
            .expect("Unable to write Metadata to file");

        // Return the directory location
        directory_location
    }

    /// Main conversion function
    // TODO: Switch R and W into borrows
    pub fn convert_fasta<'convert, W, R>(self, mut in_buf: R, mut out_fh: &mut Box<W>)
    where
        W: WriteAndSeek + 'static,
        R: Read + Send + 'convert,
    {
        let fn_start_time = std::time::Instant::now();

        let mut debug_size: Vec<(String, usize)> = Vec::new();

        // Nearly all of this needs to be fixed int encoding
        let bincode_config = bincode::config::standard().with_fixed_int_encoding();

        assert!(self.block_size < u32::MAX as usize);

        let mut sfasta = Sfasta::default().block_size(self.block_size as u32);

        // Store masks as series of 0s and 1s... Vec<bool>
        if self.masking {
            sfasta = sfasta.with_masking();
        }

        sfasta.parameters.compression_type = self.compression_type;
        sfasta.parameters.compression_dict = self.dict.clone();
        sfasta.parameters.seqlocs_chunk_size = self.seqlocs_chunk_size as u32;
        sfasta.parameters.block_size = self.block_size as u32;

        // Set dummy values for the directory
        sfasta.directory.dummy();

        // Store the location of the directory so we can update it later...
        // It's right after the SFASTA and version identifier...
        let directory_location = self.write_headers(out_fh, &sfasta);

        // Put everything in Arcs and Mutexes to allow for multithreading
        // let in_buf = Arc::new(Mutex::new(in_buf));
        // let out_buf = Arc::new(Mutex::new(out_buf));

        log::info!(
            "Writing sequences start... {}",
            out_fh.stream_position().unwrap()
        );

        let start = out_fh.stream_position().unwrap();

        // Write sequences
        // Creates the sequence block compressor configuration
        let mut sb_config = CompressionStreamBufferConfig::default()
            .with_block_size(self.block_size as u32)
            .with_compression_type(self.compression_type)
            .with_threads(self.threads as u16); // Effectively # of compression threads

        if self.compression_level.is_some() {
            sb_config = sb_config.with_compression_level(self.compression_level.unwrap());
        }

        // TODO: Untested, been awhile... Only useful for very small blocks so hasn't been used lately...
        if let Some(dict) = self.dict {
            sb_config = sb_config.with_compression_dict(dict);
        }

        let start_time = std::time::Instant::now();

        // Calls the big function write_fasta_sequence to process both sequences and masking, and write them into a file....
        // Function returns:
        // Vec<(String, Location)>
        // block_index_pos
        let (ids, mut seqlocs, block_index_pos, headers_location, ids_location, masking_location) =
            write_fasta_sequence(sb_config, &mut in_buf, out_fh, &mut debug_size);

        let end_time = std::time::Instant::now();

        log::info!(
            "Write Fasta Sequence write time: {:?}",
            end_time - start_time
        );

        let end = out_fh.stream_position().unwrap();

        debug_size.push(("sequences".to_string(), (end - start) as usize));

        log::info!(
            "Writing sequences finished... {}",
            out_fh.stream_position().unwrap()
        );

        // TODO: Here is where we would write out the Seqinfo stream (if it's decided to do it)

        // TODO: Support for Index32 (and even smaller! What if only 1 or 2 sequences?)
        let mut indexer =
            crate::dual_level_index::DualIndexBuilder::with_capacity(seqlocs.index_len());

        // The index points to the location of the Location structs.
        // Location blocks are chunked into SEQLOCS_CHUNK_SIZE
        // So index.get(id) --> integer position of the location struct
        // Which will be in integer position / SEQLOCS_CHUNK_SIZE chunk offset
        // This will then point to the different location blocks where the sequence is...
        //
        // TODO: Optional index can probably be handled better...
        let mut dual_index_pos = 0;

        let mut seqlocs_location = 0;

        // Build index in another thread...
        thread::scope(|s| {
            // Start a thread to build the index...
            let index_handle = Some(s.spawn(|_| {
                for (i, id) in ids.into_iter().enumerate() {
                    indexer.add(id, i as u32);
                }

                let indexer: DualIndexWriter = indexer.into();
                indexer
            }));

            // Use the main thread to write the sequence locations...
            log::info!(
                "Writing SeqLocs to file. {}",
                out_fh.stream_position().unwrap()
            );

            let start = out_fh.stream_position().unwrap();

            let start_time = std::time::Instant::now();

            seqlocs_location = seqlocs.write_to_buffer(&mut out_fh);
            log::info!(
                "Writing SeqLocs to file: COMPLETE. {}",
                out_fh.stream_position().unwrap()
            );

            let end_time = std::time::Instant::now();
            log::info!("SeqLocs write time: {:?}", end_time - start_time);

            let end = out_fh.stream_position().unwrap();
            debug_size.push(("seqlocs".to_string(), (end - start) as usize));

            if self.index {
                dual_index_pos = out_fh
                    .stream_position()
                    .expect("Unable to work with seek API");

                let mut index = index_handle.unwrap().join().unwrap();
                log::info!(
                    "Writing index to file. {}",
                    out_fh.stream_position().unwrap()
                );

                let start = out_fh.stream_position().unwrap();

                let start_time = std::time::Instant::now();
                index.write_to_buffer(&mut out_fh);
                let end_time = std::time::Instant::now();
                log::info!("Index write time: {:?}", end_time - start_time);

                let end = out_fh.stream_position().unwrap();
                debug_size.push(("index".to_string(), (end - start) as usize));

                log::info!(
                    "Writing index to file: COMPLETE. {}",
                    out_fh.stream_position().unwrap()
                );
            }
        })
        .expect("Error");

        sfasta.directory.seqlocs_loc = NonZeroU64::new(seqlocs_location);
        sfasta.directory.index_loc = NonZeroU64::new(dual_index_pos);
        sfasta.directory.headers_loc = headers_location;
        sfasta.directory.ids_loc = ids_location;
        sfasta.directory.masking_loc = masking_location;

        // Go to the beginning, and write the location of the index

        sfasta.directory.block_index_loc = NonZeroU64::new(block_index_pos);

        out_fh
            .seek(SeekFrom::Start(directory_location))
            .expect("Unable to rewind to start of the file");

        // Here we re-write the directory information at the start of the file, allowing for
        // easy jumps to important areas while keeping everything in a single file

        let start = out_fh.stream_position().unwrap();

        let start_time = std::time::Instant::now();

        let dir: DirectoryOnDisk = sfasta.directory.clone().into();
        bincode::encode_into_std_write(dir, &mut out_fh, bincode_config)
            .expect("Unable to write directory to file");

        let end = out_fh.stream_position().unwrap();
        debug_size.push(("directory".to_string(), (end - start) as usize));

        let end_time = std::time::Instant::now();

        log::info!("Directory write time: {:?}", end_time - start_time);

        log::info!("DEBUG: {:?}", debug_size);

        out_fh.flush().expect("Unable to flush output file");

        let fn_end_time = std::time::Instant::now();
        log::info!("Conversion time: {:?}", fn_end_time - fn_start_time);
    }
}

// TODO: Add support for metadata here...
// TODO: Will likely need to be the same builder style
// TODO: Will need to generalize this function so it works with FASTA & FASTQ & Masking

pub fn generic_open_file(filename: &str) -> (usize, bool, Box<dyn Read + Send>) {
    let filesize = metadata(filename)
        .unwrap_or_else(|_| panic!("{}", &format!("Unable to open file: {}", filename)))
        .len();

    let file = match File::open(filename) {
        Err(why) => panic!("Couldn't open {}: {}", filename, why),
        Ok(file) => file,
    };

    let file = BufReader::new(file);
    let mut compressed: bool = false;

    let fasta: Box<dyn Read + Send> = if filename.ends_with("gz") {
        compressed = true;
        Box::new(flate2::read::GzDecoder::new(file))
    } else if filename.ends_with("snappy") || filename.ends_with("sz") || filename.ends_with("sfai")
    {
        compressed = true;
        Box::new(snap::read::FrameDecoder::new(file))
    } else {
        Box::new(file)
    };

    (filesize as usize, compressed, fasta)
}

/// Input filehandle goes in, output goes out.
/// Function returns:
/// Vec<(String, Location)>
/// block_index_pos
/// in_buffer
/// out_buffer

#[derive(Debug)]
enum Work {
    FastaPayload(crate::datatypes::Sequence),
    FastqPayload(crate::datatypes::Sequence),
    Shutdown,
}

pub fn write_fasta_sequence<'convert, W, R>(
    sb_config: CompressionStreamBufferConfig,
    in_buf: &mut R,
    mut out_fh: &mut Box<W>,
    debug_size: &mut Vec<(String, usize)>,
) -> (
    Vec<std::sync::Arc<String>>,
    SeqLocs<'convert>,
    u64,
    Option<NonZeroU64>,
    Option<NonZeroU64>,
    Option<NonZeroU64>,
)
where
    W: WriteAndSeek + 'convert,
    R: Read + Send + 'convert,
{
    let bincode_config = bincode::config::standard().with_fixed_int_encoding();

    let mut sb = CompressionStreamBuffer::from_config(sb_config);

    // Get a handle for the output queue from the sequence compressor
    let oq = sb.get_output_queue();

    // Get a handle to the shutdown flag...
    let shutdown = sb.get_shutdown_flag();

    let mut seq_locs: Option<SeqLocs> = None;
    let mut block_index_pos = None;
    let mut headers = None;
    let mut headers_location = None;
    let mut ids = None;
    let mut ids_location = None;
    let mut ids_string = Vec::new();
    let mut masking = None;
    let mut masking_location = None;

    let fasta_queue: Arc<ArrayQueue<Work>> = std::sync::Arc::new(ArrayQueue::new(8192 * 4));

    let fasta_queue_in = Arc::clone(&fasta_queue);
    let fasta_queue_out = Arc::clone(&fasta_queue);

    // Pop to a new thread that pushes FASTA sequences into the sequence buffer...
    thread::scope(|s| {
        let fasta_thread = s.spawn(|_| {
            let mut fasta_thread_spins: usize = 0;
            // Convert reader into buffered reader then into the Fasta struct (and iterator)
            let mut in_buf_reader = BufReader::new(in_buf);
            let fasta = Fasta::from_buffer(&mut in_buf_reader);

            let backoff = Backoff::new();

            for x in fasta {
                backoff.reset();
                if x.is_err() {
                    while fasta_queue_in.push(Work::Shutdown).is_err() {
                        backoff.snooze();
                    }

                    log::error!("Error reading FASTA file: {:?}", x);
                    panic!("Error reading FASTA file: {:?}", x);
                }

                let mut d = Work::FastaPayload(x.unwrap());
                while let Err(z) = fasta_queue_in.push(d) {
                    d = z;
                    backoff.snooze();
                    fasta_thread_spins = fasta_thread_spins.saturating_add(1);

                    if backoff.is_completed() {
                        std::thread::park();
                        backoff.reset();
                    }
                }
            }

            log::info!("FASTA reading complete...");
            while fasta_queue_in.push(Work::Shutdown).is_err() {
                backoff.snooze();
            }

            log::info!("fasta_thread_spins: {}", fasta_thread_spins);
        });

        let fasta_thread_clone = fasta_thread.thread().clone();

        // TODO: Multithread this part
        // Thread that handles the heavy lifting of the incoming Seq structs
        let reader_handle = s.spawn(move |_| {
            sb.initialize();

            let mut headers = StringBlockStore::default().with_block_size(1024 * 1024);
            let mut seqlocs = SeqLocs::default();
            let mut ids = StringBlockStore::default().with_block_size(512 * 1024);
            let mut ids_string = Vec::new();
            let mut masking = Masking::default();

            // For each Sequence in the fasta file, make it upper case (masking is stored separately)
            // Add the sequence to the SequenceBlocks, get the SeqLocs and store them in Location struct
            // And store that in seq_locs Vec...

            let backoff = Backoff::new();

            let mut masking_time = std::time::Duration::new(0, 0);
            let mut adding_time = std::time::Duration::new(0, 0);
            let mut seq_loc_time = std::time::Duration::new(0, 0);

            let mut fasta_queue_spins: usize = 0;
            loop {
                backoff.reset();
                fasta_thread_clone.unpark();
                match fasta_queue_out.pop() {
                    Some(Work::FastaPayload(seq)) => {
                        let (seqid, seqheader, seq, _) = seq.into_parts();
                        let mut location = SeqLoc::new();
                        let now = std::time::Instant::now();
                        let masked = masking.add_masking(&seq.as_ref().unwrap()[..]);
                        if let Some(x) = masked {
                            let x = seqlocs.add_locs(&x);
                            location.masking = Some(x);
                        }
                        masking_time += now.elapsed();
                        let now = std::time::Instant::now();
                        let loc = sb.add_sequence(&mut seq.unwrap()[..]).unwrap(); // Destructive, capitalizes everything...
                        adding_time += now.elapsed();
                        let now = std::time::Instant::now();
                        let myid = std::sync::Arc::new(seqid.unwrap());
                        ids_string.push(std::sync::Arc::clone(&myid));
                        let idloc = ids.add(&(*myid));
                        if let Some(x) = seqheader {
                            let x = seqlocs.add_locs(&headers.add(x));
                            location.headers = Some((x.0, x.1 as u8));
                        }
                        let x = seqlocs.add_locs(&idloc);
                        location.ids = Some((x.0, x.1 as u8));
                        location.sequence = Some(seqlocs.add_locs(&loc));
                        seq_loc_time += now.elapsed();
                        seqlocs.add_to_index(location);
                    }
                    Some(Work::FastqPayload(_)) => panic!("Received FASTQ payload in FASTA thread"),
                    Some(Work::Shutdown) => break,
                    None => {
                        fasta_thread_clone.unpark();
                        fasta_queue_spins = fasta_queue_spins.saturating_add(1);
                        backoff.snooze();
                        if backoff.is_completed() {
                            backoff.reset();
                            std::thread::park();
                        }
                    }
                }
            }

            log::info!("Masking time: {}ms", masking_time.as_millis());
            log::info!("Adding time: {}ms", adding_time.as_millis());
            log::info!("SeqLoc time: {}ms", seq_loc_time.as_millis());
            log::info!("Finalizing SequenceBuffer");
            // Finalize pushes the last block, which is likely smaller than the complete block size
            match sb.finalize() {
                Ok(()) => (),
                Err(x) => panic!("Unable to finalize sequence buffer, {:#?}", x),
            };

            log::info!("fasta_queue_spins: {}", fasta_queue_spins);

            // Return seq_locs to the main thread
            (seqlocs, headers, ids, ids_string, masking)
        });

        // Store the location of the Sequence Blocks...
        // Stored as Vec<(u32, u64)> because multithreading means it does not have to be in order
        let mut block_locs = Vec::with_capacity(1024);
        let mut pos = out_fh
            .stream_position()
            .expect("Unable to work with seek API");

        let mut result;

        // For each entry processed by the sequence buffer, pop it out, write the block, and write the block id
        // FORMAT: Write each sequence block to file

        // This writes out the sequence blocks (SeqBlockCompressed / SBC)
        let start = out_fh.stream_position().unwrap();
        let backoff = Backoff::new();

        let mut output_spins: usize = 0;
        loop {
            result = oq.pop();

            match result {
                None => {
                    if oq.is_empty() && shutdown.load(Ordering::Relaxed) {
                        log::info!("output_spins: {}", output_spins);
                        break;
                    } else {
                        reader_handle.thread().unpark();
                        output_spins = output_spins.saturating_add(1);
                        backoff.snooze();
                    }
                }
                Some((block_id, sb)) => {
                    bincode::encode_into_std_write(sb, &mut out_fh, bincode_config)
                        .expect("Unable to write to bincode output");
                    // log::info!("Writer wrote block {}", block_id);

                    block_locs.push((block_id, pos));

                    pos = out_fh
                        .stream_position()
                        .expect("Unable to work with seek API");
                }
            }
        }

        // Join the FASTA thread (ot block until it can be joined)
        fasta_thread.join().unwrap();

        let end = out_fh.stream_position().unwrap();
        log::info!("DEBUG: Wrote {} bytes of sequence blocks", end - start);
        debug_size.push(("Sequence Blocks".to_string(), (end - start) as usize));

        let start = out_fh.stream_position().unwrap();

        // Block Index
        block_index_pos = Some(
            out_fh
                .stream_position()
                .expect("Unable to work with seek API"),
        );

        let mut block_locs_store = U64BlockStore::default();

        // Write the block index to file (We sort so it is ordinal)
        block_locs.sort_by(|a, b| a.0.cmp(&b.0));
        let block_locs: Vec<u64> = block_locs.iter().map(|x| x.1).collect();

        log::info!("DEBUG: Writing {} total blocks", block_locs.len());

        block_locs.into_iter().for_each(|x| {
            block_locs_store.add(x);
        });

        block_locs_store.write_to_buffer(&mut out_fh);

        let end = out_fh.stream_position().unwrap();

        log::info!("DEBUG: Wrote {} bytes of block index", end - start);
        debug_size.push(("Block Index".to_string(), (end - start) as usize));

        reader_handle.thread().unpark();
        let j = reader_handle.join().expect("Unable to join thread");
        seq_locs = Some(j.0);
        headers = Some(j.1);
        ids = Some(j.2);
        ids_string = j.3;
        masking = Some(j.4);

        let start_time = Instant::now();

        let start = out_fh.stream_position().unwrap();

        headers_location = match headers.as_mut().unwrap().write_to_buffer(&mut out_fh) {
            Some(x) => NonZeroU64::new(x),
            None => None,
        };

        let end = out_fh.stream_position().unwrap();

        let end_time = Instant::now();

        debug_size.push(("Headers".to_string(), (end - start) as usize));

        log::info!(
            "DEBUG: Wrote {} bytes of headers in {:#?}",
            end - start,
            end_time - start_time
        );

        let start_time = Instant::now();
        let start = out_fh.stream_position().unwrap();
        ids_location = match ids.as_mut().expect("No ids!").write_to_buffer(&mut out_fh) {
            Some(x) => NonZeroU64::new(x),
            None => None,
        };

        let end = out_fh.stream_position().unwrap();

        let end_time = Instant::now();

        debug_size.push(("IDs".to_string(), (end - start) as usize));
        log::info!(
            "DEBUG: Wrote {} bytes of ids in {:#?}",
            end - start,
            end_time - start_time
        );

        let start_time = Instant::now();
        let start = out_fh.stream_position().unwrap();
        masking_location = match masking.as_mut().unwrap().write_to_buffer(&mut out_fh) {
            Some(x) => NonZeroU64::new(x),
            None => None,
        };

        let end = out_fh.stream_position().unwrap();
        debug_size.push(("Masking".to_string(), (end - start) as usize));
        let end_time = Instant::now();
        log::info!(
            "DEBUG: Wrote {} bytes of masking in {:#?}",
            end - start,
            end_time - start_time
        );

        out_fh.flush().expect("Unable to flush output buffer");

        // TODO: Comes out about 20% smaller but about 2x as long in time...
        /*

        let start_time = Instant::now();
        let start = out_buf.seek(SeekFrom::Current(0)).unwrap();
        masking_location = match masking.as_mut().unwrap().write_to_buffer_zstd(&mut out_buf) {
            Some(x) => NonZeroU64::new(x),
            None => None,
        };
        let end = out_buf.seek(SeekFrom::Current(0)).unwrap();
        let end_time = Instant::now();
        log::info!(
            "DEBUG: Wrote {} bytes of masking ZSTD in {:#?}",
            end - start,
            end_time - start_time
        ); */
    })
    .expect("Error");

    (
        ids_string,
        seq_locs.expect("Error"),
        block_index_pos.expect("Error"),
        headers_location,
        ids_location,
        masking_location,
    )
}

// We have to iterate through the file twice...
// TODO:
#[allow(dead_code)]
pub fn write_fastq_sequence<'write, W, R>(
    sb_config: CompressionStreamBufferConfig,
    in_buf: &mut R,
    mut out_fh: &mut W,
) -> (
    Vec<std::sync::Arc<String>>,
    SeqLocs<'write>,
    u64,
    Option<NonZeroU64>,
    Option<NonZeroU64>,
    Option<NonZeroU64>,
)
where
    W: WriteAndSeek + 'write,
    R: Read + Send + Seek + 'write,
{
    let bincode_config = bincode::config::standard().with_fixed_int_encoding();

    let mut sb = CompressionStreamBuffer::from_config(sb_config.clone());

    // Get a handle for the output queue from the sequence compressor
    let oq = sb.get_output_queue();

    // Get a handle to the shutdown flag...
    let shutdown = sb.get_shutdown_flag();

    let mut seq_locs: Option<SeqLocs> = None;
    let mut block_index_pos = None;
    let mut headers = None;
    let mut headers_location = None;
    let mut ids = None;
    let mut ids_location = None;
    let mut ids_string = Vec::new();
    let mut masking = None;
    let mut masking_location = None;

    let fastq_queue: Arc<ArrayQueue<Work>> = std::sync::Arc::new(ArrayQueue::new(8192));

    let fastq_queue_in = Arc::clone(&fastq_queue);
    let fastq_queue_out = Arc::clone(&fastq_queue);

    // Pop to a new thread that pushes FASTQ sequences into the sequence buffer...
    thread::scope(|s| {
        // Sequence thread....
        let fastq_thread = s.spawn(|_| {
            // Convert reader into buffered reader then into the Fastq struct (and iterator)
            let mut in_buf_reader = BufReader::new(in_buf);
            let fastq = Fastq::from_buffer(&mut in_buf_reader);

            let backoff = Backoff::new();

            for x in fastq {
                let mut x = x.unwrap();
                x.scores.take(); // No need to send scores across threads when we aren't using them yet...
                let mut d = Work::FastqPayload(x);
                while let Err(z) = fastq_queue_in.push(d) {
                    d = z;
                    backoff.snooze();
                }
            }

            while fastq_queue_in.push(Work::Shutdown).is_err() {
                backoff.snooze();
            }

            // Return the in_buf
            in_buf_reader.into_inner()
        });

        // let mut out_buf = BufWriter::new(&mut out_fh);
        let reader_handle = s.spawn(move |_| {
            sb.initialize();

            // Store the ID of the sequence and the Location (SeqLocs)
            // TODO: Auto adjust based off of size of input FASTA file

            let mut seqlocs = SeqLocs::default();
            let mut headers = StringBlockStore::default().with_block_size(1024 * 1024);
            let mut ids = StringBlockStore::default().with_block_size(512 * 1024);
            let mut ids_string = Vec::new();
            let mut masking = Masking::default();

            // For each Sequence in the fasta file, make it upper case (masking is stored separately)
            // Add the sequence, get the SeqLocs and store them in Location struct
            // And store that in seq_locs Vec...

            let backoff = Backoff::new();

            loop {
                match fastq_queue_out.pop() {
                    Some(Work::FastqPayload(seq)) => {
                        let (seqid, seqheader, mut seq, _) = seq.into_parts();
                        let mut location = SeqLoc::new();
                        let masked = masking.add_masking(&seq.as_ref().unwrap()[..]);
                        if let Some(x) = masked {
                            let x = seqlocs.add_locs(&x);
                            location.masking = Some(x);
                        }
                        let loc = sb.add_sequence(&mut seq.unwrap()[..]).unwrap(); // Destructive, capitalizes everything...
                        let myid = std::sync::Arc::new(seqid.unwrap());
                        ids_string.push(std::sync::Arc::clone(&myid));
                        let idloc = ids.add(&(*myid));
                        if let Some(header) = seqheader {
                            let x = headers.add(header);
                            let x = seqlocs.add_locs(&x);
                            location.headers = Some((x.0, x.1 as u8));
                        }
                        let x = seqlocs.add_locs(&loc);
                        location.ids = Some((x.0, x.1 as u8));
                        location.sequence = Some(seqlocs.add_locs(&loc));
                        seqlocs.add_to_index(location);
                    }
                    Some(Work::FastaPayload(_)) => {
                        panic!("Received Fastq Payload in FASTQ thread.");
                    }
                    Some(Work::Shutdown) => break,
                    None => {
                        backoff.snooze();
                    }
                }
            }

            // Finalize pushes the last block, which is likely smaller than the complete block size
            match sb.finalize() {
                Ok(()) => (),
                Err(x) => panic!("Unable to finalize sequence buffer, {:#?}", x),
            };

            // Return seq_locs to the main thread
            (seqlocs, headers, ids, ids_string, masking)
        });

        // Store the location of the Sequence Blocks...
        // Stored as Vec<(u32, u64)> because the multithreading means it does not have to be in order
        let mut block_locs = Vec::with_capacity(8192);
        let mut pos = out_fh
            .seek(SeekFrom::Current(0))
            .expect("Unable to work with seek API");

        let mut result;

        // For each entry processed by the sequence buffer, pop it out, write the block, and write the block id
        // FORMAT: Write each sequence block to file

        // This writes out the sequence blocks (seqblockcompressed)
        let start = out_fh.seek(SeekFrom::Current(0)).unwrap();

        loop {
            result = oq.pop();

            match result {
                None => {
                    if oq.is_empty() && shutdown.load(Ordering::Relaxed) {
                        break;
                    }
                }
                Some((block_id, sb)) => {
                    bincode::encode_into_std_write(&sb, &mut out_fh, bincode_config)
                        .expect("Unable to write to bincode output");
                    // log::info!("Writer wrote block {}", block_id);

                    block_locs.push((block_id, pos));

                    pos = out_fh
                        .seek(SeekFrom::Current(0))
                        .expect("Unable to work with seek API");
                }
            }
        }

        let in_buf = fastq_thread.join().unwrap();
        let j = reader_handle.join().unwrap();

        let end = out_fh.seek(SeekFrom::Current(0)).unwrap();
        log::info!("DEBUG: Wrote {} bytes of sequence blocks", end - start);

        let _start = out_fh.seek(SeekFrom::Current(0)).unwrap();
        // Write FASTQ scores here...
        let fastq_thread = s.spawn(|_| {
            // Convert reader into buffered reader then into the Fastq struct (and iterator)
            let mut in_buf_reader = BufReader::new(in_buf);
            let fastq = Fastq::from_buffer(&mut in_buf_reader);

            let backoff = Backoff::new();

            for x in fastq {
                let mut x = x.unwrap();
                x.sequence.take(); // No need to send seqs across threads when we are done with that...
                let mut d = Work::FastqPayload(x);
                while let Err(z) = fastq_queue_in.push(d) {
                    d = z;
                    backoff.snooze();
                }
            }

            while fastq_queue_in.push(Work::Shutdown).is_err() {
                backoff.snooze();
            }
        });

        let mut sb = CompressionStreamBuffer::from_config(sb_config);
        let _fastq_queue_in = Arc::clone(&fastq_queue);
        let fastq_queue_out = Arc::clone(&fastq_queue);

        let scores_handle = s.spawn(move |_| {
            sb.initialize();

            // Store the ID of the sequence and the Location (SeqLocs)
            // TODO: Auto adjust based off of size of input FASTA file
            let mut seq_locs: Vec<SeqLoc> = Vec::with_capacity(1024);

            // For each Sequence in the fasta file, make it upper case (masking is stored separately)
            // Add the sequence, get the SeqLocs and store them in Location struct
            // And store that in seq_locs Vec...

            let backoff = Backoff::new();

            let mut idx = 0;

            loop {
                match fastq_queue_out.pop() {
                    Some(Work::FastqPayload(seq)) => {
                        idx += 1;
                        let (_, _, _, mut scores) = seq.into_parts();
                        let loc = sb.add_sequence(&mut scores.unwrap()[..]).unwrap();
                        // Destructive, capitalizes everything...
                        // TODO:
                        // seqlocs.index.as_mut().unwrap().scores = Some(loc);
                    }
                    Some(Work::FastaPayload(_)) => {
                        panic!("Received Fastq Payload in FASTQ thread.");
                    }
                    Some(Work::Shutdown) => break,
                    None => {
                        backoff.snooze();
                    }
                }
            }

            // Finalize pushes the last block, which is likely smaller than the complete block size
            match sb.finalize() {
                Ok(()) => (),
                Err(x) => panic!("Unable to finalize sequence buffer, {:#?}", x),
            };

            // Return seq_locs to the main thread
            seq_locs
        });

        // FASTQ sequence blocks, etc...
        // All the following is TODO

        // Store the location of the Sequence Blocks...
        // Stored as Vec<(u32, u64)> because the multithreading means it does not have to be in order
        let mut block_locs = Vec::with_capacity(8192);
        let mut pos = out_fh
            .seek(SeekFrom::Current(0))
            .expect("Unable to work with seek API");

        let mut result;

        // For each entry processed by the sequence buffer, pop it out, write the block, and write the block id
        // FORMAT: Write each sequence block to file

        // This writes out the sequence blocks (seqblockcompressed)
        let start = out_fh.seek(SeekFrom::Current(0)).unwrap();

        loop {
            result = oq.pop();

            match result {
                None => {
                    if oq.is_empty() && shutdown.load(Ordering::Relaxed) {
                        break;
                    }
                }
                Some((block_id, sb)) => {
                    bincode::encode_into_std_write(&sb, &mut out_fh, bincode_config)
                        .expect("Unable to write to bincode output");
                    // log::info!("Writer wrote block {}", block_id);

                    block_locs.push((block_id, pos));

                    pos = out_fh
                        .seek(SeekFrom::Current(0))
                        .expect("Unable to work with seek API");
                }
            }
        }

        fastq_thread.join().unwrap();

        let end = out_fh.seek(SeekFrom::Current(0)).unwrap();
        log::info!("DEBUG: Wrote {} bytes of sequence blocks", end - start);

        let start = out_fh.seek(SeekFrom::Current(0)).unwrap();

        // Block Index
        block_index_pos = Some(
            out_fh
                .seek(SeekFrom::Current(0))
                .expect("Unable to work with seek API"),
        );

        // Write the block index to file
        block_locs.sort_by(|a, b| a.0.cmp(&b.0));
        let block_locs: Vec<u64> = block_locs.iter().map(|x| x.1).collect();

        let block_locs_u32 = unsafe {
            std::slice::from_raw_parts(
                block_locs.as_ptr() as *const u32,
                block_locs.len() * std::mem::size_of::<u64>() / std::mem::size_of::<u32>(),
            )
        };

        let (num_bits, bitpacked) = bitpack_u32(block_locs_u32);
        bincode::encode_into_std_write(&num_bits, &mut out_fh, bincode_config)
            .expect("Unable to write to bincode output");
        bincode::encode_into_std_write(&bitpacked, &mut out_fh, bincode_config).unwrap();

        let end = out_fh.seek(SeekFrom::Current(0)).unwrap();
        log::info!("DEBUG: Wrote {} bytes of block index", end - start);

        // let j = reader_handle.join().expect("Unable to join thread");
        seq_locs = Some(j.0);
        headers = Some(j.1);
        ids = Some(j.2);
        ids_string = j.3;
        masking = Some(j.4);

        let start_time = Instant::now();
        let start = out_fh.seek(SeekFrom::Current(0)).unwrap();
        headers_location = match headers.as_mut().unwrap().write_to_buffer(&mut out_fh) {
            Some(x) => NonZeroU64::new(x),
            None => None,
        };
        let end = out_fh.seek(SeekFrom::Current(0)).unwrap();
        let end_time = Instant::now();
        log::info!(
            "DEBUG: Wrote {} bytes of headers in {:#?}",
            end - start,
            end_time - start_time
        );

        let start_time = Instant::now();
        let start = out_fh.seek(SeekFrom::Current(0)).unwrap();
        ids_location = match ids.as_mut().expect("No ids!").write_to_buffer(&mut out_fh) {
            Some(x) => NonZeroU64::new(x),
            None => None,
        };
        let end = out_fh.seek(SeekFrom::Current(0)).unwrap();
        let end_time = Instant::now();
        log::info!(
            "DEBUG: Wrote {} bytes of ids in {:#?}",
            end - start,
            end_time - start_time
        );

        let start_time = Instant::now();
        let start = out_fh.seek(SeekFrom::Current(0)).unwrap();
        masking_location = match masking.as_mut().unwrap().write_to_buffer(&mut out_fh) {
            Some(x) => NonZeroU64::new(x),
            None => None,
        };
        let end = out_fh.seek(SeekFrom::Current(0)).unwrap();
        let end_time = Instant::now();
        log::info!(
            "DEBUG: Wrote {} bytes of masking in {:#?}",
            end - start,
            end_time - start_time
        );

        // TODO: Comes out about 20% smaller but about 2x as long in time...
        /*

        let start_time = Instant::now();
        let start = out_buf.seek(SeekFrom::Current(0)).unwrap();
        masking_location = match masking.as_mut().unwrap().write_to_buffer_zstd(&mut out_buf) {
            Some(x) => NonZeroU64::new(x),
            None => None,
        };
        let end = out_buf.seek(SeekFrom::Current(0)).unwrap();
        let end_time = Instant::now();
        log::info!(
            "DEBUG: Wrote {} bytes of masking ZSTD in {:#?}",
            end - start,
            end_time - start_time
        ); */
    })
    .expect("Error");

    (
        ids_string,
        seq_locs.expect("Error"),
        block_index_pos.expect("Error"),
        headers_location,
        ids_location,
        masking_location,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::datatypes::*;
    use std::io::Cursor;

    fn init() {
        let _ = env_logger::builder().is_test(true).try_init();
    }

    #[test]
    pub fn test_create_sfasta() {
        init();

        let bincode_config = bincode::config::standard().with_fixed_int_encoding();

        let mut out_buf = Box::new(Cursor::new(Vec::new()));

        println!("test_data/test_convert.fasta");
        let mut in_buf = BufReader::new(
            File::open("test_data/test_convert.fasta").expect("Unable to open testing file"),
        );

        let converter = Converter::default().with_threads(6).with_block_size(8192);

        converter.convert_fasta(&mut in_buf, &mut out_buf);

        if let Err(x) = out_buf.seek(SeekFrom::Start(0)) {
            panic!("Unable to seek to start of file, {:#?}", x)
        };

        let mut sfasta_marker: [u8; 6] = [0; 6];
        out_buf
            .read_exact(&mut sfasta_marker)
            .expect("Unable to read SFASTA Marker");
        assert!(sfasta_marker == "sfasta".as_bytes());

        let _version: u64 = bincode::decode_from_std_read(&mut out_buf, bincode_config).unwrap();

        let directory: DirectoryOnDisk =
            bincode::decode_from_std_read(&mut out_buf, bincode_config).unwrap();

        let dir = directory;

        let _parameters: Parameters =
            bincode::decode_from_std_read(&mut out_buf, bincode_config).unwrap();

        let _metadata: Metadata =
            bincode::decode_from_std_read(&mut out_buf, bincode_config).unwrap();

        let b: SequenceBlockCompressed =
            bincode::decode_from_std_read(&mut out_buf, bincode_config).unwrap();
        let mut zstd_decompressor = zstd::bulk::Decompressor::new().unwrap();
        zstd_decompressor.include_magicbytes(false).unwrap();

        let b = b.decompress(
            CompressionType::ZSTD,
            2 * 1024 * 1024,
            Some(zstd_decompressor).as_mut(),
        );

        assert!(b.len() == 8192);
    }
}