BamToChromSizes.scala 1.63 KB
Newer Older
bow's avatar
bow committed
1
/**
2 3 4 5 6 7 8 9 10 11 12 13 14
  * 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 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.
  */
Peter van 't Hof's avatar
Peter van 't Hof committed
15 16
package nl.lumc.sasc.biopet.pipelines.bamtobigwig

17
import java.io.{File, PrintWriter}
Peter van 't Hof's avatar
Peter van 't Hof committed
18 19

import htsjdk.samtools.SamReaderFactory
Peter van 't Hof's avatar
Peter van 't Hof committed
20
import nl.lumc.sasc.biopet.utils.config.Configurable
Peter van 't Hof's avatar
Peter van 't Hof committed
21
import org.broadinstitute.gatk.queue.function.InProcessFunction
22
import org.broadinstitute.gatk.utils.commandline.{Input, Output}
Peter van 't Hof's avatar
Peter van 't Hof committed
23

Peter van 't Hof's avatar
Peter van 't Hof committed
24 25 26
import scala.collection.JavaConversions._

/**
27 28 29 30
  * Class to extract chrom.sizes files from a bam file
  *
  * Created by pjvan_thof on 1/29/15.
  */
Peter van 't Hof's avatar
Peter van 't Hof committed
31
class BamToChromSizes(val parent: Configurable) extends InProcessFunction with Configurable {
Peter van 't Hof's avatar
Peter van 't Hof committed
32 33 34 35 36 37 38 39 40 41 42 43 44
  @Input
  var bamFile: File = _

  @Output
  var chromSizesFile: File = _

  def run(): Unit = {
    val bamReader = SamReaderFactory.makeDefault().open(bamFile)
    val writer = new PrintWriter(chromSizesFile)
    for (ref <- bamReader.getFileHeader.getSequenceDictionary.getSequences) {
      writer.println(ref.getSequenceName + "\t" + ref.getSequenceLength)
    }
    bamReader.close()
Peter van 't Hof's avatar
Peter van 't Hof committed
45
    writer.close()
Peter van 't Hof's avatar
Peter van 't Hof committed
46 47
  }
}