-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdjList3.py
59 lines (53 loc) · 1.27 KB
/
AdjList3.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
fasta = open('rosalind_grph (1).txt')
output = open('rosalind_grph_output.txt', 'w')
def read_fasta(fasta):
DNAs = {}
for line in fasta:
if line[0] == '>':
name = line[1:].strip()
else:
DNA = ''.join(line.split())
if name in DNAs:
DNAs[name] += DNA
else:
DNAs[name] = DNA
return DNAs
DNAs = read_fasta(fasta)
# print DNAs
k = 3
prefix_name = {}
suffix_name = {}
for name, DNA in DNAs.items():
prefix = DNA[:k]
if prefix in prefix_name:
prefix_name[prefix].append(name)
else:
prefix_name[prefix] = [name]
suffix = DNA[-k:]
if suffix in suffix_name:
suffix_name[suffix].append(name)
else:
suffix_name[suffix] = [name]
def print_adjlist(start_nodes, end_nodes):
# print start_nodes
# print end_nodes
for start in start_nodes:
for end in end_nodes:
if start != end:
print >> output, start, end
sorted_prefix = sorted(prefix_name.items())
sorted_suffix = sorted(suffix_name.items())
# print sorted_prefix
# print sorted_suffix
i = 0
j = 0
while i < len(sorted_prefix) and j < len(sorted_suffix):
# print sorted_prefix[i][0], sorted_suffix[j][0]
if sorted_prefix[i][0] == sorted_suffix[j][0]:
print_adjlist(sorted_suffix[j][1], sorted_prefix[i][1])
i +=1
j + 1
elif sorted_prefix[i][0] < sorted_suffix[j][0]:
i += 1
else:
j += 1