Exemplo n.º 1
0
 def m(self, s):
     '''Returns the number of non-crossing matching in the string s.'''
     if not s: return 1
     if not s in self._m:
         s0c = ro.RNA_COMPLEMENT[s[0]]
         C = [i for i in xrange(1, len(s)) if s[i] == s0c]
         self._m[s] = (self.m(s[1:]) + ro.sum_mod((self.m(s[1:i]) * self.m(s[i + 1:]) for i in C), self._r)) % self._r
     return self._m[s]
Exemplo n.º 2
0
 def c(self, s):
     '''Returns the number of perfect matching for the binary tuple s.'''
     if not s in self._c:
         # print 'c[%s]' % (ro.join_list(s))
         n, r = len(s) / 2, self._r
         if n == 0: self._c[s] = 1
         else:
             K = list(self._k_range(s)) 
             a = ro.sum_mod(((self.c(s[1:2 * k + 1]) * self.c(s[2 * k + 2:]) % r) for k in K), r)
             self._c[s] = a
     return self._c[s]
Exemplo n.º 3
0
def edit_distance_and_count(x, y, r, debug=False):
    '''Integrated DP+backtracking, O(mn) time, O(min(m,n)) storage.'''
    m, n = len(x), len(y)
    if m < n: return edit_distance_and_count(y, x, r, debug=debug)
    c, c_old = zip(range(n + 1), [1] * (n + 1)), [None] * (n + 1)
    if debug: print c
    for xi in x:
        c_old[:] = c[:] # Advance to next row
        c[0] = (c_old[0][0] + 1, c_old[0][1]) # Initial condition 
        for j, yj in enumerate(y, 1): # Dynamic programming
            c_prev = ((cost(xi, yj), c_old[j - 1]), (cost(None, yj), c_old[j]), (cost(xi, None), c[j - 1]))
            d = min((z[0] + z[1][0]) for z in c_prev)
            c[j] = (d, ro.sum_mod((z[1][1] for z in c_prev if z[0] + z[1][0] == d), r))
        if debug: print c
    return c[-1][1]
Exemplo n.º 4
0
def num_quartets(t, r=1000000L):
    '''Augment a Phylo tree with the number of leaves and number of inferred quartets in sub-tree.
    Return the number of quartets at the root node = q(T) mod r.'''
    for node in t.find_clades(order='postorder'):
        children = list(node)
        node.num_terminals = 1 if node.is_terminal() else sum(child.num_terminals for child in node)
        node.num_quartets = 0 if node.is_terminal() else \
        (ro.sum_mod((child.num_quartets for child in node), r) + \
         (S(children[0].num_terminals, children[1].num_terminals) if len(children) == 2 else \
          ro.sum_mod((S(child.num_terminals,
                        sum(other_child.num_terminals for other_child in node if other_child != child))
                        for child in node), r))) % r#         sum(child.num_quartets for child in node) + \
#         (S(children[0].num_terminals, children[1].num_terminals) if len(children) == 2 else \
#          sum(S(child.num_terminals,
#                sum(other_child.num_terminals for other_child in node if other_child != child))
#              for child in node))
#         n = node.num_terminals
#         ref = cntq_ncgll(n)
#         print node, 'leaves', node.num_terminals, 'quartets', node.num_quartets, 'ref', ref
#         for child in node:
#             print '\t', child, child.num_terminals, child.num_quartets
#         if ref != node.num_quartets:
#             print 'NOT EQUAL TO WHAT WE THINK IT SHOULD BE'
    return t.root.num_quartets
Exemplo n.º 5
0
def aspc(f, r=1000000L):
    '''Main driver to solve this problem.'''
    n, m = ro.read_ints_str(f)
    return ro.sum_mod(it.islice(ro.binom_mod(r), n, n + 1).next()[m:], r)