-
Notifications
You must be signed in to change notification settings - Fork 5
/
Pipeline.py
305 lines (245 loc) · 13.6 KB
/
Pipeline.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import spaceM
import pandas as pd
import os, gc
from subprocess import call
import GUI_maldi_helper
def getPath(field):
return pd.read_json(os.path.dirname(spaceM.__file__) + '\\paths.json')[field].values[0]
def curator(load_path, plot_title):
os.rename(load_path, load_path.split('.')[0] + '_old.' + load_path.split('.')[1])
call(['python', os.path.dirname(GUI_maldi_helper.__file__) + '\\MaldiHelper.py',
load_path.split('.')[0] + '_old.' + load_path.split('.')[1],
load_path,
plot_title])
def stitchMicroscopy(MF,
merge_colors,
merge_filenames,
tf,
preMALDI=True,
postMALDI=True,
):
"""Function to stitch tile microscopy images into a single one. The function first applies a transformation (tf) on
each tile images prior to stitching. It also merges defined fields of stitched images together into an RGB .png
file.
Args:
MF (str): path to the Main Folder.
merge_colors (list): list of string of color names: 'red', 'green', 'blue', 'gray', 'cyan', 'magenta', 'yellow'.
merge_filenames (list): list of string of image files names to merge. Their sequence in the list should match their
respective color in the 'colors' argument. After stitching they should start with 'img_t1_z1_c ... '.
tf (fun): image transformation to apply to the tile images prior to stitching.
preMALDI (bool): whether or not stithcing preMALDI dataset.
postMALDI (bool): whether or not stithcing postMALDI dataset.
Data are stored in MF + /Analysis/StitchedMicroscopy/
"""
if not os.path.exists(MF + 'Analysis/'):
os.makedirs(MF + 'Analysis/')
os.mkdir(MF + 'Analysis/StitchedMicroscopy/')
if preMALDI:
if not os.path.exists(MF + 'Analysis/StitchedMicroscopy/preMALDI_FLR/'):
os.makedirs(MF + 'Analysis/StitchedMicroscopy/preMALDI_FLR/')
tif_files = spaceM.ImageFileManipulation.manipulations.PixFliplr(
tf,
MF + 'Input/Microscopy/preMALDI/',
MF + 'Analysis/StitchedMicroscopy/preMALDI_FLR/')
spaceM.ImageFileManipulation.FIJIcalls.TileConfFormat(path= MF + 'Input/Microscopy/preMALDI/',
dir_fliplr=MF + 'Analysis/StitchedMicroscopy/preMALDI_FLR/',
tif_files=tif_files)
gc.collect()
spaceM.ImageFileManipulation.FIJIcalls.callFIJIstitch(MF + 'Analysis/StitchedMicroscopy/preMALDI_FLR/')
spaceM.ImageFileManipulation.FIJIcalls.callFIJIstitch_noCompute(dir_in=MF + 'Analysis/StitchedMicroscopy/preMALDI_FLR/' + 'other_channels/',
dir_out=MF + 'Analysis/StitchedMicroscopy/preMALDI_FLR/')
print('Pre-MALDI Stitching finished')
if postMALDI:
if not os.path.exists(MF + 'Analysis/StitchedMicroscopy/postMALDI_FLR/'):
os.makedirs(MF + 'Analysis/StitchedMicroscopy/postMALDI_FLR/')
tif_files = spaceM.ImageFileManipulation.manipulations.PixFliplr(
tf,
MF + 'Input/Microscopy/postMALDI/',
MF + 'Analysis/StitchedMicroscopy/postMALDI_FLR/')
spaceM.ImageFileManipulation.FIJIcalls.TileConfFormat(path=MF + 'Input/Microscopy/postMALDI/',
dir_fliplr=MF + 'Analysis/StitchedMicroscopy/postMALDI_FLR/',
tif_files=tif_files)
gc.collect()
spaceM.ImageFileManipulation.FIJIcalls.callFIJIstitch(MF + 'Analysis/StitchedMicroscopy/postMALDI_FLR/')
try:
spaceM.ImageFileManipulation.FIJIcalls.callFIJIstitch_noCompute(dir_in=MF + 'Analysis/StitchedMicroscopy/postMALDI_FLR/' + 'other_channels/',
dir_out=MF + 'Analysis/StitchedMicroscopy/postMALDI_FLR/')
except FileNotFoundError:
print('Only one channel in postMALDI')
print('Pre-MALDI Stitching finished')
if merge_colors != []:
spaceM.ImageFileManipulation.FIJIcalls.callFIJImergeChannels(
base_path=MF + 'Analysis/StitchedMicroscopy/preMALDI_FLR/',
colors=merge_colors,
filenames=merge_filenames,
save_filename='Composite.png')
def ablationMarksFinder_old(MF):
"""Find the ablation marks on the tile images.
Args:
MF (str): path to the Main Folder.
Data are stored in MF + /Analysis/gridFit/
"""
if not os.path.exists(MF + 'Analysis/gridFit/'):
os.makedirs(MF + 'Analysis/gridFit/')
spaceM.Registration.AblationMarkFinder.MarkFinderFT(MF)
def ablationMarks_crop(MF, im_name='img_t2_z1_c1'):
if not os.path.exists(MF + 'Analysis/gridFit/'):
os.makedirs(MF + 'Analysis/gridFit/')
call(['python', os.path.dirname(GUI_maldi_helper.__file__) + '\\MaldiHelper.py',
MF + 'Analysis\\StitchedMicroscopy\\postMALDI_FLR\\{}'.format(im_name),
MF + 'Analysis\\gridFit\\AM_cropped.tif',
'Select AM, crop, save, close'])
# def ablationMarksFinder_new(MF):
#
# spaceM.Registration.AblationMarkFinder.MarkFinderFT(MF)
def ablationMarksFilter(MF, crop_2ndImg=True, im_crop_source='img_t1_z1_c1', marks_check=True, matrix='DAN'):
"""Filters ablation marks. First by re-running the ablation mark detection on the cropped stitched images where the
ablation marks are. Then by fitting a theoretical grid on the detections and taking only teh closest detection to
each grid node. This filters out double detections and re-orders the remaning ones into a uniform index which matches
later on the index of the ion image. The detections after filtering can be visualized in 'ili (https://ili.embl.de/).
Args:
MF (str): path to the Main Folder.
marks_check (bool): whether or not show the results.
Data are stored in MF + /Analysis/gridFit/
Visualization are stored in MF + /Analysis/gridFit/marks_check/
"""
if crop_2ndImg:
spaceM.Registration.AblationMarkFinder.crop_img(MF, im_p=MF+'Analysis/StitchedMicroscopy/postMALDI_FLR/{}'.format(im_crop_source),
coords_p=MF+'Analysis/gridFit/AM_cropped_coords.npy')
spaceM.Registration.AblationMarkFinder.GridFit(MF, matrix=matrix)
if marks_check:
if not os.path.exists(MF + 'Analysis/gridFit/marks_check/'):
os.makedirs(MF + 'Analysis/gridFit/marks_check/')
spaceM.ImageFileManipulation.manipulations.crop2coords(
MF + 'Analysis/gridFit/xye_clean2.npy',
MF + 'Analysis/StitchedMicroscopy/postMALDI_FLR/img_t1_z1_c0',
MF + 'Analysis/gridFit/marks_check/PHASE_crop_bin1x1_window100.tiff',
window=100)
if matrix == 'DHB':
spaceM.ImageFileManipulation.manipulations.crop2coords(
MF + 'Analysis/gridFit/xye_clean2.npy',
MF + 'Analysis/StitchedMicroscopy/postMALDI_FLR/img_t2_z1_c1',
MF + 'Analysis/gridFit/marks_check/Fluo_crop_bin1x1_window100.tiff',
window=100)
spaceM.ImageFileManipulation.manipulations.crop2coords(
MF + 'Analysis/gridFit/xye_clean2.npy',
MF + 'Analysis/StitchedMicroscopy/postMALDI_FLR/img_t1_z1_c0',
MF + 'Analysis/gridFit/marks_check/PHASE_crop_bin1x1.png',
window=0)
nbin = spaceM.ImageFileManipulation.FIJIcalls.imbin4ili(
MF + 'Analysis/gridFit/marks_check/PHASE_crop_bin1x1.png',
maxsize=50e6)
predata = spaceM.WriteILIinput.preCSVdatagen(
MF + 'Analysis/gridFit/xye_clean2.npy',
radius=10,
nbin=nbin,
PlainFirst=True)
spaceM.WriteILIinput.writeCSV(
path=MF + 'Analysis/gridFit/marks_check/ablation_marks_checkDETECTIONS.csv',
data=predata)
predata = spaceM.WriteILIinput.preCSVdatagen(
MF + 'Analysis/gridFit/xye_grid.npy',
radius=10,
nbin=nbin,
PlainFirst=True)
spaceM.WriteILIinput.writeCSV(
path=MF + 'Analysis/gridFit/marks_check/ablation_marks_checkTHEORETICAL.csv',
data=predata)
if not os.path.exists(MF + 'Analysis/gridFit/marksMask.npy'):
spaceM.Registration.AblationMarkFinder.regionGrowingAblationMarks(MF,
grow_thresh=0.60,
blur=False,
sigma=2,
matrix='DAN',
refine_seed=True,
FT_filtered=False,
maxDist=20) # blur=True and grow_thresh=0.25 for DAN
spaceM.Registration.AblationMarkFinder.AM_filter(MF, n_std=4)
def fiducialsFinder(MF):
"""Find the fiducials coordinates on the stitched images.
Args:
MF (str): path to the Main Folder.
Data are stored in MF + /Analysis/Fiducials/
"""
if not os.path.exists(MF + 'Analysis/Fiducials/'):
os.makedirs(MF + 'Analysis/Fiducials/')
spaceM.Registration.ImageRegistration.penMarksFeatures(MF, prefix='post')
spaceM.Registration.ImageRegistration.penMarksFeatures(MF, prefix='pre')
def registration(MF, tf_obj='dummy', do_transform=True, do_ili=True, ili_fdr=0.1):
if not os.path.exists(MF + 'Analysis/Fiducials/optimized_params.npy'):
spaceM.Registration.ImageRegistration.fiducialsAlignment(MF,
src=MF+'Analysis/Fiducials/postXYpenmarks.npy',
dst=MF+'Analysis/Fiducials/preXYpenmarks.npy')
if do_transform:
spaceM.Registration.ImageRegistration.TransformMarks(MF)
if do_ili:
if not os.path.exists(MF + 'Analysis/ili/'):
os.makedirs(MF + 'Analysis/ili/')
if not os.path.exists(MF + 'Analysis/ili/FLUO_crop_bin1x1.png'):
spaceM.ImageFileManipulation.manipulations.crop2coords(
coords_p=MF + 'Analysis/Fiducials/transformedMarks.npy',
img_p=MF + 'Analysis/StitchedMicroscopy/preMALDI_FLR/Composite.png',
save_p=MF + 'Analysis/ili/FLUO_crop_bin1x1.png',
window=0)
spaceM.ImageFileManipulation.manipulations.crop2coords(
MF + 'Analysis/Fiducials/transformedMarks.npy',
MF + 'Analysis/StitchedMicroscopy/preMALDI_FLR/Composite.png',
MF + 'Analysis/ili/FLUO_crop_bin1x1_window100.png',
window=100)
gc.collect()
nbin = spaceM.ImageFileManipulation.FIJIcalls.imbin4ili(MF + 'Analysis/ili/FLUO_crop_bin1x1.png', maxsize=50e6)
spaceM.WriteILIinput.annotationSM2CSV(
MFA=MF + 'Analysis/',
MFI=MF + 'Input/',
fdr=ili_fdr,
nbin=nbin,
radius=20,
tf_obj=tf_obj,
db='HMDB-v4')
def cellSegmentation(MF,
merge_colors,
merge_filenames,
prepCP_fun):
if not os.path.exists(MF + 'Analysis/CellProfilerAnalysis/'):
os.makedirs(MF + 'Analysis/CellProfilerAnalysis/')
CP_window = 100
spaceM.ImageFileManipulation.manipulations.crop2coords4CP(
MF + 'Analysis/Fiducials/transformedMarks.npy',
MF + 'Analysis/StitchedMicroscopy/preMALDI_FLR/',
MF + 'Analysis/CellProfilerAnalysis/',
window=CP_window)
spaceM.ImageFileManipulation.manipulations.crop2coords(
MF + 'Analysis/Fiducials/transformedMarks.npy',
MF + 'Analysis/StitchedMicroscopy/preMALDI_FLR/Composite.png',
MF + 'Analysis/CellProfilerAnalysis/Composite_window100_adjusted.png',
window=CP_window)
gc.collect()
prepCP_fun(MF)
print('Start CellProfiler Anlalysis')
cp_p = getPath('CellProfiler path')
cppipe_p = getPath('CellProfiler pipeline path')
spaceM.scAnalysis.Segmentation.callCP(MF + 'Analysis/', cp_p, cppipe_p)
print('Finished CellProfiler Anlalysis')
CP_window = 100
spaceM.scAnalysis.Segmentation.cellOutlines_fast(MF + 'Analysis/CellProfilerAnalysis/Composite_window100_adjusted.png',
CP_window,
MF + 'Analysis/CellProfilerAnalysis/Labelled_cells.tiff',
MF + 'Analysis/CellProfilerAnalysis/Contour_cells_adjusted.png')
def spatioMolecularMatrix(MF, tf_obj, CDs=[2], fetch_ann = 'online', filter = 'correlation', tol_fact = -0.2, hdf5_path='dummy'):
if not os.path.exists(MF + 'Analysis/scAnalysis/'):
os.makedirs(MF + 'Analysis/scAnalysis/')
# spaceM.scAnalysis.scAnalysis.defMORPHfeatures(MF)
# fetch_ann = 'online' # either 'online' or 'offline'
# either 'mean' or 'correlation'
spaceM.scAnalysis.scAnalysis_refactored.defMOLfeatures(
MF,
tf_obj=tf_obj,
CDs=CDs,
norm_method='weighted_mean_sampling_area_MarkCell_overlap_ratio_sampling_area',
fetch_ann=fetch_ann, tol_fact=tol_fact, filter=filter, hdf5_path=hdf5_path)
spaceM.scAnalysis.scAnalysis_refactored.mergeMORPHnMOL(
MF,
CDs=CDs,
fetch_ann=fetch_ann,
tol_fact=tol_fact,
filter=filter)