-
Notifications
You must be signed in to change notification settings - Fork 1
/
cbTabToBin
executable file
·96 lines (75 loc) · 2.73 KB
/
cbTabToBin
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
#!/usr/bin/env python
import logging, sys, optparse, struct
from collections import defaultdict
from os.path import join, basename, dirname, isfile
# ==== functions =====
def parseArgs():
" setup logging, parse command line arguments and options. -h shows auto-generated help page "
parser = optparse.OptionParser("usage: %prog [options] filename - convert coordinates to binary file for cluster cell browser. See ~/projects/cirm/tsneViewer/data/10xMegacell/analysis/tsne/2_components for an example.")
parser.add_option("-d", "--debug", dest="debug", action="store_true", help="show debug messages")
#parser.add_option("-f", "--file", dest="file", action="store", help="run on file")
#parser.add_option("", "--test", dest="test", action="store_true", help="do something")
(options, args) = parser.parse_args()
if args==[]:
parser.print_help()
exit(1)
if options.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
return args, options
def writeTwoNumbers(ofh, x, y):
#ofh.write(struct.pack("<f", x))
#ofh.write(struct.pack("<f", y))
ofh.write(struct.pack("<H", x))
ofh.write(struct.pack("<H", y))
# ----------- main --------------
def main():
args, options = parseArgs()
filename, outFname = args
ifh = open(filename)
sep = "\t"
headers = ifh.readline().rstrip().split(sep)
xIdx = headers.index("x")
yIdx = headers.index("y")
minX, minY = 1000000,1000000
maxX, maxY = -1E6, -1E6
vals = []
for line in ifh:
row = line.rstrip("\n").split(sep)
x = float(row[xIdx])
y = float(row[yIdx])
minX = min(minX, x)
maxX = max(maxX, x)
minY = min(minY, y)
maxY = max(maxY, y)
vals.append((x,y))
spanX = maxX-minX
spanY = maxY-minY
pxVals = []
for x, y in vals:
pxX = int((x-minX)/spanX * 65535) # result must not be 65536
pxY = int((y-minY)/spanY * 65535)
pxVals.append( (pxX, pxY) )
ofh = open(outFname, "wb")
# write number of elements
elCount = len(vals)
writeTwoNumbers(ofh, elCount/65536, elCount%65536)
print "Count of (x,y) tuples: %d" % elCount
#elStr = struct.pack(">I", elCount)
#ofh.write(elStr)
#ofh.write(elStr)
# write min and max'es
print ("X min/max: %f, %f" % (minX, maxX))
print ("Y min/max: %f, %f" % (minY, maxY))
#writeTwoNumbers(ofh, minX, maxX)
#writeTwoNumbers(ofh, minY, maxY)
writeTwoNumbers(ofh, 0, 65535)
writeTwoNumbers(ofh, 0, 65535)
for x, y in pxVals:
writeTwoNumbers(ofh, x, y)
ofh.close()
#if options.test:
#logging.debug("test is set")
#f = open(options.file, "r")
main()