Hello everyone,

I am fairly new to bioinformatics. I am currently learning the command line and was writing a very
simple script to align multiple files (Hepatitis C viral DNA sequencing) to reference genome using BWA, and then using samtools to align to the reference.

I seem to be getting a problem in the index step.

I keep getting the same error:

    [E::hts_open_format] Failed to open file HCV242.sorted.bam
    samtools index: failed to open "HCV242.sorted.bam": No such file or directory

Below is the whole script:

    SAMPLE_LIST="HCV176 HCV242"
    for SAMPLE_ID in $SAMPLE_LIST
    
    do
    	#Index the aligner with the reference genome
    	bwa index -a is HCV1.fasta
    	# Align reads 1 of samples to ref genome
    	bwa aln HCV1.fasta $SAMPLE_ID.1.fastq >$SAMPLE_ID.1.sai
    	# Align reads 2 of samples to ref genome
    	bwa aln HCV1.fasta $SAMPLE_ID.2.fastq >$SAMPLE_ID.2.sai
    	# Merge and change into SAM format for both samples
    	bwa sampe HCV1.fasta $SAMPLE_ID.1.sai $SAMPLE_ID.2.sai $SAMPLE_ID.1.fastq $SAMPLE_ID.2.fastq > $SAMPLE_ID.sam
    	bwa sampe HCV1.fasta $SAMPLE_ID.1.sai $SAMPLE_ID.2.sai $SAMPLE_ID.1.fastq $SAMPLE_ID.2.fastq > $SAMPLE_ID.sam
    	# Change the SAM to BAM format for both samples
    	samtools view -bt HCV1.fasta $SAMPLE_ID.sam>$SAMPLE_ID.bam
    	# Sort the BAM files
    	samtools sort $SAMPLE_ID.bam -o $SAMPLE_ID.sorted
    	# Create the index file for the BAM files
    	samtools index -b  $SAMPLE_ID.sorted.bam
    done


I've tested the code and everything works until the index command. All the necessary files are generated as expected up until that step.
Any help much appreciated.
Are you getting a truncated file name after the `sort` step, like `HCV242.sorted`? In that case you should change `samtools sort $SAMPLE_ID.bam -o $SAMPLE_ID.sorted` to `samtools sort $SAMPLE_ID.bam -o $SAMPLE_ID.sorted.bam` and see if that fixes the issue.