FastaReader

Struct FastaReader 

Source
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>

Source

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");
Source

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>

Source

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>

Source

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.

Source

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");

Auto Trait Implementations§

§

impl<R> Freeze for FastaReader<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for FastaReader<R>
where R: RefUnwindSafe,

§

impl<R> Send for FastaReader<R>
where R: Send,

§

impl<R> Sync for FastaReader<R>
where R: Sync,

§

impl<R> Unpin for FastaReader<R>
where R: Unpin,

§

impl<R> UnwindSafe for FastaReader<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.