]> Creatis software - clitk.git/blob - cluster_tools/mergeDoseByRegions.sh
598df6925c89c48ae1429e4635a052c85beb04c2
[clitk.git] / cluster_tools / mergeDoseByRegions.sh
1 #!/bin/bash
2 set -u
3
4 function usage {
5   echo "$0 -i <result> -j <file2> -o <result>"
6   exit 1
7 }
8
9 function addToPartialResult {
10   IN1=$2
11   IN2=$4
12   RESULT=$6
13
14   test -f ${IN1} && test -f ${IN2} || usage
15
16   TMP="$(mktemp)"
17
18   # check if all 3 text files have the same number of lines
19   nblines=`cat ${IN1} | wc -l`
20   nb2=`cat ${IN2} | wc -l`
21   nb3=`cat ${RESULT} | wc -l`
22
23   if [ $nblines -ne $nb2 ] || [ $nblines -ne $nb3 ]; then
24     echo "Files does not have the same size"
25     exit 1
26   fi
27
28   #Copy the 1st line in output
29   line1=`sed -n "1p" < ${RESULT}`
30   echo "${line1}" >> ${TMP}
31   # for all lines (except the 1st), split according tab
32   # sum the some elements (dose, edep) ...
33   for i in $(seq 2 $nblines); do
34     #Get the 3 lines
35     file1=`sed -n "${i}p" < ${IN1}`
36     file2=`sed -n "${i}p" < ${IN2}`
37     file3=`sed -n "${i}p" < ${RESULT}`
38
39     # sum edep: get the 2 values, sum them and replace it in the file3
40     # The /#./0. is important to add 0 for decimal value between 0 and 1
41     edep1=$(echo ${file1} | cut -f3 -d' ')
42     edep2=$(echo ${file2} | cut -f3 -d' ')
43     edep3=$(python -c "print($edep1+$edep2)")
44     edep3=${edep3/#./0.}
45     file3=$(echo $file3 |awk -v r=${edep3} '{$3=r}1')
46
47     # sum square_edep: get the 2 values, sum them and replace it in the file3
48     sqEdep1=$(echo ${file1} | cut -f5 -d' ')
49     sqEdep2=$(echo ${file2} | cut -f5 -d' ')
50     sqEdep3=$(python -c "print($sqEdep1+$sqEdep2)")
51     sqEdep3=${sqEdep3/#./0.}
52     file3=$(echo $file3 |awk -v r=${sqEdep3} '{$5=r}1')
53
54     # sum dose: get the 2 values, sum them and replace it in the file3
55     dose1=$(echo ${file1} | cut -f6 -d' ')
56     dose2=$(echo ${file2} | cut -f6 -d' ')
57     dose3=$(python -c "print($dose1+$dose2)")
58     dose3=${dose3/#./0.}
59     file3=$(echo $file3 |awk -v r=${dose3} '{$6=r}1')
60
61     # sum square_dose: get the 2 values, sum them and replace it in the file3
62     sqDose1=$(echo ${file1} | cut -f8 -d' ')
63     sqDose2=$(echo ${file2} | cut -f8 -d' ')
64     sqDose3=$(python -c "print($sqDose1+$sqDose2)")
65     sqDose3=${sqDose3/#./0.}
66     file3=$(echo $file3 |awk -v r=${sqDose3} '{$8=r}1')
67
68     # sum n_hits: get the 2 values, sum them and replace it in the file3
69     hit1=$(echo ${file1} | cut -f9 -d' ')
70     hit2=$(echo ${file2} | cut -f9 -d' ')
71     hit3=$(python -c "print($hit1+$hit2)")
72     file3=$(echo $file3 |awk -v r=${hit3} '{$9=r}1')
73
74     # sum n_event_hits: get the 2 values, sum them and replace it in the file3
75     event1=$(echo ${file1} | cut -f10 -d' ')
76     event2=$(echo ${file2} | cut -f10 -d' ')
77     event3=$(python -c "print($event1+$event2)")
78     file3=$(echo $file3 |awk -v r=${event3} '{$10=r}1')
79
80     #Write the output
81     echo "${file3}" >> ${TMP}
82   done
83   mv -f ${TMP} ${RESULT}
84 }
85
86 function divideUncertaintyResult {
87   IN1=$2
88   value=$4
89   RESULT=$6
90
91   TMP="$(mktemp)"
92
93   # check if all 2 text files have the same number of lines
94   nblines=`cat ${IN1} | wc -l`
95   nb3=`cat ${RESULT} | wc -l`
96
97   if [ $nblines -ne $nb3 ]; then
98     echo "Files does not have the same size"
99     exit 1
100   fi
101
102   #Copy the 1st line in output
103   line1=`sed -n "1p" < ${RESULT}`
104   echo "${line1}" >> ${TMP}
105   # for all lines (except the 1st), split according tab
106   # sum the some elements (dose, edep) ...
107   for i in $(seq 2 $nblines); do
108     #Get the lines
109     file1=`sed -n "${i}p" < ${IN1}`
110     file3=`sed -n "${i}p" < ${RESULT}`
111
112     # Divide uncertainty and replace it in the file3
113     edep1=$(echo ${file1} | cut -f3 -d' ')
114     sqEdep1=$(echo ${file1} | cut -f5 -d' ')
115     stdEdep3=$(python <<EOP
116 import math
117 temp=math.sqrt(($sqEdep1/$value-pow($edep1/$value, 2))/($value-1))/$edep1/$value
118 print(temp)
119 EOP
120     )
121     stdEdep3=${stdEdep3/#./0.}
122     file3=$(echo $file3 |awk -v r=${stdEdep3} '{$4=r}1')
123
124     # Divide uncertainty and replace it in the file3
125     dose1=$(echo ${file1} | cut -f6 -d' ')
126     sqDose1=$(echo ${file1} | cut -f8 -d' ')
127     stdDose3=$(python <<EOP
128 import math
129 temp=math.sqrt(($sqDose1/$value-pow($dose1/$value, 2))/($value-1))/$dose1/$value
130 print(temp)
131 EOP
132     )
133     stdDose3=${stdDose3/#./0.}
134     file3=$(echo $file3 |awk -v r=${stdDose3} '{$7=r}1')
135
136     #Write the output
137     echo "${file3}" >> ${TMP}
138   done
139   mv -f ${TMP} ${RESULT}
140 }
141