-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserve.py
162 lines (127 loc) · 4.57 KB
/
serve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import click
import higlass as hg
import negspy.coordinates
import os
def bedpedb(filepath, uuid=None, **kwargs):
from clodius.tiles.bed2ddb import tileset_info, tiles
return hg.Tileset(
uuid=uuid,
tileset_info=lambda: tileset_info(filepath),
tiles=lambda tile_ids: tiles(filepath, tile_ids),
**kwargs
)
def multivec(filepath, uuid=None, **kwargs):
from clodius.tiles.multivec import tileset_info, tiles
return hg.Tileset(
uuid=uuid,
tileset_info=lambda: tileset_info(filepath),
tiles=lambda tile_ids: tiles(filepath, tile_ids),
**kwargs
)
def get_filename_assembly(filepath):
_, filename_with_ext = os.path.split(filepath)
parts = filename_with_ext.split('.')
assembly = parts[-2]
if assembly in negspy.coordinates.available_chromsizes():
return ''.join(parts[:-2]), assembly, ''.join(parts)
return ''.join(parts[:-1]), None, ''.join(parts)
def get_chromsizes(filename):
assembly = filename.split('.')[-2]
if assembly in negspy.coordinates.available_chromsizes():
return negspy.coordinates.get_chromsizes(assembly)
return None
def bigwig_to_tileset(filepath):
filename, assembly, uuid = get_filename_assembly(filepath)
if assembly is not None:
chromsizes = negspy.coordinates.get_chromsizes(assembly)
return hg.tilesets.bigwig(
filepath=filepath,
chromsizes=chromsizes,
name=filename,
uuid=uuid,
datatype='vector',
)
def beddb_to_tileset(filepath):
filename, assembly, uuid = get_filename_assembly(filepath)
if assembly is not None:
chromsizes = negspy.coordinates.get_chromsizes(assembly)
return hg.tilesets.beddb(
filepath=filepath,
chromsizes=chromsizes,
name=filename,
uuid=uuid,
datatype='bedlike',
)
def bedpedb_to_tileset(filepath):
filename, assembly, uuid = get_filename_assembly(filepath)
if assembly is not None:
chromsizes = negspy.coordinates.get_chromsizes(assembly)
return bedpedb(
filepath=filepath,
chromsizes=chromsizes,
name=filename,
uuid=uuid,
datatype='bedlike',
)
def multivec_to_tileset(filepath):
filename, assembly, uuid = get_filename_assembly(filepath)
if assembly is not None:
chromsizes = negspy.coordinates.get_chromsizes(assembly)
return multivec(
filepath=filepath,
chromsizes=chromsizes,
name=filename,
uuid=uuid,
datatype='multivec',
)
def is_not_none(x):
return x is not None
@click.command()
@click.argument('dir')
@click.option('--host', '-h', type=str, default='localhost', show_default=True)
@click.option('--port', '-p', type=int, default=9876, show_default=True)
@click.option('--verbose', '-v', count=True)
def serve(dir, host, port, verbose):
bigwigs = []
bigbeds = []
beddbs = []
bedpedbs = []
multivecs = []
for file in os.listdir(dir):
if file.endswith('.bigwig'):
bigwigs.append(os.path.join(dir, file))
if file.endswith('.bigbed'):
bigbeds.append(os.path.join(dir, file))
if file.endswith('.beddb'):
beddbs.append(os.path.join(dir, file))
if file.endswith('.bedpedb'):
bedpedbs.append(os.path.join(dir, file))
if file.endswith('.multivec'):
multivecs.append(os.path.join(dir, file))
if verbose > 0:
print(f'Found {len(bigwigs)} .bigwig files')
print(f'Found {len(bigbeds)} .bigbed files')
print(f'Found {len(beddbs)} .beddb files')
print(f'Found {len(bedpedbs)} .bedpedb files')
print(f'Found {len(multivecs)} .multivec files')
tilesets = [
*list(filter(is_not_none, map(bigwig_to_tileset, bigwigs))),
*list(filter(is_not_none, map(beddb_to_tileset, beddbs))),
*list(filter(is_not_none, map(bedpedb_to_tileset, bedpedbs))),
*list(filter(is_not_none, map(multivec_to_tileset, multivecs))),
]
if len(tilesets) > 0:
server = hg.server.create_app('higlass', tilesets)
print(f'Serving {len(tilesets)} tilesets from http://{host}:{9876}')
print(f'See http://{host}:{9876}/api/v1/tilesets/')
server.run(
threaded=True,
debug=True,
host=host,
port=9876,
use_reloader=False
)
else:
print('Nothing to serve my lord! Good night.')
if __name__ == '__main__':
serve()