gen-models 0.4.0

Models for the gen sequence graph and version control system.
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
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
use core::hash::{Hash, Hasher};
use std::{io::BufReader, ops::Range, rc::Rc, str, sync};

use cached::proc_macro::cached;
use flate2::read::MultiGzDecoder;
use gen_core::{HashId, Sha256Hash, Workspace, traits::Capnp};
use indexmap::IndexMap;
use noodles::{
    bgzf::{self, gzi},
    core::Region,
    fasta::{self, fai, io::indexed_reader::Builder as IndexBuilder},
};
use rusqlite::{Result as SqlResult, Row, ToSql, params, types::Value};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use thiserror::Error;

use crate::{
    ModelSelect, ModelSelectError,
    assets::{AssetRef, AssetUri, LocalAssetUri},
    db::{GraphConnection, max_rows_per_batch},
    file_types::FileTypes,
    gen_models_capnp::sequence,
    select::ModelSelectRow,
};

#[derive(Clone, Debug, Deserialize, Serialize, ModelSelect)]
#[model_select(table = "sequences", from_row = sequence_from_row)]
pub struct Sequence {
    #[model_select(primary_key)]
    pub hash: Sha256Hash,
    pub sequence_type: String,
    sequence: String,
    // These fields are only relevant when the sequence is stored externally.
    pub name: String,
    pub asset_ref_id: Option<HashId>,
    pub length: i64,
    // Indicates whether the sequence is stored externally, a quick flag instead of checking the
    // sequence and asset reference in each caller.
    #[model_select(skip)]
    pub external_sequence: bool,
    #[serde(skip)]
    #[model_select(skip)]
    asset_ref: Option<AssetRef>,
    #[serde(skip)]
    #[model_select(skip)]
    index_asset_refs: Vec<AssetRef>,
    #[serde(skip)]
    #[model_select(skip)]
    workspace: Option<Workspace>,
    #[serde(skip)]
    #[model_select(skip)]
    asset_resolution_error: Option<String>,
}

fn sequence_from_row(row: &Row) -> SqlResult<Sequence> {
    let asset_ref_id: Option<HashId> = row.get(4)?;
    let hash: Sha256Hash = row.get(0)?;
    let sequence = row.get(2)?;
    Ok(Sequence {
        hash,
        sequence_type: row.get(1)?,
        sequence,
        name: row.get(3)?,
        asset_ref_id,
        length: row.get(5)?,
        external_sequence: asset_ref_id.is_some(),
        asset_ref: None,
        index_asset_refs: Vec::new(),
        workspace: None,
        asset_resolution_error: None,
    })
}

impl PartialEq for Sequence {
    fn eq(&self, other: &Self) -> bool {
        self.hash == other.hash
            && self.sequence_type == other.sequence_type
            && self.sequence == other.sequence
            && self.name == other.name
            && self.asset_ref_id == other.asset_ref_id
            && self.length == other.length
            && self.external_sequence == other.external_sequence
    }
}

impl Eq for Sequence {}

impl Hash for Sequence {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.hash.hash(state);
        self.sequence_type.hash(state);
        self.sequence.hash(state);
        self.name.hash(state);
        self.asset_ref_id.hash(state);
        self.length.hash(state);
        self.external_sequence.hash(state);
    }
}

#[derive(Debug, Error, PartialEq)]
pub enum SequenceError {
    #[error("Database error: {0}")]
    DatabaseError(#[from] rusqlite::Error),
    #[error("Selector error: {0}")]
    ModelSelect(#[from] ModelSelectError),
    #[error("Sequence or asset_ref_id must be set.")]
    NoSequence(),
    #[error("An asset reference must have an accompanying sequence name.")]
    AssetMissingSequenceName(),
    #[error("Sequence length must be specified.")]
    MissingSequenceLength(),
    #[error("Sequence cache lock poisoned: {0}")]
    CachePoisoned(String),
    #[error("Invalid indexed sequence region '{name}': {reason}")]
    InvalidRegion { name: String, reason: String },
    #[error("Sequence I/O error: {0}")]
    Io(String),
    #[error("Invalid UTF-8 while reading sequence data: {0}")]
    Utf8(String),
    #[error("Sequence id '{name}' not found in fasta file {file_path}")]
    IdMissing { file_path: String, name: String },
    #[error("Sequence bounds out of range: start={start}, end={end}, length={length}")]
    BoundsError { start: i64, end: i64, length: i64 },
    #[error("Unable to resolve sequence asset: {0}")]
    AssetResolution(String),
}

impl<'a> Capnp<'a> for Sequence {
    type Builder = sequence::Builder<'a>;
    type Reader = sequence::Reader<'a>;

    fn write_capnp(&self, builder: &mut Self::Builder) {
        builder.set_hash(&self.hash.0).unwrap();
        builder.set_sequence_type(&self.sequence_type);
        builder.set_sequence(&self.sequence);
        builder.set_name(&self.name);
        builder.set_length(self.length);
        builder.set_external_sequence(self.external_sequence);
        if let Some(asset_ref_id) = self.asset_ref_id {
            builder.set_asset_ref_id(&asset_ref_id.0).unwrap();
        }
    }

    fn read_capnp(reader: Self::Reader) -> Self {
        let hash = reader
            .get_hash()
            .unwrap()
            .as_slice()
            .unwrap()
            .try_into()
            .unwrap();
        let sequence_type = reader.get_sequence_type().unwrap().to_string().unwrap();
        let sequence = reader.get_sequence().unwrap().to_string().unwrap();
        let name = reader.get_name().unwrap().to_string().unwrap();
        let length = reader.get_length();
        let asset_ref_id = reader.get_asset_ref_id().ok().and_then(|value| {
            value
                .as_slice()
                .and_then(|bytes| bytes.try_into().ok())
                .map(HashId)
        });
        let external_sequence = asset_ref_id.is_some();

        Sequence {
            hash,
            sequence_type,
            sequence,
            name,
            asset_ref_id,
            length,
            external_sequence,
            asset_ref: None,
            index_asset_refs: Vec::new(),
            workspace: None,
            asset_resolution_error: None,
        }
    }
}

#[derive(Default, Debug)]
pub struct NewSequence<'a> {
    sequence_type: Option<&'a str>,
    sequence: Option<&'a str>,
    name: Option<&'a str>,
    asset_ref_id: Option<&'a HashId>,
    length: Option<i64>,
    shallow: bool,
}

impl<'a> From<&'a Sequence> for NewSequence<'a> {
    fn from(value: &'a Sequence) -> NewSequence<'a> {
        NewSequence::new()
            .sequence_type(&value.sequence_type)
            .sequence(&value.sequence)
            .name(&value.name)
            .asset_ref_id(value.asset_ref_id.as_ref())
            .length(value.length)
    }
}

impl<'a> NewSequence<'a> {
    pub fn new() -> NewSequence<'static> {
        NewSequence {
            shallow: false,
            ..NewSequence::default()
        }
    }

    pub fn shallow(mut self, setting: bool) -> Self {
        self.shallow = setting;
        self
    }

    pub fn sequence_type(mut self, seq_type: &'a str) -> Self {
        self.sequence_type = Some(seq_type);
        self
    }

    pub fn sequence(mut self, sequence: &'a str) -> Self {
        self.sequence = Some(sequence);
        self.length = Some(sequence.len() as i64);
        self
    }

    pub fn name(mut self, name: &'a str) -> Self {
        self.name = Some(name);
        self
    }

    pub fn asset_ref_id(mut self, asset_ref_id: Option<&'a HashId>) -> Self {
        if asset_ref_id.is_some() {
            self.asset_ref_id = asset_ref_id;
            self.shallow = true;
        }
        self
    }

    pub fn length(mut self, length: i64) -> Self {
        self.length = Some(length);
        self
    }

    pub fn hash(&self) -> Sha256Hash {
        let mut hasher = Sha256::new();
        hasher.update(self.sequence_type.expect("Sequence type must be defined."));
        hasher.update(";");
        if let Some(v) = self.sequence {
            hasher.update(v);
        } else {
            hasher.update("");
        }
        hasher.update(";");
        if let Some(v) = self.name {
            hasher.update(v);
        } else {
            hasher.update("");
        }
        hasher.update(";");
        if let Some(value) = self.asset_ref_id {
            hasher.update(value.0);
        } else {
            hasher.update("");
        }
        hasher.update(";");
        Sha256Hash(hasher.finalize().into())
    }

    pub fn build(self) -> Sequence {
        let external_sequence = self.asset_ref_id.is_some();
        Sequence {
            hash: self.hash(),
            sequence_type: self.sequence_type.unwrap().to_string(),
            sequence: self.sequence.unwrap_or("").to_string(),
            name: self.name.unwrap_or("").to_string(),
            asset_ref_id: self.asset_ref_id.copied(),
            length: self.length.unwrap(),
            external_sequence,
            asset_ref: None,
            index_asset_refs: Vec::new(),
            workspace: None,
            asset_resolution_error: None,
        }
    }

    #[cfg_attr(
        all(debug_assertions, feature = "profiling"),
        tracing::instrument(skip(self, conn))
    )]
    pub fn save(self, conn: &GraphConnection) -> Result<Sequence, SequenceError> {
        let mut length = 0;
        if self.sequence.is_none() && self.asset_ref_id.is_none() {
            return Err(SequenceError::NoSequence());
        }
        if self.asset_ref_id.is_some() && self.name.is_none() {
            return Err(SequenceError::AssetMissingSequenceName());
        }
        if self.length.is_none() {
            if let Some(v) = self.sequence {
                length = v.len() as i64;
            } else {
                // TODO: if name/path specified, grab length automatically
                return Err(SequenceError::MissingSequenceLength());
            }
        }
        let hash = self.hash();
        let existing = Sequence::select(conn)
            .hash(hash)
            .only(SequenceSelect::Hash)
            .load()?;
        if existing.is_empty() {
            let mut stmt = conn.prepare("INSERT INTO sequences (hash, sequence_type, sequence, name, asset_ref_id, length) VALUES (?1, ?2, ?3, ?4, ?5, ?6);")?;
            match stmt.execute(params![
                hash,
                self.sequence_type.unwrap().to_string(),
                if self.shallow {
                    ""
                } else {
                    self.sequence.unwrap()
                },
                self.name.unwrap_or(""),
                self.asset_ref_id,
                self.length.unwrap_or(length)
            ]) {
                Ok(_) => {}
                Err(err) => return Err(SequenceError::DatabaseError(err)),
            }
        }

        Ok(Sequence {
            hash,
            sequence_type: self.sequence_type.unwrap().to_string(),
            sequence: self.sequence.unwrap_or("").to_string(),
            name: self.name.unwrap_or("").to_string(),
            asset_ref_id: self.asset_ref_id.copied(),
            length: self.length.unwrap_or(length),
            external_sequence: self.asset_ref_id.is_some(),
            asset_ref: None,
            index_asset_refs: Vec::new(),
            workspace: None,
            asset_resolution_error: None,
        })
    }
}

#[cached(
    key = "String",
    convert = r#"{ format!("{}:{}", workspace.base_dir().display(), asset_ref.id) }"#
)]
fn fasta_index(workspace: &Workspace, asset_ref: &AssetRef) -> Option<fai::Index> {
    let reader = asset_ref.reader(workspace).ok()?;
    fai::io::Reader::new(reader).read_index().ok()
}

#[cached(
    key = "String",
    convert = r#"{ format!("{}:{}", workspace.base_dir().display(), asset_ref.id) }"#
)]
fn fasta_gzi_index(workspace: &Workspace, asset_ref: &AssetRef) -> Option<gzi::Index> {
    let reader = asset_ref.reader(workspace).ok()?;
    gzi::io::Reader::new(reader).read_index().ok()
}

fn asset_extension(asset_ref: &AssetRef) -> Option<String> {
    <dyn AssetUri>::from_uri(&asset_ref.uri)
        .suffix()
        .and_then(|suffix| suffix.rsplit('.').next().map(str::to_ascii_lowercase))
}

fn validate_sequence_bounds(
    start: i64,
    end: i64,
    length: i64,
) -> Result<(usize, usize), SequenceError> {
    if start < 0 || end < 0 || start > length || end > length {
        return Err(SequenceError::BoundsError { start, end, length });
    }

    Ok((start as usize, end as usize))
}

fn circular_sequence_slice(sequence: &str, start: i64, end: i64) -> Result<String, SequenceError> {
    let length = sequence.len() as i64;
    if length == 0 {
        if start == 0 && end == 0 {
            return Ok(String::new());
        }
        return Err(SequenceError::BoundsError { start, end, length });
    }

    let (start, end) = validate_sequence_bounds(start, end, length)?;
    if start <= end {
        return Ok(sequence[start..end].to_string());
    }

    let mut result = String::with_capacity((length as usize - start) + end);
    result.push_str(&sequence[start..]);
    result.push_str(&sequence[..end]);

    Ok(result)
}

fn sequence_slice(
    sequence: &str,
    start: i64,
    end: i64,
    circular: bool,
) -> Result<String, SequenceError> {
    if circular {
        return circular_sequence_slice(sequence, start, end);
    }

    let length = sequence.len() as i64;
    let (start, end) = validate_sequence_bounds(start, end, length)?;
    if start <= end {
        return Ok(sequence[start..end].to_string());
    }

    Err(SequenceError::BoundsError {
        start: start as i64,
        end: end as i64,
        length,
    })
}

type SequenceCache = sync::RwLock<Option<((HashId, String), Option<String>)>>;

pub fn cached_sequence(
    workspace: &Workspace,
    sequence_asset: &AssetRef,
    index_assets: &[AssetRef],
    name: &str,
    range: Range<i64>,
    circular: bool,
) -> Result<String, SequenceError> {
    static SEQUENCE_CACHE: sync::LazyLock<SequenceCache> =
        sync::LazyLock::new(|| sync::RwLock::new(None));
    let cache_key = (sequence_asset.id, name.to_string());

    {
        let cache = SEQUENCE_CACHE
            .read()
            .map_err(|err| SequenceError::CachePoisoned(err.to_string()))?;
        if let Some((cached_key, cached_sequence)) = cache.as_ref()
            && cached_key == &cache_key
        {
            if let Some(sequence) = cached_sequence {
                return sequence_slice(sequence, range.start, range.end, circular);
            }
            return Err(SequenceError::IdMissing {
                file_path: sequence_asset.uri.clone(),
                name: name.to_string(),
            });
        }
    }

    let mut cache = SEQUENCE_CACHE
        .write()
        .map_err(|err| SequenceError::CachePoisoned(err.to_string()))?;

    let mut sequence: Option<String> = None;
    let fasta_index_asset = index_assets
        .iter()
        .find(|asset_ref| asset_ref.file_type == FileTypes::FastaIndex.as_str())
        .or_else(|| {
            index_assets.iter().find(|asset_ref| {
                asset_ref.file_type == FileTypes::None.as_str()
                    && asset_extension(asset_ref).as_deref() == Some("fai")
            })
        });
    let gzip_index_asset = index_assets
        .iter()
        .find(|asset_ref| asset_ref.file_type == FileTypes::BgzfIndex.as_str())
        .or_else(|| {
            index_assets.iter().find(|asset_ref| {
                asset_ref.file_type == FileTypes::None.as_str()
                    && asset_extension(asset_ref).as_deref() == Some("gzi")
            })
        });
    let sequence_extension = asset_extension(sequence_asset);
    let compressed = matches!(sequence_extension.as_deref(), Some("gz" | "bgz"));
    if let Some(index) = fasta_index_asset.and_then(|asset_ref| fasta_index(workspace, asset_ref)) {
        let region = name
            .parse::<Region>()
            .map_err(|err| SequenceError::InvalidRegion {
                name: name.to_string(),
                reason: err.to_string(),
            })?;
        let builder = IndexBuilder::default().set_index(index);
        if let Some(gzi_index) =
            gzip_index_asset.and_then(|asset_ref| fasta_gzi_index(workspace, asset_ref))
        {
            let sequence_reader = sequence_asset
                .reader(workspace)
                .map_err(|err| SequenceError::Io(err.to_string()))?;
            let bgzf_reader = bgzf::io::indexed_reader::Builder::default()
                .set_index(gzi_index)
                .build_from_reader(sequence_reader)
                .map_err(|err| SequenceError::Io(err.to_string()))?;
            let mut reader = builder
                .build_from_reader(bgzf_reader)
                .map_err(|err| SequenceError::Io(err.to_string()))?;
            sequence = Some(
                str::from_utf8(
                    reader
                        .query(&region)
                        .map_err(|err| SequenceError::Io(err.to_string()))?
                        .sequence()
                        .as_ref(),
                )
                .map_err(|err| SequenceError::Utf8(err.to_string()))?
                .to_string(),
            )
        } else if !compressed {
            let sequence_reader = sequence_asset
                .reader(workspace)
                .map_err(|err| SequenceError::Io(err.to_string()))?;
            let mut reader = builder
                .build_from_reader(sequence_reader)
                .map_err(|err| SequenceError::Io(err.to_string()))?;
            sequence = Some(
                str::from_utf8(
                    reader
                        .query(&region)
                        .map_err(|err| SequenceError::Io(err.to_string()))?
                        .sequence()
                        .as_ref(),
                )
                .map_err(|err| SequenceError::Utf8(err.to_string()))?
                .to_string(),
            );
        }
    }
    if sequence.is_none() {
        let sequence_reader = sequence_asset
            .reader(workspace)
            .map_err(|err| SequenceError::Io(err.to_string()))?;
        let reader_stream: Box<dyn std::io::BufRead> = match sequence_extension.as_deref() {
            Some("gz") => Box::new(BufReader::new(MultiGzDecoder::new(sequence_reader))),
            Some("bgz") => Box::new(bgzf::io::Reader::new(sequence_reader)),
            _ => Box::new(sequence_reader),
        };
        let mut reader = fasta::io::reader::Builder
            .build_from_reader(reader_stream)
            .map_err(|err| SequenceError::Io(err.to_string()))?;
        for result in reader.records() {
            let record = result.map_err(|err| SequenceError::Io(err.to_string()))?;
            if String::from_utf8(record.name().to_vec())
                .map_err(|err| SequenceError::Utf8(err.to_string()))?
                == name
            {
                sequence = Some(
                    str::from_utf8(record.sequence().as_ref())
                        .map_err(|err| SequenceError::Utf8(err.to_string()))?
                        .to_string(),
                );
                break;
            }
        }
    }
    // This is an LRU cache setup; keep only the last sequence fetched so loading multiple plant
    // genomes does not retain every genome in memory.
    *cache = Some((cache_key, sequence));
    // we do this to avoid a clone of potentially large data.
    if let Some((_, Some(seq))) = cache.as_ref() {
        return sequence_slice(seq, range.start, range.end, circular);
    }
    Err(SequenceError::IdMissing {
        file_path: sequence_asset.uri.clone(),
        name: name.to_string(),
    })
}

impl Sequence {
    /// Saves sequences in bounded batches, retaining any row already stored under the same hash.
    #[cfg_attr(feature = "profiling", tracing::instrument(skip(conn, sequences)))]
    pub fn bulk_save(conn: &GraphConnection, sequences: &[Sequence]) -> Result<(), SequenceError> {
        let batch_size = max_rows_per_batch(conn, 6);

        for chunk in sequences.chunks(batch_size) {
            let mut sql = String::from(
                "INSERT OR IGNORE INTO sequences
                 (hash, sequence_type, sequence, name, asset_ref_id, length) VALUES ",
            );
            for row_index in 0..chunk.len() {
                if row_index > 0 {
                    sql.push(',');
                }
                sql.push_str("(?, ?, ?, ?, ?, ?)");
            }
            sql.push(';');

            let mut values = Vec::with_capacity(chunk.len() * 6);
            for sequence in chunk {
                values.push(Value::from(sequence.hash));
                values.push(Value::from(sequence.sequence_type.clone()));
                values.push(Value::from(sequence.sequence.clone()));
                values.push(Value::from(sequence.name.clone()));
                values.push(Value::from(sequence.asset_ref_id));
                values.push(Value::from(sequence.length));
            }
            let mut statement = conn.prepare_cached(&sql)?;
            statement.execute(rusqlite::params_from_iter(values))?;
        }

        Ok(())
    }

    #[allow(clippy::new_ret_no_self)]
    pub fn new() -> NewSequence<'static> {
        NewSequence::new()
    }

    fn is_circular(&self) -> bool {
        self.sequence_type
            .split_whitespace()
            .any(|part| part.eq_ignore_ascii_case("circular"))
    }

    pub fn query_by_ids<T>(
        conn: &GraphConnection,
        workspace: &Workspace,
        ids: &[T],
        history_ref: Option<&str>,
    ) -> Vec<Self>
    where
        T: Clone,
        rusqlite::types::Value: From<T>,
    {
        let sequence_table = Self::table_name_with_history_ref(history_ref);
        let asset_table = AssetRef::table_name_with_history_ref(history_ref);
        let query = format!(
            "WITH arr AS (
                SELECT value, rowid AS pos FROM rarray(:ids)
             )
             SELECT sequences.*, asset_refs.*, index_assets.*
             FROM {sequence_table} AS sequences
             LEFT JOIN {asset_table} AS asset_refs
               ON sequences.asset_ref_id = asset_refs.id
             LEFT JOIN {asset_table} AS index_assets
               ON asset_refs.id = index_assets.upstream_asset_ref_id
              AND index_assets.role = 'sequence-index'
             JOIN arr ON sequences.hash = arr.value
             ORDER BY arr.pos;"
        );
        let values: Vec<Value> = ids.iter().map(|id| Value::from(id.clone())).collect();
        let id_values = Rc::new(values);
        let mut query_params: Vec<(&str, &dyn ToSql)> = vec![(":ids", &id_values)];
        if let Some(history_ref) = history_ref.as_ref() {
            query_params.push((":history_ref", history_ref));
        }
        let mut statement = conn.prepare(&query).unwrap();
        let mut sequences: IndexMap<Sha256Hash, Self> = IndexMap::new();
        for row in statement
            .query_map(&query_params[..], |row| {
                Self::process_joined_row(row, workspace)
            })
            .unwrap()
        {
            let sequence = row.unwrap();
            let sequence_hash = sequence.hash;
            if let Some(existing) = sequences.get_mut(&sequence_hash) {
                existing.index_asset_refs.extend(sequence.index_asset_refs);
            } else {
                sequences.insert(sequence_hash, sequence);
            }
        }
        sequences.into_values().collect()
    }

    fn process_joined_row(row: &Row, workspace: &Workspace) -> SqlResult<Self> {
        let mut sequence = <Self as ModelSelectRow>::process_row(row)?;
        if let Some(asset_ref_id) = sequence.asset_ref_id {
            let Some(joined_asset_ref_id): Option<HashId> = row.get(6)? else {
                sequence.asset_resolution_error =
                    Some(format!("asset reference {asset_ref_id} does not exist"));
                return Ok(sequence);
            };
            let asset_ref = AssetRef {
                id: joined_asset_ref_id,
                uri: row.get(7)?,
                file_type: row.get(8)?,
                checksum: row.get(9)?,
                size: row.get(10)?,
                role: row.get(11)?,
                logical_path: row.get(12)?,
                name: row.get(13)?,
                created_on: row.get(14)?,
                upstream_asset_ref_id: row.get(15)?,
                materialized_checksum: row.get(16)?,
            };
            if LocalAssetUri::is_local_path_or_file_uri(&asset_ref.uri)
                && let Err(error) = asset_ref.versioned_store_path(workspace)
            {
                sequence.asset_resolution_error = Some(error.to_string());
                return Ok(sequence);
            }
            sequence.asset_ref = Some(asset_ref);
            sequence.workspace = Some(workspace.clone());

            let index_asset_ref_id: Option<HashId> = row.get(17)?;
            if let Some(index_asset_ref_id) = index_asset_ref_id {
                let index_asset = AssetRef {
                    id: index_asset_ref_id,
                    uri: row.get(18)?,
                    file_type: row.get(19)?,
                    checksum: row.get(20)?,
                    size: row.get(21)?,
                    role: row.get(22)?,
                    logical_path: row.get(23)?,
                    name: row.get(24)?,
                    created_on: row.get(25)?,
                    upstream_asset_ref_id: row.get(26)?,
                    materialized_checksum: row.get(27)?,
                };
                if !LocalAssetUri::is_local_path_or_file_uri(&index_asset.uri)
                    || index_asset.versioned_store_path(workspace).is_ok()
                {
                    sequence.index_asset_refs.push(index_asset);
                }
            }
        }
        Ok(sequence)
    }

    pub fn get_sequence(
        &self,
        start: impl Into<Option<i64>>,
        end: impl Into<Option<i64>>,
    ) -> Result<String, SequenceError> {
        // todo: handle circles

        let start: Option<i64> = start.into();
        let end: Option<i64> = end.into();
        let start = start.unwrap_or(0);
        let end = end.unwrap_or(self.length);
        if self.external_sequence {
            if let Some(error) = self.asset_resolution_error.as_ref() {
                return Err(SequenceError::AssetResolution(error.clone()));
            }
            let asset_ref = self.asset_ref.as_ref().ok_or_else(|| {
                SequenceError::AssetResolution(
                    "sequence asset was loaded without repository context".to_string(),
                )
            })?;
            let workspace = self.workspace.as_ref().ok_or_else(|| {
                SequenceError::AssetResolution(
                    "sequence asset was loaded without repository context".to_string(),
                )
            })?;
            return cached_sequence(
                workspace,
                asset_ref,
                &self.index_asset_refs,
                &self.name,
                start..end,
                self.is_circular(),
            );
        }
        if start == 0 && end == self.length {
            return Ok(self.sequence.clone());
        }
        sequence_slice(&self.sequence, start, end, self.is_circular())
    }

    pub fn delete_by_hash(conn: &GraphConnection, hash: &Sha256Hash) {
        let mut stmt = conn
            .prepare("delete from sequences where hash = ?1;")
            .unwrap();
        stmt.execute(params![hash]).unwrap();
    }

    pub fn query_by_blockgroup(
        conn: &GraphConnection,
        workspace: &Workspace,
        block_group_id: &HashId,
    ) -> Vec<Sequence> {
        let asset_table = AssetRef::table_name_with_history_ref(None);
        let query = format!(
            "SELECT sequences.*, asset_refs.*, index_assets.*
             FROM block_group_edges bge
             LEFT JOIN edges ON bge.edge_id = edges.id
             LEFT JOIN nodes ON (edges.source_node_id = nodes.id OR edges.target_node_id = nodes.id)
             LEFT JOIN sequences ON (nodes.sequence_hash = sequences.hash)
             LEFT JOIN {asset_table} AS asset_refs ON sequences.asset_ref_id = asset_refs.id
             LEFT JOIN {asset_table} AS index_assets
               ON asset_refs.id = index_assets.upstream_asset_ref_id
              AND index_assets.role = 'sequence-index'
             WHERE bge.block_group_id = ?1;"
        );
        let mut statement = conn.prepare(&query).unwrap();
        let mut sequences: IndexMap<Sha256Hash, Self> = IndexMap::new();
        for row in statement
            .query_map(params![block_group_id], |row| {
                Self::process_joined_row(row, workspace)
            })
            .unwrap()
        {
            let sequence = row.unwrap();
            let sequence_hash = sequence.hash;
            if let Some(existing) = sequences.get_mut(&sequence_hash) {
                existing.index_asset_refs.extend(sequence.index_asset_refs);
            } else {
                sequences.insert(sequence_hash, sequence);
            }
        }
        sequences.into_values().collect()
    }
}

pub fn reverse_complement(seq: &[u8]) -> Vec<u8> {
    seq.iter()
        .rev()
        .map(|&base| match base.to_ascii_uppercase() {
            b'A' => b'T',
            b'T' => b'A',
            b'C' => b'G',
            b'G' => b'C',
            b'U' => b'A',
            b'N' => b'N',
            b'R' => b'Y',
            b'Y' => b'R',
            b'S' => b'S',
            b'W' => b'W',
            b'K' => b'M',
            b'M' => b'K',
            b'B' => b'V',
            b'V' => b'B',
            b'D' => b'H',
            b'H' => b'D',
            _ => base,
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use std::{fs, fs::OpenOptions, io::Write, time};

    use gen_core::traits::Capnp as _;
    use rand::RngExt;
    use sha2::Digest as _;

    use super::{Sequence, SequenceError};
    use crate::{
        assets::{AssetRef, AssetRole},
        file_types::FileTypes,
        gen_models_capnp::sequence,
        operations::{FileAddition, OperationFile},
        test_helpers::{get_connection, setup_gen_on_disk},
    };

    fn prepare_asset(context: &crate::db::DbContext, path: &str, role: AssetRole) -> AssetRef {
        let operation_file = OperationFile::new(path).set_role(role);
        let asset_ref = operation_file
            .prepare_asset_ref(context.workspace(), 1)
            .expect("should retain test asset");
        AssetRef::create(context.graph().conn(), &asset_ref)
            .expect("should create test asset reference");
        asset_ref
    }

    fn prepare_derived_asset(
        context: &crate::db::DbContext,
        path: &str,
        role: AssetRole,
        upstream_asset_ref_id: &gen_core::HashId,
    ) -> AssetRef {
        let operation_file = OperationFile::new(path)
            .set_role(role)
            .set_upstream_asset_ref_id(upstream_asset_ref_id);
        let asset_ref = operation_file
            .prepare_asset_ref(context.workspace(), 1)
            .expect("should retain derived test asset");
        AssetRef::create(context.graph().conn(), &asset_ref)
            .expect("should create derived test asset reference");
        asset_ref
    }

    #[test]
    fn test_builder() {
        let sequence = Sequence::new()
            .sequence_type("DNA")
            .sequence("ATCG")
            .build();
        assert_eq!(sequence.length, 4);
        assert_eq!(sequence.sequence, "ATCG");
        assert_eq!(
            sequence.hash.to_string(),
            "4ab41cf208e5a797ed0052a3fc5f87bed18f94fc1fc7fdd6ed3199b4e1c34ace"
        );
    }

    #[test]
    fn test_builder_with_asset_ref() {
        let asset_ref_id = gen_core::HashId::convert_str("asset");
        let sequence = Sequence::new()
            .sequence_type("DNA")
            .name("chr1")
            .asset_ref_id(Some(&asset_ref_id))
            .length(50)
            .build();
        assert_eq!(sequence.length, 50);
        assert_eq!(sequence.sequence, "");
    }

    #[test]
    fn test_create_sequence_in_db() {
        let conn = &get_connection(None).unwrap();
        let sequence = Sequence::new()
            .sequence_type("DNA")
            .sequence("AACCTT")
            .save(conn)
            .unwrap();
        assert_eq!(&sequence.sequence, "AACCTT");
        assert_eq!(sequence.sequence_type, "DNA");
        assert!(!sequence.external_sequence);
    }

    #[test]
    fn test_delete_sequence_by_hash() {
        let conn = &get_connection(None).unwrap();
        let before_count = Sequence::all(conn).expect("should load sequences").len();
        let sequence = Sequence::new()
            .sequence_type("DNA")
            .sequence("AACCTT")
            .save(conn)
            .unwrap();
        let sequence2 = Sequence::new()
            .sequence_type("DNA")
            .sequence("AACCTTAA")
            .save(conn)
            .unwrap();

        let sequences = Sequence::all(conn).expect("should load sequences");
        assert_eq!(sequences.len(), before_count + 2);

        Sequence::delete_by_hash(conn, &sequence.hash);

        let sequences = Sequence::all(conn).expect("should load sequences");
        assert_eq!(sequences.len(), before_count + 1);
        assert!(sequences.iter().any(|s| s.hash == sequence2.hash));
    }

    #[test]
    fn test_create_sequence_on_disk() {
        let context = setup_gen_on_disk();
        let fasta_path = context
            .workspace()
            .repo_root()
            .unwrap()
            .join("reference.fa");
        fs::write(&fasta_path, ">chr1\nAACCGGTTAA\n").unwrap();
        let asset_ref = prepare_asset(&context, fasta_path.to_str().unwrap(), AssetRole::Input);
        let sequence = Sequence::new()
            .sequence_type("DNA")
            .name("chr1")
            .asset_ref_id(Some(&asset_ref.id))
            .length(10)
            .save(context.graph().conn())
            .unwrap();
        assert_eq!(sequence.sequence_type, "DNA");
        assert_eq!(&sequence.sequence, "");
        assert_eq!(sequence.name, "chr1");
        assert_eq!(sequence.asset_ref_id, Some(asset_ref.id));
        assert_eq!(sequence.length, 10);
        assert!(sequence.external_sequence);
    }

    #[test]
    fn test_get_sequence() {
        let conn = &get_connection(None).unwrap();
        let sequence = Sequence::new()
            .sequence_type("DNA")
            .sequence("ATCGATCGATCGATCGATCGGGAACACACAGAGA")
            .save(conn)
            .unwrap();
        assert_eq!(
            sequence.get_sequence(None, None).unwrap(),
            "ATCGATCGATCGATCGATCGGGAACACACAGAGA"
        );
        assert_eq!(sequence.get_sequence(0, 5).unwrap(), "ATCGA");
        assert_eq!(sequence.get_sequence(10, 15).unwrap(), "CGATC");
        assert_eq!(
            sequence.get_sequence(3, None).unwrap(),
            "GATCGATCGATCGATCGGGAACACACAGAGA"
        );
        assert_eq!(sequence.get_sequence(None, 5).unwrap(), "ATCGA");
        assert_eq!(
            sequence.get_sequence(0, 100),
            Err(SequenceError::BoundsError {
                start: 0,
                end: 100,
                length: 34,
            })
        );
        assert_eq!(
            sequence.get_sequence(5, 2),
            Err(SequenceError::BoundsError {
                start: 5,
                end: 2,
                length: 34,
            })
        );
    }

    #[test]
    fn test_get_sequence_circular() {
        let conn = &get_connection(None).unwrap();
        let sequence = Sequence::new()
            .sequence_type("circular")
            .sequence("AAACCCTTT")
            .save(conn)
            .unwrap();
        assert_eq!(sequence.get_sequence(4, 2).unwrap(), "CCTTTAA");
        assert_eq!(
            sequence.get_sequence(0, 10),
            Err(SequenceError::BoundsError {
                start: 0,
                end: 10,
                length: 9,
            })
        );
    }

    #[test]
    fn test_get_sequence_from_disk() {
        let context = setup_gen_on_disk();
        let temp_file_path = context.workspace().repo_root().unwrap().join("simple.fa");
        fs::write(
            &temp_file_path,
            ">m123\nATCGATCGATCGATCGATCGGGAACACACAGAGA\n",
        )
        .unwrap();
        let asset_ref = prepare_asset(&context, temp_file_path.to_str().unwrap(), AssetRole::Input);
        let sequence = Sequence::new()
            .sequence_type("DNA")
            .name("m123")
            .asset_ref_id(Some(&asset_ref.id))
            .length(34)
            .save(context.graph().conn())
            .unwrap();
        let seq = Sequence::query_by_ids(
            context.graph().conn(),
            context.workspace(),
            &[sequence.hash],
            None,
        )
        .remove(0);
        // Overwrite temp_file_path with a garbage sequence, showing that sequence access uses the versioned file
        // and not any local materialized file.
        fs::write(
            &temp_file_path,
            ">m123\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n",
        )
        .unwrap();
        assert_eq!(
            seq.get_sequence(None, None).unwrap(),
            "ATCGATCGATCGATCGATCGGGAACACACAGAGA"
        );
        assert_eq!(seq.get_sequence(0, 5).unwrap(), "ATCGA");
        assert_eq!(seq.get_sequence(10, 15).unwrap(), "CGATC");
        assert_eq!(
            seq.get_sequence(3, None).unwrap(),
            "GATCGATCGATCGATCGGGAACACACAGAGA"
        );
        assert_eq!(seq.get_sequence(None, 5).unwrap(), "ATCGA");
    }

    #[cfg(unix)]
    #[test]
    fn test_on_disk_sequence_rejects_asset_outside_repository() {
        use std::os::unix::fs::symlink;

        let context = setup_gen_on_disk();
        let outside_directory = tempfile::tempdir().unwrap();
        let outside_path = outside_directory.path().join("secret.fa");
        let contents = b">secret\nPRIVATE\n";
        fs::write(&outside_path, contents).unwrap();
        let checksum = gen_core::Sha256Hash(sha2::Sha256::digest(contents).into());
        let uri = format!("file://{}", outside_path.display());
        let file_addition = FileAddition {
            id: gen_core::HashId::convert_str("outside-secret"),
            asset_uri: uri.clone(),
            file_type: FileTypes::Fasta,
            checksum: Some(checksum),
            materialized_checksum: None,
        };
        let asset_ref = AssetRef {
            id: AssetRef::id_hash(
                &file_addition,
                &AssetRole::Input,
                outside_path.to_str(),
                Some("secret.fa"),
                None,
            ),
            uri,
            file_type: "fasta".to_string(),
            checksum: Some(checksum),
            size: None,
            role: AssetRole::Input,
            logical_path: Some(outside_path.to_string_lossy().to_string()),
            name: Some("secret.fa".to_string()),
            created_on: 1,
            upstream_asset_ref_id: None,
            materialized_checksum: None,
        };
        AssetRef::create(context.graph().conn(), &asset_ref).unwrap();
        let asset_filename =
            <dyn crate::assets::AssetUri>::from_uri(&asset_ref.uri).hashed_filename(&checksum);
        symlink(
            &outside_path,
            context
                .workspace()
                .asset_dir()
                .unwrap()
                .join(asset_filename),
        )
        .unwrap();
        let unresolved_sequence = Sequence::new()
            .sequence_type("DNA")
            .name("secret")
            .asset_ref_id(Some(&asset_ref.id))
            .length(7)
            .save(context.graph().conn())
            .unwrap();
        let sequence = Sequence::query_by_ids(
            context.graph().conn(),
            context.workspace(),
            &[unresolved_sequence.hash],
            None,
        )
        .remove(0);

        let error = sequence
            .get_sequence(None, None)
            .expect_err("should not read an asset outside the repository");
        assert!(
            matches!(&error, SequenceError::AssetResolution(reason) if reason.contains("not within repo root")),
            "should report the repository boundary: {error}"
        );
    }

    #[test]
    fn test_get_sequence_from_disk_circular() {
        let context = setup_gen_on_disk();
        let temp_file_path = context.workspace().repo_root().unwrap().join("simple.fa");
        fs::write(&temp_file_path, ">m123\nAAACCCTTT\n").unwrap();
        let asset_ref = prepare_asset(&context, temp_file_path.to_str().unwrap(), AssetRole::Input);
        let sequence = Sequence::new()
            .sequence_type("circular")
            .name("m123")
            .asset_ref_id(Some(&asset_ref.id))
            .length(9)
            .save(context.graph().conn())
            .unwrap();
        let seq = Sequence::query_by_ids(
            context.graph().conn(),
            context.workspace(),
            &[sequence.hash],
            None,
        )
        .remove(0);
        assert_eq!(seq.get_sequence(4, 2).unwrap(), "CCTTTAA");
        assert_eq!(
            seq.get_sequence(0, 10),
            Err(SequenceError::BoundsError {
                start: 0,
                end: 10,
                length: 9,
            })
        );
    }

    #[test]
    fn test_on_disk_sequence_uses_retained_index_asset() {
        let context = setup_gen_on_disk();
        let repo_root = context.workspace().repo_root().unwrap();
        let fasta_path = repo_root.join("indexed.fa");
        let index_path = repo_root.join("indexed.fa.fai");
        fs::write(&fasta_path, ">chr1\nACGTACGT\n").unwrap();
        fs::write(&index_path, "chr1\t8\t6\t8\t9\n").unwrap();
        let asset_ref = prepare_asset(&context, fasta_path.to_str().unwrap(), AssetRole::Input);
        let index_asset_ref = prepare_derived_asset(
            &context,
            index_path.to_str().unwrap(),
            AssetRole::SequenceIndex,
            &asset_ref.id,
        );
        let unresolved_sequence = Sequence::new()
            .sequence_type("DNA")
            .name("chr1")
            .asset_ref_id(Some(&asset_ref.id))
            .length(8)
            .save(context.graph().conn())
            .unwrap();
        let sequence = Sequence::query_by_ids(
            context.graph().conn(),
            context.workspace(),
            &[unresolved_sequence.hash],
            None,
        )
        .remove(0);
        fs::remove_file(&fasta_path).unwrap();
        fs::remove_file(&index_path).unwrap();

        assert_eq!(
            sequence.get_sequence(2, 6).unwrap(),
            "GTAC",
            "should read the retained sequence through its index"
        );
        assert!(
            sequence
                .index_asset_refs
                .iter()
                .any(|asset_ref| asset_ref.id == index_asset_ref.id),
            "resolved sequence should include its retained index AssetRef"
        );
        assert_eq!(
            index_asset_ref.upstream_asset_ref_id,
            Some(asset_ref.id),
            "index AssetRef should link to the immutable sequence AssetRef"
        );
    }

    #[test]
    // #[cfg(feature = "benchmark")]
    fn test_cached_sequence_performance() {
        let context = setup_gen_on_disk();
        let temp_file_path = context.workspace().repo_root().unwrap().join("large.fa");
        let mut file = OpenOptions::new()
            .append(true)
            .create(true)
            .open(&temp_file_path)
            .unwrap();
        writeln!(file, ">chr22").unwrap();
        for _ in 1..3_000_000 {
            writeln!(
                file,
                "ATCGATCGATCGATCGATCGGGAACACACAGAGAATCGATCGATCGATCGATCGGGAACACACAGAGA"
            )
            .unwrap();
        }
        // write index
        let index_path = context
            .workspace()
            .repo_root()
            .unwrap()
            .join("large.fa.fai");
        fs::write(&index_path, "chr22	203999932	7	68	69\n").unwrap();
        let asset_ref = prepare_asset(&context, temp_file_path.to_str().unwrap(), AssetRole::Input);
        prepare_derived_asset(
            &context,
            index_path.to_str().unwrap(),
            AssetRole::SequenceIndex,
            &asset_ref.id,
        );
        let unresolved_sequence = Sequence::new()
            .sequence_type("DNA")
            .asset_ref_id(Some(&asset_ref.id))
            .name("chr22")
            .length(203_999_932)
            .save(context.graph().conn())
            .unwrap();
        let sequence = Sequence::query_by_ids(
            context.graph().conn(),
            context.workspace(),
            &[unresolved_sequence.hash],
            None,
        )
        .remove(0);
        // The first read loads the complete BGZF contig into the sequence cache; keep cold-load
        // cost out of this repeated-access timing.
        assert_eq!(
            sequence.get_sequence(0, 20).unwrap(),
            "ATCGATCGATCGATCGATCG"
        );
        let s = time::Instant::now();
        for _ in 1..1_000_000 {
            let start = rand::rng().random_range(1..200_000_000);

            sequence.get_sequence(start, start + 20).unwrap();
        }
        let elapsed = s.elapsed().as_secs();
        assert!(
            elapsed < 5,
            "Cached sequence benchmark failed: {elapsed}s elapsed"
        );
    }

    #[test]
    fn test_capnp_serialization() {
        use capnp::message::TypedBuilder;

        let sequence = Sequence {
            hash: gen_core::Sha256Hash::convert_str("test_hash"),
            sequence_type: "DNA".to_string(),
            sequence: "ATCG".to_string(),
            name: "test_seq".to_string(),
            asset_ref_id: Some(gen_core::HashId::convert_str("asset")),
            length: 4,
            external_sequence: true,
            asset_ref: None,
            index_asset_refs: Vec::new(),
            workspace: None,
            asset_resolution_error: None,
        };

        let mut message = TypedBuilder::<sequence::Owned>::new_default();
        let mut root = message.init_root();
        sequence.write_capnp(&mut root);

        let deserialized = Sequence::read_capnp(root.into_reader());
        assert_eq!(sequence, deserialized);
    }
}