private static void SaveMProphetFeatures(MProphetResultsHandler resultsHandler, string saveFile, CultureInfo cultureInfo, IList <IPeakFeatureCalculator> calcs) { using (var saver = new FileSaver(saveFile)) using (var writer = new StreamWriter(saver.SafeName)) { resultsHandler.WriteScores(writer, cultureInfo, calcs); writer.Flush(); writer.Close(); saver.Commit(); } }
public static void WriteFeatures(string filePath, MProphetResultsHandler resultsHandler, IList <IPeakFeatureCalculator> calcs, CultureInfo cultureInfo, bool bestOnly, bool includeDecoys, IProgressMonitor progressMonitor) { using (var fs = new FileSaver(filePath)) using (var writer = new StreamWriter(fs.SafeName)) { resultsHandler.ScoreFeatures(progressMonitor); resultsHandler.WriteScores(writer, cultureInfo, calcs, bestOnly, includeDecoys, progressMonitor); writer.Close(); fs.Commit(); } }
public void ScorePeptides(SrmDocument doc, IProgressMonitor progressMonitor) { var model = doc.Settings.PeptideSettings.Integration.PeakScoringModel; if (model == null || !model.IsTrained) { model = LegacyScoringModel.DEFAULT_MODEL; } var mProphetResultsHandler = new MProphetResultsHandler(doc, model); mProphetResultsHandler.ScoreFeatures(progressMonitor, true); if (progressMonitor.IsCanceled) { return; } var scoredPeptidesDict = new Dictionary <Target, ScoredPeptide>(); foreach (var nodePep in doc.Molecules.Where(pep => pep.PercentileMeasuredRetentionTime.HasValue && !pep.IsDecoy)) { var allStats = doc.MeasuredResults.MSDataFileInfos .Select(info => mProphetResultsHandler.GetPeakFeatureStatistics(nodePep.Id.GlobalIndex, info.FileId.GlobalIndex)) .Where(stats => stats != null).ToArray(); var value = float.MaxValue; if (allStats.Length > 0) { value = model is MProphetPeakScoringModel ? allStats.Select(stats => stats.QValue.Value).Max() : -allStats.Select(stats => stats.BestScore).Min(); } if (!scoredPeptidesDict.TryGetValue(nodePep.ModifiedTarget, out var existing) || value < existing.Score) { scoredPeptidesDict[nodePep.ModifiedTarget] = new ScoredPeptide( new MeasuredPeptide(doc.Settings.GetModifiedSequence(nodePep), nodePep.PercentileMeasuredRetentionTime.Value), nodePep, value); } } _scoredPeptides = scoredPeptidesDict.Values.OrderBy(pep => pep.Peptide.RetentionTime).ToArray(); _cirtPeptides = _scoredPeptides.Where(pep => _cirtAll.ContainsKey(pep.Peptide.Target)).ToArray(); }
public void OkDialog() { var helper = new MessageBoxHelper(this); double qCutoff = double.MaxValue; if (reintegrateQCutoff.Checked) { if (!helper.ValidateDecimalTextBox(textBoxCutoff, 0.0, 1.0, out qCutoff)) { return; } } using (var longWaitDlg = new LongWaitDlg { Text = Resources.ReintegrateDlg_OkDialog_Reintegrating, }) { try { var scoringModel = _driverPeakScoringModel.SelectedItem; if (Equals(scoringModel, LegacyScoringModel.DEFAULT_UNTRAINED_MODEL)) { scoringModel = LegacyScoringModel.DEFAULT_MODEL; } if (scoringModel == null || !scoringModel.IsTrained) { throw new InvalidDataException(Resources.ReintegrateDlg_OkDialog_You_must_train_and_select_a_model_in_order_to_reintegrate_peaks_); } var resultsHandler = new MProphetResultsHandler(Document, scoringModel) { QValueCutoff = qCutoff, OverrideManual = checkBoxOverwrite.Checked, AddAnnotation = checkBoxAnnotation.Checked, AddMAnnotation = checkBoxAnnotation.Checked }; longWaitDlg.PerformWork(this, 1000, pm => { resultsHandler.ScoreFeatures(pm); if (resultsHandler.IsMissingScores()) { throw new InvalidDataException(Resources.ReintegrateDlg_OkDialog_The_current_peak_scoring_model_is_incompatible_with_one_or_more_peptides_in_the_document___Please_train_a_new_model_); } // TODO: Add a checkbox for including decoys. Probably most of the time real users won't want to bother with reintegrating decoys Document = resultsHandler.ChangePeaks(pm); }); if (longWaitDlg.IsCanceled) { return; } } catch (Exception x) { var message = TextUtil.LineSeparate(string.Format(Resources.ReintegrateDlg_OkDialog_Failed_attempting_to_reintegrate_peaks_), x.Message); MessageDlg.ShowWithException(this, message, x); return; } } var newPeakScoringModel = _driverPeakScoringModel.SelectedItem; if (!Equals(newPeakScoringModel, Document.Settings.PeptideSettings.Integration.PeakScoringModel)) { Document = Document.ChangeSettings(Document.Settings.ChangePeptideIntegration( i => i.ChangePeakScoringModel(newPeakScoringModel))); } DialogResult = DialogResult.OK; }
public void TestMProphetResultsHandler() { var testFilesDir = new TestFilesDir(TestContext, ZIP_FILE); var documentFile = testFilesDir.GetTestPath("MProphetGold-trained.sky"); SrmDocument doc = ResultsUtil.DeserializeDocument(documentFile); // Load libraries doc = doc.ChangeSettings(doc.Settings.ChangePeptideLibraries(libraries => { var lib = libraries.Libraries[0]; return(libraries.ChangeLibrarySpecs(new LibrarySpec[] { new BiblioSpecLiteSpec(lib.Name, testFilesDir.GetTestPath(lib.FileNameHint)) })); })); // Load an empty doc, so that we can make a change and // cause the .skyd to be loaded var docContainer = new ResultsTestDocumentContainer(null, documentFile); docContainer.SetDocument(doc, null, true); docContainer.AssertComplete(); SrmDocument docOriginal = docContainer.Document; var peakScoringModel = docOriginal.Settings.PeptideSettings.Integration.PeakScoringModel; var resultsHandler = new MProphetResultsHandler(docOriginal, peakScoringModel) { QValueCutoff = Q_CUTOFF }; // 1. Reintegrate and export report produces expected file resultsHandler.ScoreFeatures(); var docNew = resultsHandler.ChangePeaks(); var reportSpec = MakeReportSpec(); if (IsSaveAll) { // For regenerating expected files if things change ReportToCsv(reportSpec, docNew, testFilesDir.GetTestPath(REPORT_EXPECTED), CultureInfo.GetCultureInfo("en-US")); ReportToCsv(reportSpec, docNew, testFilesDir.GetTestPathIntl(REPORT_EXPECTED), CultureInfo.GetCultureInfo("fr-FR")); } string docNewActual = testFilesDir.GetTestPath(REPORT_ACTUAL); string docNewExpected = testFilesDir.GetTestPathLocale(REPORT_EXPECTED); ReportToCsv(reportSpec, docNew, docNewActual, CultureInfo.CurrentCulture); AssertEx.FileEquals(docNewExpected, docNewActual); // 2. Reintegrating again gives no change in document var resultsHandlerRepeat = new MProphetResultsHandler(docNew, peakScoringModel) { QValueCutoff = Q_CUTOFF }; resultsHandlerRepeat.ScoreFeatures(); var docRepeat = resultsHandlerRepeat.ChangePeaks(); Assert.AreSame(docRepeat, docNew); Assert.AreNotSame(docOriginal, docNew); // 3. Export mProphet results gives expected file var calcs = peakScoringModel.PeakFeatureCalculators; var mProphetActual = testFilesDir.GetTestPath(MPROPHET_ACTUAL); var mProphetExpected = testFilesDir.GetTestPathLocale(MPROPHET_EXPECTED); if (IsSaveAll) { // For regenerating files SaveMProphetFeatures(resultsHandler, testFilesDir.GetTestPath(MPROPHET_EXPECTED), CultureInfo.GetCultureInfo("en-US"), calcs); SaveMProphetFeatures(resultsHandler, testFilesDir.GetTestPathIntl(MPROPHET_EXPECTED), CultureInfo.GetCultureInfo("fr-FR"), calcs); } SaveMProphetFeatures(resultsHandler, mProphetActual, CultureInfo.CurrentCulture, calcs); AssertEx.FileEquals(mProphetExpected, mProphetActual); // 4. Export mProphet -> Import Peak Boundaries leads to same result as reintegrate var resultsHandlerQAll = new MProphetResultsHandler(docOriginal, peakScoringModel) { QValueCutoff = 1.0 }; resultsHandlerQAll.ScoreFeatures(); var docNewQAll = resultsHandlerQAll.ChangePeaks(); var peakBoundaryImporter = new PeakBoundaryImporter(docNewQAll); long lineCount = Helpers.CountLinesInFile(mProphetActual); peakBoundaryImporter.Import(mProphetActual, null, lineCount); var docImport = peakBoundaryImporter.Document; // Serialized documents are easier to debug when something is different string strDocNew = SerializeDoc(docNewQAll); string strDocImport = SerializeDoc(docImport); AssertEx.NoDiff(strDocNew, strDocImport); Assert.AreSame(docNewQAll, docImport); // 5. Reintegration with q value cutoff of <0 causes all peaks set to null var handlerAllNull = new MProphetResultsHandler(docOriginal, peakScoringModel) { QValueCutoff = -0.001 }; handlerAllNull.ScoreFeatures(); var docNull = handlerAllNull.ChangePeaks(); foreach (var transitionNode in docNull.PeptideTransitions) { foreach (var chromInfo in transitionNode.ChromInfos) { Assert.IsTrue(chromInfo.IsEmpty || transitionNode.IsDecoy); } } // 6. Reintegration adjusts example peak to null at q=0.005 cutoff, but adjusts it to a non-null peak at q=0.20 const int groupNum = 11; var midQNode = resultsHandler.Document.PeptideTransitionGroups.ToList()[groupNum]; foreach (var chromInfo in midQNode.Transitions.SelectMany(transition => transition.ChromInfos)) { Assert.IsTrue(chromInfo.IsEmpty); } resultsHandler.QValueCutoff = Q_CUTOFF_HIGH; resultsHandler.ChangePeaks(); var midQNodeNew = resultsHandler.Document.PeptideTransitionGroups.ToList()[groupNum]; foreach (var chromInfo in midQNodeNew.Transitions.SelectMany(transition => transition.ChromInfos)) { Assert.IsFalse(chromInfo.IsEmpty); } // 7. Labeled peptide pairs still have matching peaks foreach (var peptideNode in resultsHandler.Document.Peptides) { Assert.AreEqual(2, peptideNode.TransitionGroupCount); var groupList = peptideNode.TransitionGroups.ToList(); var lightGroup = groupList[0]; var heavyGroup = groupList[0]; var lightChromInfo = lightGroup.ChromInfos.ToList()[0]; var heavyChromInfo = heavyGroup.ChromInfos.ToList()[0]; Assert.AreEqual(lightChromInfo.StartRetentionTime, heavyChromInfo.StartRetentionTime); Assert.AreEqual(lightChromInfo.EndRetentionTime, heavyChromInfo.EndRetentionTime); Assert.AreEqual(lightChromInfo.RetentionTime, heavyChromInfo.RetentionTime); } // 8. Verify that chosen peaks and q values are the same as those in mProphet paper: // http://www.nature.com/nmeth/journal/v8/n5/full/nmeth.1584.html#/supplementary-information // TODO: Grab this data from the mProphet paper // Release open streams docContainer.Release(); }
public void OkDialog() { using (var dlg = new SaveFileDialog { Title = Resources.MProphetFeaturesDlg_OkDialog_Export_mProphet_Features, OverwritePrompt = true, DefaultExt = EXT, Filter = TextUtil.FileDialogFilterAll(Resources.MProphetFeaturesDlg_OkDialog_mProphet_Feature_Files, EXT), }) { if (!string.IsNullOrEmpty(DocumentFilePath)) { dlg.InitialDirectory = Path.GetDirectoryName(DocumentFilePath); dlg.FileName = Path.GetFileNameWithoutExtension(DocumentFilePath) + EXT; } if (dlg.ShowDialog(this) == DialogResult.Cancel) { return; } var displayCalcs = new List <IPeakFeatureCalculator>(); displayCalcs.AddRange(from object calcName in checkedListVars.CheckedItems select GetCalcFromName(calcName.ToString())); IPeakScoringModel currentPeakScoringModel = Document.Settings.PeptideSettings.Integration.PeakScoringModel; var mProphetScoringModel = currentPeakScoringModel as MProphetPeakScoringModel; // if (mProphetScoringModel == null) // { // MessageDlg.Show(this, Resources.MProphetFeaturesDlg_OkDialog_To_export_MProphet_features_first_train_an_MProphet_model_); // return; // } var resultsHandler = new MProphetResultsHandler(Document, mProphetScoringModel); using (var longWaitDlg = new LongWaitDlg { Text = Resources.SkylineWindow_OpenSharedFile_Extracting_Files, }) { try { longWaitDlg.PerformWork(this, 1000, b => WriteFeatures(dlg.FileName, resultsHandler, displayCalcs, LocalizationHelper.CurrentCulture, checkBoxBestOnly.Checked, !checkBoxTargetsOnly.Checked, b)); if (longWaitDlg.IsCanceled) { return; } } catch (Exception x) { var message = TextUtil.LineSeparate(string.Format(Resources.MProphetFeaturesDlg_OkDialog_Failed_attempting_to_save_mProphet_features_to__0__, dlg.FileName), x.Message); MessageDlg.ShowWithException(this, message, x); } } } DialogResult = DialogResult.OK; }
/// <summary> /// Changing this starts a peak reintegration when it is set on the document. /// </summary> public PeptideIntegration ChangeResultsHandler(MProphetResultsHandler prop) { return ChangeProp(ImClone(this), im => { if (prop != null) im.PeakScoringModel = prop.ScoringModel; im.ResultsHandler = prop; }); }