// File: /workspace/github_c_makefile_star2_O2/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, %r14
xorl %eax, %eax
cmpq $0x65, %rax
je 0x679c
andq $0x0, 0x30(%rsp,%rax,8)
andq $0x0, 0xcf0(%rsp,%rax,8)
andq $0x0, 0x9c0(%rsp,%rax,8)
andq $0x0, 0x690(%rsp,%rax,8)
andq $0x0, 0x360(%rsp,%rax,8)
incq %rax
jmp 0x6767
xorl %eax, %eax
callq 0x8a9d
movl $0x0, %r12d
movl $0x0, %ecx
movq %rcx, 0x10(%rsp)
movl $0x0, %ecx
movq %rcx, 0x18(%rsp)
movl $0x0, %r13d
movl $0x0, %ebp
testl %eax, %eax
jne 0x6928
xorl %ecx, %ecx
xorl %r12d, %r12d
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
xorl %r13d, %r13d
xorl %ebp, %ebp
movslq (%r14), %rax
cmpq %rax, %rcx
jge 0x68f2
movq 0xa0(%r14), %rax
movq %rcx, 0x8(%rsp)
movq (%rax,%rcx,8), %r15
movq %rbp, %rdi
callq 0x3070
movl 0x380(%rbx), %esi
movq %r15, %rdi
callq 0x95fc
movapd 0x132ca(%rip), %xmm4    # 0x19af0
movsd 0x13252(%rip), %xmm3    # 0x19a80
movq %rax, %rbp
movl 0x8(%r15), %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovlel %eax, %ecx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rcx
je 0x68e5
movq 0x30(%r15), %rax
cvtss2sd (%rax,%rsi,4), %xmm0
ucomisd %xmm0, %xmm3
jbe 0x6870
incq %r12
movq 0x10(%r15), %rax
xorl %edx, %edx
cmpb $0x4e, (%rax,%rsi)
sete %dl
addq %rdx, %rdi
jmp 0x6872
xorl %edi, %edi
movq 0x380(%rbx), %rax
cqto
pushq $0x3
popq %r8
idivq %r8
cmpq %rax, %rdi
jle 0x689c
incq %rax
xorl %edx, %edx
cmpq %rax, %rdi
sete %dl
addq %rdx, 0x18(%rsp)
incq %r13
jmp 0x68dd
movupd 0x58(%r14), %xmm1
movapd %xmm4, %xmm2
movsd %xmm0, %xmm2            # xmm2 = xmm0[0],xmm2[1]
addpd %xmm1, %xmm2
movupd %xmm2, 0x58(%r14)
movzbl (%rbp,%rsi), %eax
cmpq $0x65, %rax
jb 0x68c6
incq 0x10(%rsp)
jmp 0x68dd
shll $0x3, %eax
addsd 0x30(%rsp,%rax), %xmm0
movsd %xmm0, 0x30(%rsp,%rax)
incq 0x360(%rsp,%rax)
incq %rsi
jmp 0x6843
movq 0x8(%rsp), %rcx
incq %rcx
jmp 0x67ec
movsd 0x58(%r14), %xmm0
divsd 0x60(%r14), %xmm0
leaq 0x30(%rsp), %rdi
leaq 0x360(%rsp), %rsi
leaq 0x9c0(%rsp), %rdx
leaq 0xcf0(%rsp), %rcx
leaq 0x690(%rsp), %r8
callq 0x6666
xorl %ecx, %ecx
movq %rbx, 0x20(%rsp)
movslq (%r14), %rax
cmpq %rax, %rcx
jge 0x6c23
movq 0xa0(%r14), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %r15
movq %rbp, %rdi
callq 0x3070
movl 0x380(%rbx), %esi
movq %r15, %rdi
callq 0x95fc
movq %rax, %rbp
xorl %eax, %eax
callq 0x8a9d
testl %eax, %eax
je 0x6ab3
xorl %eax, %eax
cmpq $0x65, %rax
je 0x69aa
andq $0x0, 0x30(%rsp,%rax,8)
andq $0x0, 0xcf0(%rsp,%rax,8)
andq $0x0, 0x9c0(%rsp,%rax,8)
andq $0x0, 0x690(%rsp,%rax,8)
andq $0x0, 0x360(%rsp,%rax,8)
incq %rax
jmp 0x6975
movl 0x8(%r15), %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovlel %eax, %ecx
xorpd %xmm0, %xmm0
xorl %esi, %esi
xorl %edi, %edi
xorpd %xmm1, %xmm1
cmpq %rsi, %rcx
je 0x6a7e
movq 0x30(%r15), %rax
cvtss2sd (%rax,%rsi,4), %xmm2
movsd 0x130a2(%rip), %xmm3    # 0x19a80
ucomisd %xmm2, %xmm3
jbe 0x69f9
incq %r12
movq 0x10(%r15), %rax
xorl %edx, %edx
cmpb $0x4e, (%rax,%rsi)
sete %dl
addq %rdx, %rdi
jmp 0x69fb
xorl %edi, %edi
movq 0x380(%rbx), %rax
cqto
pushq $0x3
popq %r8
idivq %r8
cmpq %rax, %rdi
jle 0x6a25
incq %rax
xorl %edx, %edx
cmpq %rax, %rdi
sete %dl
addq %rdx, 0x18(%rsp)
incq %r13
jmp 0x6a76
movupd 0x58(%r14), %xmm3
movapd 0x130bd(%rip), %xmm4    # 0x19af0
movsd %xmm2, %xmm4            # xmm4 = xmm2[0],xmm4[1]
addpd %xmm3, %xmm4
movupd %xmm4, 0x58(%r14)
addsd %xmm2, %xmm0
addsd 0x12fe3(%rip), %xmm1    # 0x19a30
movzbl (%rbp,%rsi), %eax
cmpq $0x65, %rax
jb 0x6a5f
incq 0x10(%rsp)
jmp 0x6a76
shll $0x3, %eax
addsd 0x30(%rsp,%rax), %xmm2
movsd %xmm2, 0x30(%rsp,%rax)
incq 0x360(%rsp,%rax)
incq %rsi
jmp 0x69c4
movq %r12, 0x8(%rsp)
divsd %xmm1, %xmm0
leaq 0x30(%rsp), %rdi
leaq 0x360(%rsp), %rsi
leaq 0x9c0(%rsp), %rdx
leaq 0xcf0(%rsp), %rcx
leaq 0x690(%rsp), %r8
callq 0x6666
jmp 0x6ab8
movq %r12, 0x8(%rsp)
xorl %r12d, %r12d
movslq 0x8(%r15), %rax
cmpq %rax, %r12
jge 0x6c0c
movzbl (%rbp,%r12), %ebx
cmpq $0x65, %rbx
jb 0x6b05
xorl %eax, %eax
callq 0x8a45
cvtsd2ss %xmm0, %xmm0
movq 0x48(%r15), %rax
movss %xmm0, (%rax,%r12,4)
xorl %eax, %eax
callq 0x8a45
addsd 0x10(%r14), %xmm0
movsd %xmm0, 0x10(%r14)
xorl %eax, %eax
callq 0x8a45
jmp 0x6b68
movq 0x30(%r15), %rax
movss (%rax,%r12,4), %xmm0
callq 0x3570
cvttss2si %xmm0, %edi
movsd 0x9c0(%rsp,%rbx,8), %xmm0
movsd 0x12f1f(%rip), %xmm1    # 0x19a48
callq 0x6529
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jb 0x6c7a
subsd 0x690(%rsp,%rbx,8), %xmm0
callq 0x8a4e
cvtsd2ss %xmm0, %xmm1
movq 0x48(%r15), %rax
movss %xmm1, (%rax,%r12,4)
movapd %xmm0, %xmm1
addsd 0x10(%r14), %xmm1
movsd %xmm1, 0x10(%r14)
movupd 0x48(%r14), %xmm1
shufpd $0x2, 0x12f79(%rip), %xmm0 # xmm0 = xmm0[0],mem[1]
addpd %xmm1, %xmm0
movupd %xmm0, 0x48(%r14)
movq 0x30(%r15), %rax
movss (%rax,%r12,4), %xmm0
ucomiss 0x1718e(%rip), %xmm0    # 0x1dd20
jne 0x6bb4
jp 0x6bb4
xorl %eax, %eax
callq 0x8a45
cvtsd2ss %xmm0, %xmm0
movq 0x38(%r15), %rax
movss %xmm0, (%rax,%r12,4)
xorl %eax, %eax
callq 0x8a45
jmp 0x6bf6
movq 0x38(%r15), %rax
movss (%rax,%r12,4), %xmm1
divss %xmm0, %xmm1
cvtss2sd %xmm1, %xmm0
callq 0x8a4e
cvtsd2ss %xmm0, %xmm0
movq 0x38(%r15), %rax
movss %xmm0, (%rax,%r12,4)
movq 0x30(%r15), %rax
movq 0x40(%r15), %rcx
movss (%rcx,%r12,4), %xmm0
divss (%rax,%r12,4), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x8a4e
cvtsd2ss %xmm0, %xmm0
movq 0x40(%r15), %rax
movss %xmm0, (%rax,%r12,4)
incq %r12
jmp 0x6abb
movq 0x28(%rsp), %rcx
incq %rcx
movq 0x20(%rsp), %rbx
movq 0x8(%rsp), %r12
jmp 0x692f
movq %rbp, %rdi
callq 0x3070
leaq 0x17924(%rip), %rdi     # 0x1e556
movq 0x10(%rsp), %rsi
xorl %eax, %eax
callq 0x3210
leaq 0x17950(%rip), %rdi     # 0x1e595
movq %r13, %rsi
movq 0x18(%rsp), %rdx
xorl %eax, %eax
callq 0x3210
leaq 0x1796e(%rip), %rdi     # 0x1e5c9
movq %r12, %rsi
xorl %eax, %eax
callq 0x3210
pushq $0x1
popq %rax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17889(%rip), %rdi     # 0x1e50a
leaq 0x170ad(%rip), %rsi     # 0x1dd35
leaq 0x1788e(%rip), %rcx     # 0x1e51d
movl $0x68e, %edx            # imm = 0x68E
callq 0x3230