// File: /workspace/github_c_makefile_star2_O1/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 0xcf0(%rsp), %rdi
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 0xa8d9
movl $0x0, %ecx
movq %rcx, 0x18(%rsp)
movl $0x0, %ecx
movq %rcx, 0x10(%rsp)
movl $0x0, %r13d
movl $0x0, %r14d
testl %eax, %eax
jne 0x85fd
cmpl $0x0, (%r15)
jle 0x85a6
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 0x8491
movq %r14, %rdi
callq 0x3070
movl 0x380(%rbx), %esi
movq %rbp, %rdi
callq 0xb61b
movq %rax, %r14
movslq 0x8(%rbp), %rcx
testq %rcx, %rcx
jle 0x858b
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 0x155d9(%rip), %xmm2    # 0x1dac0
movsd 0x15549(%rip), %xmm3    # 0x1da38
xorps %xmm0, %xmm0
cvtss2sd (%rax,%rdi,4), %xmm0
ucomisd %xmm0, %xmm2
jbe 0x8515
incq %r11
movq 0x10(%rbp), %r9
xorl %r10d, %r10d
cmpb $0x4e, (%r9,%rdi)
sete %r10b
addq %r10, %r8
jmp 0x8518
xorl %r8d, %r8d
cmpq %rsi, %r8
jle 0x8531
xorl %r9d, %r9d
cmpq %rdx, %r8
sete %r9b
addq %r9, 0x10(%rsp)
incq %r13
jmp 0x857d
movsd 0x58(%r15), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x58(%r15)
movsd 0x60(%r15), %xmm1
addsd %xmm3, %xmm1
movsd %xmm1, 0x60(%r15)
movzbl (%r14,%rdi), %r9d
cmpq $0x65, %r9
jb 0x8563
incq 0x18(%rsp)
jmp 0x857d
shll $0x3, %r9d
addsd 0x30(%rsp,%r9), %xmm0
movsd %xmm0, 0x30(%rsp,%r9)
incq 0x360(%rsp,%r9)
incq %rdi
cmpq %rdi, %rcx
jne 0x84ef
jmp 0x8590
movq 0x8(%rsp), %r11
movq %r11, 0x8(%rsp)
incq %r12
movslq (%r15), %rax
cmpq %rax, %r12
jl 0x8479
jmp 0x85c7
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 0x82b6
cmpl $0x0, (%r15)
jle 0x8962
xorl %ecx, %ecx
movq %rbx, 0x20(%rsp)
movq 0xa0(%r15), %rax
movq %rcx, 0x28(%rsp)
movq (%rax,%rcx,8), %rbp
testq %r14, %r14
je 0x862b
movq %r14, %rdi
callq 0x3070
movl 0x380(%rbx), %esi
movq %rbp, %rdi
callq 0xb61b
movq %rax, %r14
xorl %eax, %eax
callq 0xa8d9
testl %eax, %eax
je 0x87ed
movl $0x328, %edx            # imm = 0x328
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
callq 0x3260
movl $0x328, %edx            # imm = 0x328
leaq 0xcf0(%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 0x87b1
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 0x153ba(%rip), %xmm3    # 0x1dac0
ucomisd %xmm2, %xmm3
jbe 0x8724
incq %r11
movq 0x10(%rbp), %r9
xorl %r10d, %r10d
cmpb $0x4e, (%r9,%rdi)
sete %r10b
addq %r10, %r8
jmp 0x8727
xorl %r8d, %r8d
cmpq %rax, %r8
jle 0x873e
xorl %r9d, %r9d
cmpq %rdx, %r8
sete %r9b
addq %r9, %rbx
incq %r13
jmp 0x879a
movsd 0x58(%r15), %xmm3
addsd %xmm2, %xmm3
movsd %xmm3, 0x58(%r15)
movsd 0x60(%r15), %xmm3
movsd 0x152dc(%rip), %xmm4    # 0x1da38
addsd %xmm4, %xmm3
movsd %xmm3, 0x60(%r15)
addsd %xmm2, %xmm0
addsd %xmm4, %xmm1
movzbl (%r14,%rdi), %r9d
cmpq $0x65, %r9
jb 0x8780
incq 0x18(%rsp)
jmp 0x879a
shll $0x3, %r9d
addsd 0x30(%rsp,%r9), %xmm2
movsd %xmm2, 0x30(%rsp,%r9)
incq 0x360(%rsp,%r9)
incq %rdi
cmpq %rdi, %rcx
jne 0x86f6
movq %r11, 0x8(%rsp)
divsd %xmm1, %xmm0
jmp 0x87be
movsd 0x1530f(%rip), %xmm0    # 0x1dac8
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 0x82b6
cmpl $0x0, 0x8(%rbp)
jle 0x8949
xorl %ebx, %ebx
movzbl (%r14,%rbx), %r12d
cmpq $0x65, %r12
jb 0x8834
xorl %eax, %eax
callq 0xa881
cvtsd2ss %xmm0, %xmm0
movq 0x48(%rbp), %rax
movss %xmm0, (%rax,%rbx,4)
xorl %eax, %eax
callq 0xa881
addsd 0x10(%r15), %xmm0
movsd %xmm0, 0x10(%r15)
xorl %eax, %eax
callq 0xa881
jmp 0x889a
movq 0x30(%rbp), %rax
movss (%rax,%rbx,4), %xmm0
callq 0x3570
cvttss2si %xmm0, %edi
movsd 0x9c0(%rsp,%r12,8), %xmm0
movsd 0x15210(%rip), %xmm1    # 0x1da68
callq 0x816e
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jb 0x89c2
subsd 0x690(%rsp,%r12,8), %xmm0
callq 0xa88a
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)
addsd 0x48(%r15), %xmm0
movsd %xmm0, 0x48(%r15)
movsd 0x50(%r15), %xmm0
addsd 0x15184(%rip), %xmm0    # 0x1da38
movsd %xmm0, 0x50(%r15)
movq 0x30(%rbp), %rax
movss (%rax,%rbx,4), %xmm0
ucomiss 0x19426(%rip), %xmm0    # 0x21cf0
jne 0x88eb
jp 0x88eb
xorl %eax, %eax
callq 0xa881
cvtsd2ss %xmm0, %xmm0
movq 0x38(%rbp), %rax
movss %xmm0, (%rax,%rbx,4)
xorl %eax, %eax
callq 0xa881
jmp 0x892c
movq 0x38(%rbp), %rax
movss (%rax,%rbx,4), %xmm1
divss %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
callq 0xa88a
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 0xa88a
cvtsd2ss %xmm0, %xmm0
movq 0x40(%rbp), %rax
movss %xmm0, (%rax,%rbx,4)
incq %rbx
movslq 0x8(%rbp), %rax
cmpq %rax, %rbx
jl 0x87f9
movq 0x28(%rsp), %rcx
incq %rcx
movslq (%r15), %rax
cmpq %rax, %rcx
movq 0x20(%rsp), %rbx
jl 0x860e
testq %r14, %r14
je 0x896f
movq %r14, %rdi
callq 0x3070
leaq 0x19bb0(%rip), %rdi     # 0x22526
movq 0x18(%rsp), %rsi
xorl %eax, %eax
callq 0x3210
leaq 0x19bdc(%rip), %rdi     # 0x22565
movq %r13, %rsi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x3210
leaq 0x19bfa(%rip), %rdi     # 0x22599
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 0x19b11(%rip), %rdi     # 0x224da
leaq 0x19335(%rip), %rsi     # 0x21d05
leaq 0x19b16(%rip), %rcx     # 0x224ed
movl $0x68e, %edx            # imm = 0x68E
callq 0x3230