pub struct FastaReader<R> { /* private fields */ }Expand description
Provides random access to nucleotide sequences in Fasta files
It parses the Fasta-index (*.fai) to calculate byte offsets
Please note that this reader is different from GtfReader
and RefGeneReader in that it does not parse transcripts
and cannot be used to build Transcripts.
Instead, FastaReader can only read nucleotide sequences from Fasta files.
§Examples
use atglib;
use atglib::fasta::FastaReader;
let mut reader = FastaReader::from_file("tests/data/small.fasta").unwrap();
let seq = reader.read_sequence("chr1", 1, 10).unwrap();
assert_eq!(&seq.to_string(), "GCCTCAGAGG");Implementations§
Source§impl FastaReader<File>
impl FastaReader<File>
Sourcepub fn from_file<P: AsRef<Path> + Display>(path: P) -> Result<Self, FastaError>
pub fn from_file<P: AsRef<Path> + Display>(path: P) -> Result<Self, FastaError>
Creates a FastaReader from a fasta file location
It assumes that the fasta index (fai) file has the same
filename as the fasta file with an appended .fai
§Examples
use atglib;
use atglib::fasta::FastaReader;
let mut reader = FastaReader::from_file("tests/data/small.fasta").unwrap();
let seq = reader.read_sequence("chr1", 1, 10).unwrap();
assert_eq!(&seq.to_string(), "GCCTCAGAGG");Sourcepub fn new<P: AsRef<Path> + Display, P2: AsRef<Path> + Display>(
fasta_path: P,
fai_path: P2,
) -> Result<Self, FastaError>
pub fn new<P: AsRef<Path> + Display, P2: AsRef<Path> + Display>( fasta_path: P, fai_path: P2, ) -> Result<Self, FastaError>
Creates a FastaReader by specifying both fasta and fai file
Use this method if the fasta-index file (fai) does not follow standard
naming conventions. In most cases, you want to use
from_file instead.
§Examples
use atglib;
use atglib::fasta::FastaReader;
let mut reader = FastaReader::new("tests/data/small.fasta", "tests/data/small.fasta.fai").unwrap();
let seq = reader.read_sequence("chr1", 1, 10).unwrap();
assert_eq!(&seq.to_string(), "GCCTCAGAGG");Source§impl<R: Read> FastaReader<R>
impl<R: Read> FastaReader<R>
Sourcepub fn from_reader<R2: Read>(
fasta_reader: R,
fai_reader: R2,
) -> Result<FastaReader<R>, FastaError>
pub fn from_reader<R2: Read>( fasta_reader: R, fai_reader: R2, ) -> Result<FastaReader<R>, FastaError>
Creates a FastaReader from Reader instaces for the Fasta file and the Fasta index
Use this method if both fasta and index are not files on the file system, but e.g. HTTP streams etc.
If you have normal files, you want to use
from_file instead.
§Examples
use std::fs::File;
use atglib;
use atglib::fasta::FastaReader;
let mut fasta = File::open("tests/data/small.fasta").unwrap();
let mut index = File::open("tests/data/small.fasta.fai").unwrap();
let mut reader = FastaReader::from_reader(fasta, index).unwrap();
let seq = reader.read_sequence("chr1", 1, 10).unwrap();
assert_eq!(&seq.to_string(), "GCCTCAGAGG");Source§impl<R: Read + Seek> FastaReader<R>
impl<R: Read + Seek> FastaReader<R>
Sourcepub fn read_range(
&mut self,
chrom: &str,
start: u64,
end: u64,
) -> Result<Vec<u8>, FastaError>
pub fn read_range( &mut self, chrom: &str, start: u64, end: u64, ) -> Result<Vec<u8>, FastaError>
Returns the raw-bytes of the Fasta file for the genomic range
Reads from the FastaReader and returns the raw bytes from the fasta file. The raw bytes include newline and return carriage characters from the Fasta file.
§Info
There are almost no use-cases to use this method. In most cases
you want to use read_sequence instead.
This method is using BufReader::read_exact internally
to read the exact required amount of bytes from the Fasta file.
Sourcepub fn read_sequence(
&mut self,
chrom: &str,
start: u64,
end: u64,
) -> Result<Sequence, FastaError>
pub fn read_sequence( &mut self, chrom: &str, start: u64, end: u64, ) -> Result<Sequence, FastaError>
Returns the Nucleotide Sequence of the specified region
The Sequence includes both start and end positions and is 1-based.
This method is using BufReader::read_exact internally
to read the exact required amount of bytes from the Fasta file.
use atglib;
use atglib::fasta::FastaReader;
let mut reader = FastaReader::from_file("tests/data/small.fasta").unwrap();
// read the nucleotide at position 150 of chromosome 5
let seq = reader.read_sequence("chrM", 150, 150).unwrap();
assert_eq!(&seq.to_string(), "G");
// read the first 10 nucleotides of chromosome 1
let seq = reader.read_sequence("chr1", 1, 10).unwrap();
assert_eq!(&seq.to_string(), "GCCTCAGAGG");