-
Notifications
You must be signed in to change notification settings - Fork 0
/
RLMdeconvolution.R
executable file
·149 lines (113 loc) · 5.21 KB
/
RLMdeconvolution.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
###############################################################################
## This code uses Robust Linear Modelling to perform deconvolution
## It works for both the fine-grained and coarse-grained sub-Challenges defined
## by the DREAM challenge on deconvolution opened in 2019.
###############################################################################
## Author: Gianni Monaco
## Date: 18/03/2022
###########
####################################################
################ LOAD REQUIRED LIBRARIES
require(optparse, quietly = TRUE, warn.conflicts=FALSE)
require(MASS, quietly = TRUE, warn.conflicts=FALSE)
require(dplyr, quietly = TRUE, warn.conflicts=FALSE)
require(readr, quietly = TRUE, warn.conflicts=FALSE)
require(tidyr, quietly = TRUE, warn.conflicts=FALSE)
require(tibble, quietly = TRUE, warn.conflicts=FALSE)
option_list = list(
make_option(c("--ExprData"), type="character", default=NULL,
help="Count data as CPM values", metavar="character"),
make_option(c("--Submission"), type="character", default="third",
help="Choose between first, second or third", metavar="character"),
make_option(c("--SubChallenge"), type="character", default="coarse",
help="Define granularity of deconvolution as defined by the DREAM challenge. Choose between coarse or fine.", metavar="character"),
make_option(c("--Cancer"), type="character", default=NULL,
help="Choose between CRC (Colon Cancer) or BRCA (Breast Cancer)", metavar="character"),
make_option(c("--OutFormat"), type="character", default="wide",
help="Output results can be saved in either wide or long format. Choose between wide or long", metavar="character"),
make_option(c("--OutFilename"), type="character", default="Predictions.csv",
help="Filename to use for the output file", metavar="character")
)
opt_parser = OptionParser(option_list=option_list);
opt = parse_args(opt_parser);
if(is.null(opt$ExprData)){
print_help(opt_parser)
stop("You need to supply the count data as CPM values (ExpressionData).n", call.=FALSE)
}
######## load signature matrices
if(opt$SubChallenge == "coarse"){
if( opt$Submission =="first"){
load("SignatureMatrix/SigMatCoarseCPM_1st.RData")
}else if( opt$Submission =="second"){
load("SignatureMatrix/SigMatCoarseCPM_2nd.RData")
}else if( opt$Submission =="third"){
load("SignatureMatrix/SigMatCoarseCPM_3rd.RData")
}
SigMat0 <- SigMatCoarse
}
if(opt$SubChallenge == "fine"){
if( opt$Submission =="first"){
load("SignatureMatrix/SigMatFineCPM_1st.RData")
}else if( opt$Submission =="second"){
load("SignatureMatrix/SigMatFineCPM_2nd.RData")
}else if( opt$Submission =="third"){
load("SignatureMatrix/SigMatFineCPM_3rd.RData")
}
SigMat0 <- SigMatFine
}
####### Obtain cancer type
cancer_type <- opt$Cancer
if( !is.null(cancer_type) && cancer_type == "CRC"){
SigMat <- SigMat0[, grep("BRCA", colnames(SigMat0), invert = T)]
}else if(!is.null(cancer_type) && cancer_type == "BRCA") {
SigMat <- SigMat0[, grep("CRC", colnames(SigMat0), invert = T)]
}else{
SigMat <- SigMat0[, grep("CRC|BRCA", colnames(SigMat0), invert = T)]
}
SigMat <- as.matrix(SigMat)
####### READ Input dataset
# This reads in the input file and converts to a matrix which will be
# input to the RLM deconvolution
expression_df <- opt$ExprData %>%
readr::read_csv( progress = FALSE, show_col_types=FALSE) %>%
as.data.frame()
# check expression data
if(any(colSums(expression_df[,2:ncol(expression_df), drop=F]) !=10^6)){
warning("Not all column values sum up to 1e+06. Make sure your expression table is in Counts Per Million (CPM)")
# expression_matrix <- apply(expression_matrix, 2, function(x) x/sum(x) * 10^6)
}
genesComm <- intersect(expression_df[,1], rownames(SigMat))
expression_df <- expression_df[expression_df[,1] %in% genesComm, ]
colnames(expression_df)[1] <- "Gene"
rownames(expression_df) <- NULL
expression_matrix <- expression_df %>%
tibble::column_to_rownames("Gene") %>%
as.matrix()
##### Perform Deconvolution
#check for NA data
if(sum(is.na(expression_matrix) >0 )) { expression_matrix[is.na(expression_matrix)] = 0 }
##### Robust Linear modelling
# We are using the HUGO version of the expression file
result_matrix <- apply(expression_matrix[genesComm,], 2, function(x) coef(rlm( SigMat[genesComm, ], x, maxit =100 ))) *100
result_matrix[result_matrix < -2] <- 0
result_matrix <- result_matrix + abs(min(result_matrix))
# result_matrix <- apply(result_matrix, 2, function(x) x/sum(x)) # sum to 1
result_matrix <- apply(result_matrix, 2, function(x) x/100) # NO sum to 1
# result_matrix <- result_matrix[ grep("CRC|BRCA", rownames(result_matrix), invert = T), ]
# Convert the result matrix back to a dataframe
result_df <- result_matrix %>%
as.data.frame() %>%
tibble::rownames_to_column("cell.type") %>%
dplyr::as_tibble()
# Stack the predictions into one column
result_df_stacked <- tidyr::gather(
result_df,
key = "sample.id",
value = "prediction",
-cell.type)
## Write result into output directory
if( opt$OutFormat == "wide"){
readr::write_csv(result_df, opt$OutFilename)
}else if(opt$OutFormat == "long"){
readr::write_csv(result_df_stacked, opt$OutFilename)
}