/// <summary> /// Process the given file or folder /// </summary> /// <returns>0 if success, otherwise an error code</returns> /// <remarks>Folder could either be a supported mass spec data folder, or a normal directory with several supported data files</remarks> public int Run() { // Normalize the input path. Only affects paths to a file/folder in a folder-type dataset Parameters.InputPath = MassSpecDataReaderFactory.NormalizeDatasetPath(Parameters.InputPath); var attr = File.GetAttributes(Parameters.InputPath); int errorCode; if ((attr & FileAttributes.Directory) == FileAttributes.Directory && !MassSpecDataReaderFactory.IsADirectoryDataset(Parameters.InputPath)) { errorCode = ProcessDirectory(Parameters.InputPath); } else { if (!MsRawFile(Parameters.InputPath) && !MsPbfFile(Parameters.InputPath)) { ShowErrorMessage(@"File extension not supported, " + Parameters.InputPath); return(-1); } errorCode = ProcessFile(Parameters.InputPath); } return(errorCode); }
public bool Validate() { // Check for folder-type datasets, and replace specFilePath with the directory name if it is. SourcePath = MassSpecDataReaderFactory.GetDatasetName(SourcePath); var isDirectoryDataset = MassSpecDataReaderFactory.IsADirectoryDataset(SourcePath); // True if specFilePath is a directory that is NOT a supported folder-type dataset. var specPathIsDirectory = Directory.Exists(SourcePath) && !isDirectoryDataset; if (!File.Exists(SourcePath) && !specPathIsDirectory && !isDirectoryDataset) { PrintError("File not found: " + SourcePath); return(false); } var types = MassSpecDataReaderFactory.MassSpecDataTypeFilterList; types.Remove(".pbf"); if (!specPathIsDirectory && !(types.Select(ext => SourcePath.ToLower().EndsWith(ext)).Any())) { PrintError("Invalid file extension: (" + Path.GetExtension(SourcePath) + ") " + SourcePath); return(false); } if (string.IsNullOrWhiteSpace(OutputDir)) { // Must use "Path.GetFullPath" to return the absolute path when the source file is in the working directory // But, it could cause problems with too-long paths. OutputDir = specPathIsDirectory ? SourcePath : Path.GetDirectoryName(Path.GetFullPath(SourcePath)); } if (string.IsNullOrWhiteSpace(OutputDir)) { PrintError("Invalid output file directory: " + OutputDir); return(false); } if (EndScan < StartScan && EndScan > -1) { PrintError($"End scan ({EndScan}) must not be less than start scan ({StartScan})!"); return(false); } if (!Directory.Exists(OutputDir)) { if (File.Exists(OutputDir) && !File.GetAttributes(OutputDir).HasFlag(FileAttributes.Directory)) { PrintError("OutputDir \"" + OutputDir + "\" is not a directory!"); return(false); } Directory.CreateDirectory(OutputDir); } return(true); }
public static int Main(string[] args) { string specFilePath; string outputDir; try { var handle = Process.GetCurrentProcess().MainWindowHandle; SetConsoleMode(handle, EnableExtendedFlags); if (args.Length == 0) { PrintUsageInfo(); return(-1); } var paramDic = new Dictionary <string, string> { { "-s", null }, { "-o", null } }; for (var i = 0; i < args.Length / 2; i++) { var key = args[2 * i]; var value = args[2 * i + 1]; if (!paramDic.ContainsKey(key)) { PrintUsageInfo("Invalid parameter: " + key); return(-1); } paramDic[key] = value; } // Parse command line parameters specFilePath = paramDic["-s"]; if (specFilePath == null) { PrintUsageInfo("Missing required parameter -s!"); return(-1); } // Check for folder-type datasets, and replace specFilePath with the directory name if it is. specFilePath = MassSpecDataReaderFactory.GetDatasetName(specFilePath); var isDirectoryDataset = MassSpecDataReaderFactory.IsADirectoryDataset(specFilePath); // True if specFilePath is a directory that is NOT a supported folder-type dataset. var specPathIsDirectory = Directory.Exists(specFilePath) && !isDirectoryDataset; if (!File.Exists(specFilePath) && !specPathIsDirectory && !isDirectoryDataset) { PrintUsageInfo("File not found: " + specFilePath); return(-1); } var types = MassSpecDataReaderFactory.MassSpecDataTypeFilterList; types.Remove(".pbf"); if (!specPathIsDirectory && !(types.Select(ext => specFilePath.ToLower().EndsWith(ext)).Any())) { PrintUsageInfo("Invalid file extension: (" + Path.GetExtension(specFilePath) + ") " + specFilePath); return(-1); } // Must use "Path.GetFullPath" to return the absolute path when the source file is in the working directory // But, it could cause problems with too-long paths. outputDir = paramDic["-o"] ?? (specPathIsDirectory ? specFilePath : Path.GetDirectoryName(Path.GetFullPath(specFilePath))); if (outputDir == null) { PrintUsageInfo("Invalid output file directory: " + specFilePath); return(-1); } if (!Directory.Exists(outputDir)) { if (File.Exists(outputDir) && !File.GetAttributes(outputDir).HasFlag(FileAttributes.Directory)) { PrintUsageInfo("OutputDir " + outputDir + " is not a directory!"); return(-1); } Directory.CreateDirectory(outputDir); } } catch (Exception ex) { Console.WriteLine("Exception while parsing the command line parameters: " + ex.Message); return(-5); } #if (!DEBUG) try { #endif string[] specFilePaths = new[] { specFilePath }; if (Directory.Exists(specFilePath) && !MassSpecDataReaderFactory.IsADirectoryDataset(specFilePath)) { specFilePaths = Directory.GetFiles(specFilePath, "*.raw"); } foreach (var rawFilePath in specFilePaths) { var pbfFilePath = Path.Combine(outputDir, Path.GetFileNameWithoutExtension(rawFilePath) + PbfLcMsRun.FileExtension); bool isCurrent; if (File.Exists(pbfFilePath) && PbfLcMsRun.CheckFileFormatVersion(pbfFilePath, out isCurrent) && isCurrent) { Console.WriteLine("{0} already exists.", pbfFilePath); continue; } Console.WriteLine("Creating {0} from {1}", pbfFilePath, rawFilePath); IMassSpecDataReader reader = MassSpecDataReaderFactory.GetMassSpecDataReader(rawFilePath); var progress = new Progress <ProgressData>(p => { p.UpdateFrequencySeconds = 2; if ((p.Percent % 25).Equals(0) || p.ShouldUpdate()) { Console.Write("\r{0}, {1:00.0}% complete ", p.Status, p.Percent); } }); var run = new PbfLcMsRun(rawFilePath, reader, pbfFilePath, 0, 0, progress); Console.WriteLine(); } Console.WriteLine("PbfFormatVersion: {0}", PbfLcMsRun.FileFormatId); return(0); #if (!DEBUG) } catch (Exception ex) { // NOTE: The DMS Analysis Manager looks for this text; do not change it Console.WriteLine("Exception while processing: " + ex.Message); Console.WriteLine(ex.StackTrace); var errorCode = -Math.Abs(ex.Message.GetHashCode()); if (errorCode == 0) { return(-1); } else { return(errorCode); } } #endif }
public static int Main(string[] args) { PbfGenInputParameters options; try { var osVersionInfo = new clsOSVersionInfo(); if (osVersionInfo.GetOSVersion().ToLower().Contains("windows")) { var handle = Process.GetCurrentProcess().MainWindowHandle; SetConsoleMode(handle, EnableExtendedFlags); } var exeName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name; var parser = new CommandLineParser <PbfGenInputParameters>(exeName, Version); parser.UsageExamples.Add($"Using -start and -end to limit the scan range to include in the .pbf file\n\t{exeName}.exe -s Dataset.raw -start 2000 -end 3000"); var results = parser.ParseArgs(args); if (!results.Success) { // Wait for 1.5 seconds System.Threading.Thread.Sleep(1500); return(-1); } if (!results.ParsedResults.Validate()) { parser.PrintHelp(); // Wait for 1.5 seconds System.Threading.Thread.Sleep(1500); return(-1); } options = results.ParsedResults; } catch (Exception ex) { Console.WriteLine("Exception while parsing the command line parameters: " + ex.Message); return(-5); } #if (!DEBUG) try #endif { var specFilePaths = new[] { options.SourcePath }; if (Directory.Exists(options.SourcePath) && !MassSpecDataReaderFactory.IsADirectoryDataset(options.SourcePath)) { specFilePaths = Directory.GetFiles(options.SourcePath, "*.raw"); // TODO: Support folders with other formats in them too... } foreach (var rawFilePath in specFilePaths) { var pbfFileName = MassSpecDataReaderFactory.ChangeExtension(rawFilePath, PbfLcMsRun.FileExtensionConst); var pbfFilePath = Path.Combine(options.OutputDir, Path.GetFileName(pbfFileName)); if (File.Exists(pbfFilePath) && PbfLcMsRun.CheckFileFormatVersion(pbfFilePath, out var isCurrent) && isCurrent) { Console.WriteLine("{0} already exists.", pbfFilePath); continue; } Console.WriteLine("Creating {0} from {1}", pbfFilePath, rawFilePath); if (options.StartScan > 0 && options.EndScan > 0) { Console.WriteLine("Only including scans {0} to {1}", options.StartScan, options.EndScan); } else if (options.StartScan > 0) { Console.WriteLine("Only including scans {0} to the end", options.StartScan); } else if (options.EndScan > 0) { Console.WriteLine("Only including scans 1 to {0}", options.EndScan); } var reader = MassSpecDataReaderFactory.GetMassSpecDataReader(rawFilePath); var progress = new Progress <ProgressData>(p => { p.UpdateFrequencySeconds = 2; if ((p.Percent % 25).Equals(0) || p.ShouldUpdate()) { Console.Write("\r{0}, {1:00.0}% complete ", p.Status, p.Percent); } }); var run = new PbfLcMsRun(rawFilePath, reader, pbfFilePath, 0, 0, progress, false, options.StartScan, options.EndScan); Console.WriteLine(); } Console.WriteLine("PbfFormatVersion: {0}", PbfLcMsRun.FileFormatId); return(0); } #if (!DEBUG) catch (Exception ex) { // NOTE: The DMS Analysis Manager looks for this text; do not change it Console.WriteLine("Exception while processing: " + ex.Message); Console.WriteLine(ex.StackTrace); var errorCode = -Math.Abs(ex.Message.GetHashCode()); if (errorCode == 0) { return(-1); } else { return(errorCode); } } #endif }
public bool Validate() { // Spec file path validation if (string.IsNullOrWhiteSpace(SpecFilePath)) { PrintError("Missing parameter for spectrum file path"); return(false); } // Check for folder-type datasets, and replace specFilePath with the directory name if it is. SpecFilePath = MassSpecDataReaderFactory.GetDatasetName(SpecFilePath); var isDirectoryDataset = MassSpecDataReaderFactory.IsADirectoryDataset(SpecFilePath); // True if specFilePath is a directory that is NOT a supported folder-type dataset. var specPathIsDirectory = Directory.Exists(SpecFilePath) && !isDirectoryDataset; if (!File.Exists(SpecFilePath) && !specPathIsDirectory && !isDirectoryDataset) { PrintError("File not found: " + SpecFilePath); return(false); } var types = MassSpecDataReaderFactory.MassSpecDataTypeFilterList; if (!specPathIsDirectory && !(types.Select(ext => SpecFilePath.ToLower().EndsWith(ext)).Any())) { PrintError("Invalid file extension for spectrum file: (" + Path.GetExtension(SpecFilePath) + ") " + SpecFilePath); return(false); } // TODO: Handle non-.raw files in the subfolder SpecFilePaths = Directory.Exists(SpecFilePath) && !MassSpecDataReaderFactory.IsADirectoryDataset(SpecFilePath) ? Directory.GetFiles(SpecFilePath, "*.raw") : new[] { SpecFilePath }; // Database path validation if (string.IsNullOrWhiteSpace(DatabaseFilePath)) { PrintError("Missing parameter for database file path"); return(false); } if (!File.Exists(DatabaseFilePath)) { PrintError("File not found: " + DatabaseFilePath); return(false); } var dbExtension = Path.GetExtension(DatabaseFilePath).ToLower(); if (!dbExtension.Equals(".fa") && !dbExtension.Equals(".fasta")) { PrintError("Invalid extension for the database file path (" + dbExtension + ")"); return(false); } // Output directory validation if (string.IsNullOrWhiteSpace(OutputDir)) { // Must use "Path.GetFullPath" to return the absolute path when the source file is in the working directory // But, it could cause problems with too-long paths. OutputDir = specPathIsDirectory ? SpecFilePath : Path.GetDirectoryName(Path.GetFullPath(SpecFilePath)); } if (string.IsNullOrWhiteSpace(OutputDir)) { PrintError("Invalid output file directory: " + OutputDir); return(false); } if (!Directory.Exists(OutputDir)) { if (File.Exists(OutputDir) && !File.GetAttributes(OutputDir).HasFlag(FileAttributes.Directory)) { PrintError("OutputDir \"" + OutputDir + "\" is not a directory!"); return(false); } Directory.CreateDirectory(OutputDir); } // Mods file validation if (!string.IsNullOrWhiteSpace(ModsFilePath) && !File.Exists(ModsFilePath)) { PrintError("Modifications file not found: " + ModsFilePath); return(false); } try { var errorMessage = LoadModsFile(ModsFilePath); if (!string.IsNullOrWhiteSpace(errorMessage)) { PrintError(errorMessage); return(false); } } catch (Exception ex) { PrintError("Exception parsing the file for parameter -mod: " + ex.Message); return(false); } // Scans file validation if (!string.IsNullOrWhiteSpace(ScansFilePath) && !File.Exists(ScansFilePath)) { PrintError("Scans File file not found: " + ScansFilePath); return(false); } try { var errorMessage = LoadScansFile(ScansFilePath); if (!string.IsNullOrWhiteSpace(errorMessage)) { PrintError(errorMessage); return(false); } } catch (Exception ex) { PrintError("Exception parsing the file for parameter -scansFile: " + ex.Message); return(false); } // Feature file validation if (!string.IsNullOrWhiteSpace(FeatureFilePath) && !File.Exists(FeatureFilePath)) { PrintError("Feature File not found: " + FeatureFilePath); return(false); } if (!string.IsNullOrWhiteSpace(FeatureFilePath) && !Path.GetExtension(FeatureFilePath).ToLower().Equals(".csv") && !Path.GetExtension(FeatureFilePath).ToLower().Equals(".ms1ft") && !Path.GetExtension(FeatureFilePath).ToLower().Equals(".msalign")) { PrintError("Invalid extension for the Feature file path (" + Path.GetExtension(FeatureFilePath) + ")"); return(false); } // MinX/MaxX validation if (MinSequenceLength > MaxSequenceLength) { PrintError("MinPrecursorCharge (" + MinPrecursorIonCharge + ") is larger than MaxPrecursorCharge (" + MaxPrecursorIonCharge + ")!"); return(false); } if (MinProductIonCharge > MaxProductIonCharge) { PrintError("MinFragmentCharge (" + MinProductIonCharge + ") is larger than MaxFragmentCharge (" + MaxProductIonCharge + ")!"); return(false); } if (MinSequenceMass > MaxSequenceMass) { PrintError("MinSequenceMassInDa (" + MinSequenceMass + ") is larger than MaxSequenceMassInDa (" + MaxSequenceMass + ")!"); return(false); } MaxNumThreads = GetOptimalMaxThreads(MaxNumThreads); return(true); }