private bool ProcessStandard() { var standard = (IrtStandard)comboExisting.SelectedItem; if (ReferenceEquals(standard, IrtStandard.CIRT_SHORT)) { var knownIrts = new TargetMap <double>(IrtStandard.CIRT.Peptides.Select(pep => new KeyValuePair <Target, double>(pep.ModifiedTarget, pep.Irt))); var matchingPeptides = _dbIrtPeptides.Where(pep => knownIrts.ContainsKey(pep.ModifiedTarget)).ToArray(); var regression = GetRegression(knownIrts, matchingPeptides, RCalcIrt.MIN_PEPTIDES_COUNT, out var graphData); if (regression != null) { var numCirts = graphData.XValues.Length; int cirtCount; using (var dlg = new AddIrtStandardsDlg(numCirts, string.Format(Resources.LibraryBuildNotificationHandler_AddIrts__0__distinct_CiRT_peptides_were_found__How_many_would_you_like_to_use_as_iRT_standards_, numCirts), graphData)) { if (dlg.ShowDialog(this) != DialogResult.OK) { return(false); } cirtCount = dlg.StandardCount; } Regression = regression; var outlierTargets = graphData.OutlierIndices.Select(idx => new Target(graphData.Tooltips[idx])).ToArray(); SetStandards(IrtPeptidePicker.Pick(cirtCount, matchingPeptides, outlierTargets)); return(true); } } var irts = new TargetMap <double>(standard.Peptides.Select(pep => new KeyValuePair <Target, double>(pep.ModifiedTarget, pep.Irt))); var peptides = _dbIrtPeptides.Where(pep => irts.ContainsKey(pep.ModifiedTarget)).ToArray(); Regression = GetRegression(irts, peptides, null, out _); if (Regression != null) { SetStandards(standard.Peptides.Select(pep => pep.ModifiedTarget)); } else if (standard.HasDocument) { var missing = new TargetMap <bool>(standard.MissingFromDocument(Document).Select(target => new KeyValuePair <Target, bool>(target, true))); _irtAdd.AddRange(standard.Peptides.Where(pep => missing.ContainsKey(pep.ModifiedTarget)) .Select(pep => new MeasuredRetentionTime(pep.ModifiedTarget, pep.Irt, true, true))); Document = standard.ImportTo(Document); } return(true); }
public static bool AddIrts(Library lib, LibrarySpec libSpec, IrtStandard standard, Control parent, bool useTopMostForm = false) { if (lib == null || !lib.IsLoaded || standard == null || standard.Name.Equals(IrtStandard.EMPTY.Name)) { return(false); } Control GetParent() { return(useTopMostForm ? FormUtil.FindTopLevelOpenForm(f => f is BuildLibraryNotification) ?? parent : parent); } IRetentionTimeProvider[] irtProviders = null; var cirtPeptides = new DbIrtPeptide[0]; using (var longWait = new LongWaitDlg { Text = Resources.LibraryBuildNotificationHandler_AddIrts_Loading_retention_time_providers }) { var status = longWait.PerformWork(GetParent(), 800, monitor => { monitor.UpdateProgress(new ProgressStatus().ChangePercentComplete(-1)); irtProviders = lib.RetentionTimeProvidersIrt.ToArray(); if (!irtProviders.Any()) { irtProviders = lib.RetentionTimeProviders.ToArray(); } if (ReferenceEquals(standard, IrtStandard.CIRT_SHORT)) { var libPeptides = irtProviders.SelectMany(provider => provider.PeptideRetentionTimes).Select(rt => rt.PeptideSequence).ToHashSet(); cirtPeptides = IrtStandard.CIRT.Peptides.Where(pep => libPeptides.Contains(pep.ModifiedTarget)).ToArray(); } }); if (status.IsCanceled) { return(false); } if (status.IsError) { throw status.ErrorException; } } int?numCirt = null; if (cirtPeptides.Length >= RCalcIrt.MIN_PEPTIDES_COUNT) { using (var dlg = new AddIrtStandardsDlg(cirtPeptides.Length, string.Format( Resources.LibraryBuildNotificationHandler_AddIrts__0__distinct_CiRT_peptides_were_found__How_many_would_you_like_to_use_as_iRT_standards_, cirtPeptides.Length))) { if (dlg.ShowDialog(GetParent()) != DialogResult.OK) { return(false); } numCirt = dlg.StandardCount; } } var standardPeptides = standard.Peptides.ToArray(); ProcessedIrtAverages processed = null; using (var longWait = new LongWaitDlg { Text = Resources.LibraryBuildNotificationHandler_AddIrts_Processing_retention_times }) { try { var status = longWait.PerformWork(GetParent(), 800, monitor => { processed = !numCirt.HasValue ? RCalcIrt.ProcessRetentionTimes(monitor, irtProviders, standardPeptides, new DbIrtPeptide[0]) : RCalcIrt.ProcessRetentionTimesCirt(monitor, irtProviders, cirtPeptides, numCirt.Value, out standardPeptides); }); if (status.IsCanceled) { return(false); } if (status.IsError) { throw status.ErrorException; } } catch (Exception x) { MessageDlg.ShowWithException(GetParent(), TextUtil.LineSeparate( Resources.BuildPeptideSearchLibraryControl_AddIrtLibraryTable_An_error_occurred_while_processing_retention_times_, x.Message), x); return(false); } } using (var resultsDlg = new AddIrtPeptidesDlg(AddIrtPeptidesLocation.spectral_library, processed)) { if (resultsDlg.ShowDialog(GetParent()) != DialogResult.OK) { return(false); } } var recalibrate = false; if (processed.CanRecalibrateStandards(standardPeptides)) { using (var dlg = new MultiButtonMsgDlg( TextUtil.LineSeparate(Resources.LibraryGridViewDriver_AddToLibrary_Do_you_want_to_recalibrate_the_iRT_standard_values_relative_to_the_peptides_being_added_, Resources.LibraryGridViewDriver_AddToLibrary_This_can_improve_retention_time_alignment_under_stable_chromatographic_conditions_), MultiButtonMsgDlg.BUTTON_YES, MultiButtonMsgDlg.BUTTON_NO, false)) { recalibrate = dlg.ShowDialog(GetParent()) == DialogResult.Yes; } } var processedDbIrtPeptides = processed.DbIrtPeptides.ToArray(); if (!processedDbIrtPeptides.Any()) { return(false); } using (var longWait = new LongWaitDlg { Text = Resources.LibraryBuildNotificationHandler_AddIrts_Adding_iRTs_to_library }) { try { DbIrtPeptide[] newStandards = null; var status = longWait.PerformWork(GetParent(), 800, monitor => { if (recalibrate) { monitor.UpdateProgress(new ProgressStatus().ChangeSegments(0, 2)); newStandards = processed.RecalibrateStandards(standardPeptides).ToArray(); processed = RCalcIrt.ProcessRetentionTimes(monitor, processed.ProviderData.Select(data => data.RetentionTimeProvider).ToArray(), newStandards.ToArray(), new DbIrtPeptide[0]); } var irtDb = IrtDb.CreateIrtDb(libSpec.FilePath); irtDb.AddPeptides(monitor, (newStandards ?? standardPeptides).Concat(processed.DbIrtPeptides).ToList()); }); if (status.IsError) { throw status.ErrorException; } } catch (Exception x) { MessageDlg.ShowWithException(GetParent(), TextUtil.LineSeparate( Resources.LibraryBuildNotificationHandler_AddIrts_An_error_occurred_trying_to_add_iRTs_to_the_library_, x.Message), x); return(false); } } return(true); }
public static bool AddIrts(IrtRegressionType regressionType, Library lib, LibrarySpec libSpec, IrtStandard standard, Control parent, bool useTopMostForm) { if (lib == null || !lib.IsLoaded || standard == null || standard.Name.Equals(IrtStandard.EMPTY.Name)) { return(false); } Control GetParent() { return(useTopMostForm ? FormUtil.FindTopLevelOpenForm(f => f is BuildLibraryNotification) ?? parent : parent); } IRetentionTimeProvider[] irtProviders = null; var isAuto = ReferenceEquals(standard, IrtStandard.AUTO); List <IrtStandard> autoStandards = null; var cirtPeptides = new DbIrtPeptide[0]; using (var longWait = new LongWaitDlg { Text = Resources.LibraryBuildNotificationHandler_AddIrts_Loading_retention_time_providers }) { var standard1 = standard; var status = longWait.PerformWork(GetParent(), 800, monitor => { ImportPeptideSearch.GetLibIrtProviders(lib, standard1, monitor, out irtProviders, out autoStandards, out cirtPeptides); }); if (status.IsCanceled) { return(false); } if (status.IsError) { throw status.ErrorException; } } int?numCirt = null; if (cirtPeptides.Length >= RCalcIrt.MIN_PEPTIDES_COUNT) { using (var dlg = new AddIrtStandardsDlg(cirtPeptides.Length, string.Format( Resources.LibraryBuildNotificationHandler_AddIrts__0__distinct_CiRT_peptides_were_found__How_many_would_you_like_to_use_as_iRT_standards_, cirtPeptides.Length))) { if (dlg.ShowDialog(GetParent()) != DialogResult.OK) { return(false); } numCirt = dlg.StandardCount; } } else if (isAuto) { switch (autoStandards.Count) { case 0: standard = new IrtStandard(XmlNamedElement.NAME_INTERNAL, null, null, IrtPeptidePicker.Pick(irtProviders, 10)); break; case 1: standard = autoStandards[0]; break; default: using (var selectIrtStandardDlg = new SelectIrtStandardDlg(autoStandards)) { if (selectIrtStandardDlg.ShowDialog(GetParent()) != DialogResult.OK) { return(false); } standard = selectIrtStandardDlg.Selected; } break; } } var standardPeptides = standard.Peptides.ToArray(); ProcessedIrtAverages processed = null; using (var longWait = new LongWaitDlg { Text = Resources.LibraryBuildNotificationHandler_AddIrts_Processing_retention_times }) { try { var status = longWait.PerformWork(GetParent(), 800, monitor => { processed = ImportPeptideSearch.ProcessRetentionTimes(numCirt, irtProviders, standardPeptides, cirtPeptides, regressionType, monitor, out var newStandardPeptides); if (newStandardPeptides != null) { standardPeptides = newStandardPeptides; } }); if (status.IsCanceled) { return(false); } if (status.IsError) { throw status.ErrorException; } } catch (Exception x) { MessageDlg.ShowWithException(GetParent(), TextUtil.LineSeparate( Resources.BuildPeptideSearchLibraryControl_AddIrtLibraryTable_An_error_occurred_while_processing_retention_times_, x.Message), x); return(false); } } using (var resultsDlg = new AddIrtPeptidesDlg(AddIrtPeptidesLocation.spectral_library, processed)) { if (resultsDlg.ShowDialog(GetParent()) != DialogResult.OK) { return(false); } } var recalibrate = false; if (processed.CanRecalibrateStandards(standardPeptides)) { using (var dlg = new MultiButtonMsgDlg( TextUtil.LineSeparate(Resources.LibraryGridViewDriver_AddToLibrary_Do_you_want_to_recalibrate_the_iRT_standard_values_relative_to_the_peptides_being_added_, Resources.LibraryGridViewDriver_AddToLibrary_This_can_improve_retention_time_alignment_under_stable_chromatographic_conditions_), MultiButtonMsgDlg.BUTTON_YES, MultiButtonMsgDlg.BUTTON_NO, false)) { recalibrate = dlg.ShowDialog(GetParent()) == DialogResult.Yes; } } if (!processed.DbIrtPeptides.Any()) { return(false); } using (var longWait = new LongWaitDlg { Text = Resources.LibraryBuildNotificationHandler_AddIrts_Adding_iRTs_to_library }) { try { var status = longWait.PerformWork(GetParent(), 800, monitor => { ImportPeptideSearch.CreateIrtDb(libSpec.FilePath, processed, standardPeptides, recalibrate, regressionType, monitor); }); if (status.IsError) { throw status.ErrorException; } } catch (Exception x) { MessageDlg.ShowWithException(GetParent(), TextUtil.LineSeparate( Resources.LibraryBuildNotificationHandler_AddIrts_An_error_occurred_trying_to_add_iRTs_to_the_library_, x.Message), x); return(false); } } return(true); }