-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompileSWC.py
171 lines (159 loc) · 6.68 KB
/
compileSWC.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
import os
import sys
minRadius = 1
synapseType = 6
synapseRadius = 1
class Circuit:
def __init__(self, pointID, type, X, Y, Z, radius, parent):
self.pointID = pointID
self.type = type
self.X = str(float(X) / 100.0)
self.Y = str(float(Y) / 100.0)
self.Z = str(float(Z) / 100.0)
self.parent = str(parent).strip(' \t\n\r')
if (int(radius)/100.0) < int(minRadius):
self.radius = str(minRadius)
else:
self.radius = str(int(radius)/100.0)
self.index = -1
def __repr__(self):
return repr((self.pointID, self.type, self.X, self.Y, self.Z, self.radius, self.parent))
def setindex(self, index):
self.index = index
def getindex(self):
return self.index
def ispoint(self, pointid):
return str(pointid).strip(' \t\n\r') == str(self.pointID).strip(' \t\n\r')
class Connection:
def __init__(self, parent, pointID, prepost, X, Y, Z):
self.pointID = pointID
self.prepost = prepost
self.X = str(float(X) / 100.0)
self.Y = str(float(Y) / 100.0)
self.Z = str(float(str(Z).strip(' \t\n\r')) / 100.0)
self.parent = parent
self.type = "6"
self.radius = minRadius
def __repr__(self):
return repr((self.pointID, self.type, self.X, self.Y, self.Z, self.radius, self.parent))
if __name__ == "__main__":
if (len(sys.argv) < 3):
print 'Error: missing arguments!'
print 'e.g. python compileSWC.py circuit.swc connections.csv outfile.swc'
else:
fnameCir = str(sys.argv[1])
outputList = []
pointList = []
conList = []
if os.path.isfile(fnameCir):
print 'Loading neuron tracing (circuit): %s...' % fnameCir
with open(fnameCir) as f:
content = f.readlines()
for line in content:
if line.startswith("#"):
outputList.append(line.strip('\n\r'))
else:
item = line.strip('\n\r').split(' ')
pointList.append(Circuit(item[0],item[1],item[2],item[3],item[4],item[5],item[6]))
conZero = list(outputList)
conOne = list(outputList)
fnameCon = str(sys.argv[2])
if os.path.isfile(fnameCon):
print 'Loading neuron synapses (connections): %s...' % fnameCon
with open(fnameCon) as f:
content = f.readlines()
for line in content:
if "x" in line:
print line
else:
item = line.strip(' \t\n\r').split(',')
conList.append(Connection(item[0], item[1], item[2], item[3], item[4], item[5]))
print 'Sorting data...'
sorted(pointList, key=lambda circuit: circuit.pointID)
sorted(conList, key=lambda connection: connection.pointID)
cirCount = 0
print 'Re-indexing data...'
for point in pointList:
cirCount += 1
point.setindex(cirCount)
print 'Adding circuit data...'
for point in pointList:
row = []
row.append(str(point.getindex()))
# used given type unless soma
if int(point.parent) > -1:
row.append(str(point.type))
else:
row.append("1")
row.append(str(point.X))
row.append(str(point.Y))
row.append(str(point.Z))
row.append(str(point.radius))
if int(point.parent) > 0:
for p in pointList:
if p.ispoint(point.parent):
row.append(str(p.getindex()))
break
else:
row.append(str(point.parent))
outputList.append(" ".join(row))
fnameOut = str(sys.argv[3])
print 'Saving data to %s' % fnameOut
with file(fnameOut, 'w') as f:
for item in outputList:
f.write("%s\r\n" % item)
print 'Creating synapse points...'
countZero = 0
countOne = 0
for con in conList:
row = []
r = []
if con.prepost == "0":
countZero += 1
row.append(str(countZero))
else:
countOne += 1
row.append(str(countOne))
row.append(str(synapseType))
row.append(str(con.X))
row.append(str(con.Y))
row.append(str(con.Z))
row.append(str(synapseRadius))
for p in pointList:
if p.ispoint(con.parent):
if con.prepost == "0":
countZero += 1
row.append(str(countZero))
r.append(str(countZero))
else:
countOne += 1
row.append(str(countOne))
r.append(str(countOne))
r.append("5")
r.append(str(p.X))
r.append(str(p.Y))
r.append(str(p.Z))
r.append(str(synapseRadius))
r.append("-1")
break
if con.prepost == "0":
conZero.append(" ".join(row))
conZero.append(" ".join(r))
else:
conOne.append(" ".join(row))
conOne.append(" ".join(r))
fnameOutZero = fnameOut.replace('.swc', '_S0.swc')
print 'Saving data to %s' % fnameOutZero
with file(fnameOutZero, 'w') as f:
for item in conZero:
f.write("%s\r\n" % item)
fnameOutOne = fnameOut.replace('.swc', '_S1.swc')
print 'Saving data to %s' % fnameOutOne
with file(fnameOutOne, 'w') as f:
for item in conOne:
f.write("%s\r\n" % item)
print 'Done.'
else:
print 'File Missing: %s !!!' % (fnameCon)
else:
print 'File Missing: %s !!!' % (fnameCir)