Skip to content
This repository has been archived by the owner on Jun 1, 2020. It is now read-only.

Commit

Permalink
Simplify 'motifMatrix', example data
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangehring committed Nov 2, 2015
1 parent 911c4d2 commit d70ac4e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
38 changes: 26 additions & 12 deletions R/motif-matrix.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
motifMatrix <- function(vr, group = "sampleNames", normalize = TRUE) {

alteration = expand.grid(ref = DNA_BASES, alt = DNA_BASES)
alteration = subset(alteration, ref != alt & ref %in% c("C", "T"))
alteration = sort(paste0(alteration$ref, alteration$alt))
motifs = expand.grid(s = DNA_BASES, p = DNA_BASES, a = alteration)
motifs = sprintf("%s %s.%s", motifs$a, motifs$p, motifs$s)

df = as(unname(vr), "data.frame")
if(!(group %in% colnames(df))) {
stop(sprintf("Column '%s' not present in input object.", group))
voi <- if( group %in% names(mcols(vr)) ) {
mcols(vr)[ ,group]
} else {
df = as(unname(vr), "data.frame")
if( !(group %in% colnames(df)) ) {
stop(sprintf("Column '%s' not present in input object.", group))
}
df[ ,group]
}

## form the matrix
df$motif = factor(paste(df$alteration, df$context), levels = motifs)
y = sapply(tapply(df$motif, df[ ,group], table), c)
motif = factor(paste(vr$alteration, vr$context),
levels = constructMotifs3())
y = as(table(motif, voi), "matrix")
dimnames(y) = unname(dimnames(y))

if(normalize)
if(normalize) {
y = t(t(y) / colSums(y))
}

return(y)
}


constructMotifs3 <- function() {

alteration = expand.grid(ref = DNA_BASES, alt = DNA_BASES)
alteration = subset(alteration, ref != alt & ref %in% c("C", "T"))
alteration = sort(paste0(alteration$ref, alteration$alt))
motifs = expand.grid(s = DNA_BASES, p = DNA_BASES, a = alteration)
motifs = sprintf("%s %s.%s", motifs$a, motifs$p, motifs$s)

return(motifs)
}
Binary file added data/sca_motifs_tiny.rda
Binary file not shown.
6 changes: 3 additions & 3 deletions man/motif-functions.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ motifMatrix(vr, group = "sampleNames", normalize = TRUE)
TRUE), the counts are transformed to frequencies, such that the sum of
frequencies of each group equal 1. Otherwise (with 'normalize' set to
FALSE), the counts for each mofis in a group is returned.

}

\value{
Expand All @@ -49,7 +49,7 @@ motifMatrix(vr, group = "sampleNames", normalize = TRUE)
}

\examples{
\dontrun{
data(sca_motifs_tiny)

motifMatrix(sca_motifs, group = "study")
}
}
3 changes: 3 additions & 0 deletions man/sca-data.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
\alias{sca-data}
\alias{sca_sigs}
\alias{sca_mm}
\alias{sca_motifs_tiny}
\alias{sigs_nmf}
\alias{sigs_pca}

Expand All @@ -26,6 +27,8 @@

\examples{

data(sca_motifs_tiny, package = "SomaticSignatures")

data(sca_mm, package = "SomaticSignatures")

data(sca_sigs, package = "SomaticSignatures")
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-SomaticSignatures.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ context("motifMatrix")

test_that("'motifMatrix' works", {

data(sca_motifs_tiny)

vr1 = readMutect(mutect_path)
vr1 = mutationContextMutect(vr1)
vr = readMutect(mutect_path, strip = TRUE)
Expand Down

0 comments on commit d70ac4e

Please sign in to comment.