VariantEffectPredictor.scala 13 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

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

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

Sander Bollen's avatar
Sander Bollen committed
264
265
266
  def summaryFiles: Map[String, File] = Map()

  def summaryStats: Map[String, Any] = {
Sander van der Zeeuw's avatar
Sander van der Zeeuw committed
267
    if (statsText) {
268
269
      val statsFile: File = new File(output.getAbsolutePath + "_summary.txt")
      parseStatsFile(statsFile)
Sander Bollen's avatar
Sander Bollen committed
270
271
272
273
274
275
276
277
    } else {
      Map()
    }
  }

  def parseStatsFile(file: File): Map[String, Any] = {
    val contents = Source.fromFile(file).getLines().toList
    val headers = getHeadersFromStatsFile(contents)
278
    headers.foldLeft(Map.empty[String, Any])((acc, x) => acc + (x.replace(" ", "_") -> getBlockFromStatsFile(contents, x)))
Sander Bollen's avatar
Sander Bollen committed
279
280
281
282
283
284
285
286
287
  }

  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("]")
      if (stripped == header) {
        inBlock = true
288
289
290
291
      } else {
        if (inBlock) {
          val key = stripped.split('\t').head.replace(" ", "_")
          val value = stripped.split('\t').last
292
          theMap ++= Map(key -> tryToParseNumber(value, fallBack = true).getOrElse(value))
293
        }
Sander Bollen's avatar
Sander Bollen committed
294
295
296
297
298
299
300
301
302
303
304
305
306
      }
      if (stripped == "") {
        inBlock = false
      }
    }
    theMap
  }

  def getHeadersFromStatsFile(contents: List[String]): List[String] = {
    // block headers are of format '[block]'
    contents.filter(_.startsWith("[")).filter(_.endsWith("]")).map(_.stripPrefix("[")).map(_.stripSuffix("]"))
  }

Sander Bollen's avatar
Sander Bollen committed
307
}