1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
from io import StringIO
import pandas as pd
import numpy as np
import pytest
from bioframe.core.construction import from_any
from bioframe.core import construction
def test_add_ucsc_name_column():
df = pd.DataFrame(
{"chrom": [f"chr{i}" for i in range(3)], "start": [1, 2, 3], "end": [4, 5, 6]}
)
pd.testing.assert_series_equal(
construction.add_ucsc_name_column(df)["name"],
pd.Series(
data=["chr0:1-4", "chr1:2-5", "chr2:3-6"], index=[0, 1, 2], name="name"
),
)
def test_any():
### tests copied from old parse_regions
# main functionality: convert to dataframe and create name
df = pd.DataFrame(
{"chrom": [f"chr{i}" for i in range(3)], "start": [1, 2, 3], "end": [4, 5, 6]}
)
parsed = from_any(df)
assert not "name" in parsed.columns
assert parsed.iloc[0]["chrom"] == "chr0"
# re-create dataframe from UCSC name alone
df2 = pd.DataFrame(
{
"regions": construction.add_ucsc_name_column(parsed, name_col="regions")[
"regions"
].values
}
)
assert (
(from_any(df2, name_col="regions")[["chrom", "start", "end"]] == parsed)
.all()
.all()
)
# re-parsing results yields the same
assert (from_any(parsed) == parsed).all().all()
# extra columns don't get overwritten
df["name"] = "test-value"
assert (from_any(df)["name"] == df["name"]).all()
# None or False will be parsed
assert from_any([("chr1", None, 5)], fill_null={"chr1": 10})["start"].values[0] == 0
# pull end from chromsizes
p2 = from_any([("chr1", 5, None)], fill_null={"chr1": 40})
assert list(p2.values[0]) == ["chr1", 5, 40]
# We could keep things as None if chromsizes were not proviced
p3 = from_any(["chr1", "chr2"], fill_null=False)
assert list(p3.values[0]) == ["chr1", None, None]
# parse the strange name
p8 = from_any(["chr1:1,000,000-4M"])
assert list(p8.values[0]) == ["chr1", 1000000, 4000000]
p9 = from_any(["chr1"])
assert list(p9.values[0]) == ["chr1", None, None]
with pytest.raises(ValueError):
from_any([("ch1", 1, 2, "chr1:1-2", "puppies")]) # puppies are not allowed
with pytest.raises(ValueError):
from_any([("chr1", 5, None)], fill_null={"chr2": 40})
# input tuple of tuples
p2 = from_any((("chr1", 5, 10), ("chrX", 10, 20)))
assert list(p2.values[0]) == ["chr1", 5, 10]
# input tuple of lists
p2 = from_any((["chr1", 5, 10], ["chrX", 10, 20]))
assert list(p2.values[0]) == ["chr1", 5, 10]
# input tuple of ucsc strings
p2 = from_any(("chr1:5-10",))
assert list(p2.values[0]) == ["chr1", 5, 10]
# input single tuple
p2 = from_any(("chr1", 5, 10))
assert list(p2.values[0]) == ["chr1", 5, 10]
def test_sanitize_bedframe():
df1 = pd.DataFrame(
[
["chr1", 10, 20],
["chr1", 10, 20],
["chr1", 15, np.nan],
["chr1", pd.NA, 25],
],
columns=["chrom", "start", "end"],
)
# drop rows with null values
sanitized_df1 = pd.DataFrame(
[["chr1", 10, 20], ["chr1", 10, 20]], columns=["chrom", "start", "end"]
)
sanitized_df1 = sanitized_df1.astype(
{"chrom": str, "start": pd.Int64Dtype(), "end": pd.Int64Dtype()}
)
pd.testing.assert_frame_equal(
sanitized_df1, construction.sanitize_bedframe(df1, drop_null=True)
)
# keep rows with null, but recast
sanitized_df1 = pd.DataFrame(
[
["chr1", 10, 20],
["chr1", 10, 20],
[pd.NA, pd.NA, pd.NA],
[pd.NA, pd.NA, pd.NA],
],
columns=["chrom", "start", "end"],
)
sanitized_df1 = sanitized_df1.astype(
{"chrom": object, "start": pd.Int64Dtype(), "end": pd.Int64Dtype()}
)
pd.testing.assert_frame_equal(
sanitized_df1.fillna(-1), construction.sanitize_bedframe(df1).fillna(-1)
)
# flip intervals as well as drop NA
df1 = pd.DataFrame(
[
["chr1", 20, 10],
["chr1", pd.NA, 25],
],
columns=["chrom", "start", "end"],
)
sanitized_df1 = pd.DataFrame([["chr1", 10, 20]], columns=["chrom", "start", "end"])
sanitized_df1 = sanitized_df1.astype(
{"chrom": str, "start": pd.Int64Dtype(), "end": pd.Int64Dtype()}
)
pd.testing.assert_frame_equal(
sanitized_df1,
construction.sanitize_bedframe(
df1, start_exceed_end_action="fLiP", drop_null=True
),
)
# flip intervals as well as drop NA
df1 = pd.DataFrame(
[
["chr1", 20, 10],
["chr1", pd.NA, 25],
],
columns=["chrom", "start", "end"],
)
sanitized_df1 = pd.DataFrame([["chr1", 10, 20]], columns=["chrom", "start", "end"])
sanitized_df1 = sanitized_df1.astype(
{"chrom": str, "start": pd.Int64Dtype(), "end": pd.Int64Dtype()}
)
assert construction.sanitize_bedframe(
df1, start_exceed_end_action="drop", drop_null=True
).empty
def test_make_viewframe():
# test dict input
view_df = pd.DataFrame(
[
["chrTESTX", 0, 10, "chrTESTX:0-10"],
["chrTESTX_p", 0, 12, "chrTESTX_p:0-12"],
],
columns=["chrom", "start", "end", "name"],
)
pd.testing.assert_frame_equal(
view_df.copy(),
construction.make_viewframe(
{"chrTESTX": 10, "chrTESTX_p": 12}, name_style="ucsc"
),
)
# test list input
region_list = [("chrTESTX", 0, 10), ("chrTESTX_p", 0, 12)]
pd.testing.assert_frame_equal(
view_df.copy(),
construction.make_viewframe(region_list, name_style="ucsc"),
)
# test pd.Series input
chromsizes = pd.Series(data=[5, 8], index=["chrTESTXq", "chrTEST_2p"])
d = """ chrom start end name
0 chrTESTXq 0 5 chrTESTXq
1 chrTEST_2p 0 8 chrTEST_2p"""
view_df = pd.read_csv(StringIO(d), sep=r"\s+")
pd.testing.assert_frame_equal(
view_df.copy(), construction.make_viewframe(chromsizes, name_style=None)
)
d = """ chrom start end name
0 chrTESTXq 0 5 chrTESTXq:0-5
1 chrTEST_2p 0 8 chrTEST_2p:0-8"""
view_df = pd.read_csv(StringIO(d), sep=r"\s+")
pd.testing.assert_frame_equal(
view_df.copy(),
construction.make_viewframe(chromsizes, name_style="UCSC"),
)
# test pd.DataFrame input
pd.testing.assert_frame_equal(view_df.copy(), construction.make_viewframe(view_df))
# if you provide unique names, this is accepted unchanged by make_viewframe
view_df = pd.DataFrame(
[["chrTESTX", 0, 10, "chrTEST_1"], ["chrTESTY", 0, 12, "chrTEST_2"]],
columns=["chrom", "start", "end", "name"],
)
region_list = [("chrTESTX", 0, 10, "chrTEST_1"), ("chrTESTY", 0, 12, "chrTEST_2")]
pd.testing.assert_frame_equal(
view_df.copy(), construction.make_viewframe(region_list)
)
pd.testing.assert_frame_equal(view_df.copy(), construction.make_viewframe(view_df))
pd.testing.assert_frame_equal(
view_df.copy(),
construction.make_viewframe(
view_df, check_bounds={"chrTESTX": 11, "chrTESTY": 13}
),
)
with pytest.raises(ValueError):
construction.make_viewframe(
view_df, check_bounds={"chrTESTX": 9, "chrTESTY": 13}
)
|