Skip to content
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
43 changes: 41 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ crate-type = ["cdylib"]
[dependencies]
pyo3 = { version = "0.24.2", features = ["extension-module", "abi3-py39"] }
noodles = { version = "0.31.1", features = ["core", "bam", "sam", "bgzf"] }
regex = { version = "1.9.6" }
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum MapTideError {
ReferenceSequenceIDNotFound,
IOError(io::Error),
ParseError(region::ParseError),
Error(String),
}

impl From<io::Error> for MapTideError {
Expand Down Expand Up @@ -44,6 +45,7 @@ impl Display for MapTideError {
MapTideError::ReferenceSequenceIDNotFound => f.write_str("ReferenceSequenceIDNotFound"),
MapTideError::IOError(ref _e) => f.write_str("IOError"),
MapTideError::ParseError(ref _e) => f.write_str("ParseError"),
MapTideError::Error(ref msg) => f.write_str(msg),
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use noodles::sam::record::sequence::{Base, Sequence};
use noodles::sam::record::{Flags, QualityScores};
use pyo3::exceptions::{PyException, PyIOError, PyIndexError, PyKeyError, PyOverflowError};
use pyo3::prelude::*;
use regex::Regex;
use std::collections::HashMap;
use std::fs::File;

Expand Down Expand Up @@ -373,6 +374,25 @@ fn merge_into_base_map(
Ok(ins_maps)
}

/// Validate that the `region` is one of the formats: `CHROM`, `CHROM:START` `CHROM:START-END`.
fn validate_region(region: &str) -> Result<(), MapTideError> {
// First group: one or more non-colon characters (chromosome name)
// Second group (optional):
// A colon
// Third group (optional): one or more digits (start position)
// Fourth group (optional): a hyphen
// Fifth group (optional): one or more digits (end position)
let re = Regex::new(r"^([^:]+)(:(\d+)?(-)?(\d+)?)?$").unwrap();
if !re.is_match(region) {
Err(MapTideError::Error(format!(
"invalid region: '{}' is not one of the supported formats: CHROM, CHROM:START, CHROM:START-END",
region
)))
} else {
Ok(())
}
}

#[pyfunction(signature = (bam_path, mapping_quality, base_quality))]
fn all(bam_path: String, mapping_quality: usize, base_quality: usize) -> PyResult<MapTide> {
// Create initial maps
Expand Down Expand Up @@ -467,6 +487,7 @@ fn query(
}

// Parse region
validate_region(&region)?;
let region: Region = region
.parse()
.map_err(|x: ParseError| PyException::new_err(x.to_string()))?;
Expand Down Expand Up @@ -567,6 +588,7 @@ fn query(

#[pyfunction(signature = (region))]
fn parse_region(region: String) -> PyResult<(String, Option<usize>, Option<usize>)> {
validate_region(&region)?;
let region: Region = region
.parse()
.map_err(|x: ParseError| PyException::new_err(x.to_string()))?;
Expand Down
Loading