Samtools sam-> Bam
1
0
Entering edit mode
7.4 years ago
V ▴ 380

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!!

rnaseq sam bam samtools • 3.9k views
ADD COMMENT
0
Entering edit mode
for f in *.sam; do filename="${f%.*}"; samtools view -@ 7 -bS $f > $filename".bam"; done
ADD REPLY
2
Entering edit mode
7.4 years ago

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.

ADD COMMENT
6
Entering edit mode

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
ADD REPLY
0
Entering edit mode

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!

ADD REPLY
0
Entering edit mode

Thanks for providing the bash version, my bash-fu has been deteriorating since snakemake was introduced :P

ADD REPLY
0
Entering edit mode

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??

ADD REPLY
0
Entering edit mode

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")).

ADD REPLY

Login before adding your answer.

Traffic: 2741 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6