forked from weizhongli/cdhit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdhit-common.c++
4408 lines (4076 loc) · 142 KB
/
cdhit-common.c++
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
// =============================================================================
// CD-HI/CD-HIT
//
// Cluster Database at High Identity
//
// CD-HIT clusters protein sequences at high identity threshold.
// This program can remove the high sequence redundance efficiently.
//
// program written by
// Weizhong Li
// UCSD, San Diego Supercomputer Center
// La Jolla, CA, 92093
// Email [email protected]
//
// at
// Adam Godzik's lab
// The Burnham Institute
// La Jolla, CA, 92037
// Email [email protected]
//
// modified by:
// Limin Fu
// Center for Research in Biological Systems (CRBS), UCSD
// La Jolla, CA, 92093
// Email: [email protected], [email protected]
// =============================================================================
#include "cdhit-common.h"
#include<valarray>
#include<stdint.h>
#include<assert.h>
#include<limits.h>
#ifndef NO_OPENMP
#include<omp.h>
#define WITH_OPENMP " (+OpenMP)"
#else
#define WITH_OPENMP ""
#define omp_set_num_threads(T) (T = T)
#define omp_get_thread_num() 0
#endif
//class function definition
const char aa[] = {"ARNDCQEGHILKMFPSTWYVBZX"};
//{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,2,6,20};
int aa2idx[] = {0, 2, 4, 3, 6, 13,7, 8, 9,20,11,10,12, 2,20,14,
5, 1,15,16,20,19,17,20,18, 6};
// idx for A B C D E F G H I J K L M N O P
// Q R S T U V W X Y Z
// so aa2idx[ X - 'A'] => idx_of_X, eg aa2idx['A' - 'A'] => 0,
// and aa2idx['M'-'A'] => 12
int BLOSUM62[] = {
4, // A
-1, 5, // R
-2, 0, 6, // N
-2,-2, 1, 6, // D
0,-3,-3,-3, 9, // C
-1, 1, 0, 0,-3, 5, // Q
-1, 0, 0, 2,-4, 2, 5, // E
0,-2, 0,-1,-3,-2,-2, 6, // G
-2, 0, 1,-1,-3, 0, 0,-2, 8, // H
-1,-3,-3,-3,-1,-3,-3,-4,-3, 4, // I
-1,-2,-3,-4,-1,-2,-3,-4,-3, 2, 4, // L
-1, 2, 0,-1,-3, 1, 1,-2,-1,-3,-2, 5, // K
-1,-1,-2,-3,-1, 0,-2,-3,-2, 1, 2,-1, 5, // M
-2,-3,-3,-3,-2,-3,-3,-3,-1, 0, 0,-3, 0, 6, // F
-1,-2,-2,-1,-3,-1,-1,-2,-2,-3,-3,-1,-2,-4, 7, // P
1,-1, 1, 0,-1, 0, 0, 0,-1,-2,-2, 0,-1,-2,-1, 4, // S
0,-1, 0,-1,-1,-1,-1,-2,-2,-1,-1,-1,-1,-2,-1, 1, 5, // T
-3,-3,-4,-4,-2,-2,-3,-2,-2,-3,-2,-3,-1, 1,-4,-3,-2,11, // W
-2,-2,-2,-3,-2,-1,-2,-3, 2,-1,-1,-2,-1, 3,-3,-2,-2, 2, 7, // Y
0,-3,-3,-3,-1,-2,-2,-3,-3, 3, 1,-2, 1,-1,-2,-2, 0,-3,-1, 4, // V
-2,-1, 3, 4,-3, 0, 1,-1, 0,-3,-4, 0,-3,-3,-2, 0,-1,-4,-3,-3, 4, // B
-1, 0, 0, 1,-3, 3, 4,-2, 0,-3,-3, 1,-1,-3,-1, 0,-1,-3,-2,-2, 1, 4, // Z
0,-1,-1,-1,-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-2, 0, 0,-2,-1,-1,-1,-1,-1 // X
//A R N D C Q E G H I L K M F P S T W Y V B Z X
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2 6 20
};
int na2idx[] = {0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 3, 3, 4, 4, 4, 4, 4};
// idx for A B C D E F G H I J K L M N O P
// Q R S T U V W X Y Z
// so aa2idx[ X - 'A'] => idx_of_X, eg aa2idx['A' - 'A'] => 0,
// and aa2idx['M'-'A'] => 4
int BLOSUM62_na[] = {
2, // A
-2, 2, // C
-2,-2, 2, // G
-2,-2,-2, 2, // T
-2,-2,-2, 1, 2, // U
-2,-2,-2,-2,-2, 1, // N
0, 0, 0, 0, 0, 0, 1 // X
//A C G T U N X
//0 1 2 3 3 4 5
};
void setaa_to_na();
struct TempFile
{
FILE *file;
char buf[512];
TempFile( const char *dir = NULL ){
int len = dir ? strlen( dir ) : 0;
assert( len < 400 );
buf[0] = 0;
if( len ){
strcat( buf, dir );
if( buf[len-1] != '/' && buf[len-1] != '\\' ){
buf[len] = '/';
len += 1;
}
}
strcat( buf, "cdhit.temp." );
len += 11;
sprintf( buf + len, "%p", this );
file = fopen( buf, "w+" );
}
~TempFile(){
if( file ){
fclose( file );
remove( buf );
}
}
};
struct TempFiles
{
NVector<TempFile*> files;
~TempFiles(){ Clear(); }
void Clear(){
int i;
#pragma omp critical
{
for(i=0; i<files.size; i++) if( files[i] ) delete files[i];
files.Clear();
}
}
};
const char *temp_dir = "";
TempFiles temp_files;
FILE* OpenTempFile( const char *dir = NULL )
{
TempFile *file = new TempFile( dir );
#pragma omp critical
{
temp_files.files.Append( file );
}
return file->file;
}
static void CleanUpTempFiles()
{
if( temp_files.files.Size() ) printf( "Clean up temporary files ...\n" );
temp_files.Clear();
}
int NAA1 ;
int NAA2 ;
int NAA3 ;
int NAA4 ;
int NAA5 ;
int NAA6 ;
int NAA7 ;
int NAA8 ;
int NAA9 ;
int NAA10;
int NAA11;
int NAA12;
int NAAN_array[13] = { 1 };
void InitNAA( int max )
{
NAA1 = NAAN_array[1] = max;
NAA2 = NAAN_array[2] = NAA1 * NAA1;
NAA3 = NAAN_array[3] = NAA1 * NAA2;
NAA4 = NAAN_array[4] = NAA2 * NAA2;
NAA5 = NAAN_array[5] = NAA2 * NAA3;
NAA6 = NAAN_array[6] = NAA3 * NAA3;
NAA7 = NAAN_array[7] = NAA3 * NAA4;
NAA8 = NAAN_array[8] = NAA4 * NAA4;
NAA9 = NAAN_array[9] = NAA4 * NAA5;
NAA10 = NAAN_array[10] = NAA5 * NAA5;
NAA11 = NAAN_array[11] = NAA5 * NAA6;
NAA12 = NAAN_array[12] = NAA6 * NAA6;
}
extern Options options;
ScoreMatrix mat;
Vector<int> Comp_AAN_idx;
void make_comp_iseq(int len, char *iseq_comp, char *iseq)
{
int i, c[6] = {3,2,1,0,4,5};
for (i=0; i<len; i++) iseq_comp[i] = c[ (int)iseq[len-i-1] ];
} // make_comp_iseq
bool Options::SetOptionCommon( const char *flag, const char *value )
{
int intval = atoi( value );
if (strcmp(flag, "-i" ) == 0) input = value;
else if (strcmp(flag, "-j" ) == 0) input_pe = value;
else if (strcmp(flag, "-o" ) == 0) output = value;
else if (strcmp(flag, "-op") == 0) output_pe = value;
else if (strcmp(flag, "-M" ) == 0) max_memory = atoll(value) * 1000000;
else if (strcmp(flag, "-l" ) == 0) min_length = intval;
else if (strcmp(flag, "-c" ) == 0) cluster_thd = atof(value), useIdentity = true;
else if (strcmp(flag, "-D" ) == 0) distance_thd = atof(value), useDistance = true;
else if (strcmp(flag, "-b" ) == 0) band_width = intval;
else if (strcmp(flag, "-n" ) == 0) NAA = intval;
else if (strcmp(flag, "-d" ) == 0) des_len = intval;
else if (strcmp(flag, "-s" ) == 0) diff_cutoff = atof(value);
else if (strcmp(flag, "-S" ) == 0) diff_cutoff_aa = intval;
else if (strcmp(flag, "-B" ) == 0) store_disk = intval;
else if (strcmp(flag, "-P" ) == 0) PE_mode = intval;
else if (strcmp(flag, "-cx") == 0) trim_len = intval;
else if (strcmp(flag, "-cy") == 0) trim_len_R2 = intval;
else if (strcmp(flag, "-ap") == 0) align_pos = intval;
else if (strcmp(flag, "-sc") == 0) sort_output = intval;
else if (strcmp(flag, "-sf") == 0) sort_outputf = intval;
else if (strcmp(flag, "-p" ) == 0) print = intval;
else if (strcmp(flag, "-g" ) == 0) cluster_best = intval;
else if (strcmp(flag, "-G" ) == 0) global_identity = intval;
else if (strcmp(flag, "-aL") == 0) long_coverage = atof(value);
else if (strcmp(flag, "-AL") == 0) long_control = intval;
else if (strcmp(flag, "-aS") == 0) short_coverage = atof(value);
else if (strcmp(flag, "-AS") == 0) short_control = intval;
else if (strcmp(flag, "-A" ) == 0) min_control = intval;
else if (strcmp(flag, "-uL") == 0) long_unmatch_per = atof(value);
else if (strcmp(flag, "-uS") == 0) short_unmatch_per = atof(value);
else if (strcmp(flag, "-U") == 0) unmatch_len = intval;
else if (strcmp(flag, "-tmp" ) == 0) temp_dir = value;
else if (strcmp(flag, "-bak" ) == 0) backupFile = intval;
else if (strcmp(flag, "-T" ) == 0){
#ifndef NO_OPENMP
int cpu = omp_get_num_procs();
threads = intval;
if( threads > cpu ){
threads = cpu;
printf( "Warning: total number of CPUs in the system is %i\n", cpu );
}else if( threads < 0 ){
threads += cpu;
if( threads < 0 ) threads = 0;
}
if( threads == 0 ){
threads = cpu;
printf( "total number of CPUs in the system is %i\n", cpu );
}
if( threads != intval ) printf( "Actual number of CPUs to be used: %i\n\n", threads );
#else
printf( "Option -T is ignored: multi-threading with OpenMP is NOT enabled!\n" );
#endif
}else return false;
return true;
}
bool Options::SetOption( const char *flag, const char *value )
{
if( is454 ){
if( strcmp(flag, "-s") == 0 ) return false;
else if( strcmp(flag, "-S") == 0 ) return false;
else if( strcmp(flag, "-G") == 0 ) return false;
else if( strcmp(flag, "-A") == 0 ) return false;
else if( strcmp(flag, "-r") == 0 ) return false;
else if( strcmp(flag, "-D") == 0 ){ max_indel = atoi(value); return true; }
}
if( SetOptionCommon( flag, value ) ) return true;
if (strcmp(flag, "-t" ) == 0) tolerance = atoi(value);
else if (strcmp(flag, "-F" ) == 0) frag_size = atoi(value);
else if (has2D && SetOption2D( flag, value ) ) return true;
else if (isEST && SetOptionEST( flag, value ) ) return true;
else return false;
return true;
}
bool Options::SetOption2D( const char *flag, const char *value )
{
if( SetOptionCommon( flag, value ) ) return true;
if (strcmp(flag, "-i2" ) == 0) input2 = value;
else if (strcmp(flag, "-j2" ) == 0) input2_pe = value;
else if (strcmp(flag, "-s2") == 0) diff_cutoff2 = atof(value);
else if (strcmp(flag, "-S2") == 0) diff_cutoff_aa2 = atoi(value);
else return false;
return true;
}
bool Options::SetOptionEST( const char *flag, const char *value )
{
NAA_top_limit = 12;
if( SetOptionCommon( flag, value ) ) return true;
if (strcmp(flag, "-r" ) == 0) option_r = atoi(value);
else if (strcmp(flag, "-gap") == 0) mat.gap = MAX_SEQ * atoi(value);
else if (strcmp(flag, "-gap-ext") == 0) mat.ext_gap = MAX_SEQ * atoi(value);
else if (strcmp(flag, "-match") == 0) mat.set_match( atoi(value) );
else if (strcmp(flag, "-mismatch") == 0) mat.set_mismatch( atoi(value) );
else if (strcmp(flag, "-mask") == 0){
string letters = value;
int i, n = letters.size();
for(i=0; i<n; i++){
char ch = toupper( letters[i] );
if( ch < 'A' || ch > 'Z' ) continue;
na2idx[ ch - 'A' ] = 5;
}
setaa_to_na();
}else return false;
return true;
}
bool Options::SetOptions( int argc, char *argv[], bool twod, bool est )
{
int i, n;
char date[100];
strcpy( date, __DATE__ );
n = strlen( date );
for(i=1; i<n; i++) if( date[i-1] == ' ' and date[i] == ' ' ) date[i] = '0';
printf( "================================================================\n" );
printf( "Program: CD-HIT, V" CDHIT_VERSION WITH_OPENMP ", %s, " __TIME__ "\n", date );
printf( "Command:" );
n = 9;
for(i=0; i<argc; i++){
n += strlen( argv[i] ) + 1;
if( n >= 64 ){
printf( "\n %s", argv[i] );
n = strlen( argv[i] ) + 9;
}else{
printf( " %s", argv[i] );
}
}
printf( "\n\n" );
time_t tm = time(NULL);
printf( "Started: %s", ctime(&tm) );
printf( "================================================================\n" );
printf( " Output \n" );
printf( "----------------------------------------------------------------\n" );
has2D = twod;
isEST = est;
for (i=1; i+1<argc; i+=2) if ( SetOption( argv[i], argv[i+1] ) == 0) return false;
if( i < argc ) return false;
atexit( CleanUpTempFiles );
return true;
}
void Options::Validate()
{
if( useIdentity and useDistance ) bomb_error( "can not use both identity cutoff and distance cutoff" );
if( useDistance ){
if ((distance_thd > 1.0) || (distance_thd < 0.0)) bomb_error("invalid distance threshold");
}else if( isEST ){
if ((cluster_thd > 1.0) || (cluster_thd < 0.8)) bomb_error("invalid clstr threshold, should >=0.8");
}else{
if ((cluster_thd > 1.0) || (cluster_thd < 0.4)) bomb_error("invalid clstr");
}
if (input.size() == 0) bomb_error("no input file");
if (output.size() == 0) bomb_error("no output file");
if (PE_mode) {
if (input_pe.size() == 0) bomb_error("no input file for R2 sequences in PE mode");
if (output_pe.size() == 0) bomb_error("no output file for R2 sequences in PE mode");
}
if (isEST && (align_pos==1)) option_r = 0;
if (band_width < 1 ) bomb_error("invalid band width");
if (NAA < 2 || NAA > NAA_top_limit) bomb_error("invalid word length");
if (des_len < 0 ) bomb_error("too short description, not enough to identify sequences");
if (not isEST && (tolerance < 0 || tolerance > 5) ) bomb_error("invalid tolerance");
if ((diff_cutoff<0) || (diff_cutoff>1)) bomb_error("invalid value for -s");
if (diff_cutoff_aa<0) bomb_error("invalid value for -S");
if( has2D ){
if ((diff_cutoff2<0) || (diff_cutoff2>1)) bomb_error("invalid value for -s2");
if (diff_cutoff_aa2<0) bomb_error("invalid value for -S2");
if (PE_mode) {
if (input2_pe.size() == 0) bomb_error("no input file for R2 sequences for 2nd db in PE mode");
}
}
if (global_identity == 0) print = 1;
if (short_coverage < long_coverage) short_coverage = long_coverage;
if (short_control > long_control) short_control = long_control;
if ((global_identity == 0) && (short_coverage == 0.0) && (min_control == 0))
bomb_error("You are using local identity, but no -aS -aL -A option");
if (frag_size < 0) bomb_error("invalid fragment size");
#if 0
if( useDistance ){
/* when required_aan becomes zero */
if( distance_thd * NAA >= 1 )
bomb_warning( "word length is too long for the distance cutoff" );
}else{
/* when required_aan becomes zero */
if( cluster_thd <= 1.0 - 1.0 / NAA )
bomb_warning( "word length is too long for the identity cutoff" );
}
#endif
const char *message = "Your word length is %i, using %i may be faster!\n";
if ( not isEST && tolerance ) {
int i, clstr_idx = (int) (cluster_thd * 100) - naa_stat_start_percent;
int tcutoff = naa_stat[tolerance-1][clstr_idx][5-NAA];
if (tcutoff < 5 )
bomb_error("Too low cluster threshold for the word length.\n"
"Increase the threshold or the tolerance, or decrease the word length.");
for ( i=5; i>NAA; i--) {
if ( naa_stat[tolerance-1][clstr_idx][5-i] > 10 ) {
printf( message, NAA, i );
break;
}
}
} else if( isEST ) {
if ( cluster_thd > 0.9 && NAA < 8 ) printf( message, NAA, 8 );
else if ( cluster_thd > 0.87 && NAA < 5 ) printf( message, NAA, 5 );
else if ( cluster_thd > 0.80 && NAA < 4 ) printf( message, NAA, 4 );
else if ( cluster_thd > 0.75 && NAA < 3 ) printf( message, NAA, 3 );
} else {
if ( cluster_thd > 0.85 && NAA < 5 ) printf( message, NAA, 5 );
else if ( cluster_thd > 0.80 && NAA < 4 ) printf( message, NAA, 4 );
else if ( cluster_thd > 0.75 && NAA < 3 ) printf( message, NAA, 3 );
}
if ( (min_length + 1) < NAA ) bomb_error("Too short -l, redefine it");
}
void Options::Print()
{
printf( "isEST = %i\n", isEST );
printf( "has2D = %i\n", has2D );
printf( "NAA = %i\n", NAA );
printf( "NAA_top_limit = %i\n", NAA_top_limit );
printf( "min_length = %i\n", min_length );
printf( "cluster_best = %i\n", cluster_best );
printf( "global_identity = %i\n", global_identity );
printf( "cluster_thd = %g\n", cluster_thd );
printf( "diff_cutoff = %g\n", diff_cutoff );
printf( "diff_cutoff_aa = %i\n", diff_cutoff_aa );
printf( "tolerance = %i\n", tolerance );
printf( "long_coverage = %g\n", long_coverage );
printf( "long_control = %i\n", long_control );
printf( "short_coverage = %g\n", short_coverage );
printf( "short_control = %i\n", short_control );
printf( "frag_size = %i\n", frag_size );
printf( "option_r = %i\n", option_r );
printf( "print = %i\n", print );
}
void bomb_error(const char *message)
{
fprintf( stderr, "\nFatal Error:\n%s\nProgram halted !!\n\n", message );
temp_files.Clear();
exit (1);
} // END void bomb_error
void bomb_error(const char *message, const char *message2)
{
fprintf( stderr, "\nFatal Error:\n%s %s\nProgram halted !!\n\n", message, message2 );
temp_files.Clear();
exit (1);
} // END void bomb_error
void bomb_warning(const char *message)
{
fprintf( stderr, "\nWarning:\n%s\nNot fatal, but may affect results !!\n\n", message );
} // END void bomb_warning
void bomb_warning(const char *message, const char *message2)
{
fprintf( stderr, "\nWarning:\n%s %s\nNot fatal, but may affect results !!\n\n", message, message2 );
} // END void bomb_warning
void format_seq(char *seq)
{
int i, j;
char c1;
int len = strlen(seq);
for (i=0,j=0; i<len; i++) {
c1 = toupper(seq[i]);
if ( isalpha(c1) ) seq[j++] = c1;
}
seq[j] = 0;
} // END void format_seq
void strrev(char *p)
{
char *q = p;
while(q && *q) ++q;
for(--q; p < q; ++p, --q)
*p = *p ^ *q,
*q = *p ^ *q,
*p = *p ^ *q;
}
////For smiple len1 <= len2, len2 is for existing representative
////walk along all diag path of two sequences,
////find the diags with most aap
////return top n diags
////added on 2006 11 13
////band 0 XXXXXXXXXXXXXXXXXX seq2, rep seq
//// XXXXXXXXXXXXXXX seq1
////band 1 XXXXXXXXXXXXXXXXXX seq2, rep seq
//// XXXXXXXXXXXXXXX seq1
////extreme right (+) XXXXXXXXXXXXXXXXXX seq2, rep seq
//// band = len2-1 XXXXXXXXXXXXXXX seq1
////band-1 XXXXXXXXXXXXXXXXXX seq2, rep seq
//// XXXXXXXXXXXXXXX seq1
////extreme left (-) XXXXXXXXXXXXXXXXXX seq2, rep seq
//// XXXXXXXXXXXXXXX band = -(len1-1) seq1
////index of diag_score = band+len1-1;
int diag_test_aapn(int NAA1, char iseq2[], int len1, int len2, WorkingBuffer & buffer,
int &best_sum, int band_width, int &band_left, int &band_center, int &band_right, int required_aa1)
{
int i, i1, j, k;
int *pp;
int nall = len1+len2-1;
Vector<int> & taap = buffer.taap;
Vector<INTs> & aap_begin = buffer.aap_begin;
Vector<INTs> & aap_list = buffer.aap_list;
Vector<int> & diag_score = buffer.diag_score;
Vector<int> & diag_score2 = buffer.diag_score2;
if (nall > MAX_DIAG) bomb_error("in diag_test_aapn, MAX_DIAG reached");
for (pp=&diag_score[0], i=nall; i; i--, pp++) *pp=0;
for (pp=&diag_score2[0], i=nall; i; i--, pp++) *pp=0;
int c22, cpx;
INTs *bip;
int len11 = len1-1;
int len22 = len2-1;
i1 = len11;
for (i=0; i<len22; i++,i1++) {
c22 = iseq2[i]*NAA1+ iseq2[i+1];
cpx = 1 + (iseq2[i] != iseq2[i+1]);
if ( (j=taap[c22]) == 0) continue;
int m = aap_begin[c22];
for(int k=0; k<j; k++){
diag_score[ i1 - aap_list[m+k] ] ++;
diag_score2[ i1 - aap_list[m+k] ] += cpx;
}
}
//find the best band range
// int band_b = required_aa1;
int band_b = required_aa1-1 >= 0 ? required_aa1-1:0; // on dec 21 2001
int band_e = nall - band_b;
int band_m = ( band_b+band_width-1 < band_e ) ? band_b+band_width-1 : band_e;
int best_score=0;
int best_score2=0;
int max_diag = 0;
int max_diag2 = 0;
int imax_diag = 0;
for (i=band_b; i<=band_m; i++){
best_score += diag_score[i];
best_score2 += diag_score2[i];
if( diag_score2[i] > max_diag2 ){
max_diag2 = diag_score2[i];
max_diag = diag_score[i];
imax_diag = i;
}
}
int from=band_b;
int end =band_m;
int score = best_score;
int score2 = best_score2;
for (k=from, j=band_m+1; j<band_e; j++, k++) {
score -= diag_score[k];
score += diag_score[j];
score2 -= diag_score2[k];
score2 += diag_score2[j];
if ( score2 > best_score2 ) {
from = k + 1;
end = j;
best_score = score;
best_score2 = score2;
if( diag_score2[j] > max_diag2 ){
max_diag2 = diag_score2[j];
max_diag = diag_score[j];
imax_diag = j;
}
}
}
int mlen = imax_diag;
if( imax_diag > len1 ) mlen = nall - imax_diag;
int emax = int((1.0 - options.cluster_thd) * mlen) + 1;
for (j=from; j<imax_diag; j++) { // if aap pairs fail to open gap
if ( (imax_diag - j) > emax || diag_score[j] < 1 ) {
best_score -= diag_score[j]; from++;
} else break;
}
for (j=end; j>imax_diag; j--) { // if aap pairs fail to open gap
if ( (j - imax_diag) > emax || diag_score[j] < 1 ) {
best_score -= diag_score[j]; end--;
} else break;
}
// delete [] diag_score;
band_left = from - len1 + 1;
band_right= end - len1 + 1;
band_center = imax_diag - len1 + 1;
best_sum = best_score;
return OK_FUNC;
}
// END diag_test_aapn
int diag_test_aapn_est(int NAA1, char iseq2[], int len1, int len2, WorkingBuffer & buffer,
int &best_sum, int band_width, int &band_left, int &band_center, int &band_right, int required_aa1)
{
int i, i1, j, k;
int *pp, *pp2;
int nall = len1+len2-1;
int NAA2 = NAA1 * NAA1;
int NAA3 = NAA2 * NAA1;
Vector<int> & taap = buffer.taap;
Vector<INTs> & aap_begin = buffer.aap_begin;
Vector<INTs> & aap_list = buffer.aap_list;
Vector<int> & diag_score = buffer.diag_score;
Vector<int> & diag_score2 = buffer.diag_score2;
if (nall > MAX_DIAG) bomb_error("in diag_test_aapn_est, MAX_DIAG reached");
pp = & diag_score[0];
pp2 = & diag_score2[0];
for (i=nall; i; i--, pp++, pp2++) *pp = *pp2 =0;
INTs *bip;
int c22, cpx;
int len22 = len2-3;
i1 = len1-1;
for (i=0; i<len22; i++,i1++,iseq2++) {
unsigned char c0 = iseq2[0];
unsigned char c1 = iseq2[1];
unsigned char c2 = iseq2[2];
unsigned char c3 = iseq2[3];
if ((c0>=4) || (c1>=4) || (c2>=4) || (c3>=4)) continue; //skip N
c22 = c0*NAA3+ c1*NAA2 + c2*NAA1 + c3;
if ( (j=taap[c22]) == 0) continue;
cpx = 1 + (c0 != c1) + (c1 != c2) + (c2 != c3);
bip = & aap_list[ aap_begin[c22] ]; // bi = aap_begin[c22];
for (; j; j--, bip++) {
diag_score[i1 - *bip]++;
diag_score2[i1 - *bip] += cpx;
}
}
#if 0
int mmax = 0;
int immax = 0;
for(i=0; i<=nall; i++){
if( i%len2 ==0 or i == nall ) printf( "\n" );
printf( "%3i ", diag_score[i] );
if( diag_score[i] > mmax ){
mmax = diag_score[i];
immax = i;
}
}
#endif
//find the best band range
// int band_b = required_aa1;
int band_b = required_aa1-1 >= 0 ? required_aa1-1:0; // on dec 21 2001
int band_e = nall - band_b;
if( options.is454 ){
band_b = len1 - band_width;
band_e = len1 + band_width;
if( band_b < 0 ) band_b = 0;
if( band_e > nall ) band_e = nall;
}
int band_m = ( band_b+band_width-1 < band_e ) ? band_b+band_width-1 : band_e;
int best_score=0;
int best_score2=0;
int max_diag = 0;
int max_diag2 = 0;
int imax_diag = 0;
for (i=band_b; i<=band_m; i++){
best_score += diag_score[i];
best_score2 += diag_score2[i];
if( diag_score2[i] > max_diag2 ){
max_diag2 = diag_score2[i];
max_diag = diag_score[i];
imax_diag = i;
}
}
int from=band_b;
int end =band_m;
int score = best_score;
int score2 = best_score2;
for (k=from, j=band_m+1; j<band_e; j++, k++) {
score -= diag_score[k];
score += diag_score[j];
score2 -= diag_score2[k];
score2 += diag_score2[j];
if ( score2 > best_score2 ) {
from = k + 1;
end = j;
best_score = score;
best_score2 = score2;
if( diag_score2[j] > max_diag2 ){
max_diag2 = diag_score2[j];
max_diag = diag_score[j];
imax_diag = j;
}
}
}
#if 0
printf( "%i %i\n", required_aa1, from );
printf( "max=%3i imax=%3i; band: %3i %3i %i\n", max_diag, imax_diag, band_b, band_e, band_m );
printf( "best: %i\n", best_score );
printf( "from: %i, end: %i, best: %i\n", from, end, best_score );
#endif
int mlen = imax_diag;
if( imax_diag > len1 ) mlen = nall - imax_diag;
int emax = int((1.0 - options.cluster_thd) * mlen) + 1;
for (j=from; j<imax_diag; j++) { // if aap pairs fail to open gap
if ( (imax_diag - j) > emax || diag_score[j] < 1 ) {
best_score -= diag_score[j]; from++;
} else break;
}
for (j=end; j>imax_diag; j--) { // if aap pairs fail to open gap
if ( (j - imax_diag) > emax || diag_score[j] < 1 ) {
best_score -= diag_score[j]; end--;
} else break;
}
band_left = from-len1+1;
band_right= end-len1+1;
band_center = imax_diag - len1 + 1;
best_sum = best_score;
if( options.is454 ){
if( band_left > 0 ) best_sum = 0;
if( band_right < 0 ) best_sum = 0;
}
#if 0
printf( "%3i: best: %i, %i %i %i\n", required_aa1, best_score, band_left, band_right, band_width );
printf( "max=%3i imax=%3i; band: %3i %3i %i\n", mmax, immax, band_b, band_e, band_m );
#endif
return OK_FUNC;
}
// END diag_test_aapn_est
/*
local alignment of two sequence within a diag band
for band 0 means direction (0,0) -> (1,1)
1 means direction (0,1) -> (1,2)
-1 means direction (1,0) -> (2,1)
added on 2006 11 13
band 0 XXXXXXXXXXXXXXXXXX seq2, rep seq
XXXXXXXXXXXXXXX seq1
band 1 XXXXXXXXXXXXXXXXXX seq2, rep seq
XXXXXXXXXXXXXXX seq1
extreme right (+) XXXXXXXXXXXXXXXXXX seq2, rep seq
band = len2-1 XXXXXXXXXXXXXXX seq1
band-1 XXXXXXXXXXXXXXXXXX seq2, rep seq
XXXXXXXXXXXXXXX seq1
extreme left (-) XXXXXXXXXXXXXXXXXX seq2, rep seq
XXXXXXXXXXXXXXX band = -(len1-1) seq1
iseq len are integer sequence and its length,
mat is matrix, return ALN_PAIR class
band: -101 seq2 len2 = 17
\\\1234567890123456
0 \xxxxxxxxxxxxxxxxx
1 xxxxxxxxxxxxxxxxx\ most right band = len2-1
2 xxxxxxxxxxxxxxxxx
seq1 3 xxxxxxxxxxxxxxxxx
len1 = 11 4 xxxxxxxxxxxxxxxxx
5 xxxxxxxxxxxxxxxxx
6 xxxxxxxxxxxxxxxxx
7 xxxxxxxxxxxxxxxxx
8 xxxxxxxxxxxxxxxxx
9 xxxxxxxxxxxxxxxxx
0 xxxxxxxxxxxxxxxxx
\
most left band = -(len1-1)
*/
int local_band_align( char iseq1[], char iseq2[], int len1, int len2, ScoreMatrix &mat,
int &best_score, int &iden_no, int &alnln, float &dist, int *alninfo,
int band_left, int band_center, int band_right, WorkingBuffer & buffer)
{
int i, j, k, j1;
int jj, kk;
int iden_no1;
int64_t best_score1;
iden_no = 0;
if ( (band_right >= len2 ) ||
(band_left <= -len1) ||
(band_left > band_right) ) return FAILED_FUNC;
// allocate mem for score_mat[len1][len2] etc
int band_width = band_right - band_left + 1;
int band_width1 = band_width + 1;
// score_mat, back_mat [i][j]: i index of seqi (0 to len(seqi)-1), j index of band (0 to band_width-1)
MatrixInt64 & score_mat = buffer.score_mat;
MatrixInt & back_mat = buffer.back_mat;
//printf( "%i %i\n", band_right, band_left );
if( score_mat.size() <= len1 ){
VectorInt row( band_width1, 0 );
VectorInt64 row2( band_width1, 0 );
while( score_mat.size() <= len1 ){
score_mat.Append( row2 );
back_mat.Append( row );
}
}
for(i=0; i<=len1; i++){
if( score_mat[i].Size() < band_width1 ) score_mat[i].Resize( band_width1 );
if( back_mat[i].Size() < band_width1 ) back_mat[i].Resize( band_width1 );
}
best_score = 0;
/* seq1 is query, seq2 is rep
seq2 len2 = 17 seq2 len2 = 17 seq2 len2 = 17
01234567890123456 01234567890123456 01234567890123456
0 xxxxxxxxxxxxxxxxx \\\\\\XXXxxxxxxxxxxxxxx xXXXXXXXxxxxxxxxx
1 \\\\\Xxxxxxxxxxxxxxxxx \\\\\Xxx\xxxxxxxxxxxxx xx\xxxxx\xxxxxxxx
2 \\\\X\xxxxxxxxxxxxxxx \\\\Xxxx\xxxxxxxxxxxx xxx\xxxxx\xxxxxxx
seq1 3 \\\Xx\xxxxxxxxxxxxxx \\\Xxxxx\xxxxxxxxxxx xxxx\xxxxx\xxxxxx
len1 4 \\Xxx\xxxxxxxxxxxxx \\Xxxxxx\xxxxxxxxxx xxxxx\xxxxx\xxxxx
= 11 5 \Xxxx\xxxxxxxxxxxx \Xxxxxxx\xxxxxxxxx xxxxxx\xxxxx\xxxx
6 Xxxxx\xxxxxxxxxxx Xxxxxxxx\xxxxxxxx xxxxxxx\xxxxx\xxx
7 x\xxxx\xxxxxxxxxx x\xxxxxxx\xxxxxxx xxxxxxxx\xxxxx\xx
8 xx\xxxx\xxxxxxxxx xx\xxxxxxx\xxxxxx xxxxxxxxx\xxxxx\x
9 xxx\xxxx\xxxxxxxx xxx\xxxxxxx\xxxxx xxxxxxxxxx\xxxxx\
0 xxxx\xxxx\xxxxxxx xxxx\xxxxxxx\xxxx xxxxxxxxxxx\xxxxx
band_left < 0 (-6) band_left < 0 (-6) band_left >=0 (1)
band_right < 0 (-1) band_right >=0 (2) band_right >=0(7)
band_width 6 band_width 9 band_width 7
init score_mat, and iden_mat (place with upper 'X')
*/
if (band_left < 0) { //set score to left border of the matrix within band
int tband = (band_right < 0) ? band_right : 0;
//for (k=band_left; k<tband; k++)
for (k=band_left; k<=tband; k++) { // fixed on 2006 11 14
i = -k;
j1 = k-band_left;
// penalty for leading gap opening = penalty for gap extension
// each of the left side query hunging residues give ext_gap (-1)
score_mat[i][j1] = mat.ext_gap * i;
back_mat[i][j1] = DP_BACK_TOP;
}
back_mat[-tband][tband-band_left] = DP_BACK_NONE;
}
if (band_right >=0) { //set score to top border of the matrix within band
int tband = (band_left > 0) ? band_left : 0;
for (j=tband; j<=band_right; j++) {
j1 = j-band_left;
score_mat[0][j1] = mat.ext_gap * j;
back_mat[0][j1] = DP_BACK_LEFT;
}
back_mat[0][tband-band_left] = DP_BACK_NONE;
}
int gap_open[2] = { mat.gap, mat.ext_gap };
int max_diag = band_center - band_left;
int extra_score[4] = { 4, 3, 2, 1 };
for (i=1; i<=len1; i++) {
int J0 = 1 - band_left - i;
int J1 = len2 - band_left - i;
if( J0 < 0 ) J0 = 0;
if( J1 >= band_width ) J1 = band_width;
for (j1=J0; j1<=J1; j1++){
j = j1+i+band_left;
int ci = iseq1[i-1];
int cj = iseq2[j-1];
int sij = mat.matrix[ci][cj];
//int iden_ij = (ci == cj);
int s1, k0, back;
/* extra score according to the distance to the best diagonal */
int extra = extra_score[ abs(j1 - max_diag) & 3 ]; // max distance 3
sij += extra * (sij>0);
back = DP_BACK_LEFT_TOP;
best_score1 = score_mat[i-1][j1] + sij;
int gap0 = gap_open[ (i == len1) | (j == len2) ];
int gap = 0;
int64_t score;
if( j1 > 0 ){
gap = gap0;
if( back_mat[i][j1-1] == DP_BACK_LEFT ) gap = mat.ext_gap;
if( (score = score_mat[i][j1-1] + gap) > best_score1 ){
back = DP_BACK_LEFT;
best_score1 = score;
}
}
if(j1+1<band_width){
gap = gap0;
if( back_mat[i-1][j1+1] == DP_BACK_TOP ) gap = mat.ext_gap;
if( (score = score_mat[i-1][j1+1] + gap) > best_score1 ){
back = DP_BACK_TOP;
best_score1 = score;
}
}
score_mat[i][j1] = best_score1;
back_mat[i][j1] = back;
//printf( "%2i(%2i) ", best_score1, iden_no1 );
}
//printf( "\n" );
}
i = j = 0;
if( len2 - band_left < len1 ){
i = len2 - band_left;
j = len2;
}else if( len1 + band_right < len2 ){
i = len1;
j = len1 + band_right;
}else{
i = len1;
j = len2;
}
j1 = j - i - band_left;
best_score = score_mat[i][j1];
best_score1 = score_mat[i][j1];
#if 1
const char *letters = "acgtnx";
const char *letters2 = "ACGTNX";
#else
const char *letters = "arndcqeghilkmfpstwyvbzx";
const char *letters2 = "ARNDCQEGHILKMFPSTWYVBZX";
#endif
int back = back_mat[i][j1];
int last = back;
int count = 0, count2 = 0, count3 = 0;
int match, begin1, begin2, end1, end2;
int gbegin1=0, gbegin2=0, gend1=0, gend2=0;
int64_t score, smin = best_score1, smax = best_score1 - 1;
int posmin, posmax, pos = 0;
int bl, dlen = 0, dcount = 0;
posmin = posmax = 0;
begin1 = begin2 = end1 = end2 = 0;
#ifdef PRINT
#define PRINT
printf( "%i %i\n", best_score, score_mat[i][j1] );
printf( "%i %i %i\n", band_left, band_center, band_right );
printf( "%i %i %i %i\n", i, j, j1, len2 );
#endif
#ifdef MAKEALIGN
#define MAKEALIGN
char AA[ MAX_SEQ ], BB[ MAX_SEQ ];
int NN = 0;
int IA, IB;
for(IA=len1;IA>i;IA--){
AA[NN] = letters[ iseq1[IA-1] ];
BB[NN++] = '-';
}
for(IB=len2;IB>j;IB--){
AA[NN] = '-';
BB[NN++] = letters[ iseq2[IB-1] ];
}
#endif
int masked = 0;
int indels = 0;
int max_indels = 0;
while( back != DP_BACK_NONE ){
switch( back ){
case DP_BACK_TOP :
#ifdef PRINT
printf( "%5i: %c %c %9i\n", pos, letters[ iseq1[i-1] ], '|', score_mat[i][j1] );
#endif
#ifdef MAKEALIGN
AA[NN] = letters[ iseq1[i-1] ];
BB[NN++] = '-';
#endif
bl = (last != back) & (j != 1) & (j != len2);
dlen += bl;
dcount += bl;
score = score_mat[i][j1];
if( score < smin ){
count2 = 0;
smin = score;
posmin = pos - 1;
begin1 = i;
begin2 = j;
}
i -= 1;
j1 += 1;