pangraphx-core 0.1.8

Core library for PanGraphX: a toolkit for genome graph loading and format conversion.
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
use super::graph_dto::{CoreGraphDTO, GraphParts};
use crate::core::core_types::{
    Edge, Node, NodeId, NodeName, Nodes, Orientation, Path, PathName, Sequence,
};
use crate::error::{PanGraphXError, PanResult};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::sync::OnceLock;

/// A `CoreGraph` type providing efficient lookup and graph manipulation capabilities.
///
/// Like [`CoreGraphDTO`] it is always valid: edges and path steps refer to
/// existing nodes and path names are unique. Its lookup indices are private
/// and kept in sync by every edit.
#[derive(Debug, Clone)]
pub struct CoreGraph {
    nodes: Nodes,
    edges: Vec<Edge>,
    /// Paths in insertion order, so conversion back to a DTO is deterministic.
    paths: Vec<Path>,
    /// Path name to its index in `paths`.
    path_index: HashMap<PathName, usize>,
    node_name_map: Option<HashMap<NodeId, NodeName>>,
    /// For each node, the indices of the edges with it as an endpoint. Built
    /// on the first lookup (the de Bruijn builders never need it), extended by
    /// `add_node`/`add_edge` and dropped by removals.
    incident_edges: OnceLock<Vec<Vec<usize>>>,
}

impl CoreGraph {
    #[must_use]
    pub fn new(graph: CoreGraphDTO) -> Self {
        let GraphParts {
            nodes,
            edges,
            paths,
            node_name_map,
        } = GraphParts::from(graph);
        Self {
            incident_edges: OnceLock::new(),
            path_index: index_paths(&paths),
            nodes,
            edges,
            paths,
            node_name_map,
        }
    }

    // ===== Accessors =====

    /// The nodes, indexed by `NodeId`.
    #[must_use]
    pub const fn nodes(&self) -> &Nodes {
        &self.nodes
    }

    /// The edges; an edge's index is its position here.
    #[must_use]
    pub const fn edges(&self) -> &[Edge] {
        self.edges.as_slice()
    }

    /// The paths, in insertion order.
    #[must_use]
    pub const fn paths(&self) -> &[Path] {
        self.paths.as_slice()
    }

    /// Mapping from `NodeId` to original node names (if available).
    #[must_use]
    pub const fn node_name_map(&self) -> Option<&HashMap<NodeId, NodeName>> {
        self.node_name_map.as_ref()
    }

    /// Indices (into [`CoreGraph::edges`]) of the edges with `node_id` as an
    /// endpoint, in edge order. Empty for a missing node.
    #[must_use]
    pub fn incident_edge_indices(&self, node_id: NodeId) -> &[usize] {
        self.incident_edges()
            .get(node_id)
            .map_or(&[], Vec::as_slice)
    }

    fn incident_edges(&self) -> &[Vec<usize>] {
        self.incident_edges
            .get_or_init(|| build_incident_edges(self.nodes.len(), &self.edges))
    }

    // ===== Graph Manipulation Methods =====

    /// Adds a new node to the graph with the given sequence.
    /// Returns the `NodeId` assigned to the new node.
    ///
    /// # Arguments
    /// * `sequence` - DNA sequence for the new node
    /// * `name` - Optional name for the node
    ///
    /// # Examples
    /// ```
    /// # use pangraphx_core::CoreGraph;
    /// # use pangraphx_core::CoreGraphDTO;
    /// let mut graph = CoreGraph::new(CoreGraphDTO::default());
    /// let node_id = graph.add_node(b"ACGT".to_vec(), None);
    /// assert_eq!(node_id, 0);
    /// ```
    pub fn add_node(&mut self, sequence: Sequence, name: Option<NodeName>) -> NodeId {
        self.nodes.push(sequence);
        let node_id = self.nodes.len() - 1;
        if let Some(incident) = self.incident_edges.get_mut() {
            incident.push(Vec::new());
        }

        if let Some(node_name) = name {
            self.node_name_map
                .get_or_insert_with(HashMap::new)
                .insert(node_id, node_name);
        }
        node_id
    }

    /// Adds a new edge to the graph.
    /// Returns the index of the added edge in the edges vector.
    ///
    /// # Arguments
    /// * `edge` - The edge to add
    ///
    /// # Errors
    /// Returns an error if the `from_node` or `to_node` doesn't exist in the graph.
    ///
    /// # Examples
    /// ```
    /// # use pangraphx_core::{CoreGraph, CoreGraphDTO};
    /// # use pangraphx_core::core::core_types::{Edge, Orientation};
    /// let mut graph = CoreGraph::new(CoreGraphDTO::default());
    /// let n1 = graph.add_node(b"ACGT".to_vec(), None);
    /// let n2 = graph.add_node(b"TGCA".to_vec(), None);
    /// let edge = Edge {
    ///     from_node: n1,
    ///     from_orient: Orientation::Forward,
    ///     to_node: n2,
    ///     to_orient: Orientation::Forward,
    ///     overlap: 0,
    /// };
    /// graph.add_edge(edge).unwrap();
    /// ```
    pub fn add_edge(&mut self, edge: Edge) -> PanResult<usize> {
        self.check_node(edge.from_node)?;
        self.check_node(edge.to_node)?;

        let edge_idx = self.edges.len();
        if let Some(incident) = self.incident_edges.get_mut() {
            add_incident(incident, &edge, edge_idx);
        }
        self.edges.push(edge);
        Ok(edge_idx)
    }

    /// Adds a new path to the graph.
    ///
    /// # Arguments
    /// * `path` - The path to add
    ///
    /// # Errors
    /// Returns an error if:
    /// - A path with the same name already exists
    /// - Any node in the path doesn't exist
    ///
    /// # Examples
    /// ```
    /// # use pangraphx_core::{CoreGraph, CoreGraphDTO};
    /// # use pangraphx_core::core::core_types::{Path, Step, Orientation};
    /// let mut graph = CoreGraph::new(CoreGraphDTO::default());
    /// let n1 = graph.add_node(b"ACGT".to_vec(), None);
    /// let path = Path {
    ///     name: b"path1".to_vec(),
    ///     steps: vec![Step::new(n1, Orientation::Forward).unwrap()],
    ///     overlaps: vec![],
    /// };
    /// graph.add_path(path).unwrap();
    /// ```
    pub fn add_path(&mut self, path: Path) -> PanResult<()> {
        if self.path_index.contains_key(&path.name) {
            return Err(PanGraphXError::InvalidGraph(format!(
                "path {} already exists",
                String::from_utf8_lossy(&path.name)
            )));
        }
        self.check_steps(&path)?;
        self.path_index.insert(path.name.clone(), self.paths.len());
        self.paths.push(path);
        Ok(())
    }

    /// Removes a node from the graph.
    /// Also removes all edges connected to this node and updates paths.
    /// This operation is potentially expensive as it requires updating node IDs and paths.
    ///
    /// # Arguments
    /// * `node_id` - The ID of the node to remove
    ///
    /// # Errors
    /// Returns an error if the node doesn't exist.
    ///
    /// # Note
    /// This operation renumbers node IDs. All nodes after the removed node
    /// will have their IDs decremented by 1. Edges and paths are updated accordingly.
    ///
    /// # Examples
    /// ```
    /// # use pangraphx_core::{CoreGraph, CoreGraphDTO};
    /// let mut graph = CoreGraph::new(CoreGraphDTO::default());
    /// let n1 = graph.add_node(b"ACGT".to_vec(), None);
    /// graph.remove_node(n1).unwrap();
    /// ```
    pub fn remove_node(&mut self, node_id: NodeId) -> PanResult<()> {
        self.check_node(node_id)?;
        self.nodes.remove(node_id);

        // Remove edges connected to this node and update edge node IDs
        self.edges
            .retain(|edge| edge.from_node != node_id && edge.to_node != node_id);
        for edge in &mut self.edges {
            if edge.from_node > node_id {
                edge.from_node -= 1;
            }
            if edge.to_node > node_id {
                edge.to_node -= 1;
            }
        }
        self.incident_edges = OnceLock::new();

        // Drop the node's steps from every path, and paths left empty.
        for path in &mut self.paths {
            remove_steps_of(path, node_id)?;
        }
        self.paths.retain(|path| !path.steps.is_empty());
        self.path_index = index_paths(&self.paths);

        if let Some(names) = self.node_name_map.take() {
            self.node_name_map = Some(
                names
                    .into_iter()
                    .filter_map(|(id, name)| match id.cmp(&node_id) {
                        Ordering::Less => Some((id, name)),
                        Ordering::Equal => None,
                        Ordering::Greater => Some((id - 1, name)),
                    })
                    .collect(),
            );
        }
        Ok(())
    }

    /// Removes an edge from the graph at the specified index.
    ///
    /// # Arguments
    /// * `edge_idx` - The index of the edge to remove in the edges vector
    ///
    /// # Errors
    /// Returns an error if the edge index is out of bounds.
    ///
    /// # Examples
    /// ```
    /// # use pangraphx_core::{CoreGraph, CoreGraphDTO};
    /// # use pangraphx_core::core::core_types::{Edge, Orientation};
    /// let mut graph = CoreGraph::new(CoreGraphDTO::default());
    /// let n1 = graph.add_node(b"ACGT".to_vec(), None);
    /// let n2 = graph.add_node(b"TGCA".to_vec(), None);
    /// let edge = Edge {
    ///     from_node: n1,
    ///     from_orient: Orientation::Forward,
    ///     to_node: n2,
    ///     to_orient: Orientation::Forward,
    ///     overlap: 0,
    /// };
    /// let idx = graph.add_edge(edge).unwrap();
    /// graph.remove_edge(idx).unwrap();
    /// ```
    pub fn remove_edge(&mut self, edge_idx: usize) -> PanResult<()> {
        if edge_idx >= self.edges.len() {
            return Err(PanGraphXError::InvalidGraph(format!(
                "edge index {edge_idx} is out of bounds"
            )));
        }

        self.edges.remove(edge_idx);

        // Later edge indices shifted; rebuild the index on the next lookup.
        self.incident_edges = OnceLock::new();

        Ok(())
    }

    /// Removes a path from the graph.
    ///
    /// # Arguments
    /// * `path_name` - The name of the path to remove
    ///
    /// # Errors
    /// Returns an error if the path doesn't exist.
    ///
    /// # Examples
    /// ```
    /// # use pangraphx_core::{CoreGraph, CoreGraphDTO};
    /// # use pangraphx_core::core::core_types::{Path, Step, Orientation};
    /// let mut graph = CoreGraph::new(CoreGraphDTO::default());
    /// let n1 = graph.add_node(b"ACGT".to_vec(), None);
    /// let path = Path {
    ///     name: b"path1".to_vec(),
    ///     steps: vec![Step::new(n1, Orientation::Forward).unwrap()],
    ///     overlaps: vec![],
    /// };
    /// graph.add_path(path).unwrap();
    /// graph.remove_path(b"path1").unwrap();
    /// ```
    pub fn remove_path(&mut self, path_name: &[u8]) -> PanResult<()> {
        let index = self
            .path_index
            .remove(path_name)
            .ok_or_else(|| missing_path(path_name))?;
        self.paths.remove(index);
        for later in self.path_index.values_mut() {
            if *later > index {
                *later -= 1;
            }
        }
        Ok(())
    }

    /// Returns the number of nodes in the graph.
    #[must_use]
    pub const fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// Returns the number of edges in the graph.
    #[must_use]
    pub const fn edge_count(&self) -> usize {
        self.edges.len()
    }

    /// Returns the number of paths in the graph.
    #[must_use]
    pub const fn path_count(&self) -> usize {
        self.paths.len()
    }

    /// Gets a reference to a node by its ID.
    ///
    /// # Errors
    /// Returns an error if the node doesn't exist.
    pub fn get_node(&self, node_id: NodeId) -> PanResult<&Node> {
        self.nodes.get(node_id).ok_or_else(|| missing_node(node_id))
    }

    /// Replaces the sequence of a node.
    ///
    /// # Errors
    /// Returns an error if the node doesn't exist.
    pub fn set_sequence(&mut self, node_id: NodeId, sequence: Sequence) -> PanResult<()> {
        *self
            .nodes
            .sequence_mut(node_id)
            .ok_or_else(|| missing_node(node_id))? = sequence;
        Ok(())
    }

    /// Gets a reference to an edge by its index.
    ///
    /// # Errors
    /// Returns an error if the edge index is out of bounds.
    pub fn get_edge(&self, edge_idx: usize) -> PanResult<&Edge> {
        self.edges.get(edge_idx).ok_or_else(|| {
            PanGraphXError::InvalidGraph(format!("edge index {edge_idx} is out of bounds"))
        })
    }

    /// Gets a reference to a path by its name.
    ///
    /// # Errors
    /// Returns an error if the path doesn't exist.
    pub fn get_path(&self, path_name: &[u8]) -> PanResult<&Path> {
        self.path_index
            .get(path_name)
            .map(|&index| &self.paths[index])
            .ok_or_else(|| missing_path(path_name))
    }

    /// Replaces the path with the same name as `path`, returning the old one.
    ///
    /// # Errors
    /// Returns an error if no path has that name or a step refers to a
    /// missing node.
    pub fn replace_path(&mut self, path: Path) -> PanResult<Path> {
        let index = *self
            .path_index
            .get(&path.name)
            .ok_or_else(|| missing_path(&path.name))?;
        self.check_steps(&path)?;
        Ok(std::mem::replace(&mut self.paths[index], path))
    }

    /// Returns the edges stored with `node_id` as their `from_node`, in edge
    /// order.
    ///
    /// # Errors
    /// Returns an error if the node doesn't exist.
    pub fn get_outgoing_edges(&self, node_id: NodeId) -> PanResult<Vec<&Edge>> {
        self.check_node(node_id)?;
        Ok(self
            .incident_edge_indices(node_id)
            .iter()
            .map(|&idx| &self.edges[idx])
            .filter(|edge| edge.from_node == node_id)
            .collect())
    }

    /// Checks if the graph links `from_node` (in `from_orient`) to `to_node`
    /// (in `to_orient`), whichever of its two equivalent forms is stored
    /// (`a+ -> b+` or `b- -> a-`; see [`Edge::is_link`]).
    #[must_use]
    pub fn has_edge(
        &self,
        from_node: NodeId,
        from_orient: Orientation,
        to_node: NodeId,
        to_orient: Orientation,
    ) -> bool {
        self.incident_edge_indices(from_node)
            .iter()
            .any(|&idx| self.edges[idx].is_link(from_node, from_orient, to_node, to_orient))
    }

    /// Releases what the path walkers never read (the edges, their index and
    /// the node names), keeping the nodes and paths.
    pub(crate) fn keep_only_paths(&mut self) {
        self.edges = Vec::new();
        self.incident_edges = OnceLock::new();
        self.node_name_map = None;
    }

    fn check_node(&self, node_id: NodeId) -> PanResult<()> {
        if node_id < self.nodes.len() {
            Ok(())
        } else {
            Err(missing_node(node_id))
        }
    }

    fn check_steps(&self, path: &Path) -> PanResult<()> {
        path.steps
            .iter()
            .try_for_each(|step| self.check_node(step.node_id()))
    }
}

fn build_incident_edges(node_count: usize, edges: &[Edge]) -> Vec<Vec<usize>> {
    let mut incident = vec![Vec::new(); node_count];
    for (i, edge) in edges.iter().enumerate() {
        add_incident(&mut incident, edge, i);
    }
    incident
}

/// Records edge `index` at both endpoints (once for a self-loop). Both
/// endpoints exist, since edges are validated before they are added.
fn add_incident(incident: &mut [Vec<usize>], edge: &Edge, index: usize) {
    incident[edge.from_node].push(index);
    if edge.to_node != edge.from_node {
        incident[edge.to_node].push(index);
    }
}

fn index_paths(paths: &[Path]) -> HashMap<PathName, usize> {
    paths
        .iter()
        .enumerate()
        .map(|(index, path)| (path.name.clone(), index))
        .collect()
}

/// Removes the steps visiting `node_id` from `path` and renumbers the steps
/// of later nodes. An overlap is kept while its two steps stay adjacent; the
/// junction of two steps that were not adjacent gets overlap 0.
fn remove_steps_of(path: &mut Path, node_id: NodeId) -> PanResult<()> {
    if !path.steps.iter().any(|step| step.node_id() >= node_id) {
        return Ok(());
    }
    let with_overlaps = !path.overlaps.is_empty();
    let mut steps = Vec::with_capacity(path.steps.len());
    let mut overlaps = Vec::new();
    let mut previous: Option<usize> = None;
    for (i, &step) in path.steps.iter().enumerate() {
        let id = step.node_id();
        if id == node_id {
            continue;
        }
        if with_overlaps && let Some(p) = previous {
            let adjacent = p + 1 == i;
            overlaps.push(if adjacent {
                path.overlaps.get(p).copied().unwrap_or(0)
            } else {
                0
            });
        }
        steps.push(if id > node_id {
            step.with_node_id(id - 1)?
        } else {
            step
        });
        previous = Some(i);
    }
    path.steps = steps;
    path.overlaps = overlaps;
    Ok(())
}

fn missing_node(node_id: NodeId) -> PanGraphXError {
    PanGraphXError::InvalidGraph(format!("node {node_id} does not exist"))
}

fn missing_path(name: &[u8]) -> PanGraphXError {
    PanGraphXError::InvalidGraph(format!(
        "path {} does not exist",
        String::from_utf8_lossy(name)
    ))
}

impl From<CoreGraph> for CoreGraphDTO {
    fn from(graph: CoreGraph) -> Self {
        // Every edit keeps the graph valid, so it needs no second check.
        Self::from_valid_parts(GraphParts {
            nodes: graph.nodes,
            edges: graph.edges,
            paths: graph.paths,
            node_name_map: graph.node_name_map,
        })
    }
}