Summary.scala 4.12 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1 2 3 4 5 6 7 8 9 10
/**
 * 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
 *
11
 * A dual licensing mode is applied. The source code within this project is freely available for non-commercial use under an AGPL
Peter van 't Hof's avatar
Peter van 't Hof committed
12 13 14
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
15
package nl.lumc.sasc.biopet.utils.summary
16 17 18 19 20 21

import java.io.File

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

/**
22 23
 * This class can read in a summary and extract values from it
 *
24 25 26 27 28
 * 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
29
  /** List of all samples in the summary */
30 31
  lazy val samples: Set[String] = {
    ConfigUtils.getValueFromPath(map, List("samples")) match {
32
      case Some(s) => ConfigUtils.any2map(s).keySet
Peter van 't Hof's avatar
Peter van 't Hof committed
33
      case _       => Set()
34 35 36
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
37
  /** List of all libraries for each sample */
38 39 40 41 42 43 44 45 46
  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
47
  /** getValue from on given nested path */
48 49 50 51
  def getValue(path: String*): Option[Any] = {
    ConfigUtils.getValueFromPath(map, path.toList)
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
52
  /** getValue from on given nested path with prefix "samples" -> [sampleId] */
53 54 55 56
  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
57
  /** Get values for all samples on given path with prefix "samples" -> [sampleId] */
58 59 60 61
  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
62
  /** Executes given function for each sample */
63
  def getSampleValues[T](function: (Summary, String) => Option[T]): Map[String, Option[T]] = {
64 65 66
    (for (sample <- samples) yield sample -> function(this, sample)).toMap
  }

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

Peter van 't Hof's avatar
Peter van 't Hof committed
72
  /** 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
73
  def getValue(sampleId: Option[String], libId: Option[String], path: String*): Option[Any] = {
74 75 76 77 78 79 80
    (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
81 82
  /** Get value on nested path with prefix depending is sampleId and/or libId is None or not */
  def getValueAsArray(sampleId: Option[String], libId: Option[String], path: String*): Option[Array[Any]] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
83
    this.getValue(sampleId, libId, path: _*).map(ConfigUtils.any2list(_).toArray)
Peter van 't Hof's avatar
Peter van 't Hof committed
84 85
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
86 87 88 89 90
  /**
   * Get values for all libraries on a given path
   * @param path path to of value
   * @return (sampleId, libId) -> value
   */
91 92 93 94 95 96
  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
97 98 99 100 101
  /**
   * Executes method for each library
   * @param function Function to execute
   * @return (sampleId, libId) -> value
   */
102
  def getLibraryValues[T](function: (Summary, String, String) => Option[T]): Map[(String, String), Option[T]] = {
103 104 105 106
    (for (sample <- samples; lib <- libraries.getOrElse(sample, Set())) yield {
      (sample, lib) -> function(this, sample, lib)
    }).toMap
  }
107
}