Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizhang committed Nov 4, 2023
1 parent dada416 commit dfeff6b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 16 deletions.
17 changes: 12 additions & 5 deletions snapatac2-core/src/preprocessing/bam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::utils::{open_file_for_write, Compression};
/// result in faster sorting and greater memory usage.
/// * `compression` - Compression algorithm to use for the output file. Valid values are `gzip` and `zstandard`.
/// * `compression_level` - Compression level to use for the output file. Valid values are 0-9 for `gzip` and 1-22 for `zstandard`.
pub fn make_fragment_file<P1: AsRef<Path>, P2: AsRef<Path>>(
pub fn make_fragment_file<P1: AsRef<Path>, P2: AsRef<Path>, P3: AsRef<Path>>(
bam_file: P1,
output_file: P2,
is_paired: bool,
Expand All @@ -56,10 +56,17 @@ pub fn make_fragment_file<P1: AsRef<Path>, P2: AsRef<Path>>(
chunk_size: usize,
compression: Option<Compression>,
compression_level: Option<u32>,
temp_dir: Option<P3>,
) -> Result<FlagStat> {
let tmp_dir = Builder::new()
.tempdir_in("./")
.expect("failed to create tmperorary directory");
let temp_dir = if let Some(tmp) = temp_dir {
Builder::new()
.tempdir_in(tmp)
.expect("failed to create tmperorary directory")
} else {
Builder::new()
.tempdir()
.expect("failed to create tmperorary directory")
};

if barcode_regex.is_some() && barcode_tag.is_some() {
bail!("Can only set barcode_tag or barcode_regex but not both");
Expand Down Expand Up @@ -99,7 +106,7 @@ pub fn make_fragment_file<P1: AsRef<Path>, P2: AsRef<Path>>(
&barcode,
umi.as_ref(),
is_paired,
tmp_dir.path().to_path_buf(),
temp_dir.path().to_path_buf(),
chunk_size,
)
.into_fragments(&header)
Expand Down
2 changes: 1 addition & 1 deletion snapatac2-python/snapatac2/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.5.2.dev0"
__version__ = "2.5.2.dev1"
7 changes: 6 additions & 1 deletion snapatac2-python/snapatac2/preprocessing/_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def make_fragment_file(
chunk_size: int = 50000000,
compression: Literal["gzip", "zstandard"] | None = None,
compression_level: int | None = None,
tempdir: Path | None = None,
) -> internal.PyFlagStat:
"""
Convert a BAM file to a fragment file.
Expand Down Expand Up @@ -87,6 +88,9 @@ def make_fragment_file(
compression_level
Compression level. 1-9 for gzip, 1-22 for zstandard.
If `None`, it is set to 6 for gzip and 3 for zstandard.
tempdir
Location to store temporary files. If `None`, system temporary directory
will be used.
Returns
-------
Expand All @@ -110,7 +114,8 @@ def make_fragment_file(

return internal.make_fragment_file(
bam_file, output_file, is_paired, shift_left, shift_right, chunk_size,
barcode_tag, barcode_regex, umi_tag, umi_regex, min_mapq, compression, compression_level,
barcode_tag, barcode_regex, umi_tag, umi_regex, min_mapq,
compression, compression_level, tempdir,
)

def import_data(
Expand Down
3 changes: 2 additions & 1 deletion snapatac2-python/src/preprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub(crate) fn make_fragment_file(
mapq: Option<u8>,
compression: Option<&str>,
compression_level: Option<u32>,
temp_dir: Option<PathBuf>,
) -> Result<PyFlagStat>
{
fn parse_tag(tag: &str) -> [u8; 2] {
Expand All @@ -60,7 +61,7 @@ pub(crate) fn make_fragment_file(
barcode_tag.map(|x| parse_tag(x)), barcode_regex,
umi_tag.map(|x| parse_tag(x)), umi_regex,
shift_left, shift_right, mapq, chunk_size,
compression.map(|x| utils::Compression::from_str(x).unwrap()), compression_level,
compression.map(|x| utils::Compression::from_str(x).unwrap()), compression_level, temp_dir,
)?;
Ok(PyFlagStat(stat))
}
Expand Down
5 changes: 1 addition & 4 deletions snapatac2-python/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ def h5ad(dir=Path("./")):
def test_exclude():
fragment_file = snap.datasets.pbmc500(downsample=True)

chr_sizes = snap.genome.hg38.chrom_sizes
chr_sizes.pop('chr1', None)
chr_sizes.pop('chr10', None)
data1 = snap.pp.import_data(
fragment_file,
chrom_sizes=chr_sizes,
chrom_sizes={chr: size for chr, size in snap.genome.hg38.chrom_sizes.items() if chr not in ["chr1", "chr10"]},
sorted_by_barcode=False
)
snap.pp.add_tile_matrix(data1, exclude_chroms=None)
Expand Down
8 changes: 4 additions & 4 deletions snapatac2-python/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from natsort import natsorted
from collections import defaultdict
import pytest
from hypothesis import given, example, settings, HealthCheck, strategies as st
from hypothesis import given, settings, HealthCheck, strategies as st
from hypothesis.extra.numpy import *
from scipy.sparse import csr_matrix
import gzip
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_import(datadir):
sorted_by_barcode=False,
)

data.obs['group'] = '1'
snap.ex.export_fragments(data, groupby="group", suffix='.bed.gz')
data.obs['group'] = 'test_import'
outputs = snap.ex.export_fragments(data, groupby="group", out_dir=str(datadir), suffix='.bed.gz')

assert read_bed("1.bed.gz") == read_bed(fl)
assert read_bed(list(outputs.values())[0]) == read_bed(fl)
3 changes: 3 additions & 0 deletions snapatac2-python/tests/test_tools/test_clean.tsv.gz
Git LFS file not shown

0 comments on commit dfeff6b

Please sign in to comment.