ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/io_module.py
(Generate patch)
# Line 16 | Line 16
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__ = "110204"
26 > __version__ = "110208"
27  
28  
29   ## Import libraries.
# Line 214 | 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])
# Line 228 | Line 229
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 237 | 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"""
241 >    """End of __icmtime2sec"""
242      
243      
244   def readbiosensor(fname):
# Line 324 | Line 325
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      """
369      Read a Gal file and apply its microarray information.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines