/// <summary>
        /// Write XML representation, possibly in backward compatible format using
        /// old style in-document serialization
        /// </summary>
        public void WriteXml(XmlWriter writer, IonMobilityWindowWidthCalculator extraInfoForPre20_12)
        {
            if (extraInfoForPre20_12 == null)
            {
                WriteXml(writer);
                return;
            }

            // Write the contents of the currently-in-use .imsdb to old style in-document serialization
            var libKeyMap = GetIonMobilityLibKeyMap();

            if (libKeyMap == null)
            {
                return;
            }
            var dict = libKeyMap.AsDictionary();

            if (dict != null && dict.Any())
            {
                var oldDict =
                    dict.ToDictionary(kvp => kvp.Key,
                                      kvp => kvp.Value.First()); // No multiple conformers in earlier formats
                var dtp = new DriftTimePredictor(Name,
                                                 oldDict, extraInfoForPre20_12.WindowWidthMode, extraInfoForPre20_12.ResolvingPower,
                                                 extraInfoForPre20_12.PeakWidthAtIonMobilityValueZero,
                                                 extraInfoForPre20_12.PeakWidthAtIonMobilityValueMax,
                                                 extraInfoForPre20_12.FixedWindowWidth);
                writer.WriteStartElement(DriftTimePredictor.EL.predict_drift_time); // N.B. EL.predict_drift_time is a misnomer, this covers all IMS types
                dtp.WriteXml(writer);
                writer.WriteEndElement();
            }
        }
        public PeptidePrediction ValidateNewSettings(bool showMessages)
        {
            var helper = new MessageBoxHelper(ParentForm, showMessages);


            bool useLibraryDriftTime = cbUseSpectralLibraryIonMobilityValues.Checked;

            var libraryDriftTimeWindowWidthCalculator = IonMobilityWindowWidthCalculator.EMPTY;

            if (useLibraryDriftTime)
            {
                double resolvingPower = 0;
                double widthAtDt0     = 0;
                double widthAtDtMax   = 0;
                IonMobilityWindowWidthCalculator.IonMobilityPeakWidthType peakWidthType;
                if (cbLinear.Checked)
                {
                    if (!helper.ValidateDecimalTextBox(textSpectralLibraryIonMobilityWindowWidthAtDt0, out widthAtDt0))
                    {
                        return(null);
                    }
                    if (!helper.ValidateDecimalTextBox(textSpectralLibraryIonMobilityWindowWidthAtDtMax, out widthAtDtMax))
                    {
                        return(null);
                    }
                    var errmsg = EditDriftTimePredictorDlg.ValidateWidth(widthAtDt0);
                    if (errmsg != null)
                    {
                        helper.ShowTextBoxError(textSpectralLibraryIonMobilityWindowWidthAtDt0, errmsg);
                        return(null);
                    }
                    errmsg = EditDriftTimePredictorDlg.ValidateWidth(widthAtDtMax);
                    if (errmsg != null)
                    {
                        helper.ShowTextBoxError(textSpectralLibraryIonMobilityWindowWidthAtDtMax, errmsg);
                        return(null);
                    }
                    peakWidthType = IonMobilityWindowWidthCalculator.IonMobilityPeakWidthType.linear_range;
                }
                else
                {
                    if (!helper.ValidateDecimalTextBox(textSpectralLibraryIonMobilityValuesResolvingPower, out resolvingPower))
                    {
                        return(null);
                    }
                    var errmsg = EditDriftTimePredictorDlg.ValidateResolvingPower(resolvingPower);
                    if (errmsg != null)
                    {
                        helper.ShowTextBoxError(textSpectralLibraryIonMobilityValuesResolvingPower, errmsg);
                        return(null);
                    }
                    peakWidthType = IonMobilityWindowWidthCalculator.IonMobilityPeakWidthType.resolving_power;
                }
                libraryDriftTimeWindowWidthCalculator = new IonMobilityWindowWidthCalculator(peakWidthType, resolvingPower, widthAtDt0, widthAtDtMax);
            }

            return(Prediction.ChangeLibraryDriftTimesWindowWidthCalculator(libraryDriftTimeWindowWidthCalculator).ChangeUseLibraryIonMobilityValues(useLibraryDriftTime));
        }
 public DriftTimePredictor(string name,
                           IDictionary <LibKey, IonMobilityAndCCS> measuredMobilityIons,
                           IonMobilityWindowWidthCalculator.IonMobilityWindowWidthType windowWidthMode,
                           double resolvingPower,
                           double widthAtIonMobilityZero, double widthAtIonMobilityMax,
                           double fixedPeakWidth)
     : base(name)
 {
     _windowWidthCalculator = new IonMobilityWindowWidthCalculator(windowWidthMode,
                                                                   resolvingPower, widthAtIonMobilityZero, widthAtIonMobilityMax, fixedPeakWidth);
     MeasuredMobilityIons = measuredMobilityIons;
     Validate();
 }
Exemple #4
0
        private static double driftTimeMax = 13.799765403988133; // Known max drift time for this file - use to mimic resolving power logic for test purposes

        private static SrmDocument InitWatersImsMseDocument(TestFilesDir testFilesDir,
                                                            IonMobilityWindowWidthCalculator.IonMobilityPeakWidthType driftPeakWidthCalcType,
                                                            bool withDriftTimeFilter, bool withDriftTimePredictor,
                                                            out string docPath)
        {
            var skyFile = withDriftTimePredictor ? "single_with_driftinfo.sky" : "single_no_driftinfo.sky";

            docPath = testFilesDir.GetTestPath(skyFile);
            var cmdline = new CommandLine();

            Assert.IsTrue(cmdline.OpenSkyFile(docPath)); // Handles any path shifts in database files, like our .imdb file
            SrmDocument doc = cmdline.Document;

            // Cause library load and subsequent document update
            using (var docContainer = new ResultsTestDocumentContainer(null, docPath))
            {
                docContainer.SetDocument(doc, null, true);
                docContainer.AssertComplete();
                doc = docContainer.Document;

                double resolvingPower = 100; // Test was originally written with resolving power 100
                double widthAtDtMax   = 2 * driftTimeMax / resolvingPower;
                var    driftTimeWindowWidthCalculator = new IonMobilityWindowWidthCalculator(driftPeakWidthCalcType, resolvingPower, 0, widthAtDtMax);

                if (withDriftTimeFilter && !withDriftTimePredictor)
                {
                    // Use the bare drift times in the spectral library
                    var librarySpec = new BiblioSpecLiteSpec("drift test",
                                                             testFilesDir.GetTestPath("waters-mobility.filtered-scaled.blib"));
                    doc = doc.ChangeSettings(
                        doc.Settings.ChangePeptideLibraries(lib => lib.ChangeLibrarySpecs(new[] { librarySpec })).
                        ChangePeptidePrediction(p => p.ChangeLibraryDriftTimesWindowWidthCalculator(driftTimeWindowWidthCalculator)).
                        ChangePeptidePrediction(p => p.ChangeUseLibraryIonMobilityValues(true))
                        );
                }
                else if (withDriftTimeFilter)
                {
                    doc = doc.ChangeSettings(
                        doc.Settings.ChangePeptideSettings(ps => ps.ChangePrediction(
                                                               ps.Prediction.ChangeDriftTimePredictor(ps.Prediction.IonMobilityPredictor.ChangeDriftTimeWindowWidthCalculator(driftTimeWindowWidthCalculator)))));
                }
            }
            return(doc);
        }
        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 TransitionIonMobilityFiltering CreateTransitionIonMobilityFiltering(string dbDir)
        {
            // Create .imsdb library from MeasuredMobilityIons, for backward compatibility
            var oldDict = MeasuredMobilityIons;
            var dict    = oldDict == null
                ? null
                : oldDict.ToDictionary(kvp => kvp.Key,
                                       kvp => new List <IonMobilityAndCCS>()
            {
                kvp.Value
            });

            if ((dict == null || dict.Count == 0) && IonMobilityWindowWidthCalculator.IsNullOrEmpty(_windowWidthCalculator))
            {
                return(TransitionIonMobilityFiltering.EMPTY);
            }
            var val = new TransitionIonMobilityFiltering(Name, dbDir, dict,
                                                         true,
                                                         _windowWidthCalculator);

            return(val.ChangeLibrary(val.IonMobilityLibrary.Initialize(null)));
        }
Exemple #7
0
        private TransitionIonMobilityFiltering ValidateIonMobilitySettings(MessageBoxHelper helper)
        {
            var IonMobilityLibrary = GetSelectedIonMobilityLibrary();

            var useSpectralLibraryIonMobilities = cbUseSpectralLibraryIonMobilities.Checked;

            var peakWidthType = ShowPeakWidthTypeControl ?
                                (IonMobilityWindowWidthCalculator.IonMobilityWindowWidthType)comboBoxWindowType.SelectedIndex :
                                IonMobilityWindowWidthCalculator.IonMobilityWindowWidthType.resolving_power;
            double resolvingPower = 0;
            double widthAtDt0     = 0;
            double widthAtDtMax   = 0;
            double fixedPeakWidth = 0;

            string errmsg;

            switch (peakWidthType)
            {
            case IonMobilityWindowWidthCalculator.IonMobilityWindowWidthType.linear_range:
                if (!helper.ValidateDecimalTextBox(textIonMobilityFilterWidthAtMobility0, out widthAtDt0))
                {
                    return(null);
                }
                if (!helper.ValidateDecimalTextBox(textIonMobilityFilterWidthAtMobilityMax, out widthAtDtMax))
                {
                    return(null);
                }
                errmsg = ValidateWidth(widthAtDt0);
                if (errmsg != null)
                {
                    helper.ShowTextBoxError(textIonMobilityFilterWidthAtMobility0, errmsg);
                    return(null);
                }

                errmsg = ValidateWidth(widthAtDtMax);
                if (errmsg != null)
                {
                    helper.ShowTextBoxError(textIonMobilityFilterWidthAtMobilityMax, errmsg);
                    return(null);
                }
                break;

            case IonMobilityWindowWidthCalculator.IonMobilityWindowWidthType.resolving_power:
                if (!helper.ValidateDecimalTextBox(textIonMobilityFilterResolvingPower, out resolvingPower))
                {
                    return(null);
                }
                errmsg = ValidateResolvingPower(resolvingPower);
                if (errmsg != null)
                {
                    helper.ShowTextBoxError(textIonMobilityFilterResolvingPower, errmsg);
                    return(null);
                }
                break;

            case IonMobilityWindowWidthCalculator.IonMobilityWindowWidthType.fixed_width:     // Fixed width
                if (!helper.ValidateDecimalTextBox(textIonMobilityFilterFixedWidth, out fixedPeakWidth))
                {
                    return(null);
                }
                errmsg = ValidateFixedWindow(fixedPeakWidth);
                if (errmsg != null)
                {
                    helper.ShowTextBoxError(textIonMobilityFilterFixedWidth, errmsg);
                    return(null);
                }
                break;
            }

            var ionMobilityWindowWidthCalculator =
                new IonMobilityWindowWidthCalculator(peakWidthType, resolvingPower, widthAtDt0, widthAtDtMax, fixedPeakWidth);

            return(new TransitionIonMobilityFiltering(IonMobilityLibrary, useSpectralLibraryIonMobilities, ionMobilityWindowWidthCalculator));
        }
        private void PerformTestMeasuredDriftValues(bool asSmallMolecules)
        {
            if (asSmallMolecules)
            {
                if (!RunSmallMoleculeTestVersions)
                {
                    Console.Write(MSG_SKIPPING_SMALLMOLECULE_TEST_VERSION);
                    return;
                }
            }
            var testFilesDir = new TestFilesDir(TestContext, @"TestData\Results\BlibDriftTimeTest.zip"); // Re-used from BlibDriftTimeTest
            // Open document with some peptides but no results
            var docPath = testFilesDir.GetTestPath("BlibDriftTimeTest.sky");
            // This was a malformed document, which caused problems after a fix to not recalculate
            // document library settings on open. To avoid rewriting this test for the document
            // which now contains 2 precursors, the first precursor is removed immediately.
            SrmDocument docOriginal      = ResultsUtil.DeserializeDocument(docPath);
            var         pathFirstPeptide = docOriginal.GetPathTo((int)SrmDocument.Level.Molecules, 0);
            var         nodeFirstPeptide = (DocNodeParent)docOriginal.FindNode(pathFirstPeptide);

            docOriginal = (SrmDocument)docOriginal.RemoveChild(pathFirstPeptide, nodeFirstPeptide.Children[0]);
            if (asSmallMolecules)
            {
                var refine = new RefinementSettings();
                docOriginal = refine.ConvertToSmallMolecules(docOriginal, testFilesDir.FullPath);
            }
            using (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" + ExtensionTestContext.ExtMz5)), }),
                };
                var docResults = doc.ChangeMeasuredResults(new MeasuredResults(chromSets));
                Assert.IsTrue(docContainer.SetDocument(docResults, docOriginal, true));
                docContainer.AssertComplete();
                var document = docContainer.Document;

                // Clear out any current settings
                document = document.ChangeSettings(document.Settings.ChangeTransitionIonMobilityFiltering(s => TransitionIonMobilityFiltering.EMPTY));

                // Verify ability to extract predictions from raw data
                var libraryName0   = "test0";
                var dbPath0        = testFilesDir.GetTestPath(libraryName0 + IonMobilityDb.EXT);
                var newIMFiltering = document.Settings.TransitionSettings.IonMobilityFiltering.ChangeLibrary(
                    IonMobilityLibrary.CreateFromResults(
                        document, docContainer.DocumentFilePath, true,
                        libraryName0, dbPath0));
                var result = newIMFiltering.IonMobilityLibrary.GetIonMobilityLibKeyMap().AsDictionary();
                Assert.AreEqual(1, result.Count);
                var expectedDT     = 4.0019;
                var expectedOffset = .4829;
                Assert.AreEqual(expectedDT, result.Values.First().First().IonMobility.Mobility.Value, .001);
                Assert.AreEqual(expectedOffset, result.Values.First().First().HighEnergyIonMobilityValueOffset ?? 0, .001);

                // Check ability to update, and to preserve unchanged
                var revised = new List <PrecursorIonMobilities>();
                var libKey  = result.Keys.First();
                revised.Add(new PrecursorIonMobilities(libKey, IonMobilityAndCCS.GetIonMobilityAndCCS(IonMobilityValue.GetIonMobilityValue(expectedDT = 4, eIonMobilityUnits.drift_time_msec), null, expectedOffset = 0.234)));  // N.B. CCS handling would require actual raw data in this test, it's covered in a perf test
                var pepSequence = "DEADEELS";
                var libKey2     = asSmallMolecules ?
                                  new LibKey(SmallMoleculeLibraryAttributes.Create(pepSequence, "C12H5", null, null, null, null), Adduct.M_PLUS_2H) :
                                  new LibKey(pepSequence, Adduct.DOUBLY_PROTONATED);
                revised.Add(new PrecursorIonMobilities(libKey2, IonMobilityAndCCS.GetIonMobilityAndCCS(IonMobilityValue.GetIonMobilityValue(5, eIonMobilityUnits.drift_time_msec), null, 0.123)));
                var libraryName = "test";
                var dbPath      = testFilesDir.GetTestPath(libraryName + IonMobilityDb.EXT);
                var imsdb       = IonMobilityDb.CreateIonMobilityDb(dbPath, libraryName, false, revised);
                var newLibIM    = new IonMobilityLibrary(libraryName, dbPath, imsdb);
                var ionMobilityWindowWidthCalculator = new IonMobilityWindowWidthCalculator(
                    IonMobilityWindowWidthCalculator.IonMobilityWindowWidthType.resolving_power, 40, 0, 0, 0);
                var calculator = ionMobilityWindowWidthCalculator;
                document = document.ChangeSettings(
                    document.Settings.ChangeTransitionIonMobilityFiltering(
                        imf => imf.ChangeFilterWindowWidthCalculator(calculator).
                        ChangeLibrary(newLibIM)));
                result = document.Settings.TransitionSettings.IonMobilityFiltering.IonMobilityLibrary.GetIonMobilityLibKeyMap().AsDictionary();
                Assert.AreEqual(2, result.Count);
                Assert.AreEqual(expectedDT, result[libKey].First().IonMobility.Mobility.Value, .001);
                Assert.AreEqual(expectedOffset, result[libKey].First().HighEnergyIonMobilityValueOffset ?? 0, .001);
                Assert.AreEqual(5, result[libKey2].First().IonMobility.Mobility.Value, .001);
                Assert.AreEqual(0.123, result[libKey2].First().HighEnergyIonMobilityValueOffset ?? 0, .001);

                ionMobilityWindowWidthCalculator = new IonMobilityWindowWidthCalculator(
                    IonMobilityWindowWidthCalculator.IonMobilityWindowWidthType.fixed_width, 40, 0, 0, 100);
                document = document.ChangeSettings(
                    document.Settings.ChangeTransitionIonMobilityFiltering(
                        imf => imf.ChangeFilterWindowWidthCalculator(ionMobilityWindowWidthCalculator).
                        ChangeLibrary(newLibIM)));
                result = document.Settings.TransitionSettings.IonMobilityFiltering.IonMobilityLibrary.GetIonMobilityLibKeyMap().AsDictionary();
                Assert.AreEqual(2, result.Count);
                Assert.AreEqual(expectedDT, result[libKey].First().IonMobility.Mobility.Value, .001);
                Assert.AreEqual(expectedOffset, result[libKey].First().HighEnergyIonMobilityValueOffset ?? 0, .001);
                Assert.AreEqual(5, result[libKey2].First().IonMobility.Mobility.Value, .001);
                Assert.AreEqual(0.123, result[libKey2].First().HighEnergyIonMobilityValueOffset ?? 0, .001);
            }
        }