示例#1
0
        private static int GetMSLevel(IXRawfile5 rawFile, int scanNo)
        {
            int msLevel = 1;

            rawFile.GetMSOrderForScanNum(scanNo, ref msLevel);
            return(msLevel);
        }
示例#2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="argFullPath"></param>
 /// <param name="argFileType">ThermoRawFile</param>
 public ThermoRawReader(string argFullPath)
 {
     _fullFilePath  = argFullPath;
     _rawConnection = (IXRawfile5) new MSFileReader_XRawfile();
     _rawConnection.Open(argFullPath);
     _rawConnection.SetCurrentController(0, 1); // first 0 is for mass spectrometer
 }
示例#3
0
        private static double GetRetentionTime(IXRawfile5 rawFile, int scanNo)
        {
            double rt = 0;

            rawFile.RTFromScanNum(scanNo, ref rt);
            return(rt);
        }
示例#4
0
        private static double GetIsolationWindowWidth(IXRawfile5 rawFile, int scanNo, int msLevel)
        {
            double width = 0;

            rawFile.GetIsolationWidthForScanNum(scanNo, msLevel - 1, ref width);
            return(width);
        }
示例#5
0
        private static double GetCollisionEnergy(IXRawfile5 rawFile, int scanNo, int msLevel)
        {
            double e = 0;

            rawFile.GetCollisionEnergyForScanNum(scanNo, msLevel - 1, ref e);
            return(e);
        }
示例#6
0
        private static double GetPrecursorMz(IXRawfile5 rawFile, int scanNo, int msLevel)
        {
            double mz = 0;

            rawFile.GetPrecursorMassForScanNum(scanNo, msLevel, ref mz);
            return(mz);
        }
        public RawFileConverter(string rawFile, string outFolder, string[] outFileTypes, ExperimentType expType, bool exportChargeState)
        {
            _rawReader = (IXRawfile5) new MSFileReader_XRawfile();
            Console.WriteLine("_rawReader created successfully.");
            int pnMajorVersion = -1, pnMinorVersion = -1, pnSubMinorVersion = -1, nBuilderNumber = -1;

            _rawReader.Version(ref pnMajorVersion, ref pnMinorVersion, ref pnSubMinorVersion, ref nBuilderNumber);
            Console.WriteLine("pnMajorVersion = " + pnMajorVersion);
            Console.WriteLine("pnMinorVersion = " + pnMinorVersion);
            Console.WriteLine("pnSubMinorVersion = " + pnSubMinorVersion);
            Console.WriteLine("nBuilderNumber = " + nBuilderNumber);

            _rawReader.Open(rawFile);
            Console.WriteLine("_rawReader open file successfully.");
            _rawReader.SetCurrentController(0, 1);

            rawFileName = rawFile;

            // get the numbers of the first and the last scans used in MSn file headers;
            _rawReader.GetFirstSpectrumNumber(ref FirstScanNum);
            _rawReader.GetLastSpectrumNumber(ref LastScanNum);
            outFolder = Path.GetDirectoryName(rawFile);
            InitWriters(Path.GetFileName(rawFile), outFolder, outFileTypes, expType, exportChargeState);

            this.expType = expType;
        }
示例#8
0
        private static int GetChargeState(IXRawfile5 rawFile, int scanNo)
        {
            object pChargeValue = null;

            rawFile.GetTrailerExtraValueForScanNum(scanNo, "Charge State:", ref pChargeValue);
            return(Convert.ToInt32(pChargeValue));
        }
示例#9
0
        private static bool IsCentroidSpectrum(IXRawfile5 rawFile, int scanNo)
        {
            int isCentroidScan = 0;

            rawFile.IsCentroidScanForScanNum(scanNo, ref isCentroidScan);
            return(isCentroidScan != 0);
        }
示例#10
0
        public ThermoRawFileReader(string rawFilePath)
        {
            this.rawFilePath = rawFilePath;

            if (string.IsNullOrWhiteSpace(rawFilePath))
            {
                throw new ArgumentNullException("rawFilePath");
            }
            if (!File.Exists(rawFilePath))
            {
                throw new FileNotFoundException("Raw file not exists.");
            }

            try
            {
                rawFile = new MSFileReader_XRawfile() as IXRawfile5;
                rawFile.Open(rawFilePath);
                rawFile.SetCurrentController(0, 1);

                startScanNo = GetFirstSpectrumNumber(rawFile);
                endScanNo   = GetLastSpectrumNumber(rawFile);

                model = MzLiteJson.HandleExternalModelFile(this, GetModelFilePath());
            }
            catch (Exception ex)
            {
                throw new MzLiteIOException(ex.Message, ex);
            }
        }
示例#11
0
        private static int GetFirstSpectrumNumber(IXRawfile5 rawFile)
        {
            int firstScanNumber = 0;

            rawFile.GetFirstSpectrumNumber(ref firstScanNumber);
            return(firstScanNumber);
        }
示例#12
0
        private static string GetFilterString(IXRawfile5 rawFile, int scanNo)
        {
            string filter = null;

            rawFile.GetFilterForScanNum(scanNo, ref filter);
            return(filter);
        }
示例#13
0
        private static int GetLastSpectrumNumber(IXRawfile5 rawFile)
        {
            int lastScanNumber = 0;

            rawFile.GetLastSpectrumNumber(ref lastScanNumber);
            return(lastScanNumber);
        }
示例#14
0
        /// <summary>
        /// Reads raw data. Puts spectra into a thread safe queue. Closes queue when done. Parses the rawfile-log into the statusLog object
        /// </summary>
        /// <param name="raw">Raw file</param>
        /// <param name="rawScanQueue">Thread safe queue</param>
        private void ReadRawFile(IXRawfile5 raw, BlockingCollection <RawScan> rawScanQueue)
        {
            int firstScanNumber = -1;

            raw.GetFirstSpectrumNumber(ref firstScanNumber);
            int lastScanNumber = -1;

            raw.GetLastSpectrumNumber(ref lastScanNumber);

            for (int scanNumber = firstScanNumber; scanNumber <= lastScanNumber; scanNumber++)
            {
                string scanFilter = null;
                raw.GetFilterForScanNum(scanNumber, ref scanFilter);

                //read scan header into key/value arrays
                object keyRef   = null;
                object valueRef = null;
                int    indexRef = 0;
                raw.GetTrailerExtraForScanNum(scanNumber, ref keyRef, ref valueRef, ref indexRef);
                string[] trailerKeys   = (string[])keyRef;
                string[] trailerValues = (string[])valueRef;

                //read tic and base peak mass using dummy variables
                double tic          = 0.0;
                int    int1         = 0;
                int    int2         = 0;
                int    int3         = 0;
                double double1      = 0.0;
                double double2      = 0.0;
                double double3      = 0.0;
                double basePeakMass = 0.0;
                double double5      = 0.0;
                double double6      = 0.0;
                raw.GetScanHeaderInfoForScanNum(scanNumber, ref int1, ref double1, ref double2, ref double3, ref tic, ref basePeakMass, ref double5, ref int2, ref int3, ref double6);

                //read data (mz,int,...) using dummy variables
                object labels_obj = null;
                object flags_obj  = null;
                raw.GetLabelData(ref labels_obj, ref flags_obj, ref scanNumber);
                double[,] data = (double[, ])labels_obj;

                //read precursorMz
                double precursorMz = 0.0;
                raw.GetPrecursorMassForScanNum(scanNumber, 2, ref precursorMz);

                RawScan rawScan = new RawScan(scanNumber, scanFilter, tic, trailerKeys, trailerValues, data, precursorMz, basePeakMass);
                rawScanQueue.Add(rawScan);

                //read status log
                double rtLog           = 0.0;
                object statuslogKeys   = null;
                object statuslogValues = null;
                int    statuslogN      = 0;
                raw.GetStatusLogForScanNum(scanNumber, ref rtLog, ref statuslogKeys, ref statuslogValues, ref statuslogN);
                statusLog.Add(rtLog, (string[])statuslogKeys, (string[])statuslogValues);
            }
            rawScanQueue.CompleteAdding();
            Log.Information("Read all spectra in base module");
        }
示例#15
0
 public void Init()
 {
     _rawConnect     = new MSFileReader_XRawfile() as IXRawfile5;
     _rawReader      = new RawReader();
     fullSpecGetter  = new FullMSSpectrumGetter();
     nSpecGetter     = new MSnSpectrumGetter();
     specParamGetter = new GeneralSpectrumGetter();
 }
示例#16
0
 public override void Dispose()
 {
     if (_rawConnection != null)
     {
         _rawConnection.Close();
         _rawConnection = null;
     }
     base.Dispose();
 }
示例#17
0
        public static ThermoDynamicData InitiateDynamicConnection(string filePath, IFilteringParams filterParams = null)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException();
            }

            if (!ThermoStaticData.CheckForMsFileReader())
            {
                throw new MzLibException("MsFileReader Not Installed");
            }

            IXRawfile5 _rawConnection = (IXRawfile5) new MSFileReader_XRawfile();

            _rawConnection.Open(filePath);
            _rawConnection.SetCurrentController(0, 1);

            int lastspectrumNumber = -1;

            _rawConnection.GetLastSpectrumNumber(ref lastspectrumNumber);
            int firstspectrumNumber = -1;

            _rawConnection.GetFirstSpectrumNumber(ref firstspectrumNumber);

            var precursorInfoArray = new ManagedThermoHelperLayer.PrecursorInfo[lastspectrumNumber - firstspectrumNumber + 1];

            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));

            var thermoGlobalParams = ThermoStaticData.GetAllGlobalStuff(_rawConnection, precursorInfoArray, filePath);

            // if the spectra file only contains 1 scan and its MS order is 0, this indicates an errored read result
            if (thermoGlobalParams.MsOrderByScan.Length == 1 && thermoGlobalParams.MsOrderByScan[0] == 0)
            {
                throw new MzLibException("Could not read data from file " + Path.GetFileName(filePath));
            }

            return(new ThermoDynamicData(_rawConnection, filterParams, lastspectrumNumber - firstspectrumNumber + 1, sourceFile, thermoGlobalParams));
        }
示例#18
0
        public ThermoRawReader(string argFullPath)
        {
            //Check if XRawfle2.dll exist
            CheckDLL();

            _fullFilePath = argFullPath;
            //Open
            _rawConnection = (IXRawfile5)new MSFileReader_XRawfile();
            _rawConnection.Open(_fullFilePath);
            _rawConnection.SetCurrentController(0, 1); // first 0 is for mass spectrometer
        }
示例#19
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (_rawConnection != null)
         {
             _rawConnection.Close();
             _rawConnection = null;
         }
     }
     base.Dispose(disposing);
 }
 public RawFileImpl()
 {
     try
     {
         this.rawFile = new MSFileReader_XRawfile() as IXRawfile5;
     }
     catch (Exception)
     {
         throw new Exception("Reading Thermo RAW files requires MSFileReader to be installed. It is available for download at: http://sjsupport.thermofinnigan.com/public/detail.asp?id=703");
     }
     this.valid    = false;
     this.FileName = string.Empty;
 }
示例#21
0
        public ThermoRawReader(string argFullPath)
        {
            //Check if XRawfle2.dll exist
            CheckDLL();



            _fullFilePath = argFullPath;
            //Open
            _rawConnection = (IXRawfile5) new MSFileReader_XRawfile();
            _rawConnection.Open(_fullFilePath);
            _rawConnection.SetCurrentController(0, 1); // first 0 is for mass spectrometer
        }
示例#22
0
        public override void Open()
        {
            if (_rawConnection != null)
            {
                return;
            }
            if (!File.Exists(FilePath) && !Directory.Exists(FilePath))
            {
                throw new IOException(string.Format("The MS data file {0} does not currently exist", FilePath));
            }

            _rawConnection = (IXRawfile5) new MSFileReader_XRawfile();
            _rawConnection.Open(FilePath);
            _rawConnection.SetCurrentController(0, 1); // first 0 is for mass spectrometer
        }
示例#23
0
        public RawFileProcessing(IXRawfile5 raw, string rawFileName, SettingsForAnalysis settingName)
        {
            //open rawfile (add error check)
            raw.Open(rawFileName);
            raw.SetCurrentController(0, 1);
            raw.GetCreationDate(ref _msStart);
            double endtime = 0.0;

            raw.GetEndTime(ref endtime);
            _msEnd        = _msStart.AddMinutes(endtime);
            _filecreation = File.GetCreationTime(rawFileName);
            raw.GetWarningMessage(ref WarningMessage);

            ProcessScans(raw, settingName);

            raw.Close();
            ReduceToMetaVariables(settingName);
        }
示例#24
0
        public static ThermoDynamicData InitiateDynamicConnection(string filePath, IFilteringParams filterParams = null)
        {
            if (CheckForMsFileReader() == false)
            {
                throw new MzLibException("MsFileReader Not Installed");
            }

            IXRawfile5 _rawConnection = (IXRawfile5) new MSFileReader_XRawfile();

            _rawConnection.Open(filePath);
            _rawConnection.SetCurrentController(0, 1);

            int lastspectrumNumber = -1;

            _rawConnection.GetLastSpectrumNumber(ref lastspectrumNumber);
            int firstspectrumNumber = -1;

            _rawConnection.GetFirstSpectrumNumber(ref firstspectrumNumber);

            var precursorInfoArray = new ManagedThermoHelperLayer.PrecursorInfo[lastspectrumNumber - firstspectrumNumber + 1];

            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));

            var thermoGlobalParams = GetAllGlobalStuff(_rawConnection, precursorInfoArray, filePath);

            return(new ThermoDynamicData(_rawConnection, filterParams, lastspectrumNumber - firstspectrumNumber + 1, precursorInfoArray, sourceFile, thermoGlobalParams));
        }
示例#25
0
        /// <summary>
        /// Read and parse all spectra, spawns a task.
        /// </summary>
        /// <param name="raw">Raw file</param>
        /// <param name="msScanParameters">Parameters</param>
        private void ProcessScans(IXRawfile5 raw, SettingsForAnalysis msScanParameters)
        {
            BlockingCollection <RawScan> rawScanQueue = new BlockingCollection <RawScan>();
            List <Task <Tuple <List <MsScan>, List <Ms2Scan> > > > parseTaskList = new List <Task <Tuple <List <MsScan>, List <Ms2Scan> > > >();

            for (int i = 0; i < 1; i++)//option here to increase threading if computations get more complicated - note possible problem with unsorted spectra?
            {
                parseTaskList.Add(Task.Run(() =>
                {
                    return(ParseSpectra(rawScanQueue, msScanParameters));
                }));
            }
            ReadRawFile(raw, rawScanQueue);
            Task.WaitAll(parseTaskList.ToArray());

            foreach (Task <Tuple <List <MsScan>, List <Ms2Scan> > > resultTask in parseTaskList)
            {
                this.msScanList.AddRange(resultTask.Result.Item1);
                this.ms2ScanList.AddRange(resultTask.Result.Item2);
            }
        }
示例#26
0
        public static ThermoGlobalParams GetAllGlobalStuff(IXRawfile5 _rawConnection, ManagedThermoHelperLayer.PrecursorInfo[] couldBePrecursor, string filePath)
        {
            int pnNumInstMethods = 0;

            _rawConnection.GetNumInstMethods(ref pnNumInstMethods);

            string[] instrumentMethods = new string[pnNumInstMethods];
            for (int nInstMethodItem = 0; nInstMethodItem < pnNumInstMethods; nInstMethodItem++)
            {
                string pbstrInstMethod = null;
                _rawConnection.GetInstMethod(nInstMethodItem, ref pbstrInstMethod);
                instrumentMethods[nInstMethodItem] = pbstrInstMethod;
            }

            string pbstrInstSoftwareVersion = null;

            _rawConnection.GetInstSoftwareVersion(ref pbstrInstSoftwareVersion);

            string pbstrInstName = null;

            _rawConnection.GetInstName(ref pbstrInstName);

            string pbstrInstModel = null;

            _rawConnection.GetInstModel(ref pbstrInstModel);

            int pnControllerNumber = 0;
            int pnControllerType   = 0;

            _rawConnection.GetCurrentController(ref pnControllerType, ref pnControllerNumber);

            int[] msOrderByScan = new int[couldBePrecursor.Length];
            for (int i = 0; i < couldBePrecursor.Length; i++)
            {
                _rawConnection.GetMSOrderForScanNum((i + 1), ref msOrderByScan[i]);
            }

            return(new ThermoGlobalParams(pnNumInstMethods, instrumentMethods, pbstrInstSoftwareVersion, pbstrInstName, pbstrInstModel, pnControllerType, pnControllerNumber, couldBePrecursor, filePath, msOrderByScan));
        }
示例#27
0
        public static ThermoStaticData LoadAllStaticData(string filePath, IFilteringParams filterParams = null)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException();
            }

            if (CheckForMsFileReader() == false)
            {
                throw new MzLibException("MsFileReader Not Installed");
            }

            var        ok            = new ManagedThermoHelperLayer.HelperClass();
            IXRawfile5 theConnection = (IXRawfile5) new MSFileReader_XRawfile();

            theConnection.Open(filePath);
            int pbSMData = 0;

            theConnection.IsThereMSData(ref pbSMData);
            if (pbSMData == 0)
            {
                throw new MzLibException("Could not read data from file " + Path.GetFileName(filePath));
            }

            theConnection.SetCurrentController(0, 1);

            var precursorInfosArray = ok.GetAllPrecursorInfos(filePath);

            for (int i = 0; i < precursorInfosArray.Length; i++)
            {
                if (precursorInfosArray[i].nScanNumber == 0)
                {
                    precursorInfosArray[i].nScanNumber = -1;
                }
            }

            int pnFirstSpectrum = 0;

            theConnection.GetFirstSpectrumNumber(ref pnFirstSpectrum);
            int pnLastSpectrum = 0;

            theConnection.GetLastSpectrumNumber(ref pnLastSpectrum);

            ThermoGlobalParams p = GetAllGlobalStuff(theConnection, precursorInfosArray, filePath);

            MsDataScan[] scans = new MsDataScan[pnLastSpectrum - pnFirstSpectrum + 1];
            for (int nScanNumber = pnFirstSpectrum; nScanNumber <= pnLastSpectrum; nScanNumber++)
            {
                scans[nScanNumber - pnFirstSpectrum] = GetMsDataOneBasedScanFromThermoFile(theConnection, nScanNumber, p, filterParams);
            }

            theConnection.Close();

            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 ThermoStaticData(scans, p, sourceFile));
        }
示例#28
0
        /// <summary>
        /// Opens the connection to the underlying data
        /// </summary>
        public override void Open()
        {
            if (IsOpen && _rawConnection != null)
                return;

            if (!File.Exists(FilePath) && !Directory.Exists(FilePath))
            {
                throw new IOException(string.Format("The MS data file {0} does not currently exist", FilePath));
            }

            _rawConnection = (IXRawfile5)new MSFileReader_XRawfile();
            _rawConnection.Open(FilePath);
            _rawConnection.SetCurrentController(0, 1); // first 0 is for mass spectrometer

            base.Open();
        }
示例#29
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="argFullPath"></param>
 /// <param name="argFileType">ThermoRawFile</param>
 public ThermoRawReader(string argFullPath)
 {
     _fullFilePath = argFullPath;
     _rawConnection = (IXRawfile5)new MSFileReader_XRawfile();
     _rawConnection.Open(argFullPath);
     _rawConnection.SetCurrentController(0, 1); // first 0 is for mass spectrometer
 }
        public void CloseRawFile()
        {
            try
            {
                if ((mXRawFile != null))
                {
                    mXRawFile.Close();
                }
                mCorruptMemoryEncountered = false;

            }
            catch (AccessViolationException)
            {
                // Ignore this error
            }
            catch (Exception)
            {
                // Ignore any errors
            }
            finally
            {
                mXRawFile = null;
                mCachedFileName = string.Empty;
                mFileInfo.Clear();
            }
        }
        /// <summary>
        /// Open the .raw file
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public bool OpenRawFile(string filePath)
        {
            var intResult = 0;
            var success = false;

            try
            {
                // Make sure any existing open files are closed
                CloseRawFile();

                mCachedScanInfo.Clear();

                if (mXRawFile == null)
                {
                    // ReSharper disable once SuspiciousTypeConversion.Global
                    mXRawFile = new MSFileReader_XRawfile() as IXRawfile5;
                }

                if (mXRawFile == null)
                {
                    throw new Exception("Could not instantiate an instance of MSFileReader_XRawfile");
                }

                mXRawFile.Open(filePath);
                mXRawFile.IsError(ref intResult);
                // Unfortunately, .IsError() always returns 0, even if an error occurred

                if (intResult == 0)
                {
                    mCachedFileName = filePath;
                    if (FillFileInfo())
                    {

                        if (mFileInfo.ScanStart == 0 && mFileInfo.ScanEnd == 0 && mFileInfo.VersionNumber == 0 && Math.Abs(mFileInfo.MassResolution - 0) < double.Epsilon && mFileInfo.InstModel == null)
                        {
                            // File actually didn't load correctly, since these shouldn't all be blank
                            success = false;
                            mFileInfo.CorruptFile = true;
                        }
                        else
                        {
                            success = true;
                        }
                    }
                    else
                    {
                        success = false;
                    }
                }
                else
                {
                    success = false;
                }

            }
            catch (Exception)
            {
                success = false;
            }
            finally
            {
                if (!success)
                {
                    mCachedFileName = string.Empty;
                }
            }

            return success;
        }
示例#32
0
 protected ThermoFile(IXRawfile5 _rawConnection, int numSpectra, ManagedThermoHelperLayer.PrecursorInfo[] couldBePrecursor, SourceFile sourceFile, ThermoGlobalParams thermoGlobalParams) : base(numSpectra, sourceFile)
 {
     this.ThermoGlobalParams = thermoGlobalParams;
 }
示例#33
0
 public ThermoRawSpectrumReader()
 {
     rawConnect = new MSFileReader_XRawfile() as IXRawfile5;
 }
示例#34
0
        public static MsDataScan GetMsDataOneBasedScanFromThermoFile(IXRawfile5 theConnection, int nScanNumber, ThermoGlobalParams globalParams, IFilteringParams filterParams = null)
        {
            int    pnNumPackets        = 0;
            double pdLowMass           = 0;
            double pdHighMass          = 0;
            double pdTIC               = 0;
            double pdBasePeakMass      = 0;
            double pdBasePeakIntensity = 0;
            int    pnNumChannels       = 0;
            int    pbUniformTime       = 0;
            double pdFrequency         = 0;
            double pdStartTime         = 0;

            theConnection.GetScanHeaderInfoForScanNum(nScanNumber, ref pnNumPackets, ref pdStartTime, ref pdLowMass, ref pdHighMass, ref pdTIC, ref pdBasePeakMass, ref pdBasePeakIntensity, ref pnNumChannels, ref pbUniformTime, ref pdFrequency);

            double?ms2isolationWidthFromTrailerExtra        = null;
            double?injectionTimeFromTrailerExtra            = null;
            double?precursorMonoisotopicMZfromTrailierExtra = null;
            int?   chargeStatefromTrailierExtra             = null;
            int?   masterScanfromTrailierExtra = null;

            object pvarValues  = null;
            object pvarLables  = null;
            int    pnArraySize = 0;

            theConnection.GetTrailerExtraForScanNum(nScanNumber, ref pvarLables, ref pvarValues, ref pnArraySize);

            string[] labels = (string[])pvarLables;
            string[] values = (string[])pvarValues;
            for (int i = labels.GetLowerBound(0); i <= labels.GetUpperBound(0); i++)
            {
                if (labels[i].StartsWith("MS2 Isolation Width", StringComparison.Ordinal))
                {
                    ms2isolationWidthFromTrailerExtra = double.Parse(values[i], CultureInfo.InvariantCulture) == 0 ?
                                                        (double?)null :
                                                        double.Parse(values[i], CultureInfo.InvariantCulture);
                }
                if (labels[i].StartsWith("Ion Injection Time (ms)", StringComparison.Ordinal))
                {
                    injectionTimeFromTrailerExtra = double.Parse(values[i], CultureInfo.InvariantCulture) == 0 ?
                                                    (double?)null :
                                                    double.Parse(values[i], CultureInfo.InvariantCulture);
                }
                if (labels[i].StartsWith("Monoisotopic M/Z", StringComparison.Ordinal))
                {
                    precursorMonoisotopicMZfromTrailierExtra = double.Parse(values[i], CultureInfo.InvariantCulture) == 0 ?
                                                               (double?)null :
                                                               double.Parse(values[i], CultureInfo.InvariantCulture);
                }
                if (labels[i].StartsWith("Charge State", StringComparison.Ordinal))
                {
                    chargeStatefromTrailierExtra = int.Parse(values[i], CultureInfo.InvariantCulture) == 0 ?
                                                   (int?)null :
                                                   int.Parse(values[i], CultureInfo.InvariantCulture);
                }
                if (labels[i].StartsWith("Master Scan Number", StringComparison.Ordinal))
                {
                    masterScanfromTrailierExtra = int.Parse(values[i], CultureInfo.InvariantCulture) == 0 ?
                                                  (int?)null :
                                                  int.Parse(values[i], CultureInfo.InvariantCulture);
                }
            }

            string pbstrFilter = null;

            theConnection.GetFilterForScanNum(nScanNumber, ref pbstrFilter);

            int pnMSOrder = 0;

            theConnection.GetMSOrderForScanNum(nScanNumber, ref pnMSOrder);

            int pnMassAnalyzerType = 0;

            theConnection.GetMassAnalyzerTypeForScanNum(nScanNumber, ref pnMassAnalyzerType);

            object pvarNoisePacket = null;

            try //if there is no noise data
            {
                theConnection.GetNoiseData(ref pvarNoisePacket, nScanNumber);
            }
            catch
            {
                //pvarNoisePAcket is already null
            }
            double[,] noiseData = pvarNoisePacket as double[, ];

            double[,] data;
            try
            {
                object pvarFlags  = null;
                object pvarLabels = null;
                theConnection.GetLabelData(ref pvarLabels, ref pvarFlags, ref nScanNumber);
                data = pvarLabels as double[, ];
                if (data == null || data.Length == 0)
                {
                    throw new MzLibException("For spectrum number " + nScanNumber + " the data is null!");
                }
            }
            catch (MzLibException)
            {
                // Warning: the masses reported by GetMassListFromScanNum when centroiding are not properly calibrated and thus could be off by 0.3 m/z or more

                double pdCentroidPeakWidth = 0;
                object pvarnMassList       = null;
                object pvarPeakFlags       = null;
                theConnection.GetMassListFromScanNum(ref nScanNumber, null, 0, 0, 0, 1, ref pdCentroidPeakWidth, ref pvarnMassList, ref pvarPeakFlags, ref pnArraySize);
                data = (double[, ])pvarnMassList;
            }

            MzSpectrum thermoSpectrum;

            if (filterParams != null && data.GetLength(1) > 0 && (filterParams.MinimumAllowedIntensityRatioToBasePeakM.HasValue || filterParams.NumberOfPeaksToKeepPerWindow.HasValue) && ((filterParams.ApplyTrimmingToMs1 && pnMSOrder == 1) || (filterParams.ApplyTrimmingToMsMs && pnMSOrder > 1)))
            {
                var count = data.GetLength(1);

                var mzArray        = new double[count];
                var intensityArray = new double[count];
                Buffer.BlockCopy(data, 0, mzArray, 0, sizeof(double) * count);
                Buffer.BlockCopy(data, sizeof(double) * count, intensityArray, 0, sizeof(double) * count);
                if (filterParams.NumberOfWindows == null)
                {
                    int numPeaks = TopNpeakHelper(ref intensityArray, ref mzArray, filterParams);
                    //the following arrays are modified after TopN helper
                    Array.Resize(ref intensityArray, numPeaks);
                    Array.Resize(ref mzArray, numPeaks);
                }
                //Array reference passed by value, array calues will be modified after calling
                else
                {
                    WindowModeHelper(ref intensityArray, ref mzArray, filterParams);
                }
                Array.Sort(mzArray, intensityArray);
                thermoSpectrum = new MzSpectrum(mzArray, intensityArray, false);
            }
            else
            {
                thermoSpectrum = new MzSpectrum(data);
            }
            MZAnalyzerType mzAnalyzerType;

            if ((ThermoMzAnalyzer)pnMassAnalyzerType == ThermoMzAnalyzer.FTMS)
            {
                mzAnalyzerType = MZAnalyzerType.Orbitrap;
            }
            else
            {
                mzAnalyzerType = MZAnalyzerType.Unknown;
            }
            string nativeId = "controllerType=0 controllerNumber=1 scan=" + nScanNumber;

            if (pnMSOrder > 1)
            {
                int pnActivationType = 0;
                theConnection.GetActivationTypeForScanNum(nScanNumber, pnMSOrder, ref pnActivationType);

                // INITIALIZE globalParams.couldBePrecursor[nScanNumber - 1] (for dynamic connections that don't have it initialized yet)
                if (globalParams.CouldBePrecursor[nScanNumber - 1].Equals(default(ManagedThermoHelperLayer.PrecursorInfo)))
                {
                    var ok = new ManagedThermoHelperLayer.HelperClass();
                    globalParams.CouldBePrecursor[nScanNumber - 1] = ok.GetSingleScanPrecursorInfo(nScanNumber, globalParams.FilePath);
                }

                var precursorInfo = globalParams.CouldBePrecursor[nScanNumber - 1];

                // THIS METHOD IS BUGGY!!! DO NOT USE
                //theConnection.FindPrecursorMassInFullScan(nScanNumber, ref pnMasterScan, ref pdFoundMass, ref pdHeaderMass, ref pnChargeState);

                int oneBasedPrecursorScanNumber = -1;
                if (precursorInfo.nScanNumber > 0)
                {
                    oneBasedPrecursorScanNumber = precursorInfo.nScanNumber;
                }
                else if (masterScanfromTrailierExtra.HasValue && masterScanfromTrailierExtra > 0)
                {
                    oneBasedPrecursorScanNumber = masterScanfromTrailierExtra.Value;
                }
                else
                {
                    // we weren't able to get the precursor scan number, so we'll have to guess;
                    // loop back to find precursor scan
                    // (assumed to be the first scan before this scan with an MS order of this scan's MS order - 1)
                    // e.g., if this is an MS2 scan, find the first MS1 scan before this and assume that's the precursor scan
                    int scanOrder          = globalParams.MsOrderByScan[nScanNumber - 1];
                    int precursorScanOrder = scanOrder - 1;

                    for (int i = nScanNumber - 1; i >= 0; i--)
                    {
                        int msOrder = globalParams.MsOrderByScan[i];

                        if (msOrder == precursorScanOrder)
                        {
                            oneBasedPrecursorScanNumber = i + 1;
                            break;
                        }
                    }
                }
                if (oneBasedPrecursorScanNumber == -1)
                {
                    throw new MzLibException("Could not find precursor info for scan #" + nScanNumber);
                }

                int?selectedIonGuessChargeStateGuess = null;
                if (precursorInfo.nChargeState > 0)
                {
                    selectedIonGuessChargeStateGuess = precursorInfo.nChargeState;
                }
                else if (chargeStatefromTrailierExtra.HasValue)
                {
                    selectedIonGuessChargeStateGuess = chargeStatefromTrailierExtra;
                }

                double?selectedIonGuessMonoisotopicMz = null;
                if (precursorMonoisotopicMZfromTrailierExtra.HasValue && precursorMonoisotopicMZfromTrailierExtra.Value > 0)
                {
                    selectedIonGuessMonoisotopicMz = precursorMonoisotopicMZfromTrailierExtra;
                }
                if (precursorInfo.dMonoIsoMass > 0 && !selectedIonGuessMonoisotopicMz.HasValue)
                {
                    selectedIonGuessMonoisotopicMz = precursorInfo.dMonoIsoMass;
                }

                Regex matcher;
                if (pbstrFilter.ToLower().Contains("msx"))
                {
                    matcher = mFindParentIonOnlyMsx;
                }
                else
                {
                    matcher = mFindParentIonOnlyNonMsx;
                }
                double selectedIonGuessMZ = double.Parse(matcher.Match(pbstrFilter).Groups["ParentMZ"].Value);

                //   int? selectedIonChargeStateGuess, double? selectedIonIntensity, double? isolationMZ, double? isolationWidth, DissociationType dissociationType, int? oneBasedPrecursorScanNumber, double? selectedIonMonoisotopicGuessMz, double? injectionTime, double[,] noiseData, string nativeId)
                // double TotalIonCurrent, double selectedIonMZ, int? selectedIonChargeStateGuess, double? selectedIonIntensity, double? isolationMZ, double? isolationWidth, DissociationType dissociationType, int? oneBasedPrecursorScanNumber, double? selectedIonMonoisotopicGuessMz, double? injectionTime, double[,] noiseData, string nativeId)
                return(new MsDataScan(
                           thermoSpectrum,
                           nScanNumber,
                           pnMSOrder,
                           true,
                           PolarityRegex.IsMatch(pbstrFilter) ? Polarity.Positive : Polarity.Negative,
                           pdStartTime,
                           new MzRange(pdLowMass, pdHighMass),
                           pbstrFilter,
                           mzAnalyzerType,
                           pdTIC,
                           injectionTimeFromTrailerExtra,
                           noiseData,
                           nativeId,
                           selectedIonGuessMZ,
                           selectedIonGuessChargeStateGuess,
                           null,
                           selectedIonGuessMZ,
                           ms2isolationWidthFromTrailerExtra,
                           (DissociationType)pnActivationType,
                           oneBasedPrecursorScanNumber,
                           selectedIonGuessMonoisotopicMz
                           ));
            }
            else
            {
                return(new MsDataScan(
                           thermoSpectrum,
                           nScanNumber,
                           1,
                           true,
                           PolarityRegex.IsMatch(pbstrFilter) ? Polarity.Positive : Polarity.Negative,
                           pdStartTime,
                           new MzRange(pdLowMass, pdHighMass),
                           pbstrFilter,
                           mzAnalyzerType,
                           pdTIC,
                           injectionTimeFromTrailerExtra,
                           noiseData,
                           nativeId));
            }
        }
示例#35
0
        private void SaveDataToCdf(MsSpectrum spec, string fpath)
        {
            DataSetFactory.SearchFolder("Microsoft.Research.Science.Data.NetCDF4.dll");
            DataSetFactory.SearchFolder("Microsoft.Research.Science.Data.dll");
            DataSetFactory.SearchFolder("Microsoft.Research.Science.Data.Imperative.dll");

            int    nControllerType = 0;                      // 0 == mass spec device
            int    nContorllerNumber = 1;                    // first MS device
            int    totalNumScans = 0;                        // Number of scans
            int    firstScanNumber = 0, lastScanNumber = -1; // Number of first and last scan
            string scanFilter             = null;            // Scan filter line
            int    numDataPoints          = -1;              // points in both the m/z and intensity arrays
            double retentionTimeInMinutes = -1;
            double minObservedMZ          = -1;
            double maxObservedMZ          = -1;
            double totalIonCurrent        = -1;
            double basePeakMZ             = -1;
            double basePeakIntensity      = -1;
            int    channel     = 0; // unused
            int    uniformTime = 0; // unused
            double frequency   = 0; // unused

            int      arraySize            = -1;
            object   rawData              = null; // rawData wil come as Double[,]
            object   peakFlags            = null;
            string   szFilter             = null; // No filter
            int      intensityCutoffType  = 1;    // No cutoff
            int      intensityCutoffValue = 0;    // No cutoff
            int      maxNumberOfPeaks     = 0;    // 0 : return all data peaks
            double   centroidPeakWidth    = 0;    // No centroiding
            int      centroidThisScan     = 0;    // No centroiding
            DateTime creationDate         = DateTime.MinValue;

            var scanPath = Path.Combine(fpath, $"{spec.Id}.cdf");

            var cdf = DataSet.Open(scanPath);

            cdf.IsAutocommitEnabled = false;
            AddMetadata(cdf);
            IXRawfile5 rawFile = (IXRawfile5) new MSFileReader_XRawfile();

            rawFile.Open(spec.RawFilePath);
            rawFile.SetCurrentController(nControllerType, nContorllerNumber);
            rawFile.GetNumSpectra(ref totalNumScans);

            rawFile.GetFirstSpectrumNumber(ref firstScanNumber);
            rawFile.GetLastSpectrumNumber(ref lastScanNumber);
            rawFile.GetCreationDate(ref creationDate);


            var massVar           = cdf.Add <double>("mass_values", new[] { "point_number" });
            var intensities       = cdf.Add <Int32>("intensity_values", new[] { "point_number" });
            var acqTimeVar        = cdf.Add <double>("scan_acquisition_time", new[] { "scan_number" });
            var scIndexVar        = cdf.Add <int>("scan_index", new[] { "scan_number" });
            var scCountVar        = cdf.Add <int>("point_count", new[] { "scan_number" });
            var totalIntensityVar = cdf.Add <double>("total_intensity", new[] { "scan_number" });

            massVar.Metadata["scale_factor"]     = 1.0;
            intensities.Metadata["scale_factor"] = 1.0;

            try
            {
                var retTimes   = new double[lastScanNumber];
                var ticArr     = new double[lastScanNumber];
                var scanIndex  = new Int32[lastScanNumber];
                var pointCount = new Int32[lastScanNumber];

                for (int curScanNum = firstScanNumber; curScanNum <= lastScanNumber; curScanNum++)
                {
                    rawFile.GetScanHeaderInfoForScanNum(curScanNum,
                                                        ref numDataPoints,
                                                        ref retentionTimeInMinutes,
                                                        ref minObservedMZ,
                                                        ref maxObservedMZ,
                                                        ref totalIonCurrent,
                                                        ref basePeakMZ,
                                                        ref basePeakIntensity,
                                                        ref channel,     // unused
                                                        ref uniformTime, // unused
                                                        ref frequency    // unused
                                                        );
                    scanFilter = null;
                    rawFile.GetFilterForScanNum(curScanNum, ref scanFilter);

                    peakFlags            = null;
                    centroidPeakWidth    = 0;
                    arraySize            = 0;
                    intensityCutoffType  = 1;
                    intensityCutoffValue = (int)(0.001 * basePeakIntensity);
                    rawFile.GetMassListFromScanNum(
                        ref curScanNum,
                        szFilter,              // filter
                        intensityCutoffType,   // intensityCutoffType
                        intensityCutoffValue,  // intensityCutoffValue
                        maxNumberOfPeaks,      // maxNumberOfPeaks
                        centroidThisScan,      // centroid result?
                        ref centroidPeakWidth, // centroidingPeakWidth
                        ref rawData,           // daw data
                        ref peakFlags,         // peakFlags
                        ref arraySize);        // array size

                    ;
                    var datArray = (Array)rawData;
                    var massArr  = new double[arraySize];
                    var intArr   = new int[arraySize];
                    for (var j = 0; j < arraySize; j++)
                    {
                        massArr[j] = (double)datArray.GetValue(0, j);
                        intArr[j]  = Convert.ToInt32(datArray.GetValue(1, j));
                    }
                    //Console.Write($"curScan = {curScanNum}\n");
                    var ind = curScanNum - 1;
                    pointCount[ind] = arraySize;
                    if (ind == 0)
                    {
                        scanIndex[ind]     = 0;
                        scanIndex[ind + 1] = arraySize;
                    }
                    else
                    {
                        scanIndex[ind] += scanIndex[ind - 1];
                        if (ind + 1 < lastScanNumber)
                        {
                            scanIndex[ind + 1] = arraySize;
                        }
                    }
                    retTimes[ind] = TimeSpan.FromMinutes(retentionTimeInMinutes).TotalSeconds;
                    ticArr[ind]   = totalIonCurrent;
                    massVar.Append(massArr);
                    intensities.Append(intArr);
                    rawData = null;
                }

                totalIntensityVar.PutData(ticArr);
                scIndexVar.PutData(scanIndex);
                acqTimeVar.PutData(retTimes);
                scCountVar.PutData(pointCount);

                cdf.Commit();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                cdf.Dispose();
                rawFile.Close();
            }
        }
示例#36
0
 private ThermoDynamicData(IXRawfile5 _rawConnection, IFilteringParams filterParams, int numSpectra, ManagedThermoHelperLayer.PrecursorInfo[] couldBePrecursor, SourceFile sourceFile, ThermoGlobalParams thermoGlobalParams) : base(_rawConnection, numSpectra, couldBePrecursor, sourceFile, thermoGlobalParams)
 {
     this._rawConnection = _rawConnection;
     this.filterParams   = filterParams;
 }