Hello,
I have a list of sam files that were created using HISAT2. I need to convert all of them into bam files for downstream analysis and I do so using the following (so it runs on all sam files in the directory)
samtools view -@ 7 -b -S -o *.sam > *.bam
the operation runs but nothing happens in the output files (I can't even see them). Ideally I would want a bam file being in the output titled as the sam file that was used for input.
Help..?
Thanks!!
1 answer
You're not going to do this with shell globbing (i.e., *.sam and *.bam). You're either going to write a little for loop in bash or something like python or snakemake. I find snakemake quite simple to use, in which case you'd do:
import glob
files = glob.glob("*.sam")
targets = ["{}.bam".format(x[:-4]) for x in files]
rule all:
input: targets
rule convert:
input "{sample}.sam"
output: "{sample}.bam"
threads: 7
shell: "samtools view -@ {threads} -b -o {output} {input}"
I've taken the liberty of fixing the samtools command.
Devon is right. just to be a little bit more explicit, the quick bash alternative to generate .bam files from .sam files would be
for file in *.sam; do samtools view -bS $file > ${file/.sam/.bam}; done
Thanks Devon & Jorge!! Jorge's approach was more straightforward as I'm new to ubuntu. I tried Devon's but it kept running into different issues and trying to run a "Yagi=Uda antenna analysis program" for some reason. Probably a mistake I did on my end even though I only copied and pasted the command.
Thanks again!
Thanks for providing the bash version, my bash-fu has been deteriorating since snakemake was introduced :P
Hi Devon,
When I actually paste your code in the first thing that happens is that my pointer turns into a target looking thing and I have to click somewhere (and then everything crashes) what is it that the import glob actually does??
This is python/snakemake code, so you would need to write this to a file. import glob imports the glob python module, which allows things like "give me all files ending in '.sam' in the current directory" (glob.glob("*.sam")).
Log in to answer this question.