-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWHFiles2XML.py
More file actions
executable file
·68 lines (52 loc) · 1.93 KB
/
WHFiles2XML.py
File metadata and controls
executable file
·68 lines (52 loc) · 1.93 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
#/usr/bin/python
import os, re, sys
from os import path
from glob import glob
from scipy import io
from optparse import OptionParser
def write_markerdata(fname, pxsize = 106.667, outfile = sys.stdout):
lineno = 1
for line in file(fname):
if line[0].isdigit():
data = map(float, line.split())
x = data[0]/pxsize
y = data[1]/pxsize
outfile.write('\t\t<Marker><MarkerX>%d</MarkerX> '
'<MarkerY>%d</MarkerY> '
'<MarkerZ>%d</MarkerZ> '
'</Marker>\n' % (x,y, int(lineno)))
elif 'x\t y' in line:
lineno = 0
lineno += 1
#def main():
if __name__ == "__main__":
parser = OptionParser(usage="Usage: %prog foldername outputfile.xml")
parser.add_option('-x', '--pixel-size', type="float", dest="pxsize",
help="Size of pixels, in nanometers", default=106.667)
opts, args = parser.parse_args()
assert len(args) == 2
fnames = glob(path.join(args[0], '*'))
outfile = open(args[1], 'w')
imgname = path.basename(re.findall('(.*)_spot', fnames[0])[0] + '.tif')
## Write XML Header stuff
outfile.write('<?xml version="1.0" encoding="UTF-8"?>\n')
outfile.write("<CellCounter_Marker_File>\n")
outfile.write("<Image_Properties>\n"
"\t<Image_Filename>%s</Image_Filename>\n"
"</Image_Properties>\n" % imgname)
outfile.write('<Marker_Data>\n')
outfile.write('\t<Current_Type>1</Current_Type>\n')
colorfinder = re.compile(r'spot\d*_(.*)_xy')
colors = set([colorfinder.findall(fname)[0] for fname in fnames])
for color_type, color in enumerate(colors):
outfile.write('\t<Marker_Type> \n')
outfile.write('\t\t<Type>%d</Type>\n' % (color_type + 1))
for fname in fnames:
if color in colorfinder.findall(fname):
write_markerdata(fname, opts.pxsize, outfile)
outfile.write('\t</Marker_Type>\n')
outfile.write('</Marker_Data>\n')
outfile.write('</CellCounter_Marker_File>\n')
outfile.close()
if __name__ == "__main__":
pass#main()