sub parse_files
{
my @files = shift;
my %headers;
my $hcounter = 0;
my %horder;
my $config = {};
foreach my $file (@files) {
if (! -e $file) {
throw("analysis file $file not found\n");
}
my $header = "";
open (FILE, $file) or throw "Couldn't open file $file";
while (<FILE>) {
chomp();
next if (/^\s$/ || /^\#/);
if (/^\[(.*)\]\s*$/) { $header = $1;
$headers{$header} = 0;
$horder{$header} = $hcounter++;
}
if (/^([^=\s]+)\s*=\s*(.+?)\s*$/) {
my $key = lc($1); my $value = $2;
if (length($header) == 0) {
throw("Found key/value pair $key/$value outside stanza");
}
if (exists($config->{$header}->{$key})) {
throw("$key is already defined for [$header]; cannot be redefined");
} else {
$config->{$header}->{$key} = $value;
$headers{$header}++; }
}
}
close FILE;
}
my @analyses;
foreach my $h (sort { $horder{$a} <=> $horder{$b} } keys (%headers)) {
if (!$config->{$h}->{'input_id_type'}) {
throw("you seem to have no input_id_type for $h can't ".
"create a an analysis object without an input_id_type");
}
if($headers{$h} == 1){
print STDERR "You seem to only have an input_id_type of ".
$config->{$h}->{'input_id_type'}." defined for $h but will ".
"create the sparse analysis object anyway\n";
}
my $analysis = Bio::EnsEMBL::Pipeline::Analysis->new
(
-db => $config->{$h}->{db},
-db_file => $config->{$h}->{db_file},
-db_version => $config->{$h}->{db_version},
-program => $config->{$h}->{program},
-program_version => $config->{$h}->{program_version},
-program_file => $config->{$h}->{program_file},
-gff_source => $config->{$h}->{gff_source},
-gff_feature => $config->{$h}->{gff_feature},
-module => $config->{$h}->{module},
-module_version => $config->{$h}->{module_version},
-parameters => $config->{$h}->{parameters},
-logic_name => $h,
-input_id_type => $config->{$h}->{input_id_type},
-description => $config->{$h}->{description},
-display_label => $config->{$h}->{display_label},
);
push(@analyses, $analysis);
}
return\@ analyses; } |
sub read_db
{ my $db = shift;
if(!($db->isa('Bio::EnsEMBL::DBSQL::DBAdaptor'))){
throw("need a DBAdaptor not ".$db);
}
my $analysis_adaptor = $db->get_AnalysisAdaptor;
return $analysis_adaptor->fetch_all; } |
sub write_file
{ my $file = shift;
my $analyses = shift;
open (FH, '>'.$file) or throw ("couldn't open $file to write to");
foreach my $a(@$analyses){
print FH "[".$a->logic_name."]\n";
print FH "db=".$a->db."\n" if($a->db);
print FH "db_version=".$a->db_version."\n" if($a->db_version);
print FH "db_file=".$a->db_file."\n" if($a->db_file);
print FH "program=".$a->program."\n" if($a->program);
print FH "program_version=".$a->program_version."\n" if($a->program_version);
print FH "program_file=".$a->program_file."\n" if($a->program_file);
print FH "parameters=".$a->parameters."\n" if($a->parameters);
print FH "module=".$a->module."\n" if($a->module);
print FH "gff_source=".$a->gff_source."\n" if($a->gff_source);
print FH "gff_feature=".$a->gff_feature."\n" if($a->gff_feature);
if($a->can("input_id_type")){
print FH "input_id_type=".$a->input_id_type."\n" if($a->input_id_type);
}
print FH "description ".$a->description."\n" if($a->description);
print FH "display_name".$a->display_label."\n" if($a->display_label);
print FH "\n\n";
} } |
sub write_into_db
{ my $db = shift;
my $analyses = shift;
my $update = shift;
if(!($db->isa('Bio::EnsEMBL::DBSQL::DBAdaptor'))){
throw("need a Pipeline::DBAdaptor not ".$db);
}
my $analysis_adaptor = $db->get_AnalysisAdaptor;
my $sql = "select analysis_id from analysis where logic_name = ?";
my $sth = $db->prepare($sql);
ANALYSIS:foreach my $a(@$analyses){
$sth->execute($a->logic_name);
my ($analysis_id)= $sth->fetchrow;
if($analysis_id){
if($a->input_id_type){
if($db->isa('Bio::EnsEMBL::Pipeline::DBSQL::DBAdaptor')){
my $sql = "select input_id_type from input_id_type_analysis ".
"where analysis_id = ?";
my $sth = $db->prepare($sql);
$sth->execute($analysis_id);
my ($type) = $sth->fetchrow;
if($type){
throw("need ".$type." to be the same as ".$a->input_id_type .
" ( " . $a->logic_name . " ) " )
unless($type eq $a->input_id_type);
}else{
my $stored_sql = "insert into input_id_type_analysis ".
"(analysis_id, input_id_type) values(?, ?)";
my $stored_sth = $db->prepare($stored_sql);
$stored_sth->execute($analysis_id, $a->input_id_type);
}
}
}
$a->dbID($analysis_id);
$a->adaptor($analysis_adaptor);
if ($update) {
my $created_sql = "SELECT NOW();";
my $created_sth = $db->prepare($created_sql);
$created_sth->execute();
my ($current_time) = $created_sth->fetchrow;
$a->created($current_time);
}
$analysis_adaptor->update($a) if($update);
next ANALYSIS;
}else{
$analysis_adaptor->store($a);
}
} } |