Samtools sort should work without redirection. http://davetang.org/wiki/tiki-index.php?page=SAMTools
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?
4 answers
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...
That's an example of old synthax.
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
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.
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.
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
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
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
Log in to answer this question.
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
Just a remark on terminology, what you describe as "script" are commonly called "commands".