-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenMSFeatureFinderNodeTools.cs
337 lines (296 loc) · 13.4 KB
/
OpenMSFeatureFinderNodeTools.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using Thermo.Magellan.BL.Data;
using Thermo.Magellan.BL.Processing;
using Thermo.Magellan.BL.Processing.Interfaces;
using Thermo.Magellan.DataLayer.FileIO;
using Thermo.Magellan.EntityDataFramework;
using Thermo.Magellan.Exceptions;
using Thermo.Magellan.MassSpec;
using Thermo.Magellan.Utilities;
using Thermo.Metabolism.Algorithms;
using Thermo.Metabolism.DataObjects;
using Thermo.Metabolism.DataObjects.EntityDataObjects;
using Thermo.Metabolism.DataObjects.PeakModels;
//using Thermo.Magellan.BL.ReportEntityData; //for own table
//using Thermo.Metabolism.DataObjects.EntityDataObjects;
using OpenMS.OpenMSFile;
using Thermo.Metabolism.Processing.Services.Interfaces;
using Thermo.Metabolism.DataObjects.Constants;
namespace OpenMS.AdapterNodes
{
public partial class OpenMSMetaboProfilerNode : ProcessingNode<UnknownFeatureConsolidationProvider, ConsensusXMLFile>,
IResultsSink<MassSpectrumCollection>
{
#region stuff_for_tools
private void create_default_ini(string execPath, string ini_path)
{
var timer = Stopwatch.StartNew();
var ini_loc = String.Format("\"{0}\"", ini_path);
//SendAndLogMessage(execPath + NodeScratchDirectory + ini_loc);
var process = new Process
{
StartInfo =
{
FileName = execPath,
WorkingDirectory = NodeScratchDirectory,
Arguments = " -write_ini " + String.Format("\"{0}\"", ini_path),
UseShellExecute = false,
RedirectStandardOutput = false,
CreateNoWindow = false
}
};
try
{
var openMS_shared_dir = Path.Combine(ServerConfiguration.ToolsDirectory, "OpenMS-2.0/share/OpenMS");
process.StartInfo.EnvironmentVariables["OPENMS_DATA_PATH"] = openMS_shared_dir;
process.Start();
try
{
process.Refresh();
//process.PriorityClass = ProcessPriorityClass.BelowNormal;//Do not use, causes exception!
process.WaitForExit();
}
catch (InvalidOperationException ex)
{
NodeLogger.ErrorFormat(ex, "The following exception is raised during the execution of \"{0}\":", execPath);
throw;
}
if (process.ExitCode != 0)
{
throw new MagellanProcessingException(
String.Format(
"The exit code of {0} was {1}. (The expected exit code is 0)",
Path.GetFileName(process.StartInfo.FileName), process.ExitCode));
}
}
catch (System.Threading.ThreadAbortException)
{
throw;
}
catch (Exception ex)
{
NodeLogger.ErrorFormat(ex, "The following exception is raised during the execution of \"{0}\":", execPath);
throw;
}
finally
{
if (!process.HasExited)
{
NodeLogger.WarnFormat(
"The process [{0}] isn't finished correctly -> force the process to exit now", process.StartInfo.FileName);
process.Kill();
}
}
}
private void WriteItem(string ini_path, Dictionary<string, string> parameters)
{
XmlDocument doc = new XmlDocument();
doc.Load(ini_path);
XmlNodeList nlist = doc.GetElementsByTagName("ITEM");
foreach (XmlNode item in nlist)
{
foreach (string param in parameters.Keys)
{
if (item.Attributes["name"].Value == param)
{
item.Attributes["value"].Value = parameters[param];
}
}
}
//doc.Save(Path.Combine(NodeScratchDirectory, "ToolParameters.xml"));
doc.Save(ini_path);
}
//Write ITEMLISTs, used for input or output file lists
private static void replaceItemList(string[] vars, string ini_path, string mode)
{
XmlDocument doc = new XmlDocument();
doc.Load(ini_path);
XmlNodeList itemlist = doc.GetElementsByTagName("ITEMLIST");
foreach (XmlNode item in itemlist)
{
//mode: in or out?
if (item.Attributes["name"].Value == mode)
{
//we want to remove e.g. H+:0.9
//XmlAttributeCollection attributes = item.Attributes.;
//item.RemoveAll();
//((XmlElement) item).IsEmpty = true;
while (item.FirstChild != null){
item.RemoveChild(item.FirstChild);
}
foreach (var fn in vars)
{
//We add LISTITEMS to until then empty ITEMLISTS
var listitem = doc.CreateElement("LISTITEM");
XmlAttribute newAttribute = doc.CreateAttribute("value");
newAttribute.Value = fn;
listitem.SetAttributeNode(newAttribute);
item.AppendChild(listitem);
}
}
}
doc.Save(ini_path);
}
//Write mz and rt parameters. different function than WriteItem due to specific structure in considered tools
private void write_MZ_RT_thresholds(string ini_path)
{
XmlDocument doc = new XmlDocument();
doc.Load(ini_path);
XmlNodeList nlist = doc.GetElementsByTagName("ITEM");
foreach (XmlNode item in nlist)
{
if ((item.ParentNode.Attributes["name"].Value == "distance_MZ") && (item.Attributes["name"].Value == "max_difference"))
{
var mzthresh = MZThreshold.ToString();
item.Attributes["value"].Value = mzthresh;
}
else if ((item.ParentNode.Attributes["name"].Value == "distance_MZ") && (item.Attributes["name"].Value == "unit"))
{
//always use ppm
item.Attributes["value"].Value = "ppm";
}
else if ((item.ParentNode.Attributes["name"].Value == "distance_RT") && (item.Attributes["name"].Value == "max_difference"))
{
item.Attributes["value"].Value = (RTThreshold.Value * 60).ToString(); //need to convert minute(CD) to seconds(OpenMS)!
}
}
doc.Save(ini_path);
}
//execute specific OpenMS Tool (execPath) with specified Ini (ParamPath)
private void RunTool(string execPath, string ParamPath, bool mute = false)
{
var timer = Stopwatch.StartNew();
var process = new Process
{
StartInfo =
{
FileName = execPath,
WorkingDirectory = NodeScratchDirectory,
//WorkingDirectory = FileHelper.GetShortFileName(NodeScratchDirectory),
Arguments = " -ini " + String.Format("\"{0}\"",ParamPath),
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = false,
//WindowStyle = ProcessWindowStyle.Hidden
}
};
var openMS_shared_dir = Path.Combine(ServerConfiguration.ToolsDirectory, "OpenMS-2.0/share/OpenMS");
process.StartInfo.EnvironmentVariables["OPENMS_DATA_PATH"] = openMS_shared_dir;
SendAndLogTemporaryMessage("Starting process [{0}] in working directory [{1}] with arguments [{2}]",
process.StartInfo.FileName, process.StartInfo.WorkingDirectory, process.StartInfo.Arguments);
WriteLogMessage(MessageLevel.Debug,
"Starting process [{0}] in working directory [{1}] with arguments [{2}]",
process.StartInfo.FileName, process.StartInfo.WorkingDirectory, process.StartInfo.Arguments);
try
{
process.Start();
try
{
//process.PriorityClass = ProcessPriorityClass.BelowNormal;
string current_work = "";
while (process.HasExited == false)
{
var output = process.StandardOutput.ReadLine();
if (mute) {
continue;
}
// move on if no new announcement.
if (String.IsNullOrEmpty(output))
{
continue;
}
//store all results (for now?) of OpenMS Tool output
WriteLogMessage(MessageLevel.Debug, output);
// Parse the output and report progress using the method SendAndLogTemporaryMessage
if (output.Contains(@"Progress of 'loading mzML file':"))
{
current_work = "Progress of 'loading mzML file':";
}
else if (output.Contains("Progress of 'loading chromatograms':"))
{
current_work = "Progress of 'loading chromatograms':";
}
else if (output.Contains("Progress of 'mass trace detection':"))
{
current_work = "Progress of 'mass trace detection':";
}
else if (output.Contains("Progress of 'elution peak detection':"))
{
current_work = "Progress of 'elution peak detection':";
}
else if (output.Contains("Progress of 'assembling mass traces to features':"))
{
current_work = "Progress of 'assembling mass traces to features':";
}
else if (output.Contains("Progress of 'Aligning input maps':"))
{
current_work = "Progress of 'Aligning input maps':";
}
else if (output.Contains("Progress of 'linking features':"))
{
current_work = "Progress of 'linking features':";
}
else if (output.Contains("%"))
{
SendAndLogTemporaryMessage("{0} {1}", current_work, output);
}
}
// Note: The child process waits until everything is read from the standard output -> A Deadlock could arise here
using (var reader = new StringReader(process.StandardOutput.ReadToEnd()))
{
string output;
if (!mute){
while ((output = reader.ReadLine()) != null)
{
WriteLogMessage(MessageLevel.Debug, output);
if (String.IsNullOrEmpty(output) == false)
{
SendAndLogMessage(output, false);
}
}
}
}
process.WaitForExit();
}
catch (InvalidOperationException ex)
{
NodeLogger.ErrorFormat(ex, "The following exception is raised during the execution of \"{0}\":", execPath);
throw;
}
if (process.ExitCode != 0)
{
throw new MagellanProcessingException(
String.Format(
"The exit code of {0} was {1}. (The expected exit code is 0)",
Path.GetFileName(process.StartInfo.FileName), process.ExitCode));
}
}
catch (System.Threading.ThreadAbortException)
{
throw;
}
catch (Exception ex)
{
NodeLogger.ErrorFormat(ex, "The following exception is raised during the execution of \"{0}\":", execPath);
throw;
}
finally
{
if (!process.HasExited)
{
NodeLogger.WarnFormat(
"The process [{0}] isn't finished correctly -> force the process to exit now", process.StartInfo.FileName);
process.Kill();
}
}
SendAndLogMessage("{0} tool processing took {1}.", execPath, StringHelper.GetDisplayString(timer.Elapsed));
}
#endregion
}
}