impor 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 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] def tupler(genes): if True: tuples = [] while len(genes) > 1: g1 = genes[0] best = len(max(genes, key=len)) sibling = "" for x in genes[1:]: if edit_distance(g1, x) < best: best = edit_distance(g1, x) sibling = x tuples += [(g1, x)] genes = genes[1:] return tuples + [genes] def best_tuples(genes): if True: 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:] return best_tuple_lst x = gene(3) y = gene(3) z = gene(3) print x print y print z print best_tuples([x, y, z])
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