// File: /workspace/github_cpp_makefile_star2_O2/schneebergerlab[P]DrLink/sample_molecule.cpp
// Function: write_sampled_molecule_stat(std::map<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>>>, std::map<int, unsigned long, std::less<int>, std::allocator<std::pair<int const, unsigned long>>>, std::map<int, unsigned long, std::less<int>, std::allocator<std::pair<int const, unsigned long>>>, std::map<int, unsigned long, std::less<int>, std::allocator<std::pair<int const, unsigned long>>>, std::map<int, unsigned long, std::less<int>, std::allocator<std::pair<int const, unsigned long>>>, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, 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, int>>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool write_sampled_molecule_stat(map<string, unsigned long> molecule_cov, 
                                 map<int,    unsigned long> molecule_len, 
                                 map<int,    unsigned long> molecule_len_sum, 
                                 map<int,    unsigned long> molecule_read_num,
                                 map<int,    unsigned long> molecule_read_num_sum,
                                 map<string, int>           barcode_molecule_cnt,
                                 string outprefix)
{
    // 1. molecule base coverage
    string outMCovFile = outprefix + "_min1000bp_sampled_moleCov_stat.txt";
    ofstream ofp;
    ofp.open(outMCovFile.c_str(), ios::out);
    if(!ofp.good())
    {
        cout << "   Error: cannot open " << outMCovFile << " to write data. " << endl;
        return false;
    }
    map<string, unsigned long>::iterator mcitr;
    map<string, unsigned long>::iterator mcitr_end;
    mcitr     = molecule_cov.begin();
    mcitr_end = molecule_cov.end();
    ofp << "#molecule_base_cov\tmolecule_num_in_bin" << endl;
    while(mcitr != mcitr_end)
    {
        ofp << (*mcitr).first << "\t" << (*mcitr).second << endl;
        mcitr ++;
    }
    ofp.close();
    
    // 2. molecule length: total legnth and number of molecules in bin
    string outMLenFile = outprefix + "_min1000bp_sampled_moleLen_stat.txt";    
    ofstream ofp2;
    ofp2.open(outMLenFile.c_str(), ios::out);
    if(!ofp2.good())
    {
        cout << "   Error: cannot open " << outMLenFile << " to write data. " << endl;
        return false;
    }
    map<int, unsigned long>::iterator mlitr;
    map<int, unsigned long>::iterator mlitr_end;
    mlitr     = molecule_len.begin();
    mlitr_end = molecule_len.end();
    ofp2 << "#molecule_len_kb\tmolecule_total_len_bp\tmolecule_num_in_bin" << endl;
    while(mlitr != mlitr_end)
    {
        ofp2 << (*mlitr).first  << "\t" 
             << molecule_len_sum[(*mlitr).first] << "\t" 
             << (*mlitr).second << endl;
        mlitr ++;
    }
    ofp2.close();    
    // 3. molecules per barcode/partition: find hist + output
    // find hist: 'val' (number) partitions have 'key' (number) molecules
    map<int, int> mbhist;
    map<int, int>::iterator hitr;
    map<string, int>::iterator mbcitr;
    map<string, int>::iterator mbcitr_end;
    mbcitr     = barcode_molecule_cnt.begin();
    mbcitr_end = barcode_molecule_cnt.end();
    while(mbcitr != mbcitr_end)
    {
        int cntkey = (*mbcitr).second;
        hitr = mbhist.find(cntkey);
        if(hitr == mbhist.end())
        {
            mbhist.insert(std::pair<int, int>(cntkey, 1));
        }
        else
        {
            mbhist[cntkey] += 1;
        }
        mbcitr ++;
    }  
    // output
    string outMpBHistFile = outprefix + "_min1000bp_sampled_moleNumPerBarc_stat.txt";        
    ofstream ofp3;
    ofp3.open(outMpBHistFile.c_str(), ios::out);
    if(!ofp3.good())
    {
        cout << "   Error: cannot open " << outMpBHistFile << " to write data. " << endl;
        return false;
    }   
    map<int, int>::iterator hitr_end;
    hitr     = mbhist.begin();
    hitr_end = mbhist.end();
    ofp3 << "#molecule_cnt\tpartition/barcode_num" << endl;
    while(hitr != hitr_end)
    {
        ofp3 << (*hitr).first  << "\t" << (*hitr).second << endl;
        hitr ++;
    }
    ofp3.close(); 
    // 4. number of reads per molecule
    string outReadNumFile = outprefix + "_min1000bp_sampled_readNum_stat.txt";    
    ofstream ofp4;
    ofp4.open(outReadNumFile.c_str(), ios::out);
    if(!ofp4.good())
    {
        cout << "   Error: cannot open " << outReadNumFile << " to write data. " << endl;
        return false;
    }    
    map<int,    unsigned long>::iterator readitr;
    map<int,    unsigned long>::iterator readitr_end;
    readitr     = molecule_read_num.begin();
    readitr_end = molecule_read_num.end();
    ofp4 << "#read_number\tmolecule_number_with_that_read_number\ttotal_covered_len" << endl;
    while(readitr != readitr_end)
    {
        ofp4 << (*readitr).first   << "\t" 
             << (*readitr).second  << "\t"
             << molecule_read_num_sum[(*readitr).first] << endl;
        readitr ++;
    }
    ofp4.close();
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x8c8, %rsp            # imm = 0x8C8
movq %r9, 0x10(%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, (%rsp)
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %rbx
movq 0x900(%rsp), %rsi
leaq 0x10937(%rip), %rdx     # 0x2d4a4
leaq 0x78(%rsp), %rdi
callq 0x95e2
leaq 0x6c8(%rsp), %rdi
callq 0x4570
movq 0x78(%rsp), %rsi
leaq 0x6c8(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x4050
movq 0x6c8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x6e8(%rsp,%rax)
je 0x1cbed
movq 0x20412(%rip), %rdi     # 0x3cfc8
leaq 0xcf5b(%rip), %rsi      # 0x29b18
callq 0x4330
leaq 0x78(%rsp), %rsi
movq %rax, %rdi
callq 0x42e0
leaq 0xd2f6(%rip), %rsi      # 0x29ecc
movq %rax, %rdi
callq 0x4330
movq %rax, %rdi
callq 0x41c0
xorl %ebp, %ebp
jmp 0x1d0da
movq 0x18(%rbx), %r14
leaq 0xd2e5(%rip), %rsi      # 0x29edd
leaq 0x6c8(%rsp), %rdi
callq 0x4330
movq %rax, %rdi
callq 0x41c0
addq $0x8, %rbx
leaq 0x6c8(%rsp), %r15
leaq 0x10ef9(%rip), %r12     # 0x2db19
cmpq %rbx, %r14
je 0x1cc5d
leaq 0x20(%r14), %rsi
movq %r15, %rdi
callq 0x42e0
movq %rax, %rdi
movq %r12, %rsi
callq 0x4330
movq 0x40(%r14), %rsi
movq %rax, %rdi
callq 0x4230
movq %rax, %rdi
callq 0x41c0
movq %r14, %rdi
callq 0x4240
movq %rax, %r14
jmp 0x1cc20
leaq 0x6c8(%rsp), %rdi
callq 0x4430
leaq 0x10857(%rip), %rdx     # 0x2d4c8
leaq 0x58(%rsp), %rdi
movq 0x900(%rsp), %rsi
callq 0x95e2
leaq 0x4c8(%rsp), %rdi
callq 0x4570
movq 0x58(%rsp), %rsi
leaq 0x4c8(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x4050
movq 0x4c8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x4e8(%rsp,%rax)
je 0x1ccf9
movq 0x20306(%rip), %rdi     # 0x3cfc8
leaq 0xce4f(%rip), %rsi      # 0x29b18
callq 0x4330
leaq 0x58(%rsp), %rsi
movq %rax, %rdi
callq 0x42e0
leaq 0xd1ea(%rip), %rsi      # 0x29ecc
movq %rax, %rdi
callq 0x4330
movq %rax, %rdi
callq 0x41c0
xorl %ebp, %ebp
jmp 0x1d0c3
movq 0x18(%rbp), %rbx
leaq 0xd200(%rip), %rsi      # 0x29f04
leaq 0x4c8(%rsp), %rdi
callq 0x4330
movq %rax, %rdi
callq 0x41c0
addq $0x8, %rbp
leaq 0x4c8(%rsp), %r15
leaq 0x10ded(%rip), %r14     # 0x2db19
cmpq %rbp, %rbx
je 0x1cd8d
movl 0x20(%rbx), %esi
movq %r15, %rdi
callq 0x4580
movq %rax, %rdi
movq %r14, %rsi
callq 0x4330
movq %rax, %r12
leaq 0x20(%rbx), %rsi
movq %r13, %rdi
callq 0x973e
movq (%rax), %rsi
movq %r12, %rdi
callq 0x4230
movq %rax, %rdi
movq %r14, %rsi
callq 0x4330
movq 0x28(%rbx), %rsi
movq %rax, %rdi
callq 0x4230
movq %rax, %rdi
callq 0x41c0
movq %rbx, %rdi
callq 0x4240
movq %rax, %rbx
jmp 0x1cd2c
leaq 0x4c8(%rsp), %rdi
callq 0x4430
leaq 0xa0(%rsp), %r12
andl $0x0, (%r12)
andq $0x0, 0x8(%r12)
movq %r12, 0x10(%r12)
movq %r12, 0x18(%r12)
andq $0x0, 0x20(%r12)
movq 0x10(%rsp), %rbp
movq 0x18(%rbp), %rbx
addq $0x8, %rbp
leaq 0x98(%rsp), %r14
leaq 0xc8(%rsp), %r15
leaq 0x2c8(%rsp), %r13
cmpq %rbp, %rbx
je 0x1ce41
movl 0x40(%rbx), %eax
movl %eax, 0xc8(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xae24
cmpq %r12, %rax
je 0x1ce10
movq %r14, %rdi
movq %r15, %rsi
callq 0x97f2
incl (%rax)
jmp 0x1ce34
movl 0xc8(%rsp), %eax
movl %eax, 0x2c8(%rsp)
movl $0x1, 0x2cc(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0xae76
movq %rbx, %rdi
callq 0x4240
movq %rax, %rbx
jmp 0x1cde2
leaq 0x106a4(%rip), %rdx     # 0x2d4ec
leaq 0x38(%rsp), %rdi
movq 0x900(%rsp), %rsi
callq 0x95e2
leaq 0x2c8(%rsp), %rdi
callq 0x4570
movq 0x8(%rsp), %r13
movq 0x38(%rsp), %rsi
leaq 0x2c8(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x4050
movq 0x2c8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x2e8(%rsp,%rax)
je 0x1ced5
movq 0x2012a(%rip), %rdi     # 0x3cfc8
leaq 0xcc73(%rip), %rsi      # 0x29b18
callq 0x4330
leaq 0x38(%rsp), %rsi
movq %rax, %rdi
callq 0x42e0
leaq 0xd00e(%rip), %rsi      # 0x29ecc
movq %rax, %rdi
callq 0x4330
movq %rax, %rdi
callq 0x41c0
xorl %ebp, %ebp
jmp 0x1d09f
movq 0xb0(%rsp), %rbx
leaq 0xd076(%rip), %rsi      # 0x29f5a
leaq 0x2c8(%rsp), %rdi
callq 0x4330
movq %rax, %rdi
callq 0x41c0
leaq 0x2c8(%rsp), %r14
leaq 0x10c11(%rip), %r15     # 0x2db19
cmpq %r12, %rbx
je 0x1cf43
movl 0x20(%rbx), %esi
movq %r14, %rdi
callq 0x4580
movq %rax, %rdi
movq %r15, %rsi
callq 0x4330
movl 0x24(%rbx), %esi
movq %rax, %rdi
callq 0x4580
movq %rax, %rdi
callq 0x41c0
movq %rbx, %rdi
callq 0x4240
movq %rax, %rbx
jmp 0x1cf08
leaq 0x2c8(%rsp), %rdi
callq 0x4430
leaq 0x105c0(%rip), %rdx     # 0x2d517
leaq 0x18(%rsp), %rdi
movq 0x900(%rsp), %rsi
callq 0x95e2
leaq 0xc8(%rsp), %rdi
callq 0x4570
movq 0x18(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x4050
movq 0xc8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0xe8(%rsp,%rax)
sete %bpl
je 0x1cfe1
movq 0x2001c(%rip), %rdi     # 0x3cfc8
leaq 0xcb65(%rip), %rsi      # 0x29b18
callq 0x4330
leaq 0x18(%rsp), %rsi
movq %rax, %rdi
callq 0x42e0
leaq 0xcf00(%rip), %rsi      # 0x29ecc
movq %rax, %rdi
callq 0x4330
movq %rax, %rdi
callq 0x41c0
jmp 0x1d088
movq (%rsp), %rax
movq 0x18(%rax), %rbx
leaq 0xcf8e(%rip), %rsi      # 0x29f7e
leaq 0xc8(%rsp), %rdi
callq 0x4330
movq %rax, %rdi
callq 0x41c0
addq $0x8, (%rsp)
leaq 0xc8(%rsp), %r15
leaq 0x10b00(%rip), %r14     # 0x2db19
cmpq (%rsp), %rbx
je 0x1d07b
movl 0x20(%rbx), %esi
movq %r15, %rdi
callq 0x4580
movq %rax, %rdi
movq %r14, %rsi
callq 0x4330
movq 0x28(%rbx), %rsi
movq %rax, %rdi
callq 0x4230
movq %rax, %rdi
movq %r14, %rsi
callq 0x4330
movq %rax, %r12
leaq 0x20(%rbx), %rsi
movq %r13, %rdi
callq 0x973e
movq (%rax), %rsi
movq %r12, %rdi
callq 0x4230
movq %rax, %rdi
callq 0x41c0
movq %rbx, %rdi
callq 0x4240
movq %rax, %rbx
jmp 0x1d019
leaq 0xc8(%rsp), %rdi
callq 0x4430
leaq 0xc8(%rsp), %rdi
callq 0x4480
leaq 0x18(%rsp), %rdi
callq 0x4638
leaq 0x2c8(%rsp), %rdi
callq 0x4480
leaq 0x38(%rsp), %rdi
callq 0x4638
leaq 0x98(%rsp), %rdi
callq 0xa0e8
leaq 0x4c8(%rsp), %rdi
callq 0x4480
leaq 0x58(%rsp), %rdi
callq 0x4638
leaq 0x6c8(%rsp), %rdi
callq 0x4480
leaq 0x78(%rsp), %rdi
callq 0x4638
movl %ebp, %eax
addq $0x8c8, %rsp            # imm = 0x8C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x1d146
jmp 0x1d157
jmp 0x1d136
movq %rax, %rbx
jmp 0x1d167
jmp 0x1d152
jmp 0x1d157
jmp 0x1d136
movq %rax, %rbx
jmp 0x1d190
jmp 0x1d19c
jmp 0x1d180
jmp 0x1d157
movq %rax, %rbx
jmp 0x1d1ac
jmp 0x1d19c
jmp 0x1d180
jmp 0x1d152
jmp 0x1d152
jmp 0x1d19c
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x4480
leaq 0x18(%rsp), %rdi
callq 0x4638
jmp 0x1d15a
movq %rax, %rbx
jmp 0x1d171
movq %rax, %rbx
leaq 0x2c8(%rsp), %rdi
callq 0x4480
leaq 0x38(%rsp), %rdi
callq 0x4638
leaq 0x98(%rsp), %rdi
callq 0xa0e8
jmp 0x1d183
movq %rax, %rbx
leaq 0x4c8(%rsp), %rdi
callq 0x4480
leaq 0x58(%rsp), %rdi
callq 0x4638
jmp 0x1d19f
movq %rax, %rbx
leaq 0x6c8(%rsp), %rdi
callq 0x4480
leaq 0x78(%rsp), %rdi
callq 0x4638
movq %rbx, %rdi
callq 0x45a0