Exemplo n.º 1
0
        private static IEnumerable <LCIMSMSFeature> SplitByScanLCGap(IEnumerable <LCIMSMSFeature> lcimsmsFeatureEnumerable)
        {
            var lcimsmsFeatureList = new List <LCIMSMSFeature>();

            int gapSizeMax = Settings.LCGapSizeMax;

            foreach (var lcimsmsFeature in lcimsmsFeatureEnumerable)
            {
                var sortByScanLCQuery = from imsmsFeature in lcimsmsFeature.IMSMSFeatureList
                                        orderby imsmsFeature.ScanLC
                                        select imsmsFeature;

                LCIMSMSFeature newLCIMSMSFeature = null;
                var            scanLCReference   = -99;

                foreach (var imsmsFeature in sortByScanLCQuery)
                {
                    if (imsmsFeature.ScanLC - scanLCReference - 1 <= gapSizeMax && newLCIMSMSFeature != null)
                    {
                        newLCIMSMSFeature.AddIMSMSFeature(imsmsFeature);
                    }
                    else
                    {
                        newLCIMSMSFeature = new LCIMSMSFeature(imsmsFeature.Charge);
                        newLCIMSMSFeature.AddIMSMSFeature(imsmsFeature);
                        lcimsmsFeatureList.Add(newLCIMSMSFeature);
                    }

                    scanLCReference = imsmsFeature.ScanLC;
                }
            }

            return(lcimsmsFeatureList);
        }
Exemplo n.º 2
0
        public static void MergeLCIMSMSFeatures(LCIMSMSFeature dominantFeature, LCIMSMSFeature recessiveFeature)
        {
            var referenceMass = dominantFeature.IMSMSFeatureList[0].MSFeatureList[0].MassMonoisotopic;
            var massToChange  = recessiveFeature.IMSMSFeatureList[0].MSFeatureList[0].MassMonoisotopic;

            var massChange = (int)Math.Round(referenceMass - massToChange);

            var scanLCToIMSMSFeatureMap = dominantFeature.IMSMSFeatureList.ToDictionary(dominantIMSMSFeature => dominantIMSMSFeature.ScanLC);

            foreach (var recessiveIMSMSFeature in recessiveFeature.IMSMSFeatureList)
            {
                // First correct the Mass of the recessive IMSMSFeature
                foreach (var msFeature in recessiveIMSMSFeature.MSFeatureList)
                {
                    msFeature.MassMonoisotopic += massChange;

                    // TODO: Keep this??
                    //msFeature.Mz = (msFeature.MassMonoisotopic / msFeature.Charge) + (float)1.00727849;
                }

                if (scanLCToIMSMSFeatureMap.TryGetValue(recessiveIMSMSFeature.ScanLC, out var dominantIMSMSFeature))
                {
                    MergeIMSMSFeatures(dominantIMSMSFeature, recessiveIMSMSFeature);
                }
                else
                {
                    dominantFeature.AddIMSMSFeature(recessiveIMSMSFeature);
                }
            }

            recessiveFeature.IMSMSFeatureList.Clear();
        }
Exemplo n.º 3
0
        public static IEnumerable <LCIMSMSFeature> ClusterByMassAndScanLC(IEnumerable <IMSMSFeature> imsmsFeatureEnumerable)
        {
            var lcimsmsFeatureList = new List <LCIMSMSFeature>();

            var massToleranceBase = Settings.MassMonoisotopicConstraint;

            var sortByMassQuery = from imsmsFeature in imsmsFeatureEnumerable
                                  orderby imsmsFeature.CalculateAverageMonoisotopicMass()
                                  select imsmsFeature;

            LCIMSMSFeature lcimsmsFeature = null;
            double         massReference  = -99;

            foreach (var imsmsFeature in sortByMassQuery)
            {
                var mass = imsmsFeature.CalculateAverageMonoisotopicMass();

                var massTolerance     = massToleranceBase * massReference / 1000000;
                var massToleranceHigh = massReference + massTolerance;
                var massToleranceLow  = massReference - massTolerance;

                if (mass >= massToleranceLow && mass <= massToleranceHigh && lcimsmsFeature != null)
                {
                    lcimsmsFeature.AddIMSMSFeature(imsmsFeature);
                }
                else
                {
                    lcimsmsFeature = new LCIMSMSFeature(imsmsFeature.Charge);
                    lcimsmsFeature.AddIMSMSFeature(imsmsFeature);
                    lcimsmsFeatureList.Add(lcimsmsFeature);
                }

                massReference = mass;
            }

            return(lcimsmsFeatureList);

            //IEnumerable<LCIMSMSFeature> splitLCIMSMSFeatureEnumerable = SplitByScanLCGap(lcimsmsFeatureList);

            //return splitLCIMSMSFeatureEnumerable;
        }
Exemplo n.º 4
0
        public static IEnumerable <LCIMSMSFeature> SplitLCIMSMSFeaturesByScanLC(IEnumerable <LCIMSMSFeature> lcimsmsFeatureEnumerable)
        {
            var lcimsmsFeatureList = new List <LCIMSMSFeature>();

            var lcimcmsFeatureIndex = 0;

            foreach (var lcimsmsFeature in lcimsmsFeatureEnumerable)
            {
                var sortByScanLC = from imsmsFeature in lcimsmsFeature.IMSMSFeatureList
                                   orderby imsmsFeature.ScanLC
                                   select imsmsFeature;

                LCIMSMSFeature newLCIMSMSFeature = null;
                var            referenceScanLC   = -99999;

                foreach (var imsmsFeature in sortByScanLC)
                {
                    var scanLC = imsmsFeature.ScanLC;

                    if (scanLC - referenceScanLC > Settings.LCGapSizeMax)
                    {
                        newLCIMSMSFeature = new LCIMSMSFeature(imsmsFeature.Charge);
                        newLCIMSMSFeature.AddIMSMSFeature(imsmsFeature);
                        newLCIMSMSFeature.OriginalIndex = lcimcmsFeatureIndex;
                        lcimcmsFeatureIndex++;
                        lcimsmsFeatureList.Add(newLCIMSMSFeature);
                    }
                    else
                    {
                        newLCIMSMSFeature?.AddIMSMSFeature(imsmsFeature);
                    }

                    referenceScanLC = scanLC;
                }
            }

            return(lcimsmsFeatureList);
        }
Exemplo n.º 5
0
        public static IEnumerable <LCIMSMSFeature> FindDriftTimePeaks(Peak driftProfilePeak, LCIMSMSFeature lcimsmsFeature, double averageTOFLength, double framePressure)
        {
            var imsmsFeatureList = lcimsmsFeature.IMSMSFeatureList;

            var sortByScanLCQuery = from imsmsFeature in imsmsFeatureList
                                    orderby imsmsFeature.ScanLC
                                    select imsmsFeature;

            var globalIMSScanMinimum = double.MaxValue;
            var globalIMSScanMaximum = double.MinValue;

            // Grab all of the intensity values for each IMS-MS Feature and find the global minimum and maximum Drift Times
            foreach (var imsmsFeature in sortByScanLCQuery)
            {
                imsmsFeature.GetMinAndMaxIMSScan(out var localIMSScanMinimum, out var localIMSScanMaximum);

                if (localIMSScanMinimum < globalIMSScanMinimum)
                {
                    globalIMSScanMinimum = localIMSScanMinimum;
                }
                if (localIMSScanMaximum > globalIMSScanMaximum)
                {
                    globalIMSScanMaximum = localIMSScanMaximum;
                }
            }

            var smoothedDriftProfilePeak = PeakUtil.KDESmooth(driftProfilePeak, Settings.SmoothingStDev); // TODO: Find a good value. 0.15? Less smooth = more conformations!

            var smoothedDriftProfileInterpolation = PeakUtil.GetLinearInterpolationMethod(smoothedDriftProfilePeak);

            var xyPairList        = new List <XYPair>();
            var peakList          = new List <Peak>();
            var previousIntensity = double.MinValue;
            var movingUp          = true;

            // lcimsmsFeature.GetMinAndMaxScanLC(out var minScanLC, out var maxScanLC);

            var minimumIntensityToConsider = smoothedDriftProfilePeak.GetMaximumYValue() * 0.05;

            //DisplayPeakXYData(smoothedDriftProfilePeak);

            //Console.WriteLine("Global IMS Scan Min = " + globalIMSScanMinimum + "\tGlobal IMS Scan Max = " + globalIMSScanMaximum);

            for (var i = globalIMSScanMinimum; i <= globalIMSScanMaximum; i += 1)
            {
                var imsScan   = i;
                var intensity = smoothedDriftProfileInterpolation.Interpolate(imsScan);

                if (intensity > minimumIntensityToConsider)
                {
                    //Console.WriteLine(imsScan + "\t" + intensity + "\t" + movingUp);

                    if (intensity > previousIntensity)
                    {
                        // End of Peak
                        if (!movingUp && xyPairList.Count > 0)
                        {
                            PadXYPairsWithZeros(ref xyPairList, 2);
                            //xyPairList = PadXYPairsWithZeros(xyPairList, imsScanMinimum, i - DRIFT_TIME_SLICE_WIDTH, 1);
                            var peak = new Peak(xyPairList);

                            if (peak.XYPairList.Count >= 7)
                            {
                                peakList.Add(peak);
                            }

                            // Start over with a new Peak
                            xyPairList.Clear();
                            movingUp = true;
                        }
                    }
                    else
                    {
                        movingUp = false;
                    }

                    var xyPair = new XYPair(imsScan, intensity);
                    xyPairList.Add(xyPair);

                    previousIntensity = intensity;
                }
                else
                {
                    movingUp          = false;
                    previousIntensity = 0;
                }
            }

            // When you get to the end, end the last Peak, but only if it has a non-zero value
            if (xyPairList.Any(xyPair => xyPair.YValue > minimumIntensityToConsider))
            {
                PadXYPairsWithZeros(ref xyPairList, 2);
                //xyPairList = PadXYPairsWithZeros(xyPairList, imsScanMinimum, globalIMSScanMaximum, 1);
                var lastPeak = new Peak(xyPairList);

                if (lastPeak.XYPairList.Count >= 7)
                {
                    peakList.Add(lastPeak);
                }
            }

            // var resolvingPower = GetResolvingPower(lcimsmsFeature.Charge);

            var newLCIMSMSFeatureList = new List <LCIMSMSFeature>();

            foreach (var peak in peakList)
            {
                var repIMSScan = peak.GetQuadraticFit();

                // TODO: Fix this
                //double theoreticalFWHM = driftTime / resolvingPower;
                double theoreticalFWHM = 3;

                peak.GetMinAndMaxXValues(out var minimumXValue, out var maximumXValue);

                const int numPoints = 100;

                var normalDistributionXYPairList = PeakUtil.CreateTheoreticalGaussianPeak(repIMSScan, theoreticalFWHM, numPoints);
                PadXYPairsWithZeros(ref normalDistributionXYPairList, 5);
                var normalDistributionPeak = new Peak(normalDistributionXYPairList);

                var peakInterpolation = PeakUtil.GetLinearInterpolationMethod(peak);

                var fitScore = PeakUtil.CalculatePeakFit(peak, normalDistributionPeak, 0);

                // Create a new LC-IMS-MS Feature
                var newLCIMSMSFeature = new LCIMSMSFeature(lcimsmsFeature.Charge)
                {
                    OriginalIndex   = lcimsmsFeature.OriginalIndex,
                    IMSScore        = (float)fitScore,
                    AbundanceMaxRaw = Math.Round(peak.GetMaximumYValue()),
                    // Using Math.Floor instaed of Math.Round because I used to cast this to an int which is esentially Math.Floor.
                    // The difference is negligible, but OHSU would complain if results were the slightest bit different if the app was re-run on the same dataset.
                    AbundanceSumRaw = Math.Floor(peakInterpolation.Integrate(peak.GetMaximumXValue())),
                    DriftTime       = ConvertIMSScanToDriftTime(repIMSScan, averageTOFLength, framePressure)
                };

                // Create new IMS-MS Features by grabbing MS Features in each LC Scan that are in the defined window of the detected drift time
                foreach (var imsmsFeature in lcimsmsFeature.IMSMSFeatureList)
                {
                    var msFeatures = imsmsFeature.FindMSFeaturesInScanIMSRange(minimumXValue, maximumXValue).ToList();

                    if (!msFeatures.Any())
                    {
                        continue;
                    }

                    var newIMSMSFeature = new IMSMSFeature(imsmsFeature.ScanLC, imsmsFeature.Charge);
                    newIMSMSFeature.AddMSFeatureList(msFeatures);
                    newLCIMSMSFeature.AddIMSMSFeature(newIMSMSFeature);
                }

                if (newLCIMSMSFeature.IMSMSFeatureList.Count > 0)
                {
                    newLCIMSMSFeatureList.Add(newLCIMSMSFeature);

                    /*
                     * // TODO: Find LC Peaks
                     * var sortByScanLC = from imsmsFeature in newLCIMSMSFeature.IMSMSFeatureList
                     *                 orderby imsmsFeature.ScanLC ascending
                     *                 select imsmsFeature;
                     *
                     * Console.WriteLine("*************************************************");
                     * Console.WriteLine("Index = " + index + "\tMass = " + newLCIMSMSFeature.CalculateAverageMass() + "\tDrift = " + driftTime + "\tLC Range = " + sortByScanLC.First().ScanLC + "\t" + sortByScanLC.Last().ScanLC);
                     *
                     * List<XYPair> lcXYPairList = new List<XYPair>();
                     * int scanLC = sortByScanLC.First().ScanLC - 1;
                     *
                     * foreach (IMSMSFeature imsmsFeature in sortByScanLC)
                     * {
                     *  int currentScanLC = imsmsFeature.ScanLC;
                     *
                     *  for (int i = scanLC + 1; i < currentScanLC; i++)
                     *  {
                     *      XYPair zeroValue = new XYPair(i, 0);
                     *      lcXYPairList.Add(zeroValue);
                     *      Console.Write("0\t");
                     *  }
                     *
                     *  XYPair xyPair = new XYPair(currentScanLC, imsmsFeature.GetIntensity());
                     *  lcXYPairList.Add(xyPair);
                     *
                     *  scanLC = currentScanLC;
                     *
                     *  Console.Write(imsmsFeature.GetIntensity() + "\t");
                     * }
                     * Console.WriteLine("");
                     * Console.WriteLine("*************************************************");
                     */
                    // TODO: Calculate LC Score
                }
                else
                {
                    //Console.WriteLine("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ FOUND EMPTY $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
                    // TODO: Figure out why this actually happens. I believe that this SHOULD NOT happen. Below is a hack to return a conformation even if this happens
                    // It actually looks like most of these occurences are due to large gaps in the drift time, which cause a small peak to be found in the gap which has no members.

                    //Console.WriteLine("**********************************************************************");
                    //Console.WriteLine("Detected Drift Time = " + driftTime + "\tLow = " + lowDriftTime + "\tHigh = " + highDriftTime);
                    //lcimsmsFeature.PrintLCAndDriftTimeMap();
                    //Console.WriteLine("**********************************************************************");

                    //Console.WriteLine("===============================================================");
                    //Console.WriteLine("DT = " + driftTime + "\tLow DT = " + lowDriftTime + "\tHigh DT = " + highDriftTime);
                    //Console.WriteLine("Global Min = " + globalDriftTimeMinimum + "\tGlobal Max = " + globalDriftTimeMaximum);
                    //peak.PrintPeakToConsole();
                    //Console.WriteLine("===============================================================");
                }
            }

            // Find the Conformation that has the highest member count and store the value into all conformations of this LC-IMS-MS Feature
            if (newLCIMSMSFeatureList.Count > 0)
            {
                var maxMemberCount = newLCIMSMSFeatureList.Select(feature => feature.GetMemberCount()).Max();

                foreach (var feature in newLCIMSMSFeatureList)
                {
                    feature.MaxMemberCount = maxMemberCount;
                }
            }

            return(newLCIMSMSFeatureList);
        }