Beispiel #1
0
 /// <summary>
 /// Close the reader
 /// </summary>
 public void Close()
 {
     if (_msfileReader != null)
     {
         _msfileReader.CloseRawFile();
     }
 }
Beispiel #2
0
        public override void Close()
        {
            try
            {
                mRawFileReader.CloseRawFile();
            }
            catch (AccessViolationException)
            {
                // Ignore errors here
            }

            base.Close();
        }
Beispiel #3
0
 /// <summary>
 /// Close the reader
 /// </summary>
 public void Close()
 {
     _msfileReader?.CloseRawFile();
 }
Beispiel #4
0
        /// <summary>
        /// Process the dataset
        /// </summary>
        /// <param name="dataFilePath"></param>
        /// <param name="datasetFileInfo"></param>
        /// <returns>True if success, False if an error or if the file has no scans</returns>
        /// <remarks></remarks>
        public override bool ProcessDataFile(string dataFilePath, DatasetFileInfo datasetFileInfo)
        {
            ResetResults();

            var dataFilePathLocal = string.Empty;

            // Obtain the full path to the file
            var rawFile = new FileInfo(dataFilePath);

            if (!rawFile.Exists)
            {
                OnErrorEvent(".Raw file not found: " + dataFilePath);
                return(false);
            }

            // Future, optional: Determine the DatasetID
            // Unfortunately, this is not present in metadata.txt
            // datasetID = LookupDatasetID(datasetName)
            var datasetID = DatasetID;

            // Record the file size and Dataset ID
            datasetFileInfo.FileSystemCreationTime     = rawFile.CreationTime;
            datasetFileInfo.FileSystemModificationTime = rawFile.LastWriteTime;

            // The acquisition times will get updated below to more accurate values
            datasetFileInfo.AcqTimeStart = datasetFileInfo.FileSystemModificationTime;
            datasetFileInfo.AcqTimeEnd   = datasetFileInfo.FileSystemModificationTime;

            datasetFileInfo.DatasetID     = datasetID;
            datasetFileInfo.DatasetName   = GetDatasetNameViaPath(rawFile.Name);
            datasetFileInfo.FileExtension = rawFile.Extension;
            datasetFileInfo.FileSizeBytes = rawFile.Length;

            datasetFileInfo.ScanCount = 0;

            mDatasetStatsSummarizer.ClearCachedData();

            var deleteLocalFile = false;
            var readError       = false;

            // Note: as of June 2018 this only works if you disable "Prefer 32-bit" when compiling as AnyCPU

            // Use XRaw to read the .Raw file
            // If reading from a SAMBA-mounted network share, and if the current user has
            //  Read privileges but not Read&Execute privileges, we will need to copy the file locally

            var readerOptions = new ThermoReaderOptions
            {
                LoadMSMethodInfo = true,
                LoadMSTuneInfo   = true
            };

            var xcaliburAccessor = new XRawFileIO(readerOptions);

            RegisterEvents(xcaliburAccessor);

            // Open a handle to the data file
            if (!xcaliburAccessor.OpenRawFile(rawFile.FullName))
            {
                // File open failed
                OnErrorEvent("Call to .OpenRawFile failed for: " + rawFile.FullName);
                ErrorCode = iMSFileInfoScanner.eMSFileScannerErrorCodes.ThermoRawFileReaderError;
                readError = true;

                if (!string.Equals(clsMSFileInfoScanner.GetAppDirectoryPath().Substring(0, 2), rawFile.FullName.Substring(0, 2), StringComparison.InvariantCultureIgnoreCase))
                {
                    if (mCopyFileLocalOnReadError)
                    {
                        // Copy the file locally and try again

                        try
                        {
                            dataFilePathLocal = Path.Combine(clsMSFileInfoScanner.GetAppDirectoryPath(), Path.GetFileName(dataFilePath));

                            if (!string.Equals(dataFilePathLocal, dataFilePath, StringComparison.InvariantCultureIgnoreCase))
                            {
                                OnDebugEvent("Copying file " + Path.GetFileName(dataFilePath) + " to the working directory");
                                File.Copy(dataFilePath, dataFilePathLocal, true);

                                dataFilePath    = string.Copy(dataFilePathLocal);
                                deleteLocalFile = true;

                                // Update rawFile then try to re-open
                                rawFile = new FileInfo(dataFilePath);

                                if (!xcaliburAccessor.OpenRawFile(rawFile.FullName))
                                {
                                    // File open failed
                                    OnErrorEvent("Call to .OpenRawFile failed for: " + rawFile.FullName);
                                    ErrorCode = iMSFileInfoScanner.eMSFileScannerErrorCodes.ThermoRawFileReaderError;
                                    readError = true;
                                }
                                else
                                {
                                    readError = false;
                                }
                            }
                        }
                        catch (Exception)
                        {
                            readError = true;
                        }
                    }
                }
            }

            if (!readError)
            {
                // Read the file info
                try
                {
                    datasetFileInfo.AcqTimeStart = xcaliburAccessor.FileInfo.CreationDate;
                }
                catch (Exception)
                {
                    // Read error
                    readError = true;
                }

                if (!readError)
                {
                    try
                    {
                        datasetFileInfo.ScanCount = xcaliburAccessor.GetNumScans();

                        if (datasetFileInfo.ScanCount > 0)
                        {
                            // Look up the end scan time then compute .AcqTimeEnd
                            var scanEnd = xcaliburAccessor.FileInfo.ScanEnd;
                            xcaliburAccessor.GetScanInfo(scanEnd, out clsScanInfo scanInfo);

                            datasetFileInfo.AcqTimeEnd = datasetFileInfo.AcqTimeStart.AddMinutes(scanInfo.RetentionTime);
                        }
                        else
                        {
                            datasetFileInfo.AcqTimeEnd = datasetFileInfo.AcqTimeStart;
                        }
                    }
                    catch (Exception)
                    {
                        // Error; use default values
                        datasetFileInfo.AcqTimeEnd = datasetFileInfo.AcqTimeStart;
                        datasetFileInfo.ScanCount  = 0;
                    }

                    if (mSaveTICAndBPI || mCreateDatasetInfoFile || mCreateScanStatsFile ||
                        mSaveLCMS2DPlots || mCheckCentroidingStatus || MS2MzMin > 0)
                    {
                        // Load data from each scan
                        // This is used to create the TIC and BPI plot, the 2D LC/MS plot, and/or to create the Dataset Info File
                        LoadScanDetails(xcaliburAccessor);
                    }

                    if (mComputeOverallQualityScores)
                    {
                        // Note that this call will also create the TICs and BPIs
                        ComputeQualityScores(xcaliburAccessor, datasetFileInfo);
                    }

                    if (MS2MzMin > 0 && datasetFileInfo.ScanCount > 0)
                    {
                        // Verify that all of the MS2 spectra have m/z values below the required minimum
                        // Useful for validating that reporter ions can be detected
                        ValidateMS2MzMin();
                    }
                }
            }

            mDatasetStatsSummarizer.SampleInfo.SampleName = xcaliburAccessor.FileInfo.SampleName;
            mDatasetStatsSummarizer.SampleInfo.Comment1   = xcaliburAccessor.FileInfo.Comment1;
            mDatasetStatsSummarizer.SampleInfo.Comment2   = xcaliburAccessor.FileInfo.Comment2;

            // Add the devices
            var deviceFilterList = new SortedSet <Device> {
                Device.MS,
                Device.MSAnalog
            };

            // First add the MS devices
            AddThermoDevices(xcaliburAccessor, datasetFileInfo, deviceFilterList, new SortedSet <Device>());

            // Now add any non-mass spec devices
            AddThermoDevices(xcaliburAccessor, datasetFileInfo, new SortedSet <Device>(), deviceFilterList);

            if (mSaveTICAndBPI && datasetFileInfo.DeviceList.Count > 0)
            {
                mInstrumentSpecificPlots.Clear();

                foreach (var device in datasetFileInfo.DeviceList)
                {
                    if (device.DeviceType == Device.MS || device.DeviceType == Device.MSAnalog)
                    {
                        continue;
                    }

                    var chromatogramData = xcaliburAccessor.GetChromatogramData(device.DeviceType, device.DeviceNumber);
                    if (chromatogramData.Count == 0)
                    {
                        continue;
                    }

                    var devicePlot = AddInstrumentSpecificPlot(device.DeviceDescription);

                    devicePlot.TICXAxisLabel = string.IsNullOrWhiteSpace(device.AxisLabelX) ? "Scan number" : device.AxisLabelX;
                    devicePlot.TICYAxisLabel = device.YAxisLabelWithUnits;

                    devicePlot.TICYAxisExponentialNotation = false;

                    devicePlot.DeviceType           = device.DeviceType;
                    devicePlot.TICPlotAbbrev        = string.Format("{0}{1}", device.DeviceType.ToString(), device.DeviceNumber);
                    devicePlot.TICAutoMinMaxY       = true;
                    devicePlot.RemoveZeroesFromEnds = false;

                    float acqLengthMinutes;
                    if (datasetFileInfo.AcqTimeEnd > datasetFileInfo.AcqTimeStart)
                    {
                        acqLengthMinutes = (float)datasetFileInfo.AcqTimeEnd.Subtract(datasetFileInfo.AcqTimeStart).TotalMinutes;
                    }
                    else
                    {
                        acqLengthMinutes = chromatogramData.Count;
                    }

                    var dataCount = chromatogramData.Count;

                    foreach (var dataPoint in chromatogramData)
                    {
                        var   scanNumber = dataPoint.Key;
                        float scanTimeMinutes;
                        if (acqLengthMinutes > 0)
                        {
                            scanTimeMinutes = scanNumber / (float)dataCount * acqLengthMinutes;
                        }
                        else
                        {
                            scanTimeMinutes = scanNumber;
                        }

                        devicePlot.AddDataTICOnly(scanNumber, 1, scanTimeMinutes, dataPoint.Value);
                    }
                }
            }

            if (!string.IsNullOrEmpty(xcaliburAccessor.FileInfo.SampleComment))
            {
                if (string.IsNullOrEmpty(mDatasetStatsSummarizer.SampleInfo.Comment1))
                {
                    mDatasetStatsSummarizer.SampleInfo.Comment1 = xcaliburAccessor.FileInfo.SampleComment;
                }
                else
                {
                    if (string.IsNullOrEmpty(mDatasetStatsSummarizer.SampleInfo.Comment2))
                    {
                        mDatasetStatsSummarizer.SampleInfo.Comment2 = xcaliburAccessor.FileInfo.SampleComment;
                    }
                    else
                    {
                        // Append the sample comment to comment 2
                        mDatasetStatsSummarizer.SampleInfo.Comment2 += "; " + xcaliburAccessor.FileInfo.SampleComment;
                    }
                }
            }

            // Close the handle to the data file
            xcaliburAccessor.CloseRawFile();

            // Read the file info from the file system
            // (much of this is already in datasetFileInfo, but we'll call UpdateDatasetFileStats() anyway to make sure all of the necessary steps are taken)
            // This will also compute the SHA-1 hash of the .Raw file and add it to mDatasetStatsSummarizer.DatasetFileInfo
            UpdateDatasetFileStats(rawFile, datasetID);

            // Copy over the updated file time info from datasetFileInfo to mDatasetStatsSummarizer.DatasetFileInfo
            UpdateDatasetStatsSummarizerUsingDatasetFileInfo(datasetFileInfo);

            // Delete the local copy of the data file
            if (deleteLocalFile)
            {
                try
                {
                    File.Delete(dataFilePathLocal);
                }
                catch (Exception)
                {
                    // Deletion failed
                    OnErrorEvent("Deletion failed for: " + Path.GetFileName(dataFilePathLocal));
                }
            }

            PostProcessTasks();

            return(!readError);
        }
Beispiel #5
0
        /// <summary>
        /// Process the dataset
        /// </summary>
        /// <param name="strDataFilePath"></param>
        /// <param name="datasetFileInfo"></param>
        /// <returns>True if success, False if an error</returns>
        /// <remarks></remarks>
        public override bool ProcessDataFile(string strDataFilePath, clsDatasetFileInfo datasetFileInfo)
        {
            var strDataFilePathLocal = string.Empty;

            // Obtain the full path to the file
            var fiRawFile = new FileInfo(strDataFilePath);

            if (!fiRawFile.Exists)
            {
                OnErrorEvent(".Raw file not found: " + strDataFilePath);
                return(false);
            }

            // Future, optional: Determine the DatasetID
            // Unfortunately, this is not present in metadata.txt
            // intDatasetID = LookupDatasetID(strDatasetName)
            var intDatasetID = DatasetID;

            // Record the file size and Dataset ID
            datasetFileInfo.FileSystemCreationTime     = fiRawFile.CreationTime;
            datasetFileInfo.FileSystemModificationTime = fiRawFile.LastWriteTime;

            // The acquisition times will get updated below to more accurate values
            datasetFileInfo.AcqTimeStart = datasetFileInfo.FileSystemModificationTime;
            datasetFileInfo.AcqTimeEnd   = datasetFileInfo.FileSystemModificationTime;

            datasetFileInfo.DatasetID     = intDatasetID;
            datasetFileInfo.DatasetName   = GetDatasetNameViaPath(fiRawFile.Name);
            datasetFileInfo.FileExtension = fiRawFile.Extension;
            datasetFileInfo.FileSizeBytes = fiRawFile.Length;

            datasetFileInfo.ScanCount = 0;

            mDatasetStatsSummarizer.ClearCachedData();

            var blnDeleteLocalFile = false;
            var blnReadError       = false;

            // Use Xraw to read the .Raw file
            // If reading from a SAMBA-mounted network share, and if the current user has
            //  Read privileges but not Read&Execute privileges, then we will need to copy the file locally
            var xcaliburAccessor = new XRawFileIO();

            // Attach event handlers
            xcaliburAccessor.ReportError   += XcaliburAccessor_ReportError;
            xcaliburAccessor.ReportWarning += XcaliburAccessor_ReportWarning;

            // Open a handle to the data file
            if (!xcaliburAccessor.OpenRawFile(fiRawFile.FullName))
            {
                // File open failed
                OnErrorEvent("Call to .OpenRawFile failed for: " + fiRawFile.FullName);
                blnReadError = true;

                if (!string.Equals(clsMSFileInfoScanner.GetAppFolderPath().Substring(0, 2), fiRawFile.FullName.Substring(0, 2), StringComparison.InvariantCultureIgnoreCase))
                {
                    if (mCopyFileLocalOnReadError)
                    {
                        // Copy the file locally and try again

                        try
                        {
                            strDataFilePathLocal = Path.Combine(clsMSFileInfoScanner.GetAppFolderPath(), Path.GetFileName(strDataFilePath));

                            if (!string.Equals(strDataFilePathLocal, strDataFilePath, StringComparison.InvariantCultureIgnoreCase))
                            {
                                OnDebugEvent("Copying file " + Path.GetFileName(strDataFilePath) + " to the working folder");
                                File.Copy(strDataFilePath, strDataFilePathLocal, true);

                                strDataFilePath    = string.Copy(strDataFilePathLocal);
                                blnDeleteLocalFile = true;

                                // Update fiRawFile then try to re-open
                                fiRawFile = new FileInfo(strDataFilePath);

                                if (!xcaliburAccessor.OpenRawFile(fiRawFile.FullName))
                                {
                                    // File open failed
                                    OnErrorEvent("Call to .OpenRawFile failed for: " + fiRawFile.FullName);
                                    blnReadError = true;
                                }
                                else
                                {
                                    blnReadError = false;
                                }
                            }
                        }
                        catch (Exception)
                        {
                            blnReadError = true;
                        }
                    }
                }
            }

            if (!blnReadError)
            {
                // Read the file info
                try
                {
                    datasetFileInfo.AcqTimeStart = xcaliburAccessor.FileInfo.CreationDate;
                }
                catch (Exception)
                {
                    // Read error
                    blnReadError = true;
                }

                if (!blnReadError)
                {
                    try
                    {
                        // Look up the end scan time then compute .AcqTimeEnd
                        var intScanEnd = xcaliburAccessor.FileInfo.ScanEnd;
                        xcaliburAccessor.GetScanInfo(intScanEnd, out clsScanInfo scanInfo);

                        datasetFileInfo.AcqTimeEnd = datasetFileInfo.AcqTimeStart.AddMinutes(scanInfo.RetentionTime);
                        datasetFileInfo.ScanCount  = xcaliburAccessor.GetNumScans();
                    }
                    catch (Exception)
                    {
                        // Error; use default values
                        datasetFileInfo.AcqTimeEnd = datasetFileInfo.AcqTimeStart;
                        datasetFileInfo.ScanCount  = 0;
                    }

                    if (mSaveTICAndBPI || mCreateDatasetInfoFile || mCreateScanStatsFile || mSaveLCMS2DPlots || mCheckCentroidingStatus)
                    {
                        // Load data from each scan
                        // This is used to create the TIC and BPI plot, the 2D LC/MS plot, and/or to create the Dataset Info File
                        LoadScanDetails(xcaliburAccessor);
                    }

                    if (mComputeOverallQualityScores)
                    {
                        // Note that this call will also create the TICs and BPIs
                        ComputeQualityScores(xcaliburAccessor, datasetFileInfo);
                    }
                }
            }

            mDatasetStatsSummarizer.SampleInfo.SampleName = xcaliburAccessor.FileInfo.SampleName;
            mDatasetStatsSummarizer.SampleInfo.Comment1   = xcaliburAccessor.FileInfo.Comment1;
            mDatasetStatsSummarizer.SampleInfo.Comment2   = xcaliburAccessor.FileInfo.Comment2;

            if (!string.IsNullOrEmpty(xcaliburAccessor.FileInfo.SampleComment))
            {
                if (string.IsNullOrEmpty(mDatasetStatsSummarizer.SampleInfo.Comment1))
                {
                    mDatasetStatsSummarizer.SampleInfo.Comment1 = xcaliburAccessor.FileInfo.SampleComment;
                }
                else
                {
                    if (string.IsNullOrEmpty(mDatasetStatsSummarizer.SampleInfo.Comment2))
                    {
                        mDatasetStatsSummarizer.SampleInfo.Comment2 = xcaliburAccessor.FileInfo.SampleComment;
                    }
                    else
                    {
                        // Append the sample comment to comment 2
                        mDatasetStatsSummarizer.SampleInfo.Comment2 += "; " + xcaliburAccessor.FileInfo.SampleComment;
                    }
                }
            }

            // Close the handle to the data file
            xcaliburAccessor.CloseRawFile();

            // Read the file info from the file system
            // (much of this is already in datasetFileInfo, but we'll call UpdateDatasetFileStats() anyway to make sure all of the necessary steps are taken)
            UpdateDatasetFileStats(fiRawFile, intDatasetID);

            // Copy over the updated filetime info from datasetFileInfo to mDatasetFileInfo
            mDatasetStatsSummarizer.DatasetFileInfo.FileSystemCreationTime     = datasetFileInfo.FileSystemCreationTime;
            mDatasetStatsSummarizer.DatasetFileInfo.FileSystemModificationTime = datasetFileInfo.FileSystemModificationTime;
            mDatasetStatsSummarizer.DatasetFileInfo.DatasetID     = datasetFileInfo.DatasetID;
            mDatasetStatsSummarizer.DatasetFileInfo.DatasetName   = string.Copy(datasetFileInfo.DatasetName);
            mDatasetStatsSummarizer.DatasetFileInfo.FileExtension = string.Copy(datasetFileInfo.FileExtension);
            mDatasetStatsSummarizer.DatasetFileInfo.AcqTimeStart  = datasetFileInfo.AcqTimeStart;
            mDatasetStatsSummarizer.DatasetFileInfo.AcqTimeEnd    = datasetFileInfo.AcqTimeEnd;
            mDatasetStatsSummarizer.DatasetFileInfo.ScanCount     = datasetFileInfo.ScanCount;
            mDatasetStatsSummarizer.DatasetFileInfo.FileSizeBytes = datasetFileInfo.FileSizeBytes;

            // Delete the local copy of the data file
            if (blnDeleteLocalFile)
            {
                try
                {
                    File.Delete(strDataFilePathLocal);
                }
                catch (Exception)
                {
                    // Deletion failed
                    OnErrorEvent("Deletion failed for: " + Path.GetFileName(strDataFilePathLocal));
                }
            }

            return(!blnReadError);
        }
Beispiel #6
0
        /// <summary>
        /// Looks for the reporter ion peaks using FindReporterIonsWork
        /// </summary>
        /// <param name="scanList"></param>
        /// <param name="spectraCache"></param>
        /// <param name="inputFilePathFull">Full path to the input file</param>
        /// <param name="outputDirectoryPath"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public bool FindReporterIons(
            clsScanList scanList,
            clsSpectraCache spectraCache,
            string inputFilePathFull,
            string outputDirectoryPath)
        {
            const char TAB_DELIMITER = '\t';

            var outputFilePath = "??";

            try
            {
                // Use Xraw to read the .Raw files
                var readerOptions = new ThermoReaderOptions()
                {
                    LoadMSMethodInfo = false,
                    LoadMSTuneInfo   = false
                };

                var rawFileReader = new XRawFileIO(readerOptions);
                RegisterEvents(rawFileReader);

                var includeFtmsColumns = false;

                if (inputFilePathFull.ToUpper().EndsWith(DataInput.clsDataImport.THERMO_RAW_FILE_EXTENSION.ToUpper()))
                {
                    // Processing a thermo .Raw file
                    // Check whether any of the frag scans has IsFTMS true
                    for (var masterOrderIndex = 0; masterOrderIndex < scanList.MasterScanOrderCount; masterOrderIndex++)
                    {
                        var scanPointer = scanList.MasterScanOrder[masterOrderIndex].ScanIndexPointer;
                        if (scanList.MasterScanOrder[masterOrderIndex].ScanType == clsScanList.eScanTypeConstants.SurveyScan)
                        {
                            // Skip survey scans
                            continue;
                        }

                        if (scanList.FragScans[scanPointer].IsFTMS)
                        {
                            includeFtmsColumns = true;
                            break;
                        }
                    }

                    if (includeFtmsColumns)
                    {
                        rawFileReader.OpenRawFile(inputFilePathFull);
                    }
                }

                if (mOptions.ReporterIons.ReporterIonList.Count == 0)
                {
                    // No reporter ions defined; default to ITraq
                    mOptions.ReporterIons.SetReporterIonMassMode(clsReporterIons.eReporterIonMassModeConstants.ITraqFourMZ);
                }

                // Populate array reporterIons, which we will sort by m/z
                var reporterIons = new clsReporterIonInfo[mOptions.ReporterIons.ReporterIonList.Count];

                var reporterIonIndex = 0;
                foreach (var reporterIon in mOptions.ReporterIons.ReporterIonList)
                {
                    reporterIons[reporterIonIndex] = reporterIon;
                    reporterIonIndex += 1;
                }

                Array.Sort(reporterIons, new clsReportIonInfoComparer());

                outputFilePath = clsDataOutput.ConstructOutputFilePath(
                    Path.GetFileName(inputFilePathFull),
                    outputDirectoryPath,
                    clsDataOutput.eOutputFileTypeConstants.ReporterIonsFile);

                using (var writer = new StreamWriter(outputFilePath))
                {
                    // Write the file headers
                    var reporterIonMZsUnique = new SortedSet <string>();
                    var headerColumns        = new List <string>(7 + reporterIons.Length + 1)
                    {
                        "Dataset",
                        "ScanNumber",
                        "Collision Mode",
                        "ParentIonMZ",
                        "BasePeakIntensity",
                        "BasePeakMZ",
                        "ReporterIonIntensityMax"
                    };

                    var obsMZHeaders = new List <string>(reporterIons.Length);
                    var uncorrectedIntensityHeaders = new List <string>(reporterIons.Length);
                    var ftmsSignalToNoise           = new List <string>(reporterIons.Length);
                    var ftmsResolution = new List <string>(reporterIons.Length);
                    //var ftmsLabelDataMz = new List<string>(reporterIons.Length);

                    var saveUncorrectedIntensities =
                        mOptions.ReporterIons.ReporterIonApplyAbundanceCorrection && mOptions.ReporterIons.ReporterIonSaveUncorrectedIntensities;

                    var dataAggregation = new clsDataAggregation();
                    RegisterEvents(dataAggregation);

                    foreach (var reporterIon in reporterIons)
                    {
                        if (!reporterIon.ContaminantIon || saveUncorrectedIntensities)
                        {
                            // Construct the reporter ion intensity header
                            // We skip contaminant ions, unless saveUncorrectedIntensities is True, then we include them

                            string mzValue;
                            if (mOptions.ReporterIons.ReporterIonMassMode == clsReporterIons.eReporterIonMassModeConstants.TMTTenMZ ||
                                mOptions.ReporterIons.ReporterIonMassMode == clsReporterIons.eReporterIonMassModeConstants.TMTElevenMZ ||
                                mOptions.ReporterIons.ReporterIonMassMode == clsReporterIons.eReporterIonMassModeConstants.TMTSixteenMZ)
                            {
                                mzValue = reporterIon.MZ.ToString("#0.000");
                            }
                            else
                            {
                                mzValue = ((int)Math.Round(reporterIon.MZ, 0)).ToString();
                            }

                            if (reporterIonMZsUnique.Contains(mzValue))
                            {
                                // Uniquify the m/z value
                                mzValue += "_" + reporterIonIndex;
                            }

                            try
                            {
                                reporterIonMZsUnique.Add(mzValue);
                            }
                            catch (Exception ex)
                            {
                                // Error updating the SortedSet;
                                // this shouldn't happen based on the .ContainsKey test above
                            }

                            // Append the reporter ion intensity title to the headers
                            headerColumns.Add("Ion_" + mzValue);

                            // This string will only be included in the header line if mOptions.ReporterIons.ReporterIonSaveObservedMasses is true
                            obsMZHeaders.Add("Ion_" + mzValue + "_ObsMZ");

                            // This string will be included in the header line if saveUncorrectedIntensities is true
                            uncorrectedIntensityHeaders.Add("Ion_" + mzValue + "_OriginalIntensity");

                            // This string will be included in the header line if includeFtmsColumns is true
                            ftmsSignalToNoise.Add("Ion_" + mzValue + "_SignalToNoise");
                            ftmsResolution.Add("Ion_" + mzValue + "_Resolution");

                            // Uncomment to include the label data m/z value in the _ReporterIons.txt file
                            // This string will only be included in the header line if mOptions.ReporterIons.ReporterIonSaveObservedMasses is true
                            //ftmsLabelDataMz.Add("Ion_" + mzValue + "_LabelDataMZ");
                        }
                    }

                    headerColumns.Add("Weighted Avg Pct Intensity Correction");

                    if (mOptions.ReporterIons.ReporterIonSaveObservedMasses)
                    {
                        headerColumns.AddRange(obsMZHeaders);
                    }

                    if (saveUncorrectedIntensities)
                    {
                        headerColumns.AddRange(uncorrectedIntensityHeaders);
                    }

                    if (includeFtmsColumns)
                    {
                        headerColumns.AddRange(ftmsSignalToNoise);
                        headerColumns.AddRange(ftmsResolution);
                        // Uncomment to include the label data m/z value in the _ReporterIons.txt file
                        // If mOptions.ReporterIons.ReporterIonSaveObservedMasses Then
                        // headerColumns.AddRange(ftmsLabelDataMz)
                        // End If
                    }

                    // Write the headers to the output file, separated by tabs
                    writer.WriteLine(string.Join(TAB_DELIMITER.ToString(), headerColumns));

                    UpdateProgress(0, "Searching for reporter ions");

                    for (var masterOrderIndex = 0; masterOrderIndex < scanList.MasterScanOrderCount; masterOrderIndex++)
                    {
                        var scanPointer = scanList.MasterScanOrder[masterOrderIndex].ScanIndexPointer;
                        if (scanList.MasterScanOrder[masterOrderIndex].ScanType == clsScanList.eScanTypeConstants.SurveyScan)
                        {
                            // Skip Survey Scans
                            continue;
                        }

                        FindReporterIonsWork(
                            rawFileReader,
                            dataAggregation,
                            includeFtmsColumns,
                            mOptions.SICOptions,
                            scanList,
                            spectraCache,
                            scanList.FragScans[scanPointer],
                            writer,
                            reporterIons,
                            TAB_DELIMITER,
                            saveUncorrectedIntensities,
                            mOptions.ReporterIons.ReporterIonSaveObservedMasses);

                        if (scanList.MasterScanOrderCount > 1)
                        {
                            UpdateProgress((short)(masterOrderIndex / (double)(scanList.MasterScanOrderCount - 1) * 100));
                        }
                        else
                        {
                            UpdateProgress(0);
                        }

                        UpdateCacheStats(spectraCache);
                        if (mOptions.AbortProcessing)
                        {
                            break;
                        }
                    }
                }

                if (includeFtmsColumns)
                {
                    // Close the handle to the data file
                    rawFileReader.CloseRawFile();
                }

                return(true);
            }
            catch (Exception ex)
            {
                ReportError("Error writing the reporter ions to: " + outputFilePath, ex, clsMASIC.eMasicErrorCodes.OutputFileWriteError);
                return(false);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Read scan data and ions from a Thermo .raw file
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="scanList"></param>
        /// <param name="spectraCache"></param>
        /// <param name="dataOutputHandler"></param>
        /// <param name="keepRawSpectra"></param>
        /// <param name="keepMSMSSpectra"></param>
        /// <returns>True if Success, False if failure</returns>
        /// <remarks>Assumes filePath exists</remarks>
        public bool ExtractScanInfoFromXcaliburDataFile(
            string filePath,
            clsScanList scanList,
            clsSpectraCache spectraCache,
            clsDataOutput dataOutputHandler,
            bool keepRawSpectra,
            bool keepMSMSSpectra)
        {
            // Use XrawFileIO to read the .Raw files (it uses ThermoFisher.CommonCore)

            var readerOptions = new ThermoReaderOptions
            {
                LoadMSMethodInfo = mOptions.WriteMSMethodFile,
                LoadMSTuneInfo   = mOptions.WriteMSTuneFile
            };

            var xcaliburAccessor = new XRawFileIO(readerOptions)
            {
                ScanInfoCacheMaxSize = 0    // Don't cache scanInfo objects
            };

            RegisterEvents(xcaliburAccessor);

            mBpiUpdateCount = 0;

            // Assume success for now
            var success = true;

            try
            {
                Console.Write("Reading Thermo .raw file ");
                ReportMessage("Reading Thermo .raw file");

                UpdateProgress(0, "Opening data file:" + Environment.NewLine + Path.GetFileName(filePath));

                // Obtain the full path to the file
                var rawFileInfo       = new FileInfo(filePath);
                var inputFileFullPath = rawFileInfo.FullName;

                // Open a handle to the data file
                if (!xcaliburAccessor.OpenRawFile(inputFileFullPath))
                {
                    ReportError("Error opening input data file: " + inputFileFullPath + " (xcaliburAccessor.OpenRawFile returned False)");
                    SetLocalErrorCode(clsMASIC.eMasicErrorCodes.InputFileAccessError);
                    return(false);
                }

                var datasetID = mOptions.SICOptions.DatasetID;

                success = UpdateDatasetFileStats(rawFileInfo, datasetID, xcaliburAccessor);

                var metadataWriter = new clsThermoMetadataWriter();
                RegisterEvents(metadataWriter);

                if (mOptions.WriteMSMethodFile)
                {
                    metadataWriter.SaveMSMethodFile(xcaliburAccessor, dataOutputHandler);
                }

                if (mOptions.WriteMSTuneFile)
                {
                    metadataWriter.SaveMSTuneFile(xcaliburAccessor, dataOutputHandler);
                }

                var scanCount = xcaliburAccessor.GetNumScans();

                if (scanCount <= 0)
                {
                    // No scans found
                    ReportError("No scans found in the input file: " + filePath);
                    SetLocalErrorCode(clsMASIC.eMasicErrorCodes.InputFileAccessError);
                    return(false);
                }

                var scanStart = xcaliburAccessor.ScanStart;
                var scanEnd   = xcaliburAccessor.ScanEnd;

                InitOptions(scanList, keepRawSpectra, keepMSMSSpectra);

                UpdateProgress(string.Format("Reading Xcalibur data ({0:N0} scans){1}", scanCount, Environment.NewLine + Path.GetFileName(filePath)));
                ReportMessage(string.Format("Reading Xcalibur data; Total scan count: {0:N0}", scanCount));

                var scanCountToRead = scanEnd - scanStart + 1;
                var scansEst        = mOptions.SICOptions.ScanRangeCount;
                if (scansEst <= 0)
                {
                    scansEst = scanCountToRead;
                }
                scanList.ReserveListCapacity(scansEst);
                mScanTracking.ReserveListCapacity(scansEst);
                spectraCache.SpectrumCount = Math.Max(spectraCache.SpectrumCount, scansEst);
                for (var scanNumber = scanStart; scanNumber <= scanEnd; scanNumber++)
                {
                    if (!mScanTracking.CheckScanInRange(scanNumber, mOptions.SICOptions))
                    {
                        mScansOutOfRange += 1;
                        continue;
                    }

                    success = xcaliburAccessor.GetScanInfo(scanNumber, out ThermoRawFileReader.clsScanInfo thermoScanInfo);
                    if (!success)
                    {
                        // GetScanInfo returned false
                        ReportWarning("xcaliburAccessor.GetScanInfo returned false for scan " + scanNumber.ToString() + "; aborting read");
                        break;
                    }

                    var percentComplete = scanList.MasterScanOrderCount / (double)(scanCountToRead) * 100;
                    var extractSuccess  = ExtractScanInfoCheckRange(xcaliburAccessor, thermoScanInfo, scanList, spectraCache, dataOutputHandler, percentComplete);

                    if (!extractSuccess)
                    {
                        break;
                    }
                }

                Console.WriteLine();

                scanList.SetListCapacityToCount();
                mScanTracking.SetListCapacityToCount();

                // Shrink the memory usage of the scanList arrays
                success = FinalizeScanList(scanList, rawFileInfo);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                ReportError("Error in ExtractScanInfoFromXcaliburDataFile", ex, clsMASIC.eMasicErrorCodes.InputFileDataReadError);
            }

            // Close the handle to the data file
            xcaliburAccessor.CloseRawFile();

            return(success);
        }