From 9cc9adaa2504214347214442099a494589a312a6 Mon Sep 17 00:00:00 2001 From: Haibao Tang Date: Mon, 2 Jul 2018 23:14:58 -0700 Subject: [PATCH] [orientation] Use SumLog in EvaluateQ() --- base.go | 16 ++++++++++++---- clm.go | 5 +++-- orientation.go | 9 ++++++--- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/base.go b/base.go index ba24cb4..59ab007 100644 --- a/base.go +++ b/base.go @@ -115,13 +115,21 @@ func Round(input float64) float64 { return math.Floor(input + 0.5) } +// SumLog returns the kernel of sum of log likelihood +func SumLog(a []int) float64 { + sum := 0.0 + for _, val := range a { + sum += math.Log(float64(val)) + } + return sum +} + // HmeanInt returns the harmonic mean // That is: n / (1/x1 + 1/x2 + ... + 1/xn) -func HmeanInt(a []int, amin, amax int) int { +func HmeanInt(a []int, amin, amax int) float64 { size := len(a) sum := 0.0 - for i := 0; i < size; i++ { - val := a[i] + for _, val := range a { if val > amax { val = amax } else if val < amin { @@ -129,7 +137,7 @@ func HmeanInt(a []int, amin, amax int) int { } sum += 1.0 / float64(val) } - return int(Round(float64(size) / sum)) + return float64(size) / sum } // GoldenArray is given list of ints, we aggregate similar values so that it becomes an diff --git a/clm.go b/clm.go index b03ccb1..bf3521f 100644 --- a/clm.go +++ b/clm.go @@ -69,7 +69,7 @@ type OrientedPair struct { type Contact struct { strandedness int nlinks int - meanDist int + meanDist float64 } // TigF stores the index to activeTigs and size of the tig @@ -192,7 +192,8 @@ func (r *CLM) ParseClm() { // Store all these info in contacts gdists := GoldenArray(line.links) - meanDist := HmeanInt(line.links, GRLB, GRUB) + // meanDist := Hmean(line.links, GRLB, GRUB) + meanDist := SumLog(line.links) strandedness := 1 if line.ao != line.bo { strandedness = -1 diff --git a/orientation.go b/orientation.go index a760402..e591aa8 100644 --- a/orientation.go +++ b/orientation.go @@ -11,6 +11,7 @@ package allhic import ( "fmt" + "math" "github.com/gonum/matrix/mat64" ) @@ -155,9 +156,10 @@ func (r *CLM) Q() [][]GArray { // plus the actual link distances. Maximize Sum(1 / distance) for all links. // For performance consideration, we actually use a histogram to approximate // all link distances. See goldenArray() for details. -func (r *CLM) EvaluateQ() (score float64) { +func (r *CLM) EvaluateQ() float64 { tour := r.Tour Q := r.Q() + score := 0.0 size := tour.Len() cumsize := make([]int, size) @@ -181,10 +183,11 @@ func (r *CLM) EvaluateQ() (score float64) { break } for k := 0; k < BB; k++ { - score += float64(Q[a][b][k]) / float64(GR[k]+dist) + // score += float64(Q[a][b][k]) / float64(GR[k]+dist) + score -= float64(Q[a][b][k]) * math.Log(float64(GR[k]+dist)) } // fmt.Println(r.Tigs[a], r.Tigs[b], Q[a][b], score) } } - return + return score }