-
Notifications
You must be signed in to change notification settings - Fork 50
/
RawFileParser.cs
executable file
·189 lines (168 loc) · 7.52 KB
/
RawFileParser.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
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using ThermoFisher.CommonCore.Data;
using ThermoFisher.CommonCore.Data.Business;
using ThermoFisher.CommonCore.Data.Interfaces;
using ThermoRawFileParser.Writer;
using ThermoRawFileParser.Util;
namespace ThermoRawFileParser
{
public static class RawFileParser
{
private static readonly log4net.ILog Log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// Process and extract the RAW file(s).
/// </summary>
/// <param name="parseInput">the parse input object</param>
public static void Parse(ParseInput parseInput)
{
// Input raw folder mode
if (parseInput.RawDirectoryPath != null)
{
Log.Info("Started analyzing folder " + parseInput.RawDirectoryPath);
var rawFilesPath = Directory
.EnumerateFiles(parseInput.RawDirectoryPath, "*", SearchOption.TopDirectoryOnly)
.Where(s => s.ToLower().EndsWith("raw")).ToArray();
Log.Info(String.Format("The folder contains {0} RAW files", rawFilesPath.Length));
if (rawFilesPath.Length == 0)
{
Log.Debug("No raw files found in folder");
throw new RawFileParserException("No raw files found in folder!");
}
foreach (var filePath in rawFilesPath)
{
parseInput.RawFilePath = filePath;
Log.Info("Started parsing " + parseInput.RawFilePath);
TryProcessFile(parseInput);
}
}
// Input raw file mode
else
{
Log.Info("Started parsing " + parseInput.RawFilePath);
TryProcessFile(parseInput);
}
}
/// <summary>
/// Process and extract the given RAW file and catch IO exceptions.
/// </summary>
/// <param name="parseInput">the parse input object</param>
private static void TryProcessFile(ParseInput parseInput)
{
try
{
ProcessFile(parseInput);
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException)
{
Log.Error(!ex.Message.IsNullOrEmpty()
? ex.Message
: "Attempting to write to an unauthorized location.");
parseInput.NewError();
}
else if (ex is RawFileParserException)
{
Log.Error(ex.Message);
parseInput.NewError();
}
else
{
Log.Error("An unexpected error occured (see below)");
Log.Error(ex.ToString());
parseInput.NewError();
}
}
}
/// <summary>
/// Process and extract the given RAW file.
/// </summary>
/// <param name="parseInput">the parse input object</param>
private static void ProcessFile(ParseInput parseInput)
{
// Create the IRawDataPlus object for accessing the RAW file
IRawDataPlus rawFile;
//checking for symlinks
var fileInfo = new FileInfo(parseInput.RawFilePath);
if (fileInfo.Attributes.HasFlag(FileAttributes.ReparsePoint)) //detected path is a symlink
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var realPath = NativeMethods.GetFinalPathName(parseInput.RawFilePath);
Log.DebugFormat("Detected reparse point, real path: {0}", realPath);
parseInput.UpdateRealPath(realPath);
}
else //Mono should handle all non-windows platforms
{
var realPath = Path.Combine(Path.GetDirectoryName(parseInput.RawFilePath), Mono.Unix.UnixPath.ReadLink(parseInput.RawFilePath));
Log.DebugFormat("Detected reparse point, real path: {0}", realPath);
parseInput.UpdateRealPath(realPath);
}
}
using (rawFile = RawFileReaderFactory.ReadFile(parseInput.RawFilePath))
{
if (!rawFile.IsOpen)
{
throw new RawFileParserException("Unable to access the RAW file using the native Thermo library.");
}
// Check for any errors in the RAW file
if (rawFile.IsError)
{
throw new RawFileParserException($"RAW file cannot be processed because of an error - {rawFile.FileError}");
}
// Check if the RAW file is being acquired
if (rawFile.InAcquisition)
{
throw new RawFileParserException("RAW file cannot be processed since it is still being acquired");
}
// Get the number of instruments (controllers) present in the RAW file and set the
// selected instrument to the MS instrument, first instance of it
var firstScanNumber = -1;
var lastScanNumber = -1;
if (rawFile.GetInstrumentCountOfType(Device.MS) != 0)
{
rawFile.SelectInstrument(Device.MS, 1);
rawFile.IncludeReferenceAndExceptionData = !parseInput.ExData;
// Get the first and last scan from the RAW file
firstScanNumber = rawFile.RunHeaderEx.FirstSpectrum;
lastScanNumber = rawFile.RunHeaderEx.LastSpectrum;
// Check for empty file
if (lastScanNumber < 1)
{
throw new RawFileParserException("Empty RAW file, no output will be produced");
}
}
if (parseInput.MetadataFormat != MetadataFormat.None)
{
MetadataWriter metadataWriter = new MetadataWriter(parseInput);
metadataWriter.WriteMetadata(rawFile, firstScanNumber, lastScanNumber);
}
if (parseInput.OutputFormat != OutputFormat.None)
{
SpectrumWriter spectrumWriter;
switch (parseInput.OutputFormat)
{
case OutputFormat.MGF:
spectrumWriter = new MgfSpectrumWriter(parseInput);
spectrumWriter.Write(rawFile, firstScanNumber, lastScanNumber);
break;
case OutputFormat.MzML:
case OutputFormat.IndexMzML:
spectrumWriter = new MzMlSpectrumWriter(parseInput);
spectrumWriter.Write(rawFile, firstScanNumber, lastScanNumber);
break;
case OutputFormat.Parquet:
spectrumWriter = new ParquetSpectrumWriter(parseInput);
spectrumWriter.Write(rawFile, firstScanNumber, lastScanNumber);
break;
}
}
Log.Info("Finished parsing " + parseInput.UserProvidedPath);
}
}
}
}