931002ab1cf9dec055bdf1be31f487e760587266
[hooke.git] / hooke / driver / mcs.py
1 # Copyright (C) 2009-2012 Allen Chen
2 #                         Massimo Sandal <devicerandom@gmail.com>
3 #                         W. Trevor King <wking@drexel.edu>
4 #
5 # This file is part of Hooke.
6 #
7 # Hooke is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
11 #
12 # Hooke is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
15 # Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with Hooke.  If not, see
19 # <http://www.gnu.org/licenses/>.
20
21 """Driver for mcs fluorescence files.
22 """
23
24 import os.path
25
26 import lib.curve
27 import lib.driver
28 import lib.plot
29 import struct
30
31 class mcsDriver(lib.driver.Driver):
32
33     def __init__(self, filename):
34         '''
35         Open the RED (A) ones; the BLUE (D) mirror ones will be automatically opened
36         '''
37         #obtain name of blue files
38         othername=filename
39         if othername[-8]=='a': #fixme: how to make it general? (maybe should not be in driverspace but in environment...)
40             oth=list(othername)
41             oth[-8]='d'
42             othername=''.join(oth)
43         self.filename=filename
44         self.othername=othername
45
46         #print self.filename, self.othername
47
48         self.filedata=open(filename,'rb')
49         self.reddata=self.filedata.read()
50         self.filedata.close()
51
52         self.filebluedata=open(othername,'rb') #open also the blue ones
53         self.bluedata=self.filebluedata.read()
54         self.filebluedata.close()
55
56     def close_all(self):
57         self.filedata.close()
58         self.filebluedata.close()
59
60     def default_plots(self):
61         #TODO: rename blue and red data to something more appropriate if possible
62         red_data=self.read_file(self.reddata)
63         blue_data=self.read_file(self.bluedata)
64         blue_data=[-1*float(item) for item in blue_data] #visualize blue as "mirror" of red
65
66         extension = lib.curve.Curve()
67         retraction = lib.curve.Curve()
68
69         extension.color = 'red'
70         extension.label = 'extension'
71         extension.style = 'plot'
72         extension.title = 'Force curve'
73         #FIXME: if there's an header saying something about the time count, should be used
74         #TODO: time is not really a unit
75         extension.units.x = 'time'
76         extension.units.y = 'count'
77         extension.x = range(len(red_data))
78         extension.y = red_data
79         retraction.color = 'blue'
80         retraction.label = 'retraction'
81         retraction.style = 'plot'
82         retraction.title = 'Force curve'
83         #FIXME: if there's an header saying something about the time count, should be used
84         #TODO: time is not really a unit
85         retraction.units.x = 'time'
86         retraction.units.y = 'count'
87         retraction.x = range(len(blue_data))
88         retraction.y = blue_data
89
90         plot = lib.plot.Plot()
91         plot.title = os.path.basename(self.filename)
92         plot.curves.append(extension)
93         plot.curves.append(retraction)
94
95         plot.normalize()
96         return plot
97
98     def is_me(self):
99         if os.path.isdir(path):
100             return False
101         if self.filename[-3:].lower()=='mcs':
102             return True
103         else:
104             return False
105
106     def read_file(self, raw_data):
107         real_data=[]
108         intervalsperfile=struct.unpack('h', raw_data[10:12])[0] #read in number of intervals in this file
109                                                                 #this data is contained in bit offset 10-12 in mcs file
110         #see http://docs.python.org/library/struct.html#module-struct for additional explanation
111
112         numbytes=len(raw_data) #data is stored in 4-byte chunks, starting with pos 256
113         for j in range(0,intervalsperfile): #read in all intervals in file
114             temp=raw_data[256+j*4:256+j*4+4]    #data starts at byte offset 256
115             real_data.append(struct.unpack('i', temp)[0]) #[0] because it returns a 1-element tuple
116         return real_data