|
| 1 | +#!/usr/bin/Rscript |
| 2 | + |
| 3 | +#' Code used to generate Supplementary Figure 20. |
| 4 | +#' Provide as input: (1) a .tsv file with the expression data, rownames |
| 5 | +#' should be [x_coordinate]x[y_coordinate]; and (2) a list of genes to |
| 6 | +#' be visualized. Output directory can be specified, default is wd. |
| 7 | + |
| 8 | + |
| 9 | +library(argparse) |
| 10 | +library(ggplot2) |
| 11 | +library(gridExtra) |
| 12 | + |
| 13 | +TEXT_SIZE = 25 |
| 14 | + |
| 15 | +gene.plot <- function(X, |
| 16 | + col.name, |
| 17 | + size = 5 |
| 18 | + ) { |
| 19 | + |
| 20 | + x <- as.numeric(sapply(rownames(X), function(x){strsplit(x,"x")[[1]][1]})) |
| 21 | + y <- as.numeric(sapply(rownames(X), function(x){strsplit(x,"x")[[1]][2]})) |
| 22 | + v <- X[,col.name] |
| 23 | + plotter <- data.frame(x = y, y = -x, v = v) |
| 24 | + g <- ggplot(plotter,aes(x,y)) + |
| 25 | + theme(text = element_text(size = TEXT_SIZE), |
| 26 | + panel.background = element_rect(fill ="white",color ="white"), |
| 27 | + axis.title.x = element_blank(), |
| 28 | + axis.title.y = element_blank(), |
| 29 | + panel.grid.major = element_blank(), |
| 30 | + panel.grid.minor = element_blank(), |
| 31 | + axis.line = element_blank(), |
| 32 | + axis.ticks.x = element_blank(), |
| 33 | + axis.ticks.y = element_blank(), |
| 34 | + axis.text.x = element_blank(), |
| 35 | + axis.text.y = element_blank() |
| 36 | + |
| 37 | + ) + |
| 38 | + |
| 39 | + labs(title = col.name) + |
| 40 | + geom_point(aes(fill = v),color = "gray",size = size,shape = 21) + |
| 41 | + scale_fill_gradient(low="white", high="blue",guide = F)+ |
| 42 | + coord_fixed() |
| 43 | + |
| 44 | + return(g) |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +parser <- ArgumentParser() |
| 49 | +parser$add_argument("-c", |
| 50 | + "--count_data", |
| 51 | + type = "character") |
| 52 | +parser$add_argument("-g", |
| 53 | + "--genes", |
| 54 | + type = "character") |
| 55 | +parser$add_argument("-o", |
| 56 | + "--out_dir", |
| 57 | + default = NULL, |
| 58 | + type = "character") |
| 59 | + |
| 60 | +args <- parser$parse_args() |
| 61 | + |
| 62 | +if is.null(args$out_dir) { |
| 63 | + args$out_dir <- getwd() |
| 64 | +} |
| 65 | + |
| 66 | +data <- read.table(args$count_data, |
| 67 | + sep = '\t', |
| 68 | + header = T, |
| 69 | + row.names = 1) |
| 70 | + |
| 71 | +gene.names <- as.character(unlist(read.delim(args$genes, |
| 72 | + header = F, |
| 73 | + sep = "\n"))) |
| 74 | + |
| 75 | +elist <- list() |
| 76 | +for (gene in gene.names) { |
| 77 | + print(gene) |
| 78 | + elist[[gene]] <- gene.plot(data, |
| 79 | + gene, |
| 80 | + size = 1.8) |
| 81 | +} |
| 82 | + |
| 83 | +n.cols <- 3 |
| 84 | +n.rows <- ceiling(length(genes) / n.cols) |
| 85 | + |
| 86 | + |
| 87 | +width <- 400*n.cols |
| 88 | +height <- 400*n.rows |
| 89 | + |
| 90 | +png(file.path(args$out_dir, |
| 91 | + "expression-visualization.png"), |
| 92 | + width = width, |
| 93 | + height = height, |
| 94 | + units = "px") |
| 95 | + |
| 96 | +E <- grid.arrange(grobs = elist, |
| 97 | + ncol = n.cols) |
| 98 | +print(E) |
| 99 | +dev.off() |
0 commit comments