-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmsPane.lua
342 lines (308 loc) · 11.5 KB
/
msPane.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
-- Copyright (c) 2016 Thermo Fisher Scientific
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
-- (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
-- merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished
-- to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-- msPane.lua
-- A zPane with the ability to plot spectra and chromatograms
-- Load necessary libraries
local configure = require("configure")
local zPane = require("zPane")
-- Get assemblies
luanet.load_assembly ("System.Drawing")
luanet.load_assembly ("ZedGraph")
-- Get constructors
local plotCtor = luanet.import_type("ZedGraph.ZedGraphControl")
local pointPairListCtor = luanet.import_type("ZedGraph.PointPairList")
local GraphPane = luanet.import_type("ZedGraph.GraphPane")
-- Get enumerations
local Color = luanet.import_type("System.Drawing.Color")
local SymbolType = luanet.import_type("ZedGraph.SymbolType") -- This is an enum
-- local variables
-- forward declarations for local functions
-- local functions
-- Only non-chromatogram mode is spectrum, so for now
-- just compare against it.
local function isChromatogram(mode)
return mode ~= "spectrum"
end
local function isSpectrum(mode)
return mode == "spectrum"
end
local function isXIC(mode)
return mode == "xic"
end
-- Start of the zPane object
local msPane = {}
msPane.__index = msPane
setmetatable(msPane, {
__index = zPane, -- this is the inheritance
__call = function (cls, ...)
local self = setmetatable({}, cls)
self:_init(...)
return self
end,})
---Create a new object of the class
function msPane:_init(args)
args = args or {}
zPane._init(self, args)
self.mode = args.mode or "tic"
self.rawFile = args.rawFile
self.scanNumber = args.scanNumber or 1
self.startRT = 0
self.stopRT = 1
self.filter = ""
self.massRange = ""
self.paneControl.Title.Text = "msPane"
-- Add properties, zPane already called properties.Inherit()
local toolTipString = "spectrum, tic, bpc, xic"
self:AddProperty({key = "mode", label = "Mode", toolTip = toolTipString,
options = {"spectrum", "tic", "bpc", "xic"}})
local firstSN = self.rawFile.FirstSpectrumNumber
local lastSN = self.rawFile.LastSpectrumNumber
self:AddProperty({key = "startRT", label = "Start", modeTest = isChromatogram})
self:AddProperty({key = "stopRT", label = "End", modeTest = isChromatogram})
toolTipString = string.format("%d - %d", firstSN, lastSN)
self:AddProperty({key = "scanNumber", label = "Scan Number",
min = firstSN, max = lastSN,
modeTest = isSpectrum, toolTip = toolTipString})
self:AddProperty({key = "filter", label = "Filter"})
self:AddProperty({key = "massRange", label = "Mass Range", modeTest = isXIC})
if args.skipDraw then return end
if self.mode ~= "spectrum" then
self:PlotChromatogram()
else
self:PlotSpectrum()
end
end
function msPane:GetChromatogramTitle(args)
args = args or {}
if args.title then return args.title end
local mode = args.mode or self.mode or "tic" -- TIC is default
local title = mode -- Start with mode
if mode == "xic" then
title = string.format("%s: %s %s", title, self.filter, self.massRange)
end
return title
end
function msPane:GetPropertyTitle()
if self.mode == "spectrum" then
return "Spectrum"
else
return "Chromatogram"
end
end
-- Required Arguments:
-- Optional Arguments:
-- rawFile: overrides self.rawFile
-- scanNumber
function msPane:GetSpectrum(args)
-- if the spectrum is passed, just return it
if args.spectrum then return args.spectrum end
local rawFile = args.rawFile or self.rawFile
if not rawFile then
print ("msPane:GetSpectrum() No rawFile available")
return nil
end
self.scanNumber = args.scanNumber or self.scanNumber
if self.scanNumber < rawFile.FirstSpectrumNumber or
self.scanNumber > rawFile.LastSpectrumNumber then
print (string.format("msPane:GetSpectrum() scanNumber %d out of range", self.scanNumber))
return nil
end
-- Get the spectrum and add some information that will help in plotting
local spectrum = rawFile:GetSpectrum(self.scanNumber)
spectrum.scanNumber = self.scanNumber
local filter = rawFile:GetScanFilter(self.scanNumber)
self:SetSpectrumMassRange(spectrum, filter)
if filter:find(" c ") then
spectrum.IsCentroid = true -- Mark as centroid spectrum
end
return spectrum
end
function msPane:SetPropertiesFinalize()
if self.mode == "spectrum" then
self:PlotSpectrum()
else
self:PlotChromatogram()
end
end
-- Required arguments
-- scanNumber
-- Optional arguments
-- rawFile - overrides self.rawFile
-- title - overrides automatic title
function msPane:GetSpectrumTitle(args)
if args.title then return args.title end
local rawFile = args.rawFile or self.rawFile
if not rawFile or not args.scanNumber then return "" end
return string.format("#%d: %0.3f min %s", args.scanNumber,
rawFile:GetRetentionTime(args.scanNumber),
rawFile:GetScanFilter(args.scanNumber))
end
-- This uses : syntax, since it's called by the page
function msPane:KeyDownCB(sender, args)
if self.mode == "spectrum" then
local newScanNumber = self.page:ChangeScanNumber(args, self.rawFile, self.scanNumber, self.filter)
if newScanNumber then
self:PlotSpectrum({scanNumber = newScanNumber})
end
-- no chromatogram key press callback yet
end
return
end
-- Alter the spectrum so that it looks like a profile spectrum
-- by adding zeros for each point
function msPane:PlotCentroidSpectrum(args)
args = args or {}
local spectrum = args.spectrum
if not spectrum then return end
if spectrum.scanNumber then
self.scanNumber = spectrum.scanNumber
args.scanNumber = spectrum.scanNumber -- Help with title
end
local pane = self.paneControl
pane.Title.Text = self:GetSpectrumTitle(args)
pane.XAxis.Title.Text = "m/z"
pane.YAxis.Title.Text = "Intensity"
-- Add a StickGraph to the pane
local stickCurve = self.centroid or
pane:AddStick("", pointPairListCtor(), configure.spectrumColor)
self.centroid = stickCurve -- In case is didn't already exist
self:Clear() -- Clear all curves
for index, point in ipairs(spectrum) do
if not point.Mass or not point.Intensity then
print ("Bad point at index ", index)
else
stickCurve:AddPoint(point.Mass, point.Intensity)
end
end
pane.XAxis.Scale.Min = spectrum.firstMass
pane.XAxis.Scale.Max = spectrum.lastMass
pane.YAxis.Scale.MinAuto = true
pane.YAxis.Scale.MaxAuto = true
-- Refresh the graph
local plotControl = self.plotControl
if plotControl and not args.skipDraw then
--print ("Refreshing Pane")
plotControl:AxisChange()
plotControl:Invalidate()
end
self:UpdatePropertyForm()
self:PushProperties()
end
-- Optional arguments:
-- chromatogram: Plot the specified chromatogram
-- rawFile: overrides pane's rawFile
-- title: overrides GetChromatogramTitle()
function msPane:PlotChromatogram(args)
args = args or {}
local chromatogram, rawFile
self.mode = args.mode or self.mode
self.filter = args.filter or self.filter
self.massRange = args.massRange or self.massRange
if args.chromatogram then
chromatogram = args.chromatogram
else
rawFile = args.rawFile or self.rawFile
if not rawFile then
print ("No raw file available")
return nil
end
local chroType
if self.mode == "xic" then
chroType = 0
elseif self.mode == "bpc" then
chroType = 2
else -- "tic" is default
chroType = 1
end
chromatogram = rawFile:GetChroData({ -- see MsFileReader doc for complete details
Type = chroType, -- 0 Mass Range, 1 TIC, Base Peak 2
Operator = 0, -- 0 None, 1 Minus, 2 Plus
Type2 = 0, -- 0 Mass Range, 1 Base Peak
Filter = self.filter, -- Scan Filter
MassRange1 = self.massRange, -- Mass Range for chro1
MassRange2 = nil, -- Mass Range for chro2
SmoothingType = 0, -- 0 None, 1 Boxcar, 2 Gaussian
SmoothingValue = 3, -- Odd value between 3-15
Delay = 0,
StartTime = args.startTime or 0,
EndTime = args.endTime or 0,
})
end
local pane = self.paneControl
self:Clear() -- Clear all data from pane
pane.Title.Text = self:GetChromatogramTitle(args)
pane.XAxis.Title.Text = "Retention Time (min)"
pane.YAxis.Title.Text = "Intensity"
local thisColor = args.color or configure.chromatogramColor
self:AddXYTable({data = chromatogram, xKey = "Time", yKey = "Intensity", color = thisColor })
self:UpdatePropertyForm()
self:PushProperties()
end
function msPane:PlotProfileSpectrum(args)
args = args or {}
local spectrum = args.spectrum
if not spectrum then return end
if spectrum.scanNumber then
self.scanNumber = spectrum.scanNumber
args.scanNumber = spectrum.scanNumber -- Help with title
end
local pane = self.paneControl
self:Clear() -- Clear all data from the pane
pane.Title.Text = self:GetSpectrumTitle(args)
pane.XAxis.Title.Text = "m/z"
pane.YAxis.Title.Text = "Intensity"
self:AddXYTable({data = spectrum, xKey = "Mass", yKey = "Intensity",
xMin = spectrum.firstMass, xMax = spectrum.lastMass,
color = configure.spectrumColor})
self:UpdatePropertyForm()
self:PushProperties()
end
function msPane:PlotSpectrum(args)
args = args or {}
args.spectrum = self:GetSpectrum(args)
if not args.spectrum then
print ("No Spectrum Available")
return nil
end
if args.spectrum.IsCentroid then
return self:PlotCentroidSpectrum(args)
else
return self:PlotProfileSpectrum(args)
end
end
function msPane:SetRawFile(rawFile)
self.rawFile = rawFile
end
function msPane:SetSpectrumMassRange(spectrum, filter)
if not spectrum or not filter then return spectrum end
spectrum.firstMass = tonumber(filter:match("%[(%d+%.%d+)%-"))
spectrum.lastMass = tonumber(filter:match("%-(%d+%.%d+)%]"))
return
end
-- User has clicked on this pane with a shift
function msPane:ShiftClick(pointF, activePane)
if self == activePane then return end -- Do nothing if the active pane
-- Get nearest point
local paneControl = self.paneControl
local curveList = paneControl.CurveList
local index, curve = paneControl:FindNearestPoint(pointF, curveList)
local point = curve[index]
--print ("Point is ", point.X, point.Y)
if isChromatogram(self.mode) then
activePane:PlotSpectrum({scanNumber = activePane.rawFile:GetScanNumberFromRT(point.X)})
else
activePane:PlotChromatogram({mode = "xic", massRange = string.format("%0.3f", point.X)})
end
end
return msPane