Skip to content

Instantly share code, notes, and snippets.

View kcha's full-sized avatar

Kevin Ha kcha

View GitHub Profile
@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)
}
@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 / 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 / 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.
# 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))