Skip to content
Hurley edited this page Feb 16, 2021 · 1 revision

This Wiki is just to write down some tips in writing scripts

OPTIONS

R

commandArgs

The easiest way is to use commandArgs()

args <- commandArgs(trailingOnly = TRUE)
a<-as.character(args[1])

optparse

This is a more controllable way

suppressPackageStartupMessages(require(optparse))
option_list <- list( 
    make_option(c("-n", "--name"), action="store", type="character", default="Jim", 
        help="Give me your name [default \"%default\"]"),
    make_option(c("-v", "--verbose"), action="store_true", default=TRUE,
        help="Print more information [default]"),
    make_option(c("-q", "--quietly"), action="store_false", dest="verbose", 
        help="Print little output"),
    make_option(c("-a", "--age"), type="integer", default=18, metavar="number",
        help="Your age [default %default]")
    )
opt <- parse_args(OptionParser(option_list=option_list))
if ( opt$verbose ) { 
    write("I will write more...\n", stderr()) 
}

bash

usage () {
    echo "help message"
}

haveInput=false
remove=false
outputSpecify=false

options=":hi:ro:"

while getopts $options opt
do
    case $opt in
        h ) usage; exit;;
        i ) input=$OPTARG; haveInput=true;;
        r ) remove=true;;
        o ) outputFile=$OPTARG; outputSpecify=true;;
        \?) echo "Unknown option: -$OPTARG" >&2; usage; exit 1;;
        : ) echo "Missing option argument for -$OPTARG" >&2; usage; exit 1;;
        * ) echo "Unimplemented option: -$OPTARG" >&2; usage; exit 1;;
    esac
done

if also want to use positional parameters after getopts, use this:

ARG1=${@:$OPTIND:1}

Python

https://docs.python.org/2/library/argparse.html#module-argparse

or use sys.argv[1]

Use ssh key to log in server

Step1: create keys at local computer

$ ssh-keygen

Step2: scp the generated public key from id_dsa.pub to remote server folder: ~/.ssh/

You can rename it if id_dsa.pub already exist

Step3: In remote server ~/.ssh, add the new public key to the file authorized_keys

$ cat id_dsa.pub >> authorized_keys

Then you can remove id_dsa.pub

Draw options

color centering on 0 for pheatmap

paletteLength <- 100
myColor <-  colorRampPalette(rev(brewer.pal(n = 9, name = "RdBu")))(paletteLength)
myBreaks <- c(seq(min(df), 0, length.out=ceiling(paletteLength/2) + 1), 
              seq(max(df)/paletteLength, max(df), length.out=floor(paletteLength/2)))

pheatmap(df, cluster_rows=FALSE, show_rownames = T, show_colnames = T, cluster_cols=FALSE,  fontsize=10, gaps_col =c(20,40), border_color = "grey40", color=myColor, breaks=myBreaks)

set color in R

library(RColorBrewer)
library(ggplot2)
options(repr.plot.width=10, repr.plot.height=0.3)
par(mar=c(0,0,0,0))
colors<-c(brewer.pal(8, "Accent"), brewer.pal(9,"Set1"), brewer.pal(12,"Set3"))
image(1:29,1,as.matrix(1:29),col=colors,xlab=NA,ylab="",xaxt="n", yaxt="n",bty="n")
text(c(1:29),1,labels=c(1:29), cex=0.5)
sigColor=colors[c(28,22,13,9,26)]
pTrackBgColor<-colors[19]
h3BedColor<-colors[5]
h3PeakFillColor<-rgb(0,0,1,0.5)
hightlightColor<-rgb(1,1,0,0.3)

subplots using plt

https://jakevdp.github.io/PythonDataScienceHandbook/04.08-multiple-subplots.html

Multi-threading in R

https://beckmw.wordpress.com/2014/01/21/a-brief-foray-into-parallel-processing-with-r/ https://www.r-bloggers.com/parallel-r-loops-for-windows-and-linux/

Setup git repo and ssh key

http://kbroman.org/github_tutorial/pages/init.html http://kbroman.org/github_tutorial/pages/first_time.html