Пример #1
0
        private double?GetLoq(FiguresOfMeritOptions options, Skyline.Model.Databinding.Entities.Peptide peptideEntity)
        {
            var peptideResults = peptideEntity.Results.Values
                                 .Where(result => Equals(result.ResultFile.Replicate.SampleType, SampleType.STANDARD) &&
                                        result.ResultFile.Replicate.AnalyteConcentration.HasValue)
                                 .ToLookup(result => result.ResultFile.Replicate.AnalyteConcentration.Value);

            if (!options.MaxLoqBias.HasValue && !options.MaxLoqCv.HasValue)
            {
                return(null);
            }
            var    calibrationCurve        = peptideEntity.CalibrationCurve.Value;
            var    concentrationMultiplier = peptideEntity.ConcentrationMultiplier.GetValueOrDefault(1);
            double?bestLoq = null;

            foreach (var grouping in peptideResults.OrderByDescending(g => g.Key))
            {
                if (options.MaxLoqBias.HasValue)
                {
                    var areas = grouping
                                .Select(peptideResult => peptideResult.Quantification.Value.NormalizedArea)
                                .Where(area => area.HasValue).Cast <double>().ToArray();
                    if (areas.Length == 0)
                    {
                        continue;
                    }
                    var meanArea = areas.Average();
                    var backCalculatedConcentration = calibrationCurve.GetFittedX(meanArea);
                    if (!backCalculatedConcentration.HasValue)
                    {
                        break;
                    }
                    var expectedConcentration = grouping.Key * concentrationMultiplier;
                    var error = Math.Abs(1.0 - backCalculatedConcentration.Value / expectedConcentration) * 100;
                    if (error > options.MaxLoqBias)
                    {
                        break;
                    }
                }

                if (options.MaxLoqCv.HasValue)
                {
                    var stats = new Statistics(grouping.Select(peptideResult =>
                                                               peptideResult.Quantification.Value.NormalizedArea).OfType <double>());
                    if (stats.Length > 1)
                    {
                        var cv = stats.StdDev() / stats.Mean();
                        if (double.IsNaN(cv) || cv * 100 > options.MaxLoqCv.Value)
                        {
                            break;
                        }
                    }
                }

                bestLoq = grouping.Key;
            }
            return(bestLoq * concentrationMultiplier);
        }
Пример #2
0
        private void VerifyFiguresOfMeritValues(FiguresOfMeritOptions options,
                                                Skyline.Model.Databinding.Entities.Peptide peptideEntity)
        {
            double?expectedLoq = GetLoq(options, peptideEntity);
            var    actualLoq   = peptideEntity.FiguresOfMerit.LimitOfQuantification;

            if (expectedLoq != actualLoq)
            {
                Assert.AreEqual(expectedLoq, actualLoq);
            }
        }
Пример #3
0
        protected override void DoTest()
        {
            RunUI(() =>
            {
                SkylineWindow.OpenFile(TestFilesDir.GetTestPath("p180test_calibration_DukeApril2016.sky"));
                SkylineWindow.ShowDocumentGrid(true);
            });
            WaitForDocumentLoaded();

            var documentGrid = FindOpenForm <DocumentGridForm>();

            RunUI(() => documentGrid.ChooseView("NormalizedAreas"));
            WaitForConditionUI(() => documentGrid.IsComplete);

            // Read the Normalized Area and Calculated Concentration for each peptide from the Document Grid

            // Dictionary of <IdentityPath, Replicate Name> to <Normalized Area, Calculated Concentration>
            var normalizedAreas = new Dictionary <Tuple <IdentityPath, string>, Tuple <double?, double?> >();

            RunUI(() =>
            {
                var colMolecule        = documentGrid.FindColumn(PropertyPath.Root);
                PropertyPath ppResults = PropertyPath.Root
                                         .Property(nameof(Skyline.Model.Databinding.Entities.Peptide.Results)).DictionaryValues();
                var colReplicate = documentGrid.FindColumn(ppResults.Property(nameof(PeptideResult.ResultFile))
                                                           .Property(nameof(ResultFile.Replicate)));

                PropertyPath ppQuantification = ppResults.Property(nameof(PeptideResult.Quantification));
                var colNormalizedArea         =
                    documentGrid.FindColumn(ppQuantification.Property(nameof(QuantificationResult.NormalizedArea)));
                var colCalculatedConcentration =
                    documentGrid.FindColumn(
                        ppQuantification.Property(nameof(QuantificationResult.CalculatedConcentration)));

                for (int rowIndex = 0; rowIndex < documentGrid.RowCount; rowIndex++)
                {
                    var row = documentGrid.DataGridView.Rows[rowIndex];
                    Skyline.Model.Databinding.Entities.Peptide molecule =
                        (Skyline.Model.Databinding.Entities.Peptide)row.Cells[colMolecule.Index].Value;
                    Assert.IsNotNull(molecule);
                    Replicate replicate            = (Replicate)row.Cells[colReplicate.Index].Value;
                    double?normalizedArea          = (double?)row.Cells[colNormalizedArea.Index].Value;
                    double?calculatedConcentration = (double?)row.Cells[colCalculatedConcentration.Index].Value;
                    normalizedAreas.Add(Tuple.Create(molecule.IdentityPath, replicate.Name), Tuple.Create(normalizedArea, calculatedConcentration));
                }
            });

            // Show the PeakAreaCVHistogram graph
            RunUI(() => SkylineWindow.ShowPeakAreaCVHistogram());
            WaitForConditionUI(() => null != FindGraphPane <AreaCVHistogramGraphPane>());
            AreaCVHistogramGraphPane areaCVHistogramGraphPane = FindGraphPane <AreaCVHistogramGraphPane>();

            // Change "Normalize To" to "Calibration Curve"
            RunUI(() => SkylineWindow.NormalizeAreaGraphTo(NormalizeOption.CALIBRATED));
            WaitForConditionUI(() =>
                               areaCVHistogramGraphPane.CurrentData?.GraphSettings?.NormalizeOption == NormalizeOption.CALIBRATED);

            // Verify that the CVs that the histogram is displaying are correct for the calculated concentrations
            var graphData = areaCVHistogramGraphPane.CurrentData;

            foreach (var cvData in graphData.Data)
            {
                var peptideAnnotationPair = cvData.PeptideAnnotationPairs.First();
                var identityPath          = new IdentityPath(peptideAnnotationPair.PeptideGroup.PeptideGroup, peptideAnnotationPair.Peptide.Peptide);
                var entries    = normalizedAreas.Where(entry => Equals(entry.Key.Item1, identityPath)).ToList();
                var statistics = new Statistics(entries.Select(entry => entry.Value.Item2).OfType <double>());
                var cv         = statistics.StdDev() / statistics.Mean();
                var cvBucketed = Math.Floor(cv / graphData.GraphSettings.BinWidth) * graphData.GraphSettings.BinWidth;
                AssertEx.AreEqual(cvBucketed, cvData.CV);
            }

            // Change "Normalize To" to "Default Normalization Method"
            RunUI(() => SkylineWindow.NormalizeAreaGraphTo(NormalizeOption.DEFAULT));
            WaitForConditionUI(() =>
                               areaCVHistogramGraphPane.CurrentData?.GraphSettings?.NormalizeOption == NormalizeOption.DEFAULT);
            graphData = areaCVHistogramGraphPane.CurrentData;

            // Verify that the CVs are correct for the Normalized Area values.
            foreach (var cvData in graphData.Data)
            {
                var peptideAnnotationPair = cvData.PeptideAnnotationPairs.First();
                var identityPath          = new IdentityPath(peptideAnnotationPair.PeptideGroup.PeptideGroup, peptideAnnotationPair.Peptide.Peptide);
                var entries    = normalizedAreas.Where(entry => Equals(entry.Key.Item1, identityPath)).ToList();
                var statistics = new Statistics(entries.Select(entry => entry.Value.Item1).OfType <double>());
                var cv         = statistics.StdDev() / statistics.Mean();
                var cvBucketed = Math.Floor(cv / graphData.GraphSettings.BinWidth) * graphData.GraphSettings.BinWidth;
                AssertEx.AreEqual(cvBucketed, cvData.CV);
            }

            // Show the Peak Area Replicate Comparison graph
            RunUI(() =>
            {
                SkylineWindow.NormalizeAreaGraphTo(NormalizeOption.CALIBRATED);
                SkylineWindow.ShowPeakAreaReplicateComparison();
            });
            var peakAreaGraph = FindGraphPane <AreaReplicateGraphPane>();

            // Make sure that the calculated concentrations displayed in the graph are correct for each molecule in the document
            foreach (var moleculeGroup in SkylineWindow.Document.MoleculeGroups)
            {
                foreach (var molecule in moleculeGroup.Molecules)
                {
                    var idPath = new IdentityPath(moleculeGroup.Id, molecule.Id);
                    RunUI(() => SkylineWindow.SelectedPath = idPath);
                    WaitForGraphs();
                    RunUI(() =>
                    {
                        var curve      = peakAreaGraph.CurveList.First();
                        var textLabels = peakAreaGraph.XAxis.Scale.TextLabels;
                        Assert.IsNotNull(textLabels);
                        Assert.AreEqual(textLabels.Length, curve.Points.Count);
                        Assert.AreEqual(SkylineWindow.Document.Settings.MeasuredResults.Chromatograms.Count, curve.Points.Count);
                        for (int i = 0; i < curve.Points.Count; i++)
                        {
                            var replicateName = SkylineWindow.Document.Settings.MeasuredResults.Chromatograms[i].Name;
                            var value         = curve.Points[i].Y;
                            Tuple <double?, double?> expected;
                            Assert.IsTrue(normalizedAreas.TryGetValue(Tuple.Create(idPath, replicateName), out expected));
                            if (expected.Item2.HasValue && !double.IsInfinity(expected.Item2.Value))
                            {
                                AssertEx.AreEqual(expected.Item2.Value, value);
                            }
                            else
                            {
                                AssertEx.IsTrue(double.IsNaN(value) || Equals(value, PointPairBase.Missing));
                            }
                        }
                    });
                }
            }
        }
Пример #4
0
        protected override void DoTest()
        {
            int seed = (int)DateTime.Now.Ticks;
            // Console.WriteLine("FiguresOfMeritTest: using random seed {0}", seed);
            var random = new Random(seed);

            RunUI(() => SkylineWindow.OpenFile(TestFilesDir.GetTestPath("FiguresOfMeritTest.sky")));
            var documentGrid = ShowDialog <DocumentGridForm>(() => SkylineWindow.ShowDocumentGrid(true));

            RunUI(() =>
            {
                documentGrid.ChooseView("FiguresOfMerit");
            });
            var calibrationForm = ShowDialog <CalibrationForm>(() => SkylineWindow.ShowCalibrationForm());

            Assert.IsNotNull(calibrationForm);
            var results = new List <Tuple <FiguresOfMeritOptions, ModifiedSequence, FiguresOfMerit> >();
            int count   = 0;

            foreach (var options in EnumerateFiguresOfMeritOptions().OrderBy(x => random.Next()).Take(10))
            {
                count++;
                bool doFullTest        = count < 5;
                var  newQuantification = SkylineWindow.Document.Settings.PeptideSettings.Quantification;
                // ReSharper disable once PossibleNullReferenceException
                newQuantification = newQuantification
                                    .ChangeRegressionFit(options.RegressionFit)
                                    .ChangeLodCalculation(options.LodCalculation)
                                    .ChangeMaxLoqCv(options.MaxLoqCv)
                                    .ChangeMaxLoqBias(options.MaxLoqBias);
                if (doFullTest)
                {
                    var peptideSettingsUi = ShowDialog <PeptideSettingsUI>(SkylineWindow.ShowPeptideSettingsUI);
                    RunUI(() =>
                    {
                        peptideSettingsUi.QuantRegressionFit = options.RegressionFit;
                        peptideSettingsUi.QuantLodMethod     = options.LodCalculation;
                        peptideSettingsUi.QuantMaxLoqBias    = options.MaxLoqBias;
                        peptideSettingsUi.QuantMaxLoqCv      = options.MaxLoqCv;
                    });
                    OkDialog(peptideSettingsUi, peptideSettingsUi.OkDialog);
                    if (!Equals(newQuantification, SkylineWindow.Document.Settings.PeptideSettings.Quantification))
                    {
                        Assert.AreEqual(newQuantification, SkylineWindow.Document.Settings.PeptideSettings.Quantification);
                    }
                }
                else
                {
                    RunUI(() =>
                    {
                        SkylineWindow.ModifyDocument("Test changed settings",
                                                     doc => doc.ChangeSettings(doc.Settings.ChangePeptideSettings(
                                                                                   doc.Settings.PeptideSettings.ChangeAbsoluteQuantification(newQuantification))));
                    });
                }
                WaitForConditionUI(() => documentGrid.IsComplete);
                var colPeptideModifiedSequence = documentGrid.DataGridView.Columns.Cast <DataGridViewColumn>()
                                                 .FirstOrDefault(col => col.HeaderText == ColumnCaptions.PeptideModifiedSequence);
                Assert.IsNotNull(colPeptideModifiedSequence);
                var colFiguresOfMerit = documentGrid.DataGridView.Columns.Cast <DataGridViewColumn>()
                                        .FirstOrDefault(col => col.HeaderText == ColumnCaptions.FiguresOfMerit);
                Assert.IsNotNull(colFiguresOfMerit);
                var docContainer = new MemoryDocumentContainer();
                Assert.IsTrue(docContainer.SetDocument(SkylineWindow.Document, docContainer.Document));
                var dataSchema = new SkylineDataSchema(docContainer, SkylineDataSchema.GetLocalizedSchemaLocalizer());
                foreach (var group in SkylineWindow.Document.MoleculeGroups)
                {
                    foreach (var peptide in group.Molecules)
                    {
                        var identityPath  = new IdentityPath(group.Id, peptide.Id);
                        var peptideEntity = new Skyline.Model.Databinding.Entities.Peptide(dataSchema, identityPath);
                        VerifyFiguresOfMeritValues(options, peptideEntity);
                        ValidateFiguresOfMerit(options, peptideEntity.FiguresOfMerit);
                        results.Add(Tuple.Create(options, peptideEntity.ModifiedSequence, peptideEntity.FiguresOfMerit));
                        if (doFullTest)
                        {
                            RunUI(() => SkylineWindow.SelectedPath = identityPath);
                            WaitForGraphs();
                        }
                    }
                }
            }
            foreach (var result in results)
            {
                foreach (var resultCompare in results)
                {
                    if (!Equals(result.Item2, resultCompare.Item2))
                    {
                        continue;
                    }
                    var options1 = result.Item1;
                    var options2 = resultCompare.Item1;
                    if (!Equals(options1.RegressionFit, options2.RegressionFit))
                    {
                        continue;
                    }
                    CompareLoq(result.Item1, result.Item3, resultCompare.Item1, resultCompare.Item3);
                }
            }
        }