-
Notifications
You must be signed in to change notification settings - Fork 2
/
cell_metrics.py
186 lines (155 loc) · 8.52 KB
/
cell_metrics.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
""" Jython script to perform cell metric quantification in Fiji """
from ij import IJ, ImagePlus
from ij.plugin import ImageCalculator
import time
# Open file with parameters for image analysis
with open("pipeline_parameters.txt", 'r') as anda_parameters:
analysis_read = anda_parameters.read().splitlines()
PATH = str(analysis_read[0])
OUTLINES = str(analysis_read[4]) # Whether or not to save motif outlines
CELL_LINE = str(analysis_read[2]) # Name of cell line
WATERSHED_CHOICE = str(analysis_read[3])
# Open file with image file names
with open("file_names.txt", 'r') as file_names:
input_files = file_names.read().splitlines()
# Open file with parameters for analyzing cell line
with open("{}_parameters.txt".format(CELL_LINE), 'r') as cell_type:
cell_read = cell_type.readlines()
cell_line_params = [str(i.rstrip()) for i in cell_read] # Cell parameters list
##### Cell metric parameters
min_cell_size = int(cell_line_params[1])
max_cell_size = int(cell_line_params[2])
min_cell_circ = float(cell_line_params[3])
max_cell_circ = float(cell_line_params[4])
min_neurite_size = int(cell_line_params[5])
max_neurite_size = int(cell_line_params[6])
min_neurite_circ = float(cell_line_params[7])
max_neurite_circ = float(cell_line_params[8])
CELL_THRESHOLD = str(cell_line_params[9])
NEURITE_THRESHOLD = str(cell_line_params[10])
# Thresholding cells and neurites
if CELL_THRESHOLD == "none":
CELL_THRESHOLD = "Default" # Default thresholding method in ImageJ
if NEURITE_THRESHOLD == "none":
NEURITE_THRESHOLD = "Default" # Default thresholding method in ImageJ
IJ.run("Set Measurements...", "area fit shape display add redirect=None decimal=3")
image_calc = ImageCalculator() # ImageJ plugin
def cell_analysis(file_):
"""Quantify cell body count"""
IJ.open("{}/{}".format(PATH, file_))
print(file_)
imp = IJ.getImage()
IJ.run(imp, "Invert", "")
IJ.run("8-bit")
IJ.run("Auto Threshold", "method={}".format(CELL_THRESHOLD))
if WATERSHED_CHOICE == "apply_watershed":
IJ.run("Watershed")
IJ.run("Analyze Particles...", "size={}-{} pixel circularity={}-{} show=Nothing display summarize".format(min_cell_size, max_cell_size, min_cell_circ, max_cell_circ))
else:
IJ.run("Analyze Particles...", "size={}-{} pixel circularity={}-{} show=Nothing display summarize".format(min_cell_size, max_cell_size, min_cell_circ, max_cell_circ))
IJ.saveAs("Results", "{}_results_cells/{}.csv".format(PATH, file_))
# IJ.run("Clear Results")
if OUTLINES == "save_outlines":
IJ.run("Analyze Particles...", "size={}-{} pixel circularity={}-{} show=Overlay".format(min_cell_size, max_cell_size, min_cell_circ, max_cell_circ))
overlay_ = ImagePlus.getOverlay(imp)
img_width = imp.getWidth()
img_height = imp.getHeight()
IJ.newImage("blank_c", "RGB white", img_width, img_height, 1)
imp2 = IJ.getImage().setOverlay(overlay_)
imp2 = IJ.getImage()
imp3 = imp2.flatten()
IJ.run(imp3, "8-bit", "")
IJ.saveAs(imp3, "Tiff", "{}_outlines_cells/{}".format(PATH, file_))
def neurite_analysis(file_):
"""Quantify neurite lengths"""
IJ.open("{}/{}".format(PATH, file_))
# imp = IJ.getImage()
IJ.run("8-bit")
IJ.run("Auto Threshold", "method={}".format(NEURITE_THRESHOLD))
if WATERSHED_CHOICE == "apply_watershed":
IJ.run("Watershed")
IJ.run("Analyze Particles...", "size={}-{} pixel circularity={}-{} show=Nothing display summarize".format(min_cell_size, max_cell_size, min_cell_circ, max_cell_circ))
else:
IJ.run("Analyze Particles...", "size={}-{} pixel circularity={}-{} show=Nothing display summarize".format(min_cell_size, max_cell_size, min_cell_circ, max_cell_circ))
IJ.run("Analyze Particles...", "size={}-{} pixel circularity={}-{} show=Nothing display summarize".format(min_neurite_size, max_neurite_size, min_neurite_circ, max_neurite_circ))
IJ.saveAs("Results", "{}_results_neurites/{}.csv".format(PATH, file_))
# IJ.run("Clear Results")
if OUTLINES == "save_outlines":
IJ.run("Analyze Particles...", "size={}-{} pixel circularity={}-{} show=Nothing display summarize".format(min_cell_size, max_cell_size, min_cell_circ, max_cell_circ))
overlay_ = ImagePlus.getOverlay(imp)
img_width = imp.getWidth()
img_height = imp.getHeight()
IJ.newImage("blank_n", "RGB white", img_width, img_height, 1)
imp2 = IJ.getImage().setOverlay(overlay_)
imp2 = IJ.getImage()
imp3 = imp2.flatten()
IJ.run(imp3, "8-bit", "")
IJ.saveAs(imp3, "Tiff", "{}_outlines_neurites/{}".format(PATH, file_))
def attachment_analysis(file_):
"""Quantify neurite attachment points"""
IJ.open("{}/{}".format(PATH, file_))
imp = IJ.getImage()
imp_c = imp # Cell bodies
imp_n = imp # Neurites
# imp_c = IJ.getImage() # Cell bodies
# imp_n = IJ.getImage() # Neurites
IJ.run(imp_c, "8-bit", "")
IJ.run(imp_n, "8-bit", "")
# Threshold highlighting neurites
IJ.run(imp_c, "Auto Threshold", "method={}".format(NEURITE_THRESHOLD))
IJ.run(imp_n, "Auto Threshold", "method={}".format(NEURITE_THRESHOLD))
if WATERSHED_CHOICE == "apply_watershed":
IJ.run(imp_c, "Watershed", "") # Watershed segmentation
IJ.run(imp_c, "Analyze Particles...", "size={}-{} pixel circularity={}-{} show=Overlay".format(min_cell_size, max_cell_size, min_cell_circ, max_cell_circ)) # Use cell body parameters to extract overlay
overlay_c = ImagePlus.getOverlay(imp_c) # Get cell body overlay
IJ.run(imp_n, "Watershed", "") # Watershed segmentation
IJ.run(imp_n, "Analyze Particles...", "size={}-{} pixel circularity={}-{} show=Overlay".format(min_neurite_size, max_neurite_size, min_neurite_circ, max_neurite_circ)) # Use neurite parameters to extract overlay
overlay_n = ImagePlus.getOverlay(imp_n) # Get neurite overlay
else:
IJ.run(imp_c, "Analyze Particles...", "size={}-{} pixel circularity={}-{} show=Overlay".format(min_cell_size, max_cell_size, min_cell_circ, max_cell_circ)) # Use cell body parameters to extract overlay
overlay_c = ImagePlus.getOverlay(imp_c) # Get cell body overlay
IJ.run(imp_n, "Analyze Particles...", "size={}-{} pixel circularity={}-{} show=Overlay".format(min_neurite_size, max_neurite_size, min_neurite_circ, max_neurite_circ)) # Use neurite parameters to extract overlay
overlay_n = ImagePlus.getOverlay(imp_n) # Get neurite overlay
# Blank image for pasting the overlay onto
img_width = imp_c.getWidth()
img_height = imp_c.getHeight()
IJ.newImage("{}_blank_c".format(file_), "RGB white", img_width, img_height, 1)
imp_c2 = IJ.getImage().setOverlay(overlay_c)
imp_c2 = IJ.getImage()
imp_c3 = imp_c2.flatten()
IJ.run(imp_c3, "8-bit", "")
IJ.run(imp_c3, "Make Binary", "")
IJ.saveAs(imp_c3, "Tiff", "{}/imp_c3.tif".format(PATH))
# Blank image for pasting the overlay onto
img_width = imp_c.getWidth()
img_height = imp_c.getHeight()
IJ.newImage("{}_blank_n".format(file_), "RGB white", img_width, img_height, 1)
imp_n2 = IJ.getImage().setOverlay(overlay_n)
imp_n2 = IJ.getImage()
imp_n3 = imp_n2.flatten()
IJ.run(imp_n3, "8-bit", "")
IJ.run(imp_n3, "Make Binary", "")
IJ.saveAs(imp_n3, "Tiff", "{}/imp_n3.tif".format(PATH))
IJ.run(imp_n3, "Find Edges", "") # Sobel edge detector
# Get the overlap between cell bodies and neurites - the attachment points
imp_res = image_calc.run("Multiply create", imp_n3, imp_c3)
IJ.saveAs(imp_res, "Tiff", "{}/{}_imp_res.tif".format(PATH, file_))
IJ.run(imp_res, "Analyze Particles...", "size=0-100 pixel circularity=0.00-1.00 show=Nothing display summarize")
IJ.saveAs("Results", "{}_results_attachments/{}.csv".format(PATH, file_))
IJ.run("Clear Results")
if OUTLINES == "save_outlines":
IJ.run("Analyze Particles...", "size={}-{} pixel circularity={}-{} show=Nothing display summarize".format(min_cell_size, max_cell_size, min_cell_circ, max_cell_circ))
overlay_ = ImagePlus.getOverlay(imp_res)
img_width = imp_c.getWidth()
img_height = imp_c.getHeight()
IJ.newImage("blank_b", "RGB white", img_width, img_height, 1)
imp2 = IJ.getImage().setOverlay(overlay_)
imp2 = IJ.getImage()
imp3 = imp2.flatten()
IJ.run(imp3, "8-bit", "")
IJ.saveAs(imp3, "Tiff", "{}_outlines_attachments/{}".format(PATH, file_))
# Perform image analysis of 3 metrics at a time
for file_name in input_files:
cell_analysis(file_name)
neurite_analysis(file_name)
attachment_analysis(file_name)