Ejemplo n.º 1
0
        /// <summary>
        /// Reads modifications from a sequence with embedded modifications,
        /// e.g. AM[16.0]VLC[57.0]
        /// This results in some loss of precision, since embedded modifications
        /// are only accurate to one decimal place.  But this is all that is necessary
        /// for further use as spectral libraries for SRM method building.
        /// </summary>
        /// <param name="refSpectra"></param>
        private static void ModsFromModifiedSequence(DbRefSpectra refSpectra)
        {
            string modSeq = refSpectra.PeptideModSeq;

            for (int i = 0, iAa = 0; i < modSeq.Length; i++)
            {
                char c = modSeq[i];
                if (c != '[') // Not L10N
                {
                    iAa++;
                }
                else
                {
                    int    iEnd = modSeq.IndexOf(']', ++i); // Not L10N
                    double modMass;
                    if (double.TryParse(modSeq.Substring(i, iEnd - i), out modMass))
                    {
                        if (refSpectra.Modifications == null)
                        {
                            refSpectra.Modifications = new List <DbModification>();
                        }

                        refSpectra.Modifications.Add(new DbModification
                        {
                            RefSpectra = refSpectra,
                            Mass       = modMass,
                            Position   = iAa
                        });
                        i = iEnd;
                        iAa++;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private static DbRefSpectra MakeRefSpectrum(SpectrumInfo spectrum, string peptideSeq, string modifiedPeptideSeq, double precMz, int precChg)
        {
            var refSpectra = new DbRefSpectra
            {
                PeptideSeq      = peptideSeq,
                PrecursorMZ     = precMz,
                PrecursorCharge = precChg,
                PeptideModSeq   = modifiedPeptideSeq
            };

            MakeRefSpectrum(spectrum, refSpectra);

            return(refSpectra);
        }
Ejemplo n.º 3
0
        private static void SaveRedundantSpectra(ISession sessionRedundant,
                                                 IEnumerable <SpectrumKeyTime> redundantSpectraIds,
                                                 IDictionary <string, long> dictFiles,
                                                 DbRefSpectra refSpectra,
                                                 Library library)
        {
            foreach (var specLiteKey in redundantSpectraIds)
            {
                if (specLiteKey.Key.RedundantId == 0)
                {
                    continue;
                }
                // If this source file has already been saved, get its database Id.
                // Otherwise, save it.
                long spectrumSourceId = GetSpecturmSourceId(sessionRedundant, specLiteKey.FilePath, dictFiles);

                // Get peaks for the redundant spectrum
                var peaksInfo        = library.LoadSpectrum(specLiteKey.Key);
                var redundantSpectra = new DbRefSpectraRedundant
                {
                    Id               = specLiteKey.Key.RedundantId,
                    PeptideSeq       = refSpectra.PeptideSeq,
                    PrecursorMZ      = refSpectra.PrecursorMZ,
                    PrecursorCharge  = refSpectra.PrecursorCharge,
                    PeptideModSeq    = refSpectra.PeptideModSeq,
                    NumPeaks         = (ushort)peaksInfo.Peaks.Count(),
                    Copies           = refSpectra.Copies,
                    RetentionTime    = specLiteKey.Time.RetentionTime,
                    IonMobilityValue = specLiteKey.Time.IonMobilityValue.GetValueOrDefault(),
                    IonMobilityType  = specLiteKey.Time.IonMobilityType.GetValueOrDefault(),
                    IonMobilityHighEnergyDriftTimeOffsetMsec = specLiteKey.Time.IonMobilityHighEnergyDriftTimeOffsetMsec,
                    FileId = spectrumSourceId
                };

                var peaks = new DbRefSpectraRedundantPeaks
                {
                    RefSpectra    = redundantSpectra,
                    PeakIntensity = IntensitiesToBytes(peaksInfo.Peaks),
                    PeakMZ        = MZsToBytes(peaksInfo.Peaks)
                };
                redundantSpectra.Peaks = peaks;

                sessionRedundant.Save(redundantSpectra);
            }

            sessionRedundant.Flush();
            sessionRedundant.Clear();
        }
Ejemplo n.º 4
0
        private static void MakeRefSpectrum(SpectrumInfo spectrum, DbRefSpectra refSpectra)
        {
            short copies    = (short)spectrum.SpectrumHeaderInfo.GetRankValue(LibrarySpec.PEP_RANK_COPIES);
            var   peaksInfo = spectrum.SpectrumPeaksInfo;

            refSpectra.Copies   = copies;
            refSpectra.NumPeaks = (ushort)peaksInfo.Peaks.Length;

            refSpectra.Peaks = new DbRefSpectraPeaks
            {
                RefSpectra    = refSpectra,
                PeakIntensity = IntensitiesToBytes(peaksInfo.Peaks),
                PeakMZ        = MZsToBytes(peaksInfo.Peaks)
            };

            ModsFromModifiedSequence(refSpectra);
        }
Ejemplo n.º 5
0
        private DbRefSpectra RefSpectrumFromPeaks(SpectrumMzInfo spectrum)
        {
            var peaksInfo  = spectrum.SpectrumPeaks;
            var refSpectra = new DbRefSpectra
            {
                PeptideSeq      = FastaSequence.StripModifications(spectrum.Key.Sequence),
                PrecursorMZ     = spectrum.PrecursorMz,
                PrecursorCharge = spectrum.Key.Charge,
                PeptideModSeq   = spectrum.Key.Sequence,
                Copies          = 1,
                NumPeaks        = (ushort)peaksInfo.Peaks.Length
            };

            refSpectra.Peaks = new DbRefSpectraPeaks
            {
                RefSpectra    = refSpectra,
                PeakIntensity = IntensitiesToBytes(peaksInfo.Peaks),
                PeakMZ        = MZsToBytes(peaksInfo.Peaks)
            };

            ModsFromModifiedSequence(refSpectra);
            return(refSpectra);
        }
Ejemplo n.º 6
0
        public static ICollection <DbRefSpectraPeakAnnotations> Create(DbRefSpectra refSpectra, SpectrumPeaksInfo peaks)
        {
            List <DbRefSpectraPeakAnnotations> resultList = null;

            // Each peak may have more than one annotation
            if (peaks.Peaks.Any(p => p.Annotations != null && p.Annotations.Any(a => !SpectrumPeakAnnotation.IsNullOrEmpty(a))))
            {
                resultList = new List <DbRefSpectraPeakAnnotations>();
                var i = 0;
                foreach (var peak in peaks.Peaks)
                {
                    if (peak.Annotations != null)
                    {
                        foreach (var annotation in peak.Annotations.Where(a => !SpectrumPeakAnnotation.IsNullOrEmpty(a)))
                        {
                            var result = new DbRefSpectraPeakAnnotations
                            {
                                RefSpectra    = refSpectra,
                                PeakIndex     = i,
                                Name          = annotation.Ion.Name,
                                Formula       = annotation.Ion.NeutralFormula,
                                InchiKey      = annotation.Ion.AccessionNumbers.GetInChiKey(),
                                OtherKeys     = annotation.Ion.AccessionNumbers.GetNonInChiKeys(),
                                Adduct        = annotation.Ion.Adduct.ToString(),
                                Charge        = annotation.Ion.Adduct.AdductCharge,
                                Comment       = annotation.Comment,
                                mzTheoretical = annotation.Ion.MonoisotopicMassMz,
                                mzObserved    = peak.Mz
                            };
                            resultList.Add(result);
                        }
                    }
                    i++;
                }
            }
            return(resultList);
        }
Ejemplo n.º 7
0
        private void BuildRefSpectra(SrmDocument document,
                                     ISession session,
                                     DbRefSpectra refSpectra,
                                     SpectrumInfo[] spectra, // Yes, this could be IEnumerable, but then Resharper throws bogus warnings about possible multiple enumeration
                                     IDictionary <string, long> dictFiles,
                                     ICollection <SpectrumKeyTime> redundantSpectraKeys)
        {
            bool foundBestSpectrum = false;

            foreach (SpectrumInfo spectrum in spectra)
            {
                if (spectrum.IsBest)
                {
                    if (foundBestSpectrum)
                    {
                        throw new InvalidDataException(
                                  string.Format(Resources.BlibDb_BuildRefSpectra_Multiple_reference_spectra_found_for_peptide__0__in_the_library__1__,
                                                refSpectra.PeptideModSeq, FilePath));
                    }

                    foundBestSpectrum = true;

                    MakeRefSpectrum(spectrum, refSpectra);
                }


                // Determine if this spectrum is from a file that is in the document.
                // If it is not, do not save the retention time for this spectrum, and do not
                // add it to the redundant library. However, if this is the reference (best) spectrum
                // we must save its retention time.
                // NOTE: Spectra not used in the results get used for too much now for this to be useful
//                var matchingFile = document.Settings.HasResults
//                    ? document.Settings.MeasuredResults.FindMatchingMSDataFile(MsDataFileUri.Parse(spectrum.FilePath))
//                    : null;
//                if (!spectrum.IsBest && matchingFile == null)
//                    continue;

                // If this source file has already been saved, get its database Id.
                // Otherwise, save it.
                long spectrumSourceId = GetSpecturmSourceId(session, spectrum.FilePath, dictFiles);

                // spectrumKey in the SpectrumInfo is an integer for reference(best) spectra,
                // or object of type SpectrumLiteKey for redundant spectra
                object key         = spectrum.SpectrumKey;
                var    specLiteKey = key as SpectrumLiteKey;

                var dbRetentionTimes = new DbRetentionTimes
                {
                    RedundantRefSpectraId = specLiteKey != null ? specLiteKey.RedundantId : 0,
                    RetentionTime         = spectrum.RetentionTime,
                    SpectrumSourceId      = spectrumSourceId,
                    BestSpectrum          = spectrum.IsBest ? 1 : 0,
                    IonMobilityType       = 0,
                    IonMobilityValue      = 0,
                };
                if (null != spectrum.IonMobilityInfo)
                {
                    if (spectrum.IonMobilityInfo.IsCollisionalCrossSection)
                    {
                        dbRetentionTimes.IonMobilityType =
                            (int)BiblioSpecLiteLibrary.IonMobilityType.collisionalCrossSection;
                    }
                    else
                    {
                        dbRetentionTimes.IonMobilityType = (int)BiblioSpecLiteLibrary.IonMobilityType.driftTime;
                    }
                    dbRetentionTimes.IonMobilityValue = spectrum.IonMobilityInfo.Value;
                    dbRetentionTimes.IonMobilityHighEnergyDriftTimeOffsetMsec = spectrum.IonMobilityInfo.HighEnergyDriftTimeOffsetMsec;
                }

                if (refSpectra.RetentionTimes == null)
                {
                    refSpectra.RetentionTimes = new List <DbRetentionTimes>();
                }

                refSpectra.RetentionTimes.Add(dbRetentionTimes);

                if (specLiteKey != null)
                {
                    redundantSpectraKeys.Add(new SpectrumKeyTime(specLiteKey, dbRetentionTimes, spectrum.FilePath));
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Minimize any library type to a fully functional BiblioSpec SQLite library.
        /// </summary>
        /// <param name="librarySpec">Library spec for which the new library is created</param>
        /// <param name="library">Existing library to minimize</param>
        /// <param name="document">Document for which only used spectra are included in the new library</param>
        /// <returns>A new minimized <see cref="BiblioSpecLiteLibrary"/></returns>
        public BiblioSpecLiteLibrary MinimizeLibrary(BiblioSpecLiteSpec librarySpec,
                                                     Library library, SrmDocument document)
        {
            if (!UpdateProgressMessage(string.Format(Resources.BlibDb_MinimizeLibrary_Minimizing_library__0__, library.Name)))
            {
                return(null);
            }

            string libAuthority = "unknown.org"; // Not L10N
            string libId        = library.Name;
            // CONSIDER: Use version numbers of the original library?
            int libraryRevision = DbLibInfo.INITIAL_LIBRARY_REVISION;
            int schemaVersion   = 0;

            bool saveRetentionTimes = false;
            bool saveRedundantLib   = false;

            var blibLib = library as BiblioSpecLiteLibrary;

            if (blibLib != null)
            {
                string libraryLsid = blibLib.Lsid;
                Match  matchLsid   = REGEX_LSID.Match(libraryLsid);
                if (matchLsid.Success)
                {
                    libAuthority = matchLsid.Groups[1].Value;
                    libId        = matchLsid.Groups[2].Value;
                }
                else
                {
                    libAuthority = BiblioSpecLiteLibrary.DEFAULT_AUTHORITY;
                }

                // We will have a RetentionTimes table if schemaVersion if 1 or greater.
                saveRetentionTimes = blibLib.SchemaVersion >= 1;
                libraryRevision    = blibLib.Revision;
                schemaVersion      = Math.Min(blibLib.SchemaVersion, DbLibInfo.SCHEMA_VERSION_CURRENT);

                // If the document has MS1 filtering enabled we will save a minimized version
                // of the redundant library, if available.
                if (document.Settings.TransitionSettings.FullScan.IsEnabledMs)
                {
                    String redundantLibPath = blibLib.FilePathRedundant;
                    if (File.Exists(redundantLibPath))
                    {
                        string path = BiblioSpecLiteSpec.GetRedundantName(FilePath);
                        CreateSessionFactory_Redundant(path);
                        saveRedundantLib = true;
                    }
                }
            }
            else if (library is BiblioSpecLibrary)
            {
                libAuthority = BiblioSpecLiteLibrary.DEFAULT_AUTHORITY;
            }
            else if (library is XHunterLibrary)
            {
                libAuthority = XHunterLibrary.DEFAULT_AUTHORITY;
            }
            else
            {
                var nistLibrary = library as NistLibrary;
                if (nistLibrary != null)
                {
                    libAuthority = NistLibrary.DEFAULT_AUTHORITY;
                    libId        = nistLibrary.Id ?? libId;
                }
            }
            // Use a very specific LSID, since it really only matches this document.
            string libLsid = string.Format("urn:lsid:{0}:spectral_libary:bibliospec:nr:minimal:{1}:{2}:{3}.{4}", // Not L10N
                                           libAuthority, libId, Guid.NewGuid(), libraryRevision, schemaVersion);

            var dictLibrary = new Dictionary <LibKey, BiblioLiteSpectrumInfo>();

            // Hash table to store the database IDs of any source files in the library
            // Source file information is available only in Bibliospec libraries, schema version >= 1
            var dictFiles          = new Dictionary <string, long>();
            var dictFilesRedundant = new Dictionary <string, long>();

            ISession     redundantSession      = null;
            ITransaction redundantTransaction  = null;
            int          redundantSpectraCount = 0;

            try
            {
                using (ISession session = OpenWriteSession())
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        var settings = document.Settings;

                        int peptideCount = document.PeptideCount;
                        int savedCount   = 0;

                        foreach (var nodePep in document.Peptides)
                        {
                            var mods = nodePep.ExplicitMods;

                            foreach (TransitionGroupDocNode nodeGroup in nodePep.Children)
                            {
                                // Only get library info from precursors that use the desired library
                                if (!nodeGroup.HasLibInfo || !Equals(nodeGroup.LibInfo.LibraryName, library.Name))
                                {
                                    continue;
                                }

                                TransitionGroup  group           = nodeGroup.TransitionGroup;
                                string           peptideSeq      = group.Peptide.Sequence;
                                int              precursorCharge = group.PrecursorCharge;
                                IsotopeLabelType labelType       = nodeGroup.TransitionGroup.LabelType;


                                var calcPre       = settings.GetPrecursorCalc(labelType, mods);
                                var peptideModSeq = calcPre.GetModifiedSequence(peptideSeq, false);
                                var libKey        = new LibKey(peptideModSeq, precursorCharge);

                                if (dictLibrary.ContainsKey(libKey))
                                {
                                    continue;
                                }


                                // saveRetentionTimes will be false unless this is a BiblioSpec(schemaVersion >=1) library.
                                if (!saveRetentionTimes)
                                {
                                    // get the best spectra
                                    foreach (var spectrumInfo in library.GetSpectra(libKey, labelType, LibraryRedundancy.best))
                                    {
                                        DbRefSpectra refSpectra = MakeRefSpectrum(spectrumInfo,
                                                                                  peptideSeq,
                                                                                  peptideModSeq,
                                                                                  nodeGroup.PrecursorMz,
                                                                                  precursorCharge);

                                        session.Save(refSpectra);

                                        dictLibrary.Add(libKey,
                                                        new BiblioLiteSpectrumInfo(libKey, refSpectra.Copies,
                                                                                   refSpectra.NumPeaks,
                                                                                   (int)(refSpectra.Id ?? 0),
                                                                                   default(IndexedRetentionTimes),
                                                                                   default(IndexedIonMobilities)));
                                    }

                                    session.Flush();
                                    session.Clear();
                                }
                                // This is a BiblioSpec(schemaVersion >=1) library.
                                else
                                {
                                    // get all the spectra, including the redundant ones if this library has any
                                    var spectra = library.GetSpectra(libKey, labelType, LibraryRedundancy.all_redundant).ToArray();
                                    // Avoid saving to the RefSpectra table for isotope label types that have no spectra
                                    if (spectra.Length == 0)
                                    {
                                        continue;
                                    }

                                    DbRefSpectra refSpectra = new DbRefSpectra
                                    {
                                        PeptideSeq      = peptideSeq,
                                        PrecursorMZ     = nodeGroup.PrecursorMz,
                                        PrecursorCharge = precursorCharge,
                                        PeptideModSeq   = peptideModSeq
                                    };

                                    // Get all the information for this reference spectrum.
                                    // For BiblioSpec (schema ver >= 1), this can include retention time information
                                    // for this spectrum as well as any redundant spectra for the peptide.
                                    // Ids of spectra in the redundant library, where available, are also returned.
                                    var redundantSpectraKeys = new List <SpectrumKeyTime>();
                                    BuildRefSpectra(document, session, refSpectra, spectra, dictFiles, redundantSpectraKeys);

                                    session.Save(refSpectra);
                                    session.Flush();
                                    session.Clear();

                                    // TODO(nicksh): preserve retention time information.
                                    var retentionTimesByFileId = default(IndexedRetentionTimes);
                                    var driftTimesByFileId     = default(IndexedIonMobilities);
                                    dictLibrary.Add(libKey,
                                                    new BiblioLiteSpectrumInfo(libKey,
                                                                               refSpectra.Copies,
                                                                               refSpectra.NumPeaks,
                                                                               (int)(refSpectra.Id ?? 0),
                                                                               retentionTimesByFileId,
                                                                               driftTimesByFileId));

                                    // Save entries in the redundant library.
                                    if (saveRedundantLib && redundantSpectraKeys.Count > 0)
                                    {
                                        if (redundantSession == null)
                                        {
                                            redundantSession     = OpenWriteSession_Redundant();
                                            redundantTransaction = redundantSession.BeginTransaction();
                                        }
                                        SaveRedundantSpectra(redundantSession, redundantSpectraKeys, dictFilesRedundant, refSpectra, library);
                                        redundantSpectraCount += redundantSpectraKeys.Count;
                                    }
                                }
                            }

                            savedCount++;
                            if (!UpdateProgress(peptideCount, savedCount))
                            {
                                return(null);
                            }
                        }

                        // Simulate ctime(d), which is what BlibBuild uses.
                        string    createTime = string.Format("{0:ddd MMM dd HH:mm:ss yyyy}", DateTime.Now); // Not L10N? different date/time format in different countries
                        DbLibInfo libInfo    = new DbLibInfo
                        {
                            LibLSID      = libLsid,
                            CreateTime   = createTime,
                            NumSpecs     = dictLibrary.Count,
                            MajorVersion = libraryRevision,
                            MinorVersion = schemaVersion
                        };

                        session.Save(libInfo);
                        session.Flush();
                        session.Clear();

                        transaction.Commit();

                        if (redundantTransaction != null)
                        {
                            var scoreType = new DbScoreTypes {
                                Id = 0, ScoreType = "UNKNOWN"
                            };                                                            // Not L10N
                            redundantSession.Save(scoreType);

                            libInfo = new DbLibInfo
                            {
                                LibLSID      = libLsid.Replace(":nr:", ":redundant:"),      // Not L10N
                                CreateTime   = createTime,
                                NumSpecs     = redundantSpectraCount,
                                MajorVersion = libraryRevision,
                                MinorVersion = schemaVersion
                            };
                            redundantSession.Save(libInfo);
                            redundantSession.Flush();
                            redundantSession.Clear();

                            redundantTransaction.Commit();
                        }
                    }
            }
            finally
            {
                if (redundantTransaction != null)
                {
                    redundantTransaction.Dispose();
                }
                if (redundantSession != null)
                {
                    redundantSession.Dispose();
                }
            }

            var libraryEntries = dictLibrary.Values.ToArray();

            return(new BiblioSpecLiteLibrary(librarySpec, libLsid, libraryRevision, schemaVersion,
                                             libraryEntries, FileStreamManager.Default));
        }
Ejemplo n.º 9
0
        private void BuildRefSpectra(SrmDocument document,
            ISession session,
            DbRefSpectra refSpectra,
            SpectrumInfo[] spectra, // Yes, this could be IEnumerable, but then Resharper throws bogus warnings about possible multiple enumeration
            IDictionary<string, long> dictFiles,
            ICollection<SpectrumKeyTime> redundantSpectraKeys)
        {
            bool foundBestSpectrum = false;

            foreach(SpectrumInfo spectrum in spectra)
            {
                if(spectrum.IsBest)
                {
                    if(foundBestSpectrum)
                    {
                        throw new InvalidDataException(
                            string.Format(Resources.BlibDb_BuildRefSpectra_Multiple_reference_spectra_found_for_peptide__0__in_the_library__1__,
                                          refSpectra.PeptideModSeq, FilePath));
                    }

                    foundBestSpectrum = true;

                    MakeRefSpectrum(spectrum, refSpectra);
                }

                // Determine if this spectrum is from a file that is in the document.
                // If it is not, do not save the retention time for this spectrum, and do not
                // add it to the redundant library. However, if this is the reference (best) spectrum
                // we must save its retention time.
                // NOTE: Spectra not used in the results get used for too much now for this to be useful
            //                var matchingFile = document.Settings.HasResults
            //                    ? document.Settings.MeasuredResults.FindMatchingMSDataFile(MsDataFileUri.Parse(spectrum.FilePath))
            //                    : null;
            //                if (!spectrum.IsBest && matchingFile == null)
            //                    continue;

                // If this source file has already been saved, get its database Id.
                // Otherwise, save it.
                long spectrumSourceId = GetSpecturmSourceId(session, spectrum.FilePath, dictFiles);

                // spectrumKey in the SpectrumInfo is an integer for reference(best) spectra,
                // or object of type SpectrumLiteKey for redundant spectra
                object key = spectrum.SpectrumKey;
                var specLiteKey = key as SpectrumLiteKey;

                var dbRetentionTimes = new DbRetentionTimes
                {
                    RedundantRefSpectraId = specLiteKey != null ? specLiteKey.RedundantId : 0,
                    RetentionTime = spectrum.RetentionTime,
                    SpectrumSourceId = spectrumSourceId,
                    BestSpectrum = spectrum.IsBest ? 1 : 0,
                    IonMobilityType = 0,
                    IonMobilityValue = 0,
                };
                if (null != spectrum.IonMobilityInfo)
                {
                    if (spectrum.IonMobilityInfo.IsCollisionalCrossSection)
                    {
                        dbRetentionTimes.IonMobilityType =
                            (int) BiblioSpecLiteLibrary.IonMobilityType.collisionalCrossSection;
                    }
                    else
                    {
                        dbRetentionTimes.IonMobilityType = (int) BiblioSpecLiteLibrary.IonMobilityType.driftTime;
                    }
                    dbRetentionTimes.IonMobilityValue = spectrum.IonMobilityInfo.Value;
                    dbRetentionTimes.IonMobilityHighEnergyDriftTimeOffsetMsec = spectrum.IonMobilityInfo.HighEnergyDriftTimeOffsetMsec;
                }

                if (refSpectra.RetentionTimes == null)
                    refSpectra.RetentionTimes = new List<DbRetentionTimes>();

                refSpectra.RetentionTimes.Add(dbRetentionTimes);

                if (specLiteKey != null)
                {
                    redundantSpectraKeys.Add(new SpectrumKeyTime(specLiteKey, dbRetentionTimes, spectrum.FilePath));
                }
            }
        }
Ejemplo n.º 10
0
        private DbRefSpectra RefSpectrumFromPeaks(SpectrumMzInfo spectrum)
        {
            var peaksInfo = spectrum.SpectrumPeaks;
            var refSpectra = new DbRefSpectra
            {
                PeptideSeq = FastaSequence.StripModifications(spectrum.Key.Sequence),
                PrecursorMZ = spectrum.PrecursorMz,
                PrecursorCharge = spectrum.Key.Charge,
                PeptideModSeq = spectrum.Key.Sequence,
                Copies = 1,
                NumPeaks = (ushort)peaksInfo.Peaks.Length
            };

            refSpectra.Peaks = new DbRefSpectraPeaks
            {
                RefSpectra = refSpectra,
                PeakIntensity = IntensitiesToBytes(peaksInfo.Peaks),
                PeakMZ = MZsToBytes(peaksInfo.Peaks)
            };

            ModsFromModifiedSequence(refSpectra);
            return refSpectra;
        }
Ejemplo n.º 11
0
        private static void SaveRedundantSpectra(ISession sessionRedundant,
            IEnumerable<SpectrumKeyTime> redundantSpectraIds,
            IDictionary<string, long> dictFiles,
            DbRefSpectra refSpectra,
            Library library)
        {
            foreach (var specLiteKey in redundantSpectraIds)
            {
                if (specLiteKey.Key.RedundantId == 0)
                {
                    continue;
                }
                // If this source file has already been saved, get its database Id.
                // Otherwise, save it.
                long spectrumSourceId = GetSpecturmSourceId(sessionRedundant, specLiteKey.FilePath, dictFiles);

                // Get peaks for the redundant spectrum
                var peaksInfo = library.LoadSpectrum(specLiteKey.Key);
                var redundantSpectra = new DbRefSpectraRedundant
                                           {
                                               Id = specLiteKey.Key.RedundantId,
                                               PeptideSeq = refSpectra.PeptideSeq,
                                               PrecursorMZ = refSpectra.PrecursorMZ,
                                               PrecursorCharge = refSpectra.PrecursorCharge,
                                               PeptideModSeq = refSpectra.PeptideModSeq,
                                               NumPeaks = (ushort) peaksInfo.Peaks.Count(),
                                               Copies = refSpectra.Copies,
                                               RetentionTime = specLiteKey.Time.RetentionTime,
                                               IonMobilityValue = specLiteKey.Time.IonMobilityValue.GetValueOrDefault(),
                                               IonMobilityType = specLiteKey.Time.IonMobilityType.GetValueOrDefault(),
                                               IonMobilityHighEnergyDriftTimeOffsetMsec = specLiteKey.Time.IonMobilityHighEnergyDriftTimeOffsetMsec,
                                               FileId = spectrumSourceId
                                           };

                var peaks = new DbRefSpectraRedundantPeaks
                                {
                                    RefSpectra = redundantSpectra,
                                    PeakIntensity = IntensitiesToBytes(peaksInfo.Peaks),
                                    PeakMZ = MZsToBytes(peaksInfo.Peaks)
                                };
                redundantSpectra.Peaks = peaks;

                sessionRedundant.Save(redundantSpectra);
            }

            sessionRedundant.Flush();
            sessionRedundant.Clear();
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Reads modifications from a sequence with embedded modifications,
        /// e.g. AM[16.0]VLC[57.0]
        /// This results in some loss of precision, since embedded modifications
        /// are only accurate to one decimal place.  But this is all that is necessary
        /// for further use as spectral libraries for SRM method building.
        /// </summary>
        /// <param name="refSpectra"></param>
        private static void ModsFromModifiedSequence(DbRefSpectra refSpectra)
        {
            string modSeq = refSpectra.PeptideModSeq;
            for (int i = 0, iAa = 0; i < modSeq.Length; i++)
            {
                char c = modSeq[i];
                if (c != '[') // Not L10N
                    iAa++;
                else
                {
                    int iEnd = modSeq.IndexOf(']', ++i); // Not L10N
                    double modMass;
                    if (double.TryParse(modSeq.Substring(i, iEnd - i), out modMass))
                    {
                        if (refSpectra.Modifications == null)
                            refSpectra.Modifications = new List<DbModification>();

                        refSpectra.Modifications.Add(new DbModification
                                                         {
                                                             RefSpectra = refSpectra,
                                                             Mass = modMass,
                                                             Position = iAa
                                                         });
                        i = iEnd;
                        iAa++;
                    }
                }
            }
        }
Ejemplo n.º 13
0
        private static void MakeRefSpectrum(SpectrumInfo spectrum, DbRefSpectra refSpectra)
        {
            short copies = (short)spectrum.SpectrumHeaderInfo.GetRankValue(LibrarySpec.PEP_RANK_COPIES);
            var peaksInfo = spectrum.SpectrumPeaksInfo;

            refSpectra.Copies = copies;
            refSpectra.NumPeaks = (ushort) peaksInfo.Peaks.Length;

            refSpectra.Peaks = new DbRefSpectraPeaks
                                   {
                                       RefSpectra = refSpectra,
                                       PeakIntensity = IntensitiesToBytes(peaksInfo.Peaks),
                                       PeakMZ = MZsToBytes(peaksInfo.Peaks)
                                   };

            ModsFromModifiedSequence(refSpectra);
        }
Ejemplo n.º 14
0
        private static DbRefSpectra MakeRefSpectrum(SpectrumInfo spectrum, string peptideSeq, string modifiedPeptideSeq, double precMz, int precChg)
        {
            var refSpectra = new DbRefSpectra
                                {
                                    PeptideSeq = peptideSeq,
                                    PrecursorMZ = precMz,
                                    PrecursorCharge = precChg,
                                    PeptideModSeq = modifiedPeptideSeq
                                };

            MakeRefSpectrum(spectrum, refSpectra);

            return refSpectra;
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Minimize any library type to a fully functional BiblioSpec SQLite library.
        /// </summary>
        /// <param name="librarySpec">Library spec for which the new library is created</param>
        /// <param name="library">Existing library to minimize</param>
        /// <param name="document">Document for which only used spectra are included in the new library</param>
        /// <returns>A new minimized <see cref="BiblioSpecLiteLibrary"/></returns>
        public BiblioSpecLiteLibrary MinimizeLibrary(BiblioSpecLiteSpec librarySpec,
            Library library, SrmDocument document)
        {
            if (!UpdateProgressMessage(string.Format(Resources.BlibDb_MinimizeLibrary_Minimizing_library__0__, library.Name)))
                return null;

            string libAuthority = "unknown.org"; // Not L10N
            string libId = library.Name;
            // CONSIDER: Use version numbers of the original library?
            int libraryRevision = DbLibInfo.INITIAL_LIBRARY_REVISION;
            int schemaVersion = 0;

            bool saveRetentionTimes = false;
            bool saveRedundantLib = false;

            var blibLib = library as BiblioSpecLiteLibrary;
            if (blibLib != null)
            {
                string libraryLsid = blibLib.Lsid;
                Match matchLsid = REGEX_LSID.Match(libraryLsid);
                if (matchLsid.Success)
                {
                    libAuthority = matchLsid.Groups[1].Value;
                    libId = matchLsid.Groups[2].Value;
                }
                else
                {
                    libAuthority = BiblioSpecLiteLibrary.DEFAULT_AUTHORITY;
                }

                // We will have a RetentionTimes table if schemaVersion if 1 or greater.
                saveRetentionTimes = blibLib.SchemaVersion >= 1;
                libraryRevision = blibLib.Revision;
                schemaVersion = Math.Min(blibLib.SchemaVersion, DbLibInfo.SCHEMA_VERSION_CURRENT);

                // If the document has MS1 filtering enabled we will save a minimized version
                // of the redundant library, if available.
                if(document.Settings.TransitionSettings.FullScan.IsEnabledMs)
                {
                    String redundantLibPath = blibLib.FilePathRedundant;
                    if(File.Exists(redundantLibPath))
                    {
                        string path = BiblioSpecLiteSpec.GetRedundantName(FilePath);
                        CreateSessionFactory_Redundant(path);
                        saveRedundantLib = true;
                    }
                }
            }
            else if (library is BiblioSpecLibrary)
                libAuthority = BiblioSpecLiteLibrary.DEFAULT_AUTHORITY;
            else if (library is XHunterLibrary)
                libAuthority = XHunterLibrary.DEFAULT_AUTHORITY;
            else
            {
                var nistLibrary = library as NistLibrary;
                if (nistLibrary != null)
                {
                    libAuthority = NistLibrary.DEFAULT_AUTHORITY;
                    libId = nistLibrary.Id ?? libId;
                }
            }
            // Use a very specific LSID, since it really only matches this document.
            string libLsid = string.Format("urn:lsid:{0}:spectral_libary:bibliospec:nr:minimal:{1}:{2}:{3}.{4}", // Not L10N
                libAuthority, libId, Guid.NewGuid(), libraryRevision, schemaVersion);

            var dictLibrary = new Dictionary<LibKey, BiblioLiteSpectrumInfo>();

            // Hash table to store the database IDs of any source files in the library
            // Source file information is available only in Bibliospec libraries, schema version >= 1
            var dictFiles = new Dictionary<string, long>();
            var dictFilesRedundant = new Dictionary<string, long>();

            ISession redundantSession = null;
            ITransaction redundantTransaction = null;
            int redundantSpectraCount = 0;

            try
            {
                using (ISession session = OpenWriteSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    var settings = document.Settings;

                    int peptideCount = document.PeptideCount;
                    int savedCount = 0;

                    foreach (var nodePep in document.Peptides)
                    {
                        var mods = nodePep.ExplicitMods;

                        foreach (TransitionGroupDocNode nodeGroup in nodePep.Children)
                        {
                            // Only get library info from precursors that use the desired library
                            if (!nodeGroup.HasLibInfo || !Equals(nodeGroup.LibInfo.LibraryName, library.Name))
                                continue;

                            TransitionGroup group = nodeGroup.TransitionGroup;
                            string peptideSeq = group.Peptide.Sequence;
                            int precursorCharge = group.PrecursorCharge;
                            IsotopeLabelType labelType = nodeGroup.TransitionGroup.LabelType;

                            var calcPre = settings.GetPrecursorCalc(labelType, mods);
                            var peptideModSeq = calcPre.GetModifiedSequence(peptideSeq, false);
                            var libKey = new LibKey(peptideModSeq, precursorCharge);

                            if (dictLibrary.ContainsKey(libKey))
                                continue;

                            // saveRetentionTimes will be false unless this is a BiblioSpec(schemaVersion >=1) library.
                            if (!saveRetentionTimes)
                            {
                                // get the best spectra
                                foreach (var spectrumInfo in library.GetSpectra(libKey, labelType, LibraryRedundancy.best))
                                {
                                    DbRefSpectra refSpectra = MakeRefSpectrum(spectrumInfo,
                                                                              peptideSeq,
                                                                              peptideModSeq,
                                                                              nodeGroup.PrecursorMz,
                                                                              precursorCharge);

                                    session.Save(refSpectra);

                                    dictLibrary.Add(libKey,
                                                    new BiblioLiteSpectrumInfo(libKey, refSpectra.Copies,
                                                                               refSpectra.NumPeaks,
                                                                               (int) (refSpectra.Id ?? 0),
                                                                               default(IndexedRetentionTimes),
                                                                               default(IndexedIonMobilities)));
                                }

                                session.Flush();
                                session.Clear();
                            }
                                // This is a BiblioSpec(schemaVersion >=1) library.
                            else
                            {
                                // get all the spectra, including the redundant ones if this library has any
                                var spectra = library.GetSpectra(libKey, labelType, LibraryRedundancy.all_redundant).ToArray();
                                // Avoid saving to the RefSpectra table for isotope label types that have no spectra
                                if (spectra.Length == 0)
                                    continue;

                                DbRefSpectra refSpectra = new DbRefSpectra
                                                              {
                                                                  PeptideSeq = peptideSeq,
                                                                  PrecursorMZ = nodeGroup.PrecursorMz,
                                                                  PrecursorCharge = precursorCharge,
                                                                  PeptideModSeq = peptideModSeq
                                                              };

                                // Get all the information for this reference spectrum.
                                // For BiblioSpec (schema ver >= 1), this can include retention time information
                                // for this spectrum as well as any redundant spectra for the peptide.
                                // Ids of spectra in the redundant library, where available, are also returned.
                                var redundantSpectraKeys = new List<SpectrumKeyTime>();
                                BuildRefSpectra(document, session, refSpectra, spectra, dictFiles, redundantSpectraKeys);

                                session.Save(refSpectra);
                                session.Flush();
                                session.Clear();

                                // TODO(nicksh): preserve retention time information.
                                var retentionTimesByFileId = default(IndexedRetentionTimes);
                                var driftTimesByFileId = default(IndexedIonMobilities);
                                dictLibrary.Add(libKey,
                                                new BiblioLiteSpectrumInfo(libKey,
                                                                           refSpectra.Copies,
                                                                           refSpectra.NumPeaks,
                                                                           (int) (refSpectra.Id ?? 0),
                                                                           retentionTimesByFileId,
                                                                           driftTimesByFileId));

                                // Save entries in the redundant library.
                                if (saveRedundantLib && redundantSpectraKeys.Count > 0)
                                {
                                    if (redundantSession == null)
                                    {
                                        redundantSession = OpenWriteSession_Redundant();
                                        redundantTransaction = redundantSession.BeginTransaction();
                                    }
                                    SaveRedundantSpectra(redundantSession, redundantSpectraKeys, dictFilesRedundant, refSpectra, library);
                                    redundantSpectraCount += redundantSpectraKeys.Count;
                                }
                            }
                        }

                        savedCount++;
                        if (!UpdateProgress(peptideCount, savedCount))
                            return null;
                    }

                    // Simulate ctime(d), which is what BlibBuild uses.
                    string createTime = string.Format("{0:ddd MMM dd HH:mm:ss yyyy}", DateTime.Now); // Not L10N? different date/time format in different countries
                    DbLibInfo libInfo = new DbLibInfo
                                            {
                                                LibLSID = libLsid,
                                                CreateTime = createTime,
                                                NumSpecs = dictLibrary.Count,
                                                MajorVersion = libraryRevision,
                                                MinorVersion = schemaVersion
                                            };

                    session.Save(libInfo);
                    session.Flush();
                    session.Clear();

                    transaction.Commit();

                    if (redundantTransaction != null)
                    {
                        var scoreType = new DbScoreTypes {Id = 0, ScoreType = "UNKNOWN"}; // Not L10N
                        redundantSession.Save(scoreType);

                        libInfo = new DbLibInfo
                                      {
                                          LibLSID = libLsid.Replace(":nr:", ":redundant:"), // Not L10N
                                          CreateTime = createTime,
                                          NumSpecs = redundantSpectraCount,
                                          MajorVersion = libraryRevision,
                                          MinorVersion = schemaVersion
                                      };
                        redundantSession.Save(libInfo);
                        redundantSession.Flush();
                        redundantSession.Clear();

                        redundantTransaction.Commit();
                    }
                }

            }
            finally
            {
                if(redundantTransaction != null)
                {
                    redundantTransaction.Dispose();
                }
                if (redundantSession != null)
                {
                    redundantSession.Dispose();
                }
            }

            var libraryEntries = dictLibrary.Values.ToArray();

            return new BiblioSpecLiteLibrary(librarySpec, libLsid, libraryRevision, schemaVersion,
                libraryEntries, FileStreamManager.Default);
        }