-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support to read genomic ranges objects
- Loading branch information
Showing
6 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from genomicranges import GenomicRanges, SeqInfo | ||
from iranges import IRanges | ||
from biocframe import BiocFrame | ||
|
||
from .parser import get_class | ||
from .pdf import as_pandas_from_dframe | ||
|
||
__author__ = "jkanche" | ||
__copyright__ = "jkanche" | ||
__license__ = "MIT" | ||
|
||
|
||
def as_granges(robj): | ||
_cls = get_class(robj) | ||
|
||
if _cls not in ["GenomicRanges", "GRanges"]: | ||
raise TypeError(f"obj is not genomic ranges, but is `{_cls}`.") | ||
|
||
_range_start = robj["attributes"]["ranges"]["attributes"]["start"]["data"] | ||
_range_width = robj["attributes"]["ranges"]["attributes"]["width"]["data"] | ||
_range_names = None | ||
if "NAMES" in robj["attributes"]["ranges"]["attributes"]: | ||
_range_names = robj["attributes"]["ranges"]["attributes"]["NAMES"]["data"] | ||
_ranges = IRanges(_range_start, _range_width, names=_range_names) | ||
|
||
_seqnames = _as_list(robj["attributes"]["seqnames"]) | ||
|
||
_strand_obj = robj["attributes"]["strand"]["attributes"]["values"] | ||
_strands = _strand_obj["data"] | ||
if "attributes" in _strands: | ||
if "levels" in _strands["attributes"]: | ||
_levels_data = _strands["attributes"]["levels"]["data"] | ||
_strands = [_levels_data[x] for x in _strands] | ||
|
||
_seqinfo_seqnames = robj["attributes"]["seqinfo"]["attributes"]["seqnames"]["data"] | ||
_seqinfo_seqlengths = robj["attributes"]["seqinfo"]["attributes"]["seqlengths"][ | ||
"data" | ||
] | ||
_seqinfo_is_circular = robj["attributes"]["seqinfo"]["attributes"]["is_circular"][ | ||
"data" | ||
] | ||
_seqinfo_genome = robj["attributes"]["seqinfo"]["attributes"]["genome"]["data"] | ||
_seqinfo = SeqInfo( | ||
seqnames=_seqinfo_seqnames, | ||
seqlengths=[None if x == -2147483648 else x for x in _seqinfo_seqlengths], | ||
is_circular=[None if x == -2147483648 else x for x in _seqinfo_is_circular], | ||
genome=_seqinfo_genome, | ||
) | ||
|
||
_mcols = BiocFrame.from_pandas(as_pandas_from_dframe(robj["attributes"]["elementMetadata"])) | ||
|
||
_gr_names = None | ||
if "NAMES" in robj["attributes"]: | ||
_gr_names = robj["attributes"]["NAMES"]["data"] | ||
|
||
return GenomicRanges( | ||
seqnames=_seqnames, | ||
ranges=_ranges, | ||
names=_gr_names, | ||
mcols=_mcols, | ||
seqinfo=_seqinfo, | ||
) | ||
|
||
|
||
def _as_list(robj): | ||
_cls = get_class(robj) | ||
|
||
if _cls not in ["Rle"]: | ||
raise TypeError(f"obj is not Rle, but is `{_cls}`.") | ||
|
||
_attr_vals = robj["attributes"] | ||
_data = _attr_vals["values"]["data"].tolist() | ||
if "attributes" in _attr_vals["values"]: | ||
if "levels" in _attr_vals["values"]["attributes"]: | ||
_levels_data = _attr_vals["values"]["attributes"]["levels"]["data"] | ||
_data = [_levels_data[x-1] for x in _data] | ||
|
||
if "lengths" in _attr_vals: | ||
_final = [] | ||
_lengths = _attr_vals["lengths"]["data"] | ||
|
||
for idx, lg in enumerate(_lengths.tolist()): | ||
_final.extend([_data[idx]] * lg) | ||
|
||
_data = _final | ||
|
||
return _data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pytest | ||
|
||
from rds2py.granges import as_granges | ||
from rds2py.parser import read_rds | ||
|
||
from genomicranges import GenomicRanges | ||
|
||
__author__ = "jkanche" | ||
__copyright__ = "jkanche" | ||
__license__ = "MIT" | ||
|
||
|
||
def test_granges(): | ||
robj = read_rds("tests/data/granges.rds") | ||
|
||
gr = as_granges(robj=robj) | ||
|
||
assert isinstance(gr, GenomicRanges) |