Пример #1
0
        public void PrintResult(BlockDictionary newBlockDictionary, int totalCase, double maxIndRate, Commands newCommands)
        {
            MyResultAValueComparer newComparer = new MyResultAValueComparer();

            result.Sort(newComparer);

            FileStream   cFile = new FileStream(newCommands.outputName, FileMode.Append);
            StreamWriter sw1   = new StreamWriter(cFile);

            if (result.Count == 0)
            {
                return;
            }

            int count = 0;

            for (int i = 0; i < result.Count; i++)
            {
                if (i > 200 && (((GroupShare)result[i]).AValue > 0.05 || ((GroupShare)result[i]).individualList.Count > totalCase * maxIndRate))
                {
                    //limit++;
                    continue;
                }
                Block  firstBlock = ((Block)(newBlockDictionary.blockList[((GroupShare)result[i]).allSharedFirstBlockID]));
                Block  lastBlock  = ((Block)(newBlockDictionary.blockList[((GroupShare)result[i]).allSharedLastBlockID]));
                double a          = 1.0 / ((GroupShare)result[i]).AValue;
                sw1.Write("{0}\t", ((GroupShare)result[i]).AValue);
                sw1.Write("{0}\t{1}\t{2:00000.0}\t{3}:{4}\t{5}\t", newCommands.chromosome, ((GroupShare)result[i]).individualList.Count, (lastBlock.GetEnd() - firstBlock.GetStart()) / 1000, firstBlock.GetStart(), lastBlock.GetEnd(), lastBlock.GetEndSnpID() - firstBlock.GetStartSnpID() + 1);
                for (int j = 0; j < ((GroupShare)result[i]).individualList.Count; j++)
                {
                    if (j < ((GroupShare)result[i]).individualList.Count - 1)
                    {
                        sw1.Write("{0},", ((GroupShare)result[i]).individualList[j]);
                    }
                    else
                    {
                        sw1.Write("{0}\t", ((GroupShare)result[i]).individualList[j]);
                    }
                }
                sw1.Write("{0}:{1}\t", firstBlock.GetStartSnpID(), lastBlock.GetEndSnpID());

                for (int j = 0; j < ((GroupShare)result[i]).individualList.Count; j++)
                {
                    if (j < ((GroupShare)result[i]).individualList.Count - 1)
                    {
                        sw1.Write("{0}:{1},", (((Block)(newBlockDictionary.blockList[((GroupShare)result[i]).partiallySharedRegion[j, 0]])).GetStartSnpID()), (((Block)(newBlockDictionary.blockList[((GroupShare)result[i]).partiallySharedRegion[j, 1]])).GetEndSnpID()));
                    }
                    else
                    {
                        sw1.Write("{0}:{1}\t", (((Block)(newBlockDictionary.blockList[((GroupShare)result[i]).partiallySharedRegion[j, 0]])).GetStartSnpID()), (((Block)(newBlockDictionary.blockList[((GroupShare)result[i]).partiallySharedRegion[j, 1]])).GetEndSnpID()));
                    }
                }

                for (int blockID = ((GroupShare)result[i]).allSharedFirstBlockID; blockID <= ((GroupShare)result[i]).allSharedLastBlockID; blockID++)
                {
                    Block currentBlock = ((Block)(newBlockDictionary.blockList[blockID]));
                    sw1.Write("|");
                    for (int snpID = 0; snpID <= currentBlock.GetEndSnpID() - currentBlock.GetStartSnpID(); snpID++)
                    {
                        sw1.Write("{0}", currentBlock.Gethaplotype((int)(((GroupShare)result[i]).haplotypeList[blockID - ((GroupShare)result[i]).allSharedFirstBlockID]), snpID));
                    }
                    if (blockID == ((GroupShare)result[i]).allSharedLastBlockID)
                    {
                        break;
                    }
                    sw1.Write("|");
                    for (int snpID = currentBlock.GetEndSnpID() + 1; snpID < ((Block)(newBlockDictionary.blockList[blockID + 1])).GetStartSnpID(); snpID++)
                    {
                        sw1.Write("-");
                    }
                }

                sw1.Write("\t");
                for (int blockID = ((GroupShare)result[i]).allSharedFirstBlockID; blockID <= ((GroupShare)result[i]).allSharedLastBlockID; blockID++)
                {
                    sw1.Write("{0},", ((Block)(newBlockDictionary.blockList[blockID])).GetFrequency((int)(((GroupShare)result[i]).haplotypeList[blockID - ((GroupShare)result[i]).allSharedFirstBlockID])).ToString("0.0000"));
                }
                sw1.WriteLine();
                if (i > 4)
                {
                    count++;
                }
            }
            Console.Write("{0} regions found in cases, {1} regions found to be with significant p value (0.05).\nFinished writing significant results in outputfile.", this.result.Count, count);
            sw1.Close();
        }
Пример #2
0
        public void SetAValue(BlockDictionary newBlockDictionary, ArrayList newIndividualList, double[,] newLinkageData)
        {
            double[] singleP = new double[this.individualList.Count];
            for (int i = 0; i < this.individualList.Count; i++)
            {
                for (int j = this.partiallySharedRegion[i, 0]; j <= this.partiallySharedRegion[i, 1] && j <= this.partiallySharedLastBlockID; j++)
                {
                    double nextFrequency = ((Block)(newBlockDictionary.blockList[j])).GetFrequency((int)(this.partiallySharedBlockHaplotype[j - this.partiallySharedFirstBlockID]));
                    nextFrequency = 0.0 - Math.Log10(nextFrequency);
                    singleP[i]   += nextFrequency;
                    if (j != this.partiallySharedRegion[i, 1] && j != this.partiallySharedLastBlockID)
                    {
                        ArrayList recombinationSnpList = new ArrayList();
                        for (int k = ((Block)(newBlockDictionary.blockList[j])).GetEndSnpID() - ((Block)(newBlockDictionary.blockList[this.partiallySharedFirstBlockID])).GetStartSnpID() + 1; k <= ((Block)(newBlockDictionary.blockList[j + 1])).GetStartSnpID() - ((Block)(newBlockDictionary.blockList[this.partiallySharedFirstBlockID])).GetStartSnpID() - 1; k++)
                        {
                            recombinationSnpList.Add(this.partiallySharedSnpHaplotype[k]);
                        }

                        nextFrequency = Math.Log10(this.GetRecombinationAValue(((Block)(newBlockDictionary.blockList[j])).GetEndSnpID() + 1, recombinationSnpList, newLinkageData));
                    }
                }
            }
            for (int i = 0; i < this.individualList.Count; i++)
            {
                this.AValue += singleP[i];
                if (i == 10)
                {
                    break;
                }
            }
            double sharedRegionAValue = 0;

            for (int i = allSharedFirstBlockID; i <= allSharedLastBlockID; i++)
            {
                sharedRegionAValue -= Math.Log10(((Block)(newBlockDictionary.blockList[i])).GetFrequency((int)haplotypeList[i - allSharedFirstBlockID]));
                if (i != allSharedLastBlockID)
                {
                    ArrayList recombinationSnpList = new ArrayList();
                    for (int j = ((Block)(newBlockDictionary.blockList[i])).GetEndSnpID() - ((Block)(newBlockDictionary.blockList[this.allSharedFirstBlockID])).GetStartSnpID() + 1; j <= ((Block)(newBlockDictionary.blockList[i + 1])).GetStartSnpID() - ((Block)(newBlockDictionary.blockList[this.allSharedFirstBlockID])).GetStartSnpID() - 1; j++)
                    {
                        recombinationSnpList.Add(this.allSharedSnpHaplotype[j]);
                    }

                    if (sharedRegionAValue > 1000000)
                    {
                        Console.Write("here");
                    }
                }
                if (i == 10)
                {
                    break;
                }
            }

            double h0 = 1;

            for (int newBlockIndex = this.partiallySharedFirstBlockID; newBlockIndex <= this.partiallySharedLastBlockID; newBlockIndex++)
            {
                int shareCount = 0;
                for (int i = 0; i < this.individualList.Count; i++)
                {
                    if (this.partiallySharedRegion[i, 0] <= newBlockIndex && this.partiallySharedRegion[i, 1] >= newBlockIndex)
                    {
                        shareCount++;
                    }
                }

                h0 += Math.Log10(Math.Pow(2, shareCount)) - CombinationLog(shareCount, shareCount * 2);
            }
            h0 = h0 - this.AValue;

            int    generation;
            double length;

            length     = (((Block)(newBlockDictionary.blockList[this.partiallySharedLastBlockID])).GetEnd() - ((Block)(newBlockDictionary.blockList[this.partiallySharedFirstBlockID])).GetStart()) / 1000000.0;
            generation = Convert.ToInt32(1.0 / length * 100);
            double h1 = Math.Log10(0.5) * this.individualList.Count * generation;

            this.AValue = h1 - h0;
        }
Пример #3
0
        static void Main(string[] args)
        {
            Commands newCommends = new Commands();

            if (!newCommends.ReadCommand(args))
            {
                return;
            }
            Stopwatch stw = new Stopwatch();

            stw.Start();

            if (newCommends.chromosome == 0)
            {
                Regex      r     = new Regex(@"[0-9]+");
                FileStream cFile = new FileStream(newCommends.outputName, FileMode.Create);
                cFile.Close();
                for (int i = 1; i < 23; i++)
                {
                    //Console.WriteLine(i);
                    newCommends.chromosome = i;

                    newCommends.pedName = r.Replace(newCommends.pedName, i.ToString());
                    newCommends.mapName = r.Replace(newCommends.mapName, i.ToString());
                    //newCommends.
                    Console.WriteLine("Start reading file data:");
                    Console.Write("\tReading genetic distance data...");
                    GeneticDistance geneticdistance = new GeneticDistance(); //store all the snp genetic distance and recombination data.
                    geneticdistance.InitialGeniticDistance(0, 300000, newCommends);
                    Console.Write("finished.\n");
                    Console.Write("\tReading map data...");

                    MapData mapdata = new MapData();                       //store all the snp physical position in the map file.
                    mapdata.ReadMapFile(newCommends);
                    mapdata.ReadGeneticDistance(geneticdistance);
                    Console.Write("finished.\n");
                    Console.Write("\tReading haplotype frequency data...");

                    BlockDictionary newDictionary = new BlockDictionary(newCommends); //store all the block haplotype information in the Dictionary, the object type is Block.
                    newDictionary.Localization(mapdata);
                    Console.Write("finished.\n");
                    Main newEvaluation = new Main();
                    newEvaluation.Evalation(geneticdistance, mapdata, newDictionary, newCommends);
                }

                return;
            }
            else
            {
                FileStream cFile = new FileStream(newCommends.outputName, FileMode.Create);
                cFile.Close();
                Console.WriteLine("Start reading file data:");
                Console.Write("\tReading genetic distance data...");
                GeneticDistance geneticdistance = new GeneticDistance(); //store all the snp genetic distance and recombination data.
                geneticdistance.InitialGeniticDistance(0, 300000, newCommends);
                Console.Write("finished.\n");
                Console.Write("\tReading map data...");

                MapData mapdata = new MapData();                       //store all the snp physical position in the map file.
                mapdata.ReadMapFile(newCommends);
                mapdata.ReadGeneticDistance(geneticdistance);
                Console.Write("finished.\n");
                Console.Write("\tReading haplotype frequency data...");

                BlockDictionary newDictionary = new BlockDictionary(newCommends); //store all the block haplotype information in the Dictionary, the object type is Block.
                newDictionary.Localization(mapdata);
                Console.Write("finished.\n");
                Main newEvaluation = new Main();
                newEvaluation.Evalation(geneticdistance, mapdata, newDictionary, newCommends);
                return;
            }
            stw.Stop();
        }
Пример #4
0
        public void SetPartiallySharedRegion(ArrayList[] IBDTable, BlockDictionary newBlockDictionary, ArrayList wholeIndividualList, int windowStart, int windowEnd)
        {
            this.partiallySharedRegion         = new int[this.individualList.Count, 2];
            this.partiallySharedBlockHaplotype = new ArrayList();
            this.partiallySharedFirstBlockID   = windowStart;
            this.partiallySharedLastBlockID    = windowEnd - 1;
            //set the start point of the partially shared region.
            ArrayList lastRemainingIndividual = (ArrayList)this.individualList.Clone();

            for (int blockID = allSharedFirstBlockID - 1; blockID >= windowStart; blockID--)
            {
                int       bestSharedHaplotype  = -1;
                ArrayList remainningIndividual = new ArrayList();
                foreach (HaplotypeSharing newHaplotypeSharing in IBDTable[blockID]) //find the best sharing haplotype in this block.
                {
                    ArrayList intersection = this.SimpleIntersection(lastRemainingIndividual, newHaplotypeSharing.individualList);

                    if (intersection.Count > remainningIndividual.Count)
                    {
                        remainningIndividual = (ArrayList)intersection.Clone();
                        bestSharedHaplotype  = newHaplotypeSharing.haplotpeID;
                        if (remainningIndividual.Count == lastRemainingIndividual.Count)
                        {
                            //if (remainningIndividual.Count == this.individualList.Count)
                            //this.allSharedFirstBlockID = blockID;
                            break;
                        }
                    }
                }

                /*
                 * for (int recombinationSnpIndex = ((Block)(newBlockDictionary.blockList[blockID + 1])).GetStartSnpID() - 1; recombinationSnpIndex > ((Block)(newBlockDictionary.blockList[blockID])).GetEndSnpID(); recombinationSnpIndex--)
                 * {
                 *  ArrayList commonAlleleList = new ArrayList();
                 *  ArrayList rareAlleleList = new ArrayList();
                 *  for (int remainingIndex = 0; remainingIndex < remainningIndividual.Count; remainingIndex++)
                 *  {
                 *      if (((Individual)(wholeIndividualList[(int)remainningIndividual[remainingIndex]])).genotype[recombinationSnpIndex] == 0)
                 *          rareAlleleList.Add(remainingIndex);
                 *      else if (((Individual)(wholeIndividualList[(int)remainningIndividual[remainingIndex]])).genotype[recombinationSnpIndex] == 2)
                 *          commonAlleleList.Add(remainingIndex);
                 *      ArrayList removeList = new ArrayList();
                 *      if (rareAlleleList.Count >= commonAlleleList.Count)
                 *          removeList = (ArrayList)commonAlleleList.Clone();
                 *      else
                 *          removeList = (ArrayList)rareAlleleList.Clone();
                 *      removeList.Sort();
                 *      removeList.Reverse();
                 *      for (int removeIndex = 0; removeIndex < removeList.Count; removeIndex++)
                 *          remainningIndividual.RemoveAt((int)removeList[removeIndex]);
                 *  }
                 * }
                 */
                if (remainningIndividual.Count > 1)
                {
                    partiallySharedBlockHaplotype.Insert(0, bestSharedHaplotype);
                    if (remainningIndividual.Count == lastRemainingIndividual.Count)
                    {
                        continue;
                    }
                    else
                    {
                        ArrayList difference = this.GetDifferenceSet(lastRemainingIndividual, remainningIndividual);
                        foreach (int i in difference)
                        {
                            this.partiallySharedRegion[this.GetIndividualOrderInList(i), 0] = blockID + 1;
                        }
                        lastRemainingIndividual = (ArrayList)remainningIndividual.Clone();
                    }
                }
                else
                {
                    foreach (int i in lastRemainingIndividual)
                    {
                        this.partiallySharedRegion[this.GetIndividualOrderInList(i), 0] = blockID + 1;
                    }
                    this.partiallySharedFirstBlockID = blockID + 1;
                    //this.firstBlockID = blockID + 1;
                    break;
                }
            }
            for (int i = 0; i < this.individualList.Count; i++)
            {
                if (partiallySharedRegion[i, 0] == 0)
                {
                    partiallySharedRegion[i, 0] = windowStart;
                }
            }
            for (int i = 0; i < this.haplotypeList.Count; i++)
            {
                partiallySharedBlockHaplotype.Add(this.haplotypeList[i]);
            }
            //set the end point of the partially shared region.
            lastRemainingIndividual = (ArrayList)this.individualList.Clone();
            for (int blockID = allSharedLastBlockID + 1; blockID < windowEnd; blockID++)
            {
                int       bestSharedHaplotype  = -1;
                ArrayList remainningIndividual = new ArrayList();
                foreach (HaplotypeSharing newHaplotypeSharing in IBDTable[blockID]) //find the best sharing haplotype in this block.
                {
                    ArrayList intersection = this.SimpleIntersection(lastRemainingIndividual, newHaplotypeSharing.individualList);

                    if (intersection.Count > remainningIndividual.Count)
                    {
                        remainningIndividual = (ArrayList)intersection.Clone();
                        bestSharedHaplotype  = newHaplotypeSharing.haplotpeID;
                        if (remainningIndividual.Count == lastRemainingIndividual.Count)
                        {
                            //if (remainningIndividual.Count == this.individualList.Count)
                            //this.allSharedLastBlockID = blockID;
                            break;
                        }
                    }
                }
                if (remainningIndividual.Count > 1)
                {
                    partiallySharedBlockHaplotype.Add(bestSharedHaplotype);
                    if (remainningIndividual.Count == lastRemainingIndividual.Count)
                    {
                        continue;
                    }
                    else
                    {
                        ArrayList difference = this.GetDifferenceSet(lastRemainingIndividual, remainningIndividual);
                        foreach (int i in difference)
                        {
                            this.partiallySharedRegion[this.GetIndividualOrderInList(i), 1] = blockID - 1;
                        }
                        lastRemainingIndividual = (ArrayList)remainningIndividual.Clone();
                    }
                }
                else
                {
                    foreach (int i in lastRemainingIndividual)
                    {
                        this.partiallySharedRegion[this.GetIndividualOrderInList(i), 1] = blockID - 1;
                    }
                    this.partiallySharedLastBlockID = blockID - 1;
                    //this.lastBlockID = blockID - 1;
                    break;
                }
            }
            for (int i = 0; i < this.individualList.Count; i++)
            {
                if (partiallySharedRegion[i, 1] == 0)
                {
                    partiallySharedRegion[i, 1] = windowEnd;
                }
            }

/*
 *          int remainIndividual = 0;
 *          for (int i = 0; i < this.individualList.Count; i++)
 *          {
 *              if (((Block)(newBlockDictionary.blockList[partiallySharedRegion[i, 1]])).GetEndSnpID() - ((Block)(newBlockDictionary.blockList[partiallySharedRegion[i, 0]])).GetStartSnpID() <= 10)
 *              {
 *                  this.partiallySharedRegion[i, 0] = -1;
 *                  this.partiallySharedRegion[i, 1] = -1;
 *              }
 *              else
 *                  remainIndividual++;
 *          }
 *          ArrayList newIndividualList = new ArrayList();
 *          int[,] newPartiallySharedRegion = new int[remainIndividual, 2];
 *          int individualOrder = 0;
 *          for (int i = 0; i < this.individualList.Count; i++)
 *          {
 *              if (this.partiallySharedRegion[i, 0] >= 0)
 *              {
 *                  newIndividualList.Add(this.individualList[i]);
 *                  newPartiallySharedRegion[individualOrder, 0] = this.partiallySharedRegion[i, 0];
 *                  newPartiallySharedRegion[individualOrder, 1] = this.partiallySharedRegion[i, 1];
 *                  individualOrder++;
 *              }
 *          }
 *          this.individualList = (ArrayList)(newIndividualList.Clone());
 *          this.partiallySharedRegion = (int[,])newPartiallySharedRegion.Clone();
 */
            if (this.individualList.Count < 2)
            {
                return;
            }
            while (this.allSharedFirstBlockID > windowStart)
            {
                bool foundUpperBound = false;
                for (int i = 0; i < this.individualList.Count; i++)
                {
                    if (this.partiallySharedRegion[i, 0] >= this.allSharedFirstBlockID)
                    {
                        foundUpperBound = true;
                        break;
                    }
                }
                if (foundUpperBound)
                {
                    break;
                }
                else
                {
                    this.allSharedFirstBlockID--;
                    this.haplotypeList.Insert(0, this.partiallySharedBlockHaplotype[this.allSharedFirstBlockID - this.partiallySharedFirstBlockID]);
                    //break;
                }
            }

            while (this.allSharedLastBlockID < this.partiallySharedLastBlockID)
            {
                bool foundLowerBound = false;
                for (int i = 0; i < this.individualList.Count; i++)
                {
                    if (this.partiallySharedRegion[i, 1] <= this.allSharedLastBlockID)
                    {
                        foundLowerBound = true;
                        break;
                    }
                }
                if (foundLowerBound)
                {
                    break;
                }
                else
                {
                    // if (this.allSharedLastBlockID >= windowEnd)
                    //     break;
                    this.allSharedLastBlockID++;
                    this.haplotypeList.Add(this.partiallySharedBlockHaplotype[this.allSharedLastBlockID - this.partiallySharedFirstBlockID]);
                }
            }
            if (this.partiallySharedLastBlockID == 0)
            {
                this.partiallySharedLastBlockID = this.allSharedLastBlockID;
            }
        }
Пример #5
0
        public bool GroupShareExtention(ArrayList[] IBDTable, ArrayList newIndividualList, BlockDictionary newBlockDictionary, ArrayList newlyComingSharingList, List <GroupShare> finalResultList, ArrayList newlyAddedList)
        {
            if (this.allSharedLastBlockID >= IBDTable.GetUpperBound(0))                                             //the end of the block list, add to result if still some sharing left
            {
                if (this.individualList.Count > 1 && (this.allSharedLastBlockID - this.allSharedFirstBlockID >= 2)) //add to result list
                {
                    finalResultList.Add((GroupShare)this.MemberwiseClone());
                }
            }
            bool      foundNext             = false; //if find haplotype that can continue all the individuals in the next block, then "this" will not be added to list
            ArrayList passList              = new ArrayList();
            ArrayList waitingToAddGroupList = new ArrayList();

            for (int haplotypeIndex = 0; haplotypeIndex < newlyComingSharingList.Count; haplotypeIndex++)
            {
                bool      foundBefore = false;
                ArrayList pairListIntersection;
                pairListIntersection = this.GetPairListIntersection(this.sharePairList, ((GroupShare)(newlyComingSharingList[haplotypeIndex])).sharePairList);
                if (pairListIntersection.Count == this.sharePairList.Count)
                {
                    foundNext = true;
                    this.allSharedLastBlockID += 1;
                    this.haplotypeList.Add(((GroupShare)(newlyComingSharingList[haplotypeIndex])).haplotypeList[0]);
                    if (pairListIntersection.Count == ((GroupShare)(newlyComingSharingList[haplotypeIndex])).sharePairList.Count)
                    {
                        newlyComingSharingList.RemoveAt(haplotypeIndex);
                        haplotypeIndex--;
                    }
                    return(true);
                }
                foreach (ArrayList eachList in passList) //check this intersection has already go checking next block, so do not need to do again for this subgroup.
                {
                    if (eachList.Count != pairListIntersection.Count)
                    {
                        continue;
                    }
                    else
                    {
                        for (int i = 0; i < pairListIntersection.Count; i++)
                        {
                            if (((int[])pairListIntersection[i])[0] != ((int[])(eachList[i]))[0] || ((int[])pairListIntersection[i])[1] != ((int[])(eachList[i]))[1])
                            {
                                break;
                            }
                            if (i == (pairListIntersection.Count - 1) && ((int[])pairListIntersection[i])[0] == ((int[])(eachList[i]))[0] && ((int[])pairListIntersection[i])[1] == ((int[])(eachList[i]))[1])
                            {
                                foundBefore = true;
                            }
                        }
                    }
                }
                if (foundBefore == true)  //checked before, do not need to go checking
                {
                    break;
                }
                if (pairListIntersection.Count > 1)
                {
                    passList.Add(pairListIntersection.Clone());
                    GroupShare newGroupShare = new GroupShare();
                    newGroupShare.sharePairList         = (ArrayList)pairListIntersection.Clone();
                    newGroupShare.allSharedLastBlockID  = this.allSharedLastBlockID + 1;
                    newGroupShare.allSharedFirstBlockID = this.allSharedFirstBlockID;
                    newGroupShare.haplotypeList         = (ArrayList)this.haplotypeList.Clone();
                    newGroupShare.haplotypeList.Add(((GroupShare)(newlyComingSharingList[haplotypeIndex])).haplotypeList[0]);
                    newGroupShare.SetIndividualList();
                    if (newGroupShare.CheckRecombinationRegion(newIndividualList, newBlockDictionary)) // need to check the recombination region first.
                    {
                        waitingToAddGroupList.Add(newGroupShare);
                    }
                }
                if (foundNext == false && (this.allSharedLastBlockID - this.allSharedFirstBlockID >= 2))
                {
                    finalResultList.Add((GroupShare)(this.MemberwiseClone()));
                }
            }
            MyPrivateResultComparer newPrivateResultComparer = new MyPrivateResultComparer();

            waitingToAddGroupList.Sort(newPrivateResultComparer);
            for (int i = 0; i < waitingToAddGroupList.Count - 1; i++)
            {
                for (int j = i + 1; j < waitingToAddGroupList.Count; j++)
                {
                    if (((GroupShare)(waitingToAddGroupList[i])).ChekcSubGroup(((GroupShare)(waitingToAddGroupList[j]))) == true)
                    {
                        waitingToAddGroupList.RemoveAt(j);
                        j--;
                    }
                }
            }
            foreach (GroupShare newGroupShare in waitingToAddGroupList)
            {
                if (newlyAddedList.Count == 0)
                {
                    newlyAddedList.Add(newGroupShare);
                }
                else
                {
                    for (int i = 0; i < newlyAddedList.Count; i++)
                    {
                        if (((GroupShare)(newlyAddedList[i])).ChekcSubGroup(newGroupShare) == true)
                        {
                            break;
                        }
                        if (newGroupShare.ChekcSubGroup((GroupShare)(newlyAddedList[i])) == true)
                        {
                            newlyAddedList[i] = newGroupShare;
                            break;
                        }
                        if (i == newlyAddedList.Count - 1)
                        {
                            newlyAddedList.Add(newGroupShare);
                        }
                    }
                }
            }
            return(false);
        }
Пример #6
0
        public void PerformeSimulation(int populationSize, GeneticDistance geneticDistance, MapData mapData, ArrayList individualList, BlockDictionary blockDictionary, double[,] caseLinkageData) //random choose offspringID, simulat all offspring
        {
            inheritPosition = new int[this.offsprings.Length, 2];
            Random randomSeed = new Random();

            this.ancenstor = randomSeed.Next(populationSize);

            for (int i = 0; i < this.offsprings.Length; i++) //sumulate every offspring id.
            {
                int newOffspring = new int();
                newOffspring = randomSeed.Next(populationSize);
                bool found = false;
                for (int j = 0; j < i; j++)                                                   //simulate offspring id, random choose from population
                {
                    if (this.offsprings[j] == newOffspring || newOffspring == this.ancenstor) //avoid same id.
                    {
                        i--;
                        found = true;
                        break;
                    }
                }
                if (found)
                {
                    continue;
                }
                else
                {
                    this.SetOffspring(i, newOffspring);
                    this.SingleSimulation(i, geneticDistance, mapData);
                    if (this.inheritPosition[i, 0] == 0 && this.inheritPosition[i, 1] == 0)
                    {
                        i--;
                    }
                }
            }
            this.SetSimulatedData(individualList, blockDictionary, mapData, caseLinkageData);
//Console.ReadKey();
        }
Пример #7
0
        private void SetSimulatedData(ArrayList individualList, BlockDictionary blockDictionary, MapData mapData, double[,] caseLinkageData) // haplotype the ancestor's genotype data, substitude one haplotype for each simulated offspring.
        {
            int[] ancenstorHaplotype1 = new int[((Individual)individualList[this.ancenstor]).genotype.Length];

            int[]        ancenstorHaplotype2 = new int[((Individual)individualList[this.ancenstor]).genotype.Length];
            FileStream   cFile = new FileStream(@"simulation.txt", FileMode.Create);
            StreamWriter sw1   = new StreamWriter(cFile);

            for (int simulationIndex = 0; simulationIndex < this.offsprings.Length + 1; simulationIndex++) //haplotyping for the ancenstor and the offspings.
            {
                int haplotypingIndex;
                if (simulationIndex == 0)
                {
                    haplotypingIndex = this.ancenstor;
                }
                else
                {
                    haplotypingIndex = this.offsprings[simulationIndex - 1];
                }
                ((Individual)individualList[haplotypingIndex]).haplotype = new ArrayList[blockDictionary.blockList.Count];
                int blockID = -1;
                foreach (Block newBlock in blockDictionary.blockList)
                {
                    blockID++;
                    if (newBlock.GetEndSnpID() + newBlock.GetStartSnpID() == 0)
                    {
                        continue;
                    }
                    int[] blockGenotype = new int[newBlock.GetEndSnpID() - newBlock.GetStartSnpID() + 1];
                    for (int snpID = newBlock.GetStartSnpID(); snpID <= newBlock.GetEndSnpID(); snpID++)
                    {
                        blockGenotype[snpID - newBlock.GetStartSnpID()] = ((Individual)individualList[haplotypingIndex]).genotype[snpID];
                    }
                    ArrayList newHaplotypePairList = newBlock.GenotypeToHaplotype(blockGenotype, caseLinkageData);
                    ((Individual)individualList[haplotypingIndex]).haplotype[blockID] = newHaplotypePairList;
                }
                //Haplotyping for the ancenstor.
                int[] haplotype1 = new int[((Individual)individualList[haplotypingIndex]).genotype.Length];
                int[] haplotype2 = new int[((Individual)individualList[haplotypingIndex]).genotype.Length];

                blockID = 0;
                Random randomseed = new Random();
                for (int snpIndex = 0; snpIndex < ((Individual)individualList[haplotypingIndex]).genotype.Length; snpIndex++)
                {
                    if (blockID >= blockDictionary.blockList.Count || mapData.physicalDistance[snpIndex] < ((Block)blockDictionary.blockList[blockID]).GetStart()) // this snp is not in the current block.
                    {
                        if (snpIndex == 0)                                                                                                                         // start of the simulated region.
                        {
                            haplotype1[snpIndex] = ((Individual)individualList[haplotypingIndex]).genotype[snpIndex] / 2;
                            haplotype1[snpIndex] = (((Individual)individualList[haplotypingIndex]).genotype[snpIndex] + 1) / 2;
                        }
                        else
                        {
                            if (((Individual)individualList[haplotypingIndex]).genotype[snpIndex] == 0) //two major
                            {
                                haplotype1[snpIndex] = 0;
                                haplotype2[snpIndex] = 0;
                            }
                            else if (((Individual)individualList[haplotypingIndex]).genotype[snpIndex] == 2) //two minor
                            {
                                haplotype1[snpIndex] = 1;
                                haplotype2[snpIndex] = 1;
                            }
                            else // major + minor combination at that snp.
                            {
                                if (haplotype1[snpIndex - 1] == haplotype2[snpIndex - 1]) //if the last snp is not the major+minor combination
                                {
                                    if (randomseed.NextDouble() <= 0.5)
                                    {
                                        haplotype1[snpIndex] = 0;
                                        haplotype2[snpIndex] = 1;
                                    }
                                    else
                                    {
                                        haplotype1[snpIndex] = 1;
                                        haplotype2[snpIndex] = 0;
                                    }
                                }
                                else
                                {
                                    if (randomseed.NextDouble() <= caseLinkageData[snpIndex - 1, 6])
                                    {
                                        haplotype1[snpIndex] = haplotype1[snpIndex - 1];
                                        haplotype2[snpIndex] = haplotype2[snpIndex - 1];
                                    }
                                    else
                                    {
                                        haplotype1[snpIndex] = haplotype2[snpIndex - 1];
                                        haplotype2[snpIndex] = haplotype1[snpIndex - 1];
                                    }
                                }
                            }
                        }
                    }
                    else //the snp is in or after the current block
                    {
                        if (mapData.physicalDistance[snpIndex] > ((Block)blockDictionary.blockList[blockID]).GetEnd()) //after the current block.
                        {
                            blockID++;
                            snpIndex--;
                        }
                        else //in the current block
                        {
                            // the two haplotype in the block is with the same start allele or the last snp is not major+minor.
                            if (((HaplotypePair)((Individual)individualList[haplotypingIndex]).haplotype[blockID][0]).secondHaplotypeID == -1)
                            {
                                for (snpIndex = ((Block)blockDictionary.blockList[blockID]).GetStartSnpID(); snpIndex <= ((Block)blockDictionary.blockList[blockID]).GetEndSnpID(); snpIndex++)
                                {
                                    haplotype1[snpIndex] = ((Block)blockDictionary.blockList[blockID]).Gethaplotype(((HaplotypePair)((Individual)individualList[haplotypingIndex]).haplotype[blockID][0]).firstHaplotypeID, snpIndex - ((Block)blockDictionary.blockList[blockID]).GetStartSnpID());
                                    haplotype2[snpIndex] = ((Individual)individualList[haplotypingIndex]).genotype[snpIndex] - haplotype1[snpIndex];
                                }
                                snpIndex--;
                            }
                            else
                            {
                                if (snpIndex == 0 || ((Individual)individualList[haplotypingIndex]).genotype[snpIndex] != 1 || ((Individual)individualList[haplotypingIndex]).genotype[snpIndex - 1] != 1)// the two haplotype in the block is with the same start allele or the last snp is not major+minor.
                                {
                                    if (randomseed.NextDouble() <= 0.5)
                                    {
                                        for (snpIndex = ((Block)blockDictionary.blockList[blockID]).GetStartSnpID(); snpIndex <= ((Block)blockDictionary.blockList[blockID]).GetEndSnpID(); snpIndex++)
                                        {
                                            haplotype1[snpIndex] = ((Block)blockDictionary.blockList[blockID]).Gethaplotype(((HaplotypePair)((Individual)individualList[haplotypingIndex]).haplotype[blockID][0]).firstHaplotypeID, snpIndex - ((Block)blockDictionary.blockList[blockID]).GetStartSnpID());
                                            haplotype2[snpIndex] = ((Individual)individualList[haplotypingIndex]).genotype[snpIndex] - haplotype1[snpIndex];
                                        }
                                    }
                                    else
                                    {
                                        for (snpIndex = ((Block)blockDictionary.blockList[blockID]).GetStartSnpID(); snpIndex <= ((Block)blockDictionary.blockList[blockID]).GetEndSnpID(); snpIndex++)
                                        {
                                            haplotype2[snpIndex] = ((Block)blockDictionary.blockList[blockID]).Gethaplotype(((HaplotypePair)((Individual)individualList[haplotypingIndex]).haplotype[blockID][0]).firstHaplotypeID, snpIndex - ((Block)blockDictionary.blockList[blockID]).GetStartSnpID());
                                            haplotype1[snpIndex] = ((Individual)individualList[haplotypingIndex]).genotype[snpIndex] - haplotype2[snpIndex];
                                        }
                                    }
                                    snpIndex--;
                                }
                                else // 1 + 1 major+minor before major + minor in block.
                                {
                                    if (randomseed.NextDouble() <= caseLinkageData[snpIndex - 1, 6]) //AaBb -> AB+ab
                                    {
                                        if (haplotype1[snpIndex - 1] == ((Block)blockDictionary.blockList[blockID]).Gethaplotype(((HaplotypePair)((Individual)individualList[haplotypingIndex]).haplotype[blockID][0]).firstHaplotypeID, 0))
                                        {
                                            for (snpIndex = ((Block)blockDictionary.blockList[blockID]).GetStartSnpID(); snpIndex <= ((Block)blockDictionary.blockList[blockID]).GetEndSnpID(); snpIndex++)
                                            {
                                                haplotype1[snpIndex] = ((Block)blockDictionary.blockList[blockID]).Gethaplotype(((HaplotypePair)((Individual)individualList[haplotypingIndex]).haplotype[blockID][0]).firstHaplotypeID, snpIndex - ((Block)blockDictionary.blockList[blockID]).GetStartSnpID());
                                                haplotype2[snpIndex] = ((Individual)individualList[haplotypingIndex]).genotype[snpIndex] - haplotype1[snpIndex];
                                            }
                                            snpIndex--;
                                        }
                                        else
                                        {
                                            for (snpIndex = ((Block)blockDictionary.blockList[blockID]).GetStartSnpID(); snpIndex <= ((Block)blockDictionary.blockList[blockID]).GetEndSnpID(); snpIndex++)
                                            {
                                                haplotype2[snpIndex] = ((Block)blockDictionary.blockList[blockID]).Gethaplotype(((HaplotypePair)((Individual)individualList[haplotypingIndex]).haplotype[blockID][0]).firstHaplotypeID, snpIndex - ((Block)blockDictionary.blockList[blockID]).GetStartSnpID());
                                                haplotype1[snpIndex] = ((Individual)individualList[haplotypingIndex]).genotype[snpIndex] - haplotype2[snpIndex];
                                            }
                                            snpIndex--;
                                        }
                                    }
                                    else  //AaBb -> Ab+aB
                                    {
                                        if (haplotype1[snpIndex - 1] == ((Block)blockDictionary.blockList[blockID]).Gethaplotype(((HaplotypePair)((Individual)individualList[haplotypingIndex]).haplotype[blockID][0]).firstHaplotypeID, 0))
                                        {
                                            for (snpIndex = ((Block)blockDictionary.blockList[blockID]).GetStartSnpID(); snpIndex <= ((Block)blockDictionary.blockList[blockID]).GetEndSnpID(); snpIndex++)
                                            {
                                                haplotype2[snpIndex] = ((Block)blockDictionary.blockList[blockID]).Gethaplotype(((HaplotypePair)((Individual)individualList[haplotypingIndex]).haplotype[blockID][0]).firstHaplotypeID, snpIndex - ((Block)blockDictionary.blockList[blockID]).GetStartSnpID());
                                                haplotype1[snpIndex] = ((Individual)individualList[haplotypingIndex]).genotype[snpIndex] - haplotype2[snpIndex];
                                            }
                                            snpIndex--;
                                        }
                                        else
                                        {
                                            for (snpIndex = ((Block)blockDictionary.blockList[blockID]).GetStartSnpID(); snpIndex <= ((Block)blockDictionary.blockList[blockID]).GetEndSnpID(); snpIndex++)
                                            {
                                                haplotype1[snpIndex] = ((Block)blockDictionary.blockList[blockID]).Gethaplotype(((HaplotypePair)((Individual)individualList[haplotypingIndex]).haplotype[blockID][0]).firstHaplotypeID, snpIndex - ((Block)blockDictionary.blockList[blockID]).GetStartSnpID());
                                                haplotype2[snpIndex] = ((Individual)individualList[haplotypingIndex]).genotype[snpIndex] - haplotype1[snpIndex];
                                            }
                                            snpIndex--;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (simulationIndex == 0)
                {
                    ancenstorHaplotype1 = (int[])haplotype1.Clone();
                    ancenstorHaplotype2 = (int[])haplotype2.Clone();
                }
                else
                {
                    for (int m = this.inheritPosition[simulationIndex - 1, 0]; m <= this.inheritPosition[simulationIndex - 1, 1]; m++)
                    {
                        haplotype2[m] = ancenstorHaplotype1[m];
                        ((Individual)individualList[haplotypingIndex]).genotype[m] = haplotype1[m] + haplotype2[m];
                    }
                }

                if (simulationIndex == 0)
                {
                    sw1.Write("{0}\t{1} to {2}\t", haplotypingIndex, 0, 3569);
                }

                else
                {
                    sw1.Write("{0}\t{1} to {2}\t", haplotypingIndex, this.inheritPosition[simulationIndex - 1, 0], this.inheritPosition[simulationIndex - 1, 1]);
                }

                for (int i = 0; i < ((Individual)individualList[haplotypingIndex]).genotype.Length; i++)
                {
                    if (simulationIndex == 0 || i >= this.inheritPosition[simulationIndex - 1, 0] && i <= this.inheritPosition[simulationIndex - 1, 1])
                    {
                        sw1.Write("{0}", ((Individual)individualList[haplotypingIndex]).genotype[i]);
                    }
                    else
                    {
                        sw1.Write("-");
                    }
                }
                sw1.WriteLine();
                sw1.WriteLine();
            }
            sw1.Close();
        }
Пример #8
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);
        }
Пример #9
0
        public void Evalation(GeneticDistance newGeneticDistance, MapData newMapData, BlockDictionary newBlockDictionary, Commands newCommands)
        {
            ResultList finalResultList = new ResultList();

            finalResultList.SetpTable();
            CaseControlEvaluation(newGeneticDistance, newMapData, newBlockDictionary, newCommands, finalResultList);
        }
Пример #10
0
        public void findGroupIBD(ArrayList newIndividualList, BlockDictionary newBlockDictionary, double[,] linkageData, List <GroupShare> result, List <GroupShare> resultList, int windowStart, int windowEnd)
        {
            ArrayList[] IBDTable = new ArrayList[newBlockDictionary.blockList.Count];
            for (int regionIndex = 0; regionIndex < pairwiseRegionList.Count; regionIndex++)//(PairwiseIBDRegion newRegion in this.pairwiseRegionList)
            {
                PairwiseIBDRegion newRegion = (PairwiseIBDRegion)(pairwiseRegionList[regionIndex]);
                for (int blockID = newRegion.GetFirstBlockID(); blockID <= newRegion.GetLastBlockID(); blockID++)
                {
                    foreach (int haplotypeID in newRegion.GetHaplotype(blockID - newRegion.GetFirstBlockID()))
                    {
                        if (IBDTable[blockID] == null)
                        {
                            int[] newPair = new int[2];
                            newPair[0] = newRegion.GetFirstIndividual();
                            newPair[1] = newRegion.GetSecondIndividual();
                            HaplotypeSharing newHaplotypeShare = new HaplotypeSharing(haplotypeID, newPair);
                            IBDTable[blockID] = new ArrayList();
                            IBDTable[blockID].Add(newHaplotypeShare);
                        }
                        else
                        {
                            bool added = false;
                            for (int newHaplotypeShare = 0; newHaplotypeShare < IBDTable[blockID].Count; newHaplotypeShare++)
                            {
                                if (((HaplotypeSharing)IBDTable[blockID][newHaplotypeShare]).haplotpeID == haplotypeID)
                                {
                                    int[] newPair = new int[2];
                                    newPair[0] = newRegion.GetFirstIndividual();
                                    newPair[1] = newRegion.GetSecondIndividual();
                                    ((HaplotypeSharing)IBDTable[blockID][newHaplotypeShare]).individualList.Add(newPair);
                                    added = true;
                                }
                            }
                            if (!added)
                            {
                                int[] newPair = new int[2];
                                newPair[0] = newRegion.GetFirstIndividual();
                                newPair[1] = newRegion.GetSecondIndividual();
                                HaplotypeSharing newHaplotypeShare = new HaplotypeSharing(haplotypeID, newPair);
                                IBDTable[blockID].Add(newHaplotypeShare);
                            }
                        }
                    }
                }
                pairwiseRegionList[regionIndex] = null;
            }

            pairwiseRegionList = null;
            GC.Collect();
            MyResultComparer newResultComparer = new MyResultComparer();

            if (IBDTable[IBDTable.GetUpperBound(0)] == null)
            {
                IBDTable[IBDTable.GetUpperBound(0)] = new ArrayList();
            }
            for (int blockIndex = windowStart; blockIndex < windowEnd; blockIndex++)
            {
                ArrayList newlyComingSharingList = new ArrayList();
                if (IBDTable[blockIndex] == null)
                {
                    foreach (GroupShare newGroupShare in resultList)
                    {
                        result.Add(newGroupShare);
                    }
                    resultList           = new List <GroupShare>();
                    IBDTable[blockIndex] = new ArrayList();
                    continue;
                }
                foreach (HaplotypeSharing newHaplotypeSharing in IBDTable[blockIndex])
                {
                    if (newHaplotypeSharing.individualList.Count > 2 && newHaplotypeSharing.individualList.Count < 0.2 * newIndividualList.Count) //at least 2 pairs sharing this haplotype.
                    {
                        GroupShare newGroupShare = new GroupShare();
                        newGroupShare.sharePairList         = (ArrayList)newHaplotypeSharing.individualList.Clone();
                        newGroupShare.allSharedFirstBlockID = blockIndex;
                        newGroupShare.allSharedLastBlockID  = blockIndex;
                        newGroupShare.haplotypeList         = new ArrayList();
                        newGroupShare.haplotypeList.Add(newHaplotypeSharing.haplotpeID);
                        newGroupShare.SetIndividualList();
                        newlyComingSharingList.Add(newGroupShare);
                    }
                }
                ArrayList newlyAddedList = new ArrayList();


                //for the next block, if no haplotype contains more than 1 pair, stop the extension and record all the region waiting to be extended.
                if (newlyComingSharingList.Count == 0)
                {
                    foreach (GroupShare newGroup in resultList)
                    {
                        result.Add(newGroup);
                    }
                }

                for (int i = 0; i < resultList.Count; i++)
                {
                    GroupShare newGroupshare1 = (GroupShare)(resultList[i]);
                    if (newGroupshare1.GroupShareExtention(IBDTable, newIndividualList, newBlockDictionary, newlyComingSharingList, result, newlyAddedList) == false)
                    {
                        resultList.RemoveAt(i);
                        i--;
                    }
                }
                foreach (GroupShare newGroupShare in newlyAddedList)
                {
                    bool found = false;
                    foreach (GroupShare checkingGroup in resultList)
                    {
                        if (checkingGroup.ChekcSubGroup(newGroupShare) == true)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found == false)
                    {
                        resultList.Add(newGroupShare);
                    }
                }
                foreach (GroupShare newGroupShare in newlyComingSharingList)
                {
                    bool found = false;
                    foreach (GroupShare checkingGroup in resultList)
                    {
                        if (checkingGroup.ChekcSubGroup(newGroupShare) == true)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found == false)
                    {
                        resultList.Add(newGroupShare);
                    }
                }

                resultList.Sort(newResultComparer);
            }


            foreach (GroupShare newShare in result)
            {
                newShare.SetIndividualList();
            }


            result.Sort(newResultComparer);

            for (int i = 0; i < result.Count - 1; i++)
            {
                if (((GroupShare)(result[i])).CheckContain((GroupShare)(result[i + 1])) == true)
                {
                    result.RemoveAt(i + 1);
                    i--;
                }
                else if (((GroupShare)(result[i + 1])).CheckContain(((GroupShare)(result[i]))) == true)
                {
                    result.RemoveAt(i);
                    i--;
                }
            }


            for (int i = 0; i < result.Count - 1; i++)
            {
                if (((GroupShare)(result[i])).CheckOverlap((GroupShare)(result[i + 1])))
                {
                    result.RemoveAt(i + 1);
                    i--;
                }
            }

            foreach (GroupShare newShare in result)
            {
                newShare.SetIndividualList();
            }

            for (int i = 0; i < result.Count; i++)
            {
                ((GroupShare)(result[i])).SetPartiallySharedRegion(IBDTable, newBlockDictionary, newIndividualList, windowStart, windowEnd);
                Console.SetCursorPosition(16, Console.CursorTop);
                Console.Write("{0} / {1}", i + 1, result.Count);
            }

            for (int i = 0; i < result.Count; i++)
            {
                ((GroupShare)(result[i])).SetAllSharedSnpHaplotype(newIndividualList, newBlockDictionary);
            }
            for (int i = 0; i < result.Count; i++)
            {
                ((GroupShare)(result[i])).SetPartiallySharedSnpHaplotype(newIndividualList, newBlockDictionary);
            }

            for (int i = 0; i < result.Count; i++)
            {
                ((GroupShare)(result[i])).SetAValue(newBlockDictionary, newIndividualList, linkageData);
            }
            Console.WriteLine("\t\t\t\t\tFinished.");
            result.Sort(newResultComparer);

            for (int i = 0; i < result.Count - 1; i++)
            {
                if (((GroupShare)(result[i])).CheckContain((GroupShare)(result[i + 1])))
                {
                    result.RemoveAt(i + 1);
                    i--;
                }
            }
            for (int i = 0; i < result.Count - 1; i++)
            {
                if (((GroupShare)(result[i])).CheckOverlap((GroupShare)(result[i + 1])))
                {
                    result.RemoveAt(i + 1);
                    i--;
                }
            }
        }