Beispiel #1
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);
        }