forked from frankMusacchia/Annocript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annocript.pl
executable file
·3472 lines (2910 loc) · 154 KB
/
annocript.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
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#Annocript - A complete tool for transcriptomes annotation
#Copyright (C) <2015> <Francesco Musacchia>
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
=head1 NAME
Annocript - A complete tool for transcriptomes annotation
=head1 SYNOPSIS
Annocript is a tool for the annotation of transcriptomes.
Please read carefully the user Readme and User Guide before to start executing it.
You must give in input the configuration file to run Annocript:
annocript.pl config_user.txt
=cut
=head1 OPTIONS
=over 8
=item B<--help>
Shows the brief help informations
=item B<--version>
Shows the version
=back
=cut
=head1 EXAMPLES
Write the config.txt file and give it in input to this script. In the config file you have to specify the locations of the data
that you want to manage.
The following is an example of call to this script:
annocript.pl config.txt
=cut
=head1 DESCRIPTION
Here you have the descriptions of the elements that you have to write on the file config.txt
=cut
=head1 AUTHORS
Francesco Musacchia
Swaraj Basu
Remo Sanges
--
http://www.szn.it
$Id: Annocript, 2015/14/19 11:00:12 Exp $
=cut
use strict;
use warnings;
use Data::Dumper;#To print the hashes
use Cwd;#To change work directory
#USE THE GENERAL UTILITY MODULES
use File::Copy;#To manage file
use FindBin;#To search libraries
use lib $FindBin::Bin;#To search libraries
use Term::ReadKey;
use Getopt::Long;#To control the input
use Pod::Usage;#Used to write an usage
use LWP::Simple;#Functions for FTP management
use File::Path qw(make_path remove_tree);#To remove directories
use IO::Handle;#To immediately print with autoflush
#Annocript libraries
use DB_CREATION::db_creator qw( execute_db_creator);
use PROGRAMS_EXEC::execute_programs qw( execute_programs );
use GFF3_AND_OUTPUT::gff3_manager qw(execute_gff_manager);
use USEFUL::utilities qw(checkLink correct_type db_present extract_name
detect_fasta_type clean_cd_name print_array
num_processors append_file_2_file check_presence my_head ram_memory
is_folder_empty save_hash check_url testFTP check_FTP_diff_sources
indexed_db_present getDBVersion checkFastaFormat append_2_file);
use USEFUL::utils qw(execute_utils);
#EXTRACT PARAMETERS FROM THE CONFIG FILE
my $configHash;#This hash holds up all the life of Annocript
my $version = '1.1.3';
my $dbTablesVersion = '0.3';
my $configUser = "config_user.txt";
my $configAnnocript = "CONFIGURATION/config_annocript.txt";
my $variablesFile = "CONFIGURATION/variables.txt";
my $dataFolder = "data";
my $logFolder = "log";
my $sessionFolder = "jobs";
my $outFolder ="output";
my $gffFolder = "gff";
my $statsFolder = "stats";
my $tempTableFolder = 'tables';#Folder to keep the tables while they are being generated
my $usefulFolder = 'USEFUL';#A folder with useful scripts and data
my $annocriptFolder = '';#Folder with scripts
my $workingFolder = '';#Folder where Annocript has to work
my $foldersFile = "folders.txt";
my $logFile = "";
my $timesFile = "";
#Variables present also in utils.pl (IF YOU CHANGE IT HERE CHANGE ALSO IN UTILS.PL!!)
my $jobsFolder = "jobs";#Folder Users
my $mainDataFolder = "data";#Folder inside Annocript with all data needed more than one time
my $utilsScript = "USEFUL/utils.pl";
#We need these variables to check if the programs are present
my @blastProgramsInUse = qw( blastx
blastp
blastn
tblastn
rpstblastn
rpsblast
makeblastdb
blastdbcmd
);
#We need these variables to check if the files of the indexed cd database are present
my @cdFilesExts = qw(.rps .psq .psi .psd .pin .phr .loo .aux);
#Admitted database in use for conserved domains
my @cdDBAdmitted = qw(cd pfam kog cog tigr smart prk);
######################################################### MAIN ########################################################
# Parse command line arguments.
parse_command_line_args();
#LOG FILE CREATION
#Here we take the current time to get a log name different at each computation
my $time = scalar(localtime);
$time =~ s/ /_/g;
$time =~ s/:/-/g;
$logFile = "annocript_exec_$time.log";
$timesFile = "annocript_times_$time.log";
#open the log file
my $logFHandle;
open($logFHandle, ">$logFile") or die "ERROR [$!]: Cannot open $logFile! Check permissions.\n";
STDOUT->autoflush(1);#This makes STDOUT hot in the sense that everything will be print immediately
annoPrint ("***********************************************************************\n");
annoPrint ("This program comes with ABSOLUTELY NO WARRANTY; for details http://opensource.org/licenses/GPL-3.0.
This is free software, and you are welcome to redistribute it
under certain conditions; http://opensource.org/licenses/GPL-3.0 for details.\n");
annoPrint ("***********************************************************************\n");
annoPrint (" /&&&&&& /&& /&& \n");
annoPrint (" /&&__ && |__/ | && \n");
annoPrint ("| && | &&/&&&&&&& /&&&&&&& /&&&&&& /&&&&&&& /&&&&&& /&& /&&&&&& /&&&&&& \n");
annoPrint ("| &&&&&&&| &&__ &| &&__ && /&&__ &&/&&_____//&&__ &| &&/&&__ &|_ &&_/ \n");
annoPrint ("| &&__ &| && | &| && | &| && | &| && | && |__| &| && | && | && \n");
annoPrint ("| && | &| && | &| && | &| && | &| && | && | &| && | && | && /&& \n");
annoPrint ("| && | &| && | &| && | &| &&&&&&| &&&&&&| && | &| &&&&&&&/ | &&&&/ \n");
annoPrint ("|__/ |__|__/ |__|__/ |__/ |______/ |_______|__/ |__| &&____/ |___/ \n");
annoPrint (" | &&\n");
annoPrint (" | &&\n");
annoPrint (" |__/\n");
annoPrint ("\nAnnocript is a tool created by the Bioinformatic Lab of SZN of Naples.");# With this software you can execute a series of commands");
annoPrint (" \nGiven a fasta file with a transcriptome, it will annotate your sequences and");
annoPrint (" it will separate putative coding and long non-coding RNA sequences.\n");
annoPrint (" Reference: Musacchia F, Basu S, Petrosino G, Salvemini M, Sanges R. \n");
annoPrint (" Annocript: a flexible pipeline for the annotation of transcriptomes which can also identify ");
annoPrint (" putative long non-coding RNA.");
annoPrint (" Bioinformatics (2015) doi:10.1093/bioinformatics/btv106");
annoPrint ("\n***********************************************************************\n\n\n");
#Creates some folders that Annocript will use for the session
createBasicFolders();
#Choose what session or if one new
my $newSession = showOptions();
#We check if the config file is already present in the given folder. If this happens, the user can decide the overwriting
find_config_file();
#The variable will take the complete path as name
$configUser = $sessionFolder."/".$ARGV[0];
#Here we change the working directory
chdir $annocriptFolder;
#prints a sentence in a square of asteriscs
nice_printing("CHECKING THE INTEGRITY OF THE CONFIGURATION FILES");
#Checks if the configuration files are present and if they have all the variables correctly written
checkConfigVariables($configUser,$configAnnocript,$variablesFile);
#Open the config file and builds the hashes for programs
configFile2Hash($configUser);
annoPrint ("They are good. Annocript can proceed!\n");
#Open the config file and builds the hashes for programs
configFile2Hash($configAnnocript);
#This routine simply add some other elements to the configuration Hash
nice_printing("RUNNING ANNOCRIPT CONFIGURATION");
#Checks folder and files if they are well written
check_Annocript_integrity();
#Complete the configuration by controlling every variable inserted in the config file and
#creating folders inside the session one
completeConfiguration();
#Saves the hash in a file to be used later
my $hashName = "configHash";
my $configHashPath = $dataFolder."/".$hashName;
save_hash($configHashPath,$configHash, $hashName);
my $newLogFile = $logFolder."/".$logFile;#Log of Annocript goes in the log folder
annoPrint ("Your configuration has been saved! A log file will be created in $logFolder/$logFile. \nThe overall computation usually"
." depends on the number of sequences and the speed of the machine you are using. \nYou may want to check Annocript step-by-step "
."with the following command: more ".$newLogFile.".\n"
."\nYou will find all the results in the folder you chose here. Please take a look at\n"
." https://github.com/frankMusacchia/Annocript/blob/master/GUIDE/OUTPUT.md to understand the output from Annocript and the"
." organization of folders."
."\n\nPlease let us know if any problem occurs during the computation or you don't get from Annocript the expected result.\n"
."You may want to use the forum at: https://groups.google.com/forum/#!forum/annocript.\n");
nice_printing("STARTING ANNOCRIPT IN BACKGROUND! BYE!");
close ( $logFHandle );
#print "moving the file now...";
move($workingFolder.'/'.$logFile, $newLogFile) or annoDie(" Annocript will not move $workingFolder/$logFile to $newLogFile\n");
$logFile = $newLogFile;
$timesFile = $logFolder."/".$timesFile;#File with times goes inside the log folder
#We also remove all the other remaining log file in the folder created by user's interruptions
system("rm -f $workingFolder/annocript_exec_*");
#This perl script will be run in background
my $runAnnocriptCmd = "nohup perl ".$configHash->{'ProgExecFolder'} ."/annocript_executor.pl $configUser $configAnnocript $timesFile $configHashPath >> $logFile &";
( system($runAnnocriptCmd) ) == 0
or die("Unable to start annocript_executor!\n");
###############################################################CHECKS AND CONTROLS###########################################
=head2 find_config_file
Title : find_config_file
Usage : find_config_file()
Function: check if a config file is present in the folder. If yes, it can overwrite it or not
Returns : nothing
=cut
sub find_config_file{
annoPrint ( "Copying the given config file in $sessionFolder\n");
#We check if the config file is already present in the given folder. If this happens, the user can decide the overwriting
if ( -e ($sessionFolder.'/'.$ARGV[0]) ){
my $sure= get_input("A config file $ARGV[0] is already present in the folder $sessionFolder. Do you want to overwrite it?(y or n) [Suggested: y]",'^[YynN]$');
annoPrint($sure."\n",1);
if ( ( $sure eq "y") or ( $sure eq "Y")){
(system("cp $ARGV[0] $sessionFolder") ) == 0
or annoDie("ERROR: Unable to copy $ARGV[0] in $sessionFolder. Please check permissions...\n");
}
else { annoPrint( "Ok, Annocript will use the one present there...\n");}
}else{
(system("cp $ARGV[0] $sessionFolder") ) == 0
or annoDie("ERROR: Unable to copy $ARGV[0] in $sessionFolder. Please check permissions...\n");
}
}
=head2 annoPrint
Title : annoPrint
Usage : annoPrint( - string -> the sentence that have to be annoPrint
- onlyLog -> a number);
Function: will print (the string in input always in the log file and on the STDOUT
if onlyLog is used then the print will be also in the log file
Returns : nothing
=cut
sub annoPrint{
my $string = shift;
my $onlyLog = shift;
if ( defined $onlyLog){
print $logFHandle $string;
}else{
my $STDOUT = *STDOUT;
#Prints on both OUT
for ($logFHandle, $STDOUT) { print $_ $string; }
}
}
=head2 annoDie
Title : annoDie
Usage : annoDie( - string -> the sentence that have to be print
Function: will print the string and die
Returns : nothing
=cut
sub annoDie{
my $string = shift;
my $STDOUT = *STDOUT;
#Print on both the log and the STDOUT the error
for ($logFHandle, $STDOUT) { print $_ $string; }
if ( -d $logFolder){
#Moving the log file to the log folder before to exit Annocript
move($workingFolder.'/'.$logFile, $logFolder."/".$logFile) or die "Cannot move $workingFolder/$logFile to $logFolder/$logFile\n";
}#else{
#print "Annocript will not move the log file: $workingFolder/$logFile. All the log files in $workingFolder will be removed".
#" at next successful Annocript run.\n";
#}
#Die and print error number
exit;# $!;
}
=head2 nice_printing
Title : nice_printing
Usage : nice_printing( - sentence -> the sentence that have to be annoPrint ();
Function: this subroutine prints in a box of asterisks the sentence in input
Returns : nothing
=cut
sub nice_printing{
my $sentence = shift;
my $length = length($sentence);
my $i;
for ($i=0; $i<($length);$i++){
annoPrint ("#");
}
annoPrint ("####\n# $sentence #\n####");
for ( $i=0; $i<$length; $i++){
annoPrint ("#");
}
annoPrint ("\n\n");
}
=head2 checkConfigVariables
Title : checkConfigVariables
Usage : checkConfigVariables( - configUser -> file with the user configuration
- configAnnocript -> file with the basic parameters of Annocript
- variablesFile -> the path to a file with all variables written
);
Function: this subroutine reads the config files and check if all variables are there and are well written.
The variables.txt file is needed fot this operation.
Returns : nothing
=cut
sub checkConfigVariablesOLD {
my $configUser = shift;
my $configAnnocript = shift;
my $variablesFile = shift;
my $hashCheck;
if (! open(VARF,"<$variablesFile")){ annoDie ("ERROR: Failure opening '$variablesFile'. Your Annocript version is corrupted - $!");}
if (! open(CUSER,"<$configUser")){ annoDie ("ERROR: Cannot find '$configUser' - You can pick it from Annocript folder and put in $workingFolder.");}
if (! open(CANN,"<$configAnnocript")){ annoDie ("ERROR: Failure opening '$configAnnocript'. Your Annocript version is corrupted - $!");}
#Stores the variables in the config user file inside the hash
my $start = 0;
while (my $line = <CUSER>){
#This code is to jump to the line where there are some ###
if ($line =~ /#########/){
$start = 1;
}
#Put variables in a hash
if( ($line =~ /(\S+)\s*=/) and ($start == 1) and !($line =~ /#/) ){
#annoPrint ($line."\n");#DEBUGCODE
$hashCheck->{$1} = "OK";
}
}
#Stores the variables in the config annocript file inside the hash
$start = 0;
while (my $line = <CANN>){
#This code is to jump to the line where there are some ###
if ($line =~ /#########/){
$start = 1;
}
#Put variables in a hash
if( ($line =~ /(\S+)\s*=/) and ($start == 1) and !($line =~ /#/) ){
$hashCheck->{$1} = "OK";
#annoPrint ($line."\n"); #DEBUGCODE
}
}
close(CUSER);
close(CANN);
my @allVars = ();
#Variables that are in the configuration file must be also in the variables.txt file
my $errors=0;
my $line=0;
#For each line of the variables file
while (my $line = <VARF>){
#get the variables in the line
my @variables = split (/;/,$line);
#put the variables inside an array
push (@allVars, @variables);
$line++;
#For each of the variables in the variables file
foreach my $var (@variables){
#annoPrint ("Variable: $var - - value: ".$hashCheck->{$var}."\n");#DEBUGCODE
$var =~ s/\n//;
if( !(defined($hashCheck->{$var})) ){
if ($line == 1){
annoDie( "ERROR: in $configAnnocript variable $var is missing. Please check the file. Closing Annocript...\n ");
$errors=1;
}
if ($line == 2){
annoDie( "ERROR: in $ARGV[0] - variable $var is missing. Please check the file. Closing Annocript...\n ");
$errors=1;
}
}#else{ annoPrint ("From the hash: ".$hashCheck->{$var}."\n";)}
}
}
#print_array(\@allVars);
#print Dumper\$hashCheck;
#Now check if all the elements in the hash are also in the array
foreach my $key (keys %$hashCheck){
# print "Search $key in array...\n";#DEBUGCODE
if (!(grep {/$key/} @allVars )){
annoDie("ERROR: Variable $key is in the config files and not in $variablesFile file. This is completely wrong. Re-install Annocript.\n ");
}
}
#if ($errors == 0){annoPrint ("ok";}
close(VARF);
}
=head2 checkConfigVariables
Title : checkConfigVariables
Usage : checkConfigVariables( - configUser -> file with the user configuration
- configAnnocript -> file with the basic parameters of Annocript
- variablesFile -> the path to a file with all variables written
);
Function: this subroutine reads the config files and check if all variables are there and are well written.
The variables.txt file is needed fot this operation.
Returns : nothing
=cut
sub checkConfigVariables {
my $configUser = shift;
my $configAnnocript = shift;
my $variablesFile = shift;
my $hashUserCheck;
my $hashAnnCheck;
if (! open(VARF,"<$variablesFile")){ annoDie ("ERROR: Failure opening '$variablesFile'. Your Annocript version is corrupted - $!");}
if (! open(CUSER,"<$configUser")){ annoDie ("ERROR: Cannot find '$configUser' - You can pick it from Annocript folder and put in $workingFolder.");}
if (! open(CANN,"<$configAnnocript")){ annoDie ("ERROR: Failure opening '$configAnnocript'. Your Annocript version is corrupted - $!");}
#Stores the variables in the config user file inside the hash
my $start = 0;
while (my $line = <CUSER>){
#This code is to jump to the line where there are some ###
if ($line =~ /#########/){
$start = 1;
}
#Put variables in a hash
if( ($line =~ /(\S+)\s*=/) and ($start == 1) and !($line =~ /#/) ){
#annoPrint ($line."\n");#DEBUGCODE
$hashUserCheck->{$1} = "OK";
}
}
#Stores the variables in the config annocript file inside the hash
$start = 0;
while (my $line = <CANN>){
#This code is to jump to the line where there are some ###
if ($line =~ /#########/){
$start = 1;
}
#Put variables in a hash
if( ($line =~ /(\S+)\s*=/) and ($start == 1) and !($line =~ /#/) ){
$hashAnnCheck->{$1} = "OK";
#annoPrint ($line."\n"); #DEBUGCODE
}
}
close(CUSER);
close(CANN);
my @userConfVars = ();
my @annConfVars = ();
#Variables that are in the configuration file must be also in the variables.txt file
my $errors=0;
my $lines=0;
#For each line of the variables file
while (my $line = <VARF>){
$line =~ s/\n//;#Remove \n in the end of the line
#get the variables in the line
my @variables = split (/;/,$line);
$lines++;
#For each of the variables in the variables file
foreach my $var (@variables){
#$var =~ s/\n//;#Remove
if ($lines == 1){
#annoPrint ("Annocript Variable: $var - - value: ".$hashAnnCheck->{$var}."\n");#DEBUGCODE
#put the variable inside an array for ann config
push (@annConfVars, $var);
if( !(defined($hashAnnCheck->{$var})) ){
annoDie( "ERROR: in $configAnnocript variable $var is missing. Please check the file. Closing Annocript...\n ");
$errors=1;
}#else{ annoPrint ("From the hash: ".$hashCheck->{$var}."\n";)}
}
if ($lines == 2){
#annoPrint ("User Variable: $var - - value: ".$hashUserCheck->{$var}."\n");#DEBUGCODE
#put the variable inside an array for user config
push (@userConfVars, $var);
if( !(defined($hashUserCheck->{$var})) ){
annoDie( "ERROR: in $ARGV[0] - variable $var is missing. Please check the file. Closing Annocript...\n ");
$errors=1;
}#else{ annoPrint ("From the hash: ".$hashCheck->{$var}."\n";)}
}
}
}
#print_array(\@allVars);
#print Dumper\$hashCheck;
#Now check if all the elements in the hash are also in the array
foreach my $key (keys %$hashUserCheck){
# print "Search $key in array...\n";#DEBUGCODE
if (!(grep {/$key/} @userConfVars )){
annoDie("ERROR: Variable $key is in the user config files and not in $variablesFile file. This is completely wrong. Annocript will not work...\n ");
}
}
#Now check if all the elements in the hash are also in the array
foreach my $key (keys %$hashAnnCheck){
# print "Search $key in array...\n";#DEBUGCODE
if (!(grep {/$key/} @annConfVars )){
annoDie("ERROR: Variable $key is in the annocript config files and not in $variablesFile file. This is completely wrong. Annocript will not work...\n ");
}
}
#if ($errors == 0){annoPrint ("ok";}
close(VARF);
}
=head2 checkVariable
Title : checkVariable
Usage : checkVariable( - var -> value of the variable to check
- name -> name of the variable
- sentence -> something to write to the user in case of error)
Function: this subroutine checks if a variable has the YES or NO value. Dies otherwise.
Returns : nothing
=cut
sub checkVariable {
my $var = shift;
my $name = shift;
my $yesSentence = shift;
my $noSentence = shift;
my $no = '';
if (defined $noSentence){
$no = $noSentence;
}
if ( ($var ne 'YES') and ($var ne 'NO')){
annoDie("ERROR: Check variable $name in the config file. $var is a wrong value!\n");
}elsif ($var eq 'YES'){
annoPrint ($yesSentence);
}elsif ($var eq 'NO'){
annoPrint ($no);
}
}
=head2 createBasicFolders
Title : createBasicFolders
Usage : createBasicFolders( );
Function: this subroutine creates the basic folders for Annocript. If the folders.txt file is not present
means that Annocript has not been installed in the folder used.
Returns : nothing
=cut
sub createBasicFolders{
#If the file with folders references is installed Annocript can start
open (FOLD, $foldersFile)
or annoDie(" ERROR: Annocript has not been installed here. Please execute install.pl in the program folder.\n");
my $line = <FOLD>;
#Extracts folders paths
my @folders = split(" ", $line);
#The working folder is the first path
$workingFolder = $folders[0];
#Cancel the final slash..if it is there it is removed
if($workingFolder =~ /\/$/){
chop($workingFolder);
}
$mainDataFolder = $workingFolder."/".$mainDataFolder;
$jobsFolder = $workingFolder."/".$jobsFolder;
$sessionFolder = $jobsFolder;
$annocriptFolder = $folders[1];
#$workingFolder = getcwd;
$configHash->{'dataFolder'} = $dataFolder;
$configHash->{'gffFolder'} = $gffFolder;
$configHash->{'outFolder'} = $outFolder;
$configHash->{'statsFolder'} = $statsFolder;
$configHash->{'logFolder'} = $logFolder;
close(FOLD);
#Creates the jobs folder if it does not exists
unless(-d $jobsFolder){
annoPrint ($jobsFolder." doesn't exists. Creating folder...\n");
mkdir $jobsFolder or annoDie("ERROR: can't create folder $jobsFolder\n ");
}
#Creates the main 'data' folder if it does not exists
unless(-d $mainDataFolder){
annoPrint ($mainDataFolder." doesn't exists. Creating folder...\n");
mkdir $mainDataFolder or annoDie( "ERROR: can't create folder $mainDataFolder\n ");
}
#annoPrint ("Annocript folder: $annocriptFolder - Users: $jobsFolder - \n");#DEBUGCODE
}
=head2 showOptions
Title : showOptions
Usage : showOptions( );
Function: Shows some options the user can choose. Actually this is the menu of Annocript.
Returns : 1 if the user choose a new session else 0
=cut
sub showOptions{
my $newSession = 0;
my $myFolder;
annoPrint ("1. New session - 2. Use Previous - 3. Remove Session - 4. Do some utils - 5. Exit\n");
my $session = get_input("Your choice: ","^[12345]\$");
annoPrint ($session."\n",1);#Print on the log file
#A construct will execute different code depending from the choose
if ($session eq "1") { $newSession = 1; $myFolder = changeSession();}
if ($session eq "2") {
if ( !(is_folder_empty($jobsFolder)) ){
my $workDir = getcwd;
chdir $jobsFolder;
(system("ls -d */")) == 0 or annoDie ("Unable to list directory $jobsFolder!\n");
annoPrint ("Choose your folder: ");
$myFolder = <STDIN>;
chomp $myFolder;
$myFolder = $1 if($myFolder=~/(.*)\/$/);#Cut last letter if it is /
#print $myFolder;
my $validSession = 0;
if (valid_session($myFolder) == 1){
$validSession = 1;
}
while ( (!(-d $myFolder)) or ($validSession == 0)){
(system("ls -d */")) == 0 or annoDie ("Unable to list directory $jobsFolder!\n");
annoPrint ("Choose your folder: ");
$myFolder = <STDIN>;
chomp $myFolder;
$myFolder = $1 if($myFolder=~/(.*)\/$/);#Cut last letter if it is /
if (valid_session($myFolder) == 1){
$validSession = 1;
}
}
$sessionFolder = $sessionFolder."/".$myFolder;
chdir $workDir;
}else{
annoPrint("There are not existing sessions. Please restart Annocript and create a new one!\n");
exit 1;
}
}
if ($session eq "3") {
my $workDir = getcwd;
chdir $jobsFolder;
(system("ls")) == 0
or annoDie ("Error: Unable to list directory $jobsFolder. Check permissions!\n");
annoPrint ("Choose the session to remove: ");
$myFolder = <STDIN>;
chomp $myFolder;
while(!(-d $myFolder)){
(system("ls")) == 0
or annoDie ("Error: Unable to list directory $jobsFolder. Check permissions!\n");
annoPrint ("Choose your folder: ");
$myFolder = <STDIN>;
chomp $myFolder;
}
my $sessCancel = get_input("Really you want to remove $myFolder ?(y or n)",'^[YynN]$');
#Code when the user wants to overwrite
annoPrint($sessCancel."\n",1);#Print on the log file
if ( ($sessCancel eq "y") or ($sessCancel eq "Y") ){
my $sure= get_input("Are you sure?(y or n)",'^[YynN]$');
annoPrint($sure."\n",1);#Print on the log file
if ( ( $sure eq "y") or ( $sure eq "Y")){
remove_tree($myFolder);
print "$myFolder correctly removed!\n";
exit 1;
}
else { print "$myFolder will not be removed!\n"; exit 1;}
}else{ print "$myFolder will not be removed!\n"; exit 1;}
#chdir $workDir;
}
if ($session eq "4"){
my $configAnnPath = $annocriptFolder."/".$configAnnocript;
my $script = $annocriptFolder."/".$utilsScript;
#(system("perl $script $configAnnPath")) == 0
#or annoDie("Error: Unable to call $script!\n");
execute_utils($configAnnPath);
exit 1;
}
if ($session eq "5") {annoPrint ("Annocript was interrupted by user... bye!\n");exit 1;}
annoPrint ($myFolder."\n",1);
return $newSession;
}
=head2 valid_session
Title : valid_session
Usage : valid_session( $session -> the name of the folder );
Function: When user wants to open an existing session Annocript will controll if this does not come
from a wrong execution: some folders must be inside
Returns : 1 if the session is valid, else -1
=cut
sub valid_session{
my $session = shift;
my $missFolders = "";
my $retVal = -1;
if (-d $session){
#Folders to be alive in the session folder if it is consistent
if ( !(-d $session."/".$dataFolder) ){
$missFolders.= " ".$dataFolder;
}
if ( !(-d $session."/".$logFolder) ){
$missFolders.= " ".$logFolder;
}
if ( !(-d $session."/".$outFolder) ){
$missFolders.= " ".$outFolder;
}
if ( !(-d $session."/".$statsFolder) ){
$missFolders.= " ".$statsFolder;
}
if ( !(-d $session."/".$gffFolder) ){
$missFolders.= " ".$gffFolder;
}
if ( $missFolders eq ""){
$retVal = 1;
}else{
my $sessCancel = get_input("Session $session is not valid. Do you want to remove it?(y or n)",'^[YynN]$');
#Code when the user wants to overwrite
annoPrint($sessCancel."\n",1);#Print on the log file
if ( ($sessCancel eq "y") or ($sessCancel eq "Y") ){
my $sure= get_input("Are you sure?(y or n)",'^[YynN]$');
annoPrint($sure."\n",1);#Print on the log file
if ( ( $sure eq "y") or ( $sure eq "Y")){
remove_tree($session);
annoPrint("Done! Annocript will exit..\n");
exit 1;
}
# else { annoPrint( "Ok, choose another name. But don't let Annocript waste its time...\n");}
}
#else { annoPrint("Ok, choose another name. But don't let Annocript waste its time...\n");}
#print "Session $session is not valid. Please remove it.\n";
#
print "Error: folders $missFolders are missing\n";
}
}else{print "Folder $session does not exist.\n";}
return $retVal;
}
=head2 changeSession
Title : changeSession
Usage : changeSession( );
Function: When user wants to start another session it asks about the name and check if that name is already been taken. At the end
of the function the folder is created.
Returns : nothing
=cut
sub changeSession{
annoPrint ("Choose a name for your session: ");
my $session = <STDIN>;
chomp $session;
#Check that the use does not insert strange chars in the name
while ( $session !~ /^[A-za-z0-9\_\-]*$/i ){
annoPrint( "The name $session is not permitted. \nChoose another using [A-za-z0-9\_\-]:");
$session = <STDIN>;
chomp $session;
}
#Controls if the session folder already exists and does something
while (-d $sessionFolder."/".$session){
my $ok = 0;
my $sesNaOverWrite = get_input("This name has already been taken, overwrite it?(y or n)",'^[YynN]$');
#Code when the user wants to overwrite
annoPrint($sesNaOverWrite."\n",1);#Print on the log file
if ( ($sesNaOverWrite eq "y") or ($sesNaOverWrite eq "Y") ){
my $sure= get_input("Are you sure?(y or n)",'^[YynN]$');
annoPrint($sure."\n",1);#Print on the log file
if ( ( $sure eq "y") or ( $sure eq "Y")){
remove_tree($sessionFolder."/".$session);
$ok = 1;
}else { annoPrint( "Ok, choose another name. But don't let Annocript waste its time...\n");}
}else { annoPrint("Ok, choose another name. But don't let Annocript waste its time...\n");}
#If the user wants to choose a new name
if($ok == 0){
annoPrint ("Choose a name for your session: ");
$session = <STDIN>;
chomp $session;
}
}
#Assigns the path to a global variable
$sessionFolder = $sessionFolder."/".$session;
#Creates the folder
mkdir $sessionFolder or annoDie("ERROR: Unable to create the directory... please check your permissions.Exiting..\n");
annoPrint ($sessionFolder." created!\n");
return $session;
}
=head2 get_input
Title : get_input
Usage : get_input( - sentence: a sentence that will printed in input to ask something to the user;
- regex: what the answer of the user have to respect
);
Function: Takes in input a sentence and a regex. Asks to the user the sentence and controls its input with regex
Returns : input given by the user
=cut
sub get_input{
my $sentence = shift;
my $regex = shift;
my $input='';
while (!($input =~ /$regex/) or ($input eq "")){
annoPrint ($sentence." ");
$input = <STDIN>;
chomp $input;
annoPrint ("\n");
}
return $input;
}
=head2 configFile2Hash
Title : configFile2Hash
Usage : configFile2Hash( - configFilePath = path of the config file
);
Function: gets the hash table with all the path and names in input from the config file in input
Returns : nothing
=cut
sub configFile2Hash{
my $configFilePath=shift;
my $start = 0;
#Here we open config file and read all its line to find elements belonging to each of the executers
open (configFile,$configFilePath) or annoDie("ERROR: The file $configFilePath doesn't exists. Annocript will exit..\n");
while (my $line = <configFile>){
if ($line =~ /#########/){
$start = 1;
}
if( ($line =~ /(\S+)\s*=\s*(\S+)/) and ($start == 1) and !($line =~ /#/) ){
$configHash->{$1} = $2;
#annoPrint ("$1 = $2\n") ;#DEBUGCODE
}
}
close(configFile);
#annoPrint (Dumper\$configHash); #DEBUGCODE
}
=head2 completeConfiguration
Title : completeConfiguration
Usage : completeConfiguration( - configFilePath = path of the config file
);
Function: this subroutine completes the configuration of annocript:
- checks the variables with YES, NO values
- creates needed folders
- check variables related to the three modules (DB_CREATOR, PROGRAMS_EXEC, GFF3_AND_OUTPUT)
- build some important variables in the configuration hash
Returns : nothing
=cut
sub completeConfiguration{
#Here I put a control of the variables in the configuration file that can be YES or NO