-
Notifications
You must be signed in to change notification settings - Fork 17
/
autoInstall.pl
1668 lines (1469 loc) · 60.1 KB
/
autoInstall.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/env perl
# autoInstaller for lotus
# Copyright (C) 2014 Falk Hildebrand, Joachim Fritscher
### use "perl autoInstall.pl -condaDBinstall -lambdaIndex" to install LotuS2 on Galaxy server
#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/>.
# contact
# ------
# Falk.Hildebrand [at] gmail.com
#
use strict;
use warnings;
use Getopt::Long qw( GetOptions );
use Cwd 'abs_path';
use Net::FTP;
sub addInfoLtS;sub finishAI;
#subroutines to download various DBs..
sub getGG; sub getGG2; sub getSLV;sub getHITdb; sub getPR2db;sub getKSGP;sub getbeetax;
sub buildIndex;
sub get_DBs;
sub getS2;
sub getInfoLtS;
sub getInstallVer;
sub compile_sdm;
sub compile_LCA;
sub compile_rtk;
sub checkLtsVer;
sub check_exists;
sub check_version;
sub user_options;
my $FILEfetch = eval{
require File::Fetch;
File::Fetch->import();
1;
};
use File::Copy qw(move);
my $LWPsimple = eval{
require LWP::Simple;
LWP::Simple->import();
1;
};
my $WGETpres=1;
#if (`command -v wget` eq ""){
if (`whereis wget` eq ""){
$WGETpres=0;
}
my $forceUpdate=0;
my $condaDBinstall=0;
my $downloadLmbdIdx = 0; #download lambda index from webpage
my $compile_lambda=0;
if (@ARGV > 0 && $ARGV[0] eq "-forceUpdate"){$forceUpdate=1};
if (@ARGV > 0 && $ARGV[0] eq "-condaDBinstall"){$condaDBinstall=1};
if (@ARGV > 1 && $ARGV[1] eq "-downloadLmbdIdx" || $ARGV[0] eq "-downloadLmbdIdx"){$downloadLmbdIdx=1};
if (@ARGV > 1 && ($ARGV[1] eq "-lambdaIndex" || $ARGV[0] eq "-lambdaIndex")){$compile_lambda=1};
if ($compile_lambda && $downloadLmbdIdx){
die "Can't use both -lambdaIndex and -downloadLmbdIdx arguments together\nAborting..\n";
}
#die "$ARGV[0] $forceUpdate\n";
my $isMac = 1;
if ($^O =~ m/linux/ || $^O =~ m/MSWin/){
$isMac = 0;
}
my $ldir = abs_path($0);
$ldir =~ s/[^\/]*$//;
#die ($ldir."\n");
my $bdir = $ldir."/bin/";
my $ddir = $ldir."/DB/";
my $finalWarning="";
my $onlyDbinstall = 0;
#options on programs to install..
my $installBlast = 2; my @refDBinstall = 0 x 10; my $ITSready = 1;my $getUTAX=1;$refDBinstall[8]=1;
#DEBUG
#get_programs();die;
#@refDBinstall = 0 x 10; $ITSready = 1;$getUTAX = 0; get_DBs();die;
#autoinstaller, test if install was done before
my @txt; my $mainCfg = "$ldir/lOTUs.cfg";
my $defCfg = "$ldir/configs/LotuS.cfg.def";
if (!-e $mainCfg ){
if ( -e $defCfg){
system "cp $defCfg $mainCfg";
} else {
die "Something wrong: can't find configs: $mainCfg and $defCfg";
}
}
open I,"<","$mainCfg" or die $!;
while (my $line = <I>){ push(@txt,$line);}
close I;
my $exe = ""; my $callret;
#print "$ldir/lOTUs.cfg";
my $UID = getInfoLtS("UID",\@txt);
my $uspath = getInfoLtS("usearch",\@txt);
#useach binary linking
my $usearchInstall ="";
if (@ARGV > 0 && $ARGV[0] eq "-link_usearch"){
$usearchInstall =$ARGV[1];
}
##### TESTING / DEBUG ##########
# @txt = getGG2(\@txt); @txt = getKSGP(\@txt);die;
###### GET USER OPTIONS ###################
my ($lver,$sver) = getInstallVer("");
if ($forceUpdate==0 && $condaDBinstall == 0){
print "\n\t####################################\n\t LotuS $lver Auto Installer script.\n\t####################################\n\n";
} elsif ($condaDBinstall){
print "\n\nConda Lotus Install: Downloading standarad DB set for LotuS2\n\n";
} else {
print "\n\nRerunning updates due to updated autoupdate.pl script\n\n";
}
user_options();
###### END GET USER OPTIONS ###################
#prepare dirs
#system("rm -rf $bdir");
mkdir $bdir unless (-d $bdir);
#system("rm -rf $ddir");
mkdir $ddir unless (-d $ddir);
($lver,$sver) = getInstallVer("$ldir/sdm_src");
#uparse pseudo
my $usearch_reset=0;
if (!-e $uspath){
$usearch_reset=1;
$exe = $bdir."usearch_bin";
@txt = addInfoLtS("usearch",$exe,\@txt,0);
}
#die "@txt\n";
######## get programs ####################
print "Several Software packages have to be downloaded and this can take some time. Please be patient & grab a tea.\n\n";
# Check if Rscript is installed
my $install_dada = 1;
if (check_exists('Rscript')) {
my $v = check_version('Rscript');
if ($v < 4) {
print("$0 requires Rscript version > 4.0.0\nThe found version is < 4.\nType\n \'c\' to install LotuS2 without dada2 and phyloseq\n \'t\' to continue and try to install phyloseq only\n \'a\' to abort installation process\n");
my $instr = <STDIN>;
chomp $instr;
if (lc($instr) eq "c") {
$install_dada = 0;
} elsif (lc($instr) eq "t") {
$install_dada = 1;
} else {
print("Abort installation.\n");
exit(0);
}
}
} else {
print("$0 requires Rscript (version > 4.0.0). No \"Rscript\" detected on current system.\n");
exit(0);
}
if ($UID eq "??"){
$UID=int(rand(999999999));
@txt = addInfoLtS("UID",$UID,\@txt,0);
}
################### database downloads ... #########################
get_DBs();
if ($condaDBinstall){
print "Finished LotuS2 DB install (Conda autointall)\nEnjoy LotuS2!\n";
exit(0)
}
################### R packages ... #########################
if ($install_dada) {
print("Install dada2 and other R packages \n");
my $r_output = `Rscript bin/R/autoInstall.R 2>&1`;
print($r_output);
}
################### prgrams downloads & installs... #########################
get_programs();
if ($onlyDbinstall){
finishAI("d");
print "\n\nInstalled databases\nExiting autoinstaller..\n";
exit(0);
}
#only binary installs after this point
#first install/compile sdm,LCA,rtk
my $nsdmp = compile_sdm("$ldir/sdm_src");
@txt = addInfoLtS("sdm",$nsdmp,\@txt,1);
$nsdmp = compile_LCA("$ldir/LCA_src");
@txt = addInfoLtS("LCA",$nsdmp,\@txt,1);
$nsdmp = compile_rtk("rtk_src");
@txt = addInfoLtS("rtk",$nsdmp,\@txt,1);
#rest of programs can be installed now
get_programs();
finishAI("");
print "\n\nInstallation script finished.\nPlease read the README for examples and references to proprietary software used in this pipeline.\n";
#After install on your system, open\n ".$ldir."lOTUs.cfg\nand search for the entry \"usearch {XX}\".\nReplace {XX} with the absolute path to your usearch install, e.g. /User/Thomas/bin/usearch/usearch7.0.1001_i86linux32\n LotuS is ready to run.\n";
sub finishAI($){
my ($vTag) = @_;
#write new cfg file
open O,">","$ldir/lOTUs.cfg" or die $!;
foreach (@txt){ print O $_;}
close O;
return if ($vTag eq "none");
if ($LWPsimple){
my $external_php = get("http://lotus2.earlham.ac.uk/lotus/in.php?ID=$UID&VERSION=$vTag$lver&SDMV=$sver") || print "";
}
if ($finalWarning ne ""){
print "################################\nWarnings occured during LotuS installation:\n".$finalWarning."\n################################\n";
}
}
sub getInstallVer($){
my ($sdmsrc) = @_;
my $lver=0.1;
system "chmod +x $ldir/lotus2";
open Q,"<","$ldir/./lotus2" or die("Can't find LotuS main script file (lotus2)\n");
while(<Q>){if (m/my.*selfID\s*=\s*\"LotuS\s(.*)\".*/){$lver=$1;last;}}
close Q;
my $sver=1.5;
if ($sdmsrc ne ""){
my $sdmF = "$sdmsrc/IO.h";
if (-e $sdmF){
open Q,"<",$sdmF or die("Can't open sdm file $sdmF\n");
#static const float sdm_version = 0.71f;
while(<Q>){if (m/static\s+const\s+float\s+sdm_version\s*=\s*(.*)f;/){$sver=$1;last;}}
close Q;
}
}
return ($lver,$sver);
}
sub addInfoLtS($ $ $ $){
my ($cmd,$ex,$aref,$reqF) = @_;
print "Installing $cmd:\n$ex\n";
if ($reqF ==1 && ! -f $ex){print "Can't find required file $ex\nPlease check if the package was correctly downloaded.\nAborting..\n"; exit(5);}
if ($reqF ==2 && ! -d $ex){print "Can't find required directory $ex\nPlease check if the package was correctly downloaded.\nAborting..\n"; exit(5);}
my @txt = @{$aref};
my $ss = quotemeta $cmd;
my $i=0; my $tagset=0;
while ($txt[$i] !~ m/^$ss\s/){
#print $txt[$i]."\n";
$i++;
if ($i >= @txt){
#die ("Could not find the entry \"$cmd\" in lotus configuration file. Aborting Installer..\n")
print "Could not find the entry \"$cmd\" in lotus configuration file. Inserting anew..\n";
push(@txt,""); last;
}
}
$txt[$i] = $cmd." ".$ex."\n";
$i++;
while ($i<@txt){ if ($txt[$i] =~ m/^$ss\s/){splice(@txt,$i,1) ; $i--;} $i++; last if ($i>=@txt); }
print "done.\n";
open O,">","$ldir/lOTUs.cfg" or die $!;
foreach (@txt){ print O $_;}
close O;
#DEBUG
#print $txt[$i]."\n";
return @txt;
}
sub getInfoLtS($ $){
my ($cmd,$aref) = @_;
my @txt = @{$aref};
my $ss = quotemeta $cmd;
my $i=0;
while ($txt[$i] !~ m/^$ss/){
# print $txt[$i]."\n";
$i++;
die ("Could not find the entry \"$cmd\" in lotus configuration file. Aborting Installer..\n") if ($i > @txt)
}
chomp $txt[$i];
if ($txt[$i] =~ m/^$ss\s(.*)/){
return $1;
} else {
return "??";
}
}
sub parse_hitdb($ $){
my ($Dpre,$Dn) = @_;
my @tdesign = (" k__"," p__"," c__"," o__"," f__"," g__"," s__");
open I,"<$Dpre"; open O,">$Dn";
while (my $l = <I>){
chomp $l;
my @spl = split /\t/,$l;
#print $spl[1]."\n";
my @spl2 = split /;/,$spl[1];
my $nline = "";
if ($spl2[0] =~ m/Euryarchaeota|Crenarchaeota/){
$nline = $spl[0]."\tk__Archaea;";
} else {
$nline = $spl[0]."\tk__Bacteria;";
}
for (my $i=1;$i<@tdesign;$i++){
if (@spl2 >= $i && $spl2[$i-1] ne ""){
my $tag = $spl2[$i-1]; chomp $tag;
$nline .= $tdesign[$i].$tag;
} else {
$nline .= $tdesign[$i]."?";
}
$nline .=";" unless ($i == (@tdesign-1));
}
print O $nline."\n";
}
close I; close O;
}
sub parse_PR2($ $){
my ($DBin, $tout) = @_;
open T,">$tout" or die "Can;t open PR2 taxout $tout\n";
open I,"<$DBin" or die "Can;t open PR2 fasta $DBin\n";
open F,">$DBin.tmp" or die "Can;t open PR2 fasta tmp $DBin.tmp\n";
while (my $l = <I>){
chomp $l;
if ($l =~ m/^>/){
my @spl = split /\|/,$l;
$spl[0] =~ s/^>//;
print F ">".$spl[0]."\n";
print T $spl[0]."\tk__".$spl[1].";p__".$spl[2].";c__".$spl[4].";o__".$spl[5].";f__".$spl[6].";g__".$spl[7].";s__".$spl[8]."\n";
} else {
$l =~ s/U/T/g;
$l =~ s/u/t/g;
$l =~ s/[^ACTGactg]/N/g;
print F $l."\n";
}
}
close T; close I; close F;
system "rm $DBin; mv $DBin.tmp $DBin";
}
sub buildIndex($){
my ($DBfna) = @_;
return unless ($compile_lambda);
my $lambdaIdxBin = "";#find where lambda is installed in
foreach my $line (@txt){
#change to lambda 3
#if ($line =~ m/^lambda_index\s+(\S+)/ ) {
# $lambdaIdxBin = $1;
#}
if ($line =~ m/^lambda3\s+(\S+)/ ) {
$lambdaIdxBin = $1;
}
}
my $BlastCores = 8; #just pick reasonable number
my $xtraLmbdI = "";
#my $cmdIdx = "$lambdaIdxBin -p blastn -t $BlastCores -d $DBfna $xtraLmbdI;";
my $cmdIdx = "$lambdaIdxBin mkindexn -t $BlastCores -d $DBfna ;\n";
my $gzipC = "gzip";
$gzipC = "pigz -p $BlastCores " if (`sh -c 'command -v pigz'`);
$cmdIdx .= "$gzipC $DBfna.lba;\n";
#system "touch $DBfna.dna5.fm.lf.drv.wtc.24;";
print "###################################\nCompiling lambda database for $DBfna using $BlastCores cores\n";
if ( system($cmdIdx) ) {
die "Could not compile index for $DBfna with call\n$cmdIdx\n\n";
} else {
print "Compile index for $DBfna \n\n";
}
}
sub getbeetax($){
my ($aref) = @_;
my @txt = @{$aref};
print "Downloading bee specific database and taxonomy.\n";
system "rm -rf $ddir/beeTax/;mkdir -p $ddir/beeTax/";
my $DB = "$ddir/beeTax/beeTax.fasta"; my $DBtax = "$ddir/beeTax/beeTax.txt";
#getS2("http://5.196.17.195/pr2/download/representative_sequence_of_each_cluster/gb203_pr2_all_10_28_99p.fasta.tar.gz",$DB.".tar.gz");
getS2("http://lotus2.earlham.ac.uk/lotus/packs/DB/beeTax_Engel/beEngel.fna",$DB);
getS2("http://lotus2.earlham.ac.uk/lotus/packs/DB/beeTax_Engel/beEngel.txt",$DBtax);
getS2("https://lotus2.earlham.ac.uk/lambdaDBs/v3.0/beeTax.fasta.lba.gz","$DB.lba.gz") if ($downloadLmbdIdx);
#parse_PR2($DB,$DBtax); #unlink ($DBtax.".pre");
@txt = addInfoLtS("TAX_REFDB_BEE",$DB,\@txt,1);
@txt = addInfoLtS("TAX_RANK_BEE",$DBtax,\@txt,1);
buildIndex($DB);
return (@txt);
}
sub getPR2db($){
my ($aref) = @_;
my @txt = @{$aref};
print "Downloading PR2 99% clustered database.\n";
system "rm -rf $ddir/PR2/;mkdir -p $ddir/PR2/";
my $DB = "$ddir/PR2/PR2_pack"; my $DBtax = "$ddir/PR2/PR2_taxonomy.txt";
#getS2("http://5.196.17.195/pr2/download/representative_sequence_of_each_cluster/gb203_pr2_all_10_28_99p.fasta.tar.gz",$DB.".tar.gz");
getS2("http://lotus2.earlham.ac.uk/lotus/packs/DB/gb203PR2.tar.gz",$DB.".tar.gz");
system "tar -xzf $DB.tar.gz -C $ddir/PR2;rm $DB.tar.gz";
getS2("https://lotus2.earlham.ac.uk/lambdaDBs/v3.0/gb203_pr2_all_10_28_99p.fasta.lba.gz","$ddir/PR2/gb203_pr2_all_10_28_99p.fasta.lba.gz") if ($downloadLmbdIdx);
$DB = "$ddir/PR2/gb203_pr2_all_10_28_99p.fasta";
#parse_PR2($DB,$DBtax); #unlink ($DBtax.".pre");
@txt = addInfoLtS("TAX_REFDB_PR2",$DB,\@txt,1);
@txt = addInfoLtS("TAX_RANK_PR2",$DBtax,\@txt,1);
buildIndex($DB);
return (@txt);
}
sub getHITdb($){
my ($aref) = @_;
my @txt = @{$aref};
print "Downloading HITdb April 2015 release..\n";
system "rm -rf $ddir/HITdb/; mkdir -p $ddir/HITdb/";
my $DB = "$ddir/HITdb/HITdb_sequences.fasta"; my $DBtax = "$ddir/HITdb/HITdb_taxonomy.txt";
getS2("http://lotus2.earlham.ac.uk/lotus/packs/hitdb/HITdb_sequences.fna",$DB);
getS2("http://lotus2.earlham.ac.uk/lotus/packs/hitdb/HITdb_taxonomy_qiime.txt",$DBtax.".pre");
getS2("https://lotus2.earlham.ac.uk/lambdaDBs/v3.0/HITdb_sequences.fasta.lba.gz","$DB.lba.gz") if ($downloadLmbdIdx);
parse_hitdb($DBtax.".pre",$DBtax); unlink ($DBtax.".pre");
@txt = addInfoLtS("TAX_REFDB_HITdb",$DB,\@txt,1);
@txt = addInfoLtS("TAX_RANK_HITdb",$DBtax,\@txt,1);
buildIndex($DB);
return (@txt);
}
sub getGG2($){
my ($aref) = @_;
my @txt = @{$aref};
#greengenes ------------------------
my $gg1 = "https://ksgp.earlham.ac.uk/downloads/greengenes2/GG2.2022.10.fasta.gz";
my $gg2 = "https://ksgp.earlham.ac.uk/downloads/greengenes2/GG2.2022.10.tax.gz";
my $DB = "$ddir/GG2.2022.10.fasta";
system "rm -f ${DB}*";
#system("wget -O $DB.gz $gg1");
print "Downloading GreenGenes2 2022 release..\n";
getS2($gg1,"$DB.gz");
getS2("https://lotus2.earlham.ac.uk/lambdaDBs/v3.0/GG2.2022.10.fasta.lba.gz","$DB.lba.gz") if ($downloadLmbdIdx);
sleep(10);
system("gunzip -c $DB.gz > $DB");
unlink("$DB.gz");
@txt = addInfoLtS("TAX_REFDB_GG2",$DB,\@txt,1);
buildIndex($DB);
$DB = "$ddir/GG2.2022.10.tax";
#system("wget -O $DB.gz $gg2");
getS2($gg2,"$DB.gz");
sleep(3);
system("gunzip -c $DB.gz > $DB");
unlink("$DB.gz");
@txt = addInfoLtS("TAX_RANK_GG2",$DB,\@txt,1);
return @txt;
}
sub getGG($){
my ($aref) = @_;
die "getGG::Greengenes is no longer supported\n";
my @txt = @{$aref};
#greengenes ------------------------
my $gg1 = "http://lotus2.earlham.ac.uk/lotus/packs/gg_13_5.fasta.gz";
my $gg2 = "http://lotus2.earlham.ac.uk/lotus/packs/gg_13_5_taxonomy.gz";
my $DB = "$ddir/gg_13_5.fasta";
system "rm -f ${DB}*";
#system("wget -O $DB.gz $gg1");
print "Downloading Greengenes may 2013 release..\n";
getS2($gg1,"$DB.gz");
sleep(10);
system("gunzip -c $DB.gz > $DB");
unlink("$DB.gz");
@txt = addInfoLtS("TAX_REFDB_GG",$DB,\@txt,1);
buildIndex($DB);
$DB = "$ddir/gg_13_5_taxonomy";
#system("wget -O $DB.gz $gg2");
getS2($gg2,"$DB.gz");
sleep(3);
system("gunzip -c $DB.gz > $DB");
unlink("$DB.gz");
@txt = addInfoLtS("TAX_RANK_GG",$DB,\@txt,1);
return @txt;
}
sub getKSGP($){
my ($aref) = @_;
my @txt = @{$aref};
my $DB = "$ddir/KSGP_v1.0";
system "rm -f ${DB}*";
print "Downloading KSGP 2023 release..\n";
my $tarUTN = "$ddir/KSGP.tar.gz";
getS2("https://ksgp.earlham.ac.uk/downloads/v1.0/KSGP_v1.0.tar.gz",$tarUTN);
getS2("https://ksgp.earlham.ac.uk/lambdaDBs/v3.0/KSGP_v1.0.fasta.lba.gz","$DB.fasta.lba.gz") if ($downloadLmbdIdx);
system "tar -xzf $tarUTN -C $ddir;rm -f $tarUTN";
@txt = addInfoLtS("TAX_RANK_KSGP","$DB.tax",\@txt,1);
@txt = addInfoLtS("TAX_REFDB_KSGP","$DB.fasta",\@txt,1);
buildIndex("$DB.fasta");
print "Added $DB.fasta and $DB.tax to lotus config.\n";
return @txt;
}
sub getSLV($){
my ($aref) = @_;
my @txt = @{$aref};
my $locSLBdl = 0;
#SILVA -----------------------------------
#TAX_REFDB_SLV TAX_REFDB_SLV
#changed to ver 119
#changed to 123
#changed to 128
#changed to 132
# my $baseSP = "http://www.arb-silva.de/fileadmin/silva_databases/release_123_1/Exports";
my $SLVver = "138.1";
#my $baseSP = "http://www.arb-silva.de/fileadmin/silva_databases/release_$SLVver/Exports";
my $baseSP = "https://ftp.arb-silva.de/release_$SLVver/Exports";
# my $baseSN = "SILVA_123.1";my $baseLN = "SLV_123.1"; my $SLVver = "123.1";
my $baseSN = "SILVA_$SLVver"; my $baseLN = "SLV_$SLVver";
my $DB2 = "$ddir/$baseLN"."_SSU.tax";
my $DB = "$ddir/$baseLN"."_SSU.fasta";
system "rm -f ${DB}*";
print "Downloading SILVA SSU release $SLVver..\n";
if ($locSLBdl){ #in case silva server doesn't work again..
$baseSP = "http://lotus2.earlham.ac.uk/lotus/packs/DB/SLV/";
#my $SlvAltFna = "http://lotus2.earlham.ac.uk/lotus/packs/DB/SLV/SLV_132_SSU.fasta.gz";
#getS2($SlvAltFna,"$DB.gz");
#system("gunzip -c $DB.gz > $DB;rm -f $DB.gz");
#my $SlvAltTax = "http://lotus2.earlham.ac.uk/lotus/packs/DB/SLV/SLV_132_SSU.tax.gz";
#getS2($SlvAltTax,"$DB2.gz");
#system("gunzip -c $DB2.gz > $DB2;rm -f $DB2.gz");
}
my $SLV = $baseSP."/".$baseSN."_SSURef_NR99_tax_silva.fasta.gz";
getS2($SLV,"$DB.gz");
getS2("https://ksgp.earlham.ac.uk/lambdaDBs/v3.0/SLV_138.1_SSU.fasta.lba.gz","$DB.lba.gz") if ($downloadLmbdIdx);
#print "$SLV\n";
system("gunzip -c $DB.gz > $ddir/SSUsilva.fasta;rm -f $DB.gz");
getS2($baseSP."/taxonomy/tax_slv_ssu_$SLVver.txt.gz","$ddir/SLVtaxSSU.csv.gz");
#print "$baseSP/taxonomy/tax_slv_ssu_$SLVver.txt.gz\n";
system("gunzip -c $ddir/SLVtaxSSU.csv.gz > $ddir/SLVtaxSSU.csv;rm -f $ddir/SLVtaxSSU.csv.gz");
prepareSILVA("$ddir/SSUsilva.fasta",$DB,$DB2,"$ddir/SLVtaxSSU.csv","");
unlink("$ddir/SSUsilva.fasta");
$finalWarning .= "\nWARNING: Silva $SLVver does not have consistent taxonomy levels for LSU's, therefore the taxonomy used in LotuS will contain \"?\" after taxonomy name.\n";
@txt = addInfoLtS("TAX_REFDB_SSU_SLV",$DB,\@txt,1);
@txt = addInfoLtS("TAX_RANK_SSU_SLV",$DB2,\@txt,1);
buildIndex($DB);
#------------------------------ LSU SLV DB --------------------------
$DB = "$ddir/$baseLN"."_LSU.fasta";
$DB2 = "$ddir/$baseLN"."_LSU.tax";
print "Downloading SILVA LSU release $SLVver..\n";
system "rm -f $DB"."*";
$locSLBdl=0; $SLVver="138.1";#change this to local (132 release), since SIVLA doesn't have that yet..
if ($locSLBdl){ #in case silva server doesn't work again..
$baseSP = "http://lotus2.earlham.ac.uk/lotus/packs/DB/SLV/";
}
# my $SlvAltFna = "http://lotus2.earlham.ac.uk/lotus/packs/DB/SLV/SLV_132_LSU.fasta.gz";
# getS2($SlvAltFna,"$DB.gz");
# system("gunzip -c $DB.gz > $DB;rm -f $DB.gz");
# my $SlvAltTax = "http://lotus2.earlham.ac.uk/lotus/packs/DB/SLV/SLV_132_LSU.tax.gz";
# getS2($SlvAltTax,"$DB2.gz");
# system("gunzip -c $DB2.gz > $DB2;rm -f $DB2.gz");
$SLV = $baseSP."/".$baseSN."_LSURef_tax_silva.fasta.gz";
getS2($SLV,"$DB.gz");
getS2($baseSP."/taxonomy/tax_slv_lsu_$SLVver.txt.gz","$ddir/SLVtaxLSU.csv");
system("gunzip -c $DB.gz > $ddir/LSUSILVA.fasta;rm -f $DB.gz"); #unlink("$DB.tgz");
prepareSILVA("$ddir/LSUSILVA.fasta",$DB,$DB2,"$ddir/SLVtaxLSU.csv","$ddir/SLVtaxSSU.csv");
unlink("$ddir/LSUSILVA.fasta"); unlink("$ddir/SLVtaxLSU.csv");unlink("$ddir/SLVtaxSSU.csv");
@txt = addInfoLtS("TAX_REFDB_LSU_SLV",$DB,\@txt,1);
@txt = addInfoLtS("TAX_RANK_LSU_SLV",$DB2,\@txt,1);
buildIndex($DB);
return @txt;
}
sub prepareSILVA($ $ $ $){
#taxf3 is for 18S/28S #taxf3 is for SSU/LSU
my ($path, $SeqF,$taxF,$taxGuide,$taxGuide2) = @_;
print("Rewriting SILVA DB..\n");
my %taxG;
open I,"<",$taxGuide or die "Can't find taxguide file $taxGuide\n";
while (my $line = <I>){
chomp($line); my @splg = split("\t",$line);
if (scalar(@splg) > 0){
my $newN = $splg[0]; #lc
$taxG{$newN} = $splg[2];
}
}
close I;
if ($taxGuide2 ne ""){
open I,"<",$taxGuide2 or die "Can't find taxguide file $taxGuide2\n";
while (my $line = <I>){
chomp($line); my @splg = split("\t",$line);
next if (@splg < 2);
my $newN = $splg[0]; #lc
$taxG{$newN} = $splg[2];
}
close I;
}
open I,"<",$path or die ("could not find SILVA file \n$path\n");
open OT,">",$taxF;
open OS,">",$SeqF;
#open OT2,">",$taxF2;open OS2,">",$SeqF2;
my @tdesign = (" k__"," p__"," c__"," o__"," f__"," g__"," s__");
my $skip = 0;
my $eukMode = 0;
my $replacementTax =0; my $allTax=0;
while (my $line = <I>){
chomp($line);
if ($line =~ m/^>/){#header
$skip=0;$eukMode = 0;
my @spl = split("\\.",$line);
if (1){
; #do nothing
}elsif ($spl[0] =~ m/>AB201750/){
$line = ">AB201750.1.1495 Bacteria;Firmicutes;Clostridia;Clostridiales;Clostridiaceae 2;Anaerovirgula;Anaerovirgula multivorans";
@spl = split("\\.",$line);
} elsif ($spl[0] =~ m/>DQ643978/){
$line = ">DQ643978.1.1627 Bacteria;Firmicutes;Clostridia;Clostridiales;Clostridiaceae 4;Geosporobacter;Geosporobacter subterraneus";
@spl = split("\\.",$line);
}elsif ($spl[0] =~ m/>X99238/){
$line = ">X99238.1.1404 Bacteria;Firmicutes;Clostridia;Clostridiales;Clostridiaceae 1;Thermobrachium;Thermobrachium celere";
@spl = split("\\.",$line);
} elsif ($spl[0] =~ m/>FJ481102/){
$line = ">FJ481102.1.1423 Bacteria;Firmicutes;Clostridia;Clostridiales;Clostridiaceae 1;Fervidicella;Fervidicella metallireducens AeB";
@spl = split("\\.",$line);
} elsif ($spl[0] =~ m/>EU443727/){
$line = ">EU443727.1.1627 Bacteria;Firmicutes;Clostridia;Clostridiales;Clostridiaceae 4;Thermotalea;Thermotalea metallivorans";
@spl = split("\\.",$line);
}elsif ($spl[0] =~ m/>FR690973/){
$line = ">FR690973.1.2373 Bacteria;Proteobacteria;Gammaproteobacteria;Thiotrichales;Thiotrichaceae;Candidatus Thiopilula;Candidatus Thiopilula aggregata";
@spl = split("\\.",$line);
}elsif ($spl[0] =~ m/>CP002161/){
$line = ">CP002161.5310.6845 Bacteria;Proteobacteria;Gammaproteobacteria;Enterobacteriales;Enterobacteriaceae;Candidatus Zinderia;Candidatus Zinderia insecticola CARI";
@spl = split("\\.",$line);
} elsif ($spl[0] =~ m/>FR690975/){
$line = ">FR690975.1.2297 Bacteria;Proteobacteria;Gammaproteobacteria;Thiotrichales;Thiotrichaceae;Candidatus Thiopilula;Candidatus Thiopilula aggregata";
@spl = split("\\.",$line);
}elsif ($spl[0] =~ m/>FR690991/){
$line = ">FR690991.1.2147 Bacteria;Proteobacteria;Gammaproteobacteria;Thiotrichales;Thiotrichaceae;Candidatus Thiopilula;Candidatus Marithioploca araucae";
@spl = split("\\.",$line);
}elsif ($spl[0] =~ m/>FR690991/){
$line = ">AB910318.1.1553 Bacteria;Firmicutes;Clostridia;Clostridiales;Clostridiaceae 4;Thermotalea;uncultured bacterium";
@spl = split("\\.",$line);
}elsif ($spl[0] =~ m/>AB910318/){
$line = ">AB910318.1.1553 Bacteria;Firmicutes;Clostridia;Clostridiales;Clostridiaceae 4;Thermotalea;uncultured bacterium";
@spl = split("\\.",$line);
}elsif ($spl[0] =~ m/>AY796047/){
$line = ">AY796047.1.1592 Bacteria;Firmicutes;Clostridia;Clostridiales;Clostridiaceae 4;Thermotalea;uncultured bacterium";
@spl = split("\\.",$line);
}
my $ID = $spl[0];
$ID = substr($ID,1);
$line =~ m/[^\s]+\s(.*)$/;
my $tax = $1; chomp $tax;
if ($tax =~ m/^\s*Eukaryota/){$eukMode = 1;}#$skip = 1; next;}
print OS ">".$ID."\n";
@spl = split(";",$tax);
for (my $i=0;$i<@spl;$i++){
$spl[$i] =~ s/^\s*//; $spl[$i] =~ s/\s*$//;
}
#die "@spl\n";
my $tline;
if (!$eukMode){
if (@spl > 7 ){
print $line."\n";
print("too many categories\n");
}
for (my $i=0;$i<7; $i++){
if ($i < scalar(@spl)){
if ($spl[$i] =~ m/^unidentified/){$spl[$i] = "?";}
$spl[$i] = $tdesign[$i].$spl[$i];
} else {
$spl[$i] = $tdesign[$i];
}
}
$tline = $ID ."\t".join(";",@spl);
} else {#parse the levels out from taxguide
my $tmpTax = "";
my @jnd;
my @soughtCls = ("domain","phylum","class","order","family","genus","species");
my $soughtLvl = 0; my $lastUsed = 0;
for (my $i=0;$i<@spl; $i++){
my $scanTax = $tmpTax.$spl[$i].";";
if (exists($taxG{$scanTax}) || $soughtLvl == 6 || $spl[$i] =~ m/^unidentified/){
#print "$taxG{$scanTax} LL\n";
#SILVA has no species level in tax guide file
$lastUsed = $soughtLvl;
if ($soughtLvl == 6){
push(@jnd,$tdesign[$soughtLvl].$spl[$i]);
$soughtLvl++;
last;
} elsif ($spl[$i] =~ m/^unidentified/ || $taxG{$scanTax} eq ""){#Euk in LSU file have no annotation..
$spl[$i] = "";
push(@jnd,$tdesign[$soughtLvl]."?");
$soughtLvl++;
} elsif ($taxG{$scanTax} eq $soughtCls[$soughtLvl]){
push(@jnd,$tdesign[$soughtLvl].$spl[$i]);
#print $tdesign[$soughtLvl].$spl[$i]."\n";
$soughtLvl++;
} elsif ($taxG{$scanTax} eq $soughtCls[$soughtLvl+1]){#fill in empty levels
push(@jnd,$tdesign[$soughtLvl]);
$soughtLvl++;
push(@jnd,$tdesign[$soughtLvl].$spl[$i]);
#print "Skipped to level ".$tdesign[$soughtLvl].$spl[$i]."\n";
$soughtLvl++;
}
} else { #more likely to be low level species
my $arS = @spl;
#species signatuer & last entry
if ($spl[$i] =~ m/\S+\s\S+/ && $arS >= ($i)){
my $ncnt=1;
while ($soughtLvl<6){
my $nIdx = $lastUsed+ $ncnt;
if ($nIdx < ($arS-1) ){
#just impute preceding levels
push(@jnd,$tdesign[$soughtLvl]."?".$spl[ $nIdx ]);
} else {
push(@jnd,$tdesign[$soughtLvl]."?");
}
$soughtLvl++;$ncnt++;
}
$soughtLvl = 6;
#almost certainly a species
push(@jnd,$tdesign[$soughtLvl].$spl[$i]);
$soughtLvl++;
$replacementTax++;
#print $ID."\t".join(";",@jnd)."\n$lastUsed\n";
last;
} else {
#print $scanTax." JJ\n";
}
}
#Eukaryota;Fungi;Ascomycota;Archaeorhizomycetes;Archaeorhizomycetales;Archaeorhizomycetales_incertae_sedis
$tmpTax .= $spl[$i].";";
$lastUsed = $i;
}
$allTax++;
for (;$soughtLvl<7;$soughtLvl++){
push(@jnd,$tdesign[$soughtLvl]);
}
$tline = $ID."\t".join(";",@jnd);
#die $tax." CC " .$tline."\n";
}
print OT $tline."\n";
#die($tline);
} elsif ($skip == 0){ #work through sequence
$line =~ s/\s//g;
$line =~ s/U/T/g;
$line =~ s/u/t/g;
#die $line;
print OS $line."\n";
}
}
#print "$replacementTax out of $allTax could not be defined to clear taxonomic levels and were imputed (with mostly empty tax levels or a \"?\" before tax name\n";
close I; close OT; close OS; #close OT2; close OS2;
}
sub getS2($ $){
my ($in,$out) = @_;
print "getS2:$in\n$out\n";
print $in."\n";
if ($WGETpres){
# print "wget\n";
print "wget -O $out $in\n";
system("wget -O $out $in");
} elsif (!$isMac && $LWPsimple){
print "LWP\n";
getstore($in,$out);
} elsif ($FILEfetch){
print "FETCH\n";
my $ff = File::Fetch->new( uri => $in);
my $file = $ff->fetch() or print "Can't download file $in with File::Fetch\n".$ff->error()."\n";
move($file, $out);
} else {
die "no suitable library / program on you system. Please ensure that \"wget\" is installed\n";
}
}
sub checkLtsVer($){
my ($lver) = @_;
my $updtmpf = get("http://lotus2.earlham.ac.uk/lotus/lotus/updates/Msg.txt");
my $msg = ""; my $hadMsg=0;
open( TF, '<', \$updtmpf ); while(<TF>){$msg .= $_;} close(TF);
foreach my $lin (split(/\n/,$msg)){
my @spl = split /\t/,$lin;
next if (@spl==0);
if ($lver<$spl[0]){print $spl[1]."\n\n"};
$hadMsg=1;
}
# compare to server version
die "LWP:simple package not installed, but required for updater!" if (!$LWPsimple);
$updtmpf = get("http://lotus2.earlham.ac.uk/lotus/lotus/updates/curVer.txt");
open( TF, '<', \$updtmpf ); my $lsv = <TF>; close(TF); chomp $lsv;
my $msgEnd = "";
$updtmpf = get("http://lotus2.earlham.ac.uk/lotus/lotus/updates/curVerMsg.txt");
open( TF, '<', \$updtmpf ); while(<TF>){$msgEnd .= $_;} close(TF);
$updtmpf = get("http://lotus2.earlham.ac.uk/lotus/lotus/updates/UpdateHist.txt");
my $updates = "";
open( TF, '<', \$updtmpf );$msg = ""; while(<TF>){$msg .= $_;} close(TF);
foreach my $lin (split(/\n/,$msg)){
my @spl = split /\t/,$lin; chomp $lin;
next if (@spl < 2 || $spl[0] eq "");
if ($spl[1] =~ m/LotuS (\d?\.\d+)/){
if ($lver<$1){$updates.= $spl[0]."\t".$spl[1]."\n"};
}
}
if ($updates ne ""){
print "--------------------------------\nThe following updates are available:\n--------------------------------\n";
print $updates;
print "\n\nCurrent Lotus version is :$lver\nLatest version is: $lsv\n";
}
if ($hadMsg || $updates ne ""){sleep(4);}
#die;
return $lsv,$msgEnd;
}
sub compile_LCA($){
my ($ldi2) = @_;
my $expPath = "$bdir/LCA";
if (-e $expPath){#test if can excecute locally
my $sdmV = `$expPath -v`;
return $expPath if ($sdmV =~ m/0\.\d+$/);
}
if (-d $ldi2 && -f "$ldi2/Makefile" ){
print "Compiling LCA..\n";
system("rm -f $ldi2/*.o");
my $stat = system("make -C $ldi2");
if ($stat == 0){
system("rm -f $ldir/LCA $bdir/LCA; mv $ldi2/LCA $bdir/LCA; chmod +x $bdir/LCA");
} elsif ($isMac && $stat){
print "\n\n=================\nCompilation of LCA failed.\n It seems this is a Mac system and no native LCA compile is available, please try installing C++0x clang or gcc support for your system first, otherwise contact falk.hildebrand\@gmail.com .\n";
print "Press any key to continue installation\n";
$finalWarning .= "LCA was not compiled (this needs to be compiled to run lotus, rerun autoinstall, after your system has a C++0x compliant C++ compiler like clang , gcc installed).\n";
<STDIN>;
} else {
print "\n\n=================\nCompilation of LCA failed (please contact falk.hildebrand\@gmail.com).\n A general prupose LCA binary is being used, but this is not recommended\n";
print "Press any key to continue installation\n";
$finalWarning .= "Non-native LCA compilation";
<STDIN>;
}
}
system("chmod +x $bdir/LCA");
return $expPath;#"$bdir/LCA";
}
sub compile_rtk($){
my ($ldi2) = @_;
my $expPath = "$bdir/rtk";
if (-e $expPath){#test if can excecute locally
my $sdmV = `$expPath -v`;
return $expPath if ($sdmV =~ m/rtk \d/);
}
if (-d $ldi2 && -f "$ldi2/Makefile" ){
print "Compiling rtk..\n";
system("rm -f $ldi2/*.o");
my $stat = system("make -C $ldi2");
if ($stat == 0){
system("rm -f $ldir/rtk $bdir/rtk; mv $ldi2/rtk $bdir/rtk; chmod +x $bdir/rtk");
} elsif ($isMac && $stat){
print "\n\n=================\nCompilation of trk failed.\n It seems this is a Mac system and no native rtk compile is available, please try installing C++0x clang or gcc support for your system first, otherwise contact falk.hildebrand\@gmail.com .\n";
print "Press any key to continue installation\n";
$finalWarning .= "rtk was not compiled (this needs to be compiled to run lotus, rerun autoinstall, after your system has a C++0x compliant C++ compiler like clang , gcc installed).\n";
<STDIN>;
} else {
print "\n\n=================\nCompilation of rtk failed (please contact falk.hildebrand\@gmail.com).\n A general prupose rtk binary is being used, but this is not recommended\n";
print "Press any key to continue installation\n";
$finalWarning .= "Non-native rtk compilation";
<STDIN>;
}
}
system("chmod +x $bdir/rtk");
return $expPath;
}
sub compile_sdm($){
my ($ldi2) = @_;
my $expPath = "$bdir/sdm";
if (-e $expPath){#test if can excecute locally
my $sdmV = `$expPath -v`;
return $expPath if ($sdmV =~ m/sdm \d/);
}
if (-d $ldi2 && -f "$ldi2/Makefile" && -f "$ldi2/DNAconsts.cpp"){
print "Compiling sdm..\n";
system("rm -f $ldi2/*.o");
my $stat = system("make -C $ldi2");
if ($stat != 0){#repeat without gzip
print "\n\n\n\n=================\nProblem compiling sdm with gzip support\nFallback to sdm compilation without gzip support\n";
system("sed -i 's/#define _gzipread/#define _notgzip/g' $ldi2/DNAconsts.h");
system("rm $ldi2/*.o");
$stat = system("make -C $ldi2");
$finalWarning .= "Can not read gzip file\n";
}
if ($stat == 0){
system("rm -f $ldir/sdm $bdir/sdm; mv $ldi2/sdm $bdir/sdm; chmod +x $bdir/sdm");
} elsif ($isMac && $stat){
print "\n\n=================\nCompilation of sdm failed.\n It seems this is a Mac system and no native sdm compile is available, please try installing C++0x clang or gcc support for your system first, otherwise contact falk.hildebrand\@gmail.com .\n";
print "Press any key to continue installation\n";
$finalWarning .= "sdm was not compiled (this needs to be compiled to run lotus, rerun autoinstall, after your system has a C++0x compliant C++ compiler like clang , gcc installed).\n";
<STDIN>;
} else {
print "\n\n=================\nCompilation of sdm failed (please contact falk.hildebrand\@gmail.com).\n A general prupose sdm binary is being used, but this is not recommended\n";
print "Press any key to continue installation\n";
$finalWarning .= "Non-native sdm compilation";
<STDIN>;
}
}
system("chmod +x $ldir/sdm");
return $expPath;
}
sub check_exists {
my $check = `sh -c 'command -v $_[0]'`;
return $check;
}
sub check_version {
my $check = `$_[0] --version 2>&1`;
if ($check =~ m/version (\d)\./){
return $1;
}
my @vstr = split /\./, $check, 2;
my $v = substr $vstr[0], -1;
return $v;
}
sub getTaxSfromUNITE{
my ($head) = @_;
my $taxS = "k__?;p__?;c__?;o__?;f__?;g__?;s__";
if ($head =~ s/\|([^\|]+)$//){
$taxS = $1;
} else {
die "Error in extrTaxFromFasta:: can't find \"|\" in string $head\n";
}
return ($taxS, $head);
}
sub extrTaxFromFasta($ $ $){
my ($inFA, $oFA, $oTax) = @_;