QcCommand.scala 3.15 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
2
3
4
5
package nl.lumc.sasc.biopet.pipelines.flexiprep

import java.io.File

import nl.lumc.sasc.biopet.core.{ BiopetCommandLineFunction, BiopetPipe }
Peter van 't Hof's avatar
Peter van 't Hof committed
6
import nl.lumc.sasc.biopet.extensions.{Cat, Gzip, Sickle, Cutadapt}
Peter van 't Hof's avatar
Peter van 't Hof committed
7
import nl.lumc.sasc.biopet.extensions.seqtk.SeqtkSeq
Peter van 't Hof's avatar
Peter van 't Hof committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import nl.lumc.sasc.biopet.utils.config.Configurable
import org.broadinstitute.gatk.utils.commandline.{ Output, Input }

/**
 * Created by pjvan_thof on 9/22/15.
 */
class QcCommand(val root: Configurable, val fastqc: Fastqc) extends BiopetCommandLineFunction {

  val flexiprep = root match {
    case f: Flexiprep => f
    case _            => throw new IllegalArgumentException("This class may only be used inside Flexiprep")
  }

  @Input(required = true)
  var input: File = _

  @Output(required = true)
  var output: File = _

Peter van 't Hof's avatar
Peter van 't Hof committed
27
28
29
30
  var compress = true

  var read: String = _

Peter van 't Hof's avatar
Peter van 't Hof committed
31
32
  override def beforeGraph(): Unit = {
    super.beforeGraph()
Peter van 't Hof's avatar
Peter van 't Hof committed
33
    require(read != null)
Peter van 't Hof's avatar
Peter van 't Hof committed
34
35
36
    deps :::= fastqc.outputFiles
  }

37
38
39
  override def defaultCoreMemory = 2.0
  override def defaultThreads = 3

Peter van 't Hof's avatar
Peter van 't Hof committed
40
41
42
  def cmdLine = {
    val seqtk = new SeqtkSeq(root)
    seqtk.input = input
Peter van 't Hof's avatar
Peter van 't Hof committed
43
44
45
46
47
48
49
50
51
52
    seqtk.Q = fastqc.encoding match {
      case null => None
      case enc if enc.contains("Sanger / Illumina 1.9") => None
      case enc if enc.contains("Illumina <1.3") => Option(64)
      case enc if enc.contains("Illumina 1.3") => Option(64)
      case enc if enc.contains("Illumina 1.5") => Option(64)
      case _ => None
    }
    if (seqtk.Q.isDefined) seqtk.V = true

Peter van 't Hof's avatar
Peter van 't Hof committed
53
54
55
56
    val clip = if (!flexiprep.skipClip) {
      val foundAdapters = fastqc.foundAdapters.map(_.seq)
      if (foundAdapters.nonEmpty) {
        val cutadept = new nl.lumc.sasc.biopet.extensions.Cutadapt(root)
Peter van 't Hof's avatar
Peter van 't Hof committed
57
        cutadept.stats_output = new File(flexiprep.outputDir, s"${flexiprep.sampleId.getOrElse("x")}-${flexiprep.libId.getOrElse("x")}.$read.clip.stats")
Peter van 't Hof's avatar
Peter van 't Hof committed
58
59
60
        if (cutadept.default_clip_mode == "3") cutadept.opt_adapter ++= foundAdapters
        else if (cutadept.default_clip_mode == "5") cutadept.opt_front ++= foundAdapters
        else if (cutadept.default_clip_mode == "both") cutadept.opt_anywhere ++= foundAdapters
Peter van 't Hof's avatar
Peter van 't Hof committed
61
62
63
64
65
66
        Some(cutadept)
      } else None
    } else None

    val trim = if (!flexiprep.skipTrim) {
      val sickle = new nl.lumc.sasc.biopet.extensions.Sickle(root)
Peter van 't Hof's avatar
Peter van 't Hof committed
67
      sickle.output_stats = new File(flexiprep.outputDir, s"${flexiprep.sampleId.getOrElse("x")}-${flexiprep.libId.getOrElse("x")}.$read.trim.stats")
Peter van 't Hof's avatar
Peter van 't Hof committed
68
69
      Some(sickle)
    } else None
Peter van 't Hof's avatar
Peter van 't Hof committed
70
71
72
73
    val outputCommand = {
      if (compress) new Gzip(root)
      else new Cat(root)
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
74
75
76
77

    val cmd = (clip, trim) match {
      case (Some(clip), Some(trim)) => {
        clip.fastq_output = Right(trim)
Peter van 't Hof's avatar
Peter van 't Hof committed
78
        trim.output_R1 = Right(outputCommand > output)
Peter van 't Hof's avatar
Peter van 't Hof committed
79
80
81
        seqtk | clip
      }
      case (Some(clip), _) => {
Peter van 't Hof's avatar
Peter van 't Hof committed
82
        clip.fastq_output = Right(outputCommand > output)
Peter van 't Hof's avatar
Peter van 't Hof committed
83
84
85
        seqtk | clip
      }
      case (_, Some(trim)) => {
Peter van 't Hof's avatar
Peter van 't Hof committed
86
        trim.output_R1 = Right(outputCommand > output)
Peter van 't Hof's avatar
Peter van 't Hof committed
87
88
89
        seqtk | trim
      }
      case _ => {
Peter van 't Hof's avatar
Peter van 't Hof committed
90
        seqtk | outputCommand > output
Peter van 't Hof's avatar
Peter van 't Hof committed
91
      }
Peter van 't Hof's avatar
Peter van 't Hof committed
92
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
93
94

    //val cmds = (Some(seqtk) :: clip :: trim :: Some(new Gzip(root)) :: Nil).flatten
Peter van 't Hof's avatar
Peter van 't Hof committed
95
96
97
98
    cmd.beforeGraph()
    cmd.commandLine
  }
}