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
4 < Last modified on 100408 (yymmdd)
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")
17   """
18 < __version__ = "100408"
18 > __version__ = "100409"
19  
20   ## Import libraries.
21   from copy import deepcopy
# Line 222 | Line 224
224      return mktime(stime) + float(ss)
225      
226      
227 + def readbiosensor(fname):
228 +    """
229 +    Read a Biacore-style text file into a ba class.
230 +    Here is a very simple example of the tab-delimited format:
231 +    
232 +    Ab1 Fc=4- 1_X  Ab1 Fc=4 -1_Y  Ab2 Fc=4 -1_X  Ab2 Fc=4 -1_Y
233 +    13.1           23.7644        93.1           0.713912
234 +    13.6           23.4265        93.6           0.0541172
235 +    14.1           23.1625        94.1           0.332768
236 +    14.6           23.5752        94.6           0.849459
237 +    """
238 +
239 +    ## Try to open file. Return tiny ba object if it fails.
240 +    ba0 = ba.BiosensorArray(1,1)
241 +    try:
242 +        fp = open(fname, "r")
243 +    except IOError:
244 +        print 'Error: Cannot open file %s for reading' % fname
245 +        return ba0
246 +    
247 +    ## Read header line.  Check number of pairs of _X and _Y labels.
248 +    txthdr = fp.readline()
249 +    cols = txthdr.count("Fc=")
250 +    xys = txthdr.count("_X\t") + txthdr.count("_Y\t")+1
251 +    if ((cols != xys) or ((cols%2) != 0)):
252 +        print "Error: This is not a valid Biosensor file."
253 +        return ba0
254 +        
255 +    ## Put data into one big text string and close.
256 +    txtfile = fp.readlines()
257 +    fp.close()
258 +    dpoints = len(txtfile)
259 +    rois = int(cols/2)
260 +    
261 +    ## Create and size ba object.
262 +    print "This Biosensor file has %i datapoints for %i ROIs." % (dpoints, rois)
263 +    ba0 = ba.BiosensorArray(rois, dpoints)
264 +    
265 +    ## Get names of ROIs from header like "Ab1 Fc=4 - 1_Y"
266 +    txthdr = txthdr.replace(" -","-").replace("- ","-") ## Remove unwanted spaces.
267 +    names = txthdr.split("\t")                          ## Use tab delimiter.
268 +    for j in range(rois):
269 +        name = names[j*2+1].replace("_Y","").strip(" -")
270 +        name = name.partition("Fc=")          ## Now a 3-tuple.
271 +        ba0.roi[j].name = name[0]             ## Text left of Fc.
272 +        ba0.roi[j].desc = ("Fc=" + name[2])   ## Text right of Fc.
273 +        
274 +    ## Move the data from txtfile to ba0.
275 +    for i, txtline in enumerate(txtfile):
276 +        x = txtline.split("\t")
277 +        for j in range(rois):
278 +            ba0.roi[j].time[i] = float(x[j*2+0])
279 +            ba0.roi[j].value[i] = float(x[j*2+1])
280 +    
281 +    return ba0
282 +    ## End of readbiosensor.
283 +
284 +
285 + def writebiosensor(ba0, fname):
286 +    """Write a ba class to a Biosensor text file."""
287 +    
288 +    ## This is like a simplified Clamp file.
289 +    fp=file(fname,'w')
290 +    ## Write sensorgram data header line.
291 +    for i,roi in enumerate(ba0.roi):
292 +        if (i>0): fp.write("\t")
293 +        name = roi.name.strip() + " " + roi.desc.strip()
294 +        fp.write("%s_X\t%s_Y" % (name, name))
295 +    ## Write sensorgram data lines. Three decimal places.
296 +    for dpoint in range(len(ba0.roi[0].time)):
297 +        fp.write("\r\n")
298 +        for i,roi in enumerate(ba0.roi):
299 +            if (i>0): fp.write("\t")
300 +            fp.write("%.3f\t%.3f" % (roi.time[dpoint], roi.value[dpoint]))
301 +    
302 +    ## Close file handle and print message.
303 +    fp.close
304 +    print "File %s saved in %s." % (fname, getcwd())
305 +    ## End of writebiosensor() function
306 +
307 +
308   def applygal(ba0, fname):
309      """Read a Gal file and apply its microarray information."""
310      print "This feature is under construction."

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines