#****************************************************************************************** #Compound Context Analysis #[Author]Tara A. Gianoulis #[Last Modified] September 3, 2011 #****************************************************************************************** #Table of contents #Normalization #Permutation Testing #Accessory Methods #Compound Co-expression and Co-occurrence Functions #Plotting and Printing #****************************************************************************************** #Functions for Normalization #****************************************************************************************** #----Quantile Normalize---# #Quantile Normalize #(1) Sort columns of x #(2) Compute row medians #(3) Use approx function to do the linear interpolation quantileNormalize<-function (x) { xm <- apply(x, 2, sort) xxm <- apply(xm,1, median) xr <- c(apply(x, 2, rank)) q<-array(approx(1:nrow(x), xxm, xr)$y, dim(x), dimnames(x)) return (q) } #---Median Normalize---# #if(x1 == med) medNorm=1 #if(x1 == 0) medNorm = 0 medianNormalize<-function(X){ #Compute median of each column m<-apply(X,2,median) #Create median matrix by repeating elements for as many rows as in X xRows<-nrow(X) xCols<-ncol(X) M<-t(matrix(m,xRows,xCols)) #Subtract each element in X by column median medNorm<-X-M return(medNorm) } #****************************************************************************************** #Functions for Shuffling #****************************************************************************************** #REQUIRES createRandIndex createShuffledMatrix<-function(X){ M<-apply(X,2,createRandIndex) return(M) } #Compute shuffled versions of each column createRandIndex<-function(X){ randIndex<-sample(X) return (X[randIndex]) } #This requires rfindCor from RNASeqfunctions.R #Create distribution of random correlations computeRandomCor<-function(A, B,times){ shuffledVal<-0 for(i in 1:times){ shuffledA<-matrix(sample(A), nrow(A), ncol(A) ) shuffledCor<-rfindCor(shuffledA, B) shuffledVal<-c(shuffledCor, shuffledVal) } return (shuffledVal) } #****************************************************************************************** #Accessory Methods #****************************************************************************************** #Returns only the t-test from the ttest object getTfromStruct<-function(struct){ p<-unlist(lapply(struct, "[",1)); return(p) } #Returns only the pvalues from the ttest object getPvalfromStruct<-function(struct){ p<-unlist(lapply(struct, "[",3)); return(p) } #Compute fold change computeFoldChange<-function(M){ M=maxAb[i,3])) } return(numerator) } #---Compound Co-occurrence Score---# #From Luke's Associates Daylight #Lance & Williams #Bray-Curtis nonmetric coefficient #DLW= (L-(Bi xnor Bj) )/(Bi +Bj) #Requires xnor function computefasterDLW<-function(data){ L<-dim(data)[2] rowNum<-dim(data)[1] rs<-rowSums(data) DLW<-matrix(0,rowNum, rowNum) xnorMat<-matrix(rowSums(apply(x, 1, xnor, y=x)),rowNum, L) numerator<-L-xnorMat for( i in 1:rowNum) { for(j in 1:rowNum){ denom<-rs[i]+rs[j] numerator<- L-xnorMat[i,j] DLW[i,j]<-numerator/denom } } return ( as.dist(DLW) ) } #Additional Logic Gates nor<-function(x,y){!(x | y)} xnor<-function(x,y){(x & y) | (!x & !y)} #****************************************************************************************** #FUNCTIONS for Pretty Plotting #****************************************************************************************** #This function expects a color vector to be given alongside dataset makePlotComplex<-function(X, color) { maxR<-max(X) minR<-min(X) plot(1:ncol(X), X[1, ], ylim=c(minR, maxR), col=color ) for(i in 1:nrow(X) ){ lines(1:ncol(X), X[i, ], ylim=c(minR, maxR), col=color ) } } #****************************************************************************************** #FUNCTIONS: Highly specific functions, limited general utility #****************************************************************************************** getCounts<-function(counts,lng){ countType<-0 for(i in 1: lng ){ ctype<-length( which ( counts==i) ) countType