예제 #1
0
 public void CalComGroup(Inputdata x)
 {
     Dx = new double[x.controlData.GetUpperBound(1), x.controlData.GetUpperBound(1)];
     double[,] linkageData = new double[x.Tem - 6, 6];
     for (int i = 0; i < x.controlData.GetUpperBound(1); i++)
     {
         for (int j = i + 1; j < x.controlData.GetUpperBound(1); j++)
         {
             int AABB = 0;
             int AABb = 0;
             int AaBB = 0;
             int AaBb = 0;
             int AAbb = 0;
             int aaBB = 0;
             int aaBb = 0;
             int aabb = 0;
             int Aabb = 0;
             for (int t = 0; t < x.PeoNum; t++)
             {
                 if (x.controlData[t, i] == 0 && x.controlData[t, j] == 0)
                 {
                     AABB++;
                 }
                 if (x.controlData[t, i] == 0 && x.controlData[t, j] == 1)
                 {
                     AABb++;
                 }
                 if (x.controlData[t, i] == 1 && x.controlData[t, j] == 0)
                 {
                     AaBB++;
                 }
                 if (x.controlData[t, i] == 1 && x.controlData[t, j] == 1)
                 {
                     AaBb++;
                 }
                 if (x.controlData[t, i] == 0 && x.controlData[t, j] == 2)
                 {
                     AAbb++;
                 }
                 if (x.controlData[t, i] == 2 && x.controlData[t, j] == 0)
                 {
                     aaBB++;
                 }
                 if (x.controlData[t, i] == 2 && x.controlData[t, j] == 1)
                 {
                     aaBb++;
                 }
                 if (x.controlData[t, i] == 2 && x.controlData[t, j] == 2)
                 {
                     aabb++;
                 }
                 if (x.controlData[t, i] == 1 && x.controlData[t, j] == 2)
                 {
                     Aabb++;
                 }
             }
             double LinkAB  = AABB + AABb + AaBB + AaBb;
             double PA      = (double)(2 * AABB + 2 * AABb + AaBB + 2 * AAbb + Aabb + AaBb) / (2 * x.PeoNum);
             double PB      = (double)(2 * AABB + AABb + 2 * AaBB + 2 * aaBB + aaBb + AaBb) / (2 * x.PeoNum);
             double PAB     = 0.25;
             double Pab     = 0.25;
             double PAb     = 0.25;
             double PaB     = 0.25;
             double LastPar = 0;
             double AB      = 0;
             double Ab      = 0;
             par = 0.5;
             double DM = 0;
             do
             {
                 LastPar = par;
                 PAB     = (2 * AABB + AABb + AaBB + AaBb * par) / (2 * x.PeoNum);
                 PAb     = (2 * AAbb + AABb + Aabb + AaBb * (1 - par)) / (2 * x.PeoNum);
                 PaB     = (2 * aaBB + AaBB + aaBb + AaBb * (1 - par)) / (2 * x.PeoNum);
                 Pab     = (2 * aabb + Aabb + aaBb + AaBb * par) / (2 * x.PeoNum);
                 par     = PAB * Pab / (PAB * Pab + PAb * PaB);
                 AB      = 2 * AABB + AABb + AaBB + AaBb * par;
                 Ab      = 2 * AAbb + AABb + Aabb + AaBb * (1 - par);
             } while (Math.Abs(par - LastPar) > 0.000001);
             double D = PAB * Pab - PAb * PaB;
             if (D > 0)
             {
                 if (PA * (1 - PB) > (1 - PA) * PB)
                 {
                     DM = (1 - PA) * PB;
                 }
                 else
                 {
                     DM = PA * (1 - PB);
                 }
             }
             if (D < 0)
             {
                 if (-PA * PB > -(1 - PA) * (1 - PB))
                 {
                     DM = -PA * PB;
                 }
                 else
                 {
                     DM = -(1 - PA) * (1 - PB);
                 }
             }
             Dx[i, j] = D / DM;
             r2       = D * D / (PAB * PAb * PaB * Pab);
             if (r2 < 0.1)
             {
                 continue;
             }
             else
             {
                 linkageData[i, 0] = i;
                 linkageData[i, 1] = j;
                 linkageData[i, 2] = PAB;
                 linkageData[i, 3] = PAb;
                 linkageData[i, 4] = PaB;
                 linkageData[i, 4] = Pab;
             }
         }
     }
 }
예제 #2
0
        public void CaseControlEvaluation(GeneticDistance newGeneticDistance, MapData newMapData, BlockDictionary newBlockDictionary, Commands newCommands, ResultList finalResultList)
        {
            SnpDataSet caseData    = new SnpDataSet(); //Creat Case data set
            SnpDataSet controlData = new SnpDataSet(); //Creat Control data set

            Console.Write("\tReading ped data...");
            Inputdata inputData = new Inputdata();

            inputData.data(caseData, controlData, newCommands);  //start getting the data and put into case/comtrol set
            ArrayList controlIndividualList = new ArrayList();
            ArrayList caseIndividualList    = new ArrayList();

            Console.Write("finished.\n");
            inputData = null;
            GC.Collect();
            Console.Write("Analyzing LD data...");
            double[,] controlLinkageData = new double[controlData.GetSnpCount(), 7];
            //caseData.Linkage(caseLinkageData);
            controlData.SetIndividualList(controlIndividualList);
            controlData = null;
            GC.Collect();
            Console.Write("finished.\n");
            finalResultList.SetControlResult(caseData.individual.Length, 0.2);

            Console.WriteLine("Haplotyping for all the control samples...");


            for (int indIndex = 0; indIndex < controlIndividualList.Count; indIndex++)
            {
                ((Individual)(controlIndividualList[indIndex])).SetIndividualID(indIndex);
            }


            int ind = 0;

            foreach (Individual newIndividual in controlIndividualList) //haplotyping for each individual.
            {
                newIndividual.Haplotyping(newBlockDictionary, newMapData, controlLinkageData);
                ind++;
                Console.SetCursorPosition(8, Console.CursorTop);
                Console.Write("{0} / {1}", ind, controlIndividualList.Count);
            }
            Console.Write("\t\tFinished.\n");



            for (int n = 0; n < newCommands.permutation; n++)  //permutation
            {
                Console.Write("Iteration: {0} / {1} \n", n + 1, newCommands.permutation);

                List <GroupShare> resultList        = new List <GroupShare>(); //unfinished result from last window
                List <int>        selectControlList = new List <int>();


                selectControlList = this.Select(controlIndividualList.Count, caseData.individual.Length, new Random());
                //selectControlList = this.Select(controlIndividualList.Count, 30, new Random());

                for (int windowStart = 0; windowStart <= newBlockDictionary.blockList.Count - 1; windowStart += 30000)
                {
                    List <GroupShare> newResultList      = new List <GroupShare>();
                    int       windowEnd                  = Math.Min(windowStart + 30000, newBlockDictionary.blockList.Count - 1);
                    ArrayList wholePairwiseIBDRegionList = new ArrayList();
                    Console.WriteLine("\t\tSearching for pairwise sharing regions");
                    for (int i = 0; i < selectControlList.Count; i++)
                    {
                        for (int j = i + 1; j < selectControlList.Count; j++)
                        {
                            ((Individual)controlIndividualList[selectControlList[i]]).PairwiseComparison(wholePairwiseIBDRegionList, (Individual)controlIndividualList[selectControlList[j]], (Block[])newBlockDictionary.blockList.ToArray(typeof(Block)), newMapData, newCommands.cut, windowStart, windowEnd);
                        }
                        Console.SetCursorPosition(16, Console.CursorTop);
                        Console.Write("{0} / {1}\tTotal regions: {2}", i + 1, selectControlList.Count, wholePairwiseIBDRegionList.Count);
                    }
                    Console.WriteLine("\t\tFinished.");
                    MyIBDComparer newComparer = new MyIBDComparer();
                    wholePairwiseIBDRegionList.Sort(newComparer);
                    GroupShareFinder newIBDList = new GroupShareFinder(wholePairwiseIBDRegionList);
                    Console.WriteLine("\t\tSrart searching for group sharing regions: ");

                    newIBDList.findGroupIBD(controlIndividualList, newBlockDictionary, controlLinkageData, newResultList, resultList, windowStart, windowEnd);
                    finalResultList.AddNewControlList(newResultList, caseData.individual.Length, 0.2);
                }
            }
            Console.WriteLine("Finished permutation in controls");

            finalResultList.CalculateControlResultParameters();
            Console.WriteLine("Finished puermutation result calculation");



            caseData.SetIndividualList(caseIndividualList);
            caseData = null;
            GC.Collect();



            Console.WriteLine("Haplotyping for all case samples...");

            for (int indIndex = 0; indIndex < caseIndividualList.Count; indIndex++)
            {
                ((Individual)(caseIndividualList[indIndex])).SetIndividualID(indIndex);
            }


            ind = 0;
            foreach (Individual newIndividual in caseIndividualList) //haplotyping for each individual.
            {
                newIndividual.Haplotyping(newBlockDictionary, newMapData, controlLinkageData);
                ind++;
                Console.SetCursorPosition(8, Console.CursorTop);
                Console.Write("{0} / {1}", ind, caseIndividualList.Count);
            }
            Console.Write("\t\tFinished.\n");



            for (int n = 0; n < 1; n++)  //case evaluation
            {
                Console.Write("\tTotally {0} blocks will be analyzed.\n", newBlockDictionary.blockList.Count);

                List <GroupShare> resultList = new List <GroupShare>(); //unfinished result from last window


                for (int windowStart = 0; windowStart <= newBlockDictionary.blockList.Count - 1; windowStart += 30000)
                {
                    List <GroupShare> newResultList = new List <GroupShare>();
                    int windowEnd = Math.Min(windowStart + 30000, newBlockDictionary.blockList.Count - 1);
                    Console.WriteLine("\tAnalyzing block {0} to {1}", windowStart, windowEnd - 1);
                    ArrayList wholePairwiseIBDRegionList = new ArrayList();
                    Console.WriteLine("\t\tSearching for pairwise sharing regions");
                    for (int i = 0; i < caseIndividualList.Count; i++)
                    {
                        for (int j = i + 1; j < caseIndividualList.Count; j++)
                        {
                            ((Individual)caseIndividualList[i]).PairwiseComparison(wholePairwiseIBDRegionList, (Individual)caseIndividualList[j], (Block[])newBlockDictionary.blockList.ToArray(typeof(Block)), newMapData, newCommands.cut, windowStart, windowEnd);
                        }
                        Console.SetCursorPosition(16, Console.CursorTop);
                        Console.Write("{0} / {1}\tTotal regions: {2}", i + 1, caseIndividualList.Count, wholePairwiseIBDRegionList.Count);
                    }
                    Console.WriteLine("\n\t\tFinished searching pairwise sharing regions");

                    MyIBDComparer newComparer = new MyIBDComparer();
                    wholePairwiseIBDRegionList.Sort(newComparer);
                    GroupShareFinder newIBDList = new GroupShareFinder(wholePairwiseIBDRegionList);
                    Console.WriteLine("\t\tSrart searching for group sharing regions:");

                    newIBDList.findGroupIBD(caseIndividualList, newBlockDictionary, controlLinkageData, newResultList, resultList, windowStart, windowEnd);
                    finalResultList.calculateAllPvalue(newResultList, caseIndividualList.Count, 0.2, newCommands);
                    finalResultList.Combine(newResultList);
                    finalResultList.ClearReplication();
                }
            }
            finalResultList.PrintResult(newBlockDictionary, caseIndividualList.Count, 0.2, newCommands);
        }