Skip to content

Instantly share code, notes, and snippets.

View jokergoo's full-sized avatar
🚀

Zuguang Gu jokergoo

🚀
View GitHub Profile
@jokergoo
jokergoo / density.heatplot.R
Last active December 19, 2015 06:09
use heatmap to represent density distribution
require(RColorBrewer)
# since gene expression data always contains a lot of samples.
# If one want to see distributions of samples, using boxplot would be not so clear.
# The function use continuous colors to represent density distributions of expression
# values in samples and can make a better illustration of the data.
heatplot = function(x, col=rev(brewer.pal(10, "Spectral")), draw.quantiles = TRUE, align = TRUE, each = FALSE, ...) {
if(is.vector(x) && class(x) != "list") {
x = as.matrix(x)
@jokergoo
jokergoo / color.pal.R
Last active December 19, 2015 06:29
linear interpolation between colors
color.pal = function(x, col = c("green", "black", "red"), breaks = c(-5, 0, 5)) {
if(length(col) != length(breaks)) {
stop("Length of col should be equal to the length of breaks.\n")
}
# change col represented as strings to RGB space
col_section = sapply(col, function(x) as.vector(col2rgb(x)))
col_section = t(col_section)
# == title
# Simple template for text interpolation
#
# == description
# I like text interpolation in Perl. But in R, if you want to connect plain text and variables,
# you need to use `paste` or functions in `stringr`. However, if there are so many variables or
# or many quotes in the string you want to construct, it would be a little terrible.
# So, this function allows you to construct strings as Perl style.
#
# == param
assignment = function(x, k = 4) {
names(x) = seq_along(x)
y = vector("list", k)
ysum = sapply(y, function(x) 0)
flag = 0
while(length(x)) {
if(flag) {
build.position.index = function(x, by = 5) {
segment = round((x/(10^by)))*10^by
se = unique(segment)
mat = matrix(nrow = length(se), ncol = 2)
for(i in seq_along(se)) {
mat[i, ] = range(which(segment == se[i]))
}
list(mat = mat, segment = se, by = 10^by)
}
bedtools = function(cmd, envir = parent.frame()) {
gr = gsub("^.*@\\{(.*?)\\}.*$", "\\1", cmd)
gr = get(gr, envir = envir)
file = tempfile(fileext = ".bed")
write.table(gr, file = file, sep = "\t", quote = FALSE, row.names = FALSE, col.names = FALSE)
cmd = gsub("@\\{.*?\\}", file, cmd)
try(tb <- read.table(pipe(cmd), stringsAsFactors = FALSE))
file.remove(file)
return(tb)
}
set_module = function(module) {
assign("__module__", module, envir = parent.frame())
}
load_module = function(file) {
e = new.env()
source(file, local = e, verbose = FALSE)
module = get("__module__", envir = e)
rm(e)
is_abs_unit <- function(x) UseMethod("is_abs_unit")
is_abs_unit.unit = function(x) {
unit = attr(x, "valid.unit")
if(unit %in% c(1:4, 7:24)) {
return(TRUE)
} else {
return(FALSE)
}
var unread = new Array();
var checked = new Array();
var from = new Array();
var subject = new Array();
var rows = new Array();
load_all = function() {
rows = document.getElementsByClassName("lvm")[0].lastChild.childNodes;
Path of SRA files:
http://sra-download.ncbi.nlm.nih.gov/srapub/SRR\\d+