public void RecalcMassDeviations(IPeakList peakList, int scanNumber, SilacType type, double isotopeCorrelationThreshold) { foreach (MascotPeptide mp in peptides.Values) { mp.RecalcMassDeviation(fixedMods, peakList, scanNumber, type, isotopeCorrelationThreshold); } }
public static List <int[]> FindIsotopeClusters( byte minCharge, byte maxCharge, double correlationThreshold, double sigmas, double isotopeValleyFactor, out byte[] clusterCharges, int peakCount, double[] centerMz, float[] centerMzErrors, float[] minTimes, float[] maxTimes, Peak[] peaks, float[] intensities, IPeakList peakList) { //** cluster assignments ** List <int[]> clusterInd = CalcClusterIndices(minCharge, maxCharge, correlationThreshold, peakCount, centerMz, centerMzErrors, minTimes, maxTimes, peaks, peakList); // ** discard any unclustered peaks ** if (peaks != null) { for (int i = 0; i < peaks.Length; i++) { if (peaks[i] != null) { peaks[i].Dispose(); peaks[i] = null; } } } clusterCharges = new byte[clusterInd.Count]; //** this is some of the worst programming I've ever seen ** T02DeIsotoping deiso = new T02DeIsotoping(); //** while(true) is this cool new thing... ** //** or you know, perhaps while(hasChanged)... ** //** iteratively improve cluster indices until no more improvements ** for (;;) { bool hasChanged; clusterInd = deiso.ImproveIsotopeClusterIndices(clusterInd, minCharge, maxCharge, correlationThreshold, sigmas, isotopeValleyFactor, ref clusterCharges, out hasChanged, centerMz, centerMzErrors, intensities, peakList); if (!hasChanged) { break; } } deiso.Dispose(); return(clusterInd); }
private static double GetFullIsotopePatternMassEstimateNoBootstrapStat(int[] isoClusterIndices, int[] isotopePatternStart, double[] massShifts, double[,] mzCalibrationPar, double[,] intensityCalibrationPar, bool discard, out int npoints, IsotopeCluster[] isotopeClusters, IPeakList peakList, float[] intensities) { npoints = 0; double result = 0; double norm = 0; for (int i = 0; i < isoClusterIndices.Length; i++) { if (isoClusterIndices[i] < 0) { continue; } double intensity; int np; double m = GetFullIsotopePatternMassEstimateNoBootstrapStat(isotopeClusters[isoClusterIndices[i]], isotopePatternStart[i], out intensity, mzCalibrationPar, intensityCalibrationPar, discard, out np, peakList, intensities); result += (m - massShifts[i]) * intensity; norm += intensity; npoints += np; } return(result / norm); }
public void RecalcMassDeviation(ushort[] mods, IPeakList peakList, int scanNumber, SilacType type, double isotopeCorrelationThreshold) { Modification[] fixedMods = ArrayUtil.SubArray(Tables.modificationList, mods); double m = CalcMonoisotopicMass(sequence, modifications, fixedMods); int charge; double mz; double mass1; T06MsmsPreparation.GetPrecursorInfo(out charge, out mz, out mass1, peakList, scanNumber, type, isotopeCorrelationThreshold, double.NaN); if (!double.IsNaN(mass1) && mass1 > 0) { float x = (float)(mass1 - m); if (!float.IsNaN(x) && Math.Abs(x / m) < 1e-5) { deltaMass = x; } } }
public static int[][] FindChargePairs(int nsilac, SilacType silacType, double matchPpm, double silacTimeCorrelationThreshold, SilacCluster[] silacClusters, IsotopeCluster[] isotopeClusters, IPeakList peakList, float[] intensities, double[] centerMz) { double[] m = new double[nsilac]; for (int i = 0; i < m.Length; i++) { SilacCluster si = silacClusters[i]; m[i] = GetUncalibratedSilacMass(si, silacType, isotopeClusters, centerMz, intensities); } int[] o = ArrayUtil.Order(m); m = ArrayUtil.SubArray(m, o); List <int[]> pairs = new List <int[]>(); for (int i = 0; i < m.Length; i++) { SilacCluster si = silacClusters[o[i]]; int chargei = isotopeClusters[si.IsotopeClusterindex1].Charge; double m1 = m[i]; int ind1 = ArrayUtil.CeilIndex(m, m1 - matchPpm * 1e-6 * m1); for (int j = ind1; j < i; j++) { SilacCluster sj = silacClusters[o[j]]; int chargej = isotopeClusters[sj.IsotopeClusterindex1].Charge; if (chargei == chargej) { continue; } int mini; double[] pi = CalcSilacClusterProfile(si, out mini, silacType, isotopeClusters, peakList, intensities); int minj; double[] pj = CalcSilacClusterProfile(sj, out minj, silacType, isotopeClusters, peakList, intensities); if (T03SilacAssembly.Correlate(pi, mini, pj, minj) > silacTimeCorrelationThreshold) { pairs.Add(new int[] { o[i], o[j] }); } } } return(pairs.ToArray()); }
public List <int[]> ImproveIsotopeClusterIndices(IList <int[]> clusterInd, byte minCharge, byte maxCharge, double correlationThreshold, double sigmas, double isotopeValleyFactor, ref byte[] clusterCharges, out bool hasChanged, double[] centerMz, float[] centerMzErrors, float[] intensities, IPeakList peakList) { hasChanged = false; resultClusters.Clear(); resultClusterCharges.Clear(); //** iterate through the clusters ** for (int i = 0; i < clusterInd.Count; i++) { //** initially, all charges are 0 ** byte charge = clusterCharges[i]; //** if the charge has already been assigned ** if (charge > 0) { //** if charge is outside of prescribed range ** //** how does a cluster get assigned an out-of-bounds charge? ** if (charge < minCharge || charge > maxCharge) { hasChanged = true; } else { resultClusters.Add(clusterInd[i]); resultClusterCharges.Add(charge); } } //** else the cluster has not been assigned a charge ** else { byte ch; //** tmpx is a 2D array of ints ** //** could be [original cluster] ** //** or [new cluster],[those removed] ** tmpx = SplitIsotopeCluster(clusterInd[i], minCharge, maxCharge, correlationThreshold, sigmas, isotopeValleyFactor, out ch, centerMz, centerMzErrors, intensities, peakList); //** if return has two arrays [new cluster][those removed] ** if (tmpx.Length > 1) { // it's changed hasChanged = true; } //** iterate through each returned array ** for (int j = 0; j < tmpx.Length; j++) { //** a returned array with 1 peak cannot be processed further ** if (tmpx[j].Length == 1) { //do nothing } //** else it has more than one peak ** else if (tmpx[j].Length > 1) { //** collect it and its charge ** resultClusters.Add(tmpx[j]); if (j == 0) { resultClusterCharges.Add(ch); } else { resultClusterCharges.Add(0); } } tmpx[j] = null; } } clusterInd[i] = null; } //** IGNORE** for (int i = 0; i < clusterInd.Count; i++) { clusterInd[i] = null; } clusterInd.Clear(); clusterCharges = resultClusterCharges.ToArray(); return(new List <int[]>(resultClusters)); }
//** cluster the peaks ** public static List <int[]> CalcClusterIndices(int minCharge, int maxCharge, double correlationThreshold, int peakCount, double[] centerMz, float[] centerMzErrors, float[] minTimes, float[] maxTimes, Peak[] peaks, IPeakList peakList) { NeighbourList neighbourList = new NeighbourList(); //** iterate through all peaks ** for (int j = 0; j < peakCount; j++) { //** current peak's mz and RT range ** double massJ = centerMz[j]; float massErrorJ = centerMzErrors[j]; float timeMinJ = minTimes[j]; float timeMaxJ = maxTimes[j]; //** get index of nearest peak at or above current mass -1.1 ** int start = ArrayUtil.CeilIndex(centerMz, massJ - 1.1); //** get index of nearest peak at or below current mass -1.2 int w = ArrayUtil.FloorIndex(centerMz, massJ - 1.2); //** remove any peaks outside of massj - 1.2 ** //** so there's a removal to the left of this peak outside 1.2 away... ** //** what is this all about?? ** for (int i = 0; i < w; i++) { if (peaks != null && peaks[i] != null) { peaks[i].Dispose(); peaks[i] = null; } } //** iterate from current peak at mass - 1.1 to current peak ** //** iterates through left "adjacent" traces, neihbors with current trace (j) if valid ** for (int i = start; i < j; i++) { //** comparing peak mz and RT range ** double massI = centerMz[i]; double massErrorI = centerMzErrors[i]; double timeMinI = minTimes[i]; double timeMaxI = maxTimes[i]; //** difference in mass and synthesized mass error double massDiff = Math.Abs(massI - massJ); double massError = 5 * Math.Sqrt(massErrorI * massErrorI + massErrorJ * massErrorJ); //** invalidating conditions: ** //** 1) mass difference is greater than minimum ** if (massDiff > MolUtil.C13C12Diff + massError) { continue; } //** 2) no RT overlap if (timeMinI >= timeMaxJ) { continue; } //** 2) no RT overlap if (timeMinJ >= timeMaxI) { continue; } //** 3) mass difference doesn't match any charge states ** if (!FitsMassDifference(massDiff, massError, minCharge, maxCharge)) { continue; } //** 4) The intensity profile correlation (cosine similarity) fails the threshold ** if (CalcCorrelation(peakList.GetPeakKeep(i), peakList.GetPeakKeep(j)) < correlationThreshold) { continue; } //** create an edge between peak I and peak J if valid: ** //** 1) mass difference exceeds minimum //** 2) RT has overlap //** 3) mass difference fits a charge state //** 4) intensity profiles have strong correlation neighbourList.Add(i, j); } } //** convert edge list to clusters! ** List <int[]> clusterList = new List <int[]>(); //** iterate through all peaks ** for (int i = 0; i < peakCount; i++) { //** if the peak has neighbors... ** if (!neighbourList.IsEmptyAt(i)) { HashSet <int> currentCluster = new HashSet <int>(); AddNeighbors(i, currentCluster, neighbourList); int[] c = SortByMass(currentCluster.ToArray(), centerMz); clusterList.Add(c); } } return(clusterList); }
//** splits a cluster into the longest envelope-consistent cluster and the excluded peaks ** private int[][] SplitIsotopeCluster(int[] m, byte minCharge, byte maxCharge, double threshold, double sigmas, double isotopeValleyFactor, out byte resultCharge, double[] centerMz, float[] centerMzErrors, float[] intensities, IPeakList peakList) { //** !!!!!!!!!! m is cluster !!!!!!!!! ** //** wonderful naming conventions ** int n = m.Length; if (n < 2) { throw new Exception("Cannot split cluster of length " + n + "."); } int[] bestMatches = new int[0]; //** if the cluster length is over 100 (why 100?)** if (n > 100) { //TODO: find more elegant way of splitting resultCharge = 0; bestMatches = new int[n / 2]; for (int i = 0; i < bestMatches.Length; i++) { bestMatches[i] = i; } } else { //** cosine correlations between each peak and all left-hand peaks ** tmpCorr = new float[n][]; //** stores all peaks in cluster locally ** tmpPeaks = new Peak[n]; //** get all peaks in cluster ** for (int i = 0; i < n; i++) { tmpPeaks[i] = peakList.GetPeakDiscard(m[i]); } //** calculate the correlation between the each peak and all left-hand peaks ** for (int i = 0; i < n; i++) { tmpCorr[i] = new float[i]; for (int j = 0; j < i; j++) { tmpCorr[i][j] = CalcCorrelation(tmpPeaks[i], tmpPeaks[j]); } } //** mass, mass error, intensity of each peak in cluster ** tmpMasses = ArrayUtil.SubArray(centerMz, m); tmpMassErrors = ArrayUtil.SubArray(centerMzErrors, m); tmpIntens = ArrayUtil.SubArray(intensities, m); byte bestMatchesCharge = 0; //** iterate through charge states, descending from maximum ** for (byte ch = maxCharge; ch >= minCharge; ch--) { //** iterate through the cluster ** for (int i = 0; i < n; i++) { //** threshold is correlation threshold ** //** i is the current peak in the cluster ** //** get set of peaks in cluster consistent with charge state, and envelope shape ** int[] matches = GetMatches(ch, i, threshold, sigmas, tmpCorr, tmpMasses, tmpMassErrors, tmpIntens, isotopeValleyFactor); //** keep the longest match and its charge ** if (matches.Length > bestMatches.Length) { bestMatches = matches; bestMatchesCharge = ch; } } } //** charge of best match ** resultCharge = bestMatchesCharge; //** IGNORE ** for (int i = 0; i < n; i++) { tmpCorr[i] = null; } for (int i = 0; i < tmpPeaks.Length; i++) { //tmpPeaks[i].Dispose(); tmpPeaks[i] = null; } tmpCorr = null; tmpPeaks = null; tmpMasses = null; tmpMassErrors = null; tmpIntens = null; } bestMatches = ArrayUtil.UniqueValues(bestMatches); //** if the best match is shorter than the inputted cluster ** if (bestMatches.Length < n) { //** then return the matched set and those not in the best match ** return(new int[][] { ArrayUtil.SubArray(m, bestMatches), ArrayUtil.SubArray(m, ArrayUtil.Complement(bestMatches, n)) }); } //** else return the original cluster as it is the best match ** return(new int[][] { m }); }
private static void ProcessMsm(string[] lines, string title, string origMass, HashSet <int> scanNumbers, StreamWriter[] silacWriters, TextWriter isoWriter, TextWriter peakWriter, TextWriter polyWriter, IPeakList peakList, double isotopeCorrelationThreshold, SilacType type, ref int silacCount, ref int isoCount, ref int peakCount, ref int polyCount) { int scanNumber = ExtractScanNumber(title); if (scanNumbers.Contains(scanNumber)) { return; } int[] silacInfo = peakList.GetSilacInfoForMsmsScanNumber(scanNumber); if (silacInfo != null) { scanNumbers.Add(scanNumber); int silacId = silacInfo[0]; int silacIndex = silacInfo[1]; int ch = peakList.GetSilacCharge(silacId); double mass = peakList.GetSilacMass(silacId, silacIndex, type); double mz = mass / ch + MolUtil.MassProton; silacWriters[silacIndex].WriteLine("BEGIN IONS"); silacWriters[silacIndex].WriteLine("PEPMASS=" + mz); silacWriters[silacIndex].WriteLine("CHARGE=" + ch + "+"); silacWriters[silacIndex].WriteLine(title + " _sil_"); for (int i = 0; i < lines.Length; i++) { silacWriters[silacIndex].WriteLine(lines[i]); } silacWriters[silacIndex].WriteLine("END IONS"); silacWriters[silacIndex].WriteLine(); silacCount++; return; } int isotopeIndex = peakList.GetIsotopeIndexForMsmsScanNumber(scanNumber); IsotopeCluster ic = null; if (isotopeIndex != -1) { ic = peakList.GetIsotopeCluster(isotopeIndex); } if (isotopeIndex != -1 && ic.IsotopeCorrelation >= isotopeCorrelationThreshold && ic.PolymerIndex == -1) { scanNumbers.Add(scanNumber); int ch = ic.Charge; double mass = peakList.GetMassEstimate(new int[] { isotopeIndex }, new int[] { ic.IsotopePatternStart }, new double[] { 0 }, false); double mz = mass / ch + MolUtil.MassProton; isoWriter.WriteLine("BEGIN IONS"); isoWriter.WriteLine("PEPMASS=" + mz); isoWriter.WriteLine("CHARGE=" + ch + "+"); isoWriter.WriteLine(title + " _iso_"); for (int i = 0; i < lines.Length; i++) { isoWriter.WriteLine(lines[i]); } isoWriter.WriteLine("END IONS"); isoWriter.WriteLine(); isoCount++; return; } if (isotopeIndex != -1 && ic.PolymerIndex != -1) { scanNumbers.Add(scanNumber); int ch = ic.Charge; double mass = peakList.GetMassEstimate(new int[] { isotopeIndex }, new int[] { ic.IsotopePatternStart }, new double[] { 0 }, false); double mz = mass / ch + MolUtil.MassProton; polyWriter.WriteLine("BEGIN IONS"); polyWriter.WriteLine("PEPMASS=" + mz); polyWriter.WriteLine("CHARGE=" + ch + "+"); polyWriter.WriteLine(title + " _iso_"); for (int i = 0; i < lines.Length; i++) { polyWriter.WriteLine(lines[i]); } polyWriter.WriteLine("END IONS"); polyWriter.WriteLine(); polyCount++; return; } scanNumbers.Add(scanNumber); peakWriter.WriteLine("BEGIN IONS"); peakWriter.WriteLine(origMass); peakWriter.WriteLine("CHARGE=2+ and 3+"); peakWriter.WriteLine(title + " _nix_"); for (int i = 0; i < lines.Length; i++) { peakWriter.WriteLine(lines[i]); } peakWriter.WriteLine("END IONS"); peakWriter.WriteLine(); peakCount++; }
public static double[] CalcSilacClusterProfile(SilacCluster cl, out int minIndex, SilacType type, IsotopeCluster[] isotopeClusters, IPeakList peakList, float[] intensities) { int[] m = cl.GetIsotopeClusterIndices(type); int n = m.Length; if (n == 1) { return(T03SilacAssembly.CalcIsotopeClusterProfile(isotopeClusters[m[0]], out minIndex, peakList)); } double[][] profiles = new double[n][]; int[] mins = new int[n]; int[] ends = new int[n]; double[] intens = new double[n]; for (int i = 0; i < n; i++) { if (m[i] == -1) { continue; } IsotopeCluster ic = isotopeClusters[m[i]]; profiles[i] = T03SilacAssembly.CalcIsotopeClusterProfile(ic, out mins[i], peakList); intens[i] = T03SilacAssembly.GetIsotopeClusterIntensity(ic, intensities); ends[i] = mins[i] + profiles[i].Length; } minIndex = ArrayUtil.Min(mins); int maxEnd = ArrayUtil.Max(ends); int len = maxEnd - minIndex; double[] result = new double[len]; double norm = 0; for (int i = 0; i < n; i++) { if (m[i] == -1) { continue; } norm += intens[i]; for (int j = 0; j < profiles[i].Length; j++) { result[j + mins[i] - minIndex] += profiles[i][j] * intens[i]; } } for (int i = 0; i < len; i++) { result[i] /= norm; } return(result); }
public static void RecalibrateImpl(SilacType silacType, int nranges, int[] iranges, int[][] chargePairs, SilacCluster[] silacClusters, ref double[,] mzCalibrationParams, ref double[,] intensityCalibrationParams, double massErrorNormalization, IsotopeCluster[] isotopeCluster, IPeakList peakList, float[] intensities) { double[] x = new double[chargePairs.Length]; double[] y = new double[chargePairs.Length]; double[] sig = new double[chargePairs.Length]; for (int i = 0; i < chargePairs.Length; i++) { x[i] = i; int ind1 = chargePairs[i][0]; int ind2 = chargePairs[i][1]; SilacCluster sc1 = silacClusters[ind1]; SilacCluster sc2 = silacClusters[ind2]; sig[i] = Math.Sqrt(sc1.GetMassError(massErrorNormalization) * sc1.GetMassError(massErrorNormalization) + sc2.GetMassError(massErrorNormalization) * sc2.GetMassError(massErrorNormalization)); } intensityCalibrationParams = new double[nranges, nparamsIntensityFit]; double[,] icp = intensityCalibrationParams; Funcs2 f = delegate(double x1, double[] aa) { int ind = (int)Math.Round(x1); int ind1 = chargePairs[ind][0]; int ind2 = chargePairs[ind][1]; double[,] mzAa = new double[nranges, nparamsMzFit]; int c = 0; for (int j = 0; j < iranges.Length; j++) { for (int i = 0; i < nparamsMzFit; i++) { mzAa[iranges[j], i] = aa[c++]; } } double y1 = GetCalibratedSilacMassStat(silacClusters[ind1], mzAa, icp, silacType, isotopeCluster, peakList, intensities); double y2 = GetCalibratedSilacMassStat(silacClusters[ind2], mzAa, icp, silacType, isotopeCluster, peakList, intensities); double yy = y1 - y2; return(yy); }; double chisq; double[] a = new double[nparamsMzFit * iranges.Length]; int county = 0; for (int j = 0; j < iranges.Length; j++) { for (int i = 0; i < nparamsMzFit; i++) { a[county++] = startValsMzFit[i]; } } NumUtil.FitNonlin(x, y, sig, a, out chisq, f); mzCalibrationParams = new double[nranges, nparamsMzFit]; int s = 0; for (int j = 0; j < iranges.Length; j++) { for (int i = 0; i < nparamsMzFit; i++) { mzCalibrationParams[iranges[j], i] = a[s++]; } } double[,] mcp = mzCalibrationParams; f = delegate(double x1, double[] aa) { int ind = (int)Math.Round(x1); int ind1 = chargePairs[ind][0]; int ind2 = chargePairs[ind][1]; double[,] intensityAa = new double[nranges, nparamsIntensityFit]; int c = 0; for (int j = 0; j < iranges.Length; j++) { for (int i = 0; i < nparamsIntensityFit; i++) { intensityAa[iranges[j], i] = aa[c++]; } } double y1 = GetCalibratedSilacMassStat(silacClusters[ind1], mcp, intensityAa, silacType, isotopeCluster, peakList, intensities); double y2 = GetCalibratedSilacMassStat(silacClusters[ind2], mcp, intensityAa, silacType, isotopeCluster, peakList, intensities); double yy = y1 - y2; return(yy); }; a = new double[nparamsIntensityFit * iranges.Length]; NumUtil.FitNonlin(x, y, sig, a, out chisq, f); s = 0; for (int j = 0; j < iranges.Length; j++) { for (int i = 0; i < nparamsIntensityFit; i++) { intensityCalibrationParams[iranges[j], i] = a[s++]; } } icp = intensityCalibrationParams; f = delegate(double x1, double[] aa) { int ind = (int)Math.Round(x1); int ind1 = chargePairs[ind][0]; int ind2 = chargePairs[ind][1]; double[,] mzAa = new double[nranges, nparamsMzFit]; int c = 0; for (int j = 0; j < iranges.Length; j++) { for (int i = 0; i < nparamsMzFit; i++) { mzAa[iranges[j], i] = aa[c++]; } } double y1 = GetCalibratedSilacMassStat(silacClusters[ind1], mzAa, icp, silacType, isotopeCluster, peakList, intensities); double y2 = GetCalibratedSilacMassStat(silacClusters[ind2], mzAa, icp, silacType, isotopeCluster, peakList, intensities); double yy = y1 - y2; return(yy); }; a = new double[nparamsMzFit * iranges.Length]; county = 0; for (int j = 0; j < iranges.Length; j++) { for (int i = 0; i < nparamsMzFit; i++) { a[county++] = mzCalibrationParams[iranges[j], i]; } } NumUtil.FitNonlin(x, y, sig, a, out chisq, f); s = 0; for (int j = 0; j < iranges.Length; j++) { for (int i = 0; i < nparamsMzFit; i++) { mzCalibrationParams[iranges[j], i] = a[s++]; } } }
public static void ParseMsm(string msmName, string[] silacMsmNames, string isoMsmName, string peakMsmName, string polyMsmName, IPeakList peakList, out int silacCount, out int isoCount, out int peakCount, out int polyCount, SilacType silacType, double isotopeCorrelationThreshold) { HashSet <int> scanNumbers = new HashSet <int>(); StreamReader reader = new StreamReader(msmName); StreamWriter[] silacWriters = new StreamWriter[silacMsmNames.GetLength(0)]; for (int i = 0; i < silacWriters.GetLength(0); i++) { silacWriters[i] = new StreamWriter(silacMsmNames[i]); } StreamWriter isoWriter = new StreamWriter(isoMsmName); StreamWriter peakWriter = new StreamWriter(peakMsmName); StreamWriter polyWriter = new StreamWriter(polyMsmName); string line; List <string> buffer = new List <string>(); string title = null; string mass = null; silacCount = 0; isoCount = 0; peakCount = 0; polyCount = 0; while ((line = reader.ReadLine()) != null) { if (line.IndexOf("BEGIN IONS") != -1) { buffer = new List <string>(); } else if (line.IndexOf("PEPMASS") != -1) { mass = line; } else if (line.IndexOf("CHARGE") != -1) { } else if (line.IndexOf("TITLE") != -1) { title = line; } else if (line.IndexOf("END IONS") != -1) { ProcessMsm(buffer.ToArray(), title, mass, scanNumbers, silacWriters, isoWriter, peakWriter, polyWriter, peakList, isotopeCorrelationThreshold, silacType, ref silacCount, ref isoCount, ref peakCount, ref polyCount); } else { if (line.Length > 0) { buffer.Add(line); } } } reader.Close(); for (int i = 0; i < silacWriters.GetLength(0); i++) { silacWriters[i].Close(); } isoWriter.Close(); peakWriter.Close(); polyWriter.Close(); }
public static double ReQuantifyDoublets(string sequence, int silacState, double[] deltaMass1, IsotopeCluster c, int icInd, IPeakList peakList, IRawFile rawFile, out double[] outIntens, out int[] counts, SpectrumCache spectrumCache, SilacLabel[] labels1, int maxSilacAa, bool subtractBackground, int backgroundSubtractionQuantile) { AminoAcid[] allAas = CalcAllAas(labels1); char[] allAaLetts = AminoAcid.GetSingleLetters(allAas); LabelCombinations labelCombinations1 = new LabelCombinations(labels1, maxSilacAa, allAaLetts); AminoAcid[] a = new AminoAcid[labels1.Length]; counts = new int[a.Length]; double delta = 0; for (int j = 0; j < a.Length; j++) { a[j] = AminoAcid.GetAminoAcidFromLabel(labels1[j]); counts[j] = GetAaCount(a[j].Letter, sequence); delta += deltaMass1[j] * counts[j]; } if (AllZero(counts)) { counts = null; outIntens = null; return(double.NaN); } if (silacState == 1) { delta *= -1; } int[] members = c.Members; int charge = c.Charge; double[] masses = new double[members.Length]; float[][] intens = new float[members.Length][]; int[][] scanIndices = new int[members.Length][]; for (int i = 0; i < members.Length; i++) { masses[i] = charge * (peakList.GetMz(members[i]) - MolUtil.MassProton) + delta; Peak peak = peakList.GetPeakDiscard(members[i]); scanIndices[i] = peak.GetScanIndices(); intens[i] = IntegrateTranslatedPeak(peak, delta / charge, spectrumCache, rawFile, subtractBackground, backgroundSubtractionQuantile); List <int> valids = new List <int>(); for (int j = 0; j < intens[i].Length; j++) { if (intens[i][j] > 0) { valids.Add(j); } } int[] val = valids.ToArray(); intens[i] = ArrayUtil.SubArray(intens[i], val); scanIndices[i] = ArrayUtil.SubArray(scanIndices[i], val); } List <int> valids1 = new List <int>(); for (int i = 0; i < members.Length; i++) { if (intens[i].Length > 0) { valids1.Add(i); } } int[] val1 = valids1.ToArray(); masses = ArrayUtil.SubArray(masses, val1); intens = ArrayUtil.SubArray(intens, val1); scanIndices = ArrayUtil.SubArray(scanIndices, val1); Molecule diff2 = labelCombinations1.CalcDiff1(counts); Molecule diff1 = labelCombinations1.CalcDiff2(counts); double[][] d1; double[][] d2; outIntens = new double[2]; if (silacState == 0) { d1 = diff1.GetIsotopeDistribution(0.2); d2 = diff2.GetIsotopeDistribution(0.2); outIntens[0] = peakList.GetIsotopeClusterIntensity(icInd); outIntens[1] = GetClusterIntensity(intens); } else { d2 = diff1.GetIsotopeDistribution(0.2); d1 = diff2.GetIsotopeDistribution(0.2); outIntens[1] = peakList.GetIsotopeClusterIntensity(icInd); outIntens[0] = GetClusterIntensity(intens); } Dictionary <int, double>[] w = peakList.Vectorize(c, d1, masses, intens, scanIndices, d2); double r = T03SilacAssembly.FitRatio(w[0], w[1]); if (silacState == 1) { r = 1.0 / r; } return(r); }
public static double[] ReQuantifyTriplets(string sequence, int silacState, double[] deltaMass1, double[] deltaMass2, IsotopeCluster c, int icInd, IPeakList peakList, IRawFile rawFile, out double[] outIntens, out int[] counts, SpectrumCache spectrumCache, SilacLabel[] labels1, SilacLabel[] labels2, int maxSilacAa, bool subtractBackground, int backgroundSubtractionQuantile) { AminoAcid[] allAas = CalcAllAas(labels1, labels2); char[] allAaLetts = AminoAcid.GetSingleLetters(allAas); counts = new int[allAaLetts.Length]; for (int j = 0; j < allAaLetts.Length; j++) { counts[j] = GetAaCount(allAaLetts[j], sequence); } if (AllZero(counts)) { counts = null; outIntens = null; return(null); } double deltaLow = 0; for (int j = 0; j < labels1.Length; j++) { int cc = GetAaCount(AminoAcid.GetAminoAcidFromLabel(labels1[j]).Letter, sequence); deltaLow += deltaMass1[j] * cc; } double deltaHigh = 0; for (int j = 0; j < labels2.Length; j++) { int cc = GetAaCount(AminoAcid.GetAminoAcidFromLabel(labels2[j]).Letter, sequence); deltaHigh += deltaMass2[j] * cc; } double delta1; double delta2; CalcDeltasForTriplets(silacState, deltaLow, deltaHigh, out delta1, out delta2); int[] members = c.Members; int charge = c.Charge; double[] masses1 = new double[members.Length]; float[][] intens1 = new float[members.Length][]; int[][] scanIndices1 = new int[members.Length][]; double[] masses2 = new double[members.Length]; float[][] intens2 = new float[members.Length][]; int[][] scanIndices2 = new int[members.Length][]; for (int i = 0; i < members.Length; i++) { double m = charge * (peakList.GetMz(members[i]) - MolUtil.MassProton); masses1[i] = m + delta1; masses2[i] = m + delta2; Peak peak = peakList.GetPeakDiscard(members[i]); scanIndices1[i] = peak.GetScanIndices(); scanIndices2[i] = peak.GetScanIndices(); intens1[i] = IntegrateTranslatedPeak(peak, delta1 / charge, spectrumCache, rawFile, subtractBackground, backgroundSubtractionQuantile); intens2[i] = IntegrateTranslatedPeak(peak, delta2 / charge, spectrumCache, rawFile, subtractBackground, backgroundSubtractionQuantile); List <int> valids = new List <int>(); for (int j = 0; j < intens1[i].Length; j++) { if (intens1[i][j] > 0) { valids.Add(j); } } int[] val = valids.ToArray(); intens1[i] = ArrayUtil.SubArray(intens1[i], val); scanIndices1[i] = ArrayUtil.SubArray(scanIndices1[i], val); valids = new List <int>(); for (int j = 0; j < intens2[i].Length; j++) { if (intens2[i][j] > 0) { valids.Add(j); } } val = valids.ToArray(); intens2[i] = ArrayUtil.SubArray(intens2[i], val); scanIndices2[i] = ArrayUtil.SubArray(scanIndices2[i], val); } List <int> valids1 = new List <int>(); for (int i = 0; i < members.Length; i++) { if (intens1[i].Length > 0) { valids1.Add(i); } } int[] val1 = valids1.ToArray(); masses1 = ArrayUtil.SubArray(masses1, val1); intens1 = ArrayUtil.SubArray(intens1, val1); scanIndices1 = ArrayUtil.SubArray(scanIndices1, val1); List <int> valids2 = new List <int>(); for (int i = 0; i < members.Length; i++) { if (intens2[i].Length > 0) { valids2.Add(i); } } int[] val2 = valids2.ToArray(); masses2 = ArrayUtil.SubArray(masses2, val2); intens2 = ArrayUtil.SubArray(intens2, val2); scanIndices2 = ArrayUtil.SubArray(scanIndices2, val2); LabelCombinations labelCombinations01 = new LabelCombinations(labels1, maxSilacAa, allAaLetts); LabelCombinations labelCombinations02 = new LabelCombinations(labels2, maxSilacAa, allAaLetts); Molecule x1 = labelCombinations01.CalcDiff1(counts); Molecule x2 = labelCombinations01.CalcDiff2(counts); Molecule y1 = labelCombinations02.CalcDiff1(counts); Molecule y2 = labelCombinations02.CalcDiff2(counts); Molecule max1 = Molecule.Max(x1, y1); Molecule x11 = Molecule.GetDifferences(max1, x1)[0]; Molecule y11 = Molecule.GetDifferences(max1, y1)[0]; Molecule diff1 = max1; Molecule diff2 = Molecule.Sum(x2, x11); Molecule diff3 = Molecule.Sum(y2, y11); Molecule max = Molecule.Max(Molecule.Max(diff1, diff2), diff3); diff1 = Molecule.GetDifferences(max, diff1)[0]; diff2 = Molecule.GetDifferences(max, diff2)[0]; diff3 = Molecule.GetDifferences(max, diff3)[0]; double[][] d1 = diff1.GetIsotopeDistribution(0.2); double[][] d2 = diff2.GetIsotopeDistribution(0.2); double[][] d3 = diff3.GetIsotopeDistribution(0.2); double ratio10; double ratio20; double ratio21; outIntens = new double[3]; switch (silacState) { case 0: { Dictionary <int, double>[] w10 = peakList.Vectorize(c, d1, masses1, intens1, scanIndices1, d2); ratio10 = T03SilacAssembly.FitRatio(w10[0], w10[1]); Dictionary <int, double>[] w20 = peakList.Vectorize(c, d1, masses2, intens2, scanIndices2, d3); ratio20 = T03SilacAssembly.FitRatio(w20[0], w20[1]); Dictionary <int, double>[] w21 = peakList.Vectorize(masses1, intens1, scanIndices1, d2, masses2, intens2, scanIndices2, d3, c.Charge); ratio21 = T03SilacAssembly.FitRatio(w21[0], w21[1]); outIntens[0] = peakList.GetIsotopeClusterIntensity(icInd); outIntens[1] = GetClusterIntensity(intens1); outIntens[2] = GetClusterIntensity(intens2); break; } case 1: { Dictionary <int, double>[] w01 = peakList.Vectorize(c, d2, masses1, intens1, scanIndices1, d1); ratio10 = 1.0 / T03SilacAssembly.FitRatio(w01[0], w01[1]); Dictionary <int, double>[] w20 = peakList.Vectorize(masses1, intens1, scanIndices1, d1, masses2, intens2, scanIndices2, d3, c.Charge); ratio20 = T03SilacAssembly.FitRatio(w20[0], w20[1]); Dictionary <int, double>[] w21 = peakList.Vectorize(c, d2, masses2, intens2, scanIndices2, d3); ratio21 = T03SilacAssembly.FitRatio(w21[0], w21[1]); outIntens[1] = peakList.GetIsotopeClusterIntensity(icInd); outIntens[0] = GetClusterIntensity(intens1); outIntens[2] = GetClusterIntensity(intens2); break; } case 2: { Dictionary <int, double>[] w10 = peakList.Vectorize(masses1, intens1, scanIndices1, d1, masses2, intens2, scanIndices2, d2, c.Charge); ratio10 = T03SilacAssembly.FitRatio(w10[0], w10[1]); Dictionary <int, double>[] w02 = peakList.Vectorize(c, d3, masses1, intens1, scanIndices1, d1); ratio20 = 1.0 / T03SilacAssembly.FitRatio(w02[0], w02[1]); Dictionary <int, double>[] w12 = peakList.Vectorize(c, d3, masses2, intens2, scanIndices2, d2); ratio21 = 1.0 / T03SilacAssembly.FitRatio(w12[0], w12[1]); outIntens[2] = peakList.GetIsotopeClusterIntensity(icInd); outIntens[0] = GetClusterIntensity(intens1); outIntens[1] = GetClusterIntensity(intens2); break; } default: throw new Exception("Impossible."); } return(new double[] { ratio10, ratio20, ratio21 }); }
private static double GetCalibratedSilacMassStat(SilacCluster sc, double[,] mzCalibrationPar, double[,] intensityCalibrationPar, SilacType type, IsotopeCluster[] isotopeCluster, IPeakList peakList, float[] intensities) { int dummy; return (GetFullIsotopePatternMassEstimateNoBootstrapStat(sc.GetIsotopeClusterIndices(type), sc.GetIsotopePatternStarts(type), sc.GetMassDiffs(type), mzCalibrationPar, intensityCalibrationPar, true, out dummy, isotopeCluster, peakList, intensities)); }
public static int[] ExtractMsm(string filename, IPeakList peakList, IRawFile rawFile, string msmName, string msmNameBinary, string msmNameIndex, int topx, double isotopeCorrelationThreshold, SilacType type, out long[] pointers) { StreamWriter sw = new StreamWriter(msmName); BinaryWriter writer = FileUtil.GetBinaryWriter(msmNameBinary); List <long> filePointers = new List <long>(); List <int> fileScanNumbers = new List <int>(); long filePos = 0; for (int i = 0; i < rawFile.MS2Count; i++) { Spectrum s = rawFile.GetMS2Spectrum(i); int scanNumber = rawFile.GetScanNumberFromMs2Index(i); double simpleMz = rawFile.GetMS2MonoisotopicMz(i); if (double.IsNaN(simpleMz) || simpleMz < 1) { simpleMz = peakList.GetMs2Mz(i); if (double.IsNaN(simpleMz) || simpleMz < 1) { simpleMz = 1; } } int charge; double mz; double mass; GetPrecursorInfo(out charge, out mz, out mass, peakList, scanNumber, type, isotopeCorrelationThreshold, simpleMz); if (rawFile.GetMS2SignalType(i) != SignalType.Centroid) { double[] specMasses; float[] specIntensities; T01PeakDetection.DetectPeaks(s, false, 3, CentroidPosition.gaussian, out specMasses, out specIntensities); s = new Spectrum(specMasses, specIntensities); } if (topx > 0) { s = s.TopX(topx, 100); } int sn = rawFile.GetScanNumberFromMs2Index(i); if (s.Count == 0) { continue; } StringBuilder sb = new StringBuilder(); writer.Write(s.Count); for (int j = 0; j < s.Count; j++) { sb.AppendFormat("{0:F3}\t{1:F2}\n", s.GetMass(j), s.GetIntensity(j)); writer.Write(s.GetMass(j)); writer.Write(1.0 * s.GetIntensity(j)); } filePointers.Add(filePos); fileScanNumbers.Add(sn); filePos += 16 * s.Count + 4; sw.WriteLine("BEGIN IONS"); sw.WriteLine("PEPMASS={0:F6}", mz); if (charge > 0) { sw.WriteLine("CHARGE={0}+", charge); } else { sw.WriteLine("CHARGE=2+ and 3+"); } sw.WriteLine("TITLE=RawFile: {0} FinneganScanNumber: {1}", filename, sn); sw.Write(sb.ToString()); sw.WriteLine("END IONS"); sw.WriteLine(); } writer.Close(); sw.Close(); pointers = filePointers.ToArray(); return(fileScanNumbers.ToArray()); }
public static void GetPrecursorInfo(out int charge, out double mz, out double mass, IPeakList peakList, int scanNumber, SilacType type, double isotopeCorrelationThreshold, double simpleMz) { charge = 0; mass = 0; int[] silacInfo = peakList.GetSilacInfoForMsmsScanNumber(scanNumber); if (silacInfo != null) { int silacId = silacInfo[0]; int silacIndex = silacInfo[1]; charge = peakList.GetSilacCharge(silacId); mass = peakList.GetSilacMass(silacId, silacIndex, type); mz = mass / charge + MolUtil.MassProton; } else { int isotopeIndex = peakList.GetIsotopeIndexForMsmsScanNumber(scanNumber); IsotopeCluster ic = null; if (isotopeIndex != -1) { ic = peakList.GetIsotopeCluster(isotopeIndex); } if (isotopeIndex != -1 && ic.IsotopeCorrelation >= isotopeCorrelationThreshold) { charge = ic.Charge; mass = peakList.GetMassEstimate(new int[] { isotopeIndex }, new int[] { ic.IsotopePatternStart }, new double[] { 0 }, false); mz = mass / charge + MolUtil.MassProton; } else { int peakIndex = peakList.GetPeakIndexForMsmsScanNumber(scanNumber); mz = peakIndex >= 0 ? peakList.GetMz(peakIndex) : simpleMz; } } }
private static double GetFullIsotopePatternMassEstimateNoBootstrapStat(IsotopeCluster ic, int isotopePatternStart, out double intensity, double[,] mzCalibrationPar, double[,] intensityCalibrationPar, bool discard, out int npoints, IPeakList peakList, float[] intensities) { double result = 0; intensity = 0; npoints = 0; int charge = ic.Charge; for (int i = 0; i < ic.Count; i++) { if (i + isotopePatternStart >= 0) { int ind = ic.Members[i]; int np; double mz = discard ? peakList.GetMzDiscard(ind, mzCalibrationPar, intensityCalibrationPar, out np) : peakList.GetMz(ind, mzCalibrationPar, intensityCalibrationPar, out np); double m = (mz - MolUtil.MassProton) * charge; result += intensities[ind] * (m - MolUtil.GetAverageDifferenceToMonoisotope(m, i + isotopePatternStart)); intensity += intensities[ind]; npoints += np; } } if (intensity == 0) { for (int i = 0; i < ic.Count; i++) { int ind = ic.Members[i]; int np; double mz = discard ? peakList.GetMzDiscard(ind, mzCalibrationPar, intensityCalibrationPar, out np) : peakList.GetMz(ind, mzCalibrationPar, intensityCalibrationPar, out np); double m = (mz - MolUtil.MassProton) * charge; result += intensities[ind] * (m - (i + isotopePatternStart) * MolUtil.GetAverageDifferenceToMonoisotope(m, 1)); intensity += intensities[ind]; npoints += np; } } return(result / intensity); }
public void ProcessPeptides(int fileIndex, Dictionary <string, int> proteinIdToGroupIndex, IPeakList peakList, MsmsData msmsData, IIdentifiedPeptide[] identifiedPeptides, string[] peptideSequences, ReQuantitationResult reQuantitationResult, bool reQuantify, HashSet <string> labelModificationSet, IProteinSet proteinSet, SilacType silacType, SilacLabel[] labels1, SilacLabel[] labels2, double ms2Tol, string ms2TolUnit, int topx, string[] fixedMods) { if (peptides == null) { Read(); } double[] monoIsoMz = peakList.MS2MonoisotopicMz; for (int i = 0; i < peptides.Length; i++) { MascotPeptide[] p = peptides[i]; int scanNumber = scanNumbers[i]; int ms2ind = peakList.GetMs2IndexFromScanNumber(scanNumber); double mz = peakList.GetMs2Mz(ms2ind); double monotopicMz = monoIsoMz[ms2ind]; double time = peakList.GetMs2Rt(ms2ind); int silacId = -1; int isotopeId = -1; int silacIndex = -1; SilacCluster silacCluster = null; IsotopeCluster isotopeCluster = null; if (type == MascotQueryType.Silac) { int[] silacInfo = peakList.GetSilacInfoForMsmsScanNumber(scanNumber); silacId = silacInfo[0]; silacIndex = silacInfo[1]; silacCluster = peakList.GetSilacCluster(silacId); } else if (type == MascotQueryType.Isotope) { isotopeId = peakList.GetIsotopeIndexForMsmsScanNumber(scanNumber); isotopeCluster = peakList.GetIsotopeCluster(isotopeId); } int index = Array.BinarySearch(peptideSequences, p[0].Sequence); if (index < 0) { continue; } HashSet <int> tmpGroupInds = new HashSet <int>(); foreach (int pi in p[0].ProteinIndex) { string protId = proteinSet.GetName(pi); if (!proteinIdToGroupIndex.ContainsKey(protId)) { continue; } int groupInd = proteinIdToGroupIndex[protId]; if (!tmpGroupInds.Contains(groupInd)) { tmpGroupInds.Add(groupInd); } } double[] specMasses; float[] specIntensities; bool uniqueProtein = (p[0].ProteinIndex.Length == 1); bool uniqueGroup = (tmpGroupInds.Count == 1); msmsData.GetSpectrumFromScanNumber(scanNumber, out specMasses, out specIntensities); identifiedPeptides[index].AddMascotPeptideHit(p, scanNumber, fileIndex, type, silacId, silacIndex, isotopeId, silacCluster, isotopeCluster, time, peakList, mz, monotopicMz, fixedModifications[i], specMasses, specIntensities, reQuantitationResult, reQuantify, labelModificationSet, silacType, labels1, labels2, ms2Tol, ms2TolUnit, topx, fixedMods); identifiedPeptides[index].UniqueProtein = uniqueProtein; identifiedPeptides[index].UniqueGroup = uniqueGroup; } }