-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCONFIGURE.pm
722 lines (623 loc) · 23.6 KB
/
CONFIGURE.pm
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
#------------------------------------------------------------------------------
# CancerRNASeq - a pipeline for transcriptomics datasets
# Author: Raphael Hablesreiter
#
# CONFIGURATION.pm
#
# Description: Configuration Module
# This module handles the configuration of the pipeline, which
# means that all the necassery parameters for the pipeline are set
# in this module.
#
#-------------------------------------------------------------------------------
#
package CONFIGURE;
use 5.010;
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use Config::Simple;
use Backticks;
use PIPELINETOOLS;
sub Main{
my $check_value = 0;
#store values of the commandline input into the global variable
$check_value = CONFIGURE::GetCmdOptions();
if ($check_value == 0)
{
PIPELINETOOLS::PrintTask("Started To Check *.config-Files");
#store values of the system configuration file into the global variable
CONFIGURE::ReadConfigFile("/export/home/rhablesreiter_914/Pipeline_Version1/Pipeline/Program/sys.ini");
#store values of the user configuration file into the global variable
CONFIGURE::ReadConfigFile($::global_cfg -> param("cmdline.usrconfig"));
CONFIGURE::CheckUsrConfig();
PIPELINETOOLS::PrintTask("Finished To Check *.config-Files");
return 0;
}
else
{
return -1;
}
}
#-------------------------------------------------------------------------------
# GetCmdOptions: This function reads all the commandline parameters, with the
# the help of the package Getopt::Long, and saves the
# parameters into the global variable
#-------------------------------------------------------------------------------
sub GetCmdOptions{
#From
#https://metacpan.org/pod/Getopt::Long
#GetOptions() parses the command line from @ARGV,
#recognizing and removing specified options and their possible values.
#pass_through (default: disabled)
#With pass_through anything that is unknown, ambiguous
#or supplied with an invalid option will not be flagged as an error.
#Instead the unknown option(s) will be passed to the catchall <> if present,
#otherwise through to @ARGV.
Getopt::Long::Configure("pass_through");
my %input_options;
my $values_chk = 0;
my $key;
my $value;
GetOptions(
'config:s' => \$input_options{usrconfig},
'help' => \$input_options{help},
'silent' => \$input_options{silent},
'threads:i' => \$input_options{threads},
'overwrite'=> \$input_options{overwrite}
);
#From
#http://www.perlhowto.com/iterate_through_a_hash
while (($key, $value) = each %input_options)
{
if ($key eq "help")
{
if ($value)
{
$values_chk += 1;
}
}
elsif($key eq "silent")
{
if ($value)
{
$::global_cfg -> param("cmdline.$key","1");
}
}
elsif($key eq "overwrite")
{
if ($value)
{
$::global_cfg -> param("cmdline.$key","1");
}
}
elsif($key eq "threads")
{
if ($value)
{
if ($value > 0)
{
$::global_cfg -> param("cmdline.$key",$value);
}
else
{
$::global_cfg -> param("cmdline.$key","1");
}
}
else
{
$::global_cfg -> param("cmdline.$key","1");
}
}
elsif($value eq "")
{
$values_chk += 1;
}
else
{
$::global_cfg -> param("cmdline.$key","$value");
}
}
if ($values_chk != 0 && $input_options{help})
{
#help has to be printed
CONFIGURE::PrintHelp();
return -1;
}
elsif ($values_chk != 0)
{
CONFIGURE::PrintUsage();
return -1;
}
else
{
return 0;
}
}
#-------------------------------------------------------------------------------
# GetCmdOptions: This function takes as input the name of a configuration file.
# The Configuration file has to be a INI-file, which is saved
# into the global variable.
# See Cinfig::Simple
#-------------------------------------------------------------------------------
sub ReadConfigFile{
#See
#http://search.cpan.org/~sherzodr/Config-Simple-4.59/Simple.pm
my $cfg_name = $_[0];
if (-e $cfg_name)
{
my $system_cfg = new Config::Simple($cfg_name);
my @cfg_layout = $system_cfg -> get_block();
while (@cfg_layout)
{
my $current_block = shift(@cfg_layout);
$::global_cfg -> param(-block => $current_block,
-values => ($system_cfg -> param(-block => $current_block)));
}
}
else
{
die "Config-File not found.";
}
return;
}
#-------------------------------------------------------------------------------
# CheckUsrConfig: This function checks the necessary parameters for a smooth
# run of the pipeline.
#-------------------------------------------------------------------------------
sub CheckUsrConfig{
my $fail = "";
my $dir_raw = $::global_cfg -> param("sdirectories.RawData");
my @tc_info = ("User", "ProjectName", "sequence_1",
"sequence_2", "ReferenceGenome");
my @tc_files = ("GenomeFASTA", "GenomeGTF");
while (@tc_info)
{
my $tmp_name = shift(@tc_info);
if (! $::global_cfg -> param("required.$tmp_name"))
{
$fail = $fail."$tmp_name is not provided!\n";
}
else
{
my $tmp = $::global_cfg -> param("required.$tmp_name");
$tmp =~ s/ /_/g;
$::global_cfg -> param("required.$tmp_name",$tmp);
}
}
if (! $::global_cfg -> param("required.seqtype"))
{
$fail = $fail."Sequences type is not provided!\n";
}
elsif($::global_cfg -> param("required.seqtype") ne "PE" and
$::global_cfg -> param("required.seqtype") ne "SE")
{
$fail = $fail."Sequences type is not known!\n";
}
if (! $::global_cfg -> param("required.MappingType"))
{
$fail = $fail."Mapping type is not provided!\n";
}
elsif($::global_cfg -> param("required.MappingType") ne "Tophat" and
$::global_cfg -> param("required.MappingType") ne "STAR" and
$::global_cfg -> param("required.MappingType") ne "HISAT2")
{
$fail = $fail."Mapping type is not known!\n";
}
if (! $::global_cfg -> param("required.savemode"))
{
$fail = $fail."Mapping type is not provided!\n";
}
elsif($::global_cfg -> param("required.savemode") == 1 and
$::global_cfg -> param("required.savemode") == 2)
{
$fail = $fail."Savemode is not known!\n";
}
if (! $::global_cfg -> param("required.qscore"))
{
$fail = $fail."Mapping type is not provided!\n";
}
elsif($::global_cfg -> param("required.qscore") ne "phred33" and
$::global_cfg -> param("required.qscore") ne "phred64")
{
$fail = $fail."Quality scoring type is not known!\n";
}
for (my $j = 1; $j <= 2; $j++)
{
my $end_seq = 0;
my $seq_nr = 1;
while ($end_seq == 0)
{
my $seqname = "sequence".$j."_".$seq_nr."_";
if ($::global_cfg -> param("required.seqtype") eq "PE")
{
if (($::global_cfg -> param("sequences.".$seqname."1")) &&
($::global_cfg -> param("sequences.".$seqname."2")))
{
if ($j == 1)
{
push(@::sequences_1,
($::global_cfg -> param("sequences.".$seqname."1")));
push(@::sequences_1,
($::global_cfg -> param("sequences.".$seqname."2")));
}
else
{
push(@::sequences_2,
($::global_cfg -> param("sequences.".$seqname."1")));
push(@::sequences_2,
($::global_cfg -> param("sequences.".$seqname."2")));
}
}
else
{
if ($j == 1)
{
$::global_cfg -> param("required.seqlength1",
scalar(@::sequences_1));
}
else
{
$::global_cfg -> param("required.seqlength2",
scalar(@::sequences_1));
}
$end_seq++;
}
}
else
{
if (($::global_cfg -> param("sequences.".$seqname."1")))
{
if ($j == 1)
{
push(@::sequences_1,
($::global_cfg -> param("sequences.".$seqname."1")));
}
else
{
push(@::sequences_2,
($::global_cfg -> param("sequences.".$seqname."1")));
}
}
else
{
if ($j == 1)
{
$::global_cfg -> param("required.seqlength1",
scalar(@::sequences_1));
}
else
{
$::global_cfg -> param("required.seqlength2",
scalar(@::sequences_2));
}
$end_seq++;
}
}
$seq_nr++;
}
if ($j == 1)
{
for (my $z = 0; $z < scalar(@::sequences_1); $z++)
{
my $file = $dir_raw."/Sequences/".($::sequences_1[$z]);
if (!-e $file)
{
$fail = $fail."Can't find ".$file."!\n";
}
}
}
else
{
for (my $z = 0; $z < scalar(@::sequences_2); $z++)
{
my $file = $dir_raw."/Sequences/".($::sequences_2[$z]);
if (!-e $file)
{
$fail = $fail."Can't find ".$file."!\n";
}
}
}
}
if ($fail ne "")
{
print $fail;
die "ERROR: INFORMATION is required!\n";
}
CONFIGURE::CreateProjectFolder();
my $dir_tmp = $::global_cfg -> param("results.tmp");
my $files_check = 0;
while (@tc_files)
{
my $tmp_file = shift(@tc_files);
if ($::global_cfg -> param("required.$tmp_file"))
{
my $tmp = $::global_cfg -> param("required.$tmp_file");
my $counf = 0;
given ($tmp_file)
{
when ("GenomeFASTA")
{
if (!-e $dir_raw."/FASTA/".$tmp) {$counf += 1;}
}
when ("GenomeGTF")
{
if (!-e $dir_raw."/GTF/".$tmp) {$counf += 1;}
}
default
{
if (!-e $dir_raw."/Sequences/".$tmp) {$counf += 1;}
}
}
if ($counf != 0)
{
$fail = $fail."Cant' find $tmp!\n";
$files_check += 1;
}
}
}
if ($files_check == 0)
{
@tc_files = ("GenomeFASTA", "GenomeGTF");
while (@tc_files)
{
my $tmp_file = shift(@tc_files);
my $tmp = $::global_cfg -> param("required.$tmp_file");
if (($tmp_file eq "GenomeGTF" or $tmp_file eq "GenomeFASTA") and
-e ($::global_cfg -> param("results.tmp"))."/".(substr($tmp,0,-3)))
{
$::global_cfg -> param("required.$tmp_file",
$dir_tmp."/".(substr($tmp,0,-3)));
}
elsif (($tmp_file eq "GenomeGTF" or $tmp_file eq "GenomeFASTA") and
-e ($::global_cfg -> param("results.tmp"))."/".$tmp)
{
$::global_cfg -> param("required.$tmp_file",
$dir_tmp."/".$tmp);
}
elsif ($tmp_file eq "GenomeFASTA" and
$tmp =~ ".gz" and
!-e $dir_tmp."/".(substr($tmp,0,-3)))
{
my $cp_cmd = "cp ".$dir_raw."/FASTA/".$tmp." ".$dir_tmp;
my $cmd = `$cp_cmd`;
if ($cmd -> success != 1)
{
PIPELINETOOLS::PrintStd($cmd -> stdout);
PIPELINETOOLS::WriteLogOut(($cmd -> merged));
die "ERROR: Can't copy $tmp to tmp-folder!\n";
}
my $gz_cmd = "gunzip ".$dir_tmp."/".$tmp;
$cmd = `$gz_cmd`;
if ($cmd -> success != 1)
{
PIPELINETOOLS::PrintStd($cmd -> stdout);
PIPELINETOOLS::WriteLogOut(($cmd -> merged));
die "ERROR: Can't gunzip $tmp in tmp-folder!\n";
}
$::global_cfg -> param("required.$tmp_file",
$dir_tmp."/".(substr($tmp,0,-3)));
}
elsif ($tmp_file eq "GenomeFASTA" and
!-e ($::global_cfg -> param("results.tmp"))."/".$tmp)
{
my $cp_cmd = "cp ".$dir_raw."/FASTA/".$tmp." ".$dir_tmp;
my $cmd = `$cp_cmd`;
if ($cmd -> success != 1)
{
PIPELINETOOLS::PrintStd($cmd -> stdout);
PIPELINETOOLS::WriteLogOut(($cmd -> merged));
die "ERROR: Can't copy $tmp to tmp-folder!\n";
}
$::global_cfg -> param("required.$tmp_file",
$dir_tmp."/".$tmp);
}
elsif ($tmp_file eq "GenomeGTF" and
$tmp =~ ".gz" and
!-e $dir_tmp."/".(substr($tmp,0,-3)))
{
my $cp_cmd = "cp ".$dir_raw."/GTF/".$tmp." ".$dir_tmp;
my $cmd = `$cp_cmd`;
if ($cmd -> success != 1)
{
PIPELINETOOLS::PrintStd($cmd -> stdout);
PIPELINETOOLS::WriteLogOut(($cmd -> merged));
die "ERROR: Can't copy $tmp to tmp-folder!\n";
}
my $gz_cmd = "gunzip ".$dir_tmp."/".$tmp;
$cmd = `$gz_cmd`;
if ($cmd -> success != 1)
{
PIPELINETOOLS::PrintStd($cmd -> stdout);
PIPELINETOOLS::WriteLogOut(($cmd -> merged));
die "ERROR: Can't gunzip $tmp in tmp-folder!\n";
}
$::global_cfg -> param("required.$tmp_file",
$dir_tmp."/".(substr($tmp,0,-3)));
}
elsif ($tmp_file eq "GenomeGTF" and
!-e ($::global_cfg -> param("results.tmp"))."/".$tmp)
{
my $cp_cmd = "cp ".$dir_raw."/GTF/".$tmp." ".$dir_tmp;
my $cmd = `$cp_cmd`;
if ($cmd -> success != 1)
{
PIPELINETOOLS::PrintStd($cmd -> stdout);
PIPELINETOOLS::WriteLogOut(($cmd -> merged));
die "ERROR: Can't copy $tmp to tmp-folder!\n";
}
$::global_cfg -> param("required.$tmp_file",
$dir_tmp."/".$tmp);
}
elsif ($tmp_file ne "GenomeFASTA" and $tmp_file ne "GenomeGTF" and
!-e ($::global_cfg -> param("results.tmp"))."/".$tmp)
{
my $cp_cmd = "cp ".$dir_raw."/Sequences/".$tmp." ".$dir_tmp;
my $cmd = `$cp_cmd`;
if ($cmd -> success != 1)
{
PIPELINETOOLS::PrintStd($cmd -> stdout);
PIPELINETOOLS::WriteLogOut(($cmd -> merged));
die "ERROR: Can't copy $tmp to tmp-folder!\n";
}
$::global_cfg -> param("required.$tmp_file",
$dir_tmp."/".$tmp);
}
}
my @init_succfiles = PIPELINETOOLS::ReadDirectory($dir_tmp,".succ");
if (!@init_succfiles)
{
my $m = 0;
while ($m < scalar(@::sequences_1))
{
if (!-e ($dir_tmp."/".($::sequences_1[$m])))
{
my $cp_cmd = "cp ".$dir_raw."/Sequences/".($::sequences_1[$m]).
" ".$dir_tmp;
my $cmd = `$cp_cmd`;
if ($cmd -> success != 1)
{
PIPELINETOOLS::PrintStd($cmd -> stdout);
PIPELINETOOLS::WriteLogOut(($cmd -> merged));
die "ERROR: Can't copy ".$::sequences_1[$m]." to tmp-folder!\n";
}
}
$::sequences_1[$m] = $dir_tmp."/".$::sequences_1[$m];
$m++;
}
my $n = 0;
while ($n < scalar(@::sequences_2))
{
#my @test = ();
my $tmp = $dir_raw."/Sequences/".($::sequences_2[$n])."/*";
#if (#@test == <$tmp> &&
if(!-e ($dir_tmp."/".($::sequences_2[$n])))
{
my $cp_cmd = "cp ".$dir_raw."/Sequences/".($::sequences_2[$n]).
" ".$dir_tmp;
my $cmd = `$cp_cmd`;
if ($cmd -> success != 1)
{
PIPELINETOOLS::PrintStd($cmd -> stdout);
PIPELINETOOLS::WriteLogOut(($cmd -> merged));
die "ERROR: Can't copy ".$::sequences_2[$n]."to tmp-folder!\n";
}
}
$::sequences_2[$n] = $dir_tmp."/".$::sequences_2[$n];
$n++;
}
}
}
return;
}
#-------------------------------------------------------------------------------
# CheckUsrConfig: This function creates the necassary folders for the project.
#-------------------------------------------------------------------------------
sub CreateProjectFolder{
#Creates the folder architecture for the project
my $project_name = $::global_cfg -> param("required.ProjectName");
my $user_name = $::global_cfg -> param("required.User");
my $dir_results = $::global_cfg -> param("sdirectories.Results");
#create user folder
$dir_results = $dir_results."/".$user_name;
CONFIGURE::CreateFolder($dir_results);
#create project folder
my $dir_prj = $dir_results."/".$project_name;
if ($::global_cfg -> param("cmdline.overwrite"))
{
my $uinput = "";
print "Do you like to overwrite folder: $dir_prj !\n
yes(y) or no(n): ";
while ($uinput ne "yes" && $uinput ne "no" &&
$uinput ne "y" && $uinput ne "n" &&
$uinput ne "Yes" && $uinput ne "No" &&
$uinput ne "Y" && $uinput ne "N" &&
$uinput ne "YES" && $uinput ne "NO")
{
chomp($uinput = <STDIN>);
}
if ($uinput eq "yes" || $uinput eq "YES" ||
$uinput eq "Y" || $uinput eq "Yes" ||
$uinput eq "y")
{
my $delete_cmd = "rm -R -f ".$dir_prj;
PIPELINETOOLS::PrintStd($delete_cmd);
my $cmd = `$delete_cmd`;
if ($cmd -> success != 1)
{
PIPELINETOOLS::PrintStd($cmd -> merged);
PIPELINETOOLS::WriteLogOut($cmd -> merged);
die "ERROR: Can't overwrite $dir_results!\n";
}
PIPELINETOOLS::PrintStd($cmd -> stdout);
PIPELINETOOLS::WriteLogOut($cmd -> stdout)
}
}
CONFIGURE::CreateFolder($dir_prj);
#create subfolders for the projet
CONFIGURE::CreateFolder($dir_prj."/tmp");
CONFIGURE::CreateFolder($dir_prj."/Results");
CONFIGURE::CreateFolder($dir_prj."/QualityReports");
CONFIGURE::CreateFolder($dir_prj."/Preprocessing");
CONFIGURE::CreateFolder($dir_prj."/Graphics");
CONFIGURE::CreateFolder($dir_prj."/Alignment");
#adding directories to the global variable
$::global_cfg -> param("results.std", $dir_prj);
$::global_cfg -> param("results.tmp", $dir_prj."/tmp");
$::global_cfg -> param("results.results", $dir_prj."/Results");
$::global_cfg -> param("results.qreports", $dir_prj."/QualityReports");
$::global_cfg -> param("results.preprocess", $dir_prj."/Preprocessing");
$::global_cfg -> param("results.graphics", $dir_prj."/Graphics");
$::global_cfg -> param("results.alignment", $dir_prj."/Alignment");
return;
}
#-------------------------------------------------------------------------------
# CreateFolder: This function creates a folder.
# Input: folder path
#-------------------------------------------------------------------------------
sub CreateFolder{
use File::Path qw(make_path);
my $folder_name = $_[0];
if (!-d $folder_name)
{
make_path($folder_name) or
die "ERROR! Can't create folders for Results!\n";
}
return;
}
#-------------------------------------------------------------------------------
# CheckUsrConfig: This function prints the help message.
#-------------------------------------------------------------------------------
sub PrintHelp{
#subroutine for printing
my $line = ("#" x 80)."\n";
print "Help: \n";
print $line;
CONFIGURE::PrintUsage();
print $line."\n";
print "--threads <n>: This can be used to set a higher number of threads\n";
print " used by the pipeline.";
print "--overwrite: If overwrite is set to TRUE, it will start the,\n";
print " pipeline always at the beginning.\n";
print " That means if there is already a folder with the same\n";
print " name it will be deleted and after that the pipeline\n";
print " starts.\n";
print "--silent: This option supresses the STDOUT, and only prints the\n";
print " crucial output.\n";
print "--help: Prints this message!\n\n";
print "Created by Raphael Hablesreiter\n";
return;
}
#-------------------------------------------------------------------------------
# CheckUsrConfig: This function prints the usage message.
#-------------------------------------------------------------------------------
sub PrintUsage{
print "Usage: perl Main.pl --config <user configuration file> [options]\n";
print " Options:\n";
print " --threads n (n = number of threads, default: n = 1)\n";
print " --overwrite (default: FALSE)\n";
print " --silent\n";
print " --help\n";
return;
}
1;