Hello, I am trying to convert sam to bam and keep getting an error:
The input file must be coordinate sorted and must have gone through fixmates with the mate scoring option on.
[W::sam_read1] Parse error at line 1
[bam_flagstat_core] Truncated file? Continue anyway.
Before this I was doing PE Trimmomatics with following BWA - MEM. Here is my code. I would appreciate any help :( Thank you!
#!/bin/bash -x
INPATH=/dir/subdir
reference=/dir/refseq.fasta
cd /mnt/home/usr/tools/samtools-1.9
./samtools faidx $reference
cd $INPATH
for file in $INPATH/*.sam ;
do
bname=$(basename $file ".sam")
echo $bname
echo file: $file
bamfile=$INPATH/$bname".bam"
namesort=$INPATH/$bname"_sorted.bam"
fixmate=$INPATH/$bname"_sorted_fixmate.bam"
positionsort=$INPATH/$bname"_sorted_fixmate_position.bam"
markdup=$INPATH/$bname"_sorted_fixmate_position_markdup.bam"
bai=$INPATH/$bname".bai"
stats=$INPATH/$bname".txt"
echo $bamfile $namesort $fixmate $positionsort $markdup
cd /mnt/home/usr/tools/samtools-1.9
./samtools view -bT $reference $file > $bamfile
ls -lh
./samtools sort -@ 12 -n $bamfile > $namesort
./samtools fixmate -m $namesort > $fixmate
./samtools sort -@ 12 $fixmate > $positionsort
./samtools markdup $positionsort > $markdup
./samtools index > $markdup
./samtools flagstat $markdup > $stats
done