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
|
#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
if 0;
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2022-07-31 09:52:44 +0300 (Sun, 31 Jul 2022) $
#$Revision: 9353 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.7.0/scripts/cif_values $
#------------------------------------------------------------------------------
#*
#* Read CIFs and print out requested tag values in a tabular form.
#*
#* USAGE:
#* $0 --options input1.cif input*.cif
#**
use strict;
use warnings;
use File::Basename qw( basename );
use COD::CIF::Parser qw( parse_cif );
use COD::SOptions qw( getOptions get_value );
use COD::SUsage qw( usage options );
use COD::ErrorHandler qw( process_parser_messages
report_message );
use COD::ToolsVersion qw( get_version_string );
my $die_on_error_level = {
ERROR => 1,
WARNING => 0,
NOTE => 0
};
my $use_parser = "c";
my $header = 1;
my $separator = "\t";
my $value_separator = ",";
my @selected_tags;
my @uniquified_tags;
my $write_data_name = 1;
my $write_filename = 1;
my $write_basename = 2;
my $write_file;
my $replace_spaces = 1; # indicates whether spaces should be replaced
# to underscores
my $fix_errors = 0;
my $treat_dots_as_underscores = 1;
#* OPTIONS:
#* -t, --tags _tag1,_tag2,_tag3
#* --tags "_tag1 _tag2 _tag3"
#* Print out values of tags _tag1, _tag2, and _tag3;
#* the specified tag list becomes a new list of printed tags.
#*
#* -a, --add-tag _tag4
#* Add tag _tag4 to the list of printed tags.
#*
#* -c, --clear-tags
#* Clear the list of printed tags accumulated so far.
#*
#* -h, --header
#* Print out header with the names of selected tags at the
#* beginning (default).
#*
#* -h-, --no-header
#* Do not print out any header.
#*
#* -r, --replace-spaces
#* Replace spaces into underscores (default).
#*
#* -r-, --dont-replace-spaces, --no-replace-spaces
#* Do not replace spaces into underscores (works
#* satisfactory only when tabs are used as separators).
#*
#* -s, --separator=", "
#* Use the specified string as a separator between tag
#* values on a line.
#*
#* -u, --uniquify-tags _tag1,_tag2
#* Print only unique values of specified tags, not the whole
#* list of values.
#*
#* --add-uniquified-tag _tag1
#* Add tag '_tag' to the uniquified tag list.
#*
#* -u-, --dont-uniquify-tags, --no-uniquify-tags
#* Print all values of all tags, do not uniquify any tags
#* (default).
#*
#* --tab-separator
#* Use TAB character as a separator between tag values on
#* a line (default).
#*
#* --vseparator, --value-separator ','
#* Use the specified string to separate multiple values
#* of a given tag (default: ',').
#*
#* --treat-dots-as-underscores
#* Convert all dots in tag names into underscores (default).
#*
#* --dont-treat-dots-as-underscores, --no-treat-dots-as-underscores
#* Leave original tags as they are.
#*
#* --filename
#* Print out full filename at the beginning of each line.
#*
#* --fix-syntax-errors
#* Try to fix those syntax errors in input CIFs that can be
#* corrected unambiguously.
#*
#* --no-fix-syntax-errors,
#* --dont-fix-syntax-errors
#* Do not try to fix syntax errors in input CIFs (default).
#*
#* --basename
#* Print out basename of each file at the beginning of each
#* line.
#*
#* --no-filename
#* Do not print file names (default).
#*
#* --dataname
#* Print out data block name at the beginning of each line
#* (default).
#*
#* --no-dataname
#* Do not print data block names.
#*
#* --use-perl-parser
#* --use-c-parser
#* Specify parser to parse CIF files (default: C parser).
#*
#* --help, --usage
#* Output a short usage message (this message) and exit.
#* --version
#* Output version information and exit.
#**
@ARGV = getOptions(
"-t,--tags" => sub { @selected_tags = split( /\s|,/, get_value()) },
"-a,--add-tag" => \@selected_tags,
"-c,--clear-tags" => sub { @selected_tags = () },
"-h,--header" => sub { $header = 1 },
"-h-,--no-header" => sub { $header = 0 },
"-r,--replace-spaces" => sub { $replace_spaces = 1 },
"-r-,--no-replace-spaces" => sub { $replace_spaces = 0 },
"--dont-replace-spaces" => sub { $replace_spaces = 0 },
"-s,--separator" => \$separator,
"--tab-separator" => sub { $separator = "\t" },
"-u,--uniquify-tags" => sub { @uniquified_tags =
split( /\s|,/, get_value())},
"--add-uniquified-tag" => \@uniquified_tags,
"-u-,--no-uniquify-tags" => sub { @uniquified_tags = () },
"--dont-uniquify-tags" => sub { @uniquified_tags = () },
"--treat-dots-as-underscores"
=> sub { $treat_dots_as_underscores = 1 },
"--dont-treat-dots-as-underscores"
=> sub { $treat_dots_as_underscores = 0 },
"--no-treat-dots-as-underscores"
=> sub { $treat_dots_as_underscores = 0 },
"--vseparator" => \$value_separator,
"--value-separator" => \$value_separator,
"--filename" => sub { $write_file = $write_filename },
"--basename" => sub { $write_file = $write_basename },
"--no-filename" => sub { $write_file = 0 },
"--fix-syntax-errors" => sub { $fix_errors = 1; },
"--dont-fix-syntax-errors" => sub { $fix_errors = 0; },
"--no-fix-syntax-errors" => sub { $fix_errors = 0; },
"--dataname" => sub { $write_data_name = 1 },
"--no-dataname" => sub { $write_data_name = 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 get_version_string(), "\n"; exit }
);
sub sunique(@);
if( $treat_dots_as_underscores ) {
@selected_tags = map { s/\./_/g; $_ } @selected_tags;
@uniquified_tags = map { s/\./_/g; $_ } @uniquified_tags;
}
@selected_tags = map { lc($_) } @selected_tags;
@uniquified_tags = map { lc($_) } @uniquified_tags;
my %uniquified_tags = map { ($_,1) } @uniquified_tags;
if( $header ) {
print "# ", join( $separator, @selected_tags ), "\n";
}
@ARGV = ( "-" ) unless @ARGV;
binmode STDOUT, ':encoding(UTF-8)';
binmode STDERR, ':encoding(UTF-8)';
for my $filename (@ARGV) {
my $options = { fix_errors => $fix_errors,
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 );
if( !defined $data->[0] || !defined $data->[0]{name} ) {
report_message( {
'program' => $0,
'filename' => $filename,
'err_level' => 'WARNING',
'message' => 'file seems to be empty'
}, $die_on_error_level->{WARNING} );
next;
}
for my $dataset (@$data) {
my @values = ();
my $values = $dataset->{values};
if( $treat_dots_as_underscores ) {
for my $tag (keys %{$values}) {
my $new_tag = $tag;
$new_tag =~ s/\./_/g;
if( $new_tag ne $tag ) {
$values->{$new_tag} = $values->{$tag};
}
}
}
if( $write_file ) {
if( $write_file == $write_filename ) {
push( @values, $filename );
} else {
push( @values, basename( $filename ));
}
}
if( $write_data_name ) {
push( @values, $dataset->{name} ? $dataset->{name} : "?" );
}
for my $tag (@selected_tags) {
if( exists $values->{$tag} ) {
my @tag_vals;
if( $replace_spaces ) {
@tag_vals = map { s/\n$//; s/[\n\s]/_/g; $_ }
@{$values->{$tag}};
} else {
@tag_vals = map { s/\n$//; s/[\n\s]/ /g; $_ }
@{$values->{$tag}};
}
if( $uniquified_tags{$tag} ) {
push( @values, join( $value_separator,
sunique( @tag_vals )));
} else {
push( @values, join( $value_separator, @tag_vals ));
}
} else {
push( @values, "?" );
}
}
print join( $separator, @values ), "\n";
}
}
sub sunique(@)
{
my @sorted = sort { $a cmp $b } @_;
my @unique = ();
for my $value (@sorted) {
if( int(@unique) == 0 || $unique[-1] ne $value ) {
push( @unique, $value );
}
}
return @unique;
}
|