Skip to content

Instantly share code, notes, and snippets.

@diazdc
diazdc / get_kegg_xml.sh
Last active December 20, 2017 23:42
Change command line text colors
# This first line grabs the organism ID number
curl -s "http://rest.kegg.jp/list/organism" | grep "Danio"
# This next line uses the ID to grab xml files
curl "http://rest.kegg.jp/list/pathway/T01004" | cut -f 1 | while read A; do curl -o "${A}.xml" "http://rest.kegg.jp/get/${A}/kgml" ; done
# https://www.biostars.org/p/202663/
@diazdc
diazdc / debounce_func.R
Last active December 20, 2017 23:42
Debounce function for Shiny apps
# Returns a reactive that debounces the given expression by the given time in
# milliseconds.
#
# This is not a true debounce in that it will not prevent \code{expr} from being
# called many times (in fact it may be called more times than usual), but
# rather, the reactive invalidation signal that is produced by expr is debounced
# instead. This means that this function should be used when \code{expr} is
# cheap but the things it will trigger (outputs and reactives that use
# \code{expr}) are expensive.
debounce <- function(expr, millis, env = parent.frame(), quoted = FALSE,
@diazdc
diazdc / sum_duplicated.R
Last active December 20, 2017 23:40
Find number of duplicated values (R)
sum(duplicated(gene_names), na.rm = TRUE)
@diazdc
diazdc / ggplot_color_generator.R
Last active December 21, 2017 04:06
Create a ggplot color palette
# This function came from a stack overflow discussion on how ggplot selects
# colors. Because there are 14 clusters in our seurat analysis, I'm going to grab
# the first 14 colors that ggplot chooses. These will the be same exact colors
# as the tSNE plot in my Seurat analysis.
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}
annot_colors <- gg_color_hue(14)
@diazdc
diazdc / grepList.R
Last active January 9, 2018 19:19
Grep all items in list
# Grep multiple items from a character vector
# https://stackoverflow.com/questions/17358289/finding-elements-of-lists-in-r
greplist <- function(mylist, targetlist, return_true_or_false)
{unique(grep(paste(mylist, collapse = "|"),
targetlist, value = return_true_or_false))
}
@diazdc
diazdc / add_vars_in_loop.R
Last active January 9, 2018 20:29
Example of assigning variables in loop
# https://stackoverflow.com/questions/16566799/change-variable-name-in-for-loop-using-r
d <- 5
for(i in 1:10) {
nam <- paste("A", i, sep = "") # create variable names here
assign(nam, rnorm(3)+d) # variable names, and the function assigned to them
}
@diazdc
diazdc / list_from_for_loop.R
Created January 9, 2018 20:43
Store loop results to a list (example)
# Creating empty list for results
rowcor.list <- list()
# Running row correlation function on each gene and storing them to a list
for(i in 1:3){
results <- paste0(mygenes[i], "results")
rowcor.list[[results]] <- rowCors(filtered.matrix,
filtered.matrix[gene.ind[i], ])
}
@diazdc
diazdc / grep.py
Created January 26, 2018 19:17
Grep function for python
import re
def grep(pattern,fileObj):
r=[]
for line in fileObj:
if re.search(pattern,line):
r.append(line)
return r
@diazdc
diazdc / Tatjanas_list.R
Last active January 31, 2018 15:41
Plot Tatjana's List of Markers
# Reading in Tatjana's list
a.df <- read.delim("/Volumes/projects/ddiaz/Analysis/Data/Gene_Lists/Tatjanas_list.csv",
sep = ",", header = TRUE, stringsAsFactors = FALSE)
a.list = list() # Initializing list
a.list <- lapply(a.df, FUN = as.character) # Converting df to list
# Plot only the genes in the current data set
for (i in 1:23){
FeaturePlot(homeo.seur, a.list[[i]][a.list[[i]] %in%
homeo.seur@var.genes], cols.use = c("grey", "blue"))
@diazdc
diazdc / 3D_plot_in_Seurat.R
Created March 14, 2018 16:46
3D Plot for Seurat
counts.seurat <- RunTSNE(counts.seurat,
reduction.use = "pca",
dims.use = 1:6,
dim.embed = 3)
DimPlot(object = counts.seurat, reduction.use = "tsne", dim.1 = 1, dim.2 = 3)
tSNE_1 <- counts.seurat@dr$tsne@cell.embeddings[,1]
tSNE_2 <- counts.seurat@dr$tsne@cell.embeddings[,2]
tSNE_3 <- counts.seurat@dr$tsne@cell.embeddings[,3]