annotate mogsa.R @ 1:1f48d23544a5 draft default tip

Uploaded
author mora-lab
date Thu, 20 May 2021 08:48:44 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
1 ###############################################################################
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
2 # title: mogsa
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
3 # author: Xiaowei
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
4 # time: Mar.31 2021
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
5 ###############################################################################
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
6 #=================================================================
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
7 #how to pass parameters
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
8 #=================================================================
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
9 spec <- matrix(c("data_file", "D",1, "character", "mogsa data file, xlsx file",
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
10 "geneSet_file", "G",1, "character", "gene set /pathway, rdata file, geneSet object",
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
11 "design_file", "S",1, "character", "CSV file, colcode, label, sample",
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
12
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
13 "PC_number", "P",1, "integer", "numbers for PCs",
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
14 "w_data","W",1, "character", "uniform, lambdal, inertia",
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
15 "proc_row", "O", 1, "character", "cnetre, center_ssq1, center_ssqN, center_ssqNm1",
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
16 "ks_B","B",1, "character", "An integer to indicate the number of bootstrapping samples to calculated the p-value of KS statistic.",
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
17 "p_adjust_method", "M", 1, "character", "p values adjust method",
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
18 "output_file1", "R", 1, "character", "genesetScoreMatrix CSV file",
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
19 "output_file2", "R2", 1, "character", "P value Matrix CSV file"
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
20
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
21 ),
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
22 byrow = TRUE, ncol = 5)
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
23
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
24
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
25 if (!requireNamespace("getopt", quietly = TRUE))
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
26 install.packages("getopt")
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
27
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
28 opt <- getopt::getopt(spec)
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
29
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
30 if (is.null(opt$PC_number)){PC_number = 3}else{PC_number = opt$PC_number}
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
31 if (is.null(opt$w_data)){w_data = "inertia"}else{w_data = opt$w_data}
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
32 if (is.null(opt$proc_row)){proc_row = "center_ssq1"}else{proc_row = opt$proc_row}
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
33 if (is.null(opt$ks_B)){ks_B = 1000}else{ks_B = opt$ks_B}
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
34 if (is.null(opt$p_adjust_method)){p_adjust_method = "fdr"}else{p_adjust_method = opt$p_adjust_method}
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
35
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
36 ##########################################################
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
37 # load packages
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
38 ##########################################################
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
39 library(mogsa)
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
40 library(openxlsx) #for load the xlsx file
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
41 ##########################################################
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
42 # Parameters
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
43 ##########################################################
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
44
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
45 sheets <- getSheetNames(opt$data_file)
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
46 mogsa_data <- vector(mode = "list", length = length(sheets))
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
47 names(mogsa_data) = sheets
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
48 for (sht in sheets){
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
49 mogsa_data[[sht]] <- read.xlsx(opt$data_file, sheet = sht, colNames = T, rowNames = T)
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
50 }
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
51
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
52
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
53
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
54 # design
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
55 design <- read.csv(opt$design_file, header = T, stringsAsFactors = F) #分类别、分组别
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
56 groups <- as.factor(design$label)
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
57 #if ("colcode" %in% colnames(design)){colcode <- design$colcode}else{colcode = NULL}
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
58
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
59
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
60 # geneSet
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
61 load(opt$geneSet_file)
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
62 sup_data <- prepSupMoa(mogsa_data, geneSets=geneSet, minMatch = 1)
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
63
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
64
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
65 ##########################################################
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
66 # mogsaBasicRun
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
67 ##########################################################
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
68 mgsa1 <- mogsa(x = mogsa_data, sup=sup_data, nf=PC_number,
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
69 proc.row = proc_row, w.data = w_data, statis = TRUE,
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
70 ks.B = ks_B, p.adjust.method = p_adjust_method)
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
71
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
72
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
73
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
74 ##########################################################
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
75 # gene Set Score (GSS) Matrix 基因集打分矩阵 每个值表示该基因集在该样本的总体活性水平
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
76 ##########################################################
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
77 # get the score matrix
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
78 scores <- getmgsa(mgsa1, "score")
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
79 write.csv(scores, file = opt$output_file1)
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
80
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
81
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
82 ##########################################################
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
83 # P value Matrix
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
84 ##########################################################
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
85 # 获取p值矩阵,行是基因集/pathway名称,列为样本名称
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
86 p.mat <- getmgsa(mgsa1, "p.val") # get p value matrix
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
87 write.csv(p.mat, file = opt$output_file2)
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
88
1f48d23544a5 Uploaded
mora-lab
parents:
diff changeset
89