Load Rsamtools and ShortRead
library(Rsamtools)
library(ShortRead)
Create a BamFile that references your file, with a yieldSize (number of records available at one time). Open it
bf = BamFile("<path/to/bam">, yieldSize = 1000000)
open(bf)
to = "<path/to/fastq>"
Write a helper function to read and write one chunk
fun <- function(bf, to) {
chunk <- scanBam(bf, param = ScanBamParam(what = c("seq", "qual")))
fq <- ShortReadQ(chunk[[1]]$seq, chunk[[1]]$qual)
writeFastq(fq, to, "a")
length(fq)
}
and apply this to each chunk
repeat {
## maybe indicate progress?
len <- fun(bf, to)
if (len == 0L)
break
}
Hi ioannis.vardaxis, I never done this in R environment but I can assure that is very easy using samtools in unix. this is the code:
if your BAM is a paired end you have to split the fastq generated
Thanks, but I need it in R since it is part of my R-package :/