ReportBuilder.scala 4.29 KB
Newer Older
1
2
3
4
package nl.lumc.sasc.biopet.core.report

import java.io.{ PrintWriter, File }

5
import nl.lumc.sasc.biopet.core.{ BiopetJavaCommandLineFunction, ToolCommand }
6
import nl.lumc.sasc.biopet.core.summary.Summary
7
import org.broadinstitute.gatk.utils.commandline.Input
8
import org.fusesource.scalate.{ TemplateSource, TemplateEngine }
9
10
11
12
13
14

import scala.io.Source

/**
 * Created by pjvan_thof on 3/27/15.
 */
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
trait ReportBuilderExtension extends BiopetJavaCommandLineFunction {

  val builder: ReportBuilder

  @Input(required = true)
  var summaryFile: File = _

  var outputDir: File = _

  var args: Map[String, String] = Map()

  override def beforeGraph: Unit = {
    super.beforeGraph
    jobOutputFile = new File(outputDir, ".report.log.out")
    javaMainClass = builder.getClass.getName.takeWhile(_ != '$')
  }

  override def commandLine: String = {
    super.commandLine +
      required("--summary", summaryFile) +
      required("--outputDir", outputDir) +
      args.map(x => required(x._1, x._2)).mkString
  }
}

40
41
trait ReportBuilder extends ToolCommand {

42
  case class Args(summary: File = null, outputDir: File = null, pageArgs: Map[String, String] = Map()) extends AbstractArgs
43
44

  class OptParser extends AbstractOptParser {
45
    opt[File]('s', "summary") required () maxOccurs 1 valueName "<file>" action { (x, c) =>
46
47
      c.copy(summary = x)
    }
48
    opt[File]('o', "outputDir") required () maxOccurs 1 valueName "<file>" action { (x, c) =>
49
50
      c.copy(outputDir = x)
    }
51
52
53
    opt[Map[String, String]]('a', "args") action { (x, c) =>
      c.copy(pageArgs = c.pageArgs ++ x)
    }
54
55
56
57
58
59
60
61
  }

  private var setSummary: Summary = _

  final def summary = setSummary

  def pageArgs: Map[String, Any] = Map()

Peter van 't Hof's avatar
Peter van 't Hof committed
62
63
64
  private var done = 0
  private var total = 0

65
66
67
68
69
70
71
72
73
  def main(args: Array[String]): Unit = {
    logger.info("Start")

    val argsParser = new OptParser
    val cmdArgs: Args = argsParser.parse(args, Args()) getOrElse sys.exit(1)

    require(cmdArgs.outputDir.exists(), "Output dir does not exist")
    require(cmdArgs.outputDir.isDirectory, "Output dir is not a directory")

Peter van 't Hof's avatar
Peter van 't Hof committed
74
    logger.info("Write Base files")
75
    // Write css to output dir
76
    val cssDir = new File(cmdArgs.outputDir, "css")
77
78
    cssDir.mkdirs()
    val cssWriter = new PrintWriter(new File(cssDir, "biopet.css"))
79
    Source.fromInputStream(getClass.getResourceAsStream("/nl/lumc/sasc/biopet/core/report/biopet.css")).getLines().foreach(cssWriter.println)
80
81
    cssWriter.close()

Peter van 't Hof's avatar
Peter van 't Hof committed
82
    logger.info("Parsing summary")
83
84
    setSummary = new Summary(cmdArgs.summary)

Peter van 't Hof's avatar
Peter van 't Hof committed
85
86
87
    total = countPages(indexPage)
    logger.info(total + " pages to be generated")

Peter van 't Hof's avatar
Peter van 't Hof committed
88
    logger.info("Generate pages")
89
    generatePage(summary, indexPage, cmdArgs.outputDir,
90
91
      args = pageArgs ++ cmdArgs.pageArgs ++
        Map("summary" -> summary, "reportName" -> reportName, "indexPage" -> indexPage))
92

Peter van 't Hof's avatar
Peter van 't Hof committed
93
    logger.info(done + " Done")
94
95
96
97
98
99
  }

  def indexPage: ReportPage

  def reportName: String

Peter van 't Hof's avatar
Peter van 't Hof committed
100
101
102
103
  def countPages(page: ReportPage): Int = {
    page.subPages.map(x => countPages(x._2)).fold(1)(_ + _)
  }

104
105
106
107
108
109
  def generatePage(summary: Summary,
                   page: ReportPage,
                   outputDir: File,
                   path: List[String] = Nil,
                   args: Map[String, Any] = Map()): Unit = {

Peter van 't Hof's avatar
Peter van 't Hof committed
110
111
    val pageOutputDir = new File(outputDir, path.mkString(File.separator))
    pageOutputDir.mkdirs()
112
113
114
115
116
117
118
    val rootPath = "./" + Array.fill(path.size)("../").mkString("")
    val pageArgs = args ++ page.args ++
      Map("page" -> page,
        "path" -> path,
        "outputDir" -> pageOutputDir,
        "rootPath" -> rootPath
      )
119

Peter van 't Hof's avatar
Peter van 't Hof committed
120
121
122
123
    val output = ReportBuilder.renderTemplate("/nl/lumc/sasc/biopet/core/report/main.ssp",
      pageArgs ++ Map("args" -> pageArgs))

    val file = new File(pageOutputDir, "index.html")
124
125
126
127
128
    val writer = new PrintWriter(file)
    writer.println(output)
    writer.close()

    // Generating subpages
129
    for ((name, subPage) <- page.subPages.par) {
130
131
      generatePage(summary, subPage, outputDir, path ::: name :: Nil, pageArgs)
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
132
133
    done += 1
    if (done % 100 == 0) logger.info(done + " Done, " + (done.toDouble / total * 100) + "%")
134
135
  }
}
Peter van 't Hof's avatar
Peter van 't Hof committed
136
137
138
139
140

object ReportBuilder {

  protected val engine = new TemplateEngine()

141
  def renderTemplate(location: String, args: Map[String, Any]): String = {
142
    engine.layout(TemplateSource.fromFile(getClass.getResource(location).getPath), args)
Peter van 't Hof's avatar
Peter van 't Hof committed
143
144
  }
}