-
Notifications
You must be signed in to change notification settings - Fork 0
/
vsi2tiff.py
71 lines (57 loc) · 2.45 KB
/
vsi2tiff.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
import argparse
import bioformats
import javabridge
import os
import re
import subprocess
from utils import run_command
def _init_logger():
"""This is so that Javabridge doesn't spill out a lot of DEBUG messages
during runtime.
From CellProfiler/python-bioformats.
"""
rootLoggerName = javabridge.get_static_field("org/slf4j/Logger",
"ROOT_LOGGER_NAME",
"Ljava/lang/String;")
rootLogger = javabridge.static_call("org/slf4j/LoggerFactory",
"getLogger",
"(Ljava/lang/String;)Lorg/slf4j/Logger;",
rootLoggerName)
logLevel = javabridge.get_static_field("ch/qos/logback/classic/Level",
"WARN",
"Lch/qos/logback/classic/Level;")
javabridge.call(rootLogger,
"setLevel",
"(Lch/qos/logback/classic/Level;)V",
logLevel)
parser = argparse.ArgumentParser()
parser.add_argument('--input_vsi', type=str, required=True)
#parser.add_argument('--sample_name', type=str, required=True)
parser.add_argument('--output_dir', type=str, required=True)
parser.add_argument('--bftools_dir', type=str, default='/mnt/data2/shared/Lymphomoid-IF-software/bftools')
args = parser.parse_args()
if not os.path.splitext(args.input_vsi):
raise ValueError('The path from --input_vsi should be the path to the .vsi file.')
parent_path, vsi_name = os.path.split(args.input_vsi)
vsi_name = os.path.splitext(vsi_name)[0]
sample_name = os.path.split(parent_path)[1]
javabridge.start_vm(class_path=bioformats.JARS)
try:
_init_logger()
omexml = bioformats.get_omexml_metadata(args.input_vsi)
o = bioformats.OMEXML(omexml)
regex = "[0-9]{2}x_[0-9]{2}"
count = 1
for i in range(o.image_count):
if re.match(regex, o.image(i).Name):
output_path = os.path.join(args.output_dir, f'{sample_name}_{vsi_name}_acq{count:02d}.ome.tif')
if os.path.exists(output_path):
os.remove(output_path)
run_command(f"{os.path.join(args.bftools_dir, 'bfconvert')} -option BF_MAXMEM 128g -series {i} -bigtiff -pyramid-resolutions 1 {args.input_vsi} {output_path}")
print(f'Acquisition {count} exported to {output_path}')
print()
count += 1
except Exception as e:
javabridge.kill_vm()
raise e
javabridge.kill_vm()