bash for merging bam files by samtools
0
0
Entering edit mode
9 days ago
QX ▴ 20

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 • 265 views
ADD COMMENT
1
Entering edit mode

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

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

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

ADD REPLY

Login before adding your answer.

Traffic: 2436 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