// File: nanjiangshu[P]calSOV/calSOV.cpp
// Function: CalQ3ConfAccumulate(char*, char*, double*, int, int**, int**, double**, int, double)
int CalQ3ConfAccumulate(char *obsSec, char *predSec, double *predConf, int seqLength, int **numCorrectHSR, int **numHSR, double **q3HSR, int numState , double halfBinWidth)/*{{{*/
/*=======================================================================
 * calculate Q3 for residues lying above a certain confidence
 * numCorrectHSR[N][4]: N is the number of bins, and [4] is for Helix, Sheet, Coil and All
 * numHSR[N][4] Helix, Sheet, Coil, All
 * q3HSR[N][4]
 * numState = 3 by default
 *
 * predConf is in the range of [0,100]
 *
 * bins are setting like this. 
 * for numCorrectHSR[50][j], residues with predConf >= 50 are included
 * predConf has two format: 
 *  1: rawConfidence: raw confidence for secondary structure is calculated as
 *  maxProb / sumProb
 *  2: nomalized confidence:  the normalization function is generated by
 *  second order polynomial regression 
 *  2009-07-31 
 *
 *====================================================================*/
{

    int iterHSRState = 0; /*iterator for the HSR state, H, S and R*/
    char thisHSRstate = ' '; /*current HSR state*/
    int i = 0;

    int iBin;         /*iterator of the bin*/
    for (i=0;i<=numState; i++)
    {
        for (iBin = 0; iBin <=100; iBin++)
        {
            numCorrectHSR[iBin][i] = 0;
            numHSR[iBin][i] = 0;
        }
    }

    int iBinBeg = 0;
    int iBinEnd = 0;
    for (iterHSRState=0; iterHSRState<3; iterHSRState++)//corrsponding to H, S, R
    {
        thisHSRstate = HSRalphabet[iterHSRState];
        for(i=0; i<seqLength; i++)
        { 
            iBinBeg = 0;
            iBinEnd = int(floor(predConf[i]));;
            /* so if predConf = 35.1, 
             * */
            
            for (iBin = iBinBeg ; iBin <= iBinEnd; iBin++)
            { 
                if (  obsSec[i] == thisHSRstate  )
                {
                    numHSR[iBin][iterHSRState] ++;
                    if (obsSec[i] == predSec[i])
                    {
                        numCorrectHSR[iBin][iterHSRState] ++;
                    }
                }
            }
        } 
    }

    for (iBin = 0; iBin <= 100; iBin++)
    {
        for (i=0; i<numState; i++)
        {   
            q3HSR[iBin][i] = numCorrectHSR[iBin][i] / double (numHSR[iBin][i]+1e-6);
            numCorrectHSR[iBin][numState]  += numCorrectHSR[iBin][i];
            numHSR[iBin][numState]  += numHSR[iBin][i];
        }
        q3HSR[iBin][numState] = numCorrectHSR[iBin][numState] / double (numHSR[iBin][numState]+1e-6); 
    }

    return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movl 0x18(%rbp), %eax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movb $0x20, -0x3d(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl 0x18(%rbp), %eax
jg 0x563f
movl $0x0, -0x48(%rbp)
cmpl $0x64, -0x48(%rbp)
jg 0x5632
movq -0x28(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x55f3
jmp 0x5634
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x55e4
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x3c(%rbp)
cmpl $0x3, -0x3c(%rbp)
jge 0x5741
movslq -0x3c(%rbp), %rcx
leaq 0xdb8b(%rip), %rax      # 0x131f4
movb (%rax,%rcx), %al
movb %al, -0x3d(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x5731
movl $0x0, -0x4c(%rbp)
movq -0x18(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x2370
cvttsd2si %xmm0, %eax
movl %eax, -0x50(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x50(%rbp), %eax
jg 0x5721
movq -0x8(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movsbl -0x3d(%rbp), %ecx
cmpl %ecx, %eax
jne 0x5714
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x3c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsbl (%rax,%rcx), %eax
movq -0x10(%rbp), %rcx
movslq -0x44(%rbp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
jne 0x5712
movq -0x28(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x3c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x5714
jmp 0x5716
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x56a8
jmp 0x5723
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x5676
jmp 0x5733
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x5654
movl $0x0, -0x48(%rbp)
cmpl $0x64, -0x48(%rbp)
jg 0x5871
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl 0x18(%rbp), %eax
jge 0x5814
movq -0x28(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm1
movsd 0x7871(%rip), %xmm2     # 0xd008
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq 0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x28(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x18(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x44(%rbp), %rcx
movl (%rax,%rcx,4), %edx
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x18(%rbp), %rcx
addl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x5759
movq -0x28(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x18(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
movq -0x30(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x18(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm1
movsd 0x77c2(%rip), %xmm2     # 0xd008
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movq 0x10(%rbp), %rax
movslq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0x18(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x5748
xorl %eax, %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)