生信常见脚本的命令行传参方式--python,R,Perl,Shell

对于最基础的生信流程分析人员,一般就只需要知道使用的脚本及其需要传入的参数文件就可以拿到结果文件了,今天就来总结一下,在编程语言中的入参代码是怎么书写的吧。

python -- argparse

argparse 模块可以让人轻松编写用户友好的命令行接口。程序定义它需要的参数,然后 argparse 将弄清如何从 sys.argv 解析出那些参数。 argparse 模块还会自动生成帮助和使用手册,并在用户给程序传入无效参数时报出错误信息。

import argparse
parser = argparse.ArgumentParser(description='manual to this script')
parser.add_argument('--signalfile', type=str, default = None)
parser.add_argument('--genelistfile', type=str, default = None)
args = parser.parse_args()
  • python脚本的调用方法: python example.py --signalfile --signal1 --genelistfile genelist
  • 参数调用,args.signalfile, args.genelistfile。

getopt
python 也可以使用getopt文件传参

import sys,getopt
usage='Usage: python transform_10x_to_dge.py -m matrix.mtx -g features.tsv -b barcodes.tsv -o transformed_dge.csv'

try:
    opts,args = getopt.getopt(sys.argv[1:], "hm:g:b:o:")
except getopt.GetoptError:
    print('Error\n',usage)
    sys.exit(2)

out_file='transformed_dge.csv'
for opt,arg in opts:
    if opt in ("-h"):
        print(usage)
        sys.exit()
    if opt in ('-m'):
        matrix_file=arg
    if opt in ('-g'):
        genes_file=arg
    if opt in ('-b'):
        barc_file=arg
    if opt in ('-o'):
        out_file=arg

data=mmread(matrix_file)
data.to_csv(out_file)

R -- getopt

getopt 是很多语言都有的传参函数,不过平时在写Rscript的时候经常用到。

library(getopt)
command= matrix(c(
  'help'                  ,"h", 0, "logical","no parameter,show help document",
  'project'               ,"r", 2, "character","required,load project.rdata or project.rds",
  'bc2ct'                 ,'b', 2, "character","barcode to celltype info, comma separated",
  'cl2ct'                 ,'c', 2, "character","cluster to celltype info, tab separated",
  'level'                 ,"l", 2, "character","seurat_clusters or cell_type level",
  'group'                 ,'g', 2, "character","group info, multiple groups are separated by comma",
  'outdir'                ,"o", 1, "character","optional,output directory,default:'.'"
), byrow=TRUE, ncol=5)

args=getopt(command)

if(!is.null(args$help) | is.null(args$project)){
  cat(paste(getopt(command, usage = T), "\n"))
  q()
  }

toutdir<-"."
if(!is.null(args$outdir)){
  toutdir<-args$outdir
}
  • R脚本的调用方法: Rscript example.R --project p1 --outdir output
    或者 Rscript example.R -p p1 -o output
  • R中用 getopt 包进行参数的接收。
  • 传入一个5列的matrix 文件, 分别代表:参数名称(长参数),参数简写(短参数),是否必选(0:不可输入;1:必选参数;2:可选参数),参数类型,参数说明。
  • 对于必选参数,需要先进行判断,不满不要求程序即终止,并打印出提示信息。
  • 参数的调用 arg$parameter。

Perl -- Getopt::Long

Getopt::Long模块执行的是扩展的getopt功能函数。它从@ARGV解析命令行参数,识别和移除指定的选项和 它们可能的值。

use strict;
use Getopt::Long;
my %args;
use FindBin qw($Bin);
use Cwd;
use vars qw($indir);
GetOptions(\%args,'list=s','outdir=s',"step=s");
my $usages="
perl $0 -list sample.list
[options]
-list   input a file with each sample on a line.
    example:
    A1
    A2
    A3
-outdir default: analysis_report
-step 1,2,3,4,5
";
$indir=getcwd;
$args{outdir}||="analysis_report";
$args{step}||="1,2,3,4";
die $usages  unless defined $args{list};
  • 参数调用 args{list},args{step}

Shell -- getopts

用getopts命令获取到脚本选项后,getopts会将获取到的选项所对应的参数(选项对应的参数,并不是选项本身,有的命令选项后面是需要跟一个参数的,例如tcpdump 的-w选项,后面需要指定一个文件来保存抓包数据)自动保存到OPTARG这个变量中。

while getopts ":r:g:t:l:o:" opt
do
case $opt in 
r) rdata=$OPTARG;;
g) group=$OPTARG;;
t) cl2ct=$OPTARG;;
l) level=$OPTARG;;
o) output=$OPTARG;;
h) func;;
?) func;;
esac
done

func(){
    echo "Usage:"
    echo "bash volcano_bash.sh [-r project.rds] [-g group.txt] [-t cluster2celltype.txt] [-l cell_type] [-o ./DEGs]"
}

一般而言:

  • R Python:用作脚本语言
  • Perl Shell:用于将上述脚本的封装成pipline
image.png
好啦,今天就总结到这里,以后再补充更多细节,转行做生信之前不知道脚本是什么,就只是到在jupyter notebook里面一行行跑,接触生信之后,觉得跟以前不同就是,会的编程语言要多一些,还有就是要把参数提出来写成脚本。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容