Skip to content

Instantly share code, notes, and snippets.

Avatar

Ram Narasimhan Ram-N

View GitHub Profile
View gist:5136633
# Genotype AA = 1, Aa = 2 and aa = 3
# Gamete A =1 and a = 0
#Uniformly distribute AA, Aa and aa
start.pop <- sample(1:3, kStartPop, replace=TRUE)
View gist:5136711
#Given a parent individual, get one of their Alleles in the gamete
getGamete <- function(indiv) {
if (indiv == 1) return(1) #AA
if (indiv == 3) return(0) #aa
#if Parent is Aa, the gamete is one binomial trial with prob.big.A
if (indiv == 2) return(rbinom(1, size=1, prob.big.A)) #Aa
}
#Two parental Gametes combine to form a zygote
combineGametes <- function(mg,dg) {
View gist:5136733
getNextGen <- function(x) {
nextgen <- list()
for(i in seq(1, kStartPop, by=2)) {
firstborn <- getOffspring(x[i], x[i+1])
secondborn <- getOffspring(x[i], x[i+1])
nextgen[i] <- firstborn
nextgen[i+1] <- secondborn
}
return(unlist(nextgen))
}
View gist:5136763
#Bookkeeping
calcAllelesInGeneration <- function(x) {
AA = sum(x==1)
AB = sum(x==2)
BB = sum(x==3)
#print(unlist(x))
#print(c(AA, AB, BB))
num.A <- (2 * AA) + AB
num.B <- (2 * BB) + AB
return(c(num.A, num.B))
View gist:5136751
simulationOneTrial <- function(start.pop, knumGenerations, trial.index) {
df.allele <- NULL
df.gen <- NULL
#Keep track of the individuals in each generation
df.gen <- rbind(df.gen, start.pop)
x <- start.pop
for ( gen in 1:knumGenerations) {
@Ram-N
Ram-N / alleleSimulation.R
Created Mar 11, 2013
Given a population of AA, Aa, and aa genotypes, how do the two Alleles (A and a) propagate across generations?
View alleleSimulation.R
rm(list=ls())
library(ggplot2)
library(reshape2)
#Function Definition
#Bookkeeping
calcAllelesInGeneration <- function(x) {
AA = sum(x==1)
AB = sum(x==2)
@Ram-N
Ram-N / visitNode.R
Last active Dec 15, 2015
A simple function to recursively visit each node in a XML document. (Uses DT Lang's XML package.)
View visitNode.R
library(XML)
#Recursive Function to visit the XML tree (depth first)
visitNode <- function(node) {
if (is.null(node)) {
#leaf node reached. Turn back
return()
}
print(paste("Node: ", xmlName(node)))
num.children = xmlSize(node)
if(num.children == 0 ) {
@Ram-N
Ram-N / custom_coloring
Created Mar 25, 2013
Correlation Heatmaps in ggplot2 with custom colors
View custom_coloring
#set up a coloring scheme using colorRampPalette
red=rgb(1,0,0); green=rgb(0,1,0); blue=rgb(0,0,1); white=rgb(1,1,1)
RtoWrange<-colorRampPalette(c(red, white ) )
WtoGrange<-colorRampPalette(c(white, green) )
p <- p + scale_fill_gradient2(low=RtoWrange(100), mid=WtoGrange(100), high="gray")
p
View gist:5240937
#set up a coloring scheme using colorRampPalette
red=rgb(1,0,0); green=rgb(0,1,0); blue=rgb(0,0,1); white=rgb(1,1,1)
RtoWrange<-colorRampPalette(c(red, white ) )
WtoGrange<-colorRampPalette(c(white, green) )
p <- p + scale_fill_gradient2(low=RtoWrange(100), mid=WtoGrange(100), high="gray")
View gist:5240959
library(ggplot2)
library(reshape2)
data(movies)
movieGenres <- movies[c(18:23)] #subset to 6 genres
cor(movieGenres) # 6x6 cor matrix
#ggplot likes the data 'melted' one value per row
m <-melt(cor(movieGenres))
p <- ggplot(data=m, aes(x=Var1, y=Var2, fill=value)) + geom_tile()