55import pathlib
66import typing
77from dataclasses import dataclass
8+ from typing import IO
89
910from ._utils import TrackType
1011from .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
8790def 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
104107def 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
121124def 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
138141def 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
155158def 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
172175def 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