Skip to content
Merged
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pyrle"
version = "0.0.42"
version = "0.0.43"
description = "Numeric run length encoding for Python."
readme = "README.md"
authors = [{ name = "Endre Bakken Stovner", email = "endbak@pm.me" }]
Expand Down
18 changes: 12 additions & 6 deletions pyrle/methods.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
from collections import defaultdict

Expand All @@ -9,6 +10,7 @@
from pyrle import rledict as rd # type: ignore
from pyrle.src.coverage import _coverage # type: ignore

logger = logging.getLogger(__name__)

class suppress_stdout_stderr(object):
"""
Expand Down Expand Up @@ -179,16 +181,20 @@ def to_ranges_df_no_strand(rle, k):


def to_ranges(grles, nb_cpu=1):
import pyranges as pr# type: ignore
try:
import pyranges1 as pr # type: ignore
except ModuleNotFoundError:
import pyranges as pr # type: ignore

func = to_ranges_df_strand if grles.stranded else to_ranges_df_no_strand

dfs = {k: func(v, k) for k, v in grles.items()}
dfs_list = [func(v, k) for k, v in grles.items()]

try: # new pyranges
return pr.from_dfs(dfs)
except: # legacy pyranges
return pr.PyRanges(pd.concat(dfs.values()))
try:
return pr.PyRanges(pd.concat(dfs_list))
except Exception:
logger.exception("It was not possible to return a PyRanges object. Returning a pandas Dataframe.")
return pd.concat(dfs_list)


def _to_ranges(rle):
Expand Down
5 changes: 4 additions & 1 deletion pyrle/rle.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,10 @@ def __getitem__(self, val):
return df
elif "PyRanges" in str(type(val)): # hack to avoid isinstance(key, pr.PyRanges) so that we
# do not need a dep on PyRanges in this library
import pyranges as pr # type: ignore
try:
import pyranges1 as pr # type: ignore
except ModuleNotFoundError:
import pyranges as pr # type: ignore

val = val.drop().df
if val.empty:
Expand Down
11 changes: 7 additions & 4 deletions pyrle/rledict.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ class RleDict:
Rle of length 10 containing 6 elements (avg. length 1.667)
Unstranded RleDict object with 2 chromosomes.

>>> import pyranges as pr
>>> gr = pr.data.chipseq()
>>> df = pr.data.chipseq_background().df
>>> import pyranges1 as pr
>>> gr = pr.example_data.chipseq
>>> df = pr.example_data.chipseq_background
>>> cs = RleDict(gr, stranded=True)
>>> bg = RleDict(df, stranded=True)

Expand Down Expand Up @@ -257,7 +257,10 @@ def __getitem__(self, key):
# do not need a dep on PyRanges in this library

import pandas as pd
import pyranges as pr # type: ignore
try:
import pyranges1 as pr # type: ignore
except ModuleNotFoundError:
import pyranges as pr # type: ignore

if not len(key):
return pd.DataFrame(columns="Chromosome Start End ID Run Value".split())
Expand Down
5 changes: 4 additions & 1 deletion tests/hypothesis_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from hypothesis.extra.numpy import arrays
import hypothesis.strategies as st

from pyranges import PyRanges
try:
from pyranges1 import PyRanges
except ModuleNotFoundError:
from pyranges import PyRanges

import numpy as np

Expand Down
5 changes: 4 additions & 1 deletion tests/test_hypothesis_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
from io import StringIO

from pyrle import Rle
import pyranges as pr
try:
import pyranges1 as pr # type: ignore
except ModuleNotFoundError:
import pyranges as pr # type: ignore

import pandas as pd
import numpy as np
Expand Down
Loading