Commit a87a80de authored by Peter van 't Hof's avatar Peter van 't Hof
Browse files

Added scopts library and log4j to ToolCommands

parent 2ce33667
......@@ -66,6 +66,11 @@
<artifactId>algebird-core_2.10</artifactId>
<version>0.8.1</version>
</dependency>
<dependency>
<groupId>com.github.scopt</groupId>
<artifactId>scopt_2.10</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
<build>
<resources>
......
package nl.lumc.sasc.biopet.core
import org.broadinstitute.gatk.queue.util.Logging
trait MainCommand extends Logging {
trait MainCommand {
lazy val name = this.getClass.getSimpleName
.split("\\$").last
......
package nl.lumc.sasc.biopet.core
import org.broadinstitute.gatk.queue.util.Logging
trait PipelineCommand extends MainCommand {
trait PipelineCommand extends MainCommand with Logging {
def pipeline = "/" + getClass.getName.stripSuffix("$").replaceAll("\\.", "/") + ".class"
......
package nl.lumc.sasc.biopet.core
import java.text.SimpleDateFormat
import java.util.Calendar
import org.apache.log4j.Logger
import org.apache.log4j.WriterAppender
import org.apache.log4j.helpers.DateLayout
import org.apache.log4j.spi.LoggingEvent
import java.text.DateFormat
trait ToolCommand extends MainCommand {
abstract class AbstractArgs {
}
abstract class AbstractOptParser extends scopt.OptionParser[Args](getClass.getSimpleName) {
head(getClass.getSimpleName)
opt[String]('l', "log") foreach { x =>
x.toLowerCase match {
case "debug" => logger.setLevel(org.apache.log4j.Level.DEBUG)
case "info" => logger.setLevel(org.apache.log4j.Level.INFO)
case "warn" => logger.setLevel(org.apache.log4j.Level.WARN)
case "error" => logger.setLevel(org.apache.log4j.Level.ERROR)
case _ =>
} } text("Log level") // TODO: add Enum to validation
}
type Args <: AbstractArgs
type OptParser <: AbstractOptParser
protected val logger = Logger.getLogger(this.getClass.getSimpleName.stripSuffix("$"))
logger.setLevel(org.apache.log4j.Level.INFO)
logger.addAppender(new WriterAppender(new DateLayout() {
val ignoresThrowable = false
def format(event:org.apache.log4j.spi.LoggingEvent): String = {
val calendar: Calendar = Calendar.getInstance
calendar.setTimeInMillis(event.getTimeStamp)
val formatter: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
val formattedDate: String = formatter.format(calendar.getTime)
var logLevel = event.getLevel.toString
while (logLevel.size < 6) logLevel += " "
logLevel + " [" + formattedDate + "] [" + event.getLoggerName + "] " + event.getMessage + "\n"
}
}, sys.process.stderr))
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ import htsjdk.samtools.SAMFileReader
import htsjdk.samtools.SAMRecord
import java.io.File
import nl.lumc.sasc.biopet.core.BiopetJavaCommandLineFunction
import nl.lumc.sasc.biopet.core.ToolCommand
import nl.lumc.sasc.biopet.core.config.Configurable
import org.broadinstitute.gatk.utils.commandline.{ Input, Output }
import org.broadinstitute.gatk.queue.util.Logging
......@@ -22,10 +23,10 @@ class BiopetFlagstat(val root: Configurable) extends BiopetJavaCommandLineFuncti
override val defaultVmem = "8G"
memoryLimit = Option(4.0)
override def commandLine = super.commandLine + required(input) + " > " + required(output)
override def commandLine = super.commandLine + required("I", input) + " > " + required(output)
}
object BiopetFlagstat extends Logging {
object BiopetFlagstat extends ToolCommand {
def apply(root: Configurable, input: File, output: File): BiopetFlagstat = {
val flagstat = new BiopetFlagstat(root)
flagstat.input = input
......@@ -38,12 +39,32 @@ object BiopetFlagstat extends Logging {
flagstat.output = new File(outputDir, input.getName.stripSuffix(".bam") + ".biopetflagstat")
return flagstat
}
case class Args (inputFile:File = null, region:Option[String] = None) extends AbstractArgs
class OptParser extends AbstractOptParser {
opt[File]('I', "inputFile") required() valueName("<file>") action { (x, c) =>
c.copy(inputFile = x) } text("out is a required file property")
opt[String]('r', "region") valueName("<chr:start-stop>") action { (x, c) =>
c.copy(region = Some(x)) } text("out is a required file property")
}
/**
* @param args the command line arguments
*/
def main(args: Array[String]): Unit = {
val inputSam = new SAMFileReader(new File(args(0)))
val argsParser = new OptParser
val commandArgs: Args = argsParser.parse(args, Args()) getOrElse sys.exit(1)
val inputSam = new SAMFileReader(commandArgs.inputFile)
val iterSam = if (commandArgs.region == None) inputSam.iterator else {
val regionRegex = """(.*):(.*)-(.*)""".r
commandArgs.region.get match {
case regionRegex(chr, start, stop) => inputSam.query(chr, start.toInt, stop.toInt, false)
case _ => sys.error("Region wrong format")
}
}
val flagstatCollector = new FlagstatCollector
flagstatCollector.loadDefaultFunctions
val m = 10
......@@ -90,9 +111,9 @@ object BiopetFlagstat extends Logging {
record.getReferenceIndex == record.getMateReferenceIndex)
flagstatCollector.addFunction("Mate on other chr", record => record.getReadPairedFlag && record.getReferenceIndex != record.getMateReferenceIndex)
for (record <- inputSam.iterator) {
for (record <- iterSam) {
if (flagstatCollector.readsCount % 1e6 == 0 && flagstatCollector.readsCount > 0)
System.err.println("Reads prosessed: " + flagstatCollector.readsCount)
logger.info("Reads prosessed: " + flagstatCollector.readsCount)
flagstatCollector.loadRecord(record)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment