/UnitTests/Phantom/PositionCollections/AlleleBlockTests.cs

https://github.com/Illumina/Nirvana
C# | 172 lines | 152 code | 20 blank | 0 comment | 0 complexity | 571c33bf719268d49c97169b1d0a60a1 MD5 | raw file
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Phantom.PositionCollections;
  4. using Xunit;
  5. namespace UnitTests.Phantom.PositionCollections
  6. {
  7. public sealed class AlleleBlockTests
  8. {
  9. [Fact]
  10. public void GetPloidyFromGenotypes_DotIsIgnored()
  11. {
  12. var genotypes = new[] { Genotype.GetGenotype("."), Genotype.GetGenotype("1|2"), Genotype.GetGenotype("0/2") };
  13. var ploidy = AlleleBlock.GetMaxPloidy(genotypes);
  14. Assert.Equal(2, ploidy);
  15. }
  16. [Fact]
  17. public void GetAlleleBlockToSampleHaplotype_AsExpected()
  18. {
  19. var genotypeBlock1 = new GenotypeBlock(new[] { "1|2", "1/1", "0|1", "./1" }.Select(Genotype.GetGenotype).ToArray());
  20. var genotypeBlock2 = new GenotypeBlock(new[] { "0/1", "0|0", "1|1", "1|1" }.Select(Genotype.GetGenotype).ToArray());
  21. var genotypeBlock3 = new GenotypeBlock(new[] { "0|1", "1|1", "0/0" }.Select(Genotype.GetGenotype).ToArray(), 1);
  22. var genotypeToSample =
  23. new Dictionary<GenotypeBlock, List<int>>
  24. {
  25. {genotypeBlock1, new List<int> {0, 1}},
  26. {genotypeBlock2, new List<int> {2}},
  27. {genotypeBlock3, new List<int> {3}}
  28. };
  29. var indexOfUnsupportedVars = Enumerable.Repeat(new HashSet<int>(), 4).ToArray();
  30. var starts = Enumerable.Range(100, 4).ToArray();
  31. var functionBlockRanges = starts.Select(x => x + 2).ToList();
  32. var alleleBlockToSampleHaplotype = AlleleBlock.GetAlleleBlockToSampleHaplotype(genotypeToSample,
  33. indexOfUnsupportedVars, starts, functionBlockRanges, out _);
  34. var expectedBlock1 = new AlleleBlock(0, new[] { 1, 1, 0 }, 0, 0);
  35. var expectedBlock2 = new AlleleBlock(0, new[] { 2, 1, 1 }, 0, 0);
  36. var expectedBlock3 = new AlleleBlock(2, new[] { 1, 1 }, 1, 0);
  37. var expectedBlock4 = new AlleleBlock(1, new[] { 1, 1 }, 0, 1);
  38. var expectedBlock5 = new AlleleBlock(1, new[] { 0, 1 }, 0, 1);
  39. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock1));
  40. Assert.True(alleleBlockToSampleHaplotype[expectedBlock1]
  41. .SequenceEqual(new[] { new SampleHaplotype(0, 0), new SampleHaplotype(1, 0) }));
  42. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock2));
  43. Assert.True(alleleBlockToSampleHaplotype[expectedBlock2]
  44. .SequenceEqual(new[] { new SampleHaplotype(0, 1), new SampleHaplotype(1, 1) }));
  45. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock3));
  46. Assert.True(alleleBlockToSampleHaplotype[expectedBlock3]
  47. .SequenceEqual(new[] { new SampleHaplotype(2, 0), new SampleHaplotype(2, 1) }));
  48. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock4));
  49. Assert.True(alleleBlockToSampleHaplotype[expectedBlock4].SequenceEqual(new[] { new SampleHaplotype(3, 1) }));
  50. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock5));
  51. Assert.True(alleleBlockToSampleHaplotype[expectedBlock5].SequenceEqual(new[] { new SampleHaplotype(3, 0) }));
  52. }
  53. [Fact]
  54. public void GetAlleleBlockToSampleHaplotype_AlleleBlock_WithInternalRefPositions_SplitIfOutOfRange()
  55. {
  56. var genotypeBlock1 = new GenotypeBlock(new[] { "1|2", "0/0", "0|0", "1/1" }.Select(Genotype.GetGenotype).ToArray());
  57. var genotypeBlock2 = new GenotypeBlock(new[] { "1/1", "0|0", "1|1" }.Select(Genotype.GetGenotype).ToArray());
  58. var genotypeBlock3 = new GenotypeBlock(new[] { "1|2", "0|0", "1|1" }.Select(Genotype.GetGenotype).ToArray(), 1);
  59. var genotypeToSample =
  60. new Dictionary<GenotypeBlock, List<int>>
  61. {
  62. {genotypeBlock1, new List<int> {0}},
  63. {genotypeBlock2, new List<int> {1}},
  64. {genotypeBlock3, new List<int> {2}}
  65. };
  66. var indexOfUnsupportedVars = Enumerable.Repeat(new HashSet<int>(), genotypeBlock1.Genotypes.Length).ToArray();
  67. var starts = new[] { 100, 102, 103, 104 };
  68. var functionBlockRanges = starts.Select(x => x + 2).ToList();
  69. var alleleBlockToSampleHaplotype = AlleleBlock.GetAlleleBlockToSampleHaplotype(genotypeToSample,
  70. indexOfUnsupportedVars, starts, functionBlockRanges, out _);
  71. var expectedBlock1 = new AlleleBlock(1, new[] { 1, 0, 1 }, 0, 0);
  72. var expectedBlock2 = new AlleleBlock(1, new[] { 2, 0, 1 }, 0, 0);
  73. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock1));
  74. Assert.True(alleleBlockToSampleHaplotype[expectedBlock1].SequenceEqual(new[] { new SampleHaplotype(2, 0) }));
  75. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock2));
  76. Assert.True(alleleBlockToSampleHaplotype[expectedBlock2].SequenceEqual(new[] { new SampleHaplotype(2, 1) }));
  77. }
  78. [Fact]
  79. public void GetAlleleBlockToSampleHaplotype_AlleleBlock_OneAlleleIsRef_EachTime()
  80. {
  81. var genotypeBlock1 = new GenotypeBlock(new[] { "1|0", "0|1", "1|0", "0|1" }.Select(Genotype.GetGenotype).ToArray());
  82. var genotypeBlock2 = new GenotypeBlock(new[] { "1/1", "0|1", "1|0" }.Select(Genotype.GetGenotype).ToArray(), 1);
  83. var genotypeBlock3 = new GenotypeBlock(new[] { "0|0", "1|0", "0|1", "0|0" }.Select(Genotype.GetGenotype).ToArray());
  84. var genotypeBlock4 = new GenotypeBlock(new[] { "0|1", "1|0", "1|0" }.Select(Genotype.GetGenotype).ToArray());
  85. var genotypeToSample =
  86. new Dictionary<GenotypeBlock, List<int>>
  87. {
  88. {genotypeBlock1, new List<int> {0}},
  89. {genotypeBlock2, new List<int> {1}},
  90. {genotypeBlock3, new List<int> {2}},
  91. {genotypeBlock4, new List<int> {3}}
  92. };
  93. var indexOfUnsupportedVars = Enumerable.Repeat(new HashSet<int>(), genotypeBlock1.Genotypes.Length).ToArray();
  94. var starts = new[] { 100, 101, 102, 104 };
  95. var functionBlockRanges = starts.Select(x => x + 2).ToList();
  96. var alleleBlockToSampleHaplotype = AlleleBlock.GetAlleleBlockToSampleHaplotype(genotypeToSample,
  97. indexOfUnsupportedVars, starts, functionBlockRanges, out _);
  98. var expectedBlock1 = new AlleleBlock(0, new[] { 1, 0, 1 }, 0, 0);
  99. var expectedBlock2 = new AlleleBlock(0, new[] { 0, 1, 0 }, 0, 0);
  100. var expectedBlock3 = new AlleleBlock(1, new[] { 1, 0 }, 0, 0);
  101. var expectedBlock4 = new AlleleBlock(1, new[] { 1, 1 }, 0, 0);
  102. var expectedBlock5 = new AlleleBlock(1, new[] { 0, 0 }, 0, 0);
  103. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock1));
  104. Assert.True(alleleBlockToSampleHaplotype[expectedBlock1].SequenceEqual(new[] { new SampleHaplotype(0, 0) }));
  105. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock2));
  106. Assert.True(alleleBlockToSampleHaplotype[expectedBlock2].SequenceEqual(new[] { new SampleHaplotype(0, 1) }));
  107. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock3));
  108. Assert.True(alleleBlockToSampleHaplotype[expectedBlock3].SequenceEqual(new[] { new SampleHaplotype(1, 0) }));
  109. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock4));
  110. Assert.True(alleleBlockToSampleHaplotype[expectedBlock4].SequenceEqual(new[] { new SampleHaplotype(1, 1), new SampleHaplotype(3, 0) }));
  111. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock5));
  112. Assert.True(alleleBlockToSampleHaplotype[expectedBlock5].SequenceEqual(new[] { new SampleHaplotype(3, 1) }));
  113. }
  114. [Fact]
  115. public void GetAlleleBlockToSampleHaplotype_AwareOfTrimmedRefPositions()
  116. {
  117. var genotypeBlock1 = new GenotypeBlock(new[] { "0|0", "1|1", "1|1", "0|0" }.Select(Genotype.GetGenotype).ToArray());
  118. var genotypeBlock2 = new GenotypeBlock(new[] { "0|0", "1|1", "1|1" }.Select(Genotype.GetGenotype).ToArray());
  119. var genotypeBlock3 = new GenotypeBlock(new[] { "1|1", "1|1", "0|0" }.Select(Genotype.GetGenotype).ToArray(), 1);
  120. var genotypeBlock4 = new GenotypeBlock(new[] { "1|1", "1|1" }.Select(Genotype.GetGenotype).ToArray(), 1);
  121. var genotypeToSample =
  122. new Dictionary<GenotypeBlock, List<int>>
  123. {
  124. {genotypeBlock1, new List<int> {0}},
  125. {genotypeBlock2, new List<int> {1}},
  126. {genotypeBlock3, new List<int> {2}},
  127. {genotypeBlock4, new List<int> {3}}
  128. };
  129. var indexOfUnsupportedVars = Enumerable.Repeat(new HashSet<int>(), 4).ToArray();
  130. var starts = Enumerable.Range(100, 4).ToArray();
  131. var functionBlockRanges = starts.Select(x => x + 2).ToList();
  132. var alleleBlockToSampleHaplotype = AlleleBlock.GetAlleleBlockToSampleHaplotype(genotypeToSample,
  133. indexOfUnsupportedVars, starts, functionBlockRanges, out _);
  134. var expectedBlock1 = new AlleleBlock(1, new[] { 1, 1 }, 1, 1);
  135. var expectedBlock2 = new AlleleBlock(1, new[] { 1, 1 }, 1, 0);
  136. var expectedBlock3 = new AlleleBlock(1, new[] { 1, 1 }, 0, 1);
  137. var expectedBlock4 = new AlleleBlock(1, new[] { 1, 1 }, 0, 0);
  138. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock1));
  139. Assert.True(alleleBlockToSampleHaplotype[expectedBlock1]
  140. .SequenceEqual(new[] { new SampleHaplotype(0, 0), new SampleHaplotype(0, 1) }));
  141. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock2));
  142. Assert.True(alleleBlockToSampleHaplotype[expectedBlock2]
  143. .SequenceEqual(new[] { new SampleHaplotype(1, 0), new SampleHaplotype(1, 1) }));
  144. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock3));
  145. Assert.True(alleleBlockToSampleHaplotype[expectedBlock3]
  146. .SequenceEqual(new[] { new SampleHaplotype(2, 0), new SampleHaplotype(2, 1) }));
  147. Assert.True(alleleBlockToSampleHaplotype.ContainsKey(expectedBlock4));
  148. Assert.True(alleleBlockToSampleHaplotype[expectedBlock4]
  149. .SequenceEqual(new[] { new SampleHaplotype(3, 0), new SampleHaplotype(3, 1) }));
  150. }
  151. }
  152. }