// File: 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:
subq $0x3e8, %rsp            # imm = 0x3E8
movq %rdi, 0x50(%rsp)
movq %rdi, 0x3d8(%rsp)
movq %rsi, 0x3d0(%rsp)
leaq 0x1c8(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x33c0
movq 0x50(%rsp), %rdi
callq 0x3150
movq 0x58(%rsp), %rdi
movq %rax, %rsi
movl $0x8, %edx
callq 0x3200
jmp 0xda5c
movq 0x1c8(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x1c8(%rsp,%rax), %rdi
callq 0x3520
movb %al, 0x4f(%rsp)
jmp 0xda7b
movb 0x4f(%rsp), %al
testb $0x1, %al
jne 0xdaf9
jmp 0xda85
movq 0x2b54c(%rip), %rdi     # 0x38fd8
leaq 0x1e5db(%rip), %rsi     # 0x2c06e
callq 0x3320
movq %rax, 0x40(%rsp)
jmp 0xda9f
movq 0x50(%rsp), %rsi
movq 0x40(%rsp), %rdi
callq 0x32e0
movq %rax, 0x38(%rsp)
jmp 0xdab5
movq 0x38(%rsp), %rdi
movq 0x2b507(%rip), %rsi     # 0x38fc8
callq 0x3370
jmp 0xdac8
movb $0x0, 0x3e7(%rsp)
movl $0x1, 0x1b8(%rsp)
jmp 0xe1e8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0xe206
jmp 0xdafb
movq 0x1c8(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x1c8(%rsp,%rax), %rdi
callq 0x3520
movb %al, 0x37(%rsp)
jmp 0xdb1a
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0xdb27
jmp 0xe1c6
leaq 0x197(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x3570
movq 0x28(%rsp), %rdx
leaq 0x1e31c(%rip), %rsi     # 0x2be61
leaq 0x198(%rsp), %rdi
callq 0x185e0
jmp 0xdb54
leaq 0x197(%rsp), %rdi
callq 0x3390
leaq 0x1c8(%rsp), %rdi
leaq 0x198(%rsp), %rsi
callq 0x30a0
jmp 0xdb78
leaq 0x198(%rsp), %rdi
callq 0x3250
cmpq $0x0, %rax
je 0xdbb0
xorl %eax, %eax
movl %eax, %esi
leaq 0x198(%rsp), %rdi
callq 0x35f0
movq %rax, 0x20(%rsp)
jmp 0xdba3
movq 0x20(%rsp), %rax
movsbl (%rax), %eax
cmpl $0x23, %eax
jne 0xdbff
movl $0x2, 0x1b8(%rsp)
jmp 0xe16b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
leaq 0x197(%rsp), %rdi
callq 0x3390
jmp 0xe206
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0xe1b7
movb $0x9, 0x177(%rsp)
leaq 0x178(%rsp), %rdi
leaq 0x198(%rsp), %rsi
leaq 0x177(%rsp), %rdx
callq 0x2aa50
jmp 0xdc26
leaq 0x178(%rsp), %rdi
callq 0x1a930
cmpq $0xa, %rax
jae 0xdc49
movl $0x2, 0x1b8(%rsp)
jmp 0xe15e
xorl %eax, %eax
movl %eax, %esi
leaq 0x178(%rsp), %rdi
callq 0x19580
movq %rax, %rsi
leaq 0x150(%rsp), %rdi
callq 0x3160
jmp 0xdc6c
leaq 0x148(%rsp), %rdi
callq 0x18890
movq 0x3d0(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x18f60
movq %rax, 0x18(%rsp)
jmp 0xdc95
movq 0x18(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x140(%rsp), %rax
movq %rax, 0x148(%rsp)
movq 0x3d0(%rsp), %rdi
callq 0x188e0
movq %rax, 0x138(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x18f90
testb $0x1, %al
jne 0xdce2
jmp 0xdd24
movl $0x2, 0x1b8(%rsp)
jmp 0xe151
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0xe1aa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0xe19d
leaq 0x178(%rsp), %rdi
movl $0x1, %esi
callq 0x19580
movq %rax, %rsi
leaq 0x118(%rsp), %rdi
callq 0x3160
jmp 0xdd48
leaq 0x118(%rsp), %rdi
callq 0x3150
movq %rax, %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x3110
movq %rax, 0x110(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x18930
movq %rax, %rdi
addq $0x20, %rdi
leaq 0x110(%rsp), %rsi
callq 0x1a950
movq %rax, 0x10(%rsp)
jmp 0xdd91
movq 0x10(%rsp), %rax
movq %rax, 0x108(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x18930
movq %rax, %rdi
addq $0x20, %rdi
callq 0x189f0
movq %rax, 0x100(%rsp)
leaq 0x108(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x1a980
testb $0x1, %al
jne 0xddda
jmp 0xde03
movl $0x2, 0x1b8(%rsp)
jmp 0xe11b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0xe137
leaq 0x108(%rsp), %rdi
callq 0x18a40
movq %rax, %rsi
addq $0x8, %rsi
leaq 0xe0(%rsp), %rdi
callq 0x3160
jmp 0xde26
leaq 0x108(%rsp), %rdi
callq 0x18a40
movq %rax, %rsi
addq $0x28, %rsi
leaq 0xc0(%rsp), %rdi
callq 0x3160
jmp 0xde49
movl $0x4, 0xbc(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3160
jmp 0xde6b
leaq 0x98(%rsp), %rdi
leaq 0xbc(%rsp), %rsi
callq 0x18490
movb %al, 0xf(%rsp)
jmp 0xde86
movb 0xf(%rsp), %al
xorb $-0x1, %al
movb %al, 0xe(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x31e0
movb 0xe(%rsp), %al
testb $0x1, %al
jne 0xdea7
jmp 0xdf17
movb $0x0, 0x3e7(%rsp)
movl $0x1, 0x1b8(%rsp)
jmp 0xe0cb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0xe101
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
jmp 0xe0f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x31e0
jmp 0xe0f4
movl $0x4, 0x94(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3160
jmp 0xdf36
leaq 0x70(%rsp), %rdi
leaq 0x94(%rsp), %rsi
callq 0x18490
movb %al, 0xd(%rsp)
jmp 0xdf4e
movb 0xd(%rsp), %al
xorb $-0x1, %al
movb %al, 0xc(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x31e0
movb 0xc(%rsp), %al
testb $0x1, %al
jne 0xdf6c
jmp 0xdfa7
movb $0x0, 0x3e7(%rsp)
movl $0x1, 0x1b8(%rsp)
jmp 0xe0cb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c0(%rsp)
movl %eax, 0x1bc(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x31e0
jmp 0xe0f4
movslq 0xbc(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0x19580
movq %rax, %rdi
callq 0x3150
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x3110
movq %rax, 0x68(%rsp)
movslq 0x94(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0x19580
movq %rax, %rdi
callq 0x3150
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x3110
movq %rax, 0x60(%rsp)
cmpq $0x0, 0x68(%rsp)
jbe 0xe03e
cmpq $0x0, 0x60(%rsp)
jbe 0xe03e
leaq 0x108(%rsp), %rdi
callq 0x18a40
movq %rax, %rdi
addq $0x48, %rdi
leaq 0x1da1c(%rip), %rsi     # 0x2ba4e
callq 0x3440
jmp 0xe039
jmp 0xe0c0
cmpq $0x0, 0x68(%rsp)
jbe 0xe06b
leaq 0x108(%rsp), %rdi
callq 0x18a40
movq %rax, %rdi
addq $0x48, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3050
jmp 0xe069
jmp 0xe0be
cmpq $0x0, 0x60(%rsp)
jbe 0xe098
leaq 0x108(%rsp), %rdi
callq 0x18a40
movq %rax, %rdi
addq $0x48, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3050
jmp 0xe096
jmp 0xe0bc
leaq 0x108(%rsp), %rdi
callq 0x18a40
movq %rax, %rdi
addq $0x48, %rdi
leaq 0x1d4f5(%rip), %rsi     # 0x2b5a8
callq 0x3440
jmp 0xe0ba
jmp 0xe0bc
jmp 0xe0be
jmp 0xe0c0
movl $0x0, 0x1b8(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x31e0
leaq 0xe0(%rsp), %rdi
callq 0x31e0
movl 0x1b8(%rsp), %eax
testl %eax, %eax
jne 0xe11b
jmp 0xe0f2
jmp 0xe110
leaq 0xc0(%rsp), %rdi
callq 0x31e0
leaq 0xe0(%rsp), %rdi
callq 0x31e0
jmp 0xe137
movl $0x0, 0x1b8(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x31e0
movl 0x1b8(%rsp), %eax
testl %eax, %eax
jne 0xe151
jmp 0xe135
jmp 0xe146
leaq 0x118(%rsp), %rdi
callq 0x31e0
jmp 0xe19d
movl $0x0, 0x1b8(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x31e0
leaq 0x178(%rsp), %rdi
callq 0x18e80
leaq 0x198(%rsp), %rdi
callq 0x31e0
movl 0x1b8(%rsp), %eax
movl %eax, 0x8(%rsp)
testl %eax, %eax
je 0xe198
jmp 0xe189
movl 0x8(%rsp), %eax
subl $0x2, %eax
je 0xdafb
jmp 0xe1e8
jmp 0xdafb
leaq 0x150(%rsp), %rdi
callq 0x31e0
leaq 0x178(%rsp), %rdi
callq 0x18e80
leaq 0x198(%rsp), %rdi
callq 0x31e0
jmp 0xe206
leaq 0x1c8(%rsp), %rdi
callq 0x30b0
jmp 0xe1d5
movb $0x1, 0x3e7(%rsp)
movl $0x1, 0x1b8(%rsp)
leaq 0x1c8(%rsp), %rdi
callq 0x30e0
movb 0x3e7(%rsp), %al
andb $0x1, %al
addq $0x3e8, %rsp            # imm = 0x3E8
retq
leaq 0x1c8(%rsp), %rdi
callq 0x30e0
movq 0x1c0(%rsp), %rdi
callq 0x3560