This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Can A Bam File Be Sorted By The Read Names

Hi

Can we sort BAM files according to the read name? As the normal sorting happens on the co-ordinates, can anyone tell how to sort a BAM file on read names? I am trying to run HT-Seq count on paired end SAM files but receiving warnings for which I have to sort the BAM in read names and then create its SAM and then run HT-Seq

rna-seq

2 answers

pass the -n flag to samtools

samtools sort -n inputfile output

http://samtools.sourceforge.net/samtools.shtml

Can samtools sort sort a sam file by name directly? If I want to sort the sam file, what can I do?

You can sort a samfile with unix sort like so:

  1. remove the headers
  2. sort on the coordinate field
  3. put back the headers

See Picard's SortSam command below. You can sort a sam or bam file using queryname or readname.

http://picard.sourceforge.net/command-line-overview.shtml#SortSam

Log in to answer this question.