Diff of /modas/utils/CMplot.r [000000] .. [a43cea]

Switch to unified view

a b/modas/utils/CMplot.r
1
#Version:3.3.3
2
#Data: 2018/12/11
3
#Author: Lilin Yin
4
5
CMplot <- function(
6
    Pmap,
7
    col=c("#377EB8", "#4DAF4A", "#984EA3", "#FF7F00"),
8
    bin.size=1e6,
9
    bin.max=NULL,
10
    pch=19,
11
    band=1,
12
    cir.band=0.5,
13
    H=1.5,
14
    ylim=NULL,
15
    cex.axis=1,
16
    plot.type="b",
17
    multracks=FALSE,
18
    cex=c(0.5,1,1),
19
    r=0.3,
20
    xlab="Chromosome",
21
    ylab=expression(-log[10](italic(p))),
22
    xaxs="i",
23
    yaxs="r",
24
    outward=FALSE,
25
    threshold = NULL, 
26
    threshold.col="red",
27
    threshold.lwd=1,
28
    threshold.lty=2,
29
    amplify= TRUE,
30
    chr.labels=NULL,
31
    signal.cex = 1.5,
32
    signal.pch = 19,
33
    signal.col="red",
34
    signal.line=1,
35
    cir.chr=TRUE,
36
    cir.chr.h=1.5,
37
    chr.den.col=c("darkgreen", "yellow", "red"),
38
    cir.legend=TRUE,
39
    cir.legend.cex=0.6,
40
    cir.legend.col="black",
41
    LOG10=TRUE,
42
    box=FALSE,
43
    conf.int.col="grey",
44
    file.output=TRUE,
45
    file="jpg",
46
    dpi=300,
47
    memo="",
48
    verbose=TRUE
49
)
50
{   
51
52
    #plot a circle with a radius of r
53
    circle.plot <- function(myr,type="l",x=NULL,lty=1,lwd=1,col="black",add=TRUE,n.point=1000)
54
    {
55
        curve(sqrt(myr^2-x^2),xlim=c(-myr,myr),n=n.point,ylim=c(-myr,myr),type=type,lty=lty,col=col,lwd=lwd,add=add)
56
        curve(-sqrt(myr^2-x^2),xlim=c(-myr,myr),n=n.point,ylim=c(-myr,myr),type=type,lty=lty,col=col,lwd=lwd,add=TRUE)
57
    }
58
    
59
    Densitplot <- function(
60
        map,
61
        col=c("darkgreen", "yellow", "red"),
62
        main="SNP Density",
63
        bin=1e6,
64
        band=3,
65
        width=5,
66
        legend.len=10,
67
        legend.max=NULL,
68
        legend.pt.cex=3,
69
        legend.cex=1,
70
        legend.y.intersp=1,
71
        legend.x.intersp=1,
72
        plot=TRUE
73
    )
74
    {
75
        map <- as.matrix(map)
76
        map <- map[!is.na(map[, 2]), ]
77
        map <- map[!is.na(as.numeric(map[, 3])), ]
78
        map <- map[map[, 2] != 0, ]
79
        #map <- map[map[, 3] != 0, ]
80
        options(warn = -1)
81
        max.chr <- max(as.numeric(map[, 2]), na.rm=TRUE)
82
        if(is.infinite(max.chr))    max.chr <- 0
83
        map.xy.index <- which(!as.numeric(map[, 2]) %in% c(0 : max.chr))
84
        if(length(map.xy.index) != 0){
85
            chr.xy <- unique(map[map.xy.index, 2])
86
            for(i in 1:length(chr.xy)){
87
                map[map[, 2] == chr.xy[i], 2] <- max.chr + i
88
            }
89
        }
90
        map <- map[order(as.numeric(map[, 2]), as.numeric(map[, 3])), ]
91
        chr <- as.numeric(map[, 2])
92
        pos <- as.numeric(map[, 3])
93
        chr.num <- unique(chr)
94
        chorm.maxlen <- max(pos)
95
        if(plot)    plot(NULL, xlim=c(0, chorm.maxlen + chorm.maxlen/10), ylim=c(0, length(chr.num) * band + band), main=main,axes=FALSE, xlab="", ylab="", xaxs="i", yaxs="i")
96
        pos.x <- list()
97
        col.index <- list()
98
        maxbin.num <- NULL
99
        for(i in 1 : length(chr.num)){
100
            pos.x[[i]] <- pos[which(chr == chr.num[i])]
101
            cut.len <- ceiling((max(pos.x[[i]]) - min(pos.x[[i]])) / bin)
102
            if(cut.len <= 1){
103
                maxbin.num <- c(maxbin.num,length(pos.x[[i]]))
104
                col.index[[i]] <- rep(length(pos.x[[i]]), length(pos.x[[i]]))
105
            }else{
106
                cut.r <- cut(pos.x[[i]], cut.len, labels=FALSE)
107
                eachbin.num <- table(cut.r)
108
                maxbin.num <- c(maxbin.num, max(eachbin.num))
109
                col.index[[i]] <- rep(eachbin.num, eachbin.num)
110
            }
111
        }
112
        Maxbin.num <- max(maxbin.num)
113
        maxbin.num <- Maxbin.num
114
        if(!is.null(legend.max)){
115
            maxbin.num <- legend.max
116
        }
117
        col=colorRampPalette(col)(maxbin.num)
118
        col.seg=NULL
119
        for(i in 1 : length(chr.num)){
120
            if(plot)    polygon(c(0, 0, max(pos.x[[i]]), max(pos.x[[i]])), 
121
                c(-width/5 - band * (i - length(chr.num) - 1), width/5 - band * (i - length(chr.num) - 1), 
122
                width/5 - band * (i - length(chr.num) - 1), -width/5 - band * (i - length(chr.num) - 1)), col="grey", border="grey")
123
            if(!is.null(legend.max)){
124
                if(legend.max < Maxbin.num){
125
                    col.index[[i]][col.index[[i]] > legend.max] <- legend.max
126
                }
127
            }
128
            col.seg <- c(col.seg, col[round(col.index[[i]] * length(col) / maxbin.num)])
129
            if(plot)    segments(pos.x[[i]], -width/5 - band * (i - length(chr.num) - 1), pos.x[[i]], width/5 - band * (i - length(chr.num) - 1), 
130
            col=col[round(col.index[[i]] * length(col) / maxbin.num)], lwd=1)
131
        }
132
        if(length(map.xy.index) != 0){
133
            for(i in 1:length(chr.xy)){
134
                chr.num[chr.num == max.chr + i] <- chr.xy[i]
135
            }
136
        }
137
        chr.num <- rev(chr.num)
138
        if(plot){
139
            if(max.chr == 0)    mtext(at=seq(band, length(chr.num) * band, band),text=chr.num, side=2, las=2, font=1, cex=cex.axis*0.6, line=0.2)
140
            if(max.chr != 0)    mtext(at=seq(band, length(chr.num) * band, band),text=paste("Chr", chr.num, sep=""), side=2, las=2, font=1, cex=cex.axis*0.6, line=0.2)
141
        }
142
        if(plot)    axis(3, at=seq(0, chorm.maxlen, length=10), labels=paste(round((seq(0, chorm.maxlen, length=10)) / 1e6, 0), "Mb", sep=""),
143
            font=1, cex.axis=cex.axis*0.8, tck=0.01, lwd=2, padj=1.2)
144
        # image(c(chorm.maxlen-chorm.maxlen * legend.width / 20 , chorm.maxlen), 
145
        # round(seq(band - width/5, (length(chr.num) * band + band) * legend.height / 2 , length=maxbin.num+1), 2), 
146
        # t(matrix(0 : maxbin.num)), col=c("white", rev(heat.colors(maxbin.num))), add=TRUE)
147
                
148
        if(maxbin.num <= legend.len)    legend.len <- maxbin.num        
149
        
150
        legend.y <- round(seq(0, maxbin.num, length=legend.len))
151
        len <- legend.y[2]
152
        legend.y <- seq(0, maxbin.num, len)
153
        if(!is.null(legend.max)){
154
            if(legend.max < Maxbin.num){
155
                if(!maxbin.num %in% legend.y){
156
                    legend.y <- c(legend.y, paste(">=", maxbin.num, sep=""))
157
                    legend.y.col <- c(legend.y[c(-1, -length(legend.y))], maxbin.num)
158
                }else{
159
                    legend.y[length(legend.y)] <- paste(">=", maxbin.num, sep="")
160
                    legend.y.col <- c(legend.y[c(-1, -length(legend.y))], maxbin.num)
161
                }
162
            }else{
163
                if(!maxbin.num %in% legend.y){
164
                    legend.y <- c(legend.y, maxbin.num)
165
                }
166
                legend.y.col <- c(legend.y[-1])
167
            }
168
        }else{
169
            if(!maxbin.num %in% legend.y){
170
                legend.y <- c(legend.y, paste(">", max(legend.y), sep=""))
171
                legend.y.col <- c(legend.y[c(-1, -length(legend.y))], maxbin.num)
172
            }else{
173
                legend.y.col <- c(legend.y[-1])
174
            }
175
        }
176
        legend.y.col <- as.numeric(legend.y.col)
177
        legend.col <- c("grey", col[round(legend.y.col * length(col) / maxbin.num)])
178
        if(plot)    legend(x=(chorm.maxlen + chorm.maxlen/100), y=( -width/2.5 - band * (length(chr.num) - length(chr.num) - 1)), title="", legend=legend.y, pch=15, pt.cex = legend.pt.cex, col=legend.col,
179
            cex=legend.cex, bty="n", y.intersp=legend.y.intersp, x.intersp=legend.x.intersp, yjust=0, xjust=0, xpd=TRUE)
180
        if(!plot)   return(list(den.col=col.seg, legend.col=legend.col, legend.y=legend.y))
181
    }
182
183
    if(sum(plot.type %in% "b")==1) plot.type=c("c","m","q","d")
184
185
    taxa=colnames(Pmap)[-c(1:3)]
186
    if(!is.null(memo) && memo != "")    memo <- paste("_", memo, sep="")
187
    if(length(taxa) == 0)   taxa <- "Index"
188
    taxa <- paste(taxa, memo, sep="")
189
190
    #SNP-Density plot
191
    if("d" %in% plot.type){
192
        if(verbose) print("SNP_Density Plotting...")
193
        if(file.output){
194
            if(file=="jpg") jpeg(paste("SNP_Density.",paste(taxa,collapse="."),".jpg",sep=""), width = 9*dpi,height=7*dpi,res=dpi,quality = 100)
195
            if(file=="pdf") pdf(paste("SNP_Density.",paste(taxa,collapse="."),".pdf",sep=""), width = 9,height=7)
196
            if(file=="tiff")    tiff(paste("SNP_Density.",paste(taxa,collapse="."),".tiff",sep=""), width = 9*dpi,height=7*dpi,res=dpi)
197
            par(xpd=TRUE)
198
        }else{
199
            if(is.null(dev.list())) dev.new(width = 9,height=7)
200
            par(xpd=TRUE)
201
        }
202
203
        Densitplot(map=Pmap[,c(1:3)], col=col, bin=bin.size, legend.max=bin.max, main=paste("The number of SNPs within ", bin.size/1e6, "Mb window size", sep=""))
204
        if(file.output) dev.off()
205
    }
206
207
    if(length(plot.type) !=1 | (!"d" %in% plot.type)){
208
    
209
        #order Pmap by the name of SNP
210
        #Pmap=Pmap[order(Pmap[,1]),]
211
        Pmap <- as.matrix(Pmap)
212
213
        #delete the column of SNPs names
214
        Pmap <- Pmap[,-1]
215
        
216
        #scale and adjust the parameters
217
        cir.chr.h <- cir.chr.h/5
218
        cir.band <- cir.band/5
219
        if(!is.null(threshold)){
220
            threshold.col <- rep(threshold.col,length(threshold))
221
            threshold.lwd <- rep(threshold.lwd,length(threshold))
222
            threshold.lty <- rep(threshold.lty,length(threshold))
223
            signal.col <- rep(signal.col,length(threshold))
224
            signal.pch <- rep(signal.pch,length(threshold))
225
            signal.cex <- rep(signal.cex,length(threshold))
226
        }
227
        if(length(cex)!=3) cex <- rep(cex,3)
228
        if(!is.null(ylim)){
229
            if(length(ylim)==1) ylim <- c(0,ylim)
230
        }
231
        
232
        if(is.null(conf.int.col))   conf.int.col <- NA
233
        if(is.na(conf.int.col)){
234
            conf.int=FALSE
235
        }else{
236
            conf.int=TRUE
237
        }
238
239
        #get the number of traits
240
        R=ncol(Pmap)-2
241
242
        #replace the non-euchromosome
243
        options(warn = -1)
244
        numeric.chr <- as.numeric(Pmap[, 1])
245
        options(warn = 0)
246
        max.chr <- max(numeric.chr, na.rm=TRUE)
247
        if(is.infinite(max.chr))    max.chr <- 0
248
        map.xy.index <- which(!numeric.chr %in% c(0:max.chr))
249
        if(length(map.xy.index) != 0){
250
            chr.xy <- unique(Pmap[map.xy.index, 1])
251
            for(i in 1:length(chr.xy)){
252
                Pmap[Pmap[, 1] == chr.xy[i], 1] <- max.chr + i
253
            }
254
        }
255
256
        Pmap <- matrix(as.numeric(Pmap), nrow(Pmap))
257
        Pmap[is.na(Pmap)] <- 0
258
259
        #order the GWAS results by chromosome and position
260
        Pmap <- Pmap[order(Pmap[, 1], Pmap[,2]), ]
261
262
        #get the index of chromosome
263
        chr <- unique(Pmap[,1])
264
        chr.ori <- chr
265
        if(length(map.xy.index) != 0){
266
            for(i in 1:length(chr.xy)){
267
                chr.ori[chr.ori == max.chr + i] <- chr.xy[i]
268
            }
269
        }
270
271
        pvalueT <- as.matrix(Pmap[,-c(1:2)])
272
        pvalue.pos <- Pmap[, 2]
273
        p0.index <- Pmap[, 1] == 0
274
        if(sum(p0.index) != 0){
275
            pvalue.pos[p0.index] <- 1:sum(p0.index)
276
        }
277
        pvalue.pos.list <- tapply(pvalue.pos, Pmap[, 1], list)
278
        
279
        #scale the space parameter between chromosomes
280
        if(!missing(band)){
281
            band <- floor(band*(sum(sapply(pvalue.pos.list, max))/100))
282
        }else{
283
            band <- floor((sum(sapply(pvalue.pos.list, max))/100))
284
        }
285
        if(band==0) band=1
286
        
287
        if(LOG10){
288
            pvalueT[pvalueT <= 0] <- 1
289
            pvalueT[pvalueT > 1] <- 1
290
        }
291
292
        #set the colors for the plot
293
        #palette(heat.colors(1024)) #(heatmap)
294
        #T=floor(1024/max(pvalue))
295
        #plot(pvalue,pch=19,cex=0.6,col=(1024-floor(pvalue*T)))
296
        if(is.vector(col)){
297
            col <- matrix(col,R,length(col),byrow=TRUE)
298
        }
299
        if(is.matrix(col)){
300
            #try to transform the colors into matrix for all traits
301
            col <- matrix(as.vector(t(col)),R,dim(col)[2],byrow=TRUE)
302
        }
303
304
        Num <- as.numeric(table(Pmap[,1]))
305
        Nchr <- length(Num)
306
        N <- NULL
307
308
        #set the colors for each traits
309
        for(i in 1:R){
310
            colx <- col[i,]
311
            colx <- colx[!is.na(colx)]
312
            N[i] <- ceiling(Nchr/length(colx))
313
        }
314
        
315
        #insert the space into chromosomes and return the midpoint of each chromosome
316
        ticks <- NULL
317
        pvalue.posN <- NULL
318
        #pvalue <- pvalueT[,j]
319
        for(i in 0:(Nchr-1)){
320
            if (i==0){
321
                #pvalue <- append(pvalue,rep(Inf,band),after=0)
322
                pvalue.posN <- pvalue.pos.list[[i+1]] + band
323
                ticks[i+1] <- max(pvalue.posN)-floor(max(pvalue.pos.list[[i+1]])/2)
324
            }else{
325
                #pvalue <- append(pvalue,rep(Inf,band),after=sum(Num[1:i])+i*band)
326
                pvalue.posN <- c(pvalue.posN, max(pvalue.posN) + band + pvalue.pos.list[[i+1]])
327
                ticks[i+1] <- max(pvalue.posN)-floor(max(pvalue.pos.list[[i+1]])/2)
328
            }
329
        }
330
        pvalue.posN.list <- tapply(pvalue.posN, Pmap[, 1], list)
331
        #NewP[[j]] <- pvalue
332
        
333
        #merge the pvalues of traits by column
334
        if(LOG10){
335
            logpvalueT <- -log10(pvalueT)
336
        }else{
337
            logpvalueT <- pvalueT
338
        }
339
340
        add <- list()
341
        for(i in 1:R){
342
            colx <- col[i,]
343
            colx <- colx[!is.na(colx)]
344
            add[[i]] <- c(Num,rep(0,N[i]*length(colx)-Nchr))
345
        }
346
347
        TotalN <- max(pvalue.posN)
348
349
        if(length(chr.den.col) > 1){
350
            cir.density=TRUE
351
            den.fold <- 20
352
            density.list <- Densitplot(map=Pmap[,c(1,1,2)], col=chr.den.col, plot=FALSE, bin=bin.size, legend.max=bin.max)
353
            #list(den.col=col.seg, legend.col=legend.col, legend.y=legend.y)
354
        }else{
355
            cir.density=FALSE
356
        }
357
        
358
        signal.line.index <- NULL
359
        if(!is.null(threshold)){
360
            if(!is.null(signal.line)){
361
                for(l in 1:R){
362
                    if(LOG10){
363
                        signal.line.index <- c(signal.line.index,which(pvalueT[,l] < min(threshold)))
364
                    }else{
365
                        signal.line.index <- c(signal.line.index,which(pvalueT[,l] > max(threshold)))
366
                    }
367
                }
368
                signal.line.index <- unique(signal.line.index)
369
            }
370
        }
371
        signal.line.index <- pvalue.posN[signal.line.index]
372
    }
373
    
374
    #plot circle Manhattan
375
    if("c" %in% plot.type){
376
        #print("Starting Circular-Manhattan plot!",quote=F)
377
        if(file.output){
378
            if(file=="jpg") jpeg(paste("Circular-Manhattan.",paste(taxa,collapse="."),".jpg",sep=""), width = 8*dpi,height=8*dpi,res=dpi,quality = 100)
379
            if(file=="pdf") pdf(paste("Circular-Manhattan.",paste(taxa,collapse="."),".pdf",sep=""), width = 10,height=10)
380
            if(file=="tiff")    tiff(paste("Circular-Manhattan.",paste(taxa,collapse="."),".tiff",sep=""), width = 8*dpi,height=8*dpi,res=dpi)
381
            par(pty="s", xpd=TRUE, mar=c(1,1,1,1))
382
        }
383
        if(!file.output){
384
            if(is.null(dev.list())) dev.new(width=8, height=8)
385
            par(pty="s", xpd=TRUE)
386
        }
387
        RR <- r+H*R+cir.band*R
388
        if(cir.density){
389
            plot(NULL,xlim=c(1.05*(-RR-4*cir.chr.h),1.1*(RR+4*cir.chr.h)),ylim=c(1.05*(-RR-4*cir.chr.h),1.1*(RR+4*cir.chr.h)),axes=FALSE,xlab="",ylab="")
390
        }else{
391
            plot(NULL,xlim=c(1.05*(-RR-4*cir.chr.h),1.05*(RR+4*cir.chr.h)),ylim=c(1.05*(-RR-4*cir.chr.h),1.05*(RR+4*cir.chr.h)),axes=FALSE,xlab="",ylab="")
392
        }
393
        if(!is.null(signal.line)){
394
            if(!is.null(signal.line.index)){
395
                X1chr <- (RR)*sin(2*pi*(signal.line.index-round(band/2))/TotalN)
396
                Y1chr <- (RR)*cos(2*pi*(signal.line.index-round(band/2))/TotalN)
397
                X2chr <- (r)*sin(2*pi*(signal.line.index-round(band/2))/TotalN)
398
                Y2chr <- (r)*cos(2*pi*(signal.line.index-round(band/2))/TotalN)
399
                segments(X1chr,Y1chr,X2chr,Y2chr,lty=2,lwd=signal.line,col="grey")
400
            }
401
        }
402
        for(i in 1:R){
403
        
404
            #get the colors for each trait
405
            colx <- col[i,]
406
            colx <- colx[!is.na(colx)]
407
            
408
            #debug
409
            #print(colx)
410
            
411
            if(verbose) print(paste("Circular_Manhattan Plotting ",taxa[i],"...",sep=""))
412
            pvalue <- pvalueT[,i]
413
            logpvalue <- logpvalueT[,i]
414
            if(is.null(ylim)){
415
                if(LOG10){
416
                    Max <- ceiling(-log10(min(pvalue[pvalue!=0])))
417
                    Min <- 0
418
                }else{
419
                    Max <- ceiling(max(pvalue[pvalue!=Inf]))
420
                    if(abs(Max)<=1) Max <- max(pvalue[pvalue!=Inf])
421
                    Min <- floor(min(pvalue[pvalue!=Inf]))
422
                    if(abs(Min)<=1) Min <- min(pvalue[pvalue!=Inf])
423
                }
424
            }else{
425
                Max <- ylim[2]
426
                Min <- ylim[1]
427
            }
428
            Cpvalue <- (H*(logpvalue-Min))/(Max-Min)
429
            if(outward==TRUE){
430
                if(cir.chr==TRUE){
431
                    
432
                    #plot the boundary which represents the chromosomes
433
                    polygon.num <- 1000
434
                    for(k in 1:length(chr)){
435
                        if(k==1){
436
                            polygon.index <- seq(round(band/2)+1,-round(band/2)+max(pvalue.posN.list[[1]]), length=polygon.num)
437
                            #change the axis from right angle into circle format
438
                            X1chr=(RR)*sin(2*pi*(polygon.index)/TotalN)
439
                            Y1chr=(RR)*cos(2*pi*(polygon.index)/TotalN)
440
                            X2chr=(RR+cir.chr.h)*sin(2*pi*(polygon.index)/TotalN)
441
                            Y2chr=(RR+cir.chr.h)*cos(2*pi*(polygon.index)/TotalN)
442
                            if(is.null(chr.den.col)){
443
                                polygon(c(rev(X1chr),X2chr),c(rev(Y1chr),Y2chr),col=rep(colx,ceiling(length(chr)/length(colx)))[k],border=rep(colx,ceiling(length(chr)/length(colx)))[k])   
444
                            }else{
445
                                if(cir.density){
446
                                        polygon(c(rev(X1chr),X2chr),c(rev(Y1chr),Y2chr),col="grey",border="grey")
447
                                }else{
448
                                        polygon(c(rev(X1chr),X2chr),c(rev(Y1chr),Y2chr),col=chr.den.col,border=chr.den.col)
449
                                }
450
                            }
451
                        }else{
452
                            polygon.index <- seq(1+round(band/2)+max(pvalue.posN.list[[k-1]]),-round(band/2)+max(pvalue.posN.list[[k]]), length=polygon.num)
453
                            X1chr=(RR)*sin(2*pi*(polygon.index)/TotalN)
454
                            Y1chr=(RR)*cos(2*pi*(polygon.index)/TotalN)
455
                            X2chr=(RR+cir.chr.h)*sin(2*pi*(polygon.index)/TotalN)
456
                            Y2chr=(RR+cir.chr.h)*cos(2*pi*(polygon.index)/TotalN)
457
                            if(is.null(chr.den.col)){
458
                                polygon(c(rev(X1chr),X2chr),c(rev(Y1chr),Y2chr),col=rep(colx,ceiling(length(chr)/length(colx)))[k],border=rep(colx,ceiling(length(chr)/length(colx)))[k])
459
                            }else{
460
                                if(cir.density){
461
                                        polygon(c(rev(X1chr),X2chr),c(rev(Y1chr),Y2chr),col="grey",border="grey")
462
                                }else{
463
                                        polygon(c(rev(X1chr),X2chr),c(rev(Y1chr),Y2chr),col=chr.den.col,border=chr.den.col)
464
                                }
465
                            }       
466
                        }
467
                    }
468
                    
469
                    if(cir.density){
470
471
                        segments(
472
                            (RR)*sin(2*pi*(pvalue.posN-round(band/2))/TotalN),
473
                            (RR)*cos(2*pi*(pvalue.posN-round(band/2))/TotalN),
474
                            (RR+cir.chr.h)*sin(2*pi*(pvalue.posN-round(band/2))/TotalN),
475
                            (RR+cir.chr.h)*cos(2*pi*(pvalue.posN-round(band/2))/TotalN),
476
                            col=density.list$den.col, lwd=0.1
477
                        )
478
                        legend(
479
                            x=RR+4*cir.chr.h,
480
                            y=(RR+4*cir.chr.h)/2,
481
                            title="", legend=density.list$legend.y, pch=15, pt.cex = 3, col=density.list$legend.col,
482
                            cex=1, bty="n",
483
                            y.intersp=1,
484
                            x.intersp=1,
485
                            yjust=0.5, xjust=0, xpd=TRUE
486
                        )
487
                        
488
                    }
489
                    
490
                    # XLine=(RR+cir.chr.h)*sin(2*pi*(1:TotalN)/TotalN)
491
                    # YLine=(RR+cir.chr.h)*cos(2*pi*(1:TotalN)/TotalN)
492
                    # lines(XLine,YLine,lwd=1.5)
493
                    if(cir.density){
494
                        circle.plot(myr=RR+cir.chr.h,lwd=1.5,add=TRUE,col='grey')
495
                        circle.plot(myr=RR,lwd=1.5,add=TRUE,col='grey')
496
                    }else{
497
                        circle.plot(myr=RR+cir.chr.h,lwd=1.5,add=TRUE)
498
                        circle.plot(myr=RR,lwd=1.5,add=TRUE)
499
                    }
500
501
                }
502
                
503
                X=(Cpvalue+r+H*(i-1)+cir.band*(i-1))*sin(2*pi*(pvalue.posN-round(band/2))/TotalN)
504
                Y=(Cpvalue+r+H*(i-1)+cir.band*(i-1))*cos(2*pi*(pvalue.posN-round(band/2))/TotalN)
505
                points(X,Y,pch=19,cex=cex[1],col=rep(rep(colx,N[i]),add[[i]]))
506
                
507
                #plot the legend for each trait
508
                if(cir.legend==TRUE){
509
                    #try to get the number after radix point
510
                    if((Max-Min) > 1) {
511
                        round.n=2
512
                    }else{
513
                        round.n=nchar(as.character(10^(-ceiling(-log10(Max)))))-1
514
                    }
515
                    segments(0,r+H*(i-1)+cir.band*(i-1),0,r+H*i+cir.band*(i-1),col=cir.legend.col,lwd=1.5)
516
                    segments(0,r+H*(i-1)+cir.band*(i-1),H/20,r+H*(i-1)+cir.band*(i-1),col=cir.legend.col,lwd=1.5)
517
                    circle.plot(myr=r+H*(i-1)+cir.band*(i-1),lwd=0.5,add=TRUE,col='grey')
518
                    segments(0,r+H*(i-0.75)+cir.band*(i-1),H/20,r+H*(i-0.75)+cir.band*(i-1),col=cir.legend.col,lwd=1.5)
519
                    circle.plot(myr=r+H*(i-0.75)+cir.band*(i-1),lwd=0.5,add=TRUE,col='grey')
520
                    segments(0,r+H*(i-0.5)+cir.band*(i-1),H/20,r+H*(i-0.5)+cir.band*(i-1),col=cir.legend.col,lwd=1.5)
521
                    circle.plot(myr=r+H*(i-0.5)+cir.band*(i-1),lwd=0.5,add=TRUE,col='grey')
522
                    segments(0,r+H*(i-0.25)+cir.band*(i-1),H/20,r+H*(i-0.25)+cir.band*(i-1),col=cir.legend.col,lwd=1.5)
523
                    circle.plot(myr=r+H*(i-0.25)+cir.band*(i-1),lwd=0.5,add=TRUE,col='grey')
524
                    segments(0,r+H*(i-0)+cir.band*(i-1),H/20,r+H*(i-0)+cir.band*(i-1),col=cir.legend.col,lwd=1.5)
525
                    circle.plot(myr=r+H*(i-0)+cir.band*(i-1),lwd=0.5,add=TRUE,col='grey')
526
                    text(-r/15,r+H*(i-0.94)+cir.band*(i-1),round(Min+(Max-Min)*0,round.n),adj=1,col=cir.legend.col,cex=cir.legend.cex,font=2)
527
                    text(-r/15,r+H*(i-0.75)+cir.band*(i-1),round(Min+(Max-Min)*0.25,round.n),adj=1,col=cir.legend.col,cex=cir.legend.cex,font=2)
528
                    text(-r/15,r+H*(i-0.5)+cir.band*(i-1),round(Min+(Max-Min)*0.5,round.n),adj=1,col=cir.legend.col,cex=cir.legend.cex,font=2)
529
                    text(-r/15,r+H*(i-0.25)+cir.band*(i-1),round(Min+(Max-Min)*0.75,round.n),adj=1,col=cir.legend.col,cex=cir.legend.cex,font=2)
530
                    text(-r/15,r+H*(i-0.06)+cir.band*(i-1),round(Min+(Max-Min)*1,round.n),adj=1,col=cir.legend.col,cex=cir.legend.cex,font=2)
531
                }
532
                
533
                if(!is.null(threshold)){
534
                    if(sum(threshold!=0)==length(threshold)){
535
                        for(thr in 1:length(threshold)){
536
                            significantline1=ifelse(LOG10, H*(-log10(threshold[thr])-Min)/(Max-Min), H*(threshold[thr]-Min)/(Max-Min))
537
                            #s1X=(significantline1+r+H*(i-1)+cir.band*(i-1))*sin(2*pi*(0:TotalN)/TotalN)
538
                            #s1Y=(significantline1+r+H*(i-1)+cir.band*(i-1))*cos(2*pi*(0:TotalN)/TotalN)
539
                            if(significantline1<H){
540
                                #lines(s1X,s1Y,type="l",col=threshold.col,lwd=threshold.col,lty=threshold.lty)
541
                                circle.plot(myr=(significantline1+r+H*(i-1)+cir.band*(i-1)),col=threshold.col[thr],lwd=threshold.lwd[thr],lty=threshold.lty[thr])
542
                            }else{
543
                                warning(paste("No significant points for ",taxa[i]," pass the threshold level using threshold=",threshold[thr],"!",sep=""))
544
                            }
545
                        }
546
                    }
547
                }
548
                
549
                if(!is.null(threshold)){
550
                    if(sum(threshold!=0)==length(threshold)){
551
                        if(amplify==TRUE){
552
                            if(LOG10){
553
                                threshold <- sort(threshold)
554
                                significantline1=H*(-log10(max(threshold))-Min)/(Max-Min)
555
                            }else{
556
                                threshold <- sort(threshold, decreasing=TRUE)
557
                                significantline1=H*(min(threshold)-Min)/(Max-Min)
558
                            }
559
                            
560
                            p_amp.index <- which(Cpvalue>=significantline1)
561
                            HX1=(Cpvalue[p_amp.index]+r+H*(i-1)+cir.band*(i-1))*sin(2*pi*(pvalue.posN[p_amp.index]-round(band/2))/TotalN)
562
                            HY1=(Cpvalue[p_amp.index]+r+H*(i-1)+cir.band*(i-1))*cos(2*pi*(pvalue.posN[p_amp.index]-round(band/2))/TotalN)
563
                            
564
                            #cover the points that exceed the threshold with the color "white"
565
                            points(HX1,HY1,pch=19,cex=cex[1],col="white")
566
                            
567
                                for(ll in 1:length(threshold)){
568
                                    if(ll == 1){
569
                                        if(LOG10){
570
                                            significantline1=H*(-log10(threshold[ll])-Min)/(Max-Min)
571
                                        }else{
572
                                            significantline1=H*(threshold[ll]-Min)/(Max-Min)
573
                                        }
574
                                        p_amp.index <- which(Cpvalue>=significantline1)
575
                                        HX1=(Cpvalue[p_amp.index]+r+H*(i-1)+cir.band*(i-1))*sin(2*pi*(pvalue.posN[p_amp.index]-round(band/2))/TotalN)
576
                                        HY1=(Cpvalue[p_amp.index]+r+H*(i-1)+cir.band*(i-1))*cos(2*pi*(pvalue.posN[p_amp.index]-round(band/2))/TotalN)
577
                                    }else{
578
                                        if(LOG10){
579
                                            significantline0=H*(-log10(threshold[ll-1])-Min)/(Max-Min)
580
                                            significantline1=H*(-log10(threshold[ll])-Min)/(Max-Min)
581
                                        }else{
582
                                            significantline0=H*(threshold[ll-1]-Min)/(Max-Min)
583
                                            significantline1=H*(threshold[ll]-Min)/(Max-Min)
584
                                        }
585
                                        p_amp.index <- which(Cpvalue>=significantline1 & Cpvalue < significantline0)
586
                                        HX1=(Cpvalue[p_amp.index]+r+H*(i-1)+cir.band*(i-1))*sin(2*pi*(pvalue.posN[p_amp.index]-round(band/2))/TotalN)
587
                                        HY1=(Cpvalue[p_amp.index]+r+H*(i-1)+cir.band*(i-1))*cos(2*pi*(pvalue.posN[p_amp.index]-round(band/2))/TotalN)
588
                                    }
589
                                
590
                                    if(is.null(signal.col)){
591
                                        points(HX1,HY1,pch=signal.pch[ll],cex=signal.cex[ll]*cex[1],col=rep(rep(colx,N[i]),add[[i]])[p_amp.index])
592
                                    }else{
593
                                        points(HX1,HY1,pch=signal.pch[ll],cex=signal.cex[ll]*cex[1],col=signal.col[ll])
594
                                    }
595
                                }
596
                        }
597
                    }
598
                }
599
                if(cir.chr==TRUE){
600
                    ticks1=1.07*(RR+cir.chr.h)*sin(2*pi*(ticks-round(band/2))/TotalN)
601
                    ticks2=1.07*(RR+cir.chr.h)*cos(2*pi*(ticks-round(band/2))/TotalN)
602
                    if(is.null(chr.labels)){
603
                        for(i in 1:length(ticks)){
604
                            angle=360*(1-(ticks-round(band/2))[i]/TotalN)
605
                            text(ticks1[i],ticks2[i],chr.ori[i],srt=angle,font=2,cex=cex.axis)
606
                        }
607
                    }else{
608
                        for(i in 1:length(ticks)){
609
                            angle=360*(1-(ticks-round(band/2))[i]/TotalN)
610
                            text(ticks1[i],ticks2[i],chr.labels[i],srt=angle,font=2,cex=cex.axis)
611
                        }
612
                    }
613
                }else{
614
                    ticks1=(0.9*r)*sin(2*pi*(ticks-round(band/2))/TotalN)
615
                    ticks2=(0.9*r)*cos(2*pi*(ticks-round(band/2))/TotalN)
616
                    if(is.null(chr.labels)){
617
                        for(i in 1:length(ticks)){
618
                        angle=360*(1-(ticks-round(band/2))[i]/TotalN)
619
                        text(ticks1[i],ticks2[i],chr.ori[i],srt=angle,font=2,cex=cex.axis)
620
                        }
621
                    }else{
622
                        for(i in 1:length(ticks)){
623
                            angle=360*(1-(ticks-round(band/2))[i]/TotalN)
624
                            text(ticks1[i],ticks2[i],chr.labels[i],srt=angle,font=2,cex=cex.axis)
625
                        }
626
                    }
627
                }
628
            }
629
            if(outward==FALSE){
630
                if(cir.chr==TRUE){
631
                    # XLine=(2*cir.band+RR+cir.chr.h)*sin(2*pi*(1:TotalN)/TotalN)
632
                    # YLine=(2*cir.band+RR+cir.chr.h)*cos(2*pi*(1:TotalN)/TotalN)
633
                    # lines(XLine,YLine,lwd=1.5)
634
635
                    polygon.num <- 1000
636
                    for(k in 1:length(chr)){
637
                        if(k==1){
638
                            polygon.index <- seq(round(band/2)+1,-round(band/2)+max(pvalue.posN.list[[1]]), length=polygon.num)
639
                            X1chr=(2*cir.band+RR)*sin(2*pi*(polygon.index)/TotalN)
640
                            Y1chr=(2*cir.band+RR)*cos(2*pi*(polygon.index)/TotalN)
641
                            X2chr=(2*cir.band+RR+cir.chr.h)*sin(2*pi*(polygon.index)/TotalN)
642
                            Y2chr=(2*cir.band+RR+cir.chr.h)*cos(2*pi*(polygon.index)/TotalN)
643
                                if(is.null(chr.den.col)){
644
                                    polygon(c(rev(X1chr),X2chr),c(rev(Y1chr),Y2chr),col=rep(colx,ceiling(length(chr)/length(colx)))[k],border=rep(colx,ceiling(length(chr)/length(colx)))[k])   
645
                                }else{
646
                                    if(cir.density){
647
                                        polygon(c(rev(X1chr),X2chr),c(rev(Y1chr),Y2chr),col="grey",border="grey")
648
                                    }else{
649
                                        polygon(c(rev(X1chr),X2chr),c(rev(Y1chr),Y2chr),col=chr.den.col,border=chr.den.col)
650
                                    }
651
                                }
652
                        }else{
653
                            polygon.index <- seq(1+round(band/2)+max(pvalue.posN.list[[k-1]]),-round(band/2)+max(pvalue.posN.list[[k]]), length=polygon.num)
654
                            X1chr=(2*cir.band+RR)*sin(2*pi*(polygon.index)/TotalN)
655
                            Y1chr=(2*cir.band+RR)*cos(2*pi*(polygon.index)/TotalN)
656
                            X2chr=(2*cir.band+RR+cir.chr.h)*sin(2*pi*(polygon.index)/TotalN)
657
                            Y2chr=(2*cir.band+RR+cir.chr.h)*cos(2*pi*(polygon.index)/TotalN)
658
                            if(is.null(chr.den.col)){
659
                                polygon(c(rev(X1chr),X2chr),c(rev(Y1chr),Y2chr),col=rep(colx,ceiling(length(chr)/length(colx)))[k],border=rep(colx,ceiling(length(chr)/length(colx)))[k])   
660
                            }else{
661
                                    if(cir.density){
662
                                        polygon(c(rev(X1chr),X2chr),c(rev(Y1chr),Y2chr),col="grey",border="grey")
663
                                    }else{
664
                                        polygon(c(rev(X1chr),X2chr),c(rev(Y1chr),Y2chr),col=chr.den.col,border=chr.den.col)
665
                                    }
666
                            }   
667
                        }
668
                    }
669
                    if(cir.density){
670
671
                        segments(
672
                            (2*cir.band+RR)*sin(2*pi*(pvalue.posN-round(band/2))/TotalN),
673
                            (2*cir.band+RR)*cos(2*pi*(pvalue.posN-round(band/2))/TotalN),
674
                            (2*cir.band+RR+cir.chr.h)*sin(2*pi*(pvalue.posN-round(band/2))/TotalN),
675
                            (2*cir.band+RR+cir.chr.h)*cos(2*pi*(pvalue.posN-round(band/2))/TotalN),
676
                            col=density.list$den.col, lwd=0.1
677
                        )
678
                        legend(
679
                            x=RR+4*cir.chr.h,
680
                            y=(RR+4*cir.chr.h)/2,
681
                            title="", legend=density.list$legend.y, pch=15, pt.cex = 3, col=density.list$legend.col,
682
                            cex=1, bty="n",
683
                            y.intersp=1,
684
                            x.intersp=1,
685
                            yjust=0.5, xjust=0, xpd=TRUE
686
                        )
687
                        
688
                    }
689
                    
690
                    if(cir.density){
691
                        circle.plot(myr=2*cir.band+RR+cir.chr.h,lwd=1.5,add=TRUE,col='grey')
692
                        circle.plot(myr=2*cir.band+RR,lwd=1.5,add=TRUE,col='grey')
693
                    }else{
694
                        circle.plot(myr=2*cir.band+RR+cir.chr.h,lwd=1.5,add=TRUE)
695
                        circle.plot(myr=2*cir.band+RR,lwd=1.5,add=TRUE)
696
                    }
697
698
                }
699
                
700
                X=(-Cpvalue+r+H*i+cir.band*(i-1))*sin(2*pi*(pvalue.posN-round(band/2))/TotalN)
701
                Y=(-Cpvalue+r+H*i+cir.band*(i-1))*cos(2*pi*(pvalue.posN-round(band/2))/TotalN)
702
                points(X,Y,pch=19,cex=cex[1],col=rep(rep(colx,N[i]),add[[i]]))
703
                
704
                                if(cir.legend==TRUE){
705
                    
706
                    #try to get the number after radix point
707
                    if((Max-Min)<=1) {
708
                        round.n=nchar(as.character(10^(-ceiling(-log10(Max)))))-1
709
                    }else{
710
                        round.n=2
711
                    }
712
                    segments(0,r+H*(i-1)+cir.band*(i-1),0,r+H*i+cir.band*(i-1),col=cir.legend.col,lwd=1.5)
713
                    segments(0,r+H*(i-1)+cir.band*(i-1),H/20,r+H*(i-1)+cir.band*(i-1),col=cir.legend.col,lwd=1.5)
714
                    circle.plot(myr=r+H*(i-1)+cir.band*(i-1),lwd=0.5,add=TRUE,col='grey')
715
                    segments(0,r+H*(i-0.75)+cir.band*(i-1),H/20,r+H*(i-0.75)+cir.band*(i-1),col=cir.legend.col,lwd=1.5)
716
                    circle.plot(myr=r+H*(i-0.75)+cir.band*(i-1),lwd=0.5,add=TRUE,col='grey')
717
                    segments(0,r+H*(i-0.5)+cir.band*(i-1),H/20,r+H*(i-0.5)+cir.band*(i-1),col=cir.legend.col,lwd=1.5)
718
                    circle.plot(myr=r+H*(i-0.5)+cir.band*(i-1),lwd=0.5,add=TRUE,col='grey')
719
                    segments(0,r+H*(i-0.25)+cir.band*(i-1),H/20,r+H*(i-0.25)+cir.band*(i-1),col=cir.legend.col,lwd=1.5)
720
                    circle.plot(myr=r+H*(i-0.25)+cir.band*(i-1),lwd=0.5,add=TRUE,col='grey')
721
                    segments(0,r+H*(i-0)+cir.band*(i-1),H/20,r+H*(i-0)+cir.band*(i-1),col=cir.legend.col,lwd=1.5)
722
                    circle.plot(myr=r+H*(i-0)+cir.band*(i-1),lwd=0.5,add=TRUE,col='grey')
723
                    text(-r/15,r+H*(i-0.06)+cir.band*(i-1),round(Min+(Max-Min)*0,round.n),adj=1,col=cir.legend.col,cex=cir.legend.cex,font=2)
724
                    text(-r/15,r+H*(i-0.25)+cir.band*(i-1),round(Min+(Max-Min)*0.25,round.n),adj=1,col=cir.legend.col,cex=cir.legend.cex,font=2)
725
                    text(-r/15,r+H*(i-0.5)+cir.band*(i-1),round(Min+(Max-Min)*0.5,round.n),adj=1,col=cir.legend.col,cex=cir.legend.cex,font=2)
726
                    text(-r/15,r+H*(i-0.75)+cir.band*(i-1),round(Min+(Max-Min)*0.75,round.n),adj=1,col=cir.legend.col,cex=cir.legend.cex,font=2)
727
                    text(-r/15,r+H*(i-0.94)+cir.band*(i-1),round(Min+(Max-Min)*1,round.n),adj=1,col=cir.legend.col,cex=cir.legend.cex,font=2)
728
                }
729
                
730
                if(!is.null(threshold)){
731
                    if(sum(threshold!=0)==length(threshold)){
732
                    
733
                        for(thr in 1:length(threshold)){
734
                            significantline1=ifelse(LOG10, H*(-log10(threshold[thr])-Min)/(Max-Min), H*(threshold[thr]-Min)/(Max-Min))
735
                            #s1X=(significantline1+r+H*(i-1)+cir.band*(i-1))*sin(2*pi*(0:TotalN)/TotalN)
736
                            #s1Y=(significantline1+r+H*(i-1)+cir.band*(i-1))*cos(2*pi*(0:TotalN)/TotalN)
737
                            if(significantline1<H){
738
                                #lines(s1X,s1Y,type="l",col=threshold.col,lwd=threshold.col,lty=threshold.lty)
739
                                circle.plot(myr=(-significantline1+r+H*i+cir.band*(i-1)),col=threshold.col[thr],lwd=threshold.lwd[thr],lty=threshold.lty[thr])
740
                            }else{
741
                                warning(paste("No significant points for ",taxa[i]," pass the threshold level using threshold=",threshold[thr],"!",sep=""))
742
                            }
743
                        }
744
                        if(amplify==TRUE){
745
                            if(LOG10){
746
                                threshold <- sort(threshold)
747
                                significantline1=H*(-log10(max(threshold))-Min)/(Max-Min)
748
                            }else{
749
                                threshold <- sort(threshold, decreasing=TRUE)
750
                                significantline1=H*(min(threshold)-Min)/(Max-Min)
751
                            }
752
                            p_amp.index <- which(Cpvalue>=significantline1)
753
                            HX1=(-Cpvalue[p_amp.index]+r+H*i+cir.band*(i-1))*sin(2*pi*(pvalue.posN[p_amp.index]-round(band/2))/TotalN)
754
                            HY1=(-Cpvalue[p_amp.index]+r+H*i+cir.band*(i-1))*cos(2*pi*(pvalue.posN[p_amp.index]-round(band/2))/TotalN)
755
                            
756
                            #cover the points that exceed the threshold with the color "white"
757
                            points(HX1,HY1,pch=19,cex=cex[1],col="white")
758
                            
759
                                for(ll in 1:length(threshold)){
760
                                    if(ll == 1){
761
                                        if(LOG10){
762
                                            significantline1=H*(-log10(threshold[ll])-Min)/(Max-Min)
763
                                        }else{
764
                                            significantline1=H*(threshold[ll]-Min)/(Max-Min)
765
                                        }
766
                                        p_amp.index <- which(Cpvalue>=significantline1)
767
                                        HX1=(-Cpvalue[p_amp.index]+r+H*i+cir.band*(i-1))*sin(2*pi*(pvalue.posN[p_amp.index]-round(band/2))/TotalN)
768
                                        HY1=(-Cpvalue[p_amp.index]+r+H*i+cir.band*(i-1))*cos(2*pi*(pvalue.posN[p_amp.index]-round(band/2))/TotalN)
769
                                    }else{
770
                                        if(LOG10){
771
                                            significantline0=H*(-log10(threshold[ll-1])-Min)/(Max-Min)
772
                                            significantline1=H*(-log10(threshold[ll])-Min)/(Max-Min)
773
                                        }else{
774
                                            significantline0=H*(threshold[ll-1]-Min)/(Max-Min)
775
                                            significantline1=H*(threshold[ll]-Min)/(Max-Min)
776
                                        }
777
                                        p_amp.index <- which(Cpvalue>=significantline1 & Cpvalue < significantline0)
778
                                        HX1=(-Cpvalue[p_amp.index]+r+H*i+cir.band*(i-1))*sin(2*pi*(pvalue.posN[p_amp.index]-round(band/2))/TotalN)
779
                                        HY1=(-Cpvalue[p_amp.index]+r+H*i+cir.band*(i-1))*cos(2*pi*(pvalue.posN[p_amp.index]-round(band/2))/TotalN)
780
                                    
781
                                    }
782
                                
783
                                    if(is.null(signal.col)){
784
                                        points(HX1,HY1,pch=signal.pch[ll],cex=signal.cex[ll]*cex[1],col=rep(rep(colx,N[i]),add[[i]])[p_amp.index])
785
                                    }else{
786
                                        points(HX1,HY1,pch=signal.pch[ll],cex=signal.cex[ll]*cex[1],col=signal.col[ll])
787
                                    }
788
                                }
789
                        }
790
                    }
791
                }
792
                
793
                if(cir.chr==TRUE){
794
                    ticks1=1.1*(2*cir.band+RR)*sin(2*pi*(ticks-round(band/2))/TotalN)
795
                    ticks2=1.1*(2*cir.band+RR)*cos(2*pi*(ticks-round(band/2))/TotalN)
796
                    if(is.null(chr.labels)){
797
                        for(i in 1:length(ticks)){
798
                          angle=360*(1-(ticks-round(band/2))[i]/TotalN)
799
                          text(ticks1[i],ticks2[i],chr.ori[i],srt=angle,font=2,cex=cex.axis)
800
                        }
801
                    }else{
802
                        for(i in 1:length(ticks)){
803
                            angle=360*(1-(ticks-round(band/2))[i]/TotalN)
804
                            text(ticks1[i],ticks2[i],chr.labels[i],srt=angle,font=2,cex=cex.axis)
805
                        }
806
                    }
807
                }else{
808
                    ticks1=1.0*(RR+cir.band)*sin(2*pi*(ticks-round(band/2))/TotalN)
809
                    ticks2=1.0*(RR+cir.band)*cos(2*pi*(ticks-round(band/2))/TotalN)
810
                    if(is.null(chr.labels)){
811
                        for(i in 1:length(ticks)){
812
                        
813
                            #adjust the angle of labels of circle plot
814
                            angle=360*(1-(ticks-round(band/2))[i]/TotalN)
815
                            text(ticks1[i],ticks2[i],chr.ori[i],srt=angle,font=2,cex=cex.axis)
816
                        }
817
                    }else{
818
                        for(i in 1:length(ticks)){
819
                            angle=360*(1-(ticks-round(band/2))[i]/TotalN)
820
                            text(ticks1[i],ticks2[i],chr.labels[i],srt=angle,font=2,cex=cex.axis)
821
                        }
822
                    }   
823
                }
824
            }
825
        }
826
        if(file.output) dev.off()
827
        #print("Circular-Manhattan has been finished!",quote=F)
828
    }
829
830
    if("m" %in% plot.type){
831
        if(multracks==FALSE){
832
            #print("Starting Rectangular-Manhattan plot!",quote=F)
833
            for(i in 1:R){
834
                colx=col[i,]
835
                colx=colx[!is.na(colx)]
836
                if(verbose) print(paste("Rectangular_Manhattan Plotting ",taxa[i],"...",sep=""))
837
                    if(file.output){
838
                        if(file=="jpg") jpeg(paste("Manhattan.",taxa[i],".jpg",sep=""), width = 14*dpi,height=5*dpi,res=dpi,quality = 100)
839
                        if(file=="pdf") pdf(paste("Manhattan.",taxa[i],".pdf",sep=""), width = 15,height=6)
840
                        if(file=="tiff")    tiff(paste("Manhattan.",taxa[i],".tiff",sep=""), width = 14*dpi,height=5*dpi,res=dpi)
841
                        par(mar = c(5,6,4,3),xaxs=xaxs,yaxs=yaxs,xpd=TRUE)
842
                    }
843
                    if(!file.output){
844
                        if(is.null(dev.list())) dev.new(width = 15, height = 6)
845
                        par(xpd=TRUE)
846
                    }
847
                    
848
                    pvalue=pvalueT[,i]
849
                    logpvalue=logpvalueT[,i]
850
                    if(is.null(ylim)){
851
                        if(!is.null(threshold)){
852
                            if(sum(threshold!=0)==length(threshold)){
853
                                if(LOG10 == TRUE){
854
                                    Max=max(ceiling(-log10(min(pvalue[pvalue!=0]))),ceiling(-log10(min(threshold))))
855
                                    Min <- 0
856
                                }else{
857
                                    Max=max(ceiling(max(pvalue[pvalue!=Inf])),max(threshold))
858
                                    if(abs(Max)<=1) Max=max(max(pvalue[pvalue!=Inf]),max(threshold))
859
                                    Min <- min(floor(min(pvalue[pvalue!=Inf])),min(threshold))
860
                                    if(abs(Min)<=1) Min=min(min(pvalue[pvalue!=Inf]),min(threshold))
861
                                }
862
                            }else{
863
                                if(LOG10){
864
                                    Max=max(ceiling(-log10(min(pvalue[pvalue!=0]))))
865
                                    Min<-0
866
                                }else{
867
                                    Max=ceiling(max(pvalue[pvalue!=Inf]))
868
                                    if(abs(Max)<=1) Max=max(max(pvalue[pvalue!=Inf]))
869
                                    Min<-floor(min(pvalue[pvalue!=Inf]))
870
                                    if(abs(Min)<=1) Min=min(pvalue[pvalue!=Inf])
871
                                    # }else{
872
                                        # Max=max(ceiling(max(pvalue[pvalue!=Inf])))
873
                                    # }
874
                                }
875
                            }
876
                        }else{
877
                            if(LOG10){
878
                                Max=max(ceiling(-log10(min(pvalue[pvalue!=0]))))
879
                                Min=0
880
                            }else{
881
                                Max=ceiling(max(pvalue[pvalue!=Inf]))
882
                                if(abs(Max)<=1) Max=max(pvalue[pvalue!=Inf])
883
                                Min=floor(min(pvalue[pvalue!=Inf]))
884
                                if(abs(Min)<=1) Min=min(pvalue[pvalue!=Inf])
885
                                # }else{
886
                                    # Max=max(ceiling(max(pvalue[pvalue!=Inf])))
887
                                # }
888
                            }
889
                        }
890
                        if((Max-Min)<=1){
891
                            if(cir.density){
892
                                plot(pvalue.posN,logpvalue,pch=pch,cex=cex[2],col=rep(rep(colx,N[i]),add[[i]]),xlim=c(0,1.01*max(pvalue.posN)),ylim=c(Min-(Max-Min)/den.fold, Max+10^(-ceiling(-log10(abs(Max))))),ylab=ylab,
893
                                    cex.axis=cex.axis,cex.lab=2,font=2,axes=FALSE,xlab=xlab,main=taxa)
894
                            }else{
895
                                plot(pvalue.posN,logpvalue,pch=pch,cex=cex[2],col=rep(rep(colx,N[i]),add[[i]]),xlim=c(0,max(pvalue.posN)),ylim=c(Min,Max+10^(-ceiling(-log10(abs(Max))))),ylab=ylab,
896
                                cex.axis=cex.axis,cex.lab=2,font=2,axes=FALSE,xlab=xlab,main=taxa)
897
                            }
898
                        }else{
899
                            if(cir.density){
900
                                plot(pvalue.posN,logpvalue,pch=pch,cex=cex[2],col=rep(rep(colx,N[i]),add[[i]]),xlim=c(0,1.01*max(pvalue.posN)),ylim=c(Min-(Max-Min)/den.fold,Max),ylab=ylab,
901
                                cex.axis=cex.axis,cex.lab=2,font=2,axes=FALSE,xlab=xlab,main=taxa)
902
                            }else{
903
                                plot(pvalue.posN,logpvalue,pch=pch,cex=cex[2],col=rep(rep(colx,N[i]),add[[i]]),xlim=c(0,max(pvalue.posN)),ylim=c(Min,Max),ylab=ylab,
904
                                cex.axis=cex.axis,cex.lab=2,font=2,axes=FALSE,xlab=xlab,main=taxa)
905
                            }
906
                        }
907
                    }else{
908
                        Max <- max(ylim)
909
                        Min <- min(ylim)
910
                        if(cir.density){
911
                            plot(pvalue.posN[logpvalue>=min(ylim)],logpvalue[logpvalue>=min(ylim)],pch=pch,cex=cex[2],col=rep(rep(colx,N[i]),add[[i]])[logpvalue>=min(ylim)],xlim=c(0,1.01*max(pvalue.posN)),ylim=c(min(ylim)-(Max-Min)/den.fold, max(ylim)),ylab=ylab,
912
                            cex.axis=cex.axis,cex.lab=2,font=2,axes=FALSE,xlab=xlab,main=taxa)
913
                        }else{
914
                            plot(pvalue.posN[logpvalue>=min(ylim)],logpvalue[logpvalue>=min(ylim)],pch=pch,cex=cex[2],col=rep(rep(colx,N[i]),add[[i]])[logpvalue>=min(ylim)],xlim=c(0,max(pvalue.posN)),ylim=ylim,ylab=ylab,
915
                            cex.axis=cex.axis,cex.lab=2,font=2,axes=FALSE,xlab=xlab,main=taxa)
916
                        }
917
                    }
918
                    Max1 <- Max
919
                    Min1 <- Min
920
                    if(abs(Max) <= 1) Max <- round(Max, ceiling(-log10(abs(Max))))
921
                    if(abs(Min) <= 1) Min <- round(Min, ceiling(-log10(abs(Min))))
922
                    if(is.null(chr.labels)){
923
                        axis(1, at=c(0,ticks),cex.axis=cex.axis,font=2,labels=c("Chr",chr.ori))
924
                    }else{
925
                        axis(1, at=c(0,ticks),cex.axis=cex.axis,font=2,labels=c("Chr",chr.labels))
926
                    }
927
                    if(is.null(ylim)){
928
                        if((Max-Min)>1){
929
                            axis(2,at=seq(Min,(Max),ceiling((Max-Min)/10)),cex.axis=cex.axis,font=2,labels=round(seq(Min,(Max),ceiling((Max-Min)/10)), 2))
930
                            legend.y <- tail(round(seq(Min,(Max),ceiling((Max-Min)/10)), 2), 1)
931
                        }else{
932
                            axis(2,at=seq(Min,Max+10^(-ceiling(-log10(abs(Max)))),10^(-ceiling(-log10(max(abs(c(Max, Min))))))),cex.axis=cex.axis,font=2,labels=seq(Min,Max+10^(-ceiling(-log10(abs(Max)))),10^(-ceiling(-log10(max(abs(c(Max, Min))))))))
933
                            legend.y <- tail(seq(Min,Max+10^(-ceiling(-log10(abs(Max)))),10^(-ceiling(-log10(max(abs(c(Max, Min))))))), 1)
934
                        }
935
                    }else{
936
                        if(ylim[2]>1){
937
                            axis(2,at=seq(min(ylim),ylim[2],ceiling((ylim[2])/10)),cex.axis=cex.axis,font=2,labels=round(seq(min(ylim),(ylim[2]),ceiling((ylim[2])/10)), 2))
938
                            legend.y <- tail(ylim[2], 1)
939
                        }else{
940
                            axis(2,at=seq(min(ylim),ylim[2],10^(-ceiling(-log10(max(abs(ylim)))))),cex.axis=cex.axis,font=2,labels=seq(min(ylim),ylim[2],10^(-ceiling(-log10(max(abs(ylim)))))))
941
                            legend.y <- tail(ylim[2], 1)
942
                        }
943
                    }
944
                    if(!is.null(threshold)){
945
                        if(sum(threshold!=0)==length(threshold)){
946
                            for(thr in 1:length(threshold)){
947
                                h <- ifelse(LOG10, -log10(threshold[thr]), threshold[thr])
948
                                # print(h)
949
                                # print(threshold.col[thr])
950
                                # print(threshold.lty[thr])
951
                                # print(threshold.lwd[thr])
952
                                par(xpd=FALSE); abline(h=h,col=threshold.col[thr],lty=threshold.lty[thr],lwd=threshold.lwd[thr]); par(xpd=TRUE)
953
                            }
954
                            if(amplify == TRUE){
955
                                if(LOG10){
956
                                    threshold <- sort(threshold)
957
                                    sgline1=-log10(max(threshold))
958
                                }else{
959
                                    threshold <- sort(threshold, decreasing=TRUE)
960
                                    sgline1=min(threshold)
961
                                }
962
963
                                sgindex=which(logpvalue>=sgline1)
964
                                HY1=logpvalue[sgindex]
965
                                HX1=pvalue.posN[sgindex]
966
                                
967
                                #cover the points that exceed the threshold with the color "white"
968
                                points(HX1,HY1,pch=pch,cex=cex[2],col="white")
969
                                
970
                                for(ll in 1:length(threshold)){
971
                                    if(ll == 1){
972
                                        if(LOG10){
973
                                            sgline1=-log10(threshold[ll])
974
                                        }else{
975
                                            sgline1=threshold[ll]
976
                                        }
977
                                        sgindex=which(logpvalue>=sgline1)
978
                                        HY1=logpvalue[sgindex]
979
                                        HX1=pvalue.posN[sgindex]
980
                                    }else{
981
                                        if(LOG10){
982
                                            sgline0=-log10(threshold[ll-1])
983
                                            sgline1=-log10(threshold[ll])
984
                                        }else{
985
                                            sgline0=threshold[ll-1]
986
                                            sgline1=threshold[ll]
987
                                        }
988
                                        sgindex=which(logpvalue>=sgline1 & logpvalue < sgline0)
989
                                        HY1=logpvalue[sgindex]
990
                                        HX1=pvalue.posN[sgindex]
991
                                    }
992
993
                                    if(is.null(signal.col)){
994
                                        points(HX1,HY1,pch=signal.pch[ll],cex=signal.cex[ll]*cex[2],col=rep(rep(colx,N[i]),add[[i]])[sgindex])
995
                                    }else{
996
                                        points(HX1,HY1,pch=signal.pch[ll],cex=signal.cex[ll]*cex[2],col=signal.col[ll])
997
                                    }
998
                                    
999
                                }
1000
                            }
1001
                        }
1002
                    }
1003
                    if(is.null(ylim)){ymin <- Min1}else{ymin <- min(ylim)}
1004
                    if(cir.density){
1005
                        for(yll in 1:length(pvalue.posN.list)){
1006
                            polygon(c(min(pvalue.posN.list[[yll]]), min(pvalue.posN.list[[yll]]), max(pvalue.posN.list[[yll]]), max(pvalue.posN.list[[yll]])), 
1007
                                c(ymin-0.5*(Max-Min)/den.fold, ymin-1.5*(Max-Min)/den.fold, 
1008
                                ymin-1.5*(Max-Min)/den.fold, ymin-0.5*(Max-Min)/den.fold), 
1009
                                col="grey", border="grey")
1010
                        }
1011
                        
1012
                        segments(
1013
                            pvalue.posN,
1014
                            ymin-0.5*(Max-Min)/den.fold,
1015
                            pvalue.posN,
1016
                            ymin-1.5*(Max-Min)/den.fold,
1017
                            col=density.list$den.col, lwd=0.1
1018
                        )
1019
                        legend(
1020
                            x=max(pvalue.posN)+band,
1021
                            y=legend.y,
1022
                            title="", legend=density.list$legend.y, pch=15, pt.cex = 2.5, col=density.list$legend.col,
1023
                            cex=0.8, bty="n",
1024
                            y.intersp=1,
1025
                            x.intersp=1,
1026
                            yjust=1, xjust=0, xpd=TRUE
1027
                        )
1028
                        
1029
                    }
1030
                if(box) box()
1031
                #if(!is.null(threshold) & (length(grep("FarmCPU",taxa[i])) != 0))   abline(v=which(pvalueT[,i] < min(threshold)/max(dim(Pmap))),col="grey",lty=2,lwd=signal.line)
1032
                if(file.output)  dev.off()
1033
            }
1034
            #print("Rectangular-Manhattan has been finished!",quote=F)
1035
        }else{
1036
            #print("Starting Rectangular-Manhattan plot!",quote=F)
1037
            #print("Plotting in multiple tracks!",quote=F)
1038
            if(file.output){
1039
                if(file=="jpg") jpeg(paste("Multracks.Rectangular-Manhattan.",paste(taxa,collapse="."),".jpg",sep=""), width = 14*dpi,height=5*dpi*R,res=dpi,quality = 100)
1040
                if(file=="pdf") pdf(paste("Multracks.Rectangular-Manhattan.",paste(taxa,collapse="."),".pdf",sep=""), width = 15,height=6*R)
1041
                if(file=="tiff")    tiff(paste("Multracks.Rectangular-Manhattan.",paste(taxa,collapse="."),".tiff",sep=""), width = 14*dpi,height=5*dpi*R,res=dpi)
1042
                par(mfcol=c(R,1),mar=c(0, 6+(R-1)*2, 0, 2),oma=c(4,0,4,0),xaxs=xaxs,yaxs=yaxs,xpd=TRUE)
1043
            }
1044
            if(!file.output){
1045
                if(is.null(dev.list())) dev.new(width = 15, height = 6)
1046
                par(xpd=TRUE)
1047
            }
1048
            for(i in 1:R){
1049
                if(verbose) print(paste("Multracks_Rectangular Plotting ",taxa[i],"...",sep=""))
1050
                colx=col[i,]
1051
                colx=colx[!is.na(colx)]
1052
                pvalue=pvalueT[,i]
1053
                logpvalue=logpvalueT[,i]
1054
                if(is.null(ylim)){
1055
                    if(!is.null(threshold)){
1056
                        if(sum(threshold!=0)==length(threshold)){
1057
                            if(LOG10){
1058
                                Max=max(ceiling(-log10(min(pvalue[pvalue!=0]))),-log10(min(threshold)))
1059
                                Min <- 0
1060
                            }else{
1061
                                Max=max(ceiling(max(pvalue[pvalue!=Inf])),max(threshold))
1062
                                if(abs(Max)<=1) Max=max(max(pvalue[pvalue!=Inf]),max(threshold))
1063
                                Min<-min(floor(min(pvalue[pvalue!=Inf])),min(threshold))
1064
                                if(abs(Min)<=1) Min=min(min(pvalue[pvalue!=Inf]),min(threshold))
1065
                            }
1066
                        }else{
1067
                            if(LOG10){
1068
                                Max=max(ceiling(-log10(min(pvalue[pvalue!=0]))))
1069
                                Min<-0
1070
                            }else{
1071
                                Max=max(ceiling(max(pvalue[pvalue!=Inf])))
1072
                                if(abs(Max)<=1) Max=max(max(pvalue[pvalue!=Inf]))
1073
                                Min=min(floor(min(pvalue[pvalue!=Inf])))
1074
                                if(abs(Min)<=1) Min=min(min(pvalue[pvalue!=Inf]))
1075
                                # }else{
1076
                                    # Max=max(ceiling(max(pvalue[pvalue!=Inf])))
1077
                                # }
1078
                            }   
1079
                        }
1080
                    }else{
1081
                        if(LOG10){
1082
                            Max=max(ceiling(-log10(min(pvalue[pvalue!=0]))))
1083
                            Min=0
1084
                        }else{
1085
                            Max=max(ceiling(max(pvalue[pvalue!=Inf])))
1086
                            if(abs(Max)<=1) Max=max(max(pvalue[pvalue!=Inf]))
1087
                            Min <- min(ceiling(min(pvalue[pvalue!=Inf])))
1088
                            if(abs(Min)<=1) Min=min(min(pvalue[pvalue!=Inf]))
1089
                            # }else{
1090
                                # Max=max(ceiling(max(pvalue[pvalue!=Inf])))
1091
                            # }
1092
                        }
1093
                    }
1094
                    xn <- ifelse(R == 1, R, R * 2/3)
1095
                    if((Max-Min)<=1){
1096
                        plot(pvalue.posN,logpvalue,pch=pch,cex=cex[2]*xn,col=rep(rep(colx,N[i]),add[[i]]),xlim=c(0,max(pvalue.posN)+band),ylim=c(Min,Max+10^(-ceiling(-log10(abs(Max))))),ylab=ylab,
1097
                            cex.axis=cex.axis*xn,cex.lab=2*xn,font=2,axes=FALSE)
1098
                    }else{
1099
                        plot(pvalue.posN,logpvalue,pch=pch,cex=cex[2]*xn,col=rep(rep(colx,N[i]),add[[i]]),xlim=c(0,max(pvalue.posN)+band),ylim=c(Min,Max),ylab=ylab,
1100
                            cex.axis=cex.axis*xn,cex.lab=2*xn,font=2,axes=FALSE)
1101
                    }
1102
                }else{
1103
                    xn <- ifelse(R == 1, R, R * 2/3)
1104
                    Max <- max(ylim)
1105
                    Min <- min(ylim)
1106
                    plot(pvalue.posN[logpvalue>=min(ylim)],logpvalue[logpvalue>=min(ylim)],pch=pch,cex=cex[2]*xn,col=rep(rep(colx,N[i]),add[[i]])[logpvalue>=min(ylim)],xlim=c(0,max(pvalue.posN)+band),ylim=ylim,ylab=ylab,
1107
                        cex.axis=cex.axis*xn,cex.lab=2*xn,font=2,axes=FALSE)
1108
                }
1109
                Max1 <- Max
1110
                Min1 <- Min
1111
                if(abs(Max) <= 1) Max <- round(Max, ceiling(-log10(abs(Max))))
1112
                if(abs(Min) <= 1) Min <- round(Min, ceiling(-log10(abs(Min))))
1113
                
1114
                #add the names of traits on plot 
1115
                if((Max-Min)<=1){
1116
                    text(ticks[1],Max+10^(-ceiling(-log10(abs(Max)))),labels=taxa[i],adj=0,font=3,cex=xn)
1117
                }else{
1118
                    text(ticks[1],Max+1,labels=taxa[i],adj=0,font=3,cex=xn)
1119
                }
1120
                if(i == R){
1121
                    if(is.null(chr.labels)){
1122
                        axis(1, at=c(0,ticks),cex.axis=cex.axis*xn,font=2,labels=c("Chr",chr.ori),padj=(xn-1)/2)
1123
                    }else{
1124
                        axis(1, at=c(0,ticks),cex.axis=cex.axis*xn,font=2,labels=c("Chr",chr.labels),padj=(xn-1)/2)
1125
                    }
1126
                }
1127
                #if(i==1) mtext("Manhattan plot",side=3,padj=-1,font=2,cex=xn)
1128
                if(is.null(ylim)){
1129
                    if((Max-Min)>1){
1130
                        axis(2,at=seq(Min,(Max),ceiling((Max-Min)/10)),cex.axis=cex.axis*xn,font=2,labels=round(seq(Min,(Max),ceiling((Max-Min)/10)), 2))
1131
                    }else{
1132
                        axis(2,at=seq(Min,Max+10^(-ceiling(-log10(abs(Max)))),10^(-ceiling(-log10(max(abs(c(Max, Min))))))),cex.axis=cex.axis*xn,font=2,labels=seq(0,Max+10^(-ceiling(-log10(abs(Max)))),10^(-ceiling(-log10(max(abs(c(Max, Min))))))))
1133
                    }
1134
                }else{
1135
                    if(ylim[2]>1){
1136
                        axis(2,at=seq(min(ylim),(ylim[2]),ceiling((ylim[2])/10)),cex.axis=cex.axis*xn,font=2,labels=round(seq(min(ylim),(ylim[2]),ceiling((ylim[2])/10)), 2))
1137
                    }else{
1138
                        axis(2,at=seq(min(ylim),ylim[2],10^(-ceiling(-log10(max(abs(ylim)))))),cex.axis=cex.axis*xn,font=2,labels=seq(min(ylim),ylim[2],10^(-ceiling(-log10(max(abs(ylim)))))))
1139
                    }
1140
                }
1141
                if(!is.null(threshold)){
1142
                    if(sum(threshold!=0)==length(threshold)){
1143
                        for(thr in 1:length(threshold)){
1144
                            h <- ifelse(LOG10, -log10(threshold[thr]), threshold[thr])
1145
                            par(xpd=FALSE); abline(h=h,col=threshold.col[thr],lwd=threshold.lwd[thr],lty=threshold.lty[thr]); par(xpd=TRUE)
1146
                        }
1147
                        if(amplify==TRUE){
1148
                                if(LOG10){
1149
                                    threshold <- sort(threshold)
1150
                                    sgline1=-log10(max(threshold))
1151
                                }else{
1152
                                    threshold <- sort(threshold, decreasing=TRUE)
1153
                                    sgline1=min(threshold)
1154
                                }
1155
                                sgindex=which(logpvalue>=sgline1)
1156
                                HY1=logpvalue[sgindex]
1157
                                HX1=pvalue.posN[sgindex]
1158
                                
1159
                                #cover the points that exceed the threshold with the color "white"
1160
                                points(HX1,HY1,pch=pch,cex=cex[2]*xn,col="white")
1161
                                
1162
                                for(ll in 1:length(threshold)){
1163
                                    if(ll == 1){
1164
                                        if(LOG10){
1165
                                            sgline1=-log10(threshold[ll])
1166
                                        }else{
1167
                                            sgline1=threshold[ll]
1168
                                        }
1169
                                        sgindex=which(logpvalue>=sgline1)
1170
                                        HY1=logpvalue[sgindex]
1171
                                        HX1=pvalue.posN[sgindex]
1172
                                    }else{
1173
                                        if(LOG10){
1174
                                            sgline0=-log10(threshold[ll-1])
1175
                                            sgline1=-log10(threshold[ll])
1176
                                        }else{
1177
                                            sgline0=threshold[ll-1]
1178
                                            sgline1=threshold[ll]
1179
                                        }
1180
                                        sgindex=which(logpvalue>=sgline1 & logpvalue < sgline0)
1181
                                        HY1=logpvalue[sgindex]
1182
                                        HX1=pvalue.posN[sgindex]
1183
                                    }
1184
1185
                                    if(is.null(signal.col)){
1186
                                        points(HX1,HY1,pch=signal.pch[ll],cex=signal.cex[ll]*cex[2]*xn,col=rep(rep(colx,N[i]),add[[i]])[sgindex])
1187
                                    }else{
1188
                                        points(HX1,HY1,pch=signal.pch[ll],cex=signal.cex[ll]*cex[2]*xn,col=signal.col[ll])
1189
                                    }
1190
                                    
1191
                                }
1192
                        }
1193
                    }
1194
                }
1195
                #if(!is.null(threshold) & (length(grep("FarmCPU",taxa[i])) != 0))   abline(v=which(pvalueT[,i] < min(threshold)/max(dim(Pmap))),col="grey",lty=2,lwd=signal.line)
1196
            }
1197
            
1198
            #add the labels of X-axis
1199
            #mtext(xlab,side=1,padj=2.5,font=2,cex=R*2/3)
1200
            if(file.output) dev.off()
1201
            
1202
            if(file.output){
1203
                if(file=="jpg") jpeg(paste("Multraits.Rectangular-Manhattan.",paste(taxa,collapse="."),".jpg",sep=""), width = 14*dpi,height=5*dpi,res=dpi,quality = 100)
1204
                if(file=="pdf") pdf(paste("Multraits.Rectangular-Manhattan.",paste(taxa,collapse="."),".pdf",sep=""), width = 15,height=6)
1205
                if(file=="tiff")    tiff(paste("Multraits.Rectangular-Manhattan.",paste(taxa,collapse="."),".tiff",sep=""), width = 14*dpi,height=5*dpi,res=dpi)
1206
                par(mar = c(5,6,4,3),xaxs=xaxs,yaxs=yaxs,xpd=TRUE)
1207
            }
1208
            if(!file.output){
1209
                if(is.null(dev.list())) dev.new(width = 15, height = 6)
1210
                par(xpd=TRUE)
1211
            }
1212
            
1213
            pvalue <- as.vector(Pmap[,3:(R+2)])
1214
            if(is.null(ylim)){
1215
                if(!is.null(threshold)){
1216
                    if(sum(threshold!=0)==length(threshold)){
1217
                        if(LOG10){
1218
                            Max=max(ceiling(-log10(min(pvalue[pvalue!=0]))),-log10(min(threshold)))
1219
                            Min<-0
1220
                        }else{
1221
                            Max=max(ceiling(max(pvalue[pvalue!=Inf])),max(threshold))
1222
                            if(abs(Max)<=1) Max=max(max(pvalue[pvalue!=Inf]),max(threshold))
1223
                            Min <- min(floor(min(pvalue[pvalue!=Inf])),min(threshold))
1224
                            if(abs(Min)<=1) Min=min(min(pvalue[pvalue!=Inf]),min(threshold))
1225
                        }
1226
                    }else{
1227
                        if(LOG10){
1228
                            Max=max(ceiling(-log10(min(pvalue[pvalue!=0]))))
1229
                            Min=0
1230
                        }else{
1231
                            Max=max(ceiling(max(pvalue[pvalue!=Inf])))
1232
                            if(abs(Max)<=1) Max=max(max(pvalue[pvalue!=Inf]))
1233
                            Min<- min(floor(min(pvalue[pvalue!=Inf])))
1234
                            if(abs(Min)<=1) Min=min(min(pvalue[pvalue!=Inf]))
1235
                            # }else{
1236
                                # Max=max(ceiling(max(pvalue[pvalue!=Inf])))
1237
                            # }
1238
                        }   
1239
                    }
1240
                }else{
1241
                    if(LOG10){
1242
                        Max=max(ceiling(-log10(min(pvalue[pvalue!=0]))))
1243
                        Min=0
1244
                    }else{
1245
                        Max=max(ceiling(max(pvalue[pvalue!=Inf])))
1246
                        
1247
                        #{
1248
                        if(abs(Max)<=1) Max=max(max(pvalue[pvalue!=Inf]))
1249
                        Min<- min(floor(min(pvalue[pvalue!=Inf])))
1250
                        if(abs(Min)<=1) Min=min(min(pvalue[pvalue!=Inf]))
1251
                        
1252
                        # }else{
1253
                            # Max=max(ceiling(max(pvalue[pvalue!=Inf])))
1254
                        # }
1255
                    }
1256
                }
1257
                if((Max-Min)<=1){
1258
                    if(cir.density){
1259
                        plot(NULL,xlim=c(0,1.01*max(pvalue.posN)),ylim=c(Min-(Max-Min)/den.fold, Max+10^(-ceiling(-log10(abs(Max))))),ylab=ylab,
1260
                            cex.axis=cex.axis,cex.lab=2,font=2,axes=FALSE,xlab=xlab,main="")
1261
                    }else{
1262
                        plot(NULL,xlim=c(0,max(pvalue.posN)),ylim=c(Min,Max+10^(-ceiling(-log10(abs(Max))))),ylab=ylab,
1263
                            cex.axis=cex.axis,cex.lab=2,font=2,axes=FALSE,xlab=xlab,main="")
1264
                    }
1265
                }else{
1266
                    if(cir.density){
1267
                        plot(NULL,xlim=c(0,1.01*max(pvalue.posN)),ylim=c(Min-(Max-Min)/den.fold,Max),ylab=ylab,
1268
                            cex.axis=cex.axis,cex.lab=2,font=2,axes=FALSE,xlab=xlab,main="")
1269
                    }else{
1270
                        plot(NULL,xlim=c(0,max(pvalue.posN)),ylim=c(Min,Max),ylab=ylab,
1271
                            cex.axis=cex.axis,cex.lab=2,font=2,axes=FALSE,xlab=xlab,main="")
1272
                    }
1273
                }
1274
            }else{
1275
                Max <- max(ylim)
1276
                Min <- min(ylim)
1277
                if(cir.density){
1278
                    plot(NULL,xlim=c(0,1.01*max(pvalue.posN)),ylim=c(min(ylim)-Max/den.fold,Max),ylab=ylab,
1279
                        cex.axis=cex.axis,cex.lab=2,font=2,axes=FALSE,xlab=xlab,main="Manhattan plot of")
1280
                }else{
1281
                    plot(NULL,xlim=c(0,max(pvalue.posN)),ylim=ylim,ylab=ylab,
1282
                        cex.axis=cex.axis,cex.lab=2,font=2,axes=FALSE,xlab=xlab,main="Manhattan plot of")
1283
                }
1284
            }
1285
            Max1 <- Max
1286
            Min1 <- Min
1287
            if(abs(Max) <= 1) Max <- round(Max, ceiling(-log10(abs(Max))))
1288
            if(abs(Min) <= 1) Min <- round(Min, ceiling(-log10(abs(Min))))
1289
            legend("topleft",taxa,col=t(col)[1:R],pch=19,text.font=6,box.col=NA)
1290
            if(is.null(chr.labels)){
1291
                axis(1, at=c(0,ticks),cex.axis=cex.axis,font=2,labels=c("Chr",chr.ori))
1292
            }else{
1293
                axis(1, at=c(0,ticks),cex.axis=cex.axis,font=2,labels=c("Chr",chr.labels))
1294
            }
1295
            if(is.null(ylim)){
1296
                if((Max-Min)>1){
1297
                    #print(seq(0,(Max+1),ceiling((Max+1)/10)))
1298
                    axis(2,at=seq(Min,(Max),ceiling((Max-Min)/10)),cex.axis=cex.axis,font=2,labels=round(seq(Min,(Max),ceiling((Max-Min)/10)),2))
1299
                    legend.y <- tail(round(seq(0,(Max),ceiling((Max)/10)),2), 1)
1300
                }else{
1301
                    axis(2,at=seq(Min,Max+10^(-ceiling(-log10(abs(Max)))),10^(-ceiling(-log10(max(abs(c(Max, Min))))))),cex.axis=cex.axis,font=2,labels=seq(0,Max+10^(-ceiling(-log10(abs(Max)))),10^(-ceiling(-log10(max(abs(c(Max, Min))))))))
1302
                    legend.y <- tail(seq(0,Max+10^(-ceiling(-log10(abs(Max)))),10^(-ceiling(-log10(max(abs(c(Max, Min))))))), 1)
1303
                }
1304
            }else{
1305
                if(ylim[2]>1){
1306
                    axis(2,at=seq(min(ylim),(ylim[2]),ceiling((ylim[2])/10)),cex.axis=cex.axis,font=2,labels=round(seq(min(ylim),(ylim[2]),ceiling((ylim[2])/10)),2))
1307
                    legend.y <- tail(ylim[2], 1)
1308
                }else{
1309
                    axis(2,at=seq(min(ylim),ylim[2],10^(-ceiling(-log10(max(abs(ylim)))))),cex.axis=cex.axis,font=2,labels=seq(min(ylim),ylim[2],10^(-ceiling(-log10(max(abs(ylim)))))))
1310
                    legend.y <- tail(ylim[2], 1)
1311
                }
1312
            }
1313
            do <- TRUE
1314
            sam.index <- list()
1315
            for(l in 1:R){
1316
                sam.index[[l]] <- 1:nrow(Pmap)
1317
            }
1318
            
1319
            #change the sample number according to Pmap
1320
            sam.num <- ceiling(nrow(Pmap)/30)
1321
            if(verbose) print("Multraits_Rectangular Plotting...")
1322
            while(do){
1323
                for(i in 1:R){
1324
                    if(length(sam.index[[i]]) < sam.num){
1325
                        plot.index <- sam.index[[i]]
1326
                    }else{
1327
                        plot.index <- sample(sam.index[[i]], sam.num, replace=FALSE)
1328
                    }
1329
                    sam.index[[i]] <- sam.index[[i]][-which(sam.index[[i]] %in% plot.index)]
1330
                    logpvalue=logpvalueT[plot.index,i]
1331
                    if(!is.null(ylim)){indexx <- logpvalue>=min(ylim)}else{indexx <- 1:length(logpvalue)}
1332
                    points(pvalue.posN[plot.index][indexx],logpvalue[indexx],pch=pch,cex=cex[2],col=rgb(col2rgb(t(col)[i])[1], col2rgb(t(col)[i])[2], col2rgb(t(col)[i])[3], 100, maxColorValue=255))
1333
                    #if(!is.null(threshold) & (length(grep("FarmCPU",taxa[i])) != 0))   abline(v=which(pvalueT[,i] < min(threshold)/max(dim(Pmap))),col="grey",lty=2,lwd=signal.line)
1334
                }
1335
                if(length(sam.index[[i]]) == 0) do <- FALSE
1336
            }
1337
            
1338
            # for(i in 1:R){
1339
                # logpvalue=logpvalueT[,i]
1340
                # points(pvalue.posN,logpvalue,pch=pch,cex=cex[2],col=t(col)[i])
1341
            # }
1342
            
1343
            if(!is.null(threshold)){
1344
                if(sum(threshold!=0)==length(threshold)){
1345
                    for(thr in 1:length(threshold)){
1346
                        h <- ifelse(LOG10, -log10(threshold[thr]), threshold[thr])
1347
                        par(xpd=FALSE); abline(h=h,col=threshold.col[thr],lwd=threshold.lwd[thr],lty=threshold.lty[thr]); par(xpd=TRUE)
1348
                    }
1349
                }
1350
            }
1351
            if(is.null(ylim)){ymin <- Min1}else{ymin <- min(ylim)}
1352
            if(cir.density){
1353
                        for(yll in 1:length(pvalue.posN.list)){
1354
                            polygon(c(min(pvalue.posN.list[[yll]]), min(pvalue.posN.list[[yll]]), max(pvalue.posN.list[[yll]]), max(pvalue.posN.list[[yll]])), 
1355
                                c(ymin-0.5*(Max-Min)/den.fold, ymin-1.5*(Max-Min)/den.fold, 
1356
                                ymin-1.5*(Max-Min)/den.fold, ymin-0.5*(Max-Min)/den.fold), 
1357
                                col="grey", border="grey")
1358
                        }
1359
                        
1360
                        segments(
1361
                            pvalue.posN,
1362
                            ymin-0.5*(Max-Min)/den.fold,
1363
                            pvalue.posN,
1364
                            ymin-1.5*(Max-Min)/den.fold,
1365
                            col=density.list$den.col, lwd=0.1
1366
                        )
1367
                        legend(
1368
                            x=max(pvalue.posN)+band,
1369
                            y=legend.y,
1370
                            title="", legend=density.list$legend.y, pch=15, pt.cex = 2.5, col=density.list$legend.col,
1371
                            cex=0.8, bty="n",
1372
                            y.intersp=1,
1373
                            x.intersp=1,
1374
                            yjust=1, xjust=0, xpd=TRUE
1375
                        )
1376
                        
1377
            }
1378
            if(file.output) dev.off()
1379
            
1380
        }
1381
    }
1382
        
1383
    if("q" %in% plot.type){
1384
        #print("Starting QQ-plot!",quote=F)
1385
        if(multracks){
1386
            if(file.output){
1387
                if(file=="jpg") jpeg(paste("Multracks.QQplot.",paste(taxa,collapse="."),".jpg",sep=""), width = R*2.5*dpi,height=5.5*dpi,res=dpi,quality = 100)
1388
                if(file=="pdf") pdf(paste("Multracks.QQplot.",paste(taxa,collapse="."),".pdf",sep=""), width = R*2.5,height=5.5)
1389
                if(file=="tiff")    tiff(paste("Multracks.QQplot.",paste(taxa,collapse="."),".tiff",sep=""), width = R*2.5*dpi,height=5.5*dpi,res=dpi)
1390
                par(mfcol=c(1,R),mar = c(0,1,4,1.5),oma=c(3,5,0,0),xpd=TRUE)
1391
            }else{
1392
                if(is.null(dev.list())) dev.new(width = 2.5*R, height = 5.5)
1393
                par(xpd=TRUE)
1394
            }
1395
            for(i in 1:R){
1396
                if(verbose) print(paste("Multracks_QQ Plotting ",taxa[i],"...",sep=""))     
1397
                P.values=as.numeric(Pmap[,i+2])
1398
                P.values=P.values[!is.na(P.values)]
1399
                if(LOG10){
1400
                    P.values=P.values[P.values>0]
1401
                    P.values=P.values[P.values<=1]
1402
                    N=length(P.values)
1403
                    P.values=P.values[order(P.values)]
1404
                }else{
1405
                    N=length(P.values)
1406
                    P.values=P.values[order(P.values,decreasing=TRUE)]
1407
                }
1408
                p_value_quantiles=(1:length(P.values))/(length(P.values)+1)
1409
                log.Quantiles <- -log10(p_value_quantiles)
1410
                if(LOG10){
1411
                    log.P.values <- -log10(P.values)
1412
                }else{
1413
                    log.P.values <- P.values
1414
                }
1415
                
1416
                #calculate the confidence interval of QQ-plot
1417
                if(conf.int){
1418
                    N1=length(log.Quantiles)
1419
                    c95 <- rep(NA,N1)
1420
                    c05 <- rep(NA,N1)
1421
                    for(j in 1:N1){
1422
                        xi=ceiling((10^-log.Quantiles[j])*N)
1423
                        if(xi==0)xi=1
1424
                        c95[j] <- qbeta(0.95,xi,N-xi+1)
1425
                        c05[j] <- qbeta(0.05,xi,N-xi+1)
1426
                    }
1427
                    index=length(c95):1
1428
                }else{
1429
                    c05 <- 1
1430
                    c95 <- 1
1431
                }
1432
                
1433
                YlimMax <- max(floor(max(max(-log10(c05)), max(-log10(c95)))+1), floor(max(log.P.values)+1))
1434
                plot(NULL, xlim = c(0,floor(max(log.Quantiles)+1)), axes=FALSE, cex.axis=cex.axis, cex.lab=1.2,ylim=c(0,YlimMax),xlab ="", ylab="", main = taxa[i])
1435
                axis(1, at=seq(0,floor(max(log.Quantiles)+1),ceiling((max(log.Quantiles)+1)/10)), labels=seq(0,floor(max(log.Quantiles)+1),ceiling((max(log.Quantiles)+1)/10)), cex.axis=cex.axis)
1436
                axis(2, at=seq(0,YlimMax,ceiling(YlimMax/10)), labels=seq(0,YlimMax,ceiling(YlimMax/10)), cex.axis=cex.axis)
1437
                
1438
                #plot the confidence interval of QQ-plot
1439
                if(conf.int)    polygon(c(log.Quantiles[index],log.Quantiles),c(-log10(c05)[index],-log10(c95)),col=conf.int.col,border=conf.int.col)
1440
                
1441
                if(!is.null(threshold.col)){par(xpd=FALSE); abline(a = 0, b = 1, col = threshold.col[1],lwd=2); par(xpd=TRUE)}
1442
                points(log.Quantiles, log.P.values, col = col[1],pch=19,cex=cex[3])
1443
                if(!is.null(threshold)){
1444
                    if(sum(threshold!=0)==length(threshold)){
1445
                        thre.line=-log10(min(threshold))
1446
                        if(amplify==TRUE){
1447
                            thre.index=which(log.P.values>=thre.line)
1448
                            if(length(thre.index)!=0){
1449
                            
1450
                                #cover the points that exceed the threshold with the color "white"
1451
                                points(log.Quantiles[thre.index],log.P.values[thre.index], col = "white",pch=19,cex=cex[3])
1452
                                if(is.null(signal.col)){
1453
                                    points(log.Quantiles[thre.index],log.P.values[thre.index],col = col[1],pch=signal.pch[1],cex=signal.cex[1])
1454
                                }else{
1455
                                    points(log.Quantiles[thre.index],log.P.values[thre.index],col = signal.col[1],pch=signal.pch[1],cex=signal.cex[1])
1456
                                }
1457
                            }
1458
                        }
1459
                    }
1460
                }
1461
            }
1462
            if(box) box()
1463
            if(file.output) dev.off()
1464
            if(R > 1){
1465
                signal.col <- NULL
1466
                if(file.output){
1467
                    if(file=="jpg") jpeg(paste("Multraits.QQplot.",paste(taxa,collapse="."),".jpg",sep=""), width = 5.5*dpi,height=5.5*dpi,res=dpi,quality = 100)
1468
                    if(file=="pdf") pdf(paste("Multraits.QQplot.",paste(taxa,collapse="."),".pdf",sep=""), width = 5.5,height=5.5)
1469
                    if(file=="tiff")    tiff(paste("Multraits.QQplot.",paste(taxa,collapse="."),".tiff",sep=""), width = 5.5*dpi,height=5.5*dpi,res=dpi)
1470
                    par(mar = c(5,5,4,2),xpd=TRUE)
1471
                }else{
1472
                    dev.new(width = 5.5, height = 5.5)
1473
                    par(xpd=TRUE)
1474
                }
1475
                p_value_quantiles=(1:nrow(Pmap))/(nrow(Pmap)+1)
1476
                log.Quantiles <- -log10(p_value_quantiles)
1477
                                            
1478
                # calculate the confidence interval of QQ-plot
1479
                if((i == 1) & conf.int){
1480
                    N1=length(log.Quantiles)
1481
                    c95 <- rep(NA,N1)
1482
                    c05 <- rep(NA,N1)
1483
                    for(j in 1:N1){
1484
                        xi=ceiling((10^-log.Quantiles[j])*N)
1485
                        if(xi==0)xi=1
1486
                        c95[j] <- qbeta(0.95,xi,N-xi+1)
1487
                        c05[j] <- qbeta(0.05,xi,N-xi+1)
1488
                    }
1489
                    index=length(c95):1
1490
                }
1491
                
1492
                if(!conf.int){c05 <- 1; c95 <- 1}
1493
                
1494
                Pmap.min <- Pmap[,3:(R+2)]
1495
                YlimMax <- max(floor(max(max(-log10(c05)), max(-log10(c95)))+1), -log10(min(Pmap.min[Pmap.min > 0])))
1496
                plot(NULL, xlim = c(0,floor(max(log.Quantiles)+1)), axes=FALSE, cex.axis=cex.axis, cex.lab=1.2,ylim=c(0, floor(YlimMax+1)),xlab =expression(Expected~~-log[10](italic(p))), ylab = expression(Observed~~-log[10](italic(p))), main = "QQplot")
1497
                legend("topleft",taxa,col=t(col)[1:R],pch=19,text.font=6,box.col=NA)
1498
                axis(1, at=seq(0,floor(max(log.Quantiles)+1),ceiling((max(log.Quantiles)+1)/10)), labels=seq(0,floor(max(log.Quantiles)+1),ceiling((max(log.Quantiles)+1)/10)), cex.axis=cex.axis)
1499
                axis(2, at=seq(0,floor(YlimMax+1),ceiling((YlimMax+1)/10)), labels=seq(0,floor((YlimMax+1)),ceiling((YlimMax+1)/10)), cex.axis=cex.axis)
1500
                
1501
                # plot the confidence interval of QQ-plot
1502
                if(conf.int)    polygon(c(log.Quantiles[index],log.Quantiles),c(-log10(c05)[index],-log10(c95)),col=conf.int.col,border=conf.int.col)
1503
                
1504
                for(i in 1:R){
1505
                    if(verbose) print(paste("Multraits_QQ Plotting ",taxa[i],"...",sep=""))
1506
                    P.values=as.numeric(Pmap[,i+2])
1507
                    P.values=P.values[!is.na(P.values)]
1508
                    if(LOG10){
1509
                        P.values=P.values[P.values>=0]
1510
                        P.values=P.values[P.values<=1]
1511
                        N=length(P.values)
1512
                        P.values=P.values[order(P.values)]
1513
                    }else{
1514
                        N=length(P.values)
1515
                        P.values=P.values[order(P.values,decreasing=TRUE)]
1516
                    }
1517
                    if(LOG10){
1518
                        log.P.values <- -log10(P.values)
1519
                    }else{
1520
                        log.P.values <- P.values
1521
                    }
1522
1523
                        
1524
                    if((i == 1) & !is.null(threshold.col)){par(xpd=FALSE); abline(a = 0, b = 1, col = threshold.col[1],lwd=2); par(xpd=TRUE)}
1525
                    points(log.Quantiles, log.P.values, col = t(col)[i],pch=19,cex=cex[3])
1526
                        
1527
                    if(!is.null(threshold)){
1528
                        if(sum(threshold!=0)==length(threshold)){
1529
                            thre.line=-log10(min(threshold))
1530
                            if(amplify==TRUE){
1531
                                thre.index=which(log.P.values>=thre.line)
1532
                                if(length(thre.index)!=0){
1533
                                
1534
                                    # cover the points that exceed the threshold with the color "white"
1535
                                    points(log.Quantiles[thre.index],log.P.values[thre.index], col = "white",pch=19,cex=cex[3])
1536
                                    if(is.null(signal.col)){
1537
                                        points(log.Quantiles[thre.index],log.P.values[thre.index],col = t(col)[i],pch=signal.pch[1],cex=signal.cex[1])
1538
                                    }else{
1539
                                        points(log.Quantiles[thre.index],log.P.values[thre.index],col = signal.col[1],pch=signal.pch[1],cex=signal.cex[1])
1540
                                    }
1541
                                }
1542
                            }
1543
                        }
1544
                    }
1545
                }
1546
                if(box) box()
1547
                if(file.output) dev.off()
1548
            }
1549
        }else{
1550
            for(i in 1:R){
1551
                if(verbose) print(paste("Q_Q Plotting ",taxa[i],"...",sep=""))
1552
                if(file.output){
1553
                    if(file=="jpg") jpeg(paste("QQplot.",taxa[i],".jpg",sep=""), width = 5.5*dpi,height=5.5*dpi,res=dpi,quality = 100)
1554
                    if(file=="pdf") pdf(paste("QQplot.",taxa[i],".pdf",sep=""), width = 5.5,height=5.5)
1555
                    if(file=="tiff")    tiff(paste("QQplot.",taxa[i],".tiff",sep=""), width = 5.5*dpi,height=5.5*dpi,res=dpi)
1556
                    par(mar = c(5,5,4,2),xpd=TRUE)
1557
                }else{
1558
                    if(is.null(dev.list())) dev.new(width = 5.5, height = 5.5)
1559
                    par(xpd=TRUE)
1560
                }
1561
                P.values=as.numeric(Pmap[,i+2])
1562
                P.values=P.values[!is.na(P.values)]
1563
                if(LOG10){
1564
                    P.values=P.values[P.values>0]
1565
                    P.values=P.values[P.values<=1]
1566
                    N=length(P.values)
1567
                    P.values=P.values[order(P.values)]
1568
                }else{
1569
                    N=length(P.values)
1570
                    P.values=P.values[order(P.values,decreasing=TRUE)]
1571
                }
1572
                p_value_quantiles=(1:length(P.values))/(length(P.values)+1)
1573
                log.Quantiles <- -log10(p_value_quantiles)
1574
                if(LOG10){
1575
                    log.P.values <- -log10(P.values)
1576
                }else{
1577
                    log.P.values <- P.values
1578
                }
1579
                
1580
                #calculate the confidence interval of QQ-plot
1581
                if(conf.int){
1582
                    N1=length(log.Quantiles)
1583
                    c95 <- rep(NA,N1)
1584
                    c05 <- rep(NA,N1)
1585
                    for(j in 1:N1){
1586
                        xi=ceiling((10^-log.Quantiles[j])*N)
1587
                        if(xi==0)xi=1
1588
                        c95[j] <- qbeta(0.95,xi,N-xi+1)
1589
                        c05[j] <- qbeta(0.05,xi,N-xi+1)
1590
                    }
1591
                    index=length(c95):1
1592
                }else{
1593
                    c05 <- 1
1594
                    c95 <- 1
1595
                }
1596
                YlimMax <- max(floor(max(max(-log10(c05)), max(-log10(c95)))+1), floor(max(log.P.values)+1))
1597
                plot(NULL, xlim = c(0,floor(max(log.Quantiles)+1)), axes=FALSE, cex.axis=cex.axis, cex.lab=1.2,ylim=c(0,YlimMax),xlab =expression(Expected~~-log[10](italic(p))), ylab = expression(Observed~~-log[10](italic(p))), main = paste("QQplot of",taxa[i]))
1598
                axis(1, at=seq(0,floor(max(log.Quantiles)+1),ceiling((max(log.Quantiles)+1)/10)), labels=seq(0,floor(max(log.Quantiles)+1),ceiling((max(log.Quantiles)+1)/10)), cex.axis=cex.axis)
1599
                axis(2, at=seq(0,YlimMax,ceiling(YlimMax/10)), labels=seq(0,YlimMax,ceiling(YlimMax/10)), cex.axis=cex.axis)
1600
                
1601
                #plot the confidence interval of QQ-plot
1602
                if(conf.int)    polygon(c(log.Quantiles[index],log.Quantiles),c(-log10(c05)[index],-log10(c95)),col=conf.int.col,border=conf.int.col)
1603
                
1604
                if(!is.null(threshold.col)){par(xpd=FALSE); abline(a = 0, b = 1, col = threshold.col[1],lwd=2); par(xpd=TRUE)}
1605
                points(log.Quantiles, log.P.values, col = col[1],pch=19,cex=cex[3])
1606
                
1607
                if(!is.null(threshold)){
1608
                    if(sum(threshold!=0)==length(threshold)){
1609
                        thre.line=-log10(min(threshold))
1610
                        if(amplify==TRUE){
1611
                            thre.index=which(log.P.values>=thre.line)
1612
                            if(length(thre.index)!=0){
1613
                            
1614
                                #cover the points that exceed the threshold with the color "white"
1615
                                points(log.Quantiles[thre.index],log.P.values[thre.index], col = "white",pch=19,cex=cex[3])
1616
                                if(is.null(signal.col)){
1617
                                    points(log.Quantiles[thre.index],log.P.values[thre.index],col = col[1],pch=signal.pch[1],cex=signal.cex[1])
1618
                                }else{
1619
                                    points(log.Quantiles[thre.index],log.P.values[thre.index],col = signal.col[1],pch=signal.pch[1],cex=signal.cex[1])
1620
                                }
1621
                            }
1622
                        }
1623
                    }
1624
                }
1625
                if(box) box()
1626
                if(file.output) dev.off()
1627
            }
1628
        }
1629
    }
1630
    if(file.output & verbose)   print(paste("Plots are stored in: ", getwd(), sep=""))
1631
}