ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/ba_class.py
Revision: 9
Committed: Sat Apr 10 00:21:02 2010 UTC (9 years ago) by clausted
File size: 5440 byte(s)
Log Message:
Add readbiosensor() and writebiosensor() capabilities to io_module.  
Line User Rev File contents
1 clausted 7 """
2     ba: Biosensor Array class for storing SPRI data.
3 clausted 9 Christopher Lausted, Institute for Systems Biology,
4     Yuhang Wan, OSPRAI developers
5     Last modified on 100409 (yymmdd)
6 clausted 7
7     Examples:
8     #ba1 = BiosensorArray(800,1500) ## Allocate object with 800 spots of 1500 time points.
9     #ba1.roi[799].time[1499] = 7502.1 ## Set time in seconds.
10     #ba1.roi[799].value[1499] = 0.50 ## Set SPR reflectance signal.
11     #ba1.roi[799].name = "anti-IgG" ## Set name of microarray feature.
12     """
13 clausted 9 __version__ = "100409"
14 clausted 7
15     ## Import libraries.
16     import numpy as np
17     import matplotlib.pyplot as plt
18     from datetime import date
19    
20    
21     class BiosensorArray(object):
22     """The Biosensor Array class holds all ROIs/spots from an SPRI microarray run."""
23     def __init__(self, roi_size, datapoint_size):
24     """This object is dimensioned upon initialization.""" ## Might improve speed.
25     ## Use a list comprehension to dimension list of classes.
26     self.roi = [RegOfInterest(i,datapoint_size) for i in range(roi_size)]
27     ## Remember the dimensions for quick bounds-checking later.
28     self.rois = roi_size
29     self.dpoints = datapoint_size
30     ## Use these to keep track of laboratory notes.
31     self.primarydatafiles = [""] ## Like "spritdata.txt"
32     self.comments = [""] ## Like "Antibody data from 1 Jan 2010"
33    
34     def xy2uid(self, gx, gy, x, y):
35     """Given roi coordinates, find corresponding uid (index)."""
36     coor1 = (gx, gy, x, y)
37     for ob in self.roi:
38     coor2 = (ob.gridx, ob.gridy, ob.spotx, ob.spoty)
39     if (coor1==coor2): return ob.uid ## Success.
40     return -1 ## Failure.
41    
42     def set_plot_all(self):
43     """Choose to plot every sensorgram."""
44     for ob in self.roi: ob.plottable = True
45    
46     def set_plot_list(self, ilist):
47     """Choose a list of sensorgrams to plot"""
48     for ob in self.roi: ob.plottable = False
49     for i in ilist: self.roi[i].plottable = True
50    
51     ## A pyplot feature for testing purposes.
52     def plot(self):
53     """Show plot of the selected sensorgrams."""
54     plt.clf()
55     plt.title('SPR Data plotted %s' % date.today().isoformat())
56     plt.xlabel('Time (s)')
57     plt.ylabel('SPR Response')
58     plt.grid(True)
59     ## Plot traces.
60     for ob in self.roi:
61     if (ob.plottable==True):
62     mylabel = "%i:%s" % (ob.index,ob.name)
63     plt.plot(ob.time, ob.value, label=mylabel)
64     plt.legend(loc='best')
65     plt.show()
66     ### End of BiosensorArray class definition.
67    
68    
69     class RegOfInterest(object):
70     """This Region Of Interest class can hold one SPR sensorgram."""
71     def __init__(self, i, datapoint_size):
72     """
73     Each ROI in a list needs a unique ID number which will be i+1.
74     The time and value arrays must be allocated using datapoint_size.
75     """
76     self.uid = i+1 ## Unique ID integer. Never changes.
77     self.index = i ## Changes if ba size changes (add/remove rois).
78     self.time = np.zeros(datapoint_size, dtype=float) ## Usually seconds.
79     self.value = np.zeros(datapoint_size, dtype=float) ## Arbitrary units.
80     self.dpoints = datapoint_size ## Can use for bounds checking.
81     ## Useful optional information. Usually from GAL or Key files.
82     self.name = "Spot%i" % (i+1)
83     self.desc = "Description"
84     self.gridx = 0 ## Can be Block number from GAL file.
85     self.gridy = 0 ## Unused?
86     self.spotx = 0 ## ROI column number.
87     self.spoty = 0 ## ROI row number.
88     self.bgroi = [0] ## One or more background ROIs.
89     self.calibM = 1 ## Slope used for calibration
90     self.calibB = 0 ## Intercept used for calibration.
91     self.plottable = True ## Whether to plot
92     ## Optional injection information. Usually from Clamp or ICM Method files.
93     self.injconc = [0.0] ## One or more analyte concentrations.
94     self.injstart = [0.0] ## One or more injection start times.
95     self.injstop = [0.0] ## One or more injection start times.
96     self.injrind = [0.0] ## One or more expected refractive index jumps.
97     self.flow = 0 ## Flowrate of injection.
98    
99     def time2dp(self, t):
100     """Find datapoint closest to given time."""
101     pos2 = self.time.searchsorted(t) ## Time point just after t.
102     pos1 = max(pos2-1,0)
103     t2 = abs(self.time[pos2] - t)
104     t1 = abs(self.time[pos1] - t)
105     if (t2<t1):
106     return pos2
107     else:
108     return pos1
109    
110     ## End of RegOfInterest definition.
111    
112    
113     ## Here are a few lines to test this class.
114     if __name__ == '__main__':
115     print "Starting demo..."
116     print "Test plotting..."
117     x = BiosensorArray(10,10)
118     x.roi[0].time = np.arange(10)
119     x.roi[0].value = np.arange(10)**2
120     x.roi[1].time = np.arange(10)
121     x.roi[1].value = np.arange(10)**1.5
122     x.roi[9].time = np.arange(10)
123     x.roi[9].value = np.arange(10)**1
124     #x.set_plot_all()
125     x.set_plot_list([0,1,9])
126     x.plot()
127     print "Test xy2uid... 9 =",
128     x.roi[9].gridx = 2
129     print x.xy2uid(2,0,0,0)
130     print "Test time2dp... 5.6 =>",
131     print x.roi[9].time2dp(5.6)
132     print "Demo finished."
133    
134    
135 clausted 9 ################################# End of module #################################