private static RegressionLine GetRegression(TargetMap <double> knownIrts, DbIrtPeptide[] matchingPeptides, int?minPoints, out RegressionGraphData graphData) { graphData = null; var matchingPeptideIrts = new TargetMap <List <double> >(matchingPeptides.Select(pep => new KeyValuePair <Target, List <double> >(pep.ModifiedTarget, new List <double>()))); foreach (var pep in matchingPeptides) { var list = matchingPeptideIrts[pep.ModifiedTarget]; list.Add(pep.Irt); } var listX = new List <double>(); var listY = new List <double>(); var targets = new Dictionary <int, Target>(); foreach (var(i, kvp) in matchingPeptideIrts.Where(kvp => kvp.Value.Count > 0).Select((kvp, i) => Tuple.Create(i, kvp))) { targets[i] = kvp.Key; listX.Add(new Statistics(kvp.Value).Median()); listY.Add(knownIrts[kvp.Key]); } var regressionMinPoints = minPoints ?? RCalcIrt.MinStandardCount(knownIrts.Count); var removed = new List <Tuple <double, double> >(); if (!IrtRegression.TryGet <RegressionLine>(listX, listY, regressionMinPoints, out var regression, removed)) { return(null); } var outliers = new HashSet <int>(); for (var i = 0; i < listX.Count; i++) { if (removed.Contains(Tuple.Create(listX[i], listY[i]))) { outliers.Add(i); } } graphData = new RegressionGraphData { Title = Resources.ChooseIrtStandardPeptidesDlg_OkDialog_Linear_regression, LabelX = Resources.ChooseIrtStandardPeptidesDlg_OkDialog_Library_iRTs, LabelY = Resources.ChooseIrtStandardPeptidesDlg_OkDialog_Known_iRTs, XValues = listX.ToArray(), YValues = listY.ToArray(), Tooltips = targets.ToDictionary(target => target.Key, target => target.Value.ToString()), OutlierIndices = outliers, RegressionLine = regression, MinCorrelation = RCalcIrt.MIN_IRT_TO_TIME_CORRELATION, MinPoints = regressionMinPoints, }; return(regression as RegressionLine); }
private double GetR(IIrtRegression regression, bool includeOutliers) { var r = IrtRegression.R(regression); if (!double.IsNaN(r)) { return(r); } var pointSet = !includeOutliers?RegularPoints.ToArray() : RegularPoints.Concat(OutlierPoints).ToArray(); var statsX = new Statistics(pointSet.Select(point => point.X)); var statsY = new Statistics(pointSet.Select(point => point.Y)); return(statsY.R(statsX)); }
public AddIrtPeptidesDlg( AddIrtPeptidesLocation location, ProcessedIrtAverages processed, IReadOnlyCollection <Target> existingPeptides, IReadOnlyCollection <Target> overwritePeptides, IReadOnlyCollection <Target> keepPeptides) { InitializeComponent(); Icon = Resources.Skyline; _regressionGraphData = new Dictionary <DataGridViewRow, RegressionGraphData[]>(); var successStyle = new DataGridViewCellStyle { BackColor = Color.LightGreen }; var failStyle = new DataGridViewCellStyle { BackColor = Color.LightCoral }; foreach (var data in processed.ProviderData) { var missingIndices = new HashSet <int>(); var outlierIndices = new HashSet <int>(); for (var i = 0; i < data.Peptides.Count; i++) { if (data.Peptides[i].Missing) { missingIndices.Add(i); } else if (data.Peptides[i].Outlier) { outlierIndices.Add(i); } } string regressionName; if (data.RegressionSuccess) { regressionName = data.Regression == null ? Resources.AddIrtPeptidesDlg_AddIrtPeptidesDlg_Regression : Resources.AddIrtPeptidesDlg_AddIrtPeptidesDlg_Regression_Refined; } else { regressionName = Resources.AddIrtPeptidesDlg_AddIrtPeptidesDlg_Regression_Attempted; } var graphData = new RegressionGraphData { Title = data.RetentionTimeProvider.Name, LabelX = Resources.AddIrtsResultsDlg_dataGridView_CellContentClick_Measured, LabelY = Resources.AddIrtPeptidesDlg_dataGridView_CellContentClick_iRT, XValues = data.Peptides.Select(peptide => peptide.RetentionTime.GetValueOrDefault()).ToArray(), YValues = data.Peptides.Select(peptide => peptide.Irt).ToArray(), Tooltips = Enumerable.Range(0, data.Peptides.Count).ToDictionary(i => i, i => data.Peptides[i].Target.ToString()), MissingIndices = missingIndices, OutlierIndices = outlierIndices, RegressionLine = data.RegressionRefined, RegressionLineCurrent = data.Regression, RegressionName = regressionName, ShowCurrentCorrelation = true, MinCorrelation = RCalcIrt.MIN_IRT_TO_TIME_CORRELATION, MinPoints = data.MinPoints }; string filename; try { filename = Path.GetFileName(data.RetentionTimeProvider.Name); } catch (Exception) { filename = data.RetentionTimeProvider.Name; } dataGridView.Rows.Add( filename, graphData.RegularPoints.Count, data.RegressionRefined != null ? data.RegressionRefined.DisplayEquation : string.Empty, data.RegressionRefined != null ? IrtRegression.R(data.RegressionRefined).ToString(@"F03") : IrtRegression.R(data.Regression).ToString(@"F03"), data.RegressionSuccess ? Resources.AddIrtPeptidesDlg_AddIrtPeptidesDlg_Success : Resources.AddIrtPeptidesDlg_AddIrtPeptidesDlg_Failed); var lastRow = dataGridView.Rows[dataGridView.RowCount - 1]; lastRow.DefaultCellStyle = data.RegressionSuccess ? successStyle : failStyle; lastRow.Tag = data; _regressionGraphData[lastRow] = new[] { graphData }; } PeptidesCount = processed.DbIrtPeptides.Count() - existingPeptides.Count - overwritePeptides.Count - keepPeptides.Count; RunsConvertedCount = processed.ProviderData.Count(data => data.RegressionSuccess); RunsFailedCount = processed.ProviderData.Count - RunsConvertedCount; string locationStr; switch (location) { default: locationStr = Resources.AddIrtPeptidesDlg_AddIrtPeptidesDlg_iRT_database; break; case AddIrtPeptidesLocation.spectral_library: locationStr = Resources.AddIrtPeptidesDlg_AddIrtPeptidesDlg_spectral_library; break; } if (PeptidesCount == 0) { labelPeptidesAdded.Text = string.Format(Resources.AddIrtPeptidesDlg_AddIrtPeptidesDlg_No_new_peptides_will_be_added_to_the__0__, locationStr); } else if (PeptidesCount == 1) { labelPeptidesAdded.Text = string.Format(Resources.AddIrtPeptidesDlg_AddIrtPeptidesDlg_1_new_peptide_will_be_added_to_the__0__, locationStr); } else { labelPeptidesAdded.Text = string.Format(labelPeptidesAdded.Text, PeptidesCount, locationStr); } if (RunsConvertedCount == 0) { labelRunsConverted.Visible = false; } else { labelRunsConverted.Text = RunsConvertedCount > 1 ? string.Format(labelRunsConverted.Text, RunsConvertedCount) : Resources.AddIrtPeptidesDlg_AddIrtPeptidesDlg_1_run_was_successfully_converted; } if (RunsFailedCount == 0) { labelRunsFailed.Visible = false; } else { labelRunsFailed.Text = RunsFailedCount > 1 ? string.Format(labelRunsFailed.Text, RunsFailedCount) : Resources.AddIrtPeptidesDlg_AddIrtPeptidesDlg_1_run_was_not_converted_due_to_insufficient_correlation; } listExisting.Items.AddRange(existingPeptides.Cast <object>().ToArray()); listOverwrite.Items.AddRange(overwritePeptides.Cast <object>().ToArray()); listKeep.Items.AddRange(keepPeptides.Cast <object>().ToArray()); labelExisting.Text = string.Format(labelExisting.Text, listExisting.Items.Count); labelOverwrite.Text = string.Format(labelOverwrite.Text, listOverwrite.Items.Count); labelKeep.Text = string.Format(labelKeep.Text, listKeep.Items.Count); panelExisting.Anchor &= ~AnchorStyles.Bottom; if (!processed.ProviderData.Any()) { dataGridView.Visible = false; panelOverwrite.Top -= dataGridView.Height; panelKeep.Top -= dataGridView.Height; panelExisting.Top -= dataGridView.Height; Height -= dataGridView.Height; } if (listOverwrite.Items.Count == 0) { panelOverwrite.Visible = false; panelKeep.Top -= panelOverwrite.Height; panelExisting.Top -= panelOverwrite.Height; Height -= panelOverwrite.Height; } if (listKeep.Items.Count == 0) { panelKeep.Visible = false; panelExisting.Top -= panelKeep.Height; Height -= panelKeep.Height; } panelExisting.Anchor |= AnchorStyles.Bottom; if (listExisting.Items.Count == 0) { panelExisting.Visible = false; Height -= panelExisting.Height; } if (!listOverwrite.Items.OfType <object>().Any() && !listKeep.Items.OfType <object>().Any() && !listExisting.Items.OfType <object>().Any()) { if (processed.ProviderData.Any()) { dataGridView.Anchor |= AnchorStyles.Bottom; } else { FormBorderStyle = FormBorderStyle.FixedDialog; } } }