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

3
4
import java.io.{FileInputStream, PrintWriter, File}
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
43
44
/**
 * 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 = {
    val writer = new PrintWriter(out)

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    val bla = for (((name, sampleId, libraryId), summarizables) <- summaryQScript.summarizables;
         summarizable <- summarizables) yield {
      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))
          case _ => map
        })))
        case _ => map
      }, (v1:Any, v2:Any, key:String) => summarizable.resolveSummaryConflict(v1, v2, key))
    }
    bla.foldRight(Map[String, Any]())((a, b) => ConfigUtils.mergeMaps(a._1, b, a._2))

    //TODO: QScript merging

61
62
    writer.close()
  }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

  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)
  }
80
}