How to randomly sample BAM files
1
@wdesouza
Last seen 4.0 years ago
Brazil
Is it possible to randomly sample BAM files using Rsamtools or GenomicAlignments packages? I would like to write a code similar to sample FASTQ files (using ShortRead):
fl <- "path_to_file.fastq"
f <- FastqSampler(fl, n=1e6)
chunk <- yield(f)
close(f)
Thank you.
rsamtools
genomicalignments
genomicfiles
• 2.8k views
@martin-morgan-1513
Last seen 4 months ago
United States
See GenomicFiles::reduceByYield
and REDUCEsampler
, including the example at the end of the help page
fl <- system.file(package="Rsamtools", "extdata", "ex1.bam")
bf <- BamFile(fl, yieldSize=1000)
yield <- function(x)
readGAlignments(x,
param=ScanBamParam(what=c( "qwidth", "mapq" )))
map <- identity
## Samples records from successive chunks of aligned reads.
reduceByYield(bf, yield, map, REDUCEsampler(1000, TRUE))
(Normally the first argument to REDUCEsampler()
might be, e.g., 1000000)
Login before adding your answer.
Traffic: 878 users visited in the last hour
Thank you Martin, it works.