forked from compomics/ThermoRawFileParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainClass.cs
306 lines (283 loc) · 11.6 KB
/
MainClass.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
using System;
using System.IO;
using log4net;
using log4net.Core;
using Mono.Options;
using ThermoFisher.CommonCore.Data;
namespace ThermoRawFileParser
{
public static class MainClass
{
private static readonly ILog Log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public const string Version = "1.1.9";
public static void Main(string[] args)
{
string rawFolderPath = null;
string rawFilePath = null;
string outputDirectory = null;
string outputFile = null;
string outputFormatString = null;
var outputFormat = OutputFormat.NONE;
var gzip = false;
string outputMetadataString = null;
var outputMetadataFormat = MetadataFormat.NONE;
string s3url = null;
string s3AccessKeyId = null;
string s3SecretAccessKey = null;
var verbose = false;
string bucketName = null;
var ignoreInstrumentErrors = false;
var noPeakPicking = false;
var noZlibCompression = false;
var help = false;
var version = false;
var optionSet = new OptionSet
{
{
"h|help", "Prints out the options.",
h => help = h != null
},
{
"version", "Prints out the library version.",
v => version = v != null
},
{
"d=|directory=", "The folder containing input raw files.",
v => rawFolderPath = v
},
{
"i=|input=", "The raw file input.",
v => rawFilePath = v
},
{
"o=|output=", "The output directory. Specify this or an output file.",
v => outputDirectory = v
},
{
"b=|output_file", "The output file. Specify this or an output directory.",
v => outputFile = v
},
{
"f=|format=",
"The output format for the spectra (0 for MGF, 1 for mzMl, 2 for indexed mzML, 3 for Parquet).",
v => outputFormatString = v
},
{
"m=|metadata=", "The metadata output format (0 for JSON, 1 for TXT).",
v => outputMetadataString = v
},
{
"g|gzip", "GZip the output file if this flag is specified (without value).",
v => gzip = v != null
},
{
"p|noPeakPicking",
"Don't use the peak picking provided by the native thermo library (by default peak picking is enabled).",
v => noPeakPicking = v != null
},
{
"z|noZlibCompression",
"Don't use zlib compression for the m/z ratios and intensities (by default zlib compression is enabled).",
v => noZlibCompression = v != null
},
{
"v|verbose", "Enable verbose logging.",
v => verbose = v != null
},
{
"e|ignoreInstrumentErrors", "Ignore missing properties by the instrument.",
v => ignoreInstrumentErrors = v != null
},
{
"u:|s3_url:",
"Optional property to write directly the data into S3 Storage.",
v => s3url = v
},
{
"k:|s3_accesskeyid:",
"Optional key for the S3 bucket to write the file output.",
v => s3AccessKeyId = v
},
{
"t:|s3_secretaccesskey:",
"Optional key for the S3 bucket to write the file output.",
v => s3SecretAccessKey = v
},
{
"n:|s3_bucketName:",
"S3 bucket name",
v => bucketName = v
}
};
try
{
// parse the command line
var extra = optionSet.Parse(args);
if (!extra.IsNullOrEmpty())
{
throw new OptionException("unexpected extra arguments", null);
}
if (help)
{
ShowHelp(" usage is (use -option=value for the optional arguments):", null,
optionSet);
return;
}
if (version)
{
Console.WriteLine(Version);
return;
}
if (outputMetadataString == null && outputFormatString == null)
{
throw new OptionException("The parameter -f or -m should be provided",
"-f|--format , -m|--format");
}
if (outputFormatString != null)
{
int outPutFormatInt;
try
{
outPutFormatInt = int.Parse(outputFormatString);
}
catch (FormatException e)
{
throw new OptionException(
"unknown output format value (0 for MGF, 1 for mzMl, 2 for indexed mzML, 3 for Parquet)",
"-f, --format");
}
if (Enum.IsDefined(typeof(OutputFormat), outPutFormatInt) &&
((OutputFormat) outPutFormatInt) != OutputFormat.NONE)
{
outputFormat = (OutputFormat) outPutFormatInt;
}
else
{
throw new OptionException(
"unknown output format value (0 for MGF, 1 for mzMl, 2 for indexed mzML, 3 for Parquet)",
"-f, --format");
}
}
if (outputMetadataString != null)
{
int metadataInt;
try
{
metadataInt = int.Parse(outputMetadataString);
}
catch (FormatException e)
{
throw new OptionException("unknown metadata format value (0 for JSON, 1 for TXT)",
"-m, --metadata");
}
if (Enum.IsDefined(typeof(MetadataFormat), metadataInt) &&
((MetadataFormat) metadataInt) != MetadataFormat.NONE)
{
outputMetadataFormat = (MetadataFormat) metadataInt;
}
else
{
throw new OptionException("unknown metadata format value (0 for JSON, 1 for TXT)",
"-m, --metadata");
}
}
if (outputFile == null && outputDirectory == null)
{
throw new OptionException(
"specify an output directory or output file",
"-o, --output or -b, --output_file");
}
if (outputFile != null && Directory.Exists(outputFile))
{
throw new OptionException(
"specify a valid output file, not a directory",
"-b, --output_file");
}
if (outputDirectory != null && !Directory.Exists(outputDirectory))
{
throw new OptionException(
"specify a valid output directory",
"-o, --output");
}
if (rawFolderPath != null && rawFilePath != null)
{
throw new OptionException(
"both input directory and input file options cannot be used simultaneously",
"-d, --directory, -i, --input");
}
if (rawFolderPath != null && outputFile != null)
{
throw new OptionException(
"one output file cannot be used when using an input directory. Use output directory instead.",
"-b, --output_file");
}
if (rawFolderPath != null && (!Directory.Exists(rawFolderPath) || Directory.GetFiles(rawFolderPath, "*", SearchOption.TopDirectoryOnly).Length == 0 ) )
{
throw new OptionException(
"specify a valid input directory with raw files",
"-d, --directory");
}
}
catch (OptionException optionException)
{
ShowHelp("Error - usage is (use -option=value for the optional arguments):", optionException,
optionSet);
}
catch (ArgumentNullException argumentNullException)
{
if (help)
{
ShowHelp(" usage is (use -option=value for the optional arguments):", null,
optionSet);
}
else
{
ShowHelp("Error - usage is (use -option=value for the optional arguments):", null,
optionSet);
}
}
try
{
if (verbose)
{
((log4net.Repository.Hierarchy.Hierarchy) LogManager.GetRepository()).Root.Level =
Level.Debug;
((log4net.Repository.Hierarchy.Hierarchy) LogManager.GetRepository())
.RaiseConfigurationChanged(EventArgs.Empty);
}
var parseInput = new ParseInput(rawFolderPath, rawFilePath, outputDirectory, outputFile, outputFormat, gzip,
outputMetadataFormat,
s3url, s3AccessKeyId, s3SecretAccessKey, bucketName, ignoreInstrumentErrors, noPeakPicking,
noZlibCompression ,verbose);
RawFileParser.Parse(parseInput);
}
catch (Exception ex)
{
Log.Error("An unexpected error occured:");
Log.Error(ex.ToString());
Environment.Exit(1);
}
}
/// <summary>
/// Show the help message
/// </summary>
/// <param name="message">the help message</param>
/// <param name="optionException">the option exception, can be null</param>
/// <param name="optionSet">the option set object</param>
private static void ShowHelp(string message, OptionException optionException, OptionSet optionSet)
{
if (optionException != null)
{
if (!optionException.OptionName.IsNullOrEmpty())
{
Console.Error.Write(optionException.OptionName + ": ");
}
Console.Error.WriteLine(optionException.Message);
}
Console.Error.WriteLine(message);
optionSet.WriteOptionDescriptions(Console.Error);
Environment.Exit(-1);
}
}
}