Skip to content

Commit 0232972

Browse files
committed
cleanup
1 parent f57f1e8 commit 0232972

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

src/main/scala/com/fulcrumgenomics/vcf/DownsampleVcf.scala

+12-14
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ object DownsampleVcf extends LazyLogging {
121121
* @param epsilon the error rate for genotyping
122122
* @return a new `Likelihood` that has the likelihoods of AA, AB, and BB
123123
*/
124-
def biallelic(alleleDepthA: Int, alleleDepthB: Int, epsilon: Double = 0.01): Likelihoods = {
124+
def biallelic(alleleDepthA: Int, alleleDepthB: Int, epsilon: Double = 0.01): IndexedSeq[Double] = {
125125
val aGivenAA = log10(1 - epsilon)
126126
val aGivenBB = log10(epsilon)
127127
val aGivenAB = log10((1 - epsilon) / 2)
@@ -130,7 +130,7 @@ object DownsampleVcf extends LazyLogging {
130130
val rawGlBB = ((alleleDepthA * aGivenBB) + (alleleDepthB * aGivenAA))
131131
val rawGlAB = ((alleleDepthA + alleleDepthB) * aGivenAB)
132132

133-
Likelihoods(2, IndexedSeq(rawGlAA, rawGlAB, rawGlBB))
133+
IndexedSeq(rawGlAA, rawGlAB, rawGlBB)
134134
}
135135

136136
/** Computes the likelihoods for each possible genotype given a sequence of read depths for any
@@ -140,30 +140,28 @@ object DownsampleVcf extends LazyLogging {
140140
* @return a new `Likelihood` that has the log likelihoods of all possible genotypes in the
141141
* order specified in VFC spec for the GL/PL tags.
142142
*/
143-
def generalized(alleleDepths: IndexedSeq[Int], epsilon: Double = 0.01): Likelihoods = {
143+
def generalized(alleleDepths: IndexedSeq[Int], epsilon: Double = 0.01): IndexedSeq[Double] = {
144144
val numAlleles = alleleDepths.length
145145
// probabilities associated with each possible genotype for a pair of alleles
146146
val logProbs: Array[Double] = Array(
147147
math.log10(epsilon),
148148
math.log10((1 - epsilon) / 2),
149149
math.log10(1 - epsilon)
150150
)
151-
// raw genotype log-likelihoods
152-
Likelihoods(
153-
numAlleles,
154-
(0 until numAlleles).flatMap(b =>
155-
(0 to b).map(a =>
156-
(0 until numAlleles).map(allele =>
157-
logProbs(Array(a, b).count(_ == allele)) * alleleDepths(allele)
158-
).sum
159-
)
151+
// compute genotype log-likelihoods
152+
(0 until numAlleles).flatMap(b =>
153+
(0 to b).map(a =>
154+
(0 until numAlleles).map(allele =>
155+
logProbs(Array(a, b).count(_ == allele)) * alleleDepths(allele)
156+
).sum
160157
)
161158
)
162159
}
163160

164161
def apply(alleleDepths: IndexedSeq[Int], epsilon: Double = 0.01): Likelihoods = {
165-
require(alleleDepths.length >= 2, "at least two alleles are required to calculate genotype likelihoods")
166-
generalized(alleleDepths, epsilon)
162+
val numAlleles = alleleDepths.length
163+
require(numAlleles >= 2, "at least two alleles are required to calculate genotype likelihoods")
164+
Likelihoods(numAlleles, generalized(alleleDepths, epsilon))
167165
}
168166
}
169167

src/test/scala/com/fulcrumgenomics/vcf/DownsampleVcfTest.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ class DownsampleVcfTest extends UnitSpec {
228228
(IndexedSeq(2, 0), IndexedSeq(math.pow((1 - e), 2), 0.25, math.pow(e, 2))),
229229
)
230230
cases.foreach { case (input, output) =>
231-
val biallelic = DownsampleVcf.Likelihoods.biallelic(input(0), input(1), e)
232-
val generalized = DownsampleVcf.Likelihoods.generalized(input, e)
231+
val biallelic = Likelihoods(2, DownsampleVcf.Likelihoods.biallelic(input(0), input(1), e))
232+
val generalized = Likelihoods(2, DownsampleVcf.Likelihoods.generalized(input, e))
233233
biallelic.pls should contain theSameElementsInOrderAs generalized.pls
234234
}
235235
}

0 commit comments

Comments
 (0)