Example #1
0
// Matrix computation function for semiglobal
func ComputeMatrix(a string, b string, scoreMatrix map[string]int, gp int) [][]int {
	F := make([][]int, len(b)+1)

	for i := 0; i < len(b)+1; i++ {
		F[i] = make([]int, len(a)+1)
	}

	// initialize first row and column to 0
	for j := 0; j < len(a)+1; j++ {
		F[0][j] = 0
	}

	for i := 0; i < len(b)+1; i++ {
		F[i][0] = 0
	}

	for i := 1; i < len(b)+1; i++ {
		for j := 1; j < len(a)+1; j++ {
			match := F[i-1][j-1] + utils.Score(a[j-1], b[i-1], scoreMatrix)
			del := F[i-1][j] + gp
			insert := F[i][j-1] + gp
			// skip the gap penalty if it's the last row or column
			if i == len(b) || j == len(a) {
				del -= gp
				insert -= gp
			}
			F[i][j] = utils.GetMax(match, del, insert)
		}
	}

	return F
}
Example #2
0
// Traceback for semiglobal
func GetTraceback(m [][]int, seqA string, seqB string, scoreMatrix map[string]int, gapPenalty int) (int, string, string) {
	alignmentA := ""
	alignmentB := ""
	i := len(seqB)
	j := len(seqA)

	var gp int

	for i > 0 || j > 0 {
		// Set the gap penalty
		if i == 0 || j == 0 || i == len(seqB) || j == len(seqA) {
			gp = 0 // 0 for first or last row/column
		} else {
			gp = gapPenalty // otherwise use regular gap penalty
		}
		x := utils.GetMax(0, i-1)
		y := utils.GetMax(0, j-1)
		if i > 0 && j > 0 && m[i][j] == (m[i-1][j-1]+utils.Score(seqA[y], seqB[x], scoreMatrix)) {
			alignmentA = string(seqA[y]) + alignmentA
			alignmentB = string(seqB[x]) + alignmentB
			i--
			j--
		} else if j > 0 && m[i][j] == (m[i][j-1]+gp) {
			alignmentA = string(seqA[y]) + alignmentA
			alignmentB = "-" + alignmentB
			j--
		} else if i > 0 && m[i][j] == (m[i-1][j]+gp) {
			alignmentA = "-" + alignmentA
			alignmentB = string(seqB[x]) + alignmentB
			i--
		}
	}
	return m[len(seqB)][len(seqA)], alignmentA, alignmentB
}
Example #3
0
// Matrix computation for Smith-Waterman alignment
func ComputeMatrix(a string, b string, scoreMatrix map[string]int, gp int) [][]int {
	F := make([][]int, len(b)+1)

	for i := 0; i < len(b)+1; i++ {
		F[i] = make([]int, len(a)+1)
	}

	for j := 0; j < len(a)+1; j++ {
		F[0][j] = 0
	}

	for i := 0; i < len(b)+1; i++ {
		F[i][0] = 0
	}

	for i := 1; i < len(b)+1; i++ {
		for j := 1; j < len(a)+1; j++ {
			match := F[i-1][j-1] + utils.Score(a[j-1], b[i-1], scoreMatrix)
			del := F[i-1][j] + gp
			insert := F[i][j-1] + gp
			F[i][j] = utils.GetMax(match, del, insert, 0) // max with 0
		}
	}

	return F
}
Example #4
0
// Traceback for Smith-Waterman
func GetTraceback(m [][]int, seqA string, seqB string, scoreMatrix map[string]int, gp int) (int, string, string) {
	alignmentA := ""
	alignmentB := ""
	i, j := getStartingPoint(m)
	I, J := i, j

	for i > 0 || j > 0 {
		x := utils.GetMax(0, i-1)
		y := utils.GetMax(0, j-1)
		// stop if there's a 0 and no pointer that produced it...which only happens in the first row/column
		if m[i][j] == 0 && (i == 0 || j == 0) {
			break
		}
		if i > 0 && j > 0 && m[i][j] == (m[i-1][j-1]+utils.Score(seqA[y], seqB[x], scoreMatrix)) {
			alignmentA = string(seqA[y]) + alignmentA
			alignmentB = string(seqB[x]) + alignmentB
			i--
			j--
		} else if j > 0 && m[i][j] == (m[i][j-1]+gp) {
			alignmentA = string(seqA[y]) + alignmentA
			alignmentB = "-" + alignmentB
			j--
		} else if i > 0 && m[i][j] == (m[i-1][j]+gp) {
			alignmentA = "-" + alignmentA
			alignmentB = string(seqB[x]) + alignmentB
			i--
		}
	}
	return m[I][J], alignmentA, alignmentB
}
Example #5
0
// Get the traceback
func GetTraceback(m [][]int, seqA string, seqB string, scoreMatrix map[string]int, gp int) (int, string, string) {
	alignmentA := ""
	alignmentB := ""
	i := len(seqB)
	j := len(seqA)

	for i > 0 || j > 0 {
		// Compute and store the string indexes
		x := utils.GetMax(0, i-1)
		y := utils.GetMax(0, j-1)
		// diagonal
		if i > 0 && j > 0 && m[i][j] == (m[i-1][j-1]+utils.Score(seqA[y], seqB[x], scoreMatrix)) {
			alignmentA = string(seqA[y]) + alignmentA
			alignmentB = string(seqB[x]) + alignmentB
			i--
			j--
			// left
		} else if j > 0 && m[i][j] == (m[i][j-1]+gp) {
			alignmentA = string(seqA[y]) + alignmentA
			alignmentB = "-" + alignmentB
			j--
			// up
		} else if i > 0 && m[i][j] == (m[i-1][j]+gp) {
			alignmentA = "-" + alignmentA
			alignmentB = string(seqB[x]) + alignmentB
			i--
		}
	}
	// return score and two alignments
	return m[len(seqB)][len(seqA)], alignmentA, alignmentB
}