public static void ReadInTargets(string filePath, ThermoRawFile rawFile) { targetPeptides = new List <LFPeptide>(); using (var csv = new CsvReader(new StreamReader(filePath), true, '\t')) { int fieldCount = csv.FieldCount; string[] headers = csv.GetFieldHeaders(); //"Spectrum number" //"Peptide" //"Charge" //"Precursor Theoretical m/z (Th)" while (csv.ReadNextRecord()) { var pep = new LFPeptide { ms2ScanNumber = int.Parse(csv["Spectrum Number"]), sequence = csv["Peptide"], charge = int.Parse(csv["Charge"]), TheoreticalMZ = double.Parse(csv["Precursor Theoretical m/z (Th)"]) }; pep.parentMS1 = rawFile.GetParentSpectrumNumber(pep.ms2ScanNumber); pep.parentMS1Time = rawFile.GetRetentionTime(pep.parentMS1); pep.UserMZ = rawFile.GetPrecursorMz(pep.ms2ScanNumber); targetPeptides.Add(pep); } } }
public LFPeptide psmToLFPeptide(PSM psm, ThermoRawFile rawFile) { var pep = new LFPeptide { ms2ScanNumber = psm.scanNumber, sequence = psm.peptide.Sequence, charge = psm.charge, TheoreticalMZ = psm.mzCalc }; pep.parentMS1 = rawFile.GetParentSpectrumNumber(pep.ms2ScanNumber); pep.parentMS1Time = rawFile.GetRetentionTime(pep.parentMS1); //pep.UserMZ = psm.mzObs; pep.UserMZ = rawFile.GetPrecursorMz(pep.ms2ScanNumber); return(pep); }
public static List <RTPeak> GetRollingAveragePeaks(LFPeptide targetPeptide, int period, bool isLibrary = true) { //add three null points to the beginning and end of the peptide //you should update this so you can change the smoothing RTPeak start = new RTPeak(0, 0, 0); RTPeak end = new RTPeak(0, 0, 100000); List <RTPeak> copyPeaks = new List <RTPeak>(); if (isLibrary) { copyPeaks.Add(start); copyPeaks.Add(start); copyPeaks.Add(start); copyPeaks.Add(start); foreach (var peak in targetPeptide.XICLibrary) { copyPeaks.Add(peak); } copyPeaks.Add(end); copyPeaks.Add(end); copyPeaks.Add(end); copyPeaks.Add(end); copyPeaks = FillInXICGaps(copyPeaks); return(GetRollingAveragePeaks(copyPeaks, 7)); } else { copyPeaks.Add(start); copyPeaks.Add(start); copyPeaks.Add(start); copyPeaks.Add(start); foreach (var peak in targetPeptide.XICExperiment) { copyPeaks.Add(peak); } copyPeaks.Add(end); copyPeaks.Add(end); copyPeaks.Add(end); copyPeaks.Add(end); copyPeaks = FillInXICGaps(copyPeaks); return(GetRollingAveragePeaks(copyPeaks, 7)); } }
public static void GetFWHMWindow(LFPeptide peptide) { var apexPeak = peptide.apexPeakLibrary; var threshold = apexPeak.Intensity * .5; var startIndex = peptide.SmoothLibrary.BinarySearch(apexPeak); if (startIndex < 0) { startIndex = ~startIndex; } RTPeak LeftFWHM = null; RTPeak RightFWHM = null; for (int i = startIndex; i < peptide.SmoothLibrary.Count; i++) { var currPeak = peptide.SmoothLibrary[i]; if (currPeak.Intensity < threshold) { break; } RightFWHM = currPeak; } for (int i = startIndex; i >= 0; i--) { var currPeak = peptide.SmoothLibrary[i]; if (currPeak.Intensity < threshold) { break; } LeftFWHM = currPeak; } peptide.LeftFWHM = LeftFWHM; peptide.RightFWHM = RightFWHM; }
public List <PSM> crunch(List <PSM> psms) { var rawFile = new ThermoRawFile(rawpath); rawFile.Open(); List <int> msOneScanNumbers = new List <int>(); foreach (var scan in rawFile) { if (scan.MsnOrder == 1) { msOneScanNumbers.Add(scan.SpectrumNumber); } } //ReadInTargets(peptidesPath, rawFile); foreach (PSM psm in psms) { LFPeptide pep = psmToLFPeptide(psm, rawFile); targetPeptides.Add(pep); } // Question: how do we sync the list of PSMs with the intensity of the LFPeptide // They will be in same order List <LFPeptide> pepsWithSmooth = new List <LFPeptide>(); if (rawFile.GetMzAnalyzer(msOneScanNumbers[0]).Equals(MZAnalyzerType.Orbitrap)) { var firstSpectrumRT = rawFile.GetRetentionTime(rawFile.FirstSpectrumNumber); var lastSpectrumRT = rawFile.GetRetentionTime(rawFile.LastSpectrumNumber); foreach (var pep in targetPeptides) { var startTime = Math.Max(pep.parentMS1Time - 2, firstSpectrumRT); var stopTime = Math.Min(pep.parentMS1Time + 2, lastSpectrumRT); pep.startLookupTime = startTime; pep.stopLookupTime = stopTime; pep.FirstScan = rawFile.GetSpectrumNumber(pep.startLookupTime); pep.LastScan = rawFile.GetSpectrumNumber(pep.stopLookupTime); pep.lookupRange = DoubleRange.FromPPM(pep.UserMZ, 10); } foreach (var ms1 in msOneScanNumbers) { var spectrum = rawFile.GetSpectrum(ms1); GetXICs(spectrum, ms1, rawFile.GetRetentionTime(ms1)); rawFile.ClearCachedScans(); List <LFPeptide> peptidesForExtract = targetPeptides.Where(x => x.doneBuildingXIC && !x.extractedXIC).ToList(); foreach (var pep in peptidesForExtract) { try { pep.SmoothLibrary = Smoothing.GetRollingAveragePeaks(pep, 11, true); if (pep.SmoothLibrary.Count != 0) { ExtractFeatures.GetApexPeak(pep, true); ExtractFeatures.GetFWHMWindow(pep); LFPeptide pepSmooth = (LFPeptide)pep.Clone(); pepsWithSmooth.Add(pepSmooth); pep.XICLibrary.Clear(); } }catch (Exception e) {/** * System.Windows.Forms.MessageBox.Show("XICLibraryCount: " + pep.XICLibrary.Count + "Peptide: " + pep.sequence + "\n" + e.ToString()); **/ } } } List <LFPeptide> peptidesToWrite = targetPeptides.Where(x => x.doneBuildingXIC && !x.extractedXIC).ToList(); //Changed from psms.Count to pepswithsmooth.Count //for(int i = 0; i < psms.Count; i++) for (int i = 0; i < pepsWithSmooth.Count; i++) { /** * if(psms[i].scanTime == 20.45) * { * double[] xs = new double[pepsWithSmooth[i].SmoothLibrary.Count]; * double[] ys = new double[pepsWithSmooth[i].SmoothLibrary.Count]; * * StreamWriter writer = new StreamWriter(@"C:\Users\gwilson\Desktop\GlycoQuant - Injs\TLN[NGlycan_1216.42286271]CSGAHVK_0.5_2_PolynomialFit.csv"); * for (int k = 0; k < pepsWithSmooth[i].SmoothLibrary.Count; k++) * { * xs[k] = pepsWithSmooth[i].SmoothLibrary[k].RT; * ys[k] = pepsWithSmooth[i].SmoothLibrary[k].Intensity; * * //writer.WriteLine(pepsWithSmooth[i].SmoothLibrary[k].RT + "," + pepsWithSmooth[i].SmoothLibrary[k].Intensity); * * * if (pepsWithSmooth[i].SmoothLibrary[k].RT > targetPeptides[i].LeftFWHM.RT && pepsWithSmooth[i].SmoothLibrary[k].RT < targetPeptides[i].RightFWHM.RT) * { * writer.WriteLine(pepsWithSmooth[i].SmoothLibrary[k].RT + "," + pepsWithSmooth[i].SmoothLibrary[k].Intensity); * } * * } * * double[] p = Fit.Polynomial(xs, ys, 3) * * for(int j = 0; j < p.Length; j++) * { * //writer.WriteLine() * } * * writer.Close(); * } **/ double intensity = 0; try { for (int j = 0; j < pepsWithSmooth[i].SmoothLibrary.Count - 1; j++) { // Intensity is the sum of peak intensities from Left FWHM to Right FWHM /** * if (pepsWithSmooth[i].SmoothLibrary[j].RT > targetPeptides[i].LeftFWHM.RT && pepsWithSmooth[i].SmoothLibrary[j].RT < targetPeptides[i].RightFWHM.RT) * { * intensity += pepsWithSmooth[i].SmoothLibrary[j].Intensity; * } **/ // Retention times double RT1 = pepsWithSmooth[i].SmoothLibrary[j].RT; double RT2 = pepsWithSmooth[i].SmoothLibrary[j + 1].RT; // Intensities double int1 = pepsWithSmooth[i].SmoothLibrary[j].Intensity; double int2 = pepsWithSmooth[i].SmoothLibrary[j + 1].Intensity; //Rectangle area double rectArea = (RT2 - RT1) * Math.Min(int1, int2); //Triangle Area double triArea = ((RT2 - RT1) * Math.Abs(int1 - int2)) / 2; intensity += rectArea + triArea; } } catch (Exception e) { Console.ReadKey(); } psms[i].intensity = intensity; //psms[i].intensity = targetPeptides[i].apexPeakLibrary.Intensity; } rawFile.Dispose(); return(psms); } rawFile.Dispose(); return(psms); }
public static void GetApexPeak(LFPeptide targetPeptide, bool isLibrary = true) { List <RTPeak> XICPeaks = new List <RTPeak>(); int startIndex = 0; if (isLibrary) { XICPeaks.AddRange(targetPeptide.SmoothLibrary); startIndex = XICPeaks.BinarySearch(new RTPeak(0, 0, targetPeptide.parentMS1Time)); if (startIndex < 0) { startIndex = ~startIndex; } } else { XICPeaks.AddRange(targetPeptide.SmoothExperiment); startIndex = XICPeaks.BinarySearch(targetPeptide.ExperimentLookupPeak); if (startIndex < 0) { startIndex = ~startIndex; } } int leftIndex = 0; int apexIndex = startIndex; int rightIndex = 0; double sumLeft = 0; double sumRight = 0; for (int i = 1; i <= 5; i++) { if ((apexIndex - i) >= 0) { sumLeft += XICPeaks[apexIndex - i].Intensity; } if ((apexIndex + i) < XICPeaks.Count) { sumRight += XICPeaks[apexIndex + i].Intensity; } } if (sumLeft == 0 && sumRight == 0 && isLibrary) { targetPeptide.apexPeakLibrary = new RTPeak(targetPeptide.AdjustedMZ_Library, 0, targetPeptide.parentMS1Time); targetPeptide.rightPeakLibrary = new RTPeak(targetPeptide.AdjustedMZ_Library, 0, targetPeptide.parentMS1Time); targetPeptide.leftPeakLibrary = new RTPeak(targetPeptide.AdjustedMZ_Library, 0, targetPeptide.parentMS1Time); return; } if (sumLeft == 0 && sumRight == 0 && !isLibrary) { targetPeptide.apexPeakExperiment = new RTPeak(targetPeptide.AdjustedMZ_Experiment, 0, targetPeptide.parentMS1Time); targetPeptide.rightPeakExperiment = new RTPeak(targetPeptide.AdjustedMZ_Experiment, 0, targetPeptide.parentMS1Time); targetPeptide.leftPeakExperiment = new RTPeak(targetPeptide.AdjustedMZ_Experiment, 0, targetPeptide.parentMS1Time); return; } try { if (sumRight > sumLeft) //assume that the max lies to the right { bool isAscending = true; bool isDescending = false; RTPeak currentMax = XICPeaks[startIndex]; double threshold = currentMax.Intensity * 0.01; RTPeak currentMinRight = null; RTPeak currentMinLeft = null; int currentIndex = 1 + startIndex; while (isAscending) { if (currentIndex < XICPeaks.Count) { var currentPeak = XICPeaks[currentIndex]; if (currentPeak.Intensity > currentMax.Intensity) //Reset max and threshold, continue on { currentMax = currentPeak; threshold = currentMax.Intensity * 0.01; currentIndex++; continue; } else //Could be that I was at the apex, or I'm in a dip { if (currentPeak.Intensity < (currentMax.Intensity * (1.0 / 1.8))) //I was at the max, so I want to keep this point { //and start looking for the right minimum isAscending = false; isDescending = true; break; } else //could be the start of the descent or some weird dip. Look ahead to the next peaks to find out. { int AdditionalCounter = 1; while (true) //keep in this loop until one of the conditions is satisfied. { int aheadIndex = currentIndex + AdditionalCounter; if ((aheadIndex) < XICPeaks.Count) { var aheadPeak = XICPeaks[aheadIndex]; if (aheadPeak.Intensity < (currentMax.Intensity * (1.0 / 1.8))) { isAscending = false; isDescending = true; break; } if (aheadPeak.Intensity > currentMax.Intensity) { currentMax = aheadPeak; currentIndex += AdditionalCounter; //currentIndex++; break; } AdditionalCounter++; } else { isAscending = false; isDescending = true; break; } } } } } } //you have located the max. assume that the peak to right is the local minima currentIndex++; if (currentIndex < XICPeaks.Count) { currentMinRight = XICPeaks[currentIndex]; } currentIndex++; while (isDescending) { if (currentIndex < XICPeaks.Count) { var currentPeak = XICPeaks[currentIndex]; if (currentPeak.Intensity <= currentMinRight.Intensity) { currentMinRight = currentPeak; if (currentPeak.Intensity < threshold) { isDescending = false; break; } } if (currentPeak.Intensity > currentMinRight.Intensity) //could be the local min or some weird dip { int AdditionalCounter = 1; while (true) { int aheadIndex = currentIndex + AdditionalCounter; if ((aheadIndex) < XICPeaks.Count) { var aheadPeak = XICPeaks[aheadIndex]; if (aheadPeak.Intensity > (1.8 * currentMinRight.Intensity)) //There is a new peak coming up. You have found the local min. Break out. { isDescending = false; break; } if (aheadPeak.Intensity < currentMinRight.Intensity || aheadPeak.Intensity == 0) { currentMinRight = aheadPeak; currentIndex += AdditionalCounter; //currentIndex++; break; } AdditionalCounter++; } else { isDescending = false; break; } } } } else { isDescending = false; break; } currentIndex++; } //you now have the max, and the right minima. Assume that the peak to the left of the start point is the left minima. currentIndex = startIndex - 1; if (currentIndex >= 0) { currentMinLeft = XICPeaks[currentIndex]; } currentIndex--; isDescending = true; while (isDescending) { if (currentIndex >= 0) { var currentPeak = XICPeaks[currentIndex]; if (currentPeak.Intensity <= currentMinLeft.Intensity) { currentMinLeft = currentPeak; if (currentPeak.Intensity < threshold) { isDescending = false; break; } } if (currentPeak.Intensity > currentMinLeft.Intensity) //could be the local min or some weird dip { int AdditionalCounter = 1; while (true) { int aheadIndex = currentIndex - AdditionalCounter; if ((aheadIndex) >= 0) { var aheadPeak = XICPeaks[aheadIndex]; if (aheadPeak.Intensity > (1.8 * currentMinLeft.Intensity)) //There is a new peak coming up. You have found the local min. Break out. { isDescending = false; break; } if (aheadPeak.Intensity < currentMinLeft.Intensity || aheadPeak.Intensity == 0) { currentMinLeft = aheadPeak; currentIndex -= AdditionalCounter; //currentIndex--; break; } AdditionalCounter++; } else { isDescending = false; break; } } } currentIndex--; } else { isDescending = false; } } if (isLibrary) { targetPeptide.apexPeakLibrary = currentMax; targetPeptide.leftPeakLibrary = currentMinLeft; targetPeptide.rightPeakLibrary = currentMinRight; } else { targetPeptide.apexPeakExperiment = currentMax; targetPeptide.leftPeakExperiment = currentMinLeft; targetPeptide.rightPeakExperiment = currentMinRight; } } else //assume the max lies to the left; { bool isAscending = true; bool isDescending = false; RTPeak currentMax = XICPeaks[startIndex]; double threshold = currentMax.Intensity * 0.01; RTPeak currentMinRight = null; RTPeak currentMinLeft = null; int currentIndex = startIndex - 1; while (isAscending) { if (currentIndex >= 0) { var currentPeak = XICPeaks[currentIndex]; if (currentPeak.Intensity > currentMax.Intensity) //Reset max and threshold, continue on { currentMax = currentPeak; threshold = currentMax.Intensity * 0.01; currentIndex--; continue; } else //Could be that I was at the apex, or I'm in a dip { if (currentPeak.Intensity < (currentMax.Intensity * (1.0 / 1.8))) //I was at the max, so I want to keep this point { //and start looking for the right minimum isAscending = false; isDescending = true; break; } else //could be the start of the descent or some weird dip. Look ahead to the next peaks to find out. { int AdditionalCounter = 1; while (true) //keep in this loop until one of the conditions is satisfied. { int aheadIndex = currentIndex - AdditionalCounter; if ((aheadIndex) >= 0) { var aheadPeak = XICPeaks[aheadIndex]; if (aheadPeak.Intensity < (currentMax.Intensity * (1.0 / 1.8))) { isAscending = false; isDescending = true; break; } if (aheadPeak.Intensity > currentMax.Intensity) { currentMax = aheadPeak; currentIndex -= AdditionalCounter; //currentIndex--; break; } AdditionalCounter++; } else { isAscending = false; isDescending = true; break; } } } } } } currentIndex--; if (currentIndex >= 0) { currentMinLeft = XICPeaks[currentIndex]; } currentIndex--; isDescending = true; while (isDescending) { if (currentIndex >= 0) { var currentPeak = XICPeaks[currentIndex]; if (currentPeak.Intensity <= currentMinLeft.Intensity) { currentMinLeft = currentPeak; if (currentPeak.Intensity < threshold) { isDescending = false; break; } } if (currentPeak.Intensity > currentMinLeft.Intensity) //could be the local min or some weird dip { int AdditionalCounter = 1; while (true) { int aheadIndex = currentIndex - AdditionalCounter; if ((aheadIndex) >= 0) { var aheadPeak = XICPeaks[aheadIndex]; if (aheadPeak.Intensity > (1.8 * currentMinLeft.Intensity)) //There is a new peak coming up. You have found the local min. Break out. { isDescending = false; break; } if (aheadPeak.Intensity < currentMinLeft.Intensity || aheadPeak.Intensity == 0) { currentMinLeft = aheadPeak; currentIndex -= AdditionalCounter; //currentIndex--; break; } AdditionalCounter++; } else { isDescending = false; break; } } } currentIndex--; } else { isDescending = false; } } isDescending = true; currentIndex = startIndex + 1; if (currentIndex < XICPeaks.Count) { currentMinRight = XICPeaks[currentIndex]; } currentIndex++; while (isDescending) { if (currentIndex < XICPeaks.Count) { var currentPeak = XICPeaks[currentIndex]; if (currentPeak.Intensity <= currentMinRight.Intensity) { currentMinRight = currentPeak; if (currentPeak.Intensity < threshold) { isDescending = false; break; } } if (currentPeak.Intensity > currentMinRight.Intensity) //could be the local min or some weird dip { int AdditionalCounter = 1; while (true) { int aheadIndex = currentIndex + AdditionalCounter; if ((aheadIndex) < XICPeaks.Count) { var aheadPeak = XICPeaks[aheadIndex]; if (aheadPeak.Intensity > (1.8 * currentMinRight.Intensity)) //There is a new peak coming up. You have found the local min. Break out. { isDescending = false; break; } if (aheadPeak.Intensity < currentMinRight.Intensity || aheadPeak.Intensity == 0) { currentMinRight = aheadPeak; currentIndex += AdditionalCounter; //currentIndex++; break; } AdditionalCounter++; } else { isDescending = false; break; } } } currentIndex++; } else { isDescending = false; } } if (isLibrary) { targetPeptide.apexPeakLibrary = currentMax; targetPeptide.leftPeakLibrary = currentMinLeft; targetPeptide.rightPeakLibrary = currentMinRight; } else { targetPeptide.apexPeakExperiment = currentMax; targetPeptide.leftPeakExperiment = currentMinLeft; targetPeptide.rightPeakExperiment = currentMinRight; } } } catch (Exception e) { if (isLibrary) { targetPeptide.apexPeakLibrary = new RTPeak(targetPeptide.AdjustedMZ_Library, 0, targetPeptide.parentMS1Time); targetPeptide.leftPeakLibrary = new RTPeak(targetPeptide.AdjustedMZ_Library, 0, targetPeptide.parentMS1Time); targetPeptide.rightPeakLibrary = new RTPeak(targetPeptide.AdjustedMZ_Library, 0, targetPeptide.parentMS1Time); } else { targetPeptide.apexPeakExperiment = new RTPeak(targetPeptide.AdjustedMZ_Library, 0, targetPeptide.parentMS1Time); targetPeptide.leftPeakExperiment = new RTPeak(targetPeptide.AdjustedMZ_Library, 0, targetPeptide.parentMS1Time); targetPeptide.rightPeakExperiment = new RTPeak(targetPeptide.AdjustedMZ_Library, 0, targetPeptide.parentMS1Time); } return; } }