-
Notifications
You must be signed in to change notification settings - Fork 11
/
h5_zarr.py
executable file
·55 lines (43 loc) · 1.48 KB
/
h5_zarr.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
#!/usr/bin/env python
from optparse import OptionParser
import h5py
import numpy as np
import zarr
'''
h5_zarr.py
Convert a coverage HDF5 to BigWig.
'''
################################################################################
# main
################################################################################
def main():
usage = 'usage: %prog [options] <in_h5_file> <out_zarr_file>'
parser = OptionParser(usage)
parser.add_option('-c', dest='chunk_size', default=None, type='int')
parser.add_option('-v', dest='verbose', default=False, action='store_true')
(options,args) = parser.parse_args()
if len(args) != 2:
parser.error('Must provide input HDF5 and output BigWig.')
else:
hdf5_file = args[0]
zarr_file = args[1]
# open files
h5_in = h5py.File(hdf5_file)
zarr_out = zarr.open_group(zarr_file, 'w')
# foreach chromosome
for chrom in h5_in.keys():
if options.verbose:
print(chrom)
# read values
x = np.array(h5_in[chrom], dtype='float16')
# write gzipped into HDF5
z = zarr_out.create_dataset(chrom, data=x, shape=x.shape, dtype='float16', chunks=options.chunk_size)
if options.verbose:
print(z)
# close files
h5_in.close()
################################################################################
# __main__
################################################################################
if __name__ == '__main__':
main()