forked from BradnerLab/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbam_liquidator_bin_counter.sh
executable file
·279 lines (224 loc) · 9.79 KB
/
bam_liquidator_bin_counter.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
#!/usr/bin/env bash
##################################################################################
# The MIT License (MIT)
#
# Copyright (c) 2013 John DiMatteo
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
##################################################################################
# todo: re-write this as a C++ program, and break out the relevant bamliquidator call to a function in
# a separate .c file. use poor man's profiler to understand why this takes so long (I think it is
# disk I/O bound) and maybe speed it up
usage_string="usage: $0 [options] path
Records bamliquidator count on each bin in the given bam file. If the path is
a directory instead of a bam file, then counts the next uncounted file in the
provided directory (can be recursive).
The counts are stored in a mysql database.
OPTIONS:
-c Check results against baseline version (non-zero rcode if any checks fail)
"
usage()
{
echo "$usage_string"
}
# I intend for major version to go up in increments of 100, and minor versions to go up in increments of 1. So for
# example if the version is 205, and a minor change is made (e.g. to speed up performance), then the version can be
# changed to 206. However if a major change is made (e.g. to fix a bug that had resulted in prior counts being
# incorrect), then the version should be changed to 300. Note that a change that fixes a bug that had previously
# prevented a bin on a chromosome from being counted may be incremented by 1 (since the prior successful counts will
# be unchanged). This will allow me to run different versions simultaneously and/or compare performance/correctness
# of different versions.
version=205
baseline_version=200 # used to verify counts haven't changed if baseline checking is enabled
database_name=meta_analysis
mysql_user=counter
gff="HG18_100KB_UNIQUENESS.gff"
both_strands="."
one_summary=1
zero_extension=0
baseline_check=0
while getopts ":c" OPTION
do
case $OPTION in
c)
baseline_check=1
;;
?)
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ -z "$@" ]; then
usage
exit 1
fi
path="$@"
if [ -d "$path" ]; then
echo searching for next unprocessed .bam in $path
while read candidate_file_path
do
echo " trying $candidate_file_path"
insert_run_sql="INSERT INTO run (file_name, start_time, counter_version)
VALUES ('$(basename $candidate_file_path)', NOW(), $version);"
mysql -u$mysql_user $database_name -e "$insert_run_sql" 2> /dev/null
insert_status=$?
if [ $insert_status -eq 0 ]; then
file_path=$candidate_file_path
break
fi
done < <(find $path -type f -name \*.bam)
if [ -z "$file_path" ]; then
echo "No un-processed/un-started bam files found under $path"
exit 1
fi
elif [ -f "$path" ]; then
insert_run_sql="INSERT INTO run (file_name, start_time, counter_version)
VALUES ('$(basename $path)', NOW(), $version);"
mysql -u$mysql_user $database_name -e "$insert_run_sql" 2> /dev/null
insert_status=$?
if [ $insert_status -ne 0 ]; then
echo "$path has already been processed (or at least started)"
echo "Incrementing resume_count"
increment_resume_count_sql="UPDATE run SET resume_count=resume_count+1
WHERE file_name = '$(basename $path)' AND counter_version = $version;"
mysql -u$mysql_user $database_name -e "$increment_resume_count_sql "
update_count_status=$?
if [ $update_count_status -ne 0 ]; then
echo "Failed to update the resume status, error code: $update_count_status"
exit -1
fi
# todo: start at the last processed chromosome/bin
fi
file_path="$path"
else
echo "$path is not a valid path"
usage
exit 1
fi
echo starting with parameters:
echo " baseline_check: $baseline_check"
echo " file_path: $file_path"
file_name=`basename $file_path`
parent_directory=$(basename $(dirname $file_path))
select_chromosomes_sql="SELECT DISTINCT chromosome FROM bins
WHERE gff_name = '$gff'
AND skip = false
ORDER BY LENGTH(chromosome), chromosome";
return_code=0
while read chromosome
do
echo --------------------------------------------------------------------------------
echo counting chromosome $chromosome - `date`
echo
select_bins_sql="SELECT start, end - 1, bin_number FROM bins
WHERE chromosome = '$chromosome' AND gff_name = '$gff' ORDER BY bin_number;"
while read start end bin
do
output=$(./bamliquidator $file_path $chromosome $start $end $both_strands $one_summary $zero_extension 2>&1)
count_status=$?
if [ $count_status -ne 0 ]; then
echo error detected in bamliquidator run
echo " count failed with return code $count_status:"
echo " output: $output"
echo " chromosome=$chromosome, start=$start, end=$end, bin=$bin"
echo " skipping the rest of this chromosome"
echo
insert_error_sql="INSERT INTO errors (file_name,chromosome,bin,status_code,output,counter_version)
VALUES ('$file_name', '$chromosome', $bin, $count_status, '$output', $version);"
mysql -u$mysql_user $database_name -e "$insert_error_sql"
insert_status=$?
if [ $insert_status -ne 0 ]; then
echo "insert failed: $1"
echo sql: $insert_error_sql
exit $insert_status
fi
break
fi
count=$output
#echo status=$count_status
#echo count=$count
insert_count_sql="INSERT DELAYED INTO counts (parent_directory,file_name,chromosome,bin,count,counter_version)
VALUES ('$parent_directory','$file_name', '$chromosome', $bin, $count, $version);"
mysql -u$mysql_user $database_name -e "$insert_count_sql"
insert_status=$?
if [ $insert_status -ne 0 ]; then
# note that this may not error in most conditions since insert delayed doesn't wait for return code
echo "insert failed: $1"
echo sql: $insert_count_sql
exit $insert_status
fi
if [ $baseline_check -ne 0 ]; then
select_baseline_count_sql="SELECT count FROM counts
WHERE counter_version=$baseline_version
AND bin=$bin AND chromosome='$chromosome'
AND file_name='$file_name'"
baseline_count=`mysql -u$mysql_user $database_name -ss -e "$select_baseline_count_sql"`
if [ -z "$baseline_count" ]; then
echo baseline check: baseline count missing
echo " current count: $count"
echo " baseline select sql: " $select_baseline_count_sql
echo
((return_code++))
elif [ $count -ne $baseline_count ]; then
echo baseline check: current count does not match baseline
echo " baseline count: $baseline_count"
echo " current count: $count"
echo " baseline select sql: " $select_baseline_count_sql
echo
((return_code++))
fi
fi
done < <(mysql -u$mysql_user $database_name -ss -e "$select_bins_sql")
done < <(mysql -u$mysql_user $database_name -ss -e "$select_chromosomes_sql")
if [ $baseline_check -ne 0 ]; then
select_baseline_number_of_counts_sql="SELECT count(*) FROM counts
WHERE counter_version=$baseline_version
AND file_name='$file_name';"
select_current_number_of_counts_sql="SELECT count(*) FROM counts
WHERE counter_version=$version
AND file_name='$file_name';"
baseline_number_of_counts=`mysql -u$mysql_user $database_name -ss -e "$select_baseline_number_of_counts_sql"`
current_number_of_counts=`mysql -u$mysql_user $database_name -ss -e "$select_current_number_of_counts_sql"`
if [ -z "$baseline_number_of_counts" ]; then
echo baseline check: baseline completely missing
echo " current number of counts: $current_number_of_counts"
echo " baseline select number of counts sql: " $select_baseline_number_of_counts_sql
echo
((return_code++))
elif [ $baseline_number_of_counts -ne $current_number_of_counts ]; then
echo baseline check: current count does not match baseline
echo " baseline number of counts: $baseline_number_of_counts"
echo " current number of counts: $current_number_of_counts"
echo " baseline select number of counts sql: " $select_baseline_number_of_counts_sql
echo
((return_code++))
fi
fi
update_run_end_time_sql="UPDATE run SET end_time=NOW()
WHERE file_name = '$file_name' AND counter_version = $version;"
mysql -u$mysql_user $database_name -e "$update_run_end_time_sql"
update_status=$?
if [ $update_status -ne 0 ]; then
echo Failed to execute sql: $update_run_end_time_sql
date
exit 1
fi
exit $return_code