// File: /workspace/github_cpp_makefile_star2_O2/schneebergerlab[P]GameteBinning/asPollinator_1.0.cpp
// Function: order_linked_contigs(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, BIPAT, 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, BIPAT>>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool order_linked_contigs(map<string, BIPAT> contigPollenGTSeq, string tmpfolder)
{
    /*  contigPollenGTSeq:
	   PMPPMPPPMMPPPPM tig00000013_pilon left  sequence
	   PPPPMPPPMMPPPPM tig00000013_pilon right sequence
	   //
   	   PPPMMPPMPMPPUMM tig00000026_pilon ...
   	   PPPMMPPMPMPMUMM tig00000026_pilon ...  	  
   	   // 
	......
    */
    map<string, int> collected_edge; // if a->b edge exists, then ignore b->a edge
    // prepare dot output file
    string dotinfo = tmpfolder + "/s3_genotype_contig_GT_ordered_LGlike.dot\0"; // 
    ofstream dotofp;
    dotofp.open(dotinfo.c_str(), ios::out);
    if(!dotofp.good())
    {
        cout   << "   Error: cannot open file " << dotinfo << endl;
        return false;
    } 
    dotofp << "/* this graph is translated from similarity of genotype sequences of contigs */" << endl;
    dotofp << "digraph\tGraph_1 {" << endl;
    //
    map<string, BIPAT>::iterator ctgitr;
    map<string, BIPAT>::iterator ctgitr_end;
    ctgitr     = contigPollenGTSeq.begin();
    ctgitr_end = contigPollenGTSeq.end();
    while(ctgitr != ctgitr_end)
    {
        string this_contig_ID = (*ctgitr).first;
        string this_contig_GT = (*ctgitr).second.rightPat; // out vertex
        //
        CTGLG contigBestLods;
        contigBestLods.lod[0] = -1000; // best
        contigBestLods.lod[1] = -1000; // second-best
        contigBestLods.edge1  = "";
        contigBestLods.edge2  = "";
        // second level traverse of contigPollenGTSeq;
        map<string, BIPAT>::iterator ctgitr2;
        map<string, BIPAT>::iterator ctgitr2_end;
        ctgitr2     = contigPollenGTSeq.begin();
        ctgitr2_end = contigPollenGTSeq.end();
        while(ctgitr2 != ctgitr2_end)
        {
            string tmp2_contig_ID = (*ctgitr2).first;
            string tmp2_contig_GT = (*ctgitr2).second.leftPat; // in vertex
            int effectiveMkrNum   = 0;
            effectiveMkrNum      += std::count(tmp2_contig_GT.begin(), tmp2_contig_GT.end(), 'P');
            effectiveMkrNum      += std::count(tmp2_contig_GT.begin(), tmp2_contig_GT.end(), 'M');            
            // IF NEXT CONTIG DOES NOT HAVE ENOUGH PATERNAL/MATERNAL INFORMATION, SKIP IT 
            if(effectiveMkrNum < tmp2_contig_GT.size()/3)
            {
                 ctgitr2 ++;
                 continue;                 
            }
            //
            if(tmp2_contig_ID.compare(this_contig_ID) != 0)
            {
                // reversed edge not in collected list
                string edge1 = this_contig_ID + "->" + tmp2_contig_ID;
                string edge2 = this_contig_ID + "->" + tmp2_contig_ID + "_rev";
                if(collected_edge.find(edge1) == collected_edge.end() &&
                   collected_edge.find(edge2) == collected_edge.end() )
                {
                    /*
                    // original
                    int score1 = 0;
                    if(!get_match_score(this_contig_GT,        tmp2_contig_GT,       &score1) ) return false;
                    // reversed
                    string tmp2_contig_GT_reversed = "";
                    if(!reverse_contig_GT(tmp2_contig_GT, &tmp2_contig_GT_reversed) )           return false;
                    int score2 = 0;
                    if(!get_match_score(this_contig_GT,    tmp2_contig_GT_reversed,  &score2) ) return false;
                    // output as dot
                    if(score1 >= score2)
                    {
                        if(score1 >= this_contig_GT.size()*0.90 )
                        {
                            // select original: 	100052 -> 358538 [color=aquamarine, penwidth=1, arrowsize=1, label=45];
                            dotofp << "\t"   << this_contig_ID 
                                   << " -> " << tmp2_contig_ID
                                   << " "    << "[color=black, penwidth=1, arrowsize=1, label=" << score1 << "];"    
                                   << endl;
                            // collect edge
                            collected_edge.insert(std::pair<string, int>(this_contig_ID + "->" + tmp2_contig_ID, 1));       
                        }                    
                    }else
                    {
                        if(score2 >= this_contig_GT.size()*0.90 )
                        {                
                            // select reversed    
                            dotofp << "\t"   << this_contig_ID 
                                   << " -> " << tmp2_contig_ID << "_rev"
                                   << " "    << "[color=red, penwidth=1, arrowsize=1, label=" << score2 << "];"
                                   << endl;  
                            // collect edge
                            collected_edge.insert(std::pair<string, int>(this_contig_ID + "->" + tmp2_contig_ID + "_rev", 1));                                                               
                        }
                    }
                    */
                    double this_lod = calculate_lod(this_contig_GT, tmp2_contig_GT);
                    /*
                    if(this_lod>=40)
                    {
                        // select original: 	100052 -> 358538 [color=aquamarine, penwidth=1, arrowsize=1, label=45];
                        dotofp << "\t"   << this_contig_ID 
                               << " -> " << tmp2_contig_ID
                               << " "    << "[color=red, penwidth=1, arrowsize=1, label=" << this_lod << "];"    
                               << endl;
                        // collect edge
                        collected_edge.insert(std::pair<string, int>(this_contig_ID + "->" + tmp2_contig_ID, 1));                               
                    }
                    */
                    if(this_lod > contigBestLods.lod[0]) // best
                    {
                        contigBestLods.lod[0] = this_lod;
                        std::stringstream sstmp;
                        sstmp.str("");
                        sstmp << "\t"   << this_contig_ID 
                              << " -> " << tmp2_contig_ID
                              << " "    << "[color=red, penwidth=1, arrowsize=1, label=" << this_lod << "];";
                        contigBestLods.edge1  = sstmp.str();
                        // collect edge
                        collected_edge.insert(std::pair<string, int>(this_contig_ID + "->" + tmp2_contig_ID, 1));                                                       
                    }else
                    if(this_lod > contigBestLods.lod[1]) // second-best
                    {
                        //// contigBestLods.lod[1] = this_lod;
                        //// std::stringstream sstmp;
                        //// sstmp.str("");
                        //// sstmp << "\t"   << this_contig_ID 
                        ////      << " -> " << tmp2_contig_ID
                        ////      << " "    << "[color=red, penwidth=1, arrowsize=1, label=" << this_lod << "];";
                        //// contigBestLods.edge2  = sstmp.str(); 
                        //// // collect edge                        
                        //// collected_edge.insert(std::pair<string, int>(this_contig_ID + "->" + tmp2_contig_ID, 1));
                    } else;
                }
            }        
            ctgitr2 ++;
        }
        //
        if(contigBestLods.lod[0] > 0) // best
        {
            dotofp << contigBestLods.edge1 << endl;
        }
        if(contigBestLods.lod[1] > 0) // second-best
        {
            //// dotofp << contigBestLods.edge2 << endl;         
        }
        //
        ctgitr ++;
    }
    // end of dot file
    dotofp << "}" << endl;
    dotofp.close();
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5b8, %rsp            # imm = 0x5B8
movq %rdi, 0x8(%rsp)
leaq 0x108(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x7b54(%rip), %rdx      # 0x14630
leaq 0x130(%rsp), %rdi
callq 0xe1af
leaq 0x3b8(%rsp), %rdi
callq 0x33e0
movq 0x130(%rsp), %rsi
leaq 0x3b8(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x3050
movq 0x3b8(%rsp), %rax
movq -0x18(%rax), %rax
movl 0x3d8(%rsp,%rax), %ebx
testl %ebx, %ebx
je 0xcb55
movq 0xf4ac(%rip), %rdi      # 0x1bfd8
leaq 0x69ed(%rip), %rsi      # 0x13520
callq 0x3260
leaq 0x130(%rsp), %rsi
movq %rax, %rdi
callq 0x3240
movq %rax, %rdi
callq 0x3140
jmp 0xd0be
leaq 0x73f9(%rip), %rsi      # 0x13f55
leaq 0x3b8(%rsp), %rdi
callq 0x3260
movq %rax, %rdi
callq 0x3140
leaq 0x742d(%rip), %rsi      # 0x13fa5
leaq 0x3b8(%rsp), %rdi
callq 0x3260
movq %rax, %rdi
callq 0x3140
movl %ebx, 0x4(%rsp)
movq 0x8(%rsp), %rax
movq 0x18(%rax), %r15
addq $0x8, %rax
movq %rax, 0x18(%rsp)
leaq 0x30(%rsp), %r12
leaq 0x210(%rsp), %rbx
leaq 0x230(%rsp), %r13
cmpq 0x18(%rsp), %r15
je 0xd091
leaq 0x20(%r15), %rsi
leaq 0x90(%rsp), %rdi
callq 0x3300
leaq 0x60(%r15), %rsi
leaq 0x190(%rsp), %rdi
callq 0x3300
movq %r15, 0x88(%rsp)
leaq 0xd0(%rsp), %rax
movq %rax, 0xc0(%rsp)
andq $0x0, 0xc8(%rsp)
xorl %ecx, %ecx
movb %cl, 0xd0(%rsp)
leaq 0xf0(%rsp), %rax
movq %rax, 0xe0(%rsp)
andq $0x0, 0xe8(%rsp)
movb %cl, 0xf0(%rsp)
movapd 0x6449(%rip), %xmm0     # 0x13080
movapd %xmm0, 0xb0(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0x7278(%rip), %r14      # 0x13ec7
movq %r14, %rsi
callq 0x3440
leaq 0xe0(%rsp), %rdi
movq %r14, %rsi
callq 0x3440
movq 0x8(%rsp), %rax
movq 0x18(%rax), %r15
cmpq 0x18(%rsp), %r15
je 0xd025
leaq 0x20(%r15), %rsi
movq %rbx, %rdi
callq 0x3300
leaq 0x40(%r15), %rsi
leaq 0x68(%rsp), %rdi
callq 0x3300
movq %r12, %r14
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq %rdi, %rsi
movb $0x50, 0x230(%rsp)
movq %r13, %rdx
callq 0x106c9
movq %rax, %r12
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
addq %rdi, %rsi
movb $0x4d, 0x230(%rsp)
movq %r13, %rdx
callq 0x106c9
addl %r12d, %eax
movslq %eax, %rcx
movq 0x70(%rsp), %rax
xorl %edx, %edx
pushq $0x3
popq %rsi
divq %rsi
cmpq %rax, %rcx
movq %r14, %r12
jb 0xd003
movq %rbx, %rdi
leaq 0x90(%rsp), %rsi
callq 0x3030
testl %eax, %eax
je 0xd003
movq %r13, %rdi
leaq 0x90(%rsp), %rsi
leaq 0x7299(%rip), %rdx      # 0x13fb7
callq 0xe1af
leaq 0x170(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0xe5f1
movq %r13, %rdi
callq 0x3120
leaq 0x20(%rsp), %r14
movq %r14, %rdi
leaq 0x90(%rsp), %rsi
leaq 0x7262(%rip), %rdx      # 0x13fb7
callq 0xe1af
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0xe5f1
leaq 0x150(%rsp), %rdi
movq %r13, %rsi
leaq 0x7240(%rip), %rdx      # 0x13fba
callq 0xe1e7
movq %r13, %rdi
callq 0x3120
leaq 0x20(%rsp), %rdi
callq 0x3120
leaq 0x100(%rsp), %rdi
leaq 0x170(%rsp), %rsi
callq 0x1125a
leaq 0x108(%rsp), %rcx
cmpq %rcx, %rax
jne 0xcfe9
leaq 0x100(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x1125a
leaq 0x108(%rsp), %rcx
cmpq %rcx, %rax
jne 0xcfe9
leaq 0x1d0(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x3300
leaq 0x1f0(%rsp), %rbp
movq %rbp, %rdi
leaq 0x68(%rsp), %rsi
callq 0x3300
leaq 0x1d0(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
callq 0xdcf9
movsd %xmm0, 0x10(%rsp)
movq %rbp, %rdi
callq 0x3120
movq %r14, %rdi
callq 0x3120
movsd 0x10(%rsp), %xmm0
ucomisd 0xb0(%rsp), %xmm0
jbe 0xcfe9
movsd %xmm0, 0xb0(%rsp)
movq %r13, %rdi
callq 0x31d0
leaq 0x20(%rsp), %r14
movq %r14, %rdi
leaq 0x7062(%rip), %rsi      # 0x13ec7
leaq 0x48(%rsp), %rdx
callq 0xe174
leaq 0x248(%rsp), %rdi
movq %r14, %rsi
callq 0x3340
movq %r14, %rdi
callq 0x3120
leaq 0x240(%rsp), %rdi
leaq 0x7030(%rip), %rsi      # 0x13ec6
callq 0x3260
movq %rax, %rdi
leaq 0x90(%rsp), %rsi
callq 0x3240
movq %rax, %rdi
leaq 0x710a(%rip), %rsi      # 0x13fbf
callq 0x3260
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3240
movq %rax, %rdi
leaq 0x6d61(%rip), %rsi      # 0x13c30
callq 0x3260
movq %rax, %rdi
leaq 0x70e6(%rip), %rsi      # 0x13fc4
callq 0x3260
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x33b0
movq %rax, %rdi
leaq 0x70f5(%rip), %rsi      # 0x13ff0
callq 0x3260
leaq 0x20(%rsp), %r14
movq %r14, %rdi
leaq 0x248(%rsp), %rsi
callq 0x3380
leaq 0xc0(%rsp), %rdi
movq %r14, %rsi
callq 0x32d0
movq %r14, %rdi
callq 0x3120
leaq 0x1b0(%rsp), %r14
movq %r14, %rdi
leaq 0x90(%rsp), %rsi
leaq 0x7070(%rip), %rdx      # 0x13fb7
callq 0xe1af
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0xe5f1
movq %r12, 0x20(%rsp)
movq 0x48(%rsp), %rax
leaq 0x58(%rsp), %rcx
cmpq %rcx, %rax
je 0xcf81
movq %rax, 0x20(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0xcf8b
movupd (%rcx), %xmm0
movupd %xmm0, (%r12)
movq 0x50(%rsp), %rax
movq %rcx, 0x48(%rsp)
andq $0x0, 0x50(%rsp)
movq %rax, 0x28(%rsp)
movb $0x0, 0x58(%rsp)
movl $0x1, 0x40(%rsp)
leaq 0x100(%rsp), %rdi
leaq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0x112d0
movq %r14, %rdi
callq 0x3120
leaq 0x48(%rsp), %rdi
callq 0x3120
leaq 0x1b0(%rsp), %rdi
callq 0x3120
movq %r13, %rdi
callq 0x31f0
leaq 0x150(%rsp), %rdi
callq 0x3120
leaq 0x170(%rsp), %rdi
callq 0x3120
movq %r15, %rdi
callq 0x31c0
movq %rax, %r15
leaq 0x68(%rsp), %rdi
callq 0x3120
movq %rbx, %rdi
callq 0x3120
jmp 0xcc70
movsd 0xb0(%rsp), %xmm0
ucomisd 0x608a(%rip), %xmm0     # 0x130c0
jbe 0xd055
leaq 0x3b8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3240
movq %rax, %rdi
callq 0x3140
movq 0x88(%rsp), %rdi
callq 0x31c0
movq %rax, %r15
leaq 0xb0(%rsp), %rdi
callq 0xe610
leaq 0x190(%rsp), %rdi
callq 0x3120
leaq 0x90(%rsp), %rdi
callq 0x3120
jmp 0xcbb8
leaq 0x6f5b(%rip), %rsi      # 0x13ff3
leaq 0x3b8(%rsp), %rdi
callq 0x3260
movl 0x4(%rsp), %ebx
movq %rax, %rdi
callq 0x3140
leaq 0x3b8(%rsp), %rdi
callq 0x3310
testl %ebx, %ebx
sete %bl
leaq 0x3b8(%rsp), %rdi
callq 0x3330
leaq 0x130(%rsp), %rdi
callq 0x3120
leaq 0x100(%rsp), %rdi
callq 0xf728
movl %ebx, %eax
addq $0x5b8, %rsp            # imm = 0x5B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0xd24b
movq %rax, %rbx
jmp 0xd258
jmp 0xd154
jmp 0xd154
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3120
leaq 0x48(%rsp), %rdi
callq 0x3120
jmp 0xd13c
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
jmp 0xd144
jmp 0xd18e
jmp 0xd18e
movq %rax, %rbx
leaq 0x1b0(%rsp), %rdi
callq 0x3120
jmp 0xd191
jmp 0xd1a0
jmp 0xd18e
jmp 0xd1f1
movq %rax, %rbx
jmp 0xd23e
movq %rax, %rbx
jmp 0xd231
movq %rax, %rbx
jmp 0xd179
movq %rax, %rbx
leaq 0x1f0(%rsp), %rdi
callq 0x3120
leaq 0x1d0(%rsp), %rdi
callq 0x3120
jmp 0xd1a3
jmp 0xd1a0
jmp 0xd1f1
jmp 0xd1a0
movq %rax, %rbx
leaq 0x230(%rsp), %rdi
callq 0x31f0
jmp 0xd1a3
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
jmp 0xd1c7
movq %rax, %rbx
leaq 0x230(%rsp), %rdi
callq 0x3120
jmp 0xd1c2
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3120
leaq 0x170(%rsp), %rdi
callq 0x3120
jmp 0xd200
movq %rax, %rbx
jmp 0xd1cc
movq %rax, %rbx
leaq 0x230(%rsp), %rdi
jmp 0xd1d4
jmp 0xd1fd
jmp 0xd1fd
movq %rax, %rbx
jmp 0xd217
movq %rax, %rbx
jmp 0xd20a
jmp 0xd1fd
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x3120
leaq 0x210(%rsp), %rdi
callq 0x3120
leaq 0xb0(%rsp), %rdi
callq 0xe610
leaq 0x190(%rsp), %rdi
callq 0x3120
leaq 0x90(%rsp), %rdi
callq 0x3120
leaq 0x3b8(%rsp), %rdi
callq 0x3330
leaq 0x130(%rsp), %rdi
callq 0x3120
leaq 0x100(%rsp), %rdi
callq 0xf728
movq %rbx, %rdi
callq 0x3410