-
Notifications
You must be signed in to change notification settings - Fork 20
/
FitHiChIP_HiCPro.sh
executable file
·2028 lines (1772 loc) · 82.4 KB
/
FitHiChIP_HiCPro.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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#===============
# A stand alone executable of FitHiChIP module
# used to process the HiC-pro pipeline output (allvalidpairs.txt file)
# to generate the contact matrices and
# the statistically significant interactions
# author: Sourya Bhattacharyya
# Vijay-AY lab
# La Jolla Institute for Immunology
#===============
#===============
# Sourya - parse a configuration file
# with the appropriate parameters
#===============
# first read the configuration file name
usage(){
cat << EOF
Options:
-C ConfigFile Name of the configuration file storing the parameters of FitHiChIP.
EOF
}
while getopts "C:" opt;
do
case "$opt" in
C) ConfigFile=$OPTARG;;
\?) usage
echo "error: unrecognized option -$OPTARG";
exit 1
;;
esac
done
#======================
# default values of the parameters
#======================
# input files
# 3 different ways to provide input to FitHiChIP
# option 1: provide a valid pairs file generated from HiC pro pipeline
InpValidPairsFile=""
# option 2: provide the bin intervakl file and the matrix file generated from HiC pro pipeline
InpBinIntervalFile=""
InpMatrixFile=""
# option 3: provide the set of locus pairs
# (6 columns storing the interacting bins and the 7th column storing the contact count)
InpInitialInteractionBedFile=""
# reference ChIP-seq / HiChIP peak file
PeakFILE=""
# prefix string used for every output file
PREFIX='FitHiChIP'
# size of the chromosome that is to be provided
ChrSizeFile=""
# reference chromosome fasta file
RefFastaFile=""
# reference chromosome based mappability file
# (may be downloaded from the site http://hgdownload.cse.ucsc.edu/goldenPath/)
MappabilityFile=""
# restriction fragment file compatible with the reference chromosome
REFragFile=""
# window size used to compute GC content
GCContentWindowSize=200
# window size used to compute the mappability
MappabilityWindowSize=500
# 5 Kb resolution
BIN_SIZE=5000
# FDR threshold
QVALUE=0.01
# default value of output directory is the present working directory
OutDir=`pwd`'/'
# lower distance threshold for two cis interactions
LowDistThres=20000 # 20 Kb
# upper distance threshold for two cis interactions
UppDistThres=2000000 # 2 Mb
# number of bins employed for FitHiChIP
NBins=200
# default value of plotting analysis figures
DrawFig=0
# option to note down the timing information
TimeProf=0
# boolean variable indicating that previous existing output files
# will be overwritten (1) or not (0 - default)
OverWrite=0
#=========================
# Significant interaction call
# and the bias correction related parameters
#=========================
# denotes the type of Interaction
# 1: peak to peak, 2: peak to non peak
# 3: peak to all (default) 4: all to all
# 5: accounting for all of 1 to 4
IntType=3
# variable indicating bias correction (1: On, 0: off)
# recommended = 1
BiasCorr=1
# type of bias vector (if bias correction is employed)
# 1: coverage specific bias
# 2: ICE specific bias (default)
BiasType=1
# temporary variable (binary)
# used to model FitHiChIP peak to all interactions
# using peak to peak background only
# applicable for only peak to all interactions
UseP2PBackgrnd=1
# Merging interactions which are near to each other
MergeInteraction=1
# if the reference genome is circular genome
CircularGenome=0
#========================
# following parameters are now obsolete
# so keeping them in a default state
# user cannot alter these values
#========================
# default lower cutoff of bias value
biaslowthr=0.2
# default higher cutoff of bias value
biashighthr=5
# boolean variable for pre-filtering the interactions according to the bias value
BeginBiasFilter=0
# boolean variable for probability adjustment of the interactions according to the bias value
EndBiasFilter=0
# temporary variable (binary)
# if 1, includes only nonzero contact based locus pairs for
# FitHiC spline fit implementation
# default : 0
UseNonzeroContacts=0
# two variables used for bias correction
# modeling the regression between observed contact count
# and the bias variables
resid_biascorr=0
eqocc_biascorr=1
# boolean variable indicating whether for bias correction
# multiplicative bias value would be used
# defult 0
MultBias=0
#========================
#==============================
# read the configuration file and store various parameters
#==============================
echo -e "\n\n ================ Parsing input configuration file ================= \n\n"
# separator used in the config file
IFS="="
while read -r name value
do
param=$name
paramval=${value//\"/}
if [[ -n $param ]]; then
if [[ $param != \#* ]]; then
# if there are multiple parameter values (separated by # - old values are kept)
# then the following operation selects the current one
paramval=$(echo "$paramval" | awk -F['#\t'] '{print $1}' | tr -d '[:space:]');
echo -e "Content of $param is $paramval"
if [ $param == "ValidPairs" ]; then
InpValidPairsFile=$paramval
fi
if [ $param == "Interval" ]; then
InpBinIntervalFile=$paramval
fi
if [ $param == "Matrix" ]; then
InpMatrixFile=$paramval
fi
if [ $param == "Bed" ]; then
InpInitialInteractionBedFile=$paramval
fi
if [ $param == "HIC" ]; then
InpHiCFile=$paramval
fi
if [ $param == "COOL" ]; then
InpCoolFile=$paramval
fi
## indicates circular genome
if [ $param == "CircularGenome" ]; then
CircularGenome=$paramval
fi
if [ $param == "PeakFile" ]; then
PeakFILE=$paramval
fi
if [ $param == "OutDir" ]; then
if [[ ! -z $paramval ]]; then
OutDir=$paramval
fi
fi
if [ $param == "ChrSizeFile" ]; then
ChrSizeFile=$paramval
fi
# optional parameters
if [ $param == "MappabilityFile" ]; then
MappabilityFile=$paramval
fi
if [ $param == "RefFasta" ]; then
RefFastaFile=$paramval
fi
if [ $param == "REFragFile" ]; then
REFragFile=$paramval
fi
if [ $param == "GCSize" ]; then
if [[ ! -z $paramval ]]; then
GCContentWindowSize=$paramval
fi
fi
if [ $param == "MappSize" ]; then
if [[ ! -z $paramval ]]; then
MappabilityWindowSize=$paramval
fi
fi
# end optional parameters
if [ $param == "BINSIZE" ]; then
if [[ ! -z $paramval ]]; then
BIN_SIZE=$paramval
fi
fi
if [ $param == "LowDistThr" ]; then
if [[ ! -z $paramval ]]; then
LowDistThres=$paramval
fi
fi
if [ $param == "UppDistThr" ]; then
if [[ ! -z $paramval ]]; then
UppDistThres=$paramval
fi
fi
if [ $param == "QVALUE" ]; then
if [[ ! -z $paramval ]]; then
QVALUE=$paramval
fi
fi
if [ $param == "NBins" ]; then
if [[ ! -z $paramval ]]; then
NBins=$paramval
fi
fi
if [ $param == "PREFIX" ]; then
if [[ ! -z $paramval ]]; then
PREFIX=$paramval
fi
fi
if [ $param == "Draw" ]; then
if [[ ! -z $paramval ]]; then
DrawFig=$paramval
fi
fi
if [ $param == "TimeProf" ]; then
if [[ ! -z $paramval ]]; then
TimeProf=$paramval
fi
fi
# these parameters are not required for the moment
# if [ $param == "BeginBiasFilter" ]; then
# if [[ ! -z $paramval ]]; then
# BeginBiasFilter=$paramval
# fi
# fi
# if [ $param == "EndBiasFilter" ]; then
# if [[ ! -z $paramval ]]; then
# EndBiasFilter=$paramval
# fi
# fi
# if [ $param == "biaslowthr" ]; then
# if [[ ! -z $paramval ]]; then
# biaslowthr=$paramval
# fi
# fi
# if [ $param == "biashighthr" ]; then
# if [[ ! -z $paramval ]]; then
# biashighthr=$paramval
# fi
# fi
# if [ $param == "MultBias" ]; then
# if [[ ! -z $paramval ]]; then
# MultBias=$paramval
# fi
# fi
# if [ $param == "BiasCorrResid" ]; then
# if [[ ! -z $paramval ]]; then
# resid_biascorr=$paramval
# fi
# fi
# if [ $param == "BiasCorrEqOcc" ]; then
# if [[ ! -z $paramval ]]; then
# eqocc_biascorr=$paramval
# fi
# fi
# if [ $param == "UseNonzeroContacts" ]; then
# if [[ ! -z $paramval ]]; then
# UseNonzeroContacts=$paramval
# fi
# fi
# end non-required parameters
if [ $param == "MergeInt" ]; then
if [[ ! -z $paramval ]]; then
MergeInteraction=$paramval
fi
fi
# these four variables are added - sourya
if [ $param == "BiasType" ]; then
if [[ ! -z $paramval ]]; then
BiasType=$paramval
fi
fi
if [ $param == "UseP2PBackgrnd" ]; then
if [[ ! -z $paramval ]]; then
UseP2PBackgrnd=$paramval
fi
fi
if [ $param == "OverWrite" ]; then
if [[ ! -z $paramval ]]; then
OverWrite=$paramval
fi
fi
if [ $param == "IntType" ]; then
if [[ ! -z $paramval ]]; then
IntType=$paramval
fi
fi
if [ $param == "BiasCorrection" ]; then
if [[ ! -z $paramval ]]; then
BiasCorr=$paramval
fi
fi
# end add - sourya
fi
fi
done < $ConfigFile
#===================
# verify the input parameters
#===================
echo -e "\n\n ================ Verifying input configuration parameters ================= \n\n"
if [[ -z $InpValidPairsFile && -z $InpInitialInteractionBedFile && -z $InpHiCFile && -z $InpCoolFile ]]; then
if [[ -z $InpBinIntervalFile || -z $InpMatrixFile ]]; then
echo -e 'There are five ways to provide FitHiChIP input: \n 1) provide valid pairs file (from HiC pro pipeline), \n 2) Provide both bin interval and matrix files (from HiC pro pipeline), \n 3) provide a file in the Bed= option - either as a set of locus pairs along with their contact counts (7 columns), 4) Provide interactions in .hic format (from Juicer), or 5) Provide interactions in .cool format. \n But user did not provide any of these input configurations. - exit !!'
exit 1
fi
fi
if [[ -z $PeakFILE ]]; then
if [[ $IntType != 4 ]]; then
echo 'Input interaction type specified requires peak file information - but no reference peak file (either ChIP or HiChIP) is provided - exit !!'
exit 1
fi
fi
#======================
# these parameters are not mandatory
# so warning messages are removed
# if [[ -z $RefFastaFile ]]; then
# echo 'User should provide reference chromosome fasta file - quit !!'
# exit 1
# fi
# if [[ -z $REFragFile ]]; then
# echo 'User should provide a restriction fragment file - quit !!'
# exit 1
# fi
# if [[ -z $MappabilityFile ]]; then
# echo 'Reference mappability file is not specified - exit !!'
# exit 1
# fi
#======================
if [[ -z $ChrSizeFile ]]; then
echo 'ERROR ====>>> Chromosome size file is not specified - exit !!'
exit 1
fi
echo '***** Specified output directory : '$OutDir
# boolean variable indicating error condition
errcond=0
##=============
## if Bed file / HiC file / cool file is not provided,
## means we need to process the valid pairs
## and there is no matrix file or bin interval file
## then, get the installed HiC-pro version
##=============
HiCProExec=`which HiC-Pro`
if [[ -z $HiCProExec ]]; then
echo 'ERROR ===>>>> HiC-pro is not installed in the system - FitHiChIP quits !!!'
exit 1
fi
# path of HiC pro executable
# of the format somedir/bin/HiC-Pro
d1=$(dirname ${HiCProExec})
HiCProBasedir=$(dirname ${d1})
echo 'Base directory containing HiCPro package : '$HiCProBasedir
if [[ -z $HiCProBasedir ]]; then
echo 'ERROR ====>>> Invalid HiC-pro installation directory - FitHiChIP quits - exit !!'
exit 1
fi
#*****************************
# error checking -
# check if the required libraries are all installed or not
#*****************************
## first check the python version
# note: FitHiChIP new versions now use python3, instead of python2
# check if python3 is installed and its version is > 3.4
pythonversion=$(python3 --version 2>&1 | head -n 1 | awk '{print $2}' -)
if [[ -z "$pythonversion" ]]; then
echo "ERROR ====>>> No Python installation is detected in the system !!! FitHiChIP quits !!!"
errcond=1
fi
numfield=`echo $pythonversion | awk -F'[.]' '{print NF}' -`
if [[ $numfield -ge 3 ]]; then
num1=`echo $pythonversion | awk -F'[.]' '{print $1}' -`
num2=`echo $pythonversion | awk -F'[.]' '{print $2}' -`
num3=`echo $pythonversion | awk -F'[.]' '{print $3}' -`
if [[ $num1 -gt 3 ]]; then
echo "Installed python version: "${pythonversion}
elif [[ $num1 -eq 3 && $num2 -gt 4 ]]; then
echo "Installed python version: "${pythonversion}
elif [[ $num1 -eq 3 && $num2 -eq 4 && $num3 -ge 1 ]]; then
echo "Installed python version: "${pythonversion}
else
echo " --- FitHiChIP now works on python 3 - should be python3 with version >= 3.4 !!! FitHiChIP quits !!!"
errcond=1
fi
else
num1=`echo $pythonversion | awk -F'[.]' '{print $1}' -`
num2=`echo $pythonversion | awk -F'[.]' '{print $2}' -`
if [[ $num1 -gt 3 ]]; then
echo "Installed python version: "${pythonversion}
elif [[ $num1 -eq 3 && $num2 -gt 3 ]]; then
echo "Installed python version: "${pythonversion}
else
echo " --- FitHiChIP now works on python 3 - should be python3 with version >= 3.4 !!! FitHiChIP quits !!!"
errcond=1
fi
fi
# check if python3 libraries are also installed
if python3 -c "import gzip"; then
echo '*** Python library gzip is installed'
else
echo 'ERROR ====>>> Python3 library gzip is not installed !!! FitHiChIP quits !!!'
errcond=1
fi
if python3 -c "from optparse import OptionParser"; then
echo '*** Python module OptionParser (from the package optparse) is installed'
else
echo 'ERROR ====>>> Python module OptionParser (from the package optparse) is not installed !!! FitHiChIP quits !!!'
errcond=1
fi
if python3 -c "import networkx"; then
echo '*** Python package networkx is installed'
else
echo 'ERROR ====>>> Python3 package networkx is not installed !!! Try to install the package by python3 -m pip install networkx. FitHiChIP quits !!!'
errcond=1
fi
# check if MACS2 package is installed
macs2version=$(macs2 --version 2>&1 | head -n 1 | awk -F[" "] '{print $2 }' -)
if [[ -z "$macs2version" ]]; then
echo "ERROR ====>>> MACS2 peak calling package is not detected in the system !!! Try to install the package by python3 -m pip install MACS2. FitHiChIP quits !!!"
errcond=1
else
echo "*** Found MACS2 package (for peak calling) installed in the system - "$macs2version
fi
# check the R version
Rversion=$(R --version | head -n 1 | awk -F[" "] '{print $3}' -)
if [[ -z "$Rversion" ]]; then
echo "ERROR ====>>> No R installation is detected in the system !!! FitHiChIP quits !!!"
errcond=1
fi
numfield=`echo $Rversion | awk -F'[.]' '{print NF}' -`
if [[ $numfield -ge 3 ]]; then
num1=`echo $Rversion | awk -F'[.]' '{print $1}' -`
num2=`echo $Rversion | awk -F'[.]' '{print $2}' -`
num3=`echo $Rversion | awk -F'[.]' '{print $3}' -`
if [[ $num1 -gt 3 ]]; then
echo "Installed R version: "${Rversion}
elif [[ $num1 -eq 3 && $num2 -gt 3 ]]; then
echo "Installed R version: "${Rversion}
elif [[ $num1 -eq 3 && $num2 -eq 3 && $num3 -ge 0 ]]; then
echo "Installed R version: "${Rversion}
else
echo " -- R version should be at least R 3.3 !!! FitHiChIP quits !!!"
errcond=1
fi
else
num1=`echo $Rversion | awk -F'[.]' '{print $1}' -`
num2=`echo $Rversion | awk -F'[.]' '{print $2}' -`
if [[ $num1 -gt 3 ]]; then
echo "Installed R version: "${Rversion}
elif [[ $num1 -eq 3 && $num2 -gt 3 ]]; then
echo "Installed R version: "${Rversion}
else
echo " -- R version should be at least R 3.3 !!! FitHiChIP quits !!!"
errcond=1
fi
fi
# check the samtools version
samtoolsversion=$(samtools 2>&1 | grep "Version" | awk -F[" "] '{print $2}' -)
if [[ -z "$samtoolsversion" ]]; then
echo "ERROR ====>>> No samtools installation is detected in the system !!! FitHiChIP quits !!!"
errcond=1
# exit 1
fi
numfield=`echo $samtoolsversion | awk -F'[.]' '{print NF}' -`
if [[ $numfield -ge 3 ]]; then
num1=`echo $samtoolsversion | awk -F'[.]' '{print $1}' -`
num2=`echo $samtoolsversion | awk -F'[.]' '{print $2}' -`
num3=`echo $samtoolsversion | awk -F'[.]' '{print $3}' -`
if [[ $num1 -gt 1 ]]; then
echo "Installed samtools version: "${samtoolsversion}
elif [[ $num1 -eq 1 && $num2 -gt 6 ]]; then
echo "Installed samtools version: "${samtoolsversion}
elif [[ $num1 -eq 1 && $num2 -eq 6 && $num3 -ge 0 ]]; then
echo "Installed samtools version: "${samtoolsversion}
else
echo " - Samtools version should be at least 1.6 !!! FitHiChIP quits !!!"
errcond=1
fi
else
num1=`echo $samtoolsversion | awk -F'[.]' '{print $1}' -`
num2=`echo $samtoolsversion | awk -F'[.]' '{print $2}' -`
if [[ $num1 -gt 1 ]]; then
echo "Installed samtools version: "${samtoolsversion}
elif [[ $num1 -eq 1 && $num2 -gt 6 ]]; then
echo "Installed samtools version: "${samtoolsversion}
else
echo " - Samtools version should be at least 1.6 !!! FitHiChIP quits !!!"
errcond=1
fi
fi
# check if bgzip is installed in the system
bgzipnum=`bgzip -h 2>&1 | wc -l`
if [[ $bgzipnum -gt 5 ]]; then
echo "*** bgzip utility is installed in the system"
else
echo "ERROR =====>> bgzip utility is NOT installed in the system -- please install it from htslib (associated with samtools) version >= 1.6"
errcond=1
fi
# check if tabix is installed in the system
tabixnum=`tabix -h 2>&1 | wc -l`
if [[ $tabixnum -gt 5 ]]; then
echo "*** tabix utility is installed in the system"
else
echo "ERROR =====>> tabix utility is NOT installed in the system -- please install it from htslib (associated with samtools) version >= 1.6"
errcond=1
fi
# check the bedtools version
# command 1
bedtoolsversion1=$(bedtools --version | head -n 1 | awk -F[" "] '{print substr($2,2); }' -)
# echo "bedtoolsversion1: "$bedtoolsversion1
# command 2
bedtoolsversion2=$(bedtools 2>&1 | grep "Version" | awk -F[" "] '{print substr($NF,2); }' -)
# echo "bedtoolsversion2: "$bedtoolsversion2
# if both commands fail then surely bedtools is not installed in the system
if [[ -z "$bedtoolsversion1" && -z "$bedtoolsversion2" ]]; then
echo "ERROR ====>>> No bedtools installation is detected in the system !!! FitHiChIP quits !!!"
errcond=1
fi
if [[ ! -z "$bedtoolsversion1" ]]; then
numfield=`echo $bedtoolsversion1 | awk -F'[.]' '{print NF}' -`
if [[ $numfield -ge 3 ]]; then
num1=`echo $bedtoolsversion1 | awk -F'[.]' '{print $1}' -`
num2=`echo $bedtoolsversion1 | awk -F'[.]' '{print $2}' -`
num3=`echo $bedtoolsversion1 | awk -F'[.]' '{print $3}' -`
if [[ $num1 -gt 2 ]]; then
echo "Installed bedtools version: "${bedtoolsversion1}
elif [[ $num1 -eq 2 && $num2 -gt 26 ]]; then
echo "Installed bedtools version: "${bedtoolsversion1}
elif [[ $num1 -eq 2 && $num2 -eq 26 && $num3 -ge 0 ]]; then
echo "Installed bedtools version: "${bedtoolsversion1}
else
echo " - bedtools version should be at least 2.26.0 !!! FitHiChIP quits !!!"
errcond=1
fi
else
num1=`echo $bedtoolsversion1 | awk -F'[.]' '{print $1}' -`
num2=`echo $bedtoolsversion1 | awk -F'[.]' '{print $2}' -`
if [[ $num1 -gt 2 ]]; then
echo "Installed bedtools version: "${bedtoolsversion1}
elif [[ $num1 -eq 2 && $num2 -gt 26 ]]; then
echo "Installed bedtools version: "${bedtoolsversion1}
else
echo " - bedtools version should be at least 2.26.0 !!! FitHiChIP quits !!!"
errcond=1
fi
fi
fi
if [[ -z "$bedtoolsversion1" && ! -z "$bedtoolsversion2" ]]; then
numfield=`echo $bedtoolsversion2 | awk -F'[.]' '{print NF}' -`
if [[ $numfield -ge 3 ]]; then
num1=`echo $bedtoolsversion2 | awk -F'[.]' '{print $1}' -`
num2=`echo $bedtoolsversion2 | awk -F'[.]' '{print $2}' -`
num3=`echo $bedtoolsversion2 | awk -F'[.]' '{print $3}' -`
if [[ $num1 -gt 2 ]]; then
echo "Installed bedtools version: "${bedtoolsversion2}
elif [[ $num1 -eq 2 && $num2 -gt 26 ]]; then
echo "Installed bedtools version: "${bedtoolsversion2}
elif [[ $num1 -eq 2 && $num2 -eq 26 && $num3 -ge 0 ]]; then
echo "Installed bedtools version: "${bedtoolsversion2}
else
echo " - bedtools version should be at least 2.26.0 !!! FitHiChIP quits !!!"
errcond=1
fi
else
num1=`echo $bedtoolsversion2 | awk -F'[.]' '{print $1}' -`
num2=`echo $bedtoolsversion2 | awk -F'[.]' '{print $2}' -`
if [[ $num1 -gt 2 ]]; then
echo "Installed bedtools version: "${bedtoolsversion2}
elif [[ $num1 -eq 2 && $num2 -gt 26 ]]; then
echo "Installed bedtools version: "${bedtoolsversion2}
else
echo " - bedtools version should be at least 2.26.0 !!! FitHiChIP quits !!!"
errcond=1
fi
fi
fi
# final evaluation
if [[ $errcond == 1 ]]; then
echo " --------->>>>>> ERROR - current system has one or more packages missing"
echo " ------- please check the above logs and install the missing packages before executing FitHiChIP"
echo " ------- exiting for the moment !!"
exit 1
fi
#*****************************
# here check if the configuration file has relative path names as the input
# in such a case, convert the relative path names (with respect to the location of the configuration file itself)
# in the absolute file
# also perform Error checking - check if input files provided are correct
# i.e. these files exist
echo -e "\n\n ====== Changing relative pathnames of the input files to their absolute path names ========== \n\n"
# directory of the configuration file
ConfigFileDir=$(dirname "${ConfigFile}")
# first go to the configuration file directory
cd $ConfigFileDir
if [[ ! -z $InpValidPairsFile ]]; then
if [ ! -f $InpValidPairsFile ]; then
echo 'ERROR ===>>>> Valid pairs file provided as input : '$InpValidPairsFile
echo 'However, no such valid pairs file exists - check input file and path - FitHiChIP quits !!'
exit 1
fi
if [[ "${InpValidPairsFile:0:1}" != / && "${InpValidPairsFile:0:2}" != ~[/a-z] ]]; then
# relative path - convert to absolute path
InpValidPairsFile="$(cd $(dirname $InpValidPairsFile); pwd)/$(basename $InpValidPairsFile)"
echo 'Absolute converted path: InpValidPairsFile: '$InpValidPairsFile
fi
fi
if [[ ! -z $InpBinIntervalFile ]]; then
if [ ! -f $InpBinIntervalFile ]; then
echo 'ERROR ===>>>> Bin interval file provided as input : '$InpBinIntervalFile
echo 'However, no such bin interval file exists - check input file and path - FitHiChIP quits !!'
exit 1
fi
if [[ "${InpBinIntervalFile:0:1}" != / && "${InpBinIntervalFile:0:2}" != ~[/a-z] ]]; then
# relative path - convert to absolute path
InpBinIntervalFile="$(cd $(dirname $InpBinIntervalFile); pwd)/$(basename $InpBinIntervalFile)"
echo 'Absolute converted path: InpBinIntervalFile: '$InpBinIntervalFile
fi
fi
if [[ ! -z $InpMatrixFile ]]; then
if [ ! -f $InpMatrixFile ]; then
echo 'ERROR ===>>>> HiChIP matrix file provided as input : '$InpMatrixFile
echo 'However, no such matrix file exists - check input file and path - FitHiChIP quits !!'
exit 1
fi
if [[ "${InpMatrixFile:0:1}" != / && "${InpMatrixFile:0:2}" != ~[/a-z] ]]; then
# relative path - convert to absolute path
InpMatrixFile="$(cd $(dirname $InpMatrixFile); pwd)/$(basename $InpMatrixFile)"
echo 'Absolute converted path: InpMatrixFile: '$InpMatrixFile
fi
fi
if [[ ! -z $InpInitialInteractionBedFile ]]; then
if [ ! -f $InpInitialInteractionBedFile ]; then
echo 'ERROR ===>>>> Pre-computed locus pairs (along with their contact counts) are provided as input : '$InpInitialInteractionBedFile
echo 'However, no such file containing locus pairs exists - check input file and path - FitHiChIP quits !!'
exit 1
fi
if [[ "${InpInitialInteractionBedFile:0:1}" != / && "${InpInitialInteractionBedFile:0:2}" != ~[/a-z] ]]; then
# relative path - convert to absolute path
InpInitialInteractionBedFile="$(cd $(dirname $InpInitialInteractionBedFile); pwd)/$(basename $InpInitialInteractionBedFile)"
echo 'Absolute converted path: InpInitialInteractionBedFile: '$InpInitialInteractionBedFile
fi
fi
if [[ ! -z $InpHiCFile ]]; then
if [ ! -f $InpHiCFile ]; then
echo 'ERROR ===>>>> .hic contact matrix file is provided as input : '$InpHiCFile
echo 'However, no such file exists - check input file and path - FitHiChIP quits !!'
exit 1
fi
if [[ "${InpHiCFile:0:1}" != / && "${InpHiCFile:0:2}" != ~[/a-z] ]]; then
# relative path - convert to absolute path
InpHiCFile="$(cd $(dirname $InpHiCFile); pwd)/$(basename $InpHiCFile)"
echo 'Absolute converted path: InpHiCFile: '$InpHiCFile
fi
fi
if [[ ! -z $InpCoolFile ]]; then
if [ ! -f $InpCoolFile ]; then
echo 'ERROR ===>>>> .cool contact matrix file is provided as input : '$InpCoolFile
echo 'However, no such file exists - check input file and path - FitHiChIP quits !!'
exit 1
fi
if [[ "${InpCoolFile:0:1}" != / && "${InpCoolFile:0:2}" != ~[/a-z] ]]; then
# relative path - convert to absolute path
InpCoolFile="$(cd $(dirname $InpCoolFile); pwd)/$(basename $InpCoolFile)"
echo 'Absolute converted path: InpCoolFile: '$InpCoolFile
fi
fi
if [[ ! -z $PeakFILE ]]; then
if [ ! -f $PeakFILE ]; then
echo 'ERROR ===>>>> Peak file provided as input : '$PeakFILE
echo 'However, no such peak file exists - check input file and path - FitHiChIP quits !!'
exit 1
fi
if [[ "${PeakFILE:0:1}" != / && "${PeakFILE:0:2}" != ~[/a-z] ]]; then
# relative path - convert to absolute path
PeakFILE="$(cd $(dirname $PeakFILE); pwd)/$(basename $PeakFILE)"
echo 'Absolute converted path: PeakFILE: '$PeakFILE
fi
fi
if [[ ! -z $RefFastaFile ]]; then
if [ ! -f $RefFastaFile ]; then
echo 'ERROR ===>>>> Fasta file of reference genome provided as input : '$RefFastaFile
echo 'However, no such fasta file exists - check input file and path - FitHiChIP quits !!'
exit 1
fi
if [[ "${RefFastaFile:0:1}" != / && "${RefFastaFile:0:2}" != ~[/a-z] ]]; then
# relative path - convert to absolute path
RefFastaFile="$(cd $(dirname $RefFastaFile); pwd)/$(basename $RefFastaFile)"
echo 'Absolute converted path: RefFastaFile: '$RefFastaFile
fi
fi
if [[ ! -z $REFragFile ]]; then
if [ ! -f $REFragFile ]; then
echo 'ERROR ===>>>> Restriction fragment file provided as input : '$REFragFile
echo 'However, no such restriction fragment file exists - check input file and path - FitHiChIP quits !!'
exit 1
fi
if [[ "${REFragFile:0:1}" != / && "${REFragFile:0:2}" != ~[/a-z] ]]; then
# relative path - convert to absolute path
REFragFile="$(cd $(dirname $REFragFile); pwd)/$(basename $REFragFile)"
echo 'Absolute converted path: REFragFile: '$REFragFile
fi
fi
if [[ ! -z $ChrSizeFile ]]; then
if [ ! -f $ChrSizeFile ]; then
echo 'ERROR ===>>>> File containing size of chromosomes for the reference genome, as provided : '$ChrSizeFile
echo 'However, no such chromosome size containing file exists - check input file and path - FitHiChIP quits !!'
exit 1
fi
if [[ "${ChrSizeFile:0:1}" != / && "${ChrSizeFile:0:2}" != ~[/a-z] ]]; then
# relative path - convert to absolute path
ChrSizeFile="$(cd $(dirname $ChrSizeFile); pwd)/$(basename $ChrSizeFile)"
echo 'Absolute converted path: ChrSizeFile: '$ChrSizeFile
fi
fi
if [[ ! -z $MappabilityFile ]]; then
if [ ! -f $MappabilityFile ]; then
echo 'ERROR ===>>>> Mappability information containing file provided as input : '$MappabilityFile
echo 'However, no such Mappability related file exists - check input file and path - FitHiChIP quits !!'
exit 1
fi
if [[ "${MappabilityFile:0:1}" != / && "${MappabilityFile:0:2}" != ~[/a-z] ]]; then
# relative path - convert to absolute path
MappabilityFile="$(cd $(dirname $MappabilityFile); pwd)/$(basename $MappabilityFile)"
echo 'Absolute converted path: MappabilityFile: '$MappabilityFile
fi
fi
if [[ ! -z $OutDir ]]; then
if [[ "${OutDir:0:1}" != / && "${OutDir:0:2}" != ~[/a-z] ]]; then
# relative path - convert to absolute path
OutDir="$(cd $(dirname $OutDir); pwd)/$(basename $OutDir)"
echo 'Absolute converted path: OutDir: '$OutDir
fi
fi
if [[ ! -z $HiCProBasedir ]]; then
if [ ! -d $HiCProBasedir ]; then
echo 'ERROR ===>>>> Base directory of HiC-Pro package as provided : '$HiCProBasedir
echo 'However, no such directory exists in the system - check input file and path - FitHiChIP quits !!'
exit 1
fi
if [[ "${HiCProBasedir:0:1}" != / && "${HiCProBasedir:0:2}" != ~[/a-z] ]]; then
# relative path - convert to absolute path
HiCProBasedir="$(cd $(dirname $HiCProBasedir); pwd)/$(basename $HiCProBasedir)"
echo 'Absolute converted path: HiCProBasedir: '$HiCProBasedir
fi
fi
# revert to the old directory
cd -
#===================
# further error checking - check for false values
#===================
echo -e "\n\n ====== Final checing of input parameters ========== \n\n"
if [[ $IntType -lt 1 || $IntType -gt 5 ]]; then
echo 'ERROR ===>>>> Parameter IntType should have value between 1 to 5: -- FitHiChIP quits !!!'
exit 1
fi
if [[ $BIN_SIZE -lt 0 ]]; then
echo 'ERROR ===>>>> Parameter Bin size is specified negative: -- FitHiChIP quits !!!'
exit 1
fi
if [[ $LowDistThres -lt 0 ]]; then
echo 'ERROR ===>>>> Parameter low distance threshold is specified negative: -- FitHiChIP quits !!!'
exit 1
fi
if [[ $UppDistThres -lt 0 ]]; then
echo 'ERROR ===>>>> Parameter upper distance threshold is specified negative: -- FitHiChIP quits !!!'
exit 1
fi
if [[ $UppDistThres -lt $LowDistThres ]]; then
echo 'ERROR ===>>>> Parameter upper distance threshold is specified lower than the lower distance threshold -- check both values -- FitHiChIP quits !!!'
exit 1
fi
if [[ $UseP2PBackgrnd -lt 0 || $UseP2PBackgrnd -gt 1 ]]; then
echo 'ERROR ===>>>> Parameter UseP2PBackgrnd should have values either 0 or 1: -- FitHiChIP quits !!!'
exit 1
fi
if [[ $BiasType -lt 1 || $BiasType -gt 2 ]]; then
echo 'ERROR ===>>>> Parameter BiasType should have values either 1 (indicating coverage bias regression) or 2 (ICE bias regression): -- FitHiChIP quits !!!'
exit 1
fi
if [[ $MergeInteraction -lt 0 || $MergeInteraction -gt 1 ]]; then
echo 'ERROR ===>>>> Parameter MergeInt should have values either 0 or 1: -- FitHiChIP quits !!!'
exit 1
fi
# if [[ $QVALUE -lt 0 || $QVALUE -ge 1 ]]; then
if [[ 1 -eq "$(echo "${QVALUE} < 0" | bc)" || 1 -eq "$(echo "${QVALUE} > 1" | bc)" ]]; then
echo 'ERROR ===>>>> Parameter q-value (FDR) should be positive but close to 0 (default 0.01) - but here invalid values are provided - check the parameters -- FitHiChIP quits !!!'
exit 1
fi
if [[ $OverWrite -lt 0 || $OverWrite -gt 1 ]]; then
echo 'ERROR ===>>>> Parameter OverWrite should have values either 0 or 1: -- FitHiChIP quits !!!'
exit 1
fi
#===================
# create the output directory
#===================
mkdir -p $OutDir
#============================
# print the parameters and values
#============================
echo -e "\n\n ====== Writing input parameters ========== \n\n"
ConfFile=$OutDir/Parameters.txt
if [[ ! -f $ConfFile || $OverWrite == 1 ]]; then
echo "Summarizing the parameters employed in this execution" > $ConfFile
echo "Listing the input files: " >> $ConfFile
if [[ ! -z $InpValidPairsFile ]]; then
echo "Input file containing the valid pairs (generated from HIC-PRO pipeline): $InpValidPairsFile " >> $ConfFile
fi
if [[ ! -z $InpBinIntervalFile ]]; then
echo "Input file containing the fixed size bin intervals (generated from HIC-PRO pipeline): $InpBinIntervalFile " >> $ConfFile
fi
if [[ ! -z $InpMatrixFile ]]; then
echo "Input file storing the contact matrix between the fixed size bins (generated from HIC-PRO pipeline): $InpMatrixFile " >> $ConfFile
fi
if [[ ! -z $InpInitialInteractionBedFile ]]; then
echo "Input file containing pre-computed locus pairs and contact count: $InpInitialInteractionBedFile " >> $ConfFile
fi
if [[ ! -z $InpHiCFile ]]; then
echo "Input interaction file in .hic format : $InpHiCFile " >> $ConfFile
fi
echo "Input ChIP-seq peak file: $PeakFILE " >> $ConfFile
echo "File containing chromosome size information corresponding to the reference chromosome: $ChrSizeFile " >> $ConfFile
if [[ ! -z $MappabilityFile ]]; then
echo "Mappability File: $MappabilityFile " >> $ConfFile
fi
if [[ ! -z $RefFastaFile ]]; then
echo "Reference Fasta sequence File: $RefFastaFile " >> $ConfFile
fi
if [[ ! -z $REFragFile ]]; then
echo "Restriction Fragment File: $REFragFile " >> $ConfFile
fi
echo "Output directory which will store all the results: $OutDir " >> $ConfFile
if [[ ! -z $HiCProBasedir ]]; then
echo "Base directory containing the HIC-PRO executable: $HiCProBasedir " >> $ConfFile
fi
echo "Low distance threshold: $LowDistThres " >> $ConfFile
echo "Higher distance threshold: $UppDistThres " >> $ConfFile
if [[ ! -z $MappabilityFile && ! -z $RefFastaFile && ! -z $REFragFile ]]; then
echo "GCContentWindowSize: $GCContentWindowSize " >> $ConfFile
echo "MappabilityWindowSize: $MappabilityWindowSize " >> $ConfFile
fi
echo "BIN_SIZE: $BIN_SIZE " >> $ConfFile
echo "PREFIX (string of output files): $PREFIX " >> $ConfFile
# echo "Timeprof (1 means timing will be noted): $TimeProf " >> $ConfFile
echo "OverWrite (1 means existing files will be overwritten): $OverWrite " >> $ConfFile
# echo "DrawFig (1 means various summary statistics will be plotted, depending on the availability of reference fasta sequence file and mappability file): $DrawFig " >> $ConfFile
fi
#=======================================
# generate a file which will contain the timing profile
if [ $TimeProf == 1 ]; then
OutTimeFile=$OutDir'/TimingProfile.txt'
if [[ ! -f $OutTimeFile || $OverWrite == 1 ]]; then
echo " ================ Time profiling =========== " > $OutTimeFile
else
echo " ================ Time profiling (append) =========== " >> $OutTimeFile
fi
start=$(date +%s.%N)
fi
#==============================
# important - sourya
# first change the current working directory to the directory containing this script
# it is useful when the script is invoked from a separate directory
#==============================
currworkdir=`pwd`
currscriptdir=`dirname $0`
cd $currscriptdir
#==============================
# note down the executables of python and Rscript
# now FitHiChIP supports python3, instead of python2
PythonExec=`which python3`
echo 'Executable of python3: '$PythonExec
RScriptExec=`which Rscript`
echo 'Executable of R : '$RScriptExec
## required for UCSC compatible browser file creation
bedToBigBedExec=`which bedToBigBed`
if [[ -z $bedToBigBedExec ]]; then
## download the "bedToBigBed" utility from UCSC
wget http://hgdownload.soe.ucsc.edu/admin/exe/linux.x86_64/bedToBigBed
bedToBigBedExec=`pwd`'/bedToBigBed'
fi
## download the interact.as utility from UCSC
interact_as_file=`pwd`'/interact.as'
if [[ ! -f $interact_as_file ]]; then
wget https://genome.ucsc.edu/goldenPath/help/examples/interact/interact.as
fi
# #************************
# # error checking