您的位置:首页 > 其它

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

2014-08-17 23:22 906 查看
在用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 $config\n";
}
}else{
die "\nConfig Error: $soft not set in $config\n";
}
}
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文件中按格式要求写好你常用软件的路径,这样管理脚本中涉及的软件路径,是不是很方便呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: