Last active
January 19, 2023 18:18
-
-
Save davetang/6460320 to your computer and use it in GitHub Desktop.
Read in BAM file and store as a data frame using Bioconductor's Rsamtools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#install if necessary | |
source("http://bioconductor.org/biocLite.R") | |
biocLite("Rsamtools") | |
#load library | |
library(Rsamtools) | |
#read in entire BAM file | |
bam <- scanBam("wgEncodeRikenCageHchCellPapAlnRep1.bam") | |
#names of the BAM fields | |
names(bam[[1]]) | |
# [1] "qname" "flag" "rname" "strand" "pos" "qwidth" "mapq" "cigar" | |
# [9] "mrnm" "mpos" "isize" "seq" "qual" | |
#distribution of BAM flags | |
table(bam[[1]]$flag) | |
# 0 4 16 | |
#1472261 775200 1652949 | |
#function for collapsing the list of lists into a single list | |
#as per the Rsamtools vignette | |
.unlist <- function (x){ | |
## do.call(c, ...) coerces factor to integer, which is undesired | |
x1 <- x[[1L]] | |
if (is.factor(x1)){ | |
structure(unlist(x), class = "factor", levels = levels(x1)) | |
} else { | |
do.call(c, x) | |
} | |
} | |
#store names of BAM fields | |
bam_field <- names(bam[[1]]) | |
#go through each BAM field and unlist | |
list <- lapply(bam_field, function(y) .unlist(lapply(bam, "[[", y))) | |
#store as data frame | |
bam_df <- do.call("DataFrame", list) | |
names(bam_df) <- bam_field | |
dim(bam_df) | |
#[1] 3900410 13 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment