BamToChromSizes.scala 1.62 KB
Newer Older
bow's avatar
bow committed
1
2
3
4
5
6
7
8
9
10
/**
 * 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
 *
11
 * A dual licensing mode is applied. The source code within this project is freely available for non-commercial use under an AGPL
bow's avatar
bow committed
12
13
14
 * 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

Peter van 't Hof's avatar
Peter van 't Hof committed
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
Peter van 't Hof's avatar
Peter van 't Hof committed
22
23
import org.broadinstitute.gatk.utils.commandline.{ Input, Output }

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

/**
Peter van 't Hof's avatar
Peter van 't Hof committed
27
28
 * Class to extract chrom.sizes files from a bam file
 *
Peter van 't Hof's avatar
Peter van 't Hof committed
29
30
 * 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
  }
}