|
| 1 | +#!/usr/bin/env Rscript |
| 2 | + |
| 3 | +# One way to run this script is: |
| 4 | +# cat makeGraph.R | R --slave --args <*_ratio.txt> [<*_BAF.txt>] |
| 5 | +# Ploidy value will be inferred from the ratio file |
| 6 | + |
| 7 | + |
| 8 | +args <- commandArgs() |
| 9 | + |
| 10 | +BAFfileInd = 0; |
| 11 | +ratioFileInd = 0; |
| 12 | + |
| 13 | +#find which argument is Ratio.txt and which BAF.txt: |
| 14 | +for (i in c(1:length(args))) { |
| 15 | + if (length(grep("ratio.txt", args[i]))) { |
| 16 | + ratioFileInd = i; |
| 17 | + } |
| 18 | + if (length(grep("BAF", args[i]))) { |
| 19 | + BAFfileInd = i; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +#------------------------------------------------------ |
| 24 | + |
| 25 | +#plot .png for the _ratio.txt file: |
| 26 | + |
| 27 | +if (ratioFileInd) { |
| 28 | + |
| 29 | + #read the file and get ploidy value: |
| 30 | + |
| 31 | + ratio <-read.table(args[ratioFileInd], header=TRUE); |
| 32 | + ratio<-data.frame(ratio) |
| 33 | + ploidy = median (ratio$CopyNumber[which(ratio$MedianRatio>0.8 & ratio$MedianRatio<1.2)], na.rm = T) |
| 34 | + cat (c("INFO: Selected ploidy:", ploidy, "\n")) |
| 35 | + |
| 36 | + #------------------------------------------------------ |
| 37 | + |
| 38 | + #Plotting in the log scale: |
| 39 | + offset = 0.01 |
| 40 | + |
| 41 | + png(filename = paste(args[ratioFileInd],".log2.png",sep = ""), width = 1180, height = 1180, |
| 42 | + units = "px", pointsize = 20, bg = "white", res = NA) |
| 43 | + plot(1:10) |
| 44 | + op <- par(mfrow = c(5,5)) |
| 45 | + |
| 46 | + for (i in c(1:22,'X','Y')) { |
| 47 | + tt <- which(ratio$Chromosome==i) |
| 48 | + if (length(tt)>0) { |
| 49 | + plot(ratio$Start[tt],log2(ratio$Ratio[tt]+offset),xlab = paste ("position, chr",i),ylab = "normalized copy number profile (log2)",pch = ".",col = colors()[88]) |
| 50 | + tt <- which(ratio$Chromosome==i & ratio$CopyNumber>ploidy ) |
| 51 | + points(ratio$Start[tt],log2(ratio$Ratio[tt]+offset),pch = ".",col = colors()[136]) |
| 52 | + |
| 53 | + |
| 54 | + tt <- which(ratio$Chromosome==i & ratio$CopyNumber<ploidy & ratio$CopyNumber!= -1) |
| 55 | + points(ratio$Start[tt],log2(ratio$Ratio[tt]+offset),pch = ".",col = colors()[461]) |
| 56 | + tt <- which(ratio$Chromosome==i) |
| 57 | + |
| 58 | + #UNCOMMENT HERE TO SEE THE PREDICTED COPY NUMBER LEVEL: |
| 59 | + #points(ratio$Start[tt],log2(ratio$CopyNumber[tt]/ploidy+offset), pch = ".", col = colors()[24],cex=4) |
| 60 | + |
| 61 | + } |
| 62 | + tt <- which(ratio$Chromosome==i) |
| 63 | + |
| 64 | + #UNCOMMENT HERE TO SEE THE EVALUATED MEDIAN LEVEL PER SEGMENT: |
| 65 | + #points(ratio$Start[tt],log2(ratio$MedianRatio[tt]+offset), pch = ".", col = colors()[463],cex=4) |
| 66 | + |
| 67 | + } |
| 68 | + dev.off() |
| 69 | + |
| 70 | + #------------------------------------------------------ |
| 71 | + #Plotting in raw ratio values: |
| 72 | + png(filename = paste(args[ratioFileInd],".png",sep = ""), width = 1180, height = 1180, |
| 73 | + units = "px", pointsize = 20, bg = "white", res = NA) |
| 74 | + plot(1:10) |
| 75 | + op <- par(mfrow = c(5,5)) |
| 76 | + |
| 77 | + #replace high values of ratio with value "maxLevelToPlot": |
| 78 | + maxLevelToPlot <- 3 |
| 79 | + ratio$Ratio[ratio$Ratio>maxLevelToPlot]=maxLevelToPlot |
| 80 | + |
| 81 | + for (i in c(1:22,'X','Y')) { |
| 82 | + tt <- which(ratio$Chromosome==i) |
| 83 | + if (length(tt)>0) { |
| 84 | + plot(ratio$Start[tt],ratio$Ratio[tt]*ploidy,ylim = c(0,maxLevelToPlot*ploidy),xlab = paste ("position, chr",i),ylab = "normalized copy number profile",pch = ".",col = colors()[88]) |
| 85 | + tt <- which(ratio$Chromosome==i & ratio$CopyNumber>ploidy ) |
| 86 | + points(ratio$Start[tt],ratio$Ratio[tt]*ploidy,pch = ".",col = colors()[136]) |
| 87 | + |
| 88 | + tt <- which(ratio$Chromosome==i & ratio$Ratio==maxLevelToPlot & ratio$CopyNumber>ploidy) |
| 89 | + points(ratio$Start[tt],ratio$Ratio[tt]*ploidy,pch = ".",col = colors()[136],cex=4) |
| 90 | + |
| 91 | + tt <- which(ratio$Chromosome==i & ratio$CopyNumber<ploidy & ratio$CopyNumber!= -1) |
| 92 | + points(ratio$Start[tt],ratio$Ratio[tt]*ploidy,pch = ".",col = colors()[461]) |
| 93 | + tt <- which(ratio$Chromosome==i) |
| 94 | + |
| 95 | + #UNCOMMENT HERE TO SEE THE PREDICTED COPY NUMBER LEVEL: |
| 96 | + #points(ratio$Start[tt],ratio$CopyNumber[tt], pch = ".", col = colors()[24],cex=4) |
| 97 | + |
| 98 | + } |
| 99 | + tt <- which(ratio$Chromosome==i) |
| 100 | + |
| 101 | + #UNCOMMENT HERE TO SEE THE EVALUATED MEDIAN LEVEL PER SEGMENT: |
| 102 | + #points(ratio$Start[tt],ratio$MedianRatio[tt]*ploidy, pch = ".", col = colors()[463],cex=4) |
| 103 | + |
| 104 | + } |
| 105 | + dev.off() |
| 106 | + |
| 107 | +} else {cat ("WARNING: To get a .png image with copy number profile, you can provide as input a file with suffix 'ratio.txt'\n")} |
| 108 | + |
| 109 | + |
| 110 | +#------------------------------------------------------ |
| 111 | + |
| 112 | +#plot .png for the _BAF.txt file: |
| 113 | + |
| 114 | + |
| 115 | +if (BAFfileInd) { |
| 116 | + BAF <-read.table(args[BAFfileInd], header=TRUE); |
| 117 | + BAF<-data.frame(BAF) |
| 118 | + |
| 119 | + png(filename = paste(args[BAFfileInd],".png",sep = ""), width = 1180, height = 1180, |
| 120 | + units = "px", pointsize = 20, bg = "white", res = NA) |
| 121 | + plot(1:10) |
| 122 | + op <- par(mfrow = c(5,5)) |
| 123 | + |
| 124 | + for (i in c(1:22,'X','Y')) { |
| 125 | + tt <- which(BAF$Chromosome==i) |
| 126 | + if (length(tt)>0){ |
| 127 | + lBAF <-BAF[tt,] |
| 128 | + plot(lBAF$Position,lBAF$BAF,ylim = c(-0.1,1.1),xlab = paste ("position, chr",i),ylab = "BAF",pch = ".",col = colors()[1]) |
| 129 | + |
| 130 | + tt <- which(lBAF$A==0.5) |
| 131 | + points(lBAF$Position[tt],lBAF$BAF[tt],pch = ".",col = colors()[92]) |
| 132 | + tt <- which(lBAF$A!=0.5 & lBAF$A>=0) |
| 133 | + points(lBAF$Position[tt],lBAF$BAF[tt],pch = ".",col = colors()[62]) |
| 134 | + tt <- 1 |
| 135 | + pres <- 1 |
| 136 | + |
| 137 | + if (length(lBAF$A)>4) { |
| 138 | + for (j in c(2:(length(lBAF$A)-pres-1))) { |
| 139 | + if (lBAF$A[j]==lBAF$A[j+pres]) { |
| 140 | + tt[length(tt)+1] <- j |
| 141 | + } |
| 142 | + } |
| 143 | + points(lBAF$Position[tt],lBAF$A[tt],pch = ".",col = colors()[24],cex=4) |
| 144 | + points(lBAF$Position[tt],lBAF$B[tt],pch = ".",col = colors()[24],cex=4) |
| 145 | + } |
| 146 | + |
| 147 | + tt <- 1 |
| 148 | + pres <- 1 |
| 149 | + if (length(lBAF$FittedA)>4) { |
| 150 | + for (j in c(2:(length(lBAF$FittedA)-pres-1))) { |
| 151 | + if (lBAF$FittedA[j]==lBAF$FittedA[j+pres]) { |
| 152 | + tt[length(tt)+1] <- j |
| 153 | + } |
| 154 | + } |
| 155 | + points(lBAF$Position[tt],lBAF$FittedA[tt],pch = ".",col = colors()[463],cex=4) |
| 156 | + points(lBAF$Position[tt],lBAF$FittedB[tt],pch = ".",col = colors()[463],cex=4) |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + dev.off() |
| 163 | + |
| 164 | +} else {cat ("WARNING: To get a .png image with BAF profile, you can provide as input a file with suffix 'BAF.txt'\n")} |
0 commit comments