Skip to content

Commit

Permalink
Revert unintended commits to main branch
Browse files Browse the repository at this point in the history
Revert "Implement file like object compatibility for fastq reader"
This reverts commit 9ff35a8.

Revert "Restructure imports and remove unused code"
This reverts commit 2279ab7.
  • Loading branch information
GarrettNg committed Nov 13, 2023
1 parent 9ff35a8 commit fb00333
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 42 deletions.
48 changes: 22 additions & 26 deletions oxbow/src/fastq.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
use arrow::array::{ArrayRef, GenericStringBuilder};
use arrow::{error::ArrowError, record_batch::RecordBatch};
// use noodles::core::Region;
use noodles::fastq;
use std::{
fs::File,
io::{self, BufReader, Read, Seek},
str,
sync::Arc,
};
// use noodles::fastq::fai;
use std::sync::Arc;

use crate::batch_builder::{write_ipc, BatchBuilder};

/// A FASTQ reader.
pub struct FastqReader<R> {
reader: fastq::Reader<BufReader<R>>,
}
type BufferedReader = std::io::BufReader<std::fs::File>;

impl FastqReader<BufReader<File>> {
/// Creates a Fasta reader from a given file path.
pub fn new_from_path(path: &str) -> io::Result<Self> {
let reader = File::open(path)
.map(BufReader::new)
.map(BufReader::new)
.map(fastq::Reader::new)?;
Ok(Self { reader })
}
/// A FASTQ reader.
pub struct FastqReader {
reader: fastq::Reader<BufferedReader>,
// index: fai::Reader<std::io::BufReader<std::fs::File>>,
}

impl<R: Read + Seek> FastqReader<R> {
impl FastqReader {
/// Creates a Fastq Reader.
pub fn new(read: R) -> io::Result<Self> {
let reader = fastq::Reader::new(BufReader::new(read));
pub fn new(path: &str) -> std::io::Result<Self> {
// let index_file = std::fs::File::open(format!("{}.fai", path))?;
// let index_bufreader = std::io::BufReader::with_capacity(1024 * 1024, index_file);
// let index = fai::Reader::new(index_bufreader);

let file = std::fs::File::open(path)?;
let bufreader = std::io::BufReader::with_capacity(1024 * 1024, file);
let reader = fastq::reader::Reader::new(bufreader);

Ok(Self { reader })
}

Expand Down Expand Up @@ -75,13 +71,13 @@ impl BatchBuilder for FastqBatchBuilder {

fn push(&mut self, record: Self::Record<'_>) {
self.name
.append_value(str::from_utf8(record.name()).unwrap());
.append_value(std::str::from_utf8(record.name()).unwrap());
self.description
.append_value(str::from_utf8(record.description()).unwrap());
.append_value(std::str::from_utf8(record.description()).unwrap());
self.sequence
.append_value(str::from_utf8(record.sequence()).unwrap());
.append_value(std::str::from_utf8(record.sequence()).unwrap());
self.quality_scores
.append_value(str::from_utf8(record.quality_scores()).unwrap());
.append_value(std::str::from_utf8(record.quality_scores()).unwrap());
}

fn finish(mut self) -> Result<RecordBatch, ArrowError> {
Expand Down
20 changes: 4 additions & 16 deletions py-oxbow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,10 @@ fn read_fasta(path: &str, region: Option<&str>) -> PyObject {
}

#[pyfunction]
fn read_fastq(py: Python, path_or_file_like: PyObject) -> PyObject {
if let Ok(string_ref) = path_or_file_like.downcast::<PyString>(py) {
// If it's a string, treat it like a path
let mut reader = FastqReader::new_from_path(string_ref.to_str().unwrap()).unwrap();
let ipc = reader.records_to_ipc().unwrap();
Python::with_gil(|py| PyBytes::new(py, &ipc).into())
} else {
// Otherwise, treat it as file-like
let file_like = match PyFileLikeObject::new(path_or_file_like, true, false, true) {
Ok(file_like) => file_like,
Err(_) => panic!("Unknown argument for `path_url_or_file_like`. Not a file path string or url, and not a file-like object."),
};
let mut reader = FastqReader::new(file_like).unwrap();
let ipc = reader.records_to_ipc().unwrap();
Python::with_gil(|py| PyBytes::new(py, &ipc).into())
}
fn read_fastq(path: &str) -> PyObject {
let mut reader = FastqReader::new(path).unwrap();
let ipc = reader.records_to_ipc().unwrap();
Python::with_gil(|py| PyBytes::new(py, &ipc).into())
}

#[pyfunction]
Expand Down

0 comments on commit fb00333

Please sign in to comment.