Skip to content

Added some tests for the rust functions #1369

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

Open
wants to merge 7 commits into
base: 4.0.0
Choose a base branch
from
Open
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
Empty file.
3 changes: 3 additions & 0 deletions pydeeptools/deeptools/test/test_data/log2ratio.bed
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
3R 0 500 2.1154773
3R 500 1000 1.4720684
3R 1000 1500 0
Binary file modified pydeeptools/deeptools/test/test_data/test2.bam
Binary file not shown.
Binary file modified pydeeptools/deeptools/test/test_data/test2.bam.bai
Binary file not shown.
Binary file added pydeeptools/deeptools/test/test_data/test4.bam
Binary file not shown.
Binary file added pydeeptools/deeptools/test/test_data/test4.bam.bai
Binary file not shown.
Binary file added pydeeptools/deeptools/test/test_data/test5.bam
Binary file not shown.
Binary file added pydeeptools/deeptools/test/test_data/test5.bam.bai
Binary file not shown.
Binary file modified pydeeptools/deeptools/test/test_data/testB.bam.bai
Binary file not shown.
Binary file added pydeeptools/deeptools/test/test_data/testC.bam
Binary file not shown.
Binary file added pydeeptools/deeptools/test/test_data/testC.bam.bai
Binary file not shown.
Binary file modified pydeeptools/deeptools/test/test_data/test_paired2.bam
Binary file not shown.
178 changes: 178 additions & 0 deletions pydeeptools/deeptools/test/test_r_alignmentsieve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import pytest
from deeptools.alignmentSieve2 import r_alignmentsieve
import os.path


ROOT = os.path.dirname(os.path.abspath(__file__)) + "/test_data/"
BAMFILE_A = ROOT + "test_paired.bam"
BAMFILE_B = ROOT + "test_paired2.bam"

def test_r_alignmentsieve_basic():
bamifile = BAMFILE_A
ofile = BAMFILE_B
nproc = 4
filter_metrics = ROOT + "filter_metrics_basic.txt"
filtered_out_readsfile = ROOT + "filtered_out_reads.bam"
verbose = False
shift = []
_bed = False
filter_rna_strand = "None"
min_mapping_quality = 30
sam_flag_incl = 0
sam_flag_excl = 0
_blacklist = ""
min_fragment_length = 0
max_fragment_length = 1000
extend_reads = 0
center_reads = False

result = r_alignmentsieve(
bamifile, ofile, nproc, filter_metrics, filtered_out_readsfile, verbose, shift, _bed,
filter_rna_strand, min_mapping_quality, sam_flag_incl, sam_flag_excl, _blacklist,
min_fragment_length, max_fragment_length, extend_reads, center_reads
)

expected_content = [
"#bamFilterReads --filterMetrics",
"#File\tReads\tRemaining Total\tInitial Reads",
"test_paired.bam\t49\t49"
]

with open(filter_metrics, 'r') as f:
lines = f.readlines()

# Remove the file path prefix from the third line
lines[2] = os.path.basename(lines[2])

# Strip newline characters from the lines
lines = [line.strip() for line in lines]

assert lines == expected_content


def test_r_alignmentsieve_with_shift():
bamifile = BAMFILE_A
ofile = BAMFILE_B
nproc = 4
filter_metrics = ROOT + "filter_metrics_shift.txt"
filtered_out_readsfile = ROOT + "filtered_out_reads.bam"
verbose = False
shift = []
_bed = False
filter_rna_strand = "None"
min_mapping_quality = 30
sam_flag_incl = 0
sam_flag_excl = 0
_blacklist = ""
min_fragment_length = 0
max_fragment_length = 1000
extend_reads = 0
center_reads = False

result = r_alignmentsieve(
bamifile, ofile, nproc, filter_metrics, filtered_out_readsfile, verbose, shift, _bed,
filter_rna_strand, min_mapping_quality, sam_flag_incl, sam_flag_excl, _blacklist,
min_fragment_length, max_fragment_length, extend_reads, center_reads
)

expected_content = [
"#bamFilterReads --filterMetrics",
"#File\tReads\tRemaining Total\tInitial Reads",
"test_paired.bam\t49\t49"
]

with open(filter_metrics, 'r') as f:
lines = f.readlines()

# Remove the file path prefix from the third line
lines[2] = os.path.basename(lines[2])

# Strip newline characters from the lines
lines = [line.strip() for line in lines]

assert lines == expected_content


def test_r_alignmentsieve_with_filtering():
bamifile = BAMFILE_A
ofile = BAMFILE_B
nproc = 4
filter_metrics = ROOT + "filter_metrics_withFiltering.txt"
filtered_out_readsfile = ROOT +"filtered_out_reads.bam"
verbose = False
shift = []
_bed = False
filter_rna_strand = "forward"
min_mapping_quality = 30
sam_flag_incl = 0
sam_flag_excl = 0
_blacklist = ""
min_fragment_length = 100
max_fragment_length = 500
extend_reads = 0
center_reads = False

result = r_alignmentsieve(
bamifile, ofile, nproc, filter_metrics, filtered_out_readsfile, verbose, shift, _bed,
filter_rna_strand, min_mapping_quality, sam_flag_incl, sam_flag_excl, _blacklist,
min_fragment_length, max_fragment_length, extend_reads, center_reads
)

expected_content = [
"#bamFilterReads --filterMetrics",
"#File\tReads\tRemaining Total\tInitial Reads",
"test_paired.bam\t27\t49"
]

with open(filter_metrics, 'r') as f:
lines = f.readlines()

# Remove the file path prefix from the third line
lines[2] = os.path.basename(lines[2])

# Strip newline characters from the lines
lines = [line.strip() for line in lines]

assert lines == expected_content

def test_r_alignmentsieve_with_filtering_extendReads():
bamifile = BAMFILE_A
ofile = BAMFILE_B
nproc = 4
filter_metrics = ROOT + "filter_metrics_extendReads.txt"
filtered_out_readsfile = ROOT +"filtered_out_reads.bam"
verbose = False
shift = []
_bed = False
filter_rna_strand = "forward"
min_mapping_quality = 30
sam_flag_incl = 0
sam_flag_excl = 0
_blacklist = ""
min_fragment_length = 100
max_fragment_length = 500
extend_reads = 100
center_reads = False

result = r_alignmentsieve(
bamifile, ofile, nproc, filter_metrics, filtered_out_readsfile, verbose, shift, _bed,
filter_rna_strand, min_mapping_quality, sam_flag_incl, sam_flag_excl, _blacklist,
min_fragment_length, max_fragment_length, extend_reads, center_reads
)

expected_content = [
"#bamFilterReads --filterMetrics",
"#File\tReads\tRemaining Total\tInitial Reads",
"test_paired.bam\t27\t49"
]

with open(filter_metrics, 'r') as f:
lines = f.readlines()

# Remove the file path prefix from the third line
lines[2] = os.path.basename(lines[2])

# Strip newline characters from the lines
lines = [line.strip() for line in lines]

assert lines == expected_content
88 changes: 88 additions & 0 deletions pydeeptools/deeptools/test/test_r_bamCompare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import pytest
from deeptools.bamCompare2 import r_bamcompare
import os.path
import filecmp
from os import unlink

ROOT = os.path.dirname(os.path.abspath(__file__)) + "/test_data/"
BAMFILE_A = ROOT + "test1.bam"
BAMFILE_B = ROOT + "test2.bam"


def test_r_bamcompare():
bamifile1 = BAMFILE_A
bamifile2 = BAMFILE_B
ofile = ROOT + "r_bamcompare_output.bedgraph"
ofiletype = "bedgraph"
norm = "None"
effective_genome_size = 0
scalefactorsmethod = "None"
operation = "ratio"
pseudocount = 0
# filtering options
ignoreduplicates = False
minmappingquality = 0
samflaginclude = 0
samflagexclude = 0
minfraglen = 0
maxfraglen = 0
nproc = 1
_ignorechr = []
binsize = 500
regions = []
verbose = False

# Call the Rust function
r_bamcompare(
bamifile1, bamifile2, ofile, ofiletype, norm, effective_genome_size, scalefactorsmethod, operation,
pseudocount, ignoreduplicates, minmappingquality, samflaginclude, samflagexclude, minfraglen, maxfraglen, nproc,
_ignorechr, binsize, regions, verbose
)
# Add assertions to verify the expected behavior
expected = ['3R\t0\t500\t4.571429\n',
'3R\t500\t1000\t2.8333333\n',
'3R\t1000\t1500\t1\n',]
_foo = open(ofile, 'r')
resp = _foo.readlines()
_foo.close()
assert f"{resp}" == f"{expected}", f"{resp} != {expected}"
unlink(ofile)

def test_r_bamcompare_RPKM():
bamifile1 = BAMFILE_A
bamifile2 = BAMFILE_B
ofile = ROOT + "r_bamcompare_output.bedgraph"
ofiletype = "bedgraph"
norm = "RPKM"
effective_genome_size = 0
scalefactorsmethod = "None"
operation = "ratio"
pseudocount = 0
# filtering options
ignoreduplicates = False
minmappingquality = 0
samflaginclude = 0
samflagexclude = 0
minfraglen = 0
maxfraglen = 0
nproc = 1
_ignorechr = []
binsize = 500
regions = []
verbose = False

# Call the Rust function
r_bamcompare(
bamifile1, bamifile2, ofile, ofiletype, norm, effective_genome_size, scalefactorsmethod, operation,
pseudocount, ignoreduplicates, minmappingquality, samflaginclude, samflagexclude, minfraglen, maxfraglen, nproc,
_ignorechr, binsize, regions, verbose
)
# Add assertions to verify the expected behavior
expected = ['3R\t0\t500\t4.571429\n',
'3R\t500\t1000\t2.8333333\n',
'3R\t1000\t1500\t1\n',]
_foo = open(ofile, 'r')
resp = _foo.readlines()
_foo.close()
assert f"{resp}" == f"{expected}", f"{resp} != {expected}"
unlink(ofile)
Loading
Loading