getopt and commandArgs examples, and AF/OR update to use getopt()

This commit is contained in:
Tanushree Tunstall 2020-06-18 17:59:28 +01:00
parent 864f814e1b
commit f9a8ed3dc7
3 changed files with 66 additions and 21 deletions

64
scripts/AF_and_OR_calcs.R Normal file → Executable file
View file

@ -1,19 +1,37 @@
#!/usr/bin/env Rscript
#require('compare')
require('getopt', quietly=TRUE) # We need to be able to parse arguments
######################################################### #########################################################
# TASK: To calculate Allele Frequency and # TASK: To calculate Allele Frequency and
# Odds Ratio from master data # Odds Ratio from master data
# and add the calculated params to meta_data extracted from # and add the calculated params to meta_data extracted from
# data_extraction.py # data_extraction.py
######################################################### #########################################################
getwd() #getwd()
setwd('~/git/LSHTM_analysis/scripts') setwd('~/git/LSHTM_analysis/scripts')
getwd() cat(c(getwd(),'\n'))
options(scipen = 999) #disabling scientific notation in R. # Command line args
spec = matrix(c(
"drug" , "d", 1, "character",
"gene" , "g", 1, "character"
), byrow = TRUE, ncol = 4)
opt = getopt(spec);
drug = opt$drug
gene = opt$gene
if(is.null(drug)|is.null(gene)) {
stop('--drug and --gene must both be specified (case-sensitive)')
}
#options(scipen = 999) #disabling scientific notation in R.
#options(scipen = 4) #options(scipen = 4)
#%% variable assignment: input and output paths & filenames #%% variable assignment: input and output paths & filenames
drug = 'pyrazinamide' #drug = 'pyrazinamide'
gene = 'pncA' #gene = 'pncA'
gene_match = paste0(gene,'_p.') gene_match = paste0(gene,'_p.')
cat(gene_match) cat(gene_match)
@ -41,7 +59,8 @@ cat(paste0('Reading infile2: gene associated metadata:', infile_metadata))
# outdir = 'git/Data/pyrazinamide/output' # outdir = 'git/Data/pyrazinamide/output'
outdir = paste0('~/git/Data', '/', drug, '/', 'output') outdir = paste0('~/git/Data', '/', drug, '/', 'output')
#out_filename = paste0(tolower(gene), '_meta_data_with_AF_OR.csv') #out_filename = paste0(tolower(gene), '_meta_data_with_AF_OR.csv')
out_filename = paste0(tolower(gene), '_AF_OR.csv') #out_filename = paste0(tolower(gene), '_AF_OR.csv')
out_filename = paste0(tolower(gene), '_XXXXX_AF_OR.csv')
outfile = paste0(outdir, '/', out_filename) outfile = paste0(outdir, '/', out_filename)
cat(paste0('Output file with full path:', outfile)) cat(paste0('Output file with full path:', outfile))
#%% end of variable assignment for input and output files #%% end of variable assignment for input and output files
@ -380,14 +399,14 @@ head(pvals_logistic)
#============================================= #=============================================
# check ..(hmmm) perhaps separate script) # check ..(hmmm) perhaps separate script)
afs['pnca_p.trp68gly'] #afs['pnca_p.trp68gly']
afs['pnca_p.gln10pro'] #afs['pnca_p.gln10pro']
afs['pnca_p.leu4ser'] #afs['pnca_p.leu4ser']
#
plot(density(log(ors_logistic))) #plot(density(log(ors_logistic)))
plot(-log10(pvals)) #plot(-log10(pvals))
hist(log(ors) #hist(log(ors)
, breaks = 100) # , breaks = 100)
# sanity check: if names are equal (just for 3 vars) # sanity check: if names are equal (just for 3 vars)
all(sapply(list(names(afs) all(sapply(list(names(afs)
@ -395,11 +414,11 @@ all(sapply(list(names(afs)
, names(statistic_chi) # should return False , names(statistic_chi) # should return False
, names(ors_chi_cus)), function (x) x == names(ors_logistic))) , names(ors_chi_cus)), function (x) x == names(ors_logistic)))
compare(names(afs) #compare(names(afs)
, names(pvals_chi) # , names(pvals_chi)
, names(statistic_chi) #TEST: should return False, but DOESN'T # , names(statistic_chi) #TEST: should return False, but DOESN'T
, names(ors_chi_cus) # , names(ors_chi_cus)
, names(stat_chi))$result # , names(stat_chi))$result
#=============== Now with all vars #=============== Now with all vars
@ -484,4 +503,7 @@ cat('End of script: calculated AF, OR, pvalues and saved file')
#df1 = gene_metadata #df1 = gene_metadata
#df2 = comb_AF_and_OR #df2 = comb_AF_and_OR
# COMMENT: will do the combining with the other OR and AF (in python)
# COMMENT: will do the combining with the other OR and AF (in python)

9
scripts/commandArgs-example.r Executable file
View file

@ -0,0 +1,9 @@
#!/usr/bin/env Rscript
print('R Argument Test')
cmd <- paste(commandArgs(), collapse=" ")
cat("How R was invoked:\n");
cat(cmd, "\n")
args <- commandArgs(trailingOnly = TRUE)
cat(c('Command Line Arguments supplied: ', args))

14
scripts/getopt-example.r Executable file
View file

@ -0,0 +1,14 @@
#!/usr/bin/env Rscript
require('getopt', quietly=TRUE)
spec = matrix(c(
"drug" , "d", 1, "character",
"gene" , "g", 1, "character"
), byrow=TRUE, ncol=4)
opt = getopt(spec);
drug = opt$drug
gene = opt$gene
cat(c('\nDrug:', drug, '\nGene:', gene, '\n'))