export GCS_OAUTH_TOKEN=$(gcloud auth application-default print-access-token)
samtools view -L <bed> does not use the index, but rather iterates over all reads start-to-finish. You need to explicitly specify samtools view -ML.
Samtools (and GATK) interval subsetting includes all reads that overlap the interval. This is sometimes not desired behavior, since it makes it difficult to create scatter intervals that are disjoint with respect to reads they contain.
As such, piping the output of samtools view to a simple helper tool is necessary.
bam_set1 behaves almost as expected, except:
read_in), l_qname already accounts for the null padding, but this is not accounted for in bam_set1. It thus must be subtracted off:bam_set1(
&read_out,
read_in.core.l_qname - read_in.core.l_extranul - 1,
bam_get_qname(&read_in),
...
bam_get_l_aux(&read_in)
);
memcpying its entire aux block. Note that the l_data field of the output record must be incremented by the aux length;
passing l_aux to bam_set1 will not automatically do this.memcpy(bam_get_aux(&read_out), bam_get_aux(&read_in), bam_get_l_aux(&read_in));
read_w.l_data += bam_get_l_aux(&read_in);