diff TableMerge/tablemerge_script.R @ 0:902ab790fb7b draft

Uploaded
author melpetera
date Thu, 23 Feb 2017 04:37:49 -0500
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TableMerge/tablemerge_script.R	Thu Feb 23 04:37:49 2017 -0500
@@ -0,0 +1,82 @@
+################################################################################################
+# TABLE MERGE                                                                                  #
+#                                                                                              #
+# User: Galaxy                                                                                 #
+# Starting date: 16-04-2015                                                                    #
+# V-0.1: First version of merge code                                                           #
+# V-0.2: Addition of data check and handling of special characters                             #
+#                                                                                              #
+#                                                                                              #
+# Input files: dataMatrix ; Metadata file                                                      #
+# Output files: dataMatrix ; Metadata file                                                     #
+#                                                                                              #
+# Dependencies: RcheckLibrary.R ; miniTools.R                                                  #
+#                                                                                              #
+################################################################################################
+
+# Parameters (for dev)
+if(FALSE){
+  DM.name <- "dataMatrix_CleanIons_CleanEch.txt"
+  meta.name <- "sampleMetadata_CleanEch.txt"
+  metype <- "sample"
+  output <- "Combined_${Metadata_in.name}"
+}
+
+
+
+tab.merge <- function(DM.name,meta.name,metype,output){
+  # This function allows to merge the dataMatrix with one metadata table.
+  #
+  # Parameters:
+  # - DM.name, meta.name: dataMatrix and metadata files' access respectively
+  # - metype: "sample" or "variable" depending on metadata content
+  # - output: output file's access
+  
+  
+# Input --------------------------------------------------------------
+
+DM <- read.table(DM.name,header=TRUE,sep="\t",check.names=FALSE)
+meta <- read.table(meta.name,header=TRUE,sep="\t",check.names=FALSE,colClasses="character")
+
+# Table match check 
+table.check <- match2(DM,meta,metype)
+check.err(table.check)
+
+# StockID
+meta.id <- stockID(DM,meta,metype)
+DM<-meta.id$dataMatrix ; meta<-meta.id$Metadata ; meta.id<-meta.id$id.match
+
+
+# Merging tables -----------------------------------------------------
+
+if(metype=="sample"){
+  ori.DM <- DM
+  rownames(DM) <- DM[,1]
+  DM <- DM[,-1]
+  DM <- t(DM)
+  DM <- data.frame(sample=row.names(DM),DM,check.names=FALSE)
+  rownames(DM) <- NULL
+}
+
+comb.data <- merge(x=meta,y=DM,by.x=1,by.y=1)
+
+
+# Output -------------------------------------------------------------
+
+# Getting back original identifiers
+if(metype=="sample"){
+  id.ori <- reproduceID(ori.DM,comb.data,metype,meta.id)
+}else{
+  id.ori <- reproduceID(DM,comb.data,metype,meta.id)
+}
+comb.data <- id.ori$Metadata
+
+# Writing the table
+write.table(comb.data,output,sep="\t",quote=FALSE,row.names=FALSE)
+
+
+} # End of tab.merge
+
+
+# Typical function call
+# tab.merge(DM.name,meta.name,metype,output)