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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2021-04-28 19:35:53 +0300 (Wed, 28 Apr 2021) $
#$Revision: 8738 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.7.0/scripts/cif_find_duplicates $
#------------------------------------------------------------------------------
#*
#* Find COD numbers for the .cif files in given directories of file lists.
#*
#* USAGE:
#* $0 --options my-cif-dir1/ my-cif-dir2/files*.cif COD-cif-dir/
#**
use strict;
use warnings;
use File::Basename qw( basename );
use COD::CIF::Parser qw( parse_cif );
use COD::CIF::Data::CODNumbers qw( cif_fill_data entries_are_the_same );
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
report_message );
use COD::ToolsVersion qw( get_version_string );
my $die_on_errors = 1;
my $die_on_warnings = 0;
my $die_on_notes = 0;
my $use_parser = 'c';
# default options
my %options = (
'max_cell_length_diff' => 0.5, # angstroms
'max_cell_angle_diff' => 1.2, # degrees
'check_bibliography' => 1,
'check_sample_history' => 0,
'use_su' => 1,
);
my %cif_fill_data_options = (
'use_attached_hydrogens' => 0,
);
#* OPTIONS:
#* -c, --always-continue
#* Continue processing and return successful return status
#* even if errors are diagnosed.
#* -c-, --always-die
#* Stop and return error status if errors are diagnosed.
#*
#* --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, --dont-ignore-sigma, --no-ignore-sigma
#* Use standard deviations (sigmas) when comparing unit
#* cell constants (default).
#* --no-use-sigma, --dont-use-sigma, --ignore-sigma
#* Ignore standard uncertainties (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).
#*
#* --use-attached-hydrogens
#* Include the number of implicit hydrogen atoms
#* when calculating the summary chemical formula.
#* Implicit hydrogen atoms are specified using
#* the _atom_site_attached_hydrogens data item.
#* --no-use-attached-hydrogens,
#* --ignore-attached-hydrogens
#* Ignore the number of implicit hydrogen atoms
#* when calculating the summary chemical formula.
#* Implicit hydrogen atoms are specified using
#* the _atom_site_attached_hydrogens data item
#* (default).
#*
#* --continue-on-errors
#* Do not terminate script if errors are raised.
#* --die-on-errors, --dont-continue-on-errors,
#* --do-not-continue-on-errors, --no-continue-on-errors
#* Terminate script immediately if errors are raised (default).
#* --continue-on-warnings
#* Do not terminate script if warnings are raised (default).
#* --die-on-warnings
#* Terminate script immediately if warnings are raised.
#* --continue-on-notes
#* Do not terminate script if notes are raised (default).
#* --die-on-notes
#* Terminate script immediately if notes are raised.
#*
#* --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(
'--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 },
'--no-check-bibliography' => sub { $options{'check_bibliography'} = 0 },
'--dont-check-bibliography' => sub { $options{'check_bibliography'} = 0 },
'--check-sample-history'
=> sub { $options{'check_sample_history'} = 1 },
'--no-check-sample-history'
=> sub { $options{'check_sample_history'} = 0 },
'--dont-check-sample-history'
=> sub { $options{'check_sample_history'} = 0 },
'--disregard-sample-history'
=> sub { $options{'check_sample_history'} = 0 },
'--use-sigma' => sub { $options{'use_su'} = 1 },
'--no-ignore-sigma' => sub { $options{'use_su'} = 1 },
'--dont-ignore-sigma' => sub { $options{'use_su'} = 1 },
'--no-use-sigma' => sub { $options{'use_su'} = 1 },
'--dont-use-sigma' => sub { $options{'use_su'} = 1 },
'--ignore-sigma' => sub { $options{'use_su'} = 1 },
'--use-attached-hydrogens'
=> sub { $cif_fill_data_options{'use_attached_hydrogens'} = 1 },
'--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 },
'-c,--always-continue' => sub { $die_on_errors = 0;
$die_on_warnings = 0;
$die_on_notes = 0 },
'-c-,--always-die' => sub { $die_on_errors = 1;
$die_on_warnings = 1;
$die_on_notes = 1 },
'--continue-on-errors' => sub { $die_on_errors = 0 },
'--dont-continue-on-errors' => sub { $die_on_errors = 1 },
'--die-on-errors' => sub { $die_on_errors = 1 },
'--do-not-continue-on-errors' => sub { $die_on_errors = 1 },
'--no-continue-on-errors' => sub { $die_on_errors = 1 },
'--continue-on-warnings' => sub { $die_on_warnings = 0 },
'--die-on-warnings' => sub { $die_on_warnings = 1 },
'--continue-on-notes' => sub { $die_on_notes = 0 },
'--die-on-notes' => sub { $die_on_notes = 1 },
'--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 }
);
my $die_on_error_level = {
'ERROR' => $die_on_errors,
'WARNING' => $die_on_warnings,
'NOTE' => $die_on_notes
};
my %has_numeric_value = (
'_journal_year' => 1,
'_journal_volume' => 1,
'_journal_issue' => 1,
);
my %skip_tag = (
'_journal_name_full' => 0,
);
my %COD = ();
binmode STDOUT, ':encoding(UTF-8)';
binmode STDERR, ':encoding(UTF-8)';
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" | sort`;
do {
print int(@COD_cif_files), "\n";
print "@COD_cif_files";
} if 0;
for my $filename (@COD_cif_files) {
chomp $filename;
my $options = { 'parser' => $use_parser, 'no_print' => 1 };
my ( $data, $err_count, $messages ) = parse_cif( $filename, $options );
process_parser_messages( $messages, $die_on_error_level );
next if ( $err_count > 0 );
canonicalize_all_names( $data );
if( !@{$data} ) {
report_message( {
'program' => $0,
'filename' => $filename,
'err_level' => 'WARNING',
'message' => 'file seems to be empty'
}, $die_on_error_level->{'WARNING'} );
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 $basename = basename( $filename );
for my $id (keys %structures) {
my $formula = $structures{$id}{chemical_formula_sum};
$formula = '?' unless defined $formula;
push @{$COD{$formula}}, $structures{$id};
if( exists $structures{$id}{calc_formula} &&
$structures{$id}{calc_formula} ne $formula ) {
my $calc_formula = $structures{$id}{calc_formula};
push @{$COD{$calc_formula}}, $structures{$id};
}
}
}
do {
use COD::Serialise qw( serialiseRef );
serialiseRef( \%COD );
} if 0;
#------------------------------------------------------------------------------
my @cif_files = `find @ARGV -name "*.cif" -o -name "*.CIF" | sort`;
for my $filename (@cif_files) {
chomp $filename;
my $options = { 'parser' => $use_parser, 'no_print' => 1 };
my ( $data, $err_count, $messages ) = parse_cif( $filename, $options );
process_parser_messages( $messages, $die_on_error_level );
next if ( $err_count > 0 );
canonicalize_all_names( $data );
if( !@{$data} ) {
report_message( {
'program' => $0,
'filename' => $filename,
'err_level' => 'WARNING',
'message' => 'file seems to be empty'
}, $die_on_error_level->{'WARNING'} );
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'} );
};
};
for my $id (keys %structures) {
my $formula = $structures{$id}{chemical_formula_sum};
my $calc_formula = $structures{$id}{calc_formula};
if (!defined $formula) {
$formula = defined $calc_formula ? $calc_formula : '?';
}
# In case the declared formula and the calculated formula
# match, the calculated formula should be undefined to avoid
# getting duplicate entry results
if (defined $calc_formula && $calc_formula eq $formula) {
$calc_formula = undef
}
my %structures_found = ();
if( defined $formula && defined $COD{$formula} ) {
for my $COD_entry (@{$COD{$formula}}) {
if( entries_are_the_same( $structures{$id},
$COD_entry,
\%options )) {
my $COD_key = $COD_entry->{filename};
$structures_found{$COD_key} = $COD_entry;
}
}
}
if( defined $calc_formula && defined $COD{$calc_formula} ) {
## print ">>> formula: '$formula', contents: '$calc_formula'\n";
for my $COD_entry (@{$COD{$calc_formula}}) {
if( entries_are_the_same( $structures{$id},
$COD_entry,
\%options )) {
my $COD_key = $COD_entry->{filename};
if( !exists $structures_found{$COD_key} ) {
$structures_found{$COD_key} = $COD_entry;
}
}
}
}
my $final_formula = $formula;
$final_formula =~ s/\s/_/g;
my $n = keys %structures_found;
if( $n > 0 ) {
for my $key (sort keys %structures_found) {
my $COD_entry = $structures_found{$key};
printf
"%-35s %15s %3d %s\n",
$final_formula,
$COD_entry->{filename}, $n, $filename;
}
} else {
printf "%-35s %15s %3d %s\n", $final_formula, '?', 0, $filename;
}
}
}
|