// File: nanjiangshu[P]calSOV/calSOV.cpp
// Function: ReadInSecPredFile(char const*, char*, char*, char*, double*, int, int)
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;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movl $0x190, -0x34(%rbp)     # imm = 0x190
movl -0x34(%rbp), %esi
incl %esi
leaq -0x48(%rbp), %rdi
callq 0x8320
movq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x5c(%rbp)
movq -0x8(%rbp), %rdi
leaq 0xa862(%rip), %rsi      # 0xda7e
callq 0x22d0
movq %rax, -0xe0(%rbp)
jmp 0x322a
movq -0xe0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
movq -0x8(%rbp), %rsi
leaq 0xa83a(%rip), %rdx      # 0xda7e
movl $0x1, %ecx
callq 0x2ed0
cmpl $0x0, 0x10(%rbp)
jne 0x346a
movl $0x0, -0x8c(%rbp)
movl $0x0, -0x90(%rbp)
movq -0x68(%rbp), %rdi
movq -0x50(%rbp), %rsi
movl -0x34(%rbp), %edx
callq 0x2dc0
movl %eax, -0xe4(%rbp)
jmp 0x3284
movl -0xe4(%rbp), %eax
movl %eax, -0x30(%rbp)
cmpl $-0x1, %eax
je 0x345c
movq -0x50(%rbp), %rdi
leaq 0xa591(%rip), %rsi      # 0xd832
leaq -0x78(%rbp), %rdx
leaq -0x51(%rbp), %rcx
leaq -0x79(%rbp), %r8
leaq -0x52(%rbp), %r9
leaq -0x80(%rbp), %r11
leaq -0x84(%rbp), %r10
leaq -0x88(%rbp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movb $0x0, %al
callq 0x2200
movl %eax, -0x58(%rbp)
cmpl $0x7, -0x58(%rbp)
je 0x336f
movq 0xfcec(%rip), %rax      # 0x12fd8
movq (%rax), %rdi
leaq 0xa551(%rip), %rsi      # 0xd847
movb $0x0, %al
callq 0x2330
movq 0xfcd4(%rip), %rax      # 0x12fd8
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
leaq 0xa543(%rip), %rsi      # 0xd855
movb $0x0, %al
callq 0x2330
movq 0xfcb8(%rip), %rax      # 0x12fd8
movq (%rax), %rdi
movq -0x50(%rbp), %rdx
leaq 0xa531(%rip), %rsi      # 0xd85f
movb $0x0, %al
callq 0x2330
cmpl $0x7, -0x58(%rbp)
jne 0x333d
jmp 0x336d
leaq 0xa525(%rip), %rdi      # 0xd869
leaq 0xa49e(%rip), %rsi      # 0xd7e9
movl $0x1f8, %edx            # imm = 0x1F8
leaq 0xa525(%rip), %rcx      # 0xd87c
callq 0x2170
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x39f5
jmp 0x336f
movl -0x80(%rbp), %edi
movl -0x84(%rbp), %esi
movl -0x88(%rbp), %edx
movl 0xfe6c(%rip), %ecx      # 0x131f0
callq 0x30d0
movl %eax, -0x8c(%rbp)
movb -0x51(%rbp), %dl
movq -0x10(%rbp), %rax
movslq -0x90(%rbp), %rcx
movb %dl, (%rax,%rcx)
movb -0x52(%rbp), %dl
movq -0x18(%rbp), %rax
movslq -0x90(%rbp), %rcx
movb %dl, (%rax,%rcx)
movslq -0x8c(%rbp), %rcx
leaq 0xfe35(%rip), %rax      # 0x131f4
movb (%rax,%rcx), %dl
movq -0x20(%rbp), %rax
movslq -0x90(%rbp), %rcx
movb %dl, (%rax,%rcx)
cmpl $0x0, -0x2c(%rbp)
jne 0x340e
movl -0x80(%rbp), %edi
movl -0x84(%rbp), %esi
movl -0x88(%rbp), %edx
callq 0x2f60
movsd %xmm0, -0xf0(%rbp)
jmp 0x33f4
movsd -0xf0(%rbp), %xmm0
movq -0x28(%rbp), %rax
movslq -0x90(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x3448
movl -0x80(%rbp), %edi
movl -0x84(%rbp), %esi
movl -0x88(%rbp), %edx
callq 0x2fd0
movss %xmm0, -0xf4(%rbp)
jmp 0x342c
movss -0xf4(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x28(%rbp), %rax
movslq -0x90(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x326c
movl -0x90(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x39c0
cmpl $0x1, 0x10(%rbp)
jne 0x3669
movq -0x68(%rbp), %rdi
movq -0x50(%rbp), %rsi
movl -0x34(%rbp), %edx
callq 0x2dc0
movl %eax, -0xf8(%rbp)
jmp 0x348c
movl -0xf8(%rbp), %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x94(%rbp)
movq -0x68(%rbp), %rdi
movq -0x50(%rbp), %rsi
movl -0x34(%rbp), %edx
callq 0x2dc0
movl %eax, -0xfc(%rbp)
jmp 0x34b7
movl -0xfc(%rbp), %eax
movl %eax, -0x30(%rbp)
cmpl $-0x1, %eax
je 0x365b
movq -0x50(%rbp), %rdi
leaq 0xa3f8(%rip), %rsi      # 0xd8cc
leaq -0x51(%rbp), %rdx
leaq -0x52(%rbp), %rcx
leaq -0x53(%rbp), %r8
movb $0x0, %al
callq 0x2200
movl %eax, -0x58(%rbp)
cmpl $0x3, -0x58(%rbp)
je 0x3569
movq 0xfae1(%rip), %rax      # 0x12fd8
movq (%rax), %rdi
leaq 0xa346(%rip), %rsi      # 0xd847
movb $0x0, %al
callq 0x2330
movq 0xfac9(%rip), %rax      # 0x12fd8
movq (%rax), %rdi
movq -0x8(%rbp), %rdx
leaq 0xa338(%rip), %rsi      # 0xd855
movb $0x0, %al
callq 0x2330
movq 0xfaad(%rip), %rax      # 0x12fd8
movq (%rax), %rdi
movq -0x50(%rbp), %rdx
leaq 0xa326(%rip), %rsi      # 0xd85f
movb $0x0, %al
callq 0x2330
cmpl $0x3, -0x58(%rbp)
jne 0x3548
jmp 0x3567
leaq 0xa387(%rip), %rdi      # 0xd8d6
leaq 0xa293(%rip), %rsi      # 0xd7e9
movl $0x219, %edx            # imm = 0x219
leaq 0xa31a(%rip), %rcx      # 0xd87c
callq 0x2170
jmp 0x3569
movsbl -0x52(%rbp), %eax
cmpl $0x45, %eax
jne 0x3578
movb $0x53, -0x52(%rbp)
jmp 0x3590
movsbl -0x52(%rbp), %eax
cmpl $0x43, %eax
je 0x358a
movsbl -0x52(%rbp), %eax
cmpl $0x4c, %eax
jne 0x358e
movb $0x52, -0x52(%rbp)
jmp 0x3590
movsbl -0x53(%rbp), %eax
cmpl $0x45, %eax
jne 0x359f
movb $0x53, -0x53(%rbp)
jmp 0x35b7
movsbl -0x53(%rbp), %eax
cmpl $0x43, %eax
je 0x35b1
movsbl -0x53(%rbp), %eax
cmpl $0x4c, %eax
jne 0x35b5
movb $0x52, -0x53(%rbp)
jmp 0x35b7
movb -0x51(%rbp), %dl
movq -0x10(%rbp), %rax
movslq -0x94(%rbp), %rcx
movb %dl, (%rax,%rcx)
movb -0x52(%rbp), %dl
movq -0x18(%rbp), %rax
movslq -0x94(%rbp), %rcx
movb %dl, (%rax,%rcx)
movb -0x53(%rbp), %dl
movq -0x20(%rbp), %rax
movslq -0x94(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
cmpl 0xfc03(%rip), %eax      # 0x13208
jl 0x3656
movq 0xf9b2(%rip), %rax      # 0x12fc0
movq (%rax), %rdi
movl 0xfbf1(%rip), %edx      # 0x13208
leaq 0xa2cb(%rip), %rsi      # 0xd8e9
movb $0x0, %al
callq 0x2330
movl -0x94(%rbp), %eax
cmpl 0xfbd7(%rip), %eax      # 0x13208
jge 0x3635
jmp 0x3654
leaq 0xa316(%rip), %rdi      # 0xd952
leaq 0xa1a6(%rip), %rsi      # 0xd7e9
movl $0x22b, %edx            # imm = 0x22B
leaq 0xa22d(%rip), %rcx      # 0xd87c
callq 0x2170
jmp 0x3656
jmp 0x349f
movl -0x94(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x39be
cmpl $0x2, 0x10(%rbp)
jne 0x39bc
movl $0x64, -0x98(%rbp)
movl -0x98(%rbp), %esi
incl %esi
leaq -0xa8(%rbp), %rdi
callq 0x8320
jmp 0x3693
movl 0xfb6f(%rip), %esi      # 0x13208
incl %esi
leaq -0xb8(%rbp), %rdi
callq 0x8320
jmp 0x36a9
movq -0xb0(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0xc8(%rbp)
movl $0x0, -0xcc(%rbp)
movl $0x0, -0xd0(%rbp)
movq -0x68(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movl 0xfb1e(%rip), %ecx      # 0x13208
movq -0xc8(%rbp), %r8
movl -0x98(%rbp), %r9d
xorl %eax, %eax
movl %eax, %edx
callq 0x2ba0
movl %eax, -0x100(%rbp)
jmp 0x3709
movl -0x100(%rbp), %eax
movl %eax, -0xcc(%rbp)
cmpl $-0x1, %eax
je 0x384e
movq -0xc8(%rbp), %rdi
leaq 0xa23a(%rip), %rsi      # 0xd966
callq 0x20b0
cmpq $0x0, %rax
je 0x3783
movq -0x10(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movl 0xfac0(%rip), %edx      # 0x13208
subl $0x1, %edx
callq 0x2830
jmp 0x37ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x39ae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x8360
jmp 0x39ae
movq -0xc8(%rbp), %rdi
leaq 0xa1d8(%rip), %rsi      # 0xd969
callq 0x20b0
cmpq $0x0, %rax
je 0x37b7
movq -0x18(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movl 0xfa5b(%rip), %edx      # 0x13208
subl $0x1, %edx
callq 0x2830
jmp 0x37eb
movq -0xc8(%rbp), %rdi
leaq 0xa1a9(%rip), %rsi      # 0xd96e
callq 0x20b0
cmpq $0x0, %rax
je 0x37e9
movq -0x20(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movl 0xfa27(%rip), %edx      # 0x13208
subl $0x1, %edx
callq 0x2830
jmp 0x37eb
jmp 0x37ed
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
cmpl $0x3, -0xd0(%rbp)
jle 0x3849
movq 0xf7cc(%rip), %rax      # 0x12fd8
movq (%rax), %rdi
leaq 0xa15d(%rip), %rsi      # 0xd973
movb $0x0, %al
callq 0x2330
cmpl $0x3, -0xd0(%rbp)
jg 0x3828
jmp 0x3847
leaq 0xa19f(%rip), %rdi      # 0xd9ce
leaq 0x9fb3(%rip), %rsi      # 0xd7e9
movl $0x246, %edx            # imm = 0x246
leaq 0xa03a(%rip), %rcx      # 0xd87c
callq 0x2170
jmp 0x3849
jmp 0x36d9
movl $0x0, -0xd4(%rbp)
movq -0x18(%rbp), %rdi
callq 0x2110
movl %eax, -0xcc(%rbp)
movl $0x0, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
cmpl -0xcc(%rbp), %eax
jge 0x38f0
movq -0x18(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x45, %eax
jne 0x38a4
movq -0x18(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movb $0x53, (%rax,%rcx)
jmp 0x38dd
movq -0x18(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x43, %eax
je 0x38cc
movq -0x18(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x4c, %eax
jne 0x38db
movq -0x18(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movb $0x52, (%rax,%rcx)
jmp 0x38dd
jmp 0x38df
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0x3871
movq -0x20(%rbp), %rdi
callq 0x2110
movl %eax, -0xcc(%rbp)
movl $0x0, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
cmpl -0xcc(%rbp), %eax
jge 0x3988
movq -0x20(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x45, %eax
jne 0x393c
movq -0x20(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movb $0x53, (%rax,%rcx)
jmp 0x3975
movq -0x20(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x43, %eax
je 0x3964
movq -0x20(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x4c, %eax
jne 0x3973
movq -0x20(%rbp), %rax
movslq -0xd4(%rbp), %rcx
movb $0x52, (%rax,%rcx)
jmp 0x3975
jmp 0x3977
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0x3909
movq -0x10(%rbp), %rdi
callq 0x2110
movl %eax, -0x5c(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x8360
leaq -0xa8(%rbp), %rdi
callq 0x8360
jmp 0x39bc
leaq -0xa8(%rbp), %rdi
callq 0x8360
jmp 0x39f5
jmp 0x39be
jmp 0x39c0
cmpq $0x0, -0x68(%rbp)
je 0x39d4
movq -0x68(%rbp), %rdi
callq 0x2210
jmp 0x39d2
jmp 0x39d4
movl -0x5c(%rbp), %eax
movl %eax, -0x104(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x8360
movl -0x104(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x8360
movq -0x70(%rbp), %rdi
callq 0x2380
nopw (%rax,%rax)