-
Notifications
You must be signed in to change notification settings - Fork 5
/
ProgR_3.1_Analyses.R
executable file
·1480 lines (1197 loc) · 57.5 KB
/
ProgR_3.1_Analyses.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
################################################################
################################################################
# Script: ProgR_3.1_Analyses.R
# Author: Ingrid Lonnstedt
# Date: 15/03/2013
# R version: R 2.15.2
# Details: Statistical Analyses with clinical data
################################################################
################################################################
################################################################
################################################################
#
# File paths, date
#
################################################################
################################################################
setwd(paste(getwd(), '/RESPONSIFY', sep=''))
#setwd('/Users/lonnstedt/Documents/RESPONSIFY')
prefix.norm = paste(getwd(), "/output/", sep='')
prefix.raw = paste(getwd(), "/rawData/responsify/", sep='')
prefix.ann = paste(getwd(), "/annotationData/chipTypes/", sep='')
date = format(Sys.Date())
### Read clinical data
###
dat = read.delim(paste(prefix.raw, 'Clinical_data/spss working-Demos combined Leuven Bordet series-Jan2013.txt',
sep = ''), na.strings = c('', NA), check.names = F, dec = ',')[1:108,1:141]
index = c(94, 95, 97:99, 101, 102, 107)
tmpdate = dat$date_diagnosis[index]
dat$date_diagnosis = as.Date(as.character(dat$date_diagnosis),format='%d/%m/%Y')
dat$date_diagnosis[index] = as.Date(as.character(tmpdate),format='%Y-%m-%d')
index = c(98, 99, 103, 105) #106 set to 1951-07-01 manually
tmpdate = dat$Date_of_birth[index]
dat$Date_of_birth = as.Date(as.character(dat$Date_of_birth),format='%Y-%m-%d')
dat$Date_of_birth[index] = as.Date(as.character(tmpdate),format='%d/%m/%Y')
dat$Date_of_birth[106] = as.Date('1951-07-01',format='%Y-%m-%d')
dat$age = dat$date_diagnosis - dat$Date_of_birth
dat$til = as.numeric(as.character(dat$Stomal_LI.))
#Warning above is OK.
dat$ID = as.character(dat$Frozen_tissue_BO_no)
################################################################
################################################################
#
# Expression data and tils: checks and data preparation
#
################################################################
################################################################
prefix.out = paste(getwd(), "/reports/report1/TILs_on_expression/", sep='')
################################################################
### Organize data
load(paste(prefix.norm, 'Expression/Expression_estimates.RData', sep = ''))
data = data.frame(array = names(expr)[6:ncol(expr)])
data$ID = substr(data$array, 3, 6)
data$index = 1:nrow(data)
data = merge(data, subset(dat, select = c('ID','age','tumor_size','Nodal_status','ER_status', 'til', 'date_diagnosis')),
all.x = T, sort = F)
data$batch = 1
data$array = as.character(data$array)
tmp = nchar(data$array, type = 'c')
data$batch[substr(data$array, tmp, tmp) == '2'] = 2
data$batch[substr(data$array, 7, 9) == 'bis'] = 3
data$batch = as.factor(data$batch)
data$age_diag = as.numeric(data$age/365.25)
data$year_diag = as.numeric(substr(as.character(data$date_diagnosis), 1, 4))
data = data[order(data$index),]
###Load array dates
tmp = read.table(paste(prefix.norm, 'Expression/Array_dates.txt', sep = ''), sep = '\t')
#All arrays are hybridized within 30 minutes, so that is not interesting
Xdata = as.data.frame(t(as.matrix(expr[,6:ncol(expr)])))
names(Xdata) = paste('unit', expr$unit, sep = '')
unitNames = expr$unitName
rm(expr)
#Finally remove samples with missing til:
index = which(!is.na(data$til))
data = data[index,]
Xdata = Xdata[index,]
#Now, data and Xdata could be cbind() and put into linear models.
################################################################
### Clinical data overview
png(paste(prefix.out,'results/Clinical_and_exprbatch_overview.png', sep = ''),
width=400, height=400)
plot(data[,c('tumor_size', 'til', 'age_diag')])
dev.off()
par(mfrow = c(1,2))
boxplot(til~Nodal_status, data = data, xlab = 'Nodal status', ylab = 'Stomal LI (%)', col = 'wheat')
boxplot(til~ER_status, data = data, xlab = 'ER status', ylab = 'Stomal LI (%)', col = 'wheat',
xaxt = 'n')
axis(1, labels = c('ER-','ER+'), at = 1:2)
dev.print(png, file=paste(prefix.out,'results/Til_by_nodal_and_ER.png', sep = ''),
width=400, height=400)
summary(data[,c(5, 8, 11)])
################################################################
### Check unwanted variation through principal components plots
pc = prcomp(Xdata)
## Get the standard deviation and variance per principal component
sd.per.pc <- pc$sdev
var.per.pc <- sd.per.pc^2
## Display the percentage of total variance explained by each
sd.per.pc.percent <- sd.per.pc/sum(sd.per.pc)
var.per.pc.percent <- var.per.pc/sum(var.per.pc)
png(paste(prefix.out,'results/Expression perc variance per pc.png', sep = ''),
width=400, height=400)
barplot(var.per.pc.percent[1:10]*100, main='Gene expression, Percent of variance per component',
xlab='Component', ylab = '% variance')
dev.off()
newids = data$ID
#newids[c(19, 78)] = paste(newids[c(19, 78)], 'bis', sep = '')
#newids[c(25, 26, 46, 51, 80)] = paste(newids[c(25, 26, 46, 51, 80)], '_2', sep = '')
dimnames(pc$x)[[1]] = newids
## Plot components PC1 and PC2
plot(pc$x[,1:2],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC1', ylab='PC2')
text(pc$x[,1:2],labels=dimnames(pc$x)[[1]],col=as.numeric(data$batch),pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$batch)),
legend=paste('Batch', as.numeric(unique(data$batch))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC1 and PC3
plot(pc$x[,c(1,3)],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC1', ylab='PC3')
text(pc$x[,c(1,3)],labels=dimnames(pc$x)[[1]],col=as.numeric(data$batch),pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$batch)),
legend=paste('Batch', as.numeric(unique(data$batch))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC1 and PC4
plot(pc$x[,c(1,4)],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC1', ylab='PC4')
text(pc$x[,c(1,4)],labels=dimnames(pc$x)[[1]],col=as.numeric(data$batch),pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$batch)),
legend=paste('Batch', as.numeric(unique(data$batch))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC2 and PC3
plot(pc$x[,c(2,3)],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC2', ylab='PC3')
text(pc$x[,c(2,3)],labels=dimnames(pc$x)[[1]],col=as.numeric(data$batch),pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$batch)),
legend=paste('Batch', as.numeric(unique(data$batch))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC2 and PC4
plot(pc$x[,c(2,4)],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC2', ylab='PC4')
text(pc$x[,c(2,4)],labels=dimnames(pc$x)[[1]],col=as.numeric(data$batch),pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$batch)),
legend=paste('Batch', as.numeric(unique(data$batch))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC3 and PC4
plot(pc$x[,c(3,4)],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC3', ylab='PC4')
text(pc$x[,c(3,4)],labels=dimnames(pc$x)[[1]],col=as.numeric(data$batch),pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$batch)),
legend=paste('Batch', as.numeric(unique(data$batch))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC1 and PC2 coloured by ER status
plot(pc$x[,1:2],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC1', ylab='PC2')
text(pc$x[,1:2],labels=dimnames(pc$x)[[1]],col=as.numeric(data$ER_status)+1,pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$ER_status)+1),
legend=paste('ER', as.numeric(unique(data$ER_status))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC1 and PC2 coloured by Nodal status
plot(pc$x[,1:2],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC1', ylab='PC2')
text(pc$x[,1:2],labels=dimnames(pc$x)[[1]],col=as.numeric(data$Nodal_status)+1,pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$Nodal_status)+1),
legend=paste('Nodal status', as.numeric(unique(data$Nodal_status))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC1 and PC2 coloured by year of diagnosis
plot(pc$x[,1:2],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC1', ylab='PC2')
text(pc$x[,1:2],labels=dimnames(pc$x)[[1]],col=data$year_diag,pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$year_diag)),
legend=paste('Year of diagnosis', as.numeric(unique(data$year_diag))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC1 and PC2 coloured by age_diag
colvec = 1
colvec[data$age_diag>55] = 2
plot(pc$x[,1:2],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC1', ylab='PC2')
text(pc$x[,1:2],labels=dimnames(pc$x)[[1]],col=unique(colvec),pch=0.5)
legend('bottomleft',col=unique(colvec),
legend=paste('Age at diag', unique(colvec)),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC1 and PC2 coloured by tumor_size
colvec = 1
colvec[data$tumor_size>40] = 2
plot(pc$x[,1:2],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC1', ylab='PC2')
text(pc$x[,1:2],labels=dimnames(pc$x)[[1]],col=unique(colvec),pch=0.5)
legend('bottomleft',col=unique(colvec),
legend=paste('Tumour size', unique(colvec)),pch=1,cex=0.7,bg='white',bty='o')
data$pc1 = pc$x[,1]
plot(data[,5:ncol(data)])
tmp = subset(data, select = c(ID, pc1))
dat = merge(dat, tmp, all.x = T)
par(ask = T)
for (i in 70:(ncol(dat)-1)){ plot(dat$pc1,dat[,i], ylab = names(dat)[i]) }
#We have dependendy between PC1 and some biology variables, but not with array dates or similar.
################################################################
################################################################
#
# Genes expressions assoxiated with TIL
#
################################################################
################################################################
prefix.out = paste(getwd(), "/reports/report1/TILs_on_expression/", sep='')
#############################################
# RUV model
library(ruv)
rinv<-RUVinv(Y=as.matrix(log2(Xdata)), X=as.matrix(data$til, ncol = 1), ctl = rep(T, ncol(Xdata)), Z = NULL)
#hist(rinv$p,breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1))
radj<-variance_adjust(rinv)
#hist(radj$p.rsvar,breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1))
#hist(radj$p.ebayes,breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1))
hst = hist(radj$p.evar,breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Empirical variances expression p-values', xlab = 'p-values')
dev.print(png, file=paste(prefix.out,'results/Pvalue_hist_expression.png', sep = ''),
width=400, height=400)
qqnorm(rinv$betahat)
mtext('Expression ~ TIL coefficients')
dev.print(png, file=paste(prefix.out,'results/TILcoefficients_expression.png', sep = ''),
width=400, height=400)
#Index of the probe sets with p-value<0.01:
indexRUV = which(radj$p.evar<0.01) #Gives 785 probe sets
ann = read.csv(paste(prefix.ann, 'HG-U133_Plus_2/HG-U133_Plus_2.na33.annot.csv',
sep = ''), skip = 25)
#radj has p-values (p.evar)
#indexRUV has probe sets with low p-values
#ann has annotation data for the complete array
#hst has p-value histogram
#############################################
# Rearrangements of RUV model results
#Gene symbol list for GOstat test of enrichment
highann = subset(ann, Probe.Set.ID %in% unitNames[indexRUV])
highsymbols = unique(highann$Gene.Symbol)
highsymbols = highsymbols[highsymbols != '---']
write.table(highsymbols, file = paste(prefix.out, 'Expr_on_TIL_symbols.txt'), col.names=F,
row.names = F, quote = F)
#FDR estimates and toptable
mean.freq = mean((hst$counts/(hst$breaks[-1] - hst$breaks[-length(hst$breaks)]))[3:length(hst$counts)])
toptable = data.frame('Probe.Set.Number' = (1:length(radj$betahat))[indexRUV], 'Probe.Set.ID' = unitNames[indexRUV],
log2exp = radj$betahat[indexRUV], p.value = radj$p.evar[indexRUV])
Nle = numeric(nrow(toptable))
Nle[order(toptable$p.value)] = 1:nrow(toptable)
Ele = toptable$p.value * mean.freq
toptable$FDR = Ele/(Nle)
highann = subset(ann, Probe.Set.ID %in% unitNames[indexRUV])
tmp = subset(highann, select = c('Probe.Set.ID','Gene.Symbol'))
toptable = merge(tmp, toptable)
toptable = toptable[order(toptable$FDR),]
toptable$rank = 1:nrow(toptable)
toptable = toptable[, c(ncol(toptable), 1:(ncol(toptable)-1))]
write.table(toptable, file = paste(prefix.out, 'Expr_by_TIL_toptable.txt'),
row.names = F, quote = F)
#Heatmap
set = paste('unit', c(sample(ncol(Xdata), size = 10), toptable$Probe.Set.Number[c(50:1)]), sep = '')
mat = Xdata[,set]
mat = t(mat)
mat = mat[, order(data$til)]
heatmap(log2(mat), Rowv = NA, Colv = NA, col = heat.colors(256), margin = c(10,7))
abline(h = 11.5)
abline(v = 53.4)
#############################################
# Multiple linear models
out = matrix(NA, ncol = 2, nrow = ncol(Xdata))
colnames(out) = c('Estimate', 'Pr(>|t|)')
for (i in 1:ncol(Xdata)){
data$x = log2(Xdata[,i])
mod = lm(til ~ x + batch + age_diag + tumor_size + Nodal_status + ER_status, data = data)
out[i,] = summary(mod)$coef['x',colnames(out)]
}
hist(out[,2],breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Expression linear model P-values', xlab = 'p-values')
out = cbind(out, p.adjust(out[,2], method = 'BH'))
hist(out[,3],breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Expression linear model FDR values', xlab = 'FDR')
mtext('Multiple models')
dev.print(png, file=paste(prefix.out,'results/Pvalue2_hist_expression.png', sep = ''),
width=400, height=400)
qqnorm(out[,1])
mtext('Multiple TIL ~ expression model coefficients')
dev.print(png, file=paste(prefix.out,'results/TILcoefficients2_expression.png', sep = ''),
width=400, height=400)
#Index of the probe sets with p-value<0.01:
indexLM = which(out[, 2]<0.001) #Gives 1187 probe sets
#############################################
# Rearrangements of Multiple linear models results
#Gene symbol list for GOstat test of enrichment
highann = subset(ann, Probe.Set.ID %in% unitNames[indexLM])
highsymbols = unique(highann$Gene.Symbol)
highsymbols = highsymbols[highsymbols != '---']
write.table(highsymbols, file = paste(prefix.out, 'TIL_on_expr2_symbols.txt'), col.names=F,
row.names = F, quote = F)
#Toptable
toptable = data.frame('Probe.Set.Number' = (1:nrow(out))[indexLM],
'Probe.Set.ID' = unitNames[indexLM],
log2exp = out[indexLM, 'Estimate'],
p.value = out[indexLM, 'Pr(>|t|)'],
FDR = out[indexLM, 3])
highann = subset(ann, Probe.Set.ID %in% unitNames[indexRUV])
tmp = subset(highann, select = c('Probe.Set.ID','Gene.Symbol'))
toptable = merge(tmp, toptable)
toptable = toptable[order(toptable$FDR),]
toptable$rank = 1:nrow(toptable)
toptable = toptable[, c(ncol(toptable), 1:(ncol(toptable)-1))]
write.table(toptable, file = paste(prefix.out, 'TIL_by_exprLM_toptable.txt'),
row.names = F, quote = F)
#Heatmap
set.seed(567890)
set = paste('unit', c(sample(ncol(Xdata), size = 10), toptable$Probe.Set.Number[c(50:1)]), sep = '')
mat = Xdata[,set]
mat = t(mat)
mat = mat[, order(data$til)]
heatmap(log2(mat), Rowv = NA, Colv = NA, col = heat.colors(256), margin = c(10,7))
save.image(paste(prefix.out, 'TILs_on_expr.RData', sep = ''))
################################################################
# Naive RUV adjustment model
## Y: expression matrix where the rows are the samples and the columns are the genes.
## cIdx: column index of the negative control genes in Y, for estimation of unwanted variation.
## nuCoeff: regularization parameter for the unwanted variation.
## k: rank of the unwanted variation term.
naiveRandRuv <- function(Y, cIdx, nuCoeff=1e-3, k=m){
## W is the square root of the empirical covariance on the control
## genes.
svdYc <- svd(Y[, cIdx])
W <- svdYc$u[, 1:k] %*% diag(svdYc$d[1:k]) #/ sqrt(length(cIdx)+1)
## Regularization heuristic: nu is a fraction of the largest eigenvalue of WW'
nu <- nuCoeff*svdYc$d[1]^2 #/ (length(cIdx)+1)
## Naive correction: ridge regression of Y against W
nY <- Y - W %*% solve(t(W)%*%W + nu*diag(k), t(W) %*% Y)
return(nY)
}
rnaive10 = naiveRandRuv(Y=as.matrix(log2(Xdata)), cIdx = rep(T, ncol(Xdata)), nuCoeff=1e-3, k=10)
res10 = matrix(NA, ncol = 2, nrow = ncol(Xdata))
colnames(res10) = c('Estimate', 'Pr(>|t|)')
for (i in 1:ncol(Xdata)){
data$x = rnaive10[,i]
mod = lm(til ~ x + age_diag + tumor_size + Nodal_status + ER_status, data = data)
res10[i,] = summary(mod)$coef['x',colnames(res10)]
}
hist(res10[,2],breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Naive RUV P-values', xlab = 'p-values')
mtext('k = 10')
rnaive = naiveRandRuv(Y=as.matrix(log2(Xdata)), cIdx = rep(T, ncol(Xdata)), nuCoeff=1e-3, k=20)
res = matrix(NA, ncol = 2, nrow = ncol(Xdata))
colnames(res) = c('Estimate', 'Pr(>|t|)')
for (i in 1:ncol(Xdata)){
data$x = rnaive[,i]
mod = lm(til ~ x + age_diag + tumor_size + Nodal_status + ER_status, data = data)
res[i,] = summary(mod)$coef['x',colnames(res)]
}
hist(res[,2],breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Naive RUV P-values', xlab = 'p-values')
mtext('k = 20')
rnaive100 = naiveRandRuv(Y=as.matrix(log2(Xdata)), cIdx = rep(T, ncol(Xdata)), nuCoeff=1e-1, k=20)
res100 = matrix(NA, ncol = 2, nrow = ncol(Xdata))
colnames(res100) = c('Estimate', 'Pr(>|t|)')
for (i in 1:ncol(Xdata)){
data$x = rnaive100[,i]
mod = lm(til ~ x + age_diag + tumor_size + Nodal_status + ER_status, data = data)
res100[i,] = summary(mod)$coef['x',colnames(res100)]
}
hist(res100[,2],breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Naive RUV P-values', xlab = 'p-values')
mtext('Times 100')
rnaive.01 = naiveRandRuv(Y=as.matrix(log2(Xdata)), cIdx = rep(T, ncol(Xdata)), nuCoeff=1e-5, k=20)
res.01 = matrix(NA, ncol = 2, nrow = ncol(Xdata))
colnames(res.01) = c('Estimate', 'Pr(>|t|)')
for (i in 1:ncol(Xdata)){
data$x = rnaive.01[,i]
mod = lm(til ~ x + age_diag + tumor_size + Nodal_status + ER_status, data = data)
res.01[i,] = summary(mod)$coef['x',colnames(res.01)]
}
hist(res.01[,2],breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Naive RUV P-values', xlab = 'p-values')
mtext('Div 100')
#Index of the probe sets with p-value<0.01:
indexNR10 = which(res10[, 2]<0.001) #Gives 862 probe sets, out of which 859 are shared with indexNR
indexNR = which(res[, 2]<0.001) #Gives 889 probe sets, out of which 835 are shared with indexLM
indexNR100 = which(res100[, 2]<0.001) #Gives 1231 probe sets, out of which 877 are shared with indexNR
#and 1021 are shared with indexLM
indexNR.01 = which(res.01[, 2]<0.001) #Gives 26 probe sets, this histogram looks bad
indexNRx = which(res[, 2]<0.01) #Gives 889 probe sets, out of which 835 are shared with indexNR
#Decided to use the intersect(indexNR, indexLM) as toptable
#Print p-value histogram of the naive RUV with k = 20 and nu = 1e-3 (indexNR model)
hstNR = hist(res[,2],breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Expression naive RUV p-values', xlab = 'p-values')
mtext('Multiple models')
dev.print(png, file=paste(prefix.out,'results/Pvalue3_hist_expression.png', sep = ''),
width=400, height=400)
#############################################
# Rearrangements of Naive RUV results, which will be considered primary
(the 835 probes that intersect with the linear models will be primary)
topindex = intersect(indexLM, indexNR)
#Gene symbol list for GOstat test of enrichment
highann = subset(ann, Probe.Set.ID %in% unitNames[topindex])
highsymbols = unique(highann$Gene.Symbol)
highsymbols = highsymbols[highsymbols != '---']
write.table(highsymbols, file = paste(prefix.out, 'TIL_on_expr_TOP_symbols.txt'), col.names=F,
row.names = F, quote = F, sep = '\t')
#Toptable
toptable = data.frame('Probe.Set.Number' = (1:nrow(res))[topindex],
'Probe.Set.ID' = unitNames[topindex],
log2exp = res[topindex, 'Estimate'],
p.value = res[topindex, 'Pr(>|t|)'],
FDR = p.adjust(res[,2], method = 'fdr')[topindex])
highann = subset(ann, Probe.Set.ID %in% unitNames[topindex])
tmp = subset(highann, select = c('Probe.Set.ID','Gene.Symbol'))
toptable = merge(tmp, toptable)
toptable = toptable[order(toptable$FDR),]
toptable$rank = 1:nrow(toptable)
toptable = toptable[, c(ncol(toptable), 1:(ncol(toptable)-1))]
write.table(toptable, file = paste(prefix.out, 'TIL_by_expression_toptable.txt'),
row.names = F, quote = F, sep = '\t')
#Heatmap
set.seed(12345)
set = paste('unit', c(sample(ncol(Xdata), size = 10), toptable$Probe.Set.Number[c(50:1)]), sep = '')
mat = Xdata[,set]
mat = t(mat)
mat = mat[, order(data$til)]
heatmap(log2(mat), Rowv = NA, Colv = NA, col = heat.colors(256), margin = c(10,7))
library(RColorBrewer)
hmcols<-colorRampPalette(c('blue','white','orange','red', 'black'))(256)
heatmap(log2(mat), Rowv = NA, Colv = NA, col = hmcols, margin = c(5,0), labRow = '', labCol = '',
RowSideColors = c(rep('darkorange', 10), rep('darkblue', 50)))
#axis(1, line = 2, labels = c('', '','','',''), at = seq(0, 800, 200))
title(xlab = 'Samples ordered by TIL (maximum TIL to the right)', line = 2)
#text(x = 1, y = 10, labels = 'Random probe sets')
title(ylab = '10 random probe sets ', line = 0, cex = .8)
title(ylab = ' Top 50 probe sets ordered by p-value', line = 0, cex = .8)
title(ylab = ' (minimum p at the top)', line = -1, cex = .8)
legend("right", fill = c('black','red','orange','white', 'blue'),
legend = rep('', 5), bty = 'n', border = 'black', y.intersp = .5, cex = 2)
text(9.5,.4,'Max expression', cex = .8)
text(9.5,-.4,'Min expression', cex = .8)
#dev.print(png, file=paste(prefix.out,'results/TIL_expression_heatmap.png', sep = ''),
# width=640, height=500)
save.image(paste(prefix.out, 'TILs_on_expr.RData', sep = ''))
#####################################################################
#####################################################################
#
# IDFS relapse associated with gene expression
#
#
#####################################################################
#####################################################################
prefix.out = paste(getwd(), "/reports/report1/Relapse_on_expression/", sep='')
### Read IDFS relapse data and add to the other clinical data in dat
tmp = read.delim(paste(prefix.raw, 'HER2-lev-bordet-Aug2012-responsify.txt',
sep = ''), na.strings = c('', NA), check.names = F, dec = ',')
tmp$ID = as.character(dat$Frozen_tissue_BO_no)
tmp = subset(tmp, select = c('ID','IDFS_Y_N','IDFS_days'))
dat = merge(dat, tmp, sort = F, all.x = T)
################################################################
### Organize data
load(paste(prefix.norm, 'Expression/Expression_estimates.RData', sep = ''))
data = data.frame(array = names(expr)[6:ncol(expr)])
data$ID = substr(data$array, 3, 6)
data$index = 1:nrow(data)
data = merge(data, subset(dat, select = c('ID','age','tumor_size','Nodal_status',
'ER_status', 'til', 'date_diagnosis',
'IDFS_Y_N','IDFS_days')),
all.x = T, sort = F)
data$batch = 1
data$array = as.character(data$array)
tmp = nchar(data$array, type = 'c')
data$batch[substr(data$array, tmp, tmp) == '2'] = 2
data$batch[substr(data$array, 7, 9) == 'bis'] = 3
data$batch = as.factor(data$batch)
data$age_diag = as.numeric(data$age/365.25)
data$year_diag = as.numeric(substr(as.character(data$date_diagnosis), 1, 4))
data = data[order(data$index),]
data$IDFS_days = as.numeric(data$IDFS_days)
Xdata = as.data.frame(t(as.matrix(expr[,6:ncol(expr)])))
names(Xdata) = paste('unit', expr$unit, sep = '')
unitNames = expr$unitName
rm(expr)
gc()
#Finally remove samples with missing IDFS:
index = which(!is.na(data$IDFS_Y_N) & !is.na(data$IDFS_days))
data = data[index,]
Xdata = Xdata[index,]
#82 samples
#Now, data and Xdata could be cbind() and put into Cox models.
############################################################
# Cox models without naive RUV adjustment
library(survival)
out = matrix(NA, ncol = 2, nrow = ncol(Xdata))
colnames(out) = c('exp(coef)', 'Pr(>|z|)')
for (i in 1:ncol(Xdata)){
data$x = log2(Xdata[,i])
mod = coxph(Surv(IDFS_days, IDFS_Y_N) ~ x + age_diag + tumor_size + Nodal_status + ER_status, data = data)
out[i,] = summary(mod)$coef['x',colnames(out)]
}
hist(out[,2],breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Cox model P-values', xlab = 'p-values')
mtext('Multiple models')
dev.print(png, file=paste(prefix.out,'Pvalue_hist_relapse_expression.png', sep = ''),
width=400, height=400)
out = cbind(out, p.adjust(out[,2], method = 'BH'))
hist(out[,3],breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Cox model FDR values', xlab = 'FDR')
qqnorm(out[,1])
mtext('Multiple Relapse time ~ expression model coefficients')
#Index of the probe sets with p-value<0.01:
indexLM = which(out[, 2]<0.01) #Gives 352 probe sets
length(indexLM)
################################################################
# Naive RUV adjustment (Try 4 different levels of adjustment)
rnaive10 = naiveRandRuv(Y=as.matrix(log2(Xdata)), cIdx = rep(T, ncol(Xdata)), nuCoeff=1e-3, k=10)
res10 = matrix(NA, ncol = 2, nrow = ncol(Xdata))
colnames(res10) = c('exp(coef)', 'Pr(>|z|)')
for (i in 1:ncol(Xdata)){
data$x = rnaive10[,i]
mod = coxph(Surv(IDFS_days, IDFS_Y_N) ~ x + age_diag + tumor_size + Nodal_status + ER_status, data = data)
res10[i,] = summary(mod)$coef['x',colnames(res10)]
}
hist(res10[,2],breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Naive RUV P-values', xlab = 'p-values')
mtext('k = 10')
rnaive = naiveRandRuv(Y=as.matrix(log2(Xdata)), cIdx = rep(T, ncol(Xdata)), nuCoeff=1e-3, k=20)
res = matrix(NA, ncol = 2, nrow = ncol(Xdata))
colnames(res) = c('exp(coef)', 'Pr(>|z|)')
for (i in 1:ncol(Xdata)){
data$x = rnaive[,i]
mod = coxph(Surv(IDFS_days, IDFS_Y_N) ~ x + age_diag + tumor_size + Nodal_status + ER_status, data = data)
res[i,] = summary(mod)$coef['x',colnames(res)]
}
hist(res[,2],breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Naive RUV P-values', xlab = 'p-values')
mtext('k = 20')
rnaive100 = naiveRandRuv(Y=as.matrix(log2(Xdata)), cIdx = rep(T, ncol(Xdata)), nuCoeff=1e-1, k=20)
res100 = matrix(NA, ncol = 2, nrow = ncol(Xdata))
colnames(res100) = c('exp(coef)', 'Pr(>|z|)')
for (i in 1:ncol(Xdata)){
data$x = rnaive100[,i]
mod = coxph(Surv(IDFS_days, IDFS_Y_N) ~ x + age_diag + tumor_size + Nodal_status + ER_status, data = data)
res100[i,] = summary(mod)$coef['x',colnames(res100)]
}
hist(res100[,2],breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Naive RUV P-values', xlab = 'p-values')
mtext('Times 100')
rnaive.01 = naiveRandRuv(Y=as.matrix(log2(Xdata)), cIdx = rep(T, ncol(Xdata)), nuCoeff=1e-5, k=20)
res.01 = matrix(NA, ncol = 2, nrow = ncol(Xdata))
colnames(res.01) = c('exp(coef)', 'Pr(>|z|)')
for (i in 1:ncol(Xdata)){
data$x = rnaive.01[,i]
mod = coxph(Surv(IDFS_days, IDFS_Y_N) ~ x + age_diag + tumor_size + Nodal_status + ER_status, data = data)
res.01[i,] = summary(mod)$coef['x',colnames(res.01)]
}
hist(res.01[,2],breaks=c(0,0.001,0.01,0.05,0.1,0.2,0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1),
main = 'Naive RUV P-values', xlab = 'p-values')
#mtext('Div 100')
mtext('Multiple models, k = 20, nu = 1e-5')
dev.print(png, file=paste(prefix.out,'Pvalue2_hist_relapse_expression.png', sep = ''),
width=400, height=400)
#No significances!!! Therefore we save no further results.
save.image(paste(prefix.out, 'IDFSs_on_expr.RData', sep = ''))
################################################################
################################################################
#
# CN segments and tils: checks and data preparation
#
################################################################
################################################################
prefix.out = paste(getwd(), "/reports/report1/TILs_on_CN/", sep='')
library(aroma.affymetrix)
library(PSCBS)
library(IRanges)
library(parallel)
################################################################
### Fileinfo: which sample IDs correspond to which arrays?
### Resulting 'info' will be used further down
### Array names
###
filenames = list.files(paste(prefix.raw, 'GenomeWideSNP_6', sep = ''))
filenames = substr(filenames, 1, nchar(filenames, type = 'c')-4)
info = data.frame(arrname = filenames)
info$AROS.ID = substr(info$arrname, 1, 8)
tmp = read.delim(paste(prefix.ann, 'GenomeWideSNP_6/A1761_work sheet_ SNP 6.0_okay_Scan_info.txt', sep = ''),
sep = '\t')
info = merge(info, tmp, all = T)[,c('AROS.ID','arrname','Sample.ID')]
info$Sample.ID[info$AROS.ID %in% c('A1974-08','A1974-09','A1974-10')] =
c('3860 T','4541 T','4868 T')
info$pass = T
info$pass[info$arrname %in% c('A1761-02','A1761-08','A1761-08rh',
'A1761-20','A1761-20rh','A1761-26',
'A1761-30','A1761-30rh','A1761-50')] = F
################################################################
### Organize CN data
info$ntcn = T
info$ntcn[info$pass == F] = NA
segs = NULL
for (arr in 1:56){
#Prepare data for this sample
samp = as.character((info$AROS.ID[info$pass])[arr])
fitname = paste('fit', arr, sep = '')
filename <- paste(prefix.norm, 'SNP/CBSfits/', fitname, '.RData', sep = '');
fit <- loadObject(filename);
segtmp = getSegments(fit)
segtmp$AROS.ID = samp
if (is.null(segtmp$ntcnCall)) {
segtmp$ntcnCall = NA
info$ntcn[info$AROS.ID == samp] = F
}
segs = rbind(segs, segtmp)
}
tmp = subset(info, pass, select = c('AROS.ID','Sample.ID'))
tmp$ID = substr(as.character(tmp$Sample.ID), 1, 4)
tmp = tmp[,c('AROS.ID','ID')]
segs = merge(segs, tmp, all.x = T, sort = F)
segs = subset(segs, !is.na(chromosome))
#segs = subset(segs, ! (AROS.ID %in% c('A1761-20rh','A1761-25','A1761-30rh','A1761-42'))) #Preliminary: exclude difficult samples
segs = subset(segs, chromosome <= 23 & !is.na(tcnMean))
ir = disjoin(IRanges(start = segs$tcnStart, end = segs$tcnEnd-1))
ir = mclapply(1:23, mc.cores = 2, function(k){
index = which(segs$chromosome == k)
disjoin(IRanges(start = segs$tcnStart[index], end = segs$tcnEnd[index]-1))
})
names(ir) = as.character(1:23)
ir = do.call('RangesList', ir) #ir defines the segments which will be correlated to TILs
seginfo = as.data.frame(ir) #seginfo has the ir information in dataframe format
seginfo$mid = (seginfo$start + seginfo$end)/2
Xdata = matrix(NA, ncol = sum(mapply(length, ir)), nrow = length(unique(segs$ID)))
colnames(Xdata) = paste('seg', 1:ncol(Xdata), sep = '')
rownames(Xdata) = unique(segs$ID)
for (i in 1:nrow(Xdata)){
index = which(segs$ID == unique(segs$ID)[i])
rd = RangedData(IRanges(start = segs$tcnStart[index], end = segs$tcnEnd[index]-1), tcnMean=segs$tcnMean[index],
space = segs$chromosome[index], universe = 'hg19') #Each interval is redefined to have an open right end
rle = coverage(rd, weight = 'tcnMean', width = tapply(segs$tcnEnd, segs$chromosome, max))
Xdata[i, ] = unlist(viewMeans(Views(rle, ir)))
} #Xdata holds segment tcn:s, one row for each sample
#There are 26075 segments
#Number of patients with changes in each segment
Ndata = matrix(NA, ncol = sum(mapply(length, ir)), nrow = length(unique(segs$ID)))
colnames(Ndata) = paste('seg', 1:ncol(Ndata), sep = '')
rownames(Ndata) = unique(segs$ID)
for (i in 1:nrow(Ndata)){
index = which(segs$ID == unique(segs$ID)[i])
rd = RangedData(IRanges(start = segs$tcnStart[index], end = segs$tcnEnd[index]-1), ntcnCall=segs$ntcnCall[index],
space = segs$chromosome[index], universe = 'hg19') #Each interval is redefined to have an open right end
?
? Ndata[i, ] = unlist(viewApply(Views(rle, ir)))
} #Xdata holds segment tcn:s, one row for each sample
################################################################
#Now prepare clinical variables
tmp = subset(info, pass, select = c('shortname','Sample.ID'))
tmp$ID = substr(as.character(tmp$Sample.ID), 1, 4)
tmp = tmp[,c('shortname','ID')]
names(tmp)[1] = 'array'
data = merge(tmp, subset(dat, select = c('ID','age','tumor_size','Nodal_status','ER_status', 'til', 'date_diagnosis')),
sort = F, all.x = T)
data$batch = 1
data$batch[nchar(as.character(data$array))>8] = 2
data = subset(data, ID %in% segs$ID) #Limit the arrays to those for which we have CNs
data$index = 1:nrow(data)
data$age_diag = as.numeric(data$age/365.25)
data$year_diag = as.numeric(substr(as.character(data$date_diagnosis), 1, 4))
data = data[order(data$index),]
###Load array dates
#Array dates were checked and were all within 3 hours in the order of info$shortname, not informative
#Finally remove samples with missing til:
index = which(!is.na(data$til))
data = data[index,]
Xdata = Xdata[index,]
#Now, data and Xdata could be cbind() and put into linear models.
################################################################
### Clinical data overview
plot(data[,c('tumor_size', 'til', 'age_diag')]) #No essential difference from the plots of those
#samples that have gene expression values
par(mfrow = c(1,2))
boxplot(til~Nodal_status, data = data, xlab = 'Nodal status', ylab = 'Stomal LI (%)', col = 'wheat')
boxplot(til~ER_status, data = data, xlab = 'ER status', ylab = 'Stomal LI (%)', col = 'wheat',
xaxt = 'n')
axis(1, labels = c('ER-','ER+'), at = 1:2)
dev.print(png, file=paste(prefix.out,'results/Til_by_nodal_and_ER_CN.png', sep = ''),
width=400, height=400) #Shows that we do not have as many patients with very high TIL as we had with expression data,
#but the ones we have show a higher median TIL in ER+ than ER-.
summary(data[,c(5, 8, 11)])
################################################################
### Check unwanted variation through principal components plots
pc = prcomp(Xdata)
## Get the standard deviation and variance per principal component
sd.per.pc <- pc$sdev
var.per.pc <- sd.per.pc^2
## Display the percentage of total variance explained by each
sd.per.pc.percent <- sd.per.pc/sum(sd.per.pc)
var.per.pc.percent <- var.per.pc/sum(var.per.pc)
png(paste(prefix.out,'results/Expression perc variance per pc.png', sep = ''),
width=400, height=400)
barplot(var.per.pc.percent[1:10]*100, main='Gene expression, Percent of variance per component',
xlab='Component', ylab = '% variance')
dev.off()
newids = data$ID
#newids[c(19, 78)] = paste(newids[c(19, 78)], 'bis', sep = '')
#newids[c(25, 26, 46, 51, 80)] = paste(newids[c(25, 26, 46, 51, 80)], '_2', sep = '')
dimnames(pc$x)[[1]] = newids
## Plot components PC1 and PC2
plot(pc$x[,1:2],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC1', ylab='PC2')
text(pc$x[,1:2],labels=dimnames(pc$x)[[1]],col=as.numeric(data$batch),pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$batch)),
legend=paste('Batch', as.numeric(unique(data$batch))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC1 and PC3
plot(pc$x[,c(1,3)],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC1', ylab='PC3')
text(pc$x[,c(1,3)],labels=dimnames(pc$x)[[1]],col=as.numeric(data$batch),pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$batch)),
legend=paste('Batch', as.numeric(unique(data$batch))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC1 and PC4
plot(pc$x[,c(1,4)],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC1', ylab='PC4')
text(pc$x[,c(1,4)],labels=dimnames(pc$x)[[1]],col=as.numeric(data$batch),pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$batch)),
legend=paste('Batch', as.numeric(unique(data$batch))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC2 and PC3
plot(pc$x[,c(2,3)],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC2', ylab='PC3')
text(pc$x[,c(2,3)],labels=dimnames(pc$x)[[1]],col=as.numeric(data$batch),pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$batch)),
legend=paste('Batch', as.numeric(unique(data$batch))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC2 and PC4
plot(pc$x[,c(2,4)],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC2', ylab='PC4')
text(pc$x[,c(2,4)],labels=dimnames(pc$x)[[1]],col=as.numeric(data$batch),pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$batch)),
legend=paste('Batch', as.numeric(unique(data$batch))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC3 and PC4
plot(pc$x[,c(3,4)],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC3', ylab='PC4')
text(pc$x[,c(3,4)],labels=dimnames(pc$x)[[1]],col=as.numeric(data$batch),pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$batch)),
legend=paste('Batch', as.numeric(unique(data$batch))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC1 and PC2 coloured by ER status
plot(pc$x[,1:2],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC1', ylab='PC2')
text(pc$x[,1:2],labels=dimnames(pc$x)[[1]],col=as.numeric(data$ER_status)+1,pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$ER_status)+1),
legend=paste('ER', as.numeric(unique(data$ER_status))),pch=1,cex=0.7,bg='white',bty='o')
## Plot components PC1 and PC2 coloured by Nodal status
plot(pc$x[,1:2],
type='n',
panel.first=grid(col='black'),
main=paste('PCA gene expression ',
nrow(Xdata), 'samples *', ncol(Xdata), 'genes', sep=' '),
xlab='PC1', ylab='PC2')
text(pc$x[,1:2],labels=dimnames(pc$x)[[1]],col=as.numeric(data$Nodal_status)+1,pch=0.5)
legend('bottomleft',col=as.numeric(unique(data$Nodal_status)+1),
legend=paste('Nodal status', as.numeric(unique(data$Nodal_status))),pch=1,cex=0.7,bg='white',bty='o')