SummaryToSqlite.scala 1.93 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
import nl.lumc.sasc.biopet.utils.summary.db.{Libraries, Samples}
Peter van 't Hof's avatar
Peter van 't Hof committed
6
7
8
import slick.driver.H2Driver.api._

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

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

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

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

  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
28
      c.copy(outputSqlite = x)
29
    } text "Output hdf5 file"
Peter van 't Hof's avatar
Peter van 't Hof committed
30
31
32
    opt[Unit]('f', "force") action { (x, c) =>
      c.copy(force = true)
    } text "If database already exist it will be moved"
33
34
35
36
37
  }

  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
38
    logger.info("Start")
39
40
41

    val jsonMap = ConfigUtils.fileToConfigMap(cmdArgs.inputJson)

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

Peter van 't Hof's avatar
Peter van 't Hof committed
47
    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
48

Peter van 't Hof's avatar
Peter van 't Hof committed
49
50
51
52
53
54
55
56
57
    try {
      val samples = TableQuery[Samples]
      val libraries = TableQuery[Libraries]

      val setup = DBIO.seq(
        (samples.schema ++ libraries.schema).create
      )
      val setupFuture = db.run(setup)
      Await.result(setupFuture, Duration.Inf)
Peter van 't Hof's avatar
Peter van 't Hof committed
58
    } finally db.close
Peter van 't Hof's avatar
Peter van 't Hof committed
59
    logger.info("Done")
60
61
62
  }

}