CanvasPartition.WaveletSegmentation.HaarWavelets C# (CSharp) Method

HaarWavelets() public static method

Enrty function for performing Unbalanced Haar (UH) wavelets decomposition for change point (breakpoint) detection
public static HaarWavelets ( double ratio, double thresholdlower, double thresholdupper, List breakpoints, bool isGermline ) : void
ratio double
thresholdlower double
thresholdupper double
breakpoints List
isGermline bool
return void
        public static void HaarWavelets(double[] ratio, double thresholdlower, double thresholdupper, List<int> breakpoints, bool isGermline)
        {
            // tree = A list of J matrices (list of lists in C#), where J represents the number of UH “scales”.
            // Each matrix is of size 5 x (the number of UH coefficients at a given scale).
            // Each column (= vector // of length 5) contains an Unbalanced Haar coefficient in the following format:
            // 1st component - an index of the coefficient; 2nd component - the value of the
            // coefficient; 3rd component - time point where the corresponding UH vector
            // starts; 4th component - last time point before the breakpoint of the UH vector;
            // 5th component - end point of the UH vector.

            List<List<double>> tree = new List<List<double>>();
            double smooth = 0;
            List<double> ratio_list = new List<double>(ratio);
            FindBestUnbalancedHaarDecomposition(ratio, tree, smooth);
            // set threshold proportional to SD as and sample size as suggested in
            // Piotr Fryzlewicz, Journal of the American Statistical Association
            // Vol. 102, No. 480 (Dec., 2007), pp. 1318-1327
            double mad = CanvasCommon.Utilities.Mad(ratio_list) * 2.0;
            if (mad < thresholdlower)
            {
                mad = thresholdlower;
            }
            if (mad > thresholdupper)
            {
                mad = thresholdupper;
            }
            HardThresh(tree, mad, isGermline);
            Console.WriteLine("Wavelet threshold:");
            Console.WriteLine(mad);
            GetSegments(tree, smooth, breakpoints);
            if (isGermline)
                RefineSegments(breakpoints, ratio_list);
        }

Usage Example

Ejemplo n.º 1
0
        public Dictionary <string, List <int> > LaunchWavelets(Dictionary <string, double[]> coverageByChr, Dictionary <string, uint[]> startByChr,
                                                               Dictionary <string, uint[]> endByChr, double?CV, List <double> factorOfThreeCMADs)
        {
            var inaByChr          = new Dictionary <string, int[]>();
            var finiteScoresByChr = new Dictionary <string, double[]>();

            var tasks = coverageByChr.Select(scoreByChrKVP => new ThreadStart(() =>
            {
                string chr = scoreByChrKVP.Key;
                Helper.GetFiniteIndices(scoreByChrKVP.Value, out int[] ina); // not NaN, -Inf, Inf

                double[] scores;
                if (ina.Length == scoreByChrKVP.Value.Length)
                {
                    scores = scoreByChrKVP.Value;
                }
                else
                {
                    Helper.ExtractValues <double>(scoreByChrKVP.Value, ina, out scores);
                }

                lock (finiteScoresByChr)
                {
                    finiteScoresByChr[chr] = scores;
                    inaByChr[chr]          = ina;
                }
            })).ToList();


            Parallel.ForEach(tasks, task => task.Invoke());
            // Quick sanity-check: If we don't have any segments, then return a dummy result.
            int n = finiteScoresByChr.Values.Sum(list => list.Length);

            if (n == 0)
            {
                return(new Dictionary <string, List <int> >());
            }

            var breakpointsByChr = new Dictionary <string, List <int> >();

            tasks = coverageByChr.Keys.Select(chr => new ThreadStart(() =>
            {
                var breakpoints = new List <int>();
                // to cover cases of no SNVs present (i.e. chrY) => chromosome becomes one segment
                int segmentLengthByChr = Math.Max(coverageByChr[chr].Length, 1);
                if (segmentLengthByChr > _parameters.MinSize)
                {
                    WaveletSegmentation.HaarWavelets(coverageByChr[chr], _parameters.ThresholdLower,
                                                     _parameters.ThresholdUpper,
                                                     breakpoints, _parameters.IsGermline, _parameters.MadFactor,
                                                     CV, factorOfThreeCMADs, chr);
                }

                lock (breakpointsByChr)
                {
                    breakpointsByChr[chr] = breakpoints;
                }
            })).ToList();

            Console.WriteLine("{0} Launching wavelet tasks", DateTime.Now);
            Parallel.ForEach(tasks, task => task.Invoke());
            Console.WriteLine("{0} Completed wavelet tasks", DateTime.Now);
            Console.WriteLine("{0} Segmentation results complete", DateTime.Now);
            return(breakpointsByChr);
        }
All Usage Examples Of CanvasPartition.WaveletSegmentation::HaarWavelets