ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/osprai/osprai/trunk/converter.py
(Generate patch)
# Line 1 | Line 1
1 < '''
1 > """
2   Converter: Perform the datafile reading, pre-processing and writing.
3 + Yuhang Wan, Christopher Lausted
4 + Last modified on 100427 (yymmdd) by YW
5  
6 < Yuhang Wan, Mar 1,2010
7 < __version__ = "1.3"
8 <
9 <                    Typical Pipeline:
10 < DatafileRead (GalfileRead, MethodRead)
11 < OutliarRemove
12 < Calibrate
13 < BackgroundSubtract
14 < CurveSplit
15 < DataOutputClamp or DataOutputBiosensor
16 <
17 < '''
6 > Typical Pipeline:
7 > >import converter as cv
8 > >dat = cv.datafile_read("example.txt")
9 > >dat = cv.outlier_remove(dat)
10 > >dat = cv.calibrate(dat)
11 > >dat = cv.background_subtract(dat)
12 > >dat = cv.curve_split(dat, 100, 800, 200)
13 > >dat = cv.data_output_clamp(dat, "/dir", 1)
14 > >dat = cv.data_output_biosensor(dat, "/dir", 1,2,3,4)
15 > >dat = cv.obj_save(dat, "saved_dataobj")
16 > """
17 > __version__ = "100427"
18  
19 + ## Import libraries.
20   import numpy as np
21   import pylab as plt
22 < import sys
22 > ##import sys
23   import xlrd
24   import time
25   import os
26   import copy
27 + import pickle
28 + import SPRdataclass as spr
29  
30 < ## import matplotlib.pyplot as plt
31 < ## import matplotlib.mlab as mlb
32 < ## import packageClamp_100210 as Pack
33 < import SPRdataclass_100301 as SPR
34 <
35 <
36 < def checkformatinput(fname):
37 <    '''Examine the format of a input .txt data file.'''
38 <    # open a file, if file doesn't exit, then exit
30 > ## Global constants and variables.
31 > FMT_UNKNOWN = 0
32 > FMT_SPRIT = 1        ## Old Plexera SPRit format.
33 > FMT_ICMTXT = 2       ## Plexera Instrument Control Module text.
34 > FMT_CLAMP34 = 3      ## Morton & Myszka CLAMP Version 3.4+
35 > FMT_DAMXLS = 4       ## Plexera Data Analysis Module Excel export.
36 > FMT_DATAOBJ = 5      ## Packed spr Data Object
37 >
38 > def check_format_input(fname):
39 >    """Examine the format of an input .txt data file."""
40 >    ## Modified 100407 CGL.
41 >    
42 >    ## Open text file. Exit if file not found.
43      try:
44          fp = open(fname, "r")
45      except IOError:
46 <        print 'Cannot open file %s for reading' % fname
47 <        sys.exit(0)
48 <    # examine the first line of the input file
49 <    # for SPRit output:"Elapsed Time (Seconds)  Average Intensity (Pixel Intensity)"
50 <    # for Plexera ICM output:
51 <    head_input1="Elapsed Time (Seconds)\tAverage Intensity (Pixel Intensity)\n"
46 >        print "Error: Unable to open file %s for reading." % fname
47 >        return
48 >    
49 >    ## Examine first line of text file to guess format.
50 >    fmt = FMT_UNKNOWN  
51 >    sprit_line1="Elapsed Time (Seconds)\tAverage Intensity (Pixel Intensity)"
52 >    clamp_line1="Vers 3.4"
53 >    line1 = fp.readline()
54 >    if (sprit_line1 in line1):
55 >        fmt = FMT_SPRIT      
56 >    elif (line1[2]=="/" and line1[5]=="/"):
57 >        ## This format has date mm/dd/yyy in first column.
58 >        fmt=FMT_ICMTXT
59 >    elif (clamp_line1 in line1):
60 >        fmt = FMT_CLAMP34
61      
44    Format_input = 0
45    # check file format  
46    Tmpstr = fp.readline()
47    if Tmpstr==head_input1:
48        Format_input=1      
49    elif Tmpstr[-1]=='\n' and Tmpstr[-2]=='\r' and Tmpstr.split(' ')[0].split('/')[2].startswith('20'):
50        Format_input=2
51    elif Tmpstr.startswith('Vers'):
52        Format_input=3
53        
62      fp.close()
63 <    return Format_input, Tmpstr
63 >    return fmt, line1
64  
65  
66 < def SPRitformat(fname):
67 <    '''Read the SPRit formatted data and reshape it.
66 > def sprit_format(fname):
67 >    """Read the SPRit formatted data and reshape it.
68      Return the data in the form of list.
69 <    '''
69 >    """
70      # open a file, if file doesn't exit, then exit
71      try:
72          fp = open(fname, "r")
73      except IOError:
74          print 'Cannot open file %s for reading' % fname
75 <        sys.exit(0)
75 >        return
76      status = {'DatainputType':'SPRit data'}
77      # skip the first line of the input file
78      Tmpstr = fp.readline()
79      # check if the second line is BEGIN
80      TmpStr = fp.readline()
81 <    if TmpStr != "BEGIN\n":
82 <        print "Second line is not Begin. Break!"
83 <        #sys.exit(0)
81 >    if ("BEGIN" not in TmpStr):
82 >        print "Warning: Second line of data file is not 'BEGIN'"
83 >        ##sys.exit(0)
84      # count the lines of each data spot
85      TmpStr = fp.readline() # skip first "0.000000e+000"        
86      num_line = 1 # so we start to count from 1
# Line 118 | Line 126
126   ##    status = {'Datainput Type':'SPRit data'}
127      dataformat = 0
128      ROInum = num_spot
129 <    dataobj=SPR.DataPass(Shaped_data_1, ROInum, dataformat)
129 >    dataobj=spr.DataPass(Shaped_data_1, ROInum, dataformat)
130      dataobj.status = {}
131      dataobj.updateStatus(**status)
132      print "There are %d ROIs, and %d lines of data" % (ROInum, num_line)  
# Line 126 | Line 134
134      return dataobj
135  
136  
137 < def PlexeraICMformat(fname):
138 <    '''Read the txt data exported from Plexera ICM software and reshape it.
137 > def plexera_icm_format(fname):
138 >    """Read the txt data exported from Plexera ICM software and reshape it.
139      Return the data in the form of list.
140 <    '''
140 >    """
141      fp=file(fname,'r')
142      Tmpdata,tmptime=[],[]
143      status = {'DatainputType':'Plexera ICM data'}
# Line 157 | Line 165
165  
166      dataformat = 0
167      ROInum = num_spot
168 <    dataobj=SPR.DataPass(Shaped_data, ROInum, dataformat)
168 >    dataobj=spr.DataPass(Shaped_data, ROInum, dataformat)
169      dataobj.status = {}
170      dataobj.updateStatus(**status)  
171      
# Line 166 | Line 174
174  
175  
176  
177 < def DAMsinglesheet(sh,DataList,ROIinfo,start,end, colsize):
178 <    '''Retrieve and shape data form one single sheet from the Spreadsheet.
177 > def dam_single_sheet(sh,DataList,ROIinfo,start,end, colsize):
178 >    """Retrieve and shape data form one single sheet from the Spreadsheet.
179      Return the data, ROI infomation in the form of list.
180 <    '''
180 >    """
181      ## and return the minimal spot Id (start_spot)
182      ## retrieve the head of the Spreadsheet, SpotList is used to store the spot Id of each sheet
183      
# Line 178 | Line 186
186      num_spot = sh.row_values(0).count('Relative Time')  
187      
188      for i in range(num_spot):
189 <        TmpHead=Head[colsize*i]      
189 >        TmpHead=unicode.encode(Head[colsize*i])
190          # now there are 5 columns for each ROI for Raw Intensity
191          # and 4 columns for each ROI for Satellite subtracted Intensity
192          DataList.append(sh.col_values(2+colsize*i,start,end))
# Line 189 | Line 197
197              key.append(j.split(': ')[0])
198              val.append(j.split(': ')[1])
199          tmpdic = dict(zip(key,val))
200 +        try:                                    # spot id
201 +            tmpdic['ID'] = int(tmpdic['ID'])    # either in form of "spot1" or "1"
202 +        except ValueError:
203 +            if str.upper(tmpdic['ID']).startswith('SPOT'):
204 +                tmpdic['ID'] = int(tmpdic['ID'][4:])
205 +            else:
206 +                print "Warning: Illegal galfile format."
207 +                print "The ID is neither 'spot1' form nor '1' form. \n", "'ID:'", tmp
208 +        
209          tmpdic['Position'] = (int(tmpdic['Block'])+1, int(tmpdic['Row'])+1, int(tmpdic['Column'])+1)
210          ROIinfo.append(tmpdic)
211  
212      return DataList,ROIinfo
213      
214 < def DAMspreadsheetformat(book):
215 <    '''Read the spreadsheet exported from Plexera DAM software and reshape it.
214 > def dam_spreadsheet_format(book):
215 >    """Read the spreadsheet exported from Plexera DAM software and reshape it.
216      Return the shaped data and ROI information.
217 <    '''
217 >    """
218      ROIinfo, newROIinfo, n_sheet = [], [], 0
219      DataList=[]
220      status = {'DatainputType':'Plexera DAM data'}
# Line 228 | Line 245
245      for i in range(book.nsheets):
246          sh=book.sheet_by_index(i)      
247          if sh.ncols!=0:        
248 <            DataList,ROIinfo = DAMsinglesheet(sh,DataList,ROIinfo,start,end, colsize)
248 >            DataList,ROIinfo = dam_single_sheet(sh,DataList,ROIinfo,start,end, colsize)
249              n_sheet=n_sheet+1
250          else:
251              break
# Line 242 | Line 259
259      newROIinfo = copy.deepcopy(ROIinfo)
260      StartspotList=np.array([i['ID'] for i in ROIinfo])
261      start_spot=min(StartspotList)
262 +
263      for i in range(num_spot):
264          tmp = ROIinfo[i]['ID']
265 <        try:
248 <            j=int(ROIinfo[i]['ID'])-(start_spot-1)
249 <        except ValueError:
250 <            ##return tmp
251 <            if str.upper(unicode.encode(tmp)).startswith(str.upper('spot')):
252 <                j=int(ROIinfo[i]['ID'][4:])-(start_spot-1)    
253 <            else:
254 <                print 'Illegal galfile format.\n', 'ID:', tmp
255 <                ##return tmp
256 <            
265 >        j=tmp-(start_spot-1)            
266          Shaped_data[j]=DataList[i+1]
267 <        newROIinfo[j]=ROIinfo[i]  
267 >        newROIinfo[j-1]=ROIinfo[i]  
268          
269      # pack the date and relative information into SPRdata obj
270      dataformat = 0
271      ROInum = num_spot
272 <    dataobj=SPR.DataPass(Shaped_data, ROInum, dataformat, newROIinfo)
272 >    dataobj=spr.DataPass(Shaped_data, ROInum, dataformat, newROIinfo)
273      dataobj.status = {}
274      dataobj.updateStatus(**status)
275      print "There are %d ROIs, and %d lines of data" % (ROInum, num_line)  
# Line 268 | Line 277
277      return dataobj
278  
279  
280 < def ReadClampData(fname):
281 <    '''Read the Clamp format data
280 > def read_clamp_data(fname):
281 >    """Read the Clamp format data
282      Return the data in the form of list.
283 <    '''
283 >    """
284      fp=open(fname,'r')
285      Tmpstr=fp.readline()
286      # examine the file head
# Line 327 | Line 336
336              sampleinfo.append(sampledic)
337              injdic = dict(zip(['ton','toff'],[float(Start[i]),float(Stop[i])]))
338              injinfo.append(injdic)
339 <    dataobj=SPR.DataPass(Data, ROInum, dataformat, ROIinfo, sampleinfo, injinfo)
339 >    dataobj=spr.DataPass(Data, ROInum, dataformat, ROIinfo, sampleinfo, injinfo)
340      dataobj.status = {}
341      dataobj.updateStatus(**status)
342      
# Line 335 | Line 344
344      
345  
346  
347 < def KeyfileRead(fkey):
347 > def keyfile_read(fkey):
348      # The Key File contains
349 <    '''Function: Read the key file for old SPR instrument.
349 >    """Function: Read the key file for old SPR instrument.
350      Input: the filename.
351      Return: the ROI information.    
352 <    '''
352 >    """
353      try:
354          fp = open(fkey, "r")
355      except IOError:
356 <        print 'Cannot open file %s for reading' % fname
357 <        sys.exit(0)
356 >        print "Cannot open file %s for reading" % fname
357 >        return
358      ROIinfo = []
359      firstline=fp.readline()     # skip the first line
360   #    Table.append([firstline.split('\t')[0],firstline.split('\t')[1],firstline.split('\t')[2],firstline.split('\t')[3]])
361      for eachline in fp:        
362          ROIdic = {}
363          tmplist = eachline.strip().split('\t')
364 <        ROIdic['ID'] = tmplist[0]       # spot id
364 >        
365 >        try:                                    # spot id
366 >            ROIdic['ID'] = int(tmplist[0])      # either in form of "spot1" or "1"
367 >        except ValueError:
368 >            if str.upper(tmplist[0]).startswith('SPOT'):
369 >                ROIdic['ID'] = int(tmplist[0][4:])
370 >            else:
371 >                print "Error: Unable to read Spot IDs"
372 >                
373          ROIdic['Name'] = tmplist[1]     # name of the protein immobilized
374          try:
375              ROIdic['Conc'] = tmplist[2]
376 <            ROIdic['Background Spot'] = int(tmplist[3])
376 >            ROIdic['Background Spot'] = map(int, tmplist[3].split(';')) # in case more than one spot set as background with ";" as SEPARATOR
377          except IndexError:
378              pass
379          ROIinfo.append(ROIdic)
# Line 365 | Line 382
382      print 'There are %d ROIs in the Key file.' % len(ROIinfo)
383      return ROIinfo
384  
385 + def keyfile_read_fake(obj):
386 +    ROIinfo = []
387 +    spot_num = obj.ROInum
388 +    for i in range(spot_num):
389 +        ROIdic = {}
390 +        ROIdic['ID'] = i+1
391 +        ROIdic['Name'] = "Ligand"+str(i+1)
392 +        ROIinfo.append(ROIdic)
393 +    return ROIinfo
394 +        
395  
396 < def GalfileRead(fname):
397 <    '''Function: Read the GAL file.
396 > def galfile_read(fname):
397 >    """Function: Read the GAL file.
398      Input: the filename.
399      Return: the ROI information.    
400 <    '''
400 >    """
401      fp=file(fname,'r')
402      ROIinfo = []
403      Tmpstr = fp.readline()      # skip the first line
# Line 397 | Line 424
424  
425          ##
426          ROIdic['Name'] = tmplist[3]
427 <        ROIdic['ID'] = tmplist[4]
427 > ##        ROIdic['ID'] = tmplist[4]
428 >        try:                                    # spot id
429 >            ROIdic['ID'] = int(tmplist[4])      # either in form of "spot1" or "1"
430 >        except ValueError:
431 >            if str.upper(tmplist[4]).startswith('SPOT'):
432 >                ROIdic['ID'] = int(tmplist[4][4:])
433 >            else:
434 >                print "Error: Unable to read Spot IDs"
435 >        
436          try:
437              ROIdic['Conc'] = tmplist[5]
438              ROIdic['Family'] = tmplist[6]
# Line 411 | Line 446
446                  Bpos = tuple(map(int, i.split(',')))
447                  Bid.append(IDcal(Bpos))
448                  ##
449 <            ROIdic['Background Spot'] = tuple(Bid)
449 >            ROIdic['Background Spot'] = Bid     ##tuple(Bid)
450          except IndexError:
451              pass
452          
453 <        if int(ROIdic['ID'][4:]) != ROIdic['IDcal']:
453 >        if ROIdic['ID'] != ROIdic['IDcal']:
454              print 'The spot ID should be consistent with the position. Please check the ID.'
455              break
456          else:
# Line 428 | Line 463
463      
464      
465  
466 < def MethodRead(fname):
467 <    '''Function: Read the analyte table.
466 > def method_read(fname):
467 >    """Function: Read the analyte table.
468      Input: the filename
469      Return: the sample information
470 <    '''
470 >    """
471      # create a concentration dictionary for the sample injected into the flowcell  
472      # export a table containing the concentration of the sample injected, and the duration
473      fp=file(fname,'r')
# Line 455 | Line 490
490          print i['Name'],'\t', i['Concentration']
491      
492      return sampleinfo
458    
493  
494 + def method_read_fake():
495 +    sampleinfo = []
496 +    sampleinfo.append({'Location':0, 'Name':0, 'Concentration':0, 'Duration':0, 'Flow Rate':0, 'Analyte Series':0, 'Buffer Blank Series':0})
497 +    return sampleinfo
498  
499 < def DatafileRead(fname):
500 <    '''Function: Read the raw data from the Plexera instrument,
499 >
500 > def datafile_read(fname):
501 >    """Function: Read the raw data from the Plexera instrument,
502          and pack the raw data (Shaped_data) into dataobject.
503          Therefore, the initial dataformat = 0
504      Input: the filename
505      Return: the packed SPR.DataPass type data object
506 <    '''
506 >    """
507      #===================check the format of the input files-------
508      if fname.split('.')[-1] == 'txt':
509      
510 <        Format_input, Firstline = checkformatinput(fname)    
511 <        if Format_input == 1:   # for SPRit file format
512 <            dataobj = SPRitformat(fname)  
510 >        Format_input, Firstline = check_format_input(fname)    
511 >        if Format_input == FMT_SPRIT:   # for SPRit file format
512 >            dataobj = sprit_format(fname)  
513              print '-'*10,' This is a SPRit data file. ','-'*10        
514              
515 <        elif Format_input == 2: # for Plexera ICM file format
516 <            dataobj = PlexeraICMformat(fname)
515 >        elif Format_input == FMT_ICMTXT: # for Plexera ICM file format
516 >            dataobj = plexera_icm_format(fname)
517              print '-'*10,' This is a Plexera ICM exported data file. ','-'*10
518  
519 <        elif Format_input == 3:         # for Clamp datafile format
520 <            dataobj = ReadClampData(fname)
519 >        elif Format_input == FMT_CLAMP34:         # for Clamp datafile format
520 >            dataobj = read_clamp_data(fname)
521              print '-'*10,' This is a Clamp file. ','-'*10
522          
523          else:
524              print '-'*10,' unreadable file input! ','-'*10
525 <            sys.exit(0)
525 >            return
526          
527 <        if Format_input == 1 or Format_input == 2:
527 >        if Format_input == FMT_SPRIT or Format_input == FMT_ICMTXT:
528              flag = str.upper(raw_input('Load the key file? (y/n): '))
529              if flag == 'Y':
530                  fkey = raw_input('Input the path of the key file: ')
531 <                ROIinfo = KeyfileRead(fkey)
531 >                ROIinfo = keyfile_read(fkey)
532                  dataobj.updateROIinfo(ROIinfo)        
533 +            else:
534 +                dataobj.updateROIinfo(keyfile_read_fake(dataobj))  
535      
536      elif fname.split('.')[-1] == 'xls':
537          # for the DAM Spreadsheet file format
538          print '-'*10,' This is a Plexera DAM exported data file. ','-'*10
539 +        Format_input = FMT_DAMXLS
540          book = xlrd.open_workbook(fname)
541          #shape the data in the Spreadsheet, whether single sheet or multiple sheets
542 <        dataobj=DAMspreadsheetformat(book)
542 >        dataobj=dam_spreadsheet_format(book)
543          flag = str.upper(raw_input('Load the gal file? (y/n): '))
544          if flag == 'Y':
545              fgal = raw_input('Input the path of the gal file: ')
546 <            ROIinfo = GalfileRead(fgal)
546 >            ROIinfo = galfile_read(fgal)
547              dataobj.updateROIinfo(ROIinfo)
548 +            
549 +    elif fname.split('.')[-1] == 'obj':
550 +        # for the saved data object file format
551 +        print '-'*10,' This is a saved data object file. ','-'*10
552 +        Format_input = FMT_DATAOBJ
553 +        fp = file(fname, 'r+')
554 +        dataobj = pickle.load(fp)  
555 +        fp.close()        
556 +    
557      flag = str.upper(raw_input('Load the experimental analyte file? (y/n): '))
558      if flag == 'Y':
559          fprotocol = raw_input('Input the path of the analyte table: ')
560 <        sampleinfo = MethodRead(fprotocol)
560 >        sampleinfo = method_read(fprotocol)
561          dataobj.updateSampleinfo(sampleinfo)
562 <            
562 >    elif Format_input != FMT_CLAMP34 and Format_input != FMT_DATAOBJ:    
563 >        # clamp data and obj data may contain the experimental protocol already
564 >        dataobj.updateSampleinfo(method_read_fake())
565 >
566      return dataobj
567      
568  
569 <
516 < def OutliarRemove(obj):
569 > def outlier_remove(obj):
570      ##dataobj.viewAll()
571 <    '''Function: Remove the noisy area that you want to get rid of.
571 >    """Function: Remove unwanted data points from noisy periods.
572      Return: the data object with clean data
573 <    '''
573 >    """
574      dataobj = copy.deepcopy(obj)
575      print ('Select the time area that you want to remove.')
576      tmpstr = raw_input('in the format of "200:240, 500:510" :')
# Line 538 | Line 591
591   ##        tmplist = tmplist
592      dataobj.data = data
593   #    dataobj.status = 'Outliar removed'
594 <    dataobj.updateStatus(OutliarRemove=True)
594 >    dataobj.updateStatus(outlier_remove=True)
595      return dataobj
596  
597 < def Calibrate(obj):
598 <    '''Function: Calibrate the Intensity response.
597 > def calibrate(obj):
598 >    """Function: calibrate the Intensity response.
599      Return: the data object with calibrated data
600      Note: at present, this function is valid only when
601          the whole procedure includes calibration precedure.
602 <    '''
602 >    """
603      dataobj = copy.deepcopy(obj)
604      data = dataobj.data
605      caldata = copy.deepcopy(data)
# Line 568 | Line 621
621              offset = s1- slope*y1
622              caldata[i+1] = slope*y+offset
623      dataobj.data = caldata    
624 <    dataobj.updateStatus(Calibrate=True)
624 >    dataobj.updateStatus(calibrate=True)
625      return dataobj
626  
627 < def getbackgroundvalue(obj,bgids):
628 <    '''Get the averaged value of background spots for each ROI.
576 <    '''
627 > def get_background_value(obj,bgids):
628 >    """Get the averaged value of background spots for each ROI."""
629      bgsignal = obj.data[0]*0
630      for j in bgids:
631 <        if j == obj.ROIinfo[j-1]['ID']:
631 >        if j == int(obj.ROIinfo[j-1]['ID']):    # in case the ID in ROIinfo is string type
632              bgsignal = bgsignal + obj.data[j]
633                  
634 <                
583 <        elif j != obj.ROIinfo[j-1]['ID']:
634 >        elif j != int(obj.ROIinfo[j-1]['ID']):
635              for n,eachspot in enumerate(obj.ROIinfo):
636 <                if j == eachspot['ID']:
636 >                if j == int(eachspot['ID']):
637                      bgsignal = bgsignal + obj.data[n+1]
638 +                    
639      bgsignal = bgsignal/len(bgids)
640      return bgsignal
641      
642  
643 < def BackgroundSubtract(obj, *bgspot):
644 <    '''Function: Perform the Background subtraction for the UNSPLIT curve.
643 > def background_subtract(obj, *bgspot):
644 >    """Function: Perform the Background subtraction for the UNSPLIT curve.
645      Input besides "obj" is the id of the spot taken as background.
646          The following inputs are acceptable:
647 <        1. BackgroundSubtract(obj): the default background in Galfile
647 >        1. background_subtract(obj): the default background in Galfile
648              will be subtracted.
649 <        2. BackgroundSubtract(obj, 1, 6): the average value of spot1
649 >        2. background_subtract(obj, 1, 6): the average value of spot1
650              and spot6 will be subtracted.
651 <    '''
651 >    """
652      dataobj = copy.deepcopy(obj)
653      ROIinfo = obj.ROIinfo
654      data = dataobj.data
# Line 609 | Line 661
661          #are to be subtracted.
662              for i in range(1,dataobj.ROInum+1):
663                  bgids = ROIinfo[i-1]['Background Spot']
664 <                bgsignal = getbackgroundvalue(dataobj,bgids)
664 >                bgsignal = get_background_value(dataobj,bgids)
665                  newdata[i] = data[i]-bgsignal
666              # update the status of the data object.
667              dataobj.updateStatus(BackgroundType='Default in Gal')
668 +            print "The background spot embodied in gal is used."
669                  
670          else:
671          # The average of the manually input background spots
672          #are to be subtracted.
673              for i in range(1,dataobj.ROInum+1):
674 <                bgsignal = getbackgroundvalue(dataobj,bgspot)
674 >                bgsignal = get_background_value(dataobj,bgspot)
675                  newdata[i] = data[i]-bgsignal
676 <            dataobj.updateStatus(BackgroundType='Manually choosen')
676 >            dataobj.updateStatus(BackgroundType='Manually chosen')
677 >            print "The background spot is manually chosen."
678 >            
679          dataobj.data = newdata
680 <        dataobj.updateStatus(BackgroundSubtraction=True)
680 >        dataobj.updateStatus(background_subtraction=True)
681          return dataobj
682          
683      else:
684          print 'The Background Subtraction should be run at the beginning, with the UNsplit curve.'    
685      return
686  
687 < def CurveSplit(obj, t_before, t_after, *t_inj):
688 <    '''Function: Split the whole curve that contains several injections
687 > def curve_split(obj, t_before, t_after, *t_inj):
688 >    """Function: Split the whole curve that contains several injections
689          into pieces.
690          The sample information will be checked during the split,
691              if the injection number in sampleinfo is not consistant
# Line 642 | Line 697
697          t_inj: the exact start time for each injection
698      Return: the data object with splitted data, and updated sample
699          information
700 <    '''
700 >    """
701      dataobj = copy.deepcopy(obj)
702      ROInum = dataobj.ROInum
703      t=dataobj.data[0]
# Line 667 | Line 722
722      for i in range(np.shape(Split_data)[0]):
723          Split_data[i]=Split_data[i]-Split_data[i][0]
724      
725 <    newsampleinfo, injectinfo = checksampleinfo(dataobj,t_before, t_after, t_inj)
725 >    newsampleinfo, injectinfo = check_sample_info(dataobj,t_before, t_after, t_inj)
726      if newsampleinfo != None :        
727          dataobj.data = Split_data
728          dataobj.updateStatus(DataSplit=True)
# Line 682 | Line 737
737      return
738      
739      
740 < def checksampleinfo(dataobj, t_before, t_after, t_inj):
741 <    '''Check if the sample information consistent with the injection
740 > def check_sample_info(dataobj, t_before, t_after, t_inj):
741 >    """Check if the sample information consistent with the injection
742          parameter input. If not, help the user to update sample information
743          for the splitted curve.
744 <    '''
744 >    """
745      injectinfo = []
746      if len(dataobj.sampleinfo) == len(t_inj):
747          for i,j in enumerate(t_inj):                
# Line 694 | Line 749
749          return dataobj.sampleinfo, injectinfo
750              
751      elif len(dataobj.sampleinfo) > len(t_inj):
752 <        print '''    The number of injection you just input does not match that in sample infomation.
753 <    If you just want to split part of the curve, please follow the following direction to update the sampleinfo in the new object.'''
752 >        print """    The number of injection you just input does not match that in sample infomation.
753 >    If you just want to split part of the curve, please follow the following direction to update the sampleinfo in the new object."""
754          choose = str.upper(raw_input('Continue? (y/n)'))
755          if choose == 'Y':
756              newsampleinfo, injectinfo = [], []
# Line 731 | Line 786
786          return
787      
788      
789 < def writedata(Head,data_out,ROI_out,DataDir,output_format):
790 <    '''Write the data into a txt file.
791 <    '''
792 <    # create a nowfolder with time stamp to save the data
789 > def write_data(Head,data_out,ROI_out,DataDir,output_format):
790 >    """Write the data into a txt file.
791 >    """
792 >    # Modified 100405 CGL
793 >    # create a newfolder with time stamp to save the data
794      if output_format=='B':
795          ftitle='biosensor'
796          Tmpstr = str(ROI_out).replace(', ','_')
# Line 744 | Line 800
800          foutname='clamp-'+str(ROI_out)+'.txt'
801      else:
802          ftitle=''
803 <    #  change to the Root dir
804 <    os.chdir('..')        
805 <    try:
806 <        os.makedirs(DataDir)
807 <    except WindowsError:
808 <        pass
809 <    os.chdir(DataDir)
803 >        
804 >    # Change directories, if requested.
805 >    if (DataDir != ""):
806 >        os.chdir('..')        
807 >        try:
808 >            os.makedirs(DataDir)
809 >        ##except WindowsError:
810 >        except OSError:
811 >            pass
812 >        os.chdir(DataDir)
813      
814      np.savetxt('temp.txt',np.transpose(data_out),fmt='%f')
815      fdt=file('temp.txt','r')
# Line 767 | Line 826
826      fdt.close()
827      fout.close()
828      print '-------The path of the exported data is "', os.getcwd(),'"-------'
829 +    return
830      
831      
832 < def DataOutputBiosensor(obj, DataDir, *ROI_out):
833 <    '''Export the data into a biosensor format txt file,
832 > def data_output_biosensor(obj, DataDir, *ROI_out):
833 >    """Export the data into a biosensor format txt file,
834          which can be operated in Scrubber.
835      Input: the data object, the output path, the ROIs selected to export
836 <    '''
836 >    """
837      # initialize the data output matrix, which will contain 'len(spot_out)*2)*n_inj' columns
838      dataobj = copy.deepcopy(obj)
839      data = dataobj.data
# Line 787 | Line 847
847      for n,i in enumerate(ROI_out):
848          new_ROIinfo.append(ROIinfo[i-1])        ##the new ROI info
849          if i != ROIinfo[i-1]['IDcal']:
850 <            # check if the ID number matchs position
850 >            # check if the ID number matches position
851              print n,i, ROIinfo[i-1]
852              return
853          else:
# Line 810 | Line 870
870      dataobj.updateStatus(Head=head1)    
871      dataobj.updateROIinfo(new_ROIinfo)
872      
873 <    writedata(head1,data_out,ROI_out,DataDir,'B')
873 >    write_data(head1,data_out,ROI_out,DataDir,'B')
874      
875      return dataobj
876      
877 < def DataOutputClamp(obj, DataDir, ROI_out):
878 <    '''Export the data into a Clamp format txt file,
877 > def data_output_clamp(obj, DataDir, ROI_out):
878 >    """Export the data into a Clamp format txt file,
879          which can be operated in Clamp.
880      Input: the data object, the output path, the selected ROI
881 <    '''
881 >    """
882      # initialize the data output matrix, which will contain '2*n_inj' columns
883      dataobj = copy.deepcopy(obj)
884      data = dataobj.data
885      n_inj = len(dataobj.sampleinfo)
886 +    n_inj = max(n_inj, 1)  # Must have at least one injection.
887      ROIinfo = dataobj.ROIinfo
888      ROInum = dataobj.ROInum
889      sampleinfo = dataobj.sampleinfo
890      
891      data_out = np.zeros((2*n_inj,np.shape(data)[1]))
892 <    if ROI_out != ROIinfo[ROI_out-1]['IDcal']:
892 > ##  if ROI_out != ROIinfo[ROI_out-1]['IDcal']:
893 >    if (ROI_out != int(ROIinfo[ROI_out-1]['ID'])):
894          print 'Please check the ROI information.'
895          print ROI_out, ROIinfo[ROI_out-1]
896          return
# Line 860 | Line 922
922      Clamphead=Clamphead+'\n'+head1
923      
924      dataobj.data = data_out
925 <    dataobj.dataformat = 4      # biosensor dataformat
925 >    dataobj.dataformat = 4      # clamp dataformat
926      dataobj.updateStatus(DataType='Clamp data')
927      dataobj.updateStatus(Head=Clamphead)    
928      dataobj.updateROIinfo(ROIinfo[ROI_out-1])
929      
930 <    writedata(Clamphead,data_out,ROI_out,DataDir,'C')
930 >    write_data(Clamphead,data_out,ROI_out,DataDir,'C')
931      
932      return dataobj
933   ##    return data_out,Clamphead
934  
935 + def obj_save(obj, foutname):
936 +    foutname = foutname + '.obj'        # the file extension of the saved data object is .obj
937 +    fout = file(foutname, 'w+')
938 +    pickle.dump(obj, fout)
939 +    fout.close()
940 +    print '-------The path of the exported obj is "', os.getcwd(),'"-------'
941 +    return
942 +
943 + if (__name__ == '__main__'):
944 +    # Print a short message if this module is run like an app.
945 +    print "This is the OSPRAI file type conversion tool."
946  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines