File: cif_diff

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 (251 lines) | stat: -rwxr-xr-x 8,637 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
#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Revision: 8845 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.7.0/scripts/cif_diff $
#$Date: 2021-08-01 23:10:01 +0300 (Sun, 01 Aug 2021) $
#------------------------------------------------------------------------------
#*
#* Parse two CIF files and compare their content
#*
#* USAGE:
#*    $0 --options input1.cif input2.cif
#**

use strict;
use warnings;
use COD::CIF::Parser qw( parse_cif );
use COD::CIF::Data::Diff qw( diff );
use COD::ErrorHandler qw( process_errors
                          process_parser_messages
                          report_message );
use COD::Precision qw( cmp_cif_numbers );
use COD::SOptions qw( getOptions get_value );
use COD::SUsage qw( options usage );
use COD::ToolsVersion qw( get_version_string );

my $use_parser = 'c';

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

my $compare_datablock_names = 1;
my $fold_long_fields        = 1;
my $folding_width           = 72;
my %comparators = ();

sub string_compare { return $_[0] cmp $_[1] }

my $compare_only_selected = 0;
my @selected_tags = ();
my @excluded_tags = ();

my $ignore_empty_values = 0;

my $left    = 1;
my $right   = 1;
my $common  = 1;

my $L_option_param;

#* OPTIONS:
#*   -t, --tags
#*                     Whitespace- or comma-separated list of tags to compare.
#*                     When present, only tags enumerated with this option are
#*                     compared.
#*
#*   -a, --add-tag
#*                     Add a tag to the list of compared tags.
#*
#*   -c, --clear-tags
#*                     Clear the list of compared tags.
#*
#*   -f, --file-with-tags
#*                     Specify a file with tags to be compared.
#*
#*   --compare-all-tags
#*                     Compare all tags present in the compared files (default).
#*
#*   --no-compare, --exclude
#*                     Whitespace- or comma-separated list of tags not to compare.
#*                     When present, enumerated tags will be excluded from the
#*                     comparison.
#*
#*   --compare-datablock-names
#*   --dont-compare-datablock-names
#*                     Also compare the names of CIF data blocks. Default: on.
#*
#*   --compare-numeric _cell_length_a
#*                     Force numeric comparison for given tag.
#*
#*   --compare-string _cell_length_a
#*                     Force string comparison for given tag.
#*
#*   --ignore-empty-values
#*                     Ignore tags with empty values.
#*
#*   --no-ignore-empty-values
#*   --dont-ignore-empty-values
#*                     Take tags with empty values into comparison (default).
#*
#*   --no-left
#*                     Do not print tags present in the left file only.
#*                     Similar to 'comm -1'.
#*
#*   --no-right
#*                     Do not print tags present in the right file only.
#*                     Similar to 'comm -2'.
#*
#*   --no-common
#*                     Do not print tags with the same values in both files.
#*
#*   --folding-width 72
#*                     Specify the length of the longest unfolded line.
#*
#*   --fold-long-fields
#*   --dont-fold-long-fields
#*                     Fold fields, longer than folding width. Default: on.
#*
#*   -L, -u
#*                     Ignored options. Used to make this script usable
#*                     as --diff-cmd for 'svn diff'.
#*
#*   --use-perl-parser
#*                     Use Perl parser to parse CIF files.
#*   --use-c-parser
#*                     Use C parser to parse CIF files (default).
#*
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    "-L" => \$L_option_param, # Ignore the '-L' option and its argument
    "-u" => sub { 1; }, # Ignore the '-u' option, so that we can use
                        # the 'cif_diff' program as the --diff-cmd for
                        # 'svn diff'
    "-t,--tags" => sub { $compare_only_selected = 1;
                         @selected_tags = split( /\s|,/, get_value()); },
    "-a,--add-tag" => sub { $compare_only_selected = 1;
                            push( @selected_tags, get_value() ); },
    "-c,--clear-tags" => sub { $compare_only_selected = 1;
                               @selected_tags = () },
    "-f,--file-with-tags" => sub {
        $compare_only_selected = 1;
        my $filename = get_value();

        eval {
            open( my $tags, '<', $filename ) or die 'ERROR, '
                  . 'could not open file for reading -- '
                  . lcfirst($!) . "\n";

            @selected_tags = ( @selected_tags,
                                   map  { s/^\s*|\s*$//g; $_ }
                                   grep { !/^\#/ } <$tags> );

            close( $tags ) or die 'ERROR, '
                 . 'error while closing file after reading -- '
                 . lcfirst($!) . "\n";
        };
        if ($@) {
            process_errors( {
              'message'  => $@,
              'program'  => $0,
              'filename' => $filename
            }, $die_on_error_level->{ERROR} );
        };
    },
    "--compare-all-tags" => sub{ $compare_only_selected = 0; },
    "--no-compare,--exclude" =>
        sub{ @excluded_tags = split( /\s|,/, get_value()); },

    "--compare-datablock-names"      => sub{ $compare_datablock_names = 1 },
    "--dont-compare-datablock-names" => sub{ $compare_datablock_names = 0 },

    "--compare-numeric" =>
        sub{ $comparators{ get_value() } = \&cmp_cif_numbers },
    "--compare-string" =>
        sub{ $comparators{ get_value() } = \&string_compare },

    "--ignore-empty-values"      => sub{ $ignore_empty_values = 1 },
    "--no-ignore-empty-values"   => sub{ $ignore_empty_values = 0 },
    "--dont-ignore-empty-values" => sub{ $ignore_empty_values = 0 },

    "--folding-width"         => \$folding_width,
    "--fold-long-fields"      => sub{ $fold_long_fields = 1 },
    "--dont-fold-long-fields" => sub{ $fold_long_fields = 0 },
    "--no-left"     => sub{ $left   = 0 },
    "--no-right"    => sub{ $right  = 0 },
    "--no-common"   => sub{ $common = 0 },
    "--use-perl-parser" => sub { $use_parser = "perl" },
    "--use-c-parser"    => sub { $use_parser = "c" },

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

push( @ARGV, "-" ) unless @ARGV > 1;

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

if( @ARGV != 2 ) {
    report_message( {
        'program'   => $0,
        'err_level' => 'ERROR',
        'message'   => 'please supply two files for comparison'
    }, $die_on_error_level->{'ERROR'} );
}

my $parser_options = { 'parser' => $use_parser, 'no_print' => 1 };
my ( $data1, $data2, $err_count, $messages );

( $data1, $err_count, $messages ) = parse_cif( $ARGV[0], $parser_options );
process_parser_messages( $messages, $die_on_error_level );

( $data2, $err_count, $messages ) = parse_cif( $ARGV[1], $parser_options );
process_parser_messages( $messages, $die_on_error_level );

my $min_datablocks  = scalar @$data1;
if( scalar @$data1 != scalar @$data2 ) {
    if( $min_datablocks > scalar @$data2 ) {
        $min_datablocks = scalar @$data2;
    }
    report_message( {
        'program'   => $0,
        'err_level' => 'WARNING',
        'message'   => 'number of data blocks in supplied files is different, '
                     . "taking first $min_datablocks data blocks for comparison"
    }, $die_on_error_level->{'WARNING'} );
}

my $options = {
        'compare_datablock_names' => $compare_datablock_names,
        'fold'                    => $fold_long_fields,
        'folding_width'           => $folding_width,
        'comparators'             => \%comparators,
        'ignore_empty_values'     => $ignore_empty_values,
        'suppress_left'           => !$left,
        'suppress_right'          => !$right,
        'suppress_common'         => !$common };

if( $compare_only_selected == 1 ) {
    @selected_tags = map { lc($_) } @selected_tags;
    $options->{compare_only} = \@selected_tags;
}
if( @excluded_tags > 0 ) {
    @excluded_tags = map { lc($_) } @excluded_tags;
    $options->{compare_not} = \@excluded_tags;
}

for( my $i = 0; $i < $min_datablocks; $i++ ) {
    diff( $data1->[$i], $data2->[$i], $options );
}