加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

用一个简单的perl包轻松管理脚本中的软件调用

发布时间:2020-12-15 23:54:16 所属栏目:大数据 来源:网络整理
导读:在用perl写的流程中,有人喜欢直接在脚本定义软件的路径,比如: my $blastall="/opt/blc/genome/bin/blastall"; 这样做,看着好像很省事的样子,但是当一个流程层次关系比较复杂时,而且当其中的路径失效时,麻烦来了:我们需要翻复杂的脚本,重新找到标量

在用perl写的流程中,有人喜欢直接在脚本定义软件的路径,比如:

my $blastall="/opt/blc/genome/bin/blastall";

这样做,看着好像很省事的样子,但是当一个流程层次关系比较复杂时,而且当其中的路径失效时,麻烦来了:我们需要翻复杂的脚本,重新找到标量的定义,然后修改之。可是,当你把所有调用的软件路径写到配置文件中,并用perl package function调用,当路径失效时,我们只需要在配置文件中修改就可以了。

比如配置文件config.txt是这样的:

###########################
#######software path#######
###########################
prank=/nas/GAG_02/liushiping/GACP/software/evolution/prank-msa/src/prank
GBlocks=/home/lixiangfeng/tools/Gblocks_0.91b/Gblocks
codeml=/share/project002/zhangpei/bin/paml44/bin/codeml
formatdb=/opt/blc/genome/bin/formatdb
blastall=/opt/blc/genome/bin/blastall
muscle=/opt/blc/genome/bin/muscle
###########################
#######END#################
###########################


针对配置文件,我们可以写一个如下的名为Soft.pm的package:

################################################
######This package contains the pathes of many softwares
######You can modify the config.txt when the software path changed
######Original from xiangfeng li,xflee0608@163.com
####################2014-7-5####################
package  Soft;
use strict;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(parse_config);
##parse the software.config file,and check the existence of each software
################################################

sub parse_config{
        my ($config,$soft)=@_;
        open IN,$config || die;
        my %ha;
        while(<IN>){
                chomp;
                next if /^#|^$/;
                s/s+//g;
                my @p=split/=/,$_;
                $ha{$p[0]}=$p[1];
        }
        close IN;
        if(exists $ha{$soft}){
                if(-e $ha{$soft}){
                        return $ha{$soft};
                }else{
                         die "nConfig Error: $soft wrong path in $confign";
                }
        }else{
                die "nConfig Error: $soft not set in $confign";
        }
}
1;              
__END__         


那么怎么用这个package呢?

可以像正常的module调用那样,use module;

示例如下:

#! /usr/bin/perl -w
use strict;
use FindBin qw($Bin $Script);
use lib $Bin;
use Soft;
......
......
my $config="$Bin/config.txt";
my $blastall=parse_config($config,"blastall");
......

这样在流程中,看起来似乎多了许多行,但是这个package可以在不同的流程中反复调用,你只需要做好两件事情:

1,把Soft.pm 放到一个适当的位置,如果是调用流程的相对路径,就要用FindBin 模块,如果直接是一个固定的路径,可以这么写:

#! /usr/bin/perl -w
use strict;
use lib 'home/lxf/';
use Soft;
......
......
my $config="home/lxf/config.txt";
my $blastall=parse_config($config,"blastall");
......
2.在config文件中按格式要求写好你常用软件的路径,这样管理脚本中涉及的软件路径,是不是很方便呢?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读