Last updated: 2022-08-15

Checks: 7 0

Knit directory: bioinformatics_tips/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20200503) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 2df43f5. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/get_option.Rmd) and HTML (docs/get_option.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 2df43f5 Dave Tang 2022-08-15 Include Python example
html e9934e7 davetang 2020-06-07 Security basics
html 56b7e19 davetang 2020-05-10 Build site.
Rmd 778bf20 davetang 2020-05-10 Copy code button
html 04be0ae davetang 2020-05-09 Build site.
Rmd f8b797f davetang 2020-05-09 No TOC
html 60d127f davetang 2020-05-09 Build site.
html 169cfdf davetang 2020-05-09 Build site.
Rmd 83ae7f7 davetang 2020-05-09 Command line arguments

If you have used Unix tools on the command line, you may have noticed that you can provide different arguments/options to the tool to modify its behaviour. For example using the ls command by itself will simply return files and directories (without a . prefix) that exist in your current working directory.

ls
analysis
bioinformatics_tips.Rproj
code
data
docs
LICENSE
output
README.md
ref
script
_workflowr.yml

If you want to show all files, use the -a (short) or --all (long) option.

ls -a
.
..
analysis
bioinformatics_tips.Rproj
code
data
docs
.git
.gitattributes
.gitignore
LICENSE
output
README.md
ref
.Rhistory
.Rprofile
.Rproj.user
script
_workflowr.yml

You can write scripts in different languages that mimic this behaviour. It is preferable to write scripts that accept command line arguments because it makes it easy to reuse the script on a different dataset or rerun the script using different parameters. In addition, this makes it easy to incorporate the script into a bioinformatics pipeline.

Some concepts to understand are usage, short and long options, and positional and optional arguments.

  • The usage explains the details of your script and it is good practice to display a script’s usage when no arguments are provided.
  • Short and long options are simply a preference but I prefer long options as they are readable and informative. Most modern day command line tools provide both options.
  • Positional arguments are mandatory arguments that need to be specified in a specific order. For example the cp command will copy the first positional argument to the last positional argument. Optional arguments are non-mandatory and can be specified using short and long options; their order does not matter.

Below are examples of writing scripts that accept command line arguments in Python, Bash, R, and Perl.

Python

In Python use the argparse module.

#!/usr/bin/env python3
#
# based on the argparse tutorial https://docs.python.org/3/howto/argparse.html
#

import argparse
parser = argparse.ArgumentParser()

#
# positional arguments
#

# default type is string
parser.add_argument(
        "echo",
        help = "display a string",
)
# specify integer type
parser.add_argument(
        "number",
        help = "display a number",
        type = int
)

#
# optional arguments
#

# short and long options
# store True if specified
parser.add_argument(
        "-v",
        "--verbose",
        help = "verbose mode",
        action = "store_true"
)

# set choices for argument and default value
parser.add_argument(
        "-p",
        "--threads",
        help = "number of threads",
        choices = range(1,9),
        default = 2,
        type = int
)

args = parser.parse_args()

if args.verbose:
    print("Verbose mode")

if args.threads:
    print("Using %d threads" % args.threads)

print("%s's type is %s" % (args.echo, type(args.echo)))
print("%s's type is %s" % (args.number, type(args.number)))

If you run the Python script by itself, a simple usage will be displayed. (Ignore the || true command, which is only needed because this document is generated programmatically.)

code/python/parse_arg.py || true
usage: parse_arg.py [-h] [-v] [-p {1,2,3,4,5,6,7,8}] echo number
parse_arg.py: error: the following arguments are required: echo, number

You can get a more detailed usage by using the help argument.

code/python/parse_arg.py -h
usage: parse_arg.py [-h] [-v] [-p {1,2,3,4,5,6,7,8}] echo number

positional arguments:
  echo                  display a string
  number                display a number

optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose         verbose mode
  -p {1,2,3,4,5,6,7,8}, --threads {1,2,3,4,5,6,7,8}
                        number of threads

Run the script with positional and optional arguments.

code/python/parse_arg.py foobar 1984 -v
Verbose mode
Using 2 threads
foobar's type is <class 'str'>
1984's type is <class 'int'>

Bash

In Bash you can use the getopts command.

#!/usr/bin/env bash

usage() {
  # redirect STDOUT to STDERR
  echo "Usage: $0 [ -f FILE ] [ -t INT ]" 1>&2 
  exit 1
}

while getopts ":f:t:" options; do
  case "${options}" in
    f)
      file=${OPTARG}
      ;;
    t)
      thread=${OPTARG}
      regex='^[1-9][0-9]*$'
      if [[ ! $thread =~ $regex ]]; then
        usage
      fi
      ;;
    :)
      echo "Error: -${OPTARG} requires an argument."
      exit 1
      ;;
    *)
      usage ;;
  esac
done

# OPTIND is the number of arguments that are options or arguments to options
if [ $OPTIND -ne 5 ]; then
  usage
fi

printf "File: %s\nThreads: %d\n" $file $thread

exit 0

Usage.

code/unix/get_option.sh || true
Usage: code/unix/get_option.sh [ -f FILE ] [ -t INT ]

R

In R use the optparse library.

#!/usr/bin/env Rscript

library(optparse)

option_list <- list(
   make_option(c("-f", "--first"), type = "character",  help = "first read pair", metavar = "read1.fastq1"),
   make_option(c("-s", "--second"), type = "character", help = "second read pair", metavar = "read2.fastq"),
   make_option(c("-t", "--thread"), type = "integer", help = "number of threads to use", default = 8)
)

opt <- parse_args(OptionParser(option_list=option_list))

if(any(is.null(opt$first), is.null(opt$second))){
   print_help(OptionParser(option_list=option_list))
   quit(status = 1)
}

message(paste0("Read 1: ", opt$first, "\nRead 2: ", opt$second, "\nThread: ", opt$thread))
quit(status = 0)

Usage.

code/r/optparse.R || true
Loading .Rprofile for the current workflowr project
This is workflowr version 1.7.0
Run ?workflowr for help getting started
Usage: code/r/optparse.R [options]


Options:
    -f READ1.FASTQ1, --first=READ1.FASTQ1
        first read pair

    -s READ2.FASTQ, --second=READ2.FASTQ
        second read pair

    -t THREAD, --thread=THREAD
        number of threads to use

    -h, --help
        Show this help message and exit

Perl

In Perl use the Getopt::Long package (or Getopt::Std).

#!/usr/bin/env perl

use warnings;
use strict;
use Getopt::Long;

my $fastq1  = "";
my $fastq2  = "";
my $thread  = 8;
my $verbose = 0;
my $help    = 0;

GetOptions ("thread=i" => \$thread,   # numeric
            "first=s"  => \$fastq1,   # string
            "second=s" => \$fastq2,   # string
            "verbose"  => \$verbose,  # flag
            "help"     => \$help)     # flag
|| die("Error in command line arguments\n");

if ($fastq1 eq "" || $fastq2 eq "" || $help == 1){
   usage()
}

if ($verbose){
   print join("\n", "First: $fastq1", "Second: $fastq2", "Thread: $thread"), "\n";
}

sub usage {
print STDERR <<EOF;
Usage: $0 -f read1.fastq -s read2.fastq -t 8

Where:   -f, --first  FILE     first read pair
         -s, --second FILE     second read pair
         -t, --thread INT      number of threads to use (default 8)
         -v, --verbose         print out arguments
         -h, --help            this helpful usage message

EOF
exit();
}

__END__

Usage.

code/perl/getopts.pl
Usage: code/perl/getopts.pl -f read1.fastq -s read2.fastq -t 8

Where:   -f, --first  FILE     first read pair
         -s, --second FILE     second read pair
         -t, --thread INT      number of threads to use (default 8)
         -v, --verbose         print out arguments
         -h, --help            this helpful usage message

sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] workflowr_1.7.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3     bslib_0.3.1      compiler_4.2.0   pillar_1.7.0    
 [5] later_1.3.0      git2r_0.30.1     jquerylib_0.1.4  tools_4.2.0     
 [9] getPass_0.2-2    digest_0.6.29    jsonlite_1.8.0   evaluate_0.15   
[13] tibble_3.1.7     lifecycle_1.0.1  pkgconfig_2.0.3  rlang_1.0.2     
[17] cli_3.3.0        rstudioapi_0.13  yaml_2.3.5       xfun_0.31       
[21] fastmap_1.1.0    httr_1.4.3       stringr_1.4.0    knitr_1.39      
[25] sass_0.4.1       fs_1.5.2         vctrs_0.4.1      rprojroot_2.0.3 
[29] glue_1.6.2       R6_2.5.1         processx_3.5.3   fansi_1.0.3     
[33] rmarkdown_2.14   callr_3.7.0      magrittr_2.0.3   whisker_0.4     
[37] ps_1.7.0         promises_1.2.0.1 htmltools_0.5.2  ellipsis_0.3.2  
[41] httpuv_1.6.5     utf8_1.2.2       stringi_1.7.6    crayon_1.5.1