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

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

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

40
41
42
43
44
45
46
47
48
49
  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"))

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

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

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

      //TODO: Plots

    }
78
79
80
  }

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

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

97
98
99
      lazy val flexiprep = new Flexiprep(qscript)
      flexiprep.sampleId = Some(sampleId)
      flexiprep.libId = Some(libId)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
100
      flexiprep.inputR1 = config("R1")
101
      flexiprep.inputR2 = config("R2")
102
103
      flexiprep.outputDir = new File(libDir, "flexiprep")

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

      /** Function that add library jobs */
      protected def addJobs(): Unit = {
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
111
        inputFiles :+= InputFile(flexiprep.inputR1, config("R1_md5"))
112
        flexiprep.inputR2.foreach(inputFiles :+= InputFile(_, config("R2_md5")))
113
114
        add(flexiprep)

115
116
117
        gs.fastqR1 = Some(flexiprep.fastqR1Qc)
        gs.fastqR2 = flexiprep.fastqR2Qc
        add(gs)
118
119
120
121
122
123
124
125
126
      }

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

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

127
128
129
    lazy val gs = new GearsSingle(qscript)
    gs.sampleId = Some(sampleId)
    gs.outputDir = sampleDir
130
131
132
133

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

135
136
137
138
139
      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
140
      val mergeR2 = if (flexipreps.exists(_.paired)) Some(new File(sampleDir, s"$sampleId.R2.fq.gz")) else None
141
142
      mergeR2.foreach { file =>
        add(Zcat(qscript, flexipreps.flatMap(_.fastqR2Qc)) | new Gzip(qscript) > file)
143
      }
144
145
146
147

      gs.fastqR1 = Some(mergeR1)
      gs.fastqR2 = mergeR2
      add(gs)
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 */
161
  def summaryFiles: Map[String, File] = (
Peter van 't Hof's avatar
Peter van 't Hof committed
162
163
164
    qiimeClosedOtuTable.map("qiime_closed_otu_table" -> _) ++
    qiimeClosedOtuMap.map("qiime_closed_otu_map" -> _)
  ).toMap
165
166
167
}

object Gears extends PipelineCommand