forked from jfmalloy1/EncXP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetexp_postprocessing.jl
185 lines (137 loc) · 6.06 KB
/
netexp_postprocessing.jl
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import JSON
function formatted_netexp_results(x::Array{Int,1},t::Array{Int,1},compounds::Array{String,1},reactions::Array{String,1},X::Array{Any,1},Y::Array{Any,1})
data = Dict("stats"=>Dict(),"generations"=>Dict())
## Store data about the scope, about the equilibrium
data["stats"]["scope_compounds"] = compounds
data["stats"]["scope_reactions"] = reactions
data["stats"]["scope_seeds"] = [compounds[i] for i in 1:length(x) if Bool(x[i])]
data["stats"]["scope_targets"] = [compounds[i] for i in 1:length(t) if Bool(t[i])]
# [Int(i in seed_list) for i in compounds]
## Store data by generation
n_generations = length(X)-1 ## Last two generations are repeated for compounds
for gen in 1:n_generations
data["generations"][gen] = Dict()
# data["generations"][gen]["reactions_cumulative"] = []
data["generations"][gen]["compounds_cumulative"] = []
data["generations"][gen]["targets_cumulative"] = []
data["generations"][gen]["reactions_new"] = []
data["generations"][gen]["compounds_new"] = []
data["generations"][gen]["targets_new"] = []
## Add cumulative reaction list for each generation
data["generations"][gen]["reactions_cumulative"] = [reactions[i] for i in 1:length(reactions) if Bool(Y[gen][i])]
## Alternative way to write
# for (i,r) in enumerate(reactions)
# if Y[gen][i]==1
# push!(data["generations"][gen]["reactions_cumulative"],reactions[i])
# end
# end
## Add cumulative compound and target list for each generation
# data["generations"][gen]["compounds_cumulative"] = [compounds[i] for i in 1:length(compounds) if Bool(X[gen][i])]
for (i,c) in enumerate(compounds)
if X[gen][i]==1
push!(data["generations"][gen]["compounds_cumulative"],compounds[i])
if t[i]==1
push!(data["generations"][gen]["targets_cumulative"],compounds[i])
end
end
end
## Store new reactions and compounds
if gen==1
data["generations"][gen]["reactions_new"] = collect(Set(data["generations"][gen]["reactions_cumulative"]))
data["generations"][gen]["compounds_new"] = collect(Set(data["generations"][gen]["compounds_cumulative"]))
data["generations"][gen]["targets_new"] = collect(Set(data["generations"][gen]["targets_cumulative"]))
end
if gen!=1
data["generations"][gen]["reactions_new"] = setdiff(data["generations"][gen]["reactions_cumulative"], data["generations"][gen-1]["reactions_cumulative"])
data["generations"][gen]["compounds_new"] = setdiff(data["generations"][gen]["compounds_cumulative"], data["generations"][gen-1]["compounds_cumulative"])
data["generations"][gen]["targets_new"] = setdiff(data["generations"][gen]["targets_cumulative"], data["generations"][gen-1]["targets_cumulative"])
end
end
data
end
function format_many(DATADIR::String,OUTDIR::String)
for FNAME in readdir(DATADIR)
FULLINPATH = DATADIR*FNAME
FULLOUTPATH = OUTDIR*FNAME
D = JSON.parsefile(FULLINPATH)
x = Array{Int,1}(D["x"])
t = Array{Int,1}(D["t"])
compounds = Array{String,1}(D["compounds"])
reactions = Array{String,1}(D["reactions"])
X = Array{Any,1}(D["X"])
Y = Array{Any,1}(D["Y"])
newdata = formatted_netexp_results(x,t,compounds,reactions,X,Y)
## Write out
open(FULLOUTPATH,"w") do f
JSON.print(f, newdata)
end
end
end
function format_many_nested(DOMAINDIR::String,OUTDIR::String)
for orgdir in readdir(DOMAINDIR)
if !ispath(joinpath(OUTDIR,orgdir))
mkpath(joinpath(OUTDIR,orgdir))
end
for seedname in readdir(joinpath(DOMAINDIR,orgdir))
seedpath_in = joinpath(DOMAINDIR,orgdir,seedname)
seedpath_out = joinpath(OUTDIR,orgdir,seedname)
if isfile(seedpath_in) && (last(splitext(seedpath_in)) == ".json")
D = JSON.parsefile(seedpath_in)
x = Array{Int,1}(D["x"])
t = Array{Int,1}(D["t"])
compounds = Array{String,1}(D["compounds"])
reactions = Array{String,1}(D["reactions"])
X = Array{Any,1}(D["X"])
Y = Array{Any,1}(D["Y"])
newdata = formatted_netexp_results(x,t,compounds,reactions,X,Y)
## Write out
open(seedpath_out,"w") do f
JSON.print(f, newdata)
end
end
end
end
end
#########################
### FORMAT MANY NESTED FILES
#########################
# for domain in ["bacteria"]
# DATADIR = "results/simple/min_seeds_partial/"*domain*"/"
# OUTDIR = "results/formatted/min_seeds_partial/"*domain*"/"
# format_many_nested(DATADIR,OUTDIR)
# end
#########################
### FORMAT MANY FILES
#########################
# const DATADIR = "results/simple/min_seeds_partial/archaea/2506520044/"
# # fsplit = split(DATADIR,"/")
# # OUTDIR = "results/formatted/"*fsplit[end-2]*"/"*fsplit[end-1]*"/"
# const OUTDIR = "results/formatted/min_seeds_partial/archaea/2506520044/"
# if !ispath(OUTDIR)
# mkpath(OUTDIR)
# end
# format_many(DATADIR,OUTDIR)
#########################
### FORMAT SINGLE FILE
#########################
fullinpath = "results/simple/kegg_edge_json_P/reaction_edges_0.json"
# fullinpath = path*"data_glucose_test.json"
D = JSON.parsefile(fullinpath)
x = Array{Int,1}(D["x"])
t = Array{Int,1}(D["t"])
compounds = Array{String,1}(D["compounds"])
reactions = Array{String,1}(D["reactions"])
X = Array{Any,1}(D["X"])
Y = Array{Any,1}(D["Y"])
newdata = formatted_netexp_results(x,t,compounds,reactions,X,Y)
fsplit = split(fullinpath,"/")
# outpath = "results/formatted/"*fsplit[end-2]*"/"*fsplit[end-1]*"/"
outpath = "results/formatted/kegg_edge_json_P/"
if !ispath(outpath)
mkpath(outpath)
end
fulloutpath = outpath*"reaction_edges_0.json"
## Write out
open(fulloutpath,"w") do f
JSON.print(f, newdata)
end