public void TestMeasuredDriftValues() { var testFilesDir = new TestFilesDir(TestContext, @"Test\Results\BlibDriftTimeTest.zip"); // Re-used from BlibDriftTimeTest // Open document with some peptides but no results var docPath = testFilesDir.GetTestPath("BlibDriftTimeTest.sky"); SrmDocument docOriginal = ResultsUtil.DeserializeDocument(docPath); var docContainer = new ResultsTestDocumentContainer(docOriginal, docPath); var doc = docContainer.Document; // Import an mz5 file that contains drift info const string replicateName = "ID12692_01_UCA168_3727_040714"; var chromSets = new[] { new ChromatogramSet(replicateName, new[] { new MsDataFilePath(testFilesDir.GetTestPath("ID12692_01_UCA168_3727_040714.mz5")), }), }; var docResults = doc.ChangeMeasuredResults(new MeasuredResults(chromSets)); Assert.IsTrue(docContainer.SetDocument(docResults, docOriginal, true)); docContainer.AssertComplete(); var document = docContainer.Document; document = document.ChangeSettings(document.Settings.ChangePeptidePrediction(prediction => new PeptidePrediction(null, DriftTimePredictor.EMPTY))); // Verify ability to extract predictions from raw data var newPred = document.Settings.PeptideSettings.Prediction.DriftTimePredictor.ChangeMeasuredDriftTimesFromResults( document, docContainer.DocumentFilePath); var result = newPred.MeasuredDriftTimePeptides; Assert.AreEqual(TestSmallMolecules? 2: 1, result.Count); const double expectedDT = 4.0019; var expectedOffset = .4829; Assert.AreEqual(expectedDT, result.Values.First().DriftTimeMsec(false).Value, .001); Assert.AreEqual(expectedOffset, result.Values.First().HighEnergyDriftTimeOffsetMsec, .001); // Check ability to update, and to preserve unchanged var revised = new Dictionary <LibKey, DriftTimeInfo>(); var libKey = result.Keys.First(); revised.Add(libKey, new DriftTimeInfo(4, 0.234)); var libKey2 = new LibKey("DEADEELS", 2); revised.Add(libKey2, new DriftTimeInfo(5, 0.123)); document = document.ChangeSettings( document.Settings.ChangePeptidePrediction(prediction => new PeptidePrediction(null, new DriftTimePredictor("test", revised, null, null, 40)))); newPred = document.Settings.PeptideSettings.Prediction.ChangeDriftTimePredictor( document.Settings.PeptideSettings.Prediction.DriftTimePredictor.ChangeMeasuredDriftTimesFromResults( document, docContainer.DocumentFilePath)).DriftTimePredictor; result = newPred.MeasuredDriftTimePeptides; Assert.AreEqual(TestSmallMolecules ? 3 : 2, result.Count); Assert.AreEqual(expectedDT, result[libKey].DriftTimeMsec(false).Value, .001); Assert.AreEqual(expectedOffset, result[libKey].HighEnergyDriftTimeOffsetMsec, .001); Assert.AreEqual(5, result[libKey2].DriftTimeMsec(false).Value, .001); Assert.AreEqual(0.123, result[libKey2].HighEnergyDriftTimeOffsetMsec, .001); docContainer.Release(); }
public IList <TItem> ItemsWithUnmodifiedSequence(Target target) { var libraryKey = new LibKey(target, Adduct.EMPTY).LibraryKey; var matches = _index.ItemsWithUnmodifiedSequence(libraryKey); return(new ItemIndexList(this, matches)); }
public override IEnumerable <SpectrumInfo> GetSpectra(LibKey key, IsotopeLabelType labelType, LibraryRedundancy redundancy) { int iEntry = FindEntry(key); if (iEntry < 0) { return(new SpectrumInfo[0]); } var entry = _libraryEntries[iEntry]; return(entry.FileDatas.Where(kvp => { if (!kvp.Value.ApexTime.HasValue) { return false; } if (redundancy == LibraryRedundancy.best && kvp.Key != entry.BestFileId) { return false; } return true; }) .Select(kvp => new SpectrumInfo(this, labelType, _sourceFiles[kvp.Key], kvp.Value.ApexTime, null, kvp.Key == entry.BestFileId, new ElibSpectrumKey(iEntry, kvp.Key)) { SpectrumHeaderInfo = CreateSpectrumHeaderInfo(entry) })); }
public ElibSpectrumInfo(String peptideModSeq, int charge, int bestFileId, IEnumerable <KeyValuePair <int, FileData> > fileDatas) { PeptideModSeq = peptideModSeq; Key = new LibKey(SequenceMassCalc.NormalizeModifiedSequence(peptideModSeq), charge); BestFileId = bestFileId; FileDatas = ImmutableSortedList.FromValues(fileDatas); }
public override bool TryGetRetentionTimes(LibKey key, MsDataFileUri filePath, out double[] retentionTimes) { retentionTimes = null; int i = FindEntry(key); if (i < 0) { return(false); } int fileId = FindFileInList(filePath, _sourceFiles); if (fileId < 0) { return(false); } var entry = _libraryEntries[i]; FileData fileData; if (!entry.FileDatas.TryGetValue(fileId, out fileData)) { return(false); } if (!fileData.ApexTime.HasValue) { return(false); } retentionTimes = new[] { fileData.ApexTime.Value }; return(true); }
public override bool TryGetRetentionTimes(LibKey key, MsDataFileUri filePath, out double[] retentionTimes) { retentionTimes = null; DbSpectrum[] spectra; if (!key.IsPrecursorKey) { spectra = GetSpectraByPeptide(filePath, key).ToArray(); } else { spectra = GetSpectraByPrecursor(filePath, key.PrecursorMz.GetValueOrDefault()).ToArray(); var keyRt = key.RetentionTime; if (keyRt.HasValue) { spectra = spectra.Where(s => Equals(keyRt.Value, s.RetentionTime)).ToArray(); } } if (!spectra.Any()) { return(false); } retentionTimes = spectra.Select(s => s.RetentionTime).ToArray(); return(true); }
public override bool TryLoadSpectrum(LibKey key, out SpectrumPeaksInfo spectrum) { spectrum = null; DbSpectrum[] spectra; if (!key.IsPrecursorKey) { spectra = GetSpectraByPeptide(null, key).ToArray(); } else { spectra = GetSpectraByPrecursor(null, key.PrecursorMz.GetValueOrDefault()).ToArray(); var keyRt = key.RetentionTime; if (keyRt.HasValue) { spectra = spectra.Where(s => Equals(keyRt.Value, s.RetentionTime)).ToArray(); } } if (!spectra.Any()) { return(false); } var spec = spectra.First(); var mi = spec.Mzs.Select((t, i) => new SpectrumPeaksInfo.MI { Mz = spec.Mzs[i], Intensity = (float)spec.Intensities[i] }); // CONSIDER(bspratt): annotation? spectrum = new SpectrumPeaksInfo(mi.ToArray()); return(true); }
public override IEnumerable <SpectrumInfoLibrary> GetSpectra(LibKey key, IsotopeLabelType labelType, LibraryRedundancy redundancy) { if (redundancy == LibraryRedundancy.best) { yield break; } if (!key.IsPrecursorKey) { foreach (var spectrum in GetSpectraByPeptide(null, key)) { yield return(new SpectrumInfoLibrary(this, labelType, spectrum.ResultsFile.FilePath, spectrum.RetentionTime, null, false, spectrum)); } yield break; } var keyRt = key.RetentionTime; foreach (var spectrum in GetSpectraByPrecursor(null, key.PrecursorMz.GetValueOrDefault())) { if (!keyRt.HasValue || Equals(keyRt.Value, spectrum.RetentionTime)) { yield return(new SpectrumInfoLibrary(this, labelType, spectrum.ResultsFile.FilePath, spectrum.RetentionTime, null, false, spectrum)); } } }
private void GetSmallMoleculeFragments(LibKey key, TransitionGroupDocNode nodeGroupMatched, SpectrumPeaksInfo spectrum, IList <DocNode> transitionDocNodes) { // We usually don't know actual charge of fragments in the library, so just note + or - if // there are no peak annotations containing that info var fragmentCharge = key.Adduct.AdductCharge < 0 ? Adduct.M_MINUS : Adduct.M_PLUS; // Get list of possible transitions based on library spectrum var transitionsUnranked = new List <DocNode>(); foreach (var peak in spectrum.Peaks) { transitionsUnranked.Add(TransitionFromPeakAndAnnotations(key, nodeGroupMatched, fragmentCharge, peak, null)); } var nodeGroupUnranked = (TransitionGroupDocNode)nodeGroupMatched.ChangeChildren(transitionsUnranked); // Filter again, retain only those with rank info, or at least an interesting name SpectrumHeaderInfo groupLibInfo = null; var transitionRanks = new Dictionary <double, LibraryRankedSpectrumInfo.RankedMI>(); nodeGroupUnranked.GetLibraryInfo(Settings, ExplicitMods.EMPTY, true, ref groupLibInfo, transitionRanks); foreach (var ranked in transitionRanks) { transitionDocNodes.Add(TransitionFromPeakAndAnnotations(key, nodeGroupMatched, fragmentCharge, ranked.Value.MI, ranked.Value.Rank)); } // And add any unranked that have names to display foreach (var unrankedT in nodeGroupUnranked.Transitions) { var unranked = unrankedT; if (!string.IsNullOrEmpty(unranked.Transition.CustomIon.Name) && !transitionDocNodes.Any(t => t is TransitionDocNode && unranked.Transition.Equivalent(((TransitionDocNode)t).Transition))) { transitionDocNodes.Add(unranked); } } }
public IEnumerable <TItem> ItemsWithUnmodifiedSequence(Target target) { var libraryKey = new LibKey(target, Adduct.EMPTY).LibraryKey; var matches = _index.ItemsWithUnmodifiedSequence(libraryKey); return(matches.Select(item => _allItems[item.OriginalIndex])); }
public ChromLibSpectrumInfo(LibKey key, int id, double peakArea, IndexedRetentionTimes retentionTimesByFileId, IEnumerable <SpectrumPeaksInfo.MI> transitionAreas) { Key = key; Id = id; PeakArea = peakArea; RetentionTimesByFileId = retentionTimesByFileId; TransitionAreas = ImmutableList.ValueOf(transitionAreas) ?? ImmutableList.Empty <SpectrumPeaksInfo.MI>(); }
public IList <IonMobilityAndCCS> GetIonMobilityInfo(LibKey key) { if (_database != null) { return(_database.GetIonMobilityInfo(key)); } return(null); }
public ChromLibSpectrumInfo(LibKey key, int id, double peakArea, IndexedRetentionTimes retentionTimesByFileId, IEnumerable<SpectrumPeaksInfo.MI> transitionAreas) { Key = key; Id = id; PeakArea = peakArea; RetentionTimesByFileId = retentionTimesByFileId; TransitionAreas = ImmutableList.ValueOf(transitionAreas) ?? ImmutableList.Empty<SpectrumPeaksInfo.MI>(); }
public IList <IonMobilityAndCCS> GetIonMobilityInfo(LibKey key) { if (DictLibrary.TryGetValue(key, out var im) && im.Count > 0) { return(im); } return(null); }
public XHunterSpectrumInfo(LibKey key, float processedIntensity, float expect, short numPeaks, long location) { _key = key; _processedIntensity = processedIntensity; _expect = expect; _numPeaks = numPeaks; _location = location; }
public override IonMobilityAndCCS GetIonMobilityInfo(LibKey key, ChargeRegressionLine regressionLine) { if (_database != null) { return(_database.GetDriftTimeInfo(key, regressionLine)); } return(null); }
public bool TryGetValue(LibKey key, out TItem value) { foreach (TItem matchingValue in ItemsMatching(key.LibraryKey, true)) { value = matchingValue; return(true); } value = default(TItem); return(false); }
// TODO(bspratt) either upgrade this for all ion mobility types, or rip out this code altogether public IonMobilityAndCCS GetDriftTimeInfo(LibKey key, ChargeRegressionLine regression) { DbIonMobilityPeptide pep; if (DictLibrary.TryGetValue(key, out pep)) { return(IonMobilityAndCCS.GetIonMobilityAndCCS(IonMobilityValue.GetIonMobilityValue(regression.GetY(pep.CollisionalCrossSection), eIonMobilityUnits.drift_time_msec), pep.CollisionalCrossSection, pep.HighEnergyDriftTimeOffsetMsec)); } return(null); }
public override bool Contains(LibKey key) { if (!key.IsPrecursorKey) { return(GetSpectraByPeptide(null, key).Any()); } var spectra = GetSpectraByPrecursor(null, key.PrecursorMz.GetValueOrDefault()); var keyRt = key.RetentionTime; return(!keyRt.HasValue ? spectra.Any() : spectra.Any(s => Equals(keyRt.Value, s.RetentionTime))); }
public void TestMeasuredDriftValues() { var testFilesDir = new TestFilesDir(TestContext, @"Test\Results\BlibDriftTimeTest.zip"); // Re-used from BlibDriftTimeTest // Open document with some peptides but no results var docPath = testFilesDir.GetTestPath("BlibDriftTimeTest.sky"); SrmDocument docOriginal = ResultsUtil.DeserializeDocument(docPath); var docContainer = new ResultsTestDocumentContainer(docOriginal, docPath); var doc = docContainer.Document; // Import an mz5 file that contains drift info const string replicateName = "ID12692_01_UCA168_3727_040714"; var chromSets = new[] { new ChromatogramSet(replicateName, new[] { new MsDataFilePath(testFilesDir.GetTestPath("ID12692_01_UCA168_3727_040714.mz5")), }), }; var docResults = doc.ChangeMeasuredResults(new MeasuredResults(chromSets)); Assert.IsTrue(docContainer.SetDocument(docResults, docOriginal, true)); docContainer.AssertComplete(); var document = docContainer.Document; document = document.ChangeSettings(document.Settings.ChangePeptidePrediction(prediction => new PeptidePrediction(null, DriftTimePredictor.EMPTY))); // Verify ability to extract predictions from raw data var newPred = document.Settings.PeptideSettings.Prediction.DriftTimePredictor.ChangeMeasuredDriftTimesFromResults( document, docContainer.DocumentFilePath); var result = newPred.MeasuredDriftTimePeptides; Assert.AreEqual(TestSmallMolecules? 2: 1, result.Count); const double expectedDT = 4.0019; var expectedOffset = .4829; Assert.AreEqual(expectedDT, result.Values.First().DriftTimeMsec(false).Value, .001); Assert.AreEqual(expectedOffset, result.Values.First().HighEnergyDriftTimeOffsetMsec, .001); // Check ability to update, and to preserve unchanged var revised = new Dictionary<LibKey, DriftTimeInfo>(); var libKey = result.Keys.First(); revised.Add(libKey, new DriftTimeInfo(4, 0.234)); var libKey2 = new LibKey("DEADEELS",2); revised.Add(libKey2, new DriftTimeInfo(5, 0.123)); document = document.ChangeSettings( document.Settings.ChangePeptidePrediction(prediction => new PeptidePrediction(null, new DriftTimePredictor("test", revised, null, null, 40)))); newPred = document.Settings.PeptideSettings.Prediction.ChangeDriftTimePredictor( document.Settings.PeptideSettings.Prediction.DriftTimePredictor.ChangeMeasuredDriftTimesFromResults( document, docContainer.DocumentFilePath)).DriftTimePredictor; result = newPred.MeasuredDriftTimePeptides; Assert.AreEqual(TestSmallMolecules ? 3 : 2, result.Count); Assert.AreEqual(expectedDT, result[libKey].DriftTimeMsec(false).Value, .001); Assert.AreEqual(expectedOffset, result[libKey].HighEnergyDriftTimeOffsetMsec, .001); Assert.AreEqual(5, result[libKey2].DriftTimeMsec(false).Value, .001); Assert.AreEqual(0.123, result[libKey2].HighEnergyDriftTimeOffsetMsec, .001); docContainer.Release(); }
public override bool TryGetLibInfo(LibKey key, out SpectrumHeaderInfo libInfo) { BiblioSpectrumInfo info; if (_dictLibrary != null && _dictLibrary.TryGetValue(key, out info)) { libInfo = new BiblioSpecSpectrumHeaderInfo(Name, info.Copies); return(true); } libInfo = null; return(false); }
public bool TryGetValue(Target target, out TItem item) { var libraryKey = new LibKey(target, Adduct.EMPTY).LibraryKey; foreach (var matchingItem in ItemsMatching(libraryKey, false)) { item = matchingItem; return(true); } item = default(TItem); return(false); }
public override bool TryLoadSpectrum(LibKey key, out SpectrumPeaksInfo spectrum) { BiblioSpectrumInfo info; if (_dictLibrary != null && _dictLibrary.TryGetValue(key, out info)) { spectrum = new SpectrumPeaksInfo(ReadSpectrum(info)); return(true); } spectrum = null; return(false); }
public override bool TryGetLibInfo(LibKey key, out SpectrumHeaderInfo libInfo) { if (_dictLibrary != null) { foreach (var item in _dictLibrary.ItemsMatching(key.LibraryKey, true)) { libInfo = new BiblioSpecSpectrumHeaderInfo(Name, item.Copies); return(true); } } libInfo = null; return(false); }
public override bool TryGetRetentionTimes(LibKey key, MsDataFileUri filePath, out double[] retentionTimes) { int i = FindEntry(key); int j = _librarySourceFiles.IndexOf(info => Equals(filePath.ToString(), info.FilePath)); if (i != -1 && j != -1) { retentionTimes = _libraryEntries[i].RetentionTimesByFileId.GetTimes(_librarySourceFiles[j].Id); return(true); } return(base.TryGetRetentionTimes(key, filePath, out retentionTimes)); }
public override IEnumerable <SpectrumInfo> GetSpectra(LibKey key, IsotopeLabelType labelType, LibraryRedundancy redundancy) { // This base class only handles best match spectra SpectrumHeaderInfo libInfo; if (redundancy == LibraryRedundancy.best && TryGetLibInfo(key, out libInfo)) { yield return new SpectrumInfo(this, labelType, key) { SpectrumHeaderInfo = libInfo } } ; }
double GetMzFromDocument(LibKey key) { foreach (var pair in _document.MoleculePrecursorPairs) { var nodePep = pair.NodePep; var nodeGroup = pair.NodeGroup; var libKey = nodeGroup.GetLibKey(nodePep); if (key.Equals(libKey)) { return(nodeGroup.PrecursorMz); } } return(0.0); }
public override bool TryLoadSpectrum(LibKey key, out SpectrumPeaksInfo spectrum) { if (_dictLibrary != null) { foreach (var item in _dictLibrary.ItemsMatching(key.LibraryKey, true)) { spectrum = new SpectrumPeaksInfo(ReadSpectrum(item)); return(true); } } spectrum = null; return(false); }
public IEnumerable <DbSpectrum> GetSpectraByPeptide(MsDataFileUri file, LibKey libKey) { foreach (var spectrum in GetSpectraByFile(file)) { if (string.IsNullOrWhiteSpace(spectrum.DocumentPeptide)) { continue; } var key = new PeptideLibraryKey(spectrum.DocumentPeptide, spectrum.DocumentPrecursorCharge.GetValueOrDefault()); if (LibKeyIndex.KeysMatch(libKey.LibraryKey, key)) { yield return(spectrum); } } }
// private const int PEPTIDE_MODIFICATION_OFFSET_LOWER = 2; // private const int PEPTIDE_MODIFICATION_OFFSET_UPPER = 1; public ViewLibraryPepInfo(LibKey key, ICollection<byte> lookupPool) : this() { Key = key; IndexLookup = lookupPool.Count; foreach (char aa in key.AminoAcids) lookupPool.Add((byte)aa); // Order extra bytes so that a byte-by-byte comparison of the // lookup bytes will order correctly. lookupPool.Add((byte)key.Charge); int countMods = key.ModificationCount; lookupPool.Add((byte)(countMods & 0xFF)); lookupPool.Add((byte)((countMods >> 8) & 0xFF)); // probably never non-zero, but to be safe LengthLookup = lookupPool.Count - IndexLookup; }
public static ChromLibSpectrumInfo Read(Stream stream) { LibKey key = LibKey.Read(stream); int id = PrimitiveArrays.ReadOneValue <int>(stream); double peakArea = PrimitiveArrays.ReadOneValue <double>(stream); var retentionTimesByFileId = IndexedRetentionTimes.Read(stream); int mzCount = PrimitiveArrays.ReadOneValue <int>(stream); var mzs = PrimitiveArrays.Read <double>(stream, mzCount); var areas = PrimitiveArrays.Read <float>(stream, mzCount); var mzAreas = ImmutableList.ValueOf(Enumerable.Range(0, mzCount) .Select(index => new SpectrumPeaksInfo.MI { Mz = mzs[index], Intensity = areas[index] })); return(new ChromLibSpectrumInfo(key, id, peakArea, retentionTimesByFileId, mzAreas)); }
// private const int PEPTIDE_MODIFICATION_OFFSET_LOWER = 2; // private const int PEPTIDE_MODIFICATION_OFFSET_UPPER = 1; public ViewLibraryPepInfo(LibKey key, ICollection <byte> lookupPool) : this() { Key = key; IndexLookup = lookupPool.Count; foreach (char aa in key.AminoAcids) { lookupPool.Add((byte)aa); } // Order extra bytes so that a byte-by-byte comparison of the // lookup bytes will order correctly. lookupPool.Add((byte)key.Charge); int countMods = key.ModificationCount; lookupPool.Add((byte)(countMods & 0xFF)); lookupPool.Add((byte)((countMods >> 8) & 0xFF)); // probably never non-zero, but to be safe LengthLookup = lookupPool.Count - IndexLookup; }
public override void ReadXml(XmlReader reader) { var name = reader.Name; // Read start tag attributes base.ReadXml(reader); _windowWidthCalculator = new IonMobilityWindowWidthCalculator(reader, true, false); // Consume start tag reader.ReadStartElement(); // Skip over ion_mobility_library stuff that never saw the light of day, but appears in some older tests while (reader.Name.Equals(@"ion_mobility_library") || reader.Name.Equals(@"regression_dt")) { reader.Read(); } // Read all measured ion mobilities var dict = new Dictionary <LibKey, IonMobilityAndCCS>(); while (reader.IsStartElement(EL.measured_dt)) // N.B. EL.measured_dt is a misnomer, this covers all IMS types { var im = MeasuredIonMobility.Deserialize(reader); var key = new LibKey(im.Target, im.Charge); if (!dict.ContainsKey(key)) { dict.Add(key, im.IonMobilityInfo); } } if (dict.Any()) { MeasuredMobilityIons = dict; } if (reader.Name.Equals(name)) // Make sure we haven't stepped off the end { reader.ReadEndElement(); // Consume end tag } Validate(); }
public bool TryLoadSpectrum(string sequence, int charge, ExplicitMods mods, out IsotopeLabelType type, out SpectrumPeaksInfo spectrum) { var libraries = PeptideSettings.Libraries; foreach (var typedSequence in GetTypedSequences(sequence, mods)) { var key = new LibKey(typedSequence.ModifiedSequence, charge); if (libraries.TryLoadSpectrum(key, out spectrum)) { type = typedSequence.LabelType; return true; } } type = IsotopeLabelType.light; spectrum = null; return false; }
private void EvaluateBestDriftTime(int msLevel, LibKey libKey, float tolerance, List<TransitionFullScanInfo> transitions) { double? driftTime = null; double maxIntensity = 0; // Avoid picking MS2 drift times wildly different from MS1 times double? ms1DriftTimeBest; if ((msLevel == 2) && _ms1DriftTimes.ContainsKey(libKey)) { ms1DriftTimeBest = _ms1DriftTimes[libKey].OrderByDescending(p => p.Intensity) .FirstOrDefault() .DriftTime; } else { ms1DriftTimeBest = null; } const int maxHighEnergyDriftOffsetMsec = 2; // CONSIDER(bspratt): user definable? or dynamically set by looking at scan to scan drift delta? Or resolving power? foreach (var scan in _msDataFileScanHelper.MsDataSpectra.Where(scan => scan != null)) { if (!scan.DriftTimeMsec.HasValue || !scan.Mzs.Any()) continue; if (ms1DriftTimeBest.HasValue && (scan.DriftTimeMsec.Value < ms1DriftTimeBest.Value - maxHighEnergyDriftOffsetMsec || scan.DriftTimeMsec.Value > ms1DriftTimeBest.Value + maxHighEnergyDriftOffsetMsec)) continue; // Get the total intensity for all transitions of current msLevel double totalIntensity = 0; foreach (var t in transitions) { var mzPeak = t.ProductMz; var halfwin = (t.ExtractionWidth ?? tolerance)/2; var mzLow = mzPeak - halfwin; var mzHigh = mzPeak + halfwin; var first = Array.BinarySearch(scan.Mzs, mzLow); if (first < 0) first = ~first; for (var i = first; i < scan.Mzs.Length; i++) { if (scan.Mzs[i] > mzHigh) break; totalIntensity += scan.Intensities[i]; } } if (maxIntensity < totalIntensity) { driftTime = scan.DriftTimeMsec; maxIntensity = totalIntensity; } } if (driftTime.HasValue) { var dict = (msLevel == 1) ? _ms1DriftTimes : _ms2DriftTimes; var result = new DriftTimeIntensityPair { DriftTime = driftTime.Value, Intensity = maxIntensity }; List<DriftTimeIntensityPair> listPairs; if (!dict.TryGetValue(libKey, out listPairs)) { listPairs = new List<DriftTimeIntensityPair>(); dict.Add(libKey, listPairs); } listPairs.Add(result); } }
public bool TryGetRetentionTimes(LibKey key, MsDataFileUri filePath, out double[] retentionTimes) { Assume.IsTrue(IsLoaded); foreach (Library lib in _libraries) { if (lib != null && lib.TryGetRetentionTimes(key, filePath, out retentionTimes)) return true; } retentionTimes = null; return false; }
// ReSharper restore UnusedMember.Local private bool Load(ILoadMonitor loader) { ProgressStatus status = new ProgressStatus(string.Format(Resources.BiblioSpecLibrary_Load_Loading__0__library, Path.GetFileName(FilePath))); loader.UpdateProgress(status); long lenRead = 0; // AdlerChecksum checksum = new AdlerChecksum(); try { // Use a buffered stream for initial read BufferedStream stream = new BufferedStream(CreateStream(loader), 32 * 1024); int countHeader = (int) LibHeaders.count*4; byte[] libHeader = new byte[countHeader]; if (stream.Read(libHeader, 0, countHeader) != countHeader) throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_library_header_File_may_be_corrupted); lenRead += countHeader; // Check the first byte of the primary version number to determine // whether the format is little- or big-endian. Little-endian will // have the version number in this byte, while big-endian will have zero. if (libHeader[(int) LibHeaders.version1 * 4] == 0) _bigEndian = true; int numSpectra = GetInt32(libHeader, (int) LibHeaders.num_spectra); var dictLibrary = new Dictionary<LibKey, BiblioSpectrumInfo>(numSpectra); var setSequences = new HashSet<LibSeqKey>(); string revStr = string.Format("{0}.{1}", // Not L10N GetInt32(libHeader, (int) LibHeaders.version1), GetInt32(libHeader, (int) LibHeaders.version2)); Revision = float.Parse(revStr, CultureInfo.InvariantCulture); // checksum.MakeForBuff(libHeader, AdlerChecksum.ADLER_START); countHeader = (int) SpectrumHeaders.count*4; byte[] specHeader = new byte[1024]; byte[] specSequence = new byte[1024]; for (int i = 0; i < numSpectra; i++) { int percent = i * 100 / numSpectra; if (status.PercentComplete != percent) { // Check for cancellation after each integer change in percent loaded. if (loader.IsCanceled) { loader.UpdateProgress(status.Cancel()); return false; } // If not cancelled, update progress. loader.UpdateProgress(status = status.ChangePercentComplete(percent)); } // Read spectrum header int bytesRead = stream.Read(specHeader, 0, countHeader); if (bytesRead != countHeader) throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_spectrum_header_File_may_be_corrupted); // If this is the first header, and the sequence length is zero, // then this is a Linux format library. Switch to linux format, // and start over. if (i == 0 && GetInt32(specHeader, (int)SpectrumHeaders.seq_len) == 0) { _linuxFormat = true; stream.Seek(lenRead, SeekOrigin.Begin); // Re-ead spectrum header countHeader = (int)SpectrumHeadersLinux.count * 4; bytesRead = stream.Read(specHeader, 0, countHeader); if (bytesRead != countHeader) throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_spectrum_header_File_may_be_corrupted); } lenRead += bytesRead; // checksum.MakeForBuff(specHeader, checksum.ChecksumValue); int charge = GetInt32(specHeader, (int)SpectrumHeaders.charge); if (charge > TransitionGroup.MAX_PRECURSOR_CHARGE) throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Invalid_precursor_charge_found_File_may_be_corrupted); int numPeaks = GetInt32(specHeader, (int)SpectrumHeaders.num_peaks); int seqLength = GetInt32(specHeader, (_linuxFormat ? (int)SpectrumHeadersLinux.seq_len : (int)SpectrumHeaders.seq_len)); int copies = GetInt32(specHeader, (_linuxFormat ? (int)SpectrumHeadersLinux.copies : (int)SpectrumHeaders.copies)); // Read sequence information int countSeq = (seqLength + 1)*2; if (stream.Read(specSequence, 0, countSeq) != countSeq) throw new InvalidDataException(Resources.BiblioSpecLibrary_Load_Data_truncation_in_spectrum_sequence_File_may_be_corrupted); lenRead += countSeq; // checksum.MakeForBuff(specSequence, checksum.ChecksumValue); // Store in dictionary if (IsUnmodified(specSequence, seqLength + 1, seqLength)) { // These libraries should not have duplicates, but just in case. // CONSIDER: Emit error about redundancy? // These legacy libraries assume [+57.0] modified Cysteine LibKey key = new LibKey(GetCModified(specSequence, ref seqLength), 0, seqLength, charge); if (!dictLibrary.ContainsKey(key)) dictLibrary.Add(key, new BiblioSpectrumInfo((short)copies, (short)numPeaks, lenRead)); setSequences.Add(new LibSeqKey(key)); } // Read over peaks int countPeaks = 2*sizeof(Single)*numPeaks; stream.Seek(countPeaks, SeekOrigin.Current); // Skip spectrum lenRead += countPeaks; // checksum.MakeForBuff(specPeaks, checksum.ChecksumValue); } // Checksum = checksum.ChecksumValue; _dictLibrary = dictLibrary; _setSequences = setSequences; loader.UpdateProgress(status.Complete()); return true; } catch (InvalidDataException x) { loader.UpdateProgress(status.ChangeErrorException(x)); return false; } catch (IOException x) { loader.UpdateProgress(status.ChangeErrorException(x)); return false; } catch (Exception x) { x = new Exception(string.Format(Resources.BiblioSpecLibrary_Load_Failed_loading_library__0__, FilePath), x); loader.UpdateProgress(status.ChangeErrorException(x)); return false; } finally { if (ReadStream != null) { // Close the read stream to ensure we never leak it. // This only costs on extra open, the first time the // active document tries to read. try { ReadStream.CloseStream(); } catch(IOException) {} } } }
/// <summary> /// Loads all the spectra found in all the loaded libraries with the /// given LibKey and IsotopeLabelType. /// </summary> /// <param name="key">The LibKey to match on</param> /// <param name="labelType">The IsotopeLabelType to match on</param> /// <param name="bestMatch">True if only best-match spectra are included</param> public IEnumerable<SpectrumInfo> GetSpectra(LibKey key, IsotopeLabelType labelType, bool bestMatch) { Assume.IsTrue(IsLoaded); var redundancy = bestMatch ? LibraryRedundancy.best : LibraryRedundancy.all; return _libraries.Where(lib => lib != null).SelectMany(lib => lib.GetSpectra(key, labelType, redundancy)); }
/// <summary> /// Made public for testing purposes only: exercises library and predictor but doesn't handle explicitly set drift times. /// Use GetDriftTime() instead. /// </summary> public DriftTimeInfo GetDriftTimeHelper(LibKey chargedPeptide, LibraryIonMobilityInfo libraryIonMobilityInfo, out double windowDtMsec) { if (DriftTimePredictor != null) { var result = DriftTimePredictor.GetDriftTimeInfo(chargedPeptide, out windowDtMsec); if (result != null && result.DriftTimeMsec(false).HasValue) return result; } if (libraryIonMobilityInfo != null) { var dt = libraryIonMobilityInfo.GetLibraryMeasuredDriftTimeAndHighEnergyOffset(chargedPeptide); if ((dt != null) && dt.DriftTimeMsec(false).HasValue && (LibraryDriftTimesResolvingPower ?? 0) > 0) { windowDtMsec = 2.0 * dt.DriftTimeMsec(false).Value / LibraryDriftTimesResolvingPower.Value; return dt; } } windowDtMsec = 0; return new DriftTimeInfo(null, 0); }
// No ion mobility data in BiblioSpec libs (those are ancient - try BiblioSpecLite instead) public override bool TryGetIonMobilities(LibKey key, MsDataFileUri filePath, out IonMobilityInfo[] ionMobilities) { ionMobilities = null; return false; }
public override IEnumerable<SpectrumInfo> GetSpectra(LibKey key, IsotopeLabelType labelType, LibraryRedundancy redundancy) { // This base class only handles best match spectra SpectrumHeaderInfo libInfo; if (redundancy == LibraryRedundancy.best && TryGetLibInfo(key, out libInfo)) yield return new SpectrumInfo(this, labelType, key) { SpectrumHeaderInfo = libInfo }; }
// Returns false on cancellation private bool ProcessFile(MsDataFileUri filePath) { var results = _document.Settings.MeasuredResults; if (!results.MSDataFilePaths.Contains(filePath)) return true; // Nothing to do if (_progressStatus != null) { _progressStatus = _progressStatus.ChangeMessage(filePath.GetFileName()); } _currentDisplayedTransitionGroupDocNode = null; var tolerance = (float)_document.Settings.TransitionSettings.Instrument.MzMatchTolerance; foreach (var pair in _document.MoleculePrecursorPairs) { var nodePep = pair.NodePep; var nodeGroup = pair.NodeGroup; var libKey = new LibKey(nodePep.RawTextId, nodeGroup.PrecursorCharge); // Across all replicates for this precursor, note the drift time at max intensity for this mz for (var i = 0; i < results.Chromatograms.Count; i++) { if (_progressMonitor != null && _progressMonitor.IsCanceled) return false; ChromatogramGroupInfo[] chromGroupInfos; results.TryLoadChromatogram(i, nodePep, nodeGroup, tolerance, true, out chromGroupInfos); foreach (var chromInfo in chromGroupInfos.Where(c => filePath == c.FilePath)) { if (!ProcessChromInfo(filePath, chromInfo, pair, nodeGroup, tolerance, libKey)) return false; // User cancelled } } } return true; }
public override bool TryGetLibInfo(LibKey key, out SpectrumHeaderInfo libInfo) { BiblioSpectrumInfo info; if (_dictLibrary != null && _dictLibrary.TryGetValue(key, out info)) { libInfo = new BiblioSpecSpectrumHeaderInfo(Name, info.Copies); return true; } libInfo = null; return false; }
private bool ProcessMSLevel(MsDataFileUri filePath, int msLevel, IEnumerable<ChromatogramInfo> transitionPointSets, ChromatogramGroupInfo chromInfo, double? apexRT, TransitionGroupDocNode nodeGroup, LibKey libKey, float tolerance) { var transitions = new List<TransitionFullScanInfo>(); var chromSource = (msLevel == 1) ? ChromSource.ms1 : ChromSource.fragment; foreach (var tranPointSet in transitionPointSets.Where(t => t.Source == chromSource)) { transitions.Add(new TransitionFullScanInfo { //Name = tranPointSet.Header., Source = chromSource, ScanIndexes = chromInfo.ScanIndexes, PrecursorMz = chromInfo.PrecursorMz, ProductMz = tranPointSet.ProductMz, ExtractionWidth = tranPointSet.ExtractionWidth, //Id = nodeTran.Id }); } var chorusUrl = filePath as ChorusUrl; IScanProvider scanProvider; if (null == chorusUrl) { scanProvider = new ScanProvider(_documentFilePath, filePath, chromSource, chromInfo.Times, transitions.ToArray(), () => _document.Settings.MeasuredResults.LoadMSDataFileScanIds(filePath)); } else { scanProvider = new ChorusScanProvider(_documentFilePath, chorusUrl, chromSource, chromInfo.Times, transitions.ToArray()); } // Across all spectra at the peak retention time, find the one with max total // intensity for the mz's of interest (ie the isotopic distribution) and note its drift time. var scanIndex = chromInfo.ScanIndexes != null ? MsDataFileScanHelper.FindScanIndex(chromInfo, apexRT.Value) : -1; _msDataFileScanHelper.UpdateScanProvider(scanProvider, 0, scanIndex); _msDataFileScanHelper.MsDataSpectra = null; // Reset scanIndex = _msDataFileScanHelper.GetScanIndex(); _msDataFileScanHelper.ScanProvider.SetScanForBackgroundLoad(scanIndex); lock (this) { while (_msDataFileScanHelper.MsDataSpectra == null && _dataFileScanHelperException == null) { if (_progressMonitor != null && _progressMonitor.IsCanceled) return false; Monitor.Wait(this, 500); // Let background loader do its thing } } if (_dataFileScanHelperException != null) { throw new IOException(TextUtil.LineSeparate(Resources.DriftTimeFinder_HandleLoadScanException_Problem_using_results_to_populate_drift_time_library__, _dataFileScanHelperException.Message), _dataFileScanHelperException); } if (_progressMonitor != null && !ReferenceEquals(nodeGroup, _currentDisplayedTransitionGroupDocNode)) { // Do this after scan load so first group after file switch doesn't seem laggy _progressStatus = _progressStatus.ChangeMessage(TextUtil.LineSeparate(filePath.GetFileName(), nodeGroup.ToString())). UpdatePercentCompleteProgress(_progressMonitor, _currentStep++, _totalSteps); _currentDisplayedTransitionGroupDocNode = nodeGroup; } EvaluateBestDriftTime(msLevel, libKey, tolerance, transitions); return true; }
public bool TryGetLibInfo(LibKey key, out SpectrumHeaderInfo libInfo) { Assume.IsTrue(IsLoaded); foreach (Library lib in _libraries) { if (lib != null && lib.TryGetLibInfo(key, out libInfo)) return true; } libInfo = null; return false; }
public override bool TryLoadSpectrum(LibKey key, out SpectrumPeaksInfo spectrum) { BiblioSpectrumInfo info; if (_dictLibrary != null && _dictLibrary.TryGetValue(key, out info)) { spectrum = new SpectrumPeaksInfo(ReadSpectrum(info)); return true; } spectrum = null; return false; }
/// <summary> /// Tests our ability to discover drift times by inspecting loaded results /// </summary> private void TestMeasuredDriftTimes() { var testFilesDir = new TestFilesDir(TestContext, @"Test\Results\BlibDriftTimeTest.zip"); // Re-used from BlibDriftTimeTest // Open document with some peptides but no results var documentFile = TestFilesDir.GetTestPath(@"..\BlibDriftTimeTest\BlibDriftTimeTest.sky"); WaitForCondition(() => File.Exists(documentFile)); RunUI(() => SkylineWindow.OpenFile(documentFile)); WaitForDocumentLoaded(); var doc = SkylineWindow.Document; // Import an mz5 file that contains drift info ImportResultsFile(testFilesDir.GetTestPath(@"..\BlibDriftTimeTest\ID12692_01_UCA168_3727_040714.mz5")); // Verify ability to extract predictions from raw data var peptideSettingsDlg = ShowDialog<PeptideSettingsUI>( () => SkylineWindow.ShowPeptideSettingsUI(PeptideSettingsUI.TABS.Prediction)); // Simulate user picking Add... from the Drift Time Predictor combo control var driftTimePredictorDlg = ShowDialog<EditDriftTimePredictorDlg>(peptideSettingsDlg.AddDriftTimePredictor); const string predictorName = "TestMeasuredDriftTimes"; const double resolvingPower = 123.4; RunUI(() => { driftTimePredictorDlg.SetResolvingPower(resolvingPower); driftTimePredictorDlg.SetPredictorName(predictorName); driftTimePredictorDlg.GetDriftTimesFromResults(); driftTimePredictorDlg.OkDialog(); }); WaitForClosedForm(driftTimePredictorDlg); var pepSetDlg = peptideSettingsDlg; RunUI(() => { pepSetDlg.OkDialog(); }); WaitForClosedForm(peptideSettingsDlg); doc = WaitForDocumentChange(doc); var result = doc.Settings.PeptideSettings.Prediction.DriftTimePredictor.MeasuredDriftTimePeptides; Assert.AreEqual(2, result.Count); var key3 = new LibKey("GLAGVENVTELKK", 3); var key2 = new LibKey("GLAGVENVTELKK", 2); const double expectedDT3= 4.0709; const double expectedOffset3 = 0.8969; Assert.AreEqual(expectedDT3, result[key3].DriftTimeMsec(false).Value, .001); Assert.AreEqual(expectedOffset3, result[key3].HighEnergyDriftTimeOffsetMsec, .001); // High energy offset const double expectedDT2 = 5.5889; const double expectedOffset2 = -1.1039; Assert.AreEqual(expectedDT2, result[key2].DriftTimeMsec(false).Value, .001); Assert.AreEqual(expectedOffset2, result[key2].HighEnergyDriftTimeOffsetMsec, .001); // High energy offset WaitForDocumentLoaded(); // Verify exception handling by deleting the msdata file File.Delete(testFilesDir.GetTestPath(@"..\BlibDriftTimeTest\ID12692_01_UCA168_3727_040714.mz5")); peptideSettingsDlg = ShowDialog<PeptideSettingsUI>( () => SkylineWindow.ShowPeptideSettingsUI(PeptideSettingsUI.TABS.Prediction)); var driftTimePredictorDoomedDlg = ShowDialog<EditDriftTimePredictorDlg>(peptideSettingsDlg.AddDriftTimePredictor); RunUI(() => { driftTimePredictorDoomedDlg.SetResolvingPower(resolvingPower); driftTimePredictorDoomedDlg.SetPredictorName(predictorName+"_doomed"); }); RunDlg<MessageDlg>(driftTimePredictorDoomedDlg.GetDriftTimesFromResults, messageDlg => { AssertEx.AreComparableStrings( Resources.DriftTimeFinder_HandleLoadScanException_Problem_using_results_to_populate_drift_time_library__, messageDlg.Message); messageDlg.OkDialog(); }); RunUI(() => driftTimePredictorDoomedDlg.CancelDialog()); WaitForClosedForm(driftTimePredictorDoomedDlg); RunUI(() => { peptideSettingsDlg.OkDialog(); }); WaitForClosedForm(peptideSettingsDlg); }
private bool ProcessChromInfo(MsDataFileUri filePath, ChromatogramGroupInfo chromInfo, PeptidePrecursorPair pair, TransitionGroupDocNode nodeGroup, float tolerance, LibKey libKey) { Assume.IsTrue(chromInfo.BestPeakIndex != -1); var resultIndex = _document.Settings.MeasuredResults.Chromatograms.IndexOf(c => c.GetFileInfo(filePath) != null); if (resultIndex == -1) return true; var chromFileInfo = _document.Settings.MeasuredResults.Chromatograms[resultIndex].GetFileInfo(filePath); Assume.IsTrue(Equals(chromFileInfo.FilePath.GetLockMassParameters(), filePath.GetLockMassParameters())); // Determine apex RT for DT measurement using most intense MS1 peak var apexRT = GetApexRT(nodeGroup, resultIndex, chromFileInfo, true) ?? GetApexRT(nodeGroup, resultIndex, chromFileInfo, false); Assume.IsTrue(chromInfo.PrecursorMz == pair.NodeGroup.PrecursorMz); // Only use the transitions currently enabled var transitionPointSets = chromInfo.TransitionPointSets.Where( tp => nodeGroup.Transitions.Any( t => (t.Mz - (tp.ExtractionWidth ?? tolerance)/2) <= tp.ProductMz && (t.Mz + (tp.ExtractionWidth ?? tolerance)/2) >= tp.ProductMz)) .ToArray(); for (var msLevel = 1; msLevel <= 2; msLevel++) { if (!ProcessMSLevel(filePath, msLevel, transitionPointSets, chromInfo, apexRT, nodeGroup, libKey, tolerance)) return false; // User cancelled } return true; }
public PeptideDocNode GetModifiedNode(LibKey key, string seqUnmod, SrmSettings settings, SrmSettingsDiff diff) { if (string.IsNullOrEmpty(seqUnmod)) return null; var peptide = new Peptide(null, seqUnmod, null, null, settings.PeptideSettings.Enzyme.CountCleavagePoints(seqUnmod)); // First try and create the match from the settings created to match the library explorer. Settings = HasMatches ? settings.ChangePeptideModifications(mods => MatcherPepMods) : settings; TransitionGroupDocNode nodeGroup; var nodePep = CreateDocNodeFromSettings(key.Sequence, peptide, diff, out nodeGroup); if (nodePep != null) { if (diff == null) { nodePep = (PeptideDocNode)nodePep.ChangeAutoManageChildren(false); } else { // Keep only the matching transition group, so that modifications // will be highlighted differently for light and heavy forms. // Only performed when getting peptides for display in the explorer. nodePep = (PeptideDocNode)nodePep.ChangeChildrenChecked( new DocNode[] { nodeGroup }); } return nodePep; } else if (Matches == null) return null; bool hasHeavy; // Create explicit mods from the found matches. nodePep = CreateDocNodeFromMatches(new PeptideDocNode(peptide), EnumerateSequenceInfos(key.Key, true), false, out hasHeavy); if (nodePep == null) return null; // Call change settings with the matched modification settings to enumerate the children. nodePep = nodePep.ChangeSettings(settings.ChangePeptideModifications(mods => !HasMatches ? settings.PeptideSettings.Modifications : MatcherPepMods), diff ?? SrmSettingsDiff.ALL); if (nodePep.Children.Count == 0) return null; // Select the correct child, only for use with the library explorer. if (diff != null && nodePep.Children.Count > 1) { nodePep = (PeptideDocNode) nodePep.ChangeChildrenChecked(new List<DocNode> { nodePep.Children[hasHeavy ? 1 : 0] }); } if (diff == null) { nodePep = (PeptideDocNode)nodePep.ChangeAutoManageChildren(false); } return nodePep; }
public override bool Contains(LibKey key) { return (_dictLibrary != null && _dictLibrary.ContainsKey(key)); }
public bool Contains(LibKey key) { Assume.IsTrue(IsLoaded); foreach (Library lib in _libraries) { if (lib != null && lib.Contains(key)) return true; } return false; }
public bool TryGetLibInfo(string sequence, int charge, ExplicitMods mods, out IsotopeLabelType type, out SpectrumHeaderInfo libInfo) { if (sequence == null) { type = null; libInfo = null; return false; } var libraries = PeptideSettings.Libraries; foreach (var typedSequence in GetTypedSequences(sequence, mods)) { var key = new LibKey(typedSequence.ModifiedSequence, charge); if (libraries.TryGetLibInfo(key, out libInfo)) { type = typedSequence.LabelType; return true; } } type = IsotopeLabelType.light; libInfo = null; return false; }
public bool TryLoadSpectrum(LibKey key, out SpectrumPeaksInfo spectrum) { Assume.IsTrue(IsLoaded); foreach (Library lib in _libraries) { if (lib != null && lib.TryLoadSpectrum(key, out spectrum)) return true; } spectrum = null; return false; }
public bool TryGetRetentionTimes(string sequence, int charge, ExplicitMods mods, MsDataFileUri filePath, out IsotopeLabelType type, out double[] retentionTimes) { var libraries = PeptideSettings.Libraries; foreach (var typedSequence in GetTypedSequences(sequence, mods)) { var key = new LibKey(typedSequence.ModifiedSequence, charge); if (libraries.TryGetRetentionTimes(key, filePath, out retentionTimes)) { type = typedSequence.LabelType; return true; } } type = IsotopeLabelType.light; retentionTimes = null; return false; }
public override bool TryGetRetentionTimes(LibKey key, MsDataFileUri filePath, out double[] retentionTimes) { retentionTimes = null; return false; }
/// <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); }