Skip to content

Instantly share code, notes, and snippets.

@josephwb
josephwb / keep.nodes.R
Last active April 27, 2021 13:39
like ape::keep.tip, but can also keep internal nodes (which become new tips)
library(ape);
# like keep.tip, but you can keep internal nodes toooooo
keep.nodes <- function (phy, nds) {
# stats
Ntip <- length(phy$tip.label);
NEWROOT <- Ntip + 1;
Nnode <- phy$Nnode;
hasNL <- !is.null(phy$node.label);
@josephwb
josephwb / check_genus_monophyly.R
Last active August 26, 2020 13:48
Check the monophyletic status of every genus in a tree
require(phangorn);
# check the monophyletic status of every genus in the tree `phy`
check_genus_monophyly <- function (phy) {
# get genera. assumes form: Genus_species_whatever
gens <- sort(unique(sub("_.*", "", phy$tip.label)));
n <- length(gens);
cat("Checking ", n, " genera for monophyly status.\n", sep="");
# the number of descendant tips of the MRCA. preallocate for monotypic genera
ndec <- rep(1, n);
@josephwb
josephwb / find_large_files.sh
Created June 7, 2018 09:10
Recursively find large files
#!/bin/bash
# report files greater than some size (default 100 MB)
# does so recursively starting at some dir (default pwd)
# to make executable, do:
# chmod +x find_large_files.sh
print_help () {
echo "Recursively find all files larger than some size"
@josephwb
josephwb / subtree.induced.R
Created April 13, 2018 10:43
(R) From a larger tree, extract the subtree induced from a specified set of tips. Requires ape.
# extract subtree induced from a set of tips
subtree.induced <- function (phy, tip) {
if (!inherits(phy, "phylo")) {
stop("object \"phy\" is not of class \"phylo\"");
}
Ntip <- length(phy$tip.label);
# convert to indices if strings passed in
if (is.character(tip)) {
idx <- match(tip, phy$tip.label);
@josephwb
josephwb / Makefile.BEAST.likes
Created February 16, 2018 15:51
Compile and install native BEAST likelihood cores (BEAST v1.8.*). Assumes linux
# run from BEASTv1.8.*/native
# will compile and install into ../lib/
CC=gcc
CFLAGS=-O2 -funroll-loops
INCLUDES=-I${JAVA_HOME}/include/ -I${JAVA_HOME}/include/linux
all: libNucleotideLikelihoodCore.so libAminoAcidLikelihoodCore.so
.c.o:
@josephwb
josephwb / get_mrca_fast.R
Last active August 26, 2020 13:52
Faster tree MRCA calculator in R (alt to APE's getMRCA)
## written by:
# Joseph W. Brown (josephwb, @j0sephwb)
# Klaus Schliep (KlausVigo, @Klaus_Schlp)
get_mrca_fast <- function(phy, tip) {
if (!inherits(phy, "phylo")) {
stop('object "phy" is not of class "phylo"');
}
if (length(tip) < 2) {
return(NULL);
}
@josephwb
josephwb / Baron_et_al_2017_Nature.NEX
Last active November 15, 2017 16:18
Baron et al 2017 Nature dino matrix
#NEXUS
[ *** NOTE (JWB) *** 1st character (all 0, except Dimorphodon_macronyx who was missing the char) in original matrix (Supp. Info. pdf) was a dummy character because TNT is zero-indexed?]
[ It has been deleted here so char indices of 'ordered' list line up. So this martix has 457 chars, while the (incorrect) published matrix has 458.]
Begin data;
Dimensions ntax=75 nchar=457;
[Format Datatype=Standard Missing=? Gap=-;] [MRBAYES]
Format Datatype=Standard SYMBOLS="0 1 2 3 4" Missing=? Gap=-; [PAUP]
MATRIX
@josephwb
josephwb / BayesTraits Makefile
Last active November 20, 2017 16:28
Simple makefile to compile BayesTraitsv3
## Simple makefile to compile BayesTraitsv3 ##
#
# Program info:
# BayesTraits V3.0 (Mar 14 2017)
# Mark Pagel and Andrew Meade
# www.evolution.reading.ac.uk
#
# source: http://www.evolution.rdg.ac.uk/BayesTraitsV3/BayesTraitsV3.html
#
# By default, compiles serial version. To compile threaded version, do:
# gist copied from: https://gist.github.com/emhart/89fd49401bc9795d790ef47ca638726f
# for Ubuntu 18, had to do:
# sudo apt-get install portaudio19-dev
# before installing audio package
# might have to install these packages first
library("dplyr");
library("audio");
@josephwb
josephwb / getopt_long
Created January 28, 2015 16:41
getopt_long: hack to read in an arbitrary number of arguments for a single option
/* Compile with:
* g++ -Wall -std=c++11 dummy.cpp -o dummy -O
* Run as:
* ./dummy -s *.txt
*/
#include <iostream>
#include <string>
#include <fstream>
#include <vector>