-
Notifications
You must be signed in to change notification settings - Fork 9
/
core_functions.R
198 lines (164 loc) · 5.83 KB
/
core_functions.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
##############
cmap_score_new <- function(sig_up, sig_down, drug_signature) {
#the old function does not support the input list with either all up genes or all down genes, this new function attempts to addess this.
#we also modify the original CMap approach: whenever the sign of ks_up/ks_down, we substract the two scores such that the final scores would not enrich at 0.
num_genes <- nrow(drug_signature)
ks_up <- 0
ks_down <- 0
connectivity_score <- 0
# I think we are re-ranking because the GeneID mapping changed the original rank range
drug_signature[,"rank"] <- rank(drug_signature[,"rank"])
# Merge the drug signature with the disease signature by GeneID. This becomes the V(j) from the algorithm description
up_tags_rank <- merge(drug_signature, sig_up, by.x = "ids", by.y = 1)
down_tags_rank <- merge(drug_signature, sig_down, by.x = "ids", by.y = 1)
up_tags_position <- sort(up_tags_rank$rank)
down_tags_position <- sort(down_tags_rank$rank)
num_tags_up <- length(up_tags_position)
num_tags_down <- length(down_tags_position)
#
if(num_tags_up > 1) {
a_up <- 0
b_up <- 0
a_up <- max(sapply(1:num_tags_up,function(j) {
j/num_tags_up - up_tags_position[j]/num_genes
}))
b_up <- max(sapply(1:num_tags_up,function(j) {
up_tags_position[j]/num_genes - (j-1)/num_tags_up
}))
if(a_up > b_up) {
ks_up <- a_up
} else {
ks_up <- -b_up
}
}else{
ks_up <- 0
}
if (num_tags_down > 1){
a_down <- 0
b_down <- 0
a_down <- max(sapply(1:num_tags_down,function(j) {
j/num_tags_down - down_tags_position[j]/num_genes
}))
b_down <- max(sapply(1:num_tags_down,function(j) {
down_tags_position[j]/num_genes - (j-1)/num_tags_down
}))
if(a_down > b_down) {
ks_down <- a_down
} else {
ks_down <- -b_down
}
}else{
ks_down <- 0
}
if (ks_up == 0 & ks_down != 0){ #only down gene inputed
connectivity_score <- -ks_down
}else if (ks_up !=0 & ks_down == 0){ #only up gene inputed
connectivity_score <- ks_up
}else if (sum(sign(c(ks_down,ks_up))) == 0) {
connectivity_score <- ks_up - ks_down # different signs
}else{
connectivity_score <- ks_up - ks_down
}
return(connectivity_score)
}
get.gene.list <- function(con){
lincs_gene <- dbReadTable(con, "probe_id_info")
return(lincs_gene$gene_id)
}
get.instance.sig <- function(id, con, landmark=F){
#
sig_file <- paste("~/Documents/stanford/lincs/data/lincs/", id, ".txt", sep="")
sig_value <- NULL
if (file.exists(sig_file)){
sig_value <- scan(sig_file )
if (landmark){
sig_value <- sig_value[1:978]
}
}else{
query <- paste("select * from proj_lincs.sig_values where id = ", id, sep="")
rs <- dbSendQuery(con, query)
result <- fetch(rs, n = -1)[1,]
value <- as.double(unlist(strsplit(result[1,2], ",")))
if (landmark == T){
instance.sig <- data.frame(sig_id = id, probe_id = seq(1, 978), value[1:978])
}else{
instance.sig <- data.frame(sig_id = id, probe_id = seq(1, length(value)), value = value)
}
dbClearResult(rs)
sig_value <- instance.sig$value
}
return (sig_value)
}
#find best alpha and beta
find_alpha_beta <- function(){
alphas <- seq(-1, 1, 0.1)
betas <- seq(-1, 1, 0.1)
all_values <- data.frame()
for (alpha in alphas){
for (beta in betas){
lincs_drug_prediction_subset <- subset(lincs_drug_prediction, cell_id %in% c(cell_line_selected)) #HT29 MCF7
lincs_drug_prediction_subset$RGES <- sapply(1:nrow(lincs_drug_prediction_subset), function(id){
getsRGES(lincs_drug_prediction_subset[id,"RGES"], lincs_drug_prediction_subset[id, "pert_dose"], lincs_drug_prediction_subset[id, "pert_time"], alpha, beta)
})
lincs_drug_prediction_subset <- aggregate(RGES ~ pert_iname, lincs_drug_prediction_subset, mean)
activity_RGES <- merge(lincs_drug_prediction_subset, lincs_drug_activity_subset, by="pert_iname")
activity_RGES_summarized <- activity_RGES #aggregate(cbind(RGES, standard_value) ~ pert_iname, activity_RGES, min)
cor <- cor(activity_RGES_summarized$RGES, log(activity_RGES_summarized$standard_value, 10), method="spearman")
all_values <- rbind(all_values, data.frame(cor, alpha, beta))
}
}
return(all_values)
}
getsRGES1 <- function(RGES, cor, pert_dose, pert_time){
sRGES <- RGES
if (pert_time == 24){
sRGES <- RGES + predict(lm_dose_24, data.frame(dose=round(log(pert_dose, 10), 1)))
}
if (pert_time == 6){
sRGES <- RGES + predict(lm_dose_6, data.frame(dose=round(log(pert_dose, 10), 1)))
}
return (sRGES * cor )
}
getsRGES2 <- function(RGES, cor, pert_dose, pert_time){
sRGES <- RGES
#older version
if (pert_time < 24){
sRGES <- sRGES - 0.1
}
if (pert_dose < 10){
sRGES <- sRGES - 0.2
}
return(sRGES * cor)
}
getsRGES3 <- function(RGES, cor, pert_dose, pert_time, diff, max_cor){
sRGES <- RGES
pert_time <- ifelse(pert_time < 24, "short", "long")
pert_dose <- ifelse(pert_dose < 10, "low", "high")
if (pert_time == "short" & pert_dose == "low"){
sRGES <- sRGES + diff[4]
}
if (pert_dose == "low" & pert_time == "long"){
sRGES <- sRGES + diff[2]
}
if (pert_dose == "high" & pert_time == "short"){
sRGES <- sRGES + diff[1]
}
return(sRGES ) #* cor/max_cor
}
##################
####
getsRGES <- function(RGES, cor, pert_dose, pert_time, diff, max_cor){
sRGES <- RGES
pert_time <- ifelse(pert_time < 24, "short", "long")
pert_dose <- ifelse(pert_dose < 10, "low", "high")
if (pert_time == "short" & pert_dose == "low"){
sRGES <- sRGES + diff[4]
}
if (pert_dose == "low" & pert_time == "long"){
sRGES <- sRGES + diff[2]
}
if (pert_dose == "high" & pert_time == "short"){
sRGES <- sRGES + diff[1]
}
return(sRGES * cor/max_cor) #
}