-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalSOV.cpp
1647 lines (1517 loc) · 59.6 KB
/
calSOV.cpp
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
/*
* =====================================================================================
*
* Filename: calSOV.cpp
*
* Description: calculate the SOV score and Q3 score given a prediction
*
* Version: 1.0
* Created: 07/23/2009 03:49:05 PM CEST
* Revision: none
* Compiler: gcc
*
* Author: Nanjiang Shu (Shu), [email protected]
* Company: Department of Biochemistry and Biophysics, Stockholm Univesity
*
* =====================================================================================
*/
/* ChangeLog 2009-10-26
* The confidence normalization function,
* a1, a2 and a3 can be modified from argument
* */
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <set>
#include <list>
#include <vector>
#include <utility>
#include "array.h"
#include "mytemplate.h"
/*#include "myfunc.h"*/
using namespace std;
#if defined(_Windows) || defined(__WINDOWS__) || \
defined(__WIN32__) || defined(WIN32) || \
defined(__WINNT__) || defined(__NT__)
# ifndef WINDOWS
# define WINDOWS
# endif
#endif
#define NUM_HSR_STATE 3
int method_HSR = 1 ;
bool isStatQ3Conf = false; /*whether analyze the Q3 for residues at each confidence bin*/
int typeConfidence = 0; /*default using the raw confidence*/
char HSRalphabet[] = "HSR-";
double binWidth = 1.0; /*halfBinWidth*/
int maxSeqLength = MAX_SEQ_LENGTH +1 ;
bool isProofReading = false; /*2009-08-03*/
int method_ProofReading = 0;
/*parameters for the function
* normConf = a1 * rawConf*rawConf + a2 * rawConf + a3
* 2009-10-26*/
double poly_a1 = -0.0071;
double poly_a2 = 1.8084;
double poly_a3 = -11.4;
void PrintHelp(char** argv)
{
fprintf(stdout,"Usage: calSOV [options] file1 file2 ...\n");
fprintf(stdout,"OPTIONS:\n");
fprintf(stdout," -o, --out <outfile> : output the result to outfile, (default = stdout)\n");
fprintf(stdout," -l, --list listfile : set the file containing a list of predicted files\n");
fprintf(stdout," -f, --format 0|1|2|3 : set the format of the input file, (default = 1)\n");
fprintf(stdout," -q3, --isOutQ3 yes|no: wheter output q3, (default = yes)\n");
fprintf(stdout," -m, --method 0|1|2|3: set the method to get predicted secondary structure state from Res file, (default = 1)\n");
fprintf(stdout," -statq3 : anylyze the Q3 for residues predicted at different confidences \n");
fprintf(stdout," -chkfstdir : folder to which Res from checkfirst program are stored, this need to be supplied when -statq3 is enabled \n");
fprintf(stdout," -selconf 0|1 : selection of the confidence, (default = 0) \n");
fprintf(stdout," : 0 -- raw confidence, 1 -- normalized confidence \n");
fprintf(stdout," -binwidth <real> : set halfbinwidth, (default = 1.0) \n");
fprintf(stdout," -max, --max-length : set the max length of the sequence, (default = %d)\n", maxSeqLength-1);
fprintf(stdout," -proof, --proof : enable proof reading\n");
fprintf(stdout," -proofmethod int : set the method for proof reading, (default = 0)\n");
fprintf(stdout," -polypara a1 a2 a3 : input three parameters for polynormail function which normalize the confidence\n");
fprintf(stdout," : default a1=%8.6lf , a2 = %8.6lf , a3 = %8.6lf \n", poly_a1, poly_a2 ,poly_a3);
fprintf(stdout," -h, --help : print this help message and exit\n");
fprintf(stdout,"\n");
fprintf(stdout,"Created on 2009-07-23, updated 2016-11-03, Nanjiang Shu\n");
fprintf(stdout,"\nFormat description of input file:\n");
fprintf(stdout," Format 0: prediction Res* file\n");
fprintf(stdout," Format 1: AA OSEC PSEC NUM\n");
fprintf(stdout," Format 2: Fasta format, >AA, >OSEC, >PSEC, order not important\n");
fprintf(stdout,"\nExamples:\n");
fprintf(stdout," %s -f 1 test/16VPA.psipred.format_1.txt\n", argv[0]);
fprintf(stdout," %s -f 2 test/16VPA.psipred.format_2.txt\n", argv[0]);
}
void PrintVerboseHelp() { }
int coverage(int a1, int b1, int a2, int b2)
/*
return the coverage of two intervals
a1, b1, a2, b2 are integers
when the return value <=0, it means there is no coverage
*/
{
return (min(b1,b2)-max(a1,a2));
}
int my_strcpy(char* to, const char* from, int max)/*{{{*/
/******************************************************************************
* my modification of strcpy
* copy max characters from "from" to "to", add NULL terminator automatically
* updated 2008-04-23, memcpy win in speed when the copying string is long
* updated 2011-10-27:
* since calling strlen will be time consuming for very large from string,
* e.g. when "from" is the buffer of a whole trunk of file. Therefore, strlen
* part is removed.
*****************************************************************************/
{
if(max < 200) {
strncpy(to,from,max);
} else {
memcpy(to,from,max);
}
to[max] = '\0';
return max;
}/*}}}*/
char *rootname(const char* filename, char* rtname, int max_rtname /*= MAX_PATH*/)/*{{{*/
/*****************************************************************************
* rootname
* given the file name,
* return the rootname of the filename
****************************************************************************/
{
const char *pch;
char *pstr;
if((pch = strrchr(filename,'/')) != NULL)
pstr = (char*) pch+1;
else
pstr = (char*) filename;
if((pch = strrchr(pstr,'.')) != NULL)
my_strcpy(rtname,pstr, min((int)(pch - pstr), max_rtname));
else
rtname = pstr;
return rtname;
}
/*}}}*/
int option_parser_filename(int argc, char **argv, int beg, char *filename)/*{{{*/
/*****************************************************************************
* beg is the current index of argument list, e.g., argv[i] = "--out"
****************************************************************************/
{
int i ;
bool isNonOptionArg = false;
bool isFileNameSet = false;
for(i = beg +1 ; i < argc ; i++)
{
if(argv[i][0] == '-' && strcmp(argv[i], "--") != 0 && !isNonOptionArg)
{
fprintf(stderr,"option '%s' must be followed by a filename, not option\n", argv[beg]);
return -1;
}
else if(strcmp(argv[i], "--") == 0 && !isNonOptionArg)
{
isNonOptionArg = true;
}
else
{
my_strcpy(filename, argv[i], MAX_PATH-1);
isFileNameSet = true;
break;
}
}
if(!isFileNameSet)
{
fprintf(stderr,"option '%s' must be followed by a filename\n", argv[beg]);
return -1;
}
else
{
return i+1;
}
}
/*}}}*/
template <class T> int option_parser_numeric(int argc, char **argv, int beg, T &x, bool isRangeSet /*= false*/, T min /*= MIN_INT*/, T max /*= MAX_INT*/)/*{{{*/
/*****************************************************************************
* beg is the current index of argument list, e.g., argv[i] = "--value"
****************************************************************************/
{
int i ;
bool isValueSet = false;
double tmp;
i = beg +1;
if (i < argc)
{
if(IsNumeric(argv[i]))
{
tmp = atof(argv[i]);
if(isRangeSet)
{
if(tmp < min || tmp > max)
{
fprintf(stderr,"Invalid value! Value after option '%s' must be in the range of [%g %g]\n", argv[beg], double(min), double(max));
return -1;
}
}
x = T(tmp);
isValueSet = true;
}
}
if(!isValueSet)
{
fprintf(stderr,"option '%s' must be followed by a numerical value\n", argv[beg]);
return -1;
}
else
{
return i+1;
}
}
template int option_parser_numeric<int> (int argc, char **argv, int beg, int &x , bool isRangeSet /*= false*/, int min /*= MIN_INT*/ , int max/* = MAX_INT*/);
template int option_parser_numeric<float> (int argc, char **argv, int beg, float &x , bool isRangeSet /*= false*/, float min /*= MIN_FLOAT*/ , float max /*= MAX_FLOAT*/);
template int option_parser_numeric<double>(int argc, char **argv, int beg, double &x, bool isRangeSet /*= false*/, double min/* = MIN_DOUBLE*/, double max /*= MAX_DOUBLE*/);
template int option_parser_numeric<int8>(int argc, char **argv, int beg, int8 &x, bool isRangeSet /*= false*/, int8 min/* = MIN_DOUBLE*/, int8 max /*= MAX_DOUBLE*/);
/*}}}*/
int ReadNextSeq_FASTA(FILE *fp, char* seq, int *pSeq_type /*= NULL*/, int maxlength /*= LONGEST_SEQ*/, char* annotationLine/*=NULL*/, int maxSizeAnnotationLine /*=50*/)/*{{{*/
/****************************************************************************
* ReadNextSeq_FASTA()
* read in the fasta format sequence from the file stream
* return the length of the sequence if successful
* return 0 or minus value if no more sequence can be read from the file stream
* The leading white spaces are ignored
* check the type (DNA or AA) of fasta sequence file based the annotation line
* Last modified, 2007-02-12, Nanjiang Shu
* Updated 2010-04-22: the annotation line (without the leading ">") can be
* read in, note that the maxSizeAnnotationLine must be supplied
***************************************************************************/
{
int c;
int i;
do{ /* ignore the leading white spaces */
c = getc(fp);
}while(isspace(c));
if(pSeq_type != NULL)
*pSeq_type = UNKNOWN_SEQ_TYPE; /* initializing sequence type*/
if(c == '>')
{
Array1D <char> line_1darray(maxSizeAnnotationLine +1);
char *line = line_1darray.array1D;
fgetline(fp,line,maxSizeAnnotationLine);
if( pSeq_type != NULL)
{
if(strstr(line, "protein") != NULL)
*pSeq_type = AA_SEQ;
else if( strstr(line,"nucleic") != NULL)
*pSeq_type = DNA_SEQ;
else if( strstr(line,"shape") != NULL)
*pSeq_type = SHAPE_SEQ;
else
*pSeq_type = UNKNOWN_SEQ_TYPE;
}
if (annotationLine != NULL)
{
my_strcpy(annotationLine,line, maxSizeAnnotationLine-1); /*read in the annotation line*/
}
}
else
{
fseek(fp, -1, SEEK_CUR); /* backward 1 byte of file stream if there is no annotation line*/
}
i = 0 ;
while((c = getc(fp)) != EOF)
{
if(c == '>')
{
fseek(fp, -1, SEEK_CUR); /* backward 1 byte of file stream*/
break; /* read in the first sequence if there are multiple sequences in the file*/
}
if(! isspace(c)) /* neglect white spaces and return characters in sequence region*/
{
seq[i] = c ; i ++ ;
if(i >= maxlength)
{
fprintf(stderr,"Error, sequence longer then maxlength = %d\n", maxlength);
return 1;
}
}
}
seq[i] = '\0' ;
if(c == EOF && i == 0)
return EOF;
else
return i; /* return the length of sequence*/
}/*}}}*/
bool IsNumeric(const char* str)/*{{{*/
//**********************************************************************
//IsNumeric(char*)
//check if a string is a numeric number
{
int i = 0;
int cntdot = 0;
while(str[i] != '\0')
{
if( i == 0)
{
if(!isdigit(str[i]) && str[i] != '+' && str[i] != '-' && str[i] != '.' )
return false;
if(str[i] == '.')
cntdot ++;
}
else
{
if(! isdigit(str[i]) && str[i] != '.')
return false;
if(str[i] == '.')
cntdot ++;
}
i ++;
}
if(cntdot >= 2)
return false;
else
return true;
}
/*}}}*/
bool IsInCharSet(const char ch, const char *charSet, int n /*= 0 */)/*{{{*/
/*****************************************************************************
* check if the character "ch" is in charSet,
****************************************************************************/
{
if(n == 0)
n = strlen(charSet);
int i;
for(i = 0 ;i < n ; i ++)
{
if(ch == charSet[i])
return true;
}
return false;
}/*}}}*/
int fgetline(FILE* fp, char* line, int max/* = 0x7FFFFFFF*/)/*{{{*/
/*****************************************************************************
* Read one line from fp, copying it to line array (but no more than max
* chars). Does not place terminating \n in line array.
* it can be called without "max" flag, but should make sure the allocated
* memory for "line" is larger than the longest line.
*
* Returns: line length, or 0 for empty line, or "EOF" for end-of-file.
*
* since fgetc(), getc(), getchar(), returns int value,always use an int
* variable to store the result of the fgetc(), getc() and getchar().
* getc() is faster than fgetc()
* LOG: 2006-04-26 16:31:29 Wednesday Week 17 <nanjiang@shu>
* bug fixed for '\n' return keys,
* now it is valid for both dos and unix
****************************************************************************/
{
int nch = 0; /* record number of characters actually read */
int c;
max = max - 1; /* leave room for '\0' */
while((c = getc(fp)) != EOF)
{
if(c == 0x0d ) continue; /* in unix, '\n'= 0x0a, thus 0x0d will be cheated as another character*/
if(c == '\n') break; /* in dos, '\n' is also equal to 0x0a, but the preceding 0x0d will not be read*/
if(nch < max)
{
line[nch] = c;
nch = nch + 1;
}
}
line[nch] = '\0';
if(c == EOF && nch == 0) return EOF;
else return nch;
}/*}}}*/
int checkfilestream(FILE *fp, const char* filename, const char *mode, bool isAssert /*= false*/)/*{{{*/
{
if( fp == NULL)
{
fprintf(stderr,"Can not open file '%s' with mode '%s'\n", filename,mode);
if(isAssert)
{
assert(fp != NULL);
}
return -1;
}
else
return 0;
}
/*}}}*/
double GetRawHSRConfidence(int probH, int probS, int probR)/*{{{*/
/*Determine raw confidence of the prediction based on the probability on H, S
* and R
* 2009-07-21, Nanjiang*/
{
double sumHSR = double(probH+probS+probR) + 1e-6; /*plus 1e-6 to avoid division by zero*/
double maxHSR = double (max (max(probH, probS), probR ));
double rawConf = maxHSR/sumHSR;
return rawConf;
}/*}}}*/
float GetNormHSRConfidence(int probH, int probS, int probR)/*{{{*/
/*Determine normalized confidence of the prediction based on the probability on H, S
* and R, parameters were obtained by polynormial regression (order 2) of the
* raw confidence and two Q3 at different raw confidence bins
* 2009-08-02
* */
{
double sumHSR = double(probH+probS+probR) + 1e-6; /*plus 0.00001 to avoid division by zero*/
double maxHSR = double (max (max(probH, probS), probR ));
double rawConf =(maxHSR/sumHSR)*100.0;
double normConf = poly_a1 * rawConf*rawConf + poly_a2 * rawConf + poly_a3;
/*method 2, three order regression, 2009-08-02 */
//y = -0.0001x3 + 0.0225x2 - 0.1356x + 28.889
//double a1 =-0.0001;
//double a2 = 0.0225;
//double a3 = -0.1356;
//double a4 =28.889;
//double normConf = a1 * rawConf*rawConf*rawConf + a2 * rawConf*rawConf + a3*rawConf + a4;
normConf /= 100.0;
if (normConf < 0.0)
{
normConf = 0.0;
}
else if (normConf> 1.0)
{
normConf = 1.0;
}
return normConf;
}/*}}}*/
int GetHSRState(int probH, int probS, int probR, int method_HSR /*= 1*/)/*{{{*/
/*Determine the state of secondary structure based on the probability on H, S
* and R
* the return value is 0 or 1 or 2, representing H, S and R respectively
* 2009-07-13, Nanjiang*/
{
int hsrState = 2;
if (method_HSR == 0)
{
if ( (probS>=probH) && (probS>=probR) ) { hsrState = 1; }
else if ((probH>=probS) && (probH>=probR) ) { hsrState = 0; }
else { hsrState = 2; }
}
else if (method_HSR == 1)
{
if ( (probR>=probH) && (probR>=probS) ) { hsrState = 2; }
else if ((probS>=probH) && (probS>=probR) ) { hsrState = 1; }
else { hsrState = 0; }
}
else
{
if ( (probR>=probH) && (probR>=probS) ) { hsrState = 2; }
else if ((probH>=probS) && (probH>=probR) ) { hsrState = 0; }
else { hsrState = 1; }
}
return hsrState;
}/*}}}*/
int ReadInSecPredFile(const char *infile, char *aaSeq, char *obsSec, char *predSec, double *predConf, int typeConfidence, int fileFormat)/*{{{*/
{
int linesize;
int maxline = 400;
Array1D <char> line_1darray(maxline+1);
char *line = line_1darray.array1D;
char aa;
char obs_sec;
char pred_sec;
int status_sscanf = 0;
int seqLength = 0;
FILE *fp = fopen(infile, "r");
checkfilestream(fp, infile, "r", true);
if (fileFormat == 0) /*Res file format*//*{{{*/
{
int num ;
char shape;
int probH;
int probS;
int probR;
int predHSRstate = 0; /*predicted HSR state, 0 -- H, 1 -- S, 2 -- R*/
int cntRes = 0;
while((linesize=fgetline(fp, line, maxline)) != EOF)
{
status_sscanf = sscanf(line,"%d %c %c %c %d %d %d" , &num,&aa,&shape, &obs_sec,&probH,&probS,&probR);
if (status_sscanf != 7)
{
fprintf(stderr,"sscanf error!");
fprintf(stderr,"File: %s\n", infile);
fprintf(stderr,"line: %s\n", line);
assert (status_sscanf == 7);
}
predHSRstate = GetHSRState(probH, probS, probR, method_HSR);
aaSeq[cntRes] = aa;
obsSec[cntRes] = obs_sec;
predSec[cntRes] = HSRalphabet[predHSRstate];
if (typeConfidence == 0) /*Raw confidence*/
{
predConf[cntRes] = GetRawHSRConfidence(probH, probS, probR);
}
else
{
predConf[cntRes] = GetNormHSRConfidence(probH, probS, probR);
}
cntRes ++;
}
seqLength = cntRes;
}/*}}}*/
else if (fileFormat == 1) /*AA OSEC PSEC NUM*//*{{{*/
{
/*AA OSEC PSEC NUM*/
linesize=fgetline(fp,line,maxline); /*neglect the header line*/
int cntRes = 0;
while((linesize=fgetline(fp, line, maxline)) != EOF)
{
status_sscanf = sscanf(line," %c %c %c" , &aa,&obs_sec,&pred_sec);
if (status_sscanf != 3)
{
fprintf(stderr,"sscanf error!");
fprintf(stderr,"File: %s\n", infile);
fprintf(stderr,"line: %s\n", line);
assert (status_sscanf == 3);
}
if (obs_sec == 'E' ) { obs_sec = 'S'; }
else if (obs_sec == 'C' || obs_sec == 'L') { obs_sec = 'R'; }
if (pred_sec == 'E' ) { pred_sec = 'S'; }
else if (pred_sec == 'C' || pred_sec == 'L') { pred_sec = 'R'; }
aaSeq[cntRes] = aa;
obsSec[cntRes] = obs_sec;
predSec[cntRes] = pred_sec;
#ifdef DEBUG
fprintf(stdout, "line=%s\n", line);
fprintf(stdout, "cntRes=%d\n", cntRes);
#endif
cntRes ++;
if (cntRes >= maxSeqLength){
fprintf(stdout,"Number of residues in the input file exceeds the maxSeqLength (%d). Please check your input file. Exit.\n", maxSeqLength);
assert(cntRes<maxSeqLength);
}
}
seqLength = cntRes;
}/*}}}*/
else if (fileFormat == 2) /*Fasta format >AA, >OSEC, >PSEC*//*{{{*/
{
int maxSizeAnno = 100;
Array1D <char> anno_1darray(maxSizeAnno+1);
Array1D <char> tmpSeq_1darray(maxSeqLength+1);
char *tmpSeq = tmpSeq_1darray.array1D;
char *anno = anno_1darray.array1D;
int seqlength = 0;
/*>AA >OSEC >PSEC*/
int cntSeq = 0;
while((seqlength = ReadNextSeq_FASTA(fp, tmpSeq, NULL, maxSeqLength, anno, maxSizeAnno))!= EOF)
{
if(strstr(anno, "AA") != NULL){
my_strcpy(aaSeq,tmpSeq, maxSeqLength-1);
}else if(strstr(anno, "OSEC") != NULL){
my_strcpy(obsSec,tmpSeq, maxSeqLength-1);
}else if(strstr(anno, "PSEC") != NULL){
my_strcpy(predSec,tmpSeq, maxSeqLength-1);
}
cntSeq ++;
if (cntSeq>3){
fprintf(stderr, "You input file contains more than 3 sequences. Please check the format of your input file\n");
assert(cntSeq<=3);
}
}
int i = 0;
seqlength = strlen(obsSec);
for (i = 0; i<seqlength; i++){
if (obsSec[i] == 'E' ) { obsSec[i] = 'S'; }
else if (obsSec[i] == 'C' || obsSec[i] == 'L') { obsSec[i] = 'R'; }
}
seqlength = strlen(predSec);
for (i = 0; i<seqlength; i++){
if (predSec[i] == 'E' ) { predSec[i] = 'S'; }
else if (predSec[i] == 'C' || predSec[i] == 'L') { predSec[i] = 'R'; }
}
seqLength = strlen(aaSeq);
}/*}}}*/
if (fp != NULL) { fclose(fp); }
return seqLength;
}/*}}}*/
int ProofReading(char *predSec, int seqLength, int method_ProofReading = 0)/*{{{*/
{
//proof reading for predicted result, for 1 long strand (sheet)
int Part1;
int Part2;
int ik;
int ij;
if (method_ProofReading == 0)/*{{{*/
{
int NShe = 0;
for (ik=0; ik<seqLength; ik++)
{
if ( predSec[ik] == 'S' )
{
NShe++;
}
else
{
if ( NShe == 1 )
{
//previous
Part1 = ik-3;
Part2 = ik +1 ;
if ( (Part1>=0) && (predSec[Part1]=='S') )
{
predSec[Part1+1] = 'S';
}
else if ( (Part2<seqLength) && (predSec[Part2]=='S') )
{
predSec[Part2-1] = 'S';
}
else
{
for (ij=ik-NShe; ij<ik; ij++)
{
predSec[ij] = 'R';
}
}
}
NShe = 0;
}
}
//proof reading for predicted result, for 1 or 2 long helix
int NHel = 0;
for (ik=0; ik<seqLength; ik++)
{
if ( predSec[ik] == 'H' )
{
NHel++;
}
else
{
if ( (NHel>=1) && (NHel<=2) )
{
//previous
Part1 = ik-NHel-2;
Part2 = ik +1 ;
if ( (Part1>=0) && (predSec[Part1]=='H') )
{
predSec[Part1+1] = 'H';
}
else if ( (Part2<seqLength) && (predSec[Part2]=='H') )
{
predSec[Part2-1] = 'H';
}
else
{
for (ij=ik-NHel; ij<ik; ij++)
{
predSec[ij] = 'R';
}
}
}
NHel = 0;
}
}
}/*}}}*/
else if (method_ProofReading == 1)/*{{{*/
{ /*do not proof reading single residue sheet, when using the DSSP8to3 scheme BE --> Sheet, 2009-10-29*/
//proof reading for predicted result, for 1 or 2 long helix
int NHel = 0;
for (ik=0; ik<seqLength; ik++)
{
if ( predSec[ik] == 'H' )
{
NHel++;
}
else
{
if ( (NHel>=1) && (NHel<=2) )
{
//previous
Part1 = ik-NHel-2;
Part2 = ik +1 ;
if ( (Part1>=0) && (predSec[Part1]=='H') )
{
predSec[Part1+1] = 'H';
}
else if ( (Part2<seqLength) && (predSec[Part2]=='H') )
{
predSec[Part2-1] = 'H';
}
else
{
for (ij=ik-NHel; ij<ik; ij++)
{
predSec[ij] = 'R';
}
}
}
NHel = 0;
}
}
}/*}}}*/
return seqLength;
}/*}}}*/
int ReadConf(const char *infile, double *predConf, int typeConfidence)/*{{{*/
{
int linesize;
int maxline = 400;
Array1D <char> line_1darray(maxline+1);
char *line = line_1darray.array1D;
char aa;
char obs_sec;
int status_sscanf = 0;
int seqLength = 0;
FILE *fp = fopen(infile, "r");
checkfilestream(fp, infile, "r", true);
int num ;
char shape;
int probH;
int probS;
int probR;
int cntRes = 0;
while((linesize=fgetline(fp, line, maxline)) != EOF)
{
status_sscanf = sscanf(line,"%d %c %c %c %d %d %d" , &num,&aa,&shape, &obs_sec,&probH,&probS,&probR);
if (status_sscanf != 7)
{
fprintf(stderr,"sscanf error!");
fprintf(stderr,"File: %s\n", infile);
fprintf(stderr,"line: %s\n", line);
assert (status_sscanf == 7);
}
if (typeConfidence == 0) /*Raw confidence*/
{
predConf[cntRes] = GetRawHSRConfidence(probH, probS, probR);
}
else
{
predConf[cntRes] = GetNormHSRConfidence(probH, probS, probR);
}
cntRes ++;
}
seqLength = cntRes;
if (fp != NULL) { fclose(fp); }
return seqLength;
}/*}}}*/
list < pair <int, int > > GetSecSeg(char *seq, char state, int seqLength)/*{{{*/
/*Get segment list for a certain secondary structure state*/
{
list < pair <int,int> > segList;
pair <int, int> seg;
int i,j;
i = 0;
while (i < seqLength)
{
int b, e;
if (seq[i] == state)
{
b = i;
j = i;
while (j < seqLength && seq[j] == state)
{
j ++;
}
e = j;
i = j;
seg = make_pair(b,e);
segList.push_back(seg);
}
else
{
i ++;
}
}
return segList;
}
/*}}}*/
int CalSOV(char *obsSec, char *predSec, int seqLength, double *numCorrectHSR, int *numHSR, double *sovHSR, int numState = 3)/*{{{*/
{
list <list <pair <int, int> > > segment_obs;
list <list <pair <int, int> > > segment_pred;
list <list <pair <int, int> > >::iterator it_sl_1;
list <list <pair <int, int> > >::iterator it_sl_2;
list <pair <int, int> > ::iterator it_seg_1;
list <pair <int, int> > ::iterator it_seg_2;
int i = 0;
for (i = 0; i < 3; i ++) /*iterator for secondary structure states*/
{
list <pair <int, int> > segList;
segList = GetSecSeg(obsSec, HSRalphabet[i], seqLength);
segment_obs.push_back(segList);
segList = GetSecSeg(predSec, HSRalphabet[i], seqLength);
segment_pred.push_back(segList);
}
vector <int> N(3,0); /*Normalization value*/
vector <double> sum_un_norm(3,0.0); /*sum of un-normalized score*/
int total_N = 0;
double total_sum_un_norm = 0.0;
int b1, e1, b2, e2, minov, maxov, delta, len_seg1, len_seg2;
double score;
for (i = 0; i < 3; i ++) /*iterator for secondary structure states*/
{
/*For all segments of the observed secondary structure*/
it_sl_1 = segment_obs.begin();
advance( it_sl_1, i );
it_sl_2 = segment_pred.begin();
advance( it_sl_2, i );
for (it_seg_1 = (*it_sl_1).begin(); it_seg_1 != (*it_sl_1).end(); ++it_seg_1)
{
b1 = (*it_seg_1).first;
e1 = (*it_seg_1).second;
len_seg1 = e1-b1;
bool isOverlap = false;
for (it_seg_2 = (*it_sl_2).begin(); it_seg_2 != (*it_sl_2).end(); ++it_seg_2)
{
b2 = (*it_seg_2).first;
e2 = (*it_seg_2).second;
len_seg2 = e2-b2;
minov = coverage(b1, e1, b2, e2);
if (minov > 0) {
maxov = max(e1,e2) - min(b1,b2);
} else {
maxov = 0;
}
if (minov > 0)
{
isOverlap = true;
double min1 = min(maxov - minov, minov);
double min2 = min(int(len_seg1/2), int(len_seg2/2));
delta = min(min1, min2);
score = (minov + delta)/double(maxov)*len_seg1;
sum_un_norm[i] += score;
N[i] += len_seg1;
}
}
if (isOverlap == false) {
N[i] += len_seg1;
}
}
sovHSR[i] = sum_un_norm[i]/N[i];
total_sum_un_norm += sum_un_norm[i];
total_N += N[i];
numHSR[i] = N[i];
}
sovHSR[numState] = total_sum_un_norm/total_N;
numHSR[numState] = total_N;
return numHSR[numState];
}/*}}}*/
int CalSOV_obsolete(char *obsSec, char *predSec, int seqLength, double *numCorrectHSR, int *numHSR, double *sovHSR, int numState = 3)/*{{{*/
/*=======================================================================
* numHSR[4] Helix, Sheet, Coil, All
* sovHSR[4]
* numState = 3 by default
*====================================================================*/
{
int Min_overlap[500] ;
int Max_extend[500] ;
int LenSeg_pred[500];
int All_HSRLen[3];
double All_Sum[3];
int iterHSRState = 0; /*iterator for the HSR state, H, S and R*/
char thisHSRstate = ' '; /*current HSR state*/
int i = 0;
int j = 0;
for (i=0;i<3;i++)
{
All_HSRLen[i] = 0;
All_Sum[i] = 0.0;
}
int Length = seqLength +1 ; /*Here Length is 1 residue longer than seqLength*/
for (iterHSRState=0; iterHSRState<3; iterHSRState++)//corrsponding to H, S, R
{
//for helix
int HSRLen = 0;/*iterator for secondary structure elements*/
thisHSRstate = HSRalphabet[iterHSRState];
for(i=0; i<Length; i++)
{
if ( obsSec[i] == thisHSRstate )
{
HSRLen++;
}
else
{
if ( HSRLen >= 1 )
{
int Num_Segment = 0;//the number of segments in an observed element
int SubLen = 0; /*length of the sub segment*/
//Min_overlap[]--the length of overlapping
//Mam_extend[] -- the extended length of two segments to be compared
for (j=i-HSRLen; j<i; j++)
{
if ( predSec[j] == thisHSRstate )
{
SubLen++;
}
else
{
if ( SubLen >= 1 )
{
Min_overlap[Num_Segment] = SubLen;
Num_Segment++;
}
SubLen = 0;
}
}
if ( SubLen >= 1 )
{
Min_overlap[Num_Segment] = SubLen;
Num_Segment++;
}
double sum = 0;
int beg = 0;
int Nextend = 0;
int Num_HSRLen = HSRLen;
if ( Num_Segment >= 1 )
{
for (j=0; j<Num_Segment; j++)
{
Max_extend[j] = HSRLen;
LenSeg_pred[j] = Min_overlap[j];
}
//for first segment
if ( predSec[i-HSRLen] == thisHSRstate )
{
beg = i - HSRLen - 1;
Nextend = 0;
while ( (beg>=0) && (predSec[beg]==thisHSRstate) && (obsSec[beg]!=thisHSRstate) )
{
Nextend++;
beg--;
}
Max_extend[0] = Max_extend[0] + Nextend;
LenSeg_pred[0] = LenSeg_pred[0] + Nextend;
}
//for last segment
if ( predSec[i-1] == thisHSRstate )
{
beg = i;
Nextend = 0;
while ( (beg<Length) && (predSec[beg]==thisHSRstate) && (obsSec[beg]!=thisHSRstate) )
{
Nextend++;
beg++;
}
Max_extend[Num_Segment-1] = Max_extend[Num_Segment-1] + Nextend;
LenSeg_pred[Num_Segment-1] = LenSeg_pred[Num_Segment-1] + Nextend;
}
for (j=0; j<Num_Segment; j++)
{
int Nmin1 = min( (Max_extend[j]-Min_overlap[j]), Min_overlap[j]);
int Nmin2 = min( HSRLen/2, LenSeg_pred[j]/2);
int Nmin = min(Nmin1, Nmin2);
double score_seg = (Min_overlap[j]+Nmin)*1.0/double(Max_extend[j]) ;
sum = sum + score_seg;
}
sum = sum*HSRLen;
Num_HSRLen = HSRLen*Num_Segment;
}
All_HSRLen[iterHSRState] = All_HSRLen[iterHSRState] + Num_HSRLen;
All_Sum[iterHSRState] = All_Sum[iterHSRState] + sum;
}
HSRLen = 0;
}
}
}
double sumOverlap = 0;
int sumAllRes = 0;
for (i=0; i<numState; i++)
{
numHSR[i] = All_HSRLen[i];