gen_models/locus.rs
1//! Nameless addresses in graph space.
2//!
3//! A specific location on the genome can be specified by a region on a path
4//! (addressing in linear space), for example "chr1:123-456". If there is no
5//! path defined that traverses the location you need to address in graph
6//! space. There are two kinds of graphs to consider in our datamodel:
7//! - Graphs with Nodes as defined in the database and edges with start/end
8//! coordinates.
9//! - Graphs with Blocks aka GraphNodes that are carved out Nodes and which
10//! most closely resemble the pangenomics literature.
11//!
12//! The search algorithm, for example, is a lot easier to understand on Blocks
13//! rather than Nodes. It's output can be used to address graph space and in
14//! the form of a list of GraphNodeSlices: segments from blocks, in the
15//! coordinate reference frame (left side = 0). But to store graph changes in
16//! the additive model in the database we must convert back to the Node format.
17
18use gen_core::{HashId, Strand};
19pub use gen_graph::GraphNodeSlice;
20
21use crate::{db::GraphConnection, node::Node, sequence::reverse_complement};
22
23/// A region in graph space expressed as an ordered list of block slices.
24/// Each slice carries its own strand, allowing trans-spliced loci where
25/// individual exons may come from opposite strands.
26#[derive(Clone, Debug, PartialEq, Eq)]
27pub struct GraphLocus {
28 pub slices: Vec<GraphNodeSlice>,
29}
30
31impl GraphLocus {
32 /// Concatenate the sequence bytes covered by this locus.
33 pub fn sequence(&self, conn: &GraphConnection) -> Vec<u8> {
34 let node_ids: Vec<HashId> = self.slices.iter().map(|s| s.block.node_id).collect();
35 let sequences = Node::get_sequences_by_node_ids(conn, &node_ids);
36
37 let mut out = Vec::new();
38 for s in &self.slices {
39 let full = sequences[&s.block.node_id]
40 .get_sequence(None, None)
41 .expect("sequence data corrupt")
42 .into_bytes();
43 let block_start = s.block.sequence_start as usize;
44 let text = &full[block_start..block_start + s.block.length() as usize];
45 let slice_bytes = &text[s.start..s.end];
46 if s.strand == Strand::Reverse {
47 out.extend_from_slice(&reverse_complement(slice_bytes));
48 } else {
49 out.extend_from_slice(slice_bytes);
50 }
51 }
52
53 out
54 }
55}