forked from mtarbit/Rosalind-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e025-long.py
executable file
·73 lines (58 loc) · 1.91 KB
/
e025-long.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# coding=utf-8
# Genome Assembly as Shortest Superstring
# =======================================
#
# Given a collection of strings, a larger string containing every one of the
# smaller strings as a substring is called a superstring.
#
# By the assumption of parsimony, a shortest possible superstring over a
# collection of reads serves as a candidate chromosome.
#
# Given: At most 50 DNA strings of equal length not exceeding 1 kbp (which
# represent reads deriving from the same strand of a single linear chromosome).
#
# The dataset is guaranteed to satisfy the following condition: there exists a
# unique way to reconstruct the entire chromosome from these reads by gluing
# together pairs of reads that overlap by more than half their length.
#
# Return: A shortest superstring containing all the given strings (thus
# corresponding to a reconstructed chromosome).
#
# Sample Dataset
# --------------
# ATTAGACCTG
# CCTGCCGGAA
# AGACCTGCCG
# GCCGGAATAC
#
# Sample Output
# -------------
# ATTAGACCTGCCGGAATAC
def find_overlaps(arr, acc=''):
if len(arr) == 0:
return acc
elif len(acc) == 0:
acc = arr.pop(0)
return find_overlaps(arr, acc)
else:
for i in range(len(arr)):
a = arr[i]
l = len(a)
for p in range(l / 2):
q = l - p
if acc.startswith(a[p:]):
arr.pop(i)
return find_overlaps(arr, a[:p] + acc)
if acc.endswith(a[:q]):
arr.pop(i)
return find_overlaps(arr, acc + a[q:])
if __name__ == "__main__":
small_dataset = """
ATTAGACCTG
CCTGCCGGAA
AGACCTGCCG
GCCGGAATAC
"""
large_dataset = open('datasets/rosalind_long.txt').read().strip()
print find_overlaps(large_dataset.split())