-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdownsample.sh
More file actions
executable file
·61 lines (49 loc) · 1.19 KB
/
downsample.sh
File metadata and controls
executable file
·61 lines (49 loc) · 1.19 KB
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
#!/bin/bash
set -e
usage() { echo "Usage: $0 [-b <bamlist>] [-d <depth>] [-n <cores>] [-o <outdir>]" 1>&2; exit 1; }
dry_run=false
while getopts ":b:d:n:o:D" o; do
case "${o}" in
b)
bamlst=${OPTARG}
;;
d)
deps=${OPTARG}
;;
n)
cores=${OPTARG}
;;
o)
OUT=${OPTARG}
;;
D)
dry_run=true
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${bamlst}" ] || [ -z "${deps}" ] || [ -z "${cores}" ] || [ -z "${OUT}" ]; then
usage
fi
if "${dry_run}"; then
cmd="--dry-run"
else
cmd=""
fi
# OUT=results/downsample
# parallel --dry-run
# deps=(0.5 1.0) # target depth not fraction
for dep in ${deps[@]};do
outdir=$OUT/${dep}x
mkdir -p $outdir
for bam in `cat $bamlst`;do
out=$outdir/`basename $bam`.bam
echo $out >>$outdir/bams.list
echo "frac=\`samtools view -h $bam chr21 | samtools depth -@ 2 -a - | awk '{s+=\$3;} END{print $dep*NR/s}'\`;samtools view -h -s \$frac -o $out $bam; samtools index $out"
done | parallel $cmd -j $cores -k "sh -c {}"
done
wait
echo "downsampling finished"