# import random pick = ["A", "T", "C", "G"] def picker(): return pick[random.randrange(0, 4)] def location_picker(length): return random.randrange(0, length) def gene(size): new_gene = [] while size > 0: new_gene += picker() size = size - 1 return new_gene def insert(gene): #have to say gene = insert(gene) after running function spot = location_picker(len(gene)) return gene[:spot] + [picker()] + gene[spot:] def delete(gene): spot = location_picker(len(gene)) return gene[:spot] + gene[(spot + 1):] def substitute(gene): spot = location_picker(len(gene)) gene[spot] = picker() return gene def mutate(gene): #FIX!!! mutation = random.randrange(0, 3) if mutation == 0: return insert(gene) elif mutation == 1: return delete(gene) else: return substitute(gene) ########################################## def edit_distance(seq1, seq2): while len(seq1[0]) > 1 and len(seq2[0]) > 1: seq1 = seq1[0] seq2 = seq2[0] matrix = {} for x in range(len(seq1) + 1): matrix[x, 0] = x for y in range(len(seq2) + 1): matrix[0, y] = y for x in range(1, len(seq1) + 1): for y in range(1, len(seq2) + 1): if seq1[x - 1] == seq2[y - 1]: mismatch = 0 else: mismatch = 1 matrix[x, y] = min(matrix[x, y - 1] + 1, matrix[x - 1, y] + 1, matrix[x - 1, y - 1] + mismatch) return matrix[x, y] ################################### class node: def __init__(self, ltree, top, rtree): self.top = top self.ltree = ltree self.rtree = rtree def __repr__(self): return str(self.top) class BT(node): def __init__(self, left=None, top=None, right=None): if top == None: # primitive self.rep = None else: # extending self.rep = node(left, top, right) def withEntry(self, key, value): return node([], [(key, value)], []) def empty(self): return self.rep == None def root(self): precondition(not self.empty()) return self.rep.top def left(self): precondition(not self.empty()) return self.rep.ltree def right(self): precondition(not self.empty()) return self.rep.rtree def size(self): if self.empty(): return 0 else: return self.left().size() + self.right().size() + 1 def __repr__(self): #inorder traversal a =[] if not self.empty(): if not self.left().empty(): a += self.left().__repr__() if not self.empty(): a += [(self.root()[0][0], self.root()[0][1])] if not self.right().empty(): a += self.right().__repr__() return a ############# class BSTD(BT): """ def rep_invariant(self): if self.empty(): return True elif not self.left().empty(): if self.left().root()[0][0] >= self.root()[0][0]: return False elif not self.right().empty(): if self.right().root()[0][0] <= self.root()[0][0]: return False if self.left().rep_invariant() and self.left().rep_invariant(): return True """ def tupler(genes): if True: tuples = [] while len(genes) > 1: g1 = genes[0] best = len(max(genes, key=len)) for x in genes[1:]: if edit_distance(g1, x) < best: best = edit_distance(g1, x) sibling = x tuples += [(g1, x)] if len(tuples) >= 1: for x in tuples: if g1 == x[1]: tuples = tuples[:x] + tuples[x+1:] genes = genes[1:] return tuples def tupler(genes): if True: tuples = [] while len(genes) > 1: g1 = genes[0] best = len(max(genes, key=len)) for x in genes[1:]: if edit_distance(g1, x) < best: best = edit_distance(g1, x) sibling = x tuples += [(g1, x)] if len(tuples) >= 1: for x in tuples: if g1 == x[1]: tuples = tuples[:x] + tuples[x+1:] genes = genes[1:] g1 = genes[0] best = len(g1) for x in tuples: if edit_distance(g1, x[0]) < best: best = edit_distance(g1, x) sibling = x return tuples """ def best_tuples(self, genes): tuples = tupler(genes) while len(tuples) > len(genes)/2: g1 = tuples[0][0] g2 = tuples[0][1] for y in tuples[1:]: if g1 == y[0]: if edit_distance(g1, y[1]) <= edit_distance(g1, g2): tuples = tuples[1:] else: tuples = tuples[:(tuples.index(y))] + tuples[(tuples.index(y)) + 1:] elif g1 == y[1]: if edit_distance(g1, y[0]) <= edit_distance(g1, g2): tuples = tuples[1:] else: tuples = tuples[:(tuples.index(y))] + tuples[(tuples.index(y)) + 1:] ############ if g2 == y[0]: if edit_distance(g2, y[1]) <= edit_distance(g1, g2): tuples = tuples[1:] else: tuples = tuples[:(tuples.index(y))] + tuples[(tuples.index(y)) + 1:] elif g2 == y[1]: if edit_distance(g2, y[0]) <= edit_distance(g1, g2): tuples = tuples[1:] else: tuples = tuples[:(tuples.index(y))] + tuples[(tuples.index(y)) + 1:] return tuples def best_tuples(genes): #search and delete extra tuples if True: tuples = tupler(genes) #while len(tuples) > len(genes)/2: if True and True: g1 = tuples[0][0] g2 = tuples[0][1] one = [] two = [] for x in tuples: one += x[0] two += x[1] count_g1 = 0 count_g2 = 0 for p in one: if p == g1: count_g1 += 1 elif p == g2: count_g2 += 1 for q in two: if q == g1: count_g1 += 1 elif q == g2: count_g2 += 1 if count_g1 > 1 and count_g2 > 1: for y in tuples[1:]: if g1 == y[0]: if edit_distance(g1, y[1]) <= edit_distance(g1, g2): tuples = tuples[1:] else: tuples = tuples[:(tuples.index(y))] + tuples[(tuples.index(y)) + 1:] elif g1 == y[1]: if edit_distance(g1, y[0]) <= edit_distance(g1, g2): tuples = tuples[1:] else: tuples = tuples[:(tuples.index(y))] + tuples[(tuples.index(y)) + 1:] ############ if g2 == y[0]: if edit_distance(g2, y[1]) <= edit_distance(g1, g2): tuples = tuples[1:] else: tuples = tuples[:(tuples.index(y))] + tuples[(tuples.index(y)) + 1:] elif g2 == y[1]: if edit_distance(g2, y[0]) <= edit_distance(g1, g2): tuples = tuples[1:] else: tuples = tuples[:(tuples.index(y))] + tuples[(tuples.index(y)) + 1:] return tuples """
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question