public static void SelectAndApplyPeak(string modifiedSequence, double?precursorMz, string resultsName, bool subsequent, double rt) { bool found = false; var skylineWindow = Program.MainWindow; var doc = skylineWindow.Document; IdentityPath identityPath = null; var libKeyToMatch = new PeptideLibraryKey(modifiedSequence, 0); foreach (PeptideGroupDocNode nodePepGroup in doc.MoleculeGroups) { foreach (var nodePep in nodePepGroup.Peptides.Where(nodePep => LibKeyIndex.KeysMatch(libKeyToMatch, nodePep.ModifiedTarget.GetLibKey(Adduct.EMPTY)))) { var nodeTranGroup = precursorMz.HasValue ? nodePep.TransitionGroups.First(tranGroup => Math.Abs(tranGroup.PrecursorMz - precursorMz.Value) < 0.01) : nodePep.TransitionGroups.First(); identityPath = new IdentityPath(nodePepGroup.Id, nodePep.Id, nodeTranGroup.Id); IdentityPath path = identityPath; skylineWindow.SelectedPath = path; found = true; break; } if (found) { break; } } if (!found) { Assert.Fail("Could not find peptide {0}", modifiedSequence); } found = false; var chromatograms = doc.Settings.MeasuredResults.Chromatograms; foreach (var chromatogram in chromatograms.Where(chromSet => chromSet.Name.Equals(resultsName))) { found = true; ChromatogramSet chromSet = chromatogram; skylineWindow.SelectedResultsIndex = chromatograms.IndexOf(chromSet); skylineWindow.ModifyDocument("change peak", document => document.ChangePeak(identityPath, chromSet.Name, chromSet.MSDataFilePaths.First(), null, rt, UserSet.TRUE)); break; } if (!found) { Assert.Fail("Could not find results {0}", resultsName); } skylineWindow.ApplyPeak(subsequent); }
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); } } }
protected override bool IsMatch(Target seqMod, PeptideDocNode nodePepMod, out TransitionGroupDocNode nodeGroup) { foreach (TransitionGroupDocNode nodeGroupChild in nodePepMod.Children) { nodeGroup = nodeGroupChild; var calc = Settings.TryGetPrecursorCalc(nodeGroupChild.TransitionGroup.LabelType, nodePepMod.ExplicitMods); if (calc == null) { return(false); } var modSequence = calc.GetModifiedSequence(nodePepMod.Peptide.Target, false); // If this sequence matches the sequence of the library peptide, a match has been found. if (LibKeyIndex.KeysMatch(seqMod.GetLibKey(Adduct.EMPTY), modSequence.GetLibKey(Adduct.EMPTY))) { return(true); } } nodeGroup = null; return(false); }
protected override void DoTest() { string skyFile = TestFilesDir.GetTestPath("test_measured_drift_times_perf.sky"); Program.ExtraRawFileSearchFolder = TestFilesDir.PersistentFilesDir; // So we don't have to reload the raw files, which have moved relative to skyd file RunUI(() => SkylineWindow.OpenFile(skyFile)); var document = WaitForDocumentLoaded(240000); // If it decides to remake chromatograms this can take awhile AssertEx.IsDocumentState(document, null, 1, 34, 38, 398); RunUI(() => { SkylineWindow.SaveDocument(TestFilesDir.GetTestPath("local.sky")); // Avoid "document changed since last edit" message document = SkylineWindow.DocumentUI; }); var curatedDTs = document.Settings.PeptideSettings.Prediction.IonMobilityPredictor.MeasuredMobilityIons; var measuredDTs = new List <IDictionary <LibKey, IonMobilityAndCCS> >(); var precursors = new LibKeyIndex(document.MoleculePrecursorPairs.Select( p => p.NodePep.ModifiedTarget.GetLibKey(p.NodeGroup.PrecursorAdduct).LibraryKey)); for (var pass = 0; pass < 2; pass++) { // Verify ability to extract predictions from raw data var peptideSettingsDlg = ShowDialog <PeptideSettingsUI>( () => SkylineWindow.ShowPeptideSettingsUI(PeptideSettingsUI.TABS.Prediction)); // Simulate user picking Edit Current from the Drift Time Predictor combo control var driftTimePredictorDlg = ShowDialog <EditDriftTimePredictorDlg>(peptideSettingsDlg.EditDriftTimePredictor); RunUI(() => { driftTimePredictorDlg.SetOffsetHighEnergySpectraCheckbox(true); driftTimePredictorDlg.GetDriftTimesFromResults(); driftTimePredictorDlg.OkDialog(true); // Force overwrite if a named predictor already exists }); WaitForClosedForm(driftTimePredictorDlg); RunUI(() => { peptideSettingsDlg.OkDialog(); }); WaitForClosedForm(peptideSettingsDlg); document = SkylineWindow.Document; measuredDTs.Add(document.Settings.PeptideSettings.Prediction.IonMobilityPredictor.MeasuredMobilityIons); var count = 0; foreach (var key in curatedDTs.Keys) { if (precursors.ItemsMatching(key.LibraryKey, true).Any()) { count++; Assert.AreNotEqual(curatedDTs[key].IonMobility.Mobility, measuredDTs[pass][key].IonMobility.Mobility, "measured drift time should differ somewhat for " + key); } Assert.AreEqual(curatedDTs[key].IonMobility.Mobility.Value, measuredDTs[pass][key].IonMobility.Mobility.Value, 1.0, "measured drift time differs too much for " + key); Assert.AreEqual(curatedDTs[key].HighEnergyIonMobilityValueOffset, measuredDTs[pass][key].HighEnergyIonMobilityValueOffset, 2.0, "measured drift time high energy offset differs too much for " + key); } Assert.AreEqual(document.MoleculeTransitionGroupCount, count, "did not find drift times for all precursors"); // Expect to find a value for each precursor if (pass == 1) { break; } // Verify that we select based on strongest results by removing the training set and relying on the other noisier set RunDlg <ManageResultsDlg>(SkylineWindow.ManageResults, dlg => { var chromatograms = document.Settings.MeasuredResults.Chromatograms; var chromTraining = chromatograms[0]; var chroms = new[] { chromTraining }; dlg.SelectedChromatograms = chroms; // Passing this chromatograms[0] results in the other set being deleted - compiler problem? dlg.RemoveReplicates(); dlg.OkDialog(); }); document = WaitForDocumentChange(document); Assert.AreEqual(1, document.Settings.MeasuredResults.Chromatograms.Count); } // Results should be slightly different without the training set of chromatograms to contain a potentially stronger peak var ccount = 0; var noChange = new List <LibKey>(); foreach (var key in measuredDTs[0].Keys) { if (precursors.ItemsMatching(key, true).Any()) { ccount++; if (measuredDTs[0][key].GetHighEnergyDriftTimeMsec().Value == measuredDTs[1][key].GetHighEnergyDriftTimeMsec().Value) { noChange.Add(key); } } Assert.AreEqual(measuredDTs[0][key].IonMobility.Mobility.Value, measuredDTs[1][key].IonMobility.Mobility.Value, 1.0, "averaged measured drift time differs for " + key); Assert.AreEqual(measuredDTs[0][key].HighEnergyIonMobilityValueOffset, measuredDTs[1][key].HighEnergyIonMobilityValueOffset, 2.0, "averaged measured drift time high energy offset differs for " + key); Assert.AreEqual(measuredDTs[0][key].CollisionalCrossSectionSqA.Value, measuredDTs[1][key].CollisionalCrossSectionSqA.Value, 1.0, "averaged measured CCS differs for " + key); } Assert.AreEqual(document.MoleculeTransitionGroupCount, ccount, "did not find drift times for all precursors"); // Expect to find a value for each precursor Assert.IsTrue(noChange.Count < ccount / 2, "expected most values to shift a little without the nice clean training data"); // And finally verify ability to reimport with altered drift filter (would formerly fail on an erroneous Assume) // Simulate user picking Edit Current from the Drift Time Predictor combo control, and messing with all the measured drift time values var peptideSettingsDlg2 = ShowDialog <PeptideSettingsUI>( () => SkylineWindow.ShowPeptideSettingsUI(PeptideSettingsUI.TABS.Prediction)); var driftTimePredictorDlg2 = ShowDialog <EditDriftTimePredictorDlg>(peptideSettingsDlg2.EditDriftTimePredictor); RunUI(() => { var oldPredictor = driftTimePredictorDlg2.Predictor; var dict = new Dictionary <LibKey, IonMobilityAndCCS>(); foreach (var entry in oldPredictor.MeasuredMobilityIons) { dict.Add(entry.Key, entry.Value.ChangeIonMobilityValue(entry.Value.IonMobility.ChangeIonMobility(entry.Value.IonMobility.Mobility ?? 0 + .5))); } driftTimePredictorDlg2.Predictor = oldPredictor.ChangeMeasuredMobilityIons(dict).ChangeName("test") as IonMobilityPredictor; driftTimePredictorDlg2.OkDialog(true); // Force overwrite if a named predictor already exists }); WaitForClosedForm(driftTimePredictorDlg2); RunUI(() => { peptideSettingsDlg2.OkDialog(); }); WaitForClosedForm(peptideSettingsDlg2); var docChangedDriftTimePredictor = WaitForDocumentChange(document); // Reimport data for a replicate - without the fix this will throw RunDlg <ManageResultsDlg>(SkylineWindow.ManageResults, dlg => { var chromatograms = docChangedDriftTimePredictor.Settings.MeasuredResults.Chromatograms; dlg.SelectedChromatograms = new[] { chromatograms[0] }; dlg.ReimportResults(); dlg.OkDialog(); }); WaitForDocumentChangeLoaded(docChangedDriftTimePredictor, WAIT_TIME * 2); }
public static OptimizationDb ConvertFromOldFormat(string path, IProgressMonitor loadMonitor, ProgressStatus status, SrmDocument document) { // Try to open assuming old format (Id, PeptideModSeq, Charge, Mz, Value, Type) var precursors = new Dictionary <Target, HashSet <int> >(); // PeptideModSeq -> charges var optimizations = new List <Tuple <DbOptimization, double> >(); // DbOptimization, product m/z int maxCharge = 1; using (SQLiteConnection connection = new SQLiteConnection(@"Data Source = " + path)) using (SQLiteCommand command = new SQLiteCommand(connection)) { connection.Open(); command.CommandText = @"SELECT PeptideModSeq, Charge, Mz, Value, Type FROM OptimizationLibrary"; using (SQLiteDataReader reader = command.ExecuteReader()) { while (reader.Read()) { var type = (OptimizationType)reader[@"Type"]; var modifiedSequence = new Target(reader[@"PeptideModSeq"].ToString()); var charge = (int)reader[@"Charge"]; var productMz = (double)reader[@"Mz"]; var value = (double)reader[@"Value"]; optimizations.Add(new Tuple <DbOptimization, double>(new DbOptimization(type, modifiedSequence, Adduct.FromChargeProtonated(charge), string.Empty, Adduct.EMPTY, value), productMz)); if (!precursors.ContainsKey(modifiedSequence)) { precursors[modifiedSequence] = new HashSet <int>(); } precursors[modifiedSequence].Add(charge); if (charge > maxCharge) { maxCharge = charge; } } } } var peptideList = (from precursor in precursors from charge in precursor.Value select string.Format(@"{0}{1}", precursor.Key, Transition.GetChargeIndicator(Adduct.FromChargeProtonated(charge))) ).ToList(); var newDoc = new SrmDocument(document != null ? document.Settings : SrmSettingsList.GetDefault()); newDoc = newDoc.ChangeSettings(newDoc.Settings .ChangePeptideLibraries(libs => libs.ChangePick(PeptidePick.filter)) .ChangeTransitionFilter(filter => filter.ChangeFragmentRangeFirstName(@"ion 1") .ChangeFragmentRangeLastName(@"last ion") .ChangePeptideProductCharges(Enumerable.Range(1, maxCharge).Select(Adduct.FromChargeProtonated).ToList()) // TODO(bspratt) negative charge peptides .ChangePeptideIonTypes(new [] { IonType.y, IonType.b })) // TODO(bspratt) generalize to molecules? .ChangeTransitionLibraries(libs => libs.ChangePick(TransitionLibraryPick.none)) ); var matcher = new ModificationMatcher(); matcher.CreateMatches(newDoc.Settings, peptideList, Settings.Default.StaticModList, Settings.Default.HeavyModList); FastaImporter importer = new FastaImporter(newDoc, matcher); // ReSharper disable LocalizableElement string text = string.Format(">>{0}\r\n{1}", newDoc.GetPeptideGroupId(true), TextUtil.LineSeparate(peptideList)); // ReSharper restore LocalizableElement PeptideGroupDocNode imported = importer.Import(new StringReader(text), null, Helpers.CountLinesInString(text)).First(); int optimizationsUpdated = 0; foreach (PeptideDocNode nodePep in imported.Children) { foreach (var nodeGroup in nodePep.TransitionGroups) { var charge = nodeGroup.PrecursorAdduct; var libKeyToMatch = newDoc.Settings.GetSourceTarget(nodePep).GetLibKey(charge).LibraryKey; foreach (var nodeTran in nodeGroup.Transitions) { double productMz = nodeTran.Mz; foreach (var optimization in optimizations.Where(opt => string.IsNullOrEmpty(opt.Item1.FragmentIon) && opt.Item1.ProductAdduct.IsEmpty && Math.Abs(opt.Item2 - productMz) < 0.00001)) { var optLibKey = optimization.Item1.Target.GetLibKey(optimization.Item1.Adduct).LibraryKey; if (!LibKeyIndex.KeysMatch(optLibKey, libKeyToMatch)) { continue; } optimization.Item1.FragmentIon = nodeTran.FragmentIonName; optimization.Item1.ProductAdduct = nodeTran.Transition.Adduct; ++optimizationsUpdated; } } } } if (optimizations.Count > optimizationsUpdated) { throw new OptimizationsOpeningException(string.Format(Resources.OptimizationDb_ConvertFromOldFormat_Failed_to_convert__0__optimizations_to_new_format_, optimizations.Count - optimizationsUpdated)); } using (var fs = new FileSaver(path)) { OptimizationDb db = CreateOptimizationDb(fs.SafeName); db.UpdateOptimizations(optimizations.Select(opt => opt.Item1).ToArray(), new DbOptimization[0]); fs.Commit(); if (loadMonitor != null) { loadMonitor.UpdateProgress(status.ChangePercentComplete(100)); } return(GetOptimizationDb(fs.RealName, null, null)); } }
protected override void DoTest() { // IsPauseForScreenShots = true; // For a quick demo when you need it string skyFile = TestFilesDir.GetTestPath("test_measured_drift_times_perf.sky"); Program.ExtraRawFileSearchFolder = TestFilesDir.PersistentFilesDir; // So we don't have to reload the raw files, which have moved relative to skyd file RunUI(() => SkylineWindow.OpenFile(skyFile)); var document = WaitForDocumentLoaded(240000); // If it decides to remake chromatograms this can take awhile AssertEx.IsDocumentState(document, null, 1, 34, 38, 398); RunUI(() => { SkylineWindow.SaveDocument(TestFilesDir.GetTestPath("local.sky")); // Avoid "document changed since last edit" message document = SkylineWindow.DocumentUI; }); List <ValidatingIonMobilityPrecursor> curatedDTs = null; var measuredDTs = new List <List <ValidatingIonMobilityPrecursor> >(); var precursors = new LibKeyIndex(document.MoleculePrecursorPairs.Select( p => p.NodePep.ModifiedTarget.GetLibKey(p.NodeGroup.PrecursorAdduct).LibraryKey)); PauseForScreenShot(@"Legacy ion mobility values loaded, placed in .imsdb database file"); // For a quick demo when you need it for (var pass = 0; pass < 2; pass++) { // Verify ability to extract predictions from raw data var transitionSettingsDlg = ShowDialog <TransitionSettingsUI>( () => SkylineWindow.ShowTransitionSettingsUI(TransitionSettingsUI.TABS.IonMobility)); PauseForScreenShot("new Transition Settings tab"); // For a quick demo when you need it // Simulate user picking Edit Current from the Ion Mobility Library combo control var ionMobilityLibraryDlg = ShowDialog <EditIonMobilityLibraryDlg>(transitionSettingsDlg.IonMobilityControl.EditIonMobilityLibrary); PauseForScreenShot("next, we'll update values with 'Use Results' button"); // For a quick demo when you need it RunUI(() => { if (curatedDTs == null) { curatedDTs = ionMobilityLibraryDlg.LibraryMobilitiesFlat.ToList(); } ionMobilityLibraryDlg.SetOffsetHighEnergySpectraCheckbox(true); ionMobilityLibraryDlg.GetIonMobilitiesFromResults(); }); PauseForScreenShot("values updated");// For a quick demo when you need it RunUI(() => { measuredDTs.Add(ionMobilityLibraryDlg.LibraryMobilitiesFlat.ToList()); ionMobilityLibraryDlg.OkDialog(); }); WaitForClosedForm(ionMobilityLibraryDlg); RunUI(() => { transitionSettingsDlg.OkDialog(); }); WaitForClosedForm(transitionSettingsDlg); document = SkylineWindow.Document; var count = 0; for (var n = 0; n < curatedDTs.Count; n++) { var cdt = curatedDTs[n]; var key = cdt.Precursor; var indexM = measuredDTs[pass].FindIndex(m => m.Precursor.Equals(key)); var measured = measuredDTs[pass][indexM]; var measuredDT = measured.IonMobility; var measuredHEO = measured.HighEnergyIonMobilityOffset; if (precursors.ItemsMatching(key, true).Any()) { count++; AssertEx.AreNotEqual(cdt.IonMobility, measuredDT, "measured drift time should differ somewhat for " + measured.Precursor); } AssertEx.AreEqual(cdt.IonMobility, measuredDT, 1.0, "measured drift time differs too much for " + key); AssertEx.AreEqual(cdt.HighEnergyIonMobilityOffset, measuredHEO, 2.0, "measured drift time high energy offset differs too much for " + key); } AssertEx.AreEqual(document.MoleculeTransitionGroupCount, count, "did not find drift times for all precursors"); // Expect to find a value for each precursor if (pass == 1) { break; } // Verify that we select based on strongest results by removing the training set and relying on the other noisier set RunDlg <ManageResultsDlg>(SkylineWindow.ManageResults, dlg => { var chromatograms = document.Settings.MeasuredResults.Chromatograms; var chromTraining = chromatograms[0]; var chroms = new[] { chromTraining }; dlg.SelectedChromatograms = chroms; // Passing this chromatograms[0] results in the other set being deleted - compiler problem? dlg.RemoveReplicates(); dlg.OkDialog(); }); document = WaitForDocumentChange(document); AssertEx.AreEqual(1, document.Settings.MeasuredResults.Chromatograms.Count); } // Results should be slightly different without the training set of chromatograms to contain a potentially stronger peak var ccount = 0; var noChange = new List <LibKey>(); for (var n = 0; n < measuredDTs[0].Count; n++) { var validatingIonMobilityPeptide0 = measuredDTs[0][n]; var validatingIonMobilityPeptide1 = measuredDTs[1][n]; var key = measuredDTs[0][n].Precursor; if (precursors.ItemsMatching(key, true).Any()) { ccount++; if (validatingIonMobilityPeptide0.HighEnergyIonMobilityOffset == validatingIonMobilityPeptide1.HighEnergyIonMobilityOffset) { noChange.Add(key); } } AssertEx.AreEqual(validatingIonMobilityPeptide0.IonMobility, validatingIonMobilityPeptide1.IonMobility, 1.0, "averaged measured drift time differs for " + key); AssertEx.AreEqual(validatingIonMobilityPeptide0.HighEnergyIonMobilityOffset, validatingIonMobilityPeptide1.HighEnergyIonMobilityOffset, 2.0, "averaged measured drift time high energy offset differs for " + key); AssertEx.AreEqual(validatingIonMobilityPeptide0.CollisionalCrossSectionSqA, validatingIonMobilityPeptide1.CollisionalCrossSectionSqA, 1.0, "averaged measured CCS differs for " + key); } AssertEx.AreEqual(document.MoleculeTransitionGroupCount, ccount, "did not find drift times for all precursors"); // Expect to find a value for each precursor AssertEx.IsTrue(noChange.Count < ccount / 2, "expected most values to shift a little without the nice clean training data"); // And finally verify ability to reimport with altered drift filter (would formerly fail on an erroneous Assume) // Simulate user picking Edit Current from the Ion Mobility Library combo control, and messing with all the measured drift time values var transitionSettingsDlg2 = ShowDialog <TransitionSettingsUI>( () => SkylineWindow.ShowTransitionSettingsUI(TransitionSettingsUI.TABS.Prediction)); var editIonMobilityLibraryDlg = ShowDialog <EditIonMobilityLibraryDlg>(transitionSettingsDlg2.IonMobilityControl.EditIonMobilityLibrary); RunUI(() => { var revised = new List <ValidatingIonMobilityPrecursor>(); foreach (var item in editIonMobilityLibraryDlg.LibraryMobilitiesFlat) { var im = item.IonMobility; var heo = item.HighEnergyIonMobilityOffset; revised.Add(new ValidatingIonMobilityPrecursor(item.Precursor, IonMobilityAndCCS.GetIonMobilityAndCCS(IonMobilityValue.GetIonMobilityValue(im * 1.02, item.IonMobilityUnits), item.CollisionalCrossSectionSqA * 1.02, heo * 1.02))); } editIonMobilityLibraryDlg.LibraryMobilitiesFlat = revised; editIonMobilityLibraryDlg.OkDialog(); }); WaitForClosedForm(editIonMobilityLibraryDlg); RunUI(() => { transitionSettingsDlg2.OkDialog(); }); WaitForClosedForm(transitionSettingsDlg2); var docChangedDriftTimePredictor = WaitForDocumentChange(document); // Reimport data for a replicate - without the fix this will throw RunDlg <ManageResultsDlg>(SkylineWindow.ManageResults, dlg => { var chromatograms = docChangedDriftTimePredictor.Settings.MeasuredResults.Chromatograms; dlg.SelectedChromatograms = new[] { chromatograms[0] }; dlg.ReimportResults(); dlg.OkDialog(); }); WaitForDocumentChangeLoaded(docChangedDriftTimePredictor, WAIT_TIME * 2); }