Skip to content

Commit

Permalink
style: Format tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nvictus committed Apr 6, 2024
1 parent c2d9930 commit 744dc33
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 109 deletions.
49 changes: 20 additions & 29 deletions tests/test_core_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ def test_is_cataloged():
columns=["chrom", "start", "end", "funny_view_region"],
)
assert is_cataloged(
df,
view_df,
df_view_col="funny_view_region",
view_name_col="funny_name"
df, view_df, df_view_col="funny_view_region", view_name_col="funny_name"
)


Expand Down Expand Up @@ -131,8 +128,8 @@ def test_is_contained():
cols=["chrom1", "start1", "end1"],
cols_view=["CHROM", "START", "END"],
df_view_col="VIEW_REGION",
view_name_col="NAME"
)
view_name_col="NAME",
)

with pytest.raises(TypeError):
# cols and view_cols are not passed as an arguments
Expand Down Expand Up @@ -219,19 +216,16 @@ def test_is_covering():
columns=["chrom1", "start1", "end1"],
)
chromsizes = pd.DataFrame(
[
["chr1", 0, 9, "chr1p"],
["chr1", 11, 20, "chr1q"]
],
[["chr1", 0, 9, "chr1p"], ["chr1", 11, 20, "chr1q"]],
columns=["CHROM", "START", "END", "NAME"],
)
)
assert is_covering(
df1,
chromsizes,
cols=["chrom1", "start1", "end1"],
cols_view=["CHROM", "START", "END"],
view_name_col="NAME"
)
df1,
chromsizes,
cols=["chrom1", "start1", "end1"],
cols_view=["CHROM", "START", "END"],
view_name_col="NAME",
)

with pytest.raises(ValueError):
# cols and view_cols are not passed as an arguments
Expand Down Expand Up @@ -274,19 +268,16 @@ def test_is_tiling():
columns=["chrom1", "start1", "end1", "view_region"],
)
chromsizes = pd.DataFrame(
[
["chr1", 0, 9, "chr1p"],
["chr1", 11, 20, "chr1q"]
],
[["chr1", 0, 9, "chr1p"], ["chr1", 11, 20, "chr1q"]],
columns=["CHROM", "START", "END", "NAME"],
)
)
assert is_tiling(
df1,
chromsizes,
cols=["chrom1", "start1", "end1"],
cols_view=["CHROM", "START", "END"],
view_name_col="NAME"
)
df1,
chromsizes,
cols=["chrom1", "start1", "end1"],
cols_view=["CHROM", "START", "END"],
view_name_col="NAME",
)

with pytest.raises(KeyError):
# cols and view_cols are not passed as an arguments
Expand Down Expand Up @@ -477,7 +468,7 @@ def test_is_sorted():
view_name_col="FRUIT",
df_view_col="FRUIT",
cols=["CHROM", "START", "END"],
cols_view=["CHROM", "START", "END"]
cols_view=["CHROM", "START", "END"],
)

with pytest.raises(ValueError):
Expand Down
1 change: 0 additions & 1 deletion tests/test_core_specs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import numpy as np
import pandas as pd
import pytest
Expand Down
17 changes: 7 additions & 10 deletions tests/test_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def test_make_chromarms():
cols_mids=["chromosome", "loc"],
)
pd.testing.assert_frame_equal(
result,
arms.rename(columns={"chrom": "chromosome", "start": "lo", "end": "hi"})
result, arms.rename(columns={"chrom": "chromosome", "start": "lo", "end": "hi"})
)

# test passing 2 columns
Expand All @@ -52,9 +51,7 @@ def test_make_chromarms():

# test for passing Series or dict
result = bioframe.make_chromarms(
pd.Series({"chrX": 8}),
mids,
cols_mids=["chromosome", "loc"]
pd.Series({"chrX": 8}), mids, cols_mids=["chromosome", "loc"]
)
pd.testing.assert_frame_equal(arms, result)

Expand Down Expand Up @@ -196,11 +193,11 @@ def test_frac_gc():

def test_seq_gc():

assert (0 == bioframe.seq_gc("AT"))
assert (np.isnan( bioframe.seq_gc("NNN")))
assert (1 == bioframe.seq_gc("NGnC"))
assert (0.5 == bioframe.seq_gc("GTCA"))
assert (0.25 == bioframe.seq_gc("nnnNgTCa", mapped_only=False))
assert 0 == bioframe.seq_gc("AT")
assert np.isnan(bioframe.seq_gc("NNN"))
assert 1 == bioframe.seq_gc("NGnC")
assert 0.5 == bioframe.seq_gc("GTCA")
assert 0.25 == bioframe.seq_gc("nnnNgTCa", mapped_only=False)
with pytest.raises(ValueError):
bioframe.seq_gc(["A", "T"])
with pytest.raises(ValueError):
Expand Down
89 changes: 29 additions & 60 deletions tests/test_ops_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,24 @@

def test_select():
df = pd.DataFrame(
[["chrX", 3, 8],
["chr1", 4, 5],
["chrX", 1, 5]],
[["chrX", 3, 8], ["chr1", 4, 5], ["chrX", 1, 5]],
columns=["chrom", "start", "end"],
)

result = pd.DataFrame(
[["chr1", 4, 5]],
columns=["chrom", "start", "end"]
)
result = pd.DataFrame([["chr1", 4, 5]], columns=["chrom", "start", "end"])
pd.testing.assert_frame_equal(
result, bioframe.select(df, "chr1:4-10").reset_index(drop=True)
)

result = pd.DataFrame(
[["chrX", 3, 8],
["chrX", 1, 5]],
columns=["chrom", "start", "end"]
[["chrX", 3, 8], ["chrX", 1, 5]], columns=["chrom", "start", "end"]
)
pd.testing.assert_frame_equal(
result, bioframe.select(df, "chrX").reset_index(drop=True)
)

result = pd.DataFrame(
[["chrX", 3, 8],
["chrX", 1, 5]],
columns=["chrom", "start", "end"]
[["chrX", 3, 8], ["chrX", 1, 5]], columns=["chrom", "start", "end"]
)
pd.testing.assert_frame_equal(
result, bioframe.select(df, "chrX:4-6").reset_index(drop=True)
Expand All @@ -52,14 +43,11 @@ def test_select__with_colnames():
### select with non-standard column names
new_names = ["chr", "chrstart", "chrend"]
df = pd.DataFrame(
[["chrX", 3, 8],
["chr1", 4, 5],
["chrX", 1, 5]],
[["chrX", 3, 8], ["chr1", 4, 5], ["chrX", 1, 5]],
columns=new_names,
)
result = pd.DataFrame(
[["chrX", 3, 8],
["chrX", 1, 5]],
[["chrX", 3, 8], ["chrX", 1, 5]],
columns=new_names,
)
pd.testing.assert_frame_equal(
Expand Down Expand Up @@ -94,47 +82,32 @@ def test_select__with_nulls():

def test_select__mask_indices_labels():
df = pd.DataFrame(
[["chrX", 3, 8],
["chr1", 4, 5],
["chrX", 1, 5]],
[["chrX", 3, 8], ["chr1", 4, 5], ["chrX", 1, 5]],
columns=["chrom", "start", "end"],
)

region = "chr1:4-10"
answer = pd.DataFrame(
[["chr1", 4, 5]],
columns=["chrom", "start", "end"]
)
answer = pd.DataFrame([["chr1", 4, 5]], columns=["chrom", "start", "end"])

result = bioframe.select(df, region)
pd.testing.assert_frame_equal(
answer, result.reset_index(drop=True)
)
pd.testing.assert_frame_equal(answer, result.reset_index(drop=True))
mask = bioframe.select_mask(df, region)
pd.testing.assert_frame_equal(
answer, df.loc[mask].reset_index(drop=True)
)
pd.testing.assert_frame_equal(answer, df.loc[mask].reset_index(drop=True))
labels = bioframe.select_labels(df, region)
pd.testing.assert_frame_equal(
answer, df.loc[labels].reset_index(drop=True)
)
pd.testing.assert_frame_equal(answer, df.loc[labels].reset_index(drop=True))
idx = bioframe.select_indices(df, region)
pd.testing.assert_frame_equal(
answer, df.iloc[idx].reset_index(drop=True)
)
pd.testing.assert_frame_equal(answer, df.iloc[idx].reset_index(drop=True))


def test_select__query_intervals_are_half_open():
df = pd.DataFrame({
"chrom": ["chr1", "chr1",
"chr2", "chr2", "chr2", "chr2", "chr2", "chr2"],
"start": [0, 10,
10, 20, 30, 40, 50, 60],
"end": [10, 20,
20, 30, 40, 50, 60, 70],
"name": ["a", "b",
"A", "B", "C", "D", "E", "F"],
})
df = pd.DataFrame(
{
"chrom": ["chr1", "chr1", "chr2", "chr2", "chr2", "chr2", "chr2", "chr2"],
"start": [0, 10, 10, 20, 30, 40, 50, 60],
"end": [10, 20, 20, 30, 40, 50, 60, 70],
"name": ["a", "b", "A", "B", "C", "D", "E", "F"],
}
)

result = bioframe.select(df, "chr1")
assert (result["name"] == ["a", "b"]).all()
Expand Down Expand Up @@ -172,16 +145,14 @@ def test_select__query_intervals_are_half_open():

def test_select__with_point_intervals():
# Dataframe containing "point intervals"
df = pd.DataFrame({
"chrom": ["chr1", "chr1",
"chr2", "chr2", "chr2", "chr2", "chr2", "chr2"],
"start": [0, 10,
10, 20, 30, 40, 50, 60],
"end": [10, 10,
20, 30, 40, 50, 50, 70],
"name": ["a", "b",
"A", "B", "C", "D", "E", "F"],
})
df = pd.DataFrame(
{
"chrom": ["chr1", "chr1", "chr2", "chr2", "chr2", "chr2", "chr2", "chr2"],
"start": [0, 10, 10, 20, 30, 40, 50, 60],
"end": [10, 10, 20, 30, 40, 50, 50, 70],
"name": ["a", "b", "A", "B", "C", "D", "E", "F"],
}
)
result = bioframe.select(df, "chr1")
assert (result["name"] == ["a", "b"]).all()

Expand Down Expand Up @@ -213,9 +184,7 @@ def test_select__with_point_intervals():
def test_select__with_points():
# Dataframe of points
df = pd.DataFrame(
[["chrX", 3, "A"],
["chr1", 4, "C"],
["chrX", 1, "B"]],
[["chrX", 3, "A"], ["chr1", 4, "C"], ["chrX", 1, "B"]],
columns=["chrom", "pos", "name"],
)

Expand Down
4 changes: 1 addition & 3 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ def test_fetch_chromsizes():
assert chromsizes.name == "length"
assert len(chromsizes) == 25

chromsizes_df = bioframe.fetch_chromsizes(
db, provider=provider, as_bed=True
)
chromsizes_df = bioframe.fetch_chromsizes(db, provider=provider, as_bed=True)
assert isinstance(chromsizes_df, pd.DataFrame)
assert list(chromsizes_df.columns) == ["chrom", "start", "end"]
assert len(chromsizes_df) == 25
Expand Down
14 changes: 8 additions & 6 deletions tests/test_vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ def test_to_ucsc_colorstring():
with pytest.raises(ValueError):
bioframe.to_ucsc_colorstring("notacolor")

df = bioframe.from_any([
["chr1", 0, 10, "red"],
["chr1", 10, 20, "blue"],
["chr2", 0, 10, "green"],
["chr2", 10, 20, None],
])
df = bioframe.from_any(
[
["chr1", 0, 10, "red"],
["chr1", 10, 20, "blue"],
["chr2", 0, 10, "green"],
["chr2", 10, 20, None],
]
)
df["itemRgb"] = df["name"].apply(bioframe.to_ucsc_colorstring)
assert df["itemRgb"].tolist() == ["255,0,0", "0,0,255", "0,128,0", "0"]

0 comments on commit 744dc33

Please sign in to comment.