1 | #!/usr/bin/perl |
---|
2 | use strict; |
---|
3 | use warnings; |
---|
4 | |
---|
5 | # ------------------------------ configure keys to keep |
---|
6 | |
---|
7 | # simply enter names of keys to remain in the list below. |
---|
8 | # Note: containers will be deleted as well (but never if name starts with 'ali_') |
---|
9 | my @keep = |
---|
10 | ( |
---|
11 | 'name', |
---|
12 | 'acc', |
---|
13 | 'full_name', |
---|
14 | 'journal', |
---|
15 | 'author', |
---|
16 | 'title', |
---|
17 | ); |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | # ------------------------------ configure keys to keep [end] |
---|
22 | |
---|
23 | BEGIN { |
---|
24 | if (not exists $ENV{'ARBHOME'}) { die "Environment variable \$ARBHOME has to be defined"; } |
---|
25 | my $arbhome = $ENV{'ARBHOME'}; |
---|
26 | push @INC, "$arbhome/lib"; |
---|
27 | push @INC, "$arbhome/PERL_SCRIPTS/lib"; |
---|
28 | 1; |
---|
29 | } |
---|
30 | |
---|
31 | use ARB; |
---|
32 | use tools; |
---|
33 | |
---|
34 | my $gb_main = ARB::open(":","r"); |
---|
35 | if (not $gb_main) { |
---|
36 | my $error = ARB::await_error(); |
---|
37 | die "$error"; |
---|
38 | } |
---|
39 | |
---|
40 | die "This script will delete most database fields.\nTo use, edit it, uncomment this line and save it locally.\nAlso edit list of kept fields to fit your needs."; |
---|
41 | |
---|
42 | # ------------------------------ refresh list of existing keys |
---|
43 | |
---|
44 | # recording started @ Sun Dec 26 11:19:16 2021 |
---|
45 | BIO::remote_action($gb_main,'ARB_NT','ARB_NT/INFO'); |
---|
46 | |
---|
47 | BIO::message($gb_main, "Refreshing list of existing keys.."); |
---|
48 | BIO::remote_action($gb_main,'ARB_NT','spec_refresh_fields'); |
---|
49 | |
---|
50 | # ------------------------------ read existing keys |
---|
51 | |
---|
52 | dieOnError(ARB::begin_transaction($gb_main), 'begin_transaction'); |
---|
53 | |
---|
54 | my $gb_keys = ARB::search($gb_main, '/presets/key_data', 'NONE'); |
---|
55 | die 'failed to find key-data' if not $gb_keys; |
---|
56 | |
---|
57 | my @keys = (); |
---|
58 | |
---|
59 | for (my $gb_key = ARB::entry($gb_keys, 'key'); $gb_key; $gb_key = ARB::nextEntry($gb_key)) { |
---|
60 | my $gb_name = ARB::entry($gb_key, 'key_name'); |
---|
61 | die 'key lacks key_name' if not $gb_name; |
---|
62 | my $name = ARB::read_string($gb_name); |
---|
63 | expectError('read name of key') if not $name; |
---|
64 | |
---|
65 | my $whyNotDelete = undef; |
---|
66 | if ($name =~ /\/.*$/o) { |
---|
67 | $whyNotDelete = "keep subentry '$name' (handled with container '$`')"; |
---|
68 | } |
---|
69 | if ($name =~ /^ali_/o) { |
---|
70 | my $gb_type = ARB::entry($gb_key, 'key_type'); |
---|
71 | die "key '$name' lacks key_type" if not $gb_type; |
---|
72 | my $type = ARB::read_int($gb_type); |
---|
73 | $whyNotDelete = "keep alignment '$name'" if $type==15; # type 15 is container; |
---|
74 | } |
---|
75 | |
---|
76 | if (defined $whyNotDelete) { |
---|
77 | BIO::message($gb_main, $whyNotDelete); |
---|
78 | } |
---|
79 | else { |
---|
80 | push @keys, $name; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | dieOnError(ARB::commit_transaction($gb_main), 'commit_transaction'); |
---|
85 | |
---|
86 | my $keys = scalar(@keys); |
---|
87 | BIO::message($gb_main, "Found $keys keys"); |
---|
88 | |
---|
89 | # ------------------------------ read existing keys [end] |
---|
90 | # ------------------------------ decide which keys to delete |
---|
91 | |
---|
92 | my %keep = map { $_ => 1; } @keep; |
---|
93 | my @delete = grep { not defined $keep{$_}; } @keys; |
---|
94 | |
---|
95 | my $keep = scalar(@keep); |
---|
96 | my $delete = scalar(@delete); |
---|
97 | BIO::message($gb_main, "Deleting $delete keys ($keep listed as protected)"); |
---|
98 | |
---|
99 | # ------------------------------ decide which keys to delete [end] |
---|
100 | |
---|
101 | BIO::remote_action($gb_main,'ARB_NT','SPECIES_INFORMATION/spec_delete_field'); |
---|
102 | BIO::remote_action($gb_main,'ARB_NT','spec_refresh_fields'); |
---|
103 | |
---|
104 | my $count = 0; |
---|
105 | foreach my $keydel (@delete) { |
---|
106 | ++$count; |
---|
107 | my $percent = int($count/$delete*100.0); |
---|
108 | print "Deleting field $count/$delete ($percent%, '$keydel')\n"; |
---|
109 | BIO::remote_awar($gb_main,'ARB_NT','tmp/adfield/species/source', $keydel); |
---|
110 | BIO::remote_action($gb_main,'ARB_NT','DELETE_FIELD/DELETE_FIELD'); |
---|
111 | } |
---|
112 | |
---|
113 | BIO::message($gb_main, "$delete unlisted keys have been deleted."); |
---|
114 | BIO::message($gb_main, "Check remaining keys before you overwrite your database."); |
---|
115 | |
---|
116 | # recording stopped @ Sun Dec 26 11:19:28 2021 |
---|
117 | ARB::close($gb_main); |
---|