-
Notifications
You must be signed in to change notification settings - Fork 12
/
orientation.go
193 lines (175 loc) · 4.59 KB
/
orientation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* Filename: /Users/htang/code/allhic/allhic/orientation.go
* Path: /Users/htang/code/allhic/allhic
* Created Date: Friday, January 5th 2018, 4:35:57 pm
* Author: bao
*
* Copyright (c) Haibao Tang
*/
package allhic
import (
"fmt"
"math"
"github.com/gonum/matrix/mat64"
)
// ACCEPT tag show to accept orientation flip
const ACCEPT = "ACCEPT"
// REJECT tag show to reject orientation flip
const REJECT = "REJECT"
// flipLog briefly logs if the orientation flip is informative
func flipLog(method string, score, scoreFlipped float64, tag string) {
log.Noticef("%v: %.5f => %.5f %v", method, score, scoreFlipped, tag)
}
// flipAll initializes the orientations based on pairwise O matrix.
func (r *CLM) flipAll() (tag string) {
var (
M mat64.Dense
e mat64.EigenSym
)
oldSigns := make([]byte, len(r.Signs))
copy(oldSigns, r.Signs)
score := r.EvaluateQ()
N := len(r.Tigs)
e.Factorize(r.O(), true)
M.EigenvectorsSym(&e)
v := M.ColView(N - 1) // v is the eigenvector corresponding to the largest eigenvalue
// fmt.Printf("%0.2v\n\n", mat64.Formatted(v))
signs := make([]byte, N)
for i := 0; i < N; i++ {
if v.At(i, 0) < 0 {
signs[i] = '-'
} else {
signs[i] = '+'
}
}
r.Signs = signs
newScore := r.EvaluateQ()
tag = ACCEPT
if newScore < score {
copy(r.Signs, oldSigns) // Recover
tag = REJECT
}
flipLog("FLIPALL", score, newScore, tag)
return
}
// flipWhole test flipping all contigs at the same time to see if score improves
func (r *CLM) flipWhole() (tag string) {
oldSigns := make([]byte, len(r.Signs))
copy(oldSigns, r.Signs)
score := r.EvaluateQ()
// Flip all the tigs
for i, s := range r.Signs {
r.Signs[i] = rr(s)
}
newScore := r.EvaluateQ()
tag = ACCEPT
if newScore <= score {
copy(r.Signs, oldSigns) // Recover
tag = REJECT
}
flipLog("FLIPWHOLE", score, newScore, tag)
return
}
// flipOne test flipping every single contig sequentially to see if score improves
func (r *CLM) flipOne() (tag string) {
nAccepts := 0
nRejects := 0
anyTagACCEPT := false
score := r.EvaluateQ()
for i, t := range r.Tour.Tigs {
idx := t.Idx
r.Signs[idx] = rr(r.Signs[idx])
newScore := r.EvaluateQ()
if newScore > score {
nAccepts++
tag = ACCEPT
} else {
r.Signs[idx] = rr(r.Signs[idx]) // Recover
nRejects++
tag = REJECT
}
if (i+1)%50 == 0 {
flipLog(fmt.Sprintf("FLIPONE (%d/%d)", i+1, r.Tour.Len()),
score, newScore, tag)
}
if tag == ACCEPT {
anyTagACCEPT = true
score = newScore
}
}
log.Noticef("FLIPONE: N_accepts=%d N_rejects=%d", nAccepts, nRejects)
if anyTagACCEPT {
tag = ACCEPT
} else {
tag = REJECT
}
return
}
// O yields a pairwise orientation matrix, where each cell contains the strandedness
// times the number of links between i-th and j-th contig
func (r *CLM) O() *mat64.SymDense {
N := len(r.Tigs)
P := mat64.NewSymDense(N, nil)
for pair, contact := range r.contacts {
score := float64(contact.strandedness * contact.nlinks)
P.SetSym(pair.ai, pair.bi, score)
}
return P
}
// Q yields a contact frequency matrix when contigs are already oriented. This is a
// similar matrix as M, but rather than having the number of links in the
// cell, it points to an array that has the actual distances.
func (r *CLM) Q() [][]GArray {
N := len(r.Tigs)
P := Make2DGArraySlice(N, N)
for i := 0; i < N; i++ {
for j := 0; j < N; j++ {
P[i][j][0] = -1 // Sentinel to signal that there is no entry
}
}
for pair, gdists := range r.orientedContacts {
ai := pair.ai
bi := pair.bi
if r.Signs[ai] == pair.ao && r.Signs[bi] == pair.bo {
P[ai][bi] = gdists
}
}
return P
}
// EvaluateQ sums up all distance is defined as the sizes of interleaving contigs
// 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() float64 {
tour := r.Tour
Q := r.Q()
score := 0.0
size := tour.Len()
cumsize := make([]int, size)
cumSum := 0
for i, t := range tour.Tigs {
cumsize[i] = cumSum
cumSum += t.Size
}
// fmt.Println(tour.Tigs, cumsize)
// Now add up all the pairwise scores
for i := 0; i < size; i++ {
a := tour.Tigs[i].Idx
for j := i + 1; j < size; j++ {
b := tour.Tigs[j].Idx
if Q[a][b][0] == -1 {
continue // Entire GArray is empty
}
dist := cumsize[j-1] - cumsize[i]
if dist > LIMIT {
break
}
for k := 0; k < BB; k++ {
// 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 score
}