segul 0.23.2

An ultrafast and memory-efficient tool for phylogenomics
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
//! Parse Multiple Alignment Format (MAF) files.
//!
//!
//! MAF format is developed by UCSC. It stores multiple sequence alignment in a
//! line-base human readable format. It uses by several aligners, such as LASTZ
//! and Progressive Cactus.
//!
//! # Example
//!
//! ```text
//! ##maf version=1 scoring=roast.v2.0
//! a score=0.000000
//! s scaffold_1 0 10 + 10 ATCGATCGAT
//! s scaffold_2 0 10 + 10 ATCGATCGAT
//!
//! a score=0.000000
//! s scaffold_1 10 10 + 10 ATCGATCGAT
//! s scaffold_2 10 10 + 10 ATCGATCGAT
//! ```
//!
//! The line with `##maf` is the header line. The line with `a` is the alignment
//! line. The line with `s` is the sequence line. It some cases, there are `q`
//! lines, which are the quality lines.
use std::{
    error::Error,
    fmt::Display,
    io::{prelude::*, BufReader},
    str::FromStr,
};

use nom::{
    bytes::complete,
    character,
    combinator::{self},
    sequence, IResult,
};

use crate::helper::types::DnaStrand;

use super::{END_OF_LINE, EOF};

#[cfg(target_os = "windows")]
use super::CAR_RETURN;

/// We can think of a paragraph as a block of text that is separated by a blank
/// line. In MAF format, there are two types of paragraphs: header and alignment.
/// Source: http://genome.ucsc.edu/FAQ/FAQformat.html#format5
pub enum MafParagraph {
    Track(TrackLine),
    Header(MafHeader),
    Comments(String),
    Alignment(MafAlignment),
    Empty,
    Unknown,
}

#[derive(Debug, Default)]
pub struct MafHeader {
    pub version: String,
    pub scoring: Option<String>,
    pub program: Option<String>,
}

impl MafHeader {
    pub fn new() -> Self {
        MafHeader {
            version: String::new(),
            scoring: None,
            program: None,
        }
    }

    pub fn from_str(&mut self, line: &str) -> Result<(), Box<dyn Error>> {
        let mut parts = line.split_whitespace();

        let _ = parts.next(); // Skip the first character

        self.version = self.parse_version(parts.next().unwrap())?;
        self.scoring = parts.next().map(|s| self.parse_scoring(s)).transpose()?;
        self.program = parts.next().map(|s| s.to_string());

        Ok(())
    }

    fn parse_version(&self, value: &str) -> Result<String, Box<dyn Error>> {
        let tag: IResult<&str, &str> = sequence::preceded(
            complete::tag("version="),
            character::complete::alphanumeric1,
        )(value);
        match tag {
            Ok((_, value)) => Ok(value.to_string()),
            Err(_) => Err("Error parsing version".into()),
        }
    }

    fn parse_scoring(&self, value: &str) -> Result<String, Box<dyn Error>> {
        let tag: IResult<&str, &str> = sequence::preceded(
            complete::tag("scoring="),
            complete::take_while(|c: char| c.is_alphanumeric() || c == '.'),
        )(value);
        match tag {
            Ok((_, value)) => Ok(value.to_string()),
            Err(_) => Err("Error parsing scoring".into()),
        }
    }
}

pub struct TrackLine {
    pub name: String,
    pub description: Option<String>,
    pub frames: Option<String>,
    pub maf_dot: bool,
    pub visibility: Option<String>,
    pub species_order: Option<String>,
}

impl Default for TrackLine {
    fn default() -> Self {
        Self::new()
    }
}

impl TrackLine {
    pub fn new() -> Self {
        TrackLine {
            name: String::new(),
            description: None,
            frames: None,
            maf_dot: false,
            visibility: None,
            species_order: None,
        }
    }

    pub fn from_str(&mut self, line: &str) -> Result<(), Box<dyn Error>> {
        let (_, tract) = self.parse_track(line).unwrap();
        let (_, name) = self.parse_name(tract).unwrap();

        self.name = name.to_string();

        if tract.contains("description") {
            let input = self.capture_tags(tract, "description=");
            if let Some(input) = input {
                let (_, description) = self.parse_description(&input).unwrap_or_default();
                self.description = description.map(|s| s.to_string());
            }
        }

        if tract.contains("frames") {
            let input = self.capture_tags(tract, "frames=");
            if let Some(input) = input {
                let (_, frames) = self.parse_frames(input.as_str()).unwrap_or_default();
                self.frames = frames.map(|s| s.to_string());
            }
        }

        if tract.contains("mafDot") {
            let input = self.capture_tags(tract, "mafDot=");
            if let Some(input) = input {
                let (_, maf_dot) = self.parse_maf_dot(input.as_str()).unwrap_or_default();
                self.maf_dot = maf_dot;
            }
        }

        if tract.contains("visibility") {
            let input = self.capture_tags(tract, "visibility=");
            if let Some(input) = input {
                let (_, visibility) = self.parse_visibility(input.as_str()).unwrap_or_default();
                self.visibility = visibility.map(|s| s.to_string());
            }
        }

        if tract.contains("speciesOrder") {
            let input = self.capture_tags(tract, "speciesOrder=");
            if let Some(input) = input {
                let (_, species_order) =
                    self.parse_species_order(input.as_str()).unwrap_or_default();
                self.species_order = species_order.map(|s| s.to_string());
            }
        }

        Ok(())
    }

    fn parse_track<'a>(&mut self, input: &'a str) -> IResult<&'a str, &'a str> {
        let track: IResult<&str, &str> =
            sequence::preceded(complete::tag("track "), complete::take_until("\n"))(input);
        track
    }

    fn parse_name<'a>(&mut self, input: &'a str) -> IResult<&'a str, &'a str> {
        let name: IResult<&str, &str> =
            sequence::preceded(complete::tag("name="), character::complete::alphanumeric1)(input);
        name
    }

    fn parse_description<'a>(&mut self, input: &'a str) -> IResult<&'a str, Option<&'a str>> {
        let description: IResult<&str, Option<&str>> = combinator::opt(sequence::preceded(
            complete::tag("description="),
            sequence::delimited(
                character::complete::char('"'),
                complete::take_while(|c: char| c != '"'),
                character::complete::char('"'),
            ),
        ))(input);

        description
    }

    fn parse_frames<'a>(&mut self, input: &'a str) -> IResult<&'a str, Option<&'a str>> {
        let (input, _) = character::complete::space0(input)?;
        let frames: IResult<&str, Option<&str>> = combinator::opt(sequence::preceded(
            complete::tag("frames="),
            character::complete::alphanumeric1,
        ))(input);
        frames
    }

    fn parse_maf_dot<'a>(&mut self, input: &'a str) -> IResult<&'a str, bool> {
        let (input, _) = character::complete::space0(input)?;
        let maf_dot: IResult<&str, &str> =
            sequence::preceded(complete::tag("mafDot="), character::complete::alphanumeric1)(input);
        match maf_dot {
            Ok((_, value)) => Ok((input, value == "on")),
            Err(_) => Ok((input, false)),
        }
    }

    fn parse_visibility<'a>(&mut self, input: &'a str) -> IResult<&'a str, Option<&'a str>> {
        let (input, _) = character::complete::space0(input)?;
        combinator::opt(sequence::preceded(
            complete::tag("visibility="),
            character::complete::alphanumeric1,
        ))(input)
    }

    fn parse_species_order<'a>(&mut self, input: &'a str) -> IResult<&'a str, Option<&'a str>> {
        let (input, _) = character::complete::space0(input)?;
        let species_order: IResult<&str, Option<&str>> = combinator::opt(sequence::preceded(
            complete::tag("speciesOrder="),
            sequence::delimited(
                character::complete::char('"'),
                complete::take_until("\""),
                character::complete::char('"'),
            ),
        ))(input);
        species_order
    }

    // Split the line at tags and the second part is the value.
    fn capture_tags(&self, input: &str, tag: &str) -> Option<String> {
        // use find pos to get the position of the first space
        let pos = input.find(tag);
        pos.map(|pos| input[pos..].to_string())
    }
}

#[derive(Debug, PartialEq)]
pub enum MafVisibility {
    Dense,
    Pack,
    Full,
}

impl Display for MafVisibility {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MafVisibility::Dense => write!(f, "dense"),
            MafVisibility::Pack => write!(f, "pack"),
            MafVisibility::Full => write!(f, "full"),
        }
    }
}

impl FromStr for MafVisibility {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "dense" => Ok(MafVisibility::Dense),
            "pack" => Ok(MafVisibility::Pack),
            "full" => Ok(MafVisibility::Full),
            _ => Err(format!("Unknown visibility: {}", s)),
        }
    }
}

/// The `a` line in MAF format.
/// It contains:
/// - `score`: the score of the alignment. At the the first line
/// - `sequences`: the `s` lines in the alignment.
/// - `information`: the `i` lines in the alignment. It is optional.
/// - `quality`: the `q` lines in the alignment. It is optional.
///
pub struct MafAlignment {
    pub score: Option<f64>,
    pub sequences: Vec<MafSequence>,
    pub quality: Option<Quality>,
    pub information: Option<MafInformation>,
    pub empty: Option<MafEmptyLine>,
}

impl Default for MafAlignment {
    fn default() -> Self {
        Self::new()
    }
}

impl MafAlignment {
    pub fn new() -> Self {
        MafAlignment {
            score: None,
            sequences: Vec::new(),
            quality: None,
            information: None,
            empty: None,
        }
    }

    pub fn add_sequence(&mut self, sequence: MafSequence) {
        self.sequences.push(sequence);
    }

    pub fn parse_scores(&mut self, value: &str) {
        let tag: IResult<&str, &str> = sequence::preceded(
            complete::tag("a score="),
            complete::take_while(|c: char| c.is_ascii_digit() || c == '.'),
        )(value);

        match tag {
            Ok((_, value)) => {
                self.score = value.parse::<f64>().ok();
            }
            Err(_) => self.score = None, // If parsing fails, set score to None
        }
    }
}

pub struct MafSequence {
    pub source: String,
    pub start: usize,
    pub size: usize,
    pub strand: DnaStrand,
    pub src_size: usize,
    pub text: Vec<u8>,
}

impl MafSequence {
    pub fn from_buf(line: &[u8]) -> Result<Self, Box<dyn Error>> {
        let mut parts = line
            .split(|b| b.is_ascii_whitespace())
            .filter(|b| !b.is_empty());

        let _ = parts.next(); // Skip the first character

        let source = parts.next().unwrap_or_default();
        let start = parts.next().unwrap_or_default();
        let size = parts.next().unwrap_or_default();
        let strand = parts.next().unwrap_or_default();
        let src_size = parts.next().unwrap_or_default();
        let text = parts.next().unwrap_or_default();

        Ok(MafSequence {
            source: String::from_utf8_lossy(source).to_string(),
            start: String::from_utf8_lossy(start).parse().unwrap_or_default(),
            size: String::from_utf8_lossy(size).parse().unwrap_or_default(),
            strand: DnaStrand::from_char(
                String::from_utf8_lossy(strand)
                    .chars()
                    .next()
                    .unwrap_or_default(),
            ),
            src_size: String::from_utf8_lossy(src_size)
                .parse()
                .unwrap_or_default(),
            text: text.to_vec(),
        })
    }
}

/// The `i` line in MAF format.
/// It describes the information before and after the block of sequences.
/// It contains:
/// - `src`: the source of the information.
/// - `leftStatus`: a character that specifies the relationship between the
///     sequence in the current block and the sequence in the previous block.
/// - `leftCount`: the number of bases in the aligning species between the start and
///     the end of the previous block.
/// - `rightStatus`: a character that specifies the relationship between the
///     sequence in the current block and the sequence in the subsequent block.
/// - `leftValues`: the values of the information before the block of sequences.
/// - `rightCount`: the number of bases in the aligning species between the start of the
///    current block and the start of the next block.
pub struct MafInformation {
    pub source: String,
    pub left_status: char,
    pub left_count: usize,
    pub right_status: char,
    pub right_count: usize,
}

impl MafInformation {
    pub fn from_buf(line: &[u8]) -> Result<Self, Box<dyn Error>> {
        let mut parts = line.split(|b| b.is_ascii_whitespace());

        let _ = parts.next(); // Skip the first character

        let source = parts.next().unwrap_or_default();
        let left_status = parts.next().unwrap_or_default();
        let left_count = parts.next().unwrap_or_default();
        let right_status = parts.next().unwrap_or_default();
        let right_count = parts.next().unwrap_or_default();

        Ok(MafInformation {
            source: String::from_utf8_lossy(source).to_string(),
            left_status: String::from_utf8_lossy(left_status)
                .chars()
                .next()
                .unwrap_or_default(),
            left_count: String::from_utf8_lossy(left_count)
                .parse()
                .unwrap_or_default(),
            right_status: String::from_utf8_lossy(right_status)
                .chars()
                .next()
                .unwrap_or_default(),
            right_count: String::from_utf8_lossy(right_count)
                .parse()
                .unwrap_or_default(),
        })
    }
}

/// This is the `e` line in MAF format.
pub struct MafEmptyLine {
    pub source: String,
    pub size: u64,
    pub strand: char,
    pub src_size: u64,
}

impl MafEmptyLine {
    pub fn from_buf(line: &[u8]) -> Result<Self, Box<dyn Error>> {
        let mut parts = line.split(|b| b.is_ascii_whitespace());

        let _ = parts.next(); // Skip the first character

        let source = parts.next().unwrap_or_default();
        let size = parts.next().unwrap_or_default();
        let strand = parts.next().unwrap_or_default();
        let src_size = parts.next().unwrap_or_default();

        Ok(MafEmptyLine {
            source: String::from_utf8_lossy(source).to_string(),
            size: String::from_utf8_lossy(size).parse().unwrap_or_default(),
            strand: String::from_utf8_lossy(strand)
                .chars()
                .next()
                .unwrap_or_default(),
            src_size: String::from_utf8_lossy(src_size)
                .parse()
                .unwrap_or_default(),
        })
    }
}

pub struct Quality {
    /// The name of the source sequence.
    pub source: String,
    /// We store the quality values as characters.
    /// It can be 0-9 or F. The F means finished.
    pub values: Vec<char>,
}

impl Quality {
    pub fn from_buf(line: &[u8]) -> Result<Self, Box<dyn Error>> {
        let mut parts = line.split(|b| b.is_ascii_whitespace());

        let _ = parts.next(); // Skip the first character

        let source = parts.next().unwrap_or_default();
        let values = parts.next().unwrap_or_default();

        Ok(Quality {
            source: String::from_utf8_lossy(source).to_string(),
            values: String::from_utf8_lossy(values).chars().collect(),
        })
    }
}

#[derive(Debug)]
pub struct MafReader<R> {
    pub reader: BufReader<R>,
    pub buf: Vec<u8>,
}

impl<R: Read> MafReader<R> {
    pub fn new(file: R) -> Self {
        MafReader {
            reader: BufReader::new(file),
            buf: Vec::new(),
        }
    }

    /// Read the next paragraph from the file.
    /// In MAF terms, a paragraph is a block of text that is separated by a blank line.
    /// It can be a header, a track line,or an alignment.
    pub fn next_paragraph(&mut self) -> Option<MafParagraph> {
        let bytes = self
            .reader
            .read_until(END_OF_LINE, &mut self.buf)
            .expect("Error reading file");
        if bytes == EOF {
            return None;
        }

        #[cfg(target_os = "windows")]
        self.check_carriage_return();

        // Check the first character of the line
        let paragraph = match self.buf[0] {
            b'#' => self.parse_header(),
            b't' => self.parse_track_line(),
            b'a' => self.parse_alignments(),
            b'\n' => Some(MafParagraph::Empty),
            _ => Some(MafParagraph::Unknown),
        };

        self.buf.clear();

        paragraph
    }

    // Check carriage return for windows
    #[cfg(target_os = "windows")]
    fn check_carriage_return(&mut self) {
        if self.buf.contains(&CAR_RETURN) {
            // Remove the carriage return
            // and leave only the line feed
            self.buf.retain(|&c| c != CAR_RETURN);
        }
    }

    fn parse_header(&mut self) -> Option<MafParagraph> {
        // We convert to a string since this line is short
        // and we can parse it easily.
        let line = String::from_utf8_lossy(&self.buf);
        if line.starts_with("##maf") {
            let mut header = MafHeader::new();
            header.from_str(&line).unwrap();
            self.buf.clear();
            Some(MafParagraph::Header(header))
        } else {
            // It must have been a comment if it does not start with ##maf
            // Typically it starts with a single #
            self.parse_comments()
        }
    }

    fn parse_comments(&mut self) -> Option<MafParagraph> {
        let line = String::from_utf8_lossy(&self.buf);
        Some(MafParagraph::Comments(line.to_string()))
    }

    fn parse_track_line(&mut self) -> Option<MafParagraph> {
        let line = String::from_utf8_lossy(&self.buf);
        let mut track = TrackLine::new();
        track.from_str(&line).unwrap();
        Some(MafParagraph::Track(track))
    }

    fn parse_alignments(&mut self) -> Option<MafParagraph> {
        let mut alignment = MafAlignment::new();
        let buf = String::from_utf8_lossy(&self.buf).to_string();
        alignment.parse_scores(&buf);
        self.buf.clear();
        loop {
            let bytes = self
                .reader
                .read_until(END_OF_LINE, &mut self.buf)
                .expect("Error reading file");
            if bytes == EOF {
                break;
            }

            match self.buf[0] {
                b's' => {
                    let sequence =
                        MafSequence::from_buf(&self.buf).expect("Error parsing sequence");
                    alignment.sequences.push(sequence);
                }
                b'q' => {
                    let quality = Quality::from_buf(&self.buf).unwrap();
                    alignment.quality = Some(quality);
                }
                b'i' => {
                    let information = MafInformation::from_buf(&self.buf).unwrap();
                    alignment.information = Some(information);
                }
                b'e' => {
                    let empty = MafEmptyLine::from_buf(&self.buf).unwrap();
                    alignment.empty = Some(empty);
                }
                END_OF_LINE | b' ' => break,

                #[cfg(target_os = "windows")]
                CAR_RETURN => break,
                _ => {}
            }
            self.buf.clear();
        }

        Some(MafParagraph::Alignment(alignment))
    }
}

impl<R: Read> Iterator for MafReader<R> {
    type Item = MafParagraph;

    fn next(&mut self) -> Option<Self::Item> {
        self.next_paragraph()
    }
}

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

    #[test]
    fn test_parse_header() {
        let line = "##maf version=1 scoring=roast.v2.0\n";
        let mut header = MafHeader::new();
        header.from_str(line).unwrap();
        assert_eq!(header.version, "1");
        assert_eq!(header.scoring, Some(String::from("roast.v2.0")));
    }

    #[test]
    fn test_parse_track_line() {
        let line = "track name=chr1 description=\"Human chromosome\" visibility=pack\n";
        let mut track = TrackLine::new();
        track.from_str(line).unwrap();
        track.from_str(line).unwrap();
        assert_eq!(track.name, "chr1");
        assert_eq!(track.description, Some(String::from("Human chromosome")));
        assert_eq!(track.visibility, Some(String::from("pack")));
    }

    #[test]
    fn test_score() {
        let line = "a score=0.000000\n";
        let mut alignment = MafAlignment::new();
        alignment.parse_scores(line);
        assert_eq!(alignment.score, Some(0.0));
    }

    #[test]
    fn test_score_int() {
        let line = "a score=1234\n";
        let mut alignment = MafAlignment::new();
        alignment.parse_scores(line);
        assert_eq!(alignment.score, Some(1234.0));
    }

    #[test]
    fn parse_name() {
        let line = "name=chr1";
        let mut track = TrackLine::new();
        let name = track.parse_name(line).unwrap();
        let (_, res) = name;
        assert_eq!(res, "chr1");
    }

    #[test]
    fn parse_description() {
        let line = " description=\"Human chromosome\"";
        let line2 = " description=\"Human chromosome\" visibility=pack";
        let mut track = TrackLine::new();
        let tags = track.capture_tags(line, "description=").unwrap();
        let description = track.parse_description(&tags).unwrap();
        let (_, res) = description;
        assert_eq!(res, Some("Human chromosome"));
        let tags2 = track.capture_tags(line2, "description=").unwrap();
        let description2 = track.parse_description(&tags2).unwrap();
        let (_, res2) = description2;
        assert_eq!(res2, Some("Human chromosome"));
    }

    #[test]
    fn parse_frames() {
        let line = " frames=multiz28wayFrames";
        let mut track = TrackLine::new();
        let frames = track.parse_frames(line).unwrap();
        let (_, res) = frames;
        assert_eq!(res, Some("multiz28wayFrames"));
    }

    #[test]
    fn parse_maf_dot() {
        let line = " mafDot=on";
        let mut track = TrackLine::new();
        let tags = track.capture_tags(line, "mafDot=").unwrap();
        let maf_dot = track.parse_maf_dot(&tags).unwrap();
        let (_, res) = maf_dot;
        assert_eq!(res, true);
    }

    #[test]
    fn parse_visibility() {
        let line = " visibility=pack";
        let mut track = TrackLine::new();
        let tags = track.capture_tags(line, "visibility=").unwrap();
        let visibility = track.parse_visibility(&tags).unwrap();
        let (_, res) = visibility;
        assert_eq!(res, Some("pack"));

        let complete_line = " description=\"Human chromosome\" visibility=pack\n";
        let tags2 = track.capture_tags(complete_line, "visibility=").unwrap();
        let visibility2 = track.parse_visibility(&tags2).unwrap();
        let (_, res2) = visibility2;
        assert_eq!(res2, Some("pack"));
    }

    #[test]
    fn parse_species_order() {
        let line = " speciesOrder=\"hg18 panTro2\"";
        let mut track = TrackLine::new();
        let species_order = track.parse_species_order(line).unwrap();
        let (_, res) = species_order;
        assert_eq!(res, Some("hg18 panTro2"));
    }

    #[test]
    fn test_parse_maf_file() {
        let file = std::fs::File::open("tests/files/maf/simple.maf").unwrap();
        let reader = MafReader::new(file);
        let mut alignments = Vec::new();
        for paragraph in reader {
            match paragraph {
                MafParagraph::Track(track) => {
                    assert_eq!(track.name, "euArc");
                    assert_eq!(track.description, Some(String::from("Primate chromosomes")));
                    assert_eq!(track.visibility, Some(String::from("pack")));
                }
                MafParagraph::Header(header) => {
                    assert_eq!(header.version, "1");
                    assert_eq!(header.scoring, Some(String::from("tba.v8")));
                }
                MafParagraph::Alignment(alignment) => {
                    alignments.push(alignment);
                }
                _ => {}
            }
        }

        assert_eq!(alignments.len(), 3);
        assert_eq!(alignments[0].score, Some(23262.0));
    }

    #[test]
    fn test_capture_tags() {
        let line = "track name=chr1 description=\"Human chromosome\" visibility=pack";
        let track = TrackLine::new();
        let tags = track.capture_tags(line, "description=");
        assert_eq!(
            tags,
            Some("description=\"Human chromosome\" visibility=pack".to_string())
        );
    }
}