/// <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)); }
/// <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); }
/// <summary> /// Make a BiblioSpec SQLite library from a list of spectra and their intensities. /// </summary> /// <param name="librarySpec">Library spec for which the new library is created</param> /// <param name="listSpectra">List of existing spectra, by LibKey</param> /// <param name="libraryName">Name of the library to be created</param> /// <param name="progressMonitor">Progress monitor to display progress in creating library</param> /// <returns>A library of type <see cref="BiblioSpecLiteLibrary"/></returns> public BiblioSpecLiteLibrary CreateLibraryFromSpectra(BiblioSpecLiteSpec librarySpec, List <SpectrumMzInfo> listSpectra, string libraryName, IProgressMonitor progressMonitor) { const string libAuthority = BiblioSpecLiteLibrary.DEFAULT_AUTHORITY; const int majorVer = 1; const int minorVer = 0; string libId = libraryName; // 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(), majorVer, minorVer); var dictLibrary = new Dictionary <LibKey, BiblioLiteSpectrumInfo>(); using (ISession session = OpenWriteSession()) using (ITransaction transaction = session.BeginTransaction()) { int progressPercent = 0; int i = 0; var status = new ProgressStatus(Resources.BlibDb_CreateLibraryFromSpectra_Creating_spectral_library_for_imported_transition_list); foreach (var spectrum in listSpectra) { ++i; var dbRefSpectrum = RefSpectrumFromPeaks(spectrum); session.Save(dbRefSpectrum); dictLibrary.Add(spectrum.Key, new BiblioLiteSpectrumInfo(spectrum.Key, dbRefSpectrum.Copies, dbRefSpectrum.NumPeaks, (int)(dbRefSpectrum.Id ?? 0), default(IndexedRetentionTimes), default(IndexedIonMobilities))); if (progressMonitor != null) { if (progressMonitor.IsCanceled) { return(null); } int progressNew = (i * 100 / listSpectra.Count); if (progressPercent != progressNew) { progressMonitor.UpdateProgress(status = status.ChangePercentComplete(progressNew)); progressPercent = progressNew; } } } session.Flush(); session.Clear(); // 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 = majorVer, MinorVersion = minorVer }; session.Save(libInfo); session.Flush(); session.Clear(); transaction.Commit(); } var libraryEntries = dictLibrary.Values.ToArray(); return(new BiblioSpecLiteLibrary(librarySpec, libLsid, majorVer, minorVer, libraryEntries, FileStreamManager.Default)); }
/// <summary> /// Make a BiblioSpec SQLite library from a list of spectra and their intensities. /// </summary> /// <param name="librarySpec">Library spec for which the new library is created</param> /// <param name="listSpectra">List of existing spectra, by LibKey</param> /// <param name="libraryName">Name of the library to be created</param> /// <param name="progressMonitor">Progress monitor to display progress in creating library</param> /// <returns>A library of type <see cref="BiblioSpecLiteLibrary"/></returns> public BiblioSpecLiteLibrary CreateLibraryFromSpectra(BiblioSpecLiteSpec librarySpec, List<SpectrumMzInfo> listSpectra, string libraryName, IProgressMonitor progressMonitor) { const string libAuthority = BiblioSpecLiteLibrary.DEFAULT_AUTHORITY; const int majorVer = 1; const int minorVer = 0; string libId = libraryName; // 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(), majorVer, minorVer); var dictLibrary = new Dictionary<LibKey, BiblioLiteSpectrumInfo>(); using (ISession session = OpenWriteSession()) using (ITransaction transaction = session.BeginTransaction()) { int progressPercent = 0; int i = 0; var status = new ProgressStatus(Resources.BlibDb_CreateLibraryFromSpectra_Creating_spectral_library_for_imported_transition_list); foreach (var spectrum in listSpectra) { ++i; var dbRefSpectrum = RefSpectrumFromPeaks(spectrum); session.Save(dbRefSpectrum); dictLibrary.Add(spectrum.Key, new BiblioLiteSpectrumInfo(spectrum.Key, dbRefSpectrum.Copies, dbRefSpectrum.NumPeaks, (int)(dbRefSpectrum.Id ?? 0), default(IndexedRetentionTimes), default(IndexedIonMobilities))); if (progressMonitor != null) { if (progressMonitor.IsCanceled) return null; int progressNew = (i * 100 / listSpectra.Count); if (progressPercent != progressNew) { progressMonitor.UpdateProgress(status = status.ChangePercentComplete(progressNew)); progressPercent = progressNew; } } } session.Flush(); session.Clear(); // 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 = majorVer, MinorVersion = minorVer }; session.Save(libInfo); session.Flush(); session.Clear(); transaction.Commit(); } var libraryEntries = dictLibrary.Values.ToArray(); return new BiblioSpecLiteLibrary(librarySpec, libLsid, majorVer, minorVer, libraryEntries, FileStreamManager.Default); }