Gears.scala 4.99 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
import nl.lumc.sasc.biopet.extensions.tools.MergeOtuMaps
Peter van 't Hof's avatar
Peter van 't Hof committed
5
import nl.lumc.sasc.biopet.extensions.{ Gzip, Zcat, Ln }
6
import nl.lumc.sasc.biopet.extensions.qiime.MergeOtuTables
7
import nl.lumc.sasc.biopet.pipelines.flexiprep.Flexiprep
8
9
10
11
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
12
13
 * Created by pjvanthof on 03/12/15.
 */
14
15
16
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
17
18
19
20
21
22
23
  override def reportClass = {
    val gearsReport = new GearsReport(this)
    gearsReport.outputDir = new File(outputDir, "report")
    gearsReport.summaryFile = summaryFile
    Some(gearsReport)
  }

24
25
  override def fixedValues = Map("gearssingle" -> Map("skip_flexiprep" -> true))

26
27
28
29
30
31
32
33
34
35
36
37
38
  /** 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()
  }

39
40
41
42
43
44
45
46
47
48
  def qiimeClosedDir: Option[File] = {
    if (samples.values.flatMap(_.gs.qiimeClosed).nonEmpty) {
      Some(new File(outputDir, "qiime_closed_reference"))
    } else None

  }

  def qiimeClosedOtuTable: Option[File] = qiimeClosedDir.map(new File(_, "otu_table.biom"))
  def qiimeClosedOtuMap: Option[File] = qiimeClosedDir.map(new File(_, "otu_map.txt"))

49
  /**
Peter van 't Hof's avatar
Peter van 't Hof committed
50
51
   * Method where the multisample jobs should be added, this will be executed only when running the -sample argument is not given.
   */
52
  def addMultiSampleJobs(): Unit = {
53
54
55
    val gss = samples.values.flatMap(_.gs.qiimeClosed).toList
    val closedOtuTables = gss.map(_.otuTable)
    val closedOtuMaps = gss.map(_.otuMap)
56
57
58
59
60
    require(closedOtuTables.size == closedOtuMaps.size)
    if (closedOtuTables.nonEmpty) {
      if (closedOtuTables.size > 1) {
        val mergeTables = new MergeOtuTables(qscript)
        mergeTables.input = closedOtuTables
61
        mergeTables.outputFile = qiimeClosedOtuTable.get
62
63
        add(mergeTables)

Peter van 't Hof's avatar
Peter van 't Hof committed
64
        val mergeMaps = new MergeOtuMaps(qscript)
65
        mergeMaps.input = closedOtuMaps
66
        mergeMaps.output = qiimeClosedOtuMap.get
67
68
69
        add(mergeMaps)

      } else {
70
71
        add(Ln(qscript, closedOtuMaps.head, qiimeClosedOtuMap.get))
        add(Ln(qscript, closedOtuTables.head, qiimeClosedOtuTable.get))
72
73
74
75
76
      }

      //TODO: Plots

    }
77
78
79
  }

  /**
Peter van 't Hof's avatar
Peter van 't Hof committed
80
81
82
83
   * Factory method for Sample class
   * @param id SampleId
   * @return Sample class
   */
84
85
86
87
  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
88
89
90
91
     * Factory method for Library class
     * @param id SampleId
     * @return Sample class
     */
92
93
94
    def makeLibrary(id: String): Library = new Library(id)

    class Library(libId: String) extends AbstractLibrary(libId) {
95

96
97
98
      lazy val flexiprep = new Flexiprep(qscript)
      flexiprep.sampleId = Some(sampleId)
      flexiprep.libId = Some(libId)
99
100
101
102
      flexiprep.input_R1 = config("R1")
      flexiprep.input_R2 = config("R2")
      flexiprep.outputDir = new File(libDir, "flexiprep")

103
104
105
      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
106
      gs.outputDir = libDir
107
108
109

      /** Function that add library jobs */
      protected def addJobs(): Unit = {
110
111
        add(flexiprep)

112
113
114
        gs.fastqR1 = Some(flexiprep.fastqR1Qc)
        gs.fastqR2 = flexiprep.fastqR2Qc
        add(gs)
115
116
117
118
119
120
121
122
123
      }

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

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

124
125
126
    lazy val gs = new GearsSingle(qscript)
    gs.sampleId = Some(sampleId)
    gs.outputDir = sampleDir
127
128
129
130

    /** Function to add sample jobs */
    protected def addJobs(): Unit = {
      addPerLibJobs()
Peter van 't Hof's avatar
Peter van 't Hof committed
131

132
133
134
135
136
      val flexipreps = libraries.values.map(_.flexiprep).toList

      val mergeR1: File = new File(sampleDir, s"$sampleId.R1.fq.gz")
      add(Zcat(qscript, flexipreps.map(_.fastqR1Qc)) | new Gzip(qscript) > mergeR1)

Peter van 't Hof's avatar
Peter van 't Hof committed
137
      val mergeR2 = if (flexipreps.exists(_.paired)) Some(new File(sampleDir, s"$sampleId.R2.fq.gz")) else None
138
139
      mergeR2.foreach { file =>
        add(Zcat(qscript, flexipreps.flatMap(_.fastqR2Qc)) | new Gzip(qscript) > file)
140
      }
141
142
143
144

      gs.fastqR1 = Some(mergeR1)
      gs.fastqR2 = mergeR2
      add(gs)
145
146
    }

147
148
149
150
151
152
153
154
155
156
157
    /** 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 */
158
159
160
161
  def summaryFiles: Map[String, File] = (
      qiimeClosedOtuTable.map("qiime_closed_otu_table" -> _) ++
      qiimeClosedOtuMap.map("qiime_closed_otu_map" -> _)
    ).toMap
162
163
164
}

object Gears extends PipelineCommand