gen 0.1.23

A 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
use std::{
    collections::{BTreeSet, HashMap, HashSet},
    fs::File,
    io::{BufWriter, Write},
    path::PathBuf,
};

use gen_core::{HashId, is_terminal, strand::Strand};
use gen_graph::{GenGraph, project_path};
use gen_models::{
    block_group::BlockGroup, block_group_edge::BlockGroupEdge, collection::Collection,
    db::GraphConnection, edge::Edge, path::Path, sample::Sample,
};
use itertools::Itertools;
use thiserror::Error;

use crate::gfa::{Link, Path as GFAPath, Segment, path_line, write_links, write_segments};

#[derive(Debug, Error)]
pub enum GfaExportError {
    #[error("I/O error while exporting GFA: {0}")]
    Io(#[from] std::io::Error),
}

pub fn export_gfa(
    conn: &GraphConnection,
    collection_name: &str,
    filename: &PathBuf,
    sample_name: Option<String>,
    max_size: impl Into<Option<i64>>,
) -> Result<(), GfaExportError> {
    let chunk_size = max_size.into().unwrap_or(i64::MAX);
    // General note about how we encode segment IDs.  The node ID and the start coordinate in the
    // sequence are all that's needed, because the end coordinate can be inferred from the length of
    // the segment's sequence.  So the segment ID is of the form <node ID>.<start coordinate>
    let block_groups = Collection::get_block_groups(conn, collection_name);

    let mut edge_set = HashSet::new();
    if let Some(sample) = sample_name.as_deref() {
        let sample_block_groups = Sample::get_block_groups(conn, collection_name, Some(sample));
        if sample_block_groups.is_empty() {
            panic!("No block groups found for collection {collection_name} and sample {sample}");
        }
        for block_group in sample_block_groups {
            let block_group_edges = BlockGroupEdge::edges_for_block_group(conn, &block_group.id);
            edge_set.extend(block_group_edges);
        }
    } else {
        for block_group in block_groups {
            let block_group_edges = BlockGroupEdge::edges_for_block_group(conn, &block_group.id);
            edge_set.extend(block_group_edges);
        }
    }

    let edges = edge_set.into_iter().collect::<Vec<_>>();

    let mut blocks = Edge::blocks_from_edges(conn, &edges);
    blocks.sort_by(|a, b| a.node_id.cmp(&b.node_id));

    let (gen_graph, _edges_by_node_pair) = Edge::build_graph(&edges, &blocks);

    // Create GenGraph from the built graph
    let mut graph = GenGraph::new();
    graph.extend(
        gen_graph
            .all_edges()
            .map(|(src, dest, weight)| (src, dest, weight.clone())),
    );

    let file = File::create(filename)?;
    let mut writer = BufWriter::new(file);

    let mut segments = BTreeSet::new();
    let mut split_segments = HashMap::new();
    for block in &blocks {
        if !is_terminal(block.node_id) {
            if block.end - block.start > chunk_size {
                let mut sub_segments = vec![];
                let block_sequence = block.sequence();
                for (index, sub_start) in (block.start..block.end)
                    .step_by(chunk_size as usize)
                    .enumerate()
                {
                    let sub_end = (sub_start + chunk_size).min(block.end);
                    let seq_start = index as i64 * chunk_size;
                    let seq_end =
                        ((index as i64 + 1) * chunk_size).min(block_sequence.len() as i64);
                    segments.insert(Segment {
                        sequence: block_sequence[seq_start as usize..seq_end as usize].to_string(),
                        node_id: block.node_id,
                        sequence_start: sub_start,
                        sequence_end: sub_end,
                        // NOTE: We can't easily get the value for strand, but it doesn't matter
                        // because this value is only used for writing segments
                        strand: Strand::Forward,
                    });
                    sub_segments.push((sub_start, sub_end));
                }
                split_segments.insert(block.node_id, sub_segments);
            } else {
                segments.insert(Segment {
                    sequence: block.sequence(),
                    node_id: block.node_id,
                    sequence_start: block.start,
                    sequence_end: block.end,
                    // NOTE: We can't easily get the value for strand, but it doesn't matter
                    // because this value is only used for writing segments
                    strand: Strand::Forward,
                });
            }
        }
    }

    let mut links = BTreeSet::new();
    for (source, target, edge_info) in graph.all_edges() {
        if !is_terminal(source.node_id) && !is_terminal(target.node_id) {
            let source_segment = if let Some(splits) = split_segments.get(&source.node_id) {
                let last_split = splits.last().unwrap();
                Segment {
                    sequence: "".to_string(),
                    node_id: source.node_id,
                    sequence_start: last_split.0,
                    sequence_end: last_split.1,
                    strand: edge_info[0].source_strand,
                }
            } else {
                Segment {
                    sequence: "".to_string(),
                    node_id: source.node_id,
                    sequence_start: source.sequence_start,
                    sequence_end: source.sequence_end,
                    strand: edge_info[0].source_strand,
                }
            };

            let target_segment = if let Some(splits) = split_segments.get(&target.node_id) {
                let first_split = splits.first().unwrap();
                Segment {
                    sequence: "".to_string(),
                    node_id: target.node_id,
                    sequence_start: first_split.0,
                    sequence_end: first_split.1,
                    strand: edge_info[0].source_strand,
                }
            } else {
                Segment {
                    sequence: "".to_string(),
                    node_id: target.node_id,
                    sequence_start: target.sequence_start,
                    sequence_end: target.sequence_end,
                    strand: edge_info[0].target_strand,
                }
            };

            links.insert(Link {
                source_segment_id: source_segment.segment_id(),
                source_strand: edge_info[0].source_strand,
                target_segment_id: target_segment.segment_id(),
                target_strand: edge_info[0].target_strand,
            });
        }
    }

    for (node_id, splits) in split_segments.iter() {
        for ((src_start, src_end), (dst_start, dst_end)) in splits.iter().tuple_windows() {
            let left = Segment {
                sequence: "".to_string(),
                node_id: *node_id,
                sequence_start: *src_start,
                sequence_end: *src_end,
                strand: Strand::Forward,
            };
            let right = Segment {
                sequence: "".to_string(),
                node_id: *node_id,
                sequence_start: *dst_start,
                sequence_end: *dst_end,
                strand: Strand::Forward,
            };
            links.insert(Link {
                source_segment_id: left.segment_id(),
                source_strand: Strand::Forward,
                target_segment_id: right.segment_id(),
                target_strand: Strand::Forward,
            });
        }
    }

    let paths = get_paths(conn, collection_name, sample_name, &graph, &split_segments);
    write_segments(&mut writer, &segments.iter().collect::<Vec<&Segment>>())?;
    write_links(&mut writer, &links.iter().collect::<Vec<&Link>>())?;
    write_paths(&mut writer, paths)?;

    Ok(())
}

fn get_paths(
    conn: &GraphConnection,
    collection_name: &str,
    sample_name: Option<String>,
    graph: &GenGraph,
    split_segments: &HashMap<HashId, Vec<(i64, i64)>>,
) -> HashMap<String, Vec<(String, Strand)>> {
    let paths = Path::query_for_collection_and_sample(conn, collection_name, sample_name);

    let mut path_links: HashMap<String, Vec<(String, Strand)>> = HashMap::new();

    for path in paths {
        let block_group = BlockGroup::get_by_id(conn, &path.block_group_id);
        let sample_name = block_group.sample_name;

        let path_blocks = path.blocks(conn);
        let projected_path = project_path(graph, &path_blocks);

        if !projected_path.is_empty() {
            let full_path_name = if let Some(sample_name) = sample_name
                && !sample_name.is_empty()
            {
                format!("{}.{}", path.name, sample_name)
            } else {
                path.name
            };
            path_links.insert(
                full_path_name,
                projected_path
                    .iter()
                    .filter_map(|(node, strand)| {
                        if !is_terminal(node.node_id) {
                            if let Some(splits) = split_segments.get(&node.node_id) {
                                Some(
                                    splits
                                        .iter()
                                        .map(|(start, end)| {
                                            (
                                                format!("{id}.{start}.{end}", id = node.node_id),
                                                *strand,
                                            )
                                        })
                                        .collect::<Vec<_>>(),
                                )
                            } else {
                                Some(vec![(
                                    format!(
                                        "{id}.{ss}.{se}",
                                        id = node.node_id,
                                        ss = node.sequence_start,
                                        se = node.sequence_end
                                    ),
                                    *strand,
                                )])
                            }
                        } else {
                            None
                        }
                    })
                    .flatten()
                    .collect::<Vec<_>>(),
            );
        } else {
            println!(
                "Path {name} is not translatable to current graph.",
                name = &path.name
            );
        }
    }
    path_links
}

fn write_paths(
    writer: &mut BufWriter<File>,
    path_links: HashMap<String, Vec<(String, Strand)>>,
) -> std::io::Result<()> {
    for (name, links) in path_links.iter() {
        let mut segment_ids = vec![];
        let mut node_strands = vec![];
        for (segment_id, strand) in links.iter() {
            segment_ids.push(segment_id.clone());
            node_strands.push(*strand);
        }
        let path = GFAPath {
            name: name.clone(),
            segment_ids,
            node_strands,
        };
        writer.write_all(&path_line(&path).into_bytes())?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    // Note this useful idiom: importing names from outer (for mod tests) scope.
    use gen_core::{PATH_END_NODE_ID, PATH_START_NODE_ID, Strand, path::PathBlock};
    use gen_models::{
        block_group::{BlockGroup, PathChange},
        block_group_edge::BlockGroupEdgeData,
        collection::Collection,
        node::Node,
        sequence::Sequence,
        traits::Query,
    };
    use tempfile::tempdir;

    use super::*;
    use crate::{
        imports::gfa::import_gfa,
        test_helpers::{setup_block_group, setup_gen},
        track_database,
    };

    #[test]
    fn test_simple_export() {
        // Sets up a basic graph and then exports it to a GFA file
        let context = setup_gen();
        let conn = context.graph().conn();
        let op_conn = context.operations().conn();

        track_database(conn, op_conn).unwrap();

        let collection_name = "test collection";
        Collection::create(conn, collection_name);
        let block_group = BlockGroup::create(conn, collection_name, None, "test block group");
        let sequence1 = Sequence::new()
            .sequence_type("DNA")
            .sequence("AAAA")
            .save(conn);
        let sequence2 = Sequence::new()
            .sequence_type("DNA")
            .sequence("TTTT")
            .save(conn);
        let sequence3 = Sequence::new()
            .sequence_type("DNA")
            .sequence("GGGG")
            .save(conn);
        let sequence4 = Sequence::new()
            .sequence_type("DNA")
            .sequence("CCCC")
            .save(conn);
        let node1_id = Node::create(conn, &sequence1.hash, &HashId::convert_str("1"));
        let node2_id = Node::create(conn, &sequence2.hash, &HashId::convert_str("2"));
        let node3_id = Node::create(conn, &sequence3.hash, &HashId::convert_str("3"));
        let node4_id = Node::create(conn, &sequence4.hash, &HashId::convert_str("4"));

        let edge1 = Edge::create(
            conn,
            PATH_START_NODE_ID,
            0,
            Strand::Forward,
            node1_id,
            0,
            Strand::Forward,
        );
        let edge2 = Edge::create(
            conn,
            node1_id,
            4,
            Strand::Forward,
            node2_id,
            0,
            Strand::Forward,
        );
        let edge3 = Edge::create(
            conn,
            node2_id,
            4,
            Strand::Forward,
            node3_id,
            0,
            Strand::Forward,
        );
        let edge4 = Edge::create(
            conn,
            node3_id,
            4,
            Strand::Forward,
            node4_id,
            0,
            Strand::Forward,
        );
        let edge5 = Edge::create(
            conn,
            node4_id,
            4,
            Strand::Forward,
            PATH_END_NODE_ID,
            0,
            Strand::Forward,
        );

        let new_block_group_edges = vec![
            BlockGroupEdgeData {
                block_group_id: block_group.id,
                edge_id: edge1.id,
                chromosome_index: 0,
                phased: 0,
            },
            BlockGroupEdgeData {
                block_group_id: block_group.id,
                edge_id: edge2.id,
                chromosome_index: 0,
                phased: 0,
            },
            BlockGroupEdgeData {
                block_group_id: block_group.id,
                edge_id: edge3.id,
                chromosome_index: 0,
                phased: 0,
            },
            BlockGroupEdgeData {
                block_group_id: block_group.id,
                edge_id: edge4.id,
                chromosome_index: 0,
                phased: 0,
            },
            BlockGroupEdgeData {
                block_group_id: block_group.id,
                edge_id: edge5.id,
                chromosome_index: 0,
                phased: 0,
            },
        ];
        BlockGroupEdge::bulk_create(conn, &new_block_group_edges);

        Path::create(
            conn,
            "1234",
            &block_group.id,
            &[edge1.id, edge2.id, edge3.id, edge4.id, edge5.id],
        );

        let all_sequences = BlockGroup::get_all_sequences(conn, &block_group.id, false);

        let temp_dir = tempdir().expect("Couldn't get handle to temp directory");
        let mut gfa_path = PathBuf::from(temp_dir.path());
        gfa_path.push("intermediate.gfa");

        export_gfa(conn, collection_name, &gfa_path, None, None).unwrap();
        // NOTE: Not directly checking file contents because segments are written in random order
        let _ = import_gfa(&context, &gfa_path, "test collection 2", None);

        let block_group2 = Collection::get_block_groups(conn, "test collection 2")
            .pop()
            .unwrap();
        let all_sequences2 = BlockGroup::get_all_sequences(conn, &block_group2.id, false);

        assert_eq!(all_sequences, all_sequences2);

        let paths = Path::query_for_collection(conn, "test collection 2");
        assert_eq!(paths.len(), 1);
        assert_eq!(paths[0].sequence(conn), "AAAATTTTGGGGCCCC");
    }

    #[test]
    fn test_splits_nodes() {
        let context = setup_gen();
        let conn = context.graph().conn();
        let op_conn = context.operations().conn();

        track_database(conn, op_conn).unwrap();

        let (bg_id, _path) = setup_block_group(conn);
        let all_sequences = BlockGroup::get_all_sequences(conn, &bg_id, false);

        let temp_dir = tempdir().expect("Couldn't get handle to temp directory");
        let gfa_path = PathBuf::from(temp_dir.path()).join("split.gfa");

        export_gfa(conn, "test", &gfa_path, None, 5).unwrap();

        let _ = import_gfa(&context, &gfa_path, "test collection 2", None);

        let block_group2 = Collection::get_block_groups(conn, "test collection 2")
            .pop()
            .unwrap();
        let all_sequences2 = BlockGroup::get_all_sequences(conn, &block_group2.id, false);

        assert_eq!(all_sequences, all_sequences2);

        let graph = BlockGroup::get_graph(conn, &block_group2.id);
        let graph_nodes = graph
            .nodes()
            .filter_map(|node| {
                if is_terminal(node.node_id) {
                    None
                } else {
                    Some(node.node_id)
                }
            })
            .collect::<Vec<_>>();
        let node_sequences = Node::get_sequences_by_node_ids(conn, &graph_nodes);
        assert!(node_sequences.len() > 1);
        for sequence in node_sequences.values() {
            assert!(
                sequence.length <= 5,
                "Sequence length {l} > 5",
                l = sequence.length
            );
        }
    }

    #[test]
    fn test_simple_round_trip() {
        let context = setup_gen();
        let mut gfa_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        gfa_path.push("fixtures/simple.gfa");
        let collection_name = "test".to_string();
        let conn = context.graph().conn();
        let op_conn = context.operations().conn();

        track_database(conn, op_conn).unwrap();

        let _ = import_gfa(&context, &gfa_path, &collection_name, None);

        let block_group_id = BlockGroup::get_id(&collection_name, None, "");
        let all_sequences = BlockGroup::get_all_sequences(conn, &block_group_id, false);

        let temp_dir = tempdir().expect("Couldn't get handle to temp directory");
        let mut gfa_path = PathBuf::from(temp_dir.path());
        gfa_path.push("intermediate.gfa");

        export_gfa(conn, &collection_name, &gfa_path, None, None).unwrap();
        let _ = import_gfa(&context, &gfa_path, "test collection 2", None);

        let block_group2 = Collection::get_block_groups(conn, "test collection 2")
            .pop()
            .unwrap();
        let all_sequences2 = BlockGroup::get_all_sequences(conn, &block_group2.id, false);

        assert_eq!(all_sequences, all_sequences2);
    }

    #[test]
    fn test_anderson_round_trip() {
        let context = setup_gen();
        let mut gfa_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        gfa_path.push("fixtures/anderson_promoters.gfa");
        let collection_name = "anderson promoters".to_string();
        let conn = context.graph().conn();
        let op_conn = context.operations().conn();

        track_database(conn, op_conn).unwrap();

        let _ = import_gfa(&context, &gfa_path, &collection_name, None);

        let block_group_id = BlockGroup::get_id(&collection_name, None, "");
        let all_sequences = BlockGroup::get_all_sequences(conn, &block_group_id, false);

        let temp_dir = tempdir().expect("Couldn't get handle to temp directory");
        let mut gfa_path = PathBuf::from(temp_dir.path());
        gfa_path.push("intermediate.gfa");

        export_gfa(conn, &collection_name, &gfa_path, None, None).unwrap();
        let _ = import_gfa(&context, &gfa_path, "anderson promoters 2", None);

        let block_group2 = Collection::get_block_groups(conn, "anderson promoters 2")
            .pop()
            .unwrap();
        let all_sequences2 = BlockGroup::get_all_sequences(conn, &block_group2.id, false);

        assert_eq!(all_sequences, all_sequences2);
    }

    #[test]
    fn test_reverse_strand_round_trip() {
        let context = setup_gen();
        let mut gfa_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        gfa_path.push("fixtures/reverse_strand.gfa");
        let collection_name = "test".to_string();
        let conn = context.graph().conn();
        let op_conn = context.operations().conn();

        track_database(conn, op_conn).unwrap();

        let _ = import_gfa(&context, &gfa_path, &collection_name, None);

        let block_group_id = BlockGroup::get_id(&collection_name, None, "");
        let all_sequences = BlockGroup::get_all_sequences(conn, &block_group_id, false);

        let temp_dir = tempdir().expect("Couldn't get handle to temp directory");
        let mut gfa_path = PathBuf::from(temp_dir.path());
        gfa_path.push("intermediate.gfa");

        export_gfa(conn, &collection_name, &gfa_path, None, None).unwrap();
        let _ = import_gfa(&context, &gfa_path, "test collection 2", None);

        let block_group2 = Collection::get_block_groups(conn, "test collection 2")
            .pop()
            .unwrap();
        let all_sequences2 = BlockGroup::get_all_sequences(conn, &block_group2.id, false);

        assert_eq!(all_sequences, all_sequences2);
    }

    #[test]
    fn test_sequence_is_split_into_multiple_segments() {
        // Confirm that if edges are added to or from a sequence, that results in the sequence being
        // split into multiple segments in the exported GFA, and that the multiple segments are
        // re-imported as multiple sequences
        let context = setup_gen();
        let conn = context.graph().conn();
        let op_conn = context.operations().conn();

        track_database(conn, op_conn).unwrap();

        let (block_group_id, path) = setup_block_group(conn);
        let insert_sequence = Sequence::new()
            .sequence_type("DNA")
            .sequence("NNNN")
            .save(conn);
        let insert_node_id = Node::create(conn, &insert_sequence.hash, &HashId::convert_str("1"));
        let insert = PathBlock {
            id: 0,
            node_id: insert_node_id,
            block_sequence: insert_sequence.get_sequence(0, 4).to_string(),
            sequence_start: 0,
            sequence_end: 4,
            path_start: 7,
            path_end: 15,
            strand: Strand::Forward,
        };
        let change = PathChange {
            block_group_id,
            path: path.clone(),
            path_accession: None,
            start: 7,
            end: 15,
            block: insert,
            chromosome_index: 1,
            phased: 0,
            preserve_edge: true,
        };
        let tree = path.intervaltree(conn);
        BlockGroup::insert_change(conn, &change, &tree).unwrap();

        let augmented_edges = BlockGroupEdge::edges_for_block_group(conn, &block_group_id);
        let mut node_ids = HashSet::new();
        let mut edge_ids = HashSet::new();
        for augmented_edge in augmented_edges {
            let edge = &augmented_edge.edge;
            if !is_terminal(edge.source_node_id) {
                node_ids.insert(edge.source_node_id);
            }
            if !is_terminal(edge.target_node_id) {
                node_ids.insert(edge.target_node_id);
            }
            if !is_terminal(edge.source_node_id) && !is_terminal(edge.target_node_id) {
                edge_ids.insert(edge.id);
            }
        }

        // The original 10-length A, T, C, G sequences, plus NNNN
        assert_eq!(node_ids.len(), 5);
        // 3 edges from A sequence -> T sequence, T sequence -> C sequence, C sequence -> G sequence
        // 2 edges to and from NNNN
        // 2 edges healing the reference
        // 7 total
        assert_eq!(edge_ids.len(), 7);

        let nodes = Node::query_by_ids(conn, &node_ids);
        let mut node_hashes = HashSet::new();
        for node in nodes {
            if !is_terminal(node.id) {
                node_hashes.insert(node.sequence_hash);
            }
        }

        // The original 10-length A, T, C, G sequences, plus NNNN
        assert_eq!(node_hashes.len(), 5);

        let temp_dir = tempdir().expect("Couldn't get handle to temp directory");
        let mut gfa_path = PathBuf::from(temp_dir.path());
        gfa_path.push("intermediate.gfa");
        export_gfa(conn, "test", &gfa_path, None, None).unwrap();
        let _ = import_gfa(&context, &gfa_path, "test collection 2", None);

        let block_group2 = Collection::get_block_groups(conn, "test collection 2")
            .pop()
            .unwrap();

        let augmented_edges2 = BlockGroupEdge::edges_for_block_group(conn, &block_group2.id);
        let mut node_ids2 = HashSet::new();
        let mut edge_ids2 = HashSet::new();
        for augmented_edge in augmented_edges2 {
            let edge = &augmented_edge.edge;
            if !is_terminal(edge.source_node_id) {
                node_ids2.insert(edge.source_node_id);
            }
            if !is_terminal(edge.target_node_id) {
                node_ids2.insert(edge.target_node_id);
            }
            if !is_terminal(edge.source_node_id) && !is_terminal(edge.target_node_id) {
                edge_ids2.insert(edge.id);
            }
        }

        // The 10-length A and T sequences have now been split in two (showing up as different
        // segments in the exported GFA), so expect two more nodes
        assert_eq!(node_ids2.len(), 7);
        // 3 edges from A sequence -> T sequence, T sequence -> C sequence, C sequence -> G sequence
        // 2 edges to and from NNNN
        // 2 edges healing the reference in NNNN
        // 7 total
        assert_eq!(edge_ids2.len(), 7);

        let nodes2 = Node::query_by_ids(conn, &node_ids2);
        let mut node_hashes2 = HashSet::new();
        for node in nodes2 {
            if !is_terminal(node.id) {
                node_hashes2.insert(node.sequence_hash);
            }
        }

        // The 10-length A and T sequences have now been split in two, but since the T sequences was
        // split in half, there's just one new TTTTT sequence shared by 2 nodes
        assert_eq!(node_hashes2.len(), 6);
    }
}