-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
502 lines (417 loc) · 24.4 KB
/
Copy pathProgram.cs
File metadata and controls
502 lines (417 loc) · 24.4 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using PRISM;
namespace ValidateFastaFile
{
/// <summary>
/// This program will read in a FASTA file and write out stats on the number of proteins and number of residues
/// It will also validate the protein name, descriptions, and sequences in the file
/// </summary>
/// <remarks>
/// <para>
/// Written by Matthew Monroe for the Department of Energy (PNNL, Richland, WA)
/// Program started March 21, 2005
/// </para>
/// <para>
/// E-mail: matthew.monroe@pnnl.gov or proteomics@pnnl.gov
/// Website: https://github.com/PNNL-Comp-Mass-Spec/ or https://panomics.pnnl.gov/ or https://www.pnnl.gov/integrative-omics
/// </para>
/// <para>
/// Licensed under the Apache License, Version 2.0; you may not use this file except
/// in compliance with the License. You may obtain a copy of the License at
/// http://www.apache.org/licenses/LICENSE-2.0
/// </para>
/// </remarks>
internal static class Program
{
// Ignore Spelling: isoleucine, leucine, parseable, pre
public const string PROGRAM_DATE = "April 27, 2022";
private static string mInputFilePath;
private static string mOutputDirectoryPath;
private static string mParameterFilePath;
private static bool mUseStatsFile;
private static bool mGenerateFixedFastaFile;
private static bool mCheckForDuplicateProteinNames;
private static bool mCheckForDuplicateProteinSequences;
private static bool mFixedFastaRenameDuplicateNameProteins;
private static bool mFixedFastaKeepDuplicateNamedProteins;
private static bool mFixedFastaConsolidateDuplicateProteinSeqs;
private static bool mFixedFastaConsolidateDupsIgnoreILDiff;
private static bool mFixedFastaRemoveInvalidResidues;
private static bool mAllowAsterisk;
private static bool mAllowDash;
private static bool mSaveBasicProteinHashInfoFile;
private static string mProteinHashFilePath;
private static bool mCreateModelXMLParameterFile;
private static bool mRecurseDirectories;
private static int mMaxLevelsToRecurse;
private static FastaValidator mValidateFastaFile;
private static DateTime mLastProgressReportPctTime;
private static DateTime mLastProgressReportTime;
private static int mLastProgressReportValue;
/// <summary>
/// Entry method
/// </summary>
/// <returns>0 if no error, error code if an error</returns>
public static int Main()
{
var commandLineParser = new clsParseCommandLine();
mInputFilePath = string.Empty;
mOutputDirectoryPath = string.Empty;
mParameterFilePath = string.Empty;
mUseStatsFile = false;
mGenerateFixedFastaFile = false;
mCheckForDuplicateProteinNames = true;
mCheckForDuplicateProteinSequences = true;
mFixedFastaRenameDuplicateNameProteins = false;
mFixedFastaKeepDuplicateNamedProteins = false;
mFixedFastaConsolidateDuplicateProteinSeqs = false;
mFixedFastaConsolidateDupsIgnoreILDiff = false;
mAllowAsterisk = false;
mAllowDash = false;
mSaveBasicProteinHashInfoFile = false;
mProteinHashFilePath = string.Empty;
mRecurseDirectories = false;
mMaxLevelsToRecurse = 0;
mLastProgressReportPctTime = DateTime.UtcNow;
mLastProgressReportTime = DateTime.UtcNow;
try
{
var proceed = commandLineParser.ParseCommandLine() && SetOptionsUsingCommandLineParameters(commandLineParser);
if (proceed && !commandLineParser.NeedToShowHelp && mCreateModelXMLParameterFile)
{
if (string.IsNullOrEmpty(mParameterFilePath))
{
mParameterFilePath = Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location) + "_ModelSettings.xml";
}
mValidateFastaFile = new FastaValidator();
mValidateFastaFile.SaveSettingsToParameterFile(mParameterFilePath);
Console.WriteLine();
Console.WriteLine("Created example XML parameter file: ");
Console.WriteLine(" " + mParameterFilePath);
Console.WriteLine();
return 0;
}
if (!proceed || commandLineParser.NeedToShowHelp || mInputFilePath.Length == 0)
{
ShowProgramHelp();
return -1;
}
mValidateFastaFile = new FastaValidator();
mValidateFastaFile.ProgressUpdate += ValidateFastaFile_ProgressChanged;
mValidateFastaFile.ProgressReset += ValidateFastaFile_ProgressReset;
mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.OutputToStatsFile, mUseStatsFile);
mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.GenerateFixedFASTAFile, mGenerateFixedFastaFile);
// Also use mGenerateFixedFastaFile to set SaveProteinSequenceHashInfoFiles
mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.SaveProteinSequenceHashInfoFiles, mGenerateFixedFastaFile);
mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.FixedFastaRenameDuplicateNameProteins, mFixedFastaRenameDuplicateNameProteins);
mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.FixedFastaKeepDuplicateNamedProteins, mFixedFastaKeepDuplicateNamedProteins);
mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.FixedFastaConsolidateDuplicateProteinSeqs, mFixedFastaConsolidateDuplicateProteinSeqs);
mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.FixedFastaConsolidateDupsIgnoreILDiff, mFixedFastaConsolidateDupsIgnoreILDiff);
mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.FixedFastaRemoveInvalidResidues, mFixedFastaRemoveInvalidResidues);
mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.AllowAsteriskInResidues, mAllowAsterisk);
mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.AllowDashInResidues, mAllowDash);
mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.SaveBasicProteinHashInfoFile, mSaveBasicProteinHashInfoFile);
mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.CheckForDuplicateProteinNames, mCheckForDuplicateProteinNames);
mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.CheckForDuplicateProteinSequences, mCheckForDuplicateProteinSequences);
// Update the rules based on the options that were set above
mValidateFastaFile.SetDefaultRules();
mValidateFastaFile.ExistingProteinHashFile = mProteinHashFilePath;
mValidateFastaFile.SkipConsoleWriteIfNoProgressListener = true;
// Note: the following settings will be overridden if mParameterFilePath points to a valid parameter file that has these settings defined
// mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.AddMissingLineFeedAtEOF, );
// mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.AllowAsteriskInResidues, );
// mValidateFastaFile.MaximumFileErrorsToTrack();
// mValidateFastaFile.MinimumProteinNameLength();
// mValidateFastaFile.MaximumProteinNameLength();
// mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.WarnBlankLinesBetweenProteins, );
// mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.CheckForDuplicateProteinSequences, );
// mValidateFastaFile.SetOptionSwitch(FastaValidator.SwitchOptions.SaveProteinSequenceHashInfoFiles, )
int returnCode;
if (mRecurseDirectories)
{
if (mValidateFastaFile.ProcessFilesAndRecurseDirectories(mInputFilePath, mOutputDirectoryPath, mOutputDirectoryPath, false, mParameterFilePath, mMaxLevelsToRecurse))
{
returnCode = 0;
}
else
{
returnCode = (int)mValidateFastaFile.ErrorCode;
}
}
else if (mValidateFastaFile.ProcessFilesWildcard(mInputFilePath, mOutputDirectoryPath, mParameterFilePath))
{
returnCode = 0;
}
else
{
returnCode = (int)mValidateFastaFile.ErrorCode;
if (returnCode != 0)
{
ShowErrorMessage("Error while processing: " + mValidateFastaFile.GetErrorMessage());
}
}
DisplayProgressPercent(mLastProgressReportValue, true);
return returnCode;
}
catch (Exception ex)
{
ShowErrorMessage("Error occurred in Program->Main: " + ex.Message, ex);
return -1;
}
}
private static void DisplayProgressPercent(int percentComplete, bool addNewline)
{
if (addNewline)
{
Console.WriteLine();
}
if (percentComplete > 100)
percentComplete = 100;
Console.Write("Processing: " + percentComplete.ToString() + "% ");
if (addNewline)
{
Console.WriteLine();
}
}
private static string GetAppVersion()
{
return PRISM.FileProcessor.ProcessFilesOrDirectoriesBase.GetAppVersion(PROGRAM_DATE);
}
private static bool SetOptionsUsingCommandLineParameters(clsParseCommandLine commandLineParser)
{
// Returns True if no problems; otherwise, returns false
var validParameters = new List<string>()
{
"I", "O", "P", "C",
"SkipDupeNameCheck", "SkipDupeSeqCheck",
"F", "R", "D", "L", "V",
"KeepSameName", "AllowDash", "AllowAsterisk",
"B", "HashFile",
"X", "S"
};
try
{
// Make sure no invalid parameters are present
if (commandLineParser.InvalidParametersPresent(validParameters))
{
ShowErrorMessage("Invalid command line parameters",
(from item in commandLineParser.InvalidParameters(validParameters) select ("/" + item)).ToList());
return false;
}
// Query commandLineParser to see if various parameters are present
if (commandLineParser.RetrieveValueForParameter("I", out var inputFile))
{
mInputFilePath = inputFile;
}
else if (commandLineParser.NonSwitchParameterCount > 0)
{
mInputFilePath = commandLineParser.RetrieveNonSwitchParameter(0);
}
if (commandLineParser.RetrieveValueForParameter("O", out var outputDirectory))
mOutputDirectoryPath = outputDirectory;
if (commandLineParser.RetrieveValueForParameter("P", out var parameterFile))
mParameterFilePath = parameterFile;
if (commandLineParser.IsParameterPresent("C"))
mUseStatsFile = true;
if (commandLineParser.IsParameterPresent("SkipDupeNameCheck"))
mCheckForDuplicateProteinNames = false;
if (commandLineParser.IsParameterPresent("SkipDupeSeqCheck"))
mCheckForDuplicateProteinSequences = false;
if (commandLineParser.IsParameterPresent("F"))
mGenerateFixedFastaFile = true;
if (commandLineParser.IsParameterPresent("R"))
mFixedFastaRenameDuplicateNameProteins = true;
if (commandLineParser.IsParameterPresent("D"))
mFixedFastaConsolidateDuplicateProteinSeqs = true;
if (commandLineParser.IsParameterPresent("L"))
mFixedFastaConsolidateDupsIgnoreILDiff = true;
if (commandLineParser.IsParameterPresent("V"))
mFixedFastaRemoveInvalidResidues = true;
if (commandLineParser.IsParameterPresent("KeepSameName"))
mFixedFastaKeepDuplicateNamedProteins = true;
if (commandLineParser.IsParameterPresent("AllowAsterisk"))
mAllowAsterisk = true;
if (commandLineParser.IsParameterPresent("AllowDash"))
mAllowDash = true;
if (commandLineParser.IsParameterPresent("B"))
mSaveBasicProteinHashInfoFile = true;
if (commandLineParser.RetrieveValueForParameter("HashFile", out var createHashFile))
mProteinHashFilePath = createHashFile;
if (commandLineParser.IsParameterPresent("X"))
mCreateModelXMLParameterFile = true;
if (commandLineParser.RetrieveValueForParameter("S", out var recurse))
{
mRecurseDirectories = true;
if (int.TryParse(recurse, out var recurseDepth))
{
mMaxLevelsToRecurse = recurseDepth;
}
}
return true;
}
catch (Exception ex)
{
ShowErrorMessage("Error parsing the command line parameters: " + ex.Message, ex);
}
return false;
}
private static void ShowErrorMessage(string message, Exception ex = null)
{
ConsoleMsgUtils.ShowError(message, ex);
}
private static void ShowErrorMessage(string title, IEnumerable<string> items)
{
ConsoleMsgUtils.ShowErrors(title, items);
}
private static void ShowProgramHelp()
{
try
{
var exeName = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
Console.WriteLine("== Overview ==");
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"This program will read a FASTA File and display statistics on the number of proteins and number of residues. " +
"It will also check that the protein names, descriptions, and sequences are in the correct format."));
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"The program can optionally create a new, fixed version of a FASTA file where proteins with duplicate sequences " +
"have been consolidated, and proteins with duplicate names have been renamed."));
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"To remove duplicates from huge FASTA files (over 1 GB in size), " +
"first create the ProteinHashes.txt file by calling this program with:"));
Console.WriteLine(" {0} Proteins.fasta /B /SkipDupeSeqCheck /SkipDupeNameCheck", exeName);
Console.WriteLine();
Console.WriteLine("Next call the program again, providing the name of the ProteinHashes file:");
Console.WriteLine(" {0} Proteins.fasta /HashFile:Proteins_ProteinHashes.txt", exeName);
Console.WriteLine();
Console.WriteLine("== Program syntax ==");
Console.WriteLine();
Console.WriteLine(exeName);
Console.WriteLine(" /I:InputFilePath.fasta [/O:OutputDirectoryPath]");
Console.WriteLine(" [/P:ParameterFilePath] [/C] ");
Console.WriteLine(" [/F] [/R] [/D] [/L] [/V] [/KeepSameName]");
Console.WriteLine(" [/AllowDash] [/AllowAsterisk]");
Console.WriteLine(" [/SkipDupeNameCheck] [/SkipDupeSeqCheck]");
Console.WriteLine(" [/B] [/HashFile]");
Console.WriteLine(" [/X] [/S:[MaxLevel]]");
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"The input file path can contain the wildcard character * and should point to a FASTA file."));
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"The output directory path is optional, and is only used if /C is used. If omitted, the output stats file " +
"will be created in the directory containing the .Exe file."));
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"The parameter file path is optional. If included, it should point to a valid XML parameter file."));
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"Use /C to specify that an output file should be created, rather than displaying the results on the screen."));
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"Use /F to generate a new, fixed .Fasta file (long protein names will be auto-shortened). " +
"At the same time, a file with protein names and hash values for each unique protein sequences " +
"will be generated (_UniqueProteinSeqs.txt). This file will also list the other proteins " +
"that have duplicate sequences as the first protein mapped to each sequence. If duplicate sequences " +
"are found, then an easily parseable mapping file will also be created (_UniqueProteinSeqDuplicates.txt)."));
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"Use /R to rename proteins with duplicate names when using /F to generate a fixed FASTA file."));
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"Use /D to consolidate proteins with duplicate protein sequences when using /F to generate a fixed FASTA file."));
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"Use /L to ignore I/L (isoleucine vs. leucine) differences when consolidating proteins " +
"with duplicate protein sequences while generating a fixed FASTA file."));
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"Use /V to remove invalid residues (non-letter characters, including an asterisk) when using /F to generate a fixed FASTA file."));
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"Use /KeepSameName to keep proteins with the same name but differing sequences when using /F to generate a fixed FASTA file " +
"(if they have the same name and same sequence, then will only retain one entry); ignored if /R or /D is used"));
Console.WriteLine();
Console.WriteLine("Use /AllowDash to allow a - in residues");
Console.WriteLine("Use /AllowAsterisk to allow * in residues");
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"When parsing large FASTA files, you can reduce the memory used by disabling the checking for duplicates"));
Console.WriteLine(" /SkipDupeSeqCheck disables duplicate sequence checking (large memory footprint)");
Console.WriteLine(" /SkipDupeNameCheck disables duplicate name checking (small memory footprint)");
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"Use /B to save a hash info file (even if not consolidating duplicates). " +
"This is useful for parsing a large FASTA file to obtain the sequence hash for each protein " +
"(hash values are not cached in memory, thus small memory footprint)."));
Console.WriteLine();
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"Use /HashFile to specify a pre-computed hash file to use for determining which proteins to keep when generating a fixed FASTA file"));
Console.WriteLine(ConsoleMsgUtils.WrapParagraph("Use of /HashFile automatically enables /F and automatically disables /D, /R, and /B"));
Console.WriteLine();
Console.WriteLine("Use /X to specify that a model XML parameter file should be created.");
Console.WriteLine(ConsoleMsgUtils.WrapParagraph(
"Use /S to process all valid files in the input directory and subdirectories. " +
"Include a number after /S (like /S:2) to limit the level of subdirectories to examine."));
Console.WriteLine();
Console.WriteLine("Program written by Matthew Monroe for the Department of Energy (PNNL, Richland, WA)");
Console.WriteLine("Version: " + GetAppVersion());
Console.WriteLine();
Console.WriteLine("E-mail: matthew.monroe@pnnl.gov or proteomics@pnnl.gov");
Console.WriteLine("Website: https://github.com/PNNL-Comp-Mass-Spec/ or https://panomics.pnnl.gov/ or https://www.pnnl.gov/integrative-omics");
Console.WriteLine();
// Delay for 750 msec in case the user double clicked this file from within Windows Explorer (or started the program via a shortcut)
Thread.Sleep(750);
}
catch (Exception ex)
{
ShowErrorMessage("Error displaying the program syntax: " + ex.Message, ex);
}
}
private static void ValidateFastaFile_ProgressChanged(string taskDescription, float percentComplete)
{
const int PERCENT_REPORT_INTERVAL = 25;
const int PROGRESS_DOT_INTERVAL_MSEC = 500;
if (percentComplete >= mLastProgressReportValue ||
DateTime.UtcNow.Subtract(mLastProgressReportPctTime).TotalSeconds >= 30)
{
mLastProgressReportPctTime = DateTime.UtcNow;
if (mLastProgressReportValue > 0)
{
Console.WriteLine();
}
if (percentComplete < mLastProgressReportValue)
{
DisplayProgressPercent((int)Math.Round(percentComplete, 0), false);
}
else
{
DisplayProgressPercent(mLastProgressReportValue, false);
}
while (percentComplete >= mLastProgressReportValue)
{
mLastProgressReportValue += PERCENT_REPORT_INTERVAL;
}
mLastProgressReportTime = DateTime.UtcNow;
}
else if (DateTime.UtcNow.Subtract(mLastProgressReportTime).TotalMilliseconds >= PROGRESS_DOT_INTERVAL_MSEC)
{
mLastProgressReportTime = DateTime.UtcNow;
Console.Write(".");
}
}
private static void ValidateFastaFile_ProgressReset()
{
mLastProgressReportTime = DateTime.UtcNow;
mLastProgressReportPctTime = DateTime.UtcNow;
mLastProgressReportValue = 0;
}
}
}