This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Merging list of bam files with samtools merge

Hi,

I want to merge list of bam files (after I already sorted them previously) using samtools merge. I have a list of bam files that I want to sent from python to the following command in bash:

${samtools} merge - ${bam_source}/${sample_name}.merged.bam

Will samtools work with list from python as input? is the above scrips in general OK?

Thanks!

python bash samtools

I don't see python code in your example.

1 answer

No, it won't work, because you are swapping input and output:

samtools merge <out.bam> <in1.bam> [<in2.bam> ... <inN.bam>]

Besides, venu has a point, you don't show how you plan to use python.

my python script is:

bam_files_list = [file for file in os.listdir(bam_file_path) if file.endswith(".bam")]
subprocess.call([".merge_and_remove_dup.sh", bam_files_list, sample_name],shell=False)

Then the bash command. So you say I need to convert the list to sequence of strings (names of the files)?

So you say I need to convert the list to sequence of strings (names of the files)?

Don't put words on my mouth! ;-)

I really don't know python, all I said is:

1) you asked how you could do something with python, but didn't show any python code,
2) it appears to me your command is samtools merge input output, whereas the correct is samtools merge output input.

What I will say, though, is that I don't understand why do you need to wrap a simple samtools merge around a bash script, then wrap this bash script around python.

Thanks, it won't solve my problem, but it seems that I did confused the output input. The python script is long, so I wrote only the relevant lines from it. It does other things then just merge, so it's easier for me to send from it to simple bash script for merge, and the rest, with python.

Log in to answer this question.