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
|
#! /bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
if 0;
#------------------------------------------------------------------------------
#$Author: andrius $
#$Date: 2018-09-04 10:49:47 +0300 (An, 04 rugs. 2018) $
#$Revision: 6413 $
#$URL: svn://www.crystallography.net/cod-tools/tags/v2.3/scripts/cif_select $
#------------------------------------------------------------------------------
#*
#* Read CIFs and print out selected tags with their values.
#*
#* USAGE:
#* $0 --options input1.cif input*.cif
#**
use strict;
use warnings;
use COD::CIF::Parser qw( parse_cif );
use COD::CIF::Tags::CanonicalNames qw( canonical_tag_name canonicalize_names );
use COD::CIF::Tags::Manage qw( exclude_tag rename_tag );
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_errors
process_parser_messages
process_warnings
report_message );
use COD::ToolsVersion;
my $use_parser = 'c';
my $die_on_error_level = {
ERROR => 1,
WARNING => 0,
NOTE => 0
};
my @selected_tags;
my $treat_dots_as_underscores = 0;
my $canonicalize_tag_names = 0;
my $output_tabular = 0;
my $invert = 0;
binmode STDOUT, ':encoding(UTF-8)';
binmode STDERR, ':encoding(UTF-8)';
#* 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.
#*
#* -f, --file-with-tags tag.lst
#* Read tags to be extracted from the file 'tag.lst'.
#* Comments (lines starting with '#') in 'tag.lst' are
#* ignored.
#*
#* -c, --clear-tags
#* Clear the list of printed tags accumulated so far.
#*
#* --invert
#* Invert the sense of selection. Exclude values of
#* the tags from the supplied list.
#*
#* --treat-dots-as-underscores
#* Convert all dots in tag names into underscores.
#*
#* --dont-treat-dots-as-underscores, --no-treat-dots-as-underscores
#* Leave original tags as they are (default).
#*
#* --tabular, --output-tabular
#* Output selected tags and their values in tabular format:
#* <data block name> <tag name> <value> <value> ... <value>
#* --cif, --output-cif
#* Output selected tags and their values in CIF format (default).
#*
#* --canonicalize-tag-names
#* Output tags in the canonical form.
#* --dont-canonicalize-tag-names, --no-canonicalize-tag-names
#* Output tags as they are given in original file(s) (default).
#*
#* --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(
"-t,--tags" => sub { @selected_tags = split( /\s|,/, get_value()) },
"-a,--add-tag" => \@selected_tags,
"-c,--clear-tags" => sub { @selected_tags = () },
"-f,--file-with-tags" => sub {
my $filename = get_value();
eval {
open( my $tags, '<', $filename ) or die 'ERROR, '
. 'could not open file for reading -- '
. lcfirst($!) . "\n";
@selected_tags = ( @selected_tags,
map { s/^\s*|\s*$//g; $_ }
grep { !/^\#/ } <$tags> );
close( $tags ) or die 'ERROR, '
.'could not close file after reading -- '
. lcfirst($!) . "\n";
};
if ($@) {
process_errors( {
'message' => $@,
'program' => $0,
'filename' => $filename
}, $die_on_error_level->{ERROR} );
};
},
"--invert" => sub { $invert = 1 },
"--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 },
"--tabular,--output-tabular" => sub { $output_tabular = 1 },
"--cif,--output-cif" => sub { $output_tabular = 0 },
"--canonicalize-tag-names"
=> sub { $canonicalize_tag_names = 1 },
"--dont-canonicalize-tag-names"
=> sub { $canonicalize_tag_names = 0 },
"--no-canonicalize-tag-names"
=> sub { $canonicalize_tag_names = 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 }
);
sub unique(@);
if( $treat_dots_as_underscores ) {
@selected_tags = map { s/\./_/g; $_ } @selected_tags;
}
@selected_tags = unique map { lc($_) } @selected_tags;
my %selected_tags = map { ($_, $_) } @selected_tags;
@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 );
if( !@{$data} || !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};
my $dataname = 'data_' . $dataset->{name};
local $SIG{__WARN__} = sub {
process_warnings( {
'message' => @_,
'program' => $0,
'add_pos' => $dataname,
'filename' => $filename
}, $die_on_error_level )
};
if( $treat_dots_as_underscores ) {
for my $tag (sort keys %{$values}) {
my $new_tag = $tag;
$new_tag =~ s/\./_/g;
next if $new_tag eq $tag;
if( exists $values->{$new_tag} ) {
warn "WARNING, will not rename data item '$tag' " .
"to '$new_tag': data item '$new_tag' already " .
"exists\n";
next;
}
rename_tag( $dataset, $tag, $new_tag );
}
}
if( $invert ) {
for my $tag (@selected_tags) {
exclude_tag( $dataset, $tag );
}
}
my @printed_tags;
if( !$invert ) {
@printed_tags = @selected_tags;
} else {
@printed_tags = @{ $dataset->{tags} };
}
my %printed_tags = map { ($_, $_) } @printed_tags;
if( $output_tabular ) {
local $, = "\t";
local $\ = "\n";
for my $tag (@printed_tags) {
print $dataset->{name},
( $canonicalize_tag_names
? canonical_tag_name( $tag )
: $tag ),
( exists $dataset->{values}{$tag}
? join( $,, map{ s/[\n\t]/ /g; $_ }
@{$dataset->{values}{$tag}} )
: '?' );
}
} else {
if( $canonicalize_tag_names ) {
canonicalize_names( $dataset );
@printed_tags = map{ canonical_tag_name( $_ ) }
@printed_tags;
%printed_tags = map{ ($_, $_) }
@printed_tags;
}
print_cif( $dataset,
{
dictionary_tags => \%printed_tags,
dictionary_tag_list => \@printed_tags,
exclude_misspelled_tags => 1,
} );
}
}
}
sub unique(@)
{
my %count;
my @unique;
for (@_) {
next if $count{$_};
$count{$_}++;
push @unique, $_;
}
return @unique;
}
|