This is a test version of Biostars. For the public version, visit https://www.biostars.org.
bash for merging bam files by samtools

Hi all,

I try to write a bash to merge all input bam files; however, I found it difficult to concatenate a path to the files.

What I would like to receive is that if i put an A_barcode, B_barcode, C_barcode.bam files, and a path: ${bam_dir}='user/bam_merge/', I would like the command to run:

samtools merge barcode.sorted.merge.bam user/bam_merge/A_barcode.bam user/bam_merge/B_barcode.bam user/bam_merge/C_barcode.bam

I try this but not working.

./merge.sh *bam

    #!/bin/bash
    input_bams=$@
    output_bam=$(echo $input_bams | awk -F'_' '{print $2}' | uniq).sorted.merge.bam
    echo "Input: $input_bams"
    echo "Output: $output_bam"

    # add path to input:
    input=($(awk -v base="${bam_dir}" '{for (i=1; i<=NF; i++) print base"/"$i}' <<< "${input_bams[@]}"))
    # add path to output:
    output=${bam_dir}$output_bam

    echo "### RUNNING merge job --------------------------------------------------"
    $samtools merge $output $input

also, do I need to sort again the merged file after I merge all these sorted files?

Best,

samtools

looks complicated , is it worth a shell script ?, how about 'just' using the option -b of samtools merge ?

  -b FILE    List of input BAM filenames, one per line [null]

I try to parallel job on slurm with many files, this is just example. I find a way to do this by loop, despite it look quite wordy:

input=""
for bam in $@; do
  # Add the path to the argument from input $
  path_bam="${bam_dir}${bam}"
  # Concatenate the argument to the result
  input="${input} ${path_bam}"
done

QX : Don't use the " for highlighting code. Use 101010 to post code in monospaced font.

0 answers

No answers yet.

Log in to answer this question.