annonars 0.33.0

Rust template repository
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
//! Schema inference and representation for TSV files.

use crate::error;

/// Possible column types.
///
/// We allow for a subset allowed in JSON.
///
/// The enum type is sorted in the sense that smaller value are more generic/less specific
/// than the larger ones.
#[derive(
    Debug,
    Default,
    Clone,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    serde::Serialize,
    serde::Deserialize,
)]
pub enum ColumnType {
    /// String type.
    String,
    /// Float type; will be store as `f64`.
    Float,
    /// Integer type; will be stored as `i32`.
    Integer,
    /// Unknown, seen only null values.
    #[default]
    Unknown,
}

impl ColumnType {
    /// Make the column type as general as necessary to hold the given value.
    ///
    /// # Arguments
    ///
    /// * `val` - Value to extend the column type with.
    /// * `null_values` - List of null values.
    pub(crate) fn extend(&self, val: &str, null_values: &[&str]) -> Self {
        if null_values.contains(&val) {
            *self
        } else {
            let compat = if val.parse::<i64>().is_ok() {
                ColumnType::Integer
            } else if val.parse::<f64>().is_ok() {
                ColumnType::Float
            } else {
                ColumnType::String
            };

            *self.min(&compat)
        }
    }

    /// Merge the types of two columns and return the most general one.
    ///
    /// # Arguments
    ///
    /// * `other` - Other column type to merge with.
    pub(crate) fn merge(&self, other: &ColumnType) -> Self {
        *self.min(other)
    }
}

/// Schema description for one column.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ColumnSchema {
    /// Column name.
    pub name: String,
    /// Column type.
    pub typ: ColumnType,
}

impl ColumnSchema {
    /// Create from given name and type.
    pub fn from<N>(name: N, typ: ColumnType) -> Self
    where
        N: AsRef<str>,
    {
        Self {
            name: name.as_ref().to_string(),
            typ,
        }
    }
}

impl FileSchema {
    /// Ensure that all null values and column names are the same and return an error if not.
    /// Otherwise, perform a column-wise extension of the column type.
    pub fn merge(&self, other: &FileSchema) -> Result<FileSchema, error::Error> {
        // Check that the column names are the same and in the same order.
        if self.columns.len() != other.columns.len() {
            return Err(error::Error::ColumnCount(
                self.columns.len(),
                other.columns.len(),
            ));
        }
        for (col1, col2) in self.columns.iter().zip(other.columns.iter()) {
            if col1.name != col2.name {
                return Err(error::Error::ColumnName(
                    col1.name.clone(),
                    col2.name.clone(),
                ));
            }
        }
        // Check that the null values are the same.
        if self.null_values != other.null_values {
            return Err(error::Error::NullValues(
                self.null_values.join(","),
                other.null_values.join(","),
            ));
        }

        // Now merge the column types.
        let columns = self
            .columns
            .iter()
            .zip(other.columns.iter())
            .map(|(col1, col2)| ColumnSchema {
                name: col1.name.clone(),
                typ: col1.typ.merge(&col2.typ),
            })
            .collect();

        Ok(FileSchema {
            columns,
            null_values: self.null_values.clone(),
        })
    }
}

/// Schema description for a table.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct FileSchema {
    /// The columns.
    pub columns: Vec<ColumnSchema>,
    /// The null values.
    pub null_values: Vec<String>,
}

impl FileSchema {
    /// Create a new schema from the given columns.
    pub fn from(columns: Vec<ColumnSchema>, null_values: Vec<String>) -> Self {
        Self {
            columns,
            null_values,
        }
    }
}

/// Schema inference.
pub mod infer {
    use std::{io::BufRead, path::Path};

    use crate::error;

    use super::{ColumnSchema, ColumnType, FileSchema};

    /// Configuration for schema inference.
    ///
    /// The `Default` trait provides appropriate defaults that could be used using
    /// VCF-style headers.
    #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub struct Config {
        /// Field delimiter to use.
        pub field_delimiter: char,
        /// Allow different number of columns in different rows.
        pub flexible: bool,
        /// Values to use for null.
        pub null_values: Vec<String>,
        /// Header prefix to strip (OK if missing).
        pub header_prefix: String,
        /// Number of rows to use for inferring schema.
        pub num_rows: usize,
        /// Number of rows to skip.
        pub skip_rows: usize,

        /// Column name for chromosome.
        pub col_chrom: String,
        /// Column name for (start) position.
        pub col_start: String,
        /// Column name for reference allele.
        pub col_ref: String,
        /// Column name for alternative allele.
        pub col_alt: String,
    }

    impl Default for Config {
        fn default() -> Self {
            Self {
                field_delimiter: '\t',
                flexible: false,
                null_values: vec![String::from("."), String::from("NA"), String::new()],
                header_prefix: String::from("#"),
                num_rows: 10_000,
                skip_rows: 0,
                col_chrom: String::from("CHROM"),
                col_start: String::from("POS"),
                col_ref: String::from("REF"),
                col_alt: String::from("ALT"),
            }
        }
    }

    /// Context for running the inference algorithm.
    #[derive(Debug, Clone, Default)]
    pub struct Context {
        /// Configuration for schema inference.
        config: Config,
    }

    impl Context {
        /// Create a new context with given configuration.
        ///
        /// # Arguments
        ///
        /// * `config` - Configuration for schema inference.
        pub fn new(config: &Config) -> Self {
            Self {
                config: config.clone(),
            }
        }

        /// Get default configuration for the given row.
        fn default_column_config(&self, name: &str) -> ColumnType {
            if name == self.config.col_chrom
                || name == self.config.col_ref
                || name == self.config.col_alt
            {
                ColumnType::String
            } else if name == self.config.col_start {
                ColumnType::Integer
            } else {
                ColumnType::Unknown
            }
        }

        /// Run the schema inference from path.
        pub fn infer_from_path<P: AsRef<Path>>(&self, path: P) -> Result<FileSchema, error::Error> {
            let p = format!("{}", path.as_ref().display());
            let reader: Box<dyn BufRead> = if p.ends_with(".gz") || p.ends_with(".bgz") {
                if let Ok(reader) =
                    bgzip::BGZFReader::new(std::fs::File::open(&path).map_err(error::Error::Io)?)
                {
                    Box::new(reader)
                } else {
                    Box::new(std::io::BufReader::new(flate2::read::GzDecoder::new(
                        std::fs::File::open(&path).map_err(error::Error::Io)?,
                    )))
                }
            } else {
                Box::new(std::io::BufReader::new(
                    std::fs::File::open(&path).map_err(error::Error::Io)?,
                ))
            };

            self.infer_from_reader(reader)
        }

        /// Run the schema inference algorithm.
        pub fn infer_from_reader<R: BufRead>(&self, reader: R) -> Result<FileSchema, error::Error> {
            // Skip the first few rows, as configured.
            let mut reader = reader;
            for _i in 0..self.config.skip_rows {
                let mut buf = String::new();
                reader.read_line(&mut buf).map_err(error::Error::Io)?;
            }

            // Get the null values as `&str` into a shortcut variable.
            let null_values = self
                .config
                .null_values
                .iter()
                .map(std::string::String::as_str)
                .collect::<Vec<_>>();
            let mut columns: Option<Vec<ColumnSchema>> = None;
            let mut seen_rows = 0;

            // Read the first few rows as configured and infer the schema.
            for result in reader.lines() {
                let line = result.map_err(error::Error::Io)?;
                let record = line.split(self.config.field_delimiter).collect::<Vec<_>>();
                // Track number of seen rows.
                seen_rows += 1;

                if let Some(columns) = columns.as_mut() {
                    // We have seen the header and can extend the schema.
                    record
                        .into_iter()
                        .zip(columns.iter_mut())
                        .for_each(|(record, column)| {
                            column.typ = column.typ.extend(record, &null_values);
                        })
                } else {
                    // Assign header for first row, optionally strip header prefix.
                    columns = Some(
                        record
                            .into_iter()
                            .enumerate()
                            .map(|(i, val)| {
                                let val = if i == 0 {
                                    val.strip_prefix(&self.config.header_prefix).unwrap_or(val)
                                } else {
                                    val
                                };

                                ColumnSchema {
                                    name: val.to_string(),
                                    typ: self.default_column_config(val),
                                }
                            })
                            .collect::<Vec<_>>(),
                    );
                }

                if seen_rows > self.config.num_rows {
                    break;
                }
            }

            if let Some(columns) = columns {
                if seen_rows == 1 {
                    tracing::warn!("only seen header row, assuming strings");
                    Ok(FileSchema {
                        columns: columns
                            .iter()
                            .map(|c| ColumnSchema {
                                name: c.name.clone(),
                                typ: self.default_column_config(&c.name),
                            })
                            .collect(),
                        null_values: self.config.null_values.clone(),
                    })
                } else {
                    Ok(FileSchema {
                        columns,
                        null_values: self.config.null_values.clone(),
                    })
                }
            } else {
                Err(error::Error::HeaderMissing)
            }
        }
    }
}

#[cfg(test)]
mod test {
    use std::io::BufReader;

    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn column_type_extend() {
        assert_eq!(ColumnType::Integer.extend("x", &["."]), ColumnType::String,);
        assert_eq!(ColumnType::Integer.extend("1.0", &["."]), ColumnType::Float,);
        assert_eq!(ColumnType::Integer.extend("1", &["."]), ColumnType::Integer,);
        assert_eq!(ColumnType::Integer.extend(".", &["."]), ColumnType::Integer,);

        assert_eq!(ColumnType::Float.extend("x", &["."]), ColumnType::String,);
        assert_eq!(ColumnType::Float.extend("1.0", &["."]), ColumnType::Float,);
        assert_eq!(ColumnType::Float.extend("1", &["."]), ColumnType::Float,);
        assert_eq!(ColumnType::Float.extend(".", &["."]), ColumnType::Float,);

        assert_eq!(ColumnType::String.extend("x", &["."]), ColumnType::String,);
        assert_eq!(ColumnType::String.extend("1.0", &["."]), ColumnType::String,);
        assert_eq!(ColumnType::String.extend("1", &["."]), ColumnType::String,);
        assert_eq!(ColumnType::String.extend(".", &["."]), ColumnType::String,);
    }

    #[test]
    fn fileschema_merge() -> Result<(), anyhow::Error> {
        let schema1 = FileSchema {
            columns: vec![
                ColumnSchema {
                    name: String::from("a"),
                    typ: ColumnType::Integer,
                },
                ColumnSchema {
                    name: String::from("b"),
                    typ: ColumnType::String,
                },
            ],
            null_values: vec![String::from(".")],
        };
        let schema2 = FileSchema {
            columns: vec![
                ColumnSchema {
                    name: String::from("a"),
                    typ: ColumnType::String,
                },
                ColumnSchema {
                    name: String::from("b"),
                    typ: ColumnType::Integer,
                },
            ],
            null_values: vec![String::from(".")],
        };

        let merged = schema1.merge(&schema2)?;

        assert_eq!(
            merged,
            FileSchema {
                columns: vec![
                    ColumnSchema {
                        name: String::from("a"),
                        typ: ColumnType::String,
                    },
                    ColumnSchema {
                        name: String::from("b"),
                        typ: ColumnType::String,
                    },
                ],
                null_values: vec![String::from(".")],
            }
        );

        Ok(())
    }

    #[test]
    fn infer_schema_empty() -> Result<(), anyhow::Error> {
        let config = infer::Config {
            field_delimiter: '\t',
            flexible: true,
            null_values: vec![String::from(".")],
            header_prefix: String::from("#"),
            num_rows: 100,
            skip_rows: 0,
            ..Default::default()
        };
        let mut reader = BufReader::new(std::fs::File::open("tests/tsv/schema/empty.tsv")?);
        let res = infer::Context::new(&config).infer_from_reader(&mut reader);

        assert!(res.is_err());

        Ok(())
    }

    #[test]
    fn infer_schema_header() -> Result<(), anyhow::Error> {
        let config = infer::Config {
            field_delimiter: '\t',
            flexible: true,
            null_values: vec![String::from(".")],
            header_prefix: String::from("#"),
            num_rows: 100,
            skip_rows: 0,
            ..Default::default()
        };
        let mut reader = BufReader::new(std::fs::File::open("tests/tsv/schema/header.tsv")?);
        let record = infer::Context::new(&config).infer_from_reader(&mut reader)?;

        insta::assert_debug_snapshot!(record);

        Ok(())
    }

    #[test]
    fn infer_schema_values() -> Result<(), anyhow::Error> {
        let config = infer::Config {
            field_delimiter: '\t',
            flexible: true,
            null_values: vec![String::from(".")],
            header_prefix: String::from("#"),
            num_rows: 100,
            skip_rows: 0,
            ..Default::default()
        };
        let mut reader = BufReader::new(std::fs::File::open("tests/tsv/schema/values.tsv")?);
        let record = infer::Context::new(&config).infer_from_reader(&mut reader)?;

        insta::assert_debug_snapshot!(record);

        Ok(())
    }
}