ngs/utils/formats/
fasta.rs

1//! Utilities related to opening and manipulating FASTA files.
2
3use std::fs::File;
4use std::io::BufReader;
5use std::path::Path;
6
7use anyhow::bail;
8use noodles::fasta;
9
10use super::BioinformaticsFileFormat;
11
12mod phred;
13
14/// Attempts to open a FASTA file from a given source.
15pub fn open<P>(src: P) -> anyhow::Result<fasta::Reader<BufReader<File>>>
16where
17    P: AsRef<Path>,
18{
19    let path = src.as_ref();
20    let file = File::open(path);
21
22    match BioinformaticsFileFormat::try_detect(path) {
23        Some(BioinformaticsFileFormat::FASTA_GZ) => bail!(
24            "This command does not yet support gzipped FASTA \
25            files. Please unzip your FASTA file and try again."
26        ),
27        Some(BioinformaticsFileFormat::FASTA) => {
28            Ok(file.map(BufReader::new).map(fasta::Reader::new)?)
29        }
30        Some(format) => bail!("incompatible formats: required FASTA, found {}", format),
31        None => {
32            let ext = path
33                .extension()
34                .expect("file extension to exist")
35                .to_str()
36                .expect("extension to be convertible to &str");
37            bail!("Not able to determine filetype for extension: {}", ext)
38        }
39    }
40}