Question: Script to sort bam file not working
1
gravatar for emth4657
2.7 years ago by
emth465710
emth465710 wrote:

I am trying to edit an assembly and I have a .fa file with the sequence I am looking at. I am ultimately trying to create a .vfc file to look at errors in my sequence. I am using the script:

bwa index draft1_assembly.fa

bwa mem draft1_assembly.fa reads_1.fastq reads_2.fastq> draft1.sam

samtools view -b -o draft1.bam -S draft1.sam

samtools sort draft1.bam draft1.sorted 

samtools index draft1.sorted.bam

samtools faidx draft1_assembly.fa

samtools tview draft1.bam draft1_assembly.fa

samtools mpileup -uf draft1_assembly.fa draft1.bam | bcftools call -vc –O v - >draft1_snps_indels.vcf

I have successfully created a .sam file using:

$bwa index H_vit_Rb.fa

$bwa mem H_vit_Rb.fa reads_1.fastq reads_2.fastq> H_vit_Rb.sam

I then successfully created a .bam file using:

$samtools view -b -o H_vit_Rb.bam -S H_vit_Rb.sam

But when I try to create a .sorted file using:

$samtools sort H_vit_Rb.bam H_vit_Rb.sorted

This message appears, not letting me create a .sorted file

[bam_sort] Use -T PREFIX / -o FILE to specify temporary and final output files

Usage: samtools sort [options...] [in.bam]

Options:

 -l INT     Set compression level, from 0 (uncompressed) to 9 (best)

-m INT     Set maximum memory per thread; suffix K/M/G recognized [768M]

-n         Sort by read name

-o FILE    Write final output to FILE rather than standard output

 -T PREFIX  Write temporary files to PREFIX.nnnn.bam

-@, --threads INT

  Set number of sorting and compression threads [1]

 --input-fmt-option OPT[=VAL]

Specify a single input file format option in the form of OPTION or OPTION=VALUE

-O, --output-fmt FORMAT[,OPT[=VAL]]...

 Specify output format (SAM, BAM, CRAM)

--output-fmt-option OPT[=VAL]

 Specify a single output file format option in the form of OPTION or OPTION=VALUE

--reference FILE

Reference sequence FASTA FILE [null]

What does this mean and how do i fix it?

sort blast unix samtools bam • 3.0k views
ADD COMMENTlink modified 2.7 years ago by Santosh Anand4.9k • written 2.7 years ago by emth465710
1

samtools has changed the way sort works - the new invocation is much simpler but is not compatible with the old style

there is a very large number of tutorials out there demonstrating the old style usage

ADD REPLYlink written 2.7 years ago by Istvan Albert ♦♦ 80k

Just a remark on terminology, what you describe as "script" are commonly called "commands".

ADD REPLYlink modified 2.7 years ago • written 2.7 years ago by WouterDeCoster40k
4
gravatar for WouterDeCoster
2.7 years ago by
Belgium
WouterDeCoster40k wrote:

Based on the usage in the error it's quite clear:

You use $samtools sort H_vit_Rb.bam H_vit_Rb.sorted and you should use samtools sort H_vit_Rb.bam > H_vit_Rb.sorted since the result is by default written to stdout. So add the redirection...

ADD COMMENTlink written 2.7 years ago by WouterDeCoster40k

Samtools sort should work without redirection. http://davetang.org/wiki/tiki-index.php?page=SAMTools

ADD REPLYlink written 2.7 years ago by Ron960

That's an example of old synthax.

ADD REPLYlink written 2.7 years ago by WouterDeCoster40k

Thank you that worked!

I am still having some issues if you wouldn't mind taking a look. I used the following script:

samtools index H_vit_Rb.sorted.bam
samtools faidx H_vit_Rb.fa
samtools tview H_vit_Rb.bam H_vit_Rb.fa

and after I run the last part, this message occurs:

Cannot read index for 'H_vit_Rb.bam
ADD REPLYlink modified 2.7 years ago • written 2.7 years ago by emth465710
1

Read the error message, and compare it to your script commands. You indexed the sorted BAM, yet referred to the unsorted BAM in 'tview'. The two files are not the same.

ADD REPLYlink modified 2.7 years ago • written 2.7 years ago by harold.smith.tarheel4.4k
2
gravatar for Jeffin Rockey
2.7 years ago by
Jeffin Rockey1.1k
Karimannoor
Jeffin Rockey1.1k wrote:

In earlier versions of samtools, the command OP have used itself should work. Now that you get error with your present version , and reading the error msg,it looks like just using -o instead of giving prefix like before should get you going forward.

ADD COMMENTlink written 2.7 years ago by Jeffin Rockey1.1k
2
gravatar for Santosh Anand
2.7 years ago by
Santosh Anand4.9k
Santosh Anand4.9k wrote:

Let's try to deparse the error message

[bam_sort] Use -T PREFIX / -o FILE to specify temporary and final output files
Usage: samtools sort [options...] [in.bam]

The program is suggesting you to use -T or -o options since it sees two files, whereas it was expecting only one. The USAGE suggests that the program should be run like this

$ samtools sort options_if_required name_of_input_BAM_file

Two things to note: If the USAGE says something in square bracket [], then that is optional. Things in angular brackets <> are obligatory. So you can run the above program in any of the following way.

$ samtools sort # will give the usage info, I guess
$ samtools sort -o H_vit_Rb.sorted.bam H_vit_Rb.bam # this is what you wanted
$ samtools sort H_vit_Rb.bam > H_vit_Rb.sorted.bam # another solution to your problem
ADD COMMENTlink modified 2.7 years ago • written 2.7 years ago by Santosh Anand4.9k
0
gravatar for Ron
2.7 years ago by
Ron960
United States
Ron960 wrote:

From the manual : Provide the parameters and values, -T and -o http://www.htslib.org/doc/samtools.html

samtools sort -T /tmp/aln.sorted -o aln.sorted.bam aln.bam

ADD COMMENTlink written 2.7 years ago by Ron960

The manual also states:

The sorted output is written to standard output by default, or to the specified file (out.bam) when -o is used.

It's also not necessary to specify the -T

ADD REPLYlink modified 2.7 years ago • written 2.7 years ago by WouterDeCoster40k
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: 986 users visited in the last hour