public bool CheckDuplication(GroupShare groupA, GroupShare groupB) //true for duplicated region. { if (groupA.individualList.Count != groupB.individualList.Count) { return(false); } for (int i = 0; i < groupA.individualList.Count; i++) { if (groupA.individualList[i] != groupB.individualList[i]) { return(false); } } if ((groupA.allSharedFirstBlockID >= groupB.allSharedFirstBlockID && groupA.allSharedFirstBlockID <= groupB.allSharedLastBlockID) || (groupA.allSharedLastBlockID >= groupB.allSharedFirstBlockID && groupA.allSharedLastBlockID <= groupB.allSharedLastBlockID)) //check the region is overlaped or not { return(true); } else { return(false); } }
public bool ChekcSubGroup(GroupShare subGroup) { //this.SetIndividualList(); //subGroup.SetIndividualList(); this.individualList.Sort(); subGroup.individualList.Sort(); if (this.individualList.Count < subGroup.individualList.Count) { return(false); } int thisIndex = 0; for (int subIndex = 0; subIndex < subGroup.individualList.Count; subIndex++) { while (thisIndex < this.individualList.Count) { if ((int)(this.individualList[thisIndex]) < (int)(subGroup.individualList[subIndex])) { thisIndex++; } else if ((int)(this.individualList[thisIndex]) == (int)(subGroup.individualList[subIndex])) { thisIndex++; break; } else { return(false); } if (thisIndex == individualList.Count) { return(false); } } } return(true); }
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); }
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--; } } }