-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.ld.groups.py
67 lines (55 loc) · 1.22 KB
/
make.ld.groups.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
import sys
import networkx
from networkx.algorithms.components.connected import connected_components
def to_graph(l):
G = networkx.Graph()
for part in l:
# each sublist is a bunch of nodes
G.add_nodes_from(part)
# it also imlies a number of edges:
G.add_edges_from(to_edges(part))
return G
def to_edges(l):
"""
treat `l` as a Graph and returns it's edges
to_edges(['a','b','c','d']) -> [(a,b), (b,c),(c,d)]
"""
it = iter(l)
last = next(it)
for current in it:
yield last, current
last = current
snpDict={}
ldDict={}
title=sys.argv[2]
for line in open(sys.argv[1]):
line=line.rstrip().split("\t")
lds=line[4].split(",")
if lds == ["Lead"] and line[3] not in ldDict:
ldDict[line[3]]=[]
else:
for ld in lds:
if ld == "Lead":
pass
else:
if line[3] in snpDict:
snpDict[line[3]].append(ld)
else:
snpDict[line[3]]=[ld]
if ld in ldDict:
ldDict[ld].append(line[3])
else:
ldDict[ld]=[line[3]]
i=1
k=[]
for entry in ldDict:
name=title+str(i)
m=ldDict[entry]+[entry]
k.append(m)
G = to_graph(k)
g=connected_components(G)
for entry in g:
name=title+str(i)
for x in entry:
print x, "\t", name
i+=1