GearsSingle.scala 7.67 KB
Newer Older
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
12
13
14
15
16
 * license; For commercial users or users who do not want to follow the AGPL
 * license, please contact us to obtain a separate license.
 */
package nl.lumc.sasc.biopet.pipelines.gears

Peter van 't Hof's avatar
Peter van 't Hof committed
17
import nl.lumc.sasc.biopet.core.summary.SummaryQScript
Peter van 't Hof's avatar
Peter van 't Hof committed
18
import nl.lumc.sasc.biopet.core.BiopetQScript.InputFile
Peter van 't Hof's avatar
Peter van 't Hof committed
19
20
import nl.lumc.sasc.biopet.core.{ PipelineCommand, SampleLibraryTag }
import nl.lumc.sasc.biopet.extensions.{ Gzip, Zcat }
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import nl.lumc.sasc.biopet.pipelines.flexiprep.Flexiprep
Peter van 't Hof's avatar
Peter van 't Hof committed
22
import nl.lumc.sasc.biopet.utils.Logging
Peter van 't Hof's avatar
Peter van 't Hof committed
23
import nl.lumc.sasc.biopet.utils.config.Configurable
24
25
import org.broadinstitute.gatk.queue.QScript

26
/**
Wai Yi Leung's avatar
Wai Yi Leung committed
27
 * Created by wyleung
28
 */
29
class GearsSingle(val root: Configurable) extends QScript with SummaryQScript with SampleLibraryTag {
30
  def this() = this(null)
31

Wai Yi Leung's avatar
Wai Yi Leung committed
32
  @Input(doc = "R1 reads in FastQ format", shortName = "R1", required = false)
Peter van 't Hof's avatar
Peter van 't Hof committed
33
  var fastqR1: List[File] = Nil
34

Wai Yi Leung's avatar
Wai Yi Leung committed
35
  @Input(doc = "R2 reads in FastQ format", shortName = "R2", required = false)
Peter van 't Hof's avatar
Peter van 't Hof committed
36
  var fastqR2: List[File] = Nil
37

Wai Yi Leung's avatar
Wai Yi Leung committed
38
  @Input(doc = "All unmapped reads will be extracted from this bam for analysis", shortName = "bam", required = false)
Peter van 't Hof's avatar
Peter van 't Hof committed
39
  var bamFile: Option[File] = None
40

Peter van 't Hof's avatar
Peter van 't Hof committed
41
42
  @Argument(required = false)
  var outputName: String = _
43

Peter van 't Hof's avatar
Peter van 't Hof committed
44
45
  lazy val krakenScript = if (config("gears_use_kraken", default = false)) Some(new GearsKraken(this)) else None
  lazy val centrifugeScript = if (config("gears_use_centrifuge", default = true)) Some(new GearsCentrifuge(this)) else None
Peter van 't Hof's avatar
Peter van 't Hof committed
46
47
  lazy val qiimeRatx = if (config("gears_use_qiime_rtax", default = false)) Some(new GearsQiimeRtax(this)) else None
  lazy val qiimeClosed = if (config("gears_use_qiime_closed", default = false)) Some(new GearsQiimeClosed(this)) else None
Peter van 't Hof's avatar
Peter van 't Hof committed
48
  lazy val qiimeOpen = if (config("gears_use_qiime_open", default = false)) Some(new GearsQiimeOpen(this)) else None
Peter van 't Hof's avatar
Peter van 't Hof committed
49
  lazy val seqCount = if (config("gears_use_seq_count", default = false)) Some(new GearsSeqCount(this)) else None
Peter van 't Hof's avatar
Peter van 't Hof committed
50

Peter van 't Hof's avatar
Peter van 't Hof committed
51
52
  /** Executed before running the script */
  def init(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
53
54
55
    if (fastqR1.isEmpty && !bamFile.isDefined) Logging.addError("Please specify fastq-file(s) or bam file")
    if (fastqR1.nonEmpty == bamFile.isDefined) Logging.addError("Provide either a bam file or a R1/R2 file")
    if (fastqR2.nonEmpty && fastqR1.size != fastqR2.size) Logging.addError("R1 and R2 has not the same number of files")
Peter van 't Hof's avatar
Peter van 't Hof committed
56
    if (sampleId == null || sampleId == None) Logging.addError("Missing sample ID on GearsSingle module")
Peter van 't Hof's avatar
Peter van 't Hof committed
57
58

    if (outputName == null) {
Peter van 't Hof's avatar
Peter van 't Hof committed
59
      if (fastqR1.nonEmpty) outputName = fastqR1.headOption.map(_.getName
Peter van 't Hof's avatar
Peter van 't Hof committed
60
61
62
63
64
        .stripSuffix(".gz")
        .stripSuffix(".fastq")
        .stripSuffix(".fq"))
        .getOrElse("noName")
      else outputName = bamFile.map(_.getName.stripSuffix(".bam")).getOrElse("noName")
65
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
66

Peter van 't Hof's avatar
Peter van 't Hof committed
67
    if (fastqR1.nonEmpty) {
Peter van 't Hof's avatar
Peter van 't Hof committed
68
69
      fastqR1.foreach(inputFiles :+= InputFile(_))
      fastqR2.foreach(inputFiles :+= InputFile(_))
Peter van 't Hof's avatar
Peter van 't Hof committed
70
    } else bamFile.foreach(inputFiles :+= InputFile(_))
Peter van 't Hof's avatar
Peter van 't Hof committed
71
  }
72

Wai Yi Leung's avatar
Wai Yi Leung committed
73
  override def reportClass = {
74
    val gears = new GearsSingleReport(this)
Wai Yi Leung's avatar
Wai Yi Leung committed
75
76
    gears.outputDir = new File(outputDir, "report")
    gears.summaryFile = summaryFile
77
78
    sampleId.foreach(gears.args += "sampleId" -> _)
    libId.foreach(gears.args += "libId" -> _)
Wai Yi Leung's avatar
Wai Yi Leung committed
79
80
    Some(gears)
  }
81

Peter van 't Hof's avatar
Peter van 't Hof committed
82
83
  protected var skipFlexiprep: Boolean = config("skip_flexiprep", default = false)

Peter van 't Hof's avatar
Peter van 't Hof committed
84
85
86
87
88
89
90
91
92
93
94
95
96
  protected def executeFlexiprep(r1: List[File], r2: List[File]): (File, Option[File]) = {
    val read1: File = if (r1.size == 1) r1.head else {
      val outputFile = new File(outputDir, "merged.R1.fq.gz")
      Zcat(this, r1) | new Gzip(this) > outputFile
      outputFile
    }

    val read2: Option[File] = if (r2.size <= 1) r2.headOption else {
      val outputFile = new File(outputDir, "merged.R2.fq.gz")
      Zcat(this, r2) | new Gzip(this) > outputFile
      Some(outputFile)
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
97
    if (!skipFlexiprep) {
98
      val flexiprep = new Flexiprep(this)
Peter van 't Hof's avatar
Peter van 't Hof committed
99
100
      flexiprep.inputR1 = read1
      flexiprep.inputR2 = read2
Peter van 't Hof's avatar
Peter van 't Hof committed
101
102
      flexiprep.sampleId = if (sampleId.isEmpty) Some("noSampleName") else sampleId
      flexiprep.libId = if (libId.isEmpty) Some("noLibName") else libId
103
104
105
      flexiprep.outputDir = new File(outputDir, "flexiprep")
      add(flexiprep)
      (flexiprep.fastqR1Qc, flexiprep.fastqR2Qc)
Peter van 't Hof's avatar
Peter van 't Hof committed
106
    } else (read1, read2)
107
108
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
109
110
  /** Method to add jobs */
  def biopetScript(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
111
    val (r1, r2): (File, Option[File]) = (fastqR1, fastqR2, bamFile) match {
Peter van 't Hof's avatar
Peter van 't Hof committed
112
      case (r1, _, _) if r1.nonEmpty => executeFlexiprep(r1, fastqR2)
Peter van 't Hof's avatar
Peter van 't Hof committed
113
114
      case (_, _, Some(bam)) =>
        val extract = new ExtractUnmappedReads(this)
Peter van 't Hof's avatar
Peter van 't Hof committed
115
        extract.outputDir = outputDir
Peter van 't Hof's avatar
Peter van 't Hof committed
116
        extract.bamFile = bam
Peter van 't Hof's avatar
Peter van 't Hof committed
117
        extract.outputName = outputName
Peter van 't Hof's avatar
Peter van 't Hof committed
118
        add(extract)
Peter van 't Hof's avatar
Peter van 't Hof committed
119
        executeFlexiprep(extract.fastqUnmappedR1 :: Nil, extract.fastqUnmappedR2.toList)
Peter van 't Hof's avatar
Peter van 't Hof committed
120
      case _ => throw new IllegalArgumentException("Missing input files")
Peter van 't Hof's avatar
Peter van 't Hof committed
121
122
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
123
124
125
    lazy val combinedFastq = {
      r2 match {
        case Some(r2) =>
126
127
128
129
130
131
          val combineReads = new CombineReads(this)
          combineReads.outputDir = new File(outputDir, "combine_reads")
          combineReads.fastqR1 = r1
          combineReads.fastqR2 = r2
          add(combineReads)
          combineReads.combinedFastq
Peter van 't Hof's avatar
Peter van 't Hof committed
132
133
134
135
        case _ => r1
      }
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
136
    krakenScript foreach { kraken =>
Peter van 't Hof's avatar
Peter van 't Hof committed
137
      kraken.outputDir = new File(outputDir, "kraken")
138
139
      kraken.fastqR1 = r1
      kraken.fastqR2 = r2
Peter van 't Hof's avatar
Peter van 't Hof committed
140
      kraken.outputName = outputName
Peter van 't Hof's avatar
Peter van 't Hof committed
141
      add(kraken)
Peter van 't Hof's avatar
Peter van 't Hof committed
142
143
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
144
145
146
147
148
149
150
151
    centrifugeScript foreach { centrifuge =>
      centrifuge.outputDir = new File(outputDir, "centrifuge")
      centrifuge.fastqR1 = r1
      centrifuge.fastqR2 = r2
      centrifuge.outputName = outputName
      add(centrifuge)
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
152
    qiimeRatx foreach { qiimeRatx =>
Peter van 't Hof's avatar
Peter van 't Hof committed
153
      qiimeRatx.outputDir = new File(outputDir, "qiime_rtax")
154
155
      qiimeRatx.fastqR1 = r1
      qiimeRatx.fastqR2 = r2
Peter van 't Hof's avatar
Peter van 't Hof committed
156
      add(qiimeRatx)
Peter van 't Hof's avatar
Peter van 't Hof committed
157
    }
158

Peter van 't Hof's avatar
Peter van 't Hof committed
159
    qiimeClosed foreach { qiimeClosed =>
Peter van 't Hof's avatar
Peter van 't Hof committed
160
      qiimeClosed.outputDir = new File(outputDir, "qiime_closed")
Peter van 't Hof's avatar
Peter van 't Hof committed
161
      qiimeClosed.fastqInput = combinedFastq
Peter van 't Hof's avatar
Peter van 't Hof committed
162
      add(qiimeClosed)
163
164
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
165
166
167
168
169
170
    qiimeOpen foreach { qiimeOpen =>
      qiimeOpen.outputDir = new File(outputDir, "qiime_open")
      qiimeOpen.fastqInput = combinedFastq
      add(qiimeOpen)
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
171
172
173
174
175
176
    seqCount.foreach { seqCount =>
      seqCount.fastqInput = combinedFastq
      seqCount.outputDir = new File(outputDir, "seq_count")
      add(seqCount)
    }

Wai Yi Leung's avatar
Wai Yi Leung committed
177
    addSummaryJobs()
178
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
179
180

  /** Location of summary file */
Peter van 't Hof's avatar
Peter van 't Hof committed
181
  def summaryFile = new File(outputDir, sampleId.getOrElse("sampleName_unknown") + ".gears.summary.json")
Peter van 't Hof's avatar
Peter van 't Hof committed
182

183
  /** Pipeline settings shown in the summary file */
Peter van 't Hof's avatar
Peter van 't Hof committed
184
185
186
187
  def summarySettings: Map[String, Any] = Map(
    "skip_flexiprep" -> skipFlexiprep,
    "gears_use_kraken" -> krakenScript.isDefined,
    "gear_use_qiime_rtax" -> qiimeRatx.isDefined,
Peter van 't Hof's avatar
Peter van 't Hof committed
188
189
    "gear_use_qiime_closed" -> qiimeClosed.isDefined,
    "gear_use_qiime_open" -> qiimeOpen.isDefined
Peter van 't Hof's avatar
Peter van 't Hof committed
190
  )
Peter van 't Hof's avatar
Peter van 't Hof committed
191

192
  /** Statistics shown in the summary file */
Wai Yi Leung's avatar
Wai Yi Leung committed
193
194
  def summaryFiles: Map[String, File] = Map.empty ++
    (if (bamFile.isDefined) Map("input_bam" -> bamFile.get) else Map()) ++
Peter van 't Hof's avatar
Peter van 't Hof committed
195
196
    fastqR1.zipWithIndex.map(x => s"input_R1_${x._2}" -> x._1) ++
    fastqR2.zipWithIndex.map(x => s"input_R2_${x._2}" -> x._1) ++
Wai Yi Leung's avatar
Wai Yi Leung committed
197
    outputFiles
198
199
200
}

/** This object give a default main method to the pipelines */
201
object GearsSingle extends PipelineCommand