Rscript with argv

Run R script with argument.

When analyzing RNAseq data with DGE analysis, it’s common to make pairwise comparisons. It’s handy to pass argments to the script.

For instance, in the edgeR pipeline, we want to compare A and B with the function makeContrasts:

...
args = commandArgs(trailingOnly=TRUE)

comp <- noquote(paste("groups", args[1], " - ", "groups", args[2], sep=""))
con<-makeContrasts(comp, levels=design)
...
logfc<-round(ttags$table$logFC, digits = 2)
logfdr <- round(-log10(ttags$table$FDR), digits = 2)
vol <- paste(rownames(ttags$table), logfc, logfdr, sep=",")
fout <- paste(args[1], args[2], ".csv", sep="")
write.table(vol, quote = F, row.names = F, col.names = F,file=fname)
...

So run the script:

Rscript --vanilla dge.R A B

We can also run the script for many comparisons, assuming that we have a file named “comp.txt”:

A|B
C|D
E|F

and run with a bash script:

#!/bin/bash
declare -A idpairs # associative array

while IFS=\| read var1 var2 
do
    idpairs[$var1]=$var2
done < comps.txt  

for var1 in ${!idpairs[*]}
do  
    Rscript --vanilla dge.R $var1 ${idpairs[$var1]}
done
Z. Lu avatar
Z. Lu
Data science, bioinfo, scripting, parasites, retro, plain text.
comments powered by Disqus