-
Notifications
You must be signed in to change notification settings - Fork 1
/
CreateQCPlots.R
1063 lines (945 loc) · 44.8 KB
/
CreateQCPlots.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
# version 1.3.2
# - Fully compatible with first dev release
require(RColorBrewer) ## Color Palette
require(affy) ## plotDensity function
require(gplots) ## heatmap.2 function
require(geneplotter) ## densCols function
require(shape) ## imageplot3by2Adp function
require(KernSmooth) ## Required for barplots / MvA plots
require(Ringo) ## Required for corPlot
maxSamples <- 65
cex.scale <- 0.125
# legendTemp <- floor( dim(x[[2]])[2] / 65 )
# cex.axis <- 1 - (legendTemp * 0.125)
# png(file=paste(fileName, ".png", sep=""), width=1600+(800 * legendTemp), height=1200, pointsize=20)
##############################
#CreateSummaryPlots function##
##############################
CreateSummaryPlots <- function(RG) {
# This function is a bit more flexible in terms of available summary fields.
# - based on the datatype make a distinction between red/green images.
# - Updated the error detection text.
# - Improved removal of the 4 fixed columns (mean, median, min, max)
error <- NULL
if(is.null(RG$datatype)) { error <- c(error, "\n- a datatype field must be present in your data object with value \"green\", \"red\" or \"both\")" ) }
if(is.null(RG$summary)) { error <- c(error, "\n- a summary field must be present in the data object, call CalculateSummary first") }
if(!is.null(error)) {
cat("[ERROR] CreateSummaryPlots could not be started due to the following errors:")
stop(paste(error, "\n"))
}
datatype <- RG$datatype
## Remove the Minimum, Mean, MEdian and Maximum rows.
temp <- c("Minimum", "Mean", "Median", "Maximum")
temp2 <- rownames(RG$summary) %in% temp
RGsummary <- RG$summary[!temp2,]
rm(temp, temp2)
totalArrays <- nrow(RGsummary)
npages <- ceiling(totalArrays/16)
x <- totalArrays/npages
ArraysPerPage <- ceiling(x)
hasRealBG <- !is.null(RG$other$rBGMeanSignal)|!is.null(RG$other$gBGMeanSignal)
hasManualFlags <- !is.null(RG$other$manualFlags)
main.titles <- c("Foreground","Background",rep("Real Background",hasRealBG),"Number of Good Probes","Percentage of Good Probes",RG$QC.vector,rep("Manually Flagged",hasManualFlags))
y.axis.labels <- c(rep("Mean Signal",2+hasRealBG),"Number of reporters","% of reporters",rep("Number of reporters",length(RG$QC.vector)+hasManualFlags))
longest.filename <- max(nchar(main.titles))
filenames <- sub(" ","_", paste("Summary_", main.titles, sep=""))
cat("Plotting Summary images\n")
pbar <- txtProgressBar(min=0, max=npages, char="*", width=20, style=3)
for (i in 1:npages) {
# Sys.sleep(0.5)
if(i == 1) {k <- 1; k.max <- k + (ArraysPerPage-1)}
if(i > 1) {k <- k + ArraysPerPage; k.max <- k + (ArraysPerPage-1)}
if(k + (ArraysPerPage-1) >= totalArrays) {k.max <- totalArrays}
if(npages > 1) {filename.suffix <- paste("_part_", i, ".png", sep="")} else {filename.suffix=".png"}
for (j in 1:length(main.titles)) {
## IF the main.title name is not present in the object, the data does not exist and should be skipped.
if(length(grep(main.titles[j], colnames(RGsummary))) == 0 ) { next }
png(paste(filenames[j], filename.suffix, sep=""), width=1600, height=1200, pointsize=20)
par(las=2, mar=c(longest.filename/1.75,5,2,2), cex.axis=0.75)
## Foreground / Background plots
if (j == 1 | j == 2) {
if (RG$datatype == "both") {
tmpObj <- rbind(RGsummary[,j],RGsummary[,j+2+hasRealBG])
legend <- colnames(RG$summary)[c(j,j+2+hasRealBG)]
} else {
tmpObj <- RGsummary[,j]
legend <- colnames(RG$summary)[j]
}
}
## Plot the realBG column if present
if (j == 3 & hasRealBG == 1) {
if (RG$datatype == "both") {
tmpObj <- rbind(RGsummary[,j],RGsummary[,j+3])
legend <- colnames(RG$summary)[c(j,j+3)]
} else {
tmpObj <- RGsummary[,j]
legend <- colnames(RG$summary)[j]
}
}
if ( j > 3 | (j == 3 & !hasRealBG == 1)) {
## check first if the name exists in your summary object. If not, skip image.
ext1 <- 2*(j>(length(main.titles)-length(RG$QC.vector)))
ext2 <- (length(RG$QC.vector)-2)*(j>(length(main.titles)-length(RG$QC.vector)))
tmpObj <- rbind(RGsummary[,j+2*(RG$datatype=="both")+(hasRealBG & RG$datatype=="both")+ext1],RGsummary[,j+2*(RG$datatype=="both")+(hasRealBG & RG$datatype=="both")+2+ext1+ext2])
legend <- colnames(RG$summary)[c(j+2*(RG$datatype=="both")+(hasRealBG & RG$datatype=="both")+ext1,j+2*(RG$datatype=="both")+(hasRealBG & RG$datatype=="both")+2+ext1+ext2)]
}
if (j == length(main.titles) & hasManualFlags) {
tmpObj <- tmpObj[2,]
legend <- legend[2]
}
if (j == 1 | j == 2 | (j == 3 & hasRealBG == 1)) {
col <- c(rep("salmon",RG$datatype!="green"),rep("springgreen3",RG$datatype!="red"))
} else {
col <- c("steelblue3","lightblue")
}
if(!is.null(dim(tmpObj))) {
plotData <- tmpObj[,k:k.max]
labels <- colnames(tmpObj)[k:k.max]
} else { #just one row
plotData <- tmpObj[k:k.max]
labels <- names(tmpObj)[k:k.max]
}
if(length(grep("percentage",tolower(main.titles[j])))>0) {
y.lim <- c(0,112)
} else {
if(length(grep("number",tolower(main.titles[j])))>0) {
if(RG$source!="genepix") {
y.lim <- c(0,1.12*sum(RG$genes$ControlType==0))
} else {
y.lim <- c(0,1.12*sum(RG$genes$ControlType=="false"))
}
} else {
y.lim <- c(0.92*min(tmpObj), 1.02*max(tmpObj)+0.1*(max(tmpObj)-min(tmpObj)))
}
}
#barplot(height=plotData, beside=TRUE, xaxt="n", lend=1, lwd=700/length(plotData), ylim=y.lim, xlab="", main=main.titles[j], ylab=y.axis.labels[j], col=c("steelblue3","mediumblue"))
barplot(height=plotData, beside=TRUE, main=main.titles[j], ylab=y.axis.labels[j], ylim=y.lim, xpd=FALSE, col=col,legend.text=legend, cex.lab=0.75)
# axis(1, labels=labels, at=1:length(labels))
if(!is.null(dim(tmpObj))) {
abline(h=mean(tmpObj[1,]),lty=2, col=col[1])
abline(h=mean(tmpObj[2,]),lty=2, col=col[2])
} else {
abline(h=mean(tmpObj),lty=2, col=col[1])
}
dev.off()
setTxtProgressBar(pbar, i)
}
cat("\n")
}
cat("\n")
}
##############################
##imageplot3by2Adp function ##
##############################
## Adapted imageplot3by2 function
imageplot3by2Adp <- function (data.object, which.field, name, high, low, log.transform=FALSE, symm=FALSE, orientation=NULL, debug=0, base.number=12) {
error <- NULL
if(!class(data.object) %in% c("RGList","MAList","EListRaw")) { error <- c(error, "- Object is not of RGList, ElistRaw, or MAList class\n") }
if(is.null(name)) { error <- c(error, "- name parameter not filled in\n") }
if(!is.character(name)) { error <- c(error, "- name parameter is not a character string\n") }
if(is.null(data.object$printer)) { error <- c(error, "- The printer field in the full object is empty, whereas this information is required for image plots") }
if(!is.null(error)) {
cat(paste("[ERROR] Please resolve the following issue(s):\n", paste(error, collapse="\n"), "\n", sep=""))
}
if(debug==1) {
cat("Paste the following below to debug:\n")
cat(paste("data.object <-", deparse(substitute(data.object)), "\nwhich.field <-", deparse(substitute(which.field)), "\n"))
cat(paste("name <-", deparse(substitute(name)), "\nhigh <-", deparse(substitute(high)), "\nlow <-", deparse(substitute(low)), "\n"))
cat(paste("log.transform <-", deparse(substitute(log.transform)), "\nsymm <-", deparse(substitute(symm)), "\norientation <-", deparse(substitute(orientation)), "\n"))
stop("Happy debugging!")
}
cat(paste(" --", paste(rep("-", nchar(name)), collapse=""), "--\n", sep="", collapse=""))
cat(paste(" - ", name, " -\n", sep="", collapse=""))
cat(paste(" --", paste(rep("-", nchar(name)), collapse=""), "--\n", sep="", collapse=""))
cat(" * Preprocessing... ")
prefix <- paste("VirtualArray", name, sep = "-")
narrays <- ncol(which.field)
npages <- ceiling(narrays/6)
cnames <- colnames(which.field)
max.dimensions <- base.number * 200
#Calculating z-ranges and their mean and stdev
minima <- NULL
maxima <- NULL
nr.of.well.above <- NULL
nr.of.saturated <- NULL
if(log.transform) {plot.field <- log(which.field,2)} else {plot.field <- which.field}
## Check if object contains binary values, predefined values or regular data:
if((sum(!(names(table(plot.field)) %in% c(0,1))) > 0)) {
binary <- FALSE
#check whether only values from (0,-3,-5,-8) are present
if(sum(names(table(plot.field)) %in% c(0,-3,-5,-8)) == length(names(table(plot.field)))) {
QCField <- TRUE
values <- as.numeric(names(table(plot.field)))
number <- apply(plot.field,2,function(f) sum(f!=0,na.rm=TRUE))
mean.number <- mean(number)
stdev.number <- sd(number)
} else {
QCField <- FALSE
minima <- apply(plot.field,2,min,na.rm=TRUE)
maxima <- apply(plot.field,2,max,na.rm=TRUE)
mean.min <- mean(minima)
stdev.min <- sd(minima)
mean.max <- mean(maxima)
stdev.max <- sd(maxima)
}
} else {
binary <- TRUE
QCField <- FALSE
number <- apply(plot.field,2,sum,na.rm=TRUE)
mean.number <- mean(number)
stdev.number <- sd(number)
}
#Adjusting object for optimal visualisation (landscape orientation)
printer.info <- data.object$printer
printer.c <- printer.info$nspot.c
printer.r <- printer.info$nspot.r
if((printer.c > printer.r) & (printer.info$ngrid.c == 1) & (printer.info$ngrid.r == 1)) {
printer.info$nspot.c <- printer.r
printer.info$nspot.r <- printer.c
plot.field <- plot.field[order(-data.object$genes[,"Col"], data.object$genes[,"Row"], decreasing=TRUE),]
}
#Plotting the array images
cat(" ok.\n * Plotting images:\n")
pbar <- txtProgressBar(min=0, max=npages, char="*", width=20, style=3)
dimensions <- c((base.number * printer.info$nspot.c * printer.info$ngrid.c), (base.number * printer.info$nspot.r * printer.info$ngrid.r))
## If the image width gets bigger than 720 px, then rescale the whole image to this maximum width. This to ensure that the legend plotted is done correctly.
if(dimensions[2] > dimensions[1]) { ## Width > height --> landscape
# print("1")
dimensions[1] <- dimensions[1] / dimensions[2] * max.dimensions
dimensions[2] <- max.dimensions
} else {
# print("2")
dimensions[2] <- dimensions[2] / dimensions[1] * max.dimensions
dimensions[1] <- max.dimensions
}
## For the newer generation arrays the width is 6-7 larger than the height. I would like to have at least a minimum height of the images of 800 to display the image properly.
if(dimensions[1] < 1000) {
dimensions[2] <- dimensions[2] / dimensions[1] * 1000
dimensions[1] <- 1000
}
for (ipage in 1:npages) {
i1 <- ipage * 6 - 5
i2 <- min(ipage * 6, narrays)
fn <- paste(prefix, "-", i1, "-", i2, ".png", sep = "")
png(filename=fn, width=dimensions[2], height=dimensions[1], pointsize=20)
layout(rbind(
matrix(data=c(rep(1,4),rep(2,4)), nrow=3, ncol=8, byrow=T),
matrix(data=c(rep(3,4),rep(4,4)), nrow=3, ncol=8, byrow=T),
matrix(data=c(rep(5,4),rep(6,4)), nrow=3, ncol=8, byrow=T)))
for (i in i1:i2) {
col.main <- "black"
if (binary | QCField) {
if (stdev.number > 0) {
if(sum(plot.field[,i]!=0, na.rm=TRUE) <= mean.number-2*stdev.number) {col.main <- "red"}
if(sum(plot.field[,i]!=0, na.rm=TRUE) >= mean.number+2*stdev.number) {col.main <- "red"}
}
} else {
if (stdev.max > 0 | stdev.min > 0) {
if(min(plot.field[,i], na.rm=TRUE) <= mean.min-2*stdev.min) {col.main <- "red"}
if(min(plot.field[,i], na.rm=TRUE) >= mean.min+2*stdev.min) {col.main <- "red"}
if(max(plot.field[,i], na.rm=TRUE) <= mean.max-2*stdev.max) {col.main <- "red"}
if(max(plot.field[,i], na.rm=TRUE) >= mean.max+2*stdev.max) {col.main <- "red"}
}
}
limit <- 0.05
## if symm is false, take the 5% and 95% values of the distribution field and plot these.
if(symm) {
zlim=c(-max(abs(quantile(plot.field[,i], c(limit,1-limit), na.rm=TRUE))),max(abs(quantile(plot.field[,i], c(limit,1-limit), na.rm=TRUE))))
} else {
zlim=quantile(plot.field[,i], c(limit,1-limit), na.rm=TRUE)
}
if(binary) zlim <-c(0, 1)
if(QCField) zlim <- c((data.object$datatype=="green") * -3 + (data.object$datatype=="red") * -5 + (data.object$datatype=="both") * -8, 0)
par(mar=(c(2,3,3,5) + 0.1))
## Function throws error if z-limits are identical. Adding artificial values to it:
if(zlim[1] == zlim[2]) { zlim <- c(zlim[1]-0.5, zlim[2]+0.5) }
imageplot(plot.field[,i], printer.info, zlim=zlim, mar=c(2, 2, 4, 4), main=cnames[i], high=high, low=low, legend=TRUE, col.main=col.main, zerocenter=symm)
# emptyplot()
ColorFunction <- colorRampPalette(c(low,high))
cex.size <- 0.6
if( dimensions[2] < dimensions[1]) {
pos.x <- c(0.88, 0.89)
pos.y <- c(0.02, 0.94)
} else {
pos.x <- c(0.96, 0.965) #0.91, 0.93 ## 0.95, 0.96
pos.y <- c(0.02, 0.94)
if(dimensions[2] >= 5000) {
pos.x <- c(0.985, 0.99)
}
}
if(binary) {
colors2use <- ColorFunction(2)
colorlegend(col=colors2use, zlim, left=FALSE, zval=c(zlim[1], zlim[2]), posy=pos.y, posx=pos.x, cex=cex.size, digit=1)
} else {
if(QCField) {
colors2use <- ColorFunction(length(values))
colorlegend(col=colors2use, zlim, left=FALSE, zval=values, posy=pos.y, posx=pos.x, cex=cex.size, digit=1)
} else {
colors2use <- ColorFunction(100)
colorlegend(col=colors2use, zlim, left=FALSE, zval=c(zlim[1], mean(zlim), zlim[2]), posy=pos.y, posx=pos.x, cex=cex.size, digit=1)
}
}
}
## Have to check if this is the proper position.
setTxtProgressBar(pbar, ipage)
dev.off()
}
cat("\n\n")
}
##############################
## Hierarchcluster function ##
##############################
HierarchCluster <- function(x, dist.method = "euclidean", clust.method = "ward", main, data.return=FALSE, image.width=NULL, image.height=1400, pointsize=25, ...) {
a <- dist(t(x), method=dist.method)
b <- hclust(a, method=clust.method)
if(is.null(image.width)) {
temp <- floor( dim(x)[2] / maxSamples )
image.width= 1600 + (800 * temp)
}
png(paste("Clustering_", dist.method,"-", clust.method,"__",main,".png", sep=""), width=image.width, height=image.height, pointsize=pointsize)
if(dim(x)[2] >= 100) par(cex=0.8, cex.axis=1.25, cex.lab=1.25, cex.main=1.5, cex.sub=1.25)
plot(b, main=main, ...)
dev.off()
if(data.return==TRUE) {return(b)}
}
##############################
## Heatmap function ##
##############################
CreateHeatMap <- function(data, main=NULL, image.width=NULL, image.height=NULL, pointsize=25) {
oldData <- data
no.title <- ifelse(is.null(main), 1, 0)
list.used <- 0
if(is.list(data)) { temp.main <- names(data); data <- data[[1]]; list.used <- 1 }
if(is.null(image.width) | is.null(image.height) ) {
temp <- floor( dim(data)[2] / maxSamples )
image.width <- 2000 + (800 * temp)
image.height <- 1400 + (600 * temp)
}
my.dist <- function(x) dist(x, method="euclidean")
my.hclust <- function(d) hclust(d, method="ward")
legend.keysize <- 0.7
if(class(data) != "matrix") {
if(data$datatype=="both") {
if(no.title == 1) {
if(list.used == 1) {
main1 <- paste("Heatmap Logratio - ", temp.main, sep="")
} else {
main1 <- paste("Heatmap Logratio - ", deparse(substitute(data)), sep="")
}
}
if(no.title == 0) {
if(length(main)>1) { stop("main variable has more than 1 argument!\n") };
main1 <- paste("Heatmap Logratio - ", main, sep="")
}
png(paste(gsub(" ", "_", main1), ".png", sep=""), width=image.width, height=image.height, pointsize=pointsize)
crp <- cor(data$M, use="complete.obs")
heatmap.2(crp, distfun=my.dist, hclustfun=my.hclust, trace="none", margins=c(20,20), symm=TRUE, density.info="density", main=main1, dendrogram="row", keysize=legend.keysize)
dev.off()
if(no.title == 1) {
if(list.used == 1) {
main2 <- paste("Heatmap Average Intensity - ", temp.main, sep="")
} else {
main2 <- paste("Heatmap Average Intensity - ", deparse(substitute(data)), sep="")
}
}
if(no.title == 0) { main2 <- paste("Heatmap Average Intensity - ", main, sep="") }
png(paste(gsub(" ", "_", main2),".png", sep=""), width=image.width, height=image.height, pointsize=pointsize)
crp <- cor(data$A, use="complete.obs")
heatmap.2(crp, distfun=my.dist, hclustfun=my.hclust, trace="none", margins=c(20,20), symm=TRUE, density.info="density", main=main2, dendrogram="row", keysize=legend.keysize)
dev.off()
} else {
if(no.title == 1) {
if(list.used == 1) {
main2 <- paste("Heatmap Estimated Intensity - ", temp.main, sep="")
} else {
main2 <- paste("Heatmap Estimated Intensity - ", deparse(substitute(data)), sep="")
}
}
if(no.title == 0) { main2 <- paste("Heatmap Estimated Intensity - ", main, sep="") }
png(paste(gsub(" ","_", main2),".png", sep=""), width=image.width, height=image.height, pointsize=pointsize)
crp <- cor(data$other$EST, use="complete.obs")
heatmap.2(crp, distfun=my.dist, hclustfun=my.hclust, trace="none", margins=c(20,20), symm=TRUE, density.info="density", main=main2, dendrogram="row", keysize=legend.keysize)
dev.off()
}
} else { # object has not been generated within arrayQC
if(is.list(oldData)) {
if(no.title == 1) { main2 <- paste("Heatmap Estimated Intensity - ", temp.main, sep="") }
} else {
if(no.title == 1) { main2 <- paste("Heatmap Estimated Intensity - ", deparse(substitute(data)), sep="") }
}
if(no.title == 0) { main2 <- paste("Heatmap Estimated Intensity - ", main, sep="") }
png(paste(gsub(" ","_", main2), ".png", sep=""), width=2000, height=1400, pointsize=18)
crp <- cor(data, use="complete.obs")
heatmap.2(crp, distfun=my.dist, hclustfun=my.hclust, trace="none", margins=c(20,20), symm=TRUE, density.info="density", main=main2, dendrogram="row", keysize=legend.keysize)
dev.off()
}
}
## CreateCorplot
CreateCorplot <- function(x, which.channel=NULL, data.type=NULL, fileName = NULL) {
y <- NULL
if(class(x) == "list") {
if(is.null(data.type)) {
data.type <- names(x)
}
x <- x[[1]]
}
checks <- c("MAList", "RGList", "EListRaw", "matrix")
if(sum(class(x) %in% checks) == 1) {
if(sum(class(x) %in% checks[1:3]) == 1) {
## Now we expect that which.channel is filled in!!
if(is.null(which.channel)) { stop("For an object of the MAList/RGList/EListRaw class the which.channel variable needs to be filled in!") }
if(is.null(fileName)) {
if(!is.null(data.type)) {
fileName <- paste("Correlation_Plot_", data.type, "_", which.channel, ".png", sep="")
} else {
cat("Please provide the type.data parameter if you would like to add a specific title to your filename!\n")
fileName <- paste("Correlation_Plot_", which.channel,".png")
}
}
y <- x[[which.channel]]
}
if( class(x) == "matrix" ) {
y <- x
if(is.null(fileName)) {
if(is.null(data.type)) {
fileName <- "Correlation_Plot.png"
} else {
fileName <- paste("Correlation_Plot_", data.type, ".png", sep="")
}
}
}
}
if(is.null(y)) { stop(paste("object x is not of the following class:\n- ", paste(checks, collapse=" / "), sep="")) }
png(file=fileName, width= (1400 * ceiling( dim(y)[2] / 15) ), height=(1400 * ceiling( dim(y)[2] / 15) ), pointsize=25)
corPlot(y, useSmoothScatter=FALSE)
dev.off()
}
##############################
## CreatePCAplot function ##
##############################
CreatePCAplot <- function(data, main=NULL, scaled_pca=TRUE, namesInPlot=FALSE){
# PCA performed on reporters NOT containing NAs only
# Scaled PCA by default
if(class(data) == "list") {
if(length(data) > 1) { stop("List with more than 1 element found!\n") }
main.temp <- names(data)
data <- data[[1]]
} else {
main.temp <- main
}
if(class(data)!= "matrix") {
if(data$datatype == "both") {
pcaData <- data$M
} else {
pcaData <- data$other$EST
}
} else {
pcaData <- data
}
if(is.null(main.temp)) { baseName <- deparse(substitute(data)) } else { baseName <- main.temp }
pca1 <- NULL
try(pca1 <- prcomp(t(pcaData[apply(pcaData,1,function(r) {sum(is.na(r)) == 0}),]), retx=T, center=T, scale=scaled_pca),TRUE)
if(!is.null(pca1)) {
perc_expl1 <- round(((pca1$sdev[1:3]^2)/sum(pca1$sdev^2))*100,2)
tmain <- paste("PCA analysis of", baseName)
plotColors <- rainbow(dim(pcaData)[2])
arrayNames <- as.character(colnames(pcaData))
cex.circle <- 1.3
cex.text <- 0.6
tcol <- "#444444"
if(!namesInPlot) {
legend.cols <- ceiling( length(arrayNames) / 64 )
image.width <- 1600 + (legend.cols * 200)
} else {
image.width <- 1600
}
# 1200+(!namesInPlot)*400
png(file = paste("PCAplot_",baseName,".png",sep=""), width=image.width, height=1200, pointsize=25)
if(!namesInPlot) {
layout(rbind(c(1,1,2,2,rep(5, legend.cols)),c(3,3,4,4,rep(5, legend.cols))))
par(cex.main=1.8, cex.lab=1.4, cex.axis=1.4)
} else {
layout(rbind(c(1,2),c(3,4)))
}
plot(pca1$x[,1],pca1$x[,2],cex=cex.circle,pch=17:0,
col=plotColors,xlab=paste("PC1 (",perc_expl1[1],"%)",sep=""),
ylab=paste("PC2 (",perc_expl1[2],"%)",sep=""))
if(namesInPlot) text(pca1$x[,1],pca1$x[,2], arrayNames,pos=4,cex=cex.text,
col=tcol)
title(main=tmain, font=2)
plot(pca1$x[,1],pca1$x[,3],cex=cex.circle,pch=17:0,
col=plotColors,xlab=paste("PC1 (",perc_expl1[1],"%)",sep=""),
ylab=paste("PC3 (",perc_expl1[3],"%)",sep=""))
if(namesInPlot) text(pca1$x[,1],pca1$x[,3], arrayNames,pos=4,cex=cex.text,
col=tcol)
plot(pca1$x[,2],pca1$x[,3],cex=cex.circle,pch=17:0,
col=plotColors,xlab=paste("PC2 (",perc_expl1[2],"%)",sep=""),
ylab=paste("PC3 (",perc_expl1[3],"%)",sep=""))
if(namesInPlot) text(pca1$x[,2],pca1$x[,3], arrayNames,pos=4,cex=cex.text,
col=tcol)
barplot((pca1$sdev^2)/sum(pca1$sdev^2),xlab="components",
ylab="% of total variance explained")
if(!namesInPlot) {
plot(1,type="n",xaxt="n",yaxt="n",xlab="",ylab="",bty="n")
legend("topright",arrayNames, ncol=legend.cols, pch=17:0,col=plotColors,cex=1, title="Sample Legend")
}
dev.off()
} else {
cat("Warning: pca on the",baseName,"data set unsuccessful, possibly due to lack of memory\n\n")
}
}
#################################
## CreateDensityPlots function ##
#################################
CreateDensityPlots <- function(x, name=NULL) {
if(is.list(x) && !class(x) %in% c("RGList", "EListRaw", "MAList")) {
name <- names(x)
x <- x[[1]]
# If maximum value of x > 1.000 then the data needs to be log transformed.
}
if((!class(x) %in% c("RGList","EListRaw","MAList")) & (!is.matrix(x))) stop("Only object of class RGList, EListRaw, MAList, or matrix can be handled")
if(is.null(name)) stop("The \"name\" parameter must be provided\n")
# Preparation of the number of pages needed
maxArrays <- ncol(x)
npages <- ceiling(maxArrays/9)
y <- maxArrays/npages
ArrayPerPage <- ceiling(y)
# Loop that calls the plotting
cat("Plotting images ")
for (i in 1:npages) {
## k and k.max are the counters, with k being the start position and
## k.max being the end position
if(i == 1) {k <- 1; k.max <- k + (ArrayPerPage-1)}
if(i > 1) {k <- k + ArrayPerPage; k.max <- k + (ArrayPerPage-1)}
if(k + (ArrayPerPage-1) >= maxArrays) { k.max <- maxArrays}
if(npages > 1) {
outputname <- paste("DensityPlots_", name, "_Part_", i, ".png", sep="")
} else {
outputname <- paste("Density_Plots_", name, ".png")
}
if(k == k.max) {
PlotDensities(x, position=c(k,k.max), outputName=outputname, name=name)
} else {
PlotDensities(x, position=c(k,k.max), outputName=outputname, name=name)
}
cat(".")
}
cat(" ok.\n")
}
##############################
## PlotDensities function ##
##############################
PlotDensities <- function (x, position, outputName=NULL, name=NULL) {
maxArrays <- position[2]-position[1]+1
if(is.null(outputName)) {stop(cat("PlotDensities() error. Please define the outputName variable in the function\n\n"))}
if(is.null(name)) {stop(cat("PlotDensities() error. Please define the name variable in the function\n\n"))}
labels.green <- NULL
labels.red <- NULL
labels.matrix <- NULL
for (i in position[1]:position[2]) {
arrayName <- colnames(x)[i]
labels.green <- c(labels.green, paste(arrayName, "Green Signal", sep=" "))
labels.red <- c(labels.red, paste(arrayName, "Red Signal", sep=" "))
labels.matrix <- c(labels.matrix, arrayName)
}
greens <- brewer.pal(9, "Greens")[-1]
reds <- brewer.pal(9, "Reds")[-1]
blues <- brewer.pal(9, "Blues")[-1]
extra.color <- brewer.pal(9, "RdBu")[-1]
colors.green <- NULL
colors.red <- NULL
colors.blue <- NULL
for (i in 1:(maxArrays)) {
if(i > 8 ) {
k <- 5
l <- i-8
colors.green <- c(colors.green, extra.color[k+l])
colors.red <- c(colors.red, extra.color[k-l])
colors.blue <- c(colors.blue, extra.color[k+l])
} else {
colors.green <- c(colors.green, greens[i])
colors.red <- c(colors.red, reds[i])
colors.blue <- c(colors.blue, blues[i])
}
}
## Preparing to Write to .png file
background <- NULL
background2 <- NULL
corr.foreground <- NULL
foreground <- NULL
if(is.matrix(x)){
#one channel data, but we don't know which channel, thus use blue for the plots
color <- colors.blue
label <- c(labels.matrix)
corr.foreground <- x
selection <- position[1]:position[2]
} else {
if(x$datatype == "both") {
color <- c(colors.green,colors.red)
label <- c(labels.green,labels.red)
if(class(x) == "RGList") {
if(!is.null(x$other$gBGMeanSignal) & !is.null(x$other$rBGMeanSignal))
background2 <- cbind(log(x$other$gBGMeanSignal,2),log(x$other$rBGMeanSignal,2))
foreground <- cbind(log(x$G,2),log(x$R,2))
background <- cbind(log(x$Gb,2),log(x$Rb,2))
} else {
transform <- RG.MA(x)
corr.foreground <- cbind(log(transform$G,2),log(transform$R,2))
}
selection <- c(position[1]:position[2],position[1]:position[2]+ncol(x))
} else {
if (x$datatype=="red") {
color <- colors.red
label <- labels.matrix
if(class(x) == "EListRaw") {
if(!is.null(x$other$rBGMeanSignal)) background2 <- log(x$other$rBGMeanSignal,2)
foreground <- log(x$E,2)
background <- log(x$Eb,2)
} else {
corr.foreground <- x$other$EST
}
selection <- position[1]:position[2]
}
if (x$datatype=="green") {
color <- colors.green
label <- labels.matrix
if(class(x) == "EListRaw") {
if(!is.null(x$other$gBGMeanSignal)) background2 <- log(x$other$gBGMeanSignal,2)
foreground <- log(x$E,2)
background <- log(x$Eb,2)
} else {
corr.foreground <- x$other$EST
}
selection <- position[1]:position[2]
}
}
}
png(file=outputName, width=1200, height=600+600*(is.null(corr.foreground)), pointsize=20)
if(class(x) == "RGList" || class(x) == "EListRaw") {
par(mfrow=c(2,2))
if(x$source == "agilent") {
# Limits for the Spatial Detrend Signal Distribution
if(!is.null(background2)) {
x.min.sd <- 0
x.max.sd <- ceiling(max(sapply(apply(background2, 2, density, na.rm=TRUE), function(z) max(z$x))))
y.min.sd <- 0
y.max.sd <- ceiling(max(sapply(apply(background2, 2, density, na.rm=TRUE), function(z) max(z$y))))
# Plotting R/G Background distribution and setting name of main.title to Spatial Detrend Distibution, because this is made when plotting Gb and Rb
plotDensity(as.matrix(background2[,selection]), col=color, xlim=c(x.min.sd,x.max.sd), lty=1:length(selection), lwd=2, ylim=c(y.min.sd,y.max.sd), main="Background Distribution", xlab=paste("Log2(Intensity) -",name), ylab="Density")
} else {
plot(0,type='n',xaxt='n',yaxt='n',xlab="",ylab="",bty='n')
}
main.title <- "Spatial Detrend Distribution"
} else {
main.title <- "Background Distribution"
}
# Limits for the Foreground Signal Distribution
if(!is.null(foreground)) {
x.min.fg <- 0
x.max.fg <- ceiling(max(sapply(apply(foreground, 2, density, na.rm=TRUE), function(z) max(z$x))))
y.min.fg <- 0
y.max.fg <- ceiling(max(sapply(apply(foreground, 2, density, na.rm=TRUE), function(z) max(z$y))))
plotDensity(as.matrix(foreground[,selection]), col=color, xlim=c(x.min.fg,x.max.fg), lty=1:length(selection), lwd=2, ylim=c(y.min.fg,y.max.fg), main="Foreground Signal Distribution", xlab=paste("Log2(Intensity) -",name), ylab="Density")
}
# Limits for the Background Signal Distribution
if(!is.null(background)) {
x.min.bg <- 0
x.max.bg <- ceiling(max(sapply(apply(background, 2, density, na.rm=TRUE), function(z) max(z$x))))
y.min.bg <- 0
y.max.bg <- ceiling(max(sapply(apply(background, 2, density, na.rm=TRUE), function(z) max(z$y))))
plotDensity(as.matrix(background[,selection]), col=color, xlim=c(x.min.bg,x.max.bg), lty=1:length(selection), lwd=2, ylim=c(y.min.bg,y.max.bg),main=main.title, xlab=paste("Log2(Intensity) -",name), ylab="Density")
}
} else {
layout( t(as.matrix(c(1,1,2)) ) )
if(!is.null(corr.foreground)) {
x.min.fg <- 0
x.max.fg <- ceiling(max(sapply(apply(corr.foreground, 2, density, na.rm=TRUE), function(z) max(z$x))))
y.min.fg <- 0
y.max.fg <- ceiling(max(sapply(apply(corr.foreground, 2, density, na.rm=TRUE), function(z) max(z$y))))
if(class(x) == "list") { lab.title <- paste(names(x), " Foreground Signal Distribution") } else { lab.title <- "BG Corrected Foreground Signal Distribution" }
plotDensity(as.matrix(corr.foreground[,selection]), col=color, lty=1:length(selection), lwd=2, xlim=c(x.min.fg,x.max.fg), ylim=c(y.min.fg,y.max.fg), main=lab.title, xlab=paste("Log2(Intensity) -",name), ylab="Density")
}
}
plot.new()
#if(is.null(corr.foreground)) plot.new()
if((class(x) == "RGList" || class(x) == "EListRaw") & is.null(background2)) plot.new()
legend("topright",label,col=color, lty=1:length(selection),text.col="black",cex=1-0.2*(!is.null(corr.foreground)), lwd=3)
dev.off()
}
################################
## naZeroWeights function ##
################################
# In order to fit a boxplot all values that were weighted zero should be removed.
# This is not done in a standard boxplot output window, so we need to remove
# the zero-weighted values.
naZeroWeights <- function(x, weights=NULL) {
if(class(x) != "matrix")
if(is.null(weights))
stop("Please provide a weights matrix")
if(dim(x)[2] != dim(weights)[2] | dim(x)[1] != dim(weights)[1] )
stop("Object dimension and weight matrix dimensions do not match!")
for(i in 1:ncol(x)) {
switch(class(x),
RGList = {
zeros <- which(weights[,i] == 0)
x$R[zeros,i] <- NA
x$G[zeros,i] <- NA
x$Rb[zeros,i] <- NA
x$Gb[zeros,i] <- NA
},
EListRaw = {
zeros <- which(weights[,i] == 0)
x$E[zeros,i] <- NA
x$Eb[zeros,i] <- NA
},
MAList = {
zeros <- which(weights[,i] == 0)
x$M[zeros,i] <- NA
x$A[zeros,i] <- NA
if(!is.null(x$other$EST)) x$other$EST[zeros,i] <- NA
},
matrix = {
if(is.null(weights)) stop("for matrix arguments - one-channel between normalizations - the weights parameter must be provided")
zeros <- which(weights[,i] == 0)
x[zeros,i] <- NA
}, stop("Given data object is not of correct class")
)
}
return(x)
}
######################################
## boxplotOverview function ##
######################################
boxplotOverview <- function(x, fileName=NULL, figTitles=NULL, groupcols=NULL, use.weights=FALSE, weights = NULL, max.characters=20, y.axis=NULL, y.lim=NULL) {
## This function assumes that x is a list coming from arrayQC consisting of four or five data matrices / MALists.
error <- NULL
if(use.weights) { tempTitle <- "[HIGH QUALITY SPOTS ONLY]" } else { tempTitle <- "[ALL]" }
if(is.null(fileName)) { fileName <- "BoxplotOverview" } ## Check fileName
## Check if y-axis is filled in properly (in combination with y.lim)
check <- 0
if(is.null(y.axis)) {
check <- 1
} else {
switch(y.axis, "M" = {
check <- 1
fileName <- paste(fileName, "_M", sep="")
if(is.null(y.lim)) { y.lim=c(-6, 6) }
}, "A" = {
check <- 1
fileName <- paste(fileName, "_AverageSignal", sep="")
})
}
if(check == 0) { error <- c(error, "- y.axis: parameter is not set to NULL, \"M\" or \"A\"") }
rm(check)
if(use.weights == TRUE) { fileName <- paste(fileName, "_weighted", sep="") }
if(is.null(y.lim)) { cat("[ WARNING ] y.axis minimum and maximum value (y.lim) were undefined. Using default values instead: c(0,20)\n"); y.lim <- c(0,20) }
if(!is.null(figTitles)) {
if( length(figTitles) != length(x) ) {
error <- c(error, "- figTitles: data object size does not correspond with figure title length")
} else {
figTitles <- paste( figTitles, rep(tempTitle, length(x)) )
figTitles <- gsub("."," + ", figTitles, fixed=TRUE)
}
} else {
## Creating Figure Titles if none are supplied
figTitles <- paste( names(x), rep(tempTitle, length(x)) )
figTitles <- gsub("."," + ", figTitles, fixed=TRUE)
}
if(is.list(x)) {
if(length(x) == 5) { ## Happens for two-color arrays (5 normalizations)
normalizations <- c("BGCORRECTED", "LOESS", "LOESS.QUANTILE", "LOESS.AQUANTILE") ## Normalization values
## Check if 4 normalizations are present:
if( sum( normalizations %in% names(x) ) != 4 ) {
error <- c(error, "- x: object is a list with 5 elements, but does not contain all recommended normalization results")
} else { ## if everything is correct, make the proper data object:
selected <- which( names(x) %in% normalizations == FALSE )
x[[selected]] <- NULL
}
} else { ## Single-channel arrays
if(length(x) != 4) { error <- c(error, "- x: object is a list with more or less than 4 data fields") }
temp <- as.vector(lapply(x, class))
if(sum(temp!="matrix") > 0) { error <- c(error, "- All elements of the list must be of the matrix class.") }
rm(temp)
}
} else {
error <- c(error, "- x: object is not a list")
}
## Check the maximum characters of the values assigned to the legend:
a <- nchar(colnames(x[[1]]))
b <- a[] > max.characters
error2 <- NULL
if(sum(b, na.rm=TRUE) > 0) {
error2 <- c(error2, paste("- The following sample descriptions are too long ( > ", max.characters, " characters) :\n", sep=""))
zzz <- colnames(x[[1]])[b]
for(k in 1:length(zzz)) {
temp <- paste(" -", zzz[k], " --> ")
zzz[k] <- substr( zzz[k], nchar(zzz[k])-(max.characters - 1), nchar(zzz[k]) )
temp <- paste(temp, zzz[k], "\n")
error2 <- c(error2, temp)
rm(temp)
}
colnames(x[[1]])[b] <- zzz
rm(zzz)
cat("\nThese names have been truncated to fit the 20 character length (last 20 letters)!")
}
cat(error2)
if(!is.null(error)) { stop(paste("[WARNING] The following error(s) occurred:\n", paste(error, collapse="\n"), "\n\nPlease adress the above issues!\n")) }
## Image legend will support up to 65 sample names per row. If more samples are present, the image needs to widen up.
legendTemp <- floor( dim(x[[1]])[2] / 65 )
cex.axis <- 1 - (legendTemp * 0.125)
png(file=paste(fileName, ".png", sep=""), width=1600+(850 * legendTemp), height=1200, pointsize=20)
# print(dim(x[[1]]$M))
if(legendTemp > 0) { image.layout <- rbind( c(1,1,2,2,5), c(3,3,4,4,5) ) } else { image.layout <- rbind( c(1,1,1,2,2,2,5), c(3,3,3,4,4,4,5) ) }
# image.layout <- rbind( c(1,1,2,2,5), c(3,3,4,4,5) )
layout(image.layout)
for(i in 1:length(x)) {
## Which value to plot
if( use.weights == 1 ) {
if(is.null(weights)) { weights <- x[[i]]$weights }
data <- naZeroWeights( x[[i]], weights=weights )
} else { data <- x[[i]] }
if( is.null(y.axis) ) { data <- data; ylab="value"; y.axis="custom" } else {
if( y.axis == "M" ) { data <- data$M; ylab="M-value"; data[data[]> max(y.lim)] <- max(y.lim); data[data[]< min(y.lim)] <- min(y.lim) }
if( y.axis == "A" ) { data <- data$A; ylab="A-value" }
}
boxplot(data, names=paste("[", 1:dim(data)[2], "]", sep=""), ylim=y.lim, main=figTitles[i], ylab=ylab, cex.axis=cex.axis, las=2)
if(y.axis == "M" ) { abline(h=0, lwd=1, lty=2, col="darkgrey") }
abline(h= mean(data, na.rm=TRUE), lwd=1, lty=3, col="darkred")
abline(h= median(data, na.rm=TRUE), lwd=1, lty=3, col="darkgreen")
}
par(mai=c(0,0,0,0))
plot.new()
#plot(0,type='n',xaxt='n',yaxt='n',xlab="",ylab="", bty='n')
legend("topright", c("Average", "Median", "", paste("[", 1:dim(data)[2], "] ", colnames(data), sep="")), lty=c(3, 3, rep(0, dim(data)[2])+1), col=c("darkred", "darkgreen", rep("lightgray", dim(data)[2] + 1)), ncol=legendTemp + 1, box.lwd = 0,box.col = "white",bg = "lightgray")
# legend("bottomright", "Mean value", ncol="darkred", lty=3)
dev.off()
}
lty=c(3, rep(NULL, dim(data)[2] + 1))
##############################
## CreateMAplots function ##
##############################
createMAplots <- function(x, lab=NULL, weight = NULL, postfix=NULL, image.width=1600, image.height=1200, pointsize=25, y.lim=NULL, x.lim=NULL, y.symm=FALSE, loess.curve=TRUE, loess.col="darkred", lwd=2, lty=1, ...) {
cat("* Preprocessing MA Plots ...")
error <- NULL
if( class(x) != "list" ) { error <- c(error, "- object x is not a list class!") }
## Check if each component of x is an MAlist
for(i in 1:length(x)) {
if(class(x[[i]]) != "MAList") { error <- c(error, paste("- Element ", names(x)[i], " is not of MAList class!", sep="")) }
}
## Only allow lists with 2 or 5 elements (single channel vs dual channel only).
if(! (length(x) == 2 | length(x) == 5) ) { error <- c(error, "- lists must consist of 2 or 5 elements!") }
## If 5 elements are supplied, extract only the more usefull normalizations:
if(length(x) == 5) {
normalizations <- c("BGCORRECTED", "LOESS", "LOESS.QUANTILE", "LOESS.AQUANTILE") ## Normalization values
## Check if 4 normalizations are present:
if( sum( normalizations %in% names(x) ) != 4 ) {
error <- c(error, "- x: object is a list with 5 elements, but does not contain all recommended normalization results")
} else { ## if everything is correct, make the proper data object:
selected <- which( names(x) %in% normalizations == FALSE )
x[[selected]] <- NULL
}
}
## Check if labels exist
if(is.null(lab)) { lab <- names(x) }
if(sum( lab %in% names(x) ) != length(lab) ) { error <- c(error, "- label names do not match names of object x!") }
subtext1 <- paste( lab, " - High quality spots (filtered)", sep="")
subtext2 <- paste( lab, " - All spots (not filtered)", sep="")
## Determine x and y limits (based on full experiment) and check whether objects have a weight field
tempM <- tempA <- NULL
for(i in 1:length(x)) {
tempM <- c( tempM, range(x[[i]]$M, na.rm=TRUE) )
tempA <- c( tempA, range(x[[i]]$A, na.rm=TRUE) )
}
if(is.null(x.lim)) { x.lim <- c( min( floor(tempA) ) - 1, max( ceiling(tempA) ) + 1 ) }
if(is.null(y.lim)) { y.lim <- c( min( floor(tempM) ) - 1, max( ceiling(tempM) ) + 1 ) }
if(y.symm) { y.lim <- c( max(abs(y.lim)), -max(abs(y.lim)) ) }
cat(" ok.\n* MA plotting progress:\n")
#### PLOTTING
#############
maxArrays <- ncol(x[[1]])
pbar <- txtProgressBar(min=0, max=maxArrays, char="*", width=20, style=3)
for(i in 1:maxArrays) {
### WEIGHTED PLOT
if(!is.null(weights)) {
titleName <- colnames(x[[1]])[i]
fileName <- paste("MA_Plots_-_", titleName, "_-_Zero_Weighted", postfix, ".png", sep="", collapse=NULL)
png(filename = fileName, width=image.width, height=image.height, pointsize=pointsize)
par( mar=c(8,4,4,3)+0.1, mfrow=c(1+ (length(x) == 2 | length(x) == 4), 1+ ( length(x) == 4)) )
for(j in 1:length(x)) {
m <- x[[j]]$M[,i]
a <- x[[j]]$A[,i]
w <- weight[,i]
k <- is.na(w) | (w <= 0)
m[k] <- NA
a[k] <- NA
#Determining color densities and plotting the MA-plot
color <- densCols(a, m)
plot(a, m, xlim=x.lim, ylim=y.lim, col=color, cex=0.3, pch=16, xlab="A", ylab="M")
title(main=titleName, sub=subtext1[j], outer=FALSE)
abline(0,0,col="darkgrey", lty=2)
if(loess.curve == 1) {
lines(loess.smooth(a,m), col=loess.col, lwd=lwd, lty=lty)
temp <- loess(m~a)
x1 <- seq( min(a, na.rm=TRUE), max(a, na.rm=TRUE), (max(a, na.rm=TRUE) - min(a, na.rm=TRUE)) / 1000)
lines(x1, predict(temp, newdata=x1), col="darkblue", lty=lty, lwd=lwd)
rm(temp, x1)
}
}
dev.off()
}