This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
ome2analysis.py
184 lines (159 loc) · 5.63 KB
/
ome2analysis.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import logging
import os
from typing import Sequence, Tuple, Type
import numpy as np
import pandas as pd
from imctools.io.ometiff.ometiffparser import OmeTiffParser
logger = logging.getLogger(__name__)
def omefile_2_analysisfolder(
filename: str,
output_folder: str,
basename: str,
panel_csv_file: str = None,
metalcolumn: str = "Metal Tag",
masscolumn: str = None,
usedcolumn: str = None,
add_sum=False,
bigtiff=False,
sort_channels=True,
dtype: Type = None,
):
"""Converts single OME-TIFF file to folder compatible with IMC segmentation pipeline.
Parameters
----------
filename
Path to input OME-TIFF file.
output_folder
Output folder.
basename
Basename of the acquisition.
panel_csv_file
Name of the CSV file that contains the channels to be written out.
metalcolumn
Column name of the metal names.
masscolumn
Column name of the mass names. If provided the metal column will be ignored.
usedcolumn
Column that should contain booleans (0, 1) if the channel should be used, i.e. "ilastik".
add_sum
Add the sum of the data as the first layer.
bigtiff
Whether to save TIFF files in BigTIFF format.
sort_channels
Whether to sort channels by mass.
dtype
Output Numpy data type.
"""
metals = None
masses = None
if not os.path.exists(output_folder):
os.makedirs(output_folder)
outname = os.path.join(output_folder, basename)
if panel_csv_file is not None:
pannel = pd.read_csv(panel_csv_file)
if pannel.shape[1] > 1:
selected = pannel[usedcolumn]
if masscolumn is None:
metalcolumn = metalcolumn
metals = [str(n) for s, n in zip(selected, pannel[metalcolumn]) if s]
else:
masses = [str(n) for s, n in zip(selected, pannel[masscolumn]) if s]
else:
metals = [pannel.columns[0]] + pannel.iloc[:, 0].tolist()
ome = OmeTiffParser(filename)
acquisition_data = ome.get_acquisition_data()
if sort_channels:
if metals is not None:
def mass_from_met(x):
return "".join([m for m in x if m.isdigit()]), x
metals = sorted(metals, key=mass_from_met)
if masses is not None:
masses = sorted(masses)
acquisition_data.save_tiff(
outname + ".tiff", names=metals, masses=masses, add_sum=add_sum, imagej=True, bigtiff=bigtiff, dtype=dtype
)
if masses is not None:
savenames = masses
elif metals is not None:
savenames = metals
else:
savenames = [s for s in acquisition_data.channel_names]
if add_sum:
savenames = ["sum"] + savenames
with open(outname + ".csv", "w") as f:
for n in savenames:
f.write(n + "\n")
def omefolder_to_analysisfolder(
input_folder: str,
output_folder: str,
panel_csv_file: str,
analysis_stacks: Sequence[Tuple[str, str, bool]],
metalcolumn: str = "Metal Tag",
masscolumn: str = None,
dtype=np.uint16,
):
"""Convert OME tiffs to analysis tiffs that are more compatible with tools. A CSV with a boolean column can be used to select subsets of channels or metals from the stack. The channels of the tiff will have the same order as in the csv.'
Parameters
----------
input_folder
Input folder
output_folder
Output folder
panel_csv_file
Name of the CSV file that contains the channels to be written out.
analysis_stacks
Array of analysis stack definitions in a tuple format (column, suffix, add_sum).
metalcolumn
Column name of the metal names.
masscolumn
Column name of the mass names. If provided the metal column will be ignored.
dtype
Output numpy dtype
"""
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for fol in os.listdir(input_folder):
sub_fol = os.path.join(input_folder, fol)
for img in os.listdir(sub_fol):
if not img.endswith(".ome.tiff"):
continue
basename = img.rstrip(".ome.tiff")
for (col, suffix, add_sum) in analysis_stacks:
try:
omefile_2_analysisfolder(
os.path.join(sub_fol, img),
output_folder,
basename + suffix,
panel_csv_file=panel_csv_file,
metalcolumn=metalcolumn,
masscolumn=masscolumn,
usedcolumn=col,
add_sum=add_sum,
bigtiff=False,
dtype=dtype,
)
except:
logger.exception("Error in {}".format(img))
if __name__ == "__main__":
import timeit
tic = timeit.default_timer()
omefile_2_analysisfolder(
"/home/anton/Downloads/imc_folder/20170905_Fluidigmworkshopfinal_SEAJa/20170905_Fluidigmworkshopfinal_SEAJa_s0_a0_ac.ome.tiff",
"/home/anton/Downloads/analysis_folder",
"test",
panel_csv_file="/home/anton/Downloads/example_panel.csv",
metalcolumn="Metal Tag",
usedcolumn="ilastik",
add_sum=True,
)
# omefolder_to_analysisfolder(
# "/home/anton/Downloads/imc_folder",
# "/home/anton/Downloads/analysis_folder",
# "/home/anton/Downloads/example_panel.csv",
# [
# ("ilastik", "_ilastik", True),
# ("full", "_full", False)
# ],
# metalcolumn="Metal Tag",
# )
print(timeit.default_timer() - tic)