3
|
1 #!/usr/bin/env Rscript
|
|
2 library(R2HTML, quietly=T)
|
|
3 library(base64enc, quietly=T)
|
|
4
|
|
5
|
5
|
6 htmlheader=
|
|
7 " <html xmlns:mml=\"http://www.w3.org/1998/Math/MathML\">
|
3
|
8 <head>
|
|
9 <title> ChIP-Seq Mapper Output </title>
|
5
|
10 <style>
|
|
11 html,body{font-family:Verdana,sans-serif;font-size:15px;line-height:1.5}
|
3
|
12
|
5
|
13 table {
|
|
14 border-collapse: collapse;
|
|
15 border: 1px solid black;
|
|
16 width: 1000pt
|
|
17 }
|
|
18 table, th, td {
|
|
19 border: 1px solid black;
|
|
20 }
|
|
21 </style>
|
3
|
22
|
|
23 </head>
|
5
|
24
|
|
25
|
|
26
|
|
27 "
|
3
|
28
|
|
29
|
|
30 #arguments
|
|
31 args <- commandArgs(trailingOnly = TRUE)
|
|
32 input <- args[1]
|
|
33 HTMLfile <- args[2]
|
8
|
34 thr = 5
|
|
35 threshld <- thr/(thr+1)
|
|
36
|
3
|
37 inputN=as.numeric(args[3])
|
|
38 chipN=as.numeric(args[4])
|
|
39 #dataframe preprocessing and table creation
|
|
40 df <- read.delim(input, comment.char="#")
|
|
41
|
|
42 df$"Ratio Chip/Input"=df$Chip_Hits/df$Input_Hits
|
|
43 df$"Normalized ratio Chip/Input"=(df$Chip_Hits/chipN)/(df$Input_Hits/inputN)
|
|
44
|
|
45 df$"Ratio Chip/(Chip+Input)"=df$Chip_Hits/(df$Chip_Hits + df$Input_Hits)
|
|
46 df$"Normalized ratio Chip/(Chip+Input)"=(df$Chip_Hits/chipN)/((df$Input_Hits/inputN)+(df$Chip_Hits/chipN))
|
|
47
|
5
|
48 outputTable = df[df$"Normalized ratio Chip/(Chip+Input)" > threshld,
|
|
49 ]
|
|
50 outputTable = outputTable[!is.na(outputTable$Cluster),
|
|
51 c('Cluster', 'Chip_Hits', 'Input_Hits',
|
|
52 'Normalized ratio Chip/Input','Normalized ratio Chip/(Chip+Input)')]
|
3
|
53 save.image("tmp.RData") #Plot creation
|
|
54 pngfile <- tempfile()
|
|
55 png(pngfile, width = 1000, height = 1200, pointsize=20)
|
5
|
56 par(mfrow=c(2,1))
|
3
|
57 lims=range(df$"Normalized ratio Chip/Input"[df$"Normalized ratio Chip/Input">0], finite = TRUE)
|
6
|
58 suppressWarnings(plot(df$Cluster,df$"Normalized ratio Chip/Input", log="y", xlab="Cluster Nr.", ylab="Normalized ChiP/Input ratio", pch=20, ylim=lims))
|
3
|
59 abline(h=1,col='#00000080', lwd = 2)
|
8
|
60 abline(h=thr,col='#FF000080', lwd = 2)
|
3
|
61
|
|
62
|
|
63 suppressWarnings(plot(df$Cluster,df$"Normalized ratio Chip/(Chip+Input)", xlab="Cluster Nr.", ylab="Normalized Chip/(Chip+Input)", pch=20))
|
|
64 abline(h=0.5,col='#00000080', lwd = 2)
|
|
65 abline(h=threshld,col='#FF000080', lwd = 2)
|
|
66
|
|
67
|
|
68 dev.off()
|
|
69 graph <- paste('<img src="data:image/png;base64 ,',
|
|
70 base64encode(pngfile),
|
|
71 '" alt="image" />'
|
|
72 )
|
|
73
|
|
74 #HMTL report creation + writing final output
|
|
75 directory=dirname(HTMLfile)
|
|
76 filename=basename(HTMLfile)
|
|
77 ## create HTML header
|
|
78 cat(htmlheader, file = filename)
|
|
79
|
|
80
|
|
81 HTML(graph, file=filename)
|
|
82 if (nrow(outputTable)>0){
|
5
|
83 HTML(outputTable, file=filename, classtable = "dataframe",
|
8
|
84 row.names=FALSE, align='left', caption=paste("Clusters with Normalized ChIP/Input ratio >", thr), captionalign="top")
|
3
|
85 }
|
|
86 HTMLEndFile(filename)
|
6
|
87 # file.rename(from=filename, to=HTMLfile)
|
|
88 system(sprintf("cp -r ./%s %s", filename, HTMLfile))
|
3
|
89 write.table(df, file=input, sep="\t", row.names = FALSE)
|