SummaryToSqlite.scala 2.1 KB
Newer Older
1
2
3
4
package nl.lumc.sasc.biopet.tools

import java.io.File

Peter van 't Hof's avatar
Peter van 't Hof committed
5
6
7
import slick.driver.H2Driver.api._

import scala.concurrent.ExecutionContext.Implicits.global
8
9
import nl.lumc.sasc.biopet.utils.{ConfigUtils, ToolCommand}

Peter van 't Hof's avatar
Peter van 't Hof committed
10
11
12
import scala.concurrent.Await
import scala.concurrent.duration.Duration

13
14
15
/**
  * Created by pjvanthof on 26/01/2017.
  */
Peter van 't Hof's avatar
Peter van 't Hof committed
16
object SummaryToSqlite extends ToolCommand {
17
18

  case class Args(inputJson: File = null,
Peter van 't Hof's avatar
Peter van 't Hof committed
19
20
                  outputSqlite: File = null,
                  force: Boolean = false) extends AbstractArgs
21
22
23
24
25
26

  class OptParser extends AbstractOptParser {
    opt[File]('I', "inputJson") required () maxOccurs 1 valueName "<file>" action { (x, c) =>
      c.copy(inputJson = x)
    } text "Input json file"
    opt[File]('o', "outputHdf5") required () maxOccurs 1 valueName "<file>" action { (x, c) =>
Peter van 't Hof's avatar
Peter van 't Hof committed
27
      c.copy(outputSqlite = x)
28
    } text "Output hdf5 file"
Peter van 't Hof's avatar
Peter van 't Hof committed
29
30
31
    opt[Unit]('f', "force") action { (x, c) =>
      c.copy(force = true)
    } text "If database already exist it will be moved"
32
33
34
35
36
  }

  def main(args: Array[String]): Unit = {
    val argsParser = new OptParser
    val cmdArgs = argsParser.parse(args, Args()) getOrElse (throw new IllegalArgumentException)
Peter van 't Hof's avatar
Peter van 't Hof committed
37
    logger.info("Start")
38
39
40

    val jsonMap = ConfigUtils.fileToConfigMap(cmdArgs.inputJson)

Peter van 't Hof's avatar
Peter van 't Hof committed
41
    if (cmdArgs.outputSqlite.exists()) {
42
43
44
45
      if (cmdArgs.force) {
        logger.warn("Deleting old database")
        cmdArgs.outputSqlite.delete()
      } else throw new IllegalArgumentException(s"Db already exist: ${cmdArgs.outputSqlite}")
Peter van 't Hof's avatar
Peter van 't Hof committed
46
    }
47

Peter van 't Hof's avatar
Peter van 't Hof committed
48
    val db = Database.forURL(s"jdbc:sqlite:${cmdArgs.outputSqlite.getAbsolutePath}", driver = "org.sqlite.JDBC")
Peter van 't Hof's avatar
Peter van 't Hof committed
49

Peter van 't Hof's avatar
Peter van 't Hof committed
50
    try {
51
      import nl.lumc.sasc.biopet.utils.summary.db.Schema._
Peter van 't Hof's avatar
Peter van 't Hof committed
52
53

      val setup = DBIO.seq(
Peter van 't Hof's avatar
Peter van 't Hof committed
54
55
        (runs.schema ++ samples.schema ++
          samplesRuns.schema ++ libraries.schema ++
Peter van 't Hof's avatar
Peter van 't Hof committed
56
57
58
          librariesRuns.schema ++ pipelineNames.schema ++
          moduleNames.schema ++ stats.schema ++
          files.schema ++ executables.schema).create
Peter van 't Hof's avatar
Peter van 't Hof committed
59
60
61
      )
      val setupFuture = db.run(setup)
      Await.result(setupFuture, Duration.Inf)
Peter van 't Hof's avatar
Peter van 't Hof committed
62
    } finally db.close
Peter van 't Hof's avatar
Peter van 't Hof committed
63
    logger.info("Done")
64
65
66
  }

}