Gears.scala 4.96 KB
Newer Older
1
2
package nl.lumc.sasc.biopet.pipelines.gears

Peter van 't Hof's avatar
Peter van 't Hof committed
3
import nl.lumc.sasc.biopet.core.{ PipelineCommand, MultiSampleQScript }
4
5
import nl.lumc.sasc.biopet.extensions.Ln
import nl.lumc.sasc.biopet.extensions.qiime.MergeOtuTables
6
7
8
9
10
import nl.lumc.sasc.biopet.utils.Logging
import nl.lumc.sasc.biopet.utils.config.Configurable
import org.broadinstitute.gatk.queue.QScript

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
11
12
 * Created by pjvanthof on 03/12/15.
 */
13
14
15
class Gears(val root: Configurable) extends QScript with MultiSampleQScript { qscript =>
  def this() = this(null)

Peter van 't Hof's avatar
Peter van 't Hof committed
16
17
18
19
20
21
22
  override def reportClass = {
    val gearsReport = new GearsReport(this)
    gearsReport.outputDir = new File(outputDir, "report")
    gearsReport.summaryFile = summaryFile
    Some(gearsReport)
  }

23
24
25
26
27
28
29
30
31
32
33
34
35
36
  /** Init for pipeline */
  def init(): Unit = {
  }

  /** Name of summary output file */
  def summaryFile: File = new File(outputDir, "gears.summary.json")

  /** Pipeline itself */
  def biopetScript(): Unit = {
    addSamplesJobs()
    addSummaryJobs()
  }

  /**
Peter van 't Hof's avatar
Peter van 't Hof committed
37
38
   * Method where the multisample jobs should be added, this will be executed only when running the -sample argument is not given.
   */
39
  def addMultiSampleJobs(): Unit = {
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    val closedOtuTables = samples.values.flatMap(_.closedOtuTable).toList
    val closedOtuMaps = samples.values.flatMap(_.closedOtuMap).toList
    require(closedOtuTables.size == closedOtuMaps.size)
    if (closedOtuTables.nonEmpty) {
      val closedDir = new File(outputDir, "qiime_closed_reference")
      val closedOtuTable = new File(closedDir, "closed.biom")
      val closedOtuMap = new File(closedDir, "closed.map.txt")

      if (closedOtuTables.size > 1) {
        val mergeTables = new MergeOtuTables(qscript)
        mergeTables.input = closedOtuTables
        mergeTables.outputFile = closedOtuTable
        add(mergeTables)

        val mergeMaps = new MergeOtuTables(qscript)
        mergeMaps.input = closedOtuMaps
        mergeMaps.outputFile = closedOtuMap
        add(mergeMaps)

      } else {
        add(Ln(qscript, closedOtuMaps.head, closedOtuMap))
        add(Ln(qscript, closedOtuTables.head, closedOtuTable))
      }

      //TODO: Plots

    }
67
68
69
  }

  /**
Peter van 't Hof's avatar
Peter van 't Hof committed
70
71
72
73
   * Factory method for Sample class
   * @param id SampleId
   * @return Sample class
   */
74
75
76
77
  def makeSample(id: String): Sample = new Sample(id)

  class Sample(sampleId: String) extends AbstractSample(sampleId) {
    /**
Peter van 't Hof's avatar
Peter van 't Hof committed
78
79
80
81
     * Factory method for Library class
     * @param id SampleId
     * @return Sample class
     */
82
83
84
85
86
87
    def makeLibrary(id: String): Library = new Library(id)

    class Library(libId: String) extends AbstractLibrary(libId) {
      lazy val gs = new GearsSingle(qscript)
      gs.sampleId = Some(sampleId)
      gs.libId = Some(libId)
Peter van 't Hof's avatar
Peter van 't Hof committed
88
      gs.outputDir = libDir
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
      gs.fastqR1 = config("R1")
      gs.fastqR2 = config("R2")
      gs.bamFile = config("bam")

      /** Function that add library jobs */
      protected def addJobs(): Unit = {
        if (gs.fastqR1.isDefined || gs.bamFile.isDefined) {
          gs.init()
          gs.biopetScript()
          addAll(gs.functions)
          addSummaryQScript(gs)
        } else Logging.addError(s"Sample: '$sampleId',  library: '$libId', No input files found")
      }

      /** Must return files to store into summary */
      def summaryFiles: Map[String, File] = Map()

      /** Must returns stats to store into summary */
      def summaryStats = Map()
    }

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    private var _closedOtuTable: Option[File] = _
    def closedOtuTable = _closedOtuTable

    private var _closedOtuMap: Option[File] = _
    def closedOtuMap = _closedOtuMap

    /** Function to add sample jobs */
    protected def addJobs(): Unit = {
      addPerLibJobs()
      val qiimeClosed = libraries.values.flatMap(_.gs.qiimeClosed).toList
      if (qiimeClosed.nonEmpty) {
        _closedOtuTable = Some(new File(sampleDir, "closed.biom"))
        _closedOtuMap = Some(new File(sampleDir, "closed.map.txt"))
        if (qiimeClosed.size > 1) {
          val mergeTables = new MergeOtuTables(qscript)
          mergeTables.input = qiimeClosed.map(_.otuTable).toList
          mergeTables.outputFile = _closedOtuTable.get
          add(mergeTables)

          val mergeMaps = new MergeOtuTables(qscript)
          mergeMaps.input = qiimeClosed.map(_.otuMap).toList
          mergeMaps.outputFile = _closedOtuMap.get
          add(mergeMaps)

        } else {
          add(Ln(qscript, qiimeClosed.head.otuMap, _closedOtuMap.get))
          add(Ln(qscript, qiimeClosed.head.otuTable, _closedOtuTable.get))
        }
Peter van 't Hof's avatar
Peter van 't Hof committed
138
139

        //TODO: Plots
140
141
142
143
144
145
      } else {
        _closedOtuTable = None
        _closedOtuMap = None
      }
    }

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    /** Must return files to store into summary */
    def summaryFiles: Map[String, File] = Map()

    /** Must returns stats to store into summary */
    def summaryStats: Any = Map()
  }

  /** Must return a map with used settings for this pipeline */
  def summarySettings: Map[String, Any] = Map()

  /** File to put in the summary for thie pipeline */
  def summaryFiles: Map[String, File] = Map()
}

object Gears extends PipelineCommand