@@ -16,7 +16,7 @@ main <- function(){
1616 # # process input ----
1717 args <- commandArgs(trailingOnly = TRUE )
1818 suppressMessages(suppressWarnings(load(tail(args ,1 ))))
19- strPath <- args [1 ]
19+ strPath <- paste0(normalizePath( args [1 ]), " / " )
2020 region <- args [2 ]
2121 bwList <- unlist(strsplit(args [3 ]," ," ))
2222 extend.upstream <- as.numeric(args [4 ])
@@ -29,24 +29,25 @@ main <- function(){
2929
3030 # # obtain the region -----
3131 annotations <- NULL
32- if (file.exists(paste0(strPath ," / annotation.rds" ))) annotations <- readRDS(paste0(strPath ," / annotation.rds" ))
32+ if (file.exists(paste0(strPath ," annotation.rds" ))) annotations <- readRDS(paste0(strPath ," annotation.rds" ))
3333 region <- customFindRegion(
3434 region = region ,
3535 annotations = annotations ,
3636 extend.upstream = extend.upstream ,
3737 extend.downstream = extend.downstream
3838 )
3939
40- # # plot the bigwig -----
41- strBW <- list.files(strPath ," bw$" , full.names = T )
42- strBW <- strBW [basename( strBW ) %in% bwList ]
40+ # # plot the bigwig -----
41+ bwList <- bwList [ bwList %in% list.files(strPath ," bw$" )]
42+ strBW <- paste0( strPath , bwList )
4343 AllPlots <- suppressWarnings(suppressMessages(customBigwigTrack(strBW ,region ,fontsize )))
4444 # h <- rep(1,length(AllPlots))
4545 h <- length(strBW )
4646 w <- 6
4747
4848 # # plot gene expression ----
49- expPlot <- customExpressionPlot(strExp ,strBW ,expCutoff ,fontsize )
49+ strCluster <- paste0(strPath ," /bw.cluster" )
50+ expPlot <- customExpressionPlot(strExp ,strCluster ,bwList ,expCutoff ,fontsize )
5051 if (! is.null(expPlot )){
5152 w <- c(w ,nlevels(expPlot $ data $ gene ))
5253 }
@@ -113,7 +114,7 @@ customFindRegion <- function(region,annotations=NULL,sep = c("-", "-"),extend.up
113114 }
114115 )
115116 if (is.null(x = region )) {
116- stop(" Gene not found" )
117+ stop(" Gene/Region is not found" )
117118 }
118119 }
119120 region <- suppressWarnings(expr = Extend(
@@ -124,6 +125,17 @@ customFindRegion <- function(region,annotations=NULL,sep = c("-", "-"),extend.up
124125 )
125126 return (region )
126127}
128+ StringToGRanges <- function (regions , sep = c(" -" , " -" ), ... ) {
129+ ranges.df <- data.frame (ranges = regions )
130+ ranges.df <- tidyr :: separate(
131+ data = ranges.df ,
132+ col = " ranges" ,
133+ sep = paste0(sep [[1 ]], " |" , sep [[2 ]]),
134+ into = c(" chr" , " start" , " end" )
135+ )
136+ granges <- makeGRangesFromDataFrame(df = ranges.df , ... )
137+ return (granges )
138+ }
127139customLookupGeneCoords <- function (annotations , gene ) {
128140 isgene <- annotations $ gene_name == gene
129141 isgene <- ! is.na(x = isgene ) & isgene
@@ -137,20 +149,30 @@ customLookupGeneCoords <- function(annotations, gene) {
137149 return (gr )
138150 }
139151}
140- customExpressionPlot <- function (strExp ,strBW ,geneCutoff = - 0.1 ,fontsize = 9 ) {
152+ customExpressionPlot <- function (strExp ,strCluster , bwList ,geneCutoff = - 0.1 ,fontsize = 9 ) {
141153 if (! file.exists(strExp )) return (NULL )
142154 Exp <- read.csv(strExp ,row.names = 1 ,as.is = T )
143- if (sum(! Exp [,1 ]%in% basename(strBW ))> 0 ) return (NULL )
155+ clusterInfo <- read.table(strCluster ,row.names = 1 ,header = T ,as.is = T ,sep = " \t " )[bwList ,,drop = F ]
156+ grp <- intersect(colnames(Exp ),colnames(clusterInfo ))
157+ if (length(grp )== 0 ) return (NULL )
158+ features <- colnames(Exp )[! colnames(Exp )%in% grp ][- 1 ]
144159 # get data
145- data.plot <- t(Exp [,- 1 ,drop = F ])
146- obj.groups <- Exp [,1 ,drop = F ]
147- features <- row.names(data.plot )
160+ df <- NULL
161+ for (oneBW in bwList ){
162+ selC <- rep(T ,nrow(Exp ))
163+ for (oneG in grp ){
164+ if (nchar(clusterInfo [oneBW ,oneG ])> 0 ){
165+ selC <- selC & Exp [,oneG ]== clusterInfo [oneBW ,oneG ]
166+ }
167+ }
168+ if (sum(selC )== 0 ) stop(paste(" No cell for" ,oneBW ))
169+ oneD = setNames(melt(Exp [selC ,features ,drop = F ],id.vars = NULL ),c(" gene" ," expression" ))
170+ oneD $ group <- oneBW
171+ df <- rbind(df ,oneD )
172+ }
173+ df $ group <- factor (df $ group ,levels = bwList )
148174 # set the color
149- levels.use <- basename(strBW )
150- colors_all <- setNames(gg_color_hue(length(levels.use )),levels.use )
151- # construct data frame
152- df <- setNames(melt(data.plot ),c(" gene" ," Cell" ," expression" ))
153- df <- cbind(df ,group = obj.groups [df $ Cell ,])
175+ colors_all <- setNames(gg_color_hue(length(bwList )),bwList )
154176
155177 p.list <- list ()
156178 for (i in seq_along(along.with = features )) {
@@ -167,6 +189,7 @@ customExpressionPlot <- function(strExp,strBW,geneCutoff=-0.1,fontsize=9) {
167189 scale_y_discrete(position = " top" ) +
168190 scale_x_continuous(position = " bottom" , limits = c(0 , NA )) +
169191 geom_text(data = df.rate ,aes(xpos ,ypos ,label = text ,color = group ),hjust = " right" ,vjust = 1.1 , size = fontsize - 7 )+
192+ scale_fill_manual(values = colors_all )+
170193 theme(
171194 axis.text.x.top = element_text(size = fontsize , face = " bold" , angle = 0 ), # Gene names @ top
172195 axis.text.x.bottom = element_text(size = fontsize - 1 , angle = 60 , vjust = 0.6 ),
@@ -175,9 +198,6 @@ customExpressionPlot <- function(strExp,strBW,geneCutoff=-0.1,fontsize=9) {
175198 strip.text.y = element_blank(),
176199 legend.position = " none"
177200 )
178- if (! is.null(x = levels.use )) {
179- p <- p + scale_fill_manual(values = colors_all )
180- }
181201 p.list [[i ]] <- p
182202 }
183203 p <- patchwork :: wrap_plots(p.list , ncol = length(x = p.list ))
0 commit comments