-
Notifications
You must be signed in to change notification settings - Fork 3
/
subset_bam_per_cb.sh
executable file
·172 lines (143 loc) · 5.92 KB
/
subset_bam_per_cb.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
#!/bin/bash
#
# Copyright (C) 2024 - Gert Hulselmans
#
# Purpose:
# Subset BAM file per cell barcode.
set -e
set -o pipefail
subset_bam_file_per_cb_chunk () {
local bam_file="${1}";
local barcodes_file="${2}";
local bam_output_prefix="${3%.}";
if [ ${#@} -ne 3 ] ; then
printf 'Usage:\n';
printf ' subset_bam_file_per_cb_chunk bam_file barcodes_file bam_output_prefix\n\n';
printf 'Arguments:\n';
printf ' bam_file: BAM file to subset per provided cell barcode.\n';
printf ' barcodes_file: File with cell barcodes to subset input BAM file.\n';
printf ' bam_output_prefix: Prefix used for output per CB BAM files.\n';
return 1;
fi
if [ "${bam_file##*.}" != "bam" ] ; then
printf 'Error: BAM file should have ".bam" extension.\n';
return 1;
fi
if [ ! -f "${bam_file}" ] ; then
printf 'Error: BAM file "%s" does not exist.\n' "${bam_file}";
return 1;
fi
if [ ! -f "${barcodes_file}" ] ; then
printf 'Error: Barcodes file "%s" does not exist.\n' "${barcodes_file}";
return 1;
fi
if [ ! -d $(dirname "${bam_output_prefix}") ] ; then
printf 'Error: Directory "%s" does not exist.\n' "$(dirname "${bam_output_prefix}")";
return 1;
fi
# First extract all requested cell barcodes from BAM file
# before writing individual per CB BAM files.
samtools view -@ 4 -h -D CB:"${barcodes_file}" "${bam_file}" \
| mawk \
-v bam_output_prefix="${bam_output_prefix}" \
-F '\t' \
'
BEGIN {
header = "";
CB_tag_found = 0;
}
{
if ( $0 ~ /^@/ ) {
# Collect SAM header.
if ( header == "" ) {
header = $0;
} else {
header = header "\n" $0;
}
} else {
CB_tag_found = 0;
# Search for CB tag.
for ( i = 12; i <= NF; i++ ) {
if ( substr($i, 1, 5) == "CB:Z:" ) {
CB = substr($i, 6);
CB_tag_found = 1;
break;
}
}
if ( CB_tag_found == 1 ) {
if (! (CB in CBs_found) ) {
# Write BAM header if this is the first time this CB was seen.
print header | "samtools view -O bam -o " bam_output_prefix "." CB ".bam -";
CBs_found[CB] = 1;
}
# Write read to CB specific BAM file.
print $0 | "samtools view -O bam -o " bam_output_prefix "." CB ".bam -";
}
}
}
END {
# Close all file handles.
for ( CB in CBs_found ) {
close("samtools view -O bam -o " bam_output_prefix "." CB ".bam -");
}
}
'
}
subset_bam_file_per_cb () {
local bam_file="${1}";
local barcodes_file="${2}";
local bam_output_prefix="${3%.}";
# Number of barcodes to process in one invocation of subset_bam_file_per_cb_chunk.
# This is used to limit the number of open file handles and the number of parallel
# spawned samtools processes.
local chunk_size="${4:-1000}";
if [ ${#@} -lt 3 ] ; then
printf 'Usage:\n';
printf ' subset_bam_file_per_cb bam_file barcodes_file bam_output_prefix [chunk_size]\n\n';
printf 'Arguments:\n';
printf ' bam_file: BAM file to subset per provided cell barcode.\n';
printf ' barcodes_file: File with cell barcodes to subset input BAM file.\n';
printf ' bam_output_prefix: Prefix used for output per CB BAM files.\n';
printf ' chunk_size: Number of cell barcodes to process simultaniously.\n'
printf ' If more cell barcodes are provided, the input BAM file\n';
printf ' will be read multiple times. It is recommended to not\n';
printf ' set this value too high as the same number of parallel\n';
printf ' spawned samtools processes will be created for writing\n';
printf ' the per CB BAM files.\n';
printf ' Default: 1000.\n';
return 1;
fi
if [ "${bam_file##*.}" != "bam" ] ; then
printf 'Error: BAM file should have ".bam" extension.\n';
return 1;
fi
if [ ! -f "${bam_file}" ] ; then
printf 'Error: BAM file "%s" does not exist.\n' "${bam_file}";
return 1;
fi
if [ ! -f "${barcodes_file}" ] ; then
printf 'Error: Barcodes file "%s" does not exist.\n' "${barcodes_file}";
return 1;
fi
if [ ! -d $(dirname "${bam_output_prefix}") ] ; then
printf 'Error: Directory "%s" does not exist.\n' "$(dirname "${bam_output_prefix}")";
return 1;
fi
if [ $(cat ${barcodes_file} | wc -l) -le ${chunk_size} ] ; then
# If barcodes file contains less than ${chunk_size} lines, process all barcodes at once.
subset_bam_file_per_cb_chunk "${bam_file}" "${barcodes_file}" "${bam_output_prefix}";
return $?;
else
# Split barcodes file in chunks of ${chunk_size} lines.
local subset_bam_file_per_cb_tmp_dir=$(mktemp -t -d subset_bam_file_per_cb_XXXXXX);
split -l ${chunk_size} -d "${barcodes_file}" ${subset_bam_file_per_cb_tmp_dir}/barcodes_part
# Subset BAM file in per chunk of barcodes.
for barcodes_part_file in ${subset_bam_file_per_cb_tmp_dir}/barcodes_part* ; do
subset_bam_file_per_cb_chunk "${bam_file}" "${barcodes_part_file}" "${bam_output_prefix}";
done
# Remove temporary files.
rm ${subset_bam_file_per_cb_tmp_dir}/barcodes_part*;
rmdir "${subset_bam_file_per_cb_tmp_dir}";
fi
}
subset_bam_file_per_cb "${@}";