File: cif_cod_numbers

package info (click to toggle)
cod-tools 3.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 154,792 kB
  • sloc: perl: 57,588; sh: 36,842; ansic: 6,402; xml: 1,982; yacc: 1,117; makefile: 727; python: 166
file content (351 lines) | stat: -rwxr-xr-x 13,620 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2020-09-29 10:54:47 +0300 (Tue, 29 Sep 2020) $
#$Revision: 8533 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.7.0/scripts/cif_cod_numbers $
#------------------------------------------------------------------------------
#*
#* Find COD numbers for the CIF files in given directories of file lists.
#*
#* USAGE:
#*    $0 --options input1.cif input*.cif
#**

use strict;
use warnings;
use COD::CIF::Parser qw( parse_cif );
use COD::CIF::Data::CODNumbers qw( cif_fill_data fetch_duplicates );
use COD::CIF::Tags::CanonicalNames qw( canonicalize_all_names );
use COD::SOptions qw( getOptions );
use COD::SUsage qw( usage options );
use COD::ErrorHandler qw( process_warnings
                          process_errors
                          process_parser_messages );
use COD::ToolsVersion qw( get_version_string );

my %database = (
        host  => 'www.crystallography.net',
        user  => 'cod_reader',
        name  => 'cod',
        table => 'data',
        password => '',
        platform => 'mysql',
    );

my $use_parser = 'c';
my $input_format = 'cif';

my $die_on_error_level = {
    ERROR   => 1,
    WARNING => 0,
    NOTE    => 0
};

my $print_datablock_name = 0;

my %options = (
    'check_bibliography' => 1,
    'check_compound_source' => 0,
    'check_sample_history' => 0,
    'use_su' => 1,
    'max_cell_length_diff' => 0.5, # angstrom
    'max_cell_angle_diff'  => 1.2, # degree of arc
);
my %cif_fill_data_options = (
    'use_attached_hydrogens' => 1,
);

#* OPTIONS:
#*   --platform 'SQLite'
#*                     Use the SQL database platform 'SQLite' to query structures
#*                     (default: 'mysql').
#*
#*   -d, --database  cod
#*                     Use database "cod" to query for structures.
#*
#*   -h, --host   www.crystallography.net
#*   -s, --server www.crystallography.net
#*                     Query COD database on the host 'www.crystallography.net'.
#*
#*   -l, --localhost
#*                     Use database server on the localhost to query the
#*                     COD database.
#*
#*   -p, --port 3306
#*                     Use use the specified port to query structures
#*                     (default: 3306).
#*
#*   -t, --table  data
#*                     Use SQL table "data" to query for structures.
#*
#*   -u, --user cod_reader
#*                     Use user name "cod_reader" to access COD database;
#*                     this reader should be granted SELECT privilege, i.e.
#*                     should be able to read the COD database without
#*                     supplying a password.
#*
#*   --password
#*                     Use the specified password to connect (default: '').
#*
#*   -P, --print-datablock-name
#*                     Print data block name as the fifth column.
#*   -P-, --no-print-datablock-name, --dont-print-datablock-name
#*                     Do not print data block names (default).
#*
#*   -S, --series 9
#*                     Check only COD entries starting with 9 (so called 9* series).
#*                     Default: check the whole COD database. Set series to
#*                     "" (empty string) to restore the default behaviour.
#*
#*   --max-cell-length-difference 0.5
#*                     Maximum difference of unit cell lengths allowed
#*                     for entries regarded as the same, in angstroms
#*                     (default: 0.5).
#*
#*   --max-cell-angle-difference 1.2
#*                     Maximum difference of unit cell angles allowed for
#*                     entries regarded as the same, in angstroms
#*                     (default: 1.2).
#*
#*   --check-bibliography
#*                     Only CIFs that have different bibliography data are
#*                     declared different if all other parameters match
#*                     CIFs with missing bibliographies are assumed to
#*                     have matching bibliographies (default).
#*   --no-check-bibliography, --dont-check-bibliography
#*                     Ignore bibliographic data of all CIFs; thus even files
#*                     with different bibliographies will be regarded the same
#*                     if their cells, chemical formulae and measurement
#*                     conditions match.
#*
#*   --use-sigma, --no-ignore-sigma, --dont-ignore-sigma
#*                     Use standard deviations (sigmas) when comparing unit
#*                     cell constants (default).
#*   --no-use-sigma, --dont-use-sigma, --ignore-sigma
#*                     Ignore standard deviations (sigmas) when comparing unit
#*                     cell constants.
#*
#*   --check-sample-history
#*                     Only CIFs that have different sample history data
#*                     (as recorded in the _exptl_crystal_thermal_history
#*                     and _exptl_crystal_pressure_history tags) are declared
#*                     different if all other parameters match.
#*   --no-check-sample-history,
#*   --dont-check-sample-history,
#*   --disregard-sample-history
#*                     Ignore sample history of all CIFs; thus even files
#*                     with different sample histories will be regarded the
#*                     same if their cells, chemical formulae and measurement
#*                     conditions match (default).
#*
#*   --check-compound-source
#*                     Only CIFs that have different compound source
#*                     (as recorded in the _chemical_compound_source)
#*                     are declared different if all other parameters
#*                     match.
#*   --no-check-compound-source,
#*   --dont-check-compound-source,
#*   --disregard-compound-source
#*                     Ignore compound source of all CIFs; thus even files
#*                     with different compound sources will be regarded the
#*                     same if other conditions match (default).
#*
#*   --use-attached-hydrogens
#*                     Include number of implicit hydrogens, specified using
#*                     _atom_site_attached_hydrogens tag, into the formula
#*                     sum (default).
#*   --no-use-attached-hydrogens,
#*   --dont-use-attached-hydrogens,
#*   --ignore-attached-hydrogens
#*                     Ignore number of implicit hydrogens, specified using
#*                     _atom_site_attached_hydrogens tag, in calculation of
#*                     the formula sum.
#*
#*   --use-perl-parser
#*                     Use the Perl-only CIF parser.
#*   --use-c-parser
#*                     Use the speed-optimised C/Perl parser (default).
#*
#*   --cif-input
#*                     Use CIF format for input (default).
#*   --json-input
#*                     Use JSON format for input.
#*
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    '-d,--database'  => \$database{'name'},
    '-l,--localhost' => sub { $database{'host'} = 'localhost' },
    '-h,--host'      => \$database{'host'},
    '-p,--port'      => \$database{'port'},
    '-s,--server'    => \$database{'host'},
    '-t,--table'     => \$database{'table'},
    '-u,--user'      => \$database{'user'},
    '--password'     => \$database{'password'},
    '--platform'     => \$database{'platform'},

    '-P,--print-datablock-name'     => sub{ $print_datablock_name = 1 },
    '-P-,--no-print-datablock-name' => sub{ $print_datablock_name = 0 },
    '--dont-print-datablock-name'   => sub{ $print_datablock_name = 0 },

    '-S,--series' => \$options{'cod_series_prefix'},

    '--max-cell-length-difference' => \$options{'max_cell_length_diff'},
    '--max-cell-angle-difference'  => \$options{'max_cell_angle_diff'},

    '--check-bibliography'      => sub { $options{'check_bibliography'} = 1 },
    '--dont-check-bibliography' => sub { $options{'check_bibliography'} = 0 },
    '--no-check-bibliography'   => sub { $options{'check_bibliography'} = 0 },

    '--check-sample-history'
                            => sub { $options{'check_sample_history'} = 1 },
    '--dont-check-sample-history'
                            => sub { $options{'check_sample_history'} = 0 },
    '--no-check-sample-history'
                            => sub { $options{'check_sample_history'} = 0 },
    '--disregard-sample-history'
                            => sub { $options{'check_sample_history'} = 0 },

    '--check-compound-source'
                            => sub { $options{'check_compound_source'} = 1 },
    '--dont-check-compound-source'
                            => sub { $options{'check_compound_source'} = 0 },
    '--no-check-compound-source'
                            => sub { $options{'check_compound_source'} = 0 },
    '--disregard-compound-source'
                            => sub { $options{'check_compound_source'} = 0 },

    '--ignore-sigma'        => sub{ $options{'use_su'} = 0 },
    '--dont-ignore-sigma'   => sub{ $options{'use_su'} = 1 },
    '--no-ignore-sigma'     => sub{ $options{'use_su'} = 1 },

    '--use-sigma'           => sub{ $options{'use_su'} = 1 },
    '--dont-use-sigma'      => sub{ $options{'use_su'} = 0 },
    '--no-use-sigma'        => sub{ $options{'use_su'} = 0 },

    '--use-attached-hydrogens' =>
            sub { $cif_fill_data_options{'use_attached_hydrogens'} = 1 },
    '--dont-use-attached-hydrogens' =>
            sub { $cif_fill_data_options{'use_attached_hydrogens'} = 0 },
    '--no-use-attached-hydrogens' =>
            sub { $cif_fill_data_options{'use_attached_hydrogens'} = 0 },
    '--ignore-attached-hydrogens' =>
            sub { $cif_fill_data_options{'use_attached_hydrogens'} = 0 },

    '--use-perl-parser'       => sub{ $use_parser = 'perl' },
    '--use-c-parser'          => sub{ $use_parser = 'c' },

    '--cif-input'  => sub { $input_format = 'cif' },
    '--json-input' => sub { $input_format = 'json' },

    '--options'      => sub { options; exit },
    '--help,--usage' => sub { usage; exit },
    '--version'      => sub { print get_version_string(), "\n"; exit }
);

if( $input_format eq 'json' ) {
    $use_parser = 'json';
}

@ARGV = ('-') unless @ARGV;

binmode STDOUT, ':encoding(UTF-8)';
binmode STDERR, ':encoding(UTF-8)';

for my $filename (@ARGV) {
    my $parser_options = { 'parser' => $use_parser, 'no_print' => 1 };
    my ( $data, $err_count, $messages ) = parse_cif( $filename, $parser_options );
    process_parser_messages( $messages, $die_on_error_level );

    canonicalize_all_names( $data );

    local $SIG{__WARN__} = sub { process_warnings( {
                                    'message'       => @_,
                                    'program'       => $0,
                                    'filename'      => $filename
                               }, $die_on_error_level ) };

    if( !defined $data->[0] || !defined $data->[0]{name} ) {
        warn "WARNING, file seems to be empty\n";
        next;
    }

    my %structures = ();
    my $index = 0;
    foreach my $dataset ( @{$data} ) {

        my $dataname = 'data_' . $dataset->{name};

        local $SIG{__WARN__} = sub { process_warnings( {
                                       'message'       => @_,
                                       'program'       => $0,
                                       'filename'      => $filename,
                                       'add_pos'       => $dataname
                                     }, $die_on_error_level ) };

        eval {
            my $structure = cif_fill_data( $dataset,
                                           $filename,
                                           $index,
                                           \%cif_fill_data_options );
            if ( defined $structure ) {
                $structures{$structure->{id}} = $structure;
                $index++;
            }
        };
        if ($@) {
            process_errors( {
              'message'  => $@,
              'program'  => $0,
              'filename' => $filename,
              'add_pos'  => $dataname
            }, $die_on_error_level->{'ERROR'} );
        };
    }

    my $duplicates;
    eval {
        $duplicates = fetch_duplicates( \%structures, \%database, \%options );
    };
    if ($@) {
        process_errors( {
            'message'  => $@,
            'program'  => $0,
            'filename' => $filename,
            }, $die_on_error_level->{'ERROR'} );
    };

    for my $datablock ( @{$duplicates} ) {
        if( keys %{ $datablock->{duplicates} } > 0 ) {
            for my $key (sort keys %{ $datablock->{duplicates} } ) {
                my $cod_entry = $datablock->{duplicates}{$key};
                printf
                    '%-35s %15s %3d %s',
                    $datablock->{formula},
                    $cod_entry->{filename},
                    scalar keys %{ $datablock->{duplicates} },
                    $filename;
                if( $print_datablock_name ) {
                    printf ' %s', $datablock->{datablock}{id};
                }
                print "\n"
            }
        } else {
            printf
                '%-35s %15s %3d %s',
                $datablock->{formula}, '?', 0,
                $filename;
            if( $print_datablock_name ) {
                printf ' %s', $datablock->{datablock}{id};
            }
            print "\n"
        }
    }
}