-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_cube_sampling_on_dataset.jl
150 lines (136 loc) · 4.99 KB
/
run_cube_sampling_on_dataset.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
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Copyright 2021-2022, Ben Cardoen
using ArgParse, SPECHT, SubPrecisionContactDetection, Images, CSV, Statistics, DataFrames, Glob, ERGO
### CLI tool to run the sampled statistics on the contacts.
function selectct(cts, selector)
for ct in cts
if occursin(selector, ct)
return ct
end
end
@assert false
end
function extract_stats(tiffs, replicate, celltype, serienr, config)
mito = tiffs[(celltype, serienr, replicate)]["mito"]
contacts = tiffs[(celltype, serienr, replicate)]["contacts"]
contact = selectct(contacts,"gradient")
@info "Loading images"
mimg, cimg = [Images.load(i) for i in [mito, contact]]
@info "Filtering"
out, ms, ls = filtermito(mimg, cimg, config["ls"], config["rmv"])
zs = config["zs"]
@info "Walking $(5*zs) x $(5*zs) x $zs over mito / ct"
OM = ERGO.tomask(out)
res = walk_cube(out, OM.*cimg, ERGO.aszero(cimg), zs*5, zs*5, zs, 1)
df, me, ce = res
nzdf = filter(:mtsurface => !iszero, df) # new data frame
return me, ce, ms, ls, nzdf, out, mimg, cimg.*OM
end
function parse_commandline()
s = ArgParseSettings()
@add_arg_table! s begin
"--cube-vesicle-size-ln"
help = "The cube sampling analysis drops any contact adjacent to a mitochondria of size (ln) <= 9"
arg_type = Int
default = 9
required = false
"--cube-vesicle-sample-size"
help = "The cube sampling analysis window size = 5*K x 5*K x K, with default K=5"
arg_type = Int
default = 5
required = false
"--cube-vesicle-intensity-mean"
help = "The cube sampling analysis drops any contact adjacent to a mitochondria of mean intensity <= 0.2"
arg_type = Float64
default = 0.2
required = false
"--outpath", "-o"
help = "output folder"
arg_type = String
required = true
"--inpath", "-i"
help = "input folder"
arg_type = String
required = true
end
return parse_args(s)
end
function run()
@debug "Todo process both channels, not just mito."
parsed_args = parse_commandline()
inpath = parsed_args["inpath"]
outpath = parsed_args["outpath"]
cubesize= parsed_args["cube-vesicle-sample-size"]
z=3
alpha=0.05
zc=cubesize
vesiclesizeln= parsed_args["cube-vesicle-size-ln"]
vesicleint = parsed_args["cube-vesicle-intensity-mean"]
tiffs = Dict()
for replicate in readdir(inpath; join=true)
r = basename(replicate)
for celltype in readdir(replicate; join=true)
ct = basename(celltype)
for cell in readdir(celltype; join=true)
snr = basename(cell)
for alphaval in readdir(cell; join=true)
_alpha = basename(alphaval)
ai = tryparse(Float64, _alpha)
if ai != alpha
@warn "Skpping $ai"
end
js = Glob.glob("parameters.json")
if length(js) == 1
@info "Found parameter file, extracting..."
meta = read_meta(js)
@info "Alpha stored = $(meta["alpha"])"
if meta["alpha"] != alpha
@warn "Stored alpha does not match!!!"
end
end
c1 = Glob.glob("*channel_1.tif", alphaval)[1]
c2 = Glob.glob("*channel_2.tif", alphaval)[1]
cts = Glob.glob("*pre_split_*.tif", alphaval)
@info "Replicate $r Cell $ct Serie $snr alpha=$ai"
tiffs[(ct, snr, r)] = Dict("mito" => c1, "er" => c2, "contacts" =>cts)
end
end
end
end
@info "Have a total of $(length(keys(tiffs)|>collect)) cells"
config = Dict("ls"=>vesiclesizeln, "rmv"=>vesicleint, "zs"=>cubesize)
RD = []
for (c, s, r) in keys(tiffs)
@info "Cell $c SNR $(s) Rep $(r)"
est = extract_stats(tiffs, r, c, s, config)
resultshts1 = est[5]
filtered_contacts, filtered_mito = est[8], est[6]
@info "Saving tifs"
Images.save(joinpath(outpath, "filtered_mito_celltype_$(c)_replicate_$(r)_serienr_$(s).tif"), filtered_mito)
Images.save(joinpath(outpath, "filtered_contacts_celltype_$(c)_replicate_$(r)_serienr_$(s).tif"), filtered_contacts)
resultshts1[!, "alpha"] .= alpha
resultshts1[!, "z"] .= z
resultshts1[!, "cube_z"] .= zc
resultshts1[!, "celltype"] .= c
resultshts1[!, "replicate"] .= r
resultshts1[!, "serienr"] .= s
push!(RD, resultshts1)
end
@info "Saving DF"
DF = vcat(RD...)
CSV.write(joinpath(outpath, "all.csv"), DF)
@info "Done"
## Save
end
run()