Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed case of CiRT peptides not covering RT range #2165

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
58e66ff
Fixed case of CiRT peptides not covering RT range
kaipot Jul 19, 2022
966c2e3
Add CiRT_all.sky
kaipot Jul 20, 2022
050efd7
Fixed bug generating DocXml with duplicate peptides in document
kaipot Jul 26, 2022
70188f7
Added CiRTFunctionalTest
kaipot Jul 26, 2022
3f37739
Merge branch 'master' into Skyline/work/20220719_cirt_fix
kaipot Jul 26, 2022
eb9b7e3
Merge branch 'master' into Skyline/work/20220719_cirt_fix
kaipot Jul 27, 2022
58c631e
Merge branch 'master' into Skyline/work/20220719_cirt_fix
kaipot Aug 2, 2022
5d88917
Fixes and change CiRT test to test all iRT documents
kaipot Jul 27, 2022
5af44cf
Merge branch 'master' into Skyline/work/20220719_cirt_fix
kaipot Sep 20, 2022
c9458a5
Merge remote-tracking branch 'remotes/origin/master' into Skyline/wor…
brendanx67 Aug 8, 2024
a266c02
Merge remote-tracking branch 'remotes/origin/master' into Skyline/wor…
brendanx67 Aug 8, 2024
2650240
- run AssortResources to move new strings
brendanx67 Aug 9, 2024
8e61fc4
- fix GetTestPath() usage to use GetTestResultsPath() for better cleanup
brendanx67 Aug 9, 2024
784f66c
- attempt to revert empty Resources.resx changes
brendanx67 Aug 9, 2024
3349b1a
- Checkpoint: fix to work for the use case in mind and added testing …
brendanx67 Aug 30, 2024
6d67be9
Merge branch 'Skyline/work/20220719_cirt_fix' of github.com:ProteoWiz…
brendanx67 Aug 30, 2024
517693b
Merge branch 'master' into Skyline/work/20220719_cirt_fix
brendanx67 Aug 30, 2024
c0b094d
- fixed IrtTest to handle working save iRT document
brendanx67 Sep 7, 2024
f3348cc
Merge branch 'Skyline/work/20220719_cirt_fix' of github.com:ProteoWiz…
brendanx67 Sep 7, 2024
8d46343
Merge branch 'master' into Skyline/work/20220719_cirt_fix
brendanx67 Sep 7, 2024
601814d
- removed unused using clause
brendanx67 Sep 7, 2024
ea5d10e
Merge branch 'master' into Skyline/work/20220719_cirt_fix
brendanx67 Sep 18, 2024
db5467d
Merge branch 'master' into Skyline/work/20220719_cirt_fix
brendanx67 Sep 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 44 additions & 46 deletions pwiz_tools/Skyline/Model/Irt/IrtPeptidePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public MeasuredPeptide(MeasuredPeptide other) : this(other.Target, other.Retenti

public Target Target { get; set; }
public double RetentionTime { get; set; }
public string Sequence { get { return Target == null ? string.Empty : Target.ToSerializableString(); } }
public string Sequence => Target?.ToSerializableString() ?? string.Empty;

public static string ValidateSequence(Target sequence)
{
Expand All @@ -63,8 +63,7 @@ public static string ValidateSequence(Target sequence)

public static string ValidateRetentionTime(string rtText, bool allowNegative)
{
double rtValue;
if (rtText == null || !double.TryParse(rtText, out rtValue))
if (rtText == null || !double.TryParse(rtText, out var rtValue))
return Resources.MeasuredPeptide_ValidateRetentionTime_Measured_retention_times_must_be_valid_decimal_numbers;
if (!allowNegative && rtValue <= 0)
return Resources.MeasuredPeptide_ValidateRetentionTime_Measured_retention_times_must_be_greater_than_zero;
Expand All @@ -87,7 +86,7 @@ public IrtPeptidePicker()
}

public bool HasScoredPeptides => _scoredPeptides != null && _scoredPeptides.Length > 0;
public int CirtPeptideCount => _cirtPeptides != null ? _cirtPeptides.Length : 0;
public int CirtPeptideCount => _cirtPeptides?.Length ?? 0;
private double MinRt => _scoredPeptides.First().Peptide.RetentionTime;
private double MaxRt => _scoredPeptides.Last().Peptide.RetentionTime;
private double RtRange => MaxRt - MinRt;
Expand Down Expand Up @@ -138,36 +137,23 @@ public void ScorePeptides(SrmDocument doc, IProgressMonitor progressMonitor)
_cirtPeptides = _scoredPeptides.Where(pep => _cirtAll.ContainsKey(pep.Peptide.Target)).ToArray();
}

public bool TryGetCirtRegression(int count, out RegressionLine regression, out IEnumerable<Tuple<DbIrtPeptide, PeptideDocNode>> matchedPeptides)
public CirtRegressionResult GetCirtRegressionResult(int count)
{
matchedPeptides = null;
var success = TryGetCirtRegression(count, out regression, out List<ScoredPeptide> peptides);
if (success)
{
matchedPeptides = peptides.Select(pep => Tuple.Create(
new DbIrtPeptide(pep.Peptide.Target, _cirtAll[pep.Peptide.Target], true, TimeSource.peak),
pep.NodePep));
}
return success;
}

private bool TryGetCirtRegression(int count, out RegressionLine regression, out List<ScoredPeptide> peptides)
{
peptides = new List<ScoredPeptide>(_cirtPeptides);
var rts = _cirtPeptides.Select(pep => pep.Peptide.RetentionTime).ToList();
var irts = _cirtPeptides.Select(pep => _cirtAll[pep.Peptide.Target]).ToList();
var removedValues = new List<Tuple<double, double>>();
var success = IrtRegression.TryGet<RegressionLine>(rts, irts, count, out var line, removedValues);
regression = (RegressionLine) line;
if (!success)
return false;

if (!IrtRegression.TryGet<RegressionLine>(rts, irts, count, out var line, removedValues))
return new CirtRegressionResult(null, null,null, false);

var peptides = new List<ScoredPeptide>(_cirtPeptides);
for (var i = peptides.Count - 1; i >= 0; i--)
{
if (removedValues.Contains(Tuple.Create(rts[i], irts[i])))
peptides.RemoveAt(i);
}
return peptides.Count >= count;
var coversRtRange = PeptideBucket<ScoredPeptide>.BucketPeptides(peptides, BucketBoundaries).All(bucket => !bucket.Empty);
return new CirtRegressionResult((RegressionLine)line, peptides, _cirtAll, coversRtRange);
}

/// <summary>
Expand All @@ -180,21 +166,13 @@ private bool TryGetCirtRegression(int count, out RegressionLine regression, out
/// <param name="count">The number of peptides to be picked</param>
/// <param name="exclude">Peptides that cannot be picked</param>
/// <param name="cirt">Use CiRT peptides, if possible</param>
public List<MeasuredPeptide> Pick(int count, ICollection<Target> exclude, bool cirt)
public List<MeasuredPeptide> Pick(int count, ICollection<Target> exclude, CirtRegressionResult cirt)
{
PeptideBucket<ScoredPeptide>[] buckets = null;
if (cirt && TryGetCirtRegression(count, out _, out List<ScoredPeptide> scoredCirtPeptides))
{
// If each bucket contains at least one, prompt to use CiRT peptides
var cirtBuckets = PeptideBucket<ScoredPeptide>.BucketPeptides(scoredCirtPeptides, BucketBoundaries);
if (cirtBuckets.All(bucket => !bucket.Empty))
buckets = cirtBuckets;
}
var bucketPeps = cirt != null && cirt.Valid ? cirt.Peptides.AsEnumerable() : _scoredPeptides;
if (exclude != null && exclude.Count > 0)
bucketPeps = bucketPeps.Where(pep => !exclude.Contains(pep.Peptide.Target));

if (buckets == null)
buckets = exclude == null || exclude.Count == 0
? PeptideBucket<ScoredPeptide>.BucketPeptides(_scoredPeptides, BucketBoundaries)
: PeptideBucket<ScoredPeptide>.BucketPeptides(_scoredPeptides.Where(pep => !exclude.Contains(pep.Peptide.Target)), BucketBoundaries);
var buckets = PeptideBucket<ScoredPeptide>.BucketPeptides(bucketPeps, BucketBoundaries);
var endBuckets = new[] { buckets.First(), buckets.Last() };
var midBuckets = buckets.Skip(1).Take(buckets.Length - 2).ToArray();

Expand Down Expand Up @@ -276,13 +254,35 @@ public static void SetStandards(IEnumerable<DbIrtPeptide> peptides, IrtStandard
SetStandards(peptides, standard.Peptides.Select(pep => pep.ModifiedTarget));
}

public class CirtRegressionResult
{
public RegressionLine Regression { get; }
public ImmutableList<ScoredPeptide> Peptides { get; }
private TargetMap<double> Irts { get; }
public bool CoversRTRange { get; }

public bool Valid => Regression != null && CoversRTRange;
public int Count => Peptides?.Count ?? 0;
public IEnumerable<DbIrtPeptide> DbIrtPeptides => Peptides.Select(pep =>
new DbIrtPeptide(pep.Peptide.Target, Irts[pep.Peptide.Target], true, TimeSource.peak));
public IEnumerable<PeptideDocNode> NodePeps => Peptides.Select(pep => pep.NodePep);

public CirtRegressionResult(RegressionLine regression, IEnumerable<ScoredPeptide> peptides, TargetMap<double> irts, bool coversRtRange)
{
Regression = regression;
Peptides = ImmutableList<ScoredPeptide>.ValueOf(peptides);
Irts = irts;
CoversRTRange = coversRtRange;
}
}

private interface IBucketable
{
double Time { get; }
float Score { get; } // lower scores get picked first
}

private class ScoredPeptide : IBucketable
public class ScoredPeptide : IBucketable
{
public MeasuredPeptide Peptide { get; }
public PeptideDocNode NodePep { get; }
Expand Down Expand Up @@ -319,23 +319,21 @@ private PeptideBucket(double maxTime)
private T Pop()
{
if (Empty)
return default(T);
return default;
var pep = _peptides.First();
_peptides.RemoveAt(0);
return pep;
}

public static PeptideBucket<T>[] BucketPeptides(IEnumerable<T> peptides, IEnumerable<double> rtBoundaries)
{
// peptides must be sorted by retention time (low to high)
var buckets = rtBoundaries.OrderBy(x => x).Select(boundary => new PeptideBucket<T>(boundary)).ToArray();
var curBucketIdx = 0;
var curBucket = buckets[0];
foreach (var pep in peptides)
var curBucket = 0;
foreach (var pep in peptides.OrderBy(pep => pep.Time))
{
if (pep.Time > curBucket._maxTime)
curBucket = buckets[++curBucketIdx];
curBucket._peptides.Add(pep);
if (pep.Time > buckets[curBucket]._maxTime)
curBucket++;
buckets[curBucket]._peptides.Add(pep);
}
buckets.ForEach(bucket => bucket._peptides.Sort((x, y) => x.Score.CompareTo(y.Score)));
return buckets;
Expand Down
2 changes: 1 addition & 1 deletion pwiz_tools/Skyline/Model/Irt/IrtStandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public class IrtStandard : XmlNamedElement
MakePeptide(@"DSTLIMQLLR", 101.79),
});

public static readonly IrtStandard CIRT = new IrtStandard(@"CiRT", null, null,
public static readonly IrtStandard CIRT = new IrtStandard(@"CiRT", @"CiRT_all.sky", null,
new[] {
MakePeptide(@"ADTLDPALLRPGR", 35.99),
MakePeptide(@"AFEEAEK", -21.36),
Expand Down
Loading