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 vcf read #121

Merged
merged 1 commit into from
Apr 19, 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
4 changes: 4 additions & 0 deletions python/biobear/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
from .biobear import FileCompressionType
from .biobear import FASTQReadOptions
from .biobear import FASTAReadOptions
from .biobear import VCFReadOptions
from .biobear import BCFReadOptions
from .biobear import connect
from .biobear import new_session
from .biobear import __runtime
Expand All @@ -54,6 +56,8 @@
"FileCompressionType",
"FASTQReadOptions",
"FASTAReadOptions",
"BCFReadOptions",
"VCFReadOptions",
"__version__",
"connect",
"new_session",
Expand Down
24 changes: 23 additions & 1 deletion python/biobear/biobear.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,25 @@ class FASTQReadOptions:
file_compression_type: Optional[FileCompressionType] = None,
) -> None: ...

class VCFReadOptions:
def __init__(
self,
/,
region: Optional[str] = None,
file_extension: Optional[str] = None,
file_compression_type: Optional[FileCompressionType] = None,
) -> None: ...

class BCFReadOptions:
def __init__(
self,
/,
region: Optional[str] = None,
) -> None: ...

class ExecutionResult:
def to_arrow(self) -> Table: ...
def to_arrow_record_batch(self) -> RecordBatchStreamReader: ...
def to_arrow_record_batch_reader(self) -> RecordBatchStreamReader: ...

if POLARS_INSTALLED:
def to_polars(self) -> pl.DataFrame: ...
Expand All @@ -46,6 +62,12 @@ class BioBearSessionContext:
def read_fasta_file(
self, file_path: str, options: FASTAReadOptions
) -> ExecutionResult: ...
def read_vcf_file(
self, file_path: str, options: VCFReadOptions
) -> ExecutionResult: ...
def read_bcf_file(
self, file_path: str, options: BCFReadOptions
) -> ExecutionResult: ...
def sql(self, query: str) -> ExecutionResult: ...
def execute(self, query: str) -> None: ...

Expand Down
7 changes: 7 additions & 0 deletions python/biobear/fasta_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"""FASTA file reader."""
import os

import warnings

from biobear.reader import Reader
from biobear.compression import Compression

Expand All @@ -37,6 +39,11 @@ def __init__(
Compression.INFERRED.

"""
warnings.warn(
"The FastaReader class is deprecated and will be removed in a future release. "
"Please use BioBearSessionContext.read_fasta instead.",
DeprecationWarning,
)
self.compression = compression.infer_or_use(path)

if self.compression == Compression.GZIP:
Expand Down
7 changes: 6 additions & 1 deletion python/biobear/fastq_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""FASTQ reader."""
import os
import warnings

from biobear.reader import Reader
from biobear.compression import Compression
Expand All @@ -37,7 +38,11 @@ def __init__(
Compression.INFERRED.

"""

warnings.warn(
"The FastaReader class is deprecated and will be removed in a future release. "
"Please use BioBearSessionContext.read_fasta instead.",
DeprecationWarning,
)
self.compression = compression.infer_or_use(path)

if self.compression == Compression.GZIP:
Expand Down
6 changes: 6 additions & 0 deletions python/biobear/vcf_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""VCF File Readers."""

import os
import warnings

import pyarrow as pa

Expand All @@ -36,6 +37,11 @@ def __init__(self, path: os.PathLike):
path (Path): Path to the VCF file.

"""
warnings.warn(
"The VCFReader class is deprecated and will be removed in a future release. "
"Please use BioBearSessionContext.read_vcf_file instead.",
DeprecationWarning,
)
self._vcf_reader = _ExonReader(str(path), "VCF", None)

@property
Expand Down
36 changes: 36 additions & 0 deletions python/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
FASTAReadOptions,
FileCompressionType,
BCFReadOptions,
VCFReadOptions,
)

DATA = Path(__file__).parent / "data"
Expand Down Expand Up @@ -319,3 +320,38 @@ def test_bcf_indexed_reader_query():
).to_arrow_record_batch_reader()

assert 191 == sum(b.num_rows for b in rbr)


@pytest.mark.skipif(
not importlib.util.find_spec("polars"), reason="polars not installed"
)
def test_vcf_reader():
session = connect()
options = VCFReadOptions()

df = session.read_vcf_file(
(DATA / "vcf_file.vcf").as_posix(), options=options
).to_polars()

assert len(df) == 15


def test_vcf_query():
session = connect()
options = VCFReadOptions(region="1", file_compression_type=FileCompressionType.GZIP)

rbr = session.read_vcf_file(
(DATA / "vcf_file.vcf.gz").as_posix(), options=options
).to_arrow_record_batch_reader()

assert 11 == sum(b.num_rows for b in rbr)

options = VCFReadOptions(
region="chr1", file_compression_type=FileCompressionType.GZIP
)

rbr = session.read_vcf_file(
(DATA / "vcf_file.vcf.gz").as_posix(), options=options
).to_arrow_record_batch_reader()

assert 0 == sum(b.num_rows for b in rbr)
49 changes: 0 additions & 49 deletions python/tests/test_vcf_reader.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/datasources/bcf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct BCFReadOptions {
#[pymethods]
impl BCFReadOptions {
#[new]
#[pyo3(signature = (/, region))]
#[pyo3(signature = (/, region = None))]
fn try_new(region: Option<String>) -> BioBearResult<Self> {
let region = region
.map(|r| Region::from_str(&r))
Expand Down
16 changes: 7 additions & 9 deletions src/datasources/fasta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

use crate::{error::BioBearResult, file_compression_type::FileCompressionType};
use datafusion::datasource::file_format::file_compression_type::FileCompressionType as DFFileCompressionType;
use exon::datasources::fasta::table_provider::ListingFASTATableOptions;
use pyo3::{pyclass, pymethods};

Expand Down Expand Up @@ -47,22 +46,22 @@ const DEFAULT_FASTA_FILE_EXTENSION: &str = "fasta";
/// ```
pub struct FASTAReadOptions {
file_extension: String,
file_compression_type: DFFileCompressionType,
file_compression_type: FileCompressionType,
}

impl Default for FASTAReadOptions {
fn default() -> Self {
Self {
file_extension: String::from(DEFAULT_FASTA_FILE_EXTENSION),
file_compression_type: DFFileCompressionType::UNCOMPRESSED,
file_compression_type: FileCompressionType::UNCOMPRESSED,
}
}
}

#[pymethods]
impl FASTAReadOptions {
#[new]
#[pyo3(signature = (*, file_extension=None, file_compression_type=None))]
#[pyo3(signature = (/, file_extension=None, file_compression_type=None))]
/// Create a new FASTAReadOptions instance.
///
/// # Arguments
Expand All @@ -81,20 +80,19 @@ impl FASTAReadOptions {
file_extension: Option<String>,
file_compression_type: Option<FileCompressionType>,
) -> BioBearResult<Self> {
let df_compression = file_compression_type
.unwrap_or(FileCompressionType::UNCOMPRESSED)
.try_into()?;
let file_compression_type =
file_compression_type.unwrap_or(FileCompressionType::UNCOMPRESSED);

Ok(Self {
file_compression_type: df_compression,
file_compression_type,
file_extension: file_extension.unwrap_or(DEFAULT_FASTA_FILE_EXTENSION.to_string()),
})
}
}

impl From<FASTAReadOptions> for ListingFASTATableOptions {
fn from(options: FASTAReadOptions) -> Self {
ListingFASTATableOptions::new(options.file_compression_type)
ListingFASTATableOptions::new(options.file_compression_type.into())
.with_some_file_extension(Some(&options.file_extension))
}
}
20 changes: 9 additions & 11 deletions src/datasources/fastq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{error::BioBearResult, file_compression_type::FileCompressionType};
use datafusion::datasource::file_format::file_compression_type::FileCompressionType as DFFileCompressionType;
use crate::file_compression_type::FileCompressionType;
use exon::datasources::fastq::table_provider::ListingFASTQTableOptions;
use pyo3::{pyclass, pymethods};

Expand Down Expand Up @@ -47,14 +46,14 @@ const DEFAULT_FASTQ_FILE_EXTENSION: &str = "fastq";
/// ```
pub struct FASTQReadOptions {
file_extension: String,
file_compression_type: DFFileCompressionType,
file_compression_type: FileCompressionType,
}

impl Default for FASTQReadOptions {
fn default() -> Self {
Self {
file_extension: DEFAULT_FASTQ_FILE_EXTENSION.to_string(),
file_compression_type: DFFileCompressionType::UNCOMPRESSED,
file_compression_type: FileCompressionType::UNCOMPRESSED,
}
}
}
Expand All @@ -80,17 +79,16 @@ impl FASTQReadOptions {
pub fn new(
file_extension: Option<String>,
file_compression_type: Option<FileCompressionType>,
) -> BioBearResult<Self> {
let file_compression_type = file_compression_type
.unwrap_or(FileCompressionType::UNCOMPRESSED)
.try_into()?;
) -> Self {
let file_compression_type =
file_compression_type.unwrap_or(FileCompressionType::UNCOMPRESSED);

let file_extension = file_extension.unwrap_or(DEFAULT_FASTQ_FILE_EXTENSION.to_string());

Ok(Self {
Self {
file_extension,
file_compression_type,
})
}
}

fn __repr__(&self) -> String {
Expand All @@ -100,7 +98,7 @@ impl FASTQReadOptions {

impl From<FASTQReadOptions> for ListingFASTQTableOptions {
fn from(options: FASTQReadOptions) -> Self {
ListingFASTQTableOptions::new(options.file_compression_type)
ListingFASTQTableOptions::new(options.file_compression_type.into())
.with_some_file_extension(Some(&options.file_extension))
}
}
1 change: 1 addition & 0 deletions src/datasources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
pub mod bcf;
pub mod fasta;
pub mod fastq;
pub mod vcf;
Loading
Loading