]> Creatis software - clitk.git/blob - cluster_tools/mergeDoseByRegions.sh
0548db6372d87354637939d608e867513e3842cf
[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     # sqrt sum square std_edep*edep: get the 2 values, sum the square, take the sqrt and replace it in the file3
48     stdEdep1=$(echo ${file1} | cut -f4 -d' ')
49     stdEdep2=$(echo ${file2} | cut -f4 -d' ')
50     stdEdep3=$(python <<EOP
51 import math;
52 temp=$edep2*$stdEdep2;
53 print(math.sqrt($stdEdep1*$stdEdep1+temp*temp))
54 EOP
55     )
56     stdEdep3=${stdEdep3/#./0.}
57     file3=$(echo $file3 |awk -v r=${stdEdep3} '{$4=r}1')
58
59     # sum square_edep: get the 2 values, sum them and replace it in the file3
60     sqEdep1=$(echo ${file1} | cut -f5 -d' ')
61     sqEdep2=$(echo ${file2} | cut -f5 -d' ')
62     sqEdep3=$(python -c "print($sqEdep1+$sqEdep2)")
63     sqEdep3=${sqEdep3/#./0.}
64     file3=$(echo $file3 |awk -v r=${sqEdep3} '{$5=r}1')
65
66     # sum dose: get the 2 values, sum them and replace it in the file3
67     dose1=$(echo ${file1} | cut -f6 -d' ')
68     dose2=$(echo ${file2} | cut -f6 -d' ')
69     dose3=$(python -c "print($dose1+$dose2)")
70     dose3=${dose3/#./0.}
71     file3=$(echo $file3 |awk -v r=${dose3} '{$6=r}1')
72
73     # sqrt sum square std_dose*dose: get the 2 values, sum the square, take the sqrt and replace it in the file3
74     stdDose1=$(echo ${file1} | cut -f7 -d' ')
75     stdDose2=$(echo ${file2} | cut -f7 -d' ')
76     stdDose3=$(python <<EOP
77 import math;
78 temp=$dose2*$stdDose2;
79 print(math.sqrt($stdDose1*$stdDose1+temp*temp))
80 EOP
81     )
82     stdDose3=${stdDose3/#./0.}
83     file3=$(echo $file3 |awk -v r=${stdDose3} '{$7=r}1')
84
85     # sum square_dose: get the 2 values, sum them and replace it in the file3
86     sqDose1=$(echo ${file1} | cut -f8 -d' ')
87     sqDose2=$(echo ${file2} | cut -f8 -d' ')
88     sqDose3=$(python -c "print($sqDose1+$sqDose2)")
89     sqDose3=${sqDose3/#./0.}
90     file3=$(echo $file3 |awk -v r=${sqDose3} '{$8=r}1')
91
92     # sum n_hits: get the 2 values, sum them and replace it in the file3
93     hit1=$(echo ${file1} | cut -f9 -d' ')
94     hit2=$(echo ${file2} | cut -f9 -d' ')
95     hit3=$(python -c "print($hit1+$hit2)")
96     file3=$(echo $file3 |awk -v r=${hit3} '{$9=r}1')
97
98     # sum n_event_hits: get the 2 values, sum them and replace it in the file3
99     event1=$(echo ${file1} | cut -f10 -d' ')
100     event2=$(echo ${file2} | cut -f10 -d' ')
101     event3=$(python -c "print($event1+$event2)")
102     file3=$(echo $file3 |awk -v r=${event3} '{$10=r}1')
103
104     #Write the output
105     echo "${file3}" >> ${TMP}
106   done
107   mv -f ${TMP} ${RESULT}
108 }
109
110 function addWithoutPartialResult {
111   IN1=$2
112   RESULT=$4
113
114   test -f ${IN1} || usage
115
116   TMP="$(mktemp)"
117
118   nblines=`cat ${IN1} | wc -l`
119
120   #Copy the 1st line in output
121   line1=`sed -n "1p" < ${IN1}`
122   echo "${line1}" >> ${TMP}
123   # for all lines (except the 1st), split according tab
124   # sum the some elements (dose, edep) ...
125   for i in $(seq 2 $nblines); do
126     #Get the lines
127     file1=`sed -n "${i}p" < ${IN1}`
128
129     # copy id
130     id1=$(echo ${file1} | cut -f1 -d' ')
131     id1=${id1/#./0.}
132     file3=$(echo $id1)
133
134     # copy volume
135     vol1=$(echo ${file1} | cut -f2 -d' ')
136     vol1=${vol1/#./0.}
137     file3=$(echo "$file3 $vol1")
138
139     # copy edep
140     edep1=$(echo ${file1} | cut -f3 -d' ')
141     edep1=${edep1/#./0.}
142     file3=$(echo "$file3 $edep1")
143
144     # sqrt sum square std_edep*edep
145     stdEdep1=$(echo ${file1} | cut -f4 -d' ')
146     stdEdep3=$(python <<EOP
147 import math;
148 temp=$edep1*$stdEdep1;
149 print(math.sqrt(temp*temp))
150 EOP
151     )
152     stdEdep3=${stdEdep3/#./0.}
153     file3=$(echo "$file3 $stdEdep3")
154
155     # copy square_edep
156     sqEdep1=$(echo ${file1} | cut -f5 -d' ')
157     sqEdep1=${sqEdep1/#./0.}
158     file3=$(echo "$file3 $sqEdep1")
159
160     # copy dose
161     dose1=$(echo ${file1} | cut -f6 -d' ')
162     dose1=${dose1/#./0.}
163     file3=$(echo "$file3 $dose1")
164
165     # sqrt sum square std_dose*dose
166     stdDose1=$(echo ${file1} | cut -f7 -d' ')
167     stdDose3=$(python <<EOP
168 import math;
169 temp=$dose1*$stdDose1;
170 print(math.sqrt(temp*temp))
171 EOP
172     )
173     stdDose3=${stdDose3/#./0.}
174     file3=$(echo "$file3 $stdDose3")
175
176     # copy square_dose
177     sqDose1=$(echo ${file1} | cut -f8 -d' ')
178     sqDose1=${sqDose1/#./0.}
179     file3=$(echo "$file3 $sqDose1")
180
181     # copy n_hits
182     hit1=$(echo ${file1} | cut -f9 -d' ')
183     hit1=${hit1/#./0.}
184     file3=$(echo "$file3 $hit1")
185
186     # copy n_event_hits
187     event1=$(echo ${file1} | cut -f10 -d' ')
188     event1=${event1/#./0.}
189     file3=$(echo "$file3 $event1")
190
191     #Write the output
192     echo "${file3}" >> ${TMP}
193   done
194   mv -f ${TMP} ${RESULT}
195 }
196
197
198 function divideUncertaintyResult {
199   IN1=$2
200   value=$4
201   RESULT=$6
202
203   TMP="$(mktemp)"
204
205   # check if all 3 text files have the same number of lines
206   nblines=`cat ${IN1} | wc -l`
207   nb3=`cat ${RESULT} | wc -l`
208
209   if [ $nblines -ne $nb3 ]; then
210     echo "Files does not have the same size"
211     exit 1
212   fi
213
214   #Copy the 1st line in output
215   line1=`sed -n "1p" < ${RESULT}`
216   echo "${line1}" >> ${TMP}
217   # for all lines (except the 1st), split according tab
218   # sum the some elements (dose, edep) ...
219   for i in $(seq 2 $nblines); do
220     #Get the lines
221     file1=`sed -n "${i}p" < ${IN1}`
222     file3=`sed -n "${i}p" < ${RESULT}`
223
224     # Divide uncertainty and replace it in the file3
225     edep1=$(echo ${file1} | cut -f3 -d' ')
226     stdEdep1=$(echo ${file1} | cut -f4 -d' ')
227     stdEdep3=$(python <<EOP
228 temp=$stdEdep1/$value
229 if $edep1!=0:
230   temp/=$edep1
231 if temp==0:
232   temp=1
233 print(temp)
234 EOP
235     )
236     stdEdep3=${stdEdep3/#./0.}
237     file3=$(echo $file3 |awk -v r=${stdEdep3} '{$4=r}1')
238
239     # Divide uncertainty and replace it in the file3
240     dose1=$(echo ${file1} | cut -f6 -d' ')
241     stdDose1=$(echo ${file1} | cut -f7 -d' ')
242     stdDose3=$(python <<EOP
243 temp=$stdDose1/$value
244 if $dose1!=0:
245   temp/=$dose1
246 if temp==0:
247   temp=1
248 print(temp)
249 EOP
250     )
251     stdDose3=${stdDose3/#./0.}
252     file3=$(echo $file3 |awk -v r=${stdDose3} '{$7=r}1')
253
254     #Write the output
255     echo "${file3}" >> ${TMP}
256   done
257   mv -f ${TMP} ${RESULT}
258 }
259