/
Lineage_analysis.py
267 lines (211 loc) · 6.76 KB
/
Lineage_analysis.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#uses pickle file from lineageTracking and makes graphs
############################################################################
############################################################################
from time import sleep
from time import clock
import math
import numpy as np
import numpy.random as rnd
import pdb
import string
import sys
import os
import pickle as pickle
from multiprocessing import Pool
import scipy.ndimage as ndimage
import scipy.misc as misc
import scipy.sparse
import cv2 as cv
import csv
############################################################################
############################################################################
if (True):
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 12
# change fonts so that PDF's do not export with outlined fonts (bad for editing)
mpl.rcParams['pdf.fonttype'] = 42
#fig = plt.figure(figsize=(12,8))
plt.ion()
plt.show()
############################################################################
############################################################################
#######################################################################
#######################################################################
# helper function for input
def text_input(DISPLAY):
str = ''
while (str == ''):
str = input(DISPLAY)
return str
# helper function for input
def bool_input(DISPLAY):
bValue = None
while (bValue is None):
str = input(DISPLAY)
if (str=='y' or str=='Y'):
bValue = True
elif (str=='n' or str=='N'):
bValue = False
return bValue
# helper function for input
def int_input(DISPLAY):
iValue = None
while (iValue is None):
str = input(DISPLAY)
try:
iValue = int(str)
except:
iValue = None
return iValue
# helper function for input
def float_input(DISPLAY):
iValue = None
while (iValue is None):
str = input(DISPLAY)
try:
iValue = float(str)
except:
iValue = None
return iValue
####################################################################
####################################################################
def getfilename(prompt):
ISFILE = False
while not ISFILE:
FILENAME = text_input(prompt)
ISFILE = os.path.isfile(FILENAME)
if not ISFILE:
print('cannot find file')
return FILENAME
def getdirname(prompt):
ISPATH = False
while not ISPATH:
PATHNAME = text_input(prompt)
ISPATH = os.path.isdir(PATHNAME)
if not ISPATH:
print('cannot find directory')
return PATHNAME
##################################################################
def text_list():
LENGTH = int_input('Enter the number of lineages you wish to analyze: ')
print('Input the full name for each lineage (e.g. 0001, 0001-2, 10001).')
LIST = []
for l in range(LENGTH):
L = text_input('Lineage ' + str(l+1) + ': ')
LIST.append(L)
return LIST
#################################################################
def CSVOUT(trajname,frame,time,area,cellXY,celllabels,fl0,divisions,dtime,FLLABELS,iXY):
#calculate doubling time
#~ if divisions != None:
#~ #make empty list length of time
#~ dtimes = []
#~ for f in frame:
#~ if f in divisions:
#~ dtimes.append('Division')
#~ else:
#~ dtimes.append(' ')
i = 0
for fl in fl0:
if fl[0] == None:
fl0[i] = 'nan'
i +=1
FRAME = np.array(frame)
TIMES = np.array(time)
AREA = np.array(area)
CELLXY = np.array(cellXY)
CELLX = CELLXY[:,1]
CELLY = CELLXY[:,0]
FL0 = np.array(fl0)
#~ if dtimes != None:
#~ DTIMES = np.array(dtimes)
f = open(OUTDIR + '/iXY' + str(iXY) + '-' + trajname + '.csv' , 'w')
f.write('frame,')
f.write('time,')
f.write('area,')
f.write('x position,')
f.write('y position,')
for fllabel in FLLABELS:
f.write(fllabel + ',')
#~ if dtimes != None:
#~ f.write('divisions,')
f.write('\n')
for ijk in range(len(FRAME)):
f.write(str(FRAME[ijk]) + ',')
f.write(str(TIMES[ijk]) + ',')
f.write(str(AREA[ijk]) + ',')
f.write(str(CELLX[ijk]) + ',')
f.write(str(CELLY[ijk]) + ',')
for fl0 in FL0[ijk]:
f.write(str(fl0) + ',')
#~ if dtimes != None:
#~ f.write(str(DTIMES[ijk]) + ',')
f.write('\n')
f.close()
CURSOR_UP_ONE = '\x1b[1A'
print(('saving ' + trajname + '.csv '))
sys.stdout.write(CURSOR_UP_ONE)
############################################################################
############################################################################
####################################################################
####################################################################
def main(argv):
global OUTDIR, iXY
OUTDIR = argv[0]
RUNALL = argv[1]
#list of trajectories to get
trajnames = argv[2]
iXY = argv[3]
#dt = argv[3]
FLLABELS = argv[4]
############################################################################
############################################################################
#load files
try:
#~ with open('global-cell-statistics.pkl', 'rb') as f:
#~ fltimeALL,fln,FLMEANALL,FLMEDIANALL,FLSTDALL,FLMEANALLFILTERED,FLMEANALLBACKGROUND = pickle.load(f)
with open('iXY' + str(iXY) + '_lineagetracking.pkl', 'rb') as f:
TRAJ = pickle.load(f)
#~ with open('lineagetrackingsummary.pkl', 'rb') as f:
#~ NAMES,ITIMES,ETIMES,MEANAREA,STDAREA,MEANFL,STDFL = pickle.load(f)
except:
print('Could not load file: '+'iXY' + str(iXY) + '_lineagetracking.pkl /n Cells must be tracked with TrackCellLineages.py (run through SegmentandTrack.py) before analyzing lineages')
############################################################################
############################################################################
DTRAJ = {}
for traj in TRAJ:
trajname,frame,time,area,cellXY,celllabels,fl0,divisions,dtime = traj
if not RUNALL:
#print trajname
DTRAJ[trajname] = frame,time,area,cellXY,celllabels,fl0,divisions,dtime
else:
CSVOUT(trajname,frame,time,area,cellXY,celllabels,fl0,divisions,dtime,FLLABELS, iXY)
#pdb.set_trace()
if not RUNALL:
for traj in trajnames:
try:
#print traj
frame,time,area,cellXY,celllabels,fl0,divisions,dtime = DTRAJ[traj]
#pdb.set_trace()
CSVOUT(traj,frame,time,area,cellXY,celllabels,fl0,divisions,dtime,FLLABELS, iXY)
except:
print(traj + ' cannot be found')
############################################################################
def run(argv):
iXY = argv[0]
OUTDIR = argv[1]
if not os.path.isdir(OUTDIR):
os.system('mkdir ' + OUTDIR)
RUNALL = argv[2]
FLLABELS = argv[3]
#list of trajectories to get
if RUNALL:
trajnames = None
else:
trajnames = text_list()
#dt = float_input('What is the time per frame in minutes (e.g. 0.5): ')
main([OUTDIR,RUNALL,trajnames,iXY,FLLABELS])
if __name__ == "__main__":
run()
############################################################################