-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix3Dviewer.py
146 lines (114 loc) · 4.8 KB
/
fix3Dviewer.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
# Script will correct .hoc file from neuromorpho.org.
# In order to make the correction, same data is needed from
# Neuron's import3D tool.
# CNS-GROUP, Tampere University
def fix_commas(IMPORT3D_FILE, _3DVIEWER_FILE):
"""
This will correct commas, change "user7" to "dendrite"
and will seek "OrginalDendrite" value. Returns False if
there was a problem opening the file.
:return orgdend = OrginalDendrite
"""
k = 0
a = 0
orgdend = "Error"
try:
with open(IMPORT3D_FILE, 'r+') as i3file:
lines_i = i3file.readlines()
with open(_3DVIEWER_FILE, 'r+') as _3dfile:
lines_view = _3dfile.readlines()
_3dfile.seek(0)
for line_v in lines_view:
# fix commas
if "pt3dadd" in line_v:
k += 1
for line_i in lines_i:
if "pt3dadd" in line_i:
a = a + 1
if k == a and "]" not in line_i: # when line is normal
line_i = line_i.strip('\t')
line_i = " " + line_i
_3dfile.write(line_i)
a = 0
break
elif k == a and "]" in line_i: # line is unnormal
parts = line_i.split('\t')
line_i = " " + parts[1]
_3dfile.write(line_i)
a = 0
break
elif "create user7" in line_v:
b = line_v.split("[")
orgdend = str(b[1]).replace("]", "")
orgdend = orgdend.replace("}", "")
elif "] connect" in line_v:
list_ = line_v.split(" ")
if "," in list_[3]:
list_[3] = list_[3].replace(",", ".")
new_line = " ".join(list_)
new_line = new_line.replace("user7", "dendrite")
_3dfile.write(new_line)
else:
line_v = line_v.replace("user7", "dendrite")
_3dfile.write(line_v)
elif "user7" in line_v:
new_line = line_v.replace("user7", "dendrite")
_3dfile.write(new_line)
else:
_3dfile.write(line_v)
_3dfile.truncate()
return str(orgdend)
except:
print("There was a problem opening import3D file.")
return False
def delete_curly_braces(_3DVIEWER_FILE):
""" Deletes unneeded braces. """
with open(_3DVIEWER_FILE, 'r+') as _3dfile:
lines_view = _3dfile.readlines()
_3dfile.seek(0)
for line_v in lines_view:
if "create" in line_v or "access" in line_v:
new_line = line_v.replace("{", "")
new_line = new_line.replace("}", "")
_3dfile.write(new_line)
else:
_3dfile.write(line_v)
_3dfile.truncate()
def insert(orgdend, _3DVIEWER_FILE):
""" Adds needed lines to 3Dviewer code. """
with open(_3DVIEWER_FILE, 'r+') as _3dfile:
lines = _3dfile.readlines()
lines.insert(5, "OriginalDendrite=" + str(orgdend) + "\n")
lines.insert(6, "NumberDendrites=OriginalDendrite+2*(OriginalDendrite-1)" + "\n")
lines.insert(7, "SeedNumber=OriginalDendrite-1" + "\n")
lines.insert(8, "\n")
lines.insert(10, "create dendrite[NumberDendrites]" + "\n")
_3dfile.seek(0)
_3dfile.truncate()
_3dfile.writelines(lines)
print("Corrections was made to", _3DVIEWER_FILE, "file.")
def check_file(_3DVIEWER_FILE):
""" This checks if file has already been fixed with this program. """
try:
with open(_3DVIEWER_FILE, 'r') as _3dfile:
lines = _3dfile.readlines()
for line in lines:
if "OriginalDendrite=" in line:
print(_3DVIEWER_FILE, "is not orginal file from neuromorpho 3Dviewer.")
return False
return True
except:
print("There was a problem opening 3Dviewer file.")
return False
def main():
""" Correct file from 3D viewer. """
IMPORT3D_FILE = input("Import3D file: ")
_3DVIEWER_FILE = input("3Dviewer file: ")
if check_file(_3DVIEWER_FILE):
orgdend = fix_commas(IMPORT3D_FILE, _3DVIEWER_FILE)
if not orgdend:
return
else:
delete_curly_braces(_3DVIEWER_FILE)
insert(orgdend, _3DVIEWER_FILE)
main()