ExtractUnmappedReads.scala 2.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
package nl.lumc.sasc.biopet.pipelines.gears

import nl.lumc.sasc.biopet.core.BiopetQScript
import nl.lumc.sasc.biopet.extensions.picard.SamToFastq
import nl.lumc.sasc.biopet.extensions.samtools.SamtoolsView
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
10
11
 * Created by pjvanthof on 04/12/15.
 */
12
13
14
15
16
17
18
19
20
21
22
23
class ExtractUnmappedReads(val root: Configurable) extends QScript with BiopetQScript {

  var bamFile: File = _

  var outputName: String = _

  override def defaults = Map(
    "samtofastq" -> Map(
      "validationstringency" -> "LENIENT"
    )
  )

24
25
  lazy val paired: Boolean = config("paired_bam", default = true)

26
27
28
29
30
31
  def init(): Unit = {
    require(bamFile != null)
    if (outputName == null) outputName = bamFile.getName.stripSuffix(".bam")
  }

  def fastqUnmappedR1 = new File(outputDir, s"$outputName.unmapped.R1.fq.gz")
Peter van 't Hof's avatar
Peter van 't Hof committed
32
33
  def fastqUnmappedR2 = if (paired) Some(new File(outputDir, s"$outputName.unmapped.R2.fq.gz"))
  else None
34
35
36
37
38
39
  def fastqUnmappedSingletons = new File(outputDir, s"$outputName.unmapped.singletons.fq.gz")

  def biopetScript(): Unit = {
    val samtoolsViewSelectUnmapped = new SamtoolsView(this)
    samtoolsViewSelectUnmapped.input = bamFile
    samtoolsViewSelectUnmapped.b = true
Peter van 't Hof's avatar
Peter van 't Hof committed
40
    samtoolsViewSelectUnmapped.h = true
Peter van 't Hof's avatar
Peter van 't Hof committed
41
    samtoolsViewSelectUnmapped.output = swapExt(outputDir, bamFile, ".bam", ".unmapped.bam")
42
43
    if (paired) samtoolsViewSelectUnmapped.f = List("12")
    else samtoolsViewSelectUnmapped.f = List("4")
44
45
46
47
48
49
50
    samtoolsViewSelectUnmapped.isIntermediate = true
    add(samtoolsViewSelectUnmapped)

    // start bam to fastq (only on unaligned reads) also extract the matesam
    val samToFastq = new SamToFastq(this)
    samToFastq.input = samtoolsViewSelectUnmapped.output
    samToFastq.fastqR1 = fastqUnmappedR1
51
    if (paired) {
Peter van 't Hof's avatar
Peter van 't Hof committed
52
      samToFastq.fastqR2 = fastqUnmappedR2.get
53
54
55
      samToFastq.fastqUnpaired = fastqUnmappedSingletons
    }
    samToFastq.isIntermediate = !config("keep_unmapped_fastq", default = false).asBoolean
56
57
58
    add(samToFastq)
  }
}