R -- Rscript传递参数

getopt包

getopt(spec = NULL, opt = NULL, command = get_Rscript_filename(),
usage = FALSE, debug = FALSE)

  • spec:
    内容由一个4-5列的matrix构成,matrix可以直接申明也可以如下申明 matrix(x,ncol=4,byrow=TRUE)
    每一列的构成要素如下:
    Column 1: the long flag name. A multi-character string.
    Column 2: short flag alias of Column 1. A single-character string.
    Column 3: Argument mask of the flag. An integer. Possible values: 0=no argument, 1=required argument, 2=optional argument.
    Column 4: Data type to which the flag’s argument shall be cast using storage.mode.
    Column 5 (optional): A brief description of the purpose of the option.
#!/path/to/Rscript
library('getopt')

# get options, using the spec as defined by the enclosed list.
# we read the options from the default: commandArgs(TRUE).
spec = matrix(c(
	'verbose', 'v', 2, "integer",
	'help' , 'h', 0, "logical",
	'count' , 'c', 1, "integer",
	'mean' , 'm', 1, "double",
	'sd' , 's', 1, "double"
	), byrow=TRUE, ncol=4)

# 脚本运行到这里时,外部传递的参数会赋值给opt
opt = getopt(spec)

# 这里设置帮助信息,并且如果接收到了帮助信息指令则打印帮助信息并且终止后续命令的执行
# if help was asked for print a friendly message
# and exit with a non-zero error code
if ( !is.null(opt$help) ) {
	cat(getopt(spec, usage=TRUE))
	q(status=1)
}

# 默认参数设置部分
# set some reasonable defaults for the options that are needed,
# but were not specified.
if ( is.null(opt$mean ) ) { opt$mean = 0 }
if ( is.null(opt$sd ) ) { opt$sd = 1 }
if ( is.null(opt$count ) ) { opt$count = 10 }
if ( is.null(opt$verbose ) ) { opt$verbose = FALSE }

# 设置脚本运行情况的信息反馈部分
# print some progress messages to stderr, if requested.
if ( opt$verbose ) { write("writing...",stderr()) }

# do some operation based on user input.
cat(paste(rnorm(opt$count,mean=opt$mean,sd=opt$sd),collapse="n"))
cat("n")
# signal success and exit.
# q(status=0)
# Start ========================================================================
#!/path/to/Rscript


# 脚本参数设置和接收部分
library(getopt)

spec = matrix(c(
    'input',  'i', 1, "character",
    'output', 'o', 1, "character",
    'help',   'h', 0, "logical"), byrow=TRUE, ncol=4)
    
args <- getopt(spec)

if(!is.null(args$help) || is.null(args$input) || is.null(args$output)){
    cat(paste(getopt(spec, usage = T), collapse = "n"))
    cat("   Description: This script is aimed to calculate the ratio in malignant cell! Example:n
            Rscript cal_ratio.R -i BRCA.rds -o BRCA_outputn
    Written by XqChai, 2023-07-20. n")
    q(status=1)
}

if (is.null(args$output)) {args$species = "out"}

# 操作系统或者数据的部分
library(tidyverse)
library(reshape2)
library(stringi)

#读取rds文件
input <- args$input 
output <- args$output
sce <- readRDS(input)

optparse包

make_option(
  opt_str, # 参数名,格式:--parameter_name
  action = NULL, # 描述optparse在遇到选项“store”、“store-true”、“store-false”或“callback”时应采取的操作的字符串。
 	  # 如果在命令字符串上找到选项,则“store”操作表示optparse应存储指定的以下值。
	  # 如果找到选项,“store_true”将存储true,如果找到选项,“store_false”将存储false。
	  # “callback”存储回调参数中指定的函数生成的返回值。如果callback不为空,则默认值为“callback”else“store”。
  type = NULL, # 参数数据类型
  dest = NULL,
  default = NULL, #设置参数的默认值
  help = "", # 该参数的描述信息
  metavar = NULL,
  callback = NULL,
  callback_args = NULL
)

option_list = list(make_option(), make_option(), make_option()) 
#!/path/to/Rscript

# 参数接受和设置部分
library(optparse)

option_list = list(
    make_option(c("-f", "--file"), type="character", default=NULL, 
                help="rpkm/fpkm matrix file", metavar="input"),
    make_option(c("-o", "--out"), type="character", default="out.tpm.xls", 
                help="output file name [default= %default]", metavar="output")
); 

opt_parser = OptionParser(option_list = option_list, epilogue = "Written by Chai X. Q. 2019-04-22.")
opt = parse_args(opt_parser);

if (is.null(opt$file)){
    print_help(opt_parser)
    stop("At least one argument must be supplied (input file).", call.=FALSE)
}

# 操作系统或者数据部分
#program
input <- opt$file
output <- opt$out
exp <- covert(input)


argparse包

该部分内容完全来自这位作者以及他的文字:
https://zhuanlan.zhihu.com/p/212997106

然后本人通常使用第一种(getopt)方法,第二种方法也使用过,但是这一部分内容仅仅是记录和比较。

argparse包分为三个步骤:
第一步是使用ArgumentParser()创建一个参数解析对象,
第二步是使用add_argument函数为前面的对象增加参数,
第三步是通过parse_args()把解析对象赋值并在后续程序中调用。

#! /usr/bin/env Rscript
library(argparse)

# 创建参数解析对象
parser <- ArgumentParser()

# 设置参数
# 设置第一个参数verbose,缩写为v,其作用是告诉脚本是否打印完整的计算过程,其缺省值为TRUE
parser$add_argument("-v", "--verbose", action="store_true", default=TRUE,
        help="Print extra output [default]")

# 设置第二个参数quietly,缩写为q,其作用是修改verbose参数,当调用改参数时,verbose被修改为FALSE,从而导致不再打印计算过程
parser$add_argument("-q", "--quietly", action="store_false", 
        dest="verbose", help="Print little output")

# 设置第三个参数count,缩写为c,这是一个整数参数,缺省值5,在后续的代码中被用作确定输出随机数的个数
parser$add_argument("-c", "--count", type="integer", default=5, 
        help="Number of random normals to generate [default %(default)s]",
        metavar="number")

# 设置第四个参数generator,无缩写,用于确定调用何种随机分布,缺省值rnorm对应于正态分布
parser$add_argument("--generator", default="rnorm", 
        help = "Function to generate random deviates [default "%(default)s"]")

# 设置第五个参数mean,无缩写,浮点数,用于确定正态分布的均值,缺省值为0
parser$add_argument("--mean", default=0, type="double",
        help="Mean if generator == "rnorm" [default %(default)s]")

# 设置第五个参数sd,无缩写,浮点数,用于确定正态分布的标准差,缺省值为1
parser$add_argument("--sd", default=1, type="double",
        metavar="standard deviation",
        help="Standard deviation if generator == "rnorm" [default %(default)s]")

# 调用解析器,此时args就被赋值为命令行参数输入的相应值
args <- parser$parse_args()

# 根据verbose确定是否打印计算过程
if ( args$verbose ) { 
        write("writing some verbose output to standard error...n", stderr()) 
}

# 根据其他参数,确定输出随机数的类型与次数
if( args$generator == "rnorm") {
        cat(paste(rnorm(args$count, mean=args$mean, sd=args$sd), collapse="n"))
} else {
        cat(paste(do.call(args$generator, list(args$count)), collapse="n"))
}
cat("n")

真实脚本示例

Rscript FoldChange.R -f /public/targets -d /public/Colongene_expression_log2_tpm.rds -o Colon_output

cat FoldChange.R
# Start ========================================================================
#!/public/home/djs/miniconda3/envs/R4/bin/Rscript



# 脚本参数设置和接收部分
library(getopt)

spec = matrix(c(
    'genelist', 'g', 2, "character",
	'genefile', 'f', 2, "character",
	'dataset', 'd', 1, "character",
	'method', 'm', 2, "character",
    'output', 'o', 2, "character",
    'help', 'h', 0, "logical"), byrow=TRUE, ncol=4)
    
args <- getopt(spec)

if(!is.null(args$help) || is.null(args$dataset) || is.null(args$output)){
    cat(paste(getopt(spec, usage = T), collapse = "n"))
    cat("   Description: This script is aimed to calculate the expression of gene foldchange(log2)! Example:n
            Rscript FoldChange.R -g ERBB2 -d BRCA.rds -o BRCA_outputn
			Rscript FoldChange.R -g ERBB2 -d BRCA.rds -o BRCA_output -m mean n
			Rscript FoldChange.R -g ERBB2 -d BRCA.rds -o BRCA_output -m median n
			Rscript FoldChange.R -f ./file_of_gene_list -d BRCA.rds -o BRCA_output -m median n
    Written by JS Dai, 2023-08-05. n")
    q(status=1)
}

if(is.null(args$genelist) & is.null(args$genefile)){
    cat(paste(getopt(spec, usage = T), collapse = "n"))
    cat("   Description: This script is aimed to calculate the expression of gene foldchange(log2)! Example:n
            Rscript FoldChange.R -g ERBB2 -d BRCA.rds -o BRCA_outputn
			Rscript FoldChange.R -g ERBB2 -d BRCA.rds -o BRCA_output -m mean n
			Rscript FoldChange.R -g ERBB2 -d BRCA.rds -o BRCA_output -m median n
			Rscript FoldChange.R -f ./file_of_gene_list -d BRCA.rds -o BRCA_output -m median n
    Written by JS Dai, 2023-08-05. n")
    q(status=1)
}

# 默认设置部分
if (is.null(args$method)) {args$method = "median"}
if (is.null(args$output)) {args$output = "out"}

# 参数解析部分
if(!is.null(args$genelist)){
	genelist <- args$genelist
	genelist <- list(genelist)
}else{
	genelist <- read.csv(args$genefile,header=F)
	genelist <- unique(unclass(genelist)[[1]])
}

dataset <- args$dataset
output <- args$output
method <- args$method

# 操作系统或者数据的部分
library(tidyverse)
library(reshape2)
library(stringi)

#step 1 :判断基因
probe <- read.csv("/public/public_dataset/TCGA_TARGET_GTEx/probeMap%2Fgencode.v23.annotation.gene.probemap",sep="t",header=T)

truegene <- unlist(lapply(genelist,function(x){x %in% probe$gene}))

cat("=======================================")
cat("Only below gene in dataset!n")
genelist[truegene]

cat("=======================================")
cat("Some gene don't in dataset!n")
genelist[!truegene]

# 
genelist <- genelist[truegene]
ensgid <- probe[which(probe$gene %in% genelist),"id"] # 使用该ID进行后续数据处理
# step 2 :读取数据集
df <- readRDS(dataset) # row(sampleid) x col(gene variable)

# 制造分组一会儿比较
df$group <- NA
df[grep(df$sample,pattern="^TCGA"),"group"] <- "Tumor"
df[grep(df$sample,pattern="^TCGA.*11$"),"group"] <- "NAT"
df[grep(df$sample,pattern="^GTEX"),"group"] <- "GTEX"

# 样本分组信息
write.table(table(df[,"group"]),file=paste(output,"_sample_type_count_info.csv",sep=""),append=T,sep=",",row.names=F)

if(method == "median"){
	x <- c("gene","log2(median(Tumor))","log2(median(NAT))","log2(median(GTEX))","LFC_Tumor_NAT","LFC_Tumor_GTEX","LFC_NAT_GTEX","P_Tumor_NAT","P_Tumor_GTEX","P_NAT_GTEX")
	write.table(t(x),file=paste(output,"_gene_expression_LF_info.csv",sep=""),append=T,sep=",",row.names=F,col.names=F)
	
	for(i in ensgid){
		x <- median(df[df$group == "Tumor",i])
		y <- median(df[df$group == "NAT",i])
		z <- median(df[df$group == "GTEX",i])
		if(is.na(y)){
			fx <- abs(x)
			fy <- abs(x-z)
			fz <- abs(z)
			px <- NA
			py <- t.test(x= df[df$group == "Tumor",i],y = df[df$group == "GTEX",i])$p.value
			pz <- NA
		}else{
			fx <- abs(x-y)
			fy <- abs(x-z)
			fz <- abs(y-z)
			px <- t.test(x= df[df$group == "Tumor",i],y = df[df$group == "NAT",i],mu=fx)$p.value
			py <- t.test(x= df[df$group == "Tumor",i],y = df[df$group == "GTEX",i],mu=fy)$p.value
			pz <- t.test(x= df[df$group == "NAT",i],y = df[df$group == "GTEX",i],mu=)$p.value
		}
		genename <- probe[probe$id == i,"gene"]
		result <- c(genename,x,y,z,fx,fy,fz,px,py,pz)
		write.table(t(result),file=paste(output,"_gene_expression_LF_info.csv",sep=""),append=T,sep=",",row.names=F,col.names=F)
	}
}
if(method == "mean"){
	x <- c("gene","log2(mean(Tumor))","log2(mean(NAT))","log2(mean(GTEX))","LFC_Tumor_NAT","LFC_Tumor_GTEX","LFC_NAT_GTEX","P_Tumor_NAT","P_Tumor_GTEX","P_NAT_GTEX")
	write.table(t(x),file=paste(output,"_gene_expression_LF_info.csv",sep=""),append=T,sep=",",row.names=F,col.names=F)
	
	for(i in ensgid){
		x <- mean(df[df$group == "Tumor",i])
		y <- mean(df[df$group == "NAT",i])
		z <- mean(df[df$group == "GTEX",i])
		if(is.na(y)){
			fx <- abs(x)
			fy <- abs(x-z)
			fz <- abs(z)
			px <- NA
			py <- t.test(x= df[df$group == "Tumor",i],y = df[df$group == "GTEX",i])$p.value
			pz <- NA
		}else{
			fx <- abs(x-y)
			fy <- abs(x-z)
			fz <- abs(y-z)
			px <- t.test(x= df[df$group == "Tumor",i],y = df[df$group == "NAT",i])$p.value
			py <- t.test(x= df[df$group == "Tumor",i],y = df[df$group == "GTEX",i])$p.value
			pz <- t.test(x= df[df$group == "NAT",i],y = df[df$group == "GTEX",i])$p.value
	}
		genename <- probe[probe$id == i,"gene"]
		result <- c(genename,x,y,z,fx,fy,fz,px,py,pz)
		write.table(t(result),file=paste(output,"_gene_expression_LF_info.csv",sep=""),append=T,sep=",",row.names=F,col.names=F)
	}
}