Summary.scala 3.87 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Biopet is built on top of GATK Queue for building bioinformatic
 * pipelines. It is mainly intended to support LUMC SHARK cluster which is running
 * SGE. But other types of HPC that are supported by GATK Queue (such as PBS)
 * should also be able to execute Biopet tools and pipelines.
 *
 * Copyright 2014 Sequencing Analysis Support Core - Leiden University Medical Center
 *
 * Contact us at: sasc@lumc.nl
 *
 * A dual licensing mode is applied. The source code within this project that are
 * not part of GATK Queue is freely available for non-commercial use under an AGPL
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
16
17
18
19
20
21
22
package nl.lumc.sasc.biopet.core.summary

import java.io.File

import nl.lumc.sasc.biopet.utils.ConfigUtils

/**
23
24
 * This class can read in a summary and extract values from it
 *
25
26
27
28
29
 * Created by pjvan_thof on 3/26/15.
 */
class Summary(file: File) {
  val map = ConfigUtils.fileToConfigMap(file)

Peter van 't Hof's avatar
Peter van 't Hof committed
30
  /** List of all samples in the summary */
31
32
  lazy val samples: Set[String] = {
    ConfigUtils.getValueFromPath(map, List("samples")) match {
33
      case Some(s) => ConfigUtils.any2map(s).keySet
Peter van 't Hof's avatar
Peter van 't Hof committed
34
      case _       => Set()
35
36
37
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
38
  /** List of all libraries for each sample */
39
40
41
42
43
44
45
46
47
  lazy val libraries: Map[String, Set[String]] = {
    (for (sample <- samples) yield sample -> {
      ConfigUtils.getValueFromPath(map, List("samples", sample, "libraries")) match {
        case Some(libs) => ConfigUtils.any2map(libs).keySet
        case _          => Set[String]()
      }
    }).toMap
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
48
  /** getValue from on given nested path */
49
50
51
52
  def getValue(path: String*): Option[Any] = {
    ConfigUtils.getValueFromPath(map, path.toList)
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
53
  /** getValue from on given nested path with prefix "samples" -> [sampleId] */
54
55
56
57
  def getSampleValue(sampleId: String, path: String*): Option[Any] = {
    ConfigUtils.getValueFromPath(map, "samples" :: sampleId :: path.toList)
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
58
  /** Get values for all samples on given path with prefix "samples" -> [sampleId] */
59
60
61
62
  def getSampleValues(path: String*): Map[String, Option[Any]] = {
    (for (sample <- samples) yield sample -> getSampleValue(sample, path: _*)).toMap
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
63
  /** Executes given function for each sample */
64
65
66
67
  def getSampleValues(function: (Summary, String) => Option[Any]): Map[String, Option[Any]] = {
    (for (sample <- samples) yield sample -> function(this, sample)).toMap
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
68
  /** Get value on nested path with prefix "samples" -> [sampleId] -> "libraries" -> [libId] */
69
70
  def getLibraryValue(sampleId: String, libId: String, path: String*): Option[Any] = {
    ConfigUtils.getValueFromPath(map, "samples" :: sampleId :: "libraries" :: libId :: path.toList)
71
  }
72

Peter van 't Hof's avatar
Peter van 't Hof committed
73
  /** Get value on nested path with prefix depending is sampleId and/or libId is None or not */
Peter van 't Hof's avatar
Peter van 't Hof committed
74
  def getValue(sampleId: Option[String], libId: Option[String], path: String*): Option[Any] = {
75
76
77
78
79
80
81
    (sampleId, libId) match {
      case (Some(sample), Some(lib)) => getLibraryValue(sample, lib, path: _*)
      case (Some(sample), _)         => getSampleValue(sample, path: _*)
      case _                         => getValue(path: _*)
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
82
83
84
85
86
  /**
   * Get values for all libraries on a given path
   * @param path path to of value
   * @return (sampleId, libId) -> value
   */
87
88
89
90
91
92
  def getLibraryValues(path: String*): Map[(String, String), Option[Any]] = {
    (for (sample <- samples; lib <- libraries.getOrElse(sample, Set())) yield {
      (sample, lib) -> getLibraryValue(sample, lib, path: _*)
    }).toMap
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
93
94
95
96
97
  /**
   * Executes method for each library
   * @param function Function to execute
   * @return (sampleId, libId) -> value
   */
98
99
100
101
102
  def getLibraryValues(function: (Summary, String, String) => Option[Any]): Map[(String, String), Option[Any]] = {
    (for (sample <- samples; lib <- libraries.getOrElse(sample, Set())) yield {
      (sample, lib) -> function(this, sample, lib)
    }).toMap
  }
103
}