-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-prep.R
290 lines (201 loc) · 9.5 KB
/
data-prep.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
##############################################
# Some initial set-up
rm(list=ls()); # start with clean slate in R
options(stringsAsFactors=F) # turn off the most annonying default in R
##############################
# Specify Local Directory for Preparing Data
local.dir="C:/Users/aelsayed/Box/BookChapterApril2022/ForGit/"
########################################
# Obtain needed packages
#install.packages("readxl")
#install.packages("writexl")
#install.packages("circlize")
# if (!require("BiocManager", quietly = TRUE))
# install.packages("BiocManager")
#
# BiocManager::install("biomaRt")
library(readxl)
library(writexl)
library(circlize)
library(biomaRt)
source("https://raw.githubusercontent.com/stjude/TALL-example/main/GRIN-ALEX-library.R")
########################################################
# Download supplementary data for Liu et al (2017)
ng.supp.link="https://static-content.springer.com/esm/art%3A10.1038%2Fng.3909/MediaObjects/41588_2017_BFng3909_MOESM2_ESM.xlsx"
ng.supp.file=paste0(local.dir,basename(ng.supp.link))
download.file(ng.supp.link,
ng.supp.file,
mode="wb")
########################################
# Download the detailed clinical data from TARGET
target.clin.link="https://target-data.nci.nih.gov/Public/ALL/clinical/Phase2/harmonized/TARGET_ALL_ClinicalData_Phase_II_Validation_20211118.xlsx"
target.clin.file=paste0(local.dir,basename(target.clin.link))
download.file(target.clin.link,
target.clin.file,
mode="wb")
############################################
# Prepare the clinical data
# Read the TARGET clinical data
target.clin.data=read_xlsx(target.clin.file,sheet=1)
target.clin.data=as.data.frame(target.clin.data)
# Read the clinical data in the supplementary materials
supp.clin.data=read_xlsx(ng.supp.file,sheet=1)
supp.clin.data=as.data.frame(supp.clin.data)
# Clean up the USI patient ID in the TARGET data
target.clin.data$USI=gsub("TARGET-10-","",
target.clin.data$`TARGET USI`,
fixed=T)
# Merge the two clinical data sets
comb.clin.data=merge(target.clin.data,
supp.clin.data,
by="USI")
# clean up the column names of the combined data set
colnames(comb.clin.data)=gsub(".x","_TARGET",
colnames(comb.clin.data),fixed=T)
colnames(comb.clin.data)=gsub(".y","_supplement",
colnames(comb.clin.data),fixed=T)
colnames(comb.clin.data)=gsub("\r","",
colnames(comb.clin.data),fixed=T)
colnames(comb.clin.data)=gsub("\n","",
colnames(comb.clin.data),fixed=T)
colnames(comb.clin.data)=gsub(" ","_",
colnames(comb.clin.data),fixed=T)
###########################################
# Double check the data consistency across the two sources
all(comb.clin.data$Gender_supplement==
comb.clin.data$Gender_TARGET)
table(paste(comb.clin.data$Race_TARGET,
comb.clin.data$Race_supplement,sep="_"))
##################################################
# Get names of spreadsheets in supplementary data file
supp.sheets=excel_sheets(ng.supp.file)
supp.sheets
##################################################
# Read the sequence mutation data
SeqMut.data=read_xlsx(ng.supp.file,
sheet="Table S8 Sequence mutations")
SeqMut.data=as.data.frame(SeqMut.data)
####################################################
# Read the fusion data
fusion.data=read_xlsx(ng.supp.file,
sheet="Table S11 fusions")
fusion.data=as.data.frame(fusion.data)
###################################################
# Read the Copy Number Abnormality data from the supplementary data file
CNA.data=read_xlsx(ng.supp.file,
sheet="Table S13 CNA")
CNA.data=as.data.frame(CNA.data)
#################################################
# Prepare combined lesion data for GRIN analysis
SeqMut.lsns=cbind.data.frame(ID=SeqMut.data$sample,
chrom=gsub("chr","",SeqMut.data$chromosome),
loc.start=SeqMut.data$start,
loc.end=SeqMut.data$start,
lsn.type="mutation")
fusion.lsnsA=cbind.data.frame(ID=fusion.data$sample,
chrom=gsub("chr","",fusion.data$chr_a),
loc.start=fusion.data$position_a,
loc.end=fusion.data$position_a,
lsn.type="fusion")
fusion.lsnsB=cbind.data.frame(ID=fusion.data$sample,
chrom=gsub("chr","",fusion.data$chr_b),
loc.start=fusion.data$position_b,
loc.end=fusion.data$position_b,
lsn.type="fusion")
fusion.lsns=rbind.data.frame(fusion.lsnsA,
fusion.lsnsB)
CNA.lsns=cbind.data.frame(ID=CNA.data$Case,
chrom=CNA.data$Chromosome,
loc.start=CNA.data$Start,
loc.end=CNA.data$End,
lsn.type="copy.number")
CNA.lsns$lsn.type[CNA.data$log2_Ratio<(-0.2)]="loss"
CNA.lsns$lsn.type[CNA.data$log2_Ratio>(+0.2)]="gain"
CNA.lsns$chrom=as.character(CNA.lsns$chrom)
CNA.lsns$chrom=gsub("23","X",CNA.lsns$chrom)
CNA.lsns$chrom=gsub("24","Y",CNA.lsns$chrom)
lsn.data=rbind.data.frame(SeqMut.lsns,
fusion.lsns,
CNA.lsns)
#####################################################################
# To retreive gene annotation data from ensembl biomart
hg19.ann=get.ensembl.annotation("Human_GRCh37")
gene.annotation=hg19.ann$gene.annotation
symbol_ensembl=cbind.data.frame(gene.name=gene.annotation$gene.name,
gene=gene.annotation$gene)
######################################################
# Read the RNAseq expression data from the supplementary data file
RNAseq.data=read_xlsx(ng.supp.file,
sheet="Table S5 RNAseq FPKM")
RNAseq.data=as.data.frame(RNAseq.data)
rownames(RNAseq.data)=RNAseq.data[,1]
RNAseq.data=RNAseq.data[,-1]
RNAseq.data=as.matrix(RNAseq.data)
RNAseq.data=log2(RNAseq.data+1)
RNAseq.data=round(RNAseq.data,3)
RNAseq.data=cbind.data.frame(gene.name=rownames(RNAseq.data),
RNAseq.data)
RNAseq.data.final=merge(symbol_ensembl,RNAseq.data,by="gene.name", all.y=TRUE)
RNAseq.data.final=RNAseq.data.final[,-1]
# Clean up expression data
row.has.na <- apply(RNAseq.data.final, 1, function(x){any(is.na(x))})
sum(row.has.na)
RNAseq.data.final <- RNAseq.data.final[!row.has.na,]
##############################################
# Find and correct a few typos in SJTALL IDs
RNAseq.clms=colnames(RNAseq.data.final)
clin.RNAseq.IDs=comb.clin.data$RNAseq_id_D
not.in.clin=setdiff(RNAseq.clms,clin.RNAseq.IDs)
not.in.RNA=setdiff(clin.RNAseq.IDs,RNAseq.clms)
not.in.clin
not.in.RNA
comb.clin.data$RNAseq_id_D=gsub("SJTALL022433_D2",
"SJTALL022433_D1",
comb.clin.data$RNAseq_id_D)
comb.clin.data$RNAseq_id_D=gsub("SJTALL171_E",
"SJTALL171_D",
comb.clin.data$RNAseq_id_D)
RNAseq.clms=colnames(RNAseq.data.final)
clin.RNAseq.IDs=comb.clin.data$RNAseq_id_D
not.in.clin=setdiff(RNAseq.clms,clin.RNAseq.IDs)
not.in.RNA=setdiff(clin.RNAseq.IDs,RNAseq.clms)
comb.clin.data$RNAseq_id_D=gsub(not.in.RNA,"",
comb.clin.data$RNAseq_id_D)
###############################################
# Replace SJTALL IDs with USI IDs in RNAseq data
clin.IDs=comb.clin.data[,c("RNAseq_id_D","USI")]
RNA.IDs=cbind.data.frame(RNAseq_id_D=colnames(RNAseq.data.final),
clm.index=1:ncol(RNAseq.data.final))
mtch.IDs=merge(clin.IDs,RNA.IDs,by=1)
colnames(RNAseq.data.final)[mtch.IDs$clm.index]=mtch.IDs$USI
##########################################
# Simplify clinical data for example analysis
ex.clin.data=comb.clin.data[,c("USI","Gender_TARGET",
"Race_TARGET","Ethnicity",
"Age_at_Diagnosis_in_Days",
"Year_of_Diagnosis",
"WBC_at_Diagnosis",
"MRD_Day_29",
"Event_Free_Survival_Time_in_Days",
"First_Event",
"Overall_Survival_Time_in_Days",
"Vital_Status")]
colnames(ex.clin.data)=c("ID","Sex","Race","Ethnicity",
"Age_Days","Year_Dx",
"WBC","MRD29",
"Event_Days","First_Event",
"OS_Days","Vital_Status")
##############################################
# Write prepared data in an Excel file
nice.data=list(clinical=ex.clin.data,
lesions=lsn.data,
expression=RNAseq.data.final,
annotation=gene.annotation)
write_xlsx(nice.data,
paste0(local.dir,"example-data-rounded.xlsx"))
clin=ex.clin.data
lsns=lsn.data
RNA=RNAseq.data.final
anns=gene.annotation
save(clin,lsns,RNA,anns,
file=paste0(local.dir,"example-data-rounded.Rdata"))