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
|
#!/bin/sh
#!perl -w # --*- Perl -*--
eval 'exec perl -x $0 ${1+"$@"}'
if 0;
#-----------------------------------------------------------------------
#$Author: antanas $
#$Date: 2021-04-28 19:35:53 +0300 (Wed, 28 Apr 2021) $
#$Revision: 8738 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.7.0/scripts/cif_split_primitive $
#------------------------------------------------------------------------
#*
#* Split CIF files into separate files with one data_ section each.
#*
#* This is a very naive and primitive version of the splitter, which
#* expects each data_... section to start on a new line. It may fail on
#* some CIF files that do not follow such convention. For splitting of
#* any correctly formatted CIF files, one must do full CIF parsing
#* using CIF grammar and tokenisation of the file.
#*
#* USAGE:
#* $0 --options input1.cif input*.cif
#**
use strict;
use warnings;
use File::Basename qw( basename );
use COD::SOptions qw( getOptions );
use COD::SUsage qw( usage options );
use COD::ErrorHandler qw( report_message );
use COD::ToolsVersion qw( get_version_string );
my $die_on_error_level = {
ERROR => 1,
WARNING => 0,
NOTE => 0
};
my $verbose = 1;
my $output_dir = '';
#* OPTIONS:
#* -o, --output-dir out/
#* Put all split files into the directory out/
#* (default: './').
#* -v, --verbose
#* Print names of the generated files to STDERR.
#* -q, --quiet
#* Do not print file names to STDERR.
#* --help, --usage
#* Output a short usage message (this message) and exit.
#* --version
#* Output version information and exit.
#**
@ARGV = getOptions(
'-o,--output-dir' => \$output_dir,
'-v,--verbose' => sub { $verbose = 1 },
'-q,--quiet' => sub { $verbose = 0 },
'--options' => sub { options; exit },
'--help,--usage' => sub { usage; exit },
'--version' => sub { print get_version_string(), "\n"; exit }
);
$output_dir =~ s./+$..;
my $output_file;
my $output_handle;
my @initial_comments = ();
my @data_global = ();
my %files = ();
my %has_end = ();
my $global = 0;
my $skip = 1;
my $initial_comments = 1;
my $data_id;
while(<>) {
if( /^\s*\#|^\s*$/ && $initial_comments ) {
push @initial_comments, $_;
next;
}
$initial_comments = 0;
if( /^\s*data_global\s*$/ || /^\s*data_global\s+/ ) {
if( int(@data_global) == 0 ) {
push @data_global, $_;
$global = 1;
$skip = 0;
} else {
report_message( {
'program' => $0,
'err_level' => 'WARNING',
'message' => 'second data_global encountered -- skipping'
}, $die_on_error_level->{'WARNING'} );
$global = 1;
$skip = 1;
}
next;
}
if( /^\s*data_([^\s]+)/ ) {
$data_id = $1;
$data_id =~ s/^data_//;
$global = 0;
$skip = 0;
close $output_handle if $output_handle;
my $suffix = $data_id;
$suffix =~ s/[^-+._a-zA-Z0-9]/_/g;
my $basename = basename( $ARGV, '.cif' );
if( $basename ne '-' ) {
$output_file = basename( $ARGV, '.cif' ) . "_${suffix}" . '.cif';
} else {
$output_file = $suffix . '.cif';
}
if( $output_dir ne '' ) {
$output_file = $output_dir . '/' . $output_file;
}
if( !exists $files{$output_file} ) {
open $output_handle, '>', $output_file or
die "$0: $output_file: ERROR, could not open file for "
. 'writing -- ' . lcfirst($!) . "\n";
$files{$output_file} = $output_file;
print "$output_file\n" if $verbose;
for ( @initial_comments, @data_global ) {
print $output_handle $_;
}
} else {
open $output_handle, '>>', $output_file or
die "$0: $output_file: ERROR, could not open file for"
. 'appending -- ' . lcfirst($!) . "\n";
print "$output_file (appending)\n" if $verbose;
}
}
if( /^\s*\#=+END/ ) {
$has_end{$output_file} = 1;
}
if( $global == 1 && $skip == 0 ) {
push @data_global, $_;
next;
}
next if $skip;
print $output_handle $_;
if( eof ARGV ) {
$output_file = undef;
close $output_file if $output_file;
@data_global = ();
@initial_comments = ();
$global = 0;
$skip = 1;
$initial_comments = 1;
}
}
close $output_file if $output_file;
|