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();
        }
        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);
        }
Пример #3
0
        //This test is left commented out. It is useful for debugging
        //[Test]
        //public void processEntireIsosTraditionalTest1()
        //{
        //    string sourceIsosFile =
        //        @"\\protoapps\UserData\Slysz\Data\Redmine_Issues\Issue947_saturationTesting\Sarc_P09_B06_0786_20Jul11_Cheetah_11-05-31_DMSVersion_isos.csv";

        //    sourceIsosFile =
        //        @"C:\Users\d3x720\Documents\PNNL\My_DataAnalysis\2012\IMS_related\2012_01_27_Saturation_threshold_analysis\Sarc_P09_B06_0786_20Jul11_Cheetah_11-05-31_Sat90000_isos.csv";

        //    sourceIsosFile =
        //        @"C:\Users\d3x720\Documents\PNNL\My_DataAnalysis\2012\IMS_related\2012_01_27_Saturation_threshold_analysis\Sarc_P09_B06_0786_20Jul11_Cheetah_11-05-31_Original_isos.csv";

        //    //sourceIsosFile =
        //    //    @"\\protoapps\UserData\Slysz\Data\Redmine_Issues\temp\Sarc_P09_B06_0786_20Jul11_Cheetah_11-05-31_mass1064_repaired_isos.csv";

        //    sourceIsosFile =
        //        @"D:\Data\UIMF\Sarc_Main_Study_Controls\Sarc_P08_G02_0746_7Dec11_Cheetah_11-09-04_isos.csv";

        //    sourceIsosFile=
        //    @"D:\Data\UIMF\Sarc_Main_Study_Controls\Sarc_P08_A01_0673_21Nov11_Cheetah_11-09-03_isos.csv";


        //    sourceIsosFile =
        //        @"D:\Data\UIMF\Sarc_Main_Study_Controls\DrillDown_2012_03_20\Sarc_P27_A01_2497_12Dec11_Cheetah_11-09-34_filtered_filtered_isos.csv";


        //    string copiedIsosFileName = @"\\protoapps\UserData\Slysz\Data\Redmine_Issues\Issue947_saturationTesting\Sarc_P09_B06_0786_20Jul11_Cheetah_11-05-31_isos.csv";

        //    File.Copy(sourceIsosFile, copiedIsosFileName, true);


        //    string testFile =
        //        @"\\protoapps\UserData\Slysz\Data\Redmine_Issues\Issue947_saturationTesting\FF_IMS4Filters_NoFlags_20ppm_Min3Pts_4MaxLCGap_NoDaCorr_ConfDtn_2011-05-16_issue947.ini";
        //    var iniReader = new IniReader(testFile);
        //    iniReader.UpdateSettings();

        //    var isosReader = new IsosReader(Settings.InputFileName, Settings.OutputDirectory);

        //    var controller = new LCIMSMSFeatureFinderController(isosReader);
        //    controller.Execute();

        //    FileInfo fileInfo = new FileInfo(sourceIsosFile);
        //    var directoryInfo = fileInfo.Directory;


        //    string sourceFeaturesFile = copiedIsosFileName.Replace("_isos.csv", "_LCMSFeatures.txt");
        //    string sourceLogFile = copiedIsosFileName.Replace("_isos.csv", "_FeatureFinder_Log.txt");

        //    string copiedFeaturesFile = directoryInfo.FullName + "\\" +
        //                                fileInfo.Name.Replace("_isos.csv", "_LCMSFeatures.txt");
        //    string copiedFeaturesLogFile = directoryInfo.FullName + "\\" +
        //                                fileInfo.Name.Replace("_isos.csv", "_FeatureFinder_Log.txt");

        //    //copy the LCMSFeatures file back to my working directory
        //    File.Copy(sourceFeaturesFile, copiedFeaturesFile,true);
        //    File.Copy(sourceLogFile, copiedFeaturesLogFile, true);


        //}


        //[Test]
        //public void processEntireIsosSaturationRepairedTest1()
        //{
        //    string sourceIsosFile =
        //        @"D:\Data\UIMF\Sarc_Main_Study_Controls\Sarc_P09_B06_0786_20Jul11_Cheetah_11-05-31_saturationFixed_all_2011_12_30_isos.csv";

        //    string copiedIsosFileName = @"\\protoapps\UserData\Slysz\Data\Redmine_Issues\Issue947_saturationTesting\Sarc_P09_B06_0786_20Jul11_Cheetah_11-05-31_isos.csv";

        //    File.Copy(sourceIsosFile, copiedIsosFileName, true);


        //    string testFile =
        //        @"\\protoapps\UserData\Slysz\Data\Redmine_Issues\Issue947_saturationTesting\FF_IMS4Filters_NoFlags_20ppm_Min3Pts_4MaxLCGap_NoDaCorr_ConfDtn_2011-05-16_issue947.ini";

        //    var iniReader = new IniReader(testFile);
        //    iniReader.UpdateSettings();

        //    var isosReader = new IsosReader(Settings.InputFileName, Settings.OutputDirectory);

        //    var controller = new LCIMSMSFeatureFinderController(isosReader);
        //    controller.Execute();
        //}

        private void DisplayFeatureStats(LCIMSMSFeature testFeature1)
        {
            Console.WriteLine("OrigIndex= \t" + testFeature1.OriginalIndex);
            testFeature1.GetMinAndMaxScanLCAndScanIMSAndMSFeatureRep(
                out var scanLCMinimum, out var scanLCMaximum,
                out var scanIMSMinimum, out var scanIMSMaximum, out _);

            Console.WriteLine("FrameStart= \t" + scanLCMinimum);
            Console.WriteLine("FrameStop= \t" + scanLCMaximum);
            Console.WriteLine("IMSScan_Start= \t" + scanIMSMinimum);
            Console.WriteLine("IMSScan_Stop= \t" + scanIMSMaximum);
            Console.WriteLine("DriftTime= \t" + testFeature1.DriftTime);
            Console.WriteLine("monoMass = \t" + testFeature1.CalculateAverageMonoisotopicMass().ToString("0.0####"));
            Console.WriteLine("maxAbundance = \t" + testFeature1.AbundanceMaxRaw);
            Console.WriteLine("summedAbundance = \t" + testFeature1.AbundanceSumRaw);
            Console.WriteLine("totMemberCount = \t" + testFeature1.GetMemberCount());
            Console.WriteLine("totSaturated = \t" + testFeature1.GetSaturatedMemberCount());
        }
        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;
        }
        public static IEnumerable <LCIMSMSFeature> SplitLCIMSMSFeaturesByScanLC(IEnumerable <LCIMSMSFeature> lcImsMsFeatureEnumerable)
        {
            var lcimsmsFeatureList = new List <LCIMSMSFeature>();

            var lcImsMsFeatureIndex = 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 = lcImsMsFeatureIndex;
                        lcImsMsFeatureIndex++;
                        lcimsmsFeatureList.Add(newLcImsMsFeature);
                    }
                    else
                    {
                        newLcImsMsFeature?.AddImsMsFeature(imsMsFeature);
                    }

                    referenceScanLC = scanLC;
                }
            }

            return(lcimsmsFeatureList);
        }
Пример #6
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);
        }
        public static bool DoLcImsMsFeaturesFitTogether(LCIMSMSFeature feature1, LCIMSMSFeature feature2)
        {
            if (feature1.Charge != feature2.Charge)
            {
                return(false);
            }

            var minLCScan1 = int.MaxValue;
            var minLCScan2 = int.MaxValue;
            var maxLCScan1 = int.MinValue;
            var maxLCScan2 = int.MaxValue;

            var lcScanToImsMsFeatureMap1 = new Dictionary <int, imsMsFeature>();

            foreach (var imsMsFeature1 in feature1.imsMsFeatureList)
            {
                var lcScan = imsMsFeature1.ScanLC;

                if (lcScan < minLCScan1)
                {
                    minLCScan1 = lcScan;
                }
                if (lcScan > maxLCScan1)
                {
                    maxLCScan1 = lcScan;
                }

                lcScanToImsMsFeatureMap1.Add(imsMsFeature1.ScanLC, imsMsFeature1);
            }

            foreach (var imsMsFeature2 in feature2.imsMsFeatureList)
            {
                var lcScan = imsMsFeature2.ScanLC;

                if (lcScan < minLCScan2)
                {
                    minLCScan2 = lcScan;
                }
                if (lcScan > maxLCScan2)
                {
                    maxLCScan2 = lcScan;
                }

                if (!lcScanToImsMsFeatureMap1.TryGetValue(lcScan, out var imsMsFeature1))
                {
                    continue;
                }
                if (DoImsMsFeaturesFitTogether(imsMsFeature1, imsMsFeature2))
                {
                    continue;
                }
                return(false);
            }

            if (minLCScan1 - maxLCScan2 - 1 > Settings.LCGapSizeMax)
            {
                return(false);
            }

            if (minLCScan2 - maxLCScan1 - 1 > Settings.LCGapSizeMax)
            {
                return(false);
            }

            return(true);
        }