]> Creatis software - clitk.git/blob - cluster_tools/mergeDoseByRegions.sh
d7a390af7fa194b5671d927bad94cd82a3bae0fd
[clitk.git] / cluster_tools / mergeDoseByRegions.sh
1 #!/bin/bash
2 set -u
3
4 function usage {
5   echo "$0 -i <file1> -j <file2> -o <result>"
6   exit 1
7 }
8
9 if [ $# != 6 ]
10 then
11   usage
12 fi
13
14 IN1=$2
15 IN2=$4
16 RESULT=$6
17
18 test -f ${IN1} && test -f ${IN2} || usage
19
20 TMP="$(mktemp)"
21 echo "merging dose by regions file"
22 # check if all 3 text files have the same number of lines
23 nblines=`cat ${IN1} | wc -l`
24 nb2=`cat ${IN2} | wc -l`
25 nb3=`cat ${RESULT} | wc -l`
26
27 if [ $nblines -ne $nb2 ] || [ $nblines -ne $nb3 ]; then
28   echo "Files does not have the same size"
29   exit 1
30 fi
31
32 #Copy the 1st line in output
33 line1=`sed -n "1p" < ${RESULT}`
34 echo "${line1}" >> ${TMP}
35
36 # for all lines (except the 1st), split according tab
37 # sum the some elements (dose, edep) ...
38 for i in $(seq 2 $nblines); do
39   #Get the 3 lines
40   line1=`sed -n "${i}p" < ${IN1}`
41   line2=`sed -n "${i}p" < ${IN2}`
42   line3=`sed -n "${i}p" < ${RESULT}`
43
44   # sum edep: get the 2 values, sum them and replace it in the line3
45   # The /#./0. is important to add 0 for decimal value between 0 and 1
46   edep1=$(echo ${line1} | cut -f3 -d' ')
47   edep2=$(echo ${line2} | cut -f3 -d' ')
48   edep3=$(echo "scale=30;$edep1+$edep2" | bc)
49   edep3=${edep3/#./0.}
50   line3=$(echo $line3 |awk -v r=${edep3} '{$3=r}1')
51
52   # sqrt sum square std_edep: get the 2 values, sum the square, take the sqrt and replace it in the line3
53   edep1=$(echo ${line1} | cut -f4 -d' ')
54   edep2=$(echo ${line2} | cut -f4 -d' ')
55   edep3=$(echo "scale=30;sqrt($edep1*$edep1+$edep2*$edep2)" | bc)
56   edep3=${edep3/#./0.}
57   line3=$(echo $line3 |awk -v r=${edep3} '{$4=r}1')
58
59   # sum square_edep: get the 2 values, sum them and replace it in the line3
60   edep1=$(echo ${line1} | cut -f5 -d' ')
61   edep2=$(echo ${line2} | cut -f5 -d' ')
62   edep3=$(echo "scale=30;$edep1+$edep2" | bc)
63   edep3=${edep3/#./0.}
64   line3=$(echo $line3 |awk -v r=${edep3} '{$5=r}1')
65
66   # sum dose: get the 2 values, sum them and replace it in the line3
67   edep1=$(echo ${line1} | cut -f6 -d' ')
68   edep2=$(echo ${line2} | cut -f6 -d' ')
69   edep3=$(echo "scale=30;$edep1+$edep2" | bc)
70   edep3=${edep3/#./0.}
71   line3=$(echo $line3 |awk -v r=${edep3} '{$6=r}1')
72
73   # sqrt sum square std_dose: get the 2 values, sum the square, take the sqrt and replace it in the line3
74   edep1=$(echo ${line1} | cut -f7 -d' ')
75   edep2=$(echo ${line2} | cut -f7 -d' ')
76   edep3=$(echo "scale=30;sqrt($edep1*$edep1+$edep2*$edep2)" | bc)
77   edep3=${edep3/#./0.}
78   line3=$(echo $line3 |awk -v r=${edep3} '{$7=r}1')
79
80   # sum square_dose: get the 2 values, sum them and replace it in the line3
81   edep1=$(echo ${line1} | cut -f8 -d' ')
82   edep2=$(echo ${line2} | cut -f8 -d' ')
83   edep3=$(echo "scale=30;$edep1+$edep2" | bc)
84   edep3=${edep3/#./0.}
85   line3=$(echo $line3 |awk -v r=${edep3} '{$8=r}1')
86
87   # sum n_hits: get the 2 values, sum them and replace it in the line3
88   edep1=$(echo ${line1} | cut -f9 -d' ')
89   edep2=$(echo ${line2} | cut -f9 -d' ')
90   edep3=$(echo "scale=30;$edep1+$edep2" | bc)
91   line3=$(echo $line3 |awk -v r=${edep3} '{$9=r}1')
92
93   # sum n_event_hits: get the 2 values, sum them and replace it in the line3
94   edep1=$(echo ${line1} | cut -f10 -d' ')
95   edep2=$(echo ${line2} | cut -f10 -d' ')
96   edep3=$(echo "scale=30;$edep1+$edep2" | bc)
97   line3=$(echo $line3 |awk -v r=${edep3} '{$10=r}1')
98
99   #Write the output
100   echo "${line3}" >> ${TMP}
101 done
102 mv -f ${TMP} ${RESULT}
103