This is a test version of Biostars. For the public version, visit https://www.biostars.org.
The error generated by Rsamtools

Hi there,

I use Rsubread to produce the BAM files and then use Rsamtools to manipulate the BAM files.

A few errors were thrown out:

1)

> indexBam(files = "sample.bam")

Error in FUN(X[[i]], ...) : failed to build index

2)

> asSam(file = "sample.bam") 

> asBam(file = "sample.sam")

Error in value[[3L]](cond) : 'asBam' truncated input file at record 8

I have checked the content of the BAM file generated by Rsubread by converting it into a SAM file and it seems that it uses quite a few CR (carriage return) in the middle of each long line. Does Rsamtools not recognise this format?

Many thanks,

Tom

rsamtools rsubread

Sounds like you have a SAM file rather than a BAM file.

No. I have checked that it is BAM file. I can use asSam() function to convert sample.bam to sample.sam but cannot convert back from sample.sam to sample.bam using asBam() function.

Can you show output of samtools quickcheck -vvv that.bam on that bam file via terminal?

I run the following command line on terminal:

samtools quickcheck -v sample.bam

The output is empty.

Please use vvv, this is not a typo. Tripple-v means "set verbosity to 3" which is "output everything". It will give a more detailed output message from the tool. A simple head(sample.bam) will also tellyou if it is not a SAM file in terms of not being binary.

OK. I have run the command line: samtools quickcheck -vvv sample.bam

The output is below:

verbosity set to 3
checking sample.bam
opened sample.bam
sample.bam is sequence data
sample.bam has 1 targets in header
sample.bam has good EOF block

Hmm, ok next step, checking the header: samtools view -H sample.bam

@HD     VN:1.0  SO:unsorted
@SQ     SN:22   LN:50818468
@PG     ID:subread      PN:subread      VN:Rsubread 1.34.7      CL:"subread-align" "-r" "sample.R1.fastq.gz" "-R" "sample.R2.fastq.gz" "-o" "sample.bam" "-i" "index" "--type" "0" "-n" "10" "-m" "3" "-p" "1" "-M" "3" "-T" "1" "-I" "5" "-B" "1" "-d" "50" "-D" "600" "-S" "fr" "--trim5" "0" "--trim3" "0" "-G" "-1" "-E" "0" "-X" "0" "-Y" "2" "-P" "3"

1 answer

Rsubread can sort and index the BAM file on-the-fly during the alignment. It is much faster to take advantage of that facility than to sort and index the BAM file afterwards.

indexBAM might perhaps assume that the BAM file is location-sorted but, by default, it won't be.

Log in to answer this question.