| 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] | 
|  | 34 threshld <- 2/(2+1) | 
|  | 35 inputN=as.numeric(args[3]) | 
|  | 36 chipN=as.numeric(args[4]) | 
|  | 37                                         #dataframe preprocessing and table creation | 
|  | 38 df <- read.delim(input, comment.char="#") | 
|  | 39 | 
|  | 40 df$"Ratio Chip/Input"=df$Chip_Hits/df$Input_Hits | 
|  | 41 df$"Normalized ratio Chip/Input"=(df$Chip_Hits/chipN)/(df$Input_Hits/inputN) | 
|  | 42 | 
|  | 43 df$"Ratio Chip/(Chip+Input)"=df$Chip_Hits/(df$Chip_Hits + df$Input_Hits) | 
|  | 44 df$"Normalized ratio Chip/(Chip+Input)"=(df$Chip_Hits/chipN)/((df$Input_Hits/inputN)+(df$Chip_Hits/chipN)) | 
|  | 45 | 
| 5 | 46 outputTable = df[df$"Normalized ratio Chip/(Chip+Input)" > threshld, | 
|  | 47                  ] | 
|  | 48 outputTable = outputTable[!is.na(outputTable$Cluster), | 
|  | 49                           c('Cluster',	'Chip_Hits',	'Input_Hits', | 
|  | 50                             'Normalized ratio Chip/Input','Normalized ratio Chip/(Chip+Input)')] | 
| 3 | 51 save.image("tmp.RData")                                        #Plot creation | 
|  | 52 pngfile <- tempfile() | 
|  | 53 png(pngfile, width = 1000, height = 1200, pointsize=20) | 
| 5 | 54 par(mfrow=c(2,1)) | 
| 3 | 55 lims=range(df$"Normalized ratio Chip/Input"[df$"Normalized ratio Chip/Input">0], finite = TRUE) | 
| 6 | 56 suppressWarnings(plot(df$Cluster,df$"Normalized ratio Chip/Input", log="y", xlab="Cluster Nr.", ylab="Normalized ChiP/Input ratio", pch=20, ylim=lims)) | 
| 3 | 57 abline(h=1,col='#00000080', lwd = 2) | 
|  | 58 abline(h=2,col='#FF000080', lwd = 2) | 
|  | 59 | 
|  | 60 | 
|  | 61 suppressWarnings(plot(df$Cluster,df$"Normalized ratio Chip/(Chip+Input)", xlab="Cluster Nr.", ylab="Normalized Chip/(Chip+Input)", pch=20)) | 
|  | 62 abline(h=0.5,col='#00000080', lwd = 2) | 
|  | 63 abline(h=threshld,col='#FF000080', lwd = 2) | 
|  | 64 | 
|  | 65 | 
|  | 66 dev.off() | 
|  | 67 graph <- paste('<img src="data:image/png;base64 ,', | 
|  | 68                base64encode(pngfile), | 
|  | 69                '" alt="image" />' | 
|  | 70 ) | 
|  | 71 | 
|  | 72                                         #HMTL report creation + writing final output | 
|  | 73 directory=dirname(HTMLfile) | 
|  | 74 filename=basename(HTMLfile) | 
|  | 75 ## create HTML header | 
|  | 76 cat(htmlheader, file = filename) | 
|  | 77 | 
|  | 78 | 
|  | 79 HTML(graph, file=filename) | 
|  | 80 if (nrow(outputTable)>0){ | 
| 5 | 81   HTML(outputTable, file=filename, classtable = "dataframe", | 
|  | 82        row.names=FALSE, align='left', caption="Clusters with Normalized ChIP/Input ratio > 2", captionalign="top") | 
| 3 | 83 } | 
|  | 84 HTMLEndFile(filename) | 
| 6 | 85 # file.rename(from=filename, to=HTMLfile) | 
|  | 86 system(sprintf("cp -r ./%s %s", filename, HTMLfile)) | 
| 3 | 87 write.table(df, file=input, sep="\t", row.names = FALSE) |