PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/gqMicroarrays/R/report.r

https://bitbucket.org/mugqic/rpackages
R | 374 lines | 149 code | 84 blank | 141 comment | 8 complexity | 34b5964c6c15c60e8bb51bead712b7aa MD5 | raw file
  1. #' This function ensures directories and css exist
  2. #'
  3. #' @title Check dir existence for reporting
  4. #' @param report.dir coming soon
  5. #' @param data.dir coming soon
  6. #' @note coming soon
  7. #' @author Lefebvre F
  8. #' @seealso coming soon
  9. #' @references coming soon
  10. #' @keywords coming soon
  11. #' @export
  12. check.dirs <-function(report.dir,data.dir)
  13. {
  14. # Prepare where the data will be written
  15. if(!file.exists(file.path(report.dir)))
  16. {
  17. dir.create(file.path(report.dir))
  18. }
  19. files.dir = file.path(report.dir,"files")
  20. if(!file.exists(files.dir))
  21. {
  22. dir.create(files.dir)
  23. }
  24. #file.copy(data.dir,files.dir,recursive=TRUE)
  25. system(paste('cp -r ',data.dir,'/* ',files.dir,'/',sep=''))
  26. css.dir = file.path(files.dir,'css')
  27. rp.dir =system.file(file.path('templates','report_parts'),package = "gqMicroarrays")
  28. if(!file.exists(css.dir))
  29. {
  30. dir.create(css.dir)
  31. file.copy( file.path(rp.dir,'report.css'), file.path(css.dir) )
  32. file.copy( file.path(rp.dir,'R2HTML.css'), file.path(css.dir) )
  33. file.copy( file.path(rp.dir,'hwriter.css'), file.path(css.dir) )
  34. }
  35. }
  36. #' This function fetches html bits need for report contruction
  37. #'
  38. #' @title getHtmlBits
  39. #' @note coming soon
  40. #' @author Lefebvre F
  41. #' @seealso coming soon
  42. #' @references coming soon
  43. #' @keywords coming soon
  44. #' @export
  45. getHtmlBits <-function(data.dir=NULL)
  46. {
  47. dir = system.file(file.path('templates','report_parts')
  48. ,package = "gqMicroarrays")
  49. #dir = '~/github/local/Lef_AT_GenomeQuebec/gqMicroarrays/inst/templates/report_parts/'
  50. #warning('TEMP html bits for devel')
  51. html = list(
  52. "head" = readLines(file.path(dir,'head.html'))
  53. ,"annotations" = readLines(file.path(dir,'annotations.html'))
  54. ,"removals" = readLines(file.path(dir,'removals.html'))
  55. ,"preprocessing" = readLines(file.path(dir,'preprocessing.html'))
  56. ,"exploratory" = readLines(file.path(dir,'exploratory.html'))
  57. ,"dgea" = readLines(file.path(dir,'dgea.html'))
  58. )
  59. return(html)
  60. }
  61. #' QC and expl functions need similar input and this operation needs to be repeated often This is just a general function to
  62. #' turn an object into a matrix and rename it's columns.
  63. #' @title Create report based on the content of the 'database'
  64. #' @param path
  65. #' @author Lefebvre F
  66. #' @export
  67. createReport <- function(
  68. path=db.path,
  69. params = getParams(path),
  70. gene.sets.db.alias = params[['gene.sets.db.alias']],
  71. report.dir = params[['report.dir']],
  72. report.title = params[['title']]
  73. #pdata = getPdata(path),
  74. #eset = getEset(path),
  75. #fdata = getFdata(params[["platform.alias"]]),
  76. )
  77. {
  78. # path=db.path
  79. if(!is.null( gene.sets.db.alias ))
  80. {
  81. gene.sets = getGeneSets(gene.sets.db.alias,as.list = TRUE)
  82. }else{gene.sets=NULL}
  83. # Prepare the report location
  84. check.dirs(report.dir, path)
  85. # Report Heading
  86. html = getHtmlBits(path)$head # report header
  87. html = gsub('TITLE',"Microarray Data Analysis",html) # Replace title
  88. html = gsub('PROJECT_NAME',report.title,html) # Replace project name
  89. html = gsub('TIMESTAMP',Sys.time(),html) # timestamp
  90. html = c( html,report.annotations(path,report.dir=report.dir) )# The Annotations section
  91. html = c( html,report.removals(path) )# Removals
  92. html = c( html,report.preprocessing(path) )# preprocessing
  93. html = c( html,report.exploratory(path) )# exploratory
  94. html = c( html,report.dgea(path,report.dir=report.dir) )# differential
  95. html = c(html,paste("<br><i>Analysis performed by ",params[['author']],", on ", format(Sys.time(), "%Y-%m-%d %X"),".</i>",sep='')) # report creation info
  96. # Report Tweaking
  97. pipeline.type = getSupportedPlatforms()[params[['platform.alias']],'pipeline.type']
  98. if( pipeline.type %in% c("methylation" , "MeDIP"))
  99. {
  100. html = gsub('expression','methylation',html)
  101. }
  102. writeLines(html,file.path(report.dir,"index.html"))
  103. # Copy markdown template to the current dir
  104. #TEMP md.fn = system.file(file.path("templates",'report.Rmd'),package = "gqMicroarrays")
  105. # md.fn = '~/github/local/Lef_AT_GenomeQuebec/gqMicroarrays/inst/templates/report.Rmd'# TODO TEMP TEMP for devel
  106. # file.copy(md.fn, '.', overwrite=TRUE)
  107. #########################
  108. ##########################
  109. # knit2html("report.Rmd")
  110. # browseURL(file.path("report.html"))
  111. # Here we will make a copy of the data in a folder, without the .Rdata
  112. # dir.create(params[['report.dir']])
  113. # file.copy('report.html',file.path(params[['report.dir']],'report.html'),overwrite=TRUE)
  114. # file.copy(path,file.path(params[['report.dir']]),overwrite=TRUE,recursive=TRUE)
  115. # TODO: figure out how not to copy .Rdata, or alt. just tar the result so as to not use too much space
  116. # # First, read the entire database to memory
  117. }
  118. #' report dgea
  119. #'
  120. #'
  121. #' coming soon
  122. #'
  123. #' @param report.dir coming soon
  124. #' @param data.dir coming soon
  125. #' @param dgea.items coming soon
  126. #' @param dgea.params coming soon
  127. #' @param third.id coming soon
  128. #' @note coming soon
  129. #' @author Lefebvre F
  130. #' @seealso coming soon
  131. #' @references coming soon
  132. #' @keywords coming soon
  133. #' @export
  134. report.dgea <- function(path,report.dir){
  135. html = getHtmlBits()$dgea
  136. params = getParams(path)
  137. fits = getFits(path) # blargh. This will be memory prohibitive with 450k I assure you. Function differential should write the necessary data as text beforehand.
  138. # Model name/description
  139. html = gsub( '_MODELNAME_', fits$model.name,html)
  140. # variables
  141. html = gsub('_VARIABLES_', paste(fits$variables,collapse=', '),html)
  142. # Replication
  143. # design
  144. write.csv(fits$design,file=file.path(report.dir,'files','differential','design.csv'),row.names=TRUE)
  145. # cont.matrix
  146. write.csv(fits$cont.matrix,file=
  147. file.path(report.dir,'files','differential','cont_matrix.csv'),row.names=TRUE)
  148. # full results file
  149. # Summary table
  150. summary = fits$summary.html
  151. # case of GSEA or not.
  152. summary = fits$summary.html
  153. if(is.null(params[["gene.sets.db.alias"]])){summary[['Gene Sets/Pathways']]=NULL}
  154. html = gsub('_SUMMARY_',
  155. hwrite(summary,row.names=FALSE,br=TRUE,center=TRUE
  156. ,row.bgcolor=c('#ffffaa',rep('#f1ecff',times=nrow(summary))))
  157. ,html)
  158. html = gsub(paste('href=\\"',path,sep=''),'href=\\"files',html)
  159. return(html)
  160. }
  161. #'
  162. #'
  163. #' coming soon
  164. #'
  165. #' @param report.dir coming soon
  166. #' @param data.dir coming soon
  167. #' @note coming soon
  168. #' @author Lefebvre F
  169. #' @seealso coming soon
  170. #' @references coming soon
  171. #' @keywords coming soon
  172. #' @export
  173. report.exploratory <- function(path)
  174. {
  175. html = getHtmlBits(data.dir)$exploratory
  176. load(file.path(path,'exploratory','index.RData'))
  177. expl.figs = index$index.html
  178. expl.figs$File = gsub(paste('\\"',path,sep=''),'\\"files',expl.figs$File)
  179. html = gsub('_EXPLORATORYTABLE_'
  180. ,hwrite(expl.figs,row.names=FALSE,br=TRUE,center=TRUE
  181. ,row.bgcolor=c('#ffffaa',rep('#f1ecff',times=nrow(expl.figs))))
  182. ,html)
  183. return(html)
  184. }
  185. #' report preprocessing
  186. #'
  187. #'
  188. #' coming soon
  189. #'
  190. #' @param report.dir coming soon
  191. #' @param data.dir coming soon
  192. #' @note coming soon
  193. #' @author Lefebvre F
  194. #' @seealso coming soon
  195. #' @references coming soon
  196. #' @keywords coming soon
  197. #' @export
  198. report.preprocessing <- function(path)
  199. {
  200. # path = db.path
  201. params = getParams(path)
  202. eset = getEset(path)
  203. load(file.path(path,'preprocess','description.RData'))
  204. html = getHtmlBits()$preprocessing
  205. # Insert description
  206. html = gsub('_PPDESCRIPTION_',description,html)
  207. # Write the paramerter values
  208. html = gsub('_NPROBES_' ,nrow(eset),html)
  209. html = gsub('_NSAMPLES_' ,ncol(eset),html)
  210. return(html)
  211. }
  212. #' report removals
  213. #'
  214. #'
  215. #' coming soon
  216. #'
  217. #' @param report.dir coming soon
  218. #' @param data.dir coming soon
  219. #' @param removal.items coming soon
  220. #' @param third.id coming soon
  221. #' @note coming soon
  222. #' @author Lefebvre F
  223. #' @seealso coming soon
  224. #' @references coming soon
  225. #' @keywords coming soon
  226. #' @export
  227. report.removals <- function(path)
  228. {
  229. load(file.path(path,'qc','index.RData'))
  230. qc.figs = index$index.html
  231. qc.figs$File = gsub(paste('\\"',path,sep=''),'\\"files',qc.figs$File) # fix URL
  232. outliers = index$outliers
  233. html = getHtmlBits(data.dir)$removals
  234. # Load the qc figs summary, don't forget to fix URLs
  235. html = gsub('_QCFIGSTABLE_ '
  236. ,hwrite(qc.figs,row.names=FALSE,br=TRUE,center=TRUE
  237. ,row.bgcolor=c('#ffffaa',rep('#f1ecff',times=nrow(qc.figs)))
  238. ),html)
  239. if(nrow(outliers)>0){
  240. html = gsub('_OUTLIERSTABLE_'
  241. ,hwrite(outliers,row.names=FALSE,br=TRUE,center=TRUE
  242. ,row.bgcolor=c('#ffffaa',rep('#f1ecff',times=nrow(outliers)))
  243. ),html)
  244. }else{
  245. html = gsub('_OUTLIERSTABLE_','No outliers!',html)
  246. }
  247. # html = hwrite(df,row.names=FALSE,br=TRUE,center=TRUE,row.bgcolor=c('#ffffaa',rep('#f1ecff',times=ncol(fit2)),'#ffbbaa'))
  248. # unhybridized samples
  249. # samples missing as a consequence of array removal (hyridizaation could be reattempted)
  250. # sucessful samples
  251. # full annotation : the usual samples annotation
  252. # check if works with empmty data frames !!
  253. return(html)
  254. }
  255. #' This function creates report files and returns the html of the annotation and raw data section
  256. #'
  257. #'
  258. #' coming soon
  259. #'
  260. #' @param report.dir coming soon
  261. #' @param data.dir coming soon
  262. #' @param annots.items coming soon
  263. #' @param third.id coming soon
  264. #' @note coming soon
  265. #' @author Lefebvre F
  266. #' @seealso coming soon
  267. #' @references coming soon
  268. #' @keywords coming soon
  269. #' @export
  270. report.annotations <-function(path,report.dir)
  271. {
  272. params = getParams(path)
  273. html = getHtmlBits()$annotations
  274. html = gsub('_PLATFORMTYPE_',getSupportedPlatforms()[params[['platform.alias']],'pipeline.type'],html)
  275. html = gsub('_PLATFORM_',getSupportedPlatforms()[params[['platform.alias']],'Platform'],html)
  276. html = gsub('_PROBESOURCE_',getSupportedPlatforms()[params[['platform.alias']],'probe.annotation.source'],html)
  277. # Produce arrays.csv
  278. pdata = getPdata(path)
  279. pdata[['FilePath']] = NULL
  280. write.csv(pdata,file=file.path(report.dir,'files','arrays.csv'),row.names=FALSE)
  281. fdata = getFdata(params[["platform.alias"]])
  282. write.csv(fdata,file=file.path(report.dir,'files','probes.csv'),row.names=FALSE)
  283. if(!is.null(params[["gene.sets.db.alias"]]))
  284. {
  285. gene.sets = getGeneSets(params[["gene.sets.db.alias"]],as.list=FALSE)
  286. write.csv(gene.sets,file=file.path(report.dir,'files','gene_sets.csv'),row.names=FALSE)
  287. html=gsub('_GENESETDB_',params[["gene.sets.db.alias"]],html)
  288. }else{html=gsub('_GENESETDB_','none',html)}
  289. return(html)
  290. }