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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
...
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”:

1
2
3
A|B
C|D
E|F

and run with a bash script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/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
comments powered by Disqus
CC-BY-NC 4.0
Built with Hugo Theme Stack