forked from compomics/ThermoRawFileParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainClass.cs
205 lines (189 loc) · 7.54 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
using System;
using Mono.Options;
using ThermoFisher.CommonCore.Data;
namespace ThermoRawFileParser
{
public static class MainClass
{
private static readonly log4net.ILog Log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void Main(string[] args)
{
string rawFilePath = null;
string outputDirectory = null;
string outputFormatString = null;
var outputFormat = OutputFormat.NON;
var gzip = false;
string outputMetadataString = null;
var outputMetadataFormat = MetadataFormat.NON;
var includeProfileData = false;
string collection = null;
string msRun = null;
string subFolder = null;
var help = false;
var optionSet = new OptionSet
{
{
"h|help", "Prints out the options.",
h => help = h != null
},
{
"i=|input=", "The raw file input.",
v => rawFilePath = v
},
{
"o=|output=", "The output directory.",
v => outputDirectory = v
},
{
"f=|format=", "The output format for the spectra (0 for MGF, 1 for MzMl, 2 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|profiledata",
"Exclude MS2 profile data if this flag is specified (without value). Only for MGF format!",
v => includeProfileData = v != null
},
{
"c:|collection", "The optional collection identifier (PXD identifier for example).",
v => collection = v
},
{
"r:|run:",
"The optional mass spectrometry run name used in the spectrum title. The RAW file name will be used if not specified.",
v => msRun = v
},
{
"s:|subfolder:",
"Optional, to disambiguate instances where the same collection has 2 or more MS runs with the same name.",
v => subFolder = 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 (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 Parquet)",
"-f, --format");
}
if (Enum.IsDefined(typeof(OutputFormat), outPutFormatInt) &&
((OutputFormat) outPutFormatInt) != OutputFormat.NON)
{
outputFormat = (OutputFormat) outPutFormatInt;
}
else
{
throw new OptionException("unknown output format value (0 for MGF, 1 for MzMl, 2 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.NON)
{
outputMetadataFormat = (MetadataFormat) metadataInt;
}
else
{
throw new OptionException("unknown metadata format value (0 for JSON, 1 for TXT)",
"-m, --metadata");
}
}
}
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
{
var parseInput = new ParseInput(rawFilePath, outputDirectory, outputFormat, gzip,
outputMetadataFormat,
includeProfileData, collection, msRun, subFolder);
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);
}
}
}