XcnvToBed.scala 2.04 KB
Newer Older
Sander Bollen's avatar
Sander Bollen committed
1
2
package nl.lumc.sasc.biopet.tools

Sander Bollen's avatar
Sander Bollen committed
3
import java.io.{ BufferedWriter, File, FileWriter, PrintWriter }
Sander Bollen's avatar
Sander Bollen committed
4
5
6
7
8
9
10

import nl.lumc.sasc.biopet.tools.VepNormalizer.Args
import nl.lumc.sasc.biopet.utils.ToolCommand

import scala.io.Source

/**
Sander Bollen's avatar
Sander Bollen committed
11
12
 * Created by Sander Bollen on 24-11-16.
 */
Sander Bollen's avatar
Sander Bollen committed
13
14
15
16
17
18
19
object XcnvToBed extends ToolCommand {

  def main(args: Array[String]): Unit = {
    val commandArgs: Args = new OptParser()
      .parse(args, Args())
      .getOrElse(throw new IllegalArgumentException)

Sander Bollen's avatar
Sander Bollen committed
20
    val writer = new PrintWriter(commandArgs.outputBed)
Sander Bollen's avatar
Sander Bollen committed
21
22
23
24
25
26
    Source.fromFile(commandArgs.inputXcnv).
      getLines().
      filter(!_.startsWith("SAMPLE")).
      map(x => x.split("\t")).
      map(x => XcnvBedLine(x(0), x(1), x(2))).
      filter(_.sample == commandArgs.sample).
Sander Bollen's avatar
Sander Bollen committed
27
      foreach(x => writer.println(x.toString))
Sander Bollen's avatar
Sander Bollen committed
28
29

    writer.close()
Sander Bollen's avatar
Sander Bollen committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  }

  case class XcnvBedLine(sample: String, cnvType: String, location: String) {
    override def toString: String = {
      val cnv = if (cnvType == "DEL") {
        -1
      } else if (cnvType == "DUP") {
        1
      } else 0

      val locs = location.split(":")
      val chr = locs(0)
      val start = locs(1).split("-")(0)
      val stop = locs(1).split("-")(1)
Sander Bollen's avatar
Sander Bollen committed
44
      s"$chr\t$start\t$stop\t$cnv"
Sander Bollen's avatar
Sander Bollen committed
45
46
47
    }
  }

Sander Bollen's avatar
Sander Bollen committed
48
  case class Args(inputXcnv: File = null, outputBed: File = null, sample: String = null) extends AbstractArgs
Sander Bollen's avatar
Sander Bollen committed
49
50
51
52
53
54
55
56
57
58
59

  class OptParser extends AbstractOptParser {
    head("Convert a sample track within an XHMM XCNV file to a BED track. Fourt column indicates deletion (-1), normal (0) or duplication (1) of region")

    opt[File]('I', "Input") required () valueName "<xcnv>" action { (x, c) =>
      c.copy(inputXcnv = x)
    } validate {
      x => if (x.exists) success else failure("Input XCNV not found")
    } text {
      "Input XCNV file"
    }
Sander Bollen's avatar
Sander Bollen committed
60
    opt[File]('O', "Output") required () valueName "<bed>" action { (x, c) =>
Sander Bollen's avatar
Sander Bollen committed
61
62
63
64
      c.copy(outputBed = x)
    } text {
      "Output BED file"
    }
Sander Bollen's avatar
Sander Bollen committed
65
    opt[String]('S', "Sample") required () action { (x, c) =>
Sander Bollen's avatar
Sander Bollen committed
66
67
68
69
70
71
72
      c.copy(sample = x)
    } text {
      "The sample which to select"
    }
  }

}