-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditDLLconf.pl
executable file
·339 lines (285 loc) · 9.69 KB
/
editDLLconf.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
#!/usr/bin/env perl
#===============================================================================
#
# FILE: editDLconf.pl
#
# USAGE: ./editDLconf.pl
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Christoph Kaempf (CK), kaempf@bioinf.uni-leipzig.de
# ORGANIZATION:
# VERSION: 1.0
# CREATED: 05.08.2012 15:28:44
# REVISION: ---
#===============================================================================
## Loading modules and initializing variables ##
use strict;
use warnings;
use File::Basename;
use File::Copy;
use File::Find;
use Getopt::Long;
use Log::Log4perl qw(get_logger :levels);
use Path::Class;
use Pod::Usage;
###############################################################################
#
# Options section
#
################################################################################
my @confs_to_combine = ();
my @directories_to_search = ();
my $delete = "";
my $help = 0;
my $insert = "";
my $man = 0;
my $model = "";
my $replace = "";
my $replace_by = "";
my $template = "";
my $find_pattern = '\.conf';
my $verbose = 0;
GetOptions(
"combine|c=s" => \@confs_to_combine,
"delete|del=s" => \$delete,
"directory|dir=s" => \@directories_to_search,
"findPattern|f=s" => \$find_pattern,
"help|h" => \$help,
"insert|i=s" => \$insert,
"man|m" => \$man,
"model=s" => \$model,
"replace|r=s" => \$replace,
"replaceBy|rb=s" => \$replace_by,
"template|t=s" => \$template,
"verbose|v+" => \$verbose);
if ( $help ) {
pod2usage( -verbose => 1 ) && exit;
} elsif ( $man ) {
pod2usage( -verbose => 2 ) && exit;
} elsif ( ($replace eq "" || $replace_by eq "") &&
(scalar(@confs_to_combine) == 0 || $template eq "" || $model eq "") ){
pod2usage( { -verbose => 1,
-message => "Use this script like this:\n"});
}
###############################################################################
#
# Logger initiation
#
###############################################################################
my $this_file = __FILE__;
$this_file =~ s/scripts/RNAprobing/g;
my $log4perl_conf = file(dirname($this_file), "RNAprobing.log.conf");
# 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. ++++");
###############################################################################
#
# Application section
#
###############################################################################
if ( $replace ne "" && $replace_by ne "") {
if ( scalar(@directories_to_search) != 0 ) {
find(\&wanted, @directories_to_search);
}
}
if ( scalar(@confs_to_combine) != 0 && $template ne "" && $model ne "" ) {
# Check files for accessibility
my @conf_files = ();
foreach my $file (@confs_to_combine) {
push(@conf_files, &checkFiles($file) );
}
$template = &checkFiles($template);
# If we do only have one file there is nothing to do
if (scalar(@conf_files) == 0){
$logger->error("Only one file given to -c , --combine. Nothing to do.");
exit 0;
}
# Extract the relevant info from each file and ...
my @lp_pos = ();
my @lp_neg = ();
for ( my $i = 1; $i <= scalar(@conf_files); $i++ ) {
open(my $conf_file, "<", $conf_files[$i-1]) or
die "Couldn't read $conf_files[$i-1].";
while(<$conf_file>){
chomp($_);
if ( $_ =~ m/^lp\.positiveExamples/ ){
$_ =~ s/^lp\.positiveExamples.*\{//g;
$_ =~ s/}.*//g;
my @pos_from_conf = split(',', $_);
push(@lp_pos, @pos_from_conf);
}
if ( $_ =~ m/^lp\.negativeExamples/ ){
$_ =~ s/^lp\.negativeExamples.*\{//g;
$_ =~ s/}.*//g;
my @neg_from_conf = split(',', $_);
push(@lp_neg, @neg_from_conf);
}
}
close($conf_file);
}
# ... combine them according to the template
my $combined = $template;
$combined =~ s/conf$/combined.conf/;
open(my $template_file, "<", $template) or die "Couldn't read $template.";
open(my $combined_file, ">", $combined) or die "Couldn't write $combined.";
while(<$template_file>){
my $line = "";
if ( $_ =~ m/^ks\d*\.fileName/ ){
$_ =~ m/(.*\")(.*)(\".*)/;
print($combined_file $1."./".$model.$3."\n");
} elsif ( $_ =~ m{^// Nr\. of positives: \d*}){
print($combined_file "// Nr. of positives: ".scalar(@lp_pos)."\n");
} elsif ($_ =~ m/^lp\.positiveExamples/){
$line = join(",", @lp_pos);
print($combined_file "lp.positiveExamples = {".$line."}\n");
} elsif ( $_ =~ m{^// Nr\. of negatives: \d*}){
print($combined_file "// Nr. of negatives: ".scalar(@lp_neg)."\n");
} elsif ($_ =~ m/^lp\.negativeExamples/){
$line = join(",", @lp_neg);
print($combined_file "lp.negativeExamples = {". $line ."}\n");
} else {
print($combined_file $_);
}
}
close($template_file);
close($combined_file);
}
###############################################################################
##
## Subroutine section
##
###############################################################################
sub wanted {
my $logger = get_logger("RNAprobing");
my $file = &checkFiles($_);
# assemble filename of file to be created
my @date = localtime;
my $year = $date[5] + 1900;
my $month = sprintf( "%02d", $date[4] + 1);
my $day = sprintf("%02d", $date[3]);
my $modified_file = "$file.mod.$date[5]$date[4]$date[3].conf";
if ( ! -B $file ) {
# see if the file is one we should modify
if ( $file =~ m/$find_pattern/ ) {
chomp($_);
$logger->info("Found pattern \"$find_pattern\" in $file.");
open(my $conf_file, ">", $modified_file) or die "Couldn't write to $modified_file.";
open(my $orig_conf_file, "<", $file) or die "Couldn't read $file.";
# Read in original file and check if we need to delete or replace
# the current line
while (<$orig_conf_file>) {
if ( ($delete ne "") && ($_ =~ m/$delete/) ) {
} elsif ( ( $_ =~ m/$replace/ ) && ($replace ne "") && ($replace_by ne "") ) {
print($conf_file "$replace_by\n");
} else {
print($conf_file "$_\n");
}
}
# Insert specified info at end of file
open(my $insert_file, "<", $insert) or die "Couldn't read from $insert";
my $insert_text = "";
while (<$insert_file>) {
$insert_text .= $_;
}
close($insert_file);
print($conf_file $insert_text) unless ( $insert_text eq "" );
close($conf_file);
close($orig_conf_file);
} else {
$logger->info("$file does not match pattern:\"$find_pattern\"");
}
} else {
$logger->info("File $file is not a text file.");
}
}
###############################################################################
##
## &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("RNAprobing");
# 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;
}
###############################################################################
##
## &configureLogger($verbosityLevel)
## - Configures and initialzes the Logger
## - $verbosityLevel = scalar value that sets log level
## -- 0 => $WARN
## -- 1 => $INFO
## -- 2 => $DEBUG
##
###############################################################################
sub configureLogger{
## Configure the logger ##
my $verbose = shift;
my $logger_name = shift;
my $logger = get_logger($logger_name);
SELECT:{
if ($verbose == 0){
$logger->level($WARN) ;
$logger->debug("Log level is WARN") ;
last SELECT;
}
if ($verbose == 1){
$logger->level($INFO) ;
$logger->debug("Log level is INFO") ;
last SELECT;
}
if ($verbose == 2){
$logger->level($DEBUG);
$logger->debug("Log level is DEBUG") ;
last SELECT;
}
else {
$logger->level($ERROR);
$logger->debug("Log level is ERROR") ;
last SELECT;
}
}
return $logger;
}
__END__
=head1 NAME
editDLLconf.pl -
=head1 SYNOPSIS
editDLLconf.pl [options]
=head1 DESCRIPTION
=head1 OPTIONS
=over 8
=item B<-h, --help>
Display help message
=item B<-m, --man>
Display whole man page
"delete=s" => \$delete,
=item B<-d, --directory>
\@directories_to_search,
=item B<-f, --findPattern>
=item B<-i,--insert>
Name of file which contains the information to be added to the files found given -d and -f.
=item B<-r, --replace>
=item B<-rb, --replaceBy>
=item B<-v, --verbose>
Increases the verbosity level. Can be used multiple times (verbosest if used 3 or more times)
=cut