-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tartare.lua
615 lines (559 loc) · 20.5 KB
/
Tartare.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
-- Copyright (c) 2018 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.
-- Tartare.lua
-- This is a framework to recreate RawMeat functionality
-- Load necessary libraries
local RawFile = require("LuaRawFile")
local menu = require("menu")
local mdiNoteBook = require("mdiNoteBook")
local textPage = require("textPage")
-- Get constructors
local Button = luanet.import_type("System.Windows.Forms.Button")
local OpenDialog = luanet.import_type("System.Windows.Forms.OpenFileDialog")
local FolderBrowserDialog = luanet.import_type("System.Windows.Forms.FolderBrowserDialog")
local ProgressBar = luanet.import_type("System.Windows.Forms.ProgressBar")
local Form = luanet.import_type("System.Windows.Forms.Form")
local Label = luanet.import_type("System.Windows.Forms.Label")
local CheckedListBox = luanet.import_type("System.Windows.Forms.CheckedListBox")
local MainMenu = luanet.import_type("System.Windows.Forms.MainMenu")
local MenuItem = luanet.import_type("System.Windows.Forms.MenuItem")
-- Get enumerations
local DialogResult = luanet.import_type("System.Windows.Forms.DialogResult")
local ProgressBarStyle = luanet.import_type("System.Windows.Forms.ProgressBarStyle")
local ContentAlignment = luanet.import_type("System.Drawing.ContentAlignment")
local FormStartPosition = luanet.import_type("System.Windows.Forms.FormStartPosition")
-- forward declarations for local functions
local MenuCheck, ProcessList, ShowMenu, TrueFunction
local SelectAllCB, UnselectAllCB
-- Local variables
local cycleNumber = 1
local activeReportDialog = {}
local progressDialog = {}
local cancelProcessing = false
local registeredReports = {}
local errorMessage
-- Create the table for the Tartare module
-- It has one public function, which is register()
local Tartare = {}
-- Make a bin averaged plot of supplied data
-- Normally used to average over retention time
function Tartare.averagePlot(args)
args = args or {}
if not args.pane or not args.data or not args.yKey then
print ("Usage: Tartare.averagePlot({data = dataTable, pane = zPane, yKey = string, [xKey = string, averageWidth = x, filterFunction = function]})")
return nil
end
local pane = args.pane
local data = args.data
local yKey = args.yKey
local xKey = args.xKey or "rt" -- Retention tme is default
local averageWidth = args.averageWidth or 1 -- RT is in minutes
local filterFunction = args.filterFunction or TrueFunction
for fileIndex, result in ipairs(data) do
-- Make Time Plot Data Table
local plotData = {}
local sum = 0
local count = 0
local nextPoint = averageWidth
local lastEntry = result[1]
for _, entry in ipairs(result) do
if filterFunction(entry) then
if lastEntry[xKey] > entry[xKey] then
print (string.format("tartare:averagePlot(): Data must be sorted on key '%s'", xKey))
return
end
if entry[xKey] > nextPoint then
if count > 0 then
table.insert(plotData, {x = entry[xKey], y = sum / count})
end
nextPoint = entry[xKey] + averageWidth
sum = 0
count = 0
end
sum = sum + entry[yKey]
count = count + 1
end
end
-- Include the last time window
if count > 0 then
table.insert(plotData, {x = nextPoint, y = sum / count})
end
-- Plot the data
pane:AddXYTable({data = plotData, xKey = "x", yKey = "y",
index = fileIndex, name = result.fileName})
Application.DoEvents()
end
end
-- Make a histogram plot of supplied data
function Tartare.histogram(args)
args = args or {}
if not args.pane or not args.data or not args.key then
print ("Usage: Tartare.histogram({data = dataTable, pane = zPane, key = string [, filterFunction = function})")
return nil
end
local pane = args.pane
local data = args.data
local key = args.key
local filter = args.filterFunction or TrueFunction
local maxValue = args.maxValue or -1
local minValue = args.minValue or 1e10
if maxValue == -1 then
for _, result in ipairs(data) do
for _, entry in ipairs(result) do
if filter(entry) then
maxValue = math.max(maxValue, entry[key])
minValue = math.min(minValue, entry[key])
end
end
end
end
if args.logScale then
maxValue = math.log10(maxValue)
minValue = math.log10(minValue)
end
local xRange = maxValue - minValue
-- Special case of everything in one bin
if xRange == 0 then
if minValue == 0 then
maxValue = 1
else
minValue = 0
end
xRange = maxValue - minValue
end
local binCount
if args.integer then
binCount = math.floor(maxValue)
else
binCount = args.binCount or 100
end
for fileIndex, result in ipairs(data) do
-- Make Histogram Data Table
local histData = {}
for n = 0, binCount + 1 do -- Include a zero bin and sentry on top
local xValue = minValue + (xRange * n) / binCount
if args.integer then xValue = math.floor(xValue) end
table.insert(histData, {x = xValue, count = 0})
end
for _, entry in ipairs(result) do
if filter(entry) then
-- Add one because of zero bin in first position
local value = entry[key]
if args.logScale then value = math.log10(value) end
local binIndex = math.ceil((value - minValue) / xRange * binCount) + 1
if not histData[binIndex] then
print (string.format("Bin Count: %d Bad Index: %d", #histData, binIndex))
print (string.format("MinValue: %f MaxValue: %f", minValue, maxValue))
print (string.format("This Value: %f XRange: %f", value, xRange))
return
end
histData[binIndex].count = histData[binIndex].count + 1
end
end
-- Remove empty bins
local i = 1
while i <= #histData do
if histData[i].count == 0 then
table.remove(histData, i)
else
i = i + 1
end
end
-- Plot the data
pane:AddXYTable({data = histData, xKey = "x", yKey = "count",
seriesType = args.seriesType, index = fileIndex, name = result.fileName})
Application.DoEvents()
end
end
-- Register a table that will generate a page in the Tartare report notebook
function Tartare.register(reportTable)
if type(reportTable.name) ~= "string" then
print ("Tartare.register: Report is missing name")
return nil
end
local requiredFunctions = {"processFile", "generateReport"}
for _, functionName in ipairs(requiredFunctions) do
if type(reportTable[functionName]) ~= "function" then
print (string.format("Tartare.register: %s is missing %s function", reportTable.name, functionName))
return nil
end
end
-- If this is the first time register is called, display the menu options
if #registeredReports == 0 then
ShowMenu()
end
table.insert(registeredReports, reportTable)
-- Add this report to the modules sub-menu
-- Careful of the syntax here, we want nil to show up enabled
if reportTable.enabled ~= false then reportTable.enabled = true end
local items = activeReportDialog.listBox.Items
items:Add(reportTable.name, reportTable.enabled)
return true
end
function Tartare.reportError(newMessage)
print (newMessage)
-- Don't add a carriage return for the first error
if string.len(errorMessage) > 0 then
errorMessage = errorMessage .. "\r\n"
end
errorMessage = errorMessage .. newMessage
end
-- Reset to prior values
local function ActiveCancelCB()
for index, reportTable in ipairs(registeredReports) do
activeReportDialog.listBox:SetItemChecked(index-1, reportTable.enabled)
end
activeReportDialog.form:Hide()
end
-- Fetech new active reports
local function ActiveOKCB()
for index, reportTable in ipairs(registeredReports) do
reportTable.enabled = activeReportDialog.listBox:GetItemChecked(index-1)
end
activeReportDialog.form:Hide()
end
local function CancelCB()
cancelProcessing = true
end
local function CreateActiveReportDialog()
-- Create a dialog
local form = Form()
activeReportDialog.form = form
form.Text = "Tartare Modules"
form.Width = 300
form.Height = 420
form.StartPosition = FormStartPosition.CenterScreen
-- Add Edit menu
-- Attach the menu to the form
local formMenu = MainMenu()
form.Menu = formMenu
local editItem = MenuItem("Edit")
formMenu.MenuItems:Add(editItem)
local selectItem = MenuItem("Select All")
editItem.MenuItems:Add(selectItem)
selectItem.Click:Add(SelectAllCB)
local unselectItem = MenuItem("Unselect All")
unselectItem.Click:Add(UnselectAllCB)
editItem.MenuItems:Add(unselectItem)
-- Add label
local label = Label()
label.Parent = form
label.Left = 0
label.Top = 10
label.Height = 20
label.Width = form.Width
label.TextAlign = ContentAlignment.MiddleCenter
label.Text = "Active Modules"
-- Add CheckedListBox
local listBox = CheckedListBox()
activeReportDialog.listBox = listBox
listBox.Parent = form
listBox.Top = 35
listBox.Height = 300
listBox.Width = 260
listBox.Left = 10
listBox.ThreeDCheckBoxes = true
listBox.IntegralHeight = true
-- Add the OK Button
local okButton = Button()
okButton.Text = "OK"
okButton.Left = 60
okButton.Width = 80
okButton.Top = 330
okButton.Click:Add(ActiveOKCB)
okButton.Parent = form
-- Add the Cancel Button
local cancelButton = Button()
cancelButton.Text = "Cancel"
cancelButton.Left = 160
cancelButton.Width = 80
cancelButton.Top = 330
cancelButton.Click:Add(ActiveCancelCB)
cancelButton.Parent = form
end
local function CreateProgressDialog()
-- Create a dialog
local form = Form()
form.Text = "Tartare Progress"
form.Width = 400
form.Height = 250
form.ControlBox = false
form.StartPosition = FormStartPosition.CenterScreen
progressDialog.form = form
-- Add file label
local fileLabel = Label()
fileLabel.Parent = form
fileLabel.Left = 0
fileLabel.Top = 20
fileLabel.Height = 40
fileLabel.Width = form.Width
fileLabel.TextAlign = ContentAlignment.MiddleCenter
progressDialog.fileLabel = fileLabel
-- Add process label
local processLabel = Label()
processLabel.Parent = form
processLabel.Left = 0
processLabel.Top = 70
processLabel.Height = 20
processLabel.Width = form.Width
processLabel.TextAlign = ContentAlignment.MiddleCenter
progressDialog.processLabel = processLabel
-- Add the progress bar
local bar = ProgressBar()
bar.Parent = form
bar.Visible = true
bar.Minimum = 0
bar.Step = 1
bar.Height = 50
bar.Width = 300
bar.Top = 90
bar.Left = 50
bar.Style = ProgressBarStyle.Continuous
progressDialog.bar = bar
-- Add the Cancel Button
local cancelButton = Button()
cancelButton.Text = "Cancel"
cancelButton.Left = 160
cancelButton.Width = 80
cancelButton.Top = 150
cancelButton.Click:Add(CancelCB)
cancelButton.Parent = form
end
-- This has a forward declaration
function MenuCheck(sender, event)
sender.Checked = not sender.Checked
end
local function OnFiles()
local dialog = OpenDialog() -- Create the dialog
dialog.Filter = "Raw files (*.raw)|*.raw|All files (*.*)|*.*" -- Set the filter
dialog.Multiselect = true -- Allow selection of more than one file
-- Due to some .NET anomoly, the file dialog will not show up when running
-- with LuaJIT as the interpreter. Lord Google suggested setting the ShowHelp
-- property to resolve it. Unbelievable, but it works
dialog.ShowHelp = true -- Workaround for dialog not being visible
local result = dialog:ShowDialog() -- Show modal dialog
local rawFileList = {}
if result == DialogResult.OK then
-- Load Lua files one by one through the console
for i = 1, dialog.FileNames.Length do
table.insert(rawFileList, {fileName = dialog.FileNames[i-1]}) -- Get the file name
end
end
dialog:Dispose()
ProcessList(rawFileList)
end
local function OnNotebooks()
-- Make a list of all open rawFiles
local rawFileList = {}
for _, noteBook in ipairs(mdiNoteBook.noteBookList) do
local rawFile = noteBook.rawFile
if rawFile then table.insert(rawFileList, {rawFileObject = rawFile,
fileName = noteBook.fullFileName}) end
end
ProcessList(rawFileList)
end
-- This function has a forward declaration
function ProcessList(list)
local startTime = os.time()
--Create list of active reports
local activeReports = {}
local spectrumReports = {}
for _, report in ipairs(registeredReports) do
if report.enabled then
table.insert(activeReports, report)
if report.wantsLabelData or report.wantsSpectrum then
table.insert(spectrumReports, report)
end
end
end
progressDialog.form:Show()
local processSteps = #list * (#activeReports + 1) -- Add one for spectrum processing step
local step = 1
progressDialog.bar.Maximum = processSteps
progressDialog.bar.Value = 0
Application.DoEvents()
-- Clear past error messages
errorMessage = ""
-- Process the files
for fileIndex, item in ipairs(list) do
-- Find a backslash followed by anthing other than a backslash,
-- right before the end of the string
local index = string.find(item.fileName, "\\[^\\]*$")
local shortFileName
if index then
shortFileName = string.sub(item.fileName, index + 1)
else
shortFileName = item.fileName
end
progressDialog.fileLabel.Text = shortFileName
Application.DoEvents()
if not item.rawFileObject then
progressDialog.processLabel.Text = "Opening"
Application.DoEvents()
item.rawFileObject = RawFile.New(item.fileName)
if type(item.rawFileObject) ~= "userdata" then
print ("Unable to open " .. item.fileName)
print (item.rawFile)
progressDialog.form:Hide()
return nil
end
item.rawFileObject:Open()
if not item.rawFileObject.IsOpen then
print ("Unable to open " .. item.rawFileObject)
progressDialog.form:Hide()
return nil
end
item.needToClose = true
end
-- Loop through and have each report process this raw file
for _, report in ipairs(activeReports) do
local processText = string.format("%s Step %d of %d", report.name, step, processSteps)
step = step + 1
progressDialog.processLabel.Text = processText
Application.DoEvents()
-- Pcall the processing function, since we don't trust the creator
local processPcall, errorMessage = pcall(
function() report.processFile(item.rawFileObject, shortFileName, fileIndex == 1) end)
if not processPcall then
Tartare.reportError(string.format("processFile() for %s failed on %s", report.name, shortFileName))
Tartare.reportError (errorMessage)
end
progressDialog.bar:PerformStep()
Application.DoEvents()
if cancelProcessing then break end
end
-- Now loop through and run reports that require spectral data or label data
-- Only fetch spectra or label data if required by the routine, and then share
-- that spectrum instead of fetching multiple times
if #spectrumReports > 0 then
progressDialog.processLabel.Text = "Processing Spectra"
Application.DoEvents()
local rf = item.rawFileObject
for scanNumber = rf.FirstSpectrumNumber, rf.LastSpectrumNumber do
if scanNumber % 200 == 0 then
Application.DoEvents()
if cancelProcessing then break end
end
if scanNumber % 5000 == 0 then
progressDialog.processLabel.Text = string.format("Processing Spectra %d of %d", scanNumber, rf.LastSpectrumNumber)
Application.DoEvents()
end
local spectrum, labelData
local description = {scanNumber = scanNumber, fineNumber = fileIndex}
description.order = rf:GetMSNOrder(scanNumber)
description.filter = rf:GetScanFilter(scanNumber)
for _, report in ipairs(spectrumReports) do
-- Pcall the processing functions, since we don't trust the creator
if report.wantsSpectrum then
local wantPcall, errorMsg = pcall(
function()
if report.wantsSpectrum(description) then
if not spectrum then spectrum = rf.GetSpectrum(scanNumber) end
if spectrum then report.processSpectrum(spectrum, description) end
end
end)
if not wantPcall then
Tartare.reportError (string.forrmat("Spectral processing failed for %s", report.name))
Tartare.reportError (errorMsg)
end
end
-- Now repeat for label data
if report.wantsLabelData then
local wantPcall, errorMsg = pcall(
function()
if report.wantsLabelData(description) then
if not labelData then labelData = rf:GetLabelData(scanNumber) end
if labelData then report.processLabelData(labelData, description) end
end
end)
if not wantPcall then
Tartare.reportError (string.format("Label Data processing failed for %s", report.name))
Tartare.reportError (errorMsg)
end
end
end
end
end
step = step + 1
progressDialog.bar:PerformStep()
Application.DoEvents()
-- Close the raw file if we opened it
if item.needToClose then item.rawFileObject:Close() end
end
progressDialog.processLabel.Text = ""
progressDialog.fileLabel.Text = ""
progressDialog.form:Hide()
if cancelProcessing then
cancelProcessing = false
return
end
--Generate the notebook pages for the reports
local resultNotebook = mdiNoteBook({title = string.format("Tartare #%d", cycleNumber)})
resultNotebook.tabControl.ShowToolTips = true
cycleNumber = cycleNumber + 1
for _, report in ipairs(activeReports) do
local result, errorMsg = pcall(
function()
report.generateReport(resultNotebook)
end)
if not result then
Tartare.reportError (string.format("Report Generation failed for %s", report.name))
Tartare.reportError (errorMsg)
end
end
-- Display error messages if any generated
if string.len(errorMessage) > 0 then
local errorPage = textPage({name = "Tartare Errors"})
resultNotebook:AddPage(errorPage)
errorPage:Fill(errorMessage)
end
local runTime = os.time() - startTime
print (string.format("Run time (sec): %0.1f", runTime))
end
-- This has a forward declaration
function SelectAllCB()
local listBox = activeReportDialog.listBox
local items = listBox.Items
for i = 0, items.Count - 1 do
listBox:SetItemChecked(i, true)
end
end
local function ShowDialog()
activeReportDialog.form:Show()
end
-- This function has a forward declaration
function ShowMenu()
-- Set up the menu for accessing these routines
local thisParentName = "Tartare"
menu.AddMenu({name = thisParentName, parentName = "Tools"}) -- Add submenu to Tools
menu.AddMenu({name = "All Notebooks", parentName = thisParentName, callBack = OnNotebooks})
menu.AddMenu({name = "Files ...", parentName = thisParentName, callBack = OnFiles})
menu.AddMenu({name = "Active Reports ...", parentName = thisParentName, callBack = ShowDialog})
end
-- This function has a forward declaration
function TrueFunction()
return true
end
-- This has a forward declaration
function UnselectAllCB()
local listBox = activeReportDialog.listBox
local items = listBox.Items
for i = 0, items.Count - 1 do
listBox:SetItemChecked(i, false)
end
end
CreateActiveReportDialog()
CreateProgressDialog()
-- Return the module
return Tartare