-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathregTransitions
More file actions
executable file
·126 lines (97 loc) · 3.75 KB
/
Copy pathregTransitions
File metadata and controls
executable file
·126 lines (97 loc) · 3.75 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/python
import sys
import networkx as nx
import numpy as np
import itertools
from regtools.regnet import *
def getOptions():
import argparse
# create the top-level parser
description = ("Statistics on pangenome scale regulatory networks")
parser = argparse.ArgumentParser(description = description)
parser.add_argument('GML_FILE', action='store',
help='Pangenome regulatory network')
return parser.parse_args()
options = getOptions()
infile = options.GML_FILE
n = nx.read_gml(infile)
# Grep the orgs in the net
orgs = set()
for x in n:
for o in n.node[x]['orgs'].split():
orgs.add(o)
norg = len(orgs)
# Inspect the proportion of conserved and variable regulatory links
regulators = filter(lambda x: n.node[x]['kind'] == 'regulator', n.nodes())
reglinks = filter(lambda x: n[x[0]][x[1]]['kind'] == 'regulated',
n.edges())
class RegTrans(object):
def __init__(self, o1, o2):
self.org1 = o1
self.org2 = o2
self.transitions = {}
self.conserved = 0.
self.plugdiff = 0.
self.plugdiffmad = 0.
# Save some time, precompute some stats
drlinks = {}
for a, b in reglinks:
# Number of orgs in which the regulator, the promoter and the gene are present
reg = set(n.node[a]['orgs'].split())
prom = set(n[a][b]['orgs'].split())
gene = set(n.node[b]['orgs'].split())
# Sanity check: promoter cannot be a superset of genes, only a subset
if prom.issuperset(gene) and not prom.issubset(gene):
raise ValueError('Found a regulator edge with more orgs than the regulated gene (%s --> %s)'%(a, b))
drlinks[a] = drlinks.get(a, {})
drlinks[a][b] = (reg, prom, gene)
z = set()
i = 0
for o1, o2 in itertools.combinations(sorted(orgs), 2):
i += 1
sys.stderr.write('%d - %s - %s\n'%(i, o1, o2))
conserved = 0
trans = {}
for a, b in getRegStatesCombinations():
trans[a] = trans.get(a, {})
trans[a][b] = 0
plugdiffs = []
r = RegTrans(o1, o2)
for a, b in reglinks:
state1 = getRegState(o1, drlinks[a][b][0], drlinks[a][b][1],
drlinks[a][b][2])
state2 = getRegState(o2, drlinks[a][b][0], drlinks[a][b][1],
drlinks[a][b][2])
if state1 == state2:
conserved += 1
if state1 == 'plugged':
# Compare plugs!
p1 = set(getPlug(n, b, o1))
p2 = set(getPlug(n, b, o2))
if len(p1.union(p2)) == 0:
continue
plugdiffs.append(
len(p1.difference(p2).union(p2.difference(p1))) / float(len(p1.union(p2)))
)
else:
st = sorted( [state1, state2] )
trans[st[0]][st[1]] += 1
r.conserved = conserved/float(len(reglinks))
for k, v in trans.iteritems():
r.transitions[k] = {}
for k1, v1 in v.iteritems():
r.transitions[k][k1] = v1/float(len(reglinks))
# data
data = np.array(plugdiffs)
r.plugdiff = data.mean()
r.plugdiffmad = np.median( np.absolute(data - np.median(data)))
z.add(r)
print('#' + '\t'.join( ['Organism 1', 'Organism 2', '# links', 'Conserved']
+ ['%s <==> %s'%(a,b) for a,b in getRegStatesCombinations()] +
['Plug diff', 'Plug diff MAD'] ))
for r in z:
print('\t'.join( [str(x) for x in [r.org1, r.org2,
len(reglinks),
r.conserved] +
[r.transitions[a][b] for a,b in getRegStatesCombinations()]
+ [r.plugdiff, r.plugdiffmad] ]))