Beispiel #1
0
        /// <summary>
        /// This function takes top N intense peaks to meet the target TIC.
        /// </summary>
        /// <param name="peaks">Peaks in the spectrum</param>
        /// <param name="percentTIC">Percentage of total ion current to be retained</param>
        /// <returns></returns>
        public static List <Peak> filterByPercentTIC(List <Peak> peaks, double percentTIC)
        {
            // Compute the total ion current in the spectrum
            double totalTIC = 0.0;

            foreach (var peak in peaks)
            {
                totalTIC += peak.rankOrIntensity;
            }

            // Sort the peaks by descending order of intensity and take as many peaks as
            // possible to meet the target TIC.
            IntensityComparer intensitySorter = new IntensityComparer(SortOrder.DESCENDING);

            peaks.Sort(intensitySorter);
            List <Peak> finalList = new List <Peak>();
            double      TICSoFar  = 0.0;

            foreach (var peak in peaks)
            {
                TICSoFar += peak.rankOrIntensity;
                if ((TICSoFar / totalTIC) >= percentTIC)
                {
                    break;
                }
                finalList.Add(peak);
            }
            MassComparer mzComparer = new MassComparer(SortOrder.ASCENDING);

            finalList.Sort(mzComparer);
            return(finalList);
        }
Beispiel #2
0
        // End comparators for Peak

        /// <summary>
        /// This function takes top N intense peaks from each spectrum
        /// </summary>
        /// <param name="peaks">Spectrum peaks</param>
        /// <param name="numPeaks">Number of peaks to be retained</param>
        /// <returns></returns>
        public static List <Peak> filterByPeakCount(List <Peak> peaks, int numPeaks)
        {
            // Sort the peaks by decreasing order of intensity and take numPeaks
            IntensityComparer intensitySorter = new IntensityComparer(SortOrder.DESCENDING);

            peaks.Sort(intensitySorter);
            List <Peak> finalList = new List <Peak>();

            foreach (var peak in peaks)
            {
                if (numPeaks == 0)
                {
                    break;
                }
                --numPeaks;
                finalList.Add(peak);
            }
            MassComparer mzComparer = new MassComparer(SortOrder.ASCENDING);

            finalList.Sort(mzComparer);
            return(finalList);
        }
Beispiel #3
0
        /// <summary>
        /// This function takes a spectrum and processes it for computing Sequest XCorr.
        /// </summary>
        /// <param name="originalPeaks">Peaks in the spectrum</param>
        /// <param name="precursorMH">Mass of the precursor</param>
        /// <returns>An intensity array of the processed spectrum</returns>
        public static double[] processSpectrumForXCorr(ref List <Peak> originalPeaks, double precursorMH)
        {
            // Sort the peaks by mass
            MassComparer massComp = new MassComparer(SortOrder.ASCENDING);

            originalPeaks.Sort(massComp);
            // Get the number of bins and bin width for the processed peak array
            double binWidth = 1.0005079;
            int    maxBins;
            double massCutOff = precursorMH + 50;

            if (massCutOff > 512)
            {
                maxBins = (int)Math.Ceiling(massCutOff / 1024) * 1024;
            }
            else
            {
                maxBins = 512;
            }

            double maxPeakMass = originalPeaks[originalPeaks.Count - 1].mz;

            if (maxPeakMass > massCutOff)
            {
                int index = originalPeaks.Count - 2;
                for (; index >= 0 && maxPeakMass > massCutOff; --index)
                {
                    maxPeakMass = originalPeaks[index].mz;
                }
            }
            int numberOfRegions = 10;

            // Square root the intensity of the spectrum
            foreach (var peak in originalPeaks)
            {
                peak.rankOrIntensity = Math.Sqrt(peak.rankOrIntensity);
            }

            // Section the original peak array in 10 bins and find the
            // base peak in each bin.
            double[] maxPeakIntensityInRegions = new double[numberOfRegions];
            // Zero out the base peak array
            for (int index = 0; index < maxPeakIntensityInRegions.Length; ++index)
            {
                maxPeakIntensityInRegions[index] = 0;
            }
            // Determine the base peak in each region
            int regionSelector = (int)maxPeakMass / numberOfRegions;

            foreach (var peak in originalPeaks)
            {
                int    peakBin       = (int)peak.mz / regionSelector;
                double peakIntensity = peak.rankOrIntensity;
                if (peakBin < 10 && maxPeakIntensityInRegions[peakBin] < peakIntensity)
                {
                    maxPeakIntensityInRegions[peakBin] = peakIntensity;
                }
            }

            // Normalize peaks in each region from 0 to 50.
            // Use base peak in each region for normalization.
            double[] processedPeaks = new double[maxBins];
            foreach (var peak in originalPeaks)
            {
                int mzLocation = (int)(peak.mz / binWidth + 0.5);
                int mzBin      = mzLocation / regionSelector;
                if (mzBin + 1 > numberOfRegions)
                {
                    continue;
                }
                double maxBinIntensity = maxPeakIntensityInRegions[mzBin];
                processedPeaks[mzLocation] = (peak.rankOrIntensity / maxBinIntensity) * 50;
            }

            // Compute the cumulative spectrum
            for (int index = 0; index < processedPeaks.Length; ++index)
            {
                for (int subIndex = index - 75; subIndex <= index + 75; ++subIndex)
                {
                    if (subIndex <= 0 || subIndex >= processedPeaks.Length)
                    {
                        continue;
                    }
                    processedPeaks[index] -= (processedPeaks[subIndex] / 151);
                }
            }

            return(processedPeaks);
        }
Beispiel #4
0
        private void ReducePsms(IList<InputFile> csvFiles, UniquePeptideType uniquePeptideType, bool isBatched = false)
        {
            string msg = "Converting PSMs into unique peptides based ";
            IEqualityComparer<Peptide> comparer;
            switch (uniquePeptideType)
            {
                default:
                    msg += "on sequence only";
                    comparer = new SequenceComparer();
                    break;
                case UniquePeptideType.Mass:
                    msg += "on mass";
                    comparer = new MassComparer();
                    break;
                case UniquePeptideType.SequenceAndModifications:
                    msg += "on sequence and positional modifications";
                    comparer = new SequenceModComparer();
                    break;
                case UniquePeptideType.SequenceAndModLocations:
                    msg += "on sequence and modification locations";
                    comparer = new SequenceAndModPositionComparer();
                    break;
                case UniquePeptideType.SequenceAndMass:
                    msg += "on sequence and mass";
                    comparer = new SequenceMassComparer();
                    break;
                case UniquePeptideType.Nothing:
                    msg += "on nothing (no reduction)";
                    comparer = new IdentityComparer<Peptide>();
                    break;
            }
            Log(msg);

            foreach (InputFile csvFile in csvFiles)
            {
                csvFile.ReducePsms(comparer);
                Log(string.Format("{0:N0} unique peptides remain from {1:N0} PSMs from {2}", csvFile.Peptides.Count, csvFile.PeptideSpectralMatches.Count, csvFile.Name));
            }

            if (!isBatched)
                return;

            Dictionary<Peptide, Peptide> peptideDictionary = new Dictionary<Peptide, Peptide>(comparer);
            foreach (Peptide peptide in csvFiles.SelectMany(csvFile => csvFile.Peptides))
            {
                Peptide realPeptide;
                if (peptideDictionary.TryGetValue(peptide, out realPeptide))
                {
                    foreach (PSM psm in peptide.PSMs)
                    {
                        realPeptide.AddPsm(psm);
                    }
                }
                else
                {
                    Peptide newPeptide = new Peptide(peptide);
                    peptideDictionary.Add(newPeptide, newPeptide);
                }
            }
            _allPeptides = peptideDictionary.Values.ToList();
            Log(string.Format("{0:N0} unique peptides from all files [Batched]", _allPeptides.Count));
        }