Skip to content
Snippets Groups Projects
Commit 7b260498 authored by bow's avatar bow
Browse files

Add relative symlink option to Ln wrapper

parent 1a6df3ca
No related branches found
No related tags found
No related merge requests found
package nl.lumc.sasc.biopet.function
import nl.lumc.sasc.biopet.core.config.Configurable
import org.broadinstitute.gatk.queue.function.InProcessFunction
import org.broadinstitute.gatk.utils.commandline.{ Input, Output }
import java.io.File
import scala.sys.process.Process
import org.apache.commons.io.FilenameUtils;
import org.broadinstitute.gatk.queue.function.InProcessFunction
import org.broadinstitute.gatk.utils.commandline.{ Input, Output }
import nl.lumc.sasc.biopet.core.config.Configurable
class Ln(val root: Configurable) extends InProcessFunction with Configurable {
this.analysisName = getClass.getSimpleName
......@@ -15,8 +16,61 @@ class Ln(val root: Configurable) extends InProcessFunction with Configurable {
@Output(doc = "Link destination")
var out: File = _
var relative: Boolean = true
private lazy val inCanonical: String = {
// need to remove "/~" to correctly expand path with tilde
in.getCanonicalPath().replace("/~", "")
}
private lazy val outCanonical: String = {
out.getCanonicalPath().replace("/~", "")
}
private lazy val inToks: Array[String] = {
inCanonical.split(File.separator)
}
private lazy val outToks: Array[String] = {
outCanonical.split(File.separator)
}
private lazy val commonPrefixLength: Int = {
val maxLength = scala.math.min(inToks.length, outToks.length)
var i: Int = 0;
while (i < maxLength && inToks(i) == outToks(i)) i += 1;
i
}
private lazy val inUnique: String = {
inToks.slice(commonPrefixLength, inToks.length).mkString(File.separator)
}
private lazy val outUnique: String = {
outToks.slice(commonPrefixLength, outToks.length).mkString(File.separator)
}
private lazy val inRelative: String = {
// calculate 'distance' from output directory to input
// which is the number of directory walks required to get to the inUnique directory from outDir
val outDir = FilenameUtils.getFullPathNoEndSeparator(outUnique)
val dist: Int = scala.math.max(0, outDir.split(File.separator).length - 1)
val result =
if (dist > 0)
((".." + File.separator) * dist) + File.separator + inUnique
else
inUnique
result
}
override def run {
val cmd = "ln -s " + in + " " + out
val cmd =
if (relative) {
// workaround until we have `ln` that works with relative path (i.e. `ln -r`)
"ln -s " + inRelative + " " + outCanonical
} else {
"ln -s " + inCanonical + " " + outCanonical
}
val process = Process(cmd).run
System.out.println("cmd: '" + cmd + "', exitcode: " + process.exitValue)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment