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
use crate::gfa::{
    gfa_string,
    graph::{segments_subgraph, GFAdigraph, GFAungraph},
};
use crate::path::GFAPath;
use crate::stats::GenomeType;
use crate::utils::{
    self, get_edge_coverage, parse_cigar, reverse_complement, GFAGraphLookups, GFAGraphPair,
};
use anyhow::{bail, Context, Result};
use gfa::gfa::{Orientation, GFA};
use gfa::optfields::{OptFieldVal, OptionalFields};
use petgraph::graph::{Graph, NodeIndex, UnGraph};
use std::collections::HashMap;

/// A wrapper around GFA from the gfa crate
#[derive(Clone)]
pub struct GFAtk(pub GFA<usize, OptionalFields>);

impl GFAtk {
    /// Returns a tuple of GFAGraphLookups (a struct of indices/node names)
    /// and an undirected GFA graph structure.
    pub fn into_ungraph(&self) -> Result<(GFAGraphLookups, GFAungraph)> {
        // alias to get GFA out
        let gfa = &self.0;
        // we're reading in now
        eprintln!("[+]\tReading GFA into an undirected graph.");
        let mut gfa_graph: UnGraph<usize, ()> = Graph::new_undirected();

        let mut graph_indices = GFAGraphLookups::new();
        // read the segments into graph nodes
        // save the indexes for populating the edges
        for node in &gfa.segments {
            let index = gfa_graph.add_node(node.name);
            graph_indices.push(GFAGraphPair {
                node_index: index,
                seg_id: node.name,
            });
        }

        // populate the edges
        for edge in &gfa.links {
            let from = edge.from_segment;
            let to = edge.to_segment;

            // get the node index for a given edge (like a map)
            let from_index = graph_indices.seg_id_to_node_index(from)?;
            let to_index = graph_indices.seg_id_to_node_index(to)?;

            // add the edges
            gfa_graph.add_edge(from_index, to_index, ());
        }

        Ok((graph_indices, GFAungraph(gfa_graph)))
    }

    /// Returns a tuple of GFAGraphLookups (a struct of indices/node names) and an directed GFA graph structure.
    ///
    /// Most functionality of this binary is on directed graph structures
    pub fn into_digraph(&self) -> Result<(GFAGraphLookups, GFAdigraph)> {
        let gfa = &self.0;
        // eprintln!("[+]\tReading GFA into a directed graph.");
        let mut gfa_graph: Graph<usize, (Orientation, Orientation, Option<i64>)> = Graph::new();

        let mut graph_indices = GFAGraphLookups::new();
        // read the segments into graph nodes
        // save the indexes for populating the edges
        for node in &gfa.segments {
            let index = gfa_graph.add_node(node.name);
            graph_indices.push(GFAGraphPair {
                node_index: index,
                seg_id: node.name,
            });
        }

        // populate the edges
        for edge in &gfa.links {
            let from = edge.from_segment;
            let to = edge.to_segment;
            let from_orient = edge.from_orient;
            let to_orient = edge.to_orient;

            let ec = get_edge_coverage(&edge.optional)?;

            // get the node index for a given edge
            let from_index = graph_indices.seg_id_to_node_index(from)?;
            let to_index = graph_indices.seg_id_to_node_index(to)?;

            // add the edges
            gfa_graph.add_edge(from_index, to_index, (from_orient, to_orient, Some(ec)));
        }

        Ok((graph_indices, GFAdigraph(gfa_graph)))
    }

    /// A method to print a GFA to STDOUT, given a vector of sequence ID's to keep.
    pub fn print_extract(&self, sequences_to_keep: Vec<usize>) {
        let gfa = &self.0;
        let subgraph_gfa = GFAtk(segments_subgraph(gfa, sequences_to_keep));

        print!("{}", gfa_string(&subgraph_gfa.0));
    }

    /// Returns the overlaps between all the segments in a GFA.
    pub fn make_overlaps(&self, extend_length: usize) -> Result<Overlaps> {
        let gfa = &self.0;
        // tuple of (from: overlap - length (incl. overlap), to: overlap + length)
        let mut from_to = Overlaps::new();
        // outer loop over links
        for link in &gfa.links {
            // get all the info out of each link
            let from_segment = link.from_segment;
            let from_orient = link.from_orient;
            let to_segment = link.to_segment;
            let to_orient = link.to_orient;
            let overlap = parse_cigar(&link.overlap)?;

            eprintln!(
                "From segment {} ({}) to segment {} ({})\nOverlap: {}",
                from_segment, from_orient, to_segment, to_orient, overlap
            );

            let mut from_seq: &[u8] = &[];
            let mut to_seq: &[u8] = &[];

            // get the from and to sequences.
            for line in gfa.lines_iter() {
                // if we meet a segment, let's do something
                match line.some_segment() {
                    Some(s) => {
                        if s.name == from_segment && s.name == to_segment {
                            from_seq = &s.sequence;
                            to_seq = &s.sequence;
                        } else if s.name == from_segment {
                            from_seq = &s.sequence;
                        } else if s.name == to_segment {
                            to_seq = &s.sequence;
                        }
                    }
                    None => (),
                }
            }

            // initiate so we can append to vec
            let mut overlap_str_from_f: Option<String> = None;
            let mut overlap_str_from_r: Option<String> = None;
            let mut overlap_str_to_f: Option<String> = None;
            let mut overlap_str_to_r: Option<String> = None;

            // deal with the from's
            match from_orient {
                Orientation::Forward => {
                    // do nothing
                    // length - overlap - extend length at the end of the sequence.
                    let overlap_seq = &from_seq.get(from_seq.len() - overlap - extend_length..);
                    // if the extend length is too long, it means that
                    // we hit the start of the sequence, so take full slice.
                    let overlap_str = match overlap_seq {
                        Some(sl) => std::str::from_utf8(sl)
                            .with_context(|| format!("Malformed UTF8: {:?}", sl))?,
                        None => std::str::from_utf8(from_seq)
                            .with_context(|| format!("Malformed UTF8: {:?}", from_seq))?,
                    };
                    overlap_str_from_f = Some(overlap_str.to_string());
                }
                // if the relative negative strand matches
                // revcomp and take the end.
                Orientation::Backward => {
                    let revcomp = reverse_complement(from_seq);
                    // let overlap_revcomp = revcomp[revcomp.len() - overlap - extend_length..].to_vec();
                    let overlap_revcomp = revcomp.get(revcomp.len() - overlap - extend_length..);

                    let overlap_str = match overlap_revcomp {
                        Some(sl) => String::from_utf8(sl.to_vec())
                            .with_context(|| format!("Malformed UTF8: {:?}", sl))?,
                        // take the whole thing.
                        None => String::from_utf8(revcomp).context("Malformed UTF8.")?,
                    };

                    overlap_str_from_r = Some(overlap_str);
                }
            }
            // deal with the to's
            // here we ignore the overlap, as that is
            // captured above.
            match to_orient {
                Orientation::Forward => {
                    // do nothing
                    // let overlap = &to_seq[overlap..overlap + extend_length];
                    let overlap_seq = &to_seq.get(overlap..overlap + extend_length);

                    let overlap_str = match overlap_seq {
                        Some(sl) => std::str::from_utf8(sl)
                            .with_context(|| format!("Malformed UTF8: {:?}", sl))?,
                        // from end of overlap to the end of the sequence
                        None => std::str::from_utf8(&to_seq[overlap..])
                            .with_context(|| format!("Malformed UTF8: {:?}", &to_seq[overlap..]))?,
                    };

                    overlap_str_to_f = Some(overlap_str.to_string());
                }
                // if the relative negative strand matches
                // revcomp and take the start.
                Orientation::Backward => {
                    let revcomp = reverse_complement(to_seq);
                    // let overlap_revcomp = revcomp[overlap..overlap + extend_length].to_vec();
                    let overlap_revcomp = revcomp.get(overlap..overlap + extend_length);

                    let overlap_str =
                        match overlap_revcomp {
                            Some(sl) => String::from_utf8(sl.to_vec())
                                .with_context(|| format!("Malformed UTF8: {:?}", sl))?,
                            None => String::from_utf8(revcomp[overlap..].to_vec()).with_context(
                                || format!("Malformed UTF8: {:?}", revcomp[overlap..].to_vec()),
                            )?,
                        };

                    overlap_str_to_r = Some(overlap_str);
                }
            }

            from_to.push(Overlap {
                overlap_str_from_f,
                overlap_str_from_r,
                overlap_str_to_f,
                overlap_str_to_r,
                from_segment,
                to_segment,
                from_orient,
                to_orient,
            });
        }
        Ok(from_to)
    }

    /// The internal function called when `gfatk fasta` is called.
    ///
    /// Prints all segments of the GFA as-is.
    pub fn print_sequences(&self, subgraph_index_header: Option<String>) -> Result<()> {
        let gfa = &self.0;
        let subgraph_index_header = subgraph_index_header.unwrap_or("".to_string());

        for line in gfa.lines_iter() {
            match line.some_segment() {
                Some(s) => {
                    let seq = std::str::from_utf8(&s.sequence)
                        .with_context(|| format!("Malformed UTF8: {:?}", &s.sequence))?;
                    let id = s.name;
                    println!(">{}{}\n{}", id, subgraph_index_header, seq);
                }
                None => (),
            }
        }
        Ok(())
    }

    /// Two internal functions below to parse coverage of a GFA segment.
    ///
    /// Used in `gfatk stats`.
    fn parse_coverage_opt(opt: &OptFieldVal) -> Result<&f32> {
        let ll = match opt {
            OptFieldVal::Float(f) => f,
            _ => bail!("ll: coverage should be Float()"),
        };
        Ok(ll)
    }

    fn get_coverage(&self) -> Result<f32> {
        let gfa = &self.0;

        let ll_tag: [u8; 2] = [108, 108];
        let mut ll_tag_vec = Vec::new();

        for seg in &gfa.segments {
            let opts = &seg.optional;
            for opt in opts {
                if opt.tag == ll_tag {
                    ll_tag_vec.push(Self::parse_coverage_opt(&opt.value)?);
                }
            }
        }
        let len = ll_tag_vec.len() as f32;
        let sum: f32 = ll_tag_vec.iter().fold(0.0, |a, b| a + **b);

        Ok(sum / len)
    }

    /// Return the coverage and sequence length for a segment, given a segment name.
    ///
    /// Note segment names are always `usize`.
    pub fn node_seq_len_and_cov(&self, node: usize) -> Result<(usize, f32)> {
        let gfa = &self.0;

        let ll_tag: [u8; 2] = [108, 108];
        let mut seq_len = None;
        let mut cov = None;

        for segment in &gfa.segments {
            if segment.name == node {
                seq_len = Some(segment.sequence.len());
                let opt = &segment.optional;
                for c in opt {
                    if c.tag == ll_tag {
                        cov = Some(*Self::parse_coverage_opt(&c.value)?);
                        break;
                    }
                }
            }
        }

        Ok((
            seq_len.context("No sequence length for each segment in GFA.")?,
            cov.context("No segment coverage for each segment in GFA.")?,
        ))
    }

    /// The internal function called in `gfatk stats`.
    ///
    /// Returns average GC%, average coverage, and total sequence length for a GFA (sub)graph.
    pub fn sequence_stats(
        &self,
        genome_type: GenomeType,
        tabular: bool,
    ) -> Result<(f32, f32, usize)> {
        let gfa = &self.0;

        let cov = Self::get_coverage(self)?;

        let mut total_overlap_length = 0;
        for link in &gfa.links {
            total_overlap_length += parse_cigar(&link.overlap)?;
        }

        let mut total_sequence_length = 0;
        let mut gc_vec = Vec::new();

        for segment in &gfa.segments {
            let seq = &segment.sequence;
            total_sequence_length += seq.len();

            let gc = utils::gc_content(seq);
            gc_vec.push(gc);
        }

        let avg_gc = gc_vec.iter().sum::<f32>() / gc_vec.len() as f32;

        if !tabular && genome_type == GenomeType::None {
            println!("\tTotal sequence length:\t{}", total_sequence_length);
            println!("\tTotal sequence overlap length:\t{}", total_overlap_length);
            println!(
                "\tSequence length minus overlaps:\t{}",
                total_sequence_length as i32 - total_overlap_length as i32
            );
            println!("\tGC content of total sequence:\t{}", avg_gc);
            println!("\tAverage coverage of total segments:\t{}", cov);
        }

        Ok((avg_gc, cov, total_sequence_length))
    }

    /// Returns a `HashMap` of relative coverage of each node (segment) in the GFA.
    ///
    /// Relative here indicates that each segment coverage is divided by the lowest coverage node, and rounded.
    pub fn gen_cov_hash(
        &self,
        graph_lookup: &GFAGraphLookups,
    ) -> Result<HashMap<NodeIndex, usize>> {
        let gfa = &self.0;

        let ll_tag: [u8; 2] = [108, 108];
        let mut node_cov_map = HashMap::new();

        // the initial map contains node index and coverage
        for seg in &gfa.segments {
            // get the coverage
            let opts = &seg.optional;
            let node_index = graph_lookup.seg_id_to_node_index(seg.name)?;

            for opt in opts {
                if opt.tag == ll_tag {
                    let cov = Self::parse_coverage_opt(&opt.value)?;
                    node_cov_map.insert(node_index, *cov);
                }
            }
        }

        // we want to convert the node index and coverage
        // to node index and *relative* coverage
        let mut lowest_cov_iter = node_cov_map.iter().map(|(_k, v)| v).enumerate();
        let init = lowest_cov_iter
            .next()
            .context("No coverage information found in this GFA.")?;
        // we process the rest
        let result = lowest_cov_iter.try_fold(init, |acc, x| {
            // return None if x is NaN
            let cmp = x.1.partial_cmp(acc.1)?;
            // if x is less than the acc
            let min = if let std::cmp::Ordering::Less = cmp {
                x
            } else {
                acc
            };
            Some(min)
        });

        // allocate to a new map as we want u32's
        let mut rel_cov_map = HashMap::new();

        for (k, v) in &node_cov_map {
            rel_cov_map.insert(*k, (v / result.unwrap().1).round() as usize);
        }

        Ok(rel_cov_map)
    }

    /// Take a [`GFAPath`] and print out the path
    /// from a GFA.
    ///
    /// Currently implemented requires two loops of the GFA, and storage
    /// of the sequences in a [`HashMap`].
    pub fn from_path_cli(
        &self,
        path: GFAPath,
        link_map: HashMap<String, usize>,
        call: &str,
        fasta_header: Option<&str>,
    ) -> Result<()> {
        let gfa = &self.0;

        // put all the segments in memory - easiest way for now.
        let mut seg_map = HashMap::new();

        for seg in &gfa.segments {
            let id = seg.name;
            let seq = seg.sequence.clone();

            seg_map.insert(id, seq);
        }

        match call {
            "path" => println!(">{}", path.to_fasta_header()),
            "linear" => println!(">{}", fasta_header.unwrap()),
            _ => bail!("Should never reach here."),
        }

        // if we have only one segment, print this fully
        if path.inner.len() == 1 {
            match path.inner[0].orientation {
                Orientation::Forward => {
                    let seq = seg_map
                        .get(&path.inner[0].segment_id)
                        .context("This segment ID does not exist in the GFA")?
                        .to_vec();
                    println!("{}", std::str::from_utf8(&seq)?);
                }
                Orientation::Backward => {
                    let seq = seg_map
                        .get(&path.inner[0].segment_id)
                        .context("This segment ID does not exist in the GFA")?;
                    println!("{}", std::str::from_utf8(&utils::reverse_complement(seq))?);
                }
            }
            // probably not strictly necessary, but nice to be explicit in the return.
            return Ok(());
        }

        // now iterate over the path itself
        for path_el in path.inner.windows(2) {
            // from
            let seg_id_from = path_el[0].segment_id;
            let orientation_from = path_el[0].orientation;
            // to
            let seg_id_to = path_el[1].segment_id;
            let orientation_to = path_el[1].orientation;

            // format so we can match on the links map
            let cigar_match = format!(
                "{}{}|{}{}",
                seg_id_from, orientation_from, seg_id_to, orientation_to
            );

            let overlap = *link_map.get(&cigar_match).context(format!(
                "This link: {} - does not occur in the input GFA. Perhaps re-consider the input path?",
                cigar_match
            ))?;

            // for the first element in the path
            // we print the entire sequence
            if path_el[0].index == 0 {
                let sequence = match orientation_from {
                    Orientation::Forward => seg_map.get(&seg_id_from).unwrap().to_vec(),
                    Orientation::Backward => {
                        let seq = seg_map.get(&seg_id_from).unwrap();
                        utils::reverse_complement(seq)
                    }
                };

                // and we must print out this second sequence, otherwise it's skipped!
                let sequence2 = match orientation_to {
                    Orientation::Forward => seg_map.get(&seg_id_to).unwrap()[overlap..].to_vec(),
                    Orientation::Backward => {
                        let seq = seg_map.get(&seg_id_to).unwrap();
                        utils::reverse_complement(seq)[overlap..].to_vec()
                    }
                };

                print!("{}", std::str::from_utf8(&sequence)?);
                print!("{}", std::str::from_utf8(&sequence2)?);
            } else {
                // otherwise we print the second element of the windows :)
                // and these are all dealt with in the same way
                let sequence = match orientation_to {
                    Orientation::Forward => seg_map.get(&seg_id_to).unwrap()[overlap..].to_vec(),
                    Orientation::Backward => {
                        let seq = seg_map.get(&seg_id_to).unwrap();
                        utils::reverse_complement(seq)[overlap..].to_vec()
                    }
                };

                print!("{}", std::str::from_utf8(&sequence)?);
            }
        }

        // newline
        println!();

        Ok(())
    }
}

/// Overlap from one segment to another.
pub struct Overlap {
    /// From segment forward.
    pub overlap_str_from_f: Option<String>,
    /// From segment reverse.
    pub overlap_str_from_r: Option<String>,
    /// To segment forward.
    pub overlap_str_to_f: Option<String>,
    /// To segment reverse.
    pub overlap_str_to_r: Option<String>,
    /// ID of from segment.
    pub from_segment: usize,
    /// ID of to segment.
    pub to_segment: usize,
    /// Orientation of from segment.
    pub from_orient: Orientation,
    /// Orientation of to segment.
    pub to_orient: Orientation,
}

/// A vector of `Overlap` structs.
pub struct Overlaps(Vec<Overlap>);

impl Overlaps {
    /// Create a new instance of `Overlaps`.
    fn new() -> Self {
        Self(Vec::new())
    }
    /// Append to `Overlaps`, adding another `Overlap`.
    fn push(&mut self, add: Overlap) {
        self.0.push(add)
    }
    /// Print overlaps to STDOUT.
    pub fn print(self, extend_length: usize) {
        // long winded...
        for o in self.0 {
            // unwrap None -> zero length string.
            let ff = o.overlap_str_from_f.unwrap_or("".to_string());
            let fr = o.overlap_str_from_r.unwrap_or("".to_string());
            let tf = o.overlap_str_to_f.unwrap_or("".to_string());
            let tr = o.overlap_str_to_r.unwrap_or("".to_string());
            let from_seg = o.from_segment;
            let to_seg = o.to_segment;
            let from_orient = o.from_orient;
            let to_orient = o.to_orient;

            println!(
                ">{}({})->{}({}): extend = {}\n{}{}{}{}",
                from_seg, from_orient, to_seg, to_orient, extend_length, ff, fr, tf, tr
            );
        }
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    use crate::load::load_gfa;
    use crate::stats::GenomeType;

    // the GFA -> GFAtk structure used in tests below.
    fn make_gfa(path: &str) -> GFAtk {
        GFAtk(load_gfa(path).unwrap())
    }

    #[test]
    fn test_gfa_sequence_stats() {
        let gfa = make_gfa("./tests/test_linear.gfa");

        // could be mitochondria/chloroplast
        let (gc, cov, len) = gfa.sequence_stats(GenomeType::Mitochondria, false).unwrap();

        assert!(cov == 40.0);
        assert!(gc == 0.39523807);
        assert!(len == 18);
    }

    #[test]
    fn test_gen_cov_hash() {
        let gfa = make_gfa("./tests/test_linear.gfa");

        let lookup = GFAGraphLookups(vec![
            crate::utils::GFAGraphPair {
                node_index: NodeIndex::new(0),
                seg_id: 11,
            },
            crate::utils::GFAGraphPair {
                node_index: NodeIndex::new(1),
                seg_id: 12,
            },
            crate::utils::GFAGraphPair {
                node_index: NodeIndex::new(2),
                seg_id: 13,
            },
        ]);

        let cov_hash = gfa.gen_cov_hash(&lookup).unwrap();

        assert_eq!(cov_hash.get(&NodeIndex::new(0)).unwrap(), &1);
        assert_eq!(cov_hash.get(&NodeIndex::new(1)).unwrap(), &2);
        assert_eq!(cov_hash.get(&NodeIndex::new(2)).unwrap(), &1);
    }
}