File: cif_printout

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 (157 lines) | stat: -rwxr-xr-x 5,383 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
#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
    if 0;
#------------------------------------------------------------------------------
#$Author: andrius $
#$Revision: 5376 $
#$URL: svn://www.crystallography.net/cod-tools/tags/v2.3/scripts/cif_printout $
#$Date: 2017-05-30 14:51:03 +0300 (An, 30 geg. 2017) $
#$Id: cif_printout 5376 2017-05-30 11:51:03Z andrius $
#------------------------------------------------------------------------------
#*
#* Parse CIF file and print out the structure generated by CIF parser.
#*
#* USAGE:
#*    $0 --options input1.cif input*.cif
#**

use strict;
use warnings;
use COD::CIF::JSON qw( cif2json json2cif );
use COD::CIF::Parser qw( parse_cif );
use COD::SOptions qw( getOptions );
use COD::SUsage qw( usage options );
use COD::ShowStruct qw( showRef );
use COD::ErrorHandler qw( process_errors
                          process_parser_messages
                          report_message );
use COD::ToolsVersion;
use Data::Dumper;

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

my $output_format = 'dump';
my $input_json  = 0;
my %options = ( no_print => 1 );

#*  OPTIONS:
#*   --input-cif,
#*   --input-json
#*                     Specify the format of input file(s). Default CIF.
#*
#*   --output-dump
#*                     Output parsed CIF file in internal dump format (default).
#*
#*   --output-json
#*                     Output generated structure in JSON.
#*
#*   --output-struct
#*                     Output generated structure using COD::ShowStruct module.
#*
#*   --json
#*                     Set both input and output formats to JSON.
#*
#*   --use-perl-parser
#*                     Use Perl parser for CIF parsing.
#*
#*   --use-c-parser
#*                     Use Perl & C parser for CIF parsing.
#*
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    "--do-not-unprefix-text" => sub{ $options{do_not_unprefix_text} = 1 },
    "--do-not-unfold-text"   => sub{ $options{do_not_unfold_text} = 1 },
    "--fix-errors"           => sub{ $options{fix_errors} = 1 },
    "--fix-duplicate-tags-with-same-values"  =>
        sub{ $options{fix_duplicate_tags_with_same_values} = 1 },
    "--fix-duplicate-tags-with-empty-values" =>
        sub{ $options{fix_duplicate_tags_with_empty_values} = 1 },
    "--fix-data-header"     => sub{ $options{fix_data_header} = 1 },
    "--fix-datablock-names" => sub{ $options{fix_datablock_names} = 1 },
    "--fix-string-quotes"   => sub{ $options{fix_string_quotes} = 1 },
    "--fix-missing-closing-double-quote" =>
        sub{ $options{fix_missing_closing_double_quote} = 1 },
    "--fix-missing-closing-single-quote" =>
        sub{ $options{fix_missing_closing_single_quote} = 1 },
    "--fix-ctrl-z"  => sub{ $options{fix_ctrl_z} = 1 },
    "--allow-uqstring-brackets" => sub{ $options{allow_uqstring_brackets} = 1 },

    # Bison parser is default:
    "--input-cif"  => sub { $options{parser} = 'c' },
    "--input-json" => sub { $options{parser} = 'json' },

    "--output-dump"   => sub { $output_format = 'dump' },
    "--output-json"   => sub { $output_format = 'json' },
    "--output-struct" => sub { $output_format = 'struct' },

    "--json" => sub { $options{parser} = 'json'; $output_format = 'json' },

    "--use-perl-parser" => sub { $options{parser} = 'perl' },
    "--use-c-parser"    => sub { $options{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( @ARGV ) {

    my( $data, $err_count, $messages );
    if( $input_json ) {
        eval {
            open my $inp, '<' . $_ or die 'ERROR, '
              . 'could not open bibliography file for reading -- '
              . lcfirst($!) . "\n";
            $data = json2cif( join( "\n", <$inp> ) );
            close $inp or die 'ERROR, '
              . 'error while closing file after reading -- '
              . lcfirst($!) . "\n";
            $err_count = 0;
        };
        if ($@) {
            process_errors( {
              'program'  => $0,
              'filename' => $_,
              'message'  => $@
            }, 1 );
        }
    } else {
        $options{no_print} = 1;
        ($data, $err_count, $messages) = parse_cif( $_, \%options );
        process_parser_messages( $messages, $die_on_error_level );
    }
    
    foreach my $datablock ( @$data ) {
        if( $output_format eq 'json' ) {
            print cif2json( $datablock );
        } elsif( $output_format eq 'struct' ) {
            showRef( $datablock );
        } else {
            local $Data::Dumper::Varname = 'datablock';
            local $Data::Dumper::Sortkeys = 1;
            print Dumper( $datablock );
        }
    }

    report_message( {
        'program'   => $0,
        'filename'  => $_,
        'err_level' => 'NOTE',
        'message'   =>  "$err_count error(s) encountered while processing the file"
    }, $die_on_error_level->{'NOTE'} );
}