GearsSingle.scala 5.04 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.pipelines.flexiprep.Flexiprep
Peter van 't Hof's avatar
Peter van 't Hof committed
22
import nl.lumc.sasc.biopet.utils.config.Configurable
23
24
import org.broadinstitute.gatk.queue.QScript

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

Wai Yi Leung's avatar
Wai Yi Leung committed
31
32
  @Input(doc = "R1 reads in FastQ format", shortName = "R1", required = false)
  var fastqR1: Option[File] = None
33

Wai Yi Leung's avatar
Wai Yi Leung committed
34
35
  @Input(doc = "R2 reads in FastQ format", shortName = "R2", required = false)
  var fastqR2: Option[File] = None
36

Wai Yi Leung's avatar
Wai Yi Leung committed
37
  @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
38
  var bamFile: Option[File] = None
39

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

Peter van 't Hof's avatar
Peter van 't Hof committed
43
  var gearsUseKraken: Boolean = config("gears_use_kraken", default = true)
Peter van 't Hof's avatar
Peter van 't Hof committed
44
  var gearsUserQiimeRtax: Boolean = config("gear_use_qiime_rtax", default = false)
45
  var gearsUserQiimeClosed: Boolean = config("gear_use_qiime_closed", default = false)
Peter van 't Hof's avatar
Peter van 't Hof committed
46

Peter van 't Hof's avatar
Peter van 't Hof committed
47
48
  /** Executed before running the script */
  def init(): Unit = {
49
    require(fastqR1.isDefined || bamFile.isDefined, "Please specify fastq-file(s) or bam file")
Peter van 't Hof's avatar
Peter van 't Hof committed
50
    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
51
52

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

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

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

Peter van 't Hof's avatar
Peter van 't Hof committed
76
77
  /** Method to add jobs */
  def biopetScript(): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
78
79
    val (r1, r2): (File, Option[File]) = (fastqR1, fastqR2, bamFile) match {
      case (Some(r1), _, _) => (r1, fastqR2)
Peter van 't Hof's avatar
Peter van 't Hof committed
80
81
      case (_, _, Some(bam)) =>
        val extract = new ExtractUnmappedReads(this)
Peter van 't Hof's avatar
Peter van 't Hof committed
82
        extract.outputDir = outputDir
Peter van 't Hof's avatar
Peter van 't Hof committed
83
        extract.bamFile = bam
Peter van 't Hof's avatar
Peter van 't Hof committed
84
        extract.outputName = outputName
Peter van 't Hof's avatar
Peter van 't Hof committed
85
        add(extract)
Peter van 't Hof's avatar
Peter van 't Hof committed
86
        (extract.fastqUnmappedR1, Some(extract.fastqUnmappedR2))
Peter van 't Hof's avatar
Peter van 't Hof committed
87
      case _ => throw new IllegalArgumentException("Missing input files")
Peter van 't Hof's avatar
Peter van 't Hof committed
88
89
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
90
91
92
    val flexiprep = new Flexiprep(this)
    flexiprep.input_R1 = r1
    flexiprep.input_R2 = r2
Peter van 't Hof's avatar
Peter van 't Hof committed
93
    flexiprep.outputDir = new File(outputDir, "flexiprep")
Peter van 't Hof's avatar
Peter van 't Hof committed
94
    add(flexiprep)
Peter van 't Hof's avatar
Peter van 't Hof committed
95

Peter van 't Hof's avatar
Peter van 't Hof committed
96
97
98
    if (gearsUseKraken) {
      val kraken = new GearsKraken(this)
      kraken.outputDir = new File(outputDir, "kraken")
Peter van 't Hof's avatar
Peter van 't Hof committed
99
100
      kraken.fastqR1 = flexiprep.fastqR1Qc
      kraken.fastqR2 = flexiprep.fastqR2Qc
Peter van 't Hof's avatar
Peter van 't Hof committed
101
      kraken.outputName = outputName
Peter van 't Hof's avatar
Peter van 't Hof committed
102
      add(kraken)
Peter van 't Hof's avatar
Peter van 't Hof committed
103
104
105
    }

    if (gearsUserQiimeRtax) {
Peter van 't Hof's avatar
Peter van 't Hof committed
106
      val qiimeRatx = new GearsQiimeRtax(this)
Peter van 't Hof's avatar
Peter van 't Hof committed
107
      qiimeRatx.outputDir = new File(outputDir, "qiime_rtax")
Peter van 't Hof's avatar
Peter van 't Hof committed
108
109
      qiimeRatx.fastqR1 = flexiprep.fastqR1Qc
      qiimeRatx.fastqR2 = flexiprep.fastqR2Qc
Peter van 't Hof's avatar
Peter van 't Hof committed
110
      add(qiimeRatx)
Peter van 't Hof's avatar
Peter van 't Hof committed
111
    }
112

113
114
    if (gearsUserQiimeClosed) {
      val qiimeClosed = new GearsQiimeClosed(this)
Peter van 't Hof's avatar
Peter van 't Hof committed
115
      qiimeClosed.outputDir = new File(outputDir, "qiime_closed")
116
117
      qiimeClosed.fastqR1 = flexiprep.fastqR1Qc
      qiimeClosed.fastqR2 = flexiprep.fastqR2Qc
Peter van 't Hof's avatar
Peter van 't Hof committed
118
      add(qiimeClosed)
119
120
    }

Wai Yi Leung's avatar
Wai Yi Leung committed
121
    addSummaryJobs()
122
  }
Peter van 't Hof's avatar
Peter van 't Hof committed
123
124

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

127
  /** Pipeline settings shown in the summary file */
128
  def summarySettings: Map[String, Any] = Map.empty
Peter van 't Hof's avatar
Peter van 't Hof committed
129

130
  /** Statistics shown in the summary file */
Wai Yi Leung's avatar
Wai Yi Leung committed
131
132
  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
133
134
    (if (fastqR1.isDefined) Map("input_R1" -> fastqR1.get) else Map()) ++
    outputFiles
135
136
137
}

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