Question: How To Sort Sam File
8
gravatar for Chen
5.0 years ago by
Chen770
Chen770 wrote:

I want to process the sam file created by BWA, if it is sorted by the sequence coordinate on reference, it will be easier.

How can I sort my sam file?

do I need to first convert sam file to bam file, then use samtools sort to sort the bam file, then convert bam file back to sam file?

Is there any tools that can do this, or should I write the tools by myself?

samtools sam • 28k views
ADD COMMENTlink modified 11 months ago by Biostar ♦♦ 20 • written 5.0 years ago by Chen770
13
gravatar for Pierre Lindenbaum
5.0 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum117k wrote:

use an unix pipeline, run your downstream analysis using the convenient BAM format.

$ bwa mem  ref.fasta f1.fq.gz f2.fq.gz | samtools view  -Sb - | samtools sort - sorted && samtools index sorted.bam
ADD COMMENTlink written 5.0 years ago by Pierre Lindenbaum117k
1

2018: with a recent samtools , the syntax has changed and you don't need to use 'samtools view'

ADD REPLYlink written 11 months ago by Pierre Lindenbaum117k

Hi,

I have tried to use the below pipeline based on the above suggestion. 

$BWAPATH/bwa mem $REFPATH/hg19 $DATPATH/R1.fastq.gz $DATPATH/R2.fastq.gz | $SAMPATH/samtools view -Sb - | $SAMPATH/samtools sort - sorted && $SAMPATH/samtools index - > $DATPATH/sorted.bam

The subsequent step is failing with error message "sorted.bam is not coordinate sorted." To my surprsie, though the step has completed successfully, it created an empty file.

Could you please help me understand what might be the reason ?

Edit:

This has been posted in a new thread not to hijack this unnoticed one.

samtools error when using pipeline view/sort/index

 

ADD REPLYlink modified 3.8 years ago • written 3.8 years ago by deepue110
2
gravatar for Ashutosh Pandey
5.0 years ago by
Philadelphia
Ashutosh Pandey11k wrote:

You can use Picard.

Picard SortSam function can take SAM formatted file as input and output coordinate sorted BAM file as output. See this: http://picard.sourceforge.net/command-line-overview.shtml#SortSam

A usage will look like:

java -jar -Xmx40g /home/xyz/picard-tools-1.78/SortSam.jar  VALIDATION_STRINGENCY=LENIENT  MAX_RECORDS_IN_RAM=7500000 TMP_DIR=/scratch/  INPUT=/home/xyz/Some.sam  OUTPUT=/home/xyz/some.bam  SORTORDER=coordinate
ADD COMMENTlink modified 5.0 years ago by Istvan Albert ♦♦ 79k • written 5.0 years ago by Ashutosh Pandey11k
2
gravatar for DVA
2.9 years ago by
DVA490
United States
DVA490 wrote:

If I understand your question correctly, you can do it using samtools sort with -O command:

samtools sort -O sam -T sample.sort -o sample.sort.sam sample.sam

where: -O is to specify output format; -T is to specify prefix, and I think it's needed because samtools sort create a few temporary files with this prefix; -o is your output file name. sample.sam is the original input.

Doing so, you basically sort sam file, and directly output sam file in one command.

Hope it help.

ADD COMMENTlink modified 2.9 years ago • written 2.9 years ago by DVA490

sort: invalid option -- 'O' sort: invalid option -- 'T'

This is the error I get if I try this command. I don't see -O and -T flags in the man pages on SamTools. From where you got this flags?

ADD REPLYlink written 2.2 years ago by Grinch30
1

Did you by chance do sort -O sam -T sample.sort -o sample.sort.sam sample.sam instead of samtools sort -O sam -T sample.sort -o sample.sort.sam sample.sam? Becuase the error you get doesn't look like a samtools error.

ADD REPLYlink written 2.2 years ago by WouterDeCoster37k

I don't see -O and -T flags in the man pages

http://www.htslib.org/doc/samtools.html

-O FORMAT

    Write the final output as sam, bam, or cram.

    By default, samtools tries to select a format based on the -o filename extension; if output is to standard output or no format can be deduced, bam is selected. 
-T PREFIX

    Write temporary files to PREFIX.nnnn.bam, or if the specified PREFIX is an existing directory, to PREFIX/samtools.mmm.mmm.tmp.nnnn.bam, where mmm is unique to this invocation of the sort command. 
ADD REPLYlink written 2.2 years ago by Pierre Lindenbaum117k
1
gravatar for QVINTVS_FABIVS_MAXIMVS
5.0 years ago by
USA SoCal
QVINTVS_FABIVS_MAXIMVS2.2k wrote:

Samtools has a build in sort command. If you're interested in knowing more about any command or program usually > x.sh --help or > x.sh -h works. For some bash command typing 'man' before a command opens the manual

ADD COMMENTlink written 5.0 years ago by QVINTVS_FABIVS_MAXIMVS2.2k
1
gravatar for gganebnyi
3.8 years ago by
gganebnyi10
Moldova
gganebnyi10 wrote:

Try this visual tool to explore SamSort options and get command you need: http://bash.works/#tools/555c90e849e7235135cbb314/show

ADD COMMENTlink modified 3.8 years ago • written 3.8 years ago by gganebnyi10
0
gravatar for Samad
3.8 years ago by
Samad90
France
Samad90 wrote:

Hi, 

I had the same question about sorting the sam files, here is another solution, using unix command: 

grep -v '^[[:space:]]*@' test.sam | sort -k3,3 -k4,4n  > test.sorted.sam

(source methylKit R package)

ADD COMMENTlink written 3.8 years ago by Samad90
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 734 users visited in the last hour