annotate TableMerge/miniTools.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 # Mini tools for Galaxy scripting
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
3 # Coded by: M.Petera,
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
4 # - -
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
5 # R functions to use in R scripts and wrappers
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
6 # to make things easier (lightening code, reducing verbose...)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
7 # - -
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
8 # V0: script structure + first functions
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
9 # V1: addition of functions to handle special characters in identifiers
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
10 #####################################################
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
11
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
12
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
13 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
14 # Function to call packages without printing all the verbose
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
15 # (only getting the essentials, like warning messages for example)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
16
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
17 shyLib <- function(...){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
18 for(i in 1:length(list(...))){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
19 suppressPackageStartupMessages(library(list(...)[[i]],character.only=TRUE))
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
20 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
21 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
22
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
23 #example: shyLib("xcms","pcaMethods")
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 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
28 # Fonction pour sourcer les scripts R requis
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
29 # /!\ ATTENTION : actuellement la fonction n'est pas chargee au lancement du script,
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
30 # il faut donc la copier-coller dans le wrapper R pour pouvoir l'utiliser.
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
31
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
32 if(FALSE){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
33 source_local <- function(...){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
34 argv <- commandArgs(trailingOnly = FALSE)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
35 base_dir <- dirname(substring(argv[grep("--file=", argv)], 8))
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
36 for(i in 1:length(list(...))){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
37 source(paste(base_dir, list(...)[[i]], sep="/"))
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
38 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
39 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
40 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
41
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
42 #example: source_local("filter_script.R","RcheckLibrary.R")
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
43
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
44
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
45
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
46 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
47 # Functions to stock identifiers before applying make.names() and
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
48 # to reinject it into final matrices
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
49 # Note: it reproduces the original order of datasets' identifiers
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
50 # - - -
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
51 # stockID: stocks original identifiers and original order
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
52 # -> needs checked data regarding table match
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
53 # reproduceID: reinjects original identifiers and original order into final tables
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
54 # -> function to be used at the very end, when exporting tables
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
55
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
56 stockID <- function(dataMatrix, Metadata, Mtype){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
57 # dataMatrix = data.frame containing dataMatrix
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
58 # Metadata = data.frame containing sampleMetadata or variableMetadata
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
59 # Mtype = "sample" or "variable" depending on Metadata content
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
60 cname <- colnames(dataMatrix)[1]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
61 # dataMatrix temporary-stock + transfo - - - -
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
62 if(Mtype=="sample"){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
63 id.ori <- colnames(dataMatrix)[-1]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
64 colnames(dataMatrix) <- make.names(colnames(dataMatrix))
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
65 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
66 if(Mtype=="variable"){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
67 id.ori <- dataMatrix[,1]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
68 dataMatrix[,1] <- make.names(dataMatrix[,1])
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
69 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
70 # global stock - - - - - - - - - - - - - - - -
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
71 id.new <- data.frame(order.ori=c(1:length(Metadata[,1])),Metadata[,1],
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
72 id.new=make.names(Metadata[,1]),id.ori,
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
73 id.new.DM=make.names(id.ori),stringsAsFactors=FALSE)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
74 colnames(id.new)[c(2,4)] <- c(colnames(Metadata)[1],cname)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
75 # Metadata transfo + returning data - - - - -
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
76 Metadata[,1] <- make.names(Metadata[,1])
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
77 return(list(id.match=id.new, dataMatrix=dataMatrix, Metadata=Metadata))
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
78 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
79 #example: A<-stockID(myDM,mysM,"sample") ; myDM<-A$dataMatrix ; mysM<-A$Metadata ; A<-A$id.match
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
80
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
81 reproduceID <- function(dataMatrix, Metadata, Mtype, id.match){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
82 # dataMatrix = data.frame containing dataMatrix
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
83 # Metadata = data.frame containing sampleMetadata or variableMetadata
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
84 # Mtype = "sample" or "variable" depending on Metadata content
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
85 # id.match = 'id.match' element produced by stockID
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
86 #Metadada - - - - - - - - - - - - - -
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
87 temp.table <- id.match[,c(1,2,3)]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
88 ## Removing deleted rows
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
89 for(i in 1:(dim(id.match)[1])){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
90 if(!(temp.table[i,3]%in%Metadata[,1])){temp.table[i,1] <- 0}
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
91 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
92 if(length(which(temp.table[,1]==0))!=0){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
93 temp.table <- temp.table[-c(which(temp.table[,1]==0)),]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
94 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
95 ## Restoring original identifiers and order
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
96 temp.table <- merge(x=temp.table,y=Metadata,by.x=3,by.y=1)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
97 temp.table <- temp.table[order(temp.table$order.ori),]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
98 Metadata <- temp.table[,-c(1,2)]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
99 rownames(Metadata) <- NULL
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
100 #dataMatrix - - - - - - - - - - - - -
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
101 rownames(dataMatrix)<-dataMatrix[,1]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
102 if(Mtype=="sample"){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
103 dataMatrix <- t(dataMatrix[,-1])
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
104 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
105 temp.table <- id.match[,c(1,4,5)]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
106 ## Removing deleted rows
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
107 for(i in 1:(dim(id.match)[1])){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
108 if(!(temp.table[i,3]%in%rownames(dataMatrix))){temp.table[i,1] <- 0}
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
109 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
110 if(length(which(temp.table[,1]==0))!=0){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
111 temp.table <- temp.table[-c(which(temp.table[,1]==0)),]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
112 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
113 ## Restoring original identifiers and order
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
114 temp.table <- merge(x=temp.table,y=dataMatrix,by.x=3,by.y=0)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
115 temp.table <- temp.table[order(temp.table$order.ori),]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
116 if(Mtype=="variable"){
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
117 dataMatrix <- temp.table[,-c(1,2,4)]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
118 colnames(dataMatrix)[1] <- colnames(id.match)[4]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
119 } else {
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
120 rownames(temp.table) <- temp.table[,3]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
121 temp.table <- t(temp.table[,-c(1,2,3)])
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
122 dataMatrix <- data.frame(rownames(temp.table),temp.table)
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
123 colnames(dataMatrix)[1] <- colnames(id.match)[4]
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
124 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
125 rownames(dataMatrix) <- NULL
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
126 # return datasets - - - - - - - - - - -
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
127 return(list(dataMatrix=dataMatrix, Metadata=Metadata))
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
128 }
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
129 #example: B<-reproduceID(myDM,mysM,"sample",A) ; myDM<-B$dataMatrix ; mysM<-B$Metadata
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
130
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
131
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
132
902ab790fb7b Uploaded
melpetera
parents:
diff changeset
133 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -