Bioconductor / Rsamtools

Binary alignment (BAM), FASTA, variant call (BCF), and tabix file import
https://bioconductor.org/packages/Rsamtools
Other
27 stars 27 forks source link

How to use Rsamtools to extracting reads map into single chromosome from BAM file #9

Closed Timothy-WANG closed 5 years ago

Timothy-WANG commented 5 years ago

Previously, in linux shell, I could extract the reads mapping to chrX like this:

$ samtools view -h -b test.bam chrX -o test.chrX.bam

How to I get the same result in R using Rsamtools, any one knows?

Thanks

mtmorgan commented 5 years ago

Please ask questions like this on the Bioconductor support site https://support.bioconductor.org

If you'd like to query the bam file for those reads, importing them into R, use GenomicAlignments::readGAlignments() or readGAlignmentPairs() or readGAlignmentList().

Restrict the reads input using the argument param = with ScanBamParam(which = GRanges("chrX:1-xxx")), where xxx is the length of the X chromosome, e.g., as(seqinfo(BamFile("test.chrX.bam")), "GRanges")["chrX"]

If you'd like to filter a large bam file to a small bam file (but this is not usually necessary or desirable) use Rsamtools::filterBam() with a similar param= argument.

mtmorgan commented 5 years ago

I'll close this issue here, anticipating any follow-up questions on the support site.