Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
jgranja24 committed Jan 11, 2020
1 parent c78a264 commit b3f7dd8
Show file tree
Hide file tree
Showing 375 changed files with 17,717 additions and 6,106 deletions.
Binary file modified .DS_Store
Binary file not shown.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ Type: Package
Date: 2019-10-17
Title: Analyzing regulatory chromatin in R
Version: 0.1.4
Author: Jeffrey Granja [aut, cre], Ryan Corces [aut]
Maintainer: Jeffrey Granja <[email protected]>
Authors@R: c(
person("Jeffrey", "Granja", email = "[email protected]", role = c("aut","cre")),
person("Ryan", "Corces", role = "aut"))
Description: This package is designed to streamline scATAC analyses in R.
Roxygen: list(markdown = TRUE)
License: GPL (>= 2)
LinkingTo: Rcpp
LazyData: TRUE
Expand Down
37 changes: 21 additions & 16 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(.batchlapply)
export(.centerRollMean)
export(.checkPath)
export(.cleanParams)
export(.computeKNN)
export(.computeROC)
export(.confusionMatrix)
export(.fileExtension)
Expand Down Expand Up @@ -61,7 +62,8 @@ export(.validTxDb)
export(ArchRPalettes)
export(ArchRProject)
export(ArchRRegionTrack)
export(addBdgPeaks)
export(addArchRThreads)
export(addBgdPeaks)
export(addCNVMatrix)
export(addCellColData)
export(addClusters)
Expand All @@ -81,21 +83,22 @@ export(addPeakSet)
export(addProjectSummary)
export(addReproduciblePeakSet)
export(addSampleColData)
export(addSeqLengths)
export(addSeqLengthsGR)
export(addTileMatrix)
export(addTrajectory)
export(columnOverlaps)
export(computeKNN)
export(constructGRanges)
export(constructGR)
export(createArrowFiles)
export(extendGRanges)
export(createGeneAnnnotation)
export(createGenomeAnnotation)
export(extendGR)
export(filterCells)
export(filterChrGR)
export(filterDoublets)
export(filterPlot)
export(findMacs2)
export(getAnnotation)
export(getArchRThreads)
export(getArrowFiles)
export(getBdgPeaks)
export(getBgdPeaks)
export(getBlacklist)
export(getCellColData)
export(getCellNames)
Expand All @@ -114,6 +117,7 @@ export(getInputFiles)
export(getMatches)
export(getMatrixFromArrow)
export(getOutputDirectory)
export(getPeakAnnotation)
export(getPeakSet)
export(getPositions)
export(getProjectSummary)
Expand All @@ -130,26 +134,27 @@ export(ggHex)
export(ggOneToOne)
export(ggPoint)
export(ggViolin)
export(keepFilteredChromosomes)
export(loadArchRProject)
export(markerAnnoEnrich)
export(markerFeatures)
export(markerGR)
export(markerHeatmap)
export(markerPlot)
export(markerRanges)
export(mergeGRanges)
export(mergeGR)
export(nCells)
export(nonOverlappingGRanges)
export(overlappingBP)
export(overlapsMany)
export(nOverlapGR)
export(nonOverlappingGR)
export(overlapsManyGR)
export(paletteContinuous)
export(paletteDiscrete)
export(plotEmbedding)
export(plotFootprints)
export(plotGroups)
export(plotPDF)
export(plotTrajectory)
export(shuffleGRanges)
export(subsetSeqnames)
export(saveArchRProject)
export(shuffleGR)
export(subsetSeqnamesGR)
export(theme_ArchR)
export(trajectoryHeatmap)
importFrom(Rcpp,sourceCpp)
Expand Down
206 changes: 188 additions & 18 deletions R/AllClasses.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ NULL

setClassUnion("characterOrNull", c("character", "NULL"))
setClassUnion("GRangesOrNull", c("GRanges", "NULL"))
setClassUnion("matrixOrNull",members = c("dgCMatrix","NULL"))

setClass("ArchRProject",
representation(
Expand All @@ -13,13 +12,13 @@ setClass("ArchRProject",
sampleColData = "DataFrame",
sampleMetadata = "SimpleList",
cellColData = "DataFrame",
cellMetadata = "SimpleList", #Where clustering output will go to
reducedDims = "SimpleList", #Where clustering output will go to
embeddings = "SimpleList", #Where clustering output will go to
cellMetadata = "SimpleList",
reducedDims = "SimpleList",
embeddings = "SimpleList",
peakSet = "GRangesOrNull",
annotations = "SimpleList", #MotifMatches ETC go here
geneAnnotation = "SimpleList", #genes exons TSS
genomeAnnotation = "SimpleList", #genome chromSizes BSgenome blacklist
peakAnnotation = "SimpleList",
geneAnnotation = "SimpleList",
genomeAnnotation = "SimpleList",
imputeWeights = "SimpleList"
)
)
Expand All @@ -44,15 +43,25 @@ setMethod("show", "ArchRProject",
}
)

#' Create ArchRProject from ArrowFiles
#'
#' This function will create an ArchRProject with given ArrowFiles.
#'
#' @param ArrowFiles A character vector containing the names of ArrowFiles to be used.
#' @param outputDirectory A name for the relative path of the outputDirectory for ArchR results
#' @param copyArrows A boolean indicating whether ArrowFiles should be copied into outputDirectory
#' @param geneAnno The geneAnnotation (see createGeneAnnotation) is used for downstream analyses such as calculate TSS Enrichment Scores, Gene Scores, etc.
#' @param genomeAnno The genomeAnnotation (see createGenomeAnnotation) is used for downstream analyses for genome information such as nucleotide information or chromosome sizes.
#' @param showLogo A boolean indicating whether to show ArchR Logo after successful creation of an ArchRProject.
#' @export
ArchRProject <- function(
ArrowFiles=NULL,
sampleNames=NULL,
outputDirectory = "ArchR_Results",
ArrowFiles = NULL,
outputDirectory = "ArchR_Output",
copyArrows = FALSE,
geneAnnotation = NULL,
genomeAnnotation = NULL,
showLogo = TRUE){
showLogo = TRUE
){

if(is.null(ArrowFiles)){
stop("Need to Provide Arrow Files!")
Expand All @@ -62,10 +71,8 @@ ArchRProject <- function(
message("Validating Arrows...")
ArrowFiles <- unlist(lapply(ArrowFiles, .validArrow))

if(is.null(sampleNames)){
message("Getting SampleNames...")
sampleNames <- unlist(lapply(seq_along(ArrowFiles), function(x) .sampleName(ArrowFiles[x])))
}
message("Getting SampleNames...")
sampleNames <- unlist(lapply(seq_along(ArrowFiles), function(x) .sampleName(ArrowFiles[x])))

if(any(duplicated(sampleNames))){
stop("Error cannot have duplicate sampleNames, please add sampleNames that will overwrite the current sample name in Arrow file!")
Expand All @@ -74,7 +81,7 @@ ArchRProject <- function(
if(length(sampleNames) != length(ArrowFiles)) stop("Samples is not equal to input ArrowFiles!")

dir.create(outputDirectory,showWarnings=FALSE)
sampleDirectory <- file.path(normalizePath(outputDirectory),"InputArrows")
sampleDirectory <- file.path(normalizePath(outputDirectory), "ArrowFiles")
dir.create(sampleDirectory,showWarnings=FALSE)

if(copyArrows){
Expand Down Expand Up @@ -102,9 +109,12 @@ ArchRProject <- function(
cellMetadata = SimpleList(),
reducedDims = SimpleList(),
embeddings = SimpleList(),
annotations = SimpleList(),
peakSet = NULL,
peakAnnotation = SimpleList(),
geneAnnotation = geneAnnotation,
genomeAnnotation = genomeAnnotation)
genomeAnnotation = genomeAnnotation
)

if(showLogo){
.ArchRLogo(ascii = "Logo")
}
Expand All @@ -115,3 +125,163 @@ ArchRProject <- function(

}

#Validity
.validArchRProject <- function(ArchRProj, ...){
if(!inherits(ArchRProj, "ArchRProject")){
stop("Not a valid ArchRProject as input!")
}else{
ArchRProj
}
}

#' Save ArchRProject for Later Usage
#'
#' This function will organize arrows and project output into a directory and save the ArchRProject for later usage.
#'
#' @param ArchRProj An `ArchRProject` object.
#' @param copyArrows A boolean indicating whether to copy or copy + remove original ArrowFiles prior to saving ArchRProject.
#' @export
saveArchRProject <- function(
ArchRProj = NULL,
copyArrows = TRUE
){

outputDir <- getOutputDirectory(ArchRProj)

#Set Up Arrow Files
ArrowDir <- file.path(basename(outputDir), "ArrowFiles")
dir.create(ArrowDir, showWarnings = FALSE)

ArrowFiles <- getArrowFiles(ArchRProj)
ArrowFilesNew <- file.path(ArrowDir, basename(ArrowFiles))
names(ArrowFilesNew) <- names(ArrowFiles)

for(i in seq_along(ArrowFiles)){
cf <- file.copy(ArrowFiles[i], ArrowFilesNew[i])
if(!copyArrows){
file.remove(ArrowFiles[i])
}
}

ArchRProj@sampleColData$ArrowFiles <- ArrowFilesNew[rownames(ArchRProj@sampleColData)]

saveRDS(ArchRProj, file.path(outputDir, "Save-ArchR-Project.rds"))

}

#' Load Previous ArchRProject into R
#'
#' This function will load a previously saved ArchRProject and re-normalize paths for usage.
#'
#' @param path A character path to an ArchRProject directory that was previously saved.
#' @param force A boolean indicating when re-normalizing paths if an annotation/bdgPeaks is not found ignore and continue
#' @param showLogo show ArchRLogo upon completion.
#' @export
loadArchRProject <- function(
path = "./",
force = FALSE,
showLogo = TRUE
){

path2Proj <- file.path(path, "Save-ArchR-Project.rds")

if(!file.exists(path2Proj)){
stop("Could not find previously saved ArchRProject in the path specified!")
}

ArchRProj <- readRDS(path2Proj)

outputDir <- getOutputDirectory(ArchRProj)
outputDirNew <- normalizePath(path)

#1. Arrows Paths
ArrowFilesNew <- file.path(outputDirNew, gsub(paste0(basename(outputDir),"/"),"",ArchRProj@sampleColData$ArrowFiles))
if(!all(file.exists(ArrowFilesNew))){
stop("ArrowFiles do not exist in saved ArchRProject!")
}
ArchRProj@sampleColData$ArrowFiles <- ArrowFilesNew

#2. Annotations Paths

if(length(ArchRProj@peakAnnotation) > 0){

keepAnno <- rep(TRUE, length(ArchRProj@peakAnnotation))

for(i in seq_along(ArchRProj@peakAnnotation)){
#Postions
if(!is.null(ArchRProj@peakAnnotation[[i]]$Positions)){

PositionsNew <- gsub(outputDir, outputDirNew, ArchRProj@peakAnnotation[[i]]$Positions)
if(!all(file.exists(PositionsNew))){
if(force){
keepAnno[i] <- FALSE
message("Positions for peakAnnotation do not exist in saved ArchRProject!")
}else{
stop("Positions for peakAnnotation do not exist in saved ArchRProject!")
}
}
ArchRProj@peakAnnotation[[i]]$Positions <- PositionsNew

}

#Matches
if(!is.null(ArchRProj@peakAnnotation[[i]]$Matches)){

MatchesNew <- gsub(outputDir, outputDirNew, ArchRProj@peakAnnotation[[i]]$Matches)
if(!all(file.exists(MatchesNew))){
if(force){
message("Matches for peakAnnotation do not exist in saved ArchRProject!")
keepAnno[i] <- FALSE
}else{
stop("Matches for peakAnnotation do not exist in saved ArchRProject!")
}
}
ArchRProj@peakAnnotation[[i]]$Matches <- MatchesNew

}

}

ArchRProj@peakAnnotation <- ArchRProj@peakAnnotation[keepAnno]

}


#3. Background Peaks Paths
if(!is.null(getPeakSet(ArchRProj))){

if(!is.null(metadata(getPeakSet(ArchRProj))$bgdPeaks)){

bgdPeaksNew <- gsub(outputDir, outputDirNew, metadata(getPeakSet(ArchRProj))$bgdPeaks)

if(!all(file.exists(bgdPeaksNew))){

if(force){
message("BackgroundPeaks do not exist in saved ArchRProject!")
metadata(ArchRProj@peakSet)$bgdPeaks <- NULL
}else{
stop("BackgroundPeaks do not exist in saved ArchRProject!")
}

}else{

metadata(ArchRProj@peakSet)$bgdPeaks <- bgdPeaksNew

}

}

}

#4. Set Output Directory

ArchRProj@projectMetadata$outputDirectory <- outputDirNew

message("Successfully loaded ArchRProject!")
if(showLogo){
.ArchRLogo(ascii = "Logo")
}

ArchRProj

}
Loading

0 comments on commit b3f7dd8

Please sign in to comment.