-
Notifications
You must be signed in to change notification settings - Fork 2
/
CustomFastaValidator.cs
357 lines (302 loc) · 11.7 KB
/
CustomFastaValidator.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using System.Collections.Generic;
using System.IO;
// ReSharper disable UnusedMember.Global
namespace ValidateFastaFile
{
/// <summary>
/// Custom FASTA validator
/// </summary>
/// <remarks>
/// Inherits from FastaValidator and has an extended error info class
/// Supports tracking errors for multiple FASTA files
/// </remarks>
public class CustomFastaValidator : FastaValidator
{
// Ignore Spelling: Validator
/// <summary>
/// Extended error info
/// </summary>
public class ErrorInfoExtended
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="lineNumber"></param>
/// <param name="proteinName"></param>
/// <param name="messageText"></param>
/// <param name="extraInfo"></param>
/// <param name="type"></param>
public ErrorInfoExtended(
int lineNumber,
string proteinName,
string messageText,
string extraInfo,
string type)
{
LineNumber = lineNumber;
ProteinName = proteinName;
MessageText = messageText;
ExtraInfo = extraInfo;
Type = type;
}
/// <summary>
/// Line number of the error
/// </summary>
public int LineNumber;
/// <summary>
/// Protein name
/// </summary>
public string ProteinName;
/// <summary>
/// Message text
/// </summary>
public string MessageText;
/// <summary>
/// Extra info
/// </summary>
public string ExtraInfo;
/// <summary>
/// Error type (Error or Warning)
/// </summary>
public string Type;
}
/// <summary>
/// Validation options
/// </summary>
public enum ValidationOptionConstants
{
/// <summary>
/// Allow asterisks in residues
/// </summary>
AllowAsterisksInResidues = 0,
/// <summary>
/// Allow dashes in residues
/// </summary>
AllowDashInResidues = 1,
/// <summary>
/// Allow symbols in protein names
/// </summary>
AllowAllSymbolsInProteinNames = 2
}
/// <summary>
/// Validation message types
/// </summary>
public enum ValidationMessageTypes
{
/// <summary>
/// Error message
/// </summary>
ErrorMsg = 0,
/// <summary>
/// Warning message
/// </summary>
WarningMsg = 1
}
private readonly List<ErrorInfoExtended> mCurrentFileErrors;
private readonly List<ErrorInfoExtended> mCurrentFileWarnings;
private string mCachedFastaFilePath;
// Note: this array gets initialized with space for 10 items
// If ValidationOptionConstants gets more than 10 entries, then this array will need to be expanded
private readonly bool[] mValidationOptions;
/// <summary>
/// Constructor
/// </summary>
public CustomFastaValidator()
{
FullErrorCollection = new Dictionary<string, List<ErrorInfoExtended>>();
FullWarningCollection = new Dictionary<string, List<ErrorInfoExtended>>();
mCurrentFileErrors = new List<ErrorInfoExtended>();
mCurrentFileWarnings = new List<ErrorInfoExtended>();
// Reserve space for tracking up to 10 validation updates (expand later if needed)
mValidationOptions = new bool[11];
}
/// <summary>
/// Clear cached errors
/// </summary>
public void ClearErrorList()
{
FullErrorCollection?.Clear();
FullWarningCollection?.Clear();
}
/// <summary>
/// Keys are FASTA filename
/// Values are the list of FASTA file errors
/// </summary>
public Dictionary<string, List<ErrorInfoExtended>> FullErrorCollection { get; }
/// <summary>
/// Keys are FASTA filename
/// Values are the list of FASTA file warnings
/// </summary>
public Dictionary<string, List<ErrorInfoExtended>> FullWarningCollection { get; }
/// <summary>
/// Returns true if dictionary FullErrorCollection contains any errors for the given FASTA file
/// </summary>
/// <param name="fastaFileName"></param>
public bool FASTAFileValid(string fastaFileName)
{
if (FullErrorCollection == null)
{
return true;
}
return !FullErrorCollection.ContainsKey(fastaFileName);
}
/// <summary>
/// Returns true if dictionary FullWarningCollection contains any warnings for the given FASTA file
/// </summary>
/// <param name="fastaFileName"></param>
public bool FASTAFileHasWarnings(string fastaFileName)
{
if (FullWarningCollection == null)
{
return false;
}
return FullWarningCollection.ContainsKey(fastaFileName);
}
/// <summary>
/// Returns the errors found for the given FASTA file
/// </summary>
/// <param name="fastaFileName"></param>
public List<ErrorInfoExtended> RecordedFASTAFileErrors(string fastaFileName)
{
if (FullErrorCollection.TryGetValue(fastaFileName, out var errorList))
{
return errorList;
}
return new List<ErrorInfoExtended>();
}
/// <summary>
/// Returns the warnings found for the given FASTA file
/// </summary>
/// <param name="fastaFileName"></param>
public List<ErrorInfoExtended> RecordedFASTAFileWarnings(string fastaFileName)
{
if (FullWarningCollection.TryGetValue(fastaFileName, out var warningList))
{
return warningList;
}
return new List<ErrorInfoExtended>();
}
/// <summary>
/// Return a count of the number of files with an error
/// </summary>
public int NumFilesWithErrors
{
get
{
if (FullWarningCollection == null)
{
return 0;
}
return FullErrorCollection.Count;
}
}
private void RecordFastaFileProblem(
int lineNumber,
string proteinName,
int errorMessageCode,
string extraInfo,
ValidationMessageTypes messageType)
{
var msgString = LookupMessageDescription(errorMessageCode, extraInfo);
RecordFastaFileProblemToHash(lineNumber, proteinName, msgString, extraInfo, messageType);
}
private void RecordFastaFileProblem(
int lineNumber,
string proteinName,
string errorMessage,
string extraInfo,
ValidationMessageTypes messageType)
{
RecordFastaFileProblemToHash(lineNumber, proteinName, errorMessage, extraInfo, messageType);
}
private void RecordFastaFileProblemToHash(
int lineNumber,
string proteinName,
string messageString,
string extraInfo,
ValidationMessageTypes messageType)
{
if (!mFastaFilePath.Equals(mCachedFastaFilePath))
{
// New File being analyzed
mCurrentFileErrors.Clear();
mCurrentFileWarnings.Clear();
mCachedFastaFilePath = mFastaFilePath;
}
if (messageType == ValidationMessageTypes.WarningMsg)
{
// Treat as warning
mCurrentFileWarnings.Add(new ErrorInfoExtended(
lineNumber, proteinName, messageString, extraInfo, "Warning"));
// Add/update the dictionary
FullWarningCollection[Path.GetFileName(mCachedFastaFilePath)] = mCurrentFileWarnings;
}
else
{
// Treat as error
mCurrentFileErrors.Add(new ErrorInfoExtended(
lineNumber, proteinName, messageString, extraInfo, "Error"));
// Add/update the dictionary
FullErrorCollection[Path.GetFileName(mCachedFastaFilePath)] = mCurrentFileErrors;
}
}
/// <summary>
/// Set validation options
/// </summary>
/// <param name="eValidationOptionName"></param>
/// <param name="enabled"></param>
public void SetValidationOptions(ValidationOptionConstants eValidationOptionName, bool enabled)
{
mValidationOptions[(int)eValidationOptionName] = enabled;
}
/// <summary>
/// Calls SimpleProcessFile(), which calls ValidateFastaFile.ProcessFile to validate filePath
/// </summary>
/// <param name="filePath"></param>
/// <returns>True if the file was successfully processed (even if it contains errors)</returns>
public bool StartValidateFASTAFile(string filePath)
{
var success = SimpleProcessFile(filePath);
if (!success)
return false;
if (GetErrorWarningCounts(MsgTypeConstants.WarningMsg, ErrorWarningCountTypes.Total) > 0)
{
// The file has warnings; we need to record them using RecordFastaFileProblem
var warnings = GetFileWarnings();
foreach (var item in warnings)
{
RecordFastaFileProblem(item.LineNumber, item.ProteinName, item.MessageCode, string.Empty, ValidationMessageTypes.WarningMsg);
}
}
if (GetErrorWarningCounts(MsgTypeConstants.ErrorMsg, ErrorWarningCountTypes.Total) <= 0)
return true;
// The file has errors; we need to record them using RecordFastaFileProblem
// However, we might ignore some of the errors
foreach (var item in GetFileErrors())
{
var errorMessage = LookupMessageDescription(item.MessageCode, item.ExtraInfo);
var ignoreError = false;
switch (errorMessage)
{
case MESSAGE_TEXT_ASTERISK_IN_RESIDUES:
if (mValidationOptions[(int)ValidationOptionConstants.AllowAsterisksInResidues])
{
ignoreError = true;
}
break;
case MESSAGE_TEXT_DASH_IN_RESIDUES:
if (mValidationOptions[(int)ValidationOptionConstants.AllowDashInResidues])
{
ignoreError = true;
}
break;
}
if (!ignoreError)
{
RecordFastaFileProblem(item.LineNumber, item.ProteinName, errorMessage, item.ExtraInfo, ValidationMessageTypes.ErrorMsg);
}
}
return true;
}
}
}