view ChipSeqRatioAnalysis.R @ 3:e320ef2d105a draft

Uploaded
author petr-novak
date Thu, 05 Sep 2019 09:04:56 -0400
parents
children 378565f5a875
line wrap: on
line source

#!/usr/bin/env Rscript
library(R2HTML, quietly=T)
library(base64enc, quietly=T)


htmlheader="
	 <html xmlns:mml=\"http://www.w3.org/1998/Math/MathML\">
  <head>
  <title> ChIP-Seq Mapper Output </title>
  <style>
  <!--
  table { background:#FFFFFF;
  border:1px solid gray;
  border-collapse:collapse;
  color:#fff;
  font:normal 13px verdana, arial, helvetica, sans-serif;
    width: 100%;

  }
  caption { border:1px solid #5C443A;
  color:#5C443A;
  font-weight:bold;
  font-size:20pt
  padding:6px 4px 8px 0px;
  text-align:center;
  
  }
  td, th { color:#363636;
  padding:.4em;
  }
  tr { border:1px dotted gray;
  }
  thead th, tfoot th { background:#5C443A;
  color:#FFFFFF;
  padding:3px 10px 3px 10px;
  text-align:left;
  text-transform:uppercase;
  }
  tbody td a { color:#3636FF;
  text-decoration:underline;
  }
  tbody td a:visited { color:gray;
  text-decoration:line-through;
  }
  tbody td a:hover { text-decoration:underline;
  }
  tbody th a { color:#3636FF;
  font-weight:normal;
  text-decoration:none;
  }
  tbody th a:hover { color:#363636;
  }
  tbody td+td+td+td a { background-image:url('bullet_blue.png');
  background-position:left center;
  background-repeat:no-repeat;
  color:#FFFFFF;
  padding-left:15px;
  }
  tbody td+td+td+td a:visited { background-image:url('bullet_white.png');
  background-position:left center;
  background-repeat:no-repeat;
  }
  tbody th, tbody td { text-align:left;
  vertical-align:top;
  }
  tfoot td { background:#5C443A;
  color:#FFFFFF;
  padding-top:3px;
  }
  .odd { background:#fff;
  }
  tbody tr:hover { background:#EEEEEE;
  border:1px solid #03476F;
  color:#000000;
  }
  -->
  </style>
  
  </head>
  
  "


                                        #arguments
args <- commandArgs(trailingOnly = TRUE)
input <- args[1]
HTMLfile <- args[2]
threshld <- 2/(2+1)
inputN=as.numeric(args[3])
chipN=as.numeric(args[4])
                                        #dataframe preprocessing and table creation
df <- read.delim(input, comment.char="#")

df$"Ratio Chip/Input"=df$Chip_Hits/df$Input_Hits
df$"Normalized ratio Chip/Input"=(df$Chip_Hits/chipN)/(df$Input_Hits/inputN)

df$"Ratio Chip/(Chip+Input)"=df$Chip_Hits/(df$Chip_Hits + df$Input_Hits)
df$"Normalized ratio Chip/(Chip+Input)"=(df$Chip_Hits/chipN)/((df$Input_Hits/inputN)+(df$Chip_Hits/chipN))

outputTable = df[df$"Normalized ratio Chip/(Chip+Input)" > threshld,]
outputTable = outputTable[!is.na(outputTable$Cluster),]
save.image("tmp.RData")                                        #Plot creation
pngfile <- tempfile()
png(pngfile, width = 1000, height = 1200, pointsize=20)
par(mfrow=c(3,1))
lims=range(df$"Normalized ratio Chip/Input"[df$"Normalized ratio Chip/Input">0], finite = TRUE)
suppressWarnings(plot(df$Cluster,df$"Normalized ratio Chip/Input", log="y", xlab="Cluster Nr.", ylab="Normalized ChiP/Seq ratio", pch=20, ylim=lims))
abline(h=1,col='#00000080', lwd = 2)
abline(h=2,col='#FF000080', lwd = 2)

lims=range(df$"Normalized ratio Chip/Input", finite = TRUE)
suppressWarnings(plot(df$Cluster,df$"Normalized ratio Chip/Input", xlab="Cluster Nr.", ylab="Normalize ChiP/Seq ratio", pch=20, ylim=lims))
abline(h=1,col='#00000080', lwd = 2)
abline(h=2,col='#FF000080', lwd = 2)

suppressWarnings(plot(df$Cluster,df$"Normalized ratio Chip/(Chip+Input)", xlab="Cluster Nr.", ylab="Normalized Chip/(Chip+Input)", pch=20))
abline(h=0.5,col='#00000080', lwd = 2)
abline(h=threshld,col='#FF000080', lwd = 2)


dev.off()
graph <- paste('<img src="data:image/png;base64 ,',
               base64encode(pngfile),
               '" alt="image" />'
)

                                        #HMTL report creation + writing final output
directory=dirname(HTMLfile)
filename=basename(HTMLfile)
## create HTML header
cat(htmlheader, file = filename)


HTML(graph, file=filename)
if (nrow(outputTable)>0){
    HTML(outputTable, file=filename, classtable = "dataframe", row.names=FALSE, Align='left')
}
HTMLEndFile(filename) 
file.rename(from=filename, to=HTMLfile)
write.table(df, file=input, sep="\t", row.names = FALSE)