annotate ChipSeqRatioAnalysis.R @ 3:e320ef2d105a draft

Uploaded
author petr-novak
date Thu, 05 Sep 2019 09:04:56 -0400
parents
children 378565f5a875
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
1 #!/usr/bin/env Rscript
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
2 library(R2HTML, quietly=T)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
3 library(base64enc, quietly=T)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
4
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
5
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
6 htmlheader="
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
7 <html xmlns:mml=\"http://www.w3.org/1998/Math/MathML\">
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
8 <head>
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
9 <title> ChIP-Seq Mapper Output </title>
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
10 <style>
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
11 <!--
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
12 table { background:#FFFFFF;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
13 border:1px solid gray;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
14 border-collapse:collapse;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
15 color:#fff;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
16 font:normal 13px verdana, arial, helvetica, sans-serif;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
17 width: 100%;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
18
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
19 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
20 caption { border:1px solid #5C443A;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
21 color:#5C443A;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
22 font-weight:bold;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
23 font-size:20pt
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
24 padding:6px 4px 8px 0px;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
25 text-align:center;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
26
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
27 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
28 td, th { color:#363636;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
29 padding:.4em;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
30 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
31 tr { border:1px dotted gray;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
32 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
33 thead th, tfoot th { background:#5C443A;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
34 color:#FFFFFF;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
35 padding:3px 10px 3px 10px;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
36 text-align:left;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
37 text-transform:uppercase;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
38 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
39 tbody td a { color:#3636FF;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
40 text-decoration:underline;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
41 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
42 tbody td a:visited { color:gray;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
43 text-decoration:line-through;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
44 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
45 tbody td a:hover { text-decoration:underline;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
46 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
47 tbody th a { color:#3636FF;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
48 font-weight:normal;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
49 text-decoration:none;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
50 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
51 tbody th a:hover { color:#363636;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
52 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
53 tbody td+td+td+td a { background-image:url('bullet_blue.png');
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
54 background-position:left center;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
55 background-repeat:no-repeat;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
56 color:#FFFFFF;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
57 padding-left:15px;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
58 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
59 tbody td+td+td+td a:visited { background-image:url('bullet_white.png');
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
60 background-position:left center;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
61 background-repeat:no-repeat;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
62 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
63 tbody th, tbody td { text-align:left;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
64 vertical-align:top;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
65 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
66 tfoot td { background:#5C443A;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
67 color:#FFFFFF;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
68 padding-top:3px;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
69 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
70 .odd { background:#fff;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
71 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
72 tbody tr:hover { background:#EEEEEE;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
73 border:1px solid #03476F;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
74 color:#000000;
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
75 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
76 -->
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
77 </style>
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
78
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
79 </head>
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
80
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
81 "
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
82
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
83
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
84 #arguments
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
85 args <- commandArgs(trailingOnly = TRUE)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
86 input <- args[1]
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
87 HTMLfile <- args[2]
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
88 threshld <- 2/(2+1)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
89 inputN=as.numeric(args[3])
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
90 chipN=as.numeric(args[4])
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
91 #dataframe preprocessing and table creation
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
92 df <- read.delim(input, comment.char="#")
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
93
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
94 df$"Ratio Chip/Input"=df$Chip_Hits/df$Input_Hits
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
95 df$"Normalized ratio Chip/Input"=(df$Chip_Hits/chipN)/(df$Input_Hits/inputN)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
96
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
97 df$"Ratio Chip/(Chip+Input)"=df$Chip_Hits/(df$Chip_Hits + df$Input_Hits)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
98 df$"Normalized ratio Chip/(Chip+Input)"=(df$Chip_Hits/chipN)/((df$Input_Hits/inputN)+(df$Chip_Hits/chipN))
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
99
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
100 outputTable = df[df$"Normalized ratio Chip/(Chip+Input)" > threshld,]
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
101 outputTable = outputTable[!is.na(outputTable$Cluster),]
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
102 save.image("tmp.RData") #Plot creation
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
103 pngfile <- tempfile()
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
104 png(pngfile, width = 1000, height = 1200, pointsize=20)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
105 par(mfrow=c(3,1))
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
106 lims=range(df$"Normalized ratio Chip/Input"[df$"Normalized ratio Chip/Input">0], finite = TRUE)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
107 suppressWarnings(plot(df$Cluster,df$"Normalized ratio Chip/Input", log="y", xlab="Cluster Nr.", ylab="Normalized ChiP/Seq ratio", pch=20, ylim=lims))
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
108 abline(h=1,col='#00000080', lwd = 2)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
109 abline(h=2,col='#FF000080', lwd = 2)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
110
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
111 lims=range(df$"Normalized ratio Chip/Input", finite = TRUE)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
112 suppressWarnings(plot(df$Cluster,df$"Normalized ratio Chip/Input", xlab="Cluster Nr.", ylab="Normalize ChiP/Seq ratio", pch=20, ylim=lims))
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
113 abline(h=1,col='#00000080', lwd = 2)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
114 abline(h=2,col='#FF000080', lwd = 2)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
115
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
116 suppressWarnings(plot(df$Cluster,df$"Normalized ratio Chip/(Chip+Input)", xlab="Cluster Nr.", ylab="Normalized Chip/(Chip+Input)", pch=20))
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
117 abline(h=0.5,col='#00000080', lwd = 2)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
118 abline(h=threshld,col='#FF000080', lwd = 2)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
119
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
120
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
121 dev.off()
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
122 graph <- paste('<img src="data:image/png;base64 ,',
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
123 base64encode(pngfile),
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
124 '" alt="image" />'
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
125 )
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
126
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
127 #HMTL report creation + writing final output
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
128 directory=dirname(HTMLfile)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
129 filename=basename(HTMLfile)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
130 ## create HTML header
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
131 cat(htmlheader, file = filename)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
132
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
133
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
134 HTML(graph, file=filename)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
135 if (nrow(outputTable)>0){
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
136 HTML(outputTable, file=filename, classtable = "dataframe", row.names=FALSE, Align='left')
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
137 }
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
138 HTMLEndFile(filename)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
139 file.rename(from=filename, to=HTMLfile)
e320ef2d105a Uploaded
petr-novak
parents:
diff changeset
140 write.table(df, file=input, sep="\t", row.names = FALSE)