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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
// This file is automatically generated. Do not edit - make changes to relevant got file.
// Copyright ©2011-2012 The bíogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package align
import (
"github.com/biogo/biogo/alphabet"
"github.com/biogo/biogo/feat"
"fmt"
"os"
"text/tabwriter"
)
//line sw_affine_type.got:17
func drawSWAffineTableQLetters(rSeq, qSeq alphabet.QLetters, index alphabet.Index, table [][3]int, a SWAffine) {
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 0, ' ', tabwriter.AlignRight|tabwriter.Debug)
fmt.Printf("rSeq: %s\n", rSeq)
fmt.Printf("qSeq: %s\n", qSeq)
for l := 0; l < 3; l++ {
fmt.Fprintf(tw, "%c\tqSeq\t", "MUL"[l])
for _, l := range qSeq {
fmt.Fprintf(tw, "%c\t", l)
}
fmt.Fprintln(tw)
r, c := rSeq.Len()+1, qSeq.Len()+1
fmt.Fprint(tw, "rSeq\t")
for i := 0; i < r; i++ {
if i != 0 {
fmt.Fprintf(tw, "%c\t", rSeq[i-1].L)
}
for j := 0; j < c; j++ {
p := pointerSWAffineQLetters(rSeq, qSeq, i, j, l, table, index, a, c)
var vi interface{}
if vi = table[i*c+j][l]; vi == minInt {
vi = "-Inf"
}
fmt.Fprintf(tw, "%s % 4v\t", p, vi)
}
fmt.Fprintln(tw)
}
fmt.Fprintln(tw)
}
tw.Flush()
}
func pointerSWAffineQLetters(rSeq, qSeq alphabet.QLetters, i, j, l int, table [][3]int, index alphabet.Index, a SWAffine, c int) string {
if i == 0 || j == 0 {
return " "
}
rVal := index[rSeq[i-1].L]
qVal := index[qSeq[j-1].L]
if rVal < 0 || qVal < 0 {
return " "
}
switch p := i*c + j; {
case table[p][l] == 0:
return " "
case table[p-c][up]+a.Matrix[rVal][gap] == table[p][l] && table[(i-1)*c+j-1][l] != 0:
return "⬆ u"
case table[p-1][left]+a.Matrix[gap][qVal] == table[p][l] && table[(i-1)*c+j-1][l] != 0:
return "⬅ l"
case table[p-c][diag]+a.GapOpen+a.Matrix[rVal][gap] == table[p][l] && table[(i-1)*c+j-1][l] != 0:
return "⬆ m"
case table[p-1][diag]+a.GapOpen+a.Matrix[gap][qVal] == table[p][l] && table[(i-1)*c+j-1][l] != 0:
return "⬅ m"
case table[p-c-1][diag]+a.Matrix[rVal][qVal] == table[p][l] && table[(i-1)*c+j-1][l] != 0:
return "⬉ m"
case table[p-c-1][up]+a.Matrix[rVal][qVal] == table[p][l] && table[(i-1)*c+j-1][l] != 0:
return "⬉ u"
case table[p-c-1][left]+a.Matrix[rVal][qVal] == table[p][l] && table[(i-1)*c+j-1][l] != 0:
return "⬉ l"
default:
return [3]string{"⌜ ", "⬆ u", "⬅ l"}[l]
}
}
func (a SWAffine) alignQLetters(rSeq, qSeq alphabet.QLetters, alpha alphabet.Alphabet) ([]feat.Pair, error) {
let := len(a.Matrix)
if let < alpha.Len() {
return nil, ErrMatrixWrongSize{Size: let, Len: alpha.Len()}
}
la := make([]int, 0, let*let)
for _, row := range a.Matrix {
if len(row) != let {
return nil, ErrMatrixNotSquare
}
la = append(la, row...)
}
r, c := rSeq.Len()+1, qSeq.Len()+1
table := make([][3]int, r*c)
var (
index = alpha.LetterIndex()
maxS, maxI, maxJ = 0, 0, 0
score int
)
for i := 1; i < r; i++ {
for j := 1; j < c; j++ {
var (
rVal = index[rSeq[i-1].L]
qVal = index[qSeq[j-1].L]
)
if rVal < 0 {
return nil, fmt.Errorf("align: illegal letter %q at position %d in rSeq", rSeq[i-1].L, i-1)
}
if qVal < 0 {
return nil, fmt.Errorf("align: illegal letter %q at position %d in qSeq", qSeq[j-1].L, j-1)
}
p := i*c + j
diagScore := table[p-c-1][diag]
upScore := table[p-c-1][up]
leftScore := table[p-c-1][left]
score = max3(diagScore, upScore, leftScore)
matched := score == diagScore
score += la[rVal*let+qVal]
switch {
case score > 0:
if score >= maxS && matched {
maxS, maxI, maxJ = score, i, j
}
default:
score = 0
}
table[p][diag] = score
score = max2(
table[p-c][diag]+a.GapOpen+la[rVal*let],
table[p-c][up]+la[rVal*let],
)
if score < 0 {
score = 0
}
table[p][up] = score
score = max2(
table[p-1][diag]+a.GapOpen+la[qVal],
table[p-1][left]+la[qVal],
)
if score < 0 {
score = 0
}
table[p][left] = score
}
}
if debugSmithAffine {
drawSWAffineTableQLetters(rSeq, qSeq, index, table, a)
}
var aln []feat.Pair
score, last, layer := 0, diag, diag
i, j := maxI, maxJ
loop:
for i > 0 && j > 0 {
var (
rVal = index[rSeq[i-1].L]
qVal = index[qSeq[j-1].L]
)
switch p := i*c + j; table[p][layer] {
case 0:
break loop
case table[p-c][up] + la[rVal*let]:
if last != up && p != len(table)-1 {
aln = append(aln, &featPair{
a: feature{start: i, end: maxI},
b: feature{start: j, end: maxJ},
score: score,
})
maxI, maxJ = i, j
score = 0
}
score += table[p][layer] - table[p-c][up]
i--
layer = up
last = up
case table[p-1][left] + la[qVal]:
if last != left && p != len(table)-1 {
aln = append(aln, &featPair{
a: feature{start: i, end: maxI},
b: feature{start: j, end: maxJ},
score: score,
})
maxI, maxJ = i, j
score = 0
}
score += table[p][layer] - table[p-1][left]
j--
layer = left
last = left
case table[p-c][diag] + a.GapOpen + la[rVal*let]:
if last != up && p != len(table)-1 {
aln = append(aln, &featPair{
a: feature{start: i, end: maxI},
b: feature{start: j, end: maxJ},
score: score,
})
maxI, maxJ = i, j
score = 0
}
score += table[p][layer] - table[p-c][diag]
i--
layer = diag
last = up
case table[p-1][diag] + a.GapOpen + la[qVal]:
if last != left && p != len(table)-1 {
aln = append(aln, &featPair{
a: feature{start: i, end: maxI},
b: feature{start: j, end: maxJ},
score: score,
})
maxI, maxJ = i, j
score = 0
}
score += table[p][layer] - table[p-1][diag]
j--
layer = diag
last = left
case table[p-c-1][diag] + la[rVal*let+qVal]:
if last != diag {
aln = append(aln, &featPair{
a: feature{start: i, end: maxI},
b: feature{start: j, end: maxJ},
score: score,
})
maxI, maxJ = i, j
score = 0
}
score += table[p][layer] - table[p-c-1][diag]
i--
j--
layer = diag
last = diag
case table[p-c-1][up] + la[rVal*let+qVal]:
if last != diag {
aln = append(aln, &featPair{
a: feature{start: i, end: maxI},
b: feature{start: j, end: maxJ},
score: score,
})
maxI, maxJ = i, j
score = 0
}
score += table[p][layer] - table[p-c-1][up]
i--
j--
layer = up
last = diag
case table[p-c-1][left] + la[rVal*let+qVal]:
if last != diag {
aln = append(aln, &featPair{
a: feature{start: i, end: maxI},
b: feature{start: j, end: maxJ},
score: score,
})
maxI, maxJ = i, j
score = 0
}
score += table[p][layer] - table[p-c-1][left]
i--
j--
layer = left
last = diag
default:
panic(fmt.Sprintf("align: sw affine internal error: no path at row: %d col:%d layer:%s\n", i, j, "mul"[layer:layer+1]))
}
}
aln = append(aln, &featPair{
a: feature{start: i, end: maxI},
b: feature{start: j, end: maxJ},
score: score,
})
for i, j := 0, len(aln)-1; i < j; i, j = i+1, j-1 {
aln[i], aln[j] = aln[j], aln[i]
}
return aln, nil
}
|