-
Notifications
You must be signed in to change notification settings - Fork 1
/
macs2hubber.sh
605 lines (432 loc) · 15 KB
/
macs2hubber.sh
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#!/bin/bash
##########################################################################
# Copyright 2017, Jelena Telenius (jelena.telenius@imm.ox.ac.uk) #
# #
# This file is part of macs2hubber . #
# #
# macs2hubber is free software: you can redistribute it and/or modify #
# it under the terms of the MIT license.
#
#
# #
# macs2hubber 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 #
# MIT license for more details.
# #
# You should have received a copy of the MIT license
# along with macs2hubber.
##########################################################################
function finish {
# Your cleanup code here
cd ${weAreHere}
rm -f versionInfoHTML.txt
rm -f versionInfo.txt
}
trap finish EXIT
printRunStartArraysBam(){
for k in $( seq 0 $((${#samplenames[@]} - 1)) ); do
echo "samplenames[$k] ${samplenames[$k]}"
done
echo
for k in $( seq 0 $((${#foldernames[@]} - 1)) ); do
echo "foldernames[$k] ${foldernames[$k]}"
done
echo
for k in $( seq 0 $((${#samplefiles[@]} - 1)) ); do
echo "samplefiles[$k] ${samplefiles[$k]}"
done
echo
if [ "${weHaveControl}" -eq 1 ]; then
for k in $( seq 0 $((${#controlfiles[@]} - 1)) ); do
echo "controlfiles[$k] ${controlfiles[$k]}"
done
echo
fi
}
# ------------------------------------------
GENOME="UNDEFINED"
timestamp=$( date +%d%b%Y_%H_%M )
MagicNumber=$$
#------------------------------------------
# For making sure we know where we are ..
weAreHere=$( pwd )
#------------------------------------------
# Help requests ..
if [ $# -eq 1 ];then
if [ $@ == "-h" ] || [ $@ == "--help" ];then
PipeTopPath="$( which $0 | sed 's/\/macs2hubber.sh$//' )"
BashHelpersPath="${PipeTopPath}/bin/bashHelpers"
. ${BashHelpersPath}/usageAndVersion.sh
usage
exit 0
fi
fi
#------------------------------------------
echo
echo "macs2hubber.sh - by Jelena Telenius, 14/02/2017"
echo
timepoint=$( date )
echo "run started : ${timepoint}"
echo
echo "Script located at"
which $0
echo
parameterList=$@
# MACS2 parameters state a parsing problem, because :
# 1) we don't want to read them in with GETOPTS (bash parameter reader), as supporting "all macs2 parameters" is easier if we don't specify all of them
# 2) parsing stuff with -- or - in the beginning and - in the middle of the name (--shift-control) are difficult, if the argument is the first in $@ list (there is no whitespace before the argument)
# 3) we still need to support the normal parameter rules : if same parameter is given multiple times accidentally, the last time the parameter is there, will be the one which "overwrites" the previous settings.
# We are using "echo" to parse these.
# Thus - the flags "echo" has are the problem.
# It has these :
# -n do not output the trailing newline
# -e enable interpretation of backslash escapes
# -E disable interpretation of backslash escapes (default)
# --help display this help and exit
# --version output version information and exit
# So, the first task is to see that the first parameter is not one of these (--help is already dealt with above)
# Macs2 does not have flag -e -E , but it has --version and -n
# And, of course, we are actually potentially using -n
# The only problem is if we have these as the FIRST parameter.
# So we SHIELD our parameter list by writing something to the start :
# We start by adding \s to the beginning of the list (to differentiate between - in the middle of flag (--shift-control) and in the beginning of it ..)
parameterList="PARAMETERS ${parameterList} END"
#------------------------------------------
# Loading subroutines in ..
PipeTopPath="$( which $0 | sed 's/\/macs2hubber.sh$//' )"
BashHelpersPath="${PipeTopPath}/bin/bashHelpers"
# PerlHelpersPath="${PipeTopPath}/perlHelpers"
# PythonHelpersPath="${PipeTopPath}/pythonHelpers"
# RHelpersPath="${PipeTopPath}/rHelpers"
# USAGE AND VERSION
. ${BashHelpersPath}/usageAndVersion.sh
# READ THE PARAMETER FILES IN
. ${BashHelpersPath}/readParameters.sh
# TEST THE FILE EXISTENCE AND INTEGRITY
. ${BashHelpersPath}/fileTesters.sh
# PRINT TO LOG FILES
. ${BashHelpersPath}/logFilePrinter.sh
# RUNNING THE ACTUAL ANALYSIS
. ${BashHelpersPath}/runscript.sh
# PRINTING HELP AND VERSION MESSAGES
. ${BashHelpersPath}/usageAndVersion.sh
#------------------------------------------
version
echo -e ${versionInfo}
echo "RUNNING IN MACHINE : "
hostname --long
echo
echo "run called with parameters :"
echo "macs2hubber.sh" $@
echo
echo "running in folder :"
pwd
echo
#------------------------------------------
# From where to call the CONFIGURATION script..
confFolder="${PipeTopPath}/conf"
supportedGenomes=()
UCSC=()
genomeBuild=""
. ${confFolder}/loadNeededTools.sh
. ${confFolder}/genomeBuildSetup.sh
. ${confFolder}/serverAddressAndPublicDiskSetup.sh
setGenomeLocations
echo
echo "Supported genomes : "
for g in $( seq 0 $((${#supportedGenomes[@]}-1)) ); do echo -n "${supportedGenomes[$g]} "; done
echo
echo
#------------------------------------------
# From where to call the main scripts operating from the mainScripts folder..
MainScriptsPath="${PipeTopPath}/bin/mainScripts"
#------------------------------------------
echo
echo "PipeTopPath ${PipeTopPath}"
echo "BashHelpersPath ${BashHelpersPath}"
echo "MainScriptsPath ${MainScriptsPath}"
echo "confFolder ${confFolder}"
# echo "PerlHelpersPath ${PerlHelpersPath}"
# echo "PythonHelpersPath ${PythonHelpersPath}"
# echo "RHelpersPath ${RHelpersPath}"
echo
#---------------------------------------------
if [ ! -s PIPE_hubbing.txt ] && [ ! -s PIPE_hubbingSymbolic.txt ] ; then
printThis="No parameter file PIPE_hubbing.txt or PIPE_hubbingSymbolic.txt provided - pipeline aborted"
printToLogFile
printThis="Exiting !"
printToLogFile
printThis="Usage instructions available with :\nmacs2hubber.sh --help "
printToLogFile
exit 1
fi
if [ -s PIPE_hubbing.txt ] && [ -s PIPE_hubbingSymbolic.txt ] ; then
printThis="Both parameter files PIPE_hubbing.txt and PIPE_hubbingSymbolic.txt provided (give ONLY ONE of these) - pipeline aborted"
printToLogFile
printThis="Exiting !"
printToLogFile
printThis="Usage instructions available with :\nmacs2hubber.sh --help "
printToLogFile
exit 1
fi
SYMBOLIC=0
if [ -s PIPE_hubbingSymbolic.txt ] ; then
SYMBOLIC=1
fi
#---------------------------------------------
# Parsing param files ( we have at least hubbing parameters )
parseWhiteSpace
#---------------------------------------------
# Dividing to branches - running WITH or WITHOUT parameter files..
printThis="Dividing to branches - running WITH or WITHOUT parameter files .."
printNewChapterToLogFile
weHaveParamFiles=0
if [ -s PIPE_sampleBamPaths.txt ] || [ -s PIPE_controlBamPaths.txt ] ; then
echo "Found PIPE_sampleBamPaths.txt - turning on PARAMETER-FILE dependent run !"
weHaveParamFiles=1
else
echo "Assuming all run parameters are given in the pipeline command !"
echo " ( didn't find PIPE_sampleBamPaths.txt / PIPE_controlBamPaths.txt )"
fi
#---------------------------------------------
# Setting and checking obligatory parameters (and other command-line parameters)..
printThis="Checking macs2 run parameters (and setting default ones) .."
printNewChapterToLogFile
# common defaults
shortlabel=""
longlabel=""
GENOME=""
FILETYPE=""
# non-parameter-file defaults
samplename="macs2"
foldername="macs2_output"
parametersOK=1
parseParameters
#---------------------------------------------
# Crashing, if command line parameters were given wrong ..
if [ "${parametersOK}" -eq 0 ];then
printThis="Command line parameters given wrong, or conflict between command line params and PIPE_*BamPaths.txt param files ! \nSee error messages in the run error log file."
printToLogFile
echo "That is : see above (in this file)" >&2
printThis="Exiting !"
printToLogFile
printThis="Usage instructions available with :\nmacs2hubber.sh --help "
printToLogFile
exit 1
fi
#---------------------------------------------
# Checking INPUT existence ..
printThis="Checking if we have CONTROL samples .."
printNewChapterToLogFile
weHaveControl=0
if [ "${weHaveParamFiles}" -eq 1 ]; then
if [ -s PIPE_controlBamPaths.txt ]; then
weHaveControl=1;
printThis="Yes we have !"
else
printThis="No control samples found !"
fi
printToLogFile
else
# If we have control samples
macsflag='-c'
macsOnOff
if [ "${macsHasThisFlag}" -eq 1 ];then
weHaveControl=1;
printThis="Yes we have !"
else
printThis="No control samples found !"
fi
printToLogFile
fi
# --------------------------------------------
# Setting and checking parameter-file-red obligatory parameters ..
# parameter-file defaults
samplenames=()
foldernames=()
samplefiles=()
controlfiles=()
parameterFilesOK=1
if [ "${weHaveParamFiles}" -eq 1 ]; then
printThis="Checking parameter files (and setting parameters) .."
printNewChapterToLogFile
parseParameterFiles
fi
#---------------------------------------------
# Crashing, if parameter file parameters were given wrong ..
if [ "${parameterFilesOK}" -eq 0 ];then
printThis="Parameter files had problems (empty columns, not unique sample names) ! \nSee error messages in the run error log file."
printToLogFile
echo "That is : see above (in this file)" >&2
printThis="Exiting !"
printToLogFile
printThis="Usage instructions available with :\nmacs2hubber.sh --help "
printToLogFile
exit 1
fi
#---------------------------------------------
# Checking BAM files existence (whether or not we have parameter files) ..
printThis="Checking BAM files existence .."
printNewChapterToLogFile
bamsFine=1
if [ "${weHaveParamFiles}" -eq 1 ]; then
BAMtype="sample"
bamExistenceParamFile
if [ "${weHaveControl}" -eq 1 ]; then
BAMtype="control"
bamExistenceParamFile
fi
else
BAMtype="sample"
bamExistence
if [ "${weHaveControl}" -eq 1 ]; then
BAMtype="control"
bamExistence
fi
fi
if [ "${bamsFine}" -eq 0 ] ; then
printThis="Problems in finding or reading some of the BAM files !\n Check error messages from the run error file !"
printToLogFile
printThis="Exiting !"
printToLogFile
printThis="Usage instructions available with :\nmacs2hubber.sh --help "
printToLogFile
exit 1
fi
# --------------------------------------------
printThis="All parameters set ! "
printNewChapterToLogFile
echo
echo "Run with parameters :"
echo ""
echo "Command line parameters : "
echo "${parameterList}"
echo
echo "GENOME ${GENOME}"
echo "HUBGENOME ${HUBGENOME}"
echo "FILETYPE ${FILETYPE}"
echo
echo "weHaveParamFiles ${weHaveParamFiles}"
echo "weHaveControl ${weHaveControl}"
echo
echo "shortlabel ${shortlabel}"
echo "longlabel ${longlabel}"
echo "SYMBOLIC ${SYMBOLIC}"
echo
if [ "${weHaveParamFiles}" -eq 0 ]; then
# non-parameter-file defaults
echo "samplename ${samplename}"
echo "foldername ${foldername}"
else
# parameter-file defaults
printRunStartArraysBam
fi
echo
#--------Setting-the-environment------------------------------------------------------------
printThis="LOADING RUNNING ENVIRONMENT"
printNewChapterToLogFile
# Call config file to load modules
setPathsForPipe
#---------------------------------------------
# Set ucsc genome sizes
printThis="Setting ucsc genome sizes .."
printNewChapterToLogFile
# Call config file
# Use config file
setUCSCgenomeSizes
# ##########################################################################################
# Starting the actual MACS2 run(s) ..
# First, re-parsing the parameter list ..
# Restoring the parameter list END ..
parameterList=$( echo ${parameterList} | sed 's/\s*END$//' )
if [ "${weHaveParamFiles}" -eq 0 ]; then
# non-parameter-file run
runParams=${parameterList}
# rm -rf ${foldername}
if [ ! -d ${foldername} ]; then
mkdir ${foldername}
else
printThis="Output folder ${foldername} already exists ! Cannot overwrite ! "
printToLogFile
printThis="Exiting !"
printToLogFile
printThis="Usage instructions available with :\nmacs2hubber.sh --help "
printToLogFile
exit 1
fi
if [ ! -d ${foldername} ]; then
printThis="Couldn't generate output folder ${foldername} ! "
printToLogFile
printThis="Exiting !"
printToLogFile
printThis="Usage instructions available with :\nmacs2hubber.sh --help "
printToLogFile
exit 1
fi
runMacsNoParamFile
cd ${foldername}
macs2runner
cd ..
else
# parameter-file run
for i in $( seq 0 $((${#foldernames[@]} - 1)) ); do
samplename=${samplenames[i]}
foldername=${foldernames[i]}
sed 's/\s*END$/ --SPMR --bdg END/'
sampleFilesWithSpace=$( echo ${samplefiles[i]} | tr ',' ' ' )
if [ "${weHaveControl}" -eq 1 ]; then
controlFilesWithSpace=$( echo ${controlfiles[i]} | tr ',' ' ' )
runParams=$( echo ${parameterList} -t ${sampleFilesWithSpace} -c ${controlFilesWithSpace} )
else
runParams=$( echo ${parameterList} -t ${sampleFilesWithSpace} )
fi
rm -rf ${foldername}
mkdir ${foldername}
cd ${foldername}
runMacsParamFile
macs2runner
cd ..
done
echo
fi
# ----------------------------------------
# All done !
#Remove the marker files from hub - run has ended !
if [ -r "./PIPE_hubbing.txt" ] ; then
hubFolderCheck=$( cat ./PIPE_hubbing.txt | awk '{print $2"/"$1 }' )
elif [ -r "./PIPE_hubbingSymbolic.txt" ] ; then
hubFolderCheck=$( cat ./PIPE_hubbingSymbolic.txt | awk '{print $2"/"$1 }' )
fi
rm -f ${hubFolderCheck}/*/${MagicNumber}.temp
rm -f versionInfoHTML.txt
rm -f versionInfo.txt
printThis="Now finished with all samples !"
printNewChapterToLogFile
# As we make only one hub, the address is always the same .
if [ $(($( ls */hub_address.txt 2> /dev/null | grep -c "" ))) -ne 0 ]; then
echo
echo "Generated / updated data hub :"
echo
cat */hub_address.txt | uniq
echo
echo 'How to load this hub to UCSC : http://userweb.molbiol.ox.ac.uk/public/telenius/DataHubs/ReadMe/HowToUseA_DataHUB_160813.pdf'
echo
echo 'Other tutorials : http://userweb.molbiol.ox.ac.uk/public/telenius/DataHubs/ReadmeFiles.html'
fi
if [ $(($( ls */hub_error.txt 2> /dev/null | grep -c "" ))) -ne 0 ]; then
echo
echo "Hubbing did not succeed for these samples :"
echo
for file in */hub_error.txt
do
echo "${file} :"
cat ${file}
echo
done
fi
timepoint=$( date )
echo
echo "run finished : ${timepoint}"
echo
exit 0