Script to sort bam file not working
4
1
Entering edit mode
7.5 years ago
emth4657 ▴ 10

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?

Unix bam blast samtools sort • 7.3k views
ADD COMMENT
1
Entering edit mode

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 REPLY
0
Entering edit mode

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

ADD REPLY
4
Entering edit mode
7.5 years ago

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 COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

That's an example of old synthax.

ADD REPLY
0
Entering edit mode

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 REPLY
1
Entering edit mode

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 REPLY
2
Entering edit mode
7.5 years ago
Jeffin Rockey ★ 1.3k

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 COMMENT
2
Entering edit mode
7.5 years ago

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 COMMENT
0
Entering edit mode
7.5 years ago
Ron ★ 1.2k

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 COMMENT
0
Entering edit mode

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 REPLY

Login before adding your answer.

Traffic: 1334 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6