private DbIrtPeptide NewPeptide(DbIrtPeptide dbPeptide) { return(new DbIrtPeptide(dbPeptide.ModifiedTarget, dbPeptide.Irt, dbPeptide.Standard, dbPeptide.TimeSource)); }
private DbIrtPeptide NewPeptide(DbIrtPeptide dbPeptide) { return(new DbIrtPeptide(dbPeptide.PeptideModSeq, dbPeptide.Irt, dbPeptide.Standard, dbPeptide.TimeSource)); }
public DbIrtPeptide(DbIrtPeptide other) : base(other) { Id = other.Id; Irt = other.Irt; Standard = other.Standard; TimeSource = other.TimeSource; }
public virtual bool Equals(DbIrtPeptide other) { if (ReferenceEquals(null, other)) { return(false); } if (ReferenceEquals(this, other)) { return(true); } return(base.Equals(other) && Equals(other.ModifiedTarget, ModifiedTarget) && other.Irt.Equals(Irt) && other.Standard.Equals(Standard) && other.TimeSource.Equals(TimeSource)); }
public IrtDb UpdatePeptides(IList <DbIrtPeptide> newPeptides, IList <DbIrtPeptide> oldPeptides) { var setNew = new HashSet <long>(newPeptides.Select(pep => pep.Id.HasValue ? pep.Id.Value : 0)); var dictOld = oldPeptides.ToDictionary(pep => pep.ModifiedTarget); using (var session = OpenWriteSession()) using (var transaction = session.BeginTransaction()) { // Remove peptides that are no longer in the list foreach (var peptideOld in oldPeptides) { if (!peptideOld.Id.HasValue) { continue; } if (!setNew.Contains(peptideOld.Id.Value)) { session.Delete(peptideOld); } } // Add or update peptides that have changed from the old list foreach (var peptideNew in newPeptides) { DbIrtPeptide peptideOld; if (dictOld.TryGetValue(peptideNew.ModifiedTarget, out peptideOld) && Equals(peptideNew, peptideOld)) { continue; } // Create a new instance, because not doing this causes a BindingSource leak var peptideNewDisconnected = new DbIrtPeptide(peptideNew); session.SaveOrUpdate(peptideNewDisconnected); } transaction.Commit(); } return(ChangeProp(ImClone(this), im => im.LoadPeptides(newPeptides))); }
public static bool ContainsMatch(IEnumerable <DbIrtPeptide> peptides, DbIrtPeptide peptide, double?irtTolerance) { return(peptides.Any(p => Match(p, peptide, irtTolerance))); }
public static IEnumerable <Target> Pick(int count, DbIrtPeptide[] peptides, IEnumerable <Target> outliers) { var targets = new TargetMap <List <DbIrtPeptide> >(peptides.Select(pep => new KeyValuePair <Target, List <DbIrtPeptide> >(pep.ModifiedTarget, new List <DbIrtPeptide>()))); foreach (var pep in peptides) { targets[pep.ModifiedTarget].Add(pep); } var distinctPeps = new List <DbIrtPeptide>(); foreach (var list in targets.Values) { if (list.Count == 0) { continue; } var median = new Statistics(list.Select(pep => pep.Irt)).Median(); DbIrtPeptide best = null; var minDiff = double.MaxValue; foreach (var pep in list) { var diff = Math.Abs(pep.Irt - median); if (diff < minDiff) { minDiff = diff; best = pep; } } distinctPeps.Add(best); } var outlierMap = new TargetMap <bool>(outliers.Select(target => new KeyValuePair <Target, bool>(target, true))); if (distinctPeps.Count(pep => !outlierMap.ContainsKey(pep.ModifiedTarget)) >= count) { // don't use outliers if we have enough other values distinctPeps.RemoveAll(pep => outlierMap.ContainsKey(pep.ModifiedTarget)); } distinctPeps.Sort((x, y) => x.Irt.CompareTo(y.Irt)); var minIrt = distinctPeps.First().Irt; var maxIrt = distinctPeps.Last().Irt; var gradientLength = maxIrt - minIrt; for (var i = 0; i < count; i++) { var targetRt = minIrt + i * (gradientLength / (count - 1)); for (var j = 0; j < distinctPeps.Count; j++) { if (j + 1 > distinctPeps.Count - 1 || Math.Abs(distinctPeps[j].Irt - targetRt) < Math.Abs(distinctPeps[j + 1].Irt - targetRt)) { yield return(distinctPeps[j].ModifiedTarget); distinctPeps.RemoveAt(j); break; } } } }
public List <DbIrtPeptide> RecalibrateStandards(DbIrtPeptide[] standardPeptideList) { var peptideAllIrtTimes = new Dictionary <Target, List <Tuple <double, double> > >(); // peptide -> list of (irt, time) foreach (var data in ProviderData) { foreach (var peptide in data.FilteredPeptides) { if (!peptideAllIrtTimes.TryGetValue(peptide.Target, out var pepTimes)) { pepTimes = new List <Tuple <double, double> >(); peptideAllIrtTimes[peptide.Target] = pepTimes; } pepTimes.Add(Tuple.Create(peptide.Irt, peptide.RetentionTime.Value)); } } var peptideBestIrtTimes = new Dictionary <Target, Tuple <double, double> >(); // peptide -> (percentile irt, percentile time) foreach (var peptide in peptideAllIrtTimes) { var statIrts = new Statistics(peptide.Value.Select(p => p.Item1)); var statTimes = new Statistics(peptide.Value.Select(p => p.Item2)); var percentile = IrtStandard.GetSpectrumTimePercentile(peptide.Key); peptideBestIrtTimes[peptide.Key] = Tuple.Create(statIrts.Percentile(percentile), statTimes.Percentile(percentile)); } DbIrtPeptide min = null, max = null; foreach (var standard in standardPeptideList) // loop over list of standard peptides to find min/max that we have values for { if ((min == null || standard.Irt < min.Irt) && peptideBestIrtTimes.ContainsKey(standard.ModifiedTarget)) { min = standard; } if ((max == null || standard.Irt > max.Irt) && peptideBestIrtTimes.ContainsKey(standard.ModifiedTarget)) { max = standard; } } if (min == null || max == null) { throw new Exception(Resources.EditIrtCalcDlg_RecalibrateStandards_Could_not_get_a_minimum_or_maximum_standard_peptide_); } var statX = new Statistics(peptideBestIrtTimes[min.ModifiedTarget].Item2, peptideBestIrtTimes[max.ModifiedTarget].Item2); var statY = new Statistics(peptideBestIrtTimes[min.ModifiedTarget].Item1, peptideBestIrtTimes[max.ModifiedTarget].Item1); var line = new RegressionLine(statY.Slope(statX), statY.Intercept(statX)); var newStandardPeptideList = new List <DbIrtPeptide>(); foreach (var peptide in standardPeptideList) { if (!peptideBestIrtTimes.TryGetValue(peptide.ModifiedTarget, out var times)) { throw new Exception(Resources.ProcessedIrtAverages_RecalibrateStandards_A_standard_peptide_was_missing_when_trying_to_recalibrate_); } newStandardPeptideList.Add(new DbIrtPeptide(peptide) { Irt = line.GetY(times.Item2) }); } return(newStandardPeptideList); }
public static ProcessedIrtAverages ProcessRetentionTimesCirt(IProgressMonitor monitor, IRetentionTimeProvider[] providers, DbIrtPeptide[] cirtPeptides, int numCirt, out DbIrtPeptide[] chosenCirtPeptides) { chosenCirtPeptides = new DbIrtPeptide[0]; var irts = new TargetMap <DbIrtPeptide>(IrtStandard.CIRT.Peptides.Select(pep => new KeyValuePair <Target, DbIrtPeptide>(pep.ModifiedTarget, pep))); var targetRts = new Dictionary <Target, List <double> >(); var targetCounts = new Dictionary <Target, int>(); // count how many successful regressions each peptide participated in foreach (var provider in providers) { if (monitor.IsCanceled) { return(null); } var times = ( from pep in cirtPeptides let rt = provider.GetRetentionTime(pep.ModifiedTarget) where rt.HasValue select Tuple.Create(pep.ModifiedTarget, rt.Value, irts[pep.ModifiedTarget].Irt)).ToList(); foreach (var(target, rt, _) in times) { if (targetRts.TryGetValue(target, out var list)) { list.Add(rt); } else { targetRts[target] = new List <double> { rt } }; } var removed = new List <Tuple <double, double> >(); if (!TryGetRegressionLine(times.Select(t => t.Item2).ToList(), times.Select(t => t.Item3).ToList(), MIN_PEPTIDES_COUNT, out _, removed)) { continue; } foreach (var(removeRt, removeIrt) in removed) { times.Remove(times.First(time => time.Item2.Equals(removeRt) && time.Item3.Equals(removeIrt))); } foreach (var(target, _, _) in times) { targetCounts[target] = targetCounts.TryGetValue(target, out var existing) ? existing + 1 : 1; } } // for each target, only keep median retention time var dupTargets = targetRts.Where(kvp => kvp.Value.Count > 1).Select(kvp => kvp.Key).ToArray(); foreach (var target in dupTargets) { targetRts[target] = new List <double> { new Statistics(targetRts[target]).Median() } } ; // separate targets into equal retention time bins var candidateBins = new List <Tuple <Target, double> > [numCirt]; for (var i = 0; i < candidateBins.Length; i++) { candidateBins[i] = new List <Tuple <Target, double> >(); } var minRt = double.MaxValue; var maxRt = -1d; foreach (var rt in targetRts.Values.Select(list => list.First())) { if (rt < minRt) { minRt = rt; } if (rt > maxRt) { maxRt = rt; } } var binSize = (maxRt - minRt) / numCirt; var lastBinIdx = candidateBins.Length - 1; foreach (var target in targetRts) { foreach (var rt in target.Value) { candidateBins[Math.Min((int)((rt - minRt) / binSize), lastBinIdx)].Add(Tuple.Create(target.Key, rt)); } } Tuple <Target, double> GetBest(List <Tuple <Target, double> > bin, int binIdx, out int bestCount, out double bestRtDelta) { if (bin.Count == 0) { bestCount = 0; bestRtDelta = 0; return(null); } bestCount = 0; var filtered = new List <Tuple <Target, double> >(); foreach (var t in bin) { if (!targetCounts.TryGetValue(t.Item1, out var count)) { continue; } if (count > bestCount) { bestCount = count; filtered.Clear(); filtered.Add(t); } else if (count == bestCount) { filtered.Add(t); } } if (filtered.Count == 0) { filtered = bin; } var targetRt = ((minRt + binSize * binIdx) + (minRt + binSize * (binIdx + 1))) / 2; var closest = filtered.Aggregate((x, y) => Math.Abs(x.Item2 - targetRt) < Math.Abs(y.Item2 - targetRt) ? x : y); bestRtDelta = Math.Abs(closest.Item2 - targetRt); return(closest); } var chosenList = new List <DbIrtPeptide>(); var emptyBins = new HashSet <int>(); for (var i = 0; i < candidateBins.Length; i++) { var bin = candidateBins[i]; if (bin.Count > 0) { // choose the best from this bin var best = GetBest(bin, i, out _, out _); chosenList.Add(irts[best.Item1]); bin.Remove(best); } else { emptyBins.Add(i); } } foreach (var emptyIdx in emptyBins) { var bins = new List <int>(); var left = emptyIdx - 1; var right = emptyIdx + 1; while (bins.Count == 0) { if (left >= 0) { bins.Add(left); } if (right < candidateBins.Length) { bins.Add(right); } left--; right++; } Tuple <Target, double> best = null; var bestBinIdx = -1; var bestCount = 0; var bestRtDelta = 0d; foreach (var i in bins) { var current = GetBest(candidateBins[i], i, out var count, out var rtDelta); if (count > bestCount || (count == bestCount && rtDelta < bestRtDelta)) { best = current; bestBinIdx = i; bestCount = count; bestRtDelta = rtDelta; } } if (best != null) { chosenList.Add(irts[best.Item1]); candidateBins[bestBinIdx].Remove(best); } } // Process retention times using the chosen peptides chosenCirtPeptides = chosenList.ToArray(); return(ProcessRetentionTimes(monitor, providers, chosenCirtPeptides, new DbIrtPeptide[0])); }
public static ProcessedIrtAverages ProcessRetentionTimes(IProgressMonitor monitor, IRetentionTimeProvider[] providers, DbIrtPeptide[] standardPeptideList, DbIrtPeptide[] items) { var matchedStandard = IrtStandard.WhichStandard(standardPeptideList.Select(pep => pep.ModifiedTarget)); if (matchedStandard != null) { var dummyDoc = new SrmDocument(SrmSettingsList.GetDefault()); using (var reader = matchedStandard.GetDocumentReader()) { if (reader != null) { dummyDoc = dummyDoc.ImportDocumentXml(reader, string.Empty, MeasuredResults.MergeAction.remove, false, null, Settings.Default.StaticModList, Settings.Default.HeavyModList, null, out _, out _, false); standardPeptideList = standardPeptideList.Select(pep => new DbIrtPeptide(pep)).ToArray(); foreach (var dummyPep in dummyDoc.Molecules.Where(pep => pep.HasExplicitMods)) { var standardPepIdx = standardPeptideList.IndexOf(pep => dummyPep.ModifiedTarget.Equals(pep.ModifiedTarget)); standardPeptideList[standardPepIdx] = new DbIrtPeptide(standardPeptideList[standardPepIdx]) { ModifiedTarget = dummyDoc.Settings.GetModifiedSequence(dummyPep.ModifiedTarget, IsotopeLabelType.heavy, dummyPep.ExplicitMods) }; } } } } IProgressStatus status = new ProgressStatus(Resources.LibraryGridViewDriver_ProcessRetentionTimes_Adding_retention_times); var dictPeptideAverages = new Dictionary <Target, IrtPeptideAverages>(); var providerData = new List <RetentionTimeProviderData>(); var runCount = 0; foreach (var retentionTimeProvider in providers) { if (monitor.IsCanceled) { return(null); } monitor.UpdateProgress(status = status.ChangeMessage(string.Format( Resources.LibraryGridViewDriver_ProcessRetentionTimes_Converting_retention_times_from__0__, retentionTimeProvider.Name))); runCount++; var data = new RetentionTimeProviderData(retentionTimeProvider, standardPeptideList); if (data.RegressionSuccess || data.CalcRegressionWith(retentionTimeProvider, standardPeptideList, items)) { AddRetentionTimesToDict(retentionTimeProvider, data.RegressionRefined, dictPeptideAverages, standardPeptideList); } providerData.Add(data); monitor.UpdateProgress(status = status.ChangePercentComplete(runCount * 100 / providers.Length)); } monitor.UpdateProgress(status.Complete()); return(new ProcessedIrtAverages(dictPeptideAverages, providerData)); }
public DbIrtPeptide(DbIrtPeptide other) : this(other.PeptideModSeq, other.Irt, other.Standard, other.TimeSource) { Id = other.Id; }
public static bool Match(DbIrtPeptide x, string peptideModSeq) { return(Equals(x.PeptideModSeq, peptideModSeq)); }
public Conflict(DbIrtPeptide newPeptide, DbIrtPeptide existingPeptide) : this() { ExistingPeptide = existingPeptide; NewPeptide = newPeptide; }
private DbIrtPeptide MatchingStandard(DbIrtPeptide peptide, double?irtTolerance) { return(Peptides.FirstOrDefault(p => Match(peptide, p, irtTolerance))); }
/// <summary> /// Determines whether a peptide is contained in this set of standard peptides. /// </summary> /// <param name="peptide">The peptide to check for.</param> /// <param name="irtTolerance">Tolerance used to compare iRTs; if null, iRTs are not required to match.</param> /// <returns>True if the peptide is contained in this set of standard peptides; false otherwise.</returns> public bool Contains(DbIrtPeptide peptide, double?irtTolerance) { return(ContainsMatch(Peptides, peptide, irtTolerance)); }
/// <summary> /// Determines whether any of the iRT standards contain the peptide. /// </summary> /// <param name="peptide">The peptide to check for.</param> /// <param name="irtTolerance">Tolerance used to compare iRTs; if null, iRTs are not required to match.</param> /// <returns>True if the peptide is in any of the iRT standards; false otherwise.</returns> public static bool AnyContains(DbIrtPeptide peptide, double?irtTolerance) { return(ALL.Any(standard => standard.Contains(peptide, irtTolerance))); }
public static bool Match(DbIrtPeptide x, DbIrtPeptide y, double?irtTolerance) { return(Equals(x.PeptideModSeq, y.PeptideModSeq) && (!irtTolerance.HasValue || Math.Abs(x.Irt - y.Irt) < irtTolerance.Value)); }
public static ProcessedIrtAverages ProcessRetentionTimes(IProgressMonitor monitor, IRetentionTimeProvider[] providers, DbIrtPeptide[] standardPeptideList, DbIrtPeptide[] items, IrtRegressionType regressionType) { var heavyStandards = new DbIrtPeptide[standardPeptideList.Length]; var matchedStandard = IrtStandard.WhichStandard(standardPeptideList.Select(pep => pep.ModifiedTarget)); if (matchedStandard != null && matchedStandard.HasDocument) { // Check embedded standard document for known standard to determine if the standard peptides should be heavy // Import iRT standard document into an empty document (rather than just getting the document), because importing also imports the modifications var standardDoc = matchedStandard.ImportTo(new SrmDocument(SrmSettingsList.GetDefault())); standardPeptideList = standardPeptideList.Select(pep => new DbIrtPeptide(pep)).ToArray(); foreach (var dummyPep in standardDoc.Molecules.Where(pep => pep.HasExplicitMods)) { var standardPepIdx = standardPeptideList.IndexOf(pep => dummyPep.ModifiedTarget.Equals(pep.ModifiedTarget)); if (standardPepIdx < 0) { continue; } var heavyTarget = standardDoc.Settings.GetModifiedSequence(dummyPep.ModifiedTarget, IsotopeLabelType.heavy, dummyPep.ExplicitMods); if (!standardPeptideList[standardPepIdx].ModifiedTarget.Equals(heavyTarget)) { heavyStandards[standardPepIdx] = new DbIrtPeptide(standardPeptideList[standardPepIdx]) { ModifiedTarget = heavyTarget } } ; } } IProgressStatus status = new ProgressStatus(Resources.LibraryGridViewDriver_ProcessRetentionTimes_Adding_retention_times); var dictPeptideAverages = new Dictionary <Target, IrtPeptideAverages>(); var providerData = new List <RetentionTimeProviderData>(); var runCount = 0; foreach (var retentionTimeProvider in providers) { if (monitor.IsCanceled) { return(null); } monitor.UpdateProgress(status = status.ChangeMessage(string.Format( Resources.LibraryGridViewDriver_ProcessRetentionTimes_Converting_retention_times_from__0__, retentionTimeProvider.Name))); runCount++; var data = new RetentionTimeProviderData(regressionType, retentionTimeProvider, standardPeptideList, heavyStandards); if (data.RegressionSuccess || (ReferenceEquals(regressionType, IrtRegressionType.LINEAR) && data.CalcRegressionWith(retentionTimeProvider, standardPeptideList, items))) { AddRetentionTimesToDict(retentionTimeProvider, data.RegressionRefined, dictPeptideAverages, standardPeptideList); } providerData.Add(data); monitor.UpdateProgress(status = status.ChangePercentComplete(runCount * 100 / providers.Length)); } monitor.UpdateProgress(status.Complete()); return(new ProcessedIrtAverages(dictPeptideAverages, providerData)); }
public static bool Match(DbIrtPeptide x, DbIrtPeptide y, double?irtTolerance) { return(Equals(x.GetNormalizedModifiedSequence(), y.GetNormalizedModifiedSequence()) && (!irtTolerance.HasValue || Math.Abs(x.Irt - y.Irt) < irtTolerance.Value)); }
/// <summary> /// Determines whether any of the iRT standards contain the peptide. /// </summary> /// <param name="peptide">The peptide to check for.</param> /// <param name="irtTolerance">Tolerance used to compare iRTs; if null, iRTs are not required to match.</param> /// <returns>True if the peptide is in any of the iRT standards; false otherwise.</returns> public static bool AnyContains(DbIrtPeptide peptide, double?irtTolerance) { // Shortcircuit with AnyContains, because it is much faster return(AnyContains(peptide.ModifiedTarget) && ALL.Any(standard => standard.Contains(peptide, irtTolerance))); }
public static IrtDb GetIrtDb(string path, IProgressMonitor loadMonitor, out IList <DbIrtPeptide> dbPeptides) { var status = new ProgressStatus(string.Format(Resources.IrtDb_GetIrtDb_Loading_iRT_database__0_, path)); if (loadMonitor != null) { loadMonitor.UpdateProgress(status); } try { if (path == null) { throw new DatabaseOpeningException(Resources.IrtDb_GetIrtDb_Database_path_cannot_be_null); } if (!File.Exists(path)) { throw new DatabaseOpeningException(String.Format(Resources.IrtDb_GetIrtDb_The_file__0__does_not_exist_, path)); } string message; Exception xInner = null; try { //Check for a valid SQLite file and that it has our schema //Allow only one thread at a time to read from the same path using (var sessionFactory = GetSessionFactory(path)) { lock (sessionFactory) { return(new IrtDb(path, sessionFactory).Load(loadMonitor, status, out dbPeptides)); } } } catch (UnauthorizedAccessException x) { message = string.Format(Resources.IrtDb_GetIrtDb_You_do_not_have_privileges_to_access_the_file__0_, path); xInner = x; } catch (DirectoryNotFoundException x) { message = string.Format(Resources.IrtDb_GetIrtDb_The_path_containing__0__does_not_exist, path); xInner = x; } catch (FileNotFoundException x) { message = string.Format(Resources.IrtDb_GetIrtDb_The_file__0__could_not_be_created_Perhaps_you_do_not_have_sufficient_privileges, path); xInner = x; } catch (SQLiteException x) { message = string.Format(Resources.IrtDb_GetIrtDb_The_file__0__is_not_a_valid_iRT_database_file, path); xInner = x; } catch (Exception x) { message = string.Format(Resources.IrtDb_GetIrtDb_The_file__0__could_not_be_opened, path); xInner = x; } throw new DatabaseOpeningException(message, xInner); } catch (DatabaseOpeningException x) { if (loadMonitor == null) { throw; } loadMonitor.UpdateProgress(status.ChangeErrorException(x)); dbPeptides = new DbIrtPeptide[0]; return(null); } }
public static bool Match(DbIrtPeptide x, DbIrtPeptide y, double?irtTolerance) { return(Equals(SequenceMassCalc.NormalizeModifiedSequence(x.PeptideModSeq), SequenceMassCalc.NormalizeModifiedSequence(y.PeptideModSeq)) && (!irtTolerance.HasValue || Math.Abs(x.Irt - y.Irt) < irtTolerance.Value)); }