-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatch_kymotrace_statistics.ijm
175 lines (145 loc) · 4.67 KB
/
Batch_kymotrace_statistics.ijm
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
#@ Float (label="Pixel size (nm)",description="pixSz") PIXSZ
#@ Float (label="Frame interval (s)",description="frTime") FRTIME
macro "Batch Kymo Trace Statistics" {
//Select directory for analysis (images can be in subfolders)
maindir = getDirectory("Choose the root Directory ");
maindirName = File.getName(maindir);
setBatchMode(true);
//Create a list of tiff files in maindir
String.resetBuffer; //needed as the buffer is used to store the Tiff list as a string in ListTiffFiles
TiffList = ListTiffFiles(maindir);
// Empties ROI manager
nROI = roiManager("count");
if (nROI!=0) {
roiManager("Deselect");
roiManager("Delete");
}
// Define threshold speed for static
staticThreshold = 0.02;
// Prepare the Results table
TableName = maindirName + "_tracks_global.csv";
title1 = "" + maindirName + "_results";
title2 = "[" + title1 + "]";
f = title2;
if (isOpen(title1))
print(f, "\\Clear");
else
run("New... ", "name=" + title2 + " type=Table");
Headings = "\\Headings:n\tROI#\tImage&ROI_Name\tDisplacement (nm)\tTime (s)\tVelocity (nm/s)\tThetaDeg\tThetaRad";
print(f, Headings);
roiNumber = 0;
resultsLine = 0;
// Measures all ROIs for all images in the list of Tiff that have a ROI set
Nimage = 0;
for (i=0; i<TiffList.length; i++) {
ImageFullPath = TiffList[i];
ImagePath = File.getParent(ImageFullPath);
ImageName = File.getName(ImageFullPath);
ImageNameCropped = substring(ImageName, 0, lastIndexOf(ImageName, ".tif"));
ROIpath = ImagePath + File.separator + ImageNameCropped + ".roi.zip";
// Open image i
open(ImageFullPath);
sourceID = getImageID();
selectImage(sourceID);
// Open ROI file for image i
if (File.exists(ROIpath)) {
roiManager("Open", ROIpath);
// Get scale and ROI number
// ImageName = getTitle();
//getVoxelSize(pX, pY, pZ, pUnit);
nROI = roiManager("count");
// Measure ROIs for image i
for (r = 0; r < nROI; r++) {
// Select ROI
roiManager("select", r);
// Get ROI name, properties and number
roiTitle = getInfo("selection.name");
ROIname = ImageNameCropped + "_" + roiTitle;
roiNumber = r + 1;
// get ROI coordinates
Roi.getCoordinates(xpoints, ypoints);
L = xpoints.length;
// Array.print(xpoints);
// Array.print(ypoints);
// Scaled coordinates
//xS = newArray(L);
//yS = newArray(L);
//for (p = 0; p < L; p++) {
//xS[p] = xpoints[p] * PIXSZ;
//yS[p] = ypoints[p] * FRTIME;
//}
// Coordinates differences
dx = newArray(L);
dy = newArray(L);
// Using index=0 to store global difference between extremities
dx[0] = xpoints[L-1] - xpoints[0];
dy[0] = ypoints[L-1] - ypoints[0];
// 1 to L for single-step differences
for (p = 1; p < L; p++) {
dx[p] = xpoints[p] - xpoints[p - 1];
dy[p] = ypoints[p] - ypoints[p - 1];
}
// Scaled differences (0 is global, 1-L for each step)
dxS = newArray(L);
dyS = newArray(L);
for (p = 0; p < L; p++) {
dxS[p] = dx[p] * PIXSZ;
dyS[p] = dy[p] * FRTIME;
}
// Speeds (O is global, 1-L for each step)
V = newArray(L);
for (p = 0; p < L; p++) {
V[p] = (dxS[p]) / (dyS[p]);
}
// Angle
ThetaDeg = newArray(L);
ThetaRad = newArray(L);
// Using index=0 to store global angle
ThetaDeg[0] = (180/PI)*atan2(dx[0], dy[0]);
ThetaRad[0] = atan2(dx[0], dy[0]);
// 1 to L for single-step differences
for (p = 1; p < L; p++) {
ThetaDeg[p] = (180/PI)*atan2(dx[p], dy[p]);
ThetaRad[p] = atan2(dx[p], dy[p]);
}
// Category (anterograde = +1, retrograde = -1, static = 0);
Cat = newArray(L);
for (p = 0; p < L; p++) {
if (V[p] > staticThreshold) Cat[p] = 1;
else if (V[p] < -staticThreshold) Cat[p] = -1;
else Cat[p] = 0;
}
for (k = 0; k < 1; k++) {
resultsLine++;
// Build the Results table line
ResultsLine = d2s(resultsLine, 0) + "\t" + roiNumber + "\t" + ROIname + "\t" + dxS[k] + "\t" + dyS[k] + "\t" + V[k] + "\t" + ThetaDeg[k] + "\t" + ThetaRad[k] ;
print(f, ResultsLine);
}
}
roiManager("Deselect");
roiManager("Delete");
print(ImageName);
Nimage = Nimage + 1;
}
selectImage(sourceID);
close();
}
print("N(image) = " + Nimage);
print("N(track) = " + resultsLine);
print("");
selectWindow(title1);
saveAs("Text", maindir + File.separator + TableName);
// run("Close");
setBatchMode(false);
}
function ListTiffFiles(dir) {
list = getFileList(dir);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], "tif") || endsWith(list[i], "tiff")) {
String.append(dir + list[i] + "\t");
}
}
str = String.buffer;
list2 = split(str, "\t");
return list2;
}