|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +## HiC-Pro |
| 4 | +## Copyleft 2017 Institut Curie |
| 5 | +## Author(s): Nicolas Servant |
| 6 | + |
| 7 | +## This software is distributed without any guarantee under the terms of the BSD licence |
| 8 | + |
| 9 | +## |
| 10 | +## First version of converter between HiCPro and higlass. |
| 11 | +## The cooler python package should be properly installed, as well as the higlass software |
| 12 | +## |
| 13 | + |
| 14 | +function usage { |
| 15 | + echo -e "usage : hicpro2higlass -i INPUT -r RESOLUTION -c CHROMSIZE [-n] [-h]" ## [-b BINS] |
| 16 | + echo -e "Use option -h|--help for more information" |
| 17 | +} |
| 18 | + |
| 19 | +function help { |
| 20 | + usage; |
| 21 | + echo |
| 22 | + echo "Generate Higlass input file from HiC-Pro results" |
| 23 | + echo "See https://github.com/hms-dbmi/higlass-website for details about Higlass" |
| 24 | + echo "---------------" |
| 25 | + echo "OPTIONS" |
| 26 | + echo |
| 27 | + echo " -i|--input INPUT : allValidPairs or matrix file generated by HiC-Pro" |
| 28 | +# echo " -b|--bed BINS : bed file generated by HiC-Pro with intervals coordinates (require if input is a .matrix file)" |
| 29 | + echo " -r|--res RESOLUTION : .matrix file resolution or maximum resolution to reach from the .allValidPairs input file" |
| 30 | + echo " -c|--chrom CHROMSIZE : chromosome size file" |
| 31 | + echo " [-n|--norm] : run cooler matrix balancing algorithm" |
| 32 | + echo " [-h|--help]: help" |
| 33 | + exit; |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +if [ $# -lt 1 ] |
| 38 | +then |
| 39 | + usage |
| 40 | + exit |
| 41 | +fi |
| 42 | + |
| 43 | +# Transform long options to short ones |
| 44 | +for arg in "$@"; do |
| 45 | + shift |
| 46 | + case "$arg" in |
| 47 | + "--input") set -- "$@" "-i" ;; |
| 48 | + "--bed") set -- "$@" "-b" ;; |
| 49 | + "--res") set -- "$@" "-r" ;; |
| 50 | + "--chrom") set -- "$@" "-c" ;; |
| 51 | + "--norm") set -- "$@" "-n" ;; |
| 52 | + "--help") set -- "$@" "-h" ;; |
| 53 | + *) set -- "$@" "$arg" |
| 54 | + esac |
| 55 | +done |
| 56 | + |
| 57 | +INPUT_HICPRO="" |
| 58 | +INPUT_BED="" |
| 59 | +NORMALIZE=0 |
| 60 | +CHROMSIZES_FILE="" |
| 61 | +RES=10000 |
| 62 | + |
| 63 | +while getopts ":i:b:c:r:nh" OPT |
| 64 | +do |
| 65 | + case $OPT in |
| 66 | + i) INPUT_HICPRO=$OPTARG;; |
| 67 | + b) INPUT_BED=$OPTARG;; |
| 68 | + n) NORMALIZE=1;; |
| 69 | + c) CHROMSIZES_FILE=$OPTARG;; |
| 70 | + r) RES=$OPTARG;; |
| 71 | + h) help ;; |
| 72 | + \?) |
| 73 | + echo "Invalid option: -$OPTARG" >&2 |
| 74 | + usage |
| 75 | + exit 1 |
| 76 | + ;; |
| 77 | + :) |
| 78 | + echo "Option -$OPTARG requires an argument." >&2 |
| 79 | + usage |
| 80 | + exit 1 |
| 81 | + ;; |
| 82 | + esac |
| 83 | +done |
| 84 | + |
| 85 | +if [[ -z $INPUT_HICPRO ]]; |
| 86 | +then |
| 87 | + usage |
| 88 | + exit |
| 89 | +fi |
| 90 | + |
| 91 | +## Detect input data type |
| 92 | +DATATYPE="" |
| 93 | +if [[ $INPUT_HICPRO == *.matrix ]]; then |
| 94 | + DATATYPE="MATRIX" |
| 95 | + if [[ -z $INPUT_BED ]]; then |
| 96 | + echo -e "Exit. BED file is required with .matrix file." |
| 97 | + usage |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | +elif [[ $INPUT_HICPRO == *allValidPairs ]]; then |
| 101 | + DATATYPE="VALID" |
| 102 | +else |
| 103 | + echo -e "Unknown input data type. Expect .matrix or _allValidPairs input files." |
| 104 | + exit 1 |
| 105 | +fi |
| 106 | +echo -e "$DATATYPE input file detected ..." |
| 107 | + |
| 108 | +## Check cooler version |
| 109 | +which cooler > /dev/null; |
| 110 | +if [ $? != "0" ]; then |
| 111 | + echo -e "Cooler is not installed or is not in your $PATH. See https://github.com/mirnylab/cooler for details." |
| 112 | + exit 1; |
| 113 | +fi |
| 114 | + |
| 115 | +COOLER_VERSION=$(cooler --version 2>&1 | awk '{print $NF}') |
| 116 | +echo "Cooler version $COOLER_VERSION detected ..." |
| 117 | +if [[ "$COOLER_VERSION" < "0.7.6" ]]; then |
| 118 | + echo "Cooler version must be >= 0.7.6 ! Stop." |
| 119 | +fi |
| 120 | + |
| 121 | +if [[ $DATATYPE == "VALID" ]]; then |
| 122 | + which pairix > /dev/null; |
| 123 | + if [ $? != "0" ]; then |
| 124 | + echo -e "Pairix is not installed or is not in your $PATH. See https://github.com/4dn-dcic/pairix." |
| 125 | + exit 1; |
| 126 | + fi |
| 127 | +fi |
| 128 | + |
| 129 | +echo -e "\nGenerating .cool files ..." |
| 130 | +tmp_dir=./_tmp$$ |
| 131 | +mkdir -p $tmp_dir |
| 132 | + |
| 133 | +if [[ $DATATYPE == "MATRIX" ]]; then |
| 134 | + out=$(basename $INPUT_HICPRO | sed -e 's/.matrix/.cool/') |
| 135 | + |
| 136 | + cooler makebins $CHROMSIZES_FILE $RES > $tmp_dir/bins.bed |
| 137 | + cooler load -f coo --one-based $tmp_dir/bins.bed $INPUT_HICPRO $out |
| 138 | + |
| 139 | + echo -e "\nZoomify .cool file ..." |
| 140 | + if [[ $NORMALIZE == 1 ]]; then |
| 141 | + cooler zoomify $out |
| 142 | + else |
| 143 | + cooler zoomify --no-balance $out |
| 144 | + fi |
| 145 | + out=$(basename $INPUT_HICPRO | sed -e 's/.matrix/.mcool/') |
| 146 | + |
| 147 | +elif [[ $DATATYPE == "VALID" ]]; then |
| 148 | + out=$(basename $INPUT_HICPRO | sed -e 's/_allValidPairs/.cool/') |
| 149 | + |
| 150 | + awk '{OFS="\t";print $2,$3,$4,$5,$6,$7,1}' $INPUT_HICPRO | sed -e 's/+/1/g' -e 's/-/16/g' > $tmp_dir/contacts.txt |
| 151 | + #awk '{print $1,$4,$2,$3,$9,$7,$5,$6,$10,$11,$12}' SRR3179588_WT_sample_allValidPairs | sed -e 's/+/1/g' -e 's/-/16/g' > contacts.txt |
| 152 | + cooler csort --nproc 2 -c1 1 -p1 2 -s1 3 -c2 4 -p2 5 -s2 6 \ |
| 153 | + -o $tmp_idr/contacts.sorted.txt.gz \ |
| 154 | + $tmp_dir/contacts.txt \ |
| 155 | + $CHROMSIZES_FILE |
| 156 | + |
| 157 | + #cooler csort -c1 2 -p1 3 -c2 5 -p2 6 --sep '\t' --out contacts.sorted.txt.gz $INPUT_HICPRO $CHROMSIZES_FILE |
| 158 | + cooler makebins $CHROMSIZES_FILE $RES > $tmp_dir/bins.bed |
| 159 | + cooler cload pairix $tmp_dir/bins.bed $tmp_dir/contacts.sorted.txt.gz $out |
| 160 | + |
| 161 | + echo -e "\nZoomify .cool file ..." |
| 162 | + if [[ $NORMALIZE == 1 ]]; then |
| 163 | + cooler zoomify $out |
| 164 | + else |
| 165 | + cooler zoomify --no-balance $out |
| 166 | + fi |
| 167 | + out=$(basename $INPUT_HICPRO | sed -e 's/_allValidPairs/.mcool/') |
| 168 | +fi |
| 169 | + |
| 170 | +## clean |
| 171 | +#/bin/rm -f $tmp_dir |
| 172 | + |
| 173 | +echo -e "\nCooler file generated with success ..." |
| 174 | +echo "Please copy the file $out in your Higlass input directory and run :" |
| 175 | +echo "docker exec higlass-container python higlass-server/manage.py ingest_tileset --filename /tmp/$out --datatype matrix --filetype cooler" |
| 176 | + |
| 177 | + |
| 178 | + |
0 commit comments