-
Notifications
You must be signed in to change notification settings - Fork 0
/
P10.py
245 lines (220 loc) · 9.4 KB
/
P10.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
# Album Shen
# Deans Lab
# 2013 June
#
# Analysis of Dendrite/Axon Exit Position
#
# Data needed:
# Scaling information used to construct neurites model
# Scaling information of the image (pixel to um conversion)
# Lowest and highest slice in which cell body appears
#
# Parameters:
# ['PA.py', input, x_d, y_d, z_d, x_s, y_s, z_s, s_l, s_h]
# input: .swc file to be read;
# results will be output to the filename plus the extension '_output.txt'
# scale_default: refers to the scaling used when plotting the neurites in NeuronStudio
# scale_actual: refers to the actual scaling of the images
# s_l: lowest slice in which the cell body appears
# s_h: highest slice in which the cell body appears
#
# Assumptions about neurites file:
# Soma is indexed with integer label n = '1', and neuronal type T = '1')
# Axon is labeled with neuronal type T = '2'
# All dendritic ends are labeled with neuronal type T = '6'
# There is a single point of origin (one soma with T = '1')
import sys, string, os
from math import sqrt, pow, ceil
class PositionAnalysis:
# initialize
def __init__(self, params):
global input, scale_default, scale_actual, slice_l, slice_h
global output, output_name, neurites, ends, branches, dendrites, axons, axon_end, endpoints, end_um, end_dist
global center, coor_swc, coor_image, coor_relative, coor_microns, coor_surface, num_pos, positions, width
input = open(params[1], 'rb')
output = open(params[1].replace('.swc', '_output_10.txt'), 'wb')
output_name = (params[1].replace('.swc', '_output_10.txt'))
scale_default = map(float, params[2:5])
scale_actual = map(float, params[5:8])
slice_l = float(params[8])
slice_h = float(params[9])
neurites = []
ends = []
branches = []
dendrites = []
axons = []
center = []
coor_swc = []
coor_image = []
coor_relative = []
coor_microns = []
coor_surface = []
positions = []
endpoints = []
end_dist = []
num_pos = 10
width = 12
# counts dendrites and axons for each branch
# performs position analysis
# outputs results to output file
def work(self):
# neurites
# n T x y z R P
for line in input.readlines():
if line.startswith('#'):
continue
neurites.append(line.split(" "))
# correct for index at 0
neurites[0] = [0] * 7
# find ends
# label indices of ends
for n in neurites:
if n[1] == '6':
ends.append(n[0])
if n[1] == '2':
axon_end = n[0]
# find primary dendrite branches
for n in neurites:
if n[6] == '1\r\n':
branches.append(n[0])
dendrites = [0] * len(branches)
axons = [0] * len(branches)
# counts primary dendrites
for e in ends:
prev = neurites[int(e)][6]
while prev != '1\r\n':
last = prev
prev = self.traceback(int(prev))
i = branches.index(str(int(last)))
dendrites[i] += 1
# assign axon value or throw exception if type unspecified
while True:
try:
prev = neurites[int(axon_end)][6]
break
except UnboundLocalError:
print "The axon end could not be found. Please modify .swc file."
print "Locate the neurite point of the axon end and set the neurite type (second element) to 2."
sys.exit(1)
while prev != '1\r\n':
last = prev
prev = self.traceback(int(prev))
i = branches.index(str(int(last)))
axons[i] += 1
# extracting cell body coordinate from neurites file
center = map(float, neurites[1][2:5])
# print '\nCell body (um):\t\t' + str(center)
midslice = float(slice_h + slice_l) / 2
z_radius = ((slice_h - slice_l) / 2) * scale_actual[2]
# cell body image coordinates
center[0] = center[0] / scale_default[0]
center[1] = center[1] / scale_default[1]
center[2] = center[2] / scale_default[2] + 1
# adjust z-center to midslice
center[2] = midslice
coor_swc = [0] * len(branches)
coor_image = [0] * len(branches)
coor_relative = [0] * len(branches)
coor_microns = [0] * len(branches)
coor_surface = [0] * len(branches)
positions = [0] * len(branches)
for i in xrange(len(branches)):
# .swc coordinates of each branch
coor_swc[i] = map(float, neurites[int(branches[i])][2:5])
# image coordinates of each branch
coor_image[i] = [coor_swc[i][0] / scale_default[0], coor_swc[i][1] / scale_default[1], coor_swc[i][2] / scale_default[2] + 1]
# coordinates relative to center
coor_relative[i] = [coor_image[i][j] - center[j] for j in xrange(3)]
# coordinates converted to microns
coor_microns[i] = [coor_relative[i][j] * scale_actual[j] for j in xrange(3)]
# coordinates adjusted to surface (point of origin)
multiplier = z_radius / sqrt(pow(coor_microns[i][0], 2) + pow(coor_microns[i][1], 2) + pow(coor_microns[i][2], 2))
coor_surface[i] = [coor_microns[i][j] * multiplier for j in xrange(3)]
# position analysis
positions[i] = ceil(coor_surface[i][2]/((float(slice_h - slice_l) / num_pos) * scale_actual[2]) + (num_pos / 2))
# corrects positions out of bound
for p in positions:
if p < 1:
p = 1
if p > num_pos:
p = num_pos
endpoints = [0] * len(ends)
end_um = [0] * len(ends)
end_dist = [0] * len(ends)
for i in xrange(len(ends)):
# .swc coordinates of each point
endpoints[i] = map(float, neurites[int(ends[i])][2:5])
# image coordinates of each point
endpoints[i] = [endpoints[i][0] / scale_default[0], endpoints[i][1] / scale_default[1], endpoints[i][2] / scale_default[2] + 1]
# coordinates relative to center in microns
end_um[i] = [((endpoints[i][j] - center[j]) * scale_actual[j]) for j in xrange(3)]
# distance from center
end_dist[i] = sqrt(pow(end_um[i][0], 2) + pow(end_um[i][1], 2) + pow(end_um[i][2], 2))
# completed output written to file
#
# output to copy to Excel
output.write('Copy to Excel: \n')
for i in xrange(len(branches)):
output.write(branches[i] + '\t' + str(dendrites[i]) + '\t' + str(axons[i]) + '\t' + str(int(positions[i])) + '\n')
# output for viewing purposes
#
# endpoint coordinates
output.write('\nEndpoint Coordinates '.ljust(6*width, '-'))
output.write('\nEndpoint'.ljust(width) + '\t\t' + 'x (px)'.ljust(width) + 'y (px)'.ljust(width) + 'z (sl)\t' + 'um from surface (xy | z - radius)'.ljust(width))
for i in xrange(len(ends)):
output.write(('\nEndpoint ' + ends[i] + ':').ljust(width) + '\t' + '\t'.join(map(str, endpoints[i])) + '\t' + str(end_dist[i] - (float(neurites[1][5])/scale_default[0]) * scale_actual[0]) + '\t' + str(end_dist[i] - z_radius))
# position analysis table
output.write('\n\n Branch #'.ljust(width) + 'Dendrites'.rjust(width) + 'Axons'.rjust(width) + 'Position\n'.rjust(width) + ''.ljust((4*width), '-') + '\n')
for i in xrange(len(branches)):
output.write(('Branch ' + branches[i]).ljust(width) + str(dendrites[i]).rjust(width) + str(axons[i]).rjust(width) + str(int(positions[i])).rjust(width) + '\n')
output.write('\nNumber of Ends:\t\t' + str(len(ends)))
output.write('\nNumber of Branches:\t' + str(len(branches)))
# information on cell body center and end points
output.write('\n\nCell body (px, px, sl):'.ljust(3*width) + str(center))
output.write('\nMidslice:'.ljust(3*width) + str(midslice))
output.write('\nPos height (slices):'.ljust(3*width) + str(float(slice_h - slice_l) / num_pos))
output.write('\nPos height (um):'.ljust(3*width) + str((float(slice_h - slice_l) / num_pos) * scale_actual[2]))
output.write('\nZ-radius (um):'.ljust(3*width) + str(z_radius))
# output tables of coordinates
toPrint = []
headers = []
headers.append('\n\n.swc Coordinates (pre-calculated um) '.ljust(6*width, '-'))
headers.append('\n\nImage Coordinates (px, px, slice) '.ljust(6*width, '-'))
headers.append('\n\nCoordinates Relative to Center (px, px, slice) '.ljust(5*width, '-'))
headers.append('\n\nCoordinates Relative to Center (um) '.ljust(6*width, '-'))
headers.append('\n\nSurface Coordinates (Points of Origin) (um) '.ljust(6*width, '-'))
tabl_coor = {}
tabl_coor[0] = coor_swc
tabl_coor[1] = coor_image
tabl_coor[2] = coor_relative
tabl_coor[3] = coor_microns
tabl_coor[4] = coor_surface
for j in xrange(0,5):
toPrint.append(headers[j])
for i in xrange(len(branches)):
toPrint.append(('\nBranch ' + branches[i] + ':').ljust(width) + '\t\t' + '\t\t'.join(map(str, tabl_coor[j][i])))
for line in toPrint:
output.write(line)
# open file for viewing in notepad++
# os.system("start notepad++ " + output_name)
# traces ends back to plotted point of origin
def traceback(self, n):
return neurites[n][6]
if __name__ == '__main__':
# read parameters
# ['PA.py', input, x_d, y_d, z_d, x_s, y_s, z_s, s_l, s_h]
# input: .swc file to be read; results will be output to the filename plus the extension '_output.txt'
# scale_default: refers to the scaling used when plotting the neurites in NeuronStudio
# scale_actual: refers to the actual scaling of the images
# s_l: lowest slice in which the cell body appears
# s_h: highest slice in which the cell body appears
# Running through command line
# ['PA.py', input, x_d, y_d, z_d, x_s, y_s, z_s, s_l, s_h]
# PA = PositionAnalysis(sys.argv)
# Running with manual input
# Sample run
# PA = PositionAnalysis(['P10.py', 'sample.swc', 0.142, 0.142, 0.5, 0.142, 0.142, 0.5, 15, 31])
# counts dendrites and axons for each branch
# performs position analysis
# outputs results to output file
PA.work()