1 |
""" |
2 |
ba_class |
3 |
-------- |
4 |
|
5 |
Biosensor Array class for storing SPRI data. |
6 |
This contains an array of SPR sensorgrams with their |
7 |
associated models and parameters. |
8 |
This can also contain the microarray geometery. |
9 |
|
10 |
.. moduleauthor:: Christopher Lausted, |
11 |
Institute for Systems Biology, |
12 |
Yuhang Wan, |
13 |
OSPRAI developers. |
14 |
|
15 |
Examples:: |
16 |
|
17 |
>>> ba1 = BiosensorArray(800,1500) ## Allocate object with 800 spots of 1500 time points. |
18 |
>>> ba1.roi[799].time[1499] = 7502.1 ## Set time in seconds. |
19 |
>>> ba1.roi[799].value[1499] = 0.50 ## Set SPR reflectance signal. |
20 |
>>> ba1.roi[799].name = "anti-IgG" ## Set name of microarray feature. |
21 |
""" |
22 |
__version__ = "110204" |
23 |
|
24 |
|
25 |
## Import libraries. |
26 |
import numpy as np |
27 |
#import matplotlib.pyplot as plt |
28 |
from datetime import date |
29 |
|
30 |
|
31 |
class BiosensorArray(object): |
32 |
""" |
33 |
The Biosensor Array class holds all ROIs (spots) |
34 |
from an SPRI microarray run. |
35 |
|
36 |
================ =============== ======================================= |
37 |
Property Type Description |
38 |
---------------- --------------- --------------------------------------- |
39 |
roi list of object List of RegOfInterest objects. |
40 |
rois integer Size of roi (Number of ROIs). |
41 |
dpoints integer Number of datapoints in each ROI. |
42 |
primarydatafiles list of string List of file names. |
43 |
comments list of string List of comments regarding experiments. |
44 |
================ =============== ======================================= |
45 |
""" |
46 |
|
47 |
def __init__(self, roi_size, datapoint_size): |
48 |
"""This object is dimensioned upon initialization.""" ## Might improve speed. |
49 |
## Use a list comprehension to dimension list of classes. |
50 |
self.roi = [RegOfInterest(i,datapoint_size) for i in range(roi_size)] |
51 |
## Remember the dimensions for quick bounds-checking later. |
52 |
self.rois = roi_size |
53 |
self.dpoints = datapoint_size |
54 |
## Use these to keep track of laboratory notes. |
55 |
self.primarydatafiles = [""] ## Like "spritdata.txt" |
56 |
self.comments = [""] ## Like "Antibody data from 1 Jan 2010" |
57 |
return |
58 |
|
59 |
def xy2uid(self, gx, gy, x, y): |
60 |
"""Given roi coordinates, find corresponding uid (index).""" |
61 |
coor1 = (gx, gy, x, y) |
62 |
for ob in self.roi: |
63 |
coor2 = (ob.gridx, ob.gridy, ob.spotx, ob.spoty) |
64 |
if (coor1==coor2): return ob.uid ## Success. |
65 |
return -1 ## Failure. |
66 |
|
67 |
def set_plot_all(self): |
68 |
"""Choose to plot every sensorgram.""" |
69 |
for ob in self.roi: ob.plottable = True |
70 |
return |
71 |
|
72 |
def set_plot_list(self, ilist): |
73 |
"""Choose a list of sensorgrams to plot""" |
74 |
for ob in self.roi: ob.plottable = False |
75 |
for i in ilist: self.roi[i].plottable = True |
76 |
return |
77 |
|
78 |
## A pyplot feature for testing purposes. |
79 |
def plot(self): |
80 |
"""Show plot of the selected sensorgrams.""" |
81 |
plt.clf() |
82 |
plt.title('SPR Data plotted %s' % date.today().isoformat()) |
83 |
plt.xlabel('Time (s)') |
84 |
plt.ylabel('SPR Response') |
85 |
plt.grid(True) |
86 |
## Plot traces. |
87 |
for ob in self.roi: |
88 |
if (ob.plottable==True): |
89 |
mylabel = "%i:%s" % (ob.index,ob.name) |
90 |
plt.plot(ob.time, ob.value, label=mylabel) |
91 |
plt.legend(loc='best') |
92 |
plt.show() |
93 |
return |
94 |
|
95 |
"""End of BiosensorArray class definition.""" |
96 |
|
97 |
|
98 |
class RegOfInterest(object): |
99 |
""" |
100 |
This Region Of Interest class can hold one SPR sensorgram. |
101 |
|
102 |
=========== =============== ============================================= |
103 |
Property Type Description |
104 |
----------- --------------- --------------------------------------------- |
105 |
uid integer Unique ID integer. Never changes. |
106 |
index integer Changes if ba size changes (add/remove rois). |
107 |
time nparray Usually seconds. |
108 |
value nparray Arbitrary units. |
109 |
dpoints integer Can use for bounds checking. |
110 |
name string Name of ROI. |
111 |
desc string Description of ROI |
112 |
|
113 |
gridx integer Can be Block number from GAL file. |
114 |
gridy integer Unused? |
115 |
spotx integer ROI column number. |
116 |
spoty integer ROI row number. |
117 |
bgroi list of integer One or more background ROIs. |
118 |
calibM float Slope used for calibration |
119 |
calibB float Intercept used for calibration. |
120 |
plottable boolean Whether to plot |
121 |
|
122 |
injconc list of float One or more analyte concentrations. |
123 |
injstart list of float One or more injection start times. |
124 |
injstop list of float One or more injection start times. |
125 |
injrind list of float One or more expected refractive index jumps. |
126 |
|
127 |
flow float Flowrate of injection. |
128 |
model ref to funct Model describing this ROI. |
129 |
params dict of dict Parameters for this model. |
130 |
=========== =============== ============================================= |
131 |
""" |
132 |
|
133 |
def __init__(self, i, datapoint_size): |
134 |
""" |
135 |
Each ROI in a list needs a unique ID number which will be i+1. |
136 |
The time and value arrays must be allocated using datapoint_size. |
137 |
""" |
138 |
self.uid = i+1 ## Unique ID integer. Never changes. |
139 |
self.index = i ## Changes if ba size changes (add/remove rois). |
140 |
self.time = np.zeros(datapoint_size, dtype=float) ## Usually seconds. |
141 |
self.value = np.zeros(datapoint_size, dtype=float) ## Arbitrary units. |
142 |
self.dpoints = datapoint_size ## Can use for bounds checking. |
143 |
## Useful optional information. Usually from GAL or Key files. |
144 |
self.name = "Spot%i" % (i+1) |
145 |
self.desc = "Description" |
146 |
self.gridx = 0 ## Can be Block number from GAL file. |
147 |
self.gridy = 0 ## Unused? |
148 |
self.spotx = 0 ## ROI column number. |
149 |
self.spoty = 0 ## ROI row number. |
150 |
self.bgroi = [0] ## One or more background ROIs. |
151 |
self.calibM = 1 ## Slope used for calibration |
152 |
self.calibB = 0 ## Intercept used for calibration. |
153 |
self.plottable = True ## Whether to plot |
154 |
## Optional injection information. Usually from Clamp or ICM Method files. |
155 |
self.injconc = [0.0] ## One or more analyte concentrations. |
156 |
self.injstart = [0.0] ## One or more injection start times. |
157 |
self.injstop = [0.0] ## One or more injection start times. |
158 |
self.injrind = [0.0] ## One or more expected refractive index jumps. |
159 |
self.flow = 0 ## Flowrate of injection. |
160 |
## Curve fitting information. Model and model parameters. |
161 |
## Example model is reference to function like data=simple1to1(times,params) |
162 |
## Example params = {'Rmax': {'value':1, 'min':0, 'max':10, 'fixed':'float'} } |
163 |
self.model = None ## Model describing this roi. Reference to a function. |
164 |
self.params = [] ## Parameters for this model. A dictionary of dictionaries. |
165 |
|
166 |
def time2dp(self, t): |
167 |
"""Find datapoint closest to given time.""" |
168 |
pos2 = self.time.searchsorted(t) ## Time point just after t. |
169 |
pos2 = min(pos2, len(self.time)-1) ## Avoid indexing error. |
170 |
pos1 = max(pos2-1,0) ## Time point just before t. |
171 |
t2 = abs(self.time[pos2] - t) |
172 |
t1 = abs(self.time[pos1] - t) |
173 |
## Decide if time point just before or just after is closer. |
174 |
if (t2<t1): |
175 |
return pos2 |
176 |
else: |
177 |
return pos1 |
178 |
|
179 |
def time2val(self, t1, t2): |
180 |
"""Return list of SPR values between t1 and t2""" |
181 |
dp1, dp2 = self.time2dp(t1), self.time2dp(t2) |
182 |
return [self.value[i] for i in range(dp1, dp2)] |
183 |
|
184 |
"""End of RegOfInterest definition.""" |
185 |
|
186 |
''' |
187 |
## Here are a few lines to test this class. |
188 |
if __name__ == '__main__': |
189 |
print "Starting demo..." |
190 |
print "Test plotting..." |
191 |
x = BiosensorArray(10,10) |
192 |
x.roi[0].time = np.arange(10) |
193 |
x.roi[0].value = np.arange(10)**2 |
194 |
x.roi[1].time = np.arange(10) |
195 |
x.roi[1].value = np.arange(10)**1.5 |
196 |
x.roi[9].time = np.arange(10) |
197 |
x.roi[9].value = np.arange(10)**1 |
198 |
#x.set_plot_all() |
199 |
x.set_plot_list([0,1,9]) |
200 |
x.plot() |
201 |
print "Test xy2uid... 9 =", |
202 |
x.roi[9].gridx = 2 |
203 |
print x.xy2uid(2,0,0,0) |
204 |
print "Test time2dp... 5.6 =>", |
205 |
print x.roi[9].time2dp(5.6) |
206 |
print "Demo finished." |
207 |
''' |
208 |
|
209 |
|
210 |
################################# End of module ################################# |