Hi friends,

I am attempting to sort my bam files that I obtained from my bowtie sam files. I am not indexing them appropriate according to this error I am receiving after creating my bam file.

    random alignment retrieval only works for indexed BAM or CRAM files.

I understand I am suppose to index the file before sorting them. 

        #creating the appropriate files
        samtools view -Sb sample.sam.pair > sample.pair
        samtools view -bt ~/bigdata/refgenome/genome.fa.fai - - | samtools sort sample.pair -o sample.pair.bam
       
     samtools view -Sb sample.sam.single > sample.single
     samtools view -bt ~/bigdata/refgenome/genome.fa.fai - - | samtools sort sample.single -o sample.single.bam
        
        #merge
        samtools merge sample.all.bam sample.pair.bam sample.single.bam -@ 2
        rm sample.pair sample.single
        
        #index the final bam
        samtools index sample.all.bam

Any help would be appreciated.
I think you're over-thinking things :)

You can only index BAM files on position, and only when the data is sorted by position to begin with (don't ask...)
So to sort by position just do:

    samtools sort my.sam > my_sorted.bam

Then index with

    samtools index my_sorted.bam

It's as easy as that. If you want to merge the output files from bowtie do that as the very first step, because I don't think samtools performs any optimisations for merging sorted BAMs/SAMs. However, i'd also recommend against bowtie2 in favour of STAR or BWA-MEM, but that's just a personal preference at the end of the day.