Commit 2540e8fd authored by Peter van 't Hof's avatar Peter van 't Hof

Inspection round biopet utils

parent 2c840f96
......@@ -31,7 +31,7 @@ class Bam2Wig(val parent: Configurable) extends QScript with BiopetQScript {
def this() = this(null)
@Input(doc = "Input bam file", required = true)
var bamFile: File = null
var bamFile: File = _
def init(): Unit = {
inputFiles :+= new InputFile(bamFile)
......
......@@ -32,7 +32,7 @@ class BamToChromSizesTest extends TestNGSuite with Matchers {
}
@Test
def testChromSizes: Unit = {
def testChromSizes(): Unit = {
val bamFile = new File(resourcePath("/empty.bam"))
val bamToChromSizes = new BamToChromSizes(null)
bamToChromSizes.bamFile = bamFile
......
......@@ -13,8 +13,8 @@
<tr><th>Version</th><td>${run.version}</td></tr>
<tr><th>Last commit hash</th><td>${run.commitHash}</td></tr>
<tr><th>Output directory</th><td>${run.outputDir}</td></tr>
<tr><th>Sample</th><td>${allSamples.filter(_.id == sampleId.get).headOption.map(_.name)}</td></tr>
<tr><th>Library</th><td>${allLibraries.filter(_.id == libId.get).headOption.map(_.name)}</td></tr>
<tr><th>Sample</th><td>${allSamples.find(_.id == sampleId.get).map(_.name)}</td></tr>
<tr><th>Library</th><td>${allLibraries.find(_.id == libId.get).map(_.name)}</td></tr>
</tbody>
</table>
<br/>
......
......@@ -48,7 +48,7 @@ class BamMetrics(val parent: Configurable)
override def defaults = Map("bedtoolscoverage" -> Map("sorted" -> true))
/** returns files to store in summary */
def summaryFiles =
def summaryFiles: Map[String, File] =
Map("reference" -> referenceFasta(), "input_bam" -> inputBam) ++
ampliconBedFile.map("amplicon" -> _).toMap ++
ampliconBedFile.map(x => "roi_" + x.getName.stripSuffix(".bed") -> x).toMap
......@@ -58,7 +58,7 @@ class BamMetrics(val parent: Configurable)
Map("amplicon_name" -> ampliconBedFile.collect { case x => x.getName.stripSuffix(".bed") },
"roi_name" -> roiBedFiles.map(_.getName.stripSuffix(".bed")))
override def reportClass = {
override def reportClass: Some[BammetricsReport] = {
val bammetricsReport = new BammetricsReport(this)
bammetricsReport.outputDir = new File(outputDir, "report")
bammetricsReport.summaryDbFile = summaryDbFile
......
......@@ -487,7 +487,7 @@ object BammetricsReport extends ReportBuilder {
}
def writeTableToTsv(tsvFile: File, table: Map[String, Array[Any]], firstColumn: String): Unit = {
require(table.map(_._2.size).toList.distinct.size == 1,
require(table.map(_._2.length).toList.distinct.size == 1,
"Not all values has the same number or rows")
val keys = table.keys.filterNot(_ == firstColumn).toList.sorted
val writer = new PrintWriter(tsvFile)
......
......@@ -44,7 +44,7 @@ class BamMetricsTest extends TestNGSuite with Matchers {
}
@DataProvider(name = "bammetricsOptions")
def bammetricsOptions = {
def bammetricsOptions: Array[Array[AnyVal]] = {
val rois = Array(0, 1, 2, 3)
val bool = Array(true, false)
......@@ -57,7 +57,7 @@ class BamMetricsTest extends TestNGSuite with Matchers {
private var dirs: List[File] = Nil
@Test(dataProvider = "bammetricsOptions")
def testBamMetrics(rois: Int, amplicon: Boolean, rna: Boolean, wgs: Boolean) = {
def testBamMetrics(rois: Int, amplicon: Boolean, rna: Boolean, wgs: Boolean): Unit = {
val outputDir = Files.createTempDir()
dirs :+= outputDir
val map = ConfigUtils.mergeMaps(Map("output_dir" -> outputDir,
......@@ -74,8 +74,6 @@ class BamMetricsTest extends TestNGSuite with Matchers {
bammetrics.libId = Some("1")
bammetrics.script()
var regions: Int = rois + (if (amplicon) 1 else 0)
bammetrics.functions.count(_.isInstanceOf[CollectRnaSeqMetrics]) shouldBe (if (rna) 1 else 0)
bammetrics.functions.count(_.isInstanceOf[CollectWgsMetrics]) shouldBe (if (wgs) 1 else 0)
bammetrics.functions.count(_.isInstanceOf[CollectMultipleMetrics]) shouldBe 1
......@@ -86,13 +84,13 @@ class BamMetricsTest extends TestNGSuite with Matchers {
}
// remove temporary run directory all tests in the class have been run
@AfterClass def removeTempOutputDir() = {
@AfterClass def removeTempOutputDir(): Unit = {
dirs.foreach(FileUtils.deleteDirectory)
}
}
object BamMetricsTest {
val inputDir = Files.createTempDir()
val inputDir: File = Files.createTempDir()
val bam = new File(inputDir, "bla.bam")
Files.touch(bam)
......@@ -100,7 +98,7 @@ object BamMetricsTest {
Files.touch(ampliconBed)
def roi(i: Int): File = {
val roi = new File(inputDir, s"roi${i}.bed")
val roi = new File(inputDir, s"roi$i.bed")
Files.touch(roi)
roi
}
......
......@@ -42,7 +42,7 @@ class Basty(val parent: Configurable) extends QScript with MultiSampleQScript {
def variantcallers = List("unifiedgenotyper")
val numBoot = config("boot_runs", default = 100, namespace = "raxml").asInt
val numBoot: Int = config("boot_runs", default = 100, namespace = "raxml").asInt
override def defaults = Map(
"ploidy" -> 1,
......
......@@ -48,7 +48,7 @@ class BastyTest extends TestNGSuite with Matchers {
}
@DataProvider(name = "bastyOptions")
def bastyOptions = {
def bastyOptions: Array[Array[Any]] = {
for (s1 <- sample1; s2 <- sample2) yield Array("", s1, s2)
}
......@@ -114,7 +114,7 @@ class BastyTest extends TestNGSuite with Matchers {
numberSamples
else 0)
pipeline.functions.count(_.isInstanceOf[BaseRecalibrator]) shouldBe (if (dbsnp && baseRecalibration)
(numberLibs * 2)
numberLibs * 2
else 0)
pipeline.functions.count(_.isInstanceOf[PrintReads]) shouldBe (if (dbsnp && baseRecalibration)
numberLibs
......@@ -125,7 +125,7 @@ class BastyTest extends TestNGSuite with Matchers {
pipeline.summaryFiles shouldBe Map()
pipeline.samples foreach {
case (sampleId, sample) =>
case (_, sample) =>
sample.summarySettings shouldBe Map()
sample.summaryFiles.get("variants_fasta") should not be None
sample.summaryFiles.get("consensus_fasta") should not be None
......@@ -135,7 +135,7 @@ class BastyTest extends TestNGSuite with Matchers {
sample.summaryFiles.get("snps_only_consensus_variants_fasta") should not be None
sample.summaryStats shouldBe Map()
sample.libraries.foreach {
case (libId, lib) =>
case (_, lib) =>
lib.summarySettings shouldBe Map()
lib.summaryFiles shouldBe Map()
lib.summaryStats shouldBe Map()
......@@ -154,14 +154,14 @@ class BastyTest extends TestNGSuite with Matchers {
}
// remove temporary run directory all tests in the class have been run
@AfterClass def removeTempOutputDir() = {
@AfterClass def removeTempOutputDir(): Unit = {
dirs.foreach(FileUtils.deleteDirectory)
}
}
object BastyTest {
def outputDir = Files.createTempDir()
val inputDir = Files.createTempDir()
def outputDir: File = Files.createTempDir()
val inputDir: File = Files.createTempDir()
def inputTouch(name: String): String = {
val file = new File(inputDir, name)
......
......@@ -35,7 +35,7 @@ object BamUtils {
*
* @throws IllegalArgumentException
* @param bamFiles input bam files
* @return
* @return Map of sample bam files
*/
def sampleBamMap(bamFiles: List[File]): Map[String, File] = {
val temp = bamFiles.map { file =>
......@@ -59,7 +59,7 @@ object BamUtils {
*
* @param inputBam input bam file
* @param contig contig to scan for
* @param end postion to stop scanning
* @param end position to stop scanning
* @return Int with insertsize for this contig
*/
def contigInsertSize(inputBam: File,
......@@ -82,7 +82,7 @@ object BamUtils {
val counts: mutable.Map[Int, Int] = mutable.Map()
for (i <- 0 until samplingSize if samIterator.hasNext) {
for (_ <- 0 until samplingSize if samIterator.hasNext) {
val rec = samIterator.next()
val isPaired = rec.getReadPairedFlag
val minQ10 = rec.getMappingQuality >= 10
......@@ -97,27 +97,24 @@ object BamUtils {
counts.keys.size match {
case 1 => Some(counts.keys.head)
case 0 => None
case _ => {
case _ =>
Some(counts.foldLeft(0)((old, observation) => {
observation match {
case (insertSize: Int, observations: Int) => {
case (insertSize: Int, observations: Int) =>
(old + (insertSize * observations)) / (observations + 1)
}
case _ => 0
}
}))
}
}
})
insertSizesOnAllFragments.size match {
case 1 => Some(insertSizesOnAllFragments.head)
case 0 => None
case _ => {
case _ =>
Some(insertSizesOnAllFragments.foldLeft(0)((old, observation) => {
(old + observation) / 2
}))
}
}
}
......@@ -140,10 +137,10 @@ object BamUtils {
binSize)
})
.toList
val counts = bamInsertSizes.flatMap(x => x)
val counts = bamInsertSizes.flatten
// avoid division by zero
if (counts.size != 0) counts.sum / counts.size
if (counts.nonEmpty) counts.sum / counts.size
else 0
}
......@@ -161,7 +158,7 @@ object BamUtils {
}.toMap
/** This class will add functionality to [[SAMSequenceDictionary]] */
implicit class SamDictCheck(samDics: SAMSequenceDictionary) {
implicit class SamDictCheck(samDicts: SAMSequenceDictionary) {
/**
* This method will check if all contig and sizes are the same without looking at the order of the contigs
......@@ -172,15 +169,15 @@ object BamUtils {
*/
def assertSameDictionary(that: SAMSequenceDictionary, ignoreOrder: Boolean): Unit = {
if (ignoreOrder) {
assert(samDics.getReferenceLength == that.getReferenceLength)
assert(samDicts.getReferenceLength == that.getReferenceLength)
val thisContigNames =
samDics.getSequences.map(x => (x.getSequenceName, x.getSequenceLength)).sorted.toSet
samDicts.getSequences.map(x => (x.getSequenceName, x.getSequenceLength)).sorted.toSet
assert(
thisContigNames == that.getSequences
.map(x => (x.getSequenceName, x.getSequenceLength))
.sorted
.toSet)
} else samDics.assertSameDictionary(that)
} else samDicts.assertSameDictionary(that)
}
}
}
......@@ -35,7 +35,7 @@ object ConfigUtils extends Logging {
*
* @param map1 input map
* @param map2 input map
* @return Uniqe map1
* @return Unique map1
*/
def uniqueKeys(map1: Map[String, Any], map2: Map[String, Any]): Map[String, Any] = {
filterEmtpyMapValues(
......@@ -57,32 +57,34 @@ object ConfigUtils extends Logging {
*/
def filterEmtpyMapValues(map: Map[String, Any]): Map[String, Any] = {
map.filter {
case (key, value: Map[_, _]) => value.nonEmpty
case (_, value: Map[_, _]) => value.nonEmpty
case _ => true
}
}
/**
* Merge 2 maps, when value is in a map in map1 and map2 the value calls recursively this function
*
* @param map1 Prio over map2
* @param map2 Backup for map1
* @return merged map
*/
//noinspection ScalaUnnecessaryParentheses,ScalaUnnecessaryParentheses,ScalaUnnecessaryParentheses,ScalaUnnecessaryParentheses,ScalaUnnecessaryParentheses
def mergeMaps(
map1: Map[String, Any],
map2: Map[String, Any],
resolveConflict: (Any, Any, String) => Any = (m1, m2, key) => m1): Map[String, Any] = {
resolveConflict: (Any, Any, String) => Any = (m1, _, _) => m1): Map[String, Any] = {
(for (key <- map1.keySet.++(map2.keySet)) yield {
if (!map2.contains(key)) (key -> map1(key))
else if (!map1.contains(key)) (key -> map2(key))
if (!map2.contains(key)) key -> map1(key)
else if (!map1.contains(key)) key -> map2(key)
else {
map1(key) match {
case m1: Map[_, _] =>
map2(key) match {
case m2: Map[_, _] => (key -> mergeMaps(any2map(m1), any2map(m2), resolveConflict))
case _ => (key -> map1(key))
case m2: Map[_, _] => key -> mergeMaps(any2map(m1), any2map(m2), resolveConflict)
case _ => key -> map1(key)
}
case _ => (key -> resolveConflict(map1(key), map2(key), key))
case _ => key -> resolveConflict(map1(key), map2(key), key)
}
}
}).toMap
......@@ -122,7 +124,7 @@ object ConfigUtils extends Logging {
} else Some(map)
}
/** Make json aboject from a file */
/** Make json object from a file */
def fileToJson(configFile: File): Json = {
logger.debug("Jsonfile: " + configFile)
val jsonText = scala.io.Source.fromFile(configFile).mkString
......@@ -172,15 +174,16 @@ object ConfigUtils extends Logging {
lazy val yaml = new Yaml()
def mapToYaml(map: Map[String, Any]) = yaml.dump(yaml.load(ConfigUtils.mapToJson(map).nospaces))
def mapToYaml(map: Map[String, Any]): JsonField =
yaml.dump(yaml.load(ConfigUtils.mapToJson(map).nospaces))
def mapToYamlFile(map: Map[String, Any], outputFile: File) = {
def mapToYamlFile(map: Map[String, Any], outputFile: File): Unit = {
val writer = new PrintWriter(outputFile)
writer.println(mapToYaml(map))
writer.close()
}
def mapToJsonFile(map: Map[String, Any], outputFile: File) = {
def mapToJsonFile(map: Map[String, Any], outputFile: File): Unit = {
val writer = new PrintWriter(outputFile)
writer.println(anyToJson(map).toString())
writer.close()
......@@ -531,13 +534,13 @@ object ConfigUtils extends Logging {
/** Convert ConfigValue to List[Double] */
implicit def configValue2doubleList(value: ConfigValue): List[Double] = {
if (requiredValue(value)) any2list(value.value).map(any2double(_))
if (requiredValue(value)) any2list(value.value).map(any2double)
else Nil
}
/** Convert ConfigValue to List[Int] */
implicit def configValue2intList(value: ConfigValue): List[Int] = {
if (requiredValue(value)) any2list(value.value).map(any2int(_))
if (requiredValue(value)) any2list(value.value).map(any2int)
else Nil
}
......
......@@ -43,7 +43,7 @@ object FastaUtils {
private var dictCache: Map[File, SAMSequenceDictionary] = Map()
/** This will clear the dict cache */
def clearCache() = {
def clearCache(): Unit = {
dictCache = Map()
}
......@@ -84,8 +84,8 @@ object FastaUtils {
.map { line =>
val columns = line.split("\t")
val refContig = columns(0)
val alterniveNames = columns(1).split(";").toSet
refContig -> alterniveNames
val alternativeNames = columns(1).split(";").toSet
refContig -> alternativeNames
}
.toMap
reader.close()
......
......@@ -72,12 +72,7 @@ object IoUtils {
else fname
}
/**
* This return the contends of a file as a List[String]
*
* @param file
* @return
*/
/** This return the contends of a file as a List[String] */
def getLinesFromFile(file: File): List[String] = {
val reader = Source.fromFile(file)
val lines = reader.getLines().toList
......@@ -87,16 +82,15 @@ object IoUtils {
def executableExist(exe: String): Boolean = {
try {
val process = Process(Seq(exe)).run()
true
} catch {
case e: IOException => false
case _: IOException => false
}
}
def writeLinesToFile(output: File, lines: List[String]): Unit = {
val writer = new PrintWriter(output)
lines.foreach(writer.println(_))
lines.foreach(writer.println)
writer.close()
}
}
......@@ -19,12 +19,12 @@ package nl.lumc.sasc.biopet.utils
*/
class LazyCheck[T](function: => T) {
private var _isSet = false
def isSet = _isSet
lazy val value = {
def isSet: Boolean = _isSet
lazy val value: T = {
val cache = function
_isSet = true
cache
}
def apply() = value
def get = value
def apply(): T = value
def get: T = value
}
......@@ -19,7 +19,7 @@ package nl.lumc.sasc.biopet.utils
*/
trait MainCommand {
lazy val commandName = this.getClass.getSimpleName
lazy val commandName: String = this.getClass.getSimpleName
.split("\\$")
.last
......
......@@ -22,24 +22,24 @@ object Question {
def string(name: String,
default: Option[String] = None,
description: Option[String] = None,
posibleValues: List[String] = Nil,
possibleValues: List[String] = Nil,
validation: List[(String) => Boolean] = Nil): String = {
description.foreach(println)
if (posibleValues.nonEmpty) println(s"possible values: ${posibleValues.mkString(", ")}")
if (possibleValues.nonEmpty) println(s"possible values: ${possibleValues.mkString(", ")}")
default.foreach(x => println(s"Default value: $x"))
print(s"$name > ")
(scala.io.StdIn.readLine.trim, default) match {
case (a, Some(d)) if a.isEmpty => d
case (a, None) if a.isEmpty =>
println("ERROR: Value is required")
string(name, default, description, posibleValues, validation)
string(name, default, description, possibleValues, validation)
case (a, _) =>
if (!validation.forall(_(a))) {
println("ERROR: Validation of failed")
string(name, default, description, posibleValues, validation)
} else if (posibleValues.nonEmpty && !posibleValues.contains(a)) {
string(name, default, description, possibleValues, validation)
} else if (possibleValues.nonEmpty && !possibleValues.contains(a)) {
println("ERROR: Value not allowed")
string(name, default, description, posibleValues, validation)
string(name, default, description, possibleValues, validation)
} else a
}
}
......@@ -69,24 +69,24 @@ object Question {
def list(name: String,
default: Option[List[String]] = None,
description: Option[String] = None,
posibleValues: List[String] = Nil,
validation: (String) => Boolean = String => true): List[String] = {
possibleValues: List[String] = Nil,
validation: (String) => Boolean = _ => true): List[String] = {
description.foreach(println)
if (posibleValues.nonEmpty) println(s"possible values: ${posibleValues.mkString(", ")}")
if (possibleValues.nonEmpty) println(s"possible values: ${possibleValues.mkString(", ")}")
default.foreach(x => println(s"Default value: $x"))
print(s"$name > ")
(scala.io.StdIn.readLine.split(",").toList.map(_.trim), default) match {
case (List(""), Some(d)) => d
case (List(""), None) =>
println("ERROR: Value is required")
list(name, default, description, posibleValues, validation)
list(name, default, description, possibleValues, validation)
case (a, _) =>
if (!a.forall(validation)) {
println("ERROR: Validation of failed")
list(name, default, description, posibleValues, validation)
} else if (posibleValues.nonEmpty && !a.forall(posibleValues.contains)) {
list(name, default, description, possibleValues, validation)
} else if (possibleValues.nonEmpty && !a.forall(possibleValues.contains)) {
println("ERROR: Value not allowed")
list(name, default, description, posibleValues, validation)
list(name, default, description, possibleValues, validation)
} else a
}
}
......
package nl.lumc.sasc.biopet.utils
import scala.util.matching.Regex
/**
* Created by pjvanthof on 29/04/2017.
......@@ -34,7 +35,7 @@ case class SemanticVersion(major: Int, minor: Int, patch: Int, build: Option[Str
}
object SemanticVersion {
val semanticVersionRegex = "[vV]?(\\d+)\\.(\\d+)\\.(\\d+)(-.*)?".r
val semanticVersionRegex: Regex = "[vV]?(\\d+)\\.(\\d+)\\.(\\d+)(-.*)?".r
/**
* Check whether a version string is a semantic version.
......
......@@ -25,7 +25,7 @@ trait ToolCommand extends MainCommand with Logging {
protected abstract class AbstractArgs {}
/**
* Abstract opt parser to add efault args to each biopet tool
* Abstract opt parser to add default args to each biopet tool
*/
protected abstract class AbstractOptParser extends scopt.OptionParser[Args](commandName) {
opt[String]('l', "log_level") foreach { x =>
......
......@@ -31,7 +31,7 @@ object VcfUtils {
* @param vcfRecord record to check
* @return allele with most nucleotides
*/
def getLongestAllele(vcfRecord: VariantContext) = {
def getLongestAllele(vcfRecord: VariantContext): Allele = {
val alleles = vcfRecord.getAlleles
val longestAlleleId = alleles.map(_.getBases.length).zipWithIndex.maxBy(_._1)._2
alleles(longestAlleleId)
......
......@@ -52,8 +52,8 @@ case class Feature(contig: String,
attributes.map(x => x._1 + s"=${x._2}").mkString(";")
).mkString("\t")
def minPosition = if (start < end) start else end
def maxPosition = if (start > end) start else end
def minPosition: Int = if (start < end) start else end
def maxPosition: Int = if (start > end) start else end
}
object Feature {
......@@ -72,11 +72,11 @@ object Feature {
s"strand only allows '+' or '-', not ${values(6)}, gtf line: $line")
}
val artibutes = values.lift(8).map(_.split(";")).getOrElse(Array()).map {
case artibutesGtfRegex(key, value) => key -> value
case artibutesGffRegex(key, value) => key -> value
val attributes = values.lift(8).map(_.split(";")).getOrElse(Array()).map {
case attributesGtfRegex(key, value) => key -> value
case attributesGffRegex(key, value) => key -> value
case _ =>
throw new IllegalArgumentException(s"Atribute it not correct formatted, gtf line: $line")
throw new IllegalArgumentException(s"Attribute it not correct formatted, gtf line: $line")
} toMap
val score = values(5) match {
......@@ -99,9 +99,9 @@ object Feature {
score,
strand,
frame,
artibutes)
attributes)
}
lazy val artibutesGtfRegex: Regex = """\s*(\S*) "(.*)"$""".r
lazy val artibutesGffRegex: Regex = """\s*(\S*)=(.*)$""".r
lazy val attributesGtfRegex: Regex = """\s*(\S*) "(.*)"$""".r
lazy val attributesGffRegex: Regex = """\s*(\S*)=(.*)$""".r
}
......@@ -38,8 +38,8 @@ class Config(protected var _map: Map[String, Any],
loadDefaultConfig()
}
def map = _map
def defaults = _defaults
def map: Map[String, Any] = _map
def defaults: Map[String, Any] = _defaults
/**
* Loading a environmental variable as location of config files to merge into the config
......@@ -85,7 +85,7 @@ class Config(protected var _map: Map[String, Any],
}
/**
* Add a single vallue to the config
* Add a single value to the config
* @param key key of value
* @param value value itself
* @param path Path to value
......@@ -231,7 +231,7 @@ class Config(protected var _map: Map[String, Any],
map.foreach {
case (key, value: Map[_, _]) =>
reportUnused(value.asInstanceOf[Map[String, Any]], path :+ key)
case (key, value) =>
case (key, _) =>
logger.warn(
s"config key in user config is never used, key: $key" +
(if (path.nonEmpty) s", path: ${path.mkString(" -> ")}" else ""))
......@@ -303,7 +303,7 @@ object Config extends Logging {
if (p.isDefined)
Option(
ConfigValue(startIndex,
ConfigValueIndex(startIndex.module, path, startIndex.key, freeVar = true),
ConfigValueIndex(startIndex.module, path, startIndex.key),
p.get))
else None
} else None
......
......@@ -24,19 +24,19 @@ class ConfigValue(val requestIndex: ConfigValueIndex,
val default: Boolean) {
/** Get value as String */
def asString = any2string(value)
def asString: String = any2string(value)
/** Get value as File */
def asFile = new File(any2string(value))
/** Get value as Int */
def asInt = any2int(value)
def asInt: Int = any2int(value)
/** Get value as Double */
def asDouble = any2double(value)
def asDouble: Double = any2double(value)
/** Get value as List[Any] */
def asList = any2list(value)
def asList: List[Any] = any2list(value)
/** Get value as List[File] */
def asFileList: List[File] = for (file <- any2stringList(value)) yield new File(file)
......@@ -45,10 +45,10 @@ class ConfigValue(val requestIndex: ConfigValueIndex,
def asStringList: List[String] = any2stringList(value)
/** Get value as Map */
def asMap = any2map(value)
def asMap: Map[String, Any] = any2map(value)