source: branches/properties/PERL_SCRIPTS/MACROS/with_all_marked.pl

Last change on this file was 18478, checked in by westram, 4 years ago
  • mark other scripts that might need "eval vs. die-catcher" fixes.
  • Property svn:executable set to *
File size: 4.3 KB
Line 
1#!/usr/bin/perl
2use strict;
3use warnings;
4
5my $sleepAmount = 0; # try to increase (to a small amount of seconds) if you encounter problems
6
7# This script calls a macro with all marked/found species.
8
9BEGIN {
10  if (not exists $ENV{'ARBHOME'}) { die "Environment variable \$ARBHOME has to be defined"; }
11  my $arbhome = $ENV{'ARBHOME'};
12  push @INC, "$arbhome/lib";
13  push @INC, "$arbhome/PERL_SCRIPTS/lib";
14  1;
15}
16
17use ARB;
18use tools;
19
20sub selectSpecies($$) {
21  my ($gb_main,$speciesName) = @_;
22  BIO::remote_awar($gb_main,"ARB_NT","tmp/focus/species_name", $speciesName);
23}
24
25sub markSpecies($$$) {
26  my ($gb_main,$gb_species,$mark) = @_;
27
28  dieOnError(ARB::begin_transaction($gb_main), 'begin_transaction');
29  ARB::write_flag($gb_species, $mark);
30  ARB::commit_transaction($gb_main);
31}
32
33sub exec_macro_with_species($$$$) {
34  my ($gb_main,$gb_species,$speciesName,$macroName) = @_;
35
36  BIO::mark_all($gb_main, 0); # unmark all
37  markSpecies($gb_main,$gb_species,1);
38  selectSpecies($gb_main,$speciesName);
39
40  my $cmd = "perl '$macroName'";
41  system($cmd)==0 || die "Error: failed to execute '$cmd'";
42
43  if ($sleepAmount>0) {
44    print "Sleep $sleepAmount sec..\n";
45    sleep($sleepAmount);
46  }
47
48  selectSpecies($gb_main,'');
49}
50
51sub collectMarked($\@\%) {
52  my ($gb_main,$name_r,$gbdata_r) = @_;
53
54  dieOnError(ARB::begin_transaction($gb_main), 'begin_transaction');
55
56  for (my $gb_species = BIO::first_marked_species($gb_main);
57       $gb_species;
58       $gb_species = BIO::next_marked_species($gb_species)) {
59    my $species_name = BIO::read_string($gb_species, "name");
60    $species_name || expectError('read_string');
61    push @$name_r, $species_name;
62    $$gbdata_r{$species_name} = $gb_species;
63  }
64
65  ARB::commit_transaction($gb_main);
66}
67
68sub acceptExisting($) {
69  my ($file) = @_;
70  return (-f $file) ? $file : undef;
71}
72sub findMacroIn($$) {
73  my ($name,$dir) = @_;
74  my $full = acceptExisting($dir.'/'.$name);
75  if (not defined $full) { $full = acceptExisting($dir.'/'.$name.'.amc'); }
76  return $full;
77}
78sub findMacro($) {
79  my ($name) = @_;
80  my $full = acceptExisting($name); # accept macro specified with full path
81  if (not defined $full) { $full = findMacroIn($name, ARB::getenvARBMACROHOME()); }
82  if (not defined $full) { $full = findMacroIn($name, ARB::getenvARBMACRO()); }
83  return $full;
84}
85
86sub execMacroWith() {
87  my $gb_main = ARB::open(":","r");
88  $gb_main || expectError('db connect (no running ARB?)');
89
90  my $err = undef;
91  {
92    my $args = scalar(@ARGV);
93    if ($args != 1) {
94      die "Usage: with_all_marked.pl macro\n".
95        "Executes 'macro' once for each marked species.\n".
96          "For each call to 'macro', exactly one species will be marked AND selected.\n ";
97    }
98
99    my ($macro) = @ARGV;
100
101    {
102      my $omacro = $macro;
103      $macro = findMacro($macro);
104      if (not defined $macro) { die "Failed to detect macro '$omacro'\n "; }
105    }
106
107    my $restoreMarked = 1;
108
109    my %gb_species = (); # key = name; value = GBDATA(species)
110    my @names      = (); # contains names of %gb_species (in DB order)
111
112    collectMarked($gb_main,@names,%gb_species);
113
114    # perform loop with collected species:
115    my $count = scalar(@names);
116    if ($count<1) { die "No marked species - nothing to do\n"; }
117
118    eval { # @@@ eval is broken (need to use set_inGlobalEvalState)
119      if ($count>0) {
120        print "Executing '$macro' with $count species:\n";
121        for (my $c=0; $c<$count; ++$c) {
122          my $species = $names[$c];
123          my $gb_species = $gb_species{$species};
124          print "- with '$species' ".($c+1)."/$count (".(int(($c+1)*10000/$count)/100)."%)\n";
125          exec_macro_with_species($gb_main,$gb_species,$species,$macro);
126        }
127
128      }
129    };
130    $err = $@;
131
132    # mark species again
133    if ($restoreMarked and ($count>0)) {
134      print "Restoring old marks..\n";
135      dieOnError(ARB::begin_transaction($gb_main), 'begin_transaction');
136      BIO::mark_all($gb_main, 0); # unmark all
137      for (my $c=0; $c<$count; ++$c) {
138        my $species = $names[$c];
139        my $gb_species = $gb_species{$species};
140        ARB::write_flag($gb_species, 1);
141      }
142      ARB::commit_transaction($gb_main);
143    }
144  }
145  ARB::close($gb_main);
146
147  if ($err) {
148    {
149      my $errEsc = $err;
150      $errEsc =~ s/\"/\\\"/go;
151      my $cmd = "arb_message \"$errEsc\"";
152      system($cmd)==0 || print "failed to execute '$cmd'";
153    }
154    die $err;
155  }
156}
157
158execMacroWith();
Note: See TracBrowser for help on using the repository browser.