// File: /workspace/github_cpp_makefile_star2_O2/schneebergerlab[P]GameteBinning/asPollinator_1.0.cpp
// Function: get_pollen_allele(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::map<unsigned long, ALLELE, std::less<unsigned long>, std::allocator<std::pair<unsigned long const, ALLELE>>>, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, std::map<unsigned long, ALLELE, std::less<unsigned long>, std::allocator<std::pair<unsigned long const, ALLELE>>>>>>*)
bool get_pollen_allele(string pollenfile, 
                       map<string, map<unsigned long, ALLELE> >* mkr)
{
    //
    ifstream ifp;
    ifp.open(pollenfile.c_str(), ios::in);
    if(!ifp.good())
    {
        cout << "   Error: cannot open pollen allele count file " << pollenfile << endl;
        return false;
    }
    while(ifp.good())
    {
        string line("");
        getline(ifp, line);
        if(line.size() == 0 || line[0]=='#') continue;
        //contig	      pos     Consensus   totalcount  A	  C   G   T   -   N   ...........
        //tig00000013_pilon   14777   T           2           0   0   0   1   0   1   1.0     1.1
        vector<string> lineinfo = split_string(line, '\t');
        if(lineinfo.size() < 10) continue; // unexpected format
        string keyctg = lineinfo[0];
        // <ctg, <pos, {ref, alt}> >
        map<string, map<unsigned long, ALLELE> >::iterator ctgitr;
        ctgitr = (*mkr).find(keyctg);
        if(ctgitr == (*mkr).end() ) 
        {
            // pollen contig not in marker list
            continue;
        }
        else
        {
            //map<unsigned long, ALLELE> indMarker = (*ctgitr).second; 
            // directly update the pointers
            string                           keypos_str = lineinfo[1];
            unsigned long                        keypos = strtoul(keypos_str.c_str(), NULL, 0);
            map<unsigned long, ALLELE>::iterator positr = ( (*ctgitr).second ).find(keypos);
            if(positr == ( (*ctgitr).second ).end())
            {
                // pollen position not in marker list
                continue;
            }
            else
            {
                // find pollen genotype data for a marker
                string refb = (*positr).second.refb; // 
                string altb = (*positr).second.altb; // 
                int refpos = 4;
                if(!get_base_position(refb, &refpos)) return false;
                int altpos = 4;
                if(!get_base_position(altb, &altpos)) return false;
                unsigned long refcnt = strtoul(lineinfo[refpos].c_str(), NULL, 0);
                unsigned long altcnt = strtoul(lineinfo[altpos].c_str(), NULL, 0);
                // refb; altb; none of refb and altb found 'N'; both refb and altb found 'U'
                if(refcnt>0 && altcnt>0)
                {
                    (*positr).second.pollenb = "U";
                }
                else
                if(refcnt>0)
                {
                    (*positr).second.pollenb = refb;
                }
                else
                if(altcnt>0)
                {
                    (*positr).second.pollenb = altb;
                }
                else
                {
                    (*positr).second.pollenb = "N";
                }
            }
        }
    }
    ifp.close();
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x328, %rsp            # imm = 0x328
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
leaq 0x120(%rsp), %r14
movq %r14, %rdi
callq 0x32b0
movq (%rbx), %rsi
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x3180
movq 0x120(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x140(%rsp,%rax)
je 0x8b57
movq 0x134a7(%rip), %rdi     # 0x1bfd8
leaq 0xb59c(%rip), %rsi      # 0x140d4
callq 0x3260
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3240
movq %rax, %rdi
callq 0x3140
xorl %ebx, %ebx
jmp 0x8e11
movq 0x10(%rsp), %rcx
addq $0x8, %rcx
movq %rcx, 0x30(%rsp)
leaq 0xb35b(%rip), %rbx      # 0x13ec7
leaq 0xa0(%rsp), %r15
leaq 0xc0(%rsp), %r12
leaq 0x120(%rsp), %r13
leaq 0x18(%rsp), %rbp
cmpl $0x0, 0x140(%rsp,%rax)
jne 0x8e02
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0xe174
movq %r13, %rdi
movq %r15, %rsi
callq 0x3070
cmpq $0x0, 0xa8(%rsp)
je 0x8bfb
movq 0xa0(%rsp), %rax
cmpb $0x23, (%rax)
je 0x8bfb
movb $0x9, 0xc0(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x11fdc
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rax
subq %rsi, %rax
cmpq $0x13f, %rax            # imm = 0x13F
ja 0x8c14
movq %rbp, %rdi
callq 0xe2d8
movq %r15, %rdi
callq 0x3120
movq 0x120(%rsp), %rax
movq -0x18(%rax), %rax
jmp 0x8b89
movq %r12, %rdi
callq 0x3300
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x103e2
movq %rax, %rbp
pushq $0x2
popq %r14
cmpq 0x30(%rsp), %rax
je 0x8dd6
movq %rbx, %r13
movq 0x18(%rsp), %rsi
addq $0x20, %rsi
leaq 0x80(%rsp), %rdi
callq 0x3300
movq 0x80(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x30d0
movq %rax, 0x38(%rsp)
leaq 0x40(%rbp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x118f6
movq %rax, %rbx
addq $0x48, %rbp
pushq $0x2
popq %r14
cmpq %rbp, %rax
je 0x8dbe
leaq 0x28(%rbx), %rsi
leaq 0x60(%rsp), %rdi
callq 0x3300
leaq 0x48(%rbx), %rsi
leaq 0x40(%rsp), %rdi
callq 0x3300
leaq 0x100(%rsp), %r14
movl $0x4, 0xc(%rsp)
movq %r14, %rdi
leaq 0x60(%rsp), %rsi
callq 0x3300
movq %r14, %rdi
leaq 0xc(%rsp), %rsi
callq 0xe078
movl %eax, %ebp
movq %r14, %rdi
callq 0x3120
pushq $0x1
popq %r14
testb %bpl, %bpl
je 0x8daa
movl $0x4, 0x8(%rsp)
leaq 0xe0(%rsp), %r14
movq %r14, %rdi
leaq 0x40(%rsp), %rsi
callq 0x3300
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq 0xe078
movl %eax, %ebp
movq %r14, %rdi
callq 0x3120
pushq $0x1
popq %r14
testb %bpl, %bpl
je 0x8daa
movslq 0xc(%rsp), %rax
movq 0x18(%rsp), %rcx
shlq $0x5, %rax
movq (%rcx,%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x30d0
movq %rax, %rbp
movslq 0x8(%rsp), %rax
movq 0x18(%rsp), %rcx
shlq $0x5, %rax
movq (%rcx,%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x30d0
addq $0x68, %rbx
testq %rbp, %rbp
je 0x8d78
leaq 0xad43(%rip), %rsi      # 0x13ab6
testq %rax, %rax
jne 0x8d9f
orq %rbp, %rax
leaq 0xa88e(%rip), %rsi      # 0x13610
je 0x8d9f
testq %rbp, %rbp
leaq 0x40(%rsp), %rsi
leaq 0x60(%rsp), %rax
cmovneq %rax, %rsi
movq %rbx, %rdi
callq 0x3170
jmp 0x8da7
movq %rbx, %rdi
callq 0x3440
xorl %r14d, %r14d
leaq 0x40(%rsp), %rdi
callq 0x3120
leaq 0x60(%rsp), %rdi
callq 0x3120
leaq 0x80(%rsp), %rdi
callq 0x3120
movq %r13, %rbx
leaq 0x120(%rsp), %r13
movq %r12, %rdi
callq 0x3120
leaq 0x18(%rsp), %rbp
movq %rbp, %rdi
callq 0xe2d8
movq %r15, %rdi
callq 0x3120
testb $0x1, %r14b
je 0x8c03
jmp 0x8b50
movb $0x1, %bl
leaq 0x120(%rsp), %rdi
callq 0x3080
leaq 0x120(%rsp), %rdi
callq 0x30a0
movl %ebx, %eax
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8e5c
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
jmp 0x8e55
jmp 0x8e5c
movq %rax, %rbx
jmp 0x8e69
jmp 0x8e75
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x3120
jmp 0x8e5f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x3120
leaq 0x60(%rsp), %rdi
callq 0x3120
jmp 0x8e78
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x3120
jmp 0x8e91
jmp 0x8e8e
movq %rax, %rbx
jmp 0x8e9e
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x3120
leaq 0x18(%rsp), %rdi
callq 0xe2d8
jmp 0x8eb6
jmp 0x8eae
jmp 0x8eb3
movq %rax, %rbx
jmp 0x8ec3
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x3120
leaq 0x120(%rsp), %rdi
callq 0x30a0
movq %rbx, %rdi
callq 0x3410