File: cod2rdf

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 (406 lines) | stat: -rwxr-xr-x 14,612 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#! /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/cod2rdf $
#------------------------------------------------------------------------------
#*
#* Describe (in RDF format) properties of COD structures.
#*
#* USAGE:
#*    $0 --options 1000000 "[1-9][0-9]\{6\}"
#**

use strict;
use warnings;
use DBI;
use COD::RDF qw( rdf_n3 rdf_ntriples rdf_xml );
use COD::SOptions qw( getOptions get_value );
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 $refering_db_name = 'cod';
my $output_format = 'xml';

my $from;
my $to;
my $revision;

my %database = (
    host  => "www.crystallography.net",
    user  => "cod_reader",
    name  => "cod",
    port  => 3306,
    password => "",
    platform => "mysql",
    definition_table => "databases",
);

my $rdf_options =
{
    url_prefix      => 'http://www.crystallography.net/cod/',
    url_postfix     => '.html',
    vocabulary_name       => 'cod',
    vocabulary_url_prefix => 'http://www.crystallography.net/cod/doc/rdf/',
    replace_utf_code_points_from => undef,
    utf_code_point_format        => '&#x%04X;', # default is HTML escapes
    split_author_names           => 1,
};

#* OPTIONS:
#*   -d, --database  cod
#*                     Use database "cod" to query for structures.
#*
#*   -h, --host   www.crystallography.net
#*   -s, --server www.crystallography.net
#*                     Query COD database on the host 'www.crystallography.net'.
#*
#*   -l, --localhost
#*                     Use database server on the localhost to query the
#*                     COD database.
#*
#*   -p, --port 3306
#*                     Use the specified port to query structures
#*                     (default: 3306).
#*
#*   --platform mysql
#*                     Use specified SQL platform (default: 'mysql').
#*
#*   -u, --user cod_reader
#*                     Use user name "cod_reader" to access COD database;
#*                     this reader should be granted SELECT privilege, i.e.
#*                     should be able to read the COD database, without
#*                     supplying a password.
#*
#*   --password
#*                     Use the specified password to connect (default: '').
#*
#*   --vocabulary-namespace cod
#*                     Use vocabulary namespace "cod" to prefix properties
#*                     native to the queried database.
#*
#*   --vocabulary-url-prefix http://www.crystallography.net/cod/doc/rdf/
#*                     Specify URL prefix for database's native namespace.
#*
#*   --prefix, --db-url-prefix http://www.crystallography.net/cod/
#*                     Specify URL prefix for all objects in the queried
#*                     database.
#*
#*   --postfix, --db-url-postfix .html
#*                     Specify URL postfix for all objects in the queried
#*                     database.
#*
#*   --def-table, --definition-table databases
#*                     Look "databases" table for descriptions of remote
#*                     databases.
#*
#*   --db-name, --database-name cod
#*                     Use "cod" as the abbreviation of queried databases.
#*                     This abbreviation is used in construction of
#*                     cross-referencing table names, i.e. "pubchem_x_cod".
#*
#*   --from
#*                     Generate RDFs for all database entries starting at
#*                     specified entry and ending at either the entry
#*                     specified using '--to' command line option or the
#*                     last entry in the database.
#*
#*   --to
#*                     Generate RDFs for all database entries from either the
#*                     entry specified using '--from' command line option or
#*                     the first entry in the database.
#*
#*   --range
#*                     Generate RDFs for all database entries in the specified
#*                     range. Range should be specified in the form 'X-Y', where
#*                     X is the first and Y is the last entry.
#*
#*   --revision
#*                     Generate RDFs for all database entries deposited in the
#*                     specified or older Subversion revision of the database.
#*
#*   --split-author-names
#*                     Split author names in internal database representation
#*                     at semicolons (';') to produce RDF-parsable list of
#*                     author names. Each author in such list is marked with
#*                     "author" property instead of "authors", which is used
#*                     for non-split value (default).
#*
#*   --no-split-author-names, --dont-split-author-names
#*                     Make no assumptions about internal database
#*                     representation of author list.
#*
#*   --xml-output
#*                     Output RDF in XML carrier format (default).
#*
#*   --n3-output
#*                     Output RDF in N3 (Notation3) carrier format.
#*
#*   --turtle-output
#*                     Output RDF in Turtle carrier format.
#*
#*   --ntriples-output, --n-triples-output
#*                     Output RDF in N-Triples carrier format.
#*
#*   --replace-utf-codepoints-from
#*                     Replace Unicode code points starting at specified point
#*                     with carrier format-specific entities.
#*
#*   --no-replace-utf-codepoints, --dont-replace-utf-code-points
#*                     Leave Unicode code points unescaped (default).
#*
#*   --html-utf-escapes
#*                     Escape Unicode code points using HTML hexadecimal format
#*                     corresponding to '&#x%04X;' formatted string (default).
#*
#*   --internal-utf-escapes
#*                     Escape Unicode code points using internal format
#*                     corresponding to '\\u%04X' formatted string.
#*
#*   --utf-escape-format
#*                     Escape Unicode code points using format corresponding
#*                     to the provided formatted string.
#*
#*   --help, --usage
#*                     Output a short usage message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
@ARGV = getOptions(
    "-d,--database"  => \$database{name},
    "-l,--localhost" => sub { $database{host} = 'localhost' },
    "-h,--host"      => \$database{host},
    "-p,--port"      => \$database{port},
    "-s,--server"    => \$database{host},
    "-u,--user"      => \$database{user},
    "--password"     => \$database{password},
    "--platform"     => \$database{platform},

    "--vocabulary-namespace" => \$rdf_options->{vocabulary_name},
    "--vocabulary-url-prefix" => \$rdf_options->{vocabulary_url_prefix},
    "--prefix,--db-url-prefix"   => \$rdf_options->{url_prefix},
    "--postfix,--db-url-postfix" => \$rdf_options->{url_postfix},
    "--def-table,--definition-table" => \$database{definition_table},
    "--db-name,--database-name" => \$refering_db_name,

    "--from" => \$from,
    "--to" => \$to,
    "--range" => sub { get_value() =~ /^([0-9]+)\-([0-9]+)$/;
                       $from = $1;
                       $to = $2; },
    "--revision" => \$revision,

    "--split-author-names" =>
        sub { $rdf_options->{split_author_names} = 1 },
    "--no-split-author-names" =>
        sub { $rdf_options->{split_author_names} = 0 },
    "--dont-split-author-names" =>
        sub { $rdf_options->{split_author_names} = 0 },

    "--xml-output"       => sub { $output_format = 'xml' },
    "--n3-output"        => sub { $output_format = 'n3' },
    "--turtle-output"    => sub { $output_format = 'turtle' },
    "--ntriples-output"  => sub { $output_format = 'ntriples' },
    "--n-triples-output" => sub { $output_format = 'ntriples' },

    "--replace-utf-codepoints-from" =>
        \$rdf_options->{replace_utf_code_points_from},
    "--no-replace-utf-codepoints" =>
        sub { $rdf_options->{replace_utf_code_points_from} = undef },
    "--dont-replace-utf-code-points" =>
        sub { $rdf_options->{replace_utf_code_points_from} = undef },

    "--html-utf-escapes" =>
        sub { $rdf_options->{utf_code_point_format} = '&#x%04X;' },
    "--internal-utf-escapes" =>
        sub { $rdf_options->{utf_code_point_format} = "\\u%04X" },
    "--utf-escape-format"    => \$rdf_options->{utf_code_point_format},

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

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

my $dbh = db_connect( $database{platform},
                      $database{host},
                      $database{name},
                      $database{port},
                      $database{user},
                      $database{password} );
if (!defined $dbh) {
    report_message( {
        'program'   => $0,
        'err_level' => 'ERROR',
        'message'   => "connection to the '$database{host}' failed"
    }, $die_on_error_level->{'ERROR'} );
}

my $where_condition = "";
if( defined $from && defined $to ) {
    $where_condition .= "file >= $from AND file <= $to";
} elsif( defined $from ) {
    $where_condition .= "file >= $from";
} elsif( defined $to ) {
    $where_condition .= "file <= $to";
}
if( defined $revision ) {
    if( $where_condition ) {
        $where_condition .= " AND (svnrevision IS NULL OR svnrevision <= $revision)";
    } else {
        $where_condition = "svnrevision IS NULL OR svnrevision <= $revision";
    }
}
if( @ARGV ) {
    if( $where_condition ) {
        $where_condition = "($where_condition) OR ";
    }
    $where_condition .= "file IN (" . join( ",", @ARGV ) . ")";
}

my $data = $dbh->selectall_arrayref(
                "SELECT * FROM data " .
                "WHERE $where_condition",
                { Slice => {} }
           );

$data = { map { $_->{file} => $_ } @$data };

my $any_data_seen = (defined $from || defined $to || defined $revision) &&
                     keys %$data > 0;
for my $codid (@ARGV) {
    if( !exists $data->{$codid} ) {
        report_message( {
            'program'   => $0,
            'err_level' => 'WARNING',
            'message'   => "structure '$codid' could not "
                         . 'be found in the COD database'
        }, $die_on_error_level->{'WARNING'} );
    } else {
        $any_data_seen = 1;
    }
}
if ( !$any_data_seen ) {
    report_message( {
        'program'   => $0,
        'err_level' => 'ERROR',
        'message'   => 'no data for RDF was found in the COD database'
        }, $die_on_error_level->{'ERROR'} );
}

# Forming URLs, removing unneeded fields
for my $entry (values %$data) {
    $entry->{url} = $rdf_options->{url_prefix} .
                    $entry->{file} .
                    $rdf_options->{url_postfix};
    delete $entry->{file};
    delete $entry->{text};
    delete $entry->{flags};

    for my $field (qw( formula calcformula cellformula )) {
        next if !exists  $entry->{$field};
        next if !defined $entry->{$field};
        $entry->{$field} =~ s/^-\s*//;
        $entry->{$field} =~ s/\s*-$//;
    }
}

my $databases = $dbh->selectall_arrayref(
    "SELECT name, url_prefix, url_postfix " .
    "FROM `$database{definition_table}`",
    { Slice => {} }
);

my %databases = map{ lc($_->{name}) => { url_prefix => $_->{url_prefix},
                                         url_postfix => $_->{url_postfix} } }
                     @$databases;

my $ids = join( ', ', @ARGV );
my %vocabularies = ( $rdf_options->{vocabulary_name} =>
                     $rdf_options->{vocabulary_url_prefix} );

for my $database (keys %databases) {
    my $table = "${database}_x_${refering_db_name}";
    my $db_data = $dbh->selectall_arrayref( <<"SQLEND"
        SELECT ${refering_db_name}_id AS db_id,
               ext_id,
               relation,
               vocabulary,
               rdf_relations.uri_prefix AS vocabulary_uri_prefix
        FROM `$table`
        JOIN rdf_relations
            ON relation_id = rdf_relations.id
        WHERE ${refering_db_name}_id IN ($ids)
SQLEND
        , { Slice => {} }
    );

    for my $entry (@$db_data) {
        my $db_id = $entry->{db_id};
        $vocabularies{$entry->{vocabulary}} =
            $entry->{vocabulary_uri_prefix};
        $data->{$db_id}{links} = [] unless exists $data->{$db_id}{links};
        push( @{$data->{$db_id}{links}},
            {
                db => $database,
                ext_id => $entry->{ext_id},
                relation => $entry->{relation},
                vocabulary => $entry->{vocabulary},
            } );
    }
}

$rdf_options->{databases}    = \%databases;
$rdf_options->{vocabularies} = \%vocabularies;

if( $output_format eq 'xml' ) {
    print rdf_xml( [ map { $data->{$_} } sort keys %$data ],
                   $rdf_options );
} elsif( $output_format eq 'n3' or $output_format eq 'turtle' ) {
    print rdf_n3( [ map { $data->{$_} } sort keys %$data ],
                  $rdf_options );
} elsif( $output_format eq 'ntriples' ) {
    print rdf_ntriples( [ map { $data->{$_} } sort keys %$data ],
                        $rdf_options );
}

# Function used to connect to database
sub db_connect
{
    my ($db_platform, $db_host, $db_name, $db_port, $db_user, $db_pass) = @_;
    my $dsn = "dbi:$db_platform:" .
              "hostname=$db_host;".
              "dbname=$db_name".
              ($db_port ? ";$db_port" : "");
    my $dbh = DBI->connect( $dsn, $db_user, $db_pass, {PrintError => 0} );
    if( !$dbh ) {
        report_message( {
            'program'   => $0,
            'err_level' => 'ERROR',
            'message'   => 'could not connect to the database'
                         . ( defined $DBI::errstr ? ' -- ' . lcfirst( $DBI::errstr )
                                                  : "" )
        }, $die_on_error_level->{'ERROR'} );
    }
    if( $db_platform ne 'SQLite' ) {
        $dbh->do( 'SET CHARACTER SET utf8' );
        $dbh->do( 'set @@character_set_client = utf8' );
        $dbh->do( 'set @@character_set_connection = utf8' );
        $dbh->do( 'set @@character_set_server = utf8' );
        $dbh->do( 'set @@character_set_database = utf8' );
    }
    return $dbh;
}