How to trim alignments in a BAM file to a restricted genomic window?


Say I have long reads which I have aligned to hg38.

Now I want to trim my alignments to a specific region of chr20, say between position 2,000,000 and 2,000,500.

I am imagining something like

trim input.bam 'chr20:2000000-2000500' > output.bam

This is similar but not identical to this: Extract Reads From A Bam File That Fall Within A Given Region

Not identical because I do not want to extract complete alignments, I want alignments to be trimmed if necessary so they only cover the region I am interested in.

How can I do this?




have a look at samtools ampliconclip (in a recent version of samtools)

Usage samtools ampliconclip -b bedfile <input.bam> -o <output.bam>

 -b  FILE            bedfile of amplicons to be removed.
 -o  FILE            output file name (default stdout).
 -f  FILE            write stats to file name (default stderr)
 -u                  Output uncompressed data
 --soft-clip         soft clip amplicons from reads (default)
 --hard-clip         hard clip amplicons from reads.
 --both-ends         clip on both ends.

before adding your answer.

Traffic: 2715 users visited in the last hour

Source link