-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmod.rs
391 lines (337 loc) · 12.4 KB
/
mod.rs
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
//! Helpers and type definitions for extended I/O functionality
//!
//! The `io` module contains a number of types and functions to assist with common
//! I/O activities, such a slurping a file by lines, or writing a collection of `Serializable`
//! objects to a path.
//!
//! The two core parts of this module are the [`Io`] and [`DelimFile`] structs. These structs provide
//! methods for reading and writing to files that transparently handle compression based on the
//! file extension of the path given to the methods.
//!
//! ## Example
//!
//! ```rust
//! use std::{
//! default::Default,
//! error::Error
//! };
//! use fgoxide::io::{Io, DelimFile};
//! use serde::{Deserialize, Serialize};
//! use tempfile::TempDir;
//!
//! #[derive(Debug, Deserialize)]
//! struct SampleInfo {
//! sample_name: String,
//! count: usize,
//! gene: String
//! }
//!
//! fn main() -> Result<(), Box<dyn Error>> {
//! let tempdir = TempDir::new()?;
//! let path = tempdir.path().join("test_file.csv.gz");
//!
//! let io = Io::default();
//! let lines = ["sample_name,count,gene", "sample1,100,SEPT14", "sample2,5,MIC"];
//! io.write_lines(&path, lines.iter())?;
//!
//! let delim = DelimFile::default();
//! let samples: Vec<SampleInfo> = delim.read(&path, b',', false)?;
//! assert_eq!(samples.len(), 2);
//! assert_eq!(&samples[1].sample_name, "sample2");
//! Ok(())
//! }
//! ```
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::Path;
use crate::{FgError, Result};
use csv::{QuoteStyle, ReaderBuilder, WriterBuilder};
use flate2::bufread::MultiGzDecoder;
use flate2::write::GzEncoder;
use flate2::Compression;
use serde::{de::DeserializeOwned, Serialize};
use zstd::stream::{Decoder, Encoder};
/// The set of file extensions to treat as GZIPPED
const GZIP_EXTENSIONS: [&str; 2] = ["gz", "bgz"];
/// The set of file extensions to treat as ZSTD compressed
const ZSTD_EXTENSIONS: [&str; 1] = ["zst"];
/// The default buffer size when creating buffered readers/writers
const BUFFER_SIZE: usize = 64 * 1024;
/// Unit-struct that contains associated functions for reading and writing Structs to/from
/// unstructured files.
pub struct Io {
compression: Compression,
buffer_size: usize,
}
/// Returns a Default implementation that will compress to gzip level 5.
impl Default for Io {
fn default() -> Self {
Io::new(5, BUFFER_SIZE)
}
}
impl Io {
/// Creates a new Io instance with the given compression level.
pub fn new(compression: u32, buffer_size: usize) -> Io {
Io { compression: flate2::Compression::new(compression), buffer_size }
}
/// Returns true if the path ends with a recognized GZIP file extension
fn is_gzip_path<P: AsRef<Path>>(p: &P) -> bool {
if let Some(ext) = p.as_ref().extension() {
match ext.to_str() {
Some(x) => GZIP_EXTENSIONS.contains(&x),
None => false,
}
} else {
false
}
}
/// Returns true if the path ends with a recognized ZSTD file extension
fn is_zstd_path<P: AsRef<Path>>(p: &P) -> bool {
if let Some(ext) = p.as_ref().extension() {
match ext.to_str() {
Some(x) => ZSTD_EXTENSIONS.contains(&x),
None => false,
}
} else {
false
}
}
/// Opens a file for reading. Transparently handles decoding gzip and zstd files.
pub fn new_reader<P>(&self, p: &P) -> Result<Box<dyn BufRead + Send>>
where
P: AsRef<Path>,
{
let file = File::open(p).map_err(FgError::IoError)?;
let buf = BufReader::with_capacity(self.buffer_size, file);
if Self::is_gzip_path(p) {
Ok(Box::new(BufReader::with_capacity(self.buffer_size, MultiGzDecoder::new(buf))))
} else if Self::is_zstd_path(p) {
Ok(Box::new(BufReader::with_capacity(self.buffer_size, Decoder::new(buf).unwrap())))
} else {
Ok(Box::new(buf))
}
}
/// Opens a file for writing. Transparently handles encoding data in gzip and zstd formats.
pub fn new_writer<P>(&self, p: &P) -> Result<BufWriter<Box<dyn Write + Send>>>
where
P: AsRef<Path>,
{
let file = File::create(p).map_err(FgError::IoError)?;
let write: Box<dyn Write + Send> = if Io::is_gzip_path(p) {
Box::new(GzEncoder::new(file, self.compression))
} else if Io::is_zstd_path(p) {
Box::new(Encoder::new(file, 0).unwrap().auto_finish())
} else {
Box::new(file)
};
Ok(BufWriter::with_capacity(self.buffer_size, write))
}
/// Reads lines from a file into a Vec
pub fn read_lines<P>(&self, p: &P) -> Result<Vec<String>>
where
P: AsRef<Path>,
{
let r = self.new_reader(p)?;
let mut v = Vec::new();
for result in r.lines() {
v.push(result.map_err(FgError::IoError)?);
}
Ok(v)
}
/// Writes all the lines from an iterable of string-like values to a file, separated by new lines.
pub fn write_lines<P, S>(&self, p: &P, lines: impl IntoIterator<Item = S>) -> Result<()>
where
P: AsRef<Path>,
S: AsRef<str>,
{
let mut out = self.new_writer(p)?;
for line in lines {
out.write_all(line.as_ref().as_bytes()).map_err(FgError::IoError)?;
out.write_all(&[b'\n']).map_err(FgError::IoError)?;
}
out.flush().map_err(FgError::IoError)
}
}
/// Unit-struct that contains associated functions for reading and writing Structs to/from
/// delimited files. Structs should use serde's Serialize/Deserialize derive macros in
/// order to be used with these functions.
pub struct DelimFile {
io: Io,
}
/// Generates a default implementation that uses the default Io instance
impl Default for DelimFile {
fn default() -> Self {
DelimFile { io: Io::default() }
}
}
impl DelimFile {
/// Writes a series of one or more structs to a delimited file. If `quote` is true then fields
/// will be quoted as necessary, otherwise they will never be quoted.
pub fn write<S, P>(
&self,
path: &P,
recs: impl IntoIterator<Item = S>,
delimiter: u8,
quote: bool,
) -> Result<()>
where
S: Serialize,
P: AsRef<Path>,
{
let write = self.io.new_writer(path)?;
let mut writer = WriterBuilder::new()
.delimiter(delimiter)
.has_headers(true)
.quote_style(if quote { QuoteStyle::Necessary } else { QuoteStyle::Never })
.from_writer(write);
for rec in recs {
writer.serialize(rec).map_err(FgError::ConversionError)?;
}
writer.flush().map_err(FgError::IoError)
}
/// Writes structs implementing `[Serialize]` to a file with tab separators between fields.
pub fn write_tsv<S, P>(&self, path: &P, recs: impl IntoIterator<Item = S>) -> Result<()>
where
S: Serialize,
P: AsRef<Path>,
{
self.write(path, recs, b'\t', true)
}
/// Writes structs implementing `[Serialize]` to a file with comma separators between fields.
pub fn write_csv<S, P>(&self, path: &P, recs: impl IntoIterator<Item = S>) -> Result<()>
where
S: Serialize,
P: AsRef<Path>,
{
self.write(path, recs, b',', true)
}
/// Reads structs implementing `[Deserialize]` from a file with the given separators between fields.
/// If `quote` is true then fields surrounded by quotes are parsed, otherwise quotes are not
/// considered.
pub fn read<D, P>(&self, path: &P, delimiter: u8, quote: bool) -> Result<Vec<D>>
where
D: DeserializeOwned,
P: AsRef<Path>,
{
let read = self.io.new_reader(path)?;
let mut reader = ReaderBuilder::new()
.delimiter(delimiter)
.has_headers(true)
.quoting(quote)
.from_reader(read);
let mut results = vec![];
for result in reader.deserialize::<D>() {
let rec = result.map_err(FgError::ConversionError)?;
results.push(rec);
}
Ok(results)
}
/// Reads structs implementing `[Deserialize]` from a file with tab separators between fields.
pub fn read_tsv<D, P>(&self, path: &P) -> Result<Vec<D>>
where
D: DeserializeOwned,
P: AsRef<Path>,
{
self.read(path, b'\t', true)
}
/// Reads structs implementing `[Deserialize]` from a file with tab separators between fields.
pub fn read_csv<D, P>(&self, path: &P) -> Result<Vec<D>>
where
D: DeserializeOwned,
P: AsRef<Path>,
{
self.read(path, b',', true)
}
}
#[cfg(test)]
mod tests {
use crate::io::{DelimFile, Io};
use serde::{Deserialize, Serialize};
use tempfile::TempDir;
/// Record type used in testing DelimFile
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Rec {
s: String,
i: usize,
b: bool,
o: Option<f64>,
}
#[test]
fn test_reading_and_writing_lines_to_file() {
let lines = vec!["foo", "bar,splat,whee", "baz\twhoopsie"];
let tempdir = TempDir::new().unwrap();
let f1 = tempdir.path().join("strs.txt");
let f2 = tempdir.path().join("Strings.txt");
let io = Io::default();
io.write_lines(&f1, &lines).unwrap();
let strings: Vec<String> = lines.iter().map(|l| l.to_string()).collect();
io.write_lines(&f2, &strings).unwrap();
let r1 = io.read_lines(&f1).unwrap();
let r2 = io.read_lines(&f2).unwrap();
assert_eq!(r1, lines);
assert_eq!(r2, lines);
}
#[test]
fn test_reading_and_writing_gzip_files() {
let lines = vec!["foo", "bar", "baz"];
let tempdir = TempDir::new().unwrap();
let text = tempdir.path().join("text.txt");
let gzipped = tempdir.path().join("gzipped.txt.gz");
let io = Io::default();
io.write_lines(&text, &mut lines.iter()).unwrap();
io.write_lines(&gzipped, &mut lines.iter()).unwrap();
let r1 = io.read_lines(&text).unwrap();
let r2 = io.read_lines(&gzipped).unwrap();
assert_eq!(r1, lines);
assert_eq!(r2, lines);
// Also check that we actually wrote gzipped data to the gzip file!
assert_ne!(text.metadata().unwrap().len(), gzipped.metadata().unwrap().len());
}
#[test]
fn test_reading_and_writing_zstd_files() {
let lines = vec!["foo", "bar", "baz"];
let tempdir = TempDir::new().unwrap();
let text = tempdir.path().join("text.txt");
let zstd_compressed = tempdir.path().join("zstd_compressed.txt.zst");
let io = Io::default();
io.write_lines(&text, &mut lines.iter()).unwrap();
io.write_lines(&zstd_compressed, &mut lines.iter()).unwrap();
let r1 = io.read_lines(&text).unwrap();
let r2 = io.read_lines(&zstd_compressed).unwrap();
assert_eq!(r1, lines);
assert_eq!(r2, lines);
// Also check that we actually wrote zstd encoded data to the zstd file!
assert_ne!(text.metadata().unwrap().len(), zstd_compressed.metadata().unwrap().len());
}
#[test]
fn test_reading_and_writing_empty_delim_file() {
let recs: Vec<Rec> = vec![];
let tmp = TempDir::new().unwrap();
let csv = tmp.path().join("recs.csv");
let tsv = tmp.path().join("recs.tsv.gz");
let df = DelimFile::default();
df.write_csv(&csv, &recs).unwrap();
df.write_tsv(&tsv, &recs).unwrap();
let from_csv: Vec<Rec> = df.read_csv(&csv).unwrap();
let from_tsv: Vec<Rec> = df.read_tsv(&tsv).unwrap();
assert_eq!(from_csv, recs);
assert_eq!(from_tsv, recs);
}
#[test]
fn test_reading_and_writing_delim_file() {
let recs: Vec<Rec> = vec![
Rec { s: "Hello".to_string(), i: 123, b: true, o: None },
Rec { s: "A,B,C".to_string(), i: 456, b: false, o: Some(123.45) },
];
let tmp = TempDir::new().unwrap();
let csv = tmp.path().join("recs.csv");
let tsv = tmp.path().join("recs.tsv.gz");
let df = DelimFile::default();
df.write_csv(&csv, &recs).unwrap();
df.write_tsv(&tsv, &recs).unwrap();
let from_csv: Vec<Rec> = df.read_csv(&csv).unwrap();
let from_tsv: Vec<Rec> = df.read_tsv(&tsv).unwrap();
assert_eq!(from_csv, recs);
assert_eq!(from_tsv, recs);
}
}