Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: TFBlearner
Title: Functionality for training TF-specific classifiers to predict TF bindings based on ATAC-seq data.
Version: 0.1.0
Version: 0.1.1
Authors@R:
person("Emanuel", "Sonder", , "emanuel.sonder@hest.ethz.ch", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-4788-9508"))
Expand Down
4 changes: 2 additions & 2 deletions R/getFeatureMatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ getFeatureMatrix <- function(mae,
}

# get the cofactors
tfCofactors <- unique(unlist(subset(colData(mae[[TFFEAT]]),
get(TFNAMECOL)==tf)[[TFCOFACTORSCOL]]))
tfCofactors <- unlist(subset(colData(mae[[TFFEAT]]),
get(TFNAMECOL)==tf)[[TFCOFACTORSCOL]])

message("Attaching Site & TF-Features")
selMotifs <- subset(colData(mae[[TFFEAT]]),
Expand Down
4 changes: 3 additions & 1 deletion R/listFeatures.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#' @export
listFeatures <- function(){
motifPrefices <- c(PRIORMOTIFPREFIX, TFMOTIFPREFIX, TFCOFACTORMOTIFPREFIX,
SELMOTIFPREFIX, CTCFMOTIFPREFIX)
paste(SELMOTIFPREFIX, COMOTIFAFFIX, sep="."),
paste(SELMOTIFPREFIX, EXMOTIFAFFIX, sep="."),
CTCFMOTIFPREFIX)
featList <- data.table(feature_name=c("Sequence",
"Width",
"Annot",
Expand Down
31 changes: 13 additions & 18 deletions R/tfFeatures.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
maxScores,
labels,
nMotifs=10,
subSample=10000)
subSample=2e5)
{
labels <- .binMat(labels, threshold=0L)
labels <- .marginMax(labels, margin="row")
Expand Down Expand Up @@ -200,17 +200,16 @@

.getCofactorBindings <- function(chIPMat, tfCofactors){
tfCols <- unlist(tstrsplit(colnames(chIPMat), split="_", keep=2))
namesSub <- names(tfCofactors)[which(tfCofactors %in% tfCols)]
tfCofactors <- intersect(tfCols, tfCofactors)
names(tfCofactors) <- namesSub
tfCofactors <- tfCofactors[tfCofactors %in% tfCols]

if(length(tfCofactors)>0){
cofactBindings <- lapply(tfCofactors, function(tfCol){
cofactBinding <- .aggregate(chIPMat[,tfCols==tfCol, drop=FALSE], aggVar="tf")
colnames(cofactBinding) <- paste(COBINDFEATNAME, tfCol, sep=".")
cofactBinding})
names(cofactBindings) <- paste(COBINDFEATNAME,
gsub(MOTIFAFFIX, "", namesSub), sep="_")
gsub(MOTIFAFFIX, "", names(tfCofactors)),
sep="_")
return(cofactBindings)}
else{
return(NULL)
Expand Down Expand Up @@ -416,10 +415,12 @@ tfFeatures <- function(mae,
if("Cofactor_Binding" %in% features){
message("Cofactor Bindings")
if(is.null(tfCofactors)){
stop("Please provide cofactor names (`tfCofactors`) if Cofactor_Bindings should be computed.")}
cofactBindings <- .getCofactorBindings(chIPMat, tfCofactors)
if(!is.null(cofactBindings)){
featMats <- append(featMats, cofactBindings)
warning("Please provide cofactor names (`tfCofactors`) if Cofactor_Bindings should be computed.")
}else{
cofactBindings <- .getCofactorBindings(chIPMat, tfCofactors)
if(!is.null(cofactBindings)){
featMats <- append(featMats, cofactBindings)
}
}
}

Expand Down Expand Up @@ -474,10 +475,7 @@ tfFeatures <- function(mae,
names(tfSimMotifCols) <- paste(PRIORMOTIFPREFIX, 1:length(tfSimMotifCols),
sep="_")}

tfCofactorCols <- intersect(tfCofactors, motifNames)
if(length(tfCofactorCols)>0){
names(tfCofactorCols) <- paste(TFCOFACTORMOTIFPREFIX,
1:length(tfCofactorCols), sep="_")}
tfCofactorCols <- tfCofactors[tfCofactors %in% motifNames]
tfMotifCols <- intersect(tfName, motifNames)
if(length(tfMotifCols)>0){
names(tfMotifCols) <- paste(TFMOTIFPREFIX, 1:length(tfMotifCols), sep="_")}
Expand Down Expand Up @@ -529,10 +527,7 @@ tfFeatures <- function(mae,
names(tfSimMotifCols) <- paste(PRIORMOTIFPREFIX, 1:length(tfSimMotifCols),
sep="_")}

tfCofactorCols <- intersect(tfCofactors, actMotifNames)
if(length(tfCofactorCols)>0){
names(tfCofactorCols) <- paste(TFCOFACTORMOTIFPREFIX,
1:length(tfCofactorCols), sep="_")}
tfCofactorCols <- tfCofactors[tfCofactors %in% actMotifNames]
tfMotifCols <- intersect(tfName, actMotifNames)
if(length(tfMotifCols)>0){
names(tfMotifCols) <- paste(TFMOTIFPREFIX, 1:length(tfMotifCols), sep="_")}
Expand Down Expand Up @@ -563,7 +558,7 @@ tfFeatures <- function(mae,
selActMotifs <- .selectMotifs(actAssoc, rep(1*scaleFactAct, ncol(actAssoc)),
labels, nMotifs=nMotifs)
if(length(selActMotifs)>0){
names(selActMotifs) <- paste0(SELMOTIFPREFIX, names(selActMotifs))}
names(selActMotifs) <- paste(SELMOTIFPREFIX, names(selActMotifs), sep=".")}
}
else{
selActMotifs <- NULL
Expand Down
11 changes: 11 additions & 0 deletions R/trainTfModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,17 @@
stop(paste("Feature matrix has been computed for", fmTfName, "and not for", tfName))
}

# check for cellular contexts containing no-positive labels
labelCountDt <- as.data.table(table(isPos=assays(fm)$features[,LABELCOLNAME]>0,
context=rowData(fm)[[annoCol]]))
labelCountDt[,isPos:=as.logical(isPos)]
zeroContexts <- subset(labelCountDt, isPos & N==0)$context
if(length(zeroContexts)>0){
warning(paste("Context(s):", paste(zeroContexts, collapse=","),
"contain(s) no positive labels - removed from training data"))
fm <- fm[!(rowData(fm)[[annoCol]] %in% zeroContexts),]
}

# sample stacked chrs
rangesFm <- unique(rowRanges(fm))
mcols(rangesFm) <- NULL
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-getFeatureMatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ test_that("Feature Matrix: Correct context selection - only for specified contex
"A549")
expect_equal(obsRanges, expRanges)
expect_equal(metadata(fm)[[annoCol]], "A549")
expect_equal(metadata(fm)[[TFCOFACTORSCOL]], "JUN")
expCofactors <- c("JUN")
names(expCofactors) <- c("tfCofactorMotif_1")
expect_equal(metadata(fm)[[TFCOFACTORSCOL]], expCofactors)
})

test_that("Feature Matrix: Correct metadata assignment", {
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-tfFeatures.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ test_that("Preselected motifs are saved in colData", {
expect_true(is.vector(preSelActMotifs))
expect_equal(preSelActMotifs[[paste(TFMOTIFPREFIX, 1, sep="_")]], "JUN")
})

test_that("No cofactors provided for co-binding features", {
experiments(maeTest2)[[TFFEAT]] <- NULL
expect_warning(tfFeatures(maeTest2, tfName="JUN", tfCofactors=NULL),
regexp="provide cofactor names")
})
9 changes: 9 additions & 0 deletions tests/testthat/test-trainTfModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,12 @@ test_that("Sampling of additional points for training unweighted",{
expect_equal(sum(labels[trainSet]==1)/length(trainSet),
posFracExp, tolerance=0.01)
})

test_that("Context containing no positive labels",{
assays(fmTest)$features[rowData(fmTest)$context=="A549", LABELCOLNAME] <- 0
expect_no_error(trainTfModel(tfName="CTCF", fmTest, evalRounds=1))
expect_warning(trainTfModel(tfName="CTCF", fmTest, evalRounds=1),
regexp="A549.*no positive labels")
})

# Context(s): A549 contain(s) no positive labels - removed from training data