GearsSingle.scala 5.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * 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
 *
 * A dual licensing mode is applied. The source code within this project that are
 * not part of GATK Queue is freely available for non-commercial use under an AGPL
 * 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
18
import nl.lumc.sasc.biopet.core.summary.SummaryQScript
Peter van 't Hof's avatar
Peter van 't Hof committed
19
import nl.lumc.sasc.biopet.core.BiopetQScript.InputFile
Peter van 't Hof's avatar
Peter van 't Hof committed
20
import nl.lumc.sasc.biopet.core.{ PipelineCommand, SampleLibraryTag }
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import nl.lumc.sasc.biopet.extensions.Flash
Peter van 't Hof's avatar
Peter van 't Hof committed
22
import nl.lumc.sasc.biopet.pipelines.flexiprep.Flexiprep
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
33
  @Input(doc = "R1 reads in FastQ format", shortName = "R1", required = false)
  var fastqR1: Option[File] = None
34

Wai Yi Leung's avatar
Wai Yi Leung committed
35
36
  @Input(doc = "R2 reads in FastQ format", shortName = "R2", required = false)
  var fastqR2: Option[File] = None
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
46
  lazy val krakenScript = if (config("gears_use_kraken", default = true)) Some(new GearsKraken(this)) else None
  lazy val qiimeRatx = if (config("gear_use_qiime_rtax", default = false)) Some(new GearsQiimeRtax(this)) else None
  lazy val qiimeClosed = if (config("gear_use_qiime_closed", default = false)) Some(new GearsQiimeClosed(this)) else None
Peter van 't Hof's avatar
Peter van 't Hof committed
47

Peter van 't Hof's avatar
Peter van 't Hof committed
48
49
  /** Executed before running the script */
  def init(): Unit = {
50
    require(fastqR1.isDefined || bamFile.isDefined, "Please specify fastq-file(s) or bam file")
Peter van 't Hof's avatar
Peter van 't Hof committed
51
    require(fastqR1.isDefined != bamFile.isDefined, "Provide either a bam file or a R1/R2 file")
Peter van 't Hof's avatar
Peter van 't Hof committed
52
53

    if (outputName == null) {
Wai Yi Leung's avatar
Wai Yi Leung committed
54
      if (fastqR1.isDefined) outputName = fastqR1.map(_.getName
Peter van 't Hof's avatar
Peter van 't Hof committed
55
56
57
58
59
        .stripSuffix(".gz")
        .stripSuffix(".fastq")
        .stripSuffix(".fq"))
        .getOrElse("noName")
      else outputName = bamFile.map(_.getName.stripSuffix(".bam")).getOrElse("noName")
60
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
61
62
63
64

    if (fastqR1.isDefined) {
      fastqR1.foreach(inputFiles :+= InputFile(_))
      fastqR2.foreach(inputFiles :+= InputFile(_))
Peter van 't Hof's avatar
Peter van 't Hof committed
65
    } else inputFiles :+= InputFile(bamFile.get)
Peter van 't Hof's avatar
Peter van 't Hof committed
66
  }
67

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

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

79
  protected def executeFlexiprep(r1: File, r2: Option[File]): (File, Option[File]) = {
Peter van 't Hof's avatar
Peter van 't Hof committed
80
    if (!skipFlexiprep) {
81
82
83
84
85
86
87
88
89
      val flexiprep = new Flexiprep(this)
      flexiprep.input_R1 = r1
      flexiprep.input_R2 = r2
      flexiprep.outputDir = new File(outputDir, "flexiprep")
      add(flexiprep)
      (flexiprep.fastqR1Qc, flexiprep.fastqR2Qc)
    } else (r1, r2)
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
90
91
  /** Method to add jobs */
  def biopetScript(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
92
    val (r1, r2): (File, Option[File]) = (fastqR1, fastqR2, bamFile) match {
93
      case (Some(r1), _, _) => executeFlexiprep(r1, fastqR2)
Peter van 't Hof's avatar
Peter van 't Hof committed
94
95
      case (_, _, Some(bam)) =>
        val extract = new ExtractUnmappedReads(this)
Peter van 't Hof's avatar
Peter van 't Hof committed
96
        extract.outputDir = outputDir
Peter van 't Hof's avatar
Peter van 't Hof committed
97
        extract.bamFile = bam
Peter van 't Hof's avatar
Peter van 't Hof committed
98
        extract.outputName = outputName
Peter van 't Hof's avatar
Peter van 't Hof committed
99
        add(extract)
100
        executeFlexiprep(extract.fastqUnmappedR1, Some(extract.fastqUnmappedR2))
Peter van 't Hof's avatar
Peter van 't Hof committed
101
      case _ => throw new IllegalArgumentException("Missing input files")
Peter van 't Hof's avatar
Peter van 't Hof committed
102
103
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
104
105
106
    lazy val combinedFastq = {
      r2 match {
        case Some(r2) =>
107
108
109
110
111
112
          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
113
114
115
116
        case _ => r1
      }
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
117
    krakenScript foreach { kraken =>
Peter van 't Hof's avatar
Peter van 't Hof committed
118
      kraken.outputDir = new File(outputDir, "kraken")
119
120
      kraken.fastqR1 = r1
      kraken.fastqR2 = r2
Peter van 't Hof's avatar
Peter van 't Hof committed
121
      kraken.outputName = outputName
Peter van 't Hof's avatar
Peter van 't Hof committed
122
      add(kraken)
Peter van 't Hof's avatar
Peter van 't Hof committed
123
124
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
125
    qiimeRatx foreach { qiimeRatx =>
Peter van 't Hof's avatar
Peter van 't Hof committed
126
      qiimeRatx.outputDir = new File(outputDir, "qiime_rtax")
127
128
      qiimeRatx.fastqR1 = r1
      qiimeRatx.fastqR2 = r2
Peter van 't Hof's avatar
Peter van 't Hof committed
129
      add(qiimeRatx)
Peter van 't Hof's avatar
Peter van 't Hof committed
130
    }
131

Peter van 't Hof's avatar
Peter van 't Hof committed
132
    qiimeClosed foreach { qiimeClosed =>
Peter van 't Hof's avatar
Peter van 't Hof committed
133
      qiimeClosed.outputDir = new File(outputDir, "qiime_closed")
Peter van 't Hof's avatar
Peter van 't Hof committed
134
      qiimeClosed.fastqInput = combinedFastq
Peter van 't Hof's avatar
Peter van 't Hof committed
135
      add(qiimeClosed)
Peter van 't Hof's avatar
Peter van 't Hof committed
136
137

      //TODO: Plots
138
139
    }

Wai Yi Leung's avatar
Wai Yi Leung committed
140
    addSummaryJobs()
141
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
142
143

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

146
  /** Pipeline settings shown in the summary file */
Peter van 't Hof's avatar
Peter van 't Hof committed
147
148
149
150
151
152
  def summarySettings: Map[String, Any] = Map(
    "skip_flexiprep" -> skipFlexiprep,
    "gears_use_kraken" -> krakenScript.isDefined,
    "gear_use_qiime_rtax" -> qiimeRatx.isDefined,
    "gear_use_qiime_closed" -> qiimeClosed.isDefined
  )
Peter van 't Hof's avatar
Peter van 't Hof committed
153

154
  /** Statistics shown in the summary file */
Wai Yi Leung's avatar
Wai Yi Leung committed
155
156
  def summaryFiles: Map[String, File] = Map.empty ++
    (if (bamFile.isDefined) Map("input_bam" -> bamFile.get) else Map()) ++
Wai Yi Leung's avatar
Wai Yi Leung committed
157
158
    (if (fastqR1.isDefined) Map("input_R1" -> fastqR1.get) else Map()) ++
    outputFiles
159
160
161
}

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