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
//! Parsing utilities

use crate::*;
use std::str::FromStr;

impl FromStr for Cgats {
    type Err = BoxErr;
    fn from_str(s: &str) -> Result<Self> {
        let mut cgats = Cgats::new();

        let mut lines = s.lines()
            .flat_map(|line| line.split('\r'))
            .map(|line| line.trim_end());

        cgats.vendor = lines.next().ok_or("NO DATA")?.parse()?;

        let mut push = Push::MetaData;
        let (mut hit_format, mut hit_data) = (false, false);
        for next in lines {
            match next.trim() {
                "BEGIN_DATA_FORMAT" => {
                    push = Push::DataFormat;
                    hit_format = true;
                    continue;
                }
                "END_DATA_FORMAT" => {
                    push = Push::MetaData;
                    cgats.data = Vec::with_capacity(cgats.len());
                    continue;
                }
                "BEGIN_DATA" => {
                    push = Push::Data;
                    hit_data = true;
                    continue;
                }
                "END_DATA" => {
                    push = Push::Stop;
                    continue;
                }
                _ => (),
            }

            match &push {
                Push::MetaData => cgats.metadata.push(next.parse()?),
                Push::DataFormat => cgats.data_format = next.parse()?,
                Push::Data => {
                    for val in next.split_whitespace() {
                        cgats.data.push(val.parse()?);
                    }
                }
                Push::Stop => (),
            }
        }

        // Set the implied ColorBurst DataFormat
        if cgats.vendor == Vendor::ColorBurst {
            cgats.data_format = DataFormat::colorburst();
        }

        // Force SAMPLE_ID into [`DataPoint::Int`]
        if let Some(index) = cgats.index_by_field(&SAMPLE_ID) {
            let id_column = cgats.get_col_mut(index);
            for id in id_column {
                *id = id.to_int().unwrap_or_else(|_| id.to_owned());
            }
        }

        // Return an error if we didn't hit BEGIN_DATA_FORMAT tag
        if !hit_format && cgats.vendor != Vendor::ColorBurst {
            return err!("DATA_FORMAT tag not found");
        }

        // Return an error if we didn't hit BEGIN_DATA tag
        if !hit_data {
            return err!("BEGIN_DATA tag not found");
        }

        // Return an error if there is data without a format
        if cgats.data_format.is_empty() && !cgats.is_empty() {
            return err!("DATA exists, but DATA_FORMAT is empty");
        }

        // Return an error if the DATA doesn't fit the DATA_FORMAT
        if cgats.n_rows() * cgats.n_cols() != cgats.len() {
            return err!("rows * cols != len");
        }

        Ok(cgats)
    }
}

#[derive(Debug)]
enum Push {
    MetaData,
    DataFormat,
    Data,
    Stop,
}

impl FromStr for MetaData {
    type Err = BoxErr;
    fn from_str(s: &str) -> Result<Self> {
        if s.trim().is_empty() {
            return Ok(MetaData::Blank);
        }

        if s.trim().starts_with('#') {
            return Ok(MetaData::Comment(s.to_owned()));
        }

        let mut split = s.split_whitespace();
        let key = split.next().ok_or("MetaData key not found")?.into();
        let val = split.collect();
        Ok(MetaData::KeyVal { key, val })
    }
}

impl FromStr for DataFormat {
    type Err = BoxErr;
    fn from_str(s: &str) -> Result<Self> {
        let s = s.trim();
        let mut fields = Vec::new();

        for field in s.split_whitespace() {
            fields.push(field.parse()?);
        }

        Ok(DataFormat { fields })
    }
}

fn alpha(c: char) -> bool {
    !c.is_ascii_digit() && c != '.' && c != '-'
}

#[test]
fn test_has_alpha() {
    assert!(alpha('a'));
    assert!(alpha('e'));
    assert!(alpha('E'));
    assert!(alpha('Z'));
    assert!(!alpha('1'));
    assert!(!alpha('2'));
    assert!(!alpha('0'));
    assert!(!alpha('9'));
}

impl FromStr for DataPoint {
    type Err = BoxErr;
    fn from_str(s: &str) -> Result<Self> {
        let s = s.trim();
        // don't try to parse scientific notation
        if s.contains(alpha) {
            return Ok(Alpha(s.into()));
        }
        Ok(if let Ok(i) = s.parse() {
            Int(i)
        } else if let Ok(f) = s.parse() {
            Float(f)
        } else {
            Alpha(s.into())
        })
    }
}

#[test]
fn data_point_from_str() -> Result<()> {
    match "42".parse()? {
        Int(i) => if i != 42 { panic!() },
        _ => panic!(),
    }

    match "42.0".parse()? {
        Float(f) => if f != 42.0 { panic!() },
        _ => panic!(),
    }

    match "1A1".parse()? {
        Alpha(a) => if a != "1A1" { panic!() },
        _ => panic!(),
    }

    match "1E3".parse()? {
        Alpha(a) => if a != "1E3" { panic!() },
        x => panic!("type is {x:?} but expected Alpha(\"1E3\")"),
    }

    Ok(())
}

#[test]
fn parse_file() {
    use std::{fs::File, io::Read};

    let mut cgats = String::new();
    File::open("test_files/cgats1.tsv").unwrap().read_to_string(&mut cgats).unwrap();

    let cgats: Cgats = cgats.parse().unwrap();
    dbg!(&cgats, cgats.n_cols(), cgats.n_rows(), cgats.len());
    
    dbg!(&cgats.data_format);

    let mut row1 = cgats.get_row(1).unwrap();
    // dbg!(&row1);
    assert_eq!(row1.nth(1), Some(&Alpha("Magenta".into())));

    let mut col1 = cgats.get_col(1);
    // dbg!(&col1);
    assert_eq!(col1.nth(3), Some(&Alpha("Black".into())));
}

#[test]
fn parse_err() {
    let cgats: Result<Cgats> =
    "CGATS.17
    BEGIN_DATA_FORMAT
    END_DATA_FORMAT
    BEGIN_DATA
    END_DATA"
    .parse();
    assert!(cgats.is_ok());

    let cgats: Result<Cgats> =
    "CGATS.17
    BEGIN_DATA_FORMAT
    END_DATA_FORMAT"
    .parse();
    assert!(cgats.is_err());

    let cgats: Result<Cgats> =
    "CGATS.17
    BEGIN_DATA
    END_DATA"
    .parse();
    assert!(cgats.is_err());
}