Esempio n. 1
0
        public static List <string> RemoveInAcquistionFiles(List <string> rawFiles)
        {
            List <string> filesToRemove = new List <string>();

            foreach (var file in rawFiles)
            {
                using (var raw = RawFileReaderFactory.ReadFile(file))
                {
                    if (raw.InAcquisition)
                    {
                        Console.WriteLine("The following file is \"in acquisition\" and will not be processed{0}: ", Path.GetFileName(file));

                        filesToRemove.Add(file);
                    }
                }
            }

            if (filesToRemove.Count > 0)
            {
                foreach (var file in filesToRemove)
                {
                    rawFiles.Remove(file);
                }
            }

            return(rawFiles);
        }
        /// <summary>
        /// Read all of the transition data from the instrument method data
        /// </summary>
        /// <returns></returns>
        public List <TransitionData> GetAllTransitions()
        {
            var srmTransitions = new List <TransitionData>();

            using (var rawReader = RawFileReaderFactory.ReadFile(DatasetPath))
            {
                //Console.WriteLine($"File \"{RawFilePath}\": {rawReader.InstrumentMethodsCount} instrument methods");
                for (var i = 0; i < rawReader.InstrumentMethodsCount; i++)
                {
                    var method = rawReader.GetInstrumentMethod(i);
                    //Console.WriteLine($"File \"{RawFilePath}\": InstMethod string length: {method.Length}");
                    if (string.IsNullOrWhiteSpace(method))
                    {
                        continue;
                    }
                    var parsed = new XCalInstMethodParser(method);
                    if (parsed.UsesCompoundName)
                    {
                        canUseCompoundNames = true;
                    }
                    srmTransitions.AddRange(parsed.ParseSrmTable());
                }
            }

            //Console.WriteLine($"File \"{RawFilePath}\": {srmTransitions.Count} transitions in instrument method");
            return(srmTransitions);
        }
Esempio n. 3
0
        public RawFileViz(string rawFile)
        {
            this.InitializeComponent();

            #region initialize fields and containers

            nextScanButton.Text     = Char.ConvertFromUtf32(0x2192);
            previousScanButton.Text = Char.ConvertFromUtf32(0x2190);

            rawFileThreadManager = RawFileReaderFactory.CreateThreadManager(rawFile);

            RawData = rawFileThreadManager.CreateThreadAccessor();

            RawData.SelectMsData();

            TotalNumScans = RawData.RunHeaderEx.SpectraCount;

            FirstScan = RawData.RunHeader.FirstSpectrum;

            LastScan = RawData.RunHeader.LastSpectrum;

            currentScan = FirstScan;

            TotalTime = RawData.RunHeader.EndTime;

            this.Text = "ParseDataViz - " + RawData.FileName;

            totalScansLabel.Text = String.Format("/ {0}", TotalNumScans);

            splitContainer1.SplitterDistance = this.Size.Width - 300;

            UpdateChromatogramData();

            InitializeChromatogramPlot();

            ChroMsLevelComboBox.SelectedIndex = 0;

            initializeMassSpecData();

            initializeMassSpectrum();

            scanNumber.Text = FirstScan.ToString();

            y = 0;

            x = 1;


            #endregion

            //#region initial chromatogram

            this.plotViewChromatogram.Model = chromatogram;

            UpdateChromatogramPlot();
        }
Esempio n. 4
0
        public static bool IsWarningFree(string rawFile)
        {
            using (var file = RawFileReaderFactory.ReadFile(rawFile))
            {
                var error = file.FileError;

                if (error.HasWarning)
                {
                    string message = String.Format("{0} has a warning message: {1}\n\n" +
                                                   "Do you wish to continue?", Path.GetFileName(rawFile), error.WarningMessage);

                    var result = MessageBox.Show(message, "WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                    return(result == DialogResult.Yes);
                }
                else
                {
                    return(true);
                }
            }
        }
Esempio n. 5
0
        public static bool IsErrorFree(string rawFile)
        {
            using (var file = RawFileReaderFactory.ReadFile(rawFile))
            {
                var error = file.FileError;

                if (error.HasError)
                {
                    string message = String.Format("{0} has error code: {1}" +
                                                   "\n\n" +
                                                   "Error message: {2}", Path.GetFileName(rawFile), error.ErrorCode, error.ErrorMessage);

                    var result = MessageBox.Show(message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return(false);
                }
                else
                {
                    return(true);
                }
            }
        }
Esempio n. 6
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));
        }
Esempio n. 7
0
        public List <ProxiSpectrum> Retrieve()
        {
            var          resultList = new List <ProxiSpectrum>();
            IRawDataPlus rawFile;

            using (rawFile = RawFileReaderFactory.ReadFile(queryParameters.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}) - {queryParameters.rawFilePath}");
                }

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

                // Set a cvGroup number counter
                var cvGroup = 1;

                foreach (var scanNumber in queryParameters.scanNumbers)
                {
                    var    proxiSpectrum  = new ProxiSpectrum();
                    double monoisotopicMz = 0.0;
                    try
                    {
                        // Get each scan from the RAW file
                        var scan = Scan.FromFile(rawFile, scanNumber);

                        // Check to see if the RAW file contains label (high-res) data and if it is present
                        // then look for any data that is out of order
                        var time = rawFile.RetentionTimeFromScanNumber(scanNumber);

                        // Get the scan filter for this scan number
                        var scanFilter = rawFile.GetFilterForScanNumber(scanNumber);

                        // Get the scan event for this scan number
                        var scanEvent = rawFile.GetScanEventForScanNumber(scanNumber);

                        IReaction reaction = null;
                        if (scanEvent.MSOrder != MSOrderType.Ms)
                        {
                            reaction = SpectrumWriter.GetReaction(scanEvent, scanNumber);
                        }

                        proxiSpectrum.AddAttribute(accession: "MS:10003057", name: "scan number",
                                                   value: scanNumber.ToString(CultureInfo.InvariantCulture));
                        proxiSpectrum.AddAttribute(accession: "MS:10000016", name: "scan start time",
                                                   value: (time * 60).ToString(CultureInfo.InvariantCulture));
                        proxiSpectrum.AddAttribute(accession: "MS:1000511", name: "ms level",
                                                   value: ((int)scanFilter.MSOrder).ToString(CultureInfo.InvariantCulture));

                        // trailer extra data list
                        var trailerData    = rawFile.GetTrailerExtraInformation(scanNumber);
                        var charge         = 0;
                        var isolationWidth = 0.0;
                        for (var i = 0; i < trailerData.Length; i++)
                        {
                            if (trailerData.Labels[i] == "Ion Injection Time (ms):")
                            {
                                proxiSpectrum.AddAttribute(accession: "MS:10000927", name: "ion injection time",
                                                           value: trailerData.Values[i], cvGroup: cvGroup.ToString());
                                proxiSpectrum.AddAttribute(accession: "UO:0000028", name: "millisecond",
                                                           cvGroup: cvGroup.ToString());
                                cvGroup++;
                            }

                            if (trailerData.Labels[i] == "Charge State:")
                            {
                                charge = Convert.ToInt32(trailerData.Values[i]);
                            }

                            if (trailerData.Labels[i] == "Monoisotopic M/Z:")
                            {
                                monoisotopicMz = double.Parse(trailerData.Values[i], NumberStyles.Any,
                                                              CultureInfo.CurrentCulture);
                            }

                            if (trailerData.Labels[i] == "MS" + (int)scanFilter.MSOrder + " Isolation Width:")
                            {
                                isolationWidth = double.Parse(trailerData.Values[i], NumberStyles.Any,
                                                              CultureInfo.CurrentCulture);
                            }
                        }

                        if (reaction != null)
                        {
                            // Store the precursor information
                            var selectedIonMz =
                                SpectrumWriter.CalculateSelectedIonMz(reaction, monoisotopicMz, isolationWidth);
                            proxiSpectrum.AddAttribute(accession: "MS:10000744", name: "selected ion m/z",
                                                       value: selectedIonMz.ToString(CultureInfo.InvariantCulture));
                            proxiSpectrum.AddAttribute(accession: "MS:1000827",
                                                       name: "isolation window target m/z",
                                                       value: selectedIonMz.ToString(CultureInfo.InvariantCulture));

                            // Store the isolation window information
                            var isolationHalfWidth = isolationWidth / 2;
                            proxiSpectrum.AddAttribute(accession: "MS:1000828",
                                                       name: "isolation window lower offset",
                                                       value: isolationHalfWidth.ToString(CultureInfo.InvariantCulture));
                            proxiSpectrum.AddAttribute(accession: "MS:1000829",
                                                       name: "isolation window upper offset",
                                                       value: isolationHalfWidth.ToString(CultureInfo.InvariantCulture));
                        }

                        // scan polarity
                        if (scanFilter.Polarity == PolarityType.Positive)
                        {
                            proxiSpectrum.AddAttribute(accession: "MS:10000465", name: "scan polarity",
                                                       value: "positive scan", valueAccession: "MS:1000130");
                        }
                        else
                        {
                            proxiSpectrum.AddAttribute(accession: "MS:10000465", name: "scan polarity",
                                                       value: "negative scan", valueAccession: "MS:1000129");
                        }

                        // charge state
                        proxiSpectrum.AddAttribute(accession: "MS:10000041", name: "charge state",
                                                   value: charge.ToString(CultureInfo.InvariantCulture));

                        // write the filter string
                        proxiSpectrum.AddAttribute(accession: "MS:10000512", name: "filter string",
                                                   value: scanEvent.ToString());

                        if (!queryParameters.noPeakPicking) // centroiding requested
                        {
                            // check if the scan has a centroid stream
                            if (scan.HasCentroidStream)
                            {
                                if (scan.CentroidScan.Length > 0)
                                {
                                    proxiSpectrum.AddAttribute(accession: "MS:1000525", name: "spectrum representation",
                                                               value: "centroid spectrum", valueAccession: "MS:1000127");

                                    proxiSpectrum.AddMz(scan.CentroidScan.Masses);
                                    proxiSpectrum.AddIntensities(scan.CentroidScan.Intensities);
                                }
                            }
                            else // otherwise take the low res segmented data
                            {
                                // if the spectrum is profile perform centroiding
                                var segmentedScan = scanEvent.ScanData == ScanDataType.Profile
                                    ? Scan.ToCentroid(scan).SegmentedScan
                                    : scan.SegmentedScan;

                                if (segmentedScan.PositionCount > 0)
                                {
                                    proxiSpectrum.AddAttribute(accession: "MS:1000525", name: "spectrum representation",
                                                               value: "centroid spectrum", valueAccession: "MS:1000127");

                                    proxiSpectrum.AddMz(segmentedScan.Positions);
                                    proxiSpectrum.AddIntensities(segmentedScan.Intensities);
                                }
                            }
                        }
                        else // use the segmented data as is
                        {
                            if (scan.SegmentedScan.Positions.Length > 0)
                            {
                                switch (scanEvent.ScanData) //check if the data is centroided already
                                {
                                case ScanDataType.Centroid:
                                    proxiSpectrum.AddAttribute(accession: "MS:1000525",
                                                               name: "spectrum representation",
                                                               value: "centroid spectrum", valueAccession: "MS:1000127");
                                    break;

                                case ScanDataType.Profile:
                                    proxiSpectrum.AddAttribute(accession: "MS:1000525",
                                                               name: "spectrum representation",
                                                               value: "profile spectrum", valueAccession: "MS:1000128");
                                    break;
                                }

                                proxiSpectrum.AddMz(scan.SegmentedScan.Positions);
                                proxiSpectrum.AddIntensities(scan.SegmentedScan.Intensities);
                            }
                        }

                        resultList.Add(proxiSpectrum);
                    }
                    catch (Exception ex)
                    {
                        if (ex.GetBaseException() is IndexOutOfRangeException)
                        {
                            // ignore
                        }
                        else
                        {
                            throw ex;
                        }
                    }
                }
            }

            return(resultList);
        }
Esempio n. 8
0
        static int DoStuff(ArgumentParser.LogDumpOptions opts)
        {
            List <string> files = new List <string>();

            if (opts.InputFiles.Count() > 0) // did the user give us a list of files?
            {
                List <string> problems = new List <string>();
                files = opts.InputFiles.ToList();

                // check if the list provided contains only .raw files
                foreach (string file in files)
                {
                    if (!file.EndsWith(".raw", StringComparison.OrdinalIgnoreCase))
                    {
                        problems.Add(file);
                    }
                }

                if (problems.Count() == 1)
                {
                    Console.WriteLine("\nERROR: {0} does not appear to be a .raw file. Invoke '>RawTools --help' if you need help.", problems.ElementAt(0));
                    Log.Error("Invalid file provided: {0}", problems.ElementAt(0));

                    return(1);
                }

                if (problems.Count() > 1)
                {
                    Console.WriteLine("\nERROR: The following {0} files do not appear to be .raw files. Invoke '>RawTools --help' if you need help." +
                                      "\n\n{1}", problems.Count(), String.Join("\n", problems));
                    Log.Error("Invalid files provided: {0}", String.Join(" ", problems));
                    return(1);
                }

                Log.Information("Files to be processed, provided as list: {0}", String.Join(" ", files));
            }

            else // did the user give us a directory?
            {
                if (Directory.Exists(opts.InputDirectory))
                {
                    files = Directory.GetFiles(opts.InputDirectory, "*.*", SearchOption.TopDirectoryOnly)
                            .Where(s => s.EndsWith(".raw", StringComparison.OrdinalIgnoreCase)).ToList();
                }
                else
                {
                    Console.WriteLine("ERROR: The provided directory does not appear to be valid.");
                    Log.Error("Invalid directory provided: {0}", opts.InputDirectory);
                    return(1);
                }

                Log.Information("Files to be processed, provided as directory: {0}", String.Join(" ", files));
            }

            Console.WriteLine();

            foreach (var file in files)
            {
                Console.WriteLine(Path.GetFileName(file));
                Console.WriteLine("----------------------------------------");
                using (IRawDataPlus rawFile = RawFileReaderFactory.ReadFile(file))
                {
                    rawFile.SelectMsData();

                    var numberOfLogs = rawFile.GetStatusLogEntriesCount();
                    var logInfo      = rawFile.GetStatusLogHeaderInformation();

                    string logName = file + ".INST_LOG.txt";

                    Dictionary <int, ISingleValueStatusLog> log = new Dictionary <int, ISingleValueStatusLog>();

                    ProgressIndicator P = new ProgressIndicator(logInfo.Count(), "Preparing log data");
                    P.Start();
                    for (int i = 0; i < logInfo.Count(); i++)
                    {
                        log.Add(i, rawFile.GetStatusLogAtPosition(i));
                        P.Update();
                    }
                    P.Done();

                    using (StreamWriter f = new StreamWriter(logName))
                    {
                        P = new ProgressIndicator(numberOfLogs, $"Writing log");
                        P.Start();
                        f.Write("Time\t");
                        foreach (var x in logInfo)
                        {
                            f.Write(x.Label + "\t");
                        }
                        f.Write("\n");

                        for (int i = 0; i < numberOfLogs; i++)
                        {
                            f.Write($"{log[0].Times[i]}\t");

                            for (int j = 0; j < logInfo.Length; j++)
                            {
                                try
                                {
                                    f.Write("{0}\t", log[j].Values[i]);
                                }
                                catch (Exception)
                                {
                                    f.Write("\t");
                                }
                            }
                            f.Write("\n");
                            P.Update();
                        }
                        P.Done();
                    }
                }
                Console.WriteLine("\n");
            }
            return(0);
        }
Esempio n. 9
0
        static int DoStuff(ArgumentParser.ParseOptions opts)
        {
            if (!opts.ParseData & !opts.Quant & !opts.Metrics & !opts.WriteMGF & (opts.Chromatogram == null))
            {
                Console.WriteLine("You have not indicated what output you want (i.e. one or more of -p, -q, -m, -x, --chro). " +
                                  "Are you sure you want to proceed? Nothing will be written to disk.");
                Console.Write("(press y to proceed): ");

                string proceed = Console.ReadKey().KeyChar.ToString();
                Console.WriteLine();

                if (proceed != "y")
                {
                    Environment.Exit(0);
                }
            }

            List <string> files = new List <string>();

            if (opts.InputFiles.Count() > 0) // did the user give us a list of files?
            {
                List <string> problems = new List <string>();
                files = opts.InputFiles.ToList();

                // check if the list provided contains only .raw files
                foreach (string file in files)
                {
                    if (!file.EndsWith(".raw", StringComparison.OrdinalIgnoreCase))
                    {
                        problems.Add(file);
                    }
                }

                if (problems.Count() == 1)
                {
                    Console.WriteLine("\nERROR: {0} does not appear to be a .raw file. Invoke '>RawTools --help' if you need help.", problems.ElementAt(0));
                    Log.Error("Invalid file provided: {0}", problems.ElementAt(0));

                    return(1);
                }

                if (problems.Count() > 1)
                {
                    Console.WriteLine("\nERROR: The following {0} files do not appear to be .raw files. Invoke '>RawTools --help' if you need help." +
                                      "\n\n{1}", problems.Count(), String.Join("\n", problems));
                    Log.Error("Invalid files provided: {0}", String.Join(" ", problems));
                    return(1);
                }

                // if the file location(s) are relative, we need to get the absolute path to them
                files.EnsureAbsolutePaths();

                Log.Information("Files to be processed, provided as list: {0}", String.Join(" ", files));
            }

            else // did the user give us a directory?
            {
                if (Directory.Exists(opts.InputDirectory))
                {
                    files = Directory.GetFiles(opts.InputDirectory, "*.*", SearchOption.TopDirectoryOnly)
                            .Where(s => s.EndsWith(".raw", StringComparison.OrdinalIgnoreCase)).ToList();
                }
                else
                {
                    Console.WriteLine("ERROR: The provided directory does not appear to be valid.");
                    Log.Error("Invalid directory provided: {0}", opts.InputDirectory);
                    return(1);
                }

                // if the file location(s) are relative, we need to get the absolute path to them
                files.EnsureAbsolutePaths();

                Log.Information("Files to be processed, provided as directory: {0}", String.Join(" ", files));
            }

            if (opts.Quant)
            {
                List <string> possible = new List <string>()
                {
                    "TMT0", "TMT2", "TMT6", "TMT10", "TMT11", "iTRAQ4", "iTRAQ8"
                };
                if (!possible.Contains(opts.LabelingReagents))
                {
                    Console.WriteLine("ERROR: For quantification, the labeling reagent must be one of {TMT0, TMT2, TMT6, TMT10, TMT11, iTRAQ4, iTRAQ8}");
                    Log.Error("Invalid labeling reagent provided: {0}", opts.LabelingReagents);
                    return(1);
                }
            }

            if (opts.Chromatogram != null)
            {
                List <string> possible = new List <string>()
                {
                    "1T", "2T", "3T", "1B", "2B", "3B", "1TB", "2TB", "3TB", "1TB", "2TB", "3TB"
                };
                if (!possible.Contains(opts.Chromatogram))
                {
                    Console.WriteLine("ERROR: Incorrect format for --chro. See help.");
                    Log.Error("Invalid chromatogram argument provided: {Chro}", opts.Chromatogram);
                    return(1);
                }
            }

            /*
             * // is the experiment type valid?
             * if (! new List<string>() { "DDA", "DIA", "PRM" }.Contains(opts.ExperimentType))
             * {
             *  Log.Error("Experiment type of {ExpType} was passed", opts.ExperimentType);
             *  Console.WriteLine("Experiment type must be one of ['DDA', 'DIA', 'PRM'], not {0}", opts.ExperimentType);
             *  Environment.Exit(1);
             * }*/


            System.Diagnostics.Stopwatch singleFileTime = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch totalTime      = new System.Diagnostics.Stopwatch();
            totalTime.Start();

            WorkflowParameters parameters = new WorkflowParameters(opts);

            foreach (string file in files)
            {
                singleFileTime.Start();

                Console.WriteLine("\nProcessing: {0}\n", file);

                //using (IRawDataPlus rawFile = RawFileReaderFactory.ReadFile(fileName:file))
                using (IRawFileThreadManager rawFile = RawFileReaderFactory.CreateThreadManager(file))
                {
                    if (parameters.ParseParams.OutputDirectory == null)
                    {
                        parameters.ParseParams.OutputDirectory = Path.GetDirectoryName(file);
                    }

                    if (parameters.ExpType == ExperimentType.DDA)
                    {
                        WorkFlowsDDA.ParseDDA(rawFile, parameters);
                    }
                    else if (parameters.ExpType == ExperimentType.DIA)
                    {
                        WorkFlowsDIA.ParseDIA(rawFile, parameters);
                    }
                }

                singleFileTime.Stop();
                Console.WriteLine("\nElapsed time: {0} s", Math.Round(Convert.ToDouble(singleFileTime.ElapsedMilliseconds) / 1000.0, 2));
                singleFileTime.Reset();
            }
            totalTime.Stop();
            Console.WriteLine("\nTime to process all {0} files: {1}", files.Count(), totalTime.Elapsed);

            return(0);
        }
Esempio n. 10
0
        static int DoStuff(ArgumentParser.TestOptions opts)
        {
            var rawFile = RawFileReaderFactory.ReadFile(opts.InputFiles.First());

            rawFile.SelectInstrument(Device.MS, 1);

            var created  = rawFile.CreationDate;
            var modified = rawFile.FileHeader.ModifiedDate;
            var diff     = modified - created;
            var estTime  = rawFile.RunHeader.ExpectedRuntime;

            var timesModified = rawFile.FileHeader.NumberOfTimesModified;

            Console.WriteLine("=============================================");
            Console.WriteLine($"Creation date/time: {created}");
            Console.WriteLine($"Last modified date/time: {modified}");
            Console.WriteLine($"Number of times modified: {timesModified}");
            Console.WriteLine($"Total time: {diff}");
            Console.WriteLine($"Expected run time: {estTime}");
            Console.WriteLine();

            Console.WriteLine($"Estimated dead time: {diff.TotalMinutes - estTime}");
            Console.WriteLine("=============================================");

            /*
             * var numberOfLogs = rawFile.GetStatusLogEntriesCount();
             * var logInfo = rawFile.GetStatusLogHeaderInformation();
             *
             * string logName = opts.InputFiles.First() + ".INST_LOG.txt";
             *
             * Dictionary<int, ISingleValueStatusLog> log = new Dictionary<int, ISingleValueStatusLog>();
             *
             * for (int i = 0; i < logInfo.Count(); i++)
             * {
             *  log.Add(i, rawFile.GetStatusLogAtPosition(i));
             * }
             *
             * using (StreamWriter f = new StreamWriter(logName))
             * {
             *  ProgressIndicator P = new ProgressIndicator(numberOfLogs, "Writing instrument log");
             *  P.Start();
             *  f.Write("Time\t");
             *  foreach (var x in logInfo)
             *  {
             *      f.Write(x.Label + "\t");
             *  }
             *  f.Write("\n");
             *
             *  for (int i = 0; i < numberOfLogs; i++)
             *  {
             *      f.Write($"{log[0].Times[i]}\t");
             *
             *      for (int j = 0; j < logInfo.Length; j++)
             *      {
             *          try
             *          {
             *              f.Write("{0}\t",log[j].Values[i]);
             *          }
             *          catch (Exception)
             *          {
             *              f.Write("\t");
             *          }
             *      }
             *      f.Write("\n");
             *      P.Update();
             *  }
             *  P.Done();
             * }
             *
             * Console.WriteLine(rawFile.GetInstrumentMethod(0));
             * Console.WriteLine(rawFile.CreationDate);
             * Console.WriteLine(rawFile.FileHeader.ModifiedDate);
             * Console.WriteLine(rawFile.RunHeader.StartTime);
             * Console.WriteLine(rawFile.RunHeader.EndTime);
             */
            return(0);
        }
Esempio n. 11
0
        static int DoStuff(ArgumentParser.ParseOptions opts)
        {
            List <string> files = new List <string>();

            if (opts.InputFiles.Count() > 0) // did the user give us a list of files?
            {
                List <string> problems = new List <string>();
                files = opts.InputFiles.ToList();

                // check if the list provided contains only .raw files
                foreach (string file in files)
                {
                    if (!file.EndsWith(".raw", StringComparison.OrdinalIgnoreCase))
                    {
                        problems.Add(file);
                    }
                }

                if (problems.Count() == 1)
                {
                    Console.WriteLine("\nERROR: {0} does not appear to be a .raw file. Invoke '>RawTools --help' if you need help.", problems.ElementAt(0));
                    Log.Error("Invalid file provided: {0}", problems.ElementAt(0));

                    return(1);
                }

                if (problems.Count() > 1)
                {
                    Console.WriteLine("\nERROR: The following {0} files do not appear to be .raw files. Invoke '>RawTools --help' if you need help." +
                                      "\n\n{1}", problems.Count(), String.Join("\n", problems));
                    Log.Error("Invalid files provided: {0}", String.Join(" ", problems));
                    return(1);
                }

                Log.Information("Files to be processed, provided as list: {0}", String.Join(" ", files));
            }

            else // did the user give us a directory?
            {
                if (Directory.Exists(opts.InputDirectory))
                {
                    files = Directory.GetFiles(opts.InputDirectory, "*.*", SearchOption.TopDirectoryOnly)
                            .Where(s => s.EndsWith(".raw", StringComparison.OrdinalIgnoreCase)).ToList();
                }
                else
                {
                    Console.WriteLine("ERROR: The provided directory does not appear to be valid.");
                    Log.Error("Invalid directory provided: {0}", opts.InputDirectory);
                    return(1);
                }

                Log.Information("Files to be processed, provided as directory: {0}", String.Join(" ", files));
            }

            if (opts.Quant)
            {
                List <string> possible = new List <string>()
                {
                    "TMT0", "TMT2", "TMT6", "TMT10", "TMT11", "iTRAQ4", "iTRAQ8"
                };
                if (!possible.Contains(opts.LabelingReagents))
                {
                    Console.WriteLine("ERROR: For quantification, the labeling reagent must be one of {TMT0, TMT2, TMT6, TMT10, TMT11, iTRAQ4, iTRAQ8}");
                    Log.Error("Invalid labeling reagent provided: {0}", opts.LabelingReagents);
                    return(1);
                }
            }

            if (opts.Chromatogram != null)
            {
                List <string> possible = new List <string>()
                {
                    "1T", "2T", "3T", "1B", "2B", "3B", "1TB", "2TB", "3TB", "1TB", "2TB", "3TB"
                };
                if (!possible.Contains(opts.Chromatogram))
                {
                    Console.WriteLine("ERROR: Incorrect format for --chro. See help.");
                    Log.Error("Invalid chromatogram argument provided: {Chro}", opts.Chromatogram);
                    return(1);
                }
            }

            System.Diagnostics.Stopwatch singleFileTime = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch totalTime      = new System.Diagnostics.Stopwatch();
            totalTime.Start();

            foreach (string file in files)
            {
                singleFileTime.Start();

                Console.WriteLine("\nProcessing: {0}\n", file);

                using (IRawDataPlus rawFile = RawFileReaderFactory.ReadFile(fileName: file))
                {
                    rawFile.SelectInstrument(Device.MS, 1);

                    Log.Information("Now processing: {File} --- Instrument: {Instrument}", Path.GetFileName(file), rawFile.GetInstrumentData().Name);

                    RawDataCollection   rawData   = new RawDataCollection(rawFile: rawFile);
                    QuantDataCollection quantData = new QuantDataCollection();

                    bool isBoxCar = rawData.isBoxCar;

                    if (rawData.isBoxCar)
                    {
                        Console.WriteLine("\nRaw file appears to be a boxcar-type experiment. Precursor peak analysis won't be performed!\n");
                    }

                    if (opts.ParseData | opts.Metrics | opts.Quant)
                    {
                        rawData.ExtractAll(rawFile);

                        if (!isBoxCar)
                        {
                            rawData.CalcPeakRetTimesAndInts(rawFile: rawFile);
                        }
                    }

                    if (opts.Quant)
                    {
                        rawData.quantData.Quantify(rawData: rawData, rawFile: rawFile, labelingReagent: opts.LabelingReagents);
                    }

                    if (opts.UnlabeledQuant & !isBoxCar)
                    {
                        rawData.QuantifyPrecursorPeaks(rawFile);
                    }

                    if (opts.Metrics)
                    {
                        rawData.metaData.AggregateMetaData(rawData, rawFile);
                    }

                    if (opts.ParseData | opts.Quant)
                    {
                        if (opts.Quant)
                        {
                            Parse.WriteMatrix(rawData: rawData, rawFile: rawFile, metaData: rawData.metaData, quantData: rawData.quantData, outputDirectory: opts.OutputDirectory);
                        }
                        else
                        {
                            Parse.WriteMatrix(rawData: rawData, rawFile: rawFile, metaData: rawData.metaData, outputDirectory: opts.OutputDirectory);
                        }
                    }

                    if (opts.WriteMGF)
                    {
                        MGF.WriteMGF(rawData: rawData, rawFile: rawFile, outputDirectory: opts.OutputDirectory, cutoff: opts.MassCutOff,
                                     intensityCutoff: opts.IntensityCutoff);
                    }

                    if (opts.Metrics)
                    {
                        MetricsData metricsData = new MetricsData();

                        if (opts.Quant)
                        {
                            metricsData.GetMetricsData(metaData: rawData.metaData, rawData: rawData, rawFile: rawFile, quantData: rawData.quantData);
                        }
                        else
                        {
                            metricsData.GetMetricsData(metaData: rawData.metaData, rawData: rawData, rawFile: rawFile);
                        }

                        metricsData.GetMetricsData(metaData: rawData.metaData, rawData: rawData, rawFile: rawFile);
                        Metrics.WriteMatrix(rawData, metricsData, opts.OutputDirectory);
                    }

                    if (opts.Chromatogram != null)
                    {
                        int order = Convert.ToInt32((opts.Chromatogram.ElementAt(0).ToString()));

                        if (order > (int)rawData.methodData.AnalysisOrder)
                        {
                            Log.Error("Specified MS order ({Order}) for chromatogram is higher than experiment order ({ExpOrder})",
                                      (MSOrderType)order, rawData.methodData.AnalysisOrder);
                            Console.WriteLine("Specified MS order ({0}) for chromatogram is higher than experiment order ({1}). Chromatogram(s) won't be written.",
                                              (MSOrderType)order, rawData.methodData.AnalysisOrder);
                        }
                        else
                        {
                            rawData.WriteChromatogram(rawFile, (MSOrderType)order, opts.Chromatogram.Contains("T"), opts.Chromatogram.Contains("B"), opts.OutputDirectory);
                        }
                    }
                }

                singleFileTime.Stop();
                Console.WriteLine("\nElapsed time: {0} s", Math.Round(Convert.ToDouble(singleFileTime.ElapsedMilliseconds) / 1000.0, 2));
                singleFileTime.Reset();
            }
            totalTime.Stop();
            Console.WriteLine("\nTime to process all {0} files: {1}", files.Count(), totalTime.Elapsed);

            return(0);
        }
Esempio n. 12
0
        static int Run(Dictionary <string, object> opts)
        {
            if ((bool)opts["ExampleCommands"] == true)
            {
                Examples.CommandLineUsage();
                Environment.Exit(0);
            }

            if ((bool)opts["ExampleModifications"] == true)
            {
                Examples.ExampleMods();
                Environment.Exit(0);
            }

            List <string>    files            = new List <string>();
            QcDataCollection qcDataCollection = new QcDataCollection();

            WorkflowParameters parameters = new WorkflowParameters(opts);

            if (parameters.InputFiles != null) // did the user give us a list of files?
            {
                List <string> problems = new List <string>();
                files = parameters.InputFiles.ToList();

                // check if the list provided contains only .raw files
                foreach (string file in files)
                {
                    if (!file.EndsWith(".raw", StringComparison.OrdinalIgnoreCase))
                    {
                        problems.Add(file);
                    }
                }

                if (problems.Count() == 1)
                {
                    Console.WriteLine("\nERROR: {0} does not appear to be a .raw file. Invoke '>RawTools --help' if you need help.", problems.ElementAt(0));
                    Log.Error("Invalid file provided: {0}", problems.ElementAt(0));
                    //Console.Write("Press any key to exit...");
                    //Console.ReadKey();
                    return(1);
                }

                if (problems.Count() > 1)
                {
                    Console.WriteLine("\nERROR: The following {0} files do not appear to be .raw files. Invoke '>RawTools --help' if you need help." +
                                      "\n\n{1}", problems.Count(), String.Join("\n", problems));
                    Log.Error("Invalid files provided: {0}", String.Join(" ", problems));
                    //Console.Write("Press any key to exit...");
                    //Console.ReadKey();
                    return(1);
                }

                files = RawFileInfo.RemoveInAcquistionFiles(files);

                // if the file location(s) are relative, we need to get the absolute path to them
                files.EnsureAbsolutePaths();

                Log.Information("Files to be processed, provided as list: {0}", String.Join(" ", files));
            }

            else if (!String.IsNullOrEmpty(parameters.RawFileDirectory)) // did the user give us a directory?
            {
                // if QC is being done, use the QC method snf get the qc data collection at the same time
                if (parameters.QcParams.QcDirectory != null)
                {
                    (files, qcDataCollection) = QcWorkflow.GetFileListAndQcFile(parameters, parameters.IncludeSubdirectories);
                }

                // if not, the parse method
                else if (Directory.Exists(parameters.RawFileDirectory))
                {
                    files = Directory.GetFiles(parameters.RawFileDirectory, "*.*", SearchOption.TopDirectoryOnly)
                            .Where(s => s.EndsWith(".raw", StringComparison.OrdinalIgnoreCase)).ToList();
                }
                else
                {
                    Console.WriteLine("ERROR: The provided directory does not appear to be valid.");
                    Log.Error("Invalid directory provided: {0}", parameters.RawFileDirectory);
                    //Console.Write("Press any key to exit...");
                    //Console.ReadKey();
                    return(1);
                }

                files = RawFileInfo.RemoveInAcquistionFiles(files);

                // if the file location(s) are relative, we need to get the absolute path to them
                files.EnsureAbsolutePaths();

                Log.Information("Files to be processed, provided as directory: {0}", String.Join(" ", files));
            }
            else
            {
                Console.WriteLine("ERROR: At least one of the following arguments is required: -f, -d");
                Log.Error("No raw files or directory specified.");
                return(1);
            }

            if (parameters.ParseParams.Quant)
            {
                List <string> possible = new List <string>()
                {
                    "TMT0", "TMT2", "TMT6", "TMT10", "TMT11", "iTRAQ4", "iTRAQ8"
                };
                if (!possible.Contains(parameters.ParseParams.LabelingReagents))
                {
                    Console.WriteLine("ERROR: For quantification, the labeling reagent must be one of {TMT0, TMT2, TMT6, TMT10, TMT11, iTRAQ4, iTRAQ8}");
                    Log.Error("Invalid labeling reagent provided: {0}", parameters.ParseParams.LabelingReagents);
                    //Console.Write("Press any key to exit...");
                    //Console.ReadKey();
                    return(1);
                }
            }

            if (parameters.ParseParams.Chromatogram != null)
            {
                List <string> possible = new List <string>()
                {
                    "1", "2", "3", "T", "B"
                };
                foreach (var x in parameters.ParseParams.Chromatogram)
                {
                    if (!possible.Contains(x.ToString()))
                    {
                        Console.WriteLine("ERROR: Incorrect format for -chro. See help.");
                        Log.Error("Invalid chromatogram argument provided: {Chro}", parameters.ParseParams.Chromatogram);
                        //Console.Write("Press any key to exit...");
                        //Console.ReadKey();
                        return(1);
                    }
                }
            }

            System.Diagnostics.Stopwatch singleFileTime = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch totalTime      = new System.Diagnostics.Stopwatch();
            totalTime.Start();

            foreach (string file in files)
            {
                singleFileTime.Start();

                Console.WriteLine("\nProcessing: {0}\n", file);

                //using (IRawDataPlus rawFile = RawFileReaderFactory.ReadFile(fileName:file))
                using (IRawFileThreadManager rawFile = RawFileReaderFactory.CreateThreadManager(file))
                {
                    if (parameters.ParseParams.OutputDirectory == null)
                    {
                        parameters.ParseParams.OutputDirectory = Path.GetDirectoryName(file);
                    }

                    WorkFlowsDDA.UniversalDDA(rawFile, parameters, qcDataCollection);
                }

                singleFileTime.Stop();
                Console.WriteLine("\nElapsed time: {0} s", Math.Round(Convert.ToDouble(singleFileTime.ElapsedMilliseconds) / 1000.0, 2));
                singleFileTime.Reset();
            }

            if (parameters.LogDump)
            {
                Write.LogDump.WriteToDisk(parameters);
            }

            totalTime.Stop();
            Console.WriteLine("\nTime to process all {0} files: {1}", files.Count(), totalTime.Elapsed);

            //Console.Write("Press any key to exit...");
            //Console.ReadKey();

            return(0);
        }
 public RawReaderMetadata(string rawFilePath)
 {
     RawFilePath       = rawFilePath;
     rawReaderThreader = RawFileReaderFactory.CreateThreadManager(rawFilePath);
 }
Esempio n. 14
0
        public static void WriteToDisk(WorkFlows.WorkflowParameters opts)
        {
            List <string> files = new List <string>();

            Console.WriteLine();

            Console.WriteLine("Writing log files");

            foreach (var file in files)
            {
                Console.WriteLine(Path.GetFileName(file));
                Console.WriteLine("----------------------------------------");
                using (IRawDataPlus rawFile = RawFileReaderFactory.ReadFile(file))
                {
                    rawFile.SelectMsData();

                    var numberOfLogs = rawFile.GetStatusLogEntriesCount();
                    var logInfo      = rawFile.GetStatusLogHeaderInformation();

                    string logName = file + ".INST_LOG.txt";

                    Dictionary <int, ISingleValueStatusLog> log = new Dictionary <int, ISingleValueStatusLog>();

                    ProgressIndicator P = new ProgressIndicator(logInfo.Count(), "Preparing log data");
                    P.Start();
                    for (int i = 0; i < logInfo.Count(); i++)
                    {
                        log.Add(i, rawFile.GetStatusLogAtPosition(i));
                        P.Update();
                    }
                    P.Done();

                    using (StreamWriter f = new StreamWriter(logName))
                    {
                        P = new ProgressIndicator(numberOfLogs, $"Writing log");
                        P.Start();
                        f.Write("Time\t");
                        foreach (var x in logInfo)
                        {
                            f.Write(x.Label + "\t");
                        }
                        f.Write("\n");

                        for (int i = 0; i < numberOfLogs; i++)
                        {
                            f.Write($"{log[0].Times[i]}\t");

                            for (int j = 0; j < logInfo.Length; j++)
                            {
                                try
                                {
                                    f.Write("{0}\t", log[j].Values[i]);
                                }
                                catch (Exception)
                                {
                                    f.Write("\t");
                                }
                            }
                            f.Write("\n");
                            P.Update();
                        }
                        P.Done();
                    }
                }
                Console.WriteLine("\n");
            }
        }
Esempio n. 15
0
        public static void ReadXic(string rawFilePath, bool base64, XicData xicData)
        {
            IRawDataPlus rawFile;

            using (rawFile = RawFileReaderFactory.ReadFile(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}) - {rawFilePath}");
                }

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

                // Get the start and end time from the RAW file
                var startTime = rawFile.RunHeaderEx.StartTime;
                var endTime   = rawFile.RunHeaderEx.EndTime;

                // Get the mass range from the RAW file
                var minMass = rawFile.RunHeaderEx.LowMass;
                var maxMass = rawFile.RunHeaderEx.HighMass;

                // Update global metadata
                xicData.OutputMeta.base64   = base64;
                xicData.OutputMeta.timeunit = "minutes";

                foreach (var xicUnit in xicData.Content)
                {
                    IChromatogramSettings settings = null;
                    if (!xicUnit.Meta.MzStart.HasValue && !xicUnit.Meta.MzEnd.HasValue)
                    {
                        settings = new ChromatogramTraceSettings()
                        {
                            Filter = xicUnit.Meta.Filter ?? "ms"
                        };
                    }

                    if (!xicUnit.Meta.MzStart.HasValue)
                    {
                        xicUnit.Meta.MzStart = minMass;
                    }

                    if (!xicUnit.Meta.MzEnd.HasValue)
                    {
                        xicUnit.Meta.MzEnd = maxMass;
                    }

                    if (settings == null)
                    {
                        settings = new ChromatogramTraceSettings(TraceType.MassRange)
                        {
                            Filter     = xicUnit.Meta.Filter ?? "ms",
                            MassRanges = new[]
                            {
                                new Range(xicUnit.Meta.MzStart.Value,
                                          xicUnit.Meta.MzEnd.Value)
                            }
                        };
                    }

                    List <int> rtFilteredScans = null;
                    if (!xicUnit.Meta.RtStart.HasValue && !xicUnit.Meta.RtEnd.HasValue)
                    {
                        rtFilteredScans = new List <int>();
                    }

                    if (!xicUnit.Meta.RtStart.HasValue)
                    {
                        xicUnit.Meta.RtStart = startTime;
                    }

                    if (!xicUnit.Meta.RtEnd.HasValue)
                    {
                        xicUnit.Meta.RtEnd = endTime;
                    }

                    IChromatogramData data = null;
                    if (rtFilteredScans == null)
                    {
                        rtFilteredScans = rawFile.GetFilteredScansListByTimeRange(MsFilter,
                                                                                  xicUnit.Meta.RtStart.Value, xicUnit.Meta.RtEnd.Value);

                        if (rtFilteredScans.Count != 0)
                        {
                            data = GetChromatogramData(rawFile, settings, rtFilteredScans[0],
                                                       rtFilteredScans[rtFilteredScans.Count - 1]);
                            if (data != null && data.PositionsArray.Length == 1 && data.PositionsArray[0].Length == 1 &&
                                (Math.Abs(data.PositionsArray[0][0] - startTime) < 0.001 ||
                                 Math.Abs(data.PositionsArray[0][0] - endTime) < 0.001))
                            {
                                Log.Warn(
                                    $"Only the minimum or maximum retention time was returned. This is an indication that the provided retention time range [{xicUnit.Meta.RtStart}-{xicUnit.Meta.RtEnd}] lies outside the max. window [{startTime}-{endTime}]");
                            }
                        }
                        else
                        {
                            Log.Warn(
                                $"No scans found in retention time range [{xicUnit.Meta.RtStart}-{xicUnit.Meta.RtEnd}]. This is an indication that the provided retention time window lies outside the max. window [{startTime}-{endTime}]");
                        }
                    }
                    else
                    {
                        data = GetChromatogramData(rawFile, settings, firstScanNumber, lastScanNumber);
                    }

                    if (data != null)
                    {
                        var chromatogramTrace = ChromatogramSignal.FromChromatogramData(data);
                        if (chromatogramTrace[0].Scans.Count != 0)
                        {
                            if (!base64)
                            {
                                xicUnit.RetentionTimes = chromatogramTrace[0].Times;
                                xicUnit.Intensities    = chromatogramTrace[0].Intensities;
                            }
                            else
                            {
                                xicUnit.RetentionTimes = GetBase64String(chromatogramTrace[0].Times);
                                xicUnit.Intensities    = GetBase64String(chromatogramTrace[0].Intensities);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 16
0
        public static void DoQc(QcParameters qcParameters)
        {
            QcDataCollection qcDataCollection;
            string           dataDirectory         = qcParameters.RawFileDirectory;
            string           qcDirectory           = qcParameters.QcDirectory;
            string           qcSearchDataDirecotry = qcParameters.QcSearchDataDirectory;
            SearchParameters searchParameters      = qcParameters.searchParameters;

            // our qc file
            string qcFile = Path.Combine(qcDirectory, "QC.xml");

            // see if the file exists
            if (File.Exists(qcFile))
            {
                // if so, open it
                try
                {
                    qcDataCollection = XmlSerialization.ReadFromXmlFile <QcDataCollection>(qcFile);
                    Log.Information("QC data file loaded successfully");
                }
                catch (Exception e)
                {
                    Log.Error(e, "Failed while loading QC data");
                    throw e;
                }
            }
            else
            {
                // if not, check if the directory exists
                if (!Directory.Exists(qcDirectory))
                {
                    Directory.CreateDirectory(qcDirectory);
                }

                qcDataCollection = new QcDataCollection(dataDirectory, qcDirectory);
                Log.Information("Appears to be a new QC directory. New QC data collection created.");
            }

            // get our list of new raw files. it is every raw file in the directory that is not listed in the qc data
            var fileList = Directory.GetFiles(dataDirectory, "*.*", SearchOption.TopDirectoryOnly)
                           .Where(s => s.EndsWith(".raw", StringComparison.OrdinalIgnoreCase)).ToList();

            if (fileList.Count() == 0)
            {
                Log.Error("No raw files found in {Directory}", dataDirectory);
                Console.WriteLine("{0} contains no raw files!", dataDirectory);
                Environment.Exit(1);
            }

            fileList.RemoveAll(s => qcDataCollection.ProcessedRawFiles.Contains(Path.GetFileName(s)));

            Log.Information("Raw files in QC queue: {Files}", fileList);

            if (fileList.Count() == 0)
            {
                Log.Information("No new files to QC");
                Console.WriteLine("No new files in the directory to QC!");
                Environment.Exit(0);
            }

            Console.WriteLine("{0} file(s) to process", fileList.Count());

            foreach (string fileName in fileList)
            {
                Console.WriteLine("Processing {0}", fileName);

                IFileHeader rawHeader;

                // try to open the raw file header
                try
                {
                    rawHeader = FileHeaderReaderFactory.ReadFile(fileName);;
                }
                catch (Exception)
                {
                    Log.Information("{File} is not a valid raw file", fileName);
                    Console.WriteLine("{0} is not a valid raw file, continuing to next file.", fileName);
                    continue;
                }

                // is it a real raw file?
                if (rawHeader.FileType == FileType.RawFile)
                {
                    Log.Information("{File} is a valid raw file", fileName);
                    Log.Information("Creation date: {Date}", rawHeader.CreationDate);
                    Log.Information("File description: {Description}", rawHeader.FileDescription);
                }
                else
                {
                    Log.Information("{File} is not a valid raw file", fileName);
                    Console.WriteLine("{0} is not a valid raw file, continuing to next file.", fileName);
                    continue;
                }
                // okay, it is probably a real raw file, let's do the QC

                // check if the raw file already exists in the QC data with a different name
                if (qcDataCollection.QcData.Keys.Contains(rawHeader.CreationDate))
                {
                    Log.Information("A file with the same creation date and time as {File} already exists in the QC data", fileName);
                    Console.WriteLine("{0} appears to already exist in the QC data with the name {1}. Skipping to next file.",
                                      fileName, qcDataCollection.QcData[rawHeader.CreationDate].RawFile);
                    continue;
                }

                using (IRawDataPlus rawFile = RawFileReaderFactory.ReadFile(fileName))
                {
                    rawFile.SelectInstrument(Device.MS, 1);
                    RawDataCollection rawData = new RawDataCollection(rawFile);
                    rawData.ExtractAll(rawFile);

                    /*
                     * if (idpyPars?.QuantMods != null)
                     * {
                     *  rawData.quantData.Quantify(rawData, rawFile, )
                     * }
                     */

                    QcDataContainer newQcData = ProcessQcData(Data: qcDataCollection, rawData: rawData, rawFile: rawFile, qcDirectory: qcDirectory);

                    if (searchParameters != null)
                    {
                        Search.WriteSearchMGF(qcParameters, rawData, rawFile, searchParameters.FixedScans);
                        Search.RunSearch(qcParameters, rawData, rawFile);
                        newQcData.ParseSearchResults(rawData, rawFile, qcParameters);

                        /*
                         * if (searchParameters.SearchAlgorithm == SearchAlgorithm.XTandem)
                         * {
                         *  SearchQC.ParseXTandem(newQcData, qcParameters);
                         *  newQcData.IdentipyParameters = String.Format("\"Algorithm: X!Tandem; fmods: {0}; nmod: {1}; kmod: {2}; xmod: {3}; fastaDB: {4}; xtandemDirectory: {5}\"",
                         *  searchParameters.FixedMods, searchParameters.NMod, searchParameters.KMod, searchParameters.XMod, searchParameters.FastaDatabase, searchParameters.XTandemDirectory);
                         * }
                         * else
                         * {
                         *  SearchQC.ParseIdentipy(newQcData, rawData, rawFile, qcParameters);
                         *  newQcData.IdentipyParameters = String.Format("\"Algorithm: IdentiPy; fmods: {0}; nmod: {1}; kmod: {2}; xmod: {3}; fastaDB: {4}; pythonExecutable: {5}; identipyScript: {6}\"",
                         *  searchParameters.FixedMods, searchParameters.NMod, searchParameters.KMod, searchParameters.XMod, searchParameters.FastaDatabase, searchParameters.PythonExecutable, searchParameters.IdentipyScript);
                         * }
                         */
                    }

                    qcDataCollection.QcData.Add(rawFile.CreationDate, newQcData);
                    qcDataCollection.ProcessedRawFiles.Add(Path.GetFileName(rawData.rawFileName));
                    qcDataCollection.WriteQcToTable();
                }

                Log.Information("QC finished: {File}", fileName);
            }

            Log.Information("QC of all files completed");
            Console.WriteLine("QC of all files completed!");

            try
            {
                XmlSerialization.WriteToXmlFile <QcDataCollection>(qcFile, qcDataCollection);
                Log.Information("QC file saved successfully");
                Console.WriteLine("QC file saved successfully");
            }
            catch (Exception e)
            {
                Log.Error(e, "Failed during serialization of QC data");
                throw e;
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Loads all scan data from a Thermo .raw file.
        /// </summary>
        public static ThermoRawFileReader LoadAllStaticData(string filePath, IFilteringParams filterParams = null, int maxThreads = -1)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException();
            }

            Loaders.LoadElements();

            // I don't know why this line needs to be here, but it does...
            var temp = RawFileReaderAdapter.FileFactory(filePath);

            var threadManager   = RawFileReaderFactory.CreateThreadManager(filePath);
            var rawFileAccessor = threadManager.CreateThreadAccessor();

            if (!rawFileAccessor.IsOpen)
            {
                throw new MzLibException("Unable to access RAW file!");
            }

            if (rawFileAccessor.IsError)
            {
                throw new MzLibException("Error opening RAW file!");
            }

            if (rawFileAccessor.InAcquisition)
            {
                throw new MzLibException("RAW file still being acquired!");
            }

            rawFileAccessor.SelectInstrument(Device.MS, 1);
            var msDataScans = new MsDataScan[rawFileAccessor.RunHeaderEx.LastSpectrum];

            Parallel.ForEach(Partitioner.Create(0, msDataScans.Length), new ParallelOptions {
                MaxDegreeOfParallelism = maxThreads
            }, (fff, loopState) =>
            {
                IRawDataPlus myThreadDataReader = threadManager.CreateThreadAccessor();
                myThreadDataReader.SelectInstrument(Device.MS, 1);

                for (int s = fff.Item1; s < fff.Item2; s++)
                {
                    try
                    {
                        var scan       = GetOneBasedScan(myThreadDataReader, filterParams, s + 1);
                        msDataScans[s] = scan;
                    }
                    catch (Exception ex)
                    {
                        throw new MzLibException("Error reading scan " + (s + 1) + ": " + ex.Message);
                    }
                }
            });

            rawFileAccessor.Dispose();

            string sendCheckSum;

            using (FileStream stream = File.OpenRead(filePath))
            {
                using (SHA1Managed sha = new SHA1Managed())
                {
                    byte[] checksum = sha.ComputeHash(stream);
                    sendCheckSum = BitConverter.ToString(checksum)
                                   .Replace("-", string.Empty);
                }
            }

            SourceFile sourceFile = new SourceFile(
                @"Thermo nativeID format",
                @"Thermo RAW format",
                sendCheckSum,
                @"SHA-1",
                filePath,
                Path.GetFileNameWithoutExtension(filePath));

            return(new ThermoRawFileReader(msDataScans, sourceFile));
        }
Esempio n. 18
0
        private static void WatchFile(string pathToRawFile)
        {
            using (var f = new StreamWriter("rawFileAcquisitionInfo.txt", append: true))
            {
                using (var rawFile = RawFileReaderFactory.CreateThreadManager(pathToRawFile).CreateThreadAccessor())
                {
                    rawFile.SelectMsData();

                    DateTime created = DateTime.Now;

                    f.WriteLine($"{pathToRawFile} created at {created}");
                    f.WriteLine($"InAcquisition: {rawFile.InAcquisition}");
                    f.WriteLine($"Run header: {rawFile.RunHeader.ToString()}");
                    f.WriteLine($"Spectra count: {rawFile.RunHeaderEx.SpectraCount}");

                    Console.WriteLine($"{pathToRawFile} created at {created}");
                    Console.WriteLine($"InAcquisition: {rawFile.InAcquisition}");
                    Console.WriteLine($"Run header: {rawFile.RunHeader.ToString()}");
                    Console.WriteLine($"Spectra count: {rawFile.RunHeaderEx.SpectraCount}");

                    int lastSpectrum = rawFile.RunHeader.LastSpectrum;
                    int spectraCount = rawFile.RunHeaderEx.SpectraCount;

                    while (spectraCount == rawFile.RunHeaderEx.SpectraCount || lastSpectrum == rawFile.RunHeader.LastSpectrum)
                    {
                        Console.WriteLine($"Waiting for a spectrum to be recorded. {(DateTime.Now - created).TotalSeconds} seconds elapsed.");
                        f.WriteLine($"Waiting for a spectrum to be recorded. {(DateTime.Now - created).TotalSeconds} seconds elapsed.");
                        Task.Delay(5000).Wait();
                        rawFile.RefreshViewOfFile();
                    }

                    Console.WriteLine("A spectrum has been recorded.");
                    f.WriteLine("A spectrum has been recorded.");

                    var firstRecordedSpec      = rawFile.RunHeader.FirstSpectrum;
                    var rtInSecBeforeFirstScan = rawFile.RetentionTimeFromScanNumber(firstRecordedSpec) * 60;

                    var deadTime = (DateTime.Now - created).TotalSeconds - rtInSecBeforeFirstScan;
                    Console.WriteLine($"Dead time: {deadTime}");
                    f.WriteLine($"Dead time: {deadTime}");

                    Console.WriteLine("Now watching the file as new spectra are recorded.");
                    f.WriteLine("Now watching the file as new spectra are recorded.");

                    while (rawFile.InAcquisition)
                    {
                        rawFile.RefreshViewOfFile();

                        lastSpectrum = rawFile.RunHeader.LastSpectrum;
                        spectraCount = rawFile.RunHeaderEx.SpectraCount;
                        var scan = rawFile.GetScanEventForScanNumber(lastSpectrum);

                        Console.WriteLine($"Seconds since file creation: {(DateTime.Now - created).TotalSeconds}");
                        Console.WriteLine($"Current retention time: {rawFile.RetentionTimeFromScanNumber(lastSpectrum)}");
                        Console.WriteLine($"Estimated time until end of run: {rawFile.RunHeader.ExpectedRuntime - rawFile.RetentionTimeFromScanNumber(lastSpectrum)}");
                        Console.WriteLine($"Latest spectrum: {lastSpectrum}\tTotal spectra: {spectraCount}");
                        Console.WriteLine($"Base intensity of latest scan: {rawFile.GetSegmentedScanFromScanNumber(lastSpectrum, null).Intensities.Max()}");
                        Console.WriteLine($"MS order of latest scan: {scan.MSOrder}");
                        Console.WriteLine();

                        f.WriteLine($"Seconds since file creation: {(DateTime.Now - created).TotalSeconds}");
                        f.WriteLine($"Current retention time: {rawFile.RetentionTimeFromScanNumber(lastSpectrum)}");
                        f.WriteLine($"Estimated time until end of run: {rawFile.RunHeader.ExpectedRuntime - rawFile.RetentionTimeFromScanNumber(lastSpectrum)}");
                        f.WriteLine($"Latest spectrum: {lastSpectrum}\tTotal spectra: {spectraCount}");
                        f.WriteLine($"Base intensity of latest scan: {rawFile.GetSegmentedScanFromScanNumber(lastSpectrum, null).Intensities.Max()}");
                        f.WriteLine($"MS order of latest scan: {scan.MSOrder}");
                        f.WriteLine();

                        Task.Delay(30000).Wait();
                    }

                    f.WriteLine($"{rawFile.FileName} reports it is done being acquired!");
                    Console.WriteLine($"{rawFile.FileName} reports it is done being acquired! Press any key to exit.");
                    Console.ReadKey();
                    Environment.Exit(0);
                }
            }
        }
Esempio n. 19
0
        public static void DoQc(WorkflowParameters parameters)
        {
            //QcDataCollectionDDA qcDataCollection;
            dynamic       qcDataCollection;
            string        dataDirectory         = parameters.RawFileDirectory;
            string        qcDirectory           = parameters.QcParams.QcDirectory;
            string        qcSearchDataDirecotry = parameters.QcParams.QcSearchDataDirectory;
            List <string> fileList = new List <string>();

            string qcFile = Path.Combine(qcDirectory, "QC.xml");

            (fileList, qcDataCollection) = GetFileListAndQcFile(parameters);

            foreach (string fileName in fileList)
            {
                Console.WriteLine("\nProcessing {0}", fileName);

                if (!RawFileInfo.CheckIfValid(fileName))
                {
                    continue;
                }
                // okay, it is probably a real raw file, let's do the QC

                // check if the raw file already exists in the QC data with a different name
                if (CheckIfFilePresentInQcCollection(fileName, qcDataCollection))
                {
                    Log.Information("A file with the same creation date and time as {File} already exists in the QC data", fileName);

                    Console.WriteLine("A file with the same creation date and time as {File} already exists in the QC data. Skipping to next file.",
                                      fileName);

                    continue;
                }

                IRawFileThreadManager rawFileThreadManager = RawFileReaderFactory.CreateThreadManager(fileName);

                if (parameters.ExpType == ExperimentType.DDA)
                {
                    WorkFlowsDDA.QcDDA(rawFileThreadManager, parameters);
                }
                //else if (parameters.ExpType == ExperimentType.DIA)
                //{
                //    WorkFlowsDIA.QcDIA(rawFileThreadManager, parameters);
                //}

                /*
                 * using (IRawDataPlus rawFile = RawFileReaderFactory.ReadFile(fileName))
                 * {
                 *  rawFile.SelectInstrument(Device.MS, 1);
                 *
                 *  if (parameters.ExpType == ExperimentType.DDA)
                 *  {
                 *      WorkFlowsDDA.QcDDA(rawFile, parameters);
                 *  }
                 *  else if (parameters.ExpType == ExperimentType.DIA)
                 *  {
                 *      WorkFlowsDIA.QcDIA(rawFile, parameters);
                 *  }
                 * }*/

                Log.Information("QC finished: {File}", fileName);
            }

            Log.Information("QC of all files completed");
            Console.WriteLine("QC of all files completed!");
        }
        /// <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);
            }
        }
Esempio n. 21
0
        /// <summary>
        /// Extract the RAW file metadata and spectra in MGF format.
        /// </summary>
        public void Extract()
        {
            // Check to see if the RAW file name was supplied as an argument to the program
            if (string.IsNullOrEmpty(rawFilePath))
            {
                Console.WriteLine("No RAW file specified!");

                return;
            }

            // Check to see if the specified RAW file exists
            if (!File.Exists(rawFilePath))
            {
                Console.WriteLine(@"The file doesn't exist in the specified location - " + rawFilePath);

                return;
            }
            else
            {
                string[] splittedPath = rawFilePath.Split('/');
                rawFileName = splittedPath[splittedPath.Length - 1];
                rawFileNameWithoutExtension = Path.GetFileNameWithoutExtension(rawFileName);
            }

            Console.WriteLine("Started parsing " + rawFilePath);

            // Create the IRawDataPlus object for accessing the RAW file
            //var rawFile = RawFileReaderAdapter.FileFactory(rawFilePath);
            IRawDataPlus rawFile;

            using (rawFile = RawFileReaderFactory.ReadFile(rawFilePath))
            {
                if (!rawFile.IsOpen)
                {
                    Console.WriteLine("Unable to access the RAW file using the RawFileReader class!");

                    return;
                }

                // Check for any errors in the RAW file
                if (rawFile.IsError)
                {
                    Console.WriteLine($"Error opening ({rawFile.FileError}) - {rawFilePath}");

                    return;
                }

                // Check if the RAW file is being acquired
                if (rawFile.InAcquisition)
                {
                    Console.WriteLine("RAW file still being acquired - " + rawFilePath);

                    return;
                }

                // Get the number of instruments (controllers) present in the RAW file and set the
                // selected instrument to the MS instrument, first instance of it
                //Console.WriteLine("The RAW file has data from {0} instruments", rawFile.InstrumentCount);

                rawFile.SelectInstrument(Device.MS, 1);

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

                if (outputMetadata)
                {
                    WriteMetada(rawFile, firstScanNumber, lastScanNumber);
                }

                rawFile.SelectInstrument(Device.MS, 2);

                WriteSpectraToMgf(rawFile, firstScanNumber, lastScanNumber);

                Console.WriteLine("Finished parsing " + rawFilePath);
            }
        }
Esempio n. 22
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);
            }
        }
        public List <CompoundData> ReadSpectraData(List <CompoundData> combinedTransitions)
        {
            var scanTimes = new Dictionary <int, double>();

            //using (var rawReader = rawReaderThreader.CreateThreadAccessor())
            using (var rawReader = RawFileReaderFactory.ReadFile(DatasetPath))
            {
                if (!rawReader.SelectMsData())
                {
                    // dataset has no MS data. Return.
                    return(null);
                }

                var header   = rawReader.RunHeaderEx;
                var minScan  = header.FirstSpectrum;
                var maxScan  = header.LastSpectrum;
                var numScans = header.SpectraCount;
                for (var i = 1; i <= numScans; i++)
                {
                    var time = rawReader.RetentionTimeFromScanNumber(i);
                    scanTimes.Add(i, time);
                    // Filter string parsing for speed: "+ c NSI SRM ms2 [precursor/parent m/z] [[list of product m/z ranges]]
                }

                // Map transition start/stop times to scans
                var scansAndTargets = new Dictionary <int, List <CompoundData> >();
                foreach (var scan in scanTimes)
                {
                    var matches = combinedTransitions
                                  .Where(x => x.StartTimeMinutes <= scan.Value && scan.Value <= x.StopTimeMinutes).ToList();
                    if (matches.Count > 0)
                    {
                        scansAndTargets.Add(scan.Key, matches);
                    }
                }

                // This works for the Altis file, but not for the Vantage file
                // Should be able to key on 'canUseCompoundNames'
                //var compounds = rawReader.GetCompoundNames();

                // read spectra data for each transition from the file, in an optimized fashion
                foreach (var scan in scansAndTargets)
                {
                    // This works for the Altis file, but not for the Vantage file
                    //var scanInfoX = rawReader.GetScanFiltersFromCompoundName(scan.Value[0].Transition.CompoundName);
                    var scanInfo = rawReader.GetFilterForScanNumber(scan.Key);
                    var reaction = scanInfo.GetReaction(0);
                    var preMz    = reaction.PrecursorMass;
                    var data     = rawReader.GetSimplifiedScan(scan.Key);

                    foreach (var compound in scan.Value.Where(x => Math.Abs(x.PrecursorMz - preMz) < 0.01))
                    {
                        for (var i = 0; i < scanInfo.MassRangeCount; i++)
                        {
                            var massRange = scanInfo.GetMassRange(i);
                            var match     = compound.Transitions.FirstOrDefault(x => massRange.Low <= x.ProductMz && x.ProductMz <= massRange.High);
                            if (match == null)
                            {
                                continue;
                            }

                            for (var j = 0; j < data.Masses.Length; j++)
                            {
                                var mz        = data.Masses[j];
                                var intensity = data.Intensities[j];

                                if (massRange.Low <= mz && mz <= massRange.High)
                                {
                                    match.IntensitySum += intensity;
                                    match.Intensities.Add(new TransitionData.DataPoint(scan.Key, scanTimes[scan.Key], intensity));
                                    if (intensity > match.MaxIntensity)
                                    {
                                        match.MaxIntensity     = intensity;
                                        match.MaxIntensityTime = scanTimes[scan.Key];
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(combinedTransitions.ToList());
        }