-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMfAnnotGeneCommands.pir
91 lines (74 loc) · 3.47 KB
/
MfAnnotGeneCommands.pir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#
# This class represents a single group of commands.
#
# See also the encapsulating object, <MfAnnotCommandSet>
#
- PerlClass PirObject::MfAnnotGeneCommands
- InheritsFrom PirObject
- FieldsTable
# Field name Struct Type Comments
#---------------------- --------------- --------------- -----------------------
debug single string any perl true/false
genename single string genename
bashcommands array string commands to run
- EndFieldsTable
- Methods
our $DEBUG = 0; # class global; debug can also be turned on for each object, see above
our $RCS_VERSION='$Id: MfAnnotGeneCommands.pir,v 1.9 2008/12/23 21:42:55 nbeck Exp $';
our ($VERSION) = ($RCS_VERSION =~ m#,v ([\w\.]+)#);
sub Execute {
my $self = shift;
my $cwd = shift || ""; # cwd of bashcommands
my $tmpdir = shift || ""; # where we can create temp files
my $substitutions = shift || {}; # hash VAR => val to substitute in place of the string %VAR% in bashcommands
my $keylabel = shift || "X"; # supplied by MfAnnotGeneCommandSet, used inside temp filenames
die "Error: no valid CWD supplied for Execute()... got '$cwd'.\n" unless $cwd && -d $cwd;
die "Error: no valid TMPDIR supplied for Execute()... got '$tmpdir'.\n" unless $tmpdir && -d $tmpdir;
my $debug = $self->get_debug() || $DEBUG;
my $genename = $substitutions->{"GENENAME"} || $self->get_genename() || "unk";
my $bashcommands = $self->get_bashcommands() || [];
my $bashscript = join("\n",@$bashcommands) . "\n";
my %moresubs = %$substitutions; # make copy; we will add some values right now
$moresubs{"GENENAME"} = $genename unless exists $moresubs{"GENENAME"};
$moresubs{"DEBUG"} = ($debug ? "#" : "") unless exists $moresubs{"DEBUG"};
foreach my $var (keys %moresubs) {
die "Not a proper variable name '$var'.\n" unless $var =~ m#^[a-zA-Z]+$#;
my $searchfor = "%" . $var . "%";
my $val = $moresubs{$var};
&CheckPath($bashscript,$searchfor,$val) if $bashscript =~ m/$searchfor/ && $var eq "MODPATH";
$bashscript =~ s/$searchfor/$val/g;
}
my $script = "$tmpdir/bashcom.$genename.$keylabel.MfGC.$$";
my $outcapt = "$tmpdir/out.$genename.$keylabel.MfGC.$$";
my $errcapt = "$tmpdir/err.$genename.$keylabel.MfGC.$$";
my $outscript = new IO::File ">$script"
or die "Cannot write to file '$script': $!\n";
print $outscript "#!/bin/bash\n",
"\n",
"# This script created automatically by " . __PACKAGE__ . " for gene '$genename' (block '$keylabel').\n",
"\n",
$bashscript;
$outscript->close();
my $command = "cd '$cwd';/bin/bash '$script' >'$outcapt' 2>'$errcapt'";
if ($debug) {
print STDERR " DEBUG: Executing external script block '$keylabel' for '$genename'.\n",
" --- COMMAND : $command\n",
" --- SCRIPT $script START ---\n",
"$bashscript",
" --- SCRIPT $script END ---\n";
}
system("/bin/bash","-c",$command);
($outcapt,$errcapt);
}
sub CheckPath {
my $bashscript = shift;
my $searchfor = shift;
my $val = shift;
my @each_string = split(/ /, $bashscript);
foreach my $string (@each_string){
next if $string !~ $searchfor;
$string =~ s/$searchfor/$val/g;
die "Model file '$string' doesn't exist or isn't readable.\nCheck installation of mfannot_models (see INSTALL.txt)."
if !(-e $string && -r $string);
}
}