VariantEffectPredictor.scala 13.2 KB
Newer Older
bow's avatar
bow committed
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.
 */
Sander Bollen's avatar
Sander Bollen committed
16
17
18
19
package nl.lumc.sasc.biopet.extensions

import java.io.File

Sander Bollen's avatar
Sander Bollen committed
20
import nl.lumc.sasc.biopet.core.summary.Summarizable
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import nl.lumc.sasc.biopet.utils.Logging
Peter van 't Hof's avatar
Peter van 't Hof committed
22
import nl.lumc.sasc.biopet.utils.config.Configurable
23
import nl.lumc.sasc.biopet.core.{ Version, BiopetCommandLineFunction, Reference }
24
import nl.lumc.sasc.biopet.utils.tryToParseNumber
Peter van 't Hof's avatar
Peter van 't Hof committed
25
import org.broadinstitute.gatk.utils.commandline.{ Input, Output }
Sander Bollen's avatar
Sander Bollen committed
26

Sander Bollen's avatar
Sander Bollen committed
27
28
import scala.io.Source

Sander Bollen's avatar
Sander Bollen committed
29
/**
Peter van 't Hof's avatar
Peter van 't Hof committed
30
 * Extension for VariantEffectPredictor
Sander Bollen's avatar
Sander Bollen committed
31
32
 * Created by ahbbollen on 15-1-15.
 */
Sander Bollen's avatar
Sander Bollen committed
33
class VariantEffectPredictor(val root: Configurable) extends BiopetCommandLineFunction with Reference with Version with Summarizable {
Sander Bollen's avatar
Sander Bollen committed
34

Sander Bollen's avatar
Sander Bollen committed
35
  executable = config("exe", namespace = "perl", default = "perl")
Peter van 't Hof's avatar
Peter van 't Hof committed
36
  var vepScript: String = config("vep_script")
Sander Bollen's avatar
Sander Bollen committed
37
38

  @Input(doc = "input VCF", required = true)
39
  var input: File = null
Sander Bollen's avatar
Sander Bollen committed
40
41

  @Output(doc = "output file", required = true)
42
  var output: File = null
Sander Bollen's avatar
Sander Bollen committed
43

44
45
  def versionRegex = """version (\d*)""".r
  def versionCommand = executable + " " + vepScript + " --help"
Sander Bollen's avatar
Sander Bollen committed
46
47

  //Boolean vars
48
49
  var v: Boolean = config("v", default = true, freeVar = false)
  var q: Boolean = config("q", default = false, freeVar = false)
Sander Bollen's avatar
Sander Bollen committed
50
  var offline: Boolean = config("offline", default = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
51
  var noProgress: Boolean = config("no_progress", default = false)
Peter van 't Hof's avatar
Peter van 't Hof committed
52
  var everything: Boolean = config("everything", default = false)
Sander Bollen's avatar
Sander Bollen committed
53
  var force: Boolean = config("force", default = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
54
55
  var noStats: Boolean = config("no_stats", default = false)
  var statsText: Boolean = config("stats_text", default = true)
Sander Bollen's avatar
Sander Bollen committed
56
57
58
59
  var html: Boolean = config("html", default = false)
  var cache: Boolean = config("cache", default = false)
  var humdiv: Boolean = config("humdiv", default = false)
  var regulatory: Boolean = config("regulatory", default = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
60
  var cellType: Boolean = config("cell_type", default = false)
Sander Bollen's avatar
Sander Bollen committed
61
  var phased: Boolean = config("phased", default = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
62
  var alleleNumber: Boolean = config("allele_number", default = false)
Sander Bollen's avatar
Sander Bollen committed
63
64
  var numbers: Boolean = config("numbers", default = false)
  var domains: Boolean = config("domains", default = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
65
  var noEscape: Boolean = config("no_escape", default = false)
Sander Bollen's avatar
Sander Bollen committed
66
67
68
69
70
71
72
73
  var hgvs: Boolean = config("hgvs", default = false)
  var protein: Boolean = config("protein", default = false)
  var symbol: Boolean = config("symbol", default = false)
  var ccds: Boolean = config("ccds", default = false)
  var uniprot: Boolean = config("uniprot", default = false)
  var tsl: Boolean = config("tsl", default = false)
  var canonical: Boolean = config("canonical", default = false)
  var biotype: Boolean = config("biotype", default = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
74
75
76
77
  var xrefRefseq: Boolean = config("xref_refseq", default = false)
  var checkExisting: Boolean = config("check_existing", default = false)
  var checkAlleles: Boolean = config("check_alleles", default = false)
  var checkSvs: Boolean = config("svs", default = false)
Sander Bollen's avatar
Sander Bollen committed
78
  var gmaf: Boolean = config("gmaf", default = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
79
80
81
  var maf1kg: Boolean = config("maf_1kg", default = false)
  var mafEsp: Boolean = config("maf_esp", default = false)
  var oldMap: Boolean = config("old_maf", default = false)
Sander Bollen's avatar
Sander Bollen committed
82
  var pubmed: Boolean = config("pubmed", default = false)
Sander Bollen's avatar
Sander Bollen committed
83

84
85
  var vcf: Boolean = config("vcf", default = true, freeVar = false)
  var json: Boolean = config("json", default = false, freeVar = false)
Sander Bollen's avatar
Sander Bollen committed
86
  var gvf: Boolean = config("gvf", default = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
87
88
89
  var checkRef: Boolean = config("check_ref", default = false)
  var codingOnly: Boolean = config("coding_only", default = false)
  var noIntergenic: Boolean = config("no_intergenic", default = false)
Sander Bollen's avatar
Sander Bollen committed
90
  var pick: Boolean = config("pick", default = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
91
92
93
94
95
  var pickAllele: Boolean = config("pick_allele", default = false)
  var flagPick: Boolean = config("flag_pick", default = false)
  var flagPickAllele: Boolean = config("flag_pick_allele", default = false)
  var perGene: Boolean = config("per_gene", default = false)
  var mostSevere: Boolean = config("most_severe", default = false)
Sander Bollen's avatar
Sander Bollen committed
96
  var summary: Boolean = config("summary", default = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
97
98
99
  var filterCommon: Boolean = config("filter_common", default = false)
  var checkFrequency: Boolean = config("check_frequency", default = false)
  var allowNonVariant: Boolean = config("allow_non_variant", default = false)
Sander Bollen's avatar
Sander Bollen committed
100
101
  var database: Boolean = config("database", default = false)
  var genomes: Boolean = config("genomes", default = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
102
  var gencodeBasic: Boolean = config("gencode_basic", default = false)
Sander Bollen's avatar
Sander Bollen committed
103
104
  var refseq: Boolean = config("refseq", default = false)
  var merged: Boolean = config("merged", default = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
105
  var allRefseq: Boolean = config("all_refseq", default = false)
Sander Bollen's avatar
Sander Bollen committed
106
  var lrg: Boolean = config("lrg", default = false)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
107
108
  var noWholeGenome: Boolean = config("no_whole_genome", default = false)
  var skibDbCheck: Boolean = config("skip_db_check", default = false)
Sander Bollen's avatar
Sander Bollen committed
109
110

  // Textual args
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
111
  var vepConfig: Option[String] = config("config", freeVar = false)
112
  var species: Option[String] = config("species", freeVar = false)
Sander Bollen's avatar
Sander Bollen committed
113
114
115
  var assembly: Option[String] = config("assembly")
  var format: Option[String] = config("format")
  var dir: Option[String] = config("dir")
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
116
117
  var dirCache: Option[String] = config("dir_cache")
  var dirPlugins: Option[String] = config("dir_plugins")
Sander Bollen's avatar
Sander Bollen committed
118
119
120
  var fasta: Option[String] = config("fasta")
  var sift: Option[String] = config("sift")
  var polyphen: Option[String] = config("polyphen")
121
  var custom: List[String] = config("custom", default = Nil)
Sander Bollen's avatar
Sander Bollen committed
122
123
124
125
126
127
  var plugin: List[String] = config("plugin", default = Nil)
  var individual: Option[String] = config("individual")
  var fields: Option[String] = config("fields")
  var convert: Option[String] = config("convert")
  var terms: Option[String] = config("terms")
  var chr: Option[String] = config("chr")
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
128
129
130
131
  var pickOrder: Option[String] = config("pick_order")
  var freqPop: Option[String] = config("check_pop")
  var freqGtLt: Option[String] = config("freq_gt_lt")
  var freqFilter: Option[String] = config("freq_filter")
Sander Bollen's avatar
Sander Bollen committed
132
133
134
135
136
137
138
  var filter: Option[String] = config("filter")
  var host: Option[String] = config("host")
  var user: Option[String] = config("user")
  var password: Option[String] = config("password")
  var registry: Option[String] = config("registry")
  var build: Option[String] = config("build")
  var compress: Option[String] = config("compress")
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
139
  var cacheRegionSize: Option[String] = config("cache_region_size")
Sander Bollen's avatar
Sander Bollen committed
140
141

  // Numeric args
Peter van 't Hof's avatar
Peter van 't Hof committed
142
  override def defaultThreads: Int = config("fork", default = 2)
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
143
144
  var cacheVersion: Option[Int] = config("cache_version")
  var freqFreq: Option[Float] = config("freq_freq")
Sander Bollen's avatar
Sander Bollen committed
145
  var port: Option[Int] = config("port")
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
146
147
  var dbVersion: Option[Int] = config("db_version")
  var bufferSize: Option[Int] = config("buffer_size")
Sander Bollen's avatar
Sander Bollen committed
148
149
  // ought to be a flag, but is BUG in VEP; becomes numeric ("1" is true)
  var failed: Option[Int] = config("failed")
Sander Bollen's avatar
Sander Bollen committed
150

151
152
  override def defaultCoreMemory = 4.0

153
154
155
  @Output
  private var _summary: File = null

Peter van 't Hof's avatar
Peter van 't Hof committed
156
157
  override def beforeGraph(): Unit = {
    super.beforeGraph()
158
    if (!cache && !database) {
Sander Bollen's avatar
Sander Bollen committed
159
      Logging.addError("Must either set 'cache' or 'database' to true for VariantEffectPredictor")
Sander Bollen's avatar
Sander Bollen committed
160
    } else if (cache && dir.isEmpty) {
Sander Bollen's avatar
Sander Bollen committed
161
      Logging.addError("Must supply 'dir_cache' to cache for VariantEffectPredictor")
162
    }
163
    if (statsText) _summary = new File(output.getAbsolutePath + "_summary.txt")
164
165
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
166
  /** Returns command to execute */
Sander Bollen's avatar
Sander Bollen committed
167
  def cmdLine = required(executable) +
Peter van 't Hof's avatar
Peter van 't Hof committed
168
    required(vepScript) +
Sander Bollen's avatar
Sander Bollen committed
169
170
171
172
    required("-i", input) +
    required("-o", output) +
    conditional(v, "-v") +
    conditional(q, "-q") +
173
    conditional(offline, "--offline") +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
174
    conditional(noProgress, "--no_progress") +
Sander Bollen's avatar
Sander Bollen committed
175
176
    conditional(everything, "--everything") +
    conditional(force, "--force_overwrite") +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
177
178
    conditional(noStats, "--no_stats") +
    conditional(statsText, "--stats_text") +
Sander Bollen's avatar
Sander Bollen committed
179
180
181
182
    conditional(html, "--html") +
    conditional(cache, "--cache") +
    conditional(humdiv, "--humdiv") +
    conditional(regulatory, "--regulatory") +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
183
    conditional(cellType, "--cel_type") +
Sander Bollen's avatar
Sander Bollen committed
184
    conditional(phased, "--phased") +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
185
    conditional(alleleNumber, "--allele_number") +
Sander Bollen's avatar
Sander Bollen committed
186
187
    conditional(numbers, "--numbers") +
    conditional(domains, "--domains") +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
188
    conditional(noEscape, "--no_escape") +
Sander Bollen's avatar
Sander Bollen committed
189
190
191
192
193
194
195
196
    conditional(hgvs, "--hgvs") +
    conditional(protein, "--protein") +
    conditional(symbol, "--symbol") +
    conditional(ccds, "--ccds") +
    conditional(uniprot, "--uniprot") +
    conditional(tsl, "--tsl") +
    conditional(canonical, "--canonical") +
    conditional(biotype, "--biotype") +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
197
198
199
200
    conditional(xrefRefseq, "--xref_refseq") +
    conditional(checkExisting, "--check_existing") +
    conditional(checkAlleles, "--check_alleles") +
    conditional(checkSvs, "--check_svs") +
Sander Bollen's avatar
Sander Bollen committed
201
    conditional(gmaf, "--gmaf") +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
202
203
    conditional(maf1kg, "--maf_1kg") +
    conditional(mafEsp, "--maf_esp") +
Sander Bollen's avatar
Sander Bollen committed
204
205
206
207
    conditional(pubmed, "--pubmed") +
    conditional(vcf, "--vcf") +
    conditional(json, "--json") +
    conditional(gvf, "--gvf") +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
208
209
210
    conditional(checkRef, "--check_ref") +
    conditional(codingOnly, "--coding_only") +
    conditional(noIntergenic, "--no_intergenic") +
Sander Bollen's avatar
Sander Bollen committed
211
    conditional(pick, "--pick") +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
212
213
214
215
216
    conditional(pickAllele, "--pick_allele") +
    conditional(flagPick, "--flag_pick") +
    conditional(flagPickAllele, "--flag_pick_allele") +
    conditional(perGene, "--per_gene") +
    conditional(mostSevere, "--most_severe") +
Sander Bollen's avatar
Sander Bollen committed
217
    conditional(summary, "--summary") +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
218
219
220
    conditional(filterCommon, "--filter_common") +
    conditional(checkFrequency, "--check_frequency") +
    conditional(allowNonVariant, "--allow_non_variant") +
Sander Bollen's avatar
Sander Bollen committed
221
222
    conditional(database, "--database") +
    conditional(genomes, "--genomes") +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
223
    conditional(gencodeBasic, "--gencode_basic") +
Sander Bollen's avatar
Sander Bollen committed
224
225
    conditional(refseq, "--refseq") +
    conditional(merged, "--merged") +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
226
    conditional(allRefseq, "--all_refseq") +
Sander Bollen's avatar
Sander Bollen committed
227
    conditional(lrg, "--lrg") +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
228
229
230
    conditional(noWholeGenome, "--no_whole_genome") +
    conditional(skibDbCheck, "--skip_db_check") +
    optional("--config", vepConfig) +
Sander Bollen's avatar
Sander Bollen committed
231
232
233
234
    optional("--species", species) +
    optional("--assembly", assembly) +
    optional("--format", format) +
    optional("--dir", dir) +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
235
236
    optional("--dir_cache", dirCache) +
    optional("--dir_plugins", dirPlugins) +
Sander Bollen's avatar
Sander Bollen committed
237
238
239
    optional("--fasta", fasta) +
    optional("--sift", sift) +
    optional("--polyphen", polyphen) +
240
    repeat("--custom", custom) +
Sander Bollen's avatar
Sander Bollen committed
241
242
243
244
245
246
    repeat("--plugin", plugin) +
    optional("--individual", individual) +
    optional("--fields", fields) +
    optional("--convert", convert) +
    optional("--terms", terms) +
    optional("--chr", chr) +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
247
248
249
250
    optional("--pick_order", pickOrder) +
    optional("--freq_pop", freqPop) +
    optional("--freq_gt_lt", freqGtLt) +
    optional("--freq_filter", freqFilter) +
Sander Bollen's avatar
Sander Bollen committed
251
252
253
254
255
256
257
    optional("--filter", filter) +
    optional("--host", host) +
    optional("--user", user) +
    optional("--password", password) +
    optional("--registry", registry) +
    optional("--build", build) +
    optional("--compress", compress) +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
258
    optional("--cache_region_size", cacheRegionSize) +
259
    optional("--fork", threads) +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
260
261
    optional("--cache_version", cacheVersion) +
    optional("--freq_freq", freqFreq) +
Sander Bollen's avatar
Sander Bollen committed
262
    optional("--port", port) +
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
263
264
    optional("--db_version", dbVersion) +
    optional("--buffer_size", bufferSize) +
Sander Bollen's avatar
Sander Bollen committed
265
    optional("--failed", failed)
Sander Bollen's avatar
Sander Bollen committed
266

Sander Bollen's avatar
Sander Bollen committed
267
268
269
  def summaryFiles: Map[String, File] = Map()

  def summaryStats: Map[String, Any] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
270
271
    if (statsText) parseStatsFile(new File(output.getAbsolutePath + "_summary.txt"))
    else Map()
Sander Bollen's avatar
Sander Bollen committed
272
273
  }

274
275
276
  override def resolveSummaryConflict(v1: Any, v2: Any, key: String): Any = {
    (v1, v2) match {
      case (x1: Int, x2: Int) => x1 + x2
Peter van 't Hof's avatar
Peter van 't Hof committed
277
      case _                  => throw new IllegalStateException("Value are not Int's, unable to sum them up")
278
279
280
    }
  }

Sander Bollen's avatar
Sander Bollen committed
281
  def parseStatsFile(file: File): Map[String, Any] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
282
283
284
    val reader = Source.fromFile(file)
    val contents = reader.getLines().filter(_ != "").toList
    reader.close()
Peter van 't Hof's avatar
Peter van 't Hof committed
285
286
287
288
    val headers = contents
      .filter(x => x.startsWith("[") && x.endsWith("]"))
      .map(_.stripPrefix("[").stripSuffix("]"))

289
    headers.foldLeft(Map.empty[String, Any])((acc, x) => acc + (x.replace(" ", "_") -> getBlockFromStatsFile(contents, x)))
Sander Bollen's avatar
Sander Bollen committed
290
291
292
293
294
295
296
  }

  def getBlockFromStatsFile(contents: List[String], header: String): Map[String, Any] = {
    var inBlock = false
    var theMap: Map[String, Any] = Map()
    for (x <- contents) {
      val stripped = x.stripPrefix("[").stripSuffix("]")
297
298
      if (stripped == header) inBlock = true
      else {
299
300
301
        if (inBlock) {
          val key = stripped.split('\t').head.replace(" ", "_")
          val value = stripped.split('\t').last
302
          theMap ++= Map(key -> tryToParseNumber(value, fallBack = true).getOrElse(value))
303
        }
Peter van 't Hof's avatar
Peter van 't Hof committed
304
305
      }
      if (stripped == "") inBlock = false
Sander Bollen's avatar
Sander Bollen committed
306
307
308
309
    }
    theMap
  }

Sander Bollen's avatar
Sander Bollen committed
310
}