/
re-run_blebbing_analysis.py
152 lines (128 loc) · 5.85 KB
/
re-run_blebbing_analysis.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
# Code to re-run membrane blebbing analysis on previous data without going through manual edge drawing again
# Motivation: calculation methods have changed slightly as version number has increased, leading to
# difficulties in comparing analysed datasets. To avoid having to redraw membranes, it's useful
# to have a tool to re-run the analysis, loading previously determined edges and skipping
# edge-determination steps
# D.J. Kelly, 2019-01-04, douglas.kelly@riken.jp
# imports
import os, sys, math
from ij import IJ, ImageStack
from ij.io import FileSaver
from ij.gui import WaitForUserDialog
from ij.plugin import ChannelSplitter, Duplicator, ZProjector
from ij import Prefs
from loci.plugins import BF as bf
release = False;
if not release:
script_path = os.path.dirname(os.path.realpath(__file__));
else:
script_path = os.getcwd();
if "Fiji.app" in script_path:
ss = script_path.split("Fiji.app");
final_folder = "blebbing analysis";
script_path = os.path.join(ss[0], "Fiji.app", "plugins", "Scripts", "Plugins", final_folder);
sys.path.insert(0, os.path.join(script_path, 'modules'));
sys.path.insert(0, os.path.join(script_path, 'classes'));
import membraneBlebbingFileio as mbio;
import membraneBlebbingUi as mbui;
import membraneBlebbingEngine as mb;
import membraneBlebbingFigures as mbfig;
import membraneBlebbingHighLevelFunctions as mbfs;
from Parameters import Parameters
from CalculatedObjects import CalculatedObjects
def main():
# ensure consistent preference settings
Prefs.blackBackground = False;
# get locations for previous and new outputs
params = Parameters();
params.loadLastParams();
output_folder_old, output_folder = mbio.rerun_location_chooser(params.input_image_path);
params.loadParametersFromJson(os.path.join(output_folder_old, 'parameters used.json'));
params.setOutputPath(output_folder);
# present user with the familiar setup UI, with options that don't make sense/aren't yet implemented disabled
params = mbui.analysis_parameters_gui(rerun_analysis=True, params=params);
# get original image file (for overlays etc.)
if not os.path.isfile(params.input_image_path):
mbui.warning_dialog(["The original data can't be found at the location specified in saved parameters. ",
"(Possibly something as simple as a change in network drive mapping has occurred)",
"Please specify the location of the original image file..."]);
params.setInputImagePath(mbio.input_file_location_chooser(params.output_path));
import_opts, params = mbui.choose_series(params.input_image_path, params);
imps = bf.openImagePlus(import_opts);
imp = imps[0];
if imp.getNSlices() > 1:
mbui.warning_dialog(["More than one Z plane detected.",
"I will do a maximum projection before proceeding",
"Continue?"]);
imp = ZProjector.run(imp,"max all");
params = mbio.get_metadata(params);
params.setCurvatureLengthUm(round(params.curvature_length_um / params.pixel_physical_size) * params.pixel_physical_size);
params.persistParameters();
IJ.run(imp, "Set Scale...", "distance=0 known=0 pixel=1 unit=pixel");
imp.show();
if imp.getNChannels() > 1:
imp.setPosition(params.membrane_channel_number, 1, 1);
mbui.autoset_zoom(imp);
IJ.run("Enhance Contrast", "saturated=0.35");
# prompt user to select ROI
original_imp = Duplicator().run(imp);
_, crop_params = mbui.crop_to_ROI(imp, params);
imp.show();
if crop_params is not None:
params.perform_spatial_crop = True;
mbui.autoset_zoom(imp);
imp.updateAndDraw();
review_crop = mb.check_cropping(output_folder_old, params);
keep_crop = not review_crop;
if review_crop:
keep_crop = mbui.crop_review();
if not keep_crop:
imp.changes = False;
imp.close();
imp = original_imp;
else:
original_imp.close();
else:
original_imp.close();
# prompt user to do time cropping
imp, start_end_tuple = mbui.time_crop(imp, params);
params.setTimeCropStartEnd(start_end_tuple);
# import edges
membrane_edges = mbio.load_qcd_edges2(os.path.join(output_folder_old, "user_defined_edges.zip"));
mbio.save_qcd_edges2(membrane_edges, params.output_path);
calculated_objects = CalculatedObjects();
calculated_objects.membrane_edges = membrane_edges;
if params.time_crop_start_end[0] is not None:
calculated_objects.timelist = [idx * params.frame_interval for idx in range(params.time_crop_start_end[0], params.time_crop_start_end[1]+1)]
else:
calculated_objects.timelist = [idx * params.frame_interval for idx in range(imp.getNFrames())];
split_channels = mbfs.split_image_plus(imp, params)
membrane_channel_imp = split_channels[0];
actin_channel_imp = split_channels[1];
segmentation_channel_imp = None;
if params.photobleaching_correction:
if os.path.isfile(os.path.join(output_folder_old, 'binary_membrane_stack.tif')):
segmentation_binary_path = os.path.join(output_folder_old, 'binary_membrane_stack.tif');
segmentation_channel_imp = IJ.openImage(segmentation_binary_path);
else:
segmentation_channel_imp = split_channels[2];
segmentation_channel_imp = mb.make_and_clean_binary(segmentation_channel_imp, params.threshold_method)
split_channels[2] = segmentation_channel_imp;
calculated_objects = mbfs.calculate_outputs(params,
calculated_objects,
split_channels);
# output colormapped images and kymographs
fig_imp_list = mbfs.generate_and_save_figures(imp, calculated_objects, params, membrane_channel_imp, segmentation_channel_imp);
mbfs.save_csvs(calculated_objects, params);
params.saveParametersToJson(os.path.join(params.output_path, "parameters used.json"));
imp.changes = False;
IJ.setTool("zoom");
if params.close_on_completion:
for fig_imp in fig_imp_list:
fig_imp.close();
imp.close();
return;
# It's best practice to create a function that contains the code that is executed when running the script.
# This enables us to stop the script by just calling return.
if __name__ in ['__builtin__','__main__']:
main()