ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/io_module.py
(Generate patch)
# Line 1 | Line 1
1   """
2 < io: Input/Output module for converting files to Biosensor Array class.
3 < Christopher Lausted, Institute for Systems Biology, OSPRAI developers
4 < Last modified on 100409 (yymmdd)
5 <
6 < Examples:
7 < #import io_module as io
8 < #ba1 = io.readsprit("spritdata.txt")
9 < #ba1 = io.readclamp("spritdata.txt")
10 < #ba1 = io.readicmtxt("icmdata.txt")
11 < #ba1 = io.readbiosensor("icmdata.txt")
12 < #ba2 = io.applygal(ba1, "galfile.gal")
13 < #ba2 = io.applykey(ba1, "keyfile.tsv")
14 < #ba3 = io.applymethod(ba2, "icmmethod.xls")
15 < #io.writesprit(ba3, "newspritfile.txt")
16 < #io.writeclamp(ba3, "newclampfile.txt")
2 > io_module
3 > ---------
4 >
5 > Input/Output module for converting files to Biosensor Array class.
6 > Supported sensorgram types include Biosensor, CLAMP, SPRit, and Plexera ICM.
7 > Supported microarray types include GAL and ISB Map.
8 >
9 > .. moduleauthor:: Christopher Lausted,
10 >                  Institute for Systems Biology,
11 >                  OSPRAI developers.                      
12 > Examples::
13 >
14 >  >>> import io_module as io
15 >  >>> ba1 = io.readsprit("spritdata.txt")
16 >  >>> ba1 = io.readclamp("spritdata.txt")
17 >  >>> ba1 = io.readicmtxt("icmdata.txt")
18 >  >>> ba1 = io.readbiosensor("icmdata.txt")
19 >  >>> ba1 = io.readcsv("rawdata.csv")
20 >  >>> ba2 = io.applygal(ba1, "galfile.gal")
21 >  >>> ba2 = io.applykey(ba1, "keyfile.tsv")
22 >  >>> ba3 = io.applymethod(ba2, "icmmethod.xls")
23 >  >>> io.writesprit(ba3, "newspritfile.txt")
24 >  >>> io.writeclamp(ba3, "newclampfile.txt")
25   """
26 < __version__ = "100409"
26 > __version__ = "110208"
27 >
28  
29   ## Import libraries.
30   from copy import deepcopy
# Line 29 | Line 38
38      """
39      Read a SPRit text file into a ba class.
40      It has two tab-delimited columns and two header lines.
41 <    Here is a very simple example with 2 rois and 3 datapoints:
41 >    Here is a very simple example with 2 rois and 3 datapoints::
42      
43 <    Elapsed Time (Seconds)   Average Intensity (Pixel Intensity)
44 <    BEGIN
45 <    0.000000e+000            2.863145e+003
46 <    5.013000e+000            2.863367e+003
47 <    1.002500e+001            2.862950e+003
48 <    0.000000e+000            2.862875e+003
49 <    5.013000e+000            2.862510e+003
43 >      Elapsed Time (Seconds)   Average Intensity (Pixel Intensity)
44 >      BEGIN
45 >      0.000000e+000            2.863145e+003
46 >      5.013000e+000            2.863367e+003
47 >      1.002500e+001            2.862950e+003
48 >      0.000000e+000            2.862875e+003
49 >      5.013000e+000            2.862510e+003
50      """
51      
52      ## Try to open file. Return tiny ba object if it fails.
# Line 84 | Line 93
93              k +=1
94              
95      return ba0
96 <    ## End of readsprit() function
96 >    """End of readsprit() function"""
97      
98      
99   def writesprit(ba0, fname):
100 <    """Write a ba class to a SPRit text file."""
100 >    """
101 >    Write a ba class to a SPRit text file.
102 >    """
103      fp=file(fname,'w')
104      fp.write("Elapsed Time (Seconds)\tAverage Intensity (Pixel Intensity)\r\n")
105      fp.write("BEGIN\r\n")
# Line 98 | Line 109
109              fp.write(txt)
110      fp.close
111      print "File %s saved in %s." % (fname, getcwd())
112 <    ## End of writesprit() function
112 >    return
113 >    """End of writesprit() function"""
114      
115      
116   def readclamp(fname):
# Line 106 | Line 118
118      Read a Clamp text file into a ba class.
119      It has two tab-delimited columns per SPR flowcell/roi..
120      It has a varying number of header lines with injection information.
121 <    Here is a very simple example with 2 rois and 3 datapoints:
121 >    Here is a very simple example with 2 rois and 3 datapoints::
122      
123 <    Vers 3.41 Data
124 <    Conc1   0       0       0       0
125 <    Start1  301.5   301.5   301.5   301.5
126 <    Stop1   949.8   949.8   949.8   949.8
127 <    RInd1   0       0       0       0
128 <    Conc2   0       0       0       0
129 <    Start2  986.4   0       0       0
130 <    Stop2   1626    0       0       0
131 <    RInd2   0       0       0       0
132 <    Flow    1       1       1       1
133 <    Time1   Data1   Time2   Data2
134 <    0.094   0.062   0.094   0.053
135 <    1.094   0.026   1.094   0.05
136 <    2.094   0.119   2.094   0.055
123 >      Vers 3.41 Data
124 >      Conc1   0       0       0       0
125 >      Start1  301.5   301.5   301.5   301.5
126 >      Stop1   949.8   949.8   949.8   949.8
127 >      RInd1   0       0       0       0
128 >      Conc2   0       0       0       0
129 >      Start2  986.4   0       0       0
130 >      Stop2   1626    0       0       0
131 >      RInd2   0       0       0       0
132 >      Flow    1       1       1       1
133 >      Time1   Data1   Time2   Data2
134 >      0.094   0.062   0.094   0.053
135 >      1.094   0.026   1.094   0.05
136 >      2.094   0.119   2.094   0.055
137      """
138      
139      print "This feature is under construction."
140 <    ## End of readclamp() function
140 >    return
141 >    """"End of readclamp() function"""
142      
143      
144   def writeclamp(ba0, fname):
145 <    """Write a ba class to a Clamp text file."""
145 >    """
146 >    Write a ba class to a Clamp text file.
147 >    """
148      
149      fp=file(fname,'w')
150      ## First header line.
# Line 167 | Line 182
182      ## Close file handle and print message.
183      fp.close
184      print "File %s saved in %s." % (fname, getcwd())
185 <    ## End of writeclamp() function
185 >    return
186 >    """End of writeclamp() function"""
187      
188      
189   def readicmtxt(fname):
190      """
191      Read a ICM text file into a ba class.
192 <    Here is a very simple example of the tab-delimited format:
192 >    Here is a very simple example of the tab-delimited format::
193      
194 <    03/05/2010 13:37:21.312   249.408   0.000   0.000
195 <    03/05/2010 13:37:22.312   249.306   0.000   0.000
194 >      03/05/2010 13:37:21.312   249.408   0.000   0.000
195 >      03/05/2010 13:37:22.312   249.306   0.000   0.000
196      """
197  
198      ## Try to open file. Return tiny ba object if it fails.
# Line 199 | Line 215
215      
216      ## Determine experiment start time so we can make other times relative.
217      x = txtfile[0].split("\t")  ## E.g. "03/05/2010 13:37:20.218"
218 <    t0 = icmtime2sec(x[0])      ## E.g. 1267825040.22
218 >    t0 = __icmtime2sec(x[0])      ## E.g. 1267825040.22
219      
220      ## Move the data from txtfile to ba0.
221      for i, txtline in enumerate(txtfile):
222          x = txtline.split("\t")
223 <        tx = icmtime2sec(x[0]) - t0
223 >        tx = __icmtime2sec(x[0]) - t0
224          for j in range(1,rois):
225              ba0.roi[j].time[i] = tx
226              ba0.roi[j].value[i] = float(x[j])
227              
228      return ba0
229 <    ## End of readicmtxt
229 >    """End of readicmtxt"""
230  
231  
232 < def icmtime2sec(timetxt):
232 > def __icmtime2sec(timetxt):
233      """
234      Take a time of the form "03/05/2010 15:19:27.312" and return seconds.
235      """
# Line 222 | Line 238
238      hh, min, ss = clock.split(":")
239      stime = (int(yy), int(mm), int(dd), int(hh), int(min), 0, 0, 0, 0)
240      return mktime(stime) + float(ss)
241 +    """End of __icmtime2sec"""
242      
243      
244   def readbiosensor(fname):
245      """
246      Read a Biacore-style text file into a ba class.
247 <    Here is a very simple example of the tab-delimited format:
247 >    Here is a very simple example of the tab-delimited format::
248      
249 <    Ab1 Fc=4- 1_X  Ab1 Fc=4 -1_Y  Ab2 Fc=4 -1_X  Ab2 Fc=4 -1_Y
250 <    13.1           23.7644        93.1           0.713912
251 <    13.6           23.4265        93.6           0.0541172
252 <    14.1           23.1625        94.1           0.332768
253 <    14.6           23.5752        94.6           0.849459
249 >      Ab1 Fc=4- 1_X  Ab1 Fc=4 -1_Y  Ab2 Fc=4 -1_X  Ab2 Fc=4 -1_Y
250 >      13.1           23.7644        93.1           0.713912
251 >      13.6           23.4265        93.6           0.0541172
252 >      14.1           23.1625        94.1           0.332768
253 >      14.6           23.5752        94.6           0.849459
254      """
255  
256      ## Try to open file. Return tiny ba object if it fails.
# Line 279 | Line 296
296              ba0.roi[j].value[i] = float(x[j*2+1])
297      
298      return ba0
299 <    ## End of readbiosensor.
299 >    """End of readbiosensor()"""
300  
301  
302   def writebiosensor(ba0, fname):
303 <    """Write a ba class to a Biosensor text file."""
303 >    """
304 >    Write a ba class to a Biosensor text file.
305 >    """
306      
307      ## This is like a simplified Clamp file.
308      fp=file(fname,'w')
# Line 302 | Line 321
321      ## Close file handle and print message.
322      fp.close
323      print "File %s saved in %s." % (fname, getcwd())
324 <    ## End of writebiosensor() function
324 >    return
325 >    """End of writebiosensor()"""
326 >
327 >
328 > def readcsv(fname):
329 >    """
330 >    Read a comma-separated value text file into a ba class.
331 >    The first column contains time data while the others contain response data.
332 >    Here is a very simple example of the tab-delimited format::
333 >    
334 >      1.0001, 23.7644, 0.7139
335 >      2.0001, 23.4265, 0.0541
336 >      3.0001, 23.1625, 0.3327
337 >      4.0001, 23.5752, 0.8494
338 >    """
339 >
340 >    ## Try to open file. Return tiny ba object if it fails.
341 >    ba0 = ba.BiosensorArray(1,1)
342 >    try:
343 >        fp = open(fname, "r")
344 >    except IOError:
345 >        print 'Error: Cannot open file %s for reading' % fname
346 >        return ba0
347 >      
348 >    ## Put data into one big text string, determine array size, and close.
349 >    txtfile = fp.readlines()  ## One big text string.
350 >    fp.close()
351 >    dpoints = len(txtfile)  ## Rows in the csv file.
352 >    rois = len(txtfile[0].split(',')) - 1  ## Columns are comma-delimited.
353 >    
354 >    ## Move the data from txtfile to ba0.
355 >    ba0 = ba.BiosensorArray(rois, dpoints)
356 >    for i, txtline in enumerate(txtfile):
357 >        x = txtline.split(',')
358 >        for j in range(rois):
359 >            ba0.roi[j].time[i] = float(x[0])
360 >            ba0.roi[j].value[i] = float(x[j+1])
361 >    
362 >    print "The file %s has %i ROIs and %i datapoints." % (fname, rois, dpoints)
363 >    return ba0
364 >    """End of readcsv()"""
365  
366  
367   def applygal(ba0, fname):
368 <    """Read a Gal file and apply its microarray information."""
368 >    """
369 >    Read a Gal file and apply its microarray information.
370 >    
371 >    *(This function is under construction)*
372 >    """
373      print "This feature is under construction."
374 <    ## End of applygal() function
374 >    return
375 >    """End of applygal()"""
376      
377      
378   def applykey(ba0, fname):
379      """
380      Read a Key file and apply its microarray information.
381      Multiple background ROIs are not yet supported.
382 <    Here is a very simple example :
382 >    Here is a very simple example::
383      
384 <    No. Description1    Description2    Background ROI  Col     Row
385 <    1   Rat TNF         Antibody01      2               1       1
386 <    2   ratIgG          Antibody02      4               2       1
387 <    3   Hum TNF         Antibody03      4               3       1
388 <    4   humIgG          Antibody04      2               4       1
384 >      No. Description1    Description2    Background ROI  Col     Row
385 >      1   Rat TNF         Antibody01      2               1       1
386 >      2   ratIgG          Antibody02      4               2       1
387 >      3   Hum TNF         Antibody03      4               3       1
388 >      4   humIgG          Antibody04      2               4       1
389      """
390      
391      ## Try to open file. Return unchanged ba object if it fails.
# Line 376 | Line 440
440      
441      print "Successfully loaded information for %i ROIs." % keys
442      return ba1
443 <    ## End of applykey() function
443 >    """End of applykey()"""
444      
445      
446   def applymethod(ba0, fname):
447 <    """Read a ICM Analyte/Method xls file and apply its information."""
447 >    """
448 >    Read a ICM Analyte/Method xls file and apply its information.
449 >    
450 >    *(This feature is under construction.)*
451 >    """
452      print "This feature is under construction."
453 <    ## End of applymethod() function
453 >    return
454 >    """End of applymethod()"""
455      
456      
457  
# Line 407 | Line 476
476          fp.write("\n")
477      fp.close
478      print "File %s saved in %s." % (fname, getcwd())
479 <    ## End of outputbindinglevels() function
479 >    return
480 >    """End of outputbindinglevels()"""
481  
482  
483   ## Here are a few lines to test this module.
484   if __name__ == '__main__':
485      print "This module isn't a stand-alone app."
486  
417 ################################# End of module #################################
487 + ################################# End of module #################################

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines