This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Failed to merge BAM files

Hi, I have the following bash script:

#!/bin/bash
#usage: sh split_bam2sam-fasta_pbs.sh /path/to/reference.fasta /path/to/outputfolder/ /path/to/input/*.sorted.dedup.bam

output=$2
mkdir $output
merged=$(basename ${output})
echo $merged
#samlist=$(printf 'I="%s" ' "$@")
samlist=$(printf '%s ' $@)

echo ${samlist}

cat << EOF  |cat #qsub
#!/bin/bash -l

#PBS -N rna-seq-fix
#PBS -l walltime=20:00:00
#PBS -j oe
#PBS -l select=1:ncpus=8:mem=70G
#PBS -M m.lorenc@qut.edu.au
##PBS -m bea

cd \$PBS_O_WORKDIR

conda activate bowtie2
samtools merge ${output}/${merged}.sorted.dedup.bam ${samlist}
samtools index ${output}/${merged}.sorted.dedup.bam
conda deactivate

cd ${output}

conda activate gatk
ln -s $1 .
java -jar /work/waterhouse_team/apps/gatk-4.1.8.0/gatk-package-4.1.8.0-local.jar SplitNCigarReads -R $1 -I ${merged}.sorted.dedup.bam -O ${merged}.sorted.dedup.splitN.bam

EOF

I ran it in the following way:

bash ~/scripts/rna-mapping-fix_pbs.sh /scratch/waterhouse_team/QLD/sparse-orginal-77/bionano/juicer/juicebox2fasta/annotation/QLD0.81-pilon-3-andDebris.fasta-windowmasker.fasta root-merged root*/bams2/Qroot*.fastp.fq.gz.uniq.sorted.bam

Unfortunately, the merged command failed because QLD0.81-pilon-3-andDebris.fasta-windowmasker.fasta and root-merged

samtools merge: failed to read header from "/scratch/waterhouse_team/QLD/sparse-orginal-77/bionano/juicer/juicebox2fasta/annotation/QLD0.81-pilon-3-andDebris.fasta-windowmasker.fasta

How is it possible to modify the above the script to only use BAM files?

Thank you in advance,

bash samtools

1 answer

You can create a samlist of SAM or BAM files only like below:

For SAM samlist=$(ls *.sam)

For BAM samlist=$(ls *.bam)

That samlist you can use in samtools merge command of your script.

Log in to answer this question.