Exemplo n.º 1
0
        public ChromatogramDataProvider(MsDataFileImpl dataFile,
                                        ChromFileInfo fileInfo,
                                        IProgressStatus status,
                                        int startPercent,
                                        int endPercent,
                                        IProgressMonitor loader)
            : base(fileInfo, status, startPercent, endPercent, loader)
        {
            _dataFile = dataFile;
            _globalChromatogramExtractor = new GlobalChromatogramExtractor(dataFile);

            if (_dataFile.IsThermoFile)
            {
                _readMaxMinutes = 4;
            }

            int len = dataFile.ChromatogramCount;

            _chromIndices = new int[len];

            bool fixCEOptForShimadzu = dataFile.IsShimadzuFile;
            int  indexPrecursor      = -1;
            var  lastPrecursor       = SignedMz.ZERO;

            for (int i = 0; i < len; i++)
            {
                int    index;
                string id = dataFile.GetChromatogramId(i, out index);

                if (!ChromKey.IsKeyId(id))
                {
                    continue;
                }

                var chromKey = ChromKey.FromId(id, fixCEOptForShimadzu);
                if (chromKey.Precursor != lastPrecursor)
                {
                    lastPrecursor = chromKey.Precursor;
                    indexPrecursor++;
                }
                if (chromKey.Precursor.IsNegative)
                {
                    _sourceHasNegativePolarityData = true;
                }
                else
                {
                    _sourceHasPositivePolarityData = true;
                }
                var ki = new ChromKeyProviderIdPair(chromKey, index);
                _chromIndices[index] = indexPrecursor;
                _chromIds.Add(ki);
            }

            // Shimadzu can't do the necessary product m/z stepping for itself.
            // So, they provide the CE values in their IDs and we need to adjust
            // product m/z values for them to support CE optimization.
            if (fixCEOptForShimadzu)
            {
                FixCEOptForShimadzu();
            }

            if (_chromIds.Count == 0)
            {
                throw new NoSrmDataException(FileInfo.FilePath);
            }

            // CONSIDER: TIC and BPC are not well defined for SRM and produced chromatograms with over 100,000 points in
            // Agilent CE optimization data. So, keep them off for now.
//            foreach (int globalIndex in _globalChromatogramExtractor.GlobalChromatogramIndexes)
//            {
//                _chromIndices[globalIndex] = globalIndex;
//                _chromIds.Add(new ChromKeyProviderIdPair(ChromKey.FromId(_globalChromatogramExtractor.GetChromatogramId(globalIndex, out int indexId), false), globalIndex));
//            }

            foreach (var qcTracePair in _globalChromatogramExtractor.QcTraceByIndex)
            {
                _chromIndices[qcTracePair.Key] = qcTracePair.Key;
                _chromIds.Add(new ChromKeyProviderIdPair(ChromKey.FromQcTrace(qcTracePair.Value), qcTracePair.Key));
            }

            // CONSIDER(kaipot): Some way to support mzML files converted from MIDAS wiff files
            _hasMidasSpectra = (dataFile.IsABFile) && SpectraChromDataProvider.HasSpectrumData(dataFile);

            _ionMobilityUnits       = dataFile.IonMobilityUnits;
            _hasIonMobilityCombined = dataFile.HasCombinedIonMobilitySpectra;  // When true, data source provides IMS data in 3-array format, which affects spectrum ID format

            SetPercentComplete(50);
        }
Exemplo n.º 2
0
        public ChromatogramDataProvider(MsDataFileImpl dataFile,
                                        ChromFileInfo fileInfo,
                                        IProgressStatus status,
                                        int startPercent,
                                        int endPercent,
                                        IProgressMonitor loader)
            : base(fileInfo, status, startPercent, endPercent, loader)
        {
            _dataFile = dataFile;

            if (_dataFile.IsThermoFile)
            {
                _readMaxMinutes = 4;
            }

            int len = dataFile.ChromatogramCount;

            _chromIndices = new int[len];

            bool fixCEOptForShimadzu = dataFile.IsShimadzuFile;
            int  indexPrecursor      = -1;
            var  lastPrecursor       = SignedMz.ZERO;

            for (int i = 0; i < len; i++)
            {
                int    index;
                string id = dataFile.GetChromatogramId(i, out index);

                if (!ChromKey.IsKeyId(id))
                {
                    continue;
                }

                var chromKey = ChromKey.FromId(id, fixCEOptForShimadzu);
                if (chromKey.Precursor != lastPrecursor)
                {
                    lastPrecursor = chromKey.Precursor;
                    indexPrecursor++;
                }
                var ki = new ChromKeyProviderIdPair(chromKey, index);
                _chromIndices[index] = indexPrecursor;
                _chromIds.Add(ki);
            }

            // Shimadzu can't do the necessary product m/z stepping for itself.
            // So, they provide the CE values in their IDs and we need to adjust
            // product m/z values for them to support CE optimization.
            if (fixCEOptForShimadzu)
            {
                FixCEOptForShimadzu();
            }

            if (_chromIds.Count == 0)
            {
                throw new NoSrmDataException(FileInfo.FilePath);
            }

            // CONSIDER(kaipot): Some way to support mzML files converted from MIDAS wiff files
            _hasMidasSpectra = (dataFile.IsABFile) && SpectraChromDataProvider.HasSpectrumData(dataFile);

            SetPercentComplete(50);
        }
Exemplo n.º 3
0
        public static void AddSpectra(MidasLibSpec libSpec, MsDataFilePath[] resultsFiles, SrmDocument doc, ILoadMonitor monitor, out List <MsDataFilePath> failedFiles)
        {
            // Get spectra from results files
            var       newSpectra          = new List <DbSpectrum>();
            var       progress            = new ProgressStatus(string.Empty).ChangeMessage(Resources.MidasLibrary_AddSpectra_Reading_MIDAS_spectra);
            const int percentResultsFiles = 80;

            failedFiles = new List <MsDataFilePath>();
            for (var i = 0; i < resultsFiles.Length; i++)
            {
                var resultsFile = resultsFiles[i];
                try
                {
                    monitor.UpdateProgress(progress.ChangePercentComplete(i * percentResultsFiles / resultsFiles.Length));

                    var filePath = resultsFile.GetFilePath();
                    if (File.Exists(filePath))
                    {
                        var sampleIndex = resultsFile.GetSampleIndex();
                        using (var msd = new MsDataFileImpl(filePath, sampleIndex == -1 ? 0 : sampleIndex, resultsFile.GetLockMassParameters(), requireVendorCentroidedMS2: true))
                        {
                            if (ChromatogramDataProvider.HasChromatogramData(msd) && SpectraChromDataProvider.HasSpectrumData(msd))
                            {
                                var chromPrecursors = ReadChromPrecursorsFromMsd(msd, monitor).ToList();
                                newSpectra.AddRange(ReadDbSpectraFromMsd(msd, monitor));
                                MatchSpectraToChrom(newSpectra, chromPrecursors, monitor);
                            }
                        }

                        MatchSpectraToPeptides(newSpectra, doc, monitor);
                    }
                    else
                    {
                        failedFiles.Add(resultsFile);
                    }
                }
                catch (Exception x)
                {
                    monitor.UpdateProgress(progress.ChangeErrorException(x));
                    failedFiles.Add(resultsFile);
                }
                if (monitor.IsCanceled)
                {
                    monitor.UpdateProgress(progress.Cancel());
                    return;
                }
            }

            if (!newSpectra.Any())
            {
                monitor.UpdateProgress(progress.Complete());
                return;
            }

            progress = progress.ChangePercentComplete(percentResultsFiles);
            monitor.UpdateProgress(progress);

            // Add spectra to library
            var midasLib = !File.Exists(libSpec.FilePath) ? Create(libSpec) : Load(libSpec, monitor);

            if (midasLib == null)
            {
                monitor.UpdateProgress(progress.ChangeErrorException(new Exception(Resources.MidasLibrary_AddSpectra_Error_loading_MIDAS_library_for_adding_spectra_)));
                return;
            }

            progress = progress.ChangeMessage(Resources.MidasLibrary_AddSpectra_Adding_spectra_to_MIDAS_library);
            monitor.UpdateProgress(progress);

            var results = new Dictionary <string, DbResultsFile>();

            if (midasLib._spectra != null)
            {
                foreach (var kvp in midasLib._spectra)
                {
                    results[kvp.Key.FilePath] = kvp.Key;
                }
            }

            using (var sessionFactory = SessionFactoryFactory.CreateSessionFactory(libSpec.FilePath, typeof(MidasLibrary), false))
                using (var session = new SessionWithLock(sessionFactory.OpenSession(), new ReaderWriterLock(), true))
                    using (var transaction = session.BeginTransaction())
                    {
                        for (var i = 0; i < newSpectra.Count; i++)
                        {
                            if (monitor.IsCanceled)
                            {
                                transaction.Rollback();
                                monitor.UpdateProgress(progress.Cancel());
                                return;
                            }
                            var spectrum = newSpectra[i];

                            monitor.UpdateProgress(progress.ChangePercentComplete(percentResultsFiles + (int)(100.0 * i / newSpectra.Count)));

                            DbResultsFile resultsFile;
                            if (!results.TryGetValue(spectrum.ResultsFile.FilePath, out resultsFile))
                            {
                                resultsFile = new DbResultsFile(spectrum.ResultsFile)
                                {
                                    Id = null
                                };
                                results[spectrum.ResultsFile.FilePath] = resultsFile;
                                session.SaveOrUpdate(resultsFile);
                            }
                            else if (midasLib._spectra != null)
                            {
                                List <DbSpectrum> existingSpectra;
                                if (midasLib._spectra.TryGetValue(resultsFile, out existingSpectra) &&
                                    existingSpectra.Any(x => Equals(x.ResultsFile.FilePath, spectrum.ResultsFile.FilePath) &&
                                                        Equals(x.PrecursorMz, spectrum.PrecursorMz) &&
                                                        Equals(x.RetentionTime, spectrum.RetentionTime)))
                                {
                                    // This spectrum already exists in the library
                                    continue;
                                }
                            }
                            var spectrumNewDisconnected = new DbSpectrum(spectrum)
                            {
                                Id = null, ResultsFile = resultsFile
                            };
                            session.SaveOrUpdate(spectrumNewDisconnected);
                        }
                        transaction.Commit();
                        monitor.UpdateProgress(progress.Complete());
                    }
        }