A closed-form solution is offered in An exact formula for the number of alignments
between two DNA sequences by Torres, Cabada, and Nieto:
$$f(m,n)=\sum_{k=0}^{min(m,n)}2^{k}\binom{m}{k}\binom{n}{k}$$
If this solution seems reasonable, you could calculate this without BioPython, but with a simple xrange
loop, the power operator, and scipy.special.binom
:
#!/usr/bin/env python
import sys
import scipy.special
def alignments(m, n):
if m < 0 or n < 0:
raise ValueError('m and n should be non-negative')
s = 0
b = min(m, n) + 1
for k in xrange(0, b):
s += (2 ** k) * scipy.special.binom(m, k) * scipy.special.binom(n, k)
return s
def main():
m = 4
n = 2
a = alignments(m, n)
assert (m == 4 and n == 2 and a == 41), "something went wrong!"
sys.stdout.write("alignments(%d, %d) = %d\n" % (m, n, a))
if __name__ == "__main__":
main()
I don't know what your m
and n
are, but keep in mind that this solution grows quickly. Also, I did some digging and scipy approximates the calculation of the binomial coefficient, which is probably going to be an issue for large m
and n
.
If you're working with large values, you may want to look into using the Python long
type and consider the accuracy of the answer. The Stack Overflow link offers alternatives to scipy that generate an exact calculation of the binomial coefficient, at the cost of a longer runtime.