Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gff file extension and compression inference #155

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added python/tests/data/test.gff3.gz
Binary file not shown.
19 changes: 19 additions & 0 deletions python/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def test_read_fastq():

assert len(df) == 2


@pytest.mark.skipif(
not importlib.util.find_spec("polars"), reason="polars not installed"
)
Expand Down Expand Up @@ -238,6 +239,7 @@ def test_read_fasta_fa():

assert len(df) == 2


@pytest.mark.skipif(
not importlib.util.find_spec("polars"), reason="polars not installed"
)
Expand Down Expand Up @@ -568,6 +570,23 @@ def test_gff_reader_gz():
assert len(df) == 2


@pytest.mark.skipif(
not importlib.util.find_spec("polars"), reason="polars not installed"
)
def test_gff_reader_no_options():
session = new_session()

reader = session.read_gff_file((DATA / "test.gff.gz").as_posix())
df = reader.to_polars()

assert len(df) == 2

reader = session.read_gff_file((DATA / "test.gff3.gz").as_posix())
df = reader.to_polars()

assert len(df) == 2


@pytest.mark.skipif(
not importlib.util.find_spec("polars"), reason="polars not installed"
)
Expand Down
10 changes: 7 additions & 3 deletions src/datasources/fasta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,16 @@ impl FASTAReadOptions {
file_options: &FileOptions,
) -> BioBearResult<()> {
if let Some(file_extension) = file_options.file_extension() {
self.file_extension = Some(file_extension.to_string());
if self.file_extension.is_none() {
self.file_extension = Some(file_extension.to_string());
}
}

if let Some(file_compression_type) = file_options.file_compression_type() {
let fct = FileCompressionType::try_from(file_compression_type)?;
self.file_compression_type = Some(fct);
if self.file_compression_type.is_none() {
let fct = FileCompressionType::try_from(file_compression_type)?;
self.file_compression_type = Some(fct);
}
}

Ok(())
Expand Down
51 changes: 35 additions & 16 deletions src/datasources/gff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,65 @@ use exon::datasources::gff::table_provider::ListingGFFTableOptions;
use noodles::core::Region;
use pyo3::{pyclass, pymethods, PyResult};

use crate::FileCompressionType;
use crate::{error::BioBearResult, file_options::FileOptions, FileCompressionType};

use super::parse_region;

#[pyclass]
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct GFFReadOptions {
region: Option<Region>,
file_compression_type: FileCompressionType,
}

impl Default for GFFReadOptions {
fn default() -> Self {
Self {
region: None,
file_compression_type: FileCompressionType::UNCOMPRESSED,
}
}
file_extension: Option<String>,
file_compression_type: Option<FileCompressionType>,
}

#[pymethods]
impl GFFReadOptions {
#[new]
#[pyo3(signature = (/, region = None, file_compression_type = None))]
#[pyo3(signature = (/, region = None, file_compression_type = None, file_extension=None))]
fn try_new(
region: Option<String>,
file_compression_type: Option<FileCompressionType>,
file_extension: Option<String>,
) -> PyResult<Self> {
let region = parse_region(region)?;
Ok(Self {
region,
file_compression_type: file_compression_type
.unwrap_or(FileCompressionType::UNCOMPRESSED),
file_compression_type,
file_extension,
})
}
}

impl GFFReadOptions {
pub(crate) fn update_from_file_options(&mut self, options: &FileOptions) -> BioBearResult<()> {
if let Some(file_extension) = options.file_extension() {
if self.file_extension.is_none() {
self.file_extension = Some(file_extension.to_string());
}
}

if let Some(file_compression_type) = options.file_compression_type() {
if self.file_compression_type.is_none() {
let fct = FileCompressionType::try_from(file_compression_type)?;
self.file_compression_type = Some(fct);
}
}

Ok(())
}
}

impl From<GFFReadOptions> for ListingGFFTableOptions {
fn from(options: GFFReadOptions) -> Self {
let mut o = ListingGFFTableOptions::new(options.file_compression_type.into());
let file_compression_type = options
.file_compression_type
.unwrap_or(FileCompressionType::UNCOMPRESSED);

let file_extension = options.file_extension.unwrap_or("gff".to_string());

let mut o = ListingGFFTableOptions::new(file_compression_type.into())
.with_file_extension(Some(file_extension));

if let Some(region) = options.region {
o = o.with_region(region);
Expand Down
4 changes: 3 additions & 1 deletion src/session_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ impl BioBearSessionContext {
options: Option<crate::datasources::gff::GFFReadOptions>,
py: Python,
) -> PyResult<ExecutionResult> {
let options = options.unwrap_or_default();
let file_options = FileOptions::from(file_path);
let mut options = options.unwrap_or_default();
options.update_from_file_options(&file_options)?;

let result = self.ctx.read_gff(file_path, options.into());
let df = wait_for_future(py, result).map_err(error::BioBearError::from)?;
Expand Down
Loading