public override IEnumerable <AllowedIntervalWithNotch> GetAllowedPrecursorMassIntervalsFromTheoreticalMass(double peptideMonoisotopicMass) { for (int j = 0; j < AcceptableSortedMassShifts.Length; j++) { yield return(new AllowedIntervalWithNotch(Tolerance.GetRange(peptideMonoisotopicMass + AcceptableSortedMassShifts[j]), j)); } }
public void ToleranceNewTest() { var tol = new Tolerance(ToleranceUnit.Absolute, 9, 10); Assert.AreEqual(4, tol.GetRange(5).Minimum); Assert.AreEqual(6, tol.GetRange(5).Maximum); }
public static double GetPValue(PSM psm, Tolerance prod_tolerance, double cutoff) { // Get the width of the product ion tolerance at the isolation mz; double productToleranceWidth = prod_tolerance.GetRange(psm.IsolationMZ).Width; double cutoffThreshold = psm.Spectrum.GetBasePeakIntensity() * cutoff; return(Math.Min(1.0, psm.Spectrum.Count(peak => peak.Intensity >= cutoffThreshold) * 2 * productToleranceWidth / psm.ScanWidth)); }
public void TolerancePPMGetRange() { var tol = new Tolerance(ToleranceUnit.PPM, 1e6 - 1, 1e6); Assert.AreEqual(20, tol.GetRange(1e7).Width); Assert.AreEqual("±1.0000 PPM", tol.ToString()); }
public IEnumerable<Fragment> MatchFragments(IEnumerable<Fragment> fragments, Tolerance tolerance, double percentCutoff, params int[] chargeStates) { double basePeakInt = MassSpectrum.GetBasePeakIntensity(); double lowThreshold = basePeakInt*percentCutoff; double summedIntensity = 0; double totalIntensity = MassSpectrum.GetTotalIonCurrent(); foreach (Fragment fragment in fragments) { foreach (int chargeState in chargeStates) { double mz = fragment.ToMz(chargeState); var peak = MassSpectrum.GetClosestPeak(tolerance.GetRange(mz)); if (peak != null && peak.Intensity >= lowThreshold) { Add(new FragmentSpectralMatch(MassSpectrum, fragment, tolerance, chargeState)); yield return fragment; summedIntensity += peak.Intensity; } } } PercentTIC = 100.0 * summedIntensity / totalIntensity; }
public IEnumerable <Fragment> MatchFragments(IEnumerable <Fragment> fragments, Tolerance tolerance, double percentCutoff, params int[] chargeStates) { double basePeakInt = Spectrum.GetBasePeakIntensity(); double lowThreshold = basePeakInt * percentCutoff; double summedIntensity = 0; double totalIntensity = Spectrum.GetTotalIonCurrent(); foreach (Fragment fragment in fragments) { foreach (int chargeState in chargeStates) { double mz = fragment.ToMz(chargeState); var peak = Spectrum.GetClosestPeak(tolerance.GetRange(mz)); if (peak != null && peak.Intensity >= lowThreshold) { Add(new FragmentSpectralMatch(Spectrum, fragment, tolerance, chargeState)); yield return(fragment); summedIntensity += peak.Intensity; } } } PercentTIC = 100.0 * summedIntensity / totalIntensity; }
//private double Search(ref double[] eMasses, ref double[] eIntenisties, double[] tMasses, double productTolerance, double tic, ref Dictionary<double, double> scores) //{ // double score = 0.0; // int eLength = eMasses.Length; // int tLength = tMasses.Length; // int e = 0; // foreach (double t in tMasses) // { // double storedScore; // if (scores.TryGetValue(t, out storedScore)) // { // score += storedScore; // continue; // } // double minMZ = t - productTolerance; // double maxMZ = t + productTolerance; // while (e < eLength && eMasses[e] < minMZ) // e++; // if (e >= eLength) // break; // if (eMasses[e] > maxMZ) // continue; // double intensities = 0; // int index = e; // switch variables to keep e the same for the next loop around // do // { // intensities += eIntenisties[index]; // index++; // } while (index < eLength && eMasses[index] < maxMZ); // storedScore = 1 + intensities/tic; // score += storedScore; // scores[t] = storedScore; // } // return score; //} /// <summary> /// The main searching algorithm of Morpheus /// </summary> /// <param name="eMasses">The experimental masses</param> /// <param name="eIntenisties">The experimental intensities</param> /// <param name="tMasses">The theoretical masses</param> /// <param name="productTolerance">The product mass tolerance</param> /// <param name="tic">The total ion current of the experimental peaks</param> /// <returns></returns> private double Search(double[] eMasses, double[] eIntenisties, double[] tMasses, Tolerance productTolerance, double tic) { double score = 0.0; double intensities = 0.0; int eLength = eMasses.Length; int tLength = tMasses.Length; int e = 0; bool forceCheck = productTolerance.GetMinimumValue(tMasses[tLength - 1]) >= eMasses[eLength - 1]; if (forceCheck) { foreach (double t in tMasses) { IRange<double> range = productTolerance.GetRange(t); double minMZ = range.Minimum; double maxMZ = range.Maximum; while (e < eLength && eMasses[e] < minMZ) e++; if (e >= eLength) break; if (eMasses[e] > maxMZ) continue; score++; int index = e; // switch variables to keep e the same for the next loop around do { intensities += eIntenisties[index]; index++; } while (index < eLength && eMasses[index] < maxMZ); } } else { foreach (double t in tMasses) { IRange<double> range = productTolerance.GetRange(t); double minMZ = range.Minimum; double maxMZ = range.Maximum; while (eMasses[e] < minMZ) e++; if (eMasses[e] > maxMZ) continue; score++; int index = e; // switch variables to keep e the same for the next loop around do { intensities += eIntenisties[index]; index++; } while (index < eLength && eMasses[index] < maxMZ); } } return score + intensities/tic; }
//private double Search(ref double[] eMasses, ref double[] eIntenisties, double[] tMasses, double productTolerance, double tic, ref Dictionary<double, double> scores) //{ // double score = 0.0; // int eLength = eMasses.Length; // int tLength = tMasses.Length; // int e = 0; // foreach (double t in tMasses) // { // double storedScore; // if (scores.TryGetValue(t, out storedScore)) // { // score += storedScore; // continue; // } // double minMZ = t - productTolerance; // double maxMZ = t + productTolerance; // while (e < eLength && eMasses[e] < minMZ) // e++; // if (e >= eLength) // break; // if (eMasses[e] > maxMZ) // continue; // double intensities = 0; // int index = e; // switch variables to keep e the same for the next loop around // do // { // intensities += eIntenisties[index]; // index++; // } while (index < eLength && eMasses[index] < maxMZ); // storedScore = 1 + intensities/tic; // score += storedScore; // scores[t] = storedScore; // } // return score; //} /// <summary> /// The main searching algorithm of Morpheus /// </summary> /// <param name="eMasses">The experimental masses</param> /// <param name="eIntenisties">The experimental intensities</param> /// <param name="tMasses">The theoretical masses</param> /// <param name="productTolerance">The product mass tolerance</param> /// <param name="tic">The total ion current of the experimental peaks</param> /// <returns></returns> private double Search(double[] eMasses, double[] eIntenisties, double[] tMasses, Tolerance productTolerance, double tic) { double score = 0.0; double intensities = 0.0; int eLength = eMasses.Length; int tLength = tMasses.Length; int e = 0; bool forceCheck = productTolerance.GetMinimumValue(tMasses[tLength - 1]) >= eMasses[eLength - 1]; if (forceCheck) { foreach (double t in tMasses) { IRange <double> range = productTolerance.GetRange(t); double minMZ = range.Minimum; double maxMZ = range.Maximum; while (e < eLength && eMasses[e] < minMZ) { e++; } if (e >= eLength) { break; } if (eMasses[e] > maxMZ) { continue; } score++; int index = e; // switch variables to keep e the same for the next loop around do { intensities += eIntenisties[index]; index++; } while (index < eLength && eMasses[index] < maxMZ); } } else { foreach (double t in tMasses) { IRange <double> range = productTolerance.GetRange(t); double minMZ = range.Minimum; double maxMZ = range.Maximum; while (eMasses[e] < minMZ) { e++; } if (eMasses[e] > maxMZ) { continue; } score++; int index = e; // switch variables to keep e the same for the next loop around do { intensities += eIntenisties[index]; index++; } while (index < eLength && eMasses[index] < maxMZ); } } return(score + intensities / tic); }
private IEnumerable <QuantFile> LoadFiles(IEnumerable <string> filePaths, bool ms3Quant = false) { MSDataFile.CacheScans = false; //int largestQuantPeak = 0; int i = 0; foreach (TagInformation tag in UsedTags.Values) { tag.UniqueTagNumber = i++; tag.TotalSignal = 0; tag.NormalizedTotalSignal = 0; } int largestQuantPeak = i - 1; //int largestQuantPeak = UsedTags.Values.Select(tag => tag.UniqueTagNumber).Concat(new[] {0}).Max(); foreach (string filePath in filePaths) { Log("Processing file:\t" + filePath); OnUpdateLog("Processing File " + filePath + "..."); QuantFile quantFile = new QuantFile(filePath); StreamReader basestreamReader = new StreamReader(filePath); int oldProgress = -1; using (CsvReader reader = new CsvReader(basestreamReader, true)) { while (reader.ReadNextRecord()) // go through csv and raw file to extract the info we want { int scanNumber = int.Parse(reader["Spectrum number"]); string filenameID = reader["Filename/id"]; string rawFileName = filenameID.Split('.')[0]; bool isDecoy = reader["DEFLINE"].StartsWith("DECOY_"); ThermoRawFile rawFile; if (!RawFiles.TryGetValue(rawFileName, out rawFile)) { throw new ArgumentException("Cannot find this raw file: " + rawFileName + ".raw"); } if (!rawFile.IsOpen) { rawFile.Open(); } int progress = (int)(100 * (double)basestreamReader.BaseStream.Position / basestreamReader.BaseStream.Length); if (progress != oldProgress) { OnProgressUpdate(progress); oldProgress = progress; } //// Set default fragmentation to CAD / HCD //FragmentationMethod ScanFragMethod = filenameID.Contains(".ETD.") // ? FragmentationMethod.ETD // : FragmentationMethod.CAD; //if (ScanFragMethod == FragmentationMethod.ETD) //{ // ScanFragMethod = FragmentationMethod.CAD; // scanNumber += ETDQuantPosition; //} // Get the scan object for the sequence ms2 scan MsnDataScan quantitationMsnScan = rawFile[scanNumber] as MsnDataScan; double purity = 1; if (CalculatePurity) { double mz = quantitationMsnScan.PrecursorMz; int charge = quantitationMsnScan.PrecursorCharge; DoubleRange isolationRange = MzRange.FromDa(mz, PurityWindowInTh); MSDataScan parentScan = rawFile[quantitationMsnScan.ParentScanNumber]; purity = DeterminePurity(parentScan, mz, charge, isolationRange); } if (quantitationMsnScan == null) { OnUpdateLog("Spectrum Number " + scanNumber + " is not a valid MS2 scan from: " + rawFile.FilePath + ". Skipping PSM..."); continue; } if (MS3Quant) { quantitationMsnScan = null; // Look forward to find associated MS3 quant scan (based on parent scan number) int ms3ScanNumber = scanNumber + 1; while (ms3ScanNumber < rawFile.LastSpectrumNumber) { if (rawFile.GetParentSpectrumNumber(ms3ScanNumber) == scanNumber) { quantitationMsnScan = rawFile[ms3ScanNumber] as MsnDataScan; break; } ms3ScanNumber++; } if (quantitationMsnScan == null) { OnUpdateLog("Cannot find a MS3 spectrum associated with spectrum number " + scanNumber + ". Skipping PSM..."); continue; } } Tolerance Tolerance = quantitationMsnScan.MzAnalyzer == MZAnalyzerType.IonTrap2D ? ItMassTolerance : FtMassTolerance; bool isETD = quantitationMsnScan.DissociationType == DissociationType.ETD; double injectionTime = quantitationMsnScan.InjectionTime; //var massSpectrum = quantitationMsnScan.MassSpectrum; var thermoSpectrum = rawFile.GetLabeledSpectrum(quantitationMsnScan.SpectrumNumber); double noise = 0; if (NoisebandCap) { // Noise is pretty constant over a small region, find the noise of the center of all isobaric tags MassRange range = new MassRange(UsedTags.Keys[0], UsedTags.Keys[UsedTags.Count - 1]); if (thermoSpectrum != null) { var peak = thermoSpectrum.GetClosestPeak(range.Mean, 500); if (peak != null) { noise = peak.Noise; } else { OnUpdateLog("Spectrum (#" + quantitationMsnScan.SpectrumNumber + ") has no m/z peaks. Skipping PSM..."); continue; } } else { OnUpdateLog("Spectrum (#" + quantitationMsnScan.SpectrumNumber + ") is low-resolution data without noise information. Skipping PSM..."); continue; } } //Dictionary<TagInformation, QuantPeak> peaks = new Dictionary<TagInformation, QuantPeak>(); QuantPeak[] peaks = new QuantPeak[largestQuantPeak + 1]; // Read in the peak data foreach (TagInformation tag in UsedTags.Values) { double tagMz = isETD ? tag.MassEtd : tag.MassCAD; var peak = thermoSpectrum.GetClosestPeak(Tolerance.GetRange(tagMz)); QuantPeak qPeak = new QuantPeak(tag, peak, injectionTime, quantitationMsnScan, noise, peak == null && NoisebandCap); peaks[tag.UniqueTagNumber] = qPeak; } PurityCorrect(peaks, isDecoy); PSM psm = new PSM(filenameID, scanNumber, peaks, purity); quantFile.AddPSM(psm); } } // Dispose of all raw files foreach (ThermoRawFile rawFile in RawFiles.Values) { rawFile.Dispose(); } OnUpdateLog("PSMs loaded " + quantFile.Psms.Count); Log("PSMs Loaded:\t" + quantFile.Psms.Count); yield return(quantFile); } }