-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwgcna_network.py
155 lines (149 loc) · 6.74 KB
/
wgcna_network.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import subprocess
import argparse
import glob
import json
import os
import pandas as pd
import numpy as np
# get arguments
parser = argparse.ArgumentParser(description="wgcna step4: get network data")
parser.add_argument('-module', type=str, metavar="target_module", required=True, help="such as: blue,red")
parser.add_argument('-threshold', type=float, metavar="weight_threshold, default 0.02", default=0.02)
parser.add_argument('-top', type=int, metavar="nTop_hubGenes", default=30)
parser.add_argument('-geneid2symbol', type=str, metavar="symbol", required=True,
help='no header, gene gene_symbol')
parser.add_argument('-step3output', type=str, metavar="output_dir", required=True,
help="NeedFiles: gene_trait.correlation.xls")
parser.add_argument('-step2output', type=str, metavar="output_dir", required=True,
help="NeedFiles: TOM-blockwise-block.*.RData, seq_id2gene_name.txt, "
"blockwiseModules_result.RData, membership.xls")
args = parser.parse_args()
tom_files = glob.glob(args.step2output+"/TOM-blockwise-block.*.RData")
bwm_result = glob.glob(args.step2output+"/blockwiseModules_result.RData")[0]
gene_annot = args.geneid2symbol
if args.module.startswith('ME'):
args.module = args.module[2:]
if not tom_files:
raise Exception("No TOM files found")
target_modules = args.module.strip().split(",")
# format R scripts
r_cmd = ["""
library("WGCNA")
load('{bwnet}')
moduleColors = membership[, 2];
probes = membership[, 1];
annot = read.table('{gene_annot}', header=F, sep='\\t');
""".format(gene_annot=gene_annot, bwnet=bwm_result, )
]
for tom_file in tom_files:
block_id = os.path.basename(tom_file).split(".")[-2]
r_cmd.append(
"""
# ---export a block network---
load('{tom_file}');
blockTOM = as.matrix(TOM);
blockModuleColors = moduleColors[bwnet$blockGenes[[{block_id}]]];
inModule = is.finite(match(blockModuleColors, c({modules})));
if (! all(inModule == FALSE)){bracket1}
blockProbes = probes[bwnet$blockGenes[[{block_id}]]];
modTOM = blockTOM[inModule, inModule];
modProbes = blockProbes[inModule];
write.table(modProbes, 'target.modules.genes', col.names=F, quote=F, sep='\\t', row.names=F)
dimnames(modTOM) = list(modProbes, modProbes);
modGenes = annot[,2][match(modProbes, annot[,1])];
modColors = blockModuleColors[inModule];
nodeAttrs = modColors;
# get top n hub genes
IMConn = softConnectivity(datExpr[, modProbes]);
top = (rank(-IMConn) <= {top})
modTOM = modTOM[top, top]
modProbes = modProbes[top]
modGenes = modGenes[top]
modColors = modColors[top]
nodeAttrs = cbind(modColors, IMConn[top])
colnames(nodeAttrs) = c('module', 'intraModuleConnectivity')
# to cytoscape
cyt = exportNetworkToCytoscape(modTOM,
edgeFile = "b{block_id}_edges.txt",
nodeFile = "b{block_id}_nodes.txt",
weighted = TRUE,
threshold = {threshold},
nodeNames = modProbes,
altNodeNames = modGenes,
nodeAttr = nodeAttrs);
{bracket2}
""".format(
tom_file=tom_file,
block_id=block_id,
modules=",".join("'"+x+"'" for x in target_modules),
threshold=args.threshold,
top=args.top,
bracket1="{",
bracket2="}"
))
else:
r_file = 'wgcna_network.r'
with open(r_file, 'w') as f:
f.write('\n'.join(r_cmd))
subprocess.check_call("Rscript {}".format(r_file), shell=True)
# merge network
all_edges = glob.glob("b*_edges.txt")
all_nodes = glob.glob("b*_nodes.txt")
# edge
links = list()
for each in all_edges:
edge_pd = pd.read_table(each, header=0)
# "fromNode toNode weight direction fromAltName toAltName"
if edge_pd.shape[0] <= 2:
continue
edge_pd.columns = ['source', 'target', 'weight', 'direction', 'fromAltName', 'toAltName']
links.extend(edge_pd.to_dict("records"))
if links:
# exit("Network is Empty! Stop!")
# node
gene_trait_corr = glob.glob(args.step3output+"/gene_trait.correlation.xls")[0]
gene_trait_corr_pd = pd.read_table(gene_trait_corr, header=0, index_col=0)
gene_trait_corr_pd.columns = [x + "_corr" for x in gene_trait_corr_pd.columns]
gene_trait_corr_pd.index.name = "seq_id"
gene_module_corr = glob.glob(args.step2output+"/membership.xls")[0]
gene_module_corr_pd = pd.read_table(gene_module_corr, header=0, index_col=0).loc[:, ["kme"]]
gene_module_corr_pd.index.name = "seq_id"
gene_annot_pd = pd.read_table(gene_annot, header=0, index_col=0)
if 'gene_id' in gene_annot_pd.columns:
gene_annot_pd = gene_annot_pd.loc[:, ["gene_id"]]
else:
gene_annot_pd.index.name = 'seq_id'
gene_annot_pd['gene_id'] = gene_annot_pd.index
gene_annot_pd = gene_annot_pd.loc[:, ["gene_id"]]
nodes = list()
for each in all_nodes:
node_pd = pd.read_table(each, header=0, index_col=0)
node_pd.columns = ['name', 'module', 'connectivity']
node_pd.index.name = "seq_id"
node_pd = node_pd.join(gene_trait_corr_pd.round(4), how="left")
node_pd = node_pd.join(gene_module_corr_pd.round(4), how="left")
if 'gene_id' not in list(node_pd.columns):
node_pd = node_pd.join(gene_annot_pd, how="left")
node_pd.index.name = "id"
node_pd.reset_index(inplace=True)
# nodeName altName nodeAttr.nodesPresent...
nodes.extend(node_pd.to_dict("records"))
# write out network for cytoscape
link_order = ['source', 'target', 'weight', 'direction', 'fromAltName', 'toAltName']
final_link_pd = pd.DataFrame(links).loc[:, link_order]
final_link_pd.to_csv("_".join(target_modules)+".network.edges.txt", header=True, index=False, sep='\t')
final_node_pd = pd.DataFrame(nodes)
node_order = ["id", "name", "connectivity", "gene_id", "kme", "module"] + list(set(final_node_pd.columns) - {"id","gene_id", "name","kme","module","connectivity"})
final_node_pd.loc[:, node_order].to_csv("_".join(target_modules)+".network.nodes.txt", header=True, index=False, sep='\t')
# write out network for mj sanger
with open("_".join(target_modules)+".network.json", "w") as f:
tmp_nodes = final_node_pd.loc[:, ["id", "name", "module"]]
tmp_nodes.columns = ["id", "name", "group"]
index_node_pd = tmp_nodes.loc[:, ["id"]].reset_index().set_index("id")
tmp_links = final_link_pd.loc[:, ["source", "target", "weight"]].round(4)
tmp_links.columns = ["source", "target", "distance"]
tmp_links = tmp_links.join(index_node_pd, on="source")
tmp_links = tmp_links.join(index_node_pd, on="target", lsuffix="_source", rsuffix="_target")
tmp_links = tmp_links.loc[:, ["index_source", "index_target", "distance"]]
tmp_links.columns = ["source", "target", "distance"]
json.dump(dict(links=json.loads(tmp_links.to_json(orient="records")), nodes=tmp_nodes.to_dict("records")), f)