1 # Copyright (C) 2009-2011 Allen Chen
2 # Massimo Sandal <devicerandom@gmail.com>
3 # W. Trevor King <wking@drexel.edu>
5 # This file is part of Hooke.
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.
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.
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/>.
21 """Driver for mcs fluorescence files.
31 class mcsDriver(lib.driver.Driver):
33 def __init__(self, filename):
35 Open the RED (A) ones; the BLUE (D) mirror ones will be automatically opened
37 #obtain name of blue files
39 if othername[-8]=='a': #fixme: how to make it general? (maybe should not be in driverspace but in environment...)
42 othername=''.join(oth)
43 self.filename=filename
44 self.othername=othername
46 #print self.filename, self.othername
48 self.filedata=open(filename,'rb')
49 self.reddata=self.filedata.read()
52 self.filebluedata=open(othername,'rb') #open also the blue ones
53 self.bluedata=self.filebluedata.read()
54 self.filebluedata.close()
58 self.filebluedata.close()
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
66 extension = lib.curve.Curve()
67 retraction = lib.curve.Curve()
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
90 plot = lib.plot.Plot()
91 plot.title = os.path.basename(self.filename)
92 plot.curves.append(extension)
93 plot.curves.append(retraction)
99 if os.path.isdir(path):
101 if self.filename[-3:].lower()=='mcs':
106 def read_file(self, raw_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
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