forked from rflrob/YildizLabCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsight2spotlist.py
More file actions
139 lines (112 loc) · 3.84 KB
/
insight2spotlist.py
File metadata and controls
139 lines (112 loc) · 3.84 KB
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
from __future__ import print_function
from Mapping import loadmapping
from glob import glob
from math import floor
from collections import defaultdict
from csv import reader as csv_reader
dist = 2
stop = 8
def get_int(prompt):
while True:
val = raw_input(prompt)
try:
return int(val)
except ValueError:
print("'%s' doesn't seem to be an integer" % val)
def get_filename(prompt, guess='', list = None):
if list is None:
list = glob(guess)
for i, fname in enumerate(list):
print('[%2d] %s' % (i+1, fname))
choice = raw_input(prompt)
try:
return list[int(choice) - 1]
except:
return choice
def get_imagename():
imlist = glob('*.tif')
for i, im in enumerate(imlist):
print('[%2d] %s' % (i+1, im))
choice = raw_input("Select image from list above, or give filename:")
try:
return imlist[int(choice) - 1]
except ValueError as oops:
print(oops)
return choice
def get_insight_file(filelist = None):
if filelist is None:
filelist = glob('*.txt')
for i, fname in enumerate(filelist):
print('[%2d] %s' % (i+1, fname))
choice = raw_input("Select datafile from Insight, or give filename: ")
try:
return filelist[int(choice) - 1]
except ValueError as oops:
print(oops)
return choice
def get_framemap(map_name):
map_file = open(map_name)
framemap = {}
for line in map_file:
line = line.split()
boframe = line[0]
start = line[2]
stop = line[3]
framemap[boframe] = (start, stop)
return framemap
def main():
mapfile_name = get_filename('Select mapfile, or give filename: ',
guess = '*_1_20')
#length = get_int('Number of frames per frameset')
framemap_name = get_filename('Select Insight to tif mapping file, '
'or give filename: ',
guess = '*.tsv')
imname = get_imagename()
framemap = get_framemap(framemap_name)
mapping = loadmapping(mapfile_name)
spotlistname = raw_input("Base name for the spotlist file? ")
datalist = glob('*.txt')
n = 0
k = 0
fname = get_insight_file(datalist)
try:
datalist.remove(fname)
except ValueError:
pass
insight = open(fname)
insight.readline()
x,y, frame = zip(*((line.split()[1], line.split()[2], line.split()[12])
for line in insight))
x = map(float, x)
y = map(float, y)
framesets = defaultdict(list)
for xi, yi, framei in zip(x, y, frame):
framesets[framei].append((xi, yi))
for frameset in framesets:
n = k * 1000
spotlist = open("%s_%d_spotlist.txt" % (spotlistname, k), 'w')
spotlist.write('FileName=%s;\n\n' % imname)
spotlist.write('Sx\tSy\tStart\tEnd\tFlag\tPeak\n')
try:
start, stop = framemap[frameset]
start = int(start)
stop = int(stop)
except:
print("Can't find start and stops for frame %s" % frameset)
start = get_int('Enter start frame: ')
stop = get_int('Enter stop frame: ')
x, y = zip(*framesets[frameset])
xprime, yprime = mapping(x, y)
for x1, y1 in zip(x, y):
for x2, y2, x2p, y2p in zip(x, y, xprime, yprime):
if ((x2p - x1)**2 + (y2p - y1)**2) < dist**2:
spotlist.write('%d\t%d\t%d\t%d\t%d\t%d\n' %
(round(x1), round(y1), start, stop, 655, n))
spotlist.write('%d\t%d\t%d\t%d\t%d\t%d\n' %
(round(x2), round(y2), start, stop, 585, n))
n += 1
break #out of the inner loop
k += 1
spotlist.close()
if __name__ == "__main__":
main()