Skip to content

Commit 66b8fd6

Browse files
authored
Allow file pointers in tileset loaders (#161)
* feat: Added by_filetype * Support file-like objects * Change wrapper filepath type * Revert widget.js * Updated the CHANGELOG * More accurate variable names and more modern type annotation
1 parent 4b9996c commit 66b8fd6

File tree

3 files changed

+399
-15
lines changed

3 files changed

+399
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [v1.0.2](https://github.com/higlass/higlass-python/compare/v1.0.2...v1.0.1)
2+
3+
- Calculate file pointer hash for track uids for tileset tracks
4+
15
## [v1.0.1](https://github.com/higlass/higlass-python/compare/v1.0.1...v1.0.0)
26

37
- Pass kwargs in to Viewconf.widget() so that they can be passed on to the higlass widget and potentially make their way to the higlass component

src/higlass/tilesets.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pathlib
66
import typing
77
from dataclasses import dataclass
8+
from typing import IO
89

910
from ._utils import TrackType
1011
from .api import track
@@ -71,19 +72,21 @@ def remote(uid: str, server: str = "https://higlass.io/api/v1", **kwargs):
7172
return RemoteTileset(uid, server, **kwargs)
7273

7374

74-
def hash_absolute_filepath_as_default_uid(
75-
fn: typing.Callable[[str, str], LocalTileset]
76-
):
77-
def wrapper(filepath: str, uid: None | str = None):
75+
def hash_file_as_default_uid(fn: typing.Callable[[str | IO[bytes], str], LocalTileset]):
76+
def wrapper(file: str | IO[bytes], uid: None | str = None):
7877
if uid is None:
79-
abspath = pathlib.Path(filepath).absolute()
80-
uid = hashlib.md5(str(abspath).encode()).hexdigest()
81-
return fn(filepath, uid)
78+
if isinstance(file, str):
79+
abspath = pathlib.Path(file).absolute()
80+
uid = hashlib.md5(str(abspath).encode()).hexdigest()
81+
else:
82+
# File-like object likely provided
83+
uid = hashlib.md5(str(hash(file)).encode()).hexdigest()
84+
return fn(file, uid)
8285

8386
return wrapper
8487

8588

86-
@hash_absolute_filepath_as_default_uid
89+
@hash_file_as_default_uid
8790
def bigwig(filepath: str, uid: str):
8891
try:
8992
from clodius.tiles.bigwig import tiles, tileset_info
@@ -100,7 +103,7 @@ def bigwig(filepath: str, uid: str):
100103
)
101104

102105

103-
@hash_absolute_filepath_as_default_uid
106+
@hash_file_as_default_uid
104107
def beddb(filepath: str, uid: str):
105108
try:
106109
from clodius.tiles.beddb import tiles, tileset_info
@@ -117,7 +120,7 @@ def beddb(filepath: str, uid: str):
117120
)
118121

119122

120-
@hash_absolute_filepath_as_default_uid
123+
@hash_file_as_default_uid
121124
def multivec(filepath: str, uid: str):
122125
try:
123126
from clodius.tiles.multivec import tiles, tileset_info
@@ -134,7 +137,7 @@ def multivec(filepath: str, uid: str):
134137
)
135138

136139

137-
@hash_absolute_filepath_as_default_uid
140+
@hash_file_as_default_uid
138141
def cooler(filepath: str, uid: str):
139142
try:
140143
from clodius.tiles.cooler import tiles, tileset_info
@@ -151,7 +154,7 @@ def cooler(filepath: str, uid: str):
151154
)
152155

153156

154-
@hash_absolute_filepath_as_default_uid
157+
@hash_file_as_default_uid
155158
def hitile(filepath: str, uid: str):
156159
try:
157160
from clodius.tiles.hitile import tiles, tileset_info
@@ -168,7 +171,7 @@ def hitile(filepath: str, uid: str):
168171
)
169172

170173

171-
@hash_absolute_filepath_as_default_uid
174+
@hash_file_as_default_uid
172175
def bed2ddb(filepath: str, uid: str):
173176
try:
174177
from clodius.tiles.bed2ddb import tiles, tileset_info
@@ -183,3 +186,6 @@ def bed2ddb(filepath: str, uid: str):
183186
info=functools.partial(tileset_info, filepath),
184187
uid=uid,
185188
)
189+
190+
191+
by_filetype = {"cooler": cooler, "bigwig": bigwig}

0 commit comments

Comments
 (0)