skesa-rs 0.2.1

Rust port of NCBI's SKESA genome assembler
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
/// Guided graph for SAUTE target-enriched assembly.
///
/// Partial Rust implementation of SKESA's CGuidedGraph concepts from guidedgraph.hpp.
///
/// The guided graph is a segment-based graph where each segment is a DNA
/// sequence with alignment metadata. Segments are connected via left/right
/// connections at specific positions. The graph tracks:
/// - Anchors: k-mers at known positions on the target
/// - Not-aligned regions: segments that deviate from the target alignment
///
/// This is used to represent the assembly of reads guided by a target sequence,
/// allowing variant representation and gap handling.
use std::collections::HashMap;

/// A base in a path through the graph
#[derive(Clone, Debug)]
pub struct PathBase {
    pub base: char,
}

/// A segment in the guided graph
#[derive(Clone, Debug)]
pub struct SeqSegment {
    /// DNA sequence of this segment
    pub seq: Vec<PathBase>,
    /// Left connections: position -> list of (segment_id, position) pairs
    pub left_connections: HashMap<i32, Vec<(usize, i32)>>,
    /// Right connections: position -> list of (segment_id, position) pairs
    pub right_connections: HashMap<i32, Vec<(usize, i32)>>,
    /// Number of bases at left end that are not aligned to target
    pub not_aligned_left: i32,
    /// Number of bases at right end that are not aligned to target
    pub not_aligned_right: i32,
}

impl SeqSegment {
    pub fn new(seq: Vec<PathBase>, not_aligned_left: i32, not_aligned_right: i32) -> Self {
        SeqSegment {
            seq,
            left_connections: HashMap::new(),
            right_connections: HashMap::new(),
            not_aligned_left,
            not_aligned_right,
        }
    }

    pub fn len(&self) -> usize {
        self.seq.len()
    }

    pub fn is_empty(&self) -> bool {
        self.seq.is_empty()
    }

    /// Get the sequence as a string
    pub fn sequence_string(&self) -> String {
        self.seq.iter().map(|b| b.base).collect()
    }
}

/// An anchor: a k-mer at a known position on the target
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Anchor {
    /// Canonical k-mer key
    pub kmer_key: Vec<u64>,
    /// Position on the target (right end of k-mer)
    pub target_pos: i32,
}

/// The guided graph: manages segments, connections, and anchors
pub struct GuidedGraph {
    /// All segments in the graph
    segments: Vec<SeqSegment>,
    /// K-mer length for the assembly
    kmer_len: usize,
    /// Left anchors: anchor -> (segment_id, position)
    left_anchors: HashMap<Anchor, (usize, i32)>,
    /// Right anchors: anchor -> (segment_id, position)
    right_anchors: HashMap<Anchor, (usize, i32)>,
    /// Not-aligned entry points: node_key -> list of (segment_id, position)
    not_aligned: HashMap<Vec<u64>, Vec<(usize, i32)>>,
    /// Current assembly chain: (segment_id, bases_used, cumulative_len)
    last_segments: Vec<(usize, usize, usize)>,
    /// Target sequence for this assembly
    target: String,
}

impl GuidedGraph {
    pub fn new(kmer_len: usize) -> Self {
        GuidedGraph {
            segments: Vec::new(),
            kmer_len,
            left_anchors: HashMap::new(),
            right_anchors: HashMap::new(),
            not_aligned: HashMap::new(),
            last_segments: Vec::new(),
            target: String::new(),
        }
    }

    /// Start a new assembly from an initial k-mer
    pub fn start_new_assembly(
        &mut self,
        init_kmer: &str,
        not_aligned_left: i32,
        not_aligned_right: i32,
    ) {
        self.last_segments.clear();
        self.not_aligned.clear();

        let seq: Vec<PathBase> = init_kmer.chars().map(|c| PathBase { base: c }).collect();
        let seg_id = self.add_segment(seq, not_aligned_left, not_aligned_right);
        self.last_segments
            .push((seg_id, init_kmer.len(), init_kmer.len()));
    }

    /// Add a new segment to the graph
    fn add_segment(
        &mut self,
        seq: Vec<PathBase>,
        not_aligned_left: i32,
        not_aligned_right: i32,
    ) -> usize {
        let id = self.segments.len();
        self.segments
            .push(SeqSegment::new(seq, not_aligned_left, not_aligned_right));
        id
    }

    /// Add bases to the right end of the current assembly
    pub fn add_right_segment(&mut self, bases: &[char], not_aligned_right: i32) {
        if bases.is_empty() {
            return;
        }

        let seq: Vec<PathBase> = bases.iter().map(|&c| PathBase { base: c }).collect();
        let new_id = self.add_segment(seq, 0, not_aligned_right);

        // Connect to the last segment
        if let Some(&(last_id, _, cumul)) = self.last_segments.last() {
            let last_pos = self.segments[last_id].len() as i32 - 1;
            self.segments[last_id]
                .right_connections
                .entry(last_pos)
                .or_default()
                .push((new_id, 0));
            self.segments[new_id]
                .left_connections
                .entry(0)
                .or_default()
                .push((last_id, last_pos));

            self.last_segments
                .push((new_id, bases.len(), cumul + bases.len()));
        }
    }

    /// Add bases to the left end of the current assembly
    pub fn add_left_segment(&mut self, bases: &[char], not_aligned_left: i32) {
        if bases.is_empty() {
            return;
        }

        let seq: Vec<PathBase> = bases.iter().map(|&c| PathBase { base: c }).collect();
        let new_id = self.add_segment(seq, not_aligned_left, 0);

        // Connect to the first segment
        if let Some(&(first_id, _, _)) = self.last_segments.first() {
            let new_pos = bases.len() as i32 - 1;
            self.segments[new_id]
                .right_connections
                .entry(new_pos)
                .or_default()
                .push((first_id, 0));
            self.segments[first_id]
                .left_connections
                .entry(0)
                .or_default()
                .push((new_id, new_pos));

            let new_entry = (new_id, bases.len(), bases.len());
            self.last_segments.insert(0, new_entry);
            // Update cumulative lengths
            for i in 1..self.last_segments.len() {
                self.last_segments[i].2 += bases.len();
            }
        }
    }

    /// Register a left anchor at a known target position
    pub fn add_left_anchor(
        &mut self,
        kmer_key: Vec<u64>,
        target_pos: i32,
        seg_id: usize,
        seg_pos: i32,
    ) {
        let anchor = Anchor {
            kmer_key,
            target_pos,
        };
        self.left_anchors.insert(anchor, (seg_id, seg_pos));
    }

    /// Register a right anchor
    pub fn add_right_anchor(
        &mut self,
        kmer_key: Vec<u64>,
        target_pos: i32,
        seg_id: usize,
        seg_pos: i32,
    ) {
        let anchor = Anchor {
            kmer_key,
            target_pos,
        };
        self.right_anchors.insert(anchor, (seg_id, seg_pos));
    }

    /// Check if a left anchor exists at a given k-mer and position
    pub fn known_left_anchor(&self, kmer_key: &[u64], target_pos: i32) -> Option<&(usize, i32)> {
        let anchor = Anchor {
            kmer_key: kmer_key.to_vec(),
            target_pos,
        };
        self.left_anchors.get(&anchor)
    }

    /// Check if a right anchor exists
    pub fn known_right_anchor(&self, kmer_key: &[u64], target_pos: i32) -> Option<&(usize, i32)> {
        let anchor = Anchor {
            kmer_key: kmer_key.to_vec(),
            target_pos,
        };
        self.right_anchors.get(&anchor)
    }

    /// Remove segments that are not well-aligned to the target.
    /// Segments with not_aligned > seq.len + anchor_frac * kmer_len and no connections
    /// on the unaligned side are removed.
    pub fn remove_not_aligned_segments(&mut self, anchor_frac: f64) {
        let threshold_extra = (anchor_frac * self.kmer_len as f64) as i32;

        // Find segments to remove from the right
        let mut to_remove_right: Vec<usize> = Vec::new();
        for (i, seg) in self.segments.iter().enumerate() {
            if seg.not_aligned_right > seg.len() as i32 + threshold_extra
                && seg.right_connections.is_empty()
            {
                to_remove_right.push(i);
            }
        }

        // Remove right-dangling segments and propagate
        for &seg_id in &to_remove_right {
            // Remove left connections pointing to this segment
            let left_conns: Vec<(i32, Vec<(usize, i32)>)> = self.segments[seg_id]
                .left_connections
                .iter()
                .map(|(&k, v)| (k, v.clone()))
                .collect();

            for (ipos, partners) in left_conns {
                for (partner_id, partner_pos) in partners {
                    if let Some(conns) = self.segments[partner_id]
                        .right_connections
                        .get_mut(&partner_pos)
                    {
                        conns.retain(|&(id, pos)| id != seg_id || pos != ipos);
                        if conns.is_empty() {
                            self.segments[partner_id]
                                .right_connections
                                .remove(&partner_pos);
                        }
                    }
                }
            }
            self.segments[seg_id].left_connections.clear();
            self.segments[seg_id].seq.clear();
        }

        // Similarly for left-dangling segments
        let mut to_remove_left: Vec<usize> = Vec::new();
        for (i, seg) in self.segments.iter().enumerate() {
            if seg.not_aligned_left > seg.len() as i32 + threshold_extra
                && seg.left_connections.is_empty()
                && !seg.is_empty()
            {
                to_remove_left.push(i);
            }
        }

        for &seg_id in &to_remove_left {
            let right_conns: Vec<(i32, Vec<(usize, i32)>)> = self.segments[seg_id]
                .right_connections
                .iter()
                .map(|(&k, v)| (k, v.clone()))
                .collect();

            for (ipos, partners) in right_conns {
                for (partner_id, partner_pos) in partners {
                    if let Some(conns) = self.segments[partner_id]
                        .left_connections
                        .get_mut(&partner_pos)
                    {
                        conns.retain(|&(id, pos)| id != seg_id || pos != ipos);
                        if conns.is_empty() {
                            self.segments[partner_id]
                                .left_connections
                                .remove(&partner_pos);
                        }
                    }
                }
            }
            self.segments[seg_id].right_connections.clear();
            self.segments[seg_id].seq.clear();
        }
    }

    /// Get all non-empty segments as sequences
    pub fn get_segments(&self) -> Vec<String> {
        self.segments
            .iter()
            .filter(|s| !s.is_empty())
            .map(|s| s.sequence_string())
            .collect()
    }

    /// Number of non-empty segments
    pub fn segment_count(&self) -> usize {
        self.segments.iter().filter(|s| !s.is_empty()).count()
    }

    /// Total bases in all segments
    pub fn total_bases(&self) -> usize {
        self.segments.iter().map(|s| s.len()).sum()
    }

    /// Get the assembled chain as a single sequence
    pub fn chain_sequence(&self) -> String {
        let mut result = String::new();
        for &(seg_id, _, _) in &self.last_segments {
            if seg_id < self.segments.len() {
                result.push_str(&self.segments[seg_id].sequence_string());
            }
        }
        result
    }

    /// Set the target sequence
    pub fn set_target(&mut self, target: &str) {
        self.target = target.to_string();
    }

    /// Get target reference
    pub fn target(&self) -> &str {
        &self.target
    }
}

/// Trim groups in the guided graph by removing poorly-supported segments.
/// Matches C++ CGuidedGraph::TrimGroups.
pub fn trim_groups(graph: &mut GuidedGraph, uniformity: f64, min_len: usize) {
    graph.remove_not_aligned_segments(uniformity);

    // Remove segments shorter than min_len that have no connections
    let to_remove: Vec<usize> = graph
        .segments
        .iter()
        .enumerate()
        .filter(|(_, s)| {
            s.len() < min_len
                && s.left_connections.is_empty()
                && s.right_connections.is_empty()
                && !s.is_empty()
        })
        .map(|(i, _)| i)
        .collect();

    for id in to_remove {
        graph.segments[id].seq.clear();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_guided_graph_basic() {
        let mut graph = GuidedGraph::new(21);
        graph.start_new_assembly("ACGTACGTACGTACGTACGTA", 0, 0);
        assert_eq!(graph.segment_count(), 1);
        assert_eq!(graph.total_bases(), 21);
    }

    #[test]
    fn test_add_right_segment() {
        let mut graph = GuidedGraph::new(21);
        graph.start_new_assembly("ACGTACGTACGTACGTACGTA", 0, 0);
        graph.add_right_segment(&['C', 'G', 'T', 'A'], 0);
        assert_eq!(graph.segment_count(), 2);
        assert_eq!(graph.total_bases(), 25);
    }

    #[test]
    fn test_add_left_segment() {
        let mut graph = GuidedGraph::new(21);
        graph.start_new_assembly("ACGTACGTACGTACGTACGTA", 0, 0);
        graph.add_left_segment(&['T', 'T', 'G', 'G'], 0);
        assert_eq!(graph.segment_count(), 2);
    }

    #[test]
    fn test_anchors() {
        let mut graph = GuidedGraph::new(21);
        graph.start_new_assembly("ACGTACGTACGTACGTACGTA", 0, 0);

        let key = vec![12345u64];
        graph.add_left_anchor(key.clone(), 100, 0, 0);
        assert!(graph.known_left_anchor(&key, 100).is_some());
        assert!(graph.known_left_anchor(&key, 200).is_none());
    }

    #[test]
    fn test_remove_not_aligned() {
        let mut graph = GuidedGraph::new(21);

        // Add a well-aligned segment
        let seq1: Vec<PathBase> = "ACGTACGTACGTACGTACGTA"
            .chars()
            .map(|c| PathBase { base: c })
            .collect();
        graph.segments.push(SeqSegment::new(seq1, 0, 0));

        // Add a poorly-aligned segment (not_aligned_right >> len)
        let seq2: Vec<PathBase> = "TTTT".chars().map(|c| PathBase { base: c }).collect();
        graph.segments.push(SeqSegment::new(seq2, 0, 100));

        assert_eq!(graph.segment_count(), 2);
        graph.remove_not_aligned_segments(1.0);
        assert_eq!(
            graph.segment_count(),
            1,
            "Poorly aligned segment should be removed"
        );
    }

    #[test]
    fn test_chain_sequence() {
        let mut graph = GuidedGraph::new(21);
        graph.start_new_assembly("ACGTACGTACGTACGTACGTA", 0, 0);
        graph.add_right_segment(&['C', 'G', 'T', 'A'], 0);
        let chain = graph.chain_sequence();
        assert_eq!(chain, "ACGTACGTACGTACGTACGTACGTA");
    }
}