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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
from io import StringIO
import pandas as pd
import numpy as np
import pytest
from bioframe.core.checks import *
from bioframe.ops import sort_bedframe
def test_is_cataloged():
### chr2q is not in view
view_df = pd.DataFrame(
[
["chr1", 0, 12, "chr1p"],
["chr1", 13, 26, "chr1q"],
["chrX", 1, 8, "chrX_0"],
],
columns=["chrom", "start", "end", "name"],
)
df = pd.DataFrame(
[
["chr1", 0, 12, "chr1p"],
["chr1", 5, 15, "chr1p"],
["chr2", 13, 26, "chr2q"],
["chrX", 1, 8, "chrX_0"],
],
columns=["chrom", "start", "end", "view_region"],
)
assert not is_cataloged(df, view_df)
### chr1q is in view, df_view_col and view_name_col have funny labels.
view_df = pd.DataFrame(
[
["chr1", 0, 12, "chr1p"],
["chr1", 13, 26, "chr1q"],
["chrX", 1, 8, "chrX_0"],
],
columns=["chrom", "start", "end", "funny_name"],
)
df = pd.DataFrame(
[
["chr1", 0, 12, "chr1p"],
["chr2", 13, 26, "chr1q"],
["chrX", 1, 8, "chrX_0"],
],
columns=["chrom", "start", "end", "funny_view_region"],
)
assert is_cataloged(
df, view_df, df_view_col="funny_view_region", view_name_col="funny_name"
)
def test_is_contained():
view_df = pd.DataFrame(
[
["chr1", 0, 20, "chr1p"],
["chr1", 21, 30, "chr1q"],
["chrX", 1, 10, "chrX_0"],
],
columns=["chrom", "start", "end", "name"],
)
### not contained because chr2q is not cataloged
df = pd.DataFrame(
[
["chr1", 0, 12, "chr1p"],
["chr2", 13, 26, "chr2q"],
["chrX", 1, 8, "chrX_0"],
],
columns=["chrom", "start", "end", "view_region"],
)
assert not is_contained(df, view_df, df_view_col="view_region")
### not contained because second interval falls outside the view regions
df = pd.DataFrame(
[
["chr1", 14, 15, "chr1p"],
["chr1", -1, 1, "chr1q"],
["chrX", 1, 8, "chrX_0"],
],
columns=["chrom", "start", "end", "view_region"],
)
assert not is_contained(df, view_df)
df = pd.DataFrame(
[
["chr1", 12, 12, "chr1p"],
["chr1", 13, 14, "chr1q"],
["chrX", 1, 8, "chrX_0"],
],
columns=["chrom", "start", "end", "view_region"],
)
# is contained, because assignments are inferred
assert is_contained(df, view_df)
# is not contained, because assignments are not inferred
assert not is_contained(df, view_df, df_view_col="view_region")
### second interval falls completely out of the view
df = pd.DataFrame(
[
["chr1", 12, 12, "chr1p"],
["chr1", 100, 101, "chr1q"],
],
columns=["chrom", "start", "end", "view_region"],
)
# fails due to NAs after overlap to infer the regions
with pytest.raises(AssertionError):
is_contained(df, view_df, raise_errors=True)
# fails due to some of the intervals being trimmed
with pytest.raises(ValueError):
is_contained(df, view_df, df_view_col="view_region", raise_errors=True)
def test_is_overlapping():
### interval on chr1 overlaps
d = """chrom start end
0 chr1 3 6
1 chr1 5 10
2 chr2 5 10"""
df = pd.read_csv(StringIO(d), sep=r"\s+")
assert is_overlapping(df) is True
### adjacent intervals do not overlap
d = """chrom start end
0 chr1 3 6
1 chr1 6 10
2 chr2 5 10"""
df = pd.read_csv(StringIO(d), sep=r"\s+")
assert is_overlapping(df) is False
def test_is_covering():
### test is_covering where an interval from df completely overlaps
### two different regions from view
df1 = pd.DataFrame(
[
["chr1", -5, 25],
],
columns=["chrom", "start", "end"],
)
chromsizes = [("chr1", 0, 9, "chr1p"), ("chr1", 11, 20, "chr1q")]
assert is_covering(df1, chromsizes) is True
### test is_covering where two intervals from df overlap
### two different regions from view
df1 = pd.DataFrame(
[
["chr1", -5, 10],
["chr1", 11, 12],
["chr1", 12, 20],
],
columns=["chrom", "start", "end"],
)
chromsizes = [("chr1", 0, 9, "chr1p"), ("chr1", 11, 20, "chr1q")]
assert is_covering(df1, chromsizes) is True
### test is_covering where two intervals from df overlap
### two different regions from view
df1 = pd.DataFrame(
[
["chr1", -5, 10, "chr1q"],
["chr1", 11, 12, "chr1q"],
["chr1", 12, 20, "chr1q"],
],
columns=["chrom", "start", "end", "view_region"],
)
chromsizes = [("chr1", 0, 9, "chr1p"), ("chr1", 11, 20, "chr1q")]
assert is_covering(df1, chromsizes) is True
def test_is_tiling():
### view region chr1p is tiled by one interval, chr1q is tiled by two
df1 = pd.DataFrame(
[
["chr1", 0, 9, "chr1p"],
["chr1", 11, 12, "chr1q"],
["chr1", 12, 20, "chr1q"],
],
columns=["chrom", "start", "end", "view_region"],
)
chromsizes = [("chr1", 0, 9, "chr1p"), ("chr1", 11, 20, "chr1q")]
assert is_tiling(df1, chromsizes) is True
### not tiling, since (chr1,0,9) is associated with chr1q
df1 = pd.DataFrame(
[
["chr1", 0, 9, "chr1q"],
["chr1", 11, 12, "chr1q"],
["chr1", 12, 20, "chr1q"],
],
columns=["chrom", "start", "end", "view_region"],
)
chromsizes = [("chr1", 0, 9, "chr1p"), ("chr1", 11, 20, "chr1q")]
assert is_tiling(df1, chromsizes) is False
### not tiling, contains overlaps
df1 = pd.DataFrame(
[
["chr1", 0, 9, "chr1p"],
["chr1", 11, 13, "chr1q"],
["chr1", 12, 20, "chr1q"],
],
columns=["chrom", "start", "end", "view_region"],
)
chromsizes = [("chr1", 0, 9, "chr1p"), ("chr1", 11, 20, "chr1q")]
assert is_tiling(df1, chromsizes) is False
### not tiling, since it doesn't cover
df1 = pd.DataFrame(
[
["chr1", 11, 12, "chr1q"],
["chr1", 12, 20, "chr1q"],
],
columns=["chrom", "start", "end", "view_region"],
)
chromsizes = [("chr1", 0, 9, "chr1p"), ("chr1", 11, 20, "chr1q")]
assert is_tiling(df1, chromsizes) is False
def test_is_bedframe():
##missing a column
df1 = pd.DataFrame(
[
["chr1", 11],
["chr1", 12],
],
columns=["chrom", "start"],
)
assert is_bedframe(df1) is False
### end column has invalid dtype
df1 = pd.DataFrame(
[
["chr1", 10, "20"],
["chr1", 10, "12"],
],
columns=["chrom", "start", "end"],
)
assert is_bedframe(df1) is False
### second interval start > ends.
df1 = pd.DataFrame(
[
["chr1", 10, 20],
["chr1", 15, 10],
],
columns=["chrom", "start", "end"],
)
assert is_bedframe(df1) is False
### third interval has a null in one column
df1 = pd.DataFrame(
[
["chr1", 10, 20, "first"],
["chr1", 10, 15, "second"],
["chr1", pd.NA, 15, "third"],
],
columns=["chrom", "start", "end", "name"],
)
# should raise a TypeError if the second column is an object
with pytest.raises(TypeError):
is_bedframe(df1, raise_errors=True)
# should raise a ValueError after recasting to pd.Int64Dtype
df1 = df1.astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()})
with pytest.raises(ValueError):
is_bedframe(df1, raise_errors=True)
### first interval is completely NA
df1 = pd.DataFrame(
[
[pd.NA, pd.NA, pd.NA, "first"],
["chr1", 10, 15, "second"],
["chr1", 10, 15, "third"],
],
columns=["chrom", "start", "end", "name"],
)
df1 = df1.astype({"start": pd.Int64Dtype(), "end": pd.Int64Dtype()})
assert is_bedframe(df1) is True
def test_is_viewframe():
# not a bedframe
df1 = pd.DataFrame(
[
["chr1", 10, 20, "chr1p"],
["chr1", 15, 10, "chr1q"],
],
columns=["chrom", "start", "end", "name"],
)
assert is_viewframe(df1) is False
# no column for region name
df1 = pd.DataFrame(
[
["chr1", 10, 20],
["chr1", 30, 40],
],
columns=["chrom", "start", "end"],
)
assert is_viewframe(df1) is False
# contains null values
df1 = pd.DataFrame(
[
["chr1", 10, 20, "chr1p"],
["chr1", pd.NA, np.nan, "chr1q"],
],
columns=["chrom", "start", "end", "name"],
)
assert is_viewframe(df1) is False
# overlapping intervals
df1 = pd.DataFrame(
[
["chr1", 10, 20, "chr1p"],
["chr1", 15, 25, "chr1q"],
],
columns=["chrom", "start", "end", "name"],
)
assert is_viewframe(df1) is False
# valid view
df1 = pd.DataFrame(
[
["chr1", 10, 20, "chr1p"],
["chr1", 20, 25, "chr1q"],
["chr2", 20, 25, "chrTEST_2p"],
],
columns=["chrom", "start", "end", "name"],
)
assert is_viewframe(df1) is True
def test_is_sorted():
view_df = pd.DataFrame(
[
["chrX", 1, 8, "oranges"],
["chrX", 8, 20, "grapefruit"],
["chr1", 0, 10, "apples"],
],
columns=["chrom", "start", "end", "fruit"],
)
df_view_cat = pd.CategoricalDtype(
categories=["oranges", "grapefruit", "apples"], ordered=True
)
view_df = view_df.astype({"fruit": df_view_cat})
assert is_sorted(
view_df, view_df=view_df, view_name_col="fruit", df_view_col="fruit"
)
df = pd.DataFrame(
[
["chr1", 0, 10, "+"],
["chrX", 5, 10, "+"],
["chrX", 0, 5, "+"],
["chr2", 5, 10, "+"],
],
columns=["chrom", "start", "end", "strand"],
)
assert not is_sorted(df)
bfs = sort_bedframe(df, view_df=view_df, view_name_col="fruit")
assert is_sorted(bfs, view_df=view_df, view_name_col="fruit")
# view_df specifies a different ordering, so should not be sorted
assert not is_sorted(bfs)
|