forked from weizhongli/cdhit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsi-cd-hit-local.pl
executable file
·1525 lines (1323 loc) · 46.5 KB
/
psi-cd-hit-local.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 -w
################################################################################
######### PSI-cd-hit written by Weizhong Li at http://cd-hit.org
################################################################################
our $pid = $$;
our $db_in; ###################
our $db_out; # input / output
our $len_t = 10; ###################
our $NR_clstr = 0.3; #
our $NR_clstre = -1; #thresholds
our $g_iden = 1; #
our $opt_aS = 0.0; #
our $opt_aL = 0.0; #
our $circle = 0; #
our $opt_g = 1; ####################
our $blast_exe = "blastall -p blastp -m 8"; #########################
our $prof_exe = "blastpgp -m 8"; #
our $prof_para = "-j 3 -F T -e 0.001 -b 500 -v 500"; #
our $prof_db = ""; #
our $bl_para = "-F T -e 0.000001 -b 100000 -v 100000"; # program
our $bl_STDIN = 1; #
our $keep_bl = 0; #
our $blast_prog= "blastp"; #
our $formatdb = "formatdb"; #########################
our $exec_mode = "local"; #######################
our $num_qsub = 1; #
our $para_no = 1; # compute
our $sh_file = ""; #
our $num_multi_seq = 50; #
our $batch_no_per_node = 100; #######################
our $reformat_seg = 50000;
our $restart_seg = 20000;
our $job = "";
our $job_file = "";
our $date = `date`;
our $restart_in = "";
our $pwd = `pwd`; chop($pwd);
our $db_clstr;
our $db_log;
our $db_out1;
our $seq_dir;
our $bl_dir;
our $blm_dir;
our $restart_file;
our $tmp_db;
our $remote_perl_script;
our $remote_sh_script;
our $bl_path;
our $bl_plus = 1; #### use blast+
our $bl_threads = 1;
our $skip_long = 0;
our %qsub_ids = (); #### a list of qsub ids
our %qstat_xml_data = ();
our @blm8_buffer = ();
our %blm8_data = ();
sub parse_para_etc {
my ($arg, $cmd);
while($arg = shift) {
## input/output:
if ($arg eq "-i") { $db_in = shift; }
elsif ($arg eq "-o") { $db_out = shift; }
elsif ($arg eq "-l") { $len_t = shift; }
## thresholds
elsif ($arg eq "-c") { $NR_clstr = shift; }
elsif ($arg eq "-ce") { $NR_clstre = shift; }
elsif ($arg eq "-G") { $g_iden = shift; }
elsif ($arg eq "-aL") { $opt_aL = shift; }
elsif ($arg eq "-aS") { $opt_aS = shift; }
elsif ($arg eq "-g") { $opt_g = shift; }
elsif ($arg eq "-circle") { $circle = shift; }
elsif ($arg eq "-sl") { $skip_long = shift; }
## program
elsif ($arg eq "-prog") { $blast_prog= shift; }
elsif ($arg eq "-p") { $prof_para = shift; }
elsif ($arg eq "-dprof") { $prof_db = shift; die "option -dprof no longer supported!";}
elsif ($arg eq "-s") { $bl_para = shift; }
elsif ($arg eq "-k") { $keep_bl = shift; }
elsif ($arg eq "-bs") { $bl_STDIN = shift; }
## compute
elsif ($arg eq "-exec") { $exec_mode = shift; }
elsif ($arg eq "-host") { $num_qsub = shift; }
elsif ($arg eq "-para") { $para_no = shift; }
elsif ($arg eq "-shf") { $sh_file = shift; }
elsif ($arg eq "-blp") { $bl_threads = shift; }
elsif ($arg eq "-bat") { $batch_no_per_node = shift; }
## job:
elsif ($arg eq "-rs") { $restart_seg = shift; }
elsif ($arg eq "-rf") { $reformat_seg= shift; }
elsif ($arg eq "-restart") { $restart_in= shift; }
elsif ($arg eq "-J") { $job = shift; $job_file = shift; }
## blast path
elsif ($arg eq "-P") { $bl_path = shift; }
else { print_usage(); exit(); }
}
# speical jobs
if ($job eq "parse_blout") { job_parse_blout(); exit();}
elsif ($job eq "parse_blout_multi") { job_parse_blout_multi(); exit();}
if (not (defined($db_in) and defined($db_out))) {
print_usage(); exit();
}
if ($blast_prog eq "blastn") {
$formatdb = "formatdb -p F";
$blast_exe = "blastall -p blastn -m 8";
}
elsif ($blast_prog eq "megablast") {
$blast_prog = "blastn"; #### back to blastn for blast parser type
$formatdb = "formatdb -p F";
$blast_exe = "megablast -H 100 -D 2 -m 8";
}
elsif ($blast_prog eq "blastpgp") {
$blast_exe = "blastpgp -m 8 -j 3";
}
#### for blast+
if ($bl_plus) {
$formatdb = "makeblastdb -dbtype prot -max_file_sz 8GB";
$blast_exe = "blastp -outfmt 6";
$bl_para = "-seg yes -evalue 0.000001 -num_alignments 100000 -num_threads $bl_threads"; # program
if ($blast_prog eq "blastn") {
$formatdb = "makeblastdb -dbtype nucl -max_file_sz 8GB";
$blast_exe = "blastn -task blastn -outfmt 6";
$bl_para = "-dust yes -evalue 0.000001 -num_alignments 100000 -num_threads $bl_threads"; # program
}
elsif ($blast_prog eq "megablast") {
$blast_prog = "blastn"; #### back to blastn for blast parser type
$formatdb = "makeblastdb -dbtype nucl -max_file_sz 8GB";
$blast_exe = "blastn -task megablast -outfmt 6";
$bl_para = "-dust yes -evalue 0.000001 -num_alignments 100000 -num_threads $bl_threads"; # program
}
elsif ($blast_prog eq "blastpgp") {
$blast_exe = "psiblast -outfmt 6 -num_iterations 3 -num_threads $bl_threads";
}
}
if ($bl_path) {
$blast_exe = "$bl_path/$blast_exe";
$formatdb = "$bl_path/$formatdb";
}
(-e $db_in) || die "No input";
($db_out) || die "No output";
$db_clstr = "$db_out.clstr";
$db_log = "$db_out.log";
$db_out1 = "$db_out.out";
$seq_dir = "$db_in-seq";
$bl_dir = "$db_in-bl";
$blm_dir = "$db_in-blm";
$restart_file =" $db_out.restart";
$tmp_db = "$db_in.$pid";
$remote_perl_script = "$tmp_db-bl.pl";
$remote_sh_script = "$tmp_db-bl.sh";
$cmd = `mkdir $bl_dir $blm_dir $seq_dir`;
write_remote_perl_script();
write_remote_sh_script();
return;
}
########## END parse_para_etc
sub read_db {
my $des = "";
my $seq = "";
my $ll;
open(DBIN, $db_in) || die "Can not open $db_in";
while($ll=<DBIN>){
chop($ll);
if ($ll =~ /^>/) {
$seq =~ s/\s//g;
if (length($seq) > $len_t) { add_seq($des, $seq); }
$des = $ll; $seq = "";
}
else { $seq .= $ll; }
}
$seq =~ s/\s//g;
if (length($seq) > $len_t) { add_seq($des, $seq); }
close(DBIN);
($NR_no >=1 ) || die "No sequence readin";
print OUTT "Total seqs $NR_no in $db_in\n";
return;
}
########## END read_db
sub add_seq {
my ($des, $seq) = @_;
$des =~ s/\s.+$//;
push(@seqs, $seq);
push(@dess, $des);
push(@lens, length($seq));
push(@idens, 0);
push(@passeds,0);
push(@NR_clstr_nos,0);
push(@in_bg, 0);
$NR_no++;
return;
}
########## END add_seq
sub open_LOG {
open(OUTT, ">> $db_out1") || die "can not open $db_out1";
select(OUTT); $|++; ### file handle flush
print OUTT "Started $date";
open(LOG, ">> $db_log") || die "Can not open $db_log";
select(LOG); $|++; ### file handle flush
select(STDOUT);
return;
}
########## END open_LOG
sub write_LOG {
my $txt=shift;
print LOG "$txt\n";
}
{## use static variables
my $last_NR90_no=0;
my $last_NR_passed=0;
sub watch_progress {
my ($i0, $NR90_no, $NR_passed, $NR_no, $flag) = @_;
my $i1 = $i0+1;
if ( $i1 % 10 == 0 ) {
print OUTT ".";
$flag = 1 if ( $i1 % 100 == 0 );
}
if ($flag) {
my $t1 = (int($NR_passed/$NR_no*10000)) / 100;
my $t90 = $NR90_no - $last_NR90_no;
my $tno = $NR_passed - $last_NR_passed;
my ($tu, $ts, $cu, $cs) = times();
my $tt = $tu + $ts + $cu + $cs;
print OUTT
"$i1 finished $NR90_no clusters $NR_passed passed $t90/$tno clstr/passed $t1% done $tt cpu\n";
$last_NR90_no = $NR90_no;
$last_NR_passed = $NR_passed;
}
return;
}
}
sub close_LOG {
my $date = `date`; print OUTT "Completed $date\n";
my $total_cpu = total_remote_cpu();
print OUTT "Total CPUs on remote hosts: $total_cpu\n";
close(OUTT);
close(LOG);
return;
}
########## END close_LOG
###### need to change to read dir because
sub total_remote_cpu {
my ($i, $j, $k, $ll);
my $tt = 0;
for ($j=0; $j<$num_qsub; $j++) {
open(TCPU, "$seq_dir/host.$j.cpu") || next;
while($ll = <TCPU>) {
chop($ll);
$tt += $ll;
}
close(TCPU);
}
return $tt;
}
########## END total_remote_cpu
#### process m8 format output from multi-query search
sub job_parse_blout_multi{
my ($i, $j, $k, $tfh, $ll, $t1, $t2);
$tfh="BLM8";
open($tfh, $job_file) || die "can not open $job_file";
@blm8_buffer = ();
my $last_id = "";
my $this_id = "";
my $tquery;
while($ll = <$tfh>) {
next if ($ll =~ /^#/);
($this_id, $t1) = split(/\s+/, $ll, 2);
if (@blm8_buffer and ($this_id ne $last_id)) { #### blast results of last query
my @hits = process_blout_blastp_blastn();
$tquery = (split(/\./, $last_id))[0];
my $no1 = $#hits+1;
print ">$tquery\t$no1\n";
foreach $i (@hits) {
print join("\t", @{$i}), "\n";
}
print "#\n";
@blm8_buffer = ();
}
push(@blm8_buffer, $ll);
$last_id = $this_id;
}
if (@blm8_buffer and ($this_id ne $last_id)) { #### blast results of last query
my @hits = process_blout_blastp_blastn();
$tquery = (split(/\./, $last_id))[0];
my $no1 = $#hits+1;
print ">$tquery\t$no1\n";
foreach $i (@hits) {
print join("\t", @{$i}), "\n";
}
print "#\n";
@blm8_buffer = ();
}
close($tfh);
return;
}
########## END job_parse_blout_multi
sub job_parse_blout {
my ($i, $j, $k);
my @hits = process_blout_blastp_blastn($job_file);
open(BLOUT2, "> $job_file.out") || return;
foreach $i (@hits) {
print BLOUT2 join("\t", @{$i}), "\n";
}
print BLOUT2 "#\n";
close(BLOUT2);
return;
}
########## END job_parse_blout
sub write_restart {
my ($i0, $i, $j, $k);
open(RES, "> $restart_file") || die;
for ($i0=0; $i0<$NR_no; $i0++) {
$i = $NR_idx[$i0];
print RES "$i\t$NR_clstr_nos[$i]\t$idens[$i]\t$passeds[$i]\n";
}
close(RES);
return;
}
########## END write_restart
sub read_restart {
my ($ii, $i0, $i, $j, $k, $ll);
my @lls;
open(RESIN, $restart_in) || die;
$NR_passed = 0;
$NR90_no = 0;
$ii = -1;
$i0 = 0;
while($ll = <RESIN>) {
chop($ll);
@lls = split(/\t/,$ll);
$i = $lls[0];
$NR_clstr_nos[$i] = $lls[1];
$idens[$i] = $lls[2];
$passeds[$i] = $lls[3];
$NR_passed++ if ($lls[3]);
if ($lls[2] eq "*") { #rep
$NR90_no++;
$ii = $i0 if ($lls[3]);
}
$NR_idx[$i0] = $i;
$i0++; # idx of sorted , see write_restart
}
close(RESIN);
$ii++; # $ii to be last rep processed
return $ii;
}
########## END read_restart
sub write_db_clstr {
my ($i0, $i, $j, $k);
my @NR90_seq = ();
for ($i=0; $i<$NR90_no; $i++) { $NR90_seq[$i] = []; }
for ($i0=0; $i0<$NR_no; $i0++) {
$i = $NR_idx[$i0];
next unless ($passeds[$i]);
$j = $NR_clstr_nos[$i];
next unless ($j < $NR90_no);
push(@{$NR90_seq[$j]}, $i);
}
open(DBCLS, "> $db_clstr") || die "Can not write $db_clstr";
for ($i=0; $i<$NR90_no; $i++) {
print DBCLS ">Cluster $i\n";
$k = 0;
foreach $j (@{ $NR90_seq[$i] }) {
my $des = (split(/\s+/,$dess[$j]))[0];
print DBCLS "$k\t$lens[$j]"."aa, $des... ";
if ($idens[$j] eq "*") { print DBCLS "*\n"; }
else { print DBCLS "at $idens[$j]\n";}
$k++;
}
}
close(DBCLS);
@NR90_seq=();
return;
}
########## END write_db_clstr
sub remove_raw_blout {
my $NR_sofar = shift;
my ($i0, $i, $j, $k, $cmd);
return if ($keep_bl);
for ($i0=$NR_sofar; $i0>=0; $i0--) {
$i = $NR_idx[$i0];
next unless $passeds[$i];
next unless ($idens[$i] eq "*"); #only reps have blout
my $fout = "$bl_dir/$i";
last unless (-e "$fout.out"); #removed from last call
if (not $bl_STDIN) { $cmd = `rm -f $fout`; }
$cmd = `rm -f $bl_dir/$i.out`;
}
return;
}
########## END remove_raw_blout
sub remove_raw_blout_bg {
my $NR_sofar = shift;
my ($i0, $i, $j, $k, $cmd);
return if ($keep_bl);
my $tmp_sh_script = "$tmp_db-rm-$NR_sofar.sh";
open(OUTRM, ">$tmp_sh_script") || die "can not write to $tmp_sh_script";
for ($i0=$NR_sofar; $i0>=0; $i0--) {
$i = $NR_idx[$i0];
next unless $passeds[$i];
next unless ($idens[$i] eq "*"); #only reps have blout
my $fout = "$bl_dir/$i";
last unless (-e "$fout.out"); #removed from last call
if (not $bl_STDIN) { print OUTRM "rm -f $fout\n"; }
print OUTRM "rm -f $bl_dir/$i.out";
}
print OUTRM "rm -f $tmp_sh_script\n"; ## remove self
close(OUTRM);
sleep(3);
$cmd = `sh $tmp_sh_script >/dev/null 2>&1 &`;
return;
}
########## END remove_raw_blout_bg
sub fish_other_homolog_multi {
my ($i, $j, $k, $i0, $j0, $k0);
$id = shift; # real idx, not sorted idx
my @hits = ();
if (defined($blm8_data{$id})) {
@hits = @{$blm8_data{$id}};
}
my $rep_len = $lens[$id];
foreach $i (@hits) {
my $id1 = $i->[0];
next unless ($id1 < $NR_no);
next if ($idens[$id1] eq "*"); #existing reps
next if ($lens[$id1] > $rep_len); # in opt_g=1 mode, preventing it from being clustered into short rep
if ( $passeds[$id1] ) { #### if this hit is better -g 1 mode
my $old_e = (split(/\//,$idens[$id1]))[0];
if ($i->[3] < $old_e) {
$idens[$id1] = "$i->[3]/$i->[2]aa/$i->[1]%";
$passeds[$id1] = 1;
$NR_clstr_nos[$id1] = $NR90_no;
}
next;
}
$idens[$id1] = "$i->[3]/$i->[2]aa/$i->[1]%";
$passeds[$id1] = 1;
$NR_clstr_nos[$id1] = $NR90_no;
$NR_passed++;
}
if (defined($blm8_data{$id})) {
delete $blm8_data{$id};
}
return;
}
########## END fish_other_homolog_multi
sub fish_other_homolog {
my ($i, $j, $k, $i0, $j0, $k0);
$id = shift; # real idx, not sorted idx
my @hits = ();
wait_blast_out("$bl_dir/$id.out");
open(BLPOUT, "$bl_dir/$id.out") || return;
while($i=<BLPOUT>) {
last if ($i =~ /^#/);
chop($i);
push(@hits, [split(/\t/,$i)]);
}
close(BLPOUT);
my $rep_len = $lens[$id];
foreach $i (@hits) {
my $id1 = $i->[0];
next unless ($id1 < $NR_no);
next if ($idens[$id1] eq "*"); #existing reps
next if ($lens[$id1] > $rep_len); # in opt_g=1 mode, preventing it from being clustered into short rep
if ( $passeds[$id1] ) { #### if this hit is better -g 1 mode
my $old_e = (split(/\//,$idens[$id1]))[0];
if ($i->[3] < $old_e) {
$idens[$id1] = "$i->[3]/$i->[2]aa/$i->[1]%";
$passeds[$id1] = 1;
$NR_clstr_nos[$id1] = $NR90_no;
}
next;
}
$idens[$id1] = "$i->[3]/$i->[2]aa/$i->[1]%";
$passeds[$id1] = 1;
$NR_clstr_nos[$id1] = $NR90_no;
$NR_passed++;
}
return;
}
########## END fish_other_homolog
########### if a hit has multiple HSPs on both + - strands
########### keep only the HSPs, whose strand is same as the top HSP
sub keep_strand_with_top_hsp {
my $self = shift;
my ($i,$j,$k);
my %id_2_strand = ();
my @new_sbj = ();
my $new_no = 0;
for ($i=0; $i<$self->{no}; $i++) {
my $p = $self->{sbj}->[$i];
my ($id1, $len_sub) = split(/\./, $p->{id});
if (not defined($id_2_strand{$id1})) {
$id_2_strand{$id1} = $p->{frame};
}
if ($p->{frame} eq $id_2_strand{$id1}) { #### this stand is same as the top strand
push(@new_sbj, $self->{sbj}->[$i]);
$new_no++;
}
}
$self->{no} = $new_no;
$self->{sbj} = [@new_sbj];
}
########## END keep_strand_with_top_hsp
########## for blastpgp -j no (no>1)
########## keep hits from the last round
sub keep_hsp_of_last_round {
my $self = shift;
my ($i,$j,$k);
my @new_sbj = ();
my $new_no = 0;
my $last_score = 9999999*9999999*9999999; # a big one
for ($i=0; $i<$self->{no}; $i++) {
my $p = $self->{sbj}->[$i];
my $score = $p->{score};
if ($score > $last_score) { ## this is new round of hits
@new_sbj = ();
$new_no = 0;
}
$last_score = $score;
push(@new_sbj, $self->{sbj}->[$i]);
$new_no++;
}
$self->{no} = $new_no;
$self->{sbj} = [@new_sbj];
}
########## END keep_hsp_of_last_round
########## if a query hit a subject with multiple HSPs
########## only the top HSP is kept
sub keep_top_hsp {
my $self = shift;
my ($i,$j,$k);
my %id_exist = ();
my @new_sbj = ();
my $new_no = 0;
for ($i=0; $i<$self->{no}; $i++) {
my $p = $self->{sbj}->[$i];
my ($id1, $len_sub) = split(/\./, $p->{id});
next unless ($len_sub >0) ;
if (not defined($id_exist{$id1})) {
$id_exist{$id1} = 1;
push(@new_sbj, $self->{sbj}->[$i]);
$new_no++;
}
}
$self->{no} = $new_no;
$self->{sbj} = [@new_sbj];
}
########## keep_top_hsp
########## let the top hsp to start at 0 for both query and subject
########## i.e. the begining of HSP to be new original - coordinate 0
########## then reset all other HSPs' alignment coordinates
sub reset_alignment_coor_for_circle_seq {
my $self = shift;
my ($i,$j,$k);
my $last_id = "";
$j = 0;
my $hsp_count = 0; # number of HSPs for a subject
for ($i=0; $i<$self->{no}; $i++) {
my $p = $self->{sbj}->[$i];
my ($id1, $len_sub) = split(/\./, $p->{id});
if ($id1 ne $last_id) {
if ($hsp_count > 1) { # it is necessary to reset coordinate when at least 2 HSP
my $p_top_hsp = $self->{sbj}->[$j];
my $len_q = (split(/\./, $p_top_hsp->{qid}))[1];
my $len_s = (split(/\./, $p_top_hsp->{id}))[1];
my $ref_q = ($p_top_hsp->{qfrom} < $p_top_hsp->{qend}) ? $p_top_hsp->{qfrom} : $p_top_hsp->{qend};
my $ref_s = ($p_top_hsp->{sfrom} < $p_top_hsp->{send}) ? $p_top_hsp->{sfrom} : $p_top_hsp->{send};
for ($k = $j; $k<$j+$hsp_count; $k++) {
$self->{sbj}->[$k]->{qfrom} -= $ref_q; if ($self->{sbj}->[$k]->{qfrom} < 0) {$self->{sbj}->[$k]->{qfrom} += $len_q;}
$self->{sbj}->[$k]->{qend} -= $ref_q; if ($self->{sbj}->[$k]->{qend} < 0) {$self->{sbj}->[$k]->{qend} += $len_q;}
$self->{sbj}->[$k]->{sfrom} -= $ref_s; if ($self->{sbj}->[$k]->{sfrom} < 0) {$self->{sbj}->[$k]->{sfrom} += $len_s;}
$self->{sbj}->[$k]->{send} -= $ref_s; if ($self->{sbj}->[$k]->{send} < 0) {$self->{sbj}->[$k]->{send} += $len_s;}
}
}
$j = $i;
$hsp_count = 0;
}
$last_id = $id1;
$hsp_count++;
}
#last subject
if ($hsp_count > 1) { # it is necessary to reset coordinate when at least 2 HSP
my $p_top_hsp = $self->{sbj}->[$j];
my $len_q = (split(/\./, $p_top_hsp->{qid}))[1];
my $len_s = (split(/\./, $p_top_hsp->{id}))[1];
my $ref_q = ($p_top_hsp->{qfrom} < $p_top_hsp->{qend}) ? $p_top_hsp->{qfrom} : $p_top_hsp->{qend};
my $ref_s = ($p_top_hsp->{sfrom} < $p_top_hsp->{send}) ? $p_top_hsp->{sfrom} : $p_top_hsp->{send};
for ($k = $j; $k<$j+$hsp_count; $k++) {
$self->{sbj}->[$k]->{qfrom} -= $ref_q; if ($self->{sbj}->[$k]->{qfrom} < 0) {$self->{sbj}->[$k]->{qfrom} += $len_q;}
$self->{sbj}->[$k]->{qend} -= $ref_q; if ($self->{sbj}->[$k]->{qend} < 0) {$self->{sbj}->[$k]->{qend} += $len_q;}
$self->{sbj}->[$k]->{sfrom} -= $ref_s; if ($self->{sbj}->[$k]->{sfrom} < 0) {$self->{sbj}->[$k]->{sfrom} += $len_s;}
$self->{sbj}->[$k]->{send} -= $ref_s; if ($self->{sbj}->[$k]->{send} < 0) {$self->{sbj}->[$k]->{send} += $len_s;}
}
}
return;
}
########## reset_alignment_coor_for_circle_seq
sub process_blout_blastp_blastn {
my ($i, $j, $k, $i0, $j0, $k0);
my $blout = shift;
my @blhits = ();
#### need $len_rep
my $len_rep = 0;
my $bl = defined($blout) ? readblast_m8("", $blout) : readblast_m8_buffer();
if ($blast_prog eq "blastn") { keep_strand_with_top_hsp($bl); }
if (($blast_prog eq "blastpgp") and (not $prof_db)) {keep_hsp_of_last_round($bl); }
if ($g_iden == 0 ) { #### Local identity
keep_top_hsp($bl); #### local alignment, only the top HSP
for ($i=0; $i<$bl->{no}; $i++) {
my $p = $bl->{sbj}->[$i];
my ($id1, $len_sub) = split(/\./, $p->{id});
my $frame = $p->{frame};
if (not $len_rep) {$len_rep = (split(/\./,$p->{qid}))[1]; }
my $iden = $p->{iden};
next unless (($len_sub >0) and ($len_rep>0));
my $cov_aS = $p->{alnln} / $len_sub;
my $cov_aL = $p->{alnln} / $len_rep;
my $exp1 = $p->{expect};
if (($iden/100 > $NR_clstr or $exp1<$NR_clstre) and ($cov_aS >= $opt_aS) and ($cov_aL >= $opt_aL) ) {
push(@blhits, [$id1, $iden, $p->{alnln}, $exp1, $frame]);
}
}
return @blhits;
} #### END if ($g_iden == 0 )
else { #### Global idnetity
if (($blast_prog eq "blastn") and $circle) { reset_alignment_coor_for_circle_seq($bl); }
#### get colinear non-overlapping HSPs
my @hsp = (); #### [id, len, qfrom, qend, sbegin, send, expect]
my $iden_letters = 0;
my $aln_letters = 0;
my @aln_lens = ();
my $hsp_no = 0;
for ($i=0; $i<$bl->{no}; $i++) {
my $p = $bl->{sbj}->[$i];
my ($id1, $len_sub) = split(/\./, $p->{id});
my $frame = $p->{frame};
if (not $len_rep) {$len_rep = (split(/\./,$p->{qid}))[1]; }
next unless (($len_sub >0) and ($len_rep>0));
if ($hsp_no) {
if ($id1 ne $hsp[0]->[0]) {
#### 1. parse previous subject's HSPs
my $iden = int($iden_letters / $hsp[0]->[1] * 10000)/100;
my $cov_aS = $aln_letters / $hsp[0]->[1];
my $cov_aL = $aln_letters / $len_rep;
my $exp1 = $hsp[0]->[6];
my $frame = $hsp[0]->[7];
if (($iden/100 > $NR_clstr or $exp1<$NR_clstre) and ($cov_aS >= $opt_aS) and ($cov_aL >= $opt_aL) ) {
#push(@blhits, [$hsp[0]->[0], $iden, $aln_letters, $exp1, $frame]);
push(@blhits, [$hsp[0]->[0], $iden, join(":", @aln_lens), $exp1, $frame]);
}
#### 2. init some values
@hsp = ();
$iden_letters = 0;
$aln_letters = 0;
@aln_lens = ();
$hsp_no = 0;
}
}
#check whether overlap with previous high score HSPs
my $overlap_flag = 0;
for ($j=0; $j<$hsp_no; $j++) {
if (overlap1($p->{qfrom}, $p->{qend}, $hsp[$j]->[2], $hsp[$j]->[3])) { $overlap_flag = 1; last; }
if (overlap1($p->{sfrom}, $p->{send}, $hsp[$j]->[4], $hsp[$j]->[5])) { $overlap_flag = 1; last; }
}
next if ($overlap_flag);
#check whether this HSP cross with previous high score HSPs
my $cross_flag = 0;
for ($j=0; $j<$hsp_no; $j++) {
if (cross1($p->{qfrom}, $p->{qend}, $hsp[$j]->[2], $hsp[$j]->[3],
$p->{sfrom}, $p->{send}, $hsp[$j]->[4], $hsp[$j]->[5])) {
$cross_flag = 1; last;
}
}
next if ($cross_flag);
push(@hsp, [$id1, $len_sub, $p->{qfrom}, $p->{qend}, $p->{sfrom}, $p->{send}, $p->{expect}, $p->{frame}]);
$iden_letters += int($p->{iden} * $p->{alnln} / 100);
$aln_letters += $p->{alnln};
push(@aln_lens, $p->{alnln});
$hsp_no++;
}
if ($hsp_no) { #last record
#### 1. parse previous subject's HSPs
my $iden = int($iden_letters / $hsp[0]->[1] * 10000)/100;
my $cov_aS = $aln_letters / $hsp[0]->[1];
my $cov_aL = $aln_letters / $len_rep;
my $exp1 = $hsp[0]->[6];
my $frame = $hsp[0]->[7];
if (($iden/100 > $NR_clstr or $exp1<$NR_clstre) and ($cov_aS >= $opt_aS) and ($cov_aL >= $opt_aL) ) {
#push(@blhits, [$hsp[0]->[0], $iden, $aln_letters, $exp1, $frame]);
push(@blhits, [$hsp[0]->[0], $iden, join(":", @aln_lens), $exp1, $frame]);
}
}
return @blhits;
}
}
########## END process_blout_blastp_blastn
sub overlap1 {
my ($b1, $e1, $b2, $e2) = @_;
my $t; ###
if ($e1 < $b1) { $t = $e1; $e1 = $b1; $b1 = $t; }
if ($e2 < $b2) { $t = $e2; $e2 = $b2; $b2 = $t; }
return 0 if ($e2 < $b1);
return 0 if ($b2 > $e1);
return ( ($e1<$e2)? $e1:$e2 )-( ($b1>$b2)? $b1:$b2);
}
########## END overlap1
## modified on 2013_0818 to hancle +- frames
sub cross1 {
my ($q_b1, $q_e1, $q_b2, $q_e2,
$s_b1, $s_e1, $s_b2, $s_e2) = @_;
my $fr_q1 = ($q_b1 < $q_e1) ? 1 : -1;
my $fr_q2 = ($q_b2 < $q_e2) ? 1 : -1;
my $fr_s1 = ($s_b1 < $s_e1) ? 1 : -1;
my $fr_s2 = ($s_b2 < $s_e2) ? 1 : -1;
my $fr1 = $fr_q1 * $fr_s1;
my $fr2 = $fr_q2 * $fr_s2;
return 1 if (($fr1 * $fr2) < 0); # one ++ and one +-
my $t;
if ($q_e1 < $q_b1) { $t = $q_e1; $q_e1 = $q_b1; $q_b1 = $t; }
if ($q_e2 < $q_b2) { $t = $q_e2; $q_e2 = $q_b2; $q_b2 = $t; }
if ($s_e1 < $s_b1) { $t = $s_e1; $s_e1 = $s_b1; $s_b1 = $t; }
if ($s_e2 < $s_b2) { $t = $s_e2; $s_e2 = $s_b2; $s_b2 = $t; }
# after above transformation
# 0 q_b1 q_e1 q_b2 q_e2 qlen
# query 5' ====================================================================
# match |||||||||||||||| |||||||||||||
# subject 5' ========================================================================>>>>>> frame +
# 0 s_b1 s_e1 s_b2 s_e2 slen
# match |||||||||||||||| |||||||||||||
# subject 3' ========================================================================>>>>>> frame -
# slen s_e1 s_b1 s_e2 s_b2 0
if (($fr1 > 0) and ($fr2>0)) { # both ++
return ( (($q_b2-$q_b1)*($s_b2-$s_b1) <0) ? 1 : 0);
}
else { # both --
return ( (($q_b2-$q_b1)*($s_e1-$s_e2) <0) ? 1 : 0);
}
}
########## END cross1
## modified on 2013_0818 to hancle +- frames
sub cross1_before_2013_0818 {
my ($q_b1, $q_e1, $q_b2, $q_e2,
$s_b1, $s_e1, $s_b2, $s_e2) = @_;
my $t;
if ($q_e1 < $q_b1) { $t = $q_e1; $q_e1 = $q_b1; $q_b1 = $t; }
if ($q_e2 < $q_b2) { $t = $q_e2; $q_e2 = $q_b2; $q_b2 = $t; }
if ($s_e1 < $s_b1) { $t = $s_e1; $s_e1 = $s_b1; $s_b1 = $t; }
if ($s_e2 < $s_b2) { $t = $s_e2; $s_e2 = $s_b2; $s_b2 = $t; }
return ( (($q_b2-$q_b1)*($s_b2-$s_b1) <0) ? 1 : 0);
}
########## END cross1
sub readblast_m8_buffer {
my ($i, $j, $k, $ll, $no);
my @this_sbj = ();
$no = 0;
while($ll = shift @blm8_buffer) {
chop($ll);
my @lls = split(/\t/,$ll);
my $frame = "";
$frame .= ($lls[6] < $lls[7]) ? "+" : "-";
$frame .= ($lls[8] < $lls[9]) ? "+" : "-";
next unless ($lls[0] and $lls[1]);
$this_sbj[$no] = {
'qid' => $lls[0],
'id' => $lls[1],
'iden' => $lls[2],
'alnln' => $lls[3],
'ms' => $lls[4],
'gap' => $lls[5],
'qfrom' => $lls[6],
'qend' => $lls[7],
'sfrom' => $lls[8],
'send' => $lls[9],
'expect' => $lls[10],
'score' => $lls[11],
'frame' => $frame,
};
$no++;
# BLASTP 2.2.24 [Aug-08-2010]
# Query: gi|388328107|pdb|4DDG|A Chain A, Crystal Structure Of Human Otub1UBCH5B~UBUB
# Database: pdbaa.fa
# Fields: Query id, Subject id, % identity, alignment length, mismatches, gap openings, q. start, q. end, s. start, s. end, e-value, bit score
#gi|388328107|pdb|4DDG|A gi|388328107|pdb|4DDG|A 91.81 171 9 3 6 171 1 171 6e-89 323
#gi|388328107|pdb|4DDG|A gi|388328107|pdb|4DDG|A 96.51 86 3 0 235 320 155 240 2e-41 166
}
my $self = {
'no' => $no,
'sbj' => [@this_sbj],
};
return $self;
}
########## END readblast_m8
sub readblast_m8 {
my ($i, $j, $k, $ll, $no);
my ($q_seq, $filename) = @_;
my $fh = "BL" ;
if ($bl_STDIN) { $fh = "STDIN"; }
else { open($fh, $filename) || return; }
my @this_sbj = ();
$no = 0;
while($ll = <$fh>) {
chop($ll);
my @lls = split(/\t/,$ll);
my $frame = "";
$frame .= ($lls[6] < $lls[7]) ? "+" : "-";
$frame .= ($lls[8] < $lls[9]) ? "+" : "-";
next unless ($lls[0] and $lls[1]);
$this_sbj[$no] = {
'qid' => $lls[0],
'id' => $lls[1],
'iden' => $lls[2],
'alnln' => $lls[3],
'ms' => $lls[4],
'gap' => $lls[5],
'qfrom' => $lls[6],
'qend' => $lls[7],
'sfrom' => $lls[8],
'send' => $lls[9],
'expect' => $lls[10],
'score' => $lls[11],
'frame' => $frame,
};
$no++;
# BLASTP 2.2.24 [Aug-08-2010]
# Query: gi|388328107|pdb|4DDG|A Chain A, Crystal Structure Of Human Otub1UBCH5B~UBUB
# Database: pdbaa.fa
# Fields: Query id, Subject id, % identity, alignment length, mismatches, gap openings, q. start, q. end, s. start, s. end, e-value, bit score
#gi|388328107|pdb|4DDG|A gi|388328107|pdb|4DDG|A 91.81 171 9 3 6 171 1 171 6e-89 323
#gi|388328107|pdb|4DDG|A gi|388328107|pdb|4DDG|A 96.51 86 3 0 235 320 155 240 2e-41 166
}
close($fh) if (not $bl_STDIN);
my $self = {
'no' => $no,
'sbj' => [@this_sbj],
};
return $self;
}
########## END readblast_m8
sub blast_formatdb {
my ($i0, $i, $j, $k, $len1);
open(FDB, "> $tmp_db") || die;
$j = 0;
$len1 = 0;
for ($i0=$NR_no-1; $i0>=0; $i0--) { ### from shortest to longest
$i = $NR_idx[$i0];
last if ($idens[$i] eq "*"); ### last if reach rep
next if ($lens[$i] < $opt_aL_lower_band);
next if ($passeds[$i] and ($opt_g==0));
my $seq = $seqs[$i];
$seq =~ s/(.{70})/$1\n/g;
$seq =~ s/\n$//;
#print FDB ">$i $dess[$i]\n$seq\n";
print FDB ">$i.$lens[$i]\n$seq\n";
$j++;
$len1 += $lens[$i];
}
close(FDB);
while(1) {
opendir(SEQDB, $seq_dir) || next;
my @leftseqs = grep {/lock/} readdir(SEQDB);
closedir(SEQDB);
last unless @leftseqs;
sleep(3);
}
return(0, 0) unless ($j > 0);
my $cmd_line = "$formatdb -i $tmp_db";
$cmd_line = "$formatdb -in $tmp_db" if ($bl_plus);
my $cmd = `$cmd_line`;
((-e "$tmp_db.phr") and (-e "$tmp_db.pin") and (-e "$tmp_db.psq")) ||