Skip to content

Instantly share code, notes, and snippets.

@huxs001
Forked from Shians/extract_bam_tag.sh
Created October 11, 2024 01:38
Show Gist options
  • Save huxs001/13b4a49b001923a358d7b4613f348674 to your computer and use it in GitHub Desktop.
Save huxs001/13b4a49b001923a358d7b4613f348674 to your computer and use it in GitHub Desktop.
Extract BAM tag as tsv
#!/usr/bin/env bash
# called by
# sh extract_bam_tag.sh input.bam BC
# to print read_id and BC tag value
# two arguments, a bam file and the tag to extract
BAM=$1
TAG=$2
# write a tsv with columns read_id and tag value
echo -e "read_id\t$TAG"
samtools view "$BAM" | grep "$TAG:." | perl -pe 's/(^.+?)\t.*'$TAG':.:(.+?)\t.*/$1\t$2/g'
# regular expression substutes pattern (read_id)*(tag_value)* for (read_id)\t(tag_value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment