Skip to content

Instantly share code, notes, and snippets.

View kcha's full-sized avatar

Kevin Ha kcha

View GitHub Profile
# Create a color bar and breaks that center at a specific value
# Source: https://stackoverflow.com/a/10986203
colorbar <- function(min, max, threshold=0, palette="RdBu", palette_length=15) {
pal <- rev(brewer.pal(9, palette))
# Get all possible breaks
my_breaks <- seq(min, max, length.out=palette_length)
# Get number of breaks above threshold and below threshold
above <- length(which(my_breaks > threshold))
@kcha
kcha / htmlbackup
Last active August 14, 2018 19:13
Backup R Markdown HTML reports
#!/bin/bash
#
# Backup R Markdown authored HTML files to a "notebook" sub-directory and add
# a timestamp to the HTML file
#
# e.g.
# R_analysis.html -> notebook/R_analysis.2015-02-12.html
#
# If file is version controlled, then a short commit hash is also added.
@kcha
kcha / download_ebi.py
Last active February 6, 2025 06:50
Download SRA files from EBI via Aspera.
#!/usr/bin/env python
from __future__ import print_function
from six import iteritems
import sys
import os
import os.path
import re
import argparse
import pandas as pd
@kcha
kcha / split_k.R
Created March 13, 2016 18:24
Split a data frame into k evenly divided (or close to even) subsets
# Split a data frame into k subsets
#
# Returns a list of subsetted data frames of equal (or as close to equal as
# possible) size. If the data frame cannot be split equally by k, then the
# remainder will be adding to the last k'th subset. User can also request
# to put the remainder in an additional k+1 subset.
#
# This is basically a wrapper around split(), but helps calculate the remainder,
# if necessary.
split_k <- function(x, k, remainder_as_additional = FALSE) {
@kcha
kcha / colorbar.R
Last active November 28, 2015 22:56 — forked from johncolby/colorbar.R
An R function to generate color bar legends for neuroimaging plots
# Source: http://stackoverflow.com/a/9314880
color.bar <- function(lut, min, max=-min, nticks=11, ticks=seq(min, max, len=nticks), title='') {
scale = (length(lut))/(max-min)
# dev.new(width=1.75, height=5)
plot(c(0,10), c(min,max), type='n', bty='n', xaxt='n', xlab='', yaxt='n', ylab='', main=title)
axis(2, round(ticks, 2), las=1)
for (i in 1:(length(lut))) {
y = (i-1)/scale + min
rect(0,y,10,y+1/scale, col=lut[i], border=NA)
}
library(readr)
library(dplyr)
library(tm)
library(wordcloud)
library(SnowballC)
library(RColorBrewer)
m <- read_csv("node_table.csv.gz") %>%
filter(EM1_fwer_qvalue_dataset1 < 0.5, EM1_gs_size_dataset1 > 95)
@kcha
kcha / chunk_jobs.sh
Last active August 29, 2015 14:18
Chunk jobs so that they don't all execute at the same time
N=4 # Chunk size
C=0 # Counter
for i in {1..12};
do
# submitjob here...
echo $i
((C=C+1))
if [[ `expr $C % $N` == 0 ]]; then
@kcha
kcha / check_exit_status.sh
Last active August 29, 2015 14:11
Check exit status after running a command in bash
#!/bin/bash
check_exit_status() {
# Checks the latest exit status and exits if status is non-zero
# usage: check_exit_status $? "my_function"
EXITSTAT=$1
FUNC=$2
if [ "$EXITSTAT" -ne "0" ]; then
echo "=> ["`date`"] $FUNC failed"
exit $EXITSTAT
# these examples rely on the R/qtl package, www.rqtl.org
# install R/qtl package, within R, by typing install.packages("qtl")
all: knitr_example.html knitr_example_asciidoc.html knitr_example.pdf markdown_example.html
R_OPTS=--no-save --no-restore --no-init-file --no-site-file # vanilla, but with --environ
knitr_example.html: knitr_example.Rmd
R ${R_OPTS} -e 'library(knitr);knit2html("knitr_example.Rmd")'
@kcha
kcha / seqLogo.R
Last active August 29, 2015 14:07
#install if necessary
source("http://bioconductor.org/biocLite.R")
biocLite("seqLogo")
biocLite("Biostrings")
library(seqLogo)
library(Biostrings)
strings <- c("GATTACA", "GATAACA", "GATAAAA", "GATTAGA")