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); }
public void PairwiseComparison(ArrayList pairwiseIBDRegionList, Individual thatIndividual, Block[] blockList, MapData newMapData, double lengthCut, int startBlockID, int endBlockID) { PairwiseIBDRegion newPairwiseIBDRegion = new PairwiseIBDRegion(this.GetIndividualID(), thatIndividual.GetIndividualID(), 0, 0, -1, -1); for (int blockID = startBlockID; blockID < endBlockID; blockID++) //foreach block, from first to end. { if (newPairwiseIBDRegion.GetLastBlockID() == -1) // last IBD region has just added or deleted, need to construc a new one. { newPairwiseIBDRegion.SetStart(blockList[blockID].GetStartSnpID()); newPairwiseIBDRegion.SetEnd(blockList[blockID].GetEndSnpID()); newPairwiseIBDRegion.SetLastBlockID(blockID); newPairwiseIBDRegion.SetFirstBlockID(blockID); newPairwiseIBDRegion.ClearHaplotype(); } ArrayList sharedHaplotypeID = new ArrayList(); ArrayList thisHaplotypeList = new ArrayList(); //store all the possible haplotype for this individual in this block. ArrayList thatHaplotypeList = new ArrayList(); //store all the possible haplotype for that individual in this block. bool thisUnknownHaplotype = false; bool thatUnknownHaplotype = false; for (int i = 0; i < this.haplotype[blockID].Count; i++) //store all the possible haplotype for this individual in this block. { if (((HaplotypePair)this.haplotype[blockID][i]).firstHaplotypeID != -1) { thisHaplotypeList.Add(((HaplotypePair)this.haplotype[blockID][i]).firstHaplotypeID); } else { thisUnknownHaplotype = true; break; } if (((HaplotypePair)this.haplotype[blockID][i]).secondHaplotypeID != -1 && ((HaplotypePair)this.haplotype[blockID][i]).secondHaplotypeID != ((HaplotypePair)this.haplotype[blockID][i]).firstHaplotypeID) { thisHaplotypeList.Add(((HaplotypePair)this.haplotype[blockID][i]).secondHaplotypeID); } else { if (((HaplotypePair)this.haplotype[blockID][i]).secondHaplotypeID == -1) { thisUnknownHaplotype = true; } break; } } for (int i = 0; i < thatIndividual.haplotype[blockID].Count; i++)//store all the possible haplotype for that individual in this block. { if (((HaplotypePair)thatIndividual.haplotype[blockID][i]).firstHaplotypeID != -1) { thatHaplotypeList.Add(((HaplotypePair)thatIndividual.haplotype[blockID][i]).firstHaplotypeID); } else { thatUnknownHaplotype = true; break; } if (((HaplotypePair)thatIndividual.haplotype[blockID][i]).secondHaplotypeID != -1 && ((HaplotypePair)thatIndividual.haplotype[blockID][i]).secondHaplotypeID != ((HaplotypePair)thatIndividual.haplotype[blockID][i]).firstHaplotypeID) { thatHaplotypeList.Add(((HaplotypePair)thatIndividual.haplotype[blockID][i]).secondHaplotypeID); } else { if (((HaplotypePair)thatIndividual.haplotype[blockID][i]).secondHaplotypeID == -1) { thatUnknownHaplotype = true; } break; } } if (thisUnknownHaplotype && thatUnknownHaplotype) //found unknown haplotype in both sample { for (int s = 0; s < blockList[blockID].GetHaplotypeCount(); s++) { sharedHaplotypeID.Add(s); } } else if (thisUnknownHaplotype && !thatUnknownHaplotype) // only in this sample { foreach (int newId in thatHaplotypeList) { sharedHaplotypeID.Add(newId); } } else if ((!thisUnknownHaplotype) && thatUnknownHaplotype) // only that sample { foreach (int newId in thisHaplotypeList) { sharedHaplotypeID.Add(newId); } } else // normal case ,non unknown { thisHaplotypeList.Sort(); thatHaplotypeList.Sort(); int thisHaplotypeIndex = 0; int thatHaplotypeIndex = 0; while (thisHaplotypeIndex < thisHaplotypeList.Count && thatHaplotypeIndex < thatHaplotypeList.Count) { // store all the shared haplotype id in the sharedHaplotypeID list. if ((int)thisHaplotypeList[thisHaplotypeIndex] == (int)thatHaplotypeList[thatHaplotypeIndex]) { sharedHaplotypeID.Add(thisHaplotypeList[thisHaplotypeIndex]); thisHaplotypeIndex++; thatHaplotypeIndex++; } else if ((int)thisHaplotypeList[thisHaplotypeIndex] < (int)thatHaplotypeList[thatHaplotypeIndex]) { thisHaplotypeIndex++; } else { thatHaplotypeIndex++; } } } bool shareRecombinationRegion = true; if (sharedHaplotypeID.Count == 0) // no shared haplotype found! { if (newMapData.GetGeneticDistance(newPairwiseIBDRegion.GetEnd()) - newMapData.GetGeneticDistance(newPairwiseIBDRegion.GetStart()) >= lengthCut / 10) // only add the pairwise IBD region whose length is >= 2cm { if (newPairwiseIBDRegion.GetLastBlockID() - newPairwiseIBDRegion.GetFirstBlockID() != 0) { if (pairwiseIBDRegionList.Count == 0 || !this.CombineAdjacentPairwiseRegion((PairwiseIBDRegion)pairwiseIBDRegionList[pairwiseIBDRegionList.Count - 1], newPairwiseIBDRegion, thatIndividual)) { if (newMapData.GetGeneticDistance(newPairwiseIBDRegion.GetEnd()) - newMapData.GetGeneticDistance(newPairwiseIBDRegion.GetStart()) >= lengthCut) { pairwiseIBDRegionList.Add(newPairwiseIBDRegion.GetCopy()); } } } } newPairwiseIBDRegion.SetLastBlockID(-1); continue; } else { newPairwiseIBDRegion.SetEnd(blockList[blockID].GetEndSnpID()); newPairwiseIBDRegion.AddHaplotype(sharedHaplotypeID); newPairwiseIBDRegion.SetLastBlockID(blockID); if (blockID == blockList.Length - 1) { if (newMapData.GetGeneticDistance(newPairwiseIBDRegion.GetEnd()) - newMapData.GetGeneticDistance(newPairwiseIBDRegion.GetStart()) >= lengthCut) { if (pairwiseIBDRegionList.Count == 0 || !this.CombineAdjacentPairwiseRegion((PairwiseIBDRegion)pairwiseIBDRegionList[pairwiseIBDRegionList.Count - 1], newPairwiseIBDRegion, thatIndividual)) { if (newMapData.GetGeneticDistance(newPairwiseIBDRegion.GetEnd()) - newMapData.GetGeneticDistance(newPairwiseIBDRegion.GetStart()) >= lengthCut) { pairwiseIBDRegionList.Add(newPairwiseIBDRegion.GetCopy()); } } } return; } else { for (int i = blockList[blockID].GetEndSnpID() + 1; i < blockList[blockID + 1].GetStartSnpID(); i++) { if (Math.Abs(this.genotype[i] - thatIndividual.genotype[i]) == 2) { if (newMapData.GetGeneticDistance(newPairwiseIBDRegion.GetEnd()) - newMapData.GetGeneticDistance(newPairwiseIBDRegion.GetStart()) >= lengthCut) // only add the pairwise IBD region whose length is >= 2cm { if (pairwiseIBDRegionList.Count == 0 || !this.CombineAdjacentPairwiseRegion((PairwiseIBDRegion)pairwiseIBDRegionList[pairwiseIBDRegionList.Count - 1], newPairwiseIBDRegion, thatIndividual)) { if (newMapData.GetGeneticDistance(newPairwiseIBDRegion.GetEnd()) - newMapData.GetGeneticDistance(newPairwiseIBDRegion.GetStart()) >= lengthCut) { pairwiseIBDRegionList.Add(newPairwiseIBDRegion.GetCopy()); } } } newPairwiseIBDRegion.SetLastBlockID(-1); newPairwiseIBDRegion.SetEnd(i - 1); break; } } } } } }