Пример #1
0
        public static void Main(string[] args)
        {
            string rawFilePath        = null;
            string outputDirectory    = null;
            string outputFile         = null;
            string outputFormatString = null;
            var    outputFormat       = OutputFormat.NONE;
            var    gzip = false;
            string outputMetadataString = null;
            var    outputMetadataFormat = MetadataFormat.NONE;
            string s3url                  = null;
            string s3AccessKeyId          = null;
            string s3SecretAccessKey      = null;
            var    verbose                = false;
            string bucketName             = null;
            var    ignoreInstrumentErrors = false;
            var    noPeakPicking          = false;
            var    help    = false;
            var    version = false;

            var optionSet = new OptionSet
            {
                {
                    "h|help", "Prints out the options.",
                    h => help = h != null
                },
                {
                    "version", "Prints out the library version.",
                    v => version = v != null
                },
                {
                    "i=|input=", "The raw file input.",
                    v => rawFilePath = v
                },
                {
                    "o=|output=", "The output directory. Specify this or an output file.",
                    v => outputDirectory = v
                },
                {
                    "b=|output_file", "The output file. Specify this or an output directory",
                    v => outputFile = v
                },
                {
                    "f=|format=",
                    "The output format for the spectra (0 for MGF, 1 for mzMl, 2 for indexed mzML, 3 for Parquet)",
                    v => outputFormatString = v
                },
                {
                    "m=|metadata=", "The metadata output format (0 for JSON, 1 for TXT).",
                    v => outputMetadataString = v
                },
                {
                    "g|gzip", "GZip the output file if this flag is specified (without value).",
                    v => gzip = v != null
                },
                {
                    "p|noPeakPicking",
                    "Don't use the peak picking provided by the native thermo library (by default peak picking is enabled)",
                    v => noPeakPicking = v != null
                },
                {
                    "v|verbose", "Enable verbose logging.",
                    v => verbose = v != null
                },
                {
                    "e|ignoreInstrumentErrors", "Ignore missing properties by the instrument.",
                    v => ignoreInstrumentErrors = v != null
                },
                {
                    "u:|s3_url:",
                    "Optional property to write directly the data into S3 Storage.",
                    v => s3url = v
                },
                {
                    "k:|s3_accesskeyid:",
                    "Optional key for the S3 bucket to write the file output.",
                    v => s3AccessKeyId = v
                },
                {
                    "t:|s3_secretaccesskey:",
                    "Optional key for the S3 bucket to write the file output.",
                    v => s3SecretAccessKey = v
                },
                {
                    "n:|s3_bucketName:",
                    "S3 bucket name",
                    v => bucketName = v
                }
            };

            try
            {
                // parse the command line
                var extra = optionSet.Parse(args);

                if (!extra.IsNullOrEmpty())
                {
                    throw new OptionException("unexpected extra arguments", null);
                }

                if (help)
                {
                    ShowHelp(" usage is (use -option=value for the optional arguments):", null,
                             optionSet);
                    return;
                }

                if (version)
                {
                    Console.WriteLine(Version);
                    return;
                }

                if (outputMetadataString == null && outputFormatString == null)
                {
                    throw new OptionException("The parameter -f or -m should be provided",
                                              "-f|--format , -m|--format");
                }

                if (outputFormatString != null)
                {
                    int outPutFormatInt;
                    try
                    {
                        outPutFormatInt = int.Parse(outputFormatString);
                    }
                    catch (FormatException e)
                    {
                        throw new OptionException(
                                  "unknown output format value (0 for MGF, 1 for mzMl, 2 for indexed mzML, 3 for Parquet)",
                                  "-f, --format");
                    }

                    if (Enum.IsDefined(typeof(OutputFormat), outPutFormatInt) &&
                        ((OutputFormat)outPutFormatInt) != OutputFormat.NONE)
                    {
                        outputFormat = (OutputFormat)outPutFormatInt;
                    }
                    else
                    {
                        throw new OptionException(
                                  "unknown output format value (0 for MGF, 1 for mzMl, 2 for indexed mzML, 3 for Parquet)",
                                  "-f, --format");
                    }
                }

                if (outputMetadataString != null)
                {
                    int metadataInt;
                    try
                    {
                        metadataInt = int.Parse(outputMetadataString);
                    }
                    catch (FormatException e)
                    {
                        throw new OptionException("unknown metadata format value (0 for JSON, 1 for TXT)",
                                                  "-m, --metadata");
                    }

                    if (Enum.IsDefined(typeof(MetadataFormat), metadataInt) &&
                        ((MetadataFormat)metadataInt) != MetadataFormat.NONE)
                    {
                        outputMetadataFormat = (MetadataFormat)metadataInt;
                    }
                    else
                    {
                        throw new OptionException("unknown metadata format value (0 for JSON, 1 for TXT)",
                                                  "-m, --metadata");
                    }
                }

                if (outputFile == null && outputDirectory == null)
                {
                    throw new OptionException(
                              "specify an output directory or output file",
                              "-o, --output or -b, --output_file");
                }

                if (outputFile != null && Directory.Exists(outputFile))
                {
                    throw new OptionException(
                              "specify a valid output file, not a directory",
                              "-b, --output_file");
                }

                if (outputDirectory != null && !Directory.Exists(outputDirectory))
                {
                    throw new OptionException(
                              "specify a valid output directory",
                              "-o, --output");
                }
            }
            catch (OptionException optionException)
            {
                ShowHelp("Error - usage is (use -option=value for the optional arguments):", optionException,
                         optionSet);
            }
            catch (ArgumentNullException argumentNullException)
            {
                if (help)
                {
                    ShowHelp(" usage is (use -option=value for the optional arguments):", null,
                             optionSet);
                }
                else
                {
                    ShowHelp("Error - usage is (use -option=value for the optional arguments):", null,
                             optionSet);
                }
            }

            try
            {
                if (verbose)
                {
                    ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Level =
                        Level.Debug;
                    ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository())
                    .RaiseConfigurationChanged(EventArgs.Empty);
                }

                var parseInput = new ParseInput(rawFilePath, outputDirectory, outputFile, outputFormat, gzip,
                                                outputMetadataFormat,
                                                s3url, s3AccessKeyId, s3SecretAccessKey, bucketName, ignoreInstrumentErrors, noPeakPicking,
                                                verbose);
                RawFileParser.Parse(parseInput);
            }
            catch (Exception ex)
            {
                Log.Error("An unexpected error occured:");
                Log.Error(ex.ToString());

                Environment.Exit(1);
            }
        }
Пример #2
0
        private static void RegularParametersParsing(string[] args)
        {
            var    help                 = false;
            var    version              = false;
            string outputFormatString   = null;
            string metadataFormatString = null;
            string logFormatString      = null;
            var    parseInput           = new ParseInput();

            var optionSet = new OptionSet
            {
                {
                    "h|help", "Prints out the options.",
                    h => help = h != null
                },
                {
                    "version", "Prints out the library version.",
                    v => version = v != null
                },
                {
                    "i=|input=", "The raw file input (Required). Specify this or an input directory -d.",
                    v => parseInput.RawFilePath = v
                },
                {
                    "d=|input_directory=",
                    "The directory containing the raw files (Required). Specify this or an input raw file -i.",
                    v => parseInput.RawDirectoryPath = v
                },
                {
                    "o=|output=",
                    "The output directory. Specify this or an output file -b. Specifying neither writes to the input directory.",
                    v => parseInput.OutputDirectory = v
                },
                {
                    "b=|output_file",
                    "The output file. Specify this or an output directory -o. Specifying neither writes to the input directory.",
                    v => parseInput.OutputFile = v
                },
                {
                    "f=|format=",
                    "The spectra output format: 0 for MGF, 1 for mzML, 2 for indexed mzML, 3 for Parquet. Defaults to mzML if no format is specified.",
                    v => outputFormatString = v
                },
                {
                    "m=|metadata=", "The metadata output format: 0 for JSON, 1 for TXT.",
                    v => metadataFormatString = v
                },
                {
                    "c=|metadata_output_file",
                    "The metadata output file. By default the metadata file is written to the output directory.",
                    v => parseInput.MetadataOutputFile = v
                },
                {
                    "g|gzip", "GZip the output file.",
                    v => parseInput.Gzip = v != null
                },
                {
                    "p|noPeakPicking",
                    "Don't use the peak picking provided by the native Thermo library. By default peak picking is enabled.",
                    v => parseInput.NoPeakPicking = v != null
                },
                {
                    "z|noZlibCompression",
                    "Don't use zlib compression for the m/z ratios and intensities. By default zlib compression is enabled.",
                    v => parseInput.NoZlibCompression = v != null
                },
                {
                    "a|allDetectors",
                    "Extract additional detector data: UV/PDA etc",
                    v => parseInput.AllDetectors = v != null
                },
                {
                    "l=|logging=", "Optional logging level: 0 for silent, 1 for verbose.",
                    v => logFormatString = v
                },
                {
                    "e|ignoreInstrumentErrors", "Ignore missing properties by the instrument.",
                    v => parseInput.IgnoreInstrumentErrors = v != null
                },
                {
                    "L=|msLevel=",
                    "Select MS levels (MS1, MS2, etc) included in the output, should be a comma-separated list of integers ( 1,2,3 ) and/or intervals ( 1-3 ), open-end intervals ( 1- ) are allowed",
                    v => parseInput.MsLevel = ParseMsLevel(v)
                },
                {
                    "P|mgfPrecursor",
                    "Include precursor scan number in MGF file TITLE",
                    v => parseInput.MGFPrecursor = v != null
                },
                {
                    "u:|s3_url:",
                    "Optional property to write directly the data into S3 Storage.",
                    v => parseInput.S3Url = v
                },
                {
                    "k:|s3_accesskeyid:",
                    "Optional key for the S3 bucket to write the file output.",
                    v => parseInput.S3AccessKeyId = v
                },
                {
                    "t:|s3_secretaccesskey:",
                    "Optional key for the S3 bucket to write the file output.",
                    v => parseInput.S3SecretAccessKey = v
                },
                {
                    "n:|s3_bucketName:",
                    "S3 bucket name",
                    v => parseInput.BucketName = v
                }
            };

            try
            {
                // parse the command line
                var extra = optionSet.Parse(args);

                if (!extra.IsNullOrEmpty())
                {
                    throw new OptionException("unexpected extra arguments", null);
                }

                if (help)
                {
                    var helpMessage =
                        $"usage is {Assembly.GetExecutingAssembly().GetName().Name}.exe [subcommand] [options]\noptional subcommands are xic|query (use [subcommand] -h for more info]):";
                    ShowHelp(helpMessage, null,
                             optionSet);
                    return;
                }

                if (version)
                {
                    Console.WriteLine(Version);
                    return;
                }

                if (parseInput.RawFilePath == null && parseInput.RawDirectoryPath == null)
                {
                    throw new OptionException(
                              "specify an input file or an input directory",
                              "-i, --input or -d, --input_directory");
                }

                if (parseInput.RawFilePath != null)
                {
                    if (parseInput.RawDirectoryPath != null)
                    {
                        throw new OptionException(
                                  "specify an input file or an input directory, not both",
                                  "-i, --input or -d, --input_directory");
                    }

                    if (!File.Exists(parseInput.RawFilePath))
                    {
                        throw new OptionException(
                                  "specify a valid RAW file location",
                                  "-i, --input");
                    }
                }

                if (parseInput.RawDirectoryPath != null && !Directory.Exists(parseInput.RawDirectoryPath))
                {
                    throw new OptionException(
                              "specify a valid input directory",
                              "-d, --input_directory");
                }

                if (parseInput.OutputFile == null && parseInput.OutputDirectory == null)
                {
                    if (parseInput.RawFilePath != null)
                    {
                        parseInput.OutputDirectory = Path.GetDirectoryName(Path.GetFullPath(parseInput.RawFilePath));
                    }
                    else if (parseInput.RawDirectoryPath != null)
                    {
                        parseInput.OutputDirectory = parseInput.RawDirectoryPath;
                    }
                }

                if (parseInput.OutputFile != null)
                {
                    if (parseInput.OutputDirectory == null)
                    {
                        parseInput.OutputDirectory = Path.GetDirectoryName(parseInput.OutputFile);
                    }
                    else
                    {
                        throw new OptionException(
                                  "specify an output directory or an output file, not both",
                                  "-o, --output or -b, --output_file");
                    }

                    if (Directory.Exists(parseInput.OutputFile))
                    {
                        throw new OptionException(
                                  "specify a valid output file, not a directory",
                                  "-b, --output_file");
                    }

                    if (parseInput.RawDirectoryPath != null)
                    {
                        throw new OptionException(
                                  "when using an input directory, specify an output directory instead of an output file",
                                  "-o, --output instead of -b, --output_file");
                    }
                }

                if (!parseInput.OutputDirectory.IsNullOrEmpty() && !Directory.Exists(parseInput.OutputDirectory))
                {
                    throw new OptionException(
                              "specify a valid output directory",
                              "-o, --output");
                }

                if (metadataFormatString == null && outputFormatString == null)
                {
                    parseInput.OutputFormat = OutputFormat.MzML;
                }

                if (outputFormatString != null)
                {
                    int outPutFormatInt;
                    try
                    {
                        outPutFormatInt = int.Parse(outputFormatString);
                    }
                    catch (FormatException)
                    {
                        throw new OptionException(
                                  "unknown output format value (0 for MGF, 1 for mzML, 2 for indexed mzML, 3 for Parquet)",
                                  "-f, --format");
                    }

                    if (Enum.IsDefined(typeof(OutputFormat), outPutFormatInt) &&
                        ((OutputFormat)outPutFormatInt) != OutputFormat.NONE)
                    {
                        parseInput.OutputFormat = (OutputFormat)outPutFormatInt;
                    }
                    else
                    {
                        throw new OptionException(
                                  "unknown output format value (0 for MGF, 1 for mzML, 2 for indexed mzML, 3 for Parquet)",
                                  "-f, --format");
                    }
                }

                if (metadataFormatString != null)
                {
                    int metadataInt;
                    try
                    {
                        metadataInt = int.Parse(metadataFormatString);
                    }
                    catch (FormatException)
                    {
                        throw new OptionException("unknown metadata format value (0 for JSON, 1 for TXT)",
                                                  "-m, --metadata");
                    }

                    if (Enum.IsDefined(typeof(MetadataFormat), metadataInt) &&
                        ((MetadataFormat)metadataInt) != MetadataFormat.NONE)
                    {
                        parseInput.MetadataFormat = (MetadataFormat)metadataInt;
                    }
                    else
                    {
                        throw new OptionException("unknown metadata format value (0 for JSON, 1 for TXT)",
                                                  "-m, --metadata");
                    }
                }

                if (parseInput.MetadataOutputFile != null && Directory.Exists(parseInput.MetadataOutputFile))
                {
                    throw new OptionException(
                              "specify a valid metadata output file, not a directory",
                              "-c, --metadata_output_file");
                }

                if (parseInput.MetadataOutputFile != null && parseInput.MetadataFormat == MetadataFormat.NONE)
                {
                    throw new OptionException("specify a metadata format (0 for JSON, 1 for TXT)",
                                              "-m, --metadata");
                }

                if (parseInput.MetadataOutputFile != null && parseInput.RawDirectoryPath != null)
                {
                    throw new OptionException(
                              "when using an input directory, specify an output directory instead of a metadata output file",
                              "-o, --output instead of -c, --metadata_output_file");
                }

                if (logFormatString != null)
                {
                    int logFormatInt;
                    try
                    {
                        logFormatInt = int.Parse(logFormatString);
                    }
                    catch (FormatException)
                    {
                        throw new OptionException("unknown log format value (0 for silent, 1 for verbose)",
                                                  "-l, --logging");
                    }

                    if (Enum.IsDefined(typeof(LogFormat), logFormatInt))
                    {
                        if ((LogFormat)logFormatInt != LogFormat.NONE)
                        {
                            parseInput.LogFormat = (LogFormat)logFormatInt;
                        }
                    }
                    else
                    {
                        throw new OptionException("unknown log format value (0 for silent, 1 for verbose)",
                                                  "-l, --logging");
                    }
                }

                if (parseInput.S3Url != null && parseInput.S3AccessKeyId != null &&
                    parseInput.S3SecretAccessKey != null && parseInput.BucketName != null)
                {
                    if (Uri.IsWellFormedUriString(parseInput.S3Url, UriKind.Absolute))
                    {
                        parseInput.InitializeS3Bucket();
                    }
                    else
                    {
                        throw new RawFileParserException("Invalid S3 url: " + parseInput.S3Url);
                    }
                }
            }
            catch (OptionException optionException)
            {
                ShowHelp("Error - usage is:", optionException,
                         optionSet);
            }
            catch (ArgumentNullException)
            {
                if (help)
                {
                    ShowHelp("usage is:", null,
                             optionSet);
                }
                else
                {
                    ShowHelp("Error - usage is:", null,
                             optionSet);
                }
            }

            var exitCode = 1;

            try
            {
                switch (parseInput.LogFormat)
                {
                case LogFormat.VERBOSE:
                    ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Level =
                        Level.Debug;
                    ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository())
                    .RaiseConfigurationChanged(EventArgs.Empty);
                    break;

                case LogFormat.SILENT:
                    ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Level =
                        Level.Off;
                    ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository())
                    .RaiseConfigurationChanged(EventArgs.Empty);
                    break;
                }

                RawFileParser.Parse(parseInput);

                exitCode = 0;
            }
            catch (UnauthorizedAccessException ex)
            {
                Log.Error(!ex.Message.IsNullOrEmpty()
                    ? ex.Message
                    : "Attempting to write to an unauthorized location.");
            }
            catch (Amazon.S3.AmazonS3Exception ex)
            {
                Log.Error(!ex.Message.IsNullOrEmpty()
                    ? "An Amazon S3 exception occured: " + ex.Message
                    : "An Amazon S3 exception occured: " + ex);
            }
            catch (Exception ex)
            {
                if (ex is RawFileParserException)
                {
                    Log.Error(ex.Message);
                }
                else
                {
                    Log.Error("An unexpected error occured:");
                    Log.Error(ex.ToString());
                }
            }
            finally
            {
                Environment.Exit(exitCode);
            }
        }
        public static void Main(string[] args)
        {
            string rawFilePath        = null;
            string outputDirectory    = null;
            string outputFormatString = null;
            var    outputFormat       = OutputFormat.NON;
            var    gzip = false;
            string outputMetadataString = null;
            var    outputMetadataFormat = MetadataFormat.NON;
            var    includeProfileData   = false;
            string collection           = null;
            string msRun     = null;
            string subFolder = null;
            var    help      = false;

            var optionSet = new OptionSet
            {
                {
                    "h|help", "Prints out the options.",
                    h => help = h != null
                },
                {
                    "i=|input=", "The raw file input.",
                    v => rawFilePath = v
                },
                {
                    "o=|output=", "The output directory.",
                    v => outputDirectory = v
                },
                {
                    "f=|format=", "The output format for the spectra (0 for MGF, 1 for MzMl, 2 for Parquet)",
                    v => outputFormatString = v
                },
                {
                    "m=|metadata=", "The metadata output format (0 for JSON, 1 for TXT).",
                    v => outputMetadataString = v
                },
                {
                    "g|gzip", "GZip the output file if this flag is specified (without value).",
                    v => gzip = v != null
                },
                {
                    "p|profiledata",
                    "Exclude MS2 profile data if this flag is specified (without value). Only for MGF format!",
                    v => includeProfileData = v != null
                },
                {
                    "c:|collection", "The optional collection identifier (PXD identifier for example).",
                    v => collection = v
                },
                {
                    "r:|run:",
                    "The optional mass spectrometry run name used in the spectrum title. The RAW file name will be used if not specified.",
                    v => msRun = v
                },
                {
                    "s:|subfolder:",
                    "Optional, to disambiguate instances where the same collection has 2 or more MS runs with the same name.",
                    v => subFolder = v
                }
            };

            try
            {
                //parse the command line
                var extra = optionSet.Parse(args);

                if (!extra.IsNullOrEmpty())
                {
                    throw new OptionException("unexpected extra arguments", null);
                }

                if (help)
                {
                    ShowHelp(" usage is (use -option=value for the optional arguments):", null,
                             optionSet);
                    return;
                }

                if (outputMetadataString == null && outputFormatString == null)
                {
                    throw new OptionException("The parameter -f or -m should be provided",
                                              "-f|--format , -m|--format");
                }

                if (outputFormatString != null)
                {
                    int outPutFormatInt;
                    try
                    {
                        outPutFormatInt = int.Parse(outputFormatString);
                    }
                    catch (FormatException e)
                    {
                        throw new OptionException("unknown output format value (0 for MGF, 1 for MzMl, 2 for Parquet)",
                                                  "-f, --format");
                    }

                    if (Enum.IsDefined(typeof(OutputFormat), outPutFormatInt) &&
                        ((OutputFormat)outPutFormatInt) != OutputFormat.NON)
                    {
                        outputFormat = (OutputFormat)outPutFormatInt;
                    }
                    else
                    {
                        throw new OptionException("unknown output format value (0 for MGF, 1 for MzMl, 2 for Parquet)",
                                                  "-f, --format");
                    }
                }

                if (outputMetadataString != null)
                {
                    int metadataInt;
                    try
                    {
                        metadataInt = int.Parse(outputMetadataString);
                    }
                    catch (FormatException e)
                    {
                        throw new OptionException("unknown metadata format value (0 for JSON, 1 for TXT)",
                                                  "-m, --metadata");
                    }

                    if (Enum.IsDefined(typeof(MetadataFormat), metadataInt) &&
                        ((MetadataFormat)metadataInt) != MetadataFormat.NON)
                    {
                        outputMetadataFormat = (MetadataFormat)metadataInt;
                    }
                    else
                    {
                        throw new OptionException("unknown metadata format value (0 for JSON, 1 for TXT)",
                                                  "-m, --metadata");
                    }
                }
            }
            catch (OptionException optionException)
            {
                ShowHelp("Error - usage is (use -option=value for the optional arguments):", optionException,
                         optionSet);
            }
            catch (ArgumentNullException argumentNullException)
            {
                if (help)
                {
                    ShowHelp(" usage is (use -option=value for the optional arguments):", null,
                             optionSet);
                }
                else
                {
                    ShowHelp("Error - usage is (use -option=value for the optional arguments):", null,
                             optionSet);
                }
            }

            try
            {
                var parseInput = new ParseInput(rawFilePath, outputDirectory, outputFormat, gzip,
                                                outputMetadataFormat,
                                                includeProfileData, collection, msRun, subFolder);
                RawFileParser.Parse(parseInput);
            }
            catch (Exception ex)
            {
                Log.Error("An unexpected error occured:");
                Log.Error(ex.ToString());

                Environment.Exit(1);
            }
        }