// File: /workspace/github_cpp_makefile_star2_O3/schneebergerlab[P]DrLink/detect_recombsite.cpp
// Function: get_bad_molecule(char const*, int, int, std::multimap<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<unsigned long, 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, std::pair<unsigned long, unsigned long>>>>*)
bool get_bad_molecule(const char* file, 
                      int         maxReadNum,
                      int         maxMoleLen,
                      multimap<string, std::pair<unsigned long, unsigned long> >* badMolecule)
{
    // 1. if molecule length > cutoff,      collect it as bad
    // 2. if molecule read number > cutoff, collect it as bad
    // 3. if rNumber > mLen/mLen_cutoff * rNumber_cutoff, collect it as bad - 2018-07-30
    // molecule table: statistics of all moleclues: generated by 'DrLink molecule'
    // format: chr#barcode    first_aligned   last_aligned    molecule_len    molecule_cov    read_num    Uni_flag
    igzstream fp;
    fp.open(file);
    if(!fp.good())
    {
        cout << "   Error: cannot open read number distribution " << file << endl;
        return false;
    }
    else
    {
        cout << "   Info: collecting bad molecules (with larger read number or size)... " << endl;
    }
    unsigned long numibad = 0;
    while(fp.good())
    {
        string line("");
        getline(fp, line);
        if(line.size()==0 || line[0]=='#') continue;
        
        vector<string> lineinfo = split_string(line, '\t');
        if(line.size() < 7) continue;
        
        int readnum = atoi(lineinfo[5].c_str());
        int molelen = atoi(lineinfo[3].c_str()); 
        if( (readnum>maxReadNum || molelen>maxMoleLen) ||  
            (readnum*maxMoleLen > maxReadNum*molelen)
          )// potential overlapping molecules
        {
            std::pair <unsigned long, unsigned long> span;
            span.first  = strtoul(lineinfo[1].c_str(), NULL, 0);
            span.second = strtoul(lineinfo[2].c_str(), NULL, 0);
            // chr+"#"+barcode => (sta, end) : allow multi-aligns with the same barcode.
            (*badMolecule).insert(std::pair<string, std::pair<unsigned long, unsigned long> >(lineinfo[0], span));  
            numibad ++;          
        }
    }
    fp.close();
    cout << "   Info: number of collected bad molecules with read number >  " << maxReadNum 
         << " or molecule length > "                                          << maxMoleLen << " bp: "
         << numibad << endl;    
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x328, %rsp            # imm = 0x328
movq %rcx, %r15
movl %edx, 0x4(%rsp)
movl %esi, 0x8(%rsp)
movq %rdi, %r14
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
callq 0xcb3a
movq %rbx, %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x39022
movq 0x88(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xa8(%rsp,%rax), %ebp
testl %ebp, %ebp
je 0x1e8ff
movq 0x2d702(%rip), %rbx     # 0x4bfc8
leaq 0x1df17(%rip), %rsi     # 0x3c7e4
movl $0x2f, %edx
movq %rbx, %rdi
callq 0x43a0
testq %r14, %r14
je 0x1e967
movq %r14, %rdi
callq 0x4180
movq 0x2d6d6(%rip), %rdi     # 0x4bfc8
movq %r14, %rsi
movq %rax, %rdx
callq 0x43a0
jmp 0x1e97e
movq 0x2d6c2(%rip), %rbx     # 0x4bfc8
leaq 0x1df07(%rip), %rsi     # 0x3c814
movl $0x47, %edx
movq %rbx, %rdi
callq 0x43a0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x4310
movsbl %al, %esi
movq 0x2d690(%rip), %rdi     # 0x4bfc8
callq 0x4030
movq %rax, %rdi
callq 0x4220
movl %ebp, 0xc(%rsp)
movq 0x88(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0xa8(%rsp,%rax)
je 0x1e9ae
xorl %r15d, %r15d
jmp 0x1eb69
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x4560
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x4310
movsbl %al, %esi
movq 0x2d62c(%rip), %rdi     # 0x4bfc8
callq 0x4030
movq %rax, %rdi
callq 0x4220
jmp 0x1ec11
movq %r15, 0x80(%rsp)
leaq 0x28(%rsp), %rbp
leaq 0x210(%rsp), %r12
xorl %r15d, %r15d
leaq 0x2013d(%rip), %r13     # 0x3eb0a
leaq 0x18(%rsp), %r14
leaq 0x38(%rsp), %rbx
movq %rbp, 0x18(%rsp)
movq %r14, %rdi
movq %r13, %rsi
movq %r13, %rdx
callq 0x5384
movq 0x210(%rsp), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x4310
movsbl %al, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x45c0
cmpq $0x0, 0x20(%rsp)
movq 0x18(%rsp), %rdi
je 0x1eb3d
cmpb $0x23, (%rdi)
je 0x1eb3d
movb $0x9, 0x50(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x50(%rsp), %rdx
callq 0x367c0
cmpq $0x7, 0x20(%rsp)
jb 0x1eb30
movq %r15, 0x10(%rsp)
movq 0x38(%rsp), %r15
movq 0xa0(%r15), %rdi
callq 0x44d0
movl %eax, %ebx
movq 0x60(%r15), %rdi
callq 0x44d0
cmpl 0x8(%rsp), %ebx
setg %cl
cmpl 0x4(%rsp), %eax
setg %dl
orb %cl, %dl
jne 0x1ea91
imull 0x4(%rsp), %ebx
imull 0x8(%rsp), %eax
cmpl %eax, %ebx
jle 0x1eb26
movq 0x20(%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4130
movq %rax, %r15
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4130
movq %rax, %rbx
movq 0x38(%rsp), %rax
leaq 0x60(%rsp), %rcx
movq %rcx, 0x50(%rsp)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
callq 0xd940
movq %r15, 0x70(%rsp)
movq %rbx, 0x78(%rsp)
movq 0x80(%rsp), %rdi
movq %rbp, %rsi
callq 0x21eac
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
movq 0x10(%rsp), %r15
leaq 0x38(%rsp), %rbx
leaq 0x28(%rsp), %rbp
je 0x1eb21
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x4340
incq %r15
jmp 0x1eb30
movq 0x10(%rsp), %r15
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x6aac
movq 0x18(%rsp), %rdi
cmpq %rbp, %rdi
je 0x1eb4f
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4340
movq 0x88(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0xa8(%rsp,%rax)
je 0x1e9d7
leaq 0x88(%rsp), %rdi
callq 0x392ca
movl 0xc(%rsp), %ebp
movq 0x2d447(%rip), %rdi     # 0x4bfc8
leaq 0x1dcd4(%rip), %rsi     # 0x3c85c
movl $0x3f, %edx
callq 0x43a0
movq 0x2d42f(%rip), %rdi     # 0x4bfc8
movl 0x8(%rsp), %esi
callq 0x4580
movq %rax, %rbx
leaq 0x1dcf0(%rip), %rsi     # 0x3c89c
movl $0x16, %edx
movq %rax, %rdi
callq 0x43a0
movq %rbx, %rdi
movl 0x4(%rsp), %esi
callq 0x4580
movq %rax, %rbx
leaq 0x1d606(%rip), %rsi     # 0x3c1d5
movl $0x5, %edx
movq %rax, %rdi
callq 0x43a0
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4210
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4310
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4030
movq %rax, %rdi
callq 0x4220
testl %ebp, %ebp
sete %bl
leaq 0x2d023(%rip), %rax     # 0x4bc40
leaq 0x220(%rsp), %r14
movq %rax, -0x10(%r14)
leaq 0x2d038(%rip), %rax     # 0x4bc68
movq %rax, (%r14)
movq $0x0, -0x8(%r14)
leaq 0x2cf66(%rip), %rsi     # 0x4bba8
leaq 0x88(%rsp), %rdi
callq 0x38fcc
movq %r14, %rdi
callq 0x4140
movl %ebx, %eax
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x1ec8f
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x4340
jmp 0x1ec8f
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x6aac
jmp 0x1eca9
jmp 0x1eca6
jmp 0x1eca1
jmp 0x1eca1
movq %rax, %rbx
jmp 0x1ecc5
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x1ecc5
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4340
leaq 0x88(%rsp), %rdi
callq 0xcdd6
movq %rbx, %rdi
callq 0x45a0