forked from SainsburyWellcomeCentre/lasagna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportedGoggleTree2LasagnaLines.py
executable file
·155 lines (111 loc) · 3.98 KB
/
exportedGoggleTree2LasagnaLines.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
#!/usr/bin/python
"""
This function accepts a csv file produced by aratools.utils.exportMaSIVPoints2Lasagna.m
Each row of the csv file produced by that function is in this format:
nodeId,parentID,z position,x position,y position
This file imports this file and turns it into a format suitable
for plotting in lasagna. i.e. into a line series format:
line series #, z, x, y
Where each line series is one segment from the tree. This is
produced using tree.findSegments, which returns all unique segments
such that only nodes at the end of each segment are duplicated.
Processed text is dumped to standard output by default unless
the user specifies otherwise with -q
Example:
1. Plot and don't dump data to screen
exportedGoggleTree2LasagnaLines.py -pqf ./ingredients/exampleTreeDump.csv
2. Dump data to text file and don't plot
exportedGoggleTree2LasagnaLines.py -f ./ingredients/exampleTreeDump.csv > /tmp/dumpedTree.csv
"""
import sys
import os
from tree import Tree, importData
#Parse command-line input arguments
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-f", help="File name to load")
parser.add_argument("-p", help="Optionally plot tree", action="store_true")
parser.add_argument("-q", help="Quiet - do not dump processed tree to standard output", action="store_true")
args = parser.parse_args()
fname = args.f
doPlot = args.p
quiet = args.q
if fname is None:
print("Please supply a file name to convert. e.g.:\nexportedGoggleTree2LasagnaLines.py -f myFile.csv\n")
sys.exit(0)
#Get the data out of the tree
if os.path.exists(fname) == False:
print('Can not find ' + fname)
sys.exit(0)
dataTree = importData(fname,headerLine=['id','parent','z','x','y'])
#Get the unique segments of each tree
paths=[]
for thisSegment in dataTree.findSegments():
paths.append(thisSegment)
def dataFromPath(tree,path):
"""
Get the data from the tree given a path.
"""
x=[]
y=[]
z=[]
for thisNode in path:
if thisNode==0:
continue
z.append(tree.nodes[thisNode].data['z'])
x.append(tree.nodes[thisNode].data['x'])
y.append(tree.nodes[thisNode].data['y'])
return (z,x,y)
#Show paths in standard output
if not quiet:
ii=0
for thisPath in paths:
data = dataFromPath(dataTree,thisPath)
for jj in range(len(data[0])):
print("%d,%d,%d,%d" % (ii,data[0][jj],data[1][jj],data[2][jj]))
ii += 1
#-------------------------------------------------
#Optionally plot
if not doPlot:
sys.exit(0)
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import numpy as np
#Set up the window
app = QtGui.QApplication([])
mw = QtGui.QMainWindow()
mw.resize(800,800)
view = pg.GraphicsLayoutWidget() # GraphicsView with GraphicsLayout inserted by default
mw.setCentralWidget(view)
mw.show()
mw.setWindowTitle('Neurite Tree')
#view 1
w1 = view.addPlot()
pathItem = []
for thisPath in paths:
pathItem.append( pg.PlotDataItem(size=10, pen='w', symbol='o', symbolSize=2, brush=pg.mkBrush(255, 255, 255, 120)) )
data = dataFromPath(dataTree,thisPath);
pathItem[-1].setData(x=data[0], y=data[1])
w1.addItem(pathItem[-1])
#view 2
w2 = view.addPlot()
pathItem = []
for thisPath in paths:
pathItem.append( pg.PlotDataItem(size=10, pen='w', symbol='o', symbolSize=2, brush=pg.mkBrush(255, 255, 255, 120)) )
data = dataFromPath(dataTree,thisPath)
pathItem[-1].setData(x=data[0], y=data[2])
w2.addItem(pathItem[-1])
#view 3
view.nextRow()
w3 = view.addPlot()
pathItem = []
for thisPath in paths:
pathItem.append( pg.PlotDataItem(size=10, pen='w', symbol='o', symbolSize=2, brush=pg.mkBrush(255, 255, 255, 120)) )
data = dataFromPath(dataTree,thisPath)
pathItem[-1].setData(x=data[1], y=data[2])
w3.addItem(pathItem[-1])
## Start Qt event loop unless running in interactive mode.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()