-
Notifications
You must be signed in to change notification settings - Fork 15
/
.monet
executable file
·461 lines (420 loc) · 14.7 KB
/
.monet
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#! /bin/bash -
# Copyright 2018 Bergmann's Lab UNIL <[email protected]>
#
# This file is part of MONET.
#
# MONET 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.
#
# MONET 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 MONET. If not, see <https://www.gnu.org/licenses/>.
#
###############################################################################
# Mattia Tomasoni - UNIL, CBG
# 2017 DREAM challenge on Disease Module Identification
# https://www.synapse.org/modulechallenge
################################################################################
# K1: Team Tusk
# M1: Team Aleph
# R1: Team Causality
#prevent execution of MONET's main script directly (i.e. without installation)
working_dir=$PWD
install_dir="$HOME/.monet"
current_script="$0"
if [[ $current_script != *".monet"* ]]; then
echo "ERROR: the MONET executable is not intended to be invoked directly."
echo " (refer to the INSTRUCTIONS and README file for assistance)"
echo " PLEASE invoke the install script."
exit 1
fi
###################################################################################################
# HELP USAGE MESSAGE
###################################################################################################
usage() {
echo "USAGE: monet --input=[...] --method=[...] --container=[...] [OPTIONS]"
echo "Run DREAM challenge Disease Module Identification methods"
echo ""
echo " MANDATORY:"
echo " --input= path to the network file to be analysed"
echo " --method= method to be used to analyse the input: [K1|M1|R1]"
echo " --container= container type: [docker|singularity]"
echo ""
echo " OPTIONAL:"
echo " --output= directory in which to output results (default is current directory)"
echo ""
echo " options for method K1"
echo " --nclusters= number of output clusters for spectral clustering step (default is 100)"
echo ""
echo " options for method M1"
echo " --linksdir= directionality of links: [undirected|directed] (default is undirected)"
echo " --avgk= desired average degree for nodes in output (default is 25)"
echo " --smallest= min size of output clusters (default is 3)"
echo " --largest= max size of output clusters (default is 100)"
echo ""
echo " options for method R1"
echo " --c= trade-off parameter for computational efficiency (default is 800)"
echo " --i= inflation parameter for standard Markov Clustering (default is 2)"
echo " --b= controlling how balanced the clustering results should be (default is 2)"
echo " --threshold= remove edges smaller than threshold from the input (default is 4)"
echo " --smallest= min size of output clusters (default is 3)"
echo " --largest= max size of output clusters (default is 100)"
echo " --post= handling too large output clusters: [recluster|discard] (def is discard)"
echo " --c2= set —-c for reclustering round (default is 500)"
echo " --i2= set —-i for reclustering round (default is 2)"
echo " --b2= set —-b for reclustering round (default is 2)"
echo ""
echo "EXAMPLE:"
echo "monet --input=/tmp/input.txt --method=K1 --container=docker"
}
###################################################################################################
# PARAMETERS
###################################################################################################
# DEFAULTS
input="not_set"
output="not_set"
method="not_set"
container="not_set"
nclusters=100
linksdir=undirected
avgk=25.0
b=2
c=800
i=2
filter=quantile
threshold=4
interWeight=yes
weighted=T
dir=result
post=discard
smallest=3
largest=100
b2=2
c2=500
i2=2
# READ USER INPUT #################################################################################
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case $PARAM in
-h | --help)
usage
exit
;;
--input)
input=$VALUE
;;
--output)
output=$VALUE
;;
--method)
method=$VALUE
;;
--container)
container=$VALUE
;;
--nclusters)
nclusters=$VALUE
;;
--linksdir)
linksdir=$VALUE
;;
--avgk)
avgk=$VALUE
;;
--b)
b=$VALUE
;;
--c)
c=$VALUE
;;
--i)
i=$VALUE
;;
--filter)
filter=$VALUE
;;
--threshold)
threshold=$VALUE
;;
--post)
post=$VALUE
;;
--smallest)
smallest=$VALUE
;;
--largest)
largest=$VALUE
;;
--b2)
b2=$VALUE
;;
--c2)
c2=$VALUE
;;
--i2)
i2=$VALUE
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
echo ""
usage
exit 1
;;
esac
shift
done
# HANDLE MANDATORY AND DEFAULT PARAMETERS #########################################################
if [ "$method" != "K1" -a "$method" != "M1" -a "$method" != "R1" ]; then
echo "ERROR: you must specify a method (--method option)"; echo ""
usage
exit 1
fi
if [ "$input" != "not_set" ]; then
if [ ! -f "$input" ]; then
echo "ERROR: the input file $input does not exist"
exit 1
else
# copy input network file to designated input location
if [ "$method" = "K1" ]; then
cp $input "$install_dir/src/K1_code/data/input_network.txt"; fi
if [ "$method" = "M1" ]; then
cp $input "$install_dir/src/M1_code/sub-challenge1/input.txt"; fi
if [ "$method" = "R1" ]; then
cp $input "$install_dir/src/R1_code/sub-challenge1/input.txt"; fi
fi
else
echo "ERROR: you must provide an input file (--input option)"; echo ""
usage
exit 1
fi
if [ "$output" != "not_set" ]; then
# create dir if it does not exist
if [ ! -d "$output" ]; then
mkdir $output
fi
# remove trailing slash if present
if [[ $output == */ ]]; then
#output=${output::-1}
output=${output::${#output}-1}
fi
else
output=$working_dir
fi
if [ "$container" != "docker" -a "$container" != "singularity" ]; then
echo "ERROR: you must specify a container type (--container option)"; echo ""
usage
exit 1
fi
if [ "$container" = "docker" ]; then
docker --help > /tmp/docker_test 2>&1
if [ $? -eq "0" ]; then docker_installed=true; else docker_installed=false; fi
if ! $docker_installed; then
echo "ERROR: you specified --container=docker, but docker is not installed"
echo "Please visit https://www.docker.com"; echo ""
exit 1
fi
fi
if [ "$container" = "singularity" ]; then
singularity --help > /tmp/singularity_test 2>&1
if [ $? -eq "0" ]; then singularity_installed=true; else singularity_installed=false; fi
if ! $singularity_installed; then
echo "ERROR: you specified --container=singularity, but singularity is not installed"
echo "Please visit http://singularity.lbl.gov"; echo ""
exit 1
fi
fi
# WRITE PARAMETERS TO FILE ########################################################################
if [ "$method" = "K1" ]
then
addToParametersFile() {
echo "$1"="$2" >> "$install_dir/src/K1_code/runTusk_parameters.txt"
}
rm -f "$install_dir/src/K1_code/runTusk_parameters.txt"
touch "$install_dir/src/K1_code/runTusk_parameters.txt"
addToParametersFile nclusters "$nclusters"
fi
if [ "$method" = "M1" ]
then
addToParametersFile() {
echo "$1"="$2" >> "$install_dir/src/M1_code/runAleph_parameters.txt"
}
rm -f "$install_dir/src/M1_code/runAleph_parameters.txt"
touch "$install_dir/src/M1_code/runAleph_parameters.txt"
addToParametersFile linksdir "$linksdir"
addToParametersFile avgk "$avgk"
addToParametersFile smallest "$smallest"
addToParametersFile largest "$largest"
fi
if [ "$method" = "R1" ]
then
addToParametersFile() {
echo "$1"="$2" >> "$install_dir/src/R1_code/runCausality_parameters.txt"
}
rm -f "$install_dir/src/R1_code/runCausality_parameters.txt"
touch "$install_dir/src/R1_code/runCausality_parameters.txt"
addToParametersFile filename "input.txt"
addToParametersFile b "$b"
addToParametersFile c "$c"
addToParametersFile i "$i"
addToParametersFile filter "$filter"
addToParametersFile threshold "$threshold"
addToParametersFile interWeight "$interWeight"
addToParametersFile weighted "$weighted"
addToParametersFile dir "$dir"
addToParametersFile post "$post"
addToParametersFile smallest "$smallest"
addToParametersFile largest "$largest"
addToParametersFile b2 "$b2"
addToParametersFile c2 "$c2"
addToParametersFile i2 "$i2"
fi
###################################################################################################
# INVOKE DOCKER/SINGULARITY TO RUN THE CHOSEN METHOD
###################################################################################################
getDockerReferenceName() {
if [ $method = "K1" ]; then
echo "k1-image"; fi
if [ $method = "M1" ]; then
echo "m1-image"; fi
if [ $method = "R1" ]; then
echo "r1-image"; fi
}
checkDockerBuildSuccess() {
new_message="naming to docker.io/library"
if grep -q "$new_message" /tmp/docker_build_output; then
new_message_present=true
echo 1
else
new_message_present=false
fi
old_message="Successfully built"
if grep -q "$old_message" /tmp/docker_build_output; then
old_message_present=true
else
old_message_present=false
fi
if ! $new_message_present && ! $old_message_present; then
echo "ERROR: docker build returned an error"
echo " - is an internet connection available?"
echo " - is the docker daemon running?"
echo "see /tmp/docker_build_output for details"
exit 1
fi
}
checkSingularityBuildSuccess() {
if ! grep -q "Finalizing Singularity container" /tmp/singularity_build_output; then
echo "ERROR: singularity build returned an error"
echo " - is an internet connection available?"
echo " - is singularity running?"
echo "see /tmp/singularity_build_output for details"
rm -f "$install_dir/containers/"$method"/singularity/"$method"-image.img"
exit 1
fi
}
# take initial timestamp
begin=$(date +%s)
timestamp=`date '+%Y-%m-%d-%H%M%S'`
# increase stack memory limit
default_ulimit=$(ulimit -s)
if [ "$method" = "K1" ]; then
ulimit -s 32768 > /dev/null 2>&1
else
ulimit -s 16384 > /dev/null 2>&1
fi
# RUN THE METHOD ##################################################################################
echo "" && echo "--------------------------------------------------------------------------------"
echo
echo "MONET: MOdularising NEtwork Toolbox"
echo " for mining of molecular and genetic networks"
echo " (DREAM challenge Disease Module Identification)" && echo ""
echo ""
echo "Method: $method"
network_name=$(basename $input)
echo "Input: $network_name"
echo "Container: $container"
if [ "$container" = "singularity" ]
then
# build the singularity image (if image already exists, skip this step)
echo "Preparing container, please wait..."
if [ ! -f "$install_dir"/containers/"$method"/singularity/"$method"-image.img ]; then
cd "$install_dir"/containers/"$method"/singularity/
singularity image.create --size 1500 ./$method-image.img > \
/tmp/singularity_create_output 2>&1
singularity build ./$method-image.img Singularity > \
/tmp/singularity_build_output 2>&1
cd ../../..
checkSingularityBuildSuccess
cd $working_dir
fi
# run container
echo "Running container, please wait..."
singularity run \
-B "$install_dir"/src/"$method"_code:/"$method"_code/ \
"$install_dir"/containers/"$method"/singularity/"$method"-image.img > \
"$output"/"$timestamp"__"$method"__console-output__"$network_name" 2>&1
fi
if [ "$container" = "docker" ]
then
# build the docker image (if image already exists, this command does nothing)
echo "Preparing container, please wait..."
docker_reference=$(getDockerReferenceName)
sudo docker build -t $docker_reference "$install_dir"/containers/"$method"/docker \
> /tmp/docker_build_output 2>&1
# This function may return error in MAC OSX docker sicen there is no "Successfully built" line int he docker output file...!
checkDockerBuildSuccess
# remove (old) container (if exists)
sudo docker rm "$method"-container > /tmp/docker_rm_output 2>&1
# run (new) container
echo "Running container, please wait..."
sudo docker run --name "$method"-container \
-v "$install_dir"/src/"$method"_code:/"$method"_code/ "$docker_reference" > \
"$output"/"$timestamp"__"$method"__console-output__"$network_name" 2>&1
fi
ulimit -s $default_ulimit
end=$(date +%s) # calculate execution time
tottime=$(expr $end - $begin)
echo "" && echo "Exectution took $tottime seconds"
# PREPARE OUTPUT AND CLEAN UP TEMP FILES ##########################################################
output_file="$output"/"$timestamp"__"$method"__result-modules__"$network_name"
if [ "$method" = "K1" ]; then
rm -f "$install_dir"/src/K1_code/data/DSD/* # remove temporary files
rm "$install_dir"/src/K1_code/data/input_network.txt;
result_file="$install_dir"/src/K1_code/data/final_clusters/clusters.txt
if [ ! -f $result_file ] || [ ! -s $result_file ]; then
# if result file not present or empty
echo ERROR: see "$output"/"$timestamp"__"$method"__console-output__"$network_name"
echo please refer to section TROUBLESHOOTING in the README
else
mv "$install_dir"/src/K1_code/data/final_clusters/clusters.txt $output_file
echo "DONE: output can be found in $output"
fi
fi
if [ "$method" = "M1" ]; then
rm "$install_dir"/src/M1_code/sub-challenge1/input.txt
result_file="$install_dir"/src/M1_code/sub-challenge1/output.txt
if [ ! -f $result_file ] || [ ! -s $result_file ]; then
echo ERROR: see "$output"/"$timestamp"__"$method"__console-output__"$network_name"
echo please refer to section TROUBLESHOOTING in the README
else
mv "$install_dir"/src/M1_code/sub-challenge1/output.txt $output_file
echo "DONE: output can be found in $output"
fi
fi
if [ "$method" = "R1" ]; then
rm "$install_dir"/src/R1_code/sub-challenge1/input.txt
result_file="$install_dir"/src/R1_code/sub-challenge1/result/input.txt
if [ ! -f $result_file ] || [ ! -s $result_file ]; then
echo ERROR: see "$output"/"$timestamp"__"$method"__console-output__"$network_name"
echo please refer to section TROUBLESHOOTING in the README
else
mv "$install_dir"/src/R1_code/sub-challenge1/result/input.txt $output_file
echo "DONE: output can be found in $output"
fi
fi