-
Notifications
You must be signed in to change notification settings - Fork 3
/
cowabunga.sh
executable file
·186 lines (158 loc) · 5.38 KB
/
cowabunga.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
#!/bin/bash
umask 0007
scriptdir="$(dirname $(which $0))"
baseconda="$scriptdir/"
working=working
worktest=work-vp-test
sampleset=sampleset
#
# Input validator
#
validateBatchName() {
if [[ "$1" =~ ^(20[0-9][0-9][0-1][0-9][0-3][0-9]_[[:alnum:]-]{4,})$ ]]; then
return;
else
echo "bad batchname ${1}"
exit 1;
fi
}
validateProto() {
case "$1" in
v3|v4|v41|v532)
return
;;
*)
echo "bad proto ${1}"
exit 1;
;;
esac
}
lastmonth=$(date '+%Y%m' --date='-1 month')
thismonth=$(date '+%Y%m')
case "$1" in
autoaddww|autoaddwastewater)
projects=( 'p23224' 'p24991' 'p26177' 'p30045' )
projpat="$( ( IFS='|';echo "${projects[*]}" ) )"
gawk -v d="${lastmonth}" '$2<d' ${working}/samples.wastewateronly.tsv > ${working}/samples.wastewateronly.tsv.old
{
# assemble samples.wastewateronly.tsv
{
printf '%s\n' ${sampleset}/projects.${thismonth}*.tsv ${sampleset}/projects.${lastmonth}*.tsv | grep -vF '*' | sort -r | while read p; do
echo -n "${p} - ${p//projects/samples} ... " >&2
gawk -v projpat="${projpat}" '(FILENAME~/\/projects\./)&&($2~projpat){ww[$1]++;nww++};(FILENAME~/\/samples\./)&&(ww[$1]);END{print nww >> "/dev/stderr"}' "${p}" "${p//projects/samples}"
done | tee >(cut -f4 >&3) # pass the protos to the second part bellow
cat ${working}/samples.wastewateronly.tsv.old
} > ${working}/samples.wastewateronly.tsv
} 3>&1 | sort | uniq -c | while read cnt PROTO o; do
echo "proto: ${PROTO} (${cnt})"
gawk -v proto="${PROTO}" '$4==proto' ${working}/samples.wastewateronly.tsv > ${working}/samples.wastewateronly.${PROTO}.tsv
done;
;;
shufflebatches)
if [[ -z "$2" || "$2" == "--help" ]]; then
echo "Usage: $0 $1 <BATCH>" 1>&2;
exit 0
fi
BATCH=$2
validateBatchName "${BATCH}"
# check existing
if [[ ! -e "sampleset/samples.${BATCH}.tsv" ]]; then
echo "Cannot find batch ${BATCH}" >&2
exit 1
fi
echo "batch ${BATCH}"
# majority votre proto
read cnt PROTO o < <(cut -f4 sampleset/samples.${BATCH}.tsv | sort | uniq -c | tee /dev/stderr)
echo "proto: ${PROTO}"
#
# Proto validate + specific files
#
case "${PROTO}" in
v3) bedfile="cojac/nCoV-2019.insert.${PROTO^^}.bed" ;;
v4) bedfile="cojac/SARS-CoV-2.insert.${PROTO^^}.txt" ;;
v41) bedfile="cojac/SARS-CoV-2.insert.${PROTO^^}.bed" ;;
*)
echo "bad proto ${PROTO}"
exit 1;
;;
esac
allprototsv=( ${working}/samples.wastewateronly.{v41,v4,v3}.tsv )
if [[ ! -r "${bedfile}" ]]; then
echo "missing bedfile ${bedfile}"
exit 1;
fi
# 'lastweek' backup
if grep -qF "${BATCH}" ${working}/samples.wastewateronly${PROTO:+.${PROTO}}.tsv; then
echo "using:"
ls -l ${working}/samples.wastewateronly${PROTO:+.${PROTO}}{,.lastweek,.thisweek}.tsv
else
echo "backing up lastweek:"
cp -v ${working}/samples.wastewateronly${PROTO:+.${PROTO}}{,.lastweek}.tsv
fi
(grep -vP '^(Y\d{2,}|NC|\d{6,})' "sampleset/samples.${BATCH}.tsv" | tee ${working}/samples.wastewateronly${PROTO:+.${PROTO}}.thisweek.tsv ; cat ${working}/samples.wastewateronly${PROTO:+.${PROTO}}.lastweek.tsv) > ${working}/samples.wastewateronly${PROTO:+.${PROTO}}.tsv
cat "${allprototsv[@]}" > ${working}/samples.wastewateronly.tsv
;;
bring_results)
TSV=samples.wastewateronly.tsv
overwrite=0
if [[ -n "$2" ]]; then
case "$2" in
--overwrite)
overwrite=1
;;
*)
echo "Unkown parameter ${2}" > /dev/stderr
exit 2
;;
esac
fi
# bring current list in
if cp ${scriptdir}/${working}/${TSV} ${scriptdir}/${worktest}/; then
ln -sf "${TSV}" ${worktest}/samples.tsv
else
echo "Cannot find ${TSV}"
exit 1
fi
if (( overwrite )); then
echo "Entirely rebuilding results/ directory"
else
echo "Appending missing samples to results/ directory"
fi
mkdir -p ${worktest}/results/
while read s b o; do
rmdir --ignore-fail-on-non-empty ${worktest}/results/${s}/${b}/
if (( overwrite )); then
rm -rvf ${worktest}/results/${s}/${b}/
elif [[ -e ${worktest}/results/${s}/${b}/ ]]; then
continue
fi
mkdir -p ${worktest}/results/${s}/${b}/
cp -alv ${working}/samples/${s}/${b}/{references,alignments} ${worktest}/results/${s}/${b}/
done < ${worktest}/${TSV}
;;
fetch_cooc)
proto=${2:-v532}
validateProto "${proto}"
echo "fetching ${proto}:"
gawk -v proto="${proto}" '$4==proto' ${worktest}/samples.tsv | while read s b o; do cat ${worktest}/results/${s}/${b}/signatures/cooc.yaml; echo -n '.' >&2; done > ${worktest}/cooc.${proto}.yaml
echo "done"
;;
export)
base_ww=/cluster/project/pangolin/work-vp-test
base_main=/cluster/project/pangolin/working
export_base=/cluster/project/pangolin/export
timeline="${base_ww}/variants/timeline.tsv" # TODO marge with main in the future
mainresults="${base_main}/samples/" # TODO switch to results in the future
tail -n +2 "${timeline}" | while read s b o; do
#mkdir -p --mode=0775 ${export_base}/{,${s}/{,${b}/{,uploads/,alignments/}}}
install --preserve-timestamps --mode=0775 -D --directory "${export_base}/${s}/${b}/"{uploads,alignments}/
install --preserve-timestamps --mode=0664 -D --target-directory="${export_base}/${s}/${b}/alignments/" "${mainresults}/${s}/${b}/alignments/"{dehuman.count,REF_aln_stats.yaml}
cp --link -vf "${mainresults}/${s}/${b}/uploads/dehuman.cram"* "${export_base}/${s}/${b}/uploads/"
chmod o+r "${export_base}/${s}/${b}/uploads/dehuman.cram"*
done
;;
*)
echo "Unkown subcommand ${1}" > /dev/stderr
exit 2
;;
esac