echo 'Here are the contents of my_file.txt' > my_file.txt
Add resources
Accessing additional resources inside a Viash component.
If a script needs access to an external file, it needs to be added as a resource in the config.
First, create a file called my_file.txt
.
echo 'Here are the contents of my_file.txt' > my_file.txt
echo 'Here are the contents of my_file.txt' > my_file.txt
echo 'Here are the contents of my_file.txt' > my_file.txt
echo 'Here are the contents of my_file.txt' > my_file.txt
echo 'Here are the contents of my_file.txt' > my_file.txt
Next, the file needs to be added to the config as a resource. This will let Viash know to copy the new file inside a component’s resource directory.
name: example_bash
description: A minimal example component.
arguments:
- type: file
name: --input
example: file.txt
required: true
- type: file
name: --output
direction: output
example: output.txt
required: true
resources:
- type: bash_script
path: script.sh
- path: my_file.txt
engines:
- type: docker
image: bash:4.0
- type: native
runners:
- type: executable
- type: nextflow
name: example_csharp
description: A minimal example component.
arguments:
- type: file
name: --input
example: file.txt
required: true
- type: file
name: --output
direction: output
example: output.txt
required: true
resources:
- type: csharp_script
path: script.csx
- path: my_file.txt
engines:
- type: docker
image: ghcr.io/data-intuitive/dotnet-script:1.3.1
- type: native
runners:
- type: executable
- type: nextflow
name: example_js
description: A minimal example component.
arguments:
- type: file
name: --input
example: file.txt
required: true
- type: file
name: --output
direction: output
example: output.txt
required: true
resources:
- type: javascript_script
path: script.js
- path: my_file.txt
engines:
- type: docker
image: node:19-bullseye-slim
- type: native
runners:
- type: executable
- type: nextflow
name: example_python
description: A minimal example component.
arguments:
- type: file
name: --input
example: file.txt
required: true
- type: file
name: --output
direction: output
example: output.txt
required: true
resources:
- type: python_script
path: script.py
- path: my_file.txt
engines:
- type: docker
image: python:3.10-slim
- type: native
runners:
- type: executable
- type: nextflow
name: example_r
description: A minimal example component.
arguments:
- type: file
name: --input
example: file.txt
required: true
- type: file
name: --output
direction: output
example: output.txt
required: true
resources:
- type: r_script
path: script.R
- path: my_file.txt
engines:
- type: docker
image: eddelbuettel/r2u:22.04
- type: native
runners:
- type: executable
- type: nextflow
name: example_scala
description: A minimal example component.
arguments:
- type: file
name: --input
example: file.txt
required: true
- type: file
name: --output
direction: output
example: output.txt
required: true
resources:
- type: scala_script
path: script.scala
- path: my_file.txt
engines:
- type: docker
image: sbtscala/scala-sbt:eclipse-temurin-19_36_1.7.2_2.13.10
- type: native
runners:
- type: executable
- type: nextflow
Lastly, to access a resource from within the script, use the resources_dir
meta-variable:
#!/bin/bash
## VIASH START
par_input=path/to/file.txt
par_output=output.txt
## VIASH END
# view resource file
cat "$meta_resources_dir/my_file.txt"
# copy file
echo "Copying '$par_input' to '$par_output'."
cp -r "$par_input" "$par_output"
using System.IO;
// VIASH START
var par = new {
= "path/to/file.txt",
input = "output.txt"
output };
// VIASH END
// view resource file
string myFile = $"{meta.resources_dir}/my_file.txt";
= File.ReadAllText(myFile);
string text .WriteLine(text);
Console
// copy file
.WriteLine($"Copying '{par.input}' to '{par.output}'.");
Console.Copy(par.input, par.output, true); File
const fs = require('fs');
// VIASH START
let par = {
'input': 'path/to/file.txt',
'output': 'output.txt'
;
}// VIASH END
// view resource file
const my_file = `${meta['resources_dir']}/my_file.txt`
.readFile(my_file, 'utf8', (err, data) => {
fsif (err) throw err;
console.log(data);
;
})
// copy file
console.log(`Copying '${par['input']}' to '${par['output']}'`)
.copyFile(par['input'], par['output'], (err) => {
fsif (err) throw err;
; })
import shutil
## VIASH START
= {
par 'input': 'file.txt',
'output': 'output.txt'
}## VIASH END
# view resource file
= f"{meta['resources_dir']}/my_file.txt"
my_file with open(my_file, "r") as f:
print(f.read())
# copy file
print(f"Copying '{par['input']}' to '{par['output']}'.")
'input'], par['output']) shutil.copyfile(par[
## VIASH START
<- list(
par "input" = 'file.txt',
"output" = 'output.txt'
)## VIASH END
# view resource file
<- readLines(paste0(meta$resources_dir, "/my_file.txt"))
lines cat(lines, sep = "\n")
# copy file
cat("Copying '", par$input, "' to '", par$output, "'.\n", sep = "")
file.copy(par$input, par$output)
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
import java.nio.file.Files
import java.nio.file.Paths
// VIASH START
case class ViashPar(input: String, output: String)
val par = ViashPar(
"path/to/file.txt",
"output.txt"
)
// VIASH END
// view resource file
val myFile = s"${meta.resources_dir}/my_file.txt"
val src = scala.io.Source.fromFile(myFile)
.getLines.foreach(println)
src
// copy file
println(s"Copying '${par.input}' to '${par.output}'.")
val fileIn = Paths.get(par.input)
val fileOut = Paths.get(par.output)
.copy(fileIn, fileOut, REPLACE_EXISTING) Files
Now we can run the component as follows:
viash run config.vsh.yaml -- --input config.vsh.yaml --output foo.txt
[notice] Checking if Docker image is available at 'example_bash:latest'
[warning] Could not pull from 'example_bash:latest'. Docker image doesn't exist or is not accessible.
[notice] Building container 'example_bash:latest' with Dockerfile
Here are the contents of my_file.txt
Copying '/viash_automount/tmp/RtmpBsgZ8t/add-dependencies4253268a52d7/bash/config.vsh.yaml' to '/viash_automount/tmp/RtmpBsgZ8t/add-dependencies4253268a52d7/bash/foo.txt'.
viash run config.vsh.yaml -- --input config.vsh.yaml --output foo.txt
[notice] Checking if Docker image is available at 'example_csharp:latest'
[warning] Could not pull from 'example_csharp:latest'. Docker image doesn't exist or is not accessible.
[notice] Building container 'example_csharp:latest' with Dockerfile
Here are the contents of my_file.txt
Copying '/viash_automount/tmp/RtmpBsgZ8t/add-dependencies4253268a52d7/csharp/config.vsh.yaml' to '/viash_automount/tmp/RtmpBsgZ8t/add-dependencies4253268a52d7/csharp/foo.txt'.
viash run config.vsh.yaml -- --input config.vsh.yaml --output foo.txt
[notice] Checking if Docker image is available at 'example_js:latest'
[warning] Could not pull from 'example_js:latest'. Docker image doesn't exist or is not accessible.
[notice] Building container 'example_js:latest' with Dockerfile
Copying '/viash_automount/tmp/RtmpBsgZ8t/add-dependencies4253268a52d7/js/config.vsh.yaml' to '/viash_automount/tmp/RtmpBsgZ8t/add-dependencies4253268a52d7/js/foo.txt'
Here are the contents of my_file.txt
viash run config.vsh.yaml -- --input config.vsh.yaml --output foo.txt
[notice] Checking if Docker image is available at 'example_python:latest'
[warning] Could not pull from 'example_python:latest'. Docker image doesn't exist or is not accessible.
[notice] Building container 'example_python:latest' with Dockerfile
Here are the contents of my_file.txt
Copying '/viash_automount/tmp/RtmpBsgZ8t/add-dependencies4253268a52d7/python/config.vsh.yaml' to '/viash_automount/tmp/RtmpBsgZ8t/add-dependencies4253268a52d7/python/foo.txt'.
viash run config.vsh.yaml -- --input config.vsh.yaml --output foo.txt
[notice] Checking if Docker image is available at 'example_r:latest'
[warning] Could not pull from 'example_r:latest'. Docker image doesn't exist or is not accessible.
[notice] Building container 'example_r:latest' with Dockerfile
Here are the contents of my_file.txt
Copying '/viash_automount/tmp/RtmpBsgZ8t/add-dependencies4253268a52d7/r/config.vsh.yaml' to '/viash_automount/tmp/RtmpBsgZ8t/add-dependencies4253268a52d7/r/foo.txt'.
[1] TRUE
viash run config.vsh.yaml -- --input config.vsh.yaml --output foo.txt
[notice] Checking if Docker image is available at 'example_scala:latest'
[warning] Could not pull from 'example_scala:latest'. Docker image doesn't exist or is not accessible.
[notice] Building container 'example_scala:latest' with Dockerfile
warning: 1 deprecation
warning: 1 deprecation (since 2.13.3)
warning: 2 deprecations in total; re-run with -deprecation for details
Here are the contents of my_file.txt
Copying '/viash_automount/tmp/RtmpBsgZ8t/add-dependencies4253268a52d7/scala/config.vsh.yaml' to '/viash_automount/tmp/RtmpBsgZ8t/add-dependencies4253268a52d7/scala/foo.txt'.