-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConvertPlateReader.py
executable file
·85 lines (70 loc) · 2.6 KB
/
ConvertPlateReader.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
import sys
import os
import datetime
import csv
usage = """usage:
ConvertPlateReader.py infile outfile row-name*
"""
def find_in_sublists(lst, value):
for sub_i, sublist in enumerate(lst):
try:
return (sub_i, sublist.index(value))
except ValueError:
pass
raise ValueError('%s is not in lists' % value)
if __name__=='__main__':
filename = sys.argv[1]
outfile = sys.argv[2]
rownames = sys.argv[3:]
nsets = len(rownames)
rawdata = list(csv.reader(open(filename, 'rU')))
# Work out location of header row, and column/row numbers
try:
(headerrow,ccol) = find_in_sublists(rawdata,'Well\nCol')
(headerrow,crow) = find_in_sublists(rawdata,'Well\nRow')
except ValueError:
(headerrow,ccol) = find_in_sublists(rawdata,'Well Col')
(headerrow,crow) = find_in_sublists(rawdata,'Well Row')
# Time is on row below headers
timerow = headerrow+1
# Data is on rows below time
datarow = timerow+1
# Find where the data is in each row by looking for 1st 'Raw Data'
headers = rawdata[headerrow]
# print headers
cdata = (i for i,v in enumerate(headers) if 'Raw Data' in v).next()
# print cdata
r = rawdata[datarow:]
r1 = r[1]
# print r1[cdata:]
# for d in r1[cdata:]:
# print map(float, d)
# Pull out row,col and data for each row
# Try combinations of alpha/numeric for row/col
try:
rows = [(ord(row[crow].upper())-64, int(row[ccol]), map(float, row[cdata:])) for row in rawdata[datarow:]]
except ValueError:
rows = [(int(row[ccol]), ord(row[crow].upper())-64, map(float, row[cdata:])) for row in rawdata[datarow:]]
# Pull out time of each data point
times = rawdata[timerow]
times = times[cdata:]
assert len(rows[0][2]) % nsets == 0
nsteps = len(rows[0][2]) / nsets
data = [['','row','col','t'] + rownames]
n = 1
for row, col, vals in rows:
for i in range(nsteps):
t = float(times[i])*60
line = [n, row, col, t]
for j in range(nsets):
k = j*nsteps + i
line.append(vals[k])
data.append(line)
n = n+1
outf = open(outfile, 'wb')
outf.write("# Automatically generated by ConvertPlateReader.py\n")
outf.write("# Source: " + filename + "\n")
outf.write("# Row Names: " + str(rownames) + "\n")
outf.write("# Date: " + str(datetime.datetime.now()) + "\n")
writer = csv.writer(outf, dialect='excel')
writer.writerows(data)