WriteSummary.scala 2.89 KB
Newer Older
1
2
package nl.lumc.sasc.biopet.core.summary

3
import java.io.{ FileInputStream, PrintWriter, File }
4
import java.security.MessageDigest
5
6

import nl.lumc.sasc.biopet.core.config.Configurable
7
import nl.lumc.sasc.biopet.utils.ConfigUtils
8
9
10
import org.broadinstitute.gatk.queue.function.{ QFunction, InProcessFunction }
import org.broadinstitute.gatk.utils.commandline.{ Output, Input }

11
12
13
import scala.collection.mutable
import scala.io.Source

14
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
40
41
42
/**
 * Created by pjvan_thof on 2/14/15.
 */
class WriteSummary(val root: Configurable) extends InProcessFunction with Configurable {
  this.analysisName = getClass.getSimpleName

  require(root.isInstanceOf[SummaryQScript], "root is not a SummaryQScript")

  val summaryQScript = root.asInstanceOf[SummaryQScript]

  @Input(doc = "deps", required = false)
  var deps: List[File] = Nil

  @Output(doc = "Summary output", required = true)
  var out: File = summaryQScript.summaryFile

  var md5sum: Boolean = config("summary_md5", default = true)
  //TODO: add more checksums types

  override def freezeFieldValues(): Unit = {
    for (q <- summaryQScript.summaryQScripts) deps :+= q.summaryFile
    for ((_, l) <- summaryQScript.summarizables; s <- l) s match {
      case f: QFunction => deps :+= f.firstOutput
      case _            =>
    }
    super.freezeFieldValues()
  }

  def run(): Unit = {
43
44
45
46
    val map = (for (
      ((name, sampleId, libraryId), summarizables) <- summaryQScript.summarizables;
      summarizable <- summarizables
    ) yield {
47
48
49
50
51
      val map = Map(name -> parseSummarizable(summarizable))

      (sampleId match {
        case Some(sampleId) => Map("samples" -> Map(sampleId -> (libraryId match {
          case Some(libraryId) => Map("libraries" -> Map(libraryId -> map))
52
          case _               => map
53
54
        })))
        case _ => map
55
56
      }, (v1: Any, v2: Any, key: String) => summarizable.resolveSummaryConflict(v1, v2, key))
    }).foldRight(Map[String, Any]())((a, b) => ConfigUtils.mergeMaps(a._1, b, a._2))
57

58
59
60
    val combinedMap = (for (qscript <- summaryQScript.summaryQScripts) yield {
      ConfigUtils.fileToConfigMap(qscript.summaryFile)
    }).foldRight(map)((a, b) => ConfigUtils.mergeMaps(a, b))
61

62
    val writer = new PrintWriter(out)
Peter van 't Hof's avatar
Peter van 't Hof committed
63
    writer.println(ConfigUtils.mapToJson(combinedMap).spaces4)
64
65
    writer.close()
  }
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

  def parseSummarizable(summarizable: Summarizable): Map[String, Map[String, Any]] = {
    Map("data" -> summarizable.summaryData, "files" -> parseFiles(summarizable.summaryFiles))
  }

  def parseFiles(files: Map[String, File]): Map[String, Map[String, Any]] = {
    for ((key, file) <- files) yield {
      val map: mutable.Map[String, Any] = mutable.Map()
      map += "path" -> file.getAbsolutePath
      if (md5sum) map += "md5" -> parseChechsum(SummaryQScript.md5sumCache(file))
      key -> map.toMap
    }
  }

  def parseChechsum(checksumFile: File): String = {
    Source.fromFile(checksumFile).getLines().toList.head.split(" ")(0)
  }
83
}