How can I parse arguments from a config file to create a command line menu in R ?
2
0
Entering edit mode
6.4 years ago
dzisis1986 ▴ 70

I have a script called config.r and it is like that :

# General info
exp_name = 'Exp_1'
file_in = '/dim/data/SRR23445.fastq'
# Quality 
trim_q = 10
trim_str = 3 
# Mapping
multimaps = 3

I would like instead of changing the config file everytime to have an argument menu for each of those parameters. The user would me able to specify the name of the output file the parameters for mapping for quality etc How can i do this in R?

R arguments script • 3.0k views
ADD COMMENT
3
Entering edit mode

if you want to pass the arguments (similar to Unix - bash style), you need to use Rscript, instead of interactive R. Check this

https://www.r-bloggers.com/passing-arguments-to-an-r-script-from-command-lines/

ADD REPLY
1
Entering edit mode
ADD REPLY
1
Entering edit mode

may be snakemake can do this. It supports passing config files to R.

ADD REPLY
1
Entering edit mode
6.4 years ago
Joe 21k

It requires python externally I believe, but the extremely good Argparse library has been ported over to R, it does a lot of the heavy lifting in creating the interfaces for you:

https://cran.r-project.org/web/packages/argparse/index.html

There are many ways, both the janky, and the robust, to read in a config file of arguments that Argparse can use. See these links for more:

Here's an example script of mine if you want to see how it's used. The output of Rscript AverageMatrices.R --help looks like (like you might expect from any commandline tool):

usage: AverageMatrices.R [-h] -i INFILES [INFILES ...] [-s SEPARATOR] -o
                         OUTFILE

optional arguments:
  -h, --help            show this help message and exit
  -i INFILES [INFILES ...], --infiles INFILES [INFILES ...]
                        All the matrices to average.
  -s SEPARATOR, --separator SEPARATOR
                        The field separator for the input matrices (they
                        should all match). [Def = "tab"].
  -o OUTFILE, --outfile OUTFILE
                        Output file to store the averaged matrix in.

Script:

ADD COMMENT
0
Entering edit mode
6.4 years ago
dzisis1986 ▴ 70

I would like to have a menu in which either you can use default config for the default values, or provide the user with the option to make a user config . More specific:

a. Necessary args: Input file, output directory

b.If user-config is passed as arg, use rest of values from there

c.If no user-config is passed as arg, use the default-config for values

d.If any variable is passed as arg, overwrite all the above for it

ADD COMMENT
0
Entering edit mode

For any kind of complicated scenario, you can always wrap your Rscript inside a regular shell script and take full advantage of powerful command line parsing of Shell arguments (eg. by using getopts)

https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/

ADD REPLY
0
Entering edit mode

I dont want to do it in Bash. I want to do it in R. I want to be able to run for example config.r -exp EXP1 -path /dim/data/SRR23445.fast -q 10 - str 3 -m 3 etc and this will overwrite the existing config file and then this config file will be used for my pipeline with source

ADD REPLY

Login before adding your answer.

Traffic: 1985 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6