-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeDLLconf.pl
executable file
·376 lines (320 loc) · 11 KB
/
makeDLLconf.pl
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
#!/usr/bin/env perl
#===============================================================================
#
# FILE: SPARQLqueryRDF.pl
#
# USAGE: ./SPARQLqueryRDF.pl
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Christoph Kaempf (CK), [email protected]
# ORGANIZATION:
# VERSION: 1.0
# CREATED: 05.08.2012 15:29:50
# REVISION: ---
#===============================================================================
## Loading modules and initializing variables ##
use strict;
use warnings;
use feature "switch";
use Data::Dumper;
use File::Basename;
use File::Spec;
use Getopt::Long;
use Image::Magick;
use List::Util qw(first max maxstr min minstr reduce shuffle sum);
use Log::Log4perl qw(get_logger :levels);
use Math::BigFloat;
use Pod::Usage;
use RDF::Query;
use RDF::Trine::Parser;
use RDF::Helper;
use Path::Class;
use Pod::Usage;
use Scalar::Util qw(blessed);
# load my modules
my $module_dir = dirname(__FILE__);
# $module_dir =~ s/scripts$/RNAprobing/g;
push(@INC, $module_dir);
my $rdf_file = "";
my $pos = "";
my $neg = "";
my $owl_file ="";
my $verbose = 0;
my $help = "";
my $man = 0;
GetOptions(
"rdf=s" => \$rdf_file,
"pos=s" => \$pos,
"neg=s" => \$neg,
"owl=s" => \$owl_file,
"verbose|v+" => \$verbose,
"help|h" => \$help,
"man|m" => \$man) or pod2usage(-verbose => 1) && exit;
if ( $help ) {
pod2usage( -verbose => 1 ) && exit;
} elsif ( $man ) {
pod2usage( -verbose => 2 ) && exit;
} elsif ( $rdf_file eq "" ){
pod2usage( { -verbose => 1,
-message => "Use this script like this:\n"});
}
###############################################################################
#
# Logger initiation
#
###############################################################################
my $log4perl_conf = file(dirname(__FILE__), "RNAprobing.log.conf");
pod2usage(-verbose => 1) && exit if ( $help );
pod2usage(-verbose => 1) && exit if ( ($rdf_file eq "") ||
($pos eq "") ||
($neg eq "") );
pod2usage(-verbose => 2) && exit if ( $man );
# Apply configuration to the logger
Log::Log4perl->init("$log4perl_conf");
# Get the logger
my $logger_name = "RNAprobing";
my $logger = &configureLogger($verbose, $logger_name);
$logger->info("++++ ".__FILE__." has been started. ++++");
# check if mandatory input files exist
$rdf_file = &checkFiles($rdf_file) if ( $rdf_file ne "" );
$pos = &checkFiles($pos) if ( $pos ne "" );
$neg = &checkFiles($neg) if ( $neg ne "" );
# Configure RDF::Helper
my $rdf = RDF::Helper->new (
BaseInterface => 'RDF::Trine',
namespaces => {
bioinf => "http://www.bioinf.uni-leipzig.de/~kaempf/RNAprobing.owl#",
rdfs => "http://www.w3.org/2000/01/rdf-schema#",
rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
xsd => "http://www.w3.org/2001/XMLSchema#",
'#default' => "http://purl.org/rss/1.0/",
},
ExpandQNames => 1);
my $model = $rdf->model();
my $base_uri = 'http://purl.org/rss/1.0/';
my $parser = RDF::Trine::Parser->new( 'rdfxml' );
$parser->parse_file_into_model( $base_uri, $rdf_file, $model );
# Read SPARQL query defining negative list
my $pos_sparql_query = "";
open( my $pos_sparql_fh, "<", $pos) or die "Couldn't open file $pos. Error: $!";
while (<$pos_sparql_fh>) {
$pos_sparql_query .= $_;
}
close $pos_sparql_fh;
# Execute query and collect results in @positive_list
my @positive_list = ();
if ( $pos_sparql_query =~ /[Ss][Ee][Ll][Ee][Cc][Tt]\s/ ) {
# SPARQL SELECT Query
my $query = RDF::Query->new( $pos_sparql_query );
my $iterator = $query->execute( $model );
while (my $row = $iterator->next) {
# $row is a HASHref containing variable name -> RDF Term bindings
my @vars = keys %$row;
foreach my $key ( @vars) {
my $pos_element = $row->{ $key }->as_string;
$pos_element =~ s/[<>]/"/g;
$logger->info($pos_element);
push(@positive_list, $pos_element);
}
}
}
# Read SPARQL query defining negative list
my $neg_sparql_query = "";
open( my $neg_sparql_fh, "<", $neg) or die "Couldn't open file $neg. Error: $!";
while (<$neg_sparql_fh>) {
$neg_sparql_query .= $_;
}
close $neg_sparql_fh;
# Execute query and collect results in @negative_list
my @negative_list = ();
if ( $neg_sparql_query =~ /[Ss][Ee][Ll][Ee][Cc][Tt]\s/ ) {
# SPARQL SELECT Query
my $query = RDF::Query->new( $neg_sparql_query );
my $iterator = $query->execute( $model );
while (my $row = $iterator->next) {
# $row is a HASHref containing variable name -> RDF Term bindings
my @vars = keys %$row;
foreach my $key ( @vars) {
my $neg_element = $row->{ $key }->as_string;
$neg_element =~ s/[<>]/"/g;
$logger->info($neg_element);
push(@negative_list, $neg_element);
}
}
}
my $pos_query_name = fileparse( $pos );
$pos_query_name =~ s/\.sparql$//g;
my $conf_file = $rdf_file;
$conf_file =~ s/\.rdf$/\.$pos_query_name\.conf/g;
# Calculate relative path between rdf file and conf file to be
# parse filename of conf file
my ($conf_name, $conf_dir, $conf_suff) = fileparse($conf_file);
my ($rdf_name, $rdf_dir, $rdf_suff) = fileparse($rdf_file);
my $rel_path = File::Spec->abs2rel( $rdf_dir, $conf_dir );
$rdf_file = File::Spec->catfile($rel_path, $rdf_name);
my $conf_content = "";
$conf_content .= "// knowledge sources\n".
"ks1.type = \"OWL File\"\n".
"ks1.fileName = \"$rdf_file\"\n";
if ($owl_file ne ""){
$conf_content .= "ks2.type = \"OWL File\"\n".
"ks2.fileName = \"$owl_file\"\n\n";
}
$conf_content .= "// reasoner\n".
"reasoner.type = \"fast instance checker\"\n".
"reasoner.sources = { ks1 }\n\n".
"// run 10-fold cross validation\n".
"cli.type = \"org.dllearner.cli.CLI\"\n".
"cli.writeSpringConfiguration = false\n".
"cli.performCrossValidation = true\n".
"cli.nrOfFolds = 10\n".
"// learning algorithm\n".
"h.type =\"celoe_heuristic\"\n".
"// h.expansionPenaltyFactor = 0.2\n".
"h.expansionPenaltyFactor = 0.01\n\n".
"op.type = \"rho\"\n".
"op.useCardinalityRestrictions = true\n".
"op.useNegation = true\n\n".
"// algorithm parameters\n".
"alg.type = \"celoe\"\n".
"// alg.nrOfThreads = 4\n".
"alg.maxExecutionTimeInSeconds = 60\n".
"alg.noisePercentage = 30\n\n".
"// learning problem\n".
"lp.type = \"posNegStandard\"\n".
"lp.accuracyMethod = \"fmeasure\"\n\n".
"// Nr. of positives: ". scalar(@positive_list). "\n".
"lp.positiveExamples = {".join(",", @positive_list)."}\n".
"// Nr. of negatives: ". scalar(@negative_list)."\n".
"lp.negativeExamples = {".join(",", @negative_list)."}\n";
$logger->debug($conf_content);
open(my $conf_fh, ">", $conf_file) or die "Couldn't open file $conf_file. Error: $!";
print $conf_fh $conf_content;
close $conf_fh;
###############################################################################
##
## Subroutine section
##
###############################################################################
###############################################################################
##
## &configureLogger($verbosityLevel)
## - Configures and initialzes the Logger
## - $verbosityLevel = scalar value that sets log level
## -- 0 => $ERROR
## -- 1 => $WARN
## -- 2 => $INFO
## -- >2 => $DEBUG
##
###############################################################################
sub configureLogger{
## Configure the logger ##
my $verbose = shift;
my $logger_name = shift;
my $logger = get_logger($logger_name);
$logger->info("Verbosity level: $verbose");
SELECT:{
if ($verbose == 0){
$logger->level($ERROR);
$logger->debug("Log level is ERROR") ;
last SELECT; }
if ($verbose == 1){
$logger->level($WARN) ;
$logger->debug("Log level is WARN") ;
last SELECT; }
if ($verbose == 2){
$logger->level($INFO) ;
$logger->debug("Log level is INFO") ;
last SELECT; }
else {
$logger->level($DEBUG);
$logger->debug("Log level is DEBUG") ;
last SELECT; }
}
return $logger;
}
###############################################################################
##
## &checkFiles(@filesToBeChecked)
## - Performs file checks and returns an array with all succesfully checked files
## - @filesToBeChecked = array of files to be checked
##
###############################################################################
sub checkFiles {
my $test_file = shift;
my $checked_file = "";
my $logger = get_logger();
# Check if files are readable
if ( -r $test_file){
$checked_file = $test_file;
$logger->info("$test_file is readable.");
} else {
$logger->error("$test_file is not readable.");
exit;
}
return $checked_file;
}
###############################################################################
## !!! Does not work for unknown reasons
## &query_model($sparql_query, $model)
## - queries $model with $sparql_query
##
###############################################################################
sub query_model {
my ($sparql_query, $model) = @_;
my $logger = get_logger("RNAprobing");
my $result_list = "";
if ( $sparql_query =~ /[Ss][Ee][Ll][Ee][Cc][Tt]\s/ ) {
# SPARQL SELECT Query
my $query = RDF::Query->new( $sparql_query );
my $iterator = $query->execute( $model );
while (my $row = $iterator->next) {
# $row is a HASHref containing variable name -> RDF Term bindings
my @vars = keys %$row;
foreach my $key ( @vars) {
$result_list .= $row->{ $key }->as_string.",";
}
}
}
if ( $sparql_query =~ /[Cc][Oo][Nn][Ss][Tt][Rr][Uu][Cc][Tt]\s/ ) {
# SPARQL CONSTRUCT/DESCRIBE Query
my $query = RDF::Query->new( $sparql_query );
my $iterator = $query->execute( $model );
while (my $st = $iterator->next) {
# $st is a RDF::Trine::Statement object representing an RDF triple
$result_list .= $st->as_string.",";
print $st->as_string;
}
}
return $result_list;
}
__END__
=head1 NAME
makeDLLconf.pl - Querys a RDF model
=head1 SYNOPSIS
makeDLLconf.pl --rdf=</path/to/rdf-model> --pos=</path/to/pos-sparql-query> --neg--neg=</path/to/neg-sparql-query> -v -v -v
=head1 OPTIONS
=over 4
=item --rdf=</path/to/rdf-model>
RDF file containing the RDF model
=item --pos=</path/to/pos-sparql-query>
File containing a SPARQL query that returns the positive set of nodes from the RDF model
=item --neg=</path/to/neg-sparql-query>
File containing a SPARQL query that returns the negative set of nodes from the RDF model
=item -v, --verbose
Verbosity level increases by multiple times option given
=item -h, --help
Prints this help page
=item -m, --man
Prints the complete man page
=back
=back
=head1 DESCRIPTION
B<This program> will read the given input file(s) and do something
useful with the contents thereof.
=cut