File: cif_bounding_box

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 (190 lines) | stat: -rwxr-xr-x 6,728 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
#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: andrius $
#$Date: 2017-05-30 14:51:03 +0300 (An, 30 geg. 2017) $
#$Revision: 5376 $
#$URL: svn://www.crystallography.net/cod-tools/tags/v2.3/scripts/cif_bounding_box $
#------------------------------------------------------------------------------
#*
#* Transform non-crystal CIF file (missing cell parameters and symmetry
#* information, Cartesian coordinates are given instead of fractional)
#* into a crystal description.
#*
#* USAGE:
#*    $0 --options input1.cif input*.cif
#**

use strict;
use warnings;
use File::Basename qw( basename );
use COD::CIF::Data qw( get_symmetry_operators );
use COD::CIF::Parser qw( parse_cif );
use COD::CIF::Tags::CanonicalNames qw( canonicalize_all_names );
use COD::CIF::Tags::Manage qw( exclude_tag set_loop_tag set_tag );
use COD::CIF::Tags::Print qw( print_cif );
use COD::ErrorHandler qw( process_errors process_warnings
                          process_parser_messages report_message );
use COD::SOptions qw( getOptions get_value );
use COD::SUsage qw( usage options );
use COD::ToolsVersion;
use List::Util qw( reduce );

my @margin = ( 5, 5, 5 );
my $use_parser = 'c';

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

#* OPTIONS:
#*   --margin-x, --margin-y, --margin-z
#*                     Set margins in x, y and z directions (default 5 angstroms)
#*
#*   --use-perl-parser
#*                     Use Perl parser for CIF parsing.
#*   --use-c-parser
#*                     Use Perl & C parser for CIF parsing (default).
#*
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**

@ARGV = getOptions(
    '--margin-x' => \$margin[0],
    '--margin-y' => \$margin[1],
    '--margin-z' => \$margin[2],

    '--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)';

for 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 );

    if( !@{$data} || !defined $data->[0] || !defined $data->[0]{name} ) {
        report_message( {
           'program'   => $0,
           'filename'  => $filename,
           'err_level' => 'WARNING',
           'message'   => 'file seems to be empty'
        }, 0 );
        next;
    }

    canonicalize_all_names( $data );

    for my $dataset (@$data) {
        my $values = $dataset->{values};

        my @bounding_box;

        if(  exists $values->{'_atom_site_Cartn_x'} &&
             exists $values->{'_atom_site_Cartn_y'} &&
             exists $values->{'_atom_site_Cartn_z'} &&
            !exists $values->{'_atom_site_fract_x'} &&
            !exists $values->{'_atom_site_fract_y'} &&
            !exists $values->{'_atom_site_fract_z'} ) {

            my @bounding_box = get_bounding_box( $values );

            my @cell = ( $bounding_box[1] - $bounding_box[0] + $margin[0],
                         $bounding_box[3] - $bounding_box[2] + $margin[1],
                         $bounding_box[5] - $bounding_box[4] + $margin[2],
                         90, 90, 90 );

            set_tag( $dataset, '_cell_length_a',    $cell[0] );
            set_tag( $dataset, '_cell_length_b',    $cell[1] );
            set_tag( $dataset, '_cell_length_c',    $cell[2] );
            set_tag( $dataset, '_cell_angle_alpha', $cell[3] );
            set_tag( $dataset, '_cell_angle_beta',  $cell[4] );
            set_tag( $dataset, '_cell_angle_gamma', $cell[5] );

            my @Cartn_x_now = map { $_ - $bounding_box[0] + $margin[0] / 2 }
                                  @{$values->{'_atom_site_Cartn_x'}};
            my @Cartn_y_now = map { $_ - $bounding_box[2] + $margin[1] / 2 }
                                  @{$values->{'_atom_site_Cartn_y'}};
            my @Cartn_z_now = map { $_ - $bounding_box[4] + $margin[2] / 2 }
                                  @{$values->{'_atom_site_Cartn_z'}};

            my @fract_x = map { $_ / $cell[0] } @Cartn_x_now;
            my @fract_y = map { $_ / $cell[1] } @Cartn_y_now;
            my @fract_z = map { $_ / $cell[2] } @Cartn_z_now;

            set_loop_tag( $dataset,
                          '_atom_site_fract_x',
                          '_atom_site_label',
                          \@fract_x );

            set_loop_tag( $dataset,
                          '_atom_site_fract_y',
                          '_atom_site_label',
                          \@fract_y );

            set_loop_tag( $dataset,
                          '_atom_site_fract_z',
                          '_atom_site_label',
                          \@fract_z );

            exclude_tag( $dataset, '_atom_site_Cartn_x' );
            exclude_tag( $dataset, '_atom_site_Cartn_y' );
            exclude_tag( $dataset, '_atom_site_Cartn_z' );
        }

        eval {
            get_symmetry_operators( $dataset );
        };
        if( $@ ) {
            # structure probably does not contain symmetry information
            set_tag( $dataset, '_space_group_name_Hall', 'P 1' );
        }

        print_cif( $dataset,
                   {
                        preserve_loop_order => 1,
                        keep_tag_order => 1
                   } );
    }
}

# Returns x_min, x_max, y_min, y_max, z_min, z_max

sub get_bounding_box
{
    my( $values ) = @_;

    return
        (reduce { $b ne '.' && $b ne '?' && $b < $a ? $b : $a }
             @{$values->{'_atom_site_Cartn_x'}}),
        (reduce { $b ne '.' && $b ne '?' && $b > $a ? $b : $a }
             @{$values->{'_atom_site_Cartn_x'}}),
        (reduce { $b ne '.' && $b ne '?' && $b < $a ? $b : $a }
             @{$values->{'_atom_site_Cartn_y'}}),
        (reduce { $b ne '.' && $b ne '?' && $b > $a ? $b : $a }
             @{$values->{'_atom_site_Cartn_y'}}),
        (reduce { $b ne '.' && $b ne '?' && $b < $a ? $b : $a }
             @{$values->{'_atom_site_Cartn_z'}}),
        (reduce { $b ne '.' && $b ne '?' && $b > $a ? $b : $a }
             @{$values->{'_atom_site_Cartn_z'}});
}