Skip to content

Commit 59075e0

Browse files
committed
Support of single file output in "xic"
1 parent 3e592f4 commit 59075e0

File tree

3 files changed

+64
-24
lines changed

3 files changed

+64
-24
lines changed

MainClass.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Globalization;
1212
using System.Collections.Generic;
1313
using System.Text.RegularExpressions;
14+
using System.Data;
1415

1516
namespace ThermoRawFileParser
1617
{
@@ -52,6 +53,8 @@ private static void XicParametersParsing(string[] args)
5253
XicParameters parameters = new XicParameters();
5354
string singleFile = null;
5455
string fileDirectory = null;
56+
string outputFile = null;
57+
string outputDirectory = null;
5558
string logFormatString = null;
5659

5760
var optionSet = new OptionSet
@@ -82,10 +85,15 @@ private static void XicParametersParsing(string[] args)
8285
{
8386
"o=|output=",
8487
"The output directory. If not specified, the output is written to the input directory",
85-
v => parameters.outputDirectory = v
88+
v => outputDirectory = v
8689
},
8790
{
88-
"b|base64",
91+
"b=|output_file",
92+
"The output file. Specify this or an output directory -o. Specifying neither writes to the input directory.",
93+
v => outputFile = v
94+
},
95+
{
96+
"6|base64",
8997
"Encodes the content of the xic vectors as base 64 encoded string.",
9098
v => parameters.base64 = v != null
9199
},
@@ -163,7 +171,7 @@ private static void XicParametersParsing(string[] args)
163171
}
164172

165173

166-
if (parameters.outputDirectory != null && !Directory.Exists(parameters.outputDirectory))
174+
if (outputDirectory != null && !Directory.Exists(outputDirectory))
167175
{
168176
throw new OptionException(
169177
"specify a valid output location",
@@ -177,18 +185,55 @@ private static void XicParametersParsing(string[] args)
177185
"-i, --input xor -d, --input_directory");
178186
}
179187

188+
if (outputFile != null && outputDirectory != null)
189+
{
190+
throw new OptionException(
191+
"cannot use an output file and an output directory simultaneously",
192+
"-b, --output_file; -o, --output");
193+
}
194+
180195
if (singleFile != null)
181196
{
182197
parameters.rawFileList.Add(Path.GetFullPath(singleFile));
198+
199+
if (outputFile != null)
200+
{
201+
parameters.outputFileList.Add(Path.GetFullPath(outputFile));
202+
}
203+
else if (outputDirectory != null)
204+
{
205+
parameters.outputFileList.Add(Path.Combine(outputDirectory ?? throw new NoNullAllowedException("Output directory cannot be null"),
206+
Path.GetFileNameWithoutExtension(singleFile) + ".json"));
207+
}
208+
else
209+
{
210+
parameters.outputFileList.Add(Path.Combine(Path.GetDirectoryName(Path.GetFullPath(singleFile)),
211+
Path.GetFileNameWithoutExtension(singleFile) + ".json"));
212+
}
183213
}
184214
else
185215
{
216+
if (outputFile != null)
217+
{
218+
throw new OptionException("Cannot use single output file to proceess a directory, use directory output instead", "-o, --output");
219+
}
220+
else if (outputDirectory != null)
221+
{
222+
outputDirectory = Path.GetFullPath(outputDirectory);
223+
}
224+
else
225+
{
226+
outputDirectory = Path.GetFullPath(fileDirectory);
227+
}
228+
186229
var directoryInfo = new DirectoryInfo(Path.GetFullPath(fileDirectory));
187230
var files = directoryInfo.GetFiles("*", SearchOption.TopDirectoryOnly)
188231
.Where(f => f.Extension.ToLower() == ".raw").ToArray<FileInfo>();
189232
foreach (var file in files)
190233
{
191234
parameters.rawFileList.Add(file.FullName);
235+
parameters.outputFileList.Add(Path.Combine(outputDirectory ?? throw new NoNullAllowedException("Output directory cannot be null"),
236+
Path.GetFileNameWithoutExtension(file.Name) + ".json"));
192237
}
193238
}
194239

XIC/XicExecutor.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22
using System.Data;
33
using System.IO;
44
using System.Text;
5+
using System.Reflection;
56
using Newtonsoft.Json;
67
using ThermoFisher.CommonCore.Data;
8+
using log4net;
79

810
namespace ThermoRawFileParser.XIC
911
{
1012
public static class XicExecutor
1113
{
14+
private static readonly ILog Log =
15+
LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
16+
1217
public static void Run(XicParameters parameters)
1318
{
19+
Log.InfoFormat("Reading and validating JSON input");
1420
var jsonString = File.ReadAllText(parameters.jsonFilePath, Encoding.UTF8);
1521
var validationErrors = JSONParser.ValidateJson(jsonString);
1622
if (!validationErrors.IsNullOrEmpty())
@@ -32,8 +38,13 @@ public static void Run(XicParameters parameters)
3238
}
3339

3440
var xicData = JSONParser.ParseJSON(jsonString);
35-
foreach (string rawFile in parameters.rawFileList)
41+
42+
Log.InfoFormat("Input contains {0} XICs", xicData.Content.Count);
43+
44+
for (int index = 0; index < parameters.rawFileList.Count; index++)
3645
{
46+
string rawFile = (string) parameters.rawFileList[index];
47+
3748
var dataInstance = new XicData(xicData);
3849
XicReader.ReadXic(rawFile, parameters.base64, dataInstance, ref parameters);
3950

@@ -43,20 +54,7 @@ public static void Run(XicParameters parameters)
4354
}
4455
else
4556
{
46-
// if outputDirectory has been defined, put output there.
47-
string directory;
48-
if (parameters.outputDirectory != null)
49-
{
50-
directory = parameters.outputDirectory;
51-
}
52-
// otherwise put output files into the same directory as the raw file input
53-
else
54-
{
55-
directory = Path.GetDirectoryName(rawFile);
56-
}
57-
58-
var outputFileName = Path.Combine(directory ?? throw new NoNullAllowedException("Output directory cannot be null"),
59-
Path.GetFileNameWithoutExtension(rawFile) + ".json");
57+
var outputFileName = (string) parameters.outputFileList[index];
6058

6159
OutputXicData(dataInstance, outputFileName);
6260
}

XIC/XicParameters.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ public class XicParameters
1010
public bool help { get; set; }
1111
public ArrayList rawFileList { get; set; }
1212
public string jsonFilePath { get; set; }
13-
public string outputDirectory { get; set; }
13+
public ArrayList outputFileList { get; set; }
1414
public bool printJsonExample { get; set; }
15-
public string outputFileName { get; set; }
1615
public bool base64 { get; set; }
1716
public bool stdout { get; set; }
1817
public bool Vigilant { get; set; }
@@ -25,9 +24,8 @@ public XicParameters()
2524
help = false;
2625
rawFileList = new ArrayList();
2726
jsonFilePath = null;
28-
outputDirectory = null;
27+
outputFileList = new ArrayList();
2928
printJsonExample = false;
30-
outputFileName = null;
3129
base64 = false;
3230
stdout = false;
3331
Vigilant = false;
@@ -57,9 +55,8 @@ public XicParameters(XicParameters copy)
5755
}
5856

5957
jsonFilePath = copy.jsonFilePath;
60-
outputDirectory = copy.outputDirectory;
58+
outputFileList = copy.outputFileList;
6159
printJsonExample = copy.printJsonExample;
62-
outputFileName = copy.outputFileName;
6360
base64 = copy.base64;
6461
stdout = copy.stdout;
6562
LogFormat = copy.LogFormat;

0 commit comments

Comments
 (0)