Skip to content

Instantly share code, notes, and snippets.

View johannesbjork's full-sized avatar

johannesbjork

View GitHub Profile
get_ord_dataframe <- function(lines, linestart){
header_line <- strsplit(lines[linestart], split="\t")[[1]]
num_rows = as.numeric(header_line[2])
num_cols = as.numeric(header_line[3])
if (num_rows == 0){return(data.frame())}
data <- strsplit(lines[(linestart+1):(linestart+num_rows)], split="\t")
names <- unlist(lapply(data, function(x) x[1]))
coords <- lapply(data, function(x) strsplit(x[2:(2+num_cols-1)], split="\t"))
coords <- data.frame(matrix(unlist(coords), nrow=length(coords), byrow=T), stringsAsFactors=FALSE)
@crsh
crsh / gglegend.R
Created April 30, 2017 13:52
Plot only legend of a ggplot
# Stolen from http://stackoverflow.com/a/12041779/914024
gglegend <- function(x){
tmp <- ggplot_gtable(ggplot_build(x))
leg <- which(sapply(tmp$grobs, function(y) y$name) == "guide-box")
tmp$grobs[[leg]]
}
@DomBennett
DomBennett / with_phylo.R
Created May 4, 2016 17:05
Extract list of node IDs and descendant tips from a phylogeny in R
# Libraries
library(MoreTreeTools) # package in dev, install via github: https://github.com/DomBennett/MoreTreeTools
library(doMC) # use doSNOW if using Windows
# find out how many cores you have
n <- detectCores()
# set-up
registerDoMC(cores=n)
# example tree
tree <- rtree(1000)
# find all internal nodes in tree
library(maptools)
library(rgdal)
library(rgeos)
setwd("~/type1")
species <- 'speciesX1'
list_shp <- list.files(path=species, pattern="*.shp", full.names = TRUE,
recursive = TRUE, include.dirs = FALSE)
shp_objects <- lapply(list_shp, function(x) {readOGR(dsn=x,
layer=ogrListLayers(x))})