Example #1
0
        /// <summary>
        /// Process and extract the given RAW file.
        /// </summary>
        /// <param name="parseInput">the parse input object</param>
        private static void ProcessFile(ParseInput parseInput)
        {
            var rawFileWrapper = InitRawFile(parseInput);

            ProcessFile(rawFileWrapper.RawFile, rawFileWrapper.FirstScanNumber, rawFileWrapper.LastScanNumber, parseInput);
            rawFileWrapper.Dispose();
        }
 public RawFileWrapper(IRawDataPlus rawFile, int firstScanNumber, int lastScanNumber, ParseInput parseInput)
 {
     RawFile         = rawFile;
     FirstScanNumber = firstScanNumber;
     LastScanNumber  = lastScanNumber;
     ParseInput      = parseInput;
 }
        /// <summary>
        /// Process and extract the RAW file(s).
        /// </summary>
        /// <param name="parseInput">the parse input object</param>
        public static void Parse(ParseInput parseInput)
        {
            // Input raw folder mode
            if (parseInput.RawDirectoryPath != null)
            {
                Log.Info("Started analyzing folder " + parseInput.RawDirectoryPath);

                var rawFilesPath = Directory
                                   .EnumerateFiles(parseInput.RawDirectoryPath, "*", SearchOption.TopDirectoryOnly)
                                   .Where(s => s.ToLower().EndsWith("raw")).ToArray();
                Log.Info(String.Format("The folder contains {0} RAW files", rawFilesPath.Length));

                if (rawFilesPath.Length == 0)
                {
                    Log.Debug("No raw files found in folder");
                    throw new RawFileParserException("No raw files found in folder!");
                }

                foreach (var filePath in rawFilesPath)
                {
                    parseInput.RawFilePath = filePath;
                    Log.Info("Started parsing " + parseInput.RawFilePath);
                    try
                    {
                        ProcessFile(parseInput);
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                        Log.Error(!ex.Message.IsNullOrEmpty()
                            ? ex.Message
                            : "Attempting to write to an unauthorized location.");
                    }
                    catch (Exception ex)
                    {
                        if (ex is RawFileParserException)
                        {
                            Log.Error(ex.Message);
                        }
                        else
                        {
                            Log.Error("An unexpected error occured while parsing file:" + parseInput.RawFilePath);
                            Log.Error(ex.ToString());
                        }
                    }
                }
            }
            // Input raw file mode
            else
            {
                Log.Info("Started parsing " + parseInput.RawFilePath);

                ProcessFile(parseInput);
            }
        }
Example #4
0
        /*private static readonly log4net.ILog Log =
         *  log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);*/

        /// <summary>
        /// Process and extract the RAW file(s).
        /// </summary>
        /// <param name="parseInput">the parse input object</param>
        public static void Parse(ParseInput parseInput)
        {
            // Input raw folder mode
            if (parseInput.RawDirectoryPath != null)
            {
                //Log.Info("Started analyzing folder " + parseInput.RawDirectoryPath);
                Console.WriteLine("Started analyzing folder " + parseInput.RawDirectoryPath);

                var rawFilesPath =
                    Directory.EnumerateFiles(parseInput.RawDirectoryPath);
                if (Directory.GetFiles(parseInput.RawDirectoryPath, "*", SearchOption.TopDirectoryOnly).Length == 0)
                {
                    //Log.Debug("No raw files found in folder");
                    throw new RawFileParserException("No raw files found in folder!");
                }

                foreach (var filePath in rawFilesPath)
                {
                    parseInput.RawFilePath = filePath;
                    //Log.Info("Started parsing " + parseInput.RawFilePath);
                    try
                    {
                        ProcessFile(parseInput);
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                        /*Log.Error(!ex.Message.IsNullOrEmpty()
                         *  ? ex.Message
                         *  : "Attempting to write to an unauthorized location.");*/
                    }
                    catch (Exception ex)
                    {
                        if (ex is RawFileParserException)
                        {
                            //Log.Error(ex.Message);
                        }
                        else
                        {
                            //Log.Error("An unexpected error occured while parsing file:" + parseInput.RawFilePath);
                            //Log.Error(ex.ToString());
                        }
                    }
                }
            }
            // Input raw file mode
            else
            {
                //Log.Info("Started parsing " + parseInput.RawFilePath);

                ProcessFile(parseInput);
            }
        }
Example #5
0
        public static RawFileWrapper InitRawFile(ParseInput parseInput)
        {
            // Create the IRawDataPlus object for accessing the RAW file
            IRawDataPlus rawFile;

            rawFile = RawFileReaderFactory.ReadFile(parseInput.RawFilePath);

            if (!rawFile.IsOpen)
            {
                throw new RawFileParserException("Unable to access the RAW file using the native Thermo library.");
            }

            // Check for any errors in the RAW file
            if (rawFile.IsError)
            {
                throw new RawFileParserException($"Error opening ({rawFile.FileError}) - {parseInput.RawFilePath}");
            }

            // Check if the RAW file is being acquired
            if (rawFile.InAcquisition)
            {
                throw new RawFileParserException("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;

            //Console.WriteLine("Last scan number is " + lastScanNumber);

            return(new RawFileWrapper(rawFile, firstScanNumber, lastScanNumber, parseInput));
        }
Example #6
0
        /// <summary>
        /// Extract the RAW file metadata and spectra in MGF format.
        /// </summary>
        /// <param name="parseInput">the parse input object</param>
        public static void Parse(ParseInput parseInput)
        {
            // Check to see if the RAW file name was supplied as an argument to the program
            if (string.IsNullOrEmpty(parseInput.RawFilePath))
            {
                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);
            }

            Log.Info("Started parsing " + parseInput.RawFilePath);

            // Create the IRawDataPlus object for accessing the RAW file
            //var rawFile = RawFileReaderAdapter.FileFactory(rawFilePath);
            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.NON)
                {
                    var metadataWriter = new MetadataWriter(parseInput.OutputDirectory, parseInput.RawFileNameWithoutExtension);
                    if (parseInput.OutputMetadata == MetadataFormat.JSON)
                    {
                        metadataWriter.WriteJsonMetada(rawFile, firstScanNumber, lastScanNumber);
                    }
                    if (parseInput.OutputMetadata == MetadataFormat.TXT)
                    {
                        metadataWriter.WriteMetada(rawFile, firstScanNumber, lastScanNumber);
                    }
                }

                if (parseInput.OutputFormat != OutputFormat.NON)
                {
                    SpectrumWriter spectrumWriter;
                    switch (parseInput.OutputFormat)
                    {
                    case OutputFormat.Mgf:
                        spectrumWriter = new MgfSpectrumWriter(parseInput);
                        spectrumWriter.Write(rawFile, firstScanNumber, lastScanNumber);
                        break;

                    case OutputFormat.Mzml:
                        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);
            }
        }
        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);
            }
        }
Example #8
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);
            }
        }
        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);
            }
        }
        /// <summary>
        /// Process and extract the given RAW file.
        /// </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 RawFileParserException("Unable to access the RAW file using the native Thermo library.");
                }

                // Check for any errors in the RAW file
                if (rawFile.IsError)
                {
                    throw new RawFileParserException($"Error opening ({rawFile.FileError}) - {parseInput.RawFilePath}");
                }

                // Check if the RAW file is being acquired
                if (rawFile.InAcquisition)
                {
                    throw new RawFileParserException("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);

                rawFile.IncludeReferenceAndExceptionData = parseInput.ExData;

                // Get the first and last scan from the RAW file
                var firstScanNumber = rawFile.RunHeaderEx.FirstSpectrum;
                var lastScanNumber  = rawFile.RunHeaderEx.LastSpectrum;

                if (parseInput.MetadataFormat != MetadataFormat.NONE)
                {
                    MetadataWriter metadataWriter;
                    if (parseInput.MetadataOutputFile != null)
                    {
                        metadataWriter = new MetadataWriter(null, parseInput.MetadataOutputFile);
                    }
                    else
                    {
                        metadataWriter = new MetadataWriter(parseInput.OutputDirectory,
                                                            parseInput.RawFileNameWithoutExtension);
                    }

                    switch (parseInput.MetadataFormat)
                    {
                    case MetadataFormat.JSON:
                        metadataWriter.WriteJsonMetada(rawFile, firstScanNumber, lastScanNumber);
                        break;

                    case MetadataFormat.TXT:
                        metadataWriter.WriteMetadata(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);
            }
        }
Example #11
0
        private static void ProcessFile(IRawDataPlus rawFile, int firstScanNumber, int lastScanNumber, ParseInput parseInput)
        {
            if (parseInput.MetadataFormat != MetadataFormat.NONE)
            {
                MetadataWriter metadataWriter;
                if (parseInput.MetadataOutputFile != null)
                {
                    metadataWriter = new MetadataWriter(null, parseInput.MetadataOutputFile);
                }
                else
                {
                    metadataWriter = new MetadataWriter(parseInput.OutputDirectory,
                                                        parseInput.RawFileNameWithoutExtension);
                }

                switch (parseInput.MetadataFormat)
                {
                case MetadataFormat.JSON:
                    metadataWriter.WriteJsonMetada(rawFile, firstScanNumber, lastScanNumber);
                    break;

                case MetadataFormat.TXT:
                    metadataWriter.WriteMetadata(rawFile, firstScanNumber, lastScanNumber);
                    break;
                }
            }

            if (parseInput.OutputFormat != OutputFormat.NONE)
            {
                SpectrumWriter spectrumWriter;
                switch (parseInput.OutputFormat)
                {
                case OutputFormat.MGF:
                    spectrumWriter = new MgfSpectrumWriter(rawFile, parseInput);
                    spectrumWriter.Write(firstScanNumber, lastScanNumber);
                    break;

                case OutputFormat.MzML:
                case OutputFormat.IndexMzML:
                    spectrumWriter = new MzMlSpectrumWriter(rawFile, parseInput);
                    spectrumWriter.Write(firstScanNumber, lastScanNumber);
                    break;

                case OutputFormat.Parquet:
                    spectrumWriter = new ParquetSpectrumWriter(rawFile, parseInput);
                    spectrumWriter.Write(firstScanNumber, lastScanNumber);
                    break;
                }
            }

            //Log.Info("Finished parsing " + parseInput.RawFilePath);
            Console.WriteLine("Finished parsing " + parseInput.RawFilePath);
        }