# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Additional File 1 # for "Principal component analysis for designed experiments". # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Importing your matrix data into R (in this demonstration, a matrix available from GEO is used) masterdata<-read.table(file="GSE31291_Summarized_Matrix.txt", sep="\t", header=T) masterdata <- masterdata[, 1:40*3+1] rownames(masterdata)<-masterdata[,1] colnames(masterdata)<-colnames(masterdata)[ 1:40*3-1] masterdata<- as.matrix(masterdata) (masterdata[1:10,] ) ## ANOVA ANOVA_negatives <- NA # In fact, many of the genes were negative. # Here, this demonstration will represent Figure 1C; # hence, for comparison, it will use ANOVA negative genes as well. # However, in practice, such genes should be removed to reduce the noise effects. ## Preparing the training_data num_sample <- ncol(masterdata) num_gene <- nrow(masterdata) num_repeat <- 4 # number of each repeat training<-array(NA, dim=c(num_gene, num_sample/num_repeat)) for(gene in 1:num_gene){ for(group in 1:(num_sample/num_repeat)){ training[gene,group]<- mean(masterdata[gene, 1:num_repeat+(group-1)*num_repeat], na.rm=T) }} rownames(training)<-masterdata[,1] colnames(training)<- 0:9 ## Centering center <- apply(training, 1, mean, na.rm=T) # setting the mean data as the reference # center <- training[,1] # alternative: 0h as the reference X <- sweep(training, 1, center) X <- t(X) # transpose of X X_c <- sweep(masterdata, 1, center) # complete data X_c <- t(X_c) ## Removing NAs (and ANOVA negatives) X[is.na(X)]<-0 X[ ,ANOVA_negatives] <- 0 # removing noisy items by replacing the data with zero X_c[is.na(X_c)]<-0 X_c[ ,ANOVA_negatives]<-0 ## SVD # singular value decomposition of X res_svd <- svd(X) Left <- res_svd$u # the left singular vectors Right <- res_svd$v # the right singular vectors sqL <- diag(res_svd$d) # diagonal matrix of the singular values # calculation of the principal components PC_gene <- t(X) %*% Left #/ Right %*% sqL PC_groups <- X %*% Right #/ Left %*% sqL PC_all_samples <- X_c %*% Right # applying the axes to other data sets proportion <- res_svd$d^2/sum(res_svd$d^2)*100 # proportion of characteristic roots # scaling of the principal components sPC_gene <- PC_gene /sqrt( nrow(X) ) sPC_groups <- PC_groups / sqrt( ncol(X) ) sPC_all_samples <- PC_all_samples / sqrt( ncol(X)) ### Presentation of sPC1 and sPC2 # barplot(proportion) groups<- sort(rep(0:9,4)) plot(sPC_gene[,1], sPC_gene[,2] ) # , pch=NA, xlim=c(-.25,.25), ylim=c(-.25,.25)) text(sPC_all_samples[,1], sPC_all_samples[,2], labels=groups, col="green2") ## Exporting the resulted PCs write.table(sPC_all_samples, file="PC_samples.txt", sep="\t") write.table(sPC_groups, file="PC_groups.txt", sep="\t") write.table(sPC_gene, file="PC_gene.txt", sep="\t") ## Tomokazu Konishi, 5 Oct 2011, konishi@akita-pu.ac.jp