github.com/biogo/biogo@v1.0.4/align/sw.go (about) 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 debugSmith to true gives verbose scoring table output for the dynamic programming. 13 const debugSmith = false 14 15 // SW is the Smith-Waterman aligner type. 16 // Matrix is a square scoring matrix with the last column and last row specifying gap penalties. 17 // Currently gap opening is not considered. 18 type SW Linear 19 20 // Align aligns two sequences using the Smith-Waterman algorithm. It returns an alignment description 21 // or an error if the scoring matrix is not square, or the sequence data types or alphabets do not match. 22 func (a SW) Align(reference, query AlphabetSlicer) ([]feat.Pair, error) { 23 alpha := reference.Alphabet() 24 if alpha == nil { 25 return nil, ErrNoAlphabet 26 } 27 if alpha != query.Alphabet() { 28 return nil, ErrMismatchedAlphabets 29 } 30 if alpha.IndexOf(alpha.Gap()) != 0 { 31 return nil, ErrNotGappedAlphabet 32 } 33 switch rSeq := reference.Slice().(type) { 34 case alphabet.Letters: 35 qSeq, ok := query.Slice().(alphabet.Letters) 36 if !ok { 37 return nil, ErrMismatchedTypes 38 } 39 return a.alignLetters(rSeq, qSeq, alpha) 40 case alphabet.QLetters: 41 qSeq, ok := query.Slice().(alphabet.QLetters) 42 if !ok { 43 return nil, ErrMismatchedTypes 44 } 45 return a.alignQLetters(rSeq, qSeq, alpha) 46 default: 47 return nil, ErrTypeNotHandled 48 } 49 }