public double GetRandomP(SnpDataSet dataSet) { double minusLogRandonP = 0; for (int i = 0; i < this.GetHaplotype().Length; i++) { if (this.GetHaplotype(i) == 2) { minusLogRandonP += -Math.Log10(dataSet.GetMAF(i + this.GetStart())); } else { minusLogRandonP += -Math.Log10((double)1 - dataSet.GetMAF(i + this.GetStart())); } } return(minusLogRandonP); }
public double GetRandomModelLikelihood(SnpDataSet dataSet) { double minorLikelihood = 0; double majorLikelihood = 0; for (int i = 0; i < this.GetSequence().Length; i++) { if (this.GetHaplotype(i) == 0) { majorLikelihood += (double)1 / (1 - dataSet.GetMAF(i + this.GetStart())); } else { minorLikelihood += (double)1 / dataSet.GetMAF(i + this.GetStart()); } } return((double)Math.Abs(majorLikelihood / (majorLikelihood + minorLikelihood))); }
public void Coverage(ArrayList rareBlockList, SnpDataSet dataSet) { int[] coverage = new int[dataSet.GetSnpCount()]; int total = 0; foreach (RareBlock rareBlock in rareBlockList) { for (int i = rareBlock.start; i <= rareBlock.end; i++) { coverage[i] = 1; } } foreach (int i in coverage) { if (i == 1) { total++; } } Console.WriteLine(total); }
public void inWindowExtend(ArrayList group, int snpPosition, SnpDataSet dataSet, ArrayList shareWindowListRef) { if (snpPosition == this.end) //when it reached the end of the window { ShareWindow newShareWindow = new ShareWindow(); //construct a shareWindow object newShareWindow.windowStart = this; newShareWindow.windowLast = this; newShareWindow.group = group; group.Sort(); shareWindowListRef.Add(newShareWindow); // Add newShareWindow to shareWindow List; return; } ArrayList group0 = new ArrayList(); //store the individuals that the data of this snp is 0 ArrayList group1 = new ArrayList(); //store the individuals that the data of this snp is 1 ArrayList group2 = new ArrayList(); //store the individuals that the data of this snp is 2 foreach (int individual in group) { if (dataSet.GetSnp(individual, snpPosition) == 0) // the data is 0 { group0.Add(individual); } else if (dataSet.GetSnp(individual, snpPosition) == 1) //the data is 1 { group1.Add(individual); } else //the data is 2 { group2.Add(individual); } } if (group2.Count + group1.Count >= 3) // construct a new group if group2+group1>=3 { ArrayList group12 = new ArrayList(); foreach (int ind in group2) //add group2 and group1 into group21 { group12.Add(ind); } foreach (int ind in group1) { group12.Add(ind); } //group12.ToArray(); inWindowExtend(group12, snpPosition + 1, dataSet, shareWindowListRef); //use group21 to do the next Extend } if (group0.Count + group1.Count >= 3) // construct a new group if group0+group1>=3, similar to last. { ArrayList group01 = new ArrayList(); foreach (int ind in group0) { group01.Add(ind); } foreach (int ind in group1) { group01.Add(ind); } //group01.ToArray(); inWindowExtend(group01, snpPosition + 1, dataSet, shareWindowListRef); //do the next Extend } }
public void Combine(int i, ArrayList rareBlockList, ArrayList combinedBlockList, SnpDataSet dataSet) { if (i == rareBlockList.Count - 1) { return; //reach the end of the rareBlockList. } ArrayList newGroup = new ArrayList(); ArrayList remainGroup = new ArrayList(); bool flag = true; for (int j = i + 1; j < i + 50 && j < rareBlockList.Count; j++) // find combined block from this block to 100 blocks away. { RareBlock block = new RareBlock(); //Console.WriteLine("{0} {1}", i, j); block = (RareBlock)((RareBlock)rareBlockList[j]).MemberwiseClone(); if (block.GetStart() < this.GetEnd()) { continue; } int m = 0; int n = 0; newGroup.Clear(); remainGroup.Clear(); for (m = 0; m < this.GetGroup().Length; m++) //find the intersection of the two group of individuals. { if (n == block.GetGroup().Length) { remainGroup.Add(this.GetGroup(m)); break; } if (this.group[m] == block.GetGroup(n)) { newGroup.Add(this.group[m]); n++; } else if (this.group[m] > block.GetGroup(n)) { n++; m--; } else { remainGroup.Add(this.GetGroup(m)); } } if (newGroup.Count > 2) //more than 1 guys appare in the two rare blocks { //Console.Write("{0} {1} {2}\t", i, j, newGroup.Count); //foreach (int ind in newGroup) // Console.Write("{0} ",ind); //Console.WriteLine(); //Console.ReadKey(); RareBlock newRareBlock = new RareBlock(); newRareBlock.SetStart(this.GetStart()); newRareBlock.SetEnd(block.GetEnd()); newRareBlock.SetGroup((int[])newGroup.ToArray(typeof(int))); if (newRareBlock.CheckCommon(dataSet)) //check the snp data between the rare blocks //if (true) { if (newRareBlock.GetGroup().Length == 3) { newRareBlock.Extend(dataSet); if (newRareBlock.GetEnd() - newRareBlock.GetStart() > 170) { if (combinedBlockList.Count > 0) { RareBlock lastRareBlock = (RareBlock)((RareBlock)combinedBlockList[combinedBlockList.Count - 1]).MemberwiseClone(); if (!lastRareBlock.CheckSame(this)) { if (this.CheckContain(lastRareBlock)) { combinedBlockList[combinedBlockList.Count - 1] = this.GetCopy(); } else { combinedBlockList.Add(this.GetCopy()); } } } else { combinedBlockList.Add(this.GetCopy()); } } } else { newRareBlock.Combine(j, rareBlockList, combinedBlockList, dataSet);//if pass, continue combining. } //break; //When we find another block to combine to this block ,stop searching for this block. //Time saving a lot, can miss some, discuss later on. if (this.group.Length - newRareBlock.group.Length == 0) { flag = false; break; } //else if (newRareBlock.group.Length / (this.group.Length - newRareBlock.group.Length) >= 3) break; } /* * if (this.group.Length - newRareBlock.group.Length >= 3) * { * RareBlock remainGroupBlock = new RareBlock(); * remainGroupBlock.start = this.start; * remainGroupBlock.end = this.end; * remainGroupBlock.sequence = this.sequence; * remainGroupBlock.group = (int[])remainGroup.ToArray(typeof(int)); * remainGroupBlock.Combine(j, rareBlockList, combinedBlockList, dataSet); * } */ } } if (flag && (this.GetEnd() - this.GetEnd()) > 70) { if (combinedBlockList.Count > 0) { RareBlock lastRareBlock = (RareBlock)combinedBlockList[combinedBlockList.Count - 1]; if (!lastRareBlock.CheckSame(this)) { if (this.CheckContain(lastRareBlock)) { combinedBlockList[combinedBlockList.Count - 1] = this.GetCopy(); } else { combinedBlockList.Add(this.GetCopy()); } } } else { combinedBlockList.Add(this.GetCopy()); } } }
public void Extend(SnpDataSet dataSet) { int newStart = this.GetStart(); int newEnd = this.GetEnd(); ArrayList newEndingSequence = new ArrayList(); while (newEnd < dataSet.GetSnpCount() - 1) { int data = -1; bool flag = false; for (int i = 1; i < this.group.Length; i++) { if (data == -1) { if (dataSet.GetSnp(this.group[i], newEnd + 1) != 1) { data = dataSet.GetSnp(this.group[i], newEnd + 1); } } else if (Math.Abs(dataSet.GetSnp(this.group[i], newEnd + 1) - data) == 2) { flag = true; break; } } if (flag) { break; } if (data == -1) { newEndingSequence.Add(1); } else if (data == 0) { newEndingSequence.Add(0); } else { newEndingSequence.Add(2); } newEnd++; } ArrayList newStartingSequence = new ArrayList(); while (newStart > 0) { int data = -1; bool flag = false; for (int i = 1; i < this.GetGroup().Length; i++) { if (data == -1) { if (dataSet.GetSnp(this.group[i], newStart - 1) != 1) { data = dataSet.GetSnp(this.group[i], newStart - 1); } } else if (Math.Abs(dataSet.GetSnp(this.group[i], newStart - 1) - data) == 2) { flag = true; break; } } if (flag) { break; } if (data == -1) { newStartingSequence.Add(1); } else if (data == 0) { newStartingSequence.Add(0); } else { newStartingSequence.Add(2); } newStart--; } newStartingSequence.Reverse(); int[] newWholeSequence = new int[this.sequence.Length + newStartingSequence.Count + newEndingSequence.Count]; newStartingSequence.CopyTo(newWholeSequence); this.GetSequence().CopyTo(newWholeSequence, newStartingSequence.Count); newEndingSequence.CopyTo(newWholeSequence, newStartingSequence.Count + this.sequence.Length); this.SetSequence(newWholeSequence); this.SetStart(newStart); this.SetEnd(newEnd); if (this.GetEnd() - this.GetEnd() > 170) { ; } while (newEnd < dataSet.GetSnpCount()) { ArrayList group0 = new ArrayList(); ArrayList group1 = new ArrayList(); ArrayList group2 = new ArrayList(); for (int i = 0; i < this.GetGroup().Length; i++) { if (this.GetGroup(i) == 0) { group0.Add(this.GetGroup(i)); } else if (this.GetGroup(i) == 2) { group2.Add(this.GetGroup(i)); } else { group1.Add(this.GetGroup(i)); } } } }
private bool CheckCommon(SnpDataSet dataSet) //check snp data between rare blocks { int[] error = new int[this.group.Length]; //error data. this.sequence = new int[this.end - this.start + 1]; for (int e = 0; e < error.Length; e++) // all is 0 at the begining. { error[e] = 0; } for (int i = start; i < end; i++) //loop from start to end for snp { int count0 = 0; //count how many 0 int count2 = 0; //count how many 2 int[] dataTmp = new int[this.group.Length]; //store the data, avoid multi data accessing //for (int d = 0; d < dataTmp.Length; d++) // // d = 0; int j = 0; foreach (int ind in this.group) // for each guy in group ,check this snp data { int data = dataSet.GetSnp(ind, i); if (data == 2) { count2++; } else if (data == 0) { count0++; } dataTmp[j] = data; //store the data. j++; } if (count2 <= count0) //two minor allele is the outlier in the group { for (int m = 0; m < this.group.Length; m++) { if (dataTmp[m] == 2) { error[m]++; } } sequence[i - start] = 0; } else //two major allele is the outlier in the group { for (int m = 0; m < this.group.Length; m++) { if (dataTmp[m] == 0) { error[m]++; } } sequence[i - start] = 2; } if (count2 == 0 && count0 == 0) { sequence[i - start] = 1; } } ArrayList newGroup = new ArrayList(); for (int n = 0; n < this.group.Length; n++) { if (!(error[n] > 0)) //no error or little error, keep this guy. { newGroup.Add(this.group[n]); } } if (newGroup.Count < 3) { return(false); } else { this.group = (int[])newGroup.ToArray(typeof(int)); return(true); } }
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 data(SnpDataSet caseDataSet, SnpDataSet controlDataSet, Commands newCommands) { string strLine; string[] strArray; char[] splitArray = new char[] { ' ' }; string[] individualName; ArrayList collectionArray; try { populationSize = 0; collectionArray = new ArrayList(); FileStream aFile = new FileStream(newCommands.pedName, FileMode.OpenOrCreate); StreamReader sr = new StreamReader(aFile); strLine = sr.ReadLine(); strArray = strLine.Split(splitArray); Tem = strArray.Length; while (strLine != null) { populationSize++; strArray = strLine.Split(splitArray); if (strArray[5] == "2") { caseSize++; } else { controlSize++; } collectionArray.Add(strLine); strLine = sr.ReadLine(); } sr.Close(); SetAlleleMap(newCommands); individualName = new string[populationSize]; finalData = new String[populationSize, Tem - 5]; for (int u = 0; u < collectionArray.Count; u++) { String R = (String)collectionArray[u]; strArray = R.Split(splitArray); individualName[u] = strArray[1]; for (int s = 0; s < Tem - 5; s++) { finalData[u, s] = strArray[s + 5]; } } } catch (IOException e) { Console.WriteLine("An IO exception has been thrown!"); Console.WriteLine(e.ToString()); Console.ReadLine(); return; } int[,] TransTab = new int[populationSize, Tem - 5]; rareallel = new int[populationSize, ((Tem - 6) / 2 + 1)]; for (int x = 0; x < populationSize; x++) { rareallel[x, 0] = (int)finalData[x, 0][0] - 48; } for (int m = 1; m < Tem - 6; m = m + 2) { for (int x = 0; x < populationSize; x++) { if (finalData[x, m][0] == Convert.ToChar(this.alleleMap[(m - 1) / 2, 0])) { TransTab[x, m] = 1; } else { TransTab[x, m] = 0; } if (finalData[x, m + 1][0] == Convert.ToChar(this.alleleMap[(m - 1) / 2, 0])) { TransTab[x, m + 1] = 1; } else { TransTab[x, m + 1] = 0; } if (finalData[x, m][0] == Convert.ToChar("0")) { TransTab[x, m] = 1; TransTab[x, m + 1] = 0; } rareallel[x, (m + 1) / 2] = TransTab[x, m] + TransTab[x, m + 1]; } } controlData = new int[controlSize, (Tem - 6) / 2]; caseData = new int[caseSize, (Tem - 6) / 2]; ArrayList caseIndividualName = new ArrayList(); ArrayList controlIndividualName = new ArrayList(); ArrayList caseIndividualId = new ArrayList(); ArrayList controlIndividualId = new ArrayList(); int casePtr = 0; int controlPtr = 0; for (int u = 0; u < populationSize; u++) { if (rareallel[u, 0] == 1) { controlIndividualName.Add(individualName[u]); controlIndividualId.Add(u); String S = (String)collectionArray[u]; for (int m = 1; m <= (Tem - 6) / 2; m++) { controlData[controlPtr, m - 1] = rareallel[u, m]; } controlPtr++; } else if (rareallel[u, 0] == 2) { caseIndividualName.Add(individualName[u]); caseIndividualId.Add(u); String S = (String)collectionArray[u]; for (int m = 1; m <= (Tem - 6) / 2; m++) { caseData[casePtr, m - 1] = rareallel[u, m]; } casePtr++; } } caseDataSet.SetSnp(caseData); caseDataSet.SetIndividualName((string[])caseIndividualName.ToArray(typeof(string))); caseDataSet.SetIndividual((int[])caseIndividualId.ToArray(typeof(int))); caseDataSet.SetSnpCount((Tem - 6) / 2); controlDataSet.SetSnp(controlData); controlDataSet.SetIndividualName((string[])controlIndividualName.ToArray(typeof(string))); controlDataSet.SetIndividual((int[])controlIndividualId.ToArray(typeof(int))); controlDataSet.SetSnpCount((Tem - 6) / 2); }