ensembl-pipeline
RuleCreation
Toolbar
Summary
RuleCreation
Package variables
No package variables defined.
Included modules
Inherit
Exporter
Synopsis
This class will parse config files to produce rule objects and store
them and will take rule tables and write a config file
[RepeatMask]
condition=SubmitContig
[Pmatch]
condition=SubmitChromosome
[Pmatch_Wait]
condition=Pmatch
[BestPmatch]
condition=Pmatch_Wait
condition=SubmitGenome
#comment lines can be made if they start with a # symbol
Description
Methods
Methods description
Arg [1] : Bio::EnsEMBL::Pipeline::DBSQL::DBAdaptor Arg [2] : hashref for hash keyed on goal analysis logic_name containing condition logic_names Function : create rules on the basis of the hash Returntype: array ref of Bio::EnsEMBL::Pipeline::Rule's Exceptions: throws if not passed a dbadaptor or it is of the wrong type or if not passed a rule hash or it is empty Example : my @rules = @{create_rules($db, $rule_hash)}' |
Arg [1] : array of filenames Function : parse a rule config file and produce rule objects Returntype: an hash ref of rule goal/conditions Exceptions: if file doesn't exist if config format is in correct if key already exists for a particular header' Caller : Example : my $rule_hash = parse_files($file); |
Arg [1] : Bio::EnsEMBL::DBAdaptor Function : Read the rule objects from the database Returntype: array ref of rule objects Exceptions: if db isn't the correct type' Caller : Example : my $rules = &read_db($db); |
Arg [1] : filename [2] : arrayref of rule objects Function : write a config file for the objects given Returntype: N/A Exceptions: if file doesnt exist Caller : Example : &write_file($file, $rules); |
Arg [1] : Bio::EnsEMBL::DBSQL::DBAdaptor [2] : Ref to an array of rule objects Function : Write the rule objects into the database Returntype: N/A Exceptions: if dbadaptor is the wrong type of object Caller : Example : &write_into_db($db, \@rules); |
Methods code
sub create_rules
{ my ($db, $config) = @_;
if(!$db || !$db->isa('Bio::EnsEMBL::Pipeline::DBSQL::DBAdaptor')){
throw("Must define a db and ".$db." must be a ".
"Bio::EnsEMBL::Pipeline::DBSQL::DBAdaptor");
}
if(!$config || keys(%$config) == 0){
throw("Not going to create any rules as rule hash ".
$config." is empty ");
}
my @rules;
my $analysis_adaptor = $db->get_AnalysisAdaptor;
my $rule_adaptor = $db->get_RuleAdaptor;
foreach my $goal(keys(%$config)){
my $analysis = $analysis_adaptor->fetch_by_logic_name($goal);
if(!$analysis){
throw("Can't have rule with goal ".$goal." if no analysis ".
"of that name exists");
}
my @conditions = @{$config->{$goal}};
my %checked;
foreach my $condition(@conditions) {
throw "$condition is not found in the database\n"
unless scalar($analysis_adaptor->fetch_by_logic_name($condition));
$checked{$condition} = 1;
}
throw("Sorry, you can't insert a rule if the goal is the same as ".
"one of the conditions") if exists $checked{$goal};
my $rule = Bio::EnsEMBL::Pipeline::Rule->new
(
-goalanalysis => $analysis
);
foreach my $condition(keys %checked) {
$rule->add_condition($condition);
}
push(@rules, $rule);
}
return\@ rules; } |
sub parse_files
{
my @files = shift;
if(@files == 0){
throw("Can't parse files if we don't have any @files");
}
my %config;
foreach my $file (@files) {
if (! -e $file) {
throw("rule file $file not found\n");
}
my $header = "";
open (FILE, $file) or throw "Couldn't open file $file";
while (<FILE>) {
chomp();
next if (/^\s$/ || /^\#/);
if (/^\[(.*)\]$/) { $header = $1;
if(!$config{$header}){
$config{$header} = [];
}else{
throw("Can't have two different rules with the same goal ".
"header");
}
}
if (/^([^=\s]+)\s*=\s*(.+)/) { my $key = lc($1); my $value = $2;
if (length($header) == 0) {
throw("Found key/value pair $key/$value outside stanza");
}
if($key eq 'condition'){
push(@{$config{$header}}, $value);
}else{
warn("Don't recognise ".$key." so doing nothing with ".
$value);
}
}
} close FILE;
}
foreach my $goal(keys(%config)){
if($config{$goal} == 0){
throw("Can't create a rule for ".$goal.
" without any conditions");
}
}
return\% config; } |
sub read_db
{ my $db = shift;
if(!($db->isa('Bio::EnsEMBL::DBSQL::DBAdaptor'))){
throw("need a DBAdaptor not ".$db);
}
my $rule_adaptor = $db->get_RuleAdaptor;
my @rules = $rule_adaptor->fetch_all;
return\@ rules; } |
sub write_file
{ my ($file, $rules) = @_;
open (FH, '>'.$file) or throw ("couldn't open $file to write to");
foreach my $rule(@$rules){
print FH "[".$rule->goalAnalysis->logic_name."]\n";
my @conditions = @{$rule->list_conditions};
foreach my $condition(@conditions){
print FH "condition=".$condition."\n";
}
} } |
sub write_into_db
{ my ($db, $rules) = @_;
if(!$db || !($db->isa('Bio::EnsEMBL::DBSQL::DBAdaptor'))){
throw("need a Pipeline::DBAdaptor not ".$db);
}
if(!$rules || @$rules == 0){
throw("Need rules to write to the database");
}
my $ruleadaptor = $db->get_RuleAdaptor;
my @existing_rules = $ruleadaptor->fetch_all;
my %existing;
foreach my $exist(@existing_rules){
$existing{$exist->goalAnalysis->logic_name} = $exist;
}
foreach my $rule(@$rules){
if(!$existing{$rule->goalAnalysis->logic_name}){
$ruleadaptor->store($rule);
}else{
warn("Not storing rule ".$rule->goalAnalysis->logic_name.
" rule with same goal already exists ".
$existing{$rule->goalAnalysis->logic_name}->dbID);
}
} } |
General documentation
Post general queries to ensembl-dev@ebi.ac.uk
the class itself obviously doesn't' need to be instantiated but the
either the script which uses it should be in the same directory as
it or the directory which contains it should be in you PERL5LIB
the rule_setup script which should be found in the directory can
perform both functions for you if you have the appropriate database
and config files
an example config file can be found in this directory called
example_rule.conf