// File: /workspace/github_c_makefile_star2_O3/sc932[P]ALE/src/ALElike.c
// Function: computeDepthStats
int computeDepthStats(assemblyT *theAssembly, libraryParametersT *libParams){
    // 1. Find the GC content of each read
    int i, j, k;
    int GCpct;
    long place;
    double depthNormalizer[102];
    long depthNormalizerCount[102];

    double negBinomParam_p[102];
    double negBinomParam_r[102];
    double negBinomParamZnorm_r[102];
    double tempLogLike;
    long tooLowCoverageBases = 0;
    long noGCInformation = 0;
    long unmappableRegions = 0;
    long unmappableBases = 0;
    long tmpunmappable = 0;
    unsigned char *GCcont = NULL;

    // initialize
    for(j = 0; j < 101; j++){
        depthNormalizer[j] = 0.0;
        negBinomParam_p[j] = 0.0;
        negBinomParam_r[j] = 0.0;
        negBinomParamZnorm_r[j] = 0.0;
        depthNormalizerCount[j] = 0;
    }
    if (!isMetagenome()) {
        for(i = 0; i < theAssembly->numContigs; i++){ // for each contig
            contig_t *contig = theAssembly->contigs[i];
            if (GCcont != NULL) free(GCcont);
            GCcont = calculateContigGCcont(contig, libParams->avgReadSize);
            tmpunmappable = 0;
            for(j = 0; j < contig->seqLen; j++){
                float depth = contig->depth[j];
                if (depth < 0.1) {
                    tooLowCoverageBases++;
        			if (contig->seq[j] == 'N')
        				tmpunmappable++;
                } else
                	tmpunmappable = 0;

                // Do not record this depth and normalization if this region is not possible to map to.
                if (tmpunmappable > libParams->avgReadSize / 3) {
                	if (tmpunmappable == (libParams->avgReadSize / 3) + 1)
                		unmappableRegions++;
                	unmappableBases++;
                	continue;
                }

                theAssembly->depthAvgSum += depth;
                theAssembly->depthAvgNorm += 1.0;
                GCpct = GCcont[j];
                if (GCpct > 100) {
                    noGCInformation++;
                    continue;
                }
                depthNormalizer[GCpct] += depth;
                depthNormalizerCount[GCpct] += 1;
            }
        }

        // 2. Find the parameters for the distributions

        computeNormaliziedDepthGCParameters(depthNormalizer, depthNormalizerCount, negBinomParam_r, negBinomParam_p, negBinomParamZnorm_r, theAssembly->depthAvgSum / theAssembly->depthAvgNorm);
    }

    for(i = 0; i < theAssembly->numContigs; i++){ // for each contig
        contig_t *contig = theAssembly->contigs[i];
        if (GCcont != NULL) free(GCcont);
        GCcont = calculateContigGCcont(contig, libParams->avgReadSize);
        if (isMetagenome()) {
        	// re-initialize depth for this contig
        	for(j = 0; j < 101; j++){
        		depthNormalizer[j] = 0.0;
        		negBinomParam_p[j] = 0.0;
        		negBinomParam_r[j] = 0.0;
        		negBinomParamZnorm_r[j] = 0.0;
        		depthNormalizerCount[j] = 0;
        	}

            tmpunmappable = 0;
            double avgDepth = 0.0;
            double countDepth = 0.0;
        	for(j = 0; j < contig->seqLen; j++){
        		float depth = contig->depth[j];
        		if (depth < 0.1) {
        			tooLowCoverageBases++;
        			if (contig->seq[j] == 'N')
        				tmpunmappable++;
        		} else
        			tmpunmappable = 0;

                // Do not record this depth and normalization if this region is not possible to map to.
                if (tmpunmappable > libParams->avgReadSize / 3) {
                	if (tmpunmappable == libParams->avgReadSize / 3 + 1)
                		unmappableRegions++;
                	unmappableBases++;
                	continue;
                }

        		theAssembly->depthAvgSum += depth;
        		theAssembly->depthAvgNorm += 1.0;
        		avgDepth += depth;
        		countDepth += 1.0;
        		GCpct = GCcont[j];
        		if (GCpct > 100) {
        			noGCInformation++;
        			continue;
        		}
        		depthNormalizer[GCpct] += depth;
        		depthNormalizerCount[GCpct] += 1;
        	}

        	// 2. Find the parameters for the distributions

            computeNormaliziedDepthGCParameters(depthNormalizer, depthNormalizerCount, negBinomParam_r, negBinomParam_p, negBinomParamZnorm_r, avgDepth / countDepth);
        }

        //printf("Calculating likelihoods for %d positions\n", contig->seqLen);
        // 3. Find the depth likelihood
        for(j = 0; j < contig->seqLen; j++){
            GCpct = GCcont[j];
            if (GCpct > 100){
                //printf("GC correction failed for contig %d at %d '%c'\n", i, j, contig->seq[j]);
            	contig->depthLogLikelihood[j]  = getMinLogLike();
            	theAssembly->totalScore += getMinLogLike();
            	theAssembly->depthScoreAvgSum += getMinLogLike();
            	theAssembly->depthScoreAvgNorm += 1.0;
            } else {

            	// compute the depth likelihood using poisson or negBinomial
            	// contig->depth[j] is the depth at position j
            	// depthNormalizer[GCpct] is avg depth for that GC content

            	// tempLike = poissonPMF(contig->depth[j], depthNormalizer[GCpct]); // log poisson pmf
            	tempLogLike = negBinomPMF((int)floor(contig->depth[j]), negBinomParam_r[GCpct], 0.5); // log neg binom pmf
            	assert(tempLogLike <= 0.0);

            	// z normalization
            	tempLogLike -= negBinomParamZnorm_r[GCpct];
            	//if((int)floor(negBinomParam_r[GCpct]) < 2047){
            	//    tempLike -= negBinomZ[(int)floor(negBinomParam_r[GCpct])];
            	//}else{
            	//    // not in lookup table, compute
            	//    tempLike -= getNegBinomZnorm(negBinomParam_r[GCpct]);
            	//}
            	tempLogLike = validateLogLikelihood( tempLogLike );

            	// apply to assembly and totalScore
            	contig->depthLogLikelihood[j] = tempLogLike;
            	theAssembly->totalScore += tempLogLike; // depth contribution to totalScore at this position
            	theAssembly->depthScoreAvgSum += tempLogLike;
            	theAssembly->depthScoreAvgNorm += 1.0;
            }
            // at this point contig->matchLikelihood[j] contains the sum of the logs of the TOTAL likelihood of all the reads that map over position j
            // then we take the gemetric average by dividing by the depth and change it (if it is a valid likelihood)
            
            if (contig->depth[j] == 0) { // avoid div by zero...
            	contig->matchLogLikelihood[j] = getMinLogLike();
            	contig->insertLogLikelihood[j] = getMinLogLike();
            } else {
                // match
            	contig->matchLogLikelihood[j] =  validateLogLikelihood( contig->matchLogLikelihood[j]/contig->depth[j] );;
                // insert
                contig->insertLogLikelihood[j] = validateLogLikelihood( contig->insertLogLikelihood[j]/contig->depth[j] );;
            }
            
        }
    }
    if (GCcont != NULL) free(GCcont);
    printf("bases with no GC depth correction metric (small contigs): %ld\n", noGCInformation);
    printf("unmappable bases: %ld across %ld different regions\n", unmappableBases, unmappableRegions);
    printf("bases with 0 coverage: %ld\n", tooLowCoverageBases);
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1028, %rsp           # imm = 0x1028
movq %rsi, %rbx
movq %rdi, %r15
leaq 0x30(%rsp), %rdi
movq $0x0, 0x8(%rsp)
movl $0x328, %edx            # imm = 0x328
xorl %esi, %esi
callq 0x3260
leaq 0x9c0(%rsp), %rdi
movl $0x328, %edx            # imm = 0x328
xorl %esi, %esi
callq 0x3260
leaq 0x690(%rsp), %rdi
movl $0x328, %edx            # imm = 0x328
xorl %esi, %esi
callq 0x3260
leaq 0x360(%rsp), %rdi
movl $0x328, %edx            # imm = 0x328
xorl %esi, %esi
callq 0x3260
xorl %eax, %eax
callq 0xaba1
movl $0x0, %ecx
movq %rcx, 0x18(%rsp)
movl $0x0, %ecx
movq %rcx, 0x10(%rsp)
movl $0x0, %r13d
movl $0x0, %r14d
testl %eax, %eax
jne 0x88ff
cmpl $0x0, (%r15)
jle 0x88a8
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
movq 0xa0(%r15), %rax
movq (%rax,%r12,8), %rbp
testq %r14, %r14
je 0x879b
movq %r14, %rdi
callq 0x3070
movl 0x380(%rbx), %esi
movq %rbp, %rdi
callq 0xb88f
movq %rax, %r14
movslq 0x8(%rbp), %rcx
testq %rcx, %rcx
jle 0x888d
movabsq $0x5555555555555556, %rax # imm = 0x5555555555555556
imulq 0x380(%rbx)
movq 0x30(%rbp), %rax
movq %rdx, %rdi
shrq $0x3f, %rdi
leaq (%rdx,%rdi), %rsi
addq %rdi, %rdx
incq %rdx
xorl %edi, %edi
xorl %r8d, %r8d
movq 0x8(%rsp), %r11
movsd 0x1528f(%rip), %xmm3    # 0x1da80
movapd 0x15327(%rip), %xmm4    # 0x1db20
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rdi,4), %xmm0
ucomisd %xmm0, %xmm3
jbe 0x881f
incq %r11
movq 0x10(%rbp), %r9
xorl %r10d, %r10d
cmpb $0x4e, (%r9,%rdi)
sete %r10b
addq %r10, %r8
jmp 0x8822
xorl %r8d, %r8d
cmpq %rsi, %r8
jle 0x883b
xorl %r9d, %r9d
cmpq %rdx, %r8
sete %r9b
addq %r9, 0x10(%rsp)
incq %r13
jmp 0x887f
movupd 0x58(%r15), %xmm1
movapd %xmm4, %xmm2
movsd %xmm0, %xmm2            # xmm2 = xmm0[0],xmm2[1]
addpd %xmm1, %xmm2
movupd %xmm2, 0x58(%r15)
movzbl (%r14,%rdi), %r9d
cmpq $0x65, %r9
jb 0x8865
incq 0x18(%rsp)
jmp 0x887f
shll $0x3, %r9d
addsd 0x30(%rsp,%r9), %xmm0
movsd %xmm0, 0x30(%rsp,%r9)
incq 0x360(%rsp,%r9)
incq %rdi
cmpq %rdi, %rcx
jne 0x87f9
jmp 0x8892
movq 0x8(%rsp), %r11
movq %r11, 0x8(%rsp)
incq %r12
movslq (%r15), %rax
cmpq %rax, %r12
jl 0x8783
jmp 0x88c9
movq $0x0, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
xorl %r13d, %r13d
xorl %r14d, %r14d
movsd 0x58(%r15), %xmm0
divsd 0x60(%r15), %xmm0
leaq 0x30(%rsp), %rdi
leaq 0x360(%rsp), %rsi
leaq 0x9c0(%rsp), %rdx
leaq 0xcf0(%rsp), %rcx
leaq 0x690(%rsp), %r8
callq 0x85d3
cmpl $0x0, (%r15)
jle 0x8c41
xorl %ecx, %ecx
movq %rbx, 0x20(%rsp)
movq 0xa0(%r15), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %rbp
testq %r14, %r14
je 0x892d
movq %r14, %rdi
callq 0x3070
movl 0x380(%rbx), %esi
movq %rbp, %rdi
callq 0xb88f
movq %rax, %r14
xorl %eax, %eax
callq 0xaba1
testl %eax, %eax
je 0x8ad3
movl $0x328, %edx            # imm = 0x328
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x3260
movl $0x328, %edx            # imm = 0x328
leaq 0x9c0(%rsp), %rdi
xorl %esi, %esi
callq 0x3260
movl $0x328, %edx            # imm = 0x328
leaq 0x690(%rsp), %rdi
xorl %esi, %esi
callq 0x3260
movl $0x328, %edx            # imm = 0x328
leaq 0x360(%rsp), %rdi
xorl %esi, %esi
callq 0x3260
movslq 0x8(%rbp), %rcx
testq %rcx, %rcx
jle 0x8a97
movq 0x30(%rbp), %rsi
movabsq $0x5555555555555556, %rax # imm = 0x5555555555555556
imulq 0x380(%rbx)
movq %rdx, %rdi
shrq $0x3f, %rdi
leaq (%rdx,%rdi), %rax
addq %rdi, %rdx
incq %rdx
xorpd %xmm1, %xmm1
xorl %edi, %edi
xorpd %xmm0, %xmm0
xorl %r8d, %r8d
movq 0x8(%rsp), %r11
movq 0x10(%rsp), %rbx
xorps %xmm2, %xmm2
cvtss2sd (%rsi,%rdi,4), %xmm2
movsd 0x1508c(%rip), %xmm3    # 0x1da80
ucomisd %xmm2, %xmm3
jbe 0x8a12
incq %r11
movq 0x10(%rbp), %r9
xorl %r10d, %r10d
cmpb $0x4e, (%r9,%rdi)
sete %r10b
addq %r10, %r8
jmp 0x8a15
xorl %r8d, %r8d
cmpq %rax, %r8
jle 0x8a2c
xorl %r9d, %r9d
cmpq %rdx, %r8
sete %r9b
addq %r9, %rbx
incq %r13
jmp 0x8a80
movupd 0x58(%r15), %xmm3
movapd 0x150e6(%rip), %xmm4    # 0x1db20
movsd %xmm2, %xmm4            # xmm4 = xmm2[0],xmm4[1]
addpd %xmm3, %xmm4
movupd %xmm4, 0x58(%r15)
addsd %xmm2, %xmm0
addsd 0x14fdc(%rip), %xmm1    # 0x1da30
movzbl (%r14,%rdi), %r9d
cmpq $0x65, %r9
jb 0x8a66
incq 0x18(%rsp)
jmp 0x8a80
shll $0x3, %r9d
addsd 0x30(%rsp,%r9), %xmm2
movsd %xmm2, 0x30(%rsp,%r9)
incq 0x360(%rsp,%r9)
incq %rdi
cmpq %rdi, %rcx
jne 0x89e4
movq %r11, 0x8(%rsp)
divsd %xmm1, %xmm0
jmp 0x8aa4
movsd 0x14fe9(%rip), %xmm0    # 0x1da88
movq 0x10(%rsp), %rbx
movq %rbx, 0x10(%rsp)
leaq 0x30(%rsp), %rdi
leaq 0x360(%rsp), %rsi
leaq 0x9c0(%rsp), %rdx
leaq 0xcf0(%rsp), %rcx
leaq 0x690(%rsp), %r8
callq 0x85d3
cmpl $0x0, 0x8(%rbp)
jle 0x8c28
xorl %ebx, %ebx
movzbl (%r14,%rbx), %r12d
cmpq $0x65, %r12
jb 0x8b1a
xorl %eax, %eax
callq 0xab49
cvtsd2ss %xmm0, %xmm0
movq 0x48(%rbp), %rax
movss %xmm0, (%rax,%rbx,4)
xorl %eax, %eax
callq 0xab49
addsd 0x10(%r15), %xmm0
movsd %xmm0, 0x10(%r15)
xorl %eax, %eax
callq 0xab49
jmp 0x8b80
movq 0x30(%rbp), %rax
movss (%rax,%rbx,4), %xmm0
callq 0x3570
cvttss2si %xmm0, %edi
movsd 0x9c0(%rsp,%r12,8), %xmm0
movsd 0x14f0a(%rip), %xmm1    # 0x1da48
callq 0x848b
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jb 0x8ca1
subsd 0x690(%rsp,%r12,8), %xmm0
callq 0xab52
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movq 0x48(%rbp), %rax
movss %xmm1, (%rax,%rbx,4)
movapd %xmm0, %xmm1
addsd 0x10(%r15), %xmm1
movsd %xmm1, 0x10(%r15)
movupd 0x48(%r15), %xmm1
shufpd $0x2, 0x14f91(%rip), %xmm0 # xmm0 = xmm0[0],mem[1]
addpd %xmm1, %xmm0
movupd %xmm0, 0x48(%r15)
movq 0x30(%rbp), %rax
movss (%rax,%rbx,4), %xmm0
ucomiss 0x191a7(%rip), %xmm0    # 0x21d50
jne 0x8bca
jp 0x8bca
xorl %eax, %eax
callq 0xab49
cvtsd2ss %xmm0, %xmm0
movq 0x38(%rbp), %rax
movss %xmm0, (%rax,%rbx,4)
xorl %eax, %eax
callq 0xab49
jmp 0x8c0b
movq 0x38(%rbp), %rax
movss (%rax,%rbx,4), %xmm1
divss %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
callq 0xab52
cvtsd2ss %xmm0, %xmm0
movq 0x38(%rbp), %rax
movss %xmm0, (%rax,%rbx,4)
movq 0x30(%rbp), %rax
movq 0x40(%rbp), %rcx
movss (%rcx,%rbx,4), %xmm0
divss (%rax,%rbx,4), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0xab52
cvtsd2ss %xmm0, %xmm0
movq 0x40(%rbp), %rax
movss %xmm0, (%rax,%rbx,4)
incq %rbx
movslq 0x8(%rbp), %rax
cmpq %rax, %rbx
jl 0x8adf
movq 0x28(%rsp), %rcx
incq %rcx
movslq (%r15), %rax
cmpq %rax, %rcx
movq 0x20(%rsp), %rbx
jl 0x8910
testq %r14, %r14
je 0x8c4e
movq %r14, %rdi
callq 0x3070
leaq 0x19931(%rip), %rdi     # 0x22586
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0x3210
leaq 0x1995d(%rip), %rdi     # 0x225c5
movq %r13, %rsi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x3210
leaq 0x1997b(%rip), %rdi     # 0x225f9
movq 0x8(%rsp), %rsi
xorl %eax, %eax
callq 0x3210
movl $0x1, %eax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x19892(%rip), %rdi     # 0x2253a
leaq 0x190b6(%rip), %rsi     # 0x21d65
leaq 0x19897(%rip), %rcx     # 0x2254d
movl $0x68e, %edx            # imm = 0x68E
callq 0x3230