annotate TableMerge/tablemerge_script.R @ 1:e44c5246247a draft default tip

Uploaded
author melpetera
date Thu, 14 Nov 2019 12:33:41 -0500
parents 902ab790fb7b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
1 ################################################################################################
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
2 # TABLE MERGE #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
3 # #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
4 # User: Galaxy #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
5 # Starting date: 16-04-2015 #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
6 # V-0.1: First version of merge code #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
7 # V-0.2: Addition of data check and handling of special characters #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
8 # #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
9 # #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
10 # Input files: dataMatrix ; Metadata file #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
11 # Output files: dataMatrix ; Metadata file #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
12 # #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
13 # Dependencies: RcheckLibrary.R ; miniTools.R #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
14 # #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
15 ################################################################################################
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
16
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
17 # Parameters (for dev)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
18 if(FALSE){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
19 DM.name <- "dataMatrix_CleanIons_CleanEch.txt"
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
20 meta.name <- "sampleMetadata_CleanEch.txt"
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
21 metype <- "sample"
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
22 output <- "Combined_${Metadata_in.name}"
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
23 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
24
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
25
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
26
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
27 tab.merge <- function(DM.name,meta.name,metype,output){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
28 # This function allows to merge the dataMatrix with one metadata table.
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
29 #
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
30 # Parameters:
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
31 # - DM.name, meta.name: dataMatrix and metadata files' access respectively
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
32 # - metype: "sample" or "variable" depending on metadata content
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
33 # - output: output file's access
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
34
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
35
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
36 # Input --------------------------------------------------------------
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
37
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
38 DM <- read.table(DM.name,header=TRUE,sep="\t",check.names=FALSE)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
39 meta <- read.table(meta.name,header=TRUE,sep="\t",check.names=FALSE,colClasses="character")
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
40
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
41 # Table match check
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
42 table.check <- match2(DM,meta,metype)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
43 check.err(table.check)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
44
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
45 # StockID
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
46 meta.id <- stockID(DM,meta,metype)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
47 DM<-meta.id$dataMatrix ; meta<-meta.id$Metadata ; meta.id<-meta.id$id.match
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
48
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
49
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
50 # Merging tables -----------------------------------------------------
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
51
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
52 if(metype=="sample"){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
53 ori.DM <- DM
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
54 rownames(DM) <- DM[,1]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
55 DM <- DM[,-1]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
56 DM <- t(DM)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
57 DM <- data.frame(sample=row.names(DM),DM,check.names=FALSE)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
58 rownames(DM) <- NULL
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
59 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
60
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
61 comb.data <- merge(x=meta,y=DM,by.x=1,by.y=1)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
62
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
63
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
64 # Output -------------------------------------------------------------
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
65
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
66 # Getting back original identifiers
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
67 if(metype=="sample"){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
68 id.ori <- reproduceID(ori.DM,comb.data,metype,meta.id)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
69 }else{
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
70 id.ori <- reproduceID(DM,comb.data,metype,meta.id)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
71 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
72 comb.data <- id.ori$Metadata
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
73
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
74 # Writing the table
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
75 write.table(comb.data,output,sep="\t",quote=FALSE,row.names=FALSE)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
76
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
77
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
78 } # End of tab.merge
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
79
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
80
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
81 # Typical function call
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
82 # tab.merge(DM.name,meta.name,metype,output)