I would like to create a density/histogram of the distribution of a particular DNA sequence over the entire transcript using R and/or command line tools. From here, I would like to use the coordinates of the bins to map the intron-exon diagram below the plot. In this way, I can tell the distribution of various DNA sequences across the entire transcript visually and see differences among alternatively spliced isoforms.
I have already found the number of occurrences of the sequences I am interested in using the seqinr
and biostrings
packages. I have been using biomaRt
to get the exon start and stop sequences as well as the full gene sequence. Between all this, I feel I have all the tools I need but I am missing how to put it all together.
The issue I am having is that biomaRt
returns multiple exon sequences with identical start positions but different end positions for a total of 37 exons sequences. Meanwhile, Genatlas and NCBI are returning anywhere from 19 to 21 exons for Grin1, my gene of interest. How can I put all of these tools together to get the plot I need? Is there a better approach?
Finally, if there is an R package or command line tool that already has this function built in, that would be ideal.
Example code for one transcript. This code is actually from an earlier project where I used multiple transcripts but it works fine with one as well. I masked the sequences I'm searching for as "XXXX" at the request of my project manager.
library(magrittr)
library(biomaRt)
library(org.Mm.eg.db)
library(biostrings)
library(seqinr)
### Select only the longest sequences
getMaxLengthSequences <- function(x) {
seqMax <- split(x, factor(x$mgi_symbol))
seqMax <- lapply(seqMax, function(x) max(x$length)) %>%
unlist() %>%
as.data.frame()
seqMax$mgi_symbol <- row.names(seqMax)
row.names(seqMax) <- NULL
names(seqMax) <- c("length", "mgi_symbol")
seqUnique <- merge(seqMax, x)
seqUnique <- seqUnique[order(seqUnique$mgi_symbol), ]
return(seqUnique)
}
### Get sequence for gene of interest
getSequences <- function(genelist, seqType) {
ensembl <- useMart("ensembl", dataset = "mmusculus_gene_ensembl")
sequ <- biomaRt::getSequence(id = genelist,
type = "mgi_symbol",
seqType = seqType,
mart = ensembl)
### Remove unavailable sequences
sequ <- sequ[!grepl("Sequence unavailable", sequ[ ,seqType], fixed = TRUE), ]
### Find the length of each sequence then pick the max length sequence if there are multiple sequences returned from BioMart
sequ <- sequ[order(sequ$mgi_symbol), ]
sequ$length <- sapply(sequ[ ,seqType], nchar)
sequ <- getMaxLengthSequences(sequ)
return(sequ)
}
### Get motif counts of interest (substrings are masked)
getmotifs <- function(x) {
seq <- x %>%
tolower() %>%
s2c()
output <- c("XXXX" = count(seq, 4)[c("xxxx", "xxxx")] %>% sum(),
"XXXX" = count(seq, 4)[c("xxxx", "xxxx")] %>% sum(),
"XXX" = count(seq, 3)["xxx"] %>% sum())
return(output)
}
### Using the above functions: Get sequence and then find motifs
seq <- getSequences("Grin1")
targetMotifs <- matrix(sapply(seq[ ,"coding"], getmotifs), nrow = nrow(targets), ncol = 3, byrow = TRUE)
targetMotifTable <- data.frame(Length = seq$length)
rownames(targetMotifTable) <- make.names(seq$mgi_symbol, unique = TRUE)
targetMotifTable <- cbind(targetMotifTable, targetMotifs) %>% as.data.frame()
names(targetMotifTable) <- c("Length", "XXXX", "XXXX", "XXX")
### Use biomaRt to get exons positions
ensembl <- useMart("ensembl", dataset = "mmusculus_gene_ensembl")
gb <- getBM(attributes = c('ensembl_exon_id', "exon_chrom_start","exon_chrom_end","gene_exon"),
filters = "mgi_symbol",
values="Grin1",
mart = ensembl,
bmHeader = TRUE)
count()
function had some C wizardry going on that made it faster. I think this is why I used it instead of grep but I can't remember exactly. $\endgroup$