| 1 |
// Copyright ©2011-2012 The bíogo Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package align |
| 6 |
|
| 7 |
import ( |
| 8 |
"github.com/biogo/biogo/alphabet" |
| 9 |
"github.com/biogo/biogo/feat" |
| 10 |
) |
| 11 |
|
| 12 |
// Setting debugFitted to true gives verbose scoring table output for the dynamic programming. |
| 13 |
const debugFitted = false |
| 14 |
|
| 15 |
// Fitted is the linear gap penalty fitted Needleman-Wunsch aligner type. |
| 16 |
type Fitted Linear |
| 17 |
|
| 18 |
// Align aligns two sequences using a modified Needleman-Wunsch algorithm that finds a local region of |
| 19 |
// the reference with high similarity to the query. It returns an alignment description or an error if |
| 20 |
// the scoring matrix is not square, or the sequence data types or alphabets do not match. |
| 21 |
func (a Fitted) Align(reference, query AlphabetSlicer) ([]feat.Pair, error) { |
| 22 |
alpha := reference.Alphabet() |
| 23 |
if alpha == nil { |
| 24 |
return nil, ErrNoAlphabet |
| 25 |
} |
| 26 |
if alpha != query.Alphabet() { |
| 27 |
return nil, ErrMismatchedAlphabets |
| 28 |
} |
| 29 |
if alpha.IndexOf(alpha.Gap()) != 0 { |
| 30 |
return nil, ErrNotGappedAlphabet |
| 31 |
} |
| 32 |
switch rSeq := reference.Slice().(type) { |
| 33 |
case alphabet.Letters: |
| 34 |
qSeq, ok := query.Slice().(alphabet.Letters) |
| 35 |
if !ok { |
| 36 |
return nil, ErrMismatchedTypes |
| 37 |
} |
| 38 |
return a.alignLetters(rSeq, qSeq, alpha) |
| 39 |
case alphabet.QLetters: |
| 40 |
qSeq, ok := query.Slice().(alphabet.QLetters) |
| 41 |
if !ok { |
| 42 |
return nil, ErrMismatchedTypes |
| 43 |
} |
| 44 |
return a.alignQLetters(rSeq, qSeq, alpha) |
| 45 |
default: |
| 46 |
return nil, ErrTypeNotHandled |
| 47 |
} |
| 48 |
} |