-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFUN_Group_GE.R
97 lines (68 loc) · 3.6 KB
/
FUN_Group_GE.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
## Build files for GSEA official input
FUN_Group_GE = function(GeneExp.df, MetaData.df,
TarGeneName = TarGene_name, GroupSet = GeneExpSet.lt,
Save.Path = Save.Path, ExportName = ExportName
){
##### Load Packages #####
if(!require("tidyverse")) install.packages("tidyverse")
# if(!require("patchwork")) install.packages("patchwork")
# if(!require("eoffice")) install.packages("eoffice")
library(tidyverse)
# library(patchwork)
# library(eoffice)
##### Extract Target gene and Statistics ####
# Extract data with TarGeneName
TarGene_Mean <- GeneExp.df[TarGeneName,] %>%
as.numeric() %>%
mean()
# rowMeans(data.matrix(TarGene))
TarGene_SD <- GeneExp.df[TarGeneName,] %>%
as.numeric() %>%
sd()
# Quartile
TarGene_Q <- GeneExp.df[TarGeneName,] %>%
as.numeric() %>%
quantile()
##### Group the expression matrix according to the expression level of Target gene ####
if(GroupSet$GEGroupMode == "Mean"){
GeneExp_high.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] >= TarGene_Mean+TarGene_SD*0]
GeneExp_low.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] < TarGene_Mean-TarGene_SD*0]
}else if(GroupSet$GEGroupMode == "Mean1SD"){
GeneExp_high.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] >= TarGene_Mean+TarGene_SD*1]
GeneExp_low.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] <= TarGene_Mean-TarGene_SD*1]
}else if(GroupSet$GEGroupMode == "Mean2SD"){
GeneExp_high.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] >= TarGene_Mean+TarGene_SD*2]
GeneExp_low.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] <= TarGene_Mean-TarGene_SD*2]
}else if(GroupSet$GEGroupMode == "Mean3SD"){
GeneExp_high.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] >= TarGene_Mean+TarGene_SD*3]
GeneExp_low.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] <= TarGene_Mean-TarGene_SD*3]
}else if(GroupSet$GEGroupMode == "Median"){
GeneExp_high.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] >= TarGene_Q[3]]
GeneExp_low.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] < TarGene_Q[3]]
}else if(GroupSet$GEGroupMode == "Quartile"){
GeneExp_high.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] >= TarGene_Q[4]]
GeneExp_low.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] <= TarGene_Q[2]]
}else if(GroupSet$GEGroupMode == "Customize"){
GeneExp_high.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] >= GroupSet$UpCutoff]
GeneExp_low.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] <= GroupSet$LowerCutoff]
}else{
GeneExp_high.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] >= TarGene_Mean+TarGene_SD*0]
GeneExp_low.set <- colnames(GeneExp.df)[GeneExp.df[TarGeneName,] < TarGene_Mean-TarGene_SD*0]
}
##### Add new anno #####
GeneExp_high.df <- data.frame(ID = GeneExp_high.set %>% as.data.frame(), TarGene = "High")
GeneExp_low.df <- data.frame(ID = GeneExp_low.set %>% as.data.frame(), TarGene = "Low")
GeneExpAnno.df <- rbind(GeneExp_high.df, GeneExp_low.df)
colnames(GeneExpAnno.df) <- c(colnames(MetaData.df)[1], TarGeneName)
AnnoNew.df <- left_join(MetaData.df, GeneExpAnno.df)
## Export tsv
write.table( AnnoNew.df,
file=paste0(Save.Path,"/AnnoNew_",ExportName,".tsv"),
quote = FALSE,row.names = FALSE, na = "",col.names = TRUE,sep = '\t')
## Set Output
Output <- list()
Output[["GeneExp_high.set"]] <- GeneExp_high.set
Output[["GeneExp_low.set"]] <- GeneExp_low.set
Output[["AnnoNew.df"]] <- AnnoNew.df
return(Output)
}