// File: /workspace/github_cpp_makefile_star2_O2/bobverity[P]MavericK/MCMCobject_admixture.cpp
// Function: MCMCobject_admixture::group_update_indLevel()
void MCMCobject_admixture::group_update_indLevel() {
    
    double logLike_old;
    double logLike_new;
    double propose_logProb_old;
    double propose_logProb_new;
    double MH_diff;
    double rand1;
    int thisGroup;
    
    // loop over all individuals
    groupIndex=-1;
    for (int ind=0; ind<n; ind++) {
        
        // subtract all gene copies in this individual and calculate likelihood of current grouping at the same time
        logLike_old = 0;
        propose_logProb_old = 0;
        for (int l=0; l<loci; l++) {
            for (int p=0; p<ploidy_vec[ind]; p++) {
                thisData = data[ind][l][p];
                thisGroup = group[ind][l][p];
                
                // subtract this gene copy from allele counts and admix counts
                if (thisData!=0) {   // if not missing data
                    alleleCounts[thisGroup-1][l][thisData-1]--;
                    alleleCountsTotals[thisGroup-1][l]--;
                    
                    admixCounts[ind][thisGroup-1]--;
                    admixCountsTotals[ind]--;
                }
                
                // calculate probability of this gene copy from all demes
                probVecSum = 0;
                for (int k=0; k<K; k++) {
                    if (thisData==0) {
                        logProbVec[k] = 0;
                    } else {
                        thisAlleleCounts = alleleCounts[k][l][thisData-1];
                        thisAlleleCountsTotals = alleleCountsTotals[k][l];
                        if ((thisAlleleCounts<int(1e4)) && (thisAlleleCountsTotals<int(1e4))) {
                            logProbVec[k] = beta*log_lookup_0[thisAlleleCounts] - beta*log_lookup[thisAlleleCountsTotals][J[l]-1];
                        } else {
                            logProbVec[k] = beta*log((thisAlleleCounts + lambda)/double(thisAlleleCountsTotals + J[l]*lambda));
                        }
                    }
                    probVec[k] = double(admixCounts[ind][k]+alpha)*exp(logProbVec[k]);
                    probVecSum += probVec[k];
                }
                
                // calculate probability of chosen grouping
                propose_logProb_old += log(probVec[thisGroup-1]/probVecSum);
                logLike_old += log(probVec[thisGroup-1]);
                
            }
        }
        
        // propose a new grouping and calculate likelihood
        logLike_new = 0;
        propose_logProb_new = 0;
        for (int l=0; l<loci; l++) {
            for (int p=0; p<ploidy_vec[ind]; p++) {
                thisData = data[ind][l][p];
                
                // calculate probability of this gene copy from all demes
                probVecSum = 0;
                for (int k=0; k<K; k++) {
                    if (thisData==0) {
                        logProbVec[k] = 0;
                    } else {
                        thisAlleleCounts = alleleCounts[k][l][thisData-1];
                        thisAlleleCountsTotals = alleleCountsTotals[k][l];
                        if ((thisAlleleCounts<int(1e4)) && (thisAlleleCountsTotals<int(1e4))) {
                            logProbVec[k] = beta*log_lookup_0[thisAlleleCounts] - beta*log_lookup[thisAlleleCountsTotals][J[l]-1];
                        } else {
                            logProbVec[k] = beta*log((thisAlleleCounts + lambda)/double(thisAlleleCountsTotals + J[l]*lambda));
                        }
                    }
                    probVec[k] = double(admixCounts[ind][k]+alpha)*exp(logProbVec[k]);
                    probVecSum += probVec[k];
                }
                
                // resample grouping
                group_propose[ind][l][p] = sample1(probVec, probVecSum);
                
                // calculate probability of new grouping
                propose_logProb_new += log(probVec[group_propose[ind][l][p]-1]/probVecSum);
                logLike_new += log(probVec[group_propose[ind][l][p]-1]);
                
                // add this gene copy to allele counts and admix counts
                if (thisData!=0) {   // if not missing data
                    thisGroup = group_propose[ind][l][p];
                    alleleCounts[thisGroup-1][l][thisData-1]++;
                    alleleCountsTotals[thisGroup-1][l]++;
                    
                    admixCounts[ind][thisGroup-1]++;
                    admixCountsTotals[ind]++;
                }
            }
        }
        
        // Metropolis-Hastings step. If accept then stick with new grouping, otherwise revert back
        MH_diff = (logLike_new - propose_logProb_new) - (logLike_old - propose_logProb_old);
        rand1 = runif1(0,1);
        if (log(rand1)<MH_diff) {
            
            // accept move
            for (int l=0; l<loci; l++) {
                for (int p=0; p<ploidy_vec[ind]; p++) {
                    groupIndex++;
                    linearGroup[groupIndex] = group_propose[ind][l][p];
                    group[ind][l][p] = group_propose[ind][l][p];
                }
            }
            
        } else {
            
            // reject move
            for (int l=0; l<loci; l++) {
                for (int p=0; p<ploidy_vec[ind]; p++) {
                    groupIndex++;
                    thisData = data[ind][l][p];
                    if (thisData!=0) {   // if not missing data
                        
                        // subtract new group
                        thisGroup = group_propose[ind][l][p];
                        alleleCounts[thisGroup-1][l][thisData-1]--;
                        alleleCountsTotals[thisGroup-1][l]--;
                        
                        admixCounts[ind][thisGroup-1]--;
                        admixCountsTotals[ind]--;
                        
                        // reinstate old group
                        thisGroup = group[ind][l][p];
                        alleleCounts[thisGroup-1][l][thisData-1]++;
                        alleleCountsTotals[thisGroup-1][l]++;
                        
                        admixCounts[ind][thisGroup-1]++;
                        admixCountsTotals[ind]++;
                    }
                    
                }
            }
            
        }
        
    }   // end loop over individuals
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
orl $-0x1, 0x108(%rdi)
leaq 0x260(%rdi), %rax
movq %rax, 0x38(%rsp)
xorl %r13d, %r13d
movslq 0x20(%rbx), %rax
cmpq %rax, %r13
jge 0x8925
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
imulq $0x18, %r13, %rbp
xorl %r14d, %r14d
movsd %xmm0, 0x30(%rsp)
movq %r13, 0x28(%rsp)
movslq 0x24(%rbx), %rax
cmpq %rax, %r14
jge 0x84a1
imulq $0x18, %r14, %r15
xorl %edx, %edx
movq 0x40(%rbx), %rax
movslq (%rax,%r13,4), %rax
cmpq %rax, %rdx
jge 0x8499
movq (%rbx), %rax
movq 0xd8(%rbx), %rcx
movq (%rax,%rbp), %rax
movq (%rax,%r15), %rax
movslq (%rax,%rdx,4), %rax
movl %eax, 0xb0(%rbx)
movq (%rcx,%rbp), %rcx
movq (%rcx,%r15), %rcx
movq %rdx, 0x18(%rsp)
movslq (%rcx,%rdx,4), %rsi
testq %rax, %rax
je 0x82d8
leaq -0x1(%rsi), %rcx
movq 0x110(%rbx), %rdx
imulq $0x18, %rcx, %rcx
movq (%rdx,%rcx), %rdx
movq (%rdx,%r15), %rdx
decl -0x4(%rdx,%rax,4)
movq 0x128(%rbx), %rax
movq (%rax,%rcx), %rax
decl (%rax,%r14,4)
movq 0x188(%rbx), %rax
movq (%rax,%rbp), %rax
decl -0x4(%rax,%rsi,4)
movq 0x1a0(%rbx), %rax
decl (%rax,%r13,4)
movq %rsi, 0x10(%rsp)
andq $0x0, 0x278(%rbx)
xorpd %xmm0, %xmm0
xorl %r12d, %r12d
xorl %r13d, %r13d
movslq 0x1c(%rbx), %rax
cmpq %rax, %r13
jge 0x8435
movslq 0xb0(%rbx), %rax
testq %rax, %rax
je 0x838f
movq 0x110(%rbx), %rcx
movq (%rcx,%r12), %rcx
movq (%rcx,%r15), %rcx
movslq -0x4(%rcx,%rax,4), %rax
movl %eax, 0xb4(%rbx)
movq 0x128(%rbx), %rcx
movq (%rcx,%r12), %rcx
movl (%rcx,%r14,4), %ecx
movl %ecx, 0xb8(%rbx)
movsd 0x98(%rbx), %xmm1
cmpq $0x270f, %rax           # imm = 0x270F
jg 0x8395
cmpl $0x270f, %ecx           # imm = 0x270F
jg 0x8395
movslq %ecx, %rcx
leaq 0x2bffb(%rip), %rdx     # 0x34358
movq (%rdx), %rdx
leaq 0x2bfd9(%rip), %rsi     # 0x34340
movq (%rsi), %rsi
imulq $0x18, %rcx, %rcx
movq 0x28(%rbx), %rdi
movslq (%rdi,%r14,4), %rdi
movq (%rsi,%rcx), %rcx
movsd -0x8(%rcx,%rdi,8), %xmm0
mulsd %xmm1, %xmm0
mulsd (%rdx,%rax,8), %xmm1
subsd %xmm0, %xmm1
jmp 0x83d1
xorpd %xmm1, %xmm1
jmp 0x83d1
cvtsi2sd %eax, %xmm0
movsd %xmm1, 0x8(%rsp)
cvtsi2sd %ecx, %xmm1
movsd 0x78(%rbx), %xmm2
movq 0x28(%rbx), %rax
cvtsi2sdl (%rax,%r14,4), %xmm3
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
divsd %xmm3, %xmm0
callq 0x32d0
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movq 0x238(%rbx), %rax
movsd %xmm1, (%rax,%r13,8)
movq 0x188(%rbx), %rax
movq (%rax,%rbp), %rax
cvtsi2sdl (%rax,%r13,4), %xmm0
addsd 0x88(%rbx), %xmm0
movsd %xmm0, 0x8(%rsp)
movapd %xmm1, %xmm0
callq 0x35c0
mulsd 0x8(%rsp), %xmm0
movq 0x260(%rbx), %rax
movsd %xmm0, (%rax,%r13,8)
addsd 0x278(%rbx), %xmm0
movsd %xmm0, 0x278(%rbx)
incq %r13
addq $0x18, %r12
jmp 0x82ef
movq 0x38(%rsp), %r12
movq (%r12), %rax
movq 0x10(%rsp), %r13
movsd -0x8(%rax,%r13,8), %xmm1
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x32d0
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
movq (%r12), %rax
movsd -0x8(%rax,%r13,8), %xmm0
callq 0x32d0
movsd 0x30(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
movq 0x18(%rsp), %rdx
incq %rdx
movq 0x28(%rsp), %r13
jmp 0x8251
incq %r14
jmp 0x823e
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r14d, %r14d
cltq
cmpq %rax, %r14
jge 0x874d
xorl %r15d, %r15d
movq 0x40(%rbx), %rax
movslq (%rax,%r13,4), %rax
cmpq %rax, %r15
jge 0x8742
movsd %xmm0, 0x18(%rsp)
movq (%rbx), %rax
movq (%rax,%rbp), %rax
imulq $0x18, %r14, %r12
movq (%rax,%r12), %rax
movq %r15, 0x40(%rsp)
movl (%rax,%r15,4), %eax
movl %eax, 0xb0(%rbx)
andq $0x0, 0x278(%rbx)
xorpd %xmm0, %xmm0
xorl %r13d, %r13d
xorl %r15d, %r15d
movslq 0x1c(%rbx), %rax
cmpq %rax, %r15
jge 0x8649
movslq 0xb0(%rbx), %rax
testq %rax, %rax
je 0x85a3
movq 0x110(%rbx), %rcx
movq (%rcx,%r13), %rcx
movq (%rcx,%r12), %rcx
movslq -0x4(%rcx,%rax,4), %rax
movl %eax, 0xb4(%rbx)
movq 0x128(%rbx), %rcx
movq (%rcx,%r13), %rcx
movl (%rcx,%r14,4), %ecx
movl %ecx, 0xb8(%rbx)
movsd 0x98(%rbx), %xmm1
cmpq $0x270f, %rax           # imm = 0x270F
jg 0x85a9
cmpl $0x270f, %ecx           # imm = 0x270F
jg 0x85a9
movslq %ecx, %rcx
leaq 0x2bde7(%rip), %rdx     # 0x34358
movq (%rdx), %rdx
leaq 0x2bdc5(%rip), %rsi     # 0x34340
movq (%rsi), %rsi
imulq $0x18, %rcx, %rcx
movq 0x28(%rbx), %rdi
movslq (%rdi,%r14,4), %rdi
movq (%rsi,%rcx), %rcx
movsd -0x8(%rcx,%rdi,8), %xmm0
mulsd %xmm1, %xmm0
mulsd (%rdx,%rax,8), %xmm1
subsd %xmm0, %xmm1
jmp 0x85e5
xorpd %xmm1, %xmm1
jmp 0x85e5
cvtsi2sd %eax, %xmm0
movsd %xmm1, 0x8(%rsp)
cvtsi2sd %ecx, %xmm1
movsd 0x78(%rbx), %xmm2
movq 0x28(%rbx), %rax
cvtsi2sdl (%rax,%r14,4), %xmm3
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm3
addsd %xmm1, %xmm3
divsd %xmm3, %xmm0
callq 0x32d0
movsd 0x8(%rsp), %xmm1
mulsd %xmm0, %xmm1
movq 0x238(%rbx), %rax
movsd %xmm1, (%rax,%r15,8)
movq 0x188(%rbx), %rax
movq (%rax,%rbp), %rax
cvtsi2sdl (%rax,%r15,4), %xmm0
addsd 0x88(%rbx), %xmm0
movsd %xmm0, 0x8(%rsp)
movapd %xmm1, %xmm0
callq 0x35c0
mulsd 0x8(%rsp), %xmm0
movq 0x260(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
addsd 0x278(%rbx), %xmm0
movsd %xmm0, 0x278(%rbx)
incq %r15
addq $0x18, %r13
jmp 0x8503
movq 0x38(%rsp), %rdi
callq 0x17127
movq 0xf0(%rbx), %rcx
movq (%rcx,%rbp), %rcx
movq (%rcx,%r12), %rcx
movq 0x40(%rsp), %r15
movl %eax, (%rcx,%r15,4)
movq 0x260(%rbx), %rcx
cltq
movsd -0x8(%rcx,%rax,8), %xmm0
divsd 0x278(%rbx), %xmm0
callq 0x32d0
movsd %xmm0, 0x8(%rsp)
movq 0xf0(%rbx), %rax
movq 0x260(%rbx), %rcx
movq (%rax,%rbp), %rax
movq (%rax,%r12), %rax
movslq (%rax,%r15,4), %rax
movsd -0x8(%rcx,%rax,8), %xmm0
callq 0x32d0
movslq 0xb0(%rbx), %rax
testq %rax, %rax
movq 0x28(%rsp), %r13
je 0x871a
movq 0xf0(%rbx), %rcx
movq 0x110(%rbx), %rdx
movq (%rcx,%rbp), %rcx
movq (%rcx,%r12), %rcx
movslq (%rcx,%r15,4), %rcx
leaq -0x1(%rcx), %rsi
imulq $0x18, %rsi, %rsi
movq (%rdx,%rsi), %rdx
movq (%rdx,%r12), %rdx
incl -0x4(%rdx,%rax,4)
movq 0x128(%rbx), %rax
movq (%rax,%rsi), %rax
incl (%rax,%r14,4)
movq 0x188(%rbx), %rax
movq (%rax,%rbp), %rax
incl -0x4(%rax,%rcx,4)
movq 0x1a0(%rbx), %rax
incl (%rax,%r13,4)
movsd 0x10(%rsp), %xmm1
addsd 0x8(%rsp), %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
incq %r15
jmp 0x84bc
incq %r14
movl 0x24(%rbx), %eax
jmp 0x84ae
subsd 0x10(%rsp), %xmm0
movapd %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
subsd 0x20(%rsp), %xmm0
subsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
xorpd %xmm0, %xmm0
movsd 0x1ea97(%rip), %xmm1    # 0x27210
callq 0x1707c
callq 0x32d0
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movq 0x40(%rbx), %rax
jbe 0x8804
xorl %ecx, %ecx
movslq 0x24(%rbx), %rdx
cmpq %rdx, %rcx
jge 0x891d
movq 0xf0(%rbx), %rdx
addq %rbp, %rdx
movq 0xc0(%rbx), %rsi
movq 0xd8(%rbx), %rdi
addq %rbp, %rdi
xorl %r8d, %r8d
movslq (%rax,%r13,4), %r9
cmpq %r9, %r8
jge 0x87ff
movslq 0x108(%rbx), %r9
leaq 0x1(%r9), %r10
movl %r10d, 0x108(%rbx)
movq (%rdx), %r10
imulq $0x18, %rcx, %r11
movq (%r10,%r11), %r10
movl (%r10,%r8,4), %r10d
movl %r10d, 0x4(%rsi,%r9,4)
movq (%rdi), %r9
movq (%r9,%r11), %r9
movl %r10d, (%r9,%r8,4)
incq %r8
jmp 0x87c0
incq %rcx
jmp 0x8795
xorl %ecx, %ecx
movslq 0x24(%rbx), %rdx
cmpq %rdx, %rcx
jge 0x891d
movq (%rbx), %rdx
addq %rbp, %rdx
movq 0xf0(%rbx), %rsi
addq %rbp, %rsi
movq %rsi, 0x8(%rsp)
movq 0x110(%rbx), %rdi
movq 0x128(%rbx), %r8
movq 0x188(%rbx), %rsi
addq %rbp, %rsi
movq %rsi, 0x20(%rsp)
movq 0x1a0(%rbx), %r10
movq 0xd8(%rbx), %r11
addq %rbp, %r11
xorl %r14d, %r14d
movslq (%rax,%r13,4), %r15
cmpq %r15, %r14
jge 0x8915
incl 0x108(%rbx)
movq (%rdx), %r12
imulq $0x18, %rcx, %r15
movq (%r12,%r15), %r12
movslq (%r12,%r14,4), %r12
movl %r12d, 0xb0(%rbx)
testq %r12, %r12
je 0x890d
movq 0x8(%rsp), %rsi
movq (%rsi), %r13
movq (%r13,%r15), %r13
movslq (%r13,%r14,4), %r13
leaq -0x1(%r13), %rsi
imulq $0x18, %rsi, %rsi
movq (%rdi,%rsi), %r9
movq (%r9,%r15), %r9
decl -0x4(%r9,%r12,4)
movq (%r8,%rsi), %rsi
decl (%rsi,%rcx,4)
movq 0x20(%rsp), %rsi
movq (%rsi), %rsi
decl -0x4(%rsi,%r13,4)
movq 0x28(%rsp), %r9
decl (%r10,%r9,4)
movq (%r11), %r9
movq (%r9,%r15), %r9
movslq (%r9,%r14,4), %r9
leaq -0x1(%r9), %r12
imulq $0x18, %r12, %r12
movq (%rdi,%r12), %r13
movq (%r13,%r15), %r15
movslq 0xb0(%rbx), %r13
incl -0x4(%r15,%r13,4)
movq 0x28(%rsp), %r13
movq (%r8,%r12), %r15
incl (%r15,%rcx,4)
incl -0x4(%rsi,%r9,4)
incl (%r10,%r13,4)
incq %r14
jmp 0x8859
incq %rcx
jmp 0x8806
incq %r13
jmp 0x8215
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq