source: branches/properties/lib/macros/keep_listed_fields.amc

Last change on this file was 19350, checked in by westram, 2 years ago
  • fix info message: protected are only kept when they exist.
  • show warning after deletion.
  • Property svn:executable set to *
File size: 3.4 KB
Line 
1#!/usr/bin/perl
2use strict;
3use 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_')
9my @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
23BEGIN {
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
31use ARB;
32use tools;
33
34my $gb_main = ARB::open(":","r");
35if (not $gb_main) {
36  my $error = ARB::await_error();
37  die "$error";
38}
39
40die "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
45BIO::remote_action($gb_main,'ARB_NT','ARB_NT/INFO');
46
47BIO::message($gb_main, "Refreshing list of existing keys..");
48BIO::remote_action($gb_main,'ARB_NT','spec_refresh_fields');
49
50# ------------------------------ read existing keys
51
52dieOnError(ARB::begin_transaction($gb_main), 'begin_transaction');
53
54my $gb_keys = ARB::search($gb_main, '/presets/key_data', 'NONE');
55die 'failed to find key-data' if not $gb_keys;
56
57my @keys = ();
58
59for (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
84dieOnError(ARB::commit_transaction($gb_main), 'commit_transaction');
85
86my $keys = scalar(@keys);
87BIO::message($gb_main, "Found $keys keys");
88
89# ------------------------------ read existing keys [end]
90# ------------------------------ decide which keys to delete
91
92my %keep = map { $_ => 1; } @keep;
93my @delete = grep { not defined $keep{$_}; } @keys;
94
95my $keep = scalar(@keep);
96my $delete = scalar(@delete);
97BIO::message($gb_main, "Deleting $delete keys ($keep listed as protected)");
98
99# ------------------------------ decide which keys to delete [end]
100
101BIO::remote_action($gb_main,'ARB_NT','SPECIES_INFORMATION/spec_delete_field');
102BIO::remote_action($gb_main,'ARB_NT','spec_refresh_fields');
103
104my $count = 0;
105foreach 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
113BIO::message($gb_main, "$delete unlisted keys have been deleted.");
114BIO::message($gb_main, "Check remaining keys before you overwrite your database.");
115
116# recording stopped @ Sun Dec 26 11:19:28 2021
117ARB::close($gb_main);
Note: See TracBrowser for help on using the repository browser.