#NEWLINE# def clusterPCA( infile , cluster_file , image_dir ) : #NEWLINE# #INDENT# '\n    PCA for each module within an experimental condition across\n    the time series.\n    Take PC1 as the module eigengene and return the loadings and proportion\n    of variance explained for the eigengene.\n    The eigengene expression across the time series is taken to be the\n    value for PC1 at each timepoint as a vector.\n    This is basically what WGCNA moduleEigengenes does but it\n    does not recover the PC loadings.\n\n    Warning: this script will error if there is only one\n    cluster. Make sure you have more than one cluster before\n    trying to perform uninformative analyses.\n\n    Parameters:\n\n    rpath : string\n        Path of R support libraries\n\n    ' #NEWLINE# header = cluster_file . split ( '/' ) [ ( - 1 ) ] . split ( '-' ) [ 0 ] #NEWLINE# R ( "sink(file='sink_file.txt')" ) #NEWLINE# R ( 'suppressMessages(library("reshape2"))' ) #NEWLINE# R ( 'suppressMessages(library("WGCNA"))' ) #NEWLINE# R ( ( 'source("%s")' % os . path . join ( get_r_path ( ) , 'summarySE.R' ) ) ) #NEWLINE# R ( ( 'source("%s")' % os . path . join ( get_r_path ( ) , 'clusterEigengenes.R' ) ) ) #NEWLINE# R ( ( "cluster_match <- read.table('%(cluster_file)s', h=T, row.names=1)" % locals ( ) ) ) #NEWLINE# R ( ( "express_data <- read.table('%(infile)s', h=T, row.names=1, stringsAsFactors=F)" % locals ( ) ) ) #NEWLINE# R ( 'sink(file=NULL)' ) #NEWLINE# R ( 'colnames(cluster_match) <- c("genes", "cluster")' ) #NEWLINE# R ( 'express_data <- data.frame(t(express_data))' ) #NEWLINE# R ( 'express_data$times <- as.numeric(as.character(express_data$times))' ) #NEWLINE# R ( 'data_melt <- melt(express_data, id.vars=c("times", "replicates"))' ) #NEWLINE# R ( 'data_melt$value <- as.numeric(as.character(data_melt$value))' ) #NEWLINE# R ( 'data_sum <- summarySE(data_melt, measurevar="value", groupvars=c("times", "variable"))' ) #NEWLINE# R ( 'data_mod <- data.frame(data_sum$times, data_sum$variable, data_sum$value)' ) #NEWLINE# R ( 'colnames(data_mod) <- c("times", "gene", "value")' ) #NEWLINE# R ( 'data_wide <- dcast(data_mod, gene ~ times, value.var="value")' ) #NEWLINE# R ( 'rownames(data_wide) <- data_wide$gene' ) #NEWLINE# R ( 'times <- as.numeric(as.character(unique(express_data$times)))' ) #NEWLINE# R ( 'data_wide <- data.frame(data_wide[,-1])' ) #NEWLINE# R ( 'colnames(data_wide) <- times' ) #NEWLINE# R ( 'eigen_clustered <- clusterPCA(cluster_frame=cluster_match, expression_frame=data_wide, n=times)' ) #NEWLINE# R ( 'eigen_frame <- eigenExpress(eigen_clustered, n=times)' ) #NEWLINE# R ( ( 'eigenLoad(clusterPCA(cluster_frame=cluster_match, expression_frame=data_wide, n=times), image.dir="%(image_dir)s", condition="%(header)s")' % locals ( ) ) ) #NEWLINE# R ( ( 'eigenPlot(eigen_frame, image.dir="%(image_dir)s", condition="%(header)s")' % locals ( ) ) ) #NEWLINE# eigen_frame = pandas2ri . ri2py ( R [ 'eigen_frame' ] ) #NEWLINE# eigen_frame . index = header [ 'cluster' ] #NEWLINE# eigen_frame . drop ( [ 'cluster' ] , inplace = True , axis = 1 ) #NEWLINE# return eigen_frame