Skip to content

Instantly share code, notes, and snippets.

@chamalis
Created August 21, 2019 20:36
Show Gist options
  • Save chamalis/54e5a78c03114611648ec59a5f19fd80 to your computer and use it in GitHub Desktop.
Save chamalis/54e5a78c03114611648ec59a5f19fd80 to your computer and use it in GitHub Desktop.
Bash script to convert all SAM files found in a directory to BAM. Directory is passed as positional command line argument.
#!/usr/bin/env bash
for i in $(ls $1); do
filename_prefix="${i%.*}"
filename_extension="${i##*.}"
if [ "$filename_extension" == "sam" ]; then
samtools view -bs $i > $filename_prefix.bam
samtools sort $filename_prefix.bam -o $2
samtools index $2 $2.index
rm $filename_prefix.bam
fi;
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment