Exemplo n.º 1
0
        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;
        }
Exemplo n.º 2
0
        private static int GetFirstSpectrumNumber(IXRawfile5 rawFile)
        {
            int firstScanNumber = 0;

            rawFile.GetFirstSpectrumNumber(ref firstScanNumber);
            return(firstScanNumber);
        }
Exemplo n.º 3
0
        protected override int GetFirstSpectrumNumber()
        {
            int spectrumNumber = 0;

            _rawConnection.GetFirstSpectrumNumber(ref spectrumNumber);
            return(spectrumNumber);
        }
Exemplo n.º 4
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");
        }
Exemplo n.º 5
0
        public int GetFirstScan()
        {
            int startScanNum = 0;

            rawConnect.GetFirstSpectrumNumber(ref startScanNum);

            return(startScanNum);
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
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));
        }
Exemplo n.º 8
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();
            }
        }
Exemplo n.º 9
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));
        }