SquishBed.scala 1.86 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1 2 3 4 5 6 7 8 9 10 11 12
package nl.lumc.sasc.biopet.tools

import java.io.File

import nl.lumc.sasc.biopet.core.ToolCommand
import nl.lumc.sasc.biopet.utils.intervals.BedRecordList

/**
 * Created by pjvanthof on 22/08/15.
 */
object SquishBed extends ToolCommand {

Peter van 't Hof's avatar
Peter van 't Hof committed
13 14 15
  case class Args(input: File = null,
                  output: File = null,
                  strandSensitive: Boolean = false) extends AbstractArgs
Peter van 't Hof's avatar
Peter van 't Hof committed
16 17 18 19 20 21 22 23

  class OptParser extends AbstractOptParser {
    opt[File]('I', "input") required () valueName "<file>" action { (x, c) =>
      c.copy(input = x)
    }
    opt[File]('o', "output") required () unbounded () valueName "<file>" action { (x, c) =>
      c.copy(output = x)
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
24 25 26
    opt[Unit]('s', "strandSensitive") unbounded () valueName "<file>" action { (x, c) =>
      c.copy(strandSensitive = true)
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
27 28 29 30 31 32 33 34 35 36 37
  }

  /**
   * @param args the command line arguments
   */
  def main(args: Array[String]): Unit = {
    val argsParser = new OptParser
    val cmdArgs: Args = argsParser.parse(args, Args()) getOrElse sys.exit(1)

    if (!cmdArgs.input.exists) throw new IllegalStateException("Input file not found, file: " + cmdArgs.input)

Peter van 't Hof's avatar
Peter van 't Hof committed
38 39
    logger.info("Start")

Peter van 't Hof's avatar
Peter van 't Hof committed
40
    val records = BedRecordList.fromFile(cmdArgs.input)
Peter van 't Hof's avatar
Peter van 't Hof committed
41
    val length = records.length
42
    val refLength = records.combineOverlap.length
Peter van 't Hof's avatar
Peter van 't Hof committed
43 44 45
    logger.info(s"Total bases: $length")
    logger.info(s"Total bases on reference: $refLength")
    logger.info("Start squishing")
46
    val squishBed = records.squishBed(cmdArgs.strandSensitive).sorted
Peter van 't Hof's avatar
Peter van 't Hof committed
47 48
    logger.info("Done squishing")
    val squishLength = squishBed.length
49
    val squishRefLength = squishBed.combineOverlap.length
Peter van 't Hof's avatar
Peter van 't Hof committed
50 51 52
    logger.info(s"Total bases left: $squishLength")
    logger.info(s"Total bases left on reference: $squishRefLength")
    logger.info(s"Total bases removed from ref: ${refLength - squishRefLength}")
Peter van 't Hof's avatar
Peter van 't Hof committed
53
    squishBed.writeToFile(cmdArgs.output)
Peter van 't Hof's avatar
Peter van 't Hof committed
54 55

    logger.info("Done")
Peter van 't Hof's avatar
Peter van 't Hof committed
56 57
  }
}