File: find_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 (212 lines) | stat: -rwxr-xr-x 6,007 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
#! /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/find_numbers $
#------------------------------------------------------------------------------
#*
#* Find COD numbers for the .cif files in given directories of file lists.
#*
#* USAGE:
#*    $0 my-cif-dir1/ my-cif-dir2/files*.cif COD-cif-dir/
#* USAGE:
#*    $0 input1.cif input1_alt.cif
#**

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

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

#* OPTIONS:
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    '--help,--usage' => sub { usage; exit },
    '--version'      => sub { print get_version_string(), "\n"; exit }
);

my $check_bibliography = 1;

my $max_cell_length_diff = 0.5; # Angstroems
my $max_cell_angle_diff  = 1.2; # degrees

my %has_numeric_value = (
    '_journal_year'   => 1,
    '_journal_volume' => 1,
    '_journal_issue'  => 1,
);

my %skip_tag = (
    '_journal_name_full' => 0,
);

my %COD = ();

if( @ARGV < 2 ) {
    report_message( {
        'program'   => $0,
        'err_level' => 'ERROR',
        'message'   => 'please supply at least two directory names on the '
                     . 'command line -- names of directories with the '
                     . 'analysed CIF files come first and the name of the '
                     . 'directory with COD CIF files comes last'
    }, 1 );
}

my $COD_cif_dir = pop @ARGV;
my @COD_cif_files = `find $COD_cif_dir -name "*.cif" -o -name "*.CIF"`;

do {
    print int @COD_cif_files . "\n";
    print "@COD_cif_files";
} if 0;

for my $file (sort @COD_cif_files) {

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

    chomp $file;
    my %structures = ();
    eval {
        my $structures = structures_from_cif_file( $file );
        %structures = %$structures;
    }; # end of eval
    if ($@) {
        process_errors( {
          'program'       => $0,
          'filename'      => $file,
          'message'       => $@,
        }, $die_on_error_level->{'ERROR'} )
    };

    for my $id (sort keys %structures) {
        my $formula = $structures{$id}{chemical_formula_sum};

        $formula = '?' unless defined $formula;

        push( @{$COD{$formula}}, $structures{$id} );
    }
}

do {
    use COD::Serialise qw( serialiseRef );
    serialiseRef( \%COD );
} if 0;

#------------------------------------------------------------------------------

my @cif_files = `find @ARGV -name "*.cif" -o -name "*.CIF"`;

for my $file (sort @cif_files) {

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

    chomp $file;
    my %structures = ();
    eval {
        my $structures = structures_from_cif_file( $file );
        %structures = %$structures;
    }; # end of eval
    if ($@) {
        process_errors( {
          'program'       => $0,
          'filename'      => $file,
          'message'       => $@,
        }, $die_on_error_level->{'ERROR'} )
    };

    for my $id (sort keys %structures) {
        my $formula = $structures{$id}{chemical_formula_sum};

        $formula = '?' unless defined $formula;

        my $final_formula = $formula;
        $final_formula =~ s/\s/_/g;

        my $n = 0;
        if( defined $COD{$formula} ) {
            for my $COD_entry (@{$COD{$formula}}) {
                if( entries_are_the_same( $structures{$id},
                                          $COD_entry,
                                          {  check_bibliography =>
                                            $check_bibliography } ) ) {
                    $n++;
                }
            }
        }
        if( $n > 0 ) {
            for my $COD_entry (@{$COD{$formula}}) {
                if( entries_are_the_same( $structures{$id},
                                          $COD_entry,
                                          {  check_bibliography =>
                                            $check_bibliography } ) ) {
                    printf
                        "%-35s %15s %3d %s\n",
                        $final_formula,
                        $COD_entry->{filename}, $n, $file;
                }
            }
        } else {
            printf "%-35s %15s %3d %s\n", $final_formula, '?', 0, $file;
        }
    }

}

#------------------------------------------------------------------------------

sub structures_from_cif_file
{
    my( $filename ) = @_;

    my $options = { 'no_print' => 1 };
    my ( $data, $err_count, $messages ) = parse_cif( $filename, $options );
    process_parser_messages( $messages, $die_on_error_level );

    return {} if $err_count > 0;

    canonicalize_all_names( $data );

    my %structures;
    for my $datablock (@$data) {
        my $structure = cif_fill_data( $datablock, $filename );
        $structures{$datablock->{name}} = $structure;
    }

    return \%structures;
}