Flexiprep.scala 11.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * 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.
 */
16
17
package nl.lumc.sasc.biopet.pipelines.flexiprep

18
import nl.lumc.sasc.biopet.core.summary.SummaryQScript
19
20
21
import org.broadinstitute.gatk.queue.QScript
import org.broadinstitute.gatk.utils.commandline.{ Input, Argument }

22
import nl.lumc.sasc.biopet.core.{ SampleLibraryTag, BiopetQScript, PipelineCommand }
23
import nl.lumc.sasc.biopet.core.config.Configurable
Peter van 't Hof's avatar
Peter van 't Hof committed
24
import nl.lumc.sasc.biopet.extensions._
bow's avatar
bow committed
25
import nl.lumc.sasc.biopet.tools.SeqStat
bow's avatar
bow committed
26
import nl.lumc.sasc.biopet.tools.FastqSync
27

28
class Flexiprep(val root: Configurable) extends QScript with SummaryQScript with SampleLibraryTag {
29
30
31
32
33
34
  def this() = this(null)

  @Input(doc = "R1 fastq file (gzipped allowed)", shortName = "R1", required = true)
  var input_R1: File = _

  @Input(doc = "R2 fastq file (gzipped allowed)", shortName = "R2", required = false)
35
  var input_R2: Option[File] = None
36

37
38
  /** Skip Trim fastq files */
  var skipTrim: Boolean = config("skip_trim", default = false)
39

40
41
  /** Skip Clip fastq files */
  var skipClip: Boolean = config("skip_clip", default = false)
42

Peter van 't Hof's avatar
Peter van 't Hof committed
43
  /** Location of summary file */
44
  def summaryFile = new File(outputDir, sampleId.getOrElse("x") + "-" + libId.getOrElse("x") + ".qc.summary.json")
45

Peter van 't Hof's avatar
Peter van 't Hof committed
46
  /** Returns files to store in summary */
Peter van 't Hof's avatar
Peter van 't Hof committed
47
48
49
50
  def summaryFiles: Map[String, File] = {
    Map("input_R1" -> input_R1, "output_R1" -> outputFiles("output_R1_gzip")) ++
      (if (paired) Map("input_R2" -> input_R2.get, "output_R2" -> outputFiles("output_R2_gzip")) else Map())
  }
51

Peter van 't Hof's avatar
Peter van 't Hof committed
52
  /** returns settings to store in summary */
Peter van 't Hof's avatar
Peter van 't Hof committed
53
  def summarySettings = Map("skip_trim" -> skipTrim, "skip_clip" -> skipClip, "paired" -> paired)
54

55
  var paired: Boolean = input_R2.isDefined
56
57
58
59
60
61
62
63
64
65
  var R1_ext: String = _
  var R2_ext: String = _
  var R1_name: String = _
  var R2_name: String = _

  var fastqc_R1: Fastqc = _
  var fastqc_R2: Fastqc = _
  var fastqc_R1_after: Fastqc = _
  var fastqc_R2_after: Fastqc = _

Peter van 't Hof's avatar
Peter van 't Hof committed
66
  /** Function that's need to be executed before the script is accessed */
67
  def init() {
68
    require(outputDir != null, "Missing output directory on flexiprep module")
69
    require(input_R1 != null, "Missing input R1 on flexiprep module")
Peter van 't Hof's avatar
Peter van 't Hof committed
70
71
    require(sampleId != null, "Missing sample ID on flexiprep module")
    require(libId != null, "Missing library ID on flexiprep module")
72

73
    paired = input_R2.isDefined
74
75
76
77
78
79
80

    if (input_R1.endsWith(".gz")) R1_name = input_R1.getName.substring(0, input_R1.getName.lastIndexOf(".gz"))
    else if (input_R1.endsWith(".gzip")) R1_name = input_R1.getName.substring(0, input_R1.getName.lastIndexOf(".gzip"))
    else R1_name = input_R1.getName
    R1_ext = R1_name.substring(R1_name.lastIndexOf("."), R1_name.size)
    R1_name = R1_name.substring(0, R1_name.lastIndexOf(R1_ext))

Peter van 't Hof's avatar
Peter van 't Hof committed
81
82
83
84
85
86
87
88
89
90
    input_R2 match {
      case Some(fileR2) => {
        paired = true
        if (fileR2.endsWith(".gz")) R2_name = fileR2.getName.substring(0, fileR2.getName.lastIndexOf(".gz"))
        else if (fileR2.endsWith(".gzip")) R2_name = fileR2.getName.substring(0, fileR2.getName.lastIndexOf(".gzip"))
        else R2_name = fileR2.getName
        R2_ext = R2_name.substring(R2_name.lastIndexOf("."), R2_name.size)
        R2_name = R2_name.substring(0, R2_name.lastIndexOf(R2_ext))
      }
      case _ =>
91
92
93
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
94
  /** Script to add jobs */
95
96
97
  def biopetScript() {
    runInitialJobs()

Peter van 't Hof's avatar
Peter van 't Hof committed
98
    val out = if (paired) runTrimClip(outputFiles("fastq_input_R1"), Some(outputFiles("fastq_input_R2")), outputDir)
99
100
101
102
103
104
105
    else runTrimClip(outputFiles("fastq_input_R1"), outputDir)

    val R1_files = for ((k, v) <- outputFiles if k.endsWith("output_R1")) yield v
    val R2_files = for ((k, v) <- outputFiles if k.endsWith("output_R2")) yield v
    runFinalize(R1_files.toList, R2_files.toList)
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
106
  /** Add init non chunkable jobs */
107
108
  def runInitialJobs() {
    outputFiles += ("fastq_input_R1" -> extractIfNeeded(input_R1, outputDir))
Peter van 't Hof's avatar
Peter van 't Hof committed
109
    if (paired) outputFiles += ("fastq_input_R2" -> extractIfNeeded(input_R2.get, outputDir))
110

Peter van 't Hof's avatar
Peter van 't Hof committed
111
    fastqc_R1 = Fastqc(this, input_R1, new File(outputDir, R1_name + ".fastqc/"))
112
    add(fastqc_R1)
113
    addSummarizable(fastqc_R1, "fastqc_R1")
114
115
116
    outputFiles += ("fastqc_R1" -> fastqc_R1.output)

    if (paired) {
Peter van 't Hof's avatar
Peter van 't Hof committed
117
      fastqc_R2 = Fastqc(this, input_R2.get, new File(outputDir, R2_name + ".fastqc/"))
118
      add(fastqc_R2)
119
      addSummarizable(fastqc_R2, "fastqc_R2")
120
121
122
123
      outputFiles += ("fastqc_R2" -> fastqc_R2.output)
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
124
125
126
127
128
129
130
131
132
  //TODO: Refactor need to combine all this functions

  /**
   * Adds all chunkable jobs of flexiprep
   * @param R1_in
   * @param outDir
   * @param chunk
   * @return
   */
Peter van 't Hof's avatar
Peter van 't Hof committed
133
134
  def runTrimClip(R1_in: File, outDir: File, chunk: String): (File, Option[File], List[File]) =
    runTrimClip(R1_in, None, outDir, chunk)
Peter van 't Hof's avatar
Peter van 't Hof committed
135
136
137
138
139
140
141

  /**
   * Adds all chunkable jobs of flexiprep
   * @param R1_in
   * @param outDir
   * @return
   */
Peter van 't Hof's avatar
Peter van 't Hof committed
142
143
  def runTrimClip(R1_in: File, outDir: File): (File, Option[File], List[File]) =
    runTrimClip(R1_in, None, outDir, "")
Peter van 't Hof's avatar
Peter van 't Hof committed
144
145
146
147
148
149
150
151

  /**
   * Adds all chunkable jobs of flexiprep
   * @param R1_in
   * @param R2_in
   * @param outDir
   * @return
   */
Peter van 't Hof's avatar
Peter van 't Hof committed
152
  def runTrimClip(R1_in: File, R2_in: Option[File], outDir: File): (File, Option[File], List[File]) =
153
    runTrimClip(R1_in, R2_in, outDir, "")
Peter van 't Hof's avatar
Peter van 't Hof committed
154
155
156
157
158
159
160
161
162

  /**
   * Adds all chunkable jobs of flexiprep
   * @param R1_in
   * @param R2_in
   * @param outDir
   * @param chunkarg
   * @return
   */
Peter van 't Hof's avatar
Peter van 't Hof committed
163
  def runTrimClip(R1_in: File, R2_in: Option[File], outDir: File, chunkarg: String): (File, Option[File], List[File]) = {
164
165
166
    val chunk = if (chunkarg.isEmpty || chunkarg.endsWith("_")) chunkarg else chunkarg + "_"
    var results: Map[String, File] = Map()

Peter van 't Hof's avatar
Peter van 't Hof committed
167
168
169
170
    var R1 = R1_in
    var R2 = R2_in
    var deps_R1 = R1 :: Nil
    var deps_R2 = if (paired) R2.get :: Nil else Nil
171
    def deps: List[File] = deps_R1 ::: deps_R2
172

173
    val seqtkSeq_R1 = SeqtkSeq(this, R1, swapExt(outDir, R1, R1_ext, ".sanger" + R1_ext), fastqc_R1)
174
175
    seqtkSeq_R1.isIntermediate = true
    add(seqtkSeq_R1)
Peter van 't Hof's avatar
Peter van 't Hof committed
176
    addSummarizable(seqtkSeq_R1, "seqtkSeq_R1")
177
    R1 = seqtkSeq_R1.output
178
    deps_R1 ::= R1
179
180

    if (paired) {
Peter van 't Hof's avatar
Peter van 't Hof committed
181
      val seqtkSeq_R2 = SeqtkSeq(this, R2.get, swapExt(outDir, R2.get, R2_ext, ".sanger" + R2_ext), fastqc_R2)
182
183
      seqtkSeq_R2.isIntermediate = true
      add(seqtkSeq_R2)
Peter van 't Hof's avatar
Peter van 't Hof committed
184
      addSummarizable(seqtkSeq_R2, "seqtkSeq_R2")
Peter van 't Hof's avatar
Peter van 't Hof committed
185
186
      R2 = Some(seqtkSeq_R2.output)
      deps_R2 ::= R2.get
187
188
    }

bow's avatar
bow committed
189
    val seqstat_R1 = SeqStat(this, R1, outDir)
190
    seqstat_R1.isIntermediate = true
191
    seqstat_R1.deps = deps_R1
192
    add(seqstat_R1)
Peter van 't Hof's avatar
Peter van 't Hof committed
193
    addSummarizable(seqstat_R1, "seqstat_R1")
194
195

    if (paired) {
bow's avatar
bow committed
196
      val seqstat_R2 = SeqStat(this, R2.get, outDir)
197
      seqstat_R2.isIntermediate = true
198
      seqstat_R2.deps = deps_R2
199
      add(seqstat_R2)
Peter van 't Hof's avatar
Peter van 't Hof committed
200
      addSummarizable(seqstat_R2, "seqstat_R2")
201
202
203
204
205
    }

    if (!skipClip) { // Adapter clipping

      val cutadapt_R1 = Cutadapt(this, R1, swapExt(outDir, R1, R1_ext, ".clip" + R1_ext))
206
      cutadapt_R1.fastqc = fastqc_R1
207
      cutadapt_R1.isIntermediate = true
208
      cutadapt_R1.deps = deps_R1
209
      add(cutadapt_R1)
Peter van 't Hof's avatar
Peter van 't Hof committed
210
      addSummarizable(cutadapt_R1, "clipping_R1")
211
      R1 = cutadapt_R1.fastq_output
212
      deps_R1 ::= R1
213
214
215
      outputFiles += ("cutadapt_R1_stats" -> cutadapt_R1.stats_output)

      if (paired) {
Peter van 't Hof's avatar
Peter van 't Hof committed
216
        val cutadapt_R2 = Cutadapt(this, R2.get, swapExt(outDir, R2.get, R2_ext, ".clip" + R2_ext))
217
        outputFiles += ("cutadapt_R2_stats" -> cutadapt_R2.stats_output)
218
        cutadapt_R2.fastqc = fastqc_R2
219
        cutadapt_R2.isIntermediate = true
220
        cutadapt_R2.deps = deps_R2
221
        add(cutadapt_R2)
Peter van 't Hof's avatar
Peter van 't Hof committed
222
        addSummarizable(cutadapt_R2, "clipping_R2")
Peter van 't Hof's avatar
Peter van 't Hof committed
223
224
        R2 = Some(cutadapt_R2.fastq_output)
        deps_R2 ::= R2.get
225

bow's avatar
bow committed
226
227
228
229
230
        val fqSync = new FastqSync(this)
        fqSync.refFastq = cutadapt_R1.fastq_input
        fqSync.inputFastq1 = cutadapt_R1.fastq_output
        fqSync.inputFastq2 = cutadapt_R2.fastq_output
        fqSync.outputFastq1 = swapExt(outDir, R1, R1_ext, ".sync" + R1_ext)
Peter van 't Hof's avatar
Peter van 't Hof committed
231
        fqSync.outputFastq2 = swapExt(outDir, R2.get, R2_ext, ".sync" + R2_ext)
bow's avatar
bow committed
232
233
234
        fqSync.outputStats = swapExt(outDir, R1, R1_ext, ".sync.stats")
        fqSync.deps :::= deps
        add(fqSync)
Peter van 't Hof's avatar
Peter van 't Hof committed
235
        addSummarizable(fqSync, "fastq_sync")
bow's avatar
bow committed
236
237
        outputFiles += ("syncStats" -> fqSync.outputStats)
        R1 = fqSync.outputFastq1
Peter van 't Hof's avatar
Peter van 't Hof committed
238
        R2 = Some(fqSync.outputFastq2)
239
        deps_R1 ::= R1
Peter van 't Hof's avatar
Peter van 't Hof committed
240
        deps_R2 ::= R2.get
241
242
243
244
245
246
247
248
      }
    }

    if (!skipTrim) { // Quality trimming
      val sickle = new Sickle(this)
      sickle.input_R1 = R1
      sickle.output_R1 = swapExt(outDir, R1, R1_ext, ".trim" + R1_ext)
      if (paired) {
Peter van 't Hof's avatar
Peter van 't Hof committed
249
250
251
        sickle.input_R2 = R2.get
        sickle.output_R2 = swapExt(outDir, R2.get, R2_ext, ".trim" + R2_ext)
        sickle.output_singles = swapExt(outDir, R2.get, R2_ext, ".trim.singles" + R1_ext)
252
253
254
      }
      sickle.output_stats = swapExt(outDir, R1, R1_ext, ".trim.stats")
      sickle.deps = deps
Peter van 't Hof's avatar
Peter van 't Hof committed
255
256
      sickle.isIntermediate = true
      add(sickle)
Peter van 't Hof's avatar
Peter van 't Hof committed
257
      addSummarizable(sickle, "trimming")
258
      R1 = sickle.output_R1
Peter van 't Hof's avatar
Peter van 't Hof committed
259
      if (paired) R2 = Some(sickle.output_R2)
260
261
    }

bow's avatar
bow committed
262
    val seqstat_R1_after = SeqStat(this, R1, outDir)
263
    seqstat_R1_after.deps = deps_R1
264
    add(seqstat_R1_after)
265
    addSummarizable(seqstat_R1_after, "seqstat_R1_qc")
266
267

    if (paired) {
bow's avatar
bow committed
268
      val seqstat_R2_after = SeqStat(this, R2.get, outDir)
269
      seqstat_R2_after.deps = deps_R2
270
      add(seqstat_R2_after)
271
      addSummarizable(seqstat_R2_after, "seqstat_R2_qc")
272
273
274
    }

    outputFiles += (chunk + "output_R1" -> R1)
Peter van 't Hof's avatar
Peter van 't Hof committed
275
    if (paired) outputFiles += (chunk + "output_R2" -> R2.get)
276
277
278
    return (R1, R2, deps)
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
279
280
281
282
283
  /**
   * Adds last non chunkable jobs
   * @param fastq_R1
   * @param fastq_R2
   */
284
285
  def runFinalize(fastq_R1: List[File], fastq_R2: List[File]) {
    if (fastq_R1.length != fastq_R2.length && paired) throw new IllegalStateException("R1 and R2 file number is not the same")
Peter van 't Hof's avatar
Peter van 't Hof committed
286
287
    val R1 = new File(outputDir, R1_name + ".qc" + R1_ext + ".gz")
    val R2 = new File(outputDir, R2_name + ".qc" + R2_ext + ".gz")
288
289
290
291
292
293
294
295

    add(Gzip(this, fastq_R1, R1))
    if (paired) add(Gzip(this, fastq_R2, R2))

    outputFiles += ("output_R1_gzip" -> R1)
    if (paired) outputFiles += ("output_R2_gzip" -> R2)

    if (!skipTrim || !skipClip) {
Peter van 't Hof's avatar
Peter van 't Hof committed
296
      fastqc_R1_after = Fastqc(this, R1, new File(outputDir, R1_name + ".qc.fastqc/"))
297
      add(fastqc_R1_after)
298
      addSummarizable(fastqc_R1_after, "fastqc_R1_qc")
Peter van 't Hof's avatar
Peter van 't Hof committed
299

300
      if (paired) {
Peter van 't Hof's avatar
Peter van 't Hof committed
301
        fastqc_R2_after = Fastqc(this, R2, new File(outputDir, R2_name + ".qc.fastqc/"))
302
        add(fastqc_R2_after)
303
        addSummarizable(fastqc_R2_after, "fastqc_R2_qc")
304
305
306
      }
    }

307
    addSummaryJobs
308
309
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
310
311
312
313
314
315
  /**
   * Extracts file if file is compressed
   * @param file
   * @param runDir
   * @return returns extracted file
   */
Peter van 't Hof's avatar
Peter van 't Hof committed
316
  def extractIfNeeded(file: File, runDir: File): File = {
Peter van 't Hof's avatar
Peter van 't Hof committed
317
318
    if (file == null) return file
    else if (file.getName().endsWith(".gz") || file.getName().endsWith(".gzip")) {
319
320
321
322
323
324
325
      var newFile: File = swapExt(runDir, file, ".gz", "")
      if (file.getName().endsWith(".gzip")) newFile = swapExt(runDir, file, ".gzip", "")
      val zcatCommand = Zcat(this, file, newFile)
      zcatCommand.isIntermediate = true
      add(zcatCommand)
      return newFile
    } else if (file.getName().endsWith(".bz2")) {
Peter van 't Hof's avatar
Peter van 't Hof committed
326
      val newFile = swapExt(runDir, file, ".bz2", "")
327
328
329
330
331
332
333
334
335
      val pbzip2 = Pbzip2(this, file, newFile)
      pbzip2.isIntermediate = true
      add(pbzip2)
      return newFile
    } else return file
  }
}

object Flexiprep extends PipelineCommand