Skip to content

Commit

Permalink
update sort-related docstring, and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergpolly committed Nov 15, 2021
1 parent faca970 commit 4b1e193
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
11 changes: 8 additions & 3 deletions bioframe/core/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def is_sorted(
df,
view_df=None,
reset_index=True,
df_view_col="view_region",
df_view_col=None,
view_name_col="name",
cols=None,
):
Expand All @@ -439,14 +439,19 @@ def is_sorted(
----------
df : pandas.DataFrame
view_df : pandas.DataFrame or None
view_df : pandas.DataFrame | dict-like
Optional view to pass to ``sort_bedframe``.
When it is dict-like :func:'bioframe.make_viewframe' will
be used to convert to viewframe. If view_df is not provided
df is assumed to be sorted by chrom and start.
reset_index : bool
Optional argument to pass to ``sort_bedframe``.
df_view_col: str
df_view_col: None | str
Name of column from df that indicates region in view.
If None, :func:'bioframe.assign_view' will be used to assign view regions.
Default None.
view_name_col: str
Name of column from view that specifies unique region name.
Expand Down
12 changes: 7 additions & 5 deletions bioframe/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1577,19 +1577,21 @@ def sort_bedframe(
df : pandas.DataFrame
Valid bedframe.
view_df : pandas.DataFrame
Valid input to make a viewframe.
view_df : pandas.DataFrame | dict-like
Valid input to make a viewframe. When it is dict-like
:func:'bioframe.make_viewframe' will be used to convert
to viewframe. If view_df is not provided df is sorted by chrom and start.
reset_index : bool
Default True.
df_view_col:
df_view_col: None | str
Column from 'df' used to associate intervals with view regions.
The associated region in 'view_df' is then used for sorting.
If None, :func:'bioframe.ops.assign_view' will be used to assign view regions.
If None, :func:'bioframe.assign_view' will be used to assign view regions.
Default None.
view_name_col:
view_name_col: str
Column from view_df with names of regions.
Default `name`.
Expand Down
25 changes: 7 additions & 18 deletions tests/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1649,10 +1649,10 @@ def test_sort_bedframe():

df = pd.DataFrame(
[
["chr2", 5, 10, "+"],
["chr1", 0, 10, "+"],
["chrX", 5, 10, "+"],
["chrX", 0, 5, "+"],
["chr2", 5, 10, "+"],
],
columns=["chrom", "start", "end", "strand"],
)
Expand All @@ -1674,23 +1674,12 @@ def test_sort_bedframe():
# are placed last and view_region is returned as a categorical
df_sorted = pd.DataFrame(
[
["chrX", 0, 5, "+", "oranges"],
["chrX", 5, 10, "+", "oranges"],
["chr1", 0, 10, "+", "apples"],
["chr2", 5, 10, "+", pd.NA],
["chrX", 0, 5, "+"],
["chrX", 5, 10, "+"],
["chr1", 0, 10, "+"],
["chr2", 5, 10, "+"],
],
columns=["chrom", "start", "end", "strand", "view_region"],
)
df_view_cat = pd.CategoricalDtype(
categories=["oranges", "grapefruit", "apples"], ordered=True
)
df_sorted = df_sorted.astype(
{
"chrom": str,
"start": pd.Int64Dtype(),
"end": pd.Int64Dtype(),
"view_region": df_view_cat,
}
columns=["chrom", "start", "end", "strand"],
)

pd.testing.assert_frame_equal(
Expand Down Expand Up @@ -1737,5 +1726,5 @@ def test_sort_bedframe():
)
assert (
df.dtypes
== bioframe.sort_bedframe(df, view_df, view_name_col="fruit").dtypes[:-1]
== bioframe.sort_bedframe(df, view_df, view_name_col="fruit").dtypes
).all()

0 comments on commit 4b1e193

Please sign in to comment.