Skip to content

Instantly share code, notes, and snippets.

View aaronsaunders's full-sized avatar

Aaron Marc Saunders aaronsaunders

View GitHub Profile
#
# Steve Pittard - [email protected], 03/19/12
# Code to illustrate motivations for using apply function
#
# See www.bimcore.emory.edu/bbseries for slides and code downloads
#
# References include:
# http://statland.org/R/R/Rpulse2.htm , http://www.cyclismo.org/tutorial/R/tables.html#manipulations
# http://nsaunders.wordpress.com/2010/08/20/a-brief-introduction-to-apply-in-r/
#
@aaronsaunders
aaronsaunders / Manipulating_data
Created August 8, 2013 10:45
Manipulating data
with()
within()
round(x, n) # rounds x to n decimal places
ceiling(x) # vector x of smallest integers > x
floor(x) # vector x of largest interger < x
as.integer(x) # truncates real x to integers (compare to round(x, 0)
as.integer(x < cutpoint) # vector x of 0 if less than cutpoint, 1 if greater than cutpoint)
$ git init
# Print & Set Some Basic Config Variables (Global)
$ git config --global user.email
$ git config --global user.name
$ git config --global user.email "[email protected]"
$ git config --global user.name "My Name"
1:10
c(1, 2, 3, 4)
seq(1, 10, 1)
seq(from=1, to=10, by=1)
sequence(c(10, 5))
seq(along.with=myData) # makes seq of 1:length(myData))
seq(10, along.with=myData) # makes seq of 10:length(myData))
seq(from=2, length.out=10)
rep(x, times = 1, length.out = NA, each = 1)
@aaronsaunders
aaronsaunders / ggplot_help.R
Created August 5, 2013 18:41
ggplot tricks
# ggplot2 implements the idea of a "grammar of graphics". The grammar implemented
# by ggplot2 could be summarized as follows:
#
# plot: coord, scale, facet(?) + layers
# layer: data mapping stat geom position?
#
# A plot is defined by a coordinate system (coord), one or more scales (scale), an
# optional faceting specification (facet), and one or more layers (layer). A layer
# is defined as an R data frame (data), a specification mapping columns of that
# frame into aesthetic properties (mapping), a statistical approach to summarize
### MATPLOTLIBRC FORMAT
# This is a sample matplotlib configuration file - you can find a copy
# of it on your system in
# site-packages/matplotlib/mpl-data/matplotlibrc. If you edit it
# there, please note that it will be overridden in your next install.
# If you want to keep a permanent local copy that will not be
# over-written, place it in HOME/.matplotlib/matplotlibrc (unix/linux
# like systems) and C:\Documents and Settings\yourname\.matplotlib
# (win32 systems).
source('prep_data.R')
library(vegan)
library(RColorBrewer)
# select env/process data
meta <- data[ , 1:( which( names(data) == "amplib_id" )) ]
env <- data[ , ( which( names(data) == "primary_sed" )):( which( names(data) == "otu_0" ) - 1) ]
otu <- as.matrix(data[, (which(names(data) == "otu_0" )):ncol(data) ])
summary(env)
@aaronsaunders
aaronsaunders / ipy_batch_rename.py
Created July 23, 2013 07:17
Batch renaming files in iPython
# infile is 'rename_mapping.txt' tab-sep: oldname newname
# ext of file to be replace can be specified
with open('rename_mapping.txt', 'r') as fh:
map = { line.strip().split()[0]:line.strip().split()[1] for line in fh.readlines() }
oldnames = !ls *.fastq.gz
for oldname in oldnames:
if oldname in map:
newname = map[oldname]
!mv $oldname $newname
@aaronsaunders
aaronsaunders / bar_charts_alt.R
Created July 23, 2013 07:15
side by side barcharts
# http://timotheepoisot.fr/2013/02/17/stacked-barcharts/
library(RColorBrewer)
Abund = round(matrix(runif(200,0,1),nc=10),2)
Abund = Abund/max(Abund)
rownames(Abund) = LETTERS[c(1:nrow(Abund))]
colnames(Abund) = letters[c(1:ncol(Abund))]
@aaronsaunders
aaronsaunders / usearch_derep.bash
Created July 23, 2013 07:13
usear dereplication command line
# Dereplication
usearch --sort reads.fa --output reads.sorted.fa
usearch --derep_fullseq --cluster reads.sorted.fa --seedsout unique.fa [--uc results.uc] [--sizeoout] [--minsize n] [--slots n] [--w wordlength]
usearch --derep_fullseq --cluster reads.sorted.fa --seedsout unique.fa --uc results.uc