| 1 | #!/usr/bin/perl |
|---|
| 2 | use strict; |
|---|
| 3 | use warnings; |
|---|
| 4 | |
|---|
| 5 | # ------------------------------ configure trees to keep |
|---|
| 6 | |
|---|
| 7 | # simply enter names of trees to remain in the list below. |
|---|
| 8 | my @keep = |
|---|
| 9 | ( |
|---|
| 10 | 'tree_to_keep', |
|---|
| 11 | ); |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | # ------------------------------ configure trees to keep [end] |
|---|
| 16 | |
|---|
| 17 | BEGIN { |
|---|
| 18 | if (not exists $ENV{'ARBHOME'}) { die "Environment variable \$ARBHOME has to be defined"; } |
|---|
| 19 | my $arbhome = $ENV{'ARBHOME'}; |
|---|
| 20 | push @INC, "$arbhome/lib"; |
|---|
| 21 | push @INC, "$arbhome/PERL_SCRIPTS/lib"; |
|---|
| 22 | 1; |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | use ARB; |
|---|
| 26 | use tools; |
|---|
| 27 | |
|---|
| 28 | my $gb_main = ARB::open(":","r"); |
|---|
| 29 | if (not $gb_main) { |
|---|
| 30 | my $error = ARB::await_error(); |
|---|
| 31 | die "$error"; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | die "This script will delete most trees from your database.\nTo use, edit it, uncomment this line and save it locally.\nAlso edit list of kept trees to fit your needs."; |
|---|
| 35 | |
|---|
| 36 | # ------------------------------ read existing trees |
|---|
| 37 | |
|---|
| 38 | dieOnError(ARB::begin_transaction($gb_main), 'begin_transaction'); |
|---|
| 39 | |
|---|
| 40 | my $gb_trees = ARB::search($gb_main, '/tree_data', 'NONE'); |
|---|
| 41 | die 'failed to find tree-data' if not $gb_trees; |
|---|
| 42 | |
|---|
| 43 | my @trees = (); |
|---|
| 44 | |
|---|
| 45 | for (my $gb_sub = ARB::child($gb_trees); $gb_sub; $gb_sub = ARB::nextChild($gb_sub)) { # read all childs |
|---|
| 46 | my $name = ARB::read_key($gb_sub); # the tree name is "stored" in the key name :-( |
|---|
| 47 | expectError('read name of tree') if not $name; |
|---|
| 48 | if (substr($name, 0, 5) eq 'tree_') { |
|---|
| 49 | push @trees, $name; |
|---|
| 50 | } |
|---|
| 51 | else { |
|---|
| 52 | print "Skipping entry '$name' (not a tree)\n"; |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | dieOnError(ARB::commit_transaction($gb_main), 'commit_transaction'); |
|---|
| 57 | |
|---|
| 58 | my $trees = scalar(@trees); |
|---|
| 59 | BIO::message($gb_main, "Found $trees trees"); |
|---|
| 60 | |
|---|
| 61 | # ------------------------------ read existing trees [end] |
|---|
| 62 | # ------------------------------ decide which trees to delete |
|---|
| 63 | |
|---|
| 64 | my %keep = map { $_ => 1; } @keep; |
|---|
| 65 | my @delete = grep { not defined $keep{$_}; } @trees; |
|---|
| 66 | |
|---|
| 67 | my $keep = scalar(@keep); |
|---|
| 68 | my $delete = scalar(@delete); |
|---|
| 69 | BIO::message($gb_main, "Deleting $delete trees ($keep listed as protected)"); |
|---|
| 70 | |
|---|
| 71 | # ------------------------------ decide which trees to delete [end] |
|---|
| 72 | |
|---|
| 73 | BIO::remote_action($gb_main,'ARB_NT','tree_admin'); |
|---|
| 74 | |
|---|
| 75 | my $count = 0; |
|---|
| 76 | foreach my $treedel (@delete) { |
|---|
| 77 | ++$count; |
|---|
| 78 | my $percent = int($count/$delete*100.0); |
|---|
| 79 | print "Deleting tree $count/$delete ($percent%, '$treedel')\n"; |
|---|
| 80 | BIO::remote_awar($gb_main,'ARB_NT','tmp/ad_tree/tree_name', $treedel); |
|---|
| 81 | BIO::remote_action($gb_main,'ARB_NT','TREE_ADMIN/DELETE'); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | BIO::message($gb_main, "$delete unlisted trees have been deleted."); |
|---|
| 85 | BIO::message($gb_main, "Check remaining trees before you overwrite your database."); |
|---|
| 86 | |
|---|
| 87 | # recording stopped @ Sun Dec 26 11:19:28 2021 |
|---|
| 88 | ARB::close($gb_main); |
|---|