-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcassis.pl
2348 lines (2128 loc) · 80.3 KB
/
cassis.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
# TODO option to provide a set of genes as "guidance" for the cluster prediction
##########################################
# ..::| C A S S I S |::.. #
# #
# Cluster ASSignment by Islands of Sites #
##########################################
# Copyright (C) 2015 Leibniz Institute for Natural Product Research and
# Infection Biology -- Hans-Knoell-Institute (HKI)
#
# 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/>.
#
# Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
# der GNU General Public License, wie von der Free Software Foundation,
# Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
# veröffentlichten Version, weiterverbreiten und/oder modifizieren.
#
# Dieses Programm wird in der Hoffnung, dass es nützlich sein wird, aber
# OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
# Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
# Siehe die GNU General Public License für weitere Details.
#
# Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
# Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
#
# Contact: [email protected], [email protected]
#
# Cite: If you use CASSIS, please cite https://doi.org/10.1093/bioinformatics/btv713
use 5.14.2;
use strict;
use warnings;
use Getopt::Long; # handle command line arguments
use File::Path qw(make_path); # create directories (and SUBdirectories!)
use File::Basename; # parse file paths into directory, filename and suffix
use File::Copy::Recursive qw(dircopy);
use File::Copy;
use File::Compare; # compare files --> check if same file already present
use Roman; # conversion between roman and arabic numbers
use Bio::SeqIO; # handle sequences in fasta format
use List::AllUtils qw(sum max min any none uniq);
use IPC::System::Simple qw(system); # improved behavior of Perl's "system()" call
use File::Which; # emulation of "which" command line tool
use Sys::CpuAffinity; # parallelization
use Parallel::ForkManager; # parallelization
use Text::CSV; # read and write csv files
use Text::Autoformat; # wrap text automatically (especially helpful for usage text)
# debug
#use Data::Dumper;
#$Data::Dumper::Terse = 1;
#use Devel::Size qw(size total_size);
#########
# usage #
#########
sub usage
{
my $usage = autoformat(
"Copyright (C) 2015 Leibniz Institute for Natural Product Research and Infection Biology -- Hans-Knoell-Institute (HKI).\n\n"
. "This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the file COPYING, which you should have received along with this program, for details.\n\n"
. " ###########################################################\n"
. " # CASSIS - (C)luster (ASS)ignment by (I)slands of (S)ites #\n"
. " ###########################################################\n\n"
. "CASSIS is a tool to precisely predict secondary metabolite (SM) gene clusters around a given anchor (or backbone) gene. Genes encoding SMs tend to be clustered. Gene clusters are small groups of normally up to 20 genes; tightly co-localized, co-regulated, and participating in the same metabolic pathway. The predictions are based on transcription factor binding sites shared by promoter sequences of the putative cluster genes.\n\n"
. "\n"
. "usage: cassis.pl <parameters>\n\n" . "\n"
. "required parameters:\n\n"
. " --annotation, -a <file>\n\n"
. " Genome annotation file. A simple text file in tabular format with at least five columns. These are:\n\n"
. " gene <string> | contig <string> | start position <int> | stop position <int> | strand <+ or->.\n\n"
. " The column separator must be tab (\\t). The annotation file must be sorted ascending by contig, start and stop. Start positions must be smaller than stop positions. Contig names have to coincide with the genome sequence file.\n\n"
. " --genome, -g <file>\n\n"
. " Genomic sequence file. A multiFASTA file containing the DNA sequences of all contigs of the species. Contig names have to coincide with the annotation file.\n\n"
. "\n"
. "optional parameters:\n\n"
. " --anchor, -b <ID>\n\n"
. " Feature ID of the clusters anchor gene. The ID has to coincide with the annotation file. This gene will be the starting point of the cluster prediction.\n\n"
. " --cluster, -c <name>\n\n"
. " Name of the gene cluster to predict. Creates a sub-directory with the given name to separate predictions for different clusters but same species (in the same working directory). Will be set to the cluster's anchor gene ID, if ommitted.\n\n"
. " --dir, -d <directory>\n\n"
. " Working directory. All directories and files generated by CASSIS will go in here. Choosing different directories for e.g. different species might be a good idea.\n\n"
. " --fimo, -f [<p-value cut-off>]\n\n"
. " Enables the motif search with FIMO. Setting a maximum p-value, which will restrict the number of binding sites found by FIMO, is optional. If no p-value cut-off is specied, the default value of 0.00006 will be used. To run the motiv search, the MEME suite must be installed on your system. See http://meme-suite.org/doc/download.html\n\n"
. " --frequency, -fq <frequency cut-off between 0 and 100>\n\n"
. " By default, CASSIS will skip the cluster prediction for a certain motif, if this motif results in a number of binding sites found in more than 14 % of all promoters in the genome. By changing this paramater you may set a different frequency cut-off.\n\n"
. " --gap-length, -gl <0-5>\n\n"
. " Sets the maximum number of promoters in a row WITHOUT binding sites, which are allowed inside a cluster prediction. Allowed are 0, 1, 2, 3, 4, and 5. The default value is 2. Only applies if the cluster prediction has been enabled (see parameter --prediction).\n\n"
. " --help, -h\n\n"
. " Show the help page, which you are currently reading.\n\n"
. " --meme, -m [<e-value cut-off>]\n\n"
. " Enables the motif prediction around the anchor gene with MEME. Setting a maximum e-value for found motifs is optional. MEME will stop, if it reaches the e-value, regardless if any motif has been found or not. If no e-value cut-off is specified, the default value of 1.0e+005 will be used. The cut-off -1 has a special meaning: This means NO cut-off and MEME will not stop until it finds at least one motif, regardless of its e-value. To run the motiv prediction, the MEME suite must be installed on your system. See http://meme-suite.org/doc/download.html\n\n"
. " --mismatches, -mm <allowed mismatches>\n\n"
. " Setting a maximum number of allowed mismatches (0-3) per binding site sequence is optional. The default is 0. See --sitar.\n\n"
. " --num-cpus, -n <number>\n\n"
. " Set number of CPUs (or CPU cores) to use. The motif prediction (parameter --meme) and the motif search (parameter --fimo) steps will then run with multiple forks in parallel. CASSIS uses only one CPU by default.\n\n"
. " --prediction, -p\n\n"
. " Enables the cluster prediction, including the motif prediction via paramter --meme and the motif search via parameter --fimo. By default it's disabled.\n\n"
. " --sitar, -s <file>\n\n"
. " Alternative to MEME and FIMO: Enables the motif search with SiTaR. You have to provide a file in (multi) FASTA format with binding site sequences of at least one transcription factor.\n\n"
. " --verbose, -v\n\n"
. " By default, only the most important information is printed. Use this parameter if you prefer a more verbose output.\n\n" . "\n"
. "runtime (Intel Xeon, running at 2.7 GHz):\n\n"
. " Using 2 CPUs (via --num-cpus), predicting the cluster to a given anchor gene takes about 40 min. Using 4 CPUs, it takes about 22 min. And using 60 CPUs, about 3 min.",
{ justify => "full", all => 1 }
)
. "\n\n"
. "command-line example:" . "\n"
. "cassis.pl --dir fungi/fumigatus/ --annotation fungi/fumigatus/A_fumigatus_Af293_version_s03-m04-r22_features.csv --genome fungi/fumigatus/A_fumigatus_Af293_version_s03-m04-r22_chromosomes.fasta --anchor Afu6g09660 --cluster Gliotoxin --meme 1.0e+005 --fimo 0.00006 -frequency 14 --prediction --gap-length 2 --num-cpus 2 --verbose\n"
. "\n\n"
. "Contact: gianni.panagiotou\@leibniz-hki.de, thomas.wolf\@leibniz-hki.de"
. "\n\n"
. "Cite: If you use CASSIS, please cite https://doi.org/10.1093/bioinformatics/btv713";
# print README to file
open( my $readme, ">", "README" ) or die "Cannot write to file \"./README\".\n", $!;
say $readme $usage;
close $readme;
# and show it on the screen
if ( which("tty") )
{
if ( system( "tty", "-s" ) == 0 ) # check if stdin is a terminal
{
system( "less", "README" );
}
else
{
system( "cat", "README" );
}
}
else # no tty --> possibly running on windows
{
system("type README");
}
exit;
}
##########################
# Command line arguments #
##########################
sub command_line_arguments { } # the only purpuse of such empty subs is to create an entry in Eclipse' outline view for faster navigation
my $verbose = 0; # print a lot of information (1) or only a minimum (0)
my $dir = ""; # working directory --> organism
my $annotation_file = "";
my $genome_file = "";
my $cluster_prediction = 0; # enable cluster prediction yes/no? --> default: no
my $max_gap_length = 2; # maxium gap length allowed in cluster predictions --> default: 2
my $meme; # predict motifs around anchor gene with MEME (e-value cut-off)?
my @meme_parameters = qw(-dna -nostatus -mod anr -nmotifs 1 -minw 6 -maxw 12 -revcomp);
my $fimo; # compute frame scores based on FIMO results (p-value cut-off)?
my $sitar; # compute frame scores based on SiTaR results?
my $mismatches; # maximum number of allowed mismatches, if applying SiTaR instead of MEME and FIMO
my $frequency_cutoff = 14; # binding site frequency cut-off --> default: 14 (determinded by parameter estimation of set of known gene clusters)
my $backbone_id; # gene ID of the clusters anchor/backbone protein (NRPS, PKS, …)
my $cluster_name; # name of the SM gene cluster (aka subfolder)
my $num_cpus = 1; # number of CPUs to use --> default: 1 (CPU-) core
my $help; # display help for command line arguments?
# no command line arguments specified at all?
usage() if !@ARGV;
# save whole command line
my $commandline = join( " ", ( fileparse($0) )[0], @ARGV );
# read arguments from command line
GetOptions(
"dir|d=s" => \$dir,
"annotation|a=s" => \$annotation_file,
"genome|g=s" => \$genome_file,
"meme|m:s" => \$meme,
"anchor|b=s" => \$backbone_id,
"cluster|c=s" => \$cluster_name,
"fimo|f:s" => \$fimo,
"sitar|s=s" => \$sitar,
"mismatches|mm=i" => \$mismatches,
"frequency|fq=i" => \$frequency_cutoff,
"prediction|p" => \$cluster_prediction,
"gap-length|gl=i" => \$max_gap_length,
"num-cpus|n=i" => \$num_cpus,
"verbose|v" => \$verbose,
"help|h" => \$help
) or die "[ERROR] Please check your command line arguments.\nStopped";
# or asked for help?
usage() if $help;
say "(0) " . localtime . " - SETTINGS …";
# check ANNOTATION argument
if ( $annotation_file =~ m/^([-\w\.\/\+]+)$/ and -s $1 )
{
$annotation_file = $1;
say "Genome annotation file: $annotation_file";
}
elsif ( !$annotation_file )
{
die "[ERROR] Please specify an ANNOTATION file via the --annotation or -a argument.\nStopped";
}
else
{
die "[ERROR] in ANNOTATION (--annotation, -a) argument \"$annotation_file\".\nStopped";
}
# check GENOME argument
if ( $genome_file =~ m/^([-\w\.\/\+]+)$/ and -s $1 )
{
$genome_file = $1;
say "Genomic sequence file: $genome_file";
}
elsif ( !$genome_file )
{
die "[ERROR] Please specify a GENOME file via the --genome or -g argument.\nStopped";
}
else
{
die "[ERROR] in GENOME (--genome, -g) argument \"$genome_file\".\nStopped";
}
# check DIR argument
if ($dir)
{
if ( $dir =~ m/^([-\w\.\/\+]+)$/ and -d $1 )
{
$dir = $1;
$dir .= "/" if ( substr( $dir, -1, 1 ) ne "/" );
make_path($dir);
say "Working directory: $dir";
}
else
{
die "[ERROR] in DIR (--dir, -d) argument \"$dir\".\nStopped";
}
}
elsif ( ( fileparse($annotation_file) )[1] eq ( fileparse($genome_file) )[1] )
{
$dir = ( fileparse($annotation_file) )[1]; # or $genome_file - doesn't matter, because they are the same
say "Working directory: $dir";
}
else
{
die
"[ERROR] Either make sure the annotation (--annotation, -a) and sequence file (--genome, -g) are located in the same directory or specify a working directory via --dir.\nStopped";
}
# check BACKBONE and CLUSTER argument
if ($backbone_id)
{
if ( $backbone_id =~ m/^([-\w\.:]+)$/ )
{
$backbone_id = $1;
say "Anchor gene: $backbone_id";
if ($cluster_name)
{
if ( $cluster_name =~ m/^([-\w\.:]+)$/ )
{
$cluster_name = $1;
say "Cluster name: $cluster_name";
}
else
{
die "[ERROR] in CLUSTER (--cluster, -c) argument \"$cluster_name\".\nStopped";
}
}
else
{
$cluster_name = $backbone_id;
}
}
else
{
die "[ERROR] in ANCHOR gene (--anchor, -b) argument \"$backbone_id\".\nStopped";
}
}
# check PREDICTION argument
if ($cluster_prediction)
{
$meme = "" if !defined $meme;
$fimo = "" if !defined $fimo;
say "Cluster prediction: yes";
}
else
{
say "Cluster prediction: no";
}
# check MOTIF SEARCH (FIMO/SiTaR) argument
my $fimo_exec = which("fimo"); # get location of fimo executable, may be undefined
my $sitar_exec = which("sitar"); # get location of sitar executable, may be undefined
if ( defined $fimo or defined $sitar )
{
if ( defined $sitar )
{
die "[ERROR] Please get a copy of SiTaR and add it to your PATH environment.\nStopped" if !$sitar_exec;
if ( $sitar =~ m/^([-\w\.\/\+]+)$/ and -s $1 )
{
# sitar overrides/deactivates meme and fimo settings
$meme = undef;
$fimo = undef;
$sitar = $1;
$mismatches = "" if !defined $mismatches;
say "SiTaR binding site file: $sitar";
}
elsif ( !$sitar )
{
die "[ERROR] Please specify a BINDING SITE file via the --sitar or -s argument.\nStopped";
}
else
{
die "[ERROR] in BINDING SITE (--sitar, -s) argument \"$sitar\".\nStopped";
}
if ( $mismatches eq "" )
{
$mismatches = 0;
say "Binding site search with: SiTaR, up to $mismatches mismatche(s) allowed (default)";
}
elsif ( $mismatches =~ m/^(\d)$/ and $1 >= 0 and $1 <= 3 )
{
$mismatches = $1;
say "Binding site search with: SiTaR, up to $mismatches mismatches allowed";
}
else
{
die "[ERROR] in SiTaR mismatch (--mismatches, -mm) value \"$mismatches\", which should be 0-3.\nStopped";
}
}
elsif ( defined $fimo )
{
die "[ERROR] Cannot find FIMO. Please install the MEME suite and add it to your PATH environment.\nStopped" if ( !$fimo_exec );
$meme = "" if ( !defined $meme );
if ( $fimo eq "" )
{
# default value determinded by parameter estimation of set of known gene clusters
$fimo = 0.00006;
say "Binding site search with: FIMO, p-value cut-off " . sprintf( "%e", $fimo ) . " (default)";
}
elsif ( $fimo =~ m/^(0\.\d+)\z/ )
{
$fimo = $1;
say "Binding site search with: FIMO, p-value cut-off " . sprintf( "%e", $fimo );
}
else
{
die "[ERROR] in FIMO p-value cut-off (--fimo, -f) argument \"$fimo\", which should rather look like e.g. \"0.0001\".\nStopped";
}
}
}
# check MOTIF PREDICTION (MEME) argument
my $meme_exec = which("meme"); # get location of meme executable, may be undefined
if ( defined $meme )
{
die "[ERROR] Cannot find MEME. Please install the MEME suite and add it to your PATH environment.\nStopped" if !$meme_exec;
die "[ERROR] Please specify an ANCHOR gene via --anchor, if you want to predict motifs around it with MEME.\nStopped" if !$backbone_id;
if ( $meme eq "" )
{
# default value determinded by parameter estimation of set of known gene clusters
$meme = "1.0e+005";
say "Motif prediction with: MEME, e-value cut-off 1.0e+005 (default)";
}
elsif ( $meme =~ m/(\d\.\de[+|-]\d{3})\z/ )
{
$meme = $1;
say "Motif prediction with: MEME, e-value cut-off $meme";
}
else
{
die "[ERROR] in MEME e-value cut-off (--meme, -m) argument \"$meme\", which should look like e.g. \"1.2e-003\".\nStopped";
}
push( @meme_parameters, "-evt", $meme );
}
# check BINDING SITE FREQUENCY argument
if ( defined $fimo )
{
if ( $frequency_cutoff =~ m/^(\d{1,2})$/ and $frequency_cutoff > 0 and $frequency_cutoff < 100 )
{
if ( $frequency_cutoff == 14 )
{
say "Binding site frequency cut-off: $frequency_cutoff % (default)";
}
else
{
say "Binding site frequency cut-off: $frequency_cutoff %";
}
$frequency_cutoff = $frequency_cutoff / 100;
}
else
{
die
"[ERROR] in binding site FREQUENCY cut-off (--frequency, -fq) argument, which must be greater than 0 and lower than 100, f.e. 20.\nStopped";
}
}
# check GAP LENGTH argument
if ($cluster_prediction)
{
if ( $max_gap_length =~ m/^(\d)$/ and $max_gap_length >= 0 and $max_gap_length <= 5 )
{
if ( $max_gap_length == 2 )
{
say "Maximum gap length: $max_gap_length (default)";
}
else
{
say "Maximum gap length: $max_gap_length";
}
}
else
{
die
"[ERROR] in GAP LENGTH (--gap-length, -gl) argument \"$max_gap_length\": Please specify a maximum gap length of either 0, 1, 2, 3, 4, or 5.\nStopped";
}
}
# check CPU argument (if it's not the default value)
# --> only necessary if we are going to run MEME or FIMO
if ( $meme or $fimo )
{
if ( $num_cpus != 1 )
{
die "[ERROR] in CPU (--num-cpus, -n) argument \"$num_cpus\": please specify a number greater than 0.\nStopped"
if ( $num_cpus < 1 ); # die if zero or negative
$num_cpus = Sys::CpuAffinity::getNumCpus() if ( $num_cpus > Sys::CpuAffinity::getNumCpus() ); # set to installed CPUs if greater
say "CPUs to use: $num_cpus";
}
else
{
say "CPUs to use: 1 (default, no parallelization)";
}
}
# print whole command line
say "\n" . "Command line summary:\n" . $commandline if $verbose;
# additional scipts should be in the same directory like the main script itselfubu
my $self_dir = ( fileparse(__FILE__) )[1];
require $self_dir . "cassis_meme.pl";
# made all checks --> set new and UNTAINTED path
delete @ENV{ "IFS", "CDPATH", "ENV", "BASH_ENV" }; # make %ENV safer
my @minimal_path = qw(/bin /usr/bin); # "select" minimal path
my @untainted;
# path to executable may be undefined, if not in original (potentially tainted) $PATH; maybe don't need that one this time
for ( grep { defined } ( $meme_exec, $fimo_exec, $sitar_exec ) ) # $R_exec
{
# remove executable (keep dir only) and trailing "/" to match usual style in $PATH variables
if ( $_ =~ m/^(\/[-\w\/]+)\/[-\w\.]+$/ )
{
push( @untainted, $1 );
}
else
{
die "[ERROR] \"$_\" seems to be an insecure path.\nStopped";
}
}
# prevent multiple occurrences of same path
$ENV{PATH} = join( ":", uniq( @minimal_path, @untainted ) );
#########################
# Additional parameters #
#########################
# assumed start and end of promotors related to TSS (+1)
my $upstream_tss = 1000; # nucleotides upstream TSS
my $downstream_tss = 50; # nucleotides downstream TSS
my $complete_intergenic = 0; # use complete intergenic region --> ignore $upstream_tss (but keep $downstream_tss)
#####################
# Genome annotation #
#####################
sub genome_annotation { }
say "\n(1) Processing genome ANNOTATION …";
my @features; # {ID, first, last, strand, contig}
my $csv = Text::CSV->new(
{
allow_loose_quotes => 1, # bad practice in csv format (blame creator of the input file?)
allow_loose_escapes => 1, # bad practice in csv format (blame creator of the input file?)
sep_char => "\t",
eol => $/,
binary => 1
}
);
# open annotation file
open( my $annotation_io, "<", $annotation_file ) or die "Cannot read from genome annotation file \"$annotation_file\".\n", $!;
# analyse each line of the annotation file
while ( my $row = $csv->getline($annotation_io) )
{
# jump to next line if current line is:
# - empty
# - comment (! or #)
next if ( "@$row" eq "" or any { index( $row->[0], $_ ) == 0 } ( "!", "#" ) );
# gene <string> | contig <string> | start position <string> | end position <int> | strand <+ or-> | … ### taken right from usage string
my $ID = $row->[0];
my $contig_number = $row->[1];
my $first = $row->[2];
my $last = $row->[3];
my $strand = $row->[4]; # W = +; C = -
$strand =~ s/\r$//; # remove trailing carriage return (if present) --> necessary to correctly handle windows newline encoding "\r\n"
die "[ERROR] Invalid whitespace in contig string \"$contig_number\".\nStopped" if ( index( $contig_number, " " ) != -1 );
die "[ERROR] No gene name in line \"@$row\".\nStopped" if ( !$ID );
die "[ERROR] No contig name in line \"@$row\".\nStopped" if ( !$contig_number );
die "[ERROR] No gene start position in line \"@$row\".\nStopped" if ( !$first and $first != 0 ); # 0 is false, but still a valid start pos
die "[ERROR] No gene stop position in line \"@$row\".\nStopped" if ( !$last );
die "[ERROR] No strand information in line \"@$row\".\nStopped" if ( !$strand );
die "[ERROR] Start position is not an positive integer in line \"@$row\".\nStopped" if ( $first !~ /^\d+$/ );
die "[ERROR] Stop position is not an positive integer in line \"@$row\".\nStopped" if ( $last !~ /^\d+$/ );
die "[ERROR] Start position identical to stop position in line \"@$row\".\nStopped" if ( $first == $last );
die "[ERROR] Start position AFTER stop position in line \"@$row\".\nStopped" if ( $first > $last and $strand eq "+" );
( $first, $last ) = ( $last, $first ) if ( $first > $last ); # switch start/stop position
push( @features, { ID => $ID, first => $first, last => $last, strand => $strand, contig => $contig_number } );
}
if ( !$csv->eof )
{
$csv->error_diag;
die $csv->error_input;
}
close $annotation_io;
die "There is a problem with the annotation file.\nStopped" if ( @features == 0 );
die "Anchor $backbone_id doesn't appear in annotation file \"$annotation_file\".\nStopped"
if ( $backbone_id and none { $_->{ID} eq $backbone_id } @features );
say "\"$annotation_file\"";
say "--> " . @features . " features";
###################
# Genome sequence #
###################
sub genome_sequence { }
say "\n(2) Processing genomic SEQUENCE …";
my %sequences; # contig => sequence
my $sequence_io = Bio::SeqIO->new( -file => $genome_file );
while ( my $sequence = $sequence_io->next_seq() )
{
my $contig_id = $sequence->display_id();
die "Looks like there is more then one contig $contig_id. \""
. $sequence->display_id()
. "\" looks like another incarnation of the already processed contig number $contig_id, at least to me.\nStopped"
if exists $sequences{$contig_id};
$sequences{$contig_id} = lc( $sequence->seq() );
}
say "\"$genome_file\"";
say "--> " . keys(%sequences) . " contigs";
#############
# Promoters #
#############
sub promoters { }
say "\n(3) Processing PROMOTERS …";
my @promoters; # {ID, first, last, contig}
my $backbone_promoter_nr; # number of promoter of backbone locus
my $backbone_contig; # yet unkown, if genome-wide
my @faulty_promoters_short; # to console
my @faulty_promoters_long; # to file
my $contig_number;
my $contig_sequence;
my $contig_length;
my $promoter_sequences_file = $dir . "PROMOTERS/all_promoter_sequences.fasta";
my $promoter_positions_file = $dir . "PROMOTERS/all_promoter_positions.csv";
my $skip = 0; # indicator for "special case 9"
# extract promoters from the genome sequence
make_path( ( fileparse($promoter_sequences_file) )[1] );
open( my $promoter_sequences_io, ">", $promoter_sequences_file )
or die "Cannot write to promoter sequences file \"$promoter_sequences_file\".\n", $!;
open( my $promoter_positions_io, ">", $promoter_positions_file )
or die "Cannot write to promoter positions file \"$promoter_positions_file\".\n", $!;
say "Writing sequences to \"$promoter_sequences_file\"";
say "Writing positions to \"$promoter_positions_file\"";
say $promoter_positions_io join( "\t", "#", "promoter", "start", "end", "length", "contig" ); # print head to positions file
# compute borders of the promoter for each locus
for ( 0 .. $#features )
{
$contig_number = $features[$_]{contig};
$contig_sequence = \$sequences{$contig_number};
if ( !$sequences{$contig_number} )
{
die "[ERROR] Contig \"$contig_number\" (gene $features[$_]{ID}) does not occur in the genome sequence file \"$genome_file\".\nStopped";
}
else
{
$contig_length = length( $sequences{$contig_number} );
}
# promoter region = complete intergenic region
if ($complete_intergenic)
{
if ( $_ == 0 and $features[$_]{strand} eq "+" ) # first gene in the contig
{
$upstream_tss = $features[$_]{first} - 1;
}
elsif ( $_ == $#features and $features[$_]{strand} eq "-" ) # last gene in contig
{
$upstream_tss = $contig_length - $features[$_]{last} - 1;
}
elsif ( $features[$_]{strand} eq "-" )
{
$upstream_tss = $features[ $_ + 1 ]{first} - $features[$_]{last} - 1;
}
else
{
$upstream_tss = $features[$_]{first} - $features[ $_ - 1 ]{last} - 1;
}
}
# two genes share the same promotor > special case 9
# did computation with first gene > skip second gene
if ($skip)
{
$skip = 0;
}
# only one locus within current contig
elsif (
( $_ == 0 and $contig_number ne $features[ $_ + 1 ]{contig} ) # first locus
or ( $_ == $#features
and $contig_number ne $features[ $_ - 1 ]{contig} ) # last locus
or ( $contig_number ne $features[ $_ - 1 ]{contig}
and $contig_number ne $features[ $_ + 1 ]{contig} )
) # elsewhere
{
if ( $features[$_]{strand} eq "+" )
{
if ( ( ( $features[$_]{first} - $upstream_tss ) >= 0 )
and ( $features[$_]{last} > $features[$_]{first} + $downstream_tss ) )
{ # 1
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{first} - $upstream_tss,
last => $features[$_]{first} + $downstream_tss
}
);
}
elsif ( ( ( $features[$_]{first} - $upstream_tss ) < 0 )
and ( $features[$_]{last} > $features[$_]{first} + $downstream_tss ) )
{ # 2
push(
@promoters,
{
ID => $features[$_]{ID},
first => 1,
last => $features[$_]{first} + $downstream_tss
}
);
}
elsif ( ( ( $features[$_]{first} - $upstream_tss ) >= 0 )
and ( $features[$_]{first} + $downstream_tss >= $features[$_]{last} ) )
{ # 3
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{first} - $upstream_tss,
last => $features[$_]{last}
}
);
}
elsif ( ( ( $features[$_]{first} - $upstream_tss ) < 0 )
and ( $features[$_]{first} + $downstream_tss >= $features[$_]{last} ) )
{ # 7
push(
@promoters,
{
ID => $features[$_]{ID},
first => 1,
last => $features[$_]{last}
}
);
}
else
{
die "Problem with promoter $features[$_]{ID}.\nStopped";
}
}
elsif ( $features[$_]{strand} eq "-" )
{
if ( ( $features[$_]{first} < $features[$_]{last} - $downstream_tss )
and ( ( $features[$_]{last} + $upstream_tss ) <= $contig_length ) )
{ # 4
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{last} - $downstream_tss,
last => $features[$_]{last} + $upstream_tss
}
);
}
elsif ( ( $features[$_]{first} < $features[$_]{last} - $downstream_tss )
and ( ( $features[$_]{last} + $upstream_tss ) > $contig_length ) )
{ # 5
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{last} - $downstream_tss,
last => $contig_length
}
);
}
elsif ( ( $features[$_]{first} >= $features[$_]{last} - $downstream_tss )
and ( ( $features[$_]{last} + $upstream_tss ) <= $contig_length ) )
{ # 6
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{first},
last => $features[$_]{last} + $upstream_tss
}
);
}
elsif ( ( $features[ $_ + 1 ]{first} >= $features[$_]{last} - $upstream_tss )
and ( ( $features[$_]{last} + $upstream_tss ) > $contig_length ) )
{ # 8
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{first},
last => $contig_length
}
);
}
else
{
die "Problem with promoter $features[$_]{ID}.\nStopped";
}
}
else
{
die "Strand-problem at $features[$_]{ID}.\nStopped";
}
}
# first gene of the contig
# AND NOT special case 9
# [ $_ -1 ] not present (first gene of genome) or
# current contig number differs from last contig number
elsif (
( $_ == 0 or $contig_number ne $features[ $_ - 1 ]{contig} and $_ != $#features )
and !(
( ( $features[$_]{strand} eq "-" ) and ( $features[ $_ + 1 ]{strand} eq "+" ) )
and ( $features[$_]{last} + $upstream_tss >= $features[ $_ + 1 ]{first} - $upstream_tss )
)
)
{
if ( $features[$_]{strand} eq "+" )
{
if ( ( ( $features[$_]{first} - $upstream_tss ) >= 0 )
and ( $features[$_]{last} > $features[$_]{first} + $downstream_tss ) )
{ # 1
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{first} - $upstream_tss,
last => $features[$_]{first} + $downstream_tss
}
);
}
elsif ( ( ( $features[$_]{first} - $upstream_tss ) < 0 )
and ( $features[$_]{last} > $features[$_]{first} + $downstream_tss ) )
{ # 2
push(
@promoters,
{
ID => $features[$_]{ID},
first => 1,
last => $features[$_]{first} + $downstream_tss
}
);
}
elsif ( ( ( $features[$_]{first} - $upstream_tss ) >= 0 )
and ( $features[$_]{first} + $downstream_tss >= $features[$_]{last} ) )
{ # 3
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{first} - $upstream_tss,
last => $features[$_]{last}
}
);
}
elsif ( ( ( $features[$_]{first} - $upstream_tss ) < 0 )
and ( $features[$_]{first} + $downstream_tss >= $features[$_]{last} ) )
{ # 7
push(
@promoters,
{
ID => $features[$_]{ID},
first => 1,
last => $features[$_]{last}
}
);
}
else
{
die "Problem with promoter $features[$_]{ID}.\nStopped";
}
}
elsif ( $features[$_]{strand} eq "-" )
{
if ( ( $features[$_]{first} < $features[$_]{last} - $downstream_tss )
and ( $features[ $_ + 1 ]{first} > $features[$_]{last} + $upstream_tss ) )
{ # 4
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{last} - $downstream_tss,
last => $features[$_]{last} + $upstream_tss
}
);
}
elsif ( ( $features[$_]{first} < $features[$_]{last} - $downstream_tss )
and ( $features[ $_ + 1 ]{first} <= $features[$_]{last} + $upstream_tss ) )
{ # 5
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{last} - $downstream_tss,
last => $features[ $_ + 1 ]{first} - 1
}
);
}
elsif ( ( $features[$_]{first} >= $features[$_]{last} - $downstream_tss )
and ( $features[ $_ + 1 ]{first} > $features[$_]{last} + $upstream_tss ) )
{ # 6
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{first},
last => $features[$_]{last} + $upstream_tss
}
);
}
elsif ( ( $features[ $_ + 1 ]{first} <= $features[$_]{last} + $upstream_tss )
and ( $features[$_]{first} >= $features[$_]{last} - $downstream_tss ) )
{ # 8
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{first},
last => $features[ $_ + 1 ]{first} - 1
}
);
}
else
{
die "Problem with promoter $features[$_]{ID}.\nStopped";
}
}
else
{
die "Strand-problem at $features[$_]{ID}.\nStopped";
}
}
# last gene of contig
# [ $_ +1 ] not present (last gene of genome) or
# current contig number differs from next contig number
elsif ( ( $_ == $#features or $contig_number ne $features[ $_ + 1 ]{contig} )
and !$skip )
{
if ( $features[$_]{strand} eq "+" )
{
if ( ( $features[ $_ - 1 ]{last} < $features[$_]{first} - $upstream_tss )
and ( $features[$_]{last} > $features[$_]{first} + $downstream_tss ) )
{ # 1
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{first} - $upstream_tss,
last => $features[$_]{first} + $downstream_tss
}
);
}
elsif ( ( $features[ $_ - 1 ]{last} >= $features[$_]{first} - $upstream_tss )
and ( $features[$_]{last} > $features[$_]{first} + $downstream_tss ) )
{ # 2
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[ $_ - 1 ]{last} + 1,
last => $features[$_]{first} + $downstream_tss
}
);
}
elsif ( ( $features[ $_ - 1 ]{last} < $features[$_]{first} - $upstream_tss )
and ( $features[$_]{first} + $downstream_tss >= $features[$_]{last} ) )
{ # 3
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{first} - $upstream_tss,
last => $features[$_]{last}
}
);
}
elsif ( ( $features[ $_ - 1 ]{last} >= $features[$_]{first} - $upstream_tss )
and ( $features[$_]{first} + $downstream_tss >= $features[$_]{last} ) )
{ # 7
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[ $_ - 1 ]{last} + 1,
last => $features[$_]{last}
}
);
}
else
{
die "Problem with promoter $features[$_]{ID}.\nStopped";
}
}
elsif ( $features[$_]{strand} eq "-" )
{
if ( ( $features[$_]{first} < $features[$_]{last} - $downstream_tss )
and ( ( $features[$_]{last} + $upstream_tss ) <= $contig_length ) )
{ # 4
push(
@promoters,
{
ID => $features[$_]{ID},
first => $features[$_]{last} - $downstream_tss,
last => $features[$_]{last} + $upstream_tss
}
);
}
elsif ( ( $features[$_]{first} < $features[$_]{last} - $downstream_tss )
and ( ( $features[$_]{last} + $upstream_tss ) > $contig_length ) )
{ # 5
push(
@promoters,