annotate GalFilter/miniTools.R @ 2:12cf1eed21f4 draft

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