// File: /workspace/github_cpp_makefile_star2_O2/schneebergerlab[P]DrLink/detect_recombsite.cpp
// Function: get_var_ref_barcode(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>*, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::multimap<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned long, 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, unsigned long>>>*)
bool get_var_ref_barcode(string                formatInfo, 
                         string                barcodeString, 
                         vector<string>*       vbc, 
                         vector<string>*       rbc,
                         string                this_chr,
                         string                this_pos,
                         multimap<string, unsigned long>* HeteroBarcode)
{
    //    2017-10-30: filter interleaving reads
    //                check if there are same barcode-reads covering the same allele at some variants!!!
    //                more possibly, these reads are from molecules with the same chr#barcode but from different plants.
    
    // formatInfo   : GT:DP:RO:QR:AO:QA:GL:BX:PS - can be different but must contain 'BX' flag
    // barcodeString: real values corresponding to the above format
    // vbc          : barcodes related to variant   allele
    // rbc          : barcodes related to reference allele
    vector<string> fmt = split_string(formatInfo, ':');
    int pos = -1;
    for(int i = 0; i < fmt.size(); i ++)
    {
        if(fmt[i].compare("BX") == 0)
        {
            pos = i;
            break;
        }
    }
    if(pos < 0)
    {
        cout << "   Error: no BX found in FORMAT. " << endl;
        return false;
    }
    vector<string> bcode = split_string(barcodeString, ':');
    if(bcode.size() != fmt.size())
    {
        cout << "   Error: sizes of FORMAT and VALUE mismatched." << endl;
        return false;
    }
    // find barcodes related to all alleles
    string b_all     = bcode[pos];
    // find barcodes related to alternative allele, after ',': "bcr1-;bcr2-,bcv1-;bcv2-;bcv3-"
    size_t seRefAlt  = b_all.find(",");
    assert(seRefAlt != std::string::npos);    
    //
    string b_var     = b_all.substr(seRefAlt+1);
    if(b_var.size()>15)
    {
        (*vbc)           = split_string(b_var, ';');
        // remove redundant info
        vector<string>::iterator itr;
        vector<string>::iterator itr_end;
        itr        = (*vbc).begin();
        itr_end    = (*vbc).end();  
        while(itr != itr_end)
        {
            string tmp      = *itr;
            size_t trashpos = tmp.find("-");
            if(trashpos == std::string::npos)
            {
                trashpos = tmp.size();
            }
            if(trashpos > 0)
            (*itr)          = tmp.substr(0, trashpos);
            itr             ++;
        }
    }
    else
    {
        (*vbc).clear(); // possibly no variant barcode? no!
    }
    // find barcodes related to reference allele, before ',': "bcr1-;bcr2-,bcv1-;bcv2-;bcv3-"
    string b_ref     = b_all.substr(0, seRefAlt);
    if(b_ref.size()>15)
    {
        (*rbc)           = split_string(b_ref, ';');
        // remove redundant info
        vector<string>::iterator itr;
        vector<string>::iterator itr_end;        
        itr        = (*rbc).begin();
        itr_end    = (*rbc).end();
        while(itr != (*rbc).end()) // note that (*rbc).end() is changing, so itr_end cannot be used here!!!
        {
            string tmp      = *itr;
            size_t trashpos = tmp.find("-");
            if(trashpos == std::string::npos)
            {
                trashpos = tmp.size();
            }
            if(trashpos > 0)
            (*itr)          = tmp.substr(0, trashpos);
            vector<string>::iterator overlapitr = std::find((*vbc).begin(), (*vbc).end(), *itr);
            if(overlapitr != (*vbc).end())
            {
                if(badBarcodeCov == 0)
                {
                    cout << "   Info: barcode " <<  *itr << " of "     << barcodeString 
                         << " covers both reference and alternative. " << endl;
                    cout << "   Warning: molecules with such barcodes removed (a total number given in the end). " 
                         << endl;
                }
                else
                {
                    if(false)
                    {
                        cout << "   Info: barcode " <<  *itr << " of " 
                             << barcodeString << " covers both reference and alternative. " << endl;
                    }
                }
                // count
                badBarcodeCov ++;
                // this is the barcode for removing falsely merged molecule covering the heterozgous mkr pos
                //(*vbc).erase(overlapitr);
                //itr = (*rbc).erase(itr);
                unsigned long mkrposwithbadbc = strtoul(this_pos.c_str(), NULL, 0);
                (*HeteroBarcode).insert(std::pair<string, unsigned long>(this_chr+"#"+(*itr), mkrposwithbadbc));
            }
            // next barcode
            itr ++;
        }
    }
    else
    {
        (*rbc).clear(); // possibly no variant barcode? yes!
    }
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %r9, 0x98(%rsp)
movq %r8, 0x90(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, %r12
movq %rsi, 0x18(%rsp)
movq %rdi, %rsi
leaq 0x20(%rsp), %rdx
movb $0x3a, (%rdx)
leaq 0x68(%rsp), %rdi
callq 0x26120
xorl %r15d, %r15d
leaq 0x16c4f(%rip), %rbx     # 0x2bb0a
xorl %r14d, %r14d
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq %r14, %rax
jbe 0x14eec
addq %r15, %rdi
movq %rbx, %rsi
callq 0x4110
testl %eax, %eax
je 0x14f0e
incq %r14
addq $0x20, %r15
jmp 0x14ebe
movq 0x280d5(%rip), %rdi     # 0x3cfc8
leaq 0x16c13(%rip), %rsi     # 0x2bb0d
callq 0x4330
movq %rax, %rdi
callq 0x41c0
xorl %ebx, %ebx
jmp 0x1534e
leaq 0x20(%rsp), %rdx
movb $0x3a, (%rdx)
leaq 0xc0(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x26120
movq 0xc0(%rsp), %rsi
movq 0xc8(%rsp), %rax
subq %rsi, %rax
movq 0x70(%rsp), %rcx
subq 0x68(%rsp), %rcx
cmpq %rcx, %rax
sete %al
movl %eax, 0x14(%rsp)
jne 0x1506e
movl %r14d, %eax
shlq $0x5, %rax
addq %rax, %rsi
leaq 0xd8(%rsp), %rdi
callq 0x4400
leaq 0x14f2d(%rip), %rsi     # 0x29ea0
leaq 0xd8(%rsp), %rdi
xorl %edx, %edx
callq 0x4600
cmpq $-0x1, %rax
je 0x1536c
movq %rax, 0x80(%rsp)
leaq 0x1(%rax), %rdx
leaq 0x118(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
pushq $-0x1
popq %rcx
callq 0x4360
cmpq $0x10, 0x120(%rsp)
jb 0x1508e
leaq 0xa0(%rsp), %rdx
movb $0x3b, (%rdx)
leaq 0x20(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x26120
leaq 0x20(%rsp), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x17e38
movq %r15, %rdi
callq 0x5d02
movq (%r12), %r13
movq 0x8(%r12), %rbx
leaq 0x145aa(%rip), %rbp     # 0x295ae
leaq 0xa0(%rsp), %r14
cmpq %rbx, %r13
je 0x15096
movq %r15, %rdi
movq %r13, %rsi
callq 0x4400
movq %r15, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x4600
cmpq $-0x1, %rax
jne 0x15038
movq 0x28(%rsp), %rax
testq %rax, %rax
je 0x15060
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4360
movq %r13, %rdi
movq %r14, %rsi
callq 0x43c0
movq %r14, %rdi
callq 0x4638
addq $0x20, %r13
movq %r15, %rdi
callq 0x4638
jmp 0x1500c
movq 0x27f53(%rip), %rdi     # 0x3cfc8
leaq 0x16ab3(%rip), %rsi     # 0x2bb2f
callq 0x4330
movq %rax, %rdi
callq 0x41c0
jmp 0x1533d
movq %r12, %rdi
callq 0x158e2
leaq 0xa0(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
xorl %edx, %edx
movq 0x80(%rsp), %rcx
callq 0x4360
cmpq $0x10, 0xa8(%rsp)
jb 0x1530c
leaq 0xf8(%rsp), %rdx
movb $0x3b, (%rdx)
leaq 0x20(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x26120
leaq 0x20(%rsp), %rbp
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x17e38
movq %rbp, %rdi
callq 0x5d02
movq (%rbx), %r13
leaq 0x30(%rsp), %rbx
leaq 0x58(%rsp), %r15
leaq 0xf8(%rsp), %r14
movq %r12, 0x88(%rsp)
movq 0x8(%rsp), %rax
cmpq 0x8(%rax), %r13
je 0x15316
movq %r14, %rdi
movq %r13, %rsi
callq 0x4400
movq %r14, %rdi
leaq 0x1446f(%rip), %rsi     # 0x295ae
xorl %edx, %edx
callq 0x4600
cmpq $-0x1, %rax
jne 0x15154
movq 0x100(%rsp), %rax
testq %rax, %rax
je 0x1517c
movq %rbp, %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4360
movq %r13, %rdi
movq %rbp, %rsi
callq 0x43c0
movq %rbp, %rdi
callq 0x4638
movq (%r12), %rdi
movq 0x8(%r12), %rsi
movq %r13, %rdx
callq 0x17eb9
cmpq 0x8(%r12), %rax
je 0x152fb
movq 0x281c9(%rip), %rax     # 0x3d368
testq %rax, %rax
jne 0x15217
movq 0x27e1d(%rip), %rdi     # 0x3cfc8
leaq 0x16a4b(%rip), %rsi     # 0x2bbfd
callq 0x4330
movq %rax, %rdi
movq %r13, %rsi
callq 0x42e0
movq %rax, %rdi
leaq 0x16d0a(%rip), %rsi     # 0x2bed6
callq 0x4330
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x42e0
movq %rax, %rdi
leaq 0x16a27(%rip), %rsi     # 0x2bc0f
callq 0x4330
movq %rax, %rdi
callq 0x41c0
movq 0x27dcc(%rip), %rdi     # 0x3cfc8
leaq 0x16a35(%rip), %rsi     # 0x2bc38
callq 0x4330
movq %rax, %rdi
callq 0x41c0
movq 0x28151(%rip), %rax     # 0x3d368
movq %r15, %r12
movq %rbx, %r15
incq %rax
movq %rax, 0x28141(%rip)     # 0x3d368
movq 0x98(%rsp), %rax
movq (%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4130
movq %rax, %rbx
leaq 0x138(%rsp), %r14
movq %r14, %rdi
movq 0x90(%rsp), %rsi
leaq 0x18d8a(%rip), %rdx     # 0x2dfe2
callq 0x95e2
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x961a
movq %r15, 0x20(%rsp)
movq 0x48(%rsp), %rax
movq %r12, %rdx
cmpq %r12, %rax
je 0x15290
movq %rax, 0x20(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0x15297
movups (%rdx), %xmm0
movups %xmm0, (%r15)
movq 0x50(%rsp), %rax
movq %rdx, 0x48(%rsp)
andq $0x0, 0x50(%rsp)
movq %rax, 0x28(%rsp)
movb $0x0, 0x58(%rsp)
movq %rbx, 0x40(%rsp)
movq 0x190(%rsp), %rdi
movq %rbp, %rsi
callq 0x17fcc
movq %rbp, %rdi
callq 0x4638
leaq 0x48(%rsp), %rdi
callq 0x4638
leaq 0x138(%rsp), %rdi
callq 0x4638
movq %r15, %rbx
movq %r12, %r15
movq 0x88(%rsp), %r12
leaq 0xf8(%rsp), %r14
addq $0x20, %r13
movq %r14, %rdi
callq 0x4638
jmp 0x1511b
movq 0x8(%rsp), %rdi
callq 0x158e2
leaq 0xa0(%rsp), %rdi
callq 0x4638
leaq 0x118(%rsp), %rdi
callq 0x4638
leaq 0xd8(%rsp), %rdi
callq 0x4638
leaq 0xc0(%rsp), %rdi
callq 0x5d02
movl 0x14(%rsp), %ebx
leaq 0x68(%rsp), %rdi
callq 0x5d02
movl %ebx, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x167ec(%rip), %rdi     # 0x2bb5f
leaq 0x15b87(%rip), %rsi     # 0x2af01
leaq 0x167fc(%rip), %rcx     # 0x2bb7d
movl $0x6c8, %edx            # imm = 0x6C8
callq 0x4210
jmp 0x153e6
jmp 0x153eb
jmp 0x153eb
movq %rax, %rbx
jmp 0x1541a
jmp 0x1539b
movq %rax, %rbx
jmp 0x15427
jmp 0x153a5
movq %rax, %rbx
jmp 0x15434
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x4638
leaq 0x48(%rsp), %rdi
callq 0x4638
jmp 0x153cd
jmp 0x153f0
jmp 0x153f0
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
callq 0x4638
jmp 0x153f3
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
jmp 0x15408
movq %rax, %rbx
jmp 0x15400
movq %rax, %rbx
jmp 0x1540d
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x4638
leaq 0xa0(%rsp), %rdi
callq 0x4638
leaq 0x118(%rsp), %rdi
callq 0x4638
leaq 0xd8(%rsp), %rdi
callq 0x4638
leaq 0xc0(%rsp), %rdi
callq 0x5d02
leaq 0x68(%rsp), %rdi
callq 0x5d02
movq %rbx, %rdi
callq 0x45a0