-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_data_tcga.R
205 lines (172 loc) · 5.42 KB
/
get_data_tcga.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
199
200
201
202
203
204
205
library(xlsx)
library(stringr)
library(data.table)
colclass <- c(
c('character', 'numeric'),
rep('character', times = 6),
rep('numeric', times = 1),
rep('character', times = 2),
rep('character', times = 1),
rep('character', times = 1))
clinical <- read.xlsx("data/2010-09-11380C-Table_S1.2.xlsx",
sheetName = 'KeyclinicalDAta',
colClasses = colclass,
stringsAsFactors=FALSE)
missing_to_na <- function(x) {
if (x == 'Missing') {
return(NA_character_)
} else {
return(x)
}
}
clinical$ProgressionFreeSurvival..mos.. <- sapply(
clinical$ProgressionFreeSurvival..mos..,
missing_to_na
)
clinical$ProgressionFreeSurvival..mos.. <- as.numeric(clinical$ProgressionFreeSurvival..mos..)
colclass <- rep('character', times = 4)
muts_amps <- read.xlsx("data/TCGA-and-HPA_DATA-MYC-BRCA1-BRCA2.xlsx",
sheetName = 'TCGA samples_mutations',
colClasses = colclass,
stringsAsFactors=FALSE)
muts_amps[, 1] <- sapply(
muts_amps[, 1], function(x) {substr(x, 1, nchar(x) - 3)})
na_none <- function (x) {
x[is.na(x)] <- 'NONE'
return(x)
}
muts_amps <- as.data.frame(apply(muts_amps, 2, na_none))
colclass <- c(
c('character', 'numeric'),
rep('character', times = 10),
rep('numeric', times = 2),
rep('character', times = 2))
brca1 <- read.xlsx("data/TCGA-and-HPA_DATA-MYC-BRCA1-BRCA2.xlsx",
sheetName = 'BRCA1',
colClasses = colclass,
stringsAsFactors=FALSE)
brca1[, 1] <- sapply(
brca1[, 1], function(x) {substr(x, 1, nchar(x) - 3)})
brca2 <- read.xlsx("data/TCGA-and-HPA_DATA-MYC-BRCA1-BRCA2.xlsx",
sheetName = 'BRCA2',
colClasses = colclass,
stringsAsFactors=FALSE)
brca2[, 1] <- sapply(
brca2[, 1], function(x) {substr(x, 1, nchar(x) - 3)})
colclass <- c('character', 'character', 'numeric')
char_check <- function(x){
if (identical(x, character(0))) {
return(NA_character_)
} else {
return(x)
}
}
subs <- function(x){
v <- strsplit(x[1], ',')[[1]]
year <- str_detect(v, 'year')
sex <- str_detect(v, 'male')
race <- !as.logical(year + sex)
new_list <- c(
age = trimws(gsub("([0-9]+).*$", "\\1", char_check(v[year]))),
sex = trimws(char_check(v[sex])),
race = trimws(char_check(v[race])))
return(new_list)
}
merge_new_cols <- function(df){
temp_l <- lapply(as.vector(df$Description), subs)
indata <- as.data.frame(do.call(rbind, temp_l))
indata$age <- as.numeric(as.character(indata$age))
indata$sex <- as.character(indata$sex)
indata$race <- as.character(indata$race)
df$Description <- NULL
new_df <- cbind(df, indata)
return(new_df)
}
hpa_myc <- read.xlsx("data/TCGA-and-HPA_DATA-MYC-BRCA1-BRCA2.xlsx",
sheetName = 'HPA_MYC',
colClasses = colclass,
stringsAsFactors=FALSE)
hpa_myc[, 1] <- sapply(
hpa_myc[, 1], function(x) {substr(x, 1, nchar(x) - 3)})
# GET DEMOGRAPHICS
hpa_myc <- merge_new_cols(hpa_myc)
demographics <- hpa_myc[,c('Sample.ID', 'age', 'sex', 'race')]
hpa_myc <- hpa_myc[,c('Sample.ID', 'FPKM')]
names(hpa_myc)[2] <- 'fpkm_myc'
hpa_brca1 <- read.xlsx("data/TCGA-and-HPA_DATA-MYC-BRCA1-BRCA2.xlsx",
sheetName = 'HPA_BRCA1',
colClasses = colclass)
hpa_brca1 <- hpa_brca1[,c('Sample', 'FPKM')]
names(hpa_brca1)[2] <- 'fpkm_brca1'
hpa_brca2 <- read.xlsx("data/TCGA-and-HPA_DATA-MYC-BRCA1-BRCA2.xlsx",
sheetName = 'HPA_BRCA2',
colClasses = colclass)
hpa_brca2 <- hpa_brca2[,c('Sample', 'FPKM')]
names(hpa_brca2)[2] <- 'fpkm_brca2'
# Homogenize sample_id key to merge data
set_sample_id <- function(x){
y <- append(c('sample_id'), names(x)[2:length(x)])
colnames(x) <- y
return(x)
}
# Fix sample IDs by removing last letter (unrequired) from selected datasets
fix_sample_ids <- function(s_id){
return(
substr(s_id, 1, nchar(s_id) - 1)
)
}
df_list <- list(
muts_amps, brca1, brca2, demographics, clinical) # pack dfs
df_list <- lapply(df_list, setDT) # conver dfs to data tables
df_list <- lapply(df_list, set_sample_id) # apply change 'sample_id' t all dts
# FIX SAMPLE IDS BY REMOVING LETTER TERMINATION
range_fix <- seq(4,7,1)
range_fix <- seq(4,4,1)
for (i in range_fix) {
df_list[[i]]$sample_id <- sapply(
as.vector(df_list[[i]]$sample_id),
fix_sample_ids)
}
# CATEGORIZE INTO NUMERIC-BOOLEAN VECTOR MYC AND BRCA DATA
categorize <- function(x, myc = F){
if (as.logical(myc)) {
if (x == 'NONE') {
return('NOT_AMP')
} else {
if (str_detect(x, 'AMP')){
return('AMP')
} else {
return('NOT_AMP')
}
}
} else {
if (str_detect(x, 'MUT:')){
return('MUTATED')
} else {
return('NOT_MUTATED')
}
}
}
vars_fix <- list(
c(2, 'myc_cat', T),
c(3, 'brca1_cat', F),
c(4, 'brca2_cat', F)
)
for (v in vars_fix) {
df_list[[1]][, v[[2]]] <- sapply(
df_list[[1]][, as.integer(v[[1]]), with = F][[1]],
categorize,
myc = v[[3]])
}
brca_mutated <- function(x, y){
if ('MUTATED' %in% c(x, y)){
return('MUTATED')
} else{
return('NOT_MUTATED')
}
}
df_list[[1]][, 'brca_mutated'] <- with(
df_list[[1]], ifelse(brca1_cat == 'MUTATED' |
brca2_cat == 'MUTATED',
"MUTATED", "NON_MUTATED"))
rm(list = setdiff(ls(), "df_list"))