samtools and Rsamtools in r
1
0
Entering edit mode
11 months ago
Agamemnon ▴ 80

Hello,

How can I use Rsamtools, so I can import the results from a bam file? E.g. if I use samtools view sample.bam > sample.sam, I can import this sam file easily into R. However, if I run Rsamtools, and do something like

input_bam <- "sample.bam"
output_sam <- "sample"
sam <- asSam(input_bam, destination = output_sam)

sample <- read.delim(output_sam, header=FALSE)

The imported data looks quite different, the latter one has a log of unaligned entries.

samtools • 1.1k views
ADD COMMENT
0
Entering edit mode

what do you mean by a "log of unaligned entries"?

ADD REPLY
0
Entering edit mode

The top sam file has been created with samtools view, and I can easily import it into R. The second has been done with asSAM, it does not properly load into R.

ADD REPLY
0
Entering edit mode
11 months ago

one of your files has a SAM header (the @SQ tags etc); the other does not

the file that you generate with

samtools view sample.bam > sample.sam

is not actually a valid SAM file; it is missing the header because in the usecase the output is typically used to view the alignments at command line, in that usecase the headers get in the way,

to make it equivalent, and to produce a valid SAM file you would need to add the -h flag

samtools view -h sample.bam > sample.sam
ADD COMMENT
0
Entering edit mode

Thank you Istvan Albert , I want the inverse to though, I want rsamtools to generate a Sam file without the headers and then load it into at for further manipulation. So I want the Sam file generated by samtools view.

ADD REPLY
0
Entering edit mode

Rsamtools likely won't generate that output, since that is a subset of a proper SAM output.

You could post-process that file to remove the headers with another line of code or ignore the lines that start with @ when reading the file.

perhaps you could set

read.delim(..., comment.char ='@')
ADD REPLY
0
Entering edit mode

Thanks, will try that.

ADD REPLY

Login before adding your answer.

Traffic: 2704 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