File: cif_split

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 (313 lines) | stat: -rwxr-xr-x 10,450 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
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
#!/bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#-----------------------------------------------------------------------
#$Author: antanas $
#$Date: 2022-07-31 23:15:34 +0300 (Sun, 31 Jul 2022) $
#$Revision: 9355 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.7.0/scripts/cif_split $
#------------------------------------------------------------------------
#*
#* Split CIF files into separate files with one data_ section each.
#*
#* This script parses given CIF files to separate the data blocks, so is
#* capable of splitting non-correctly formatted and nested CIF files.
#*
#* USAGE:
#*    $0 --options input1.cif input*.cif
#**

use strict;
use warnings;

use Encode qw( decode );
use File::Basename qw( basename );

use COD::CIF::JSON qw( cif2json );
use COD::CIF::Parser qw( parse_cif );
use COD::CIF::Tags::CanonicalNames qw( canonicalize_all_names );
use COD::CIF::Tags::Print qw( print_cif );
use COD::SOptions qw( getOptions get_value );
use COD::SUsage qw( usage options );
use COD::ErrorHandler qw( process_warnings
                          process_errors
                          process_parser_messages );
use COD::ToolsVersion qw( get_version_string );

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

my $verbose = 1;
my $output_dir = '';
my $output_prefixed = 0;
my $output_tar = 0;
my $split_global = 1;
my $cif_header_file;

my $use_parser = 'c';
my $input_format  = 'cif';
my $output_format = 'cif';

#* OPTIONS:
#*   -o, --output-dir out/
#*                     Put all split files into the directory out/.
#*
#*   -p, --prefixed, --output-prefixed
#*                     Print split data blocks to the STDOUT, each line
#*                     prefixed by the data block name. Do not create any files.
#*
#*   --output-tar
#*                     Produce TAR archive with split files to the STDOUT.
#*
#*   -h, --add-cif-header header_file.cif
#*                     Prepend each of the output files with the comments
#*                     from the beginning of the specified file.
#*
#*   --split-global-data-block
#*                     Put the 'data_global' data block (if any) to each
#*                     split file (default).
#*   --do-not-split-global-data-block,
#*   --dont-split-global-data-block,
#*   --no-split-global-data-block
#*                     Do not treat 'data_global' data block (if any) as
#*                     special.
#*
#*   -v, --verbose
#*                     Print names of the generated files to STDERR.
#*   -q, --quiet
#*                     Do not print file names to STDERR.
#*
#*   --use-perl-parser
#*                     Use Perl parser for CIF parsing.
#*   --use-c-parser
#*                     Use Perl & C parser for CIF parsing.
#*
#*   --cif-input
#*                     Use CIF format for input (default).
#*   --json-input
#*                     Use JSON format for input.
#*   --cif-output
#*                     Use CIF format for output (default).
#*   --json-output
#*                     Use JSON format for output.
#*   --cif
#*                     Use CIF format for both input and output (default).
#*   --json
#*                     Use JSON format for both input and output.
#*
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    '-o,--output-dir' => sub { $output_dir = get_value(),
                               $output_prefixed = 0;
                               $output_tar = 0 },
    '-p,--prefixed,--output-prefixed' => sub { $output_prefixed = 1;
                                               $output_tar = 0 },
    '--output-tar'    => sub { $output_tar = 1;
                               $output_prefixed = 0 },

    '-h,--add-cif-header' => \$cif_header_file,
    '-v,--verbose'    => sub { $verbose = 1 },
    '-q,--quiet'      => sub { $verbose = 0 },

    '--split-global-data-block'        => sub { $split_global = 1 },
    '--do-not-split-global-data-block' => sub { $split_global = 0 },
    '--dont-split-global-data-block'   => sub { $split_global = 0 },
    '--no-split-global-data-block'     => sub { $split_global = 0 },

    '--use-perl-parser' => sub { $use_parser = 'perl' },
    '--use-c-parser'    => sub { $use_parser = 'c' },

    '--cif-input'   => sub { $input_format = 'cif' },
    '--json-input'  => sub { $input_format = 'json' },

    '--cif-output'  => sub { $output_format = 'cif' },
    '--json-output' => sub { $output_format = 'json' },

    '--cif'  => sub { $input_format = $output_format = 'cif' },
    '--json' => sub { $input_format = $output_format = 'json' },

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

if( $input_format eq 'json' ) {
    $use_parser = 'json';
}

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

# Reading the header file
my $cif_header;
eval {
    if( defined $cif_header_file ) {
        open( my $header, '<', "$cif_header_file" ) or die 'ERROR, '
          . 'could not open CIF header file for reading -- '
          . lcfirst($!) . "\n";

        $cif_header = '';
        while( <$header> ) {
            last unless /^#/;
            $cif_header .= $_;
        }

        close( $header ) or die 'ERROR, '
         . 'error while closing CIF header file after reading -- '
         . lcfirst($!) . "\n";

        # The header must not contain CIF 2.0 magic code. For CIF 2.0
        # files the magic code is printed explicitly before the header.
        $cif_header =~ s/^#\\#CIF_2\.0[ \t]*\n//;
    }
};
if ($@) {
    process_errors( {
      'message'  => $@,
      'program'  => $0,
      'filename' => $cif_header_file
    }, $die_on_error_level->{ERROR} );
};

$output_dir =~ s./+$..;

my %files = ();
my $tar;
if( $output_tar ) {
    require Archive::Tar;
    $tar = Archive::Tar->new;
}

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

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 );
    next if ( $err_count > 0 );

    canonicalize_all_names( $data );

    my $data_global;
    for my $dataset (@$data) {

        local $SIG{__WARN__} = sub {
            process_warnings( {
                'message'  => @_,
                'program'  => $0,
                'filename' => $filename,
                'add_pos'  => 'data_' . $dataset->{'name'}
            }, $die_on_error_level )
        };

        if( $split_global && $dataset->{name} eq 'global' ) {
            if( !$data_global ) {
                $data_global = $dataset;
            } else {
                warn "WARNING, second data_global encountered -- skipping\n";
            }
            next;
        }

        my $print_cif_stdout;
        if( $output_format eq 'cif' ) {
            local *STDOUT;
            open( STDOUT, '>', \$print_cif_stdout );
            binmode STDOUT, ':encoding(UTF-8)';
            if( $data_global ) {
                print_cif( $data_global,
                           {
                                preserve_loop_order => 1,
                                keep_tag_order => 1
                           }
                         );
            }
            print_cif( $dataset,
                       {
                            preserve_loop_order => 1,
                            keep_tag_order => 1
                       }
                     );
            close STDOUT;
            $print_cif_stdout = decode( 'utf8', $print_cif_stdout );
        } else {
            my @to_json = ( $dataset );
            if( $data_global ) {
                unshift( @to_json, $data_global );
            }
            $print_cif_stdout = cif2json( \@to_json );
        }

        my $cif_contents = $print_cif_stdout;
        if( $output_format eq 'cif' && $cif_header ) {
            if( $cif_contents =~ s/^(#\\#CIF_2.0[ \t]*\n)// ) {
                $cif_contents = $1 . $cif_header . $cif_contents;
            } else {
                $cif_contents = $cif_header . $cif_contents;
            }
        }

        if( $output_prefixed ) {
            foreach ( split m/\n/, $cif_contents ) {
                print $dataset->{name}, "\t", $_, "\n";
            }
        } else {
            my $suffix = $dataset->{name};
            $suffix =~ s/[^-+._a-zA-Z0-9]/_/g;

            my $basename = basename( $filename, '.cif' );
            my $output_file;
            if( $basename ne '-' ) {
                $output_file = "${basename}_${suffix}.${output_format}";
            } else {
                $output_file = "${suffix}.${output_format}";
            }
            if( $output_tar ) {
                $tar->add_data( $output_file, $cif_contents );
            } else {
                eval {
                    if( $output_dir ne '' ) {
                        $output_file = $output_dir . '/' . $output_file;
                    }
                    my $output_handle;
                    if( !exists $files{$output_file} ) {
                        $files{$output_file} = $output_file;

                        print "$output_file\n" if $verbose;
                        open $output_handle, '>', $output_file or
                            die "ERROR, could not open file for writing -- "
                               . lcfirst($!) . "\n";
                    } else {
                        print "$output_file (appending)\n" if $verbose;
                        open $output_handle, '>>', $output_file or
                            die "ERROR, could not open file for appending -- "
                              . lcfirst($!) . "\n";
                    }
                    binmode $output_handle, ':encoding(UTF-8)';
                    print $output_handle $cif_contents;
                    close $output_handle;
                };
                if ($@) {
                    process_errors( {
                    'message'  => $@,
                    'program'  => $0,
                    'filename' => $output_file
                    }, $die_on_error_level->{ERROR} );
                };
            }
        }
    }
}

if( $output_tar ) {
    print $tar->write();
}