-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontigs.py
61 lines (47 loc) · 1.21 KB
/
contigs.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
import sys, fileinput
import bioinfo
source = fileinput.input()
kmers = bioinfo.read_kmer_list(source)
source.close()
#print kmers
"""
for kmerset in kmers.values():
for kmer in kmerset:
tail = kmer[1:]
if tail in kmers:
if kmer not in graph:
graph[kmer] = set()
graph[kmer].update(kmers[tail])
"""
(graph, graph_counts) = bioinfo.make_graph_with_counts(kmers)
walked_graph_counts = {kmer:[0,0] for kmer in graph_counts}
#print graph
#print graph_counts
#print walked_graph_counts
#for head in sorted(graph.keys()):
# for tail in sorted(graph[head]):
# print "%s -> %s" % (head, tail)
middles = []
contigs = []
for head in sorted(graph.keys()):
if graph_counts[head]==[1,1]:
middles.append(head)
continue
while walked_graph_counts[head][1]<graph_counts[head][1]:
the_head = head
contig = the_head
while True:
tails = [t for t in graph[the_head] if walked_graph_counts[t][0]<graph_counts[t][0]]
if len(tails)==0:
break
the_tail = tails[0]
walked_graph_counts[the_head][1]+=1
walked_graph_counts[the_tail][0]+=1
contig+=the_tail[-1]
the_head = the_tail
if graph_counts[the_head]!=[1,1]:
break
contigs.append(contig)
for contig in contigs:
print contig
#print middles