File: cif_fillcell

package info (click to toggle)
cod-tools 2.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 114,852 kB
  • sloc: perl: 53,336; sh: 23,842; ansic: 6,318; xml: 1,982; yacc: 1,112; makefile: 716; python: 158; sql: 73
file content (253 lines) | stat: -rwxr-xr-x 8,849 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
#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2017-11-23 13:50:21 +0200 (Kt, 23 lapkr. 2017) $ 
#$Revision: 5884 $
#$URL: svn://www.crystallography.net/cod-tools/tags/v2.3/scripts/cif_fillcell $
#------------------------------------------------------------------------------
#*
#* Generates symmetric atoms from a CIF file.
#*
#* USAGE:
#*    $0 --options input.cif inputs*.cif
#**

use strict;
use warnings;
use File::Basename qw( fileparse );
use COD::AtomProperties;
use COD::CIF::Parser qw( parse_cif );
use COD::CIF::Data qw( get_symmetry_operators );
use COD::CIF::Data::AtomList qw( atom_array_from_cif
                                 datablock_from_atom_array );
use COD::CIF::Data::SymmetryGenerator qw( apply_shifts
                                          shift_atom
                                          symop_generate_atoms
                                          symop_apply );
use COD::CIF::Tags::CanonicalNames qw( canonicalize_names );
use COD::CIF::Tags::Manage qw( new_datablock set_tag set_loop_tag );
use COD::CIF::Tags::Merge qw( merge_datablocks );
use COD::CIF::Tags::Print qw( print_cif );
use COD::Spacegroups::Symop::Parse qw( modulo_1
                                       symop_from_string
                                       symop_string_canonical_form );
use COD::Spacegroups::Lookup::COD;
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;

my $Id = '$Id: cif_fillcell 5884 2017-11-23 11:50:21Z antanas $';
my $use_parser = 'c';
my $die_on_errors    = 1;
my $die_on_warnings  = 0;
my $die_on_notes     = 0;
my $die_on_error_level = {
    ERROR   => $die_on_errors,
    WARNING => $die_on_warnings,
    NOTE    => $die_on_notes
};

my $build_supercell = 0;
my $merge_special_positions = 0;

#* OPTIONS:
#*   --merge-special-positions
#*                     Merge atoms that are located in special positions.
#*   --supercell
#*                     Build the 3x3x3 supercell by shifting atoms of the
#*                     unit cell in all 3D directions.
#*   --unit-cell, --no-supercell
#*                     Only build the unit cell (default).
#*   --use-c-parser
#*                     Use the faster C parser for CIFs (default).
#*   --use-perl-parser
#*                     Use the Perl parser for parsing CIFs.
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    '--merge-special-positions' => sub { $merge_special_positions = 1 },

    '--supercell'    => sub { $build_supercell = 1 },
    '--no-supercell' => sub { $build_supercell = 0 },
    '--unit-cell'    => sub { $build_supercell = 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 'cod-tools version ',
                                 $COD::ToolsVersion::Version, "\n";
                                 exit }
);

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

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

foreach my $filename (@ARGV) {
    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 );

    foreach my $dataset (@{$data}) {
        canonicalize_names( $dataset );

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

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

        my $atoms;
        eval {
            # Create a list of symmetry operators:
            my $sym_data = get_symmetry_operators( $dataset );
            my @sym_operators = map { symop_from_string($_) }
                                    @{$sym_data};
            my $symop_list = { symops => \@sym_operators,
                               symop_ids => {} };
            for (my $i = 0; $i < @{$sym_data}; $i++) {
                $symop_list->{symop_ids}
                             {symop_string_canonical_form($sym_data->[$i])} = $i;
            }

            my $cif_atom_list_options = {
                allow_unknown_chemical_types => 1,
                atom_properties => \%COD::AtomProperties::atoms,
                exclude_dummy_atoms => 1,
                exclude_dummy_coordinates => 1,
                exclude_unknown_coordinates => 1,
                modulo_1 => 1,
                symop_list => $symop_list,
                uniquify_atom_names => 1,
            };

            # Build an atom array from the CIF data structure:
            my $initial_atoms =
                atom_array_from_cif( $dataset, $cif_atom_list_options );

            if( $merge_special_positions ) {
                $atoms =
                    symop_generate_atoms( \@sym_operators,
                                          $initial_atoms,
                                          undef,
                                          { append_atoms_mapping_to_self => 0 } );
            } else {
                $atoms = fillcell( $initial_atoms, $symop_list );
            }

            if( $build_supercell ) {
                $atoms = apply_shifts( $atoms );
            }

            my $atom_list_datablock = datablock_from_atom_array( $atoms );
            my $new_datablock = p1_datablock( $dataset );
            merge_datablocks( $atom_list_datablock, $new_datablock );

            my @squeeze_tags = grep { /^_platon_squeeze_void_/ }
                                    @{$dataset->{tags}};
            my %squeeze_loops =
                map { $dataset->{inloop}{$_} => 1 } @squeeze_tags;
            # All tags should belong in the same loop
            if( scalar keys %squeeze_loops == 1 ) {
                my $first_tag = $squeeze_tags[0];
                foreach (@squeeze_tags) {
                    set_loop_tag( $new_datablock,
                                  $_,
                                  $first_tag,
                                  $dataset->{values}{$_} );
                }
            }

            print_cif( $new_datablock,
                       {
                            preserve_loop_order => 1,
                            keep_tag_order => 1
                       }
                     );
        };
        if ( $@ ) {
            process_errors( {
              'message'       => $@,
              'program'       => $0,
              'filename'      => $filename,
              'add_pos'       => $dataname
            }, $die_on_errors )
        }
    }
}

#===============================================================#
sub fillcell
{
    my( $atoms, $symop_list ) = @_;

    my @new_atoms;
    for my $symop ( @{$symop_list->{'symops'}} ) {
        for my $atom ( @{$atoms} ) {
            push @new_atoms, symop_apply( $atom, $symop, { 'modulo_1' => 1 } );
        }
    }

    return \@new_atoms;
}

#==============================================================#
sub p1_datablock
{
    my( $dataset ) = @_;

    my $new_datablock = new_datablock( $dataset->{name} . '_' .
                                       fileparse($0) );

    set_tag( $new_datablock, '_space_group_IT_number',     1 );
    set_tag( $new_datablock, '_space_group_name_Hall',    'P 1' );
    set_tag( $new_datablock, '_space_group_name_H-M_alt', 'P 1' );

    my $values = $dataset->{values};

    # set unit cell lengths and angles
    foreach ( qw( _cell_angle_alpha
                  _cell_angle_beta
                  _cell_angle_gamma ) )
    {
        if( defined $values->{$_}[0] ) {
            set_tag( $new_datablock, $_, $values->{$_}[0] );
        }
    }

    foreach ( qw( _cell_length_a
                  _cell_length_b
                  _cell_length_c ) )
    {
        set_tag( $new_datablock, $_, $values->{$_}[0] );
    }

    # set symmetry operation information
    set_loop_tag( $new_datablock,
                  '_space_group_symop_id',
                  '_space_group_symop_id',
                  [ 1 ] );
    set_loop_tag( $new_datablock,
                  '_space_group_symop_operation_xyz',
                  '_space_group_symop_id',
                  [ 'x, y, z' ] );

    return $new_datablock;
}