forked from compomics/ThermoRawFileParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRawFileParser.cs
136 lines (122 loc) · 5.78 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
using System;
using System.IO;
using ThermoFisher.CommonCore.Data.Business;
using ThermoFisher.CommonCore.Data.Interfaces;
using ThermoRawFileParser.Writer;
namespace ThermoRawFileParser
{
public static class RawFileParser
{
private static readonly log4net.ILog Log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// Extract the RAW file or files (from directory) metadata and spectra in MGF format.
/// </summary>
/// <param name="parseInput">the parse input object</param>
public static void Parse(ParseInput parseInput)
{
// Input raw folder mode
if (parseInput.RawFolderPath != null)
{
Log.Info("Started analysing folder " + parseInput.RawFolderPath);
System.Collections.Generic.IEnumerable<String> filesPath = Directory.EnumerateFiles(parseInput.RawFolderPath);
if (Directory.GetFiles(parseInput.RawFolderPath, "*", SearchOption.TopDirectoryOnly).Length == 0)
{
Log.Debug("No raw file found in folder");
throw new Exception("No raw file found in folder!");
}else
{
foreach (String filePath in filesPath)
{
parseInput.RawFilePath = filePath;
Log.Info("Started parsing " + parseInput.RawFilePath);
ProcessFile(parseInput);
}
}
}
// Input raw file mode
else
{
Log.Info("Started parsing " + parseInput.RawFilePath);
// Check to see if the RAW file name was supplied as an argument to the program
if (string.IsNullOrEmpty(parseInput.RawFilePath))
{
Log.Debug("No raw file specified or found in path");
throw new Exception("No RAW file specified!");
}
// Check to see if the specified RAW file exists
if (!File.Exists(parseInput.RawFilePath))
{
throw new Exception(@"The file doesn't exist in the specified location - " + parseInput.RawFilePath);
}
ProcessFile(parseInput);
}
}
/// <summary>
/// Extract the RAW file metadata and spectra in MGF format.
/// </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;
using (rawFile = RawFileReaderFactory.ReadFile(parseInput.RawFilePath))
{
if (!rawFile.IsOpen)
{
throw new Exception("Unable to access the RAW file using the RawFileReader class!");
}
// Check for any errors in the RAW file
if (rawFile.IsError)
{
throw new Exception($"Error opening ({rawFile.FileError}) - {parseInput.RawFilePath}");
}
// Check if the RAW file is being acquired
if (rawFile.InAcquisition)
{
throw new Exception("RAW file still being acquired - " + parseInput.RawFilePath);
}
// Get the number of instruments (controllers) present in the RAW file and set the
// selected instrument to the MS instrument, first instance of it
rawFile.SelectInstrument(Device.MS, 1);
// Get the first and last scan from the RAW file
var firstScanNumber = rawFile.RunHeaderEx.FirstSpectrum;
var lastScanNumber = rawFile.RunHeaderEx.LastSpectrum;
if (parseInput.OutputMetadata != MetadataFormat.NONE)
{
var metadataWriter = new MetadataWriter(parseInput.OutputDirectory, parseInput.RawFileNameWithoutExtension);
switch (parseInput.OutputMetadata)
{
case MetadataFormat.JSON:
metadataWriter.WriteJsonMetada(rawFile, firstScanNumber, lastScanNumber);
break;
case MetadataFormat.TXT:
metadataWriter.WriteMetada(rawFile, firstScanNumber, lastScanNumber);
break;
}
}
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.RawFilePath);
}
}
}
}