-
Notifications
You must be signed in to change notification settings - Fork 2
/
egn_ni.pl
executable file
·2584 lines (2210 loc) · 85.6 KB
/
egn_ni.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
#Copyright (C) 2013 Sebastien Halary
#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/>
use 5.14.0;
use Carp;
use POSIX qw(floor ceil);
use Scalar::Util qw(looks_like_number);
use File::Basename;
use File::Spec;
# {{{ Getopt::Long
use Getopt::Long;
my $ecp = qq(10,40,40,14,5,50); # edge calculation parameters.
my $task;
my $progind;
my $help;
GetOptions (
"task:s" => \$task,
"progress" => \$progind,
"ecp|edgecalc:s" => \$ecp,
"help" => \$help
);
# }}}
# {{{ POD
=head1 Name
egn.1.0.plus.pl
=head2 Examples
Tasks should be run in the following order
perl code/egn.1.0.plus.pl -task check
perl code/egn.1.0.plus.pl -task blast
perl code/egn.1.0.plus.pl -task edge
perl code/egn.1.0.plus.pl -task net
Or
perl code/egn.1.0.plus.pl -task all
The above commands assume that the data (.faa file) is in the
current directory and this script is in the subdirectory named
C<code> in the current directory. Please modify the command
according to your data and script locations. I<EGN> recommends
that data be in the current directory.
Below, to enable I<EGN> progress messages
perl code/egn.1.0.plus.pl -progress -task all
=head2 Description
I<EGN> modified for non-interactive use.
Data is fasta files in the current directory. Intermediate files
are also written in the current directory. The result files are
written in a directory named C<GENENET_*>.
For example, C<GENENET_10.40.40.0.0>
=head2 Options
=over 2
=item -task
Required. Alternatives are C<check, blast, edge, net> and C<all>.
Unless C<all> is being used, these tasks should be carried out
in the order C<check, blast, edge, net> because earlier tasks
produce output files which as input for later tasks. If C<all> is
used then all the tasks are carried out in the proper order.
=item -progress
This is a boolean option (disabled by default) which allows
I<EGN> progress messages to be printed. If this is not specified
then I<EGN> progress messages are suppressed.
=item -ecp or -edgecalc
String. This string has to consist of 6 comma separated integers
without any spaces. For example, the default is shown below.
-ecp 10,40,40,14,5,50
The 6 numbers are described by I<EGN> as below.
1. E-value threshold
2. Hit identity threshold
3. Hit covers at least [0-100]% of the shortest sequence
4. Minimal hit length in nucleic acids (/ by 3 for amino-acids)
5. Best-reciprocity: sequences in top [0-100]% of the best E-value
6. Hit covers at least [0-100%] of both sequences
=back
=head2 Dev notes (ignore)
Only C<genetw()> has been modified. C<genomnetw()> has not been modified.
C<blat()> has also been left untouched because I don't have I<blat> on
my machine.
Below is a list of calls determined by C<mapcalls.pl>. Numbers may not be
correct, especially from C<MAIN>.
MAIN genetw 4
MAIN genomnetw 2
MAIN checkFiles 3
MAIN blast 3
MAIN blat 1
MAIN quickPrepTables 3
MAIN slowPrepTables 1
MAIN linelistE 5
pilot printTitle 1
pilot pilot 2
checkFiles _findExactSample 1
checkFiles _findAttributes 1
checkFiles printTable 2
getConfig createConfig 1
blast getConfig 1
blast getNSeq 4
blat getConfig 1
blat splitFastaFile 2
submenu printTitle 1
quickPrepTables getConfig 1
quickPrepTables getNLine 1
slowPrepTables getConfig 1
slowPrepTables submenu 1
slowPrepTables getNLine 3
simpleLink _sl 1
genomeSimpleLink _sl 1
_sl _sl 1
genetw printTitle 1
genetw simpleLink 1
genetw getNGroup 1
genetw _rsd 3
genomnetw submenu 2
genomnetw printTitle 1
genomnetw simpleLink 1
genomnetw genomeSimpleLink 1
=cut
# }}}
unless($task) {
exec("perldoc $0");
exit;
}
my @edgeCalcParams = split(/,|;/, $ecp);
unless(scalar(@edgeCalcParams) == 6) {
my $parnum = scalar(@edgeCalcParams);
my $croakmsg = qq/Exactly 6 edges calculation parameters are required./;
$croakmsg = qq/You have provided $parnum./;
croak($croakmsg);
}
# Need to know the directory in which the script lives because
# configuration files are looked for in the same directory.
my($noex, $execdir, $ext) = fileparse($0, qr/\.[^.]*/);
my $configfile = File::Spec->catfile($execdir, $noex . ".config");
# {{{ $MENU
my $MENU=([
"EGN Main Menu",
'Create the input files' => \&checkFiles,
'Similarity search in sequences' => [
"Local alignment software\n",
'BLAST' => \&blast,
'BLAT' => \&blat,
'Back to Main menu' => undef,
],
'Prepare Edges File' => [
"Optimization for Edges File\n",
"Quicker (requires a maximum of RAM)" => \&quickPrepTables,
"Slower (less RAM, but more free disc space)" => \&slowPrepTables,
'Back to Main menu' => undef,
],
'Networks' => [
"Network type\n",
'Gene network' => \&genetw,
'Genome network' => \&genomnetw,
'Quit' => -1
],
'Exit' => -1,
]);
# }}}
if($task =~ m/^ch/i) {
unlink(glob("seq*"));
unlink(glob("proteic*"));
checkFiles();
}
elsif($task =~ m/^bl|^se|^simi/i) {
unlink(glob("*.log"));
# unlink("align.pp.out"); # Not needed. blast() does unlink *.out.
blast();
}
elsif($task =~ m/^ed/i) {
print(join("\t", @edgeCalcParams), "\n");
quickPrepTables();
}
elsif($task =~ m/^net/i) {
genetw();
}
elsif($task =~ m/^all/i) {
linelistE("1. Checking");
checkFiles();
linelistE("2. Similarity searching");
blast();
linelistE("3. Calculating edges");
quickPrepTables();
linelistE("4. Making network");
genetw();
linelistE("Done.");
}
sub linelistPE {
if($progind) {
print(STDERR join("\n", @_), "\n");
}
}
sub linelistE {
print(STDERR join("\n", @_), "\n");
}
# {{{ sub pilot
sub pilot {
my $tableRef = shift;
my @copytab=@{$tableRef};
my $title = shift(@copytab);
my (@name, @action);
($name[@name], $action[@action]) = splice @copytab, 0, 2
while @copytab;
while(1){
print `clear`;
printTitle($title);
print "\n";
print map "$_. $name[$_ - 1]\n", 1 .. @name;
print "\n";
print '> ';
chomp(my $choice = readline *STDIN);
if ($choice and $choice > 0 and $choice <= @action) {
$choice-=1;
my $do_action = $action[$choice];
pilot($MENU) unless defined $do_action;
if($do_action==-1){exit(0)}
if('CODE' eq ref $action[$choice]){
$do_action->() ;
}
pilot($action[$choice]) if 'ARRAY' eq ref $action[$choice];
} else {
print "Invalid choice: $choice\n";
}
}
}
# }}}
# caracteres acceptes pour les identifiants: alphanumerique,_ et .
# caracteres acceptes pour les samples : tous
# caracteres acceptes pour les attributs : tous sauf #
# {{{ sub checkFiles
sub checkFiles {
my @fastas;
my %resume;
my $dbflag=0;
my $nucFiles;
opendir(DIR,".");
my @files = readdir(DIR);
@fastas = grep(/\.fna$/,@files);
$nucFiles = @fastas;
@fastas = (@fastas, grep(/\.faa$/,@files));
$dbflag=1 if grep(/\.db(n|p)/,@files);
closedir(DIR);
die "Input files should be renamed .fna or .faa\n" if(!@fastas);
# printTable("File", "Sequences", "Samples", "Attributes");
#for(@fastas){print $_."\n";}
my $gi;
my $id;my $c=0;
my (%gi2id,%sample,%attr,%seq);
for my $i (1..@fastas){
my $file=$fastas[$i-1];
my $type = 'p';
$type = 'n' if($i<=$nucFiles);
my (%nattr,%nsamp);
open(IN,$file) or die "problem openning $file!\n";
while(my $line=<IN>){
if($line=~/^>/){
if($line=~/>([A-Z])+\|((\w|\.)+)(\||\s|\[)/i){
$gi=$2;
$c++;
$id=$type.$c;
$gi2id{$id}=$gi;
my $samp=_findExactSample($line);
$nsamp{$samp}++;
push(@{$sample{$samp}},$id);
my @attributes=_findAttributes($line);
$nattr{scalar(@attributes)}++;
push(@{$attr{$_}},$id) for(@attributes);
$seq{$file}->{$id}='';
}
else{
chomp($line);
die(
"\nWrong format for sequence \"$line\" header in $file!
The correct header format must consist in:
>tag|seqID|additional information [sample] #attribute1 #attribute2 #...
- tag must be a word ([a-z] or _). Typically gi, emb,ref, or lcl.
- SeqID may contain [a-z], [0-9], \".\" and \"_\".
- additional information may contain any character except \"\#\", \"[\" and \"]\".
- sample may contain any character except \"\#\", \"[\" and \"]\" but have to be surrounded by \"[\" and \"]\".
- attributes may contain any character except \"\#\", \"[\" and \"]\" but have to be preceded by a \"\#\".
additional informations, sample and attributes are optionnal.\n\n");
}
}
else{
chomp($line);
if($line!~m/^([A-Z]|-|\*|\s)*$/i){
$line=~s/[A-Z]|-|\*|\s//ig;
my %c;
$c{chop($line)}++ while($line);
my $label=join(",",sort keys %c);
die "Wrong character(s) ($label) in sequence $gi ($file):\n";
}
$seq{$file}->{$id}.=$line;
}
}
#determiner le nombre d'attributs par fichier
my @attrange = sort { return $a <=> $b } keys %nattr;
my $abstrattr;
if(scalar(@attrange)==1){$abstrattr=$attrange[0];}
else{ $abstrattr = $attrange[0]."-".$attrange[$#attrange];}
#IMPRIMER FILE|NB_HEADER|NB_SAMPLE|NB_ATTRIBUTES_PAR_SEQUENCES[]
push(@{$resume{$file}},(my $a= keys %{$seq{$file}}),(my $b= keys %nsamp), $abstrattr);
}
if($progind) {
print "\n";
printTable("File", "Sequences", "Samples", "Attributes");
foreach my $key(keys %resume){
printTable($key,$resume{$key}[0],$resume{$key}[1],$resume{$key}[2]);
}
}
#User's checking point
my $choice='Y';
while($choice !~/Y|N/i){
print "\nWould you like to proceed with this data? (y/n)\n>";
chomp($choice = readline *STDIN);
return(undef) unless ($choice=~/Y/i);
}
#print the input files
##files 1: dbn,dbp
#file 2: id,gi,length => en se servant du tableau @gi2id
#file 3: sample->id list, attribute-n -> id list
# #### => On dumpe les fichiers fastas dans 1 fichier dbn et 1 fichier dbd
open(FNA,">nucleic.dbn");
open(FAA,">proteic.dbp");
open(INF,">seq.info");
open(EMPTY,">empty.log");
for my $i (1..@fastas){
my $outhandle = *FAA;
if($i<=$nucFiles){
$outhandle = *FNA;
}
foreach my $id (keys %{$seq{$fastas[$i-1]}}){
my $sequence=$seq{$fastas[$i-1]}->{$id};
if(length($sequence)==0){
print EMPTY "id: ".$gi2id{$id}." file: $fastas[$i-1]\n";
}
else{
print $outhandle ">$id\n".$sequence."\n";
print INF $id."\t".$gi2id{$id}."\t".length($sequence)."\t\n";
}
}
}
close(FNA);
close(FAA);
close(INF);
close(EMPTY);
if(-z "empty.log"){unlink "empty.log";}
else{
print "\nWarning: Empty sequences were detected and reported in empty.log\n";
}
if(-z "nucleic.dbn"){unlink "nucleic.dbn";}
if(-z "proteic.dbp"){unlink "proteic.dbp";}
open(ATT,">seq.att");
print ATT "#SAMPLE\n";
foreach my $sample (keys %sample){
print ATT $sample."|#|".join(',',@{$sample{$sample}})."\n";
}
print ATT "#ATTRIB\n" if(keys %attr);
foreach my $att (keys %attr){
print ATT ($att,"|#|",join(',',@{$attr{$att}}),"\n");
}
close(ATT);
if($progind) {
print "\nData file(s) created.\n";
}
# <STDIN>;
return 0;
}
# }}}
# {{{ sub printTable
sub printTable {
my ($file,$nseq,$nsample,$natt)=@_;
format TABLE =
+---------------------------------------------------------------------+
| @|||||||||||||||||||||||||| | @|||||||||| | @||||||||| | @||||||||| |
$file,$nseq,$nsample,$natt
+---------------------------------------------------------------------+
.
$~ = 'TABLE';
write();
}
# }}}
# {{{ sub printTitle
sub printTitle {
my $title=shift;
format TITLE =
#################################
# @|||||||||||||||||||||||||||| #
$title
#################################
.
$~ = 'TITLE';
write();
}
# }}}
# {{{ sub _findExactSample
sub _findExactSample {
my $string=shift;
my $char='';
my $sample='';
my $score=1;
my $l=1;
if($string=~/\[/ && $string=~/\]/){
while($char ne "\]"){
$char=chop($string);
}
while($score!=0){
$char=chop($string);
$sample=$char.$sample;
if($char eq "\]"){
$score++;
}
if($char eq "\["){
$score--;
}
}
$sample=~s/\[//;
}
return $sample;
}
# }}}
# {{{ sub _findAttributes
sub _findAttributes {
chomp(my $string=shift);
my @attributes=split(/\s*#\s*/,$string);
shift(@attributes);
return @attributes;
}
# }}}
# {{{ sub getConfig
sub getConfig {
my $prog=shift;
my %options;
createConfig($configfile) if(! -e $configfile);
open(CFG,"<", $configfile) or
die ("unable to find $configfile: $!\n");
while((my $line=<CFG>)!~m/begin $prog;/i){}
while((my $line=<CFG>)!~m/end $prog;/i){
if($line!~m/^(#|\s)/){
chomp($line);
my @tmp=split(/\s*=\s*/,$line);
if($tmp[1]!~m/def/){
$options{$tmp[0]}=$tmp[1];
}
}
}
return %options;
}
# }}}
# {{{ sub createConfig
sub createConfig {
my $path=shift;
open(CFG,">$path") or warn ("unable to create $path: $!");
print CFG "#############################################################################################\n";
print CFG "# #\n";
print CFG "# This file contains options of alignement tools available in EGN #\n";
print CFG "# It was written from documentation of: #\n";
print CFG "# -BLAST Altschul, S.F., Gish, W., Miller, W., Myers, E.W. & Lipman, D.J. (1990) #\n";
print CFG "# \"Basic local alignment search tool.\" J. Mol. Biol. 215:403-410 #\n";
print CFG "# -BLAT BLAT--the BLAST-like alignment tool. Kent WJ. #\n";
print CFG "# Genome Res. 2002 Apr;12(4):656-64. #\n";
print CFG "# #\n";
print CFG "# -Please verify that your PATH is properly configured for each of them #\n";
print CFG "# -Edit these values to modify their usage in EGN #\n";
print CFG "# #\n";
print CFG "#############################################################################################\n\n";
print CFG "#################################\n";
print CFG "# BLAST+ options #\n";
print CFG "#################################\n";
print CFG "begin BLAST;\n\n";
print CFG "#Expectation value (E) threshold for saving hits Default = 10\n";
print CFG "-evalue=1e-05\n\n";
print CFG "#Number of threads (CPUs) to use in the BLAST search. default = 1\n";
print CFG "-num_threads=4\n\n";
print CFG "#Cost to open a gap (-1 invokes default behavior) [Integer] default = -1\n";
print CFG "-gapopen=def\n\n";
print CFG "#Cost to extend a gap (-1 invokes default behavior) [Integer] default = -1\n";
print CFG "-gapextend=def\n\n";
print CFG "#Number of database sequences to show one-line descriptions for (V) [Integer] default = 500\n";
print CFG "-num_descriptions=def\n\n";
print CFG "end BLAST;\n";
print CFG "#\n\n";
print CFG "#####################################\n";
print CFG "# BLAT options #\n";
print CFG "#####################################\n";
print CFG "begin BLAT;\n\n";
print CFG "#external file of over-occurring 11-mers\n";
print CFG "#-ooc=BLATPath/11.ooc\n\n";
print CFG "#If set to 1 this allows one mismatch in tile and still triggers an alignments. Default is 0. \n";
print CFG "-oneOff=def \n\n";
print CFG "#-minScore=N sets minimum score. This is the matches minus the mismatches minus some sort of gap penalty. Default is 30\n";
print CFG "-minScore=def\n\n";
print CFG "#-maxGap=N sets the size of maximum gap between tiles in a clump. Usually set from 0 to 3. Default is 2. Only relevant for minMatch > 1.\n";
print CFG "-maxGap=def\n\n";
print CFG "#Number of threads (this option is not a legacy blat parameter) default=1 \n";
print CFG "-num_threads=2\n\n";
print CFG "end BLAT;\n";
close(CFG);
}
# }}}
# {{{ sub blast
sub blast {
my $choice='Y';
my %options=getConfig("BLAST"); # call getConfig
my $dbsoft="makeblastdb";
my ($nucFile,$protFile);
my $nfile=0;
unlink glob "*.out";
# #Parameters display
# printTitle "BLAST parameters:";
# foreach (sort keys %options){
# print "\t$_ = $options{$_}\n";
# }
# print "\t-Other parameters are setup to default values\n";
# while($choice!~/y|q/i){
# print "\n\tAre these parameters correct (y) ? \n\tElse you can edit them in:\n\t$execdir/egn.plus.config (q): ";
# $choice=readline *STDIN;
# }
#
# exit(0) if ($choice=~/q/i);
# print `clear`;
# {{{ if(-e ($nucFile="nucleic.dbn"))
if(-e ($nucFile="nucleic.dbn")) {
$nfile++;
print "\n#Nucleic BLAST database creation using $dbsoft...\n";
open(my $oldout, ">&STDOUT") or die "Can't dup STDERR: $!";
open(STDOUT,">$dbsoft.log");
die($!) if(system($dbsoft,"-in",$nucFile,"-dbtype","nucl"));
open(STDOUT, ">&", $oldout) or die "Can't dup \$oldout: $!";
print "done.\n\n";
print "#BLASTN... ";
my @command= ("blastn","-task","blastn","-query",$nucFile,"-db",$nucFile, "-outfmt",6, %options);
#Output configuration
open(my $olderr, ">&STDERR") or die "Can't dup STDERR: $!";
open(STDERR,">blastn.log");
open(OUT,">align.nn.out");
select STDOUT; $| = 1;
defined(my $pid = open(TO_KID, "-|",@command)) or die "can't fork: $!";
if($pid){
my $prevQ='';
my $count=1;
my $nq=ceil((getNSeq($nucFile))/100); # call getNSeq
my $percent=0;
while(<TO_KID>){
if($_=~/^(\w+)\t/){
if($1 ne $prevQ){
if(($count++ % $nq)==0){
$percent++;
print $percent."%";
print "\b" x length($percent."%");
}
$prevQ=$1;
}
}
print OUT $_;
}
print "100%\ndone.\n";
}
wait();
open(STDERR, ">&", $olderr) or die "Can't dup \$olderr: $!";
close(OUT);
}
# }}}
# {{{ if(-e ($protFile="proteic.dbp"))
if(-e ($protFile="proteic.dbp")) {
$nfile++;
if($progind) {
print "\n#Proteic BLAST database creation using $dbsoft...\n";
}
open(my $oldout, ">&STDOUT") or die "Can't dup STDERR: $!";
open(STDOUT,">$dbsoft.log");
die($!) if(system($dbsoft,"-in",$protFile,"-dbtype","prot"));
open(STDOUT, ">&", $oldout) or die "Can't dup \$oldout: $!";
if($progind) {
print "done.\n\n";
}
if($progind) {
print "\n#BLASTP... ";
}
my @command=("blastp","-task","blastp","-query",$protFile,"-db",$protFile,"-outfmt",6, %options);
#Output configuration
open(my $olderr, ">&STDERR") or die "Can't dup STDERR: $!";
open(STDERR,">blastp.log");
open(OUT,">align.pp.out");
select STDOUT; $| = 1;
defined(my $pid = open(TO_KID, "-|",@command)) or die "can't fork: $!";
if($pid){
my $prevQ='';
my $count=1;
my $nq=ceil(getNSeq($protFile)/100); # call getNSeq
my $percent=0;
while(<TO_KID>){
if($_=~/^(\w+)\t/){
if($1 ne $prevQ){
if(($count++ % $nq)==0){
$percent++;
if($progind) {
print $percent."%";
print "\b" x length($percent."%");
}
}
$prevQ=$1;
}
}
print OUT $_;
}
if($progind) {
print "100%\ndone.\n";
}
}
wait();
open(STDERR, ">&", $olderr) or die "Can't dup \$olderr: $!";
close(OUT);
}
# }}}
if(!$nfile) {
print "\n\tNo datafiles detected.\n\tYou must create data files before processing alignments.\n";
print "\nPress a key to continue...";
<STDIN>;
return;
}
# {{{ elsif($nfile==2)
elsif($nfile==2) {
print "\n#BLASTX...";
my @command=("blastx","-query",$nucFile,"-db",$protFile,"-outfmt",6, %options);
#Output configuration
open(my $olderr, ">&STDERR") or die "Can't dup STDERR: $!";
open(STDERR,">blastnp.log");
open(OUT,">align.np.out");
select STDOUT; $| = 1;
defined(my $pid = open(TO_KID, "-|",@command)) or die "can't fork: $!";
if($pid){
my $prevQ='';
my $count=1;
my $nq=ceil(getNSeq($nucFile)/100); # call getNSeq
my $percent=0;
while(<TO_KID>){
if($_=~/^(\w+)\t/){
if($1 ne $prevQ){
if(($count++ % $nq)==0){
$percent++;
print $percent."%";
print "\b" x length($percent."%");
}
$prevQ=$1;
}
}
print OUT $_;
}
print "100%\ndone.\n";
}
wait();
print "\n#TBLASTN...";
#/usr/bin/blastn -task blastn -db seqs.fna -query seqs.fna -num_threads 4 -outfmt 6
@command=("tblastn","-query",$protFile,"-db",$nucFile,"-outfmt",6, %options);
defined($pid = open(TO_KID, "-|",@command)) or die "can't fork: $!";
if($pid){
my $prevQ='';
my $count=1;
my $nq=(getNSeq($protFile))/100; # call getNSeq
my $percent=0;
while(<TO_KID>){
if($_=~/^(\w+)\t/){
if($1 ne $prevQ){
if(($count++ % $nq)==0){
$percent++;
print $percent."%";
print "\b" x length($percent."%");
}
$prevQ=$1;
}
}
print OUT $_;
}
print "100%\ndone.\n";
}
wait();
open(STDERR, ">&", $olderr) or die "Can't dup \$olderr: $!";
close(OUT);
}
# }}}
unlink glob ("*dbn.* *dbp.*"); # Possibly bad glob specification.
if($progind) {
print "\nAll alignments processed\n";
}
}
# }}} # end of sub blast
# {{{ sub getNLine
sub getNLine {
my $file=shift;
my $nseq=0;
my $buffer=0;
open(FILE, $file) or die "Can't open `$file': $!";
while (sysread FILE, $buffer, 4096) {
$nseq += ($buffer =~ tr/\n//);
}
close FILE;
return $nseq;
}
# }}}
#fonction pour compter les lignes super vite:
# {{{ sub getNSeq
sub getNSeq {
my $file=shift;
my $nseq=0;
my $buffer=0;
open(FILE, $file) or die "Can't open `$file': $!";
while (sysread FILE, $buffer, 4096) {
$nseq += ($buffer =~ tr/>//);
}
close FILE;
return $nseq;
}
# }}}
#fonction pour compter les lignes super vite:
# {{{ sub getNGroup
sub getNGroup {
my $file=shift;
my $ngp=0;
my $buffer=0;
open(FILE, $file) or die "Can't open `$file': $!";
while (sysread FILE, $buffer, 4096) {
$ngp += ($buffer =~ tr{/}{});
}
close FILE;
return ($ngp/2);
}
# }}}
# {{{ sub blat
sub blat {
my $choice='';
my %options=getConfig("BLAT");
my @foptions;
my $nucFile="nucleic.dbn";
my $protFile="proteic.dbp";
my $nfile=0;
print "\n";
#Parameters display
printTitle "BLAT parameters:";
foreach (sort keys %options){
print "\t$_ = $options{$_}\n";
}
print "\t-Other parameters setup to default values\n";
while($choice!~/y|q/i){
print "\n\tAre these parameters correct (y) ? \n\tElse you can edit them in $configfile (q): ";
$choice=readline *STDIN;
}
exit(0) if ($choice=~/q/i);
###
my $num_threads=delete $options{"-num_threads"};
push(@foptions, $_."=".$options{$_}) foreach(keys %options);
if(-e $nucFile){
if(-e $protFile){
my $choice='';
print "\n\n\tWarning: Data contain nucleic and proteic sequences.\n\tNucleic sequences wont be aligned with proteic ones.";
while($choice!~/y|Y|n|N/){
print "\n\tDo you want to continue? (y/n): ";
chomp($choice=readline *STDIN);
}
if($choice=~/n|N/){
return 0;
}
}
print "\n#Nucleic BLAT...\n\n";
my @subFiles;
if($num_threads>1){
print "Preparation of data for parallelization\n";
unlink glob "nucleic.dbn.*";
unlink glob "tmp.blatn.*";
@subFiles=splitFastaFile($nucFile,$num_threads);
print "done.\n\n";
}
else{
$num_threads=1;
}
open(my $olderr, ">&STDERR") or die "Can't dup STDERR: $!";
open(STDERR,">blatn.log");
my @pid;
for(my $i=0;$i<$num_threads;$i++){
push(@pid,my $pid=fork);
unless($pid){
my @command= ("blat",$nucFile,$nucFile.".".$i,@foptions,"-out=blast8","tmp.blatn.$i");
exec(@command) or die ("Problem: ".join("\t",@command)." $!\n");
exit(0);
}
}
foreach (@pid) {
waitpid($_,0);
}
open(OUT,">align.nn.out");
for(my $i=0;$i<$num_threads;$i++){
open IN, "tmp.blatn.$i" or die "unable to open tmp.blatn.$i: $!\n";
print OUT while(<IN>);
close IN;
}
close(OUT);
unlink glob("tmp.blatn.* nucleic.dbn.*");
open(STDERR, ">&", $olderr) or die "Can't dup \$olderr: $!";
print "\ndone.\n";
}
###########
# PROTEIC BLAT
###########
if(-e $protFile){
print "\n#Proteic BLAT...\n\n";
if($num_threads>1){
unlink glob "proteic.dbp.*";
unlink glob "tmp.blatp.*";
splitFastaFile($protFile,$num_threads);
}
else{
$num_threads=1;
}
open(my $olderr, ">&STDERR") or die "Can't dup STDERR: $!";
open(STDERR,">blatp.log");
my @pid;
for(my $i=0;$i<$num_threads;$i++){
push(@pid,my $pid=fork);
unless($pid){
my @command= ("blat",$protFile,$protFile.".".$i,"-prot",@foptions,"-out=blast8","tmp.blatp.$i");
exec(@command) or die ("Problem: ".join("\t",@command)." $!\n");
exit(0);
}
}
foreach (@pid) {
waitpid($_,0);
}
open(OUT,">align.pp.out");
for(my $i=0;$i<$num_threads;$i++){
open IN, "tmp.blatp.$i" or die "unable to open tmp.blatp.$i: $!\n";
print OUT while(<IN>);
close IN;
}
close(OUT);
unlink glob("tmp.blatp.* proteic.dbp.*");
open(STDERR, ">&", $olderr) or die "Can't dup \$olderr: $!";
print "done.\n\n";
}
#unlink glob ("*dbn.* *dbp.*");
print "\nAll alignments processed\n\tPress enter to continue...";
<STDIN>;
}
# }}}
# {{{ sub splitFastaFile
sub splitFastaFile {
my $file=shift;
my $nF=shift;
open(IN,$file) or die "unable to open $file: $!\n";
my $c=0;
my $handle=*STDOUT;
while(my $line=<IN>){
if($line=~/>/){
close(OUT);
$c++;