// File: /workspace/github_cpp_makefile_star2_O1/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 0xcc30
movq %rbx, %rdi
movq %r14, %rsi
movl $0x8, %edx
callq 0x39386
movq 0x88(%rsp), %rax
movq -0x18(%rax), %rax
movl 0xa8(%rsp,%rax), %ebp
testl %ebp, %ebp
je 0x1e9b9
movq 0x2d648(%rip), %rbx     # 0x4bfc8
leaq 0x1de5d(%rip), %rsi     # 0x3c7e4
movl $0x2f, %edx
movq %rbx, %rdi
callq 0x43a0
testq %r14, %r14
je 0x1ea21
movq %r14, %rdi
callq 0x4180
movq 0x2d61c(%rip), %rdi     # 0x4bfc8
movq %r14, %rsi
movq %rax, %rdx
callq 0x43a0
jmp 0x1ea38
movq 0x2d608(%rip), %rbx     # 0x4bfc8
leaq 0x1de4d(%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 0x2d5d6(%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 0x1ea68
xorl %r15d, %r15d
jmp 0x1ec23
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 0x2d572(%rip), %rdi     # 0x4bfc8
callq 0x4030
movq %rax, %rdi
callq 0x4220
jmp 0x1eccb
movq %r15, 0x80(%rsp)
leaq 0x28(%rsp), %rbp
leaq 0x210(%rsp), %r12
xorl %r15d, %r15d
leaq 0x2009b(%rip), %r13     # 0x3eb22
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)
je 0x1ebf2
movq 0x18(%rsp), %rax
cmpb $0x23, (%rax)
je 0x1ebf2
movb $0x9, 0x50(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
leaq 0x50(%rsp), %rdx
callq 0x36b7c
cmpq $0x7, 0x20(%rsp)
jb 0x1ebea
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 0x1eb4b
imull 0x4(%rsp), %ebx
imull 0x8(%rsp), %eax
cmpl %eax, %ebx
jle 0x1ebe0
movq 0x20(%r15), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4130
movq %rax, %rbx
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4130
movq %rax, %r15
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 0xda32
movq %rbx, 0x70(%rsp)
movq %r15, 0x78(%rsp)
movq 0x80(%rsp), %rdi
movq %rbp, %rsi
callq 0x21fee
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
movq 0x10(%rsp), %r15
leaq 0x38(%rsp), %rbx
leaq 0x28(%rsp), %rbp
je 0x1ebdb
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x4340
incq %r15
jmp 0x1ebea
movq 0x10(%rsp), %r15
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x6aa2
movq 0x18(%rsp), %rdi
cmpq %rbp, %rdi
je 0x1ec09
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4340
movq 0x88(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0xa8(%rsp,%rax)
je 0x1ea91
leaq 0x88(%rsp), %rdi
callq 0x3962e
movl 0xc(%rsp), %ebp
movq 0x2d38d(%rip), %rdi     # 0x4bfc8
leaq 0x1dc1a(%rip), %rsi     # 0x3c85c
movl $0x3f, %edx
callq 0x43a0
movq 0x2d375(%rip), %rdi     # 0x4bfc8
movl 0x8(%rsp), %esi
callq 0x4580
movq %rax, %rbx
leaq 0x1dc36(%rip), %rsi     # 0x3c89c
movl $0x16, %edx
movq %rax, %rdi
callq 0x43a0
movq %rbx, %rdi
movl 0x4(%rsp), %esi
callq 0x4580
movq %rax, %rbx
leaq 0x1d54c(%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 0x2cf69(%rip), %rax     # 0x4bc40
leaq 0x220(%rsp), %r14
movq %rax, -0x10(%r14)
leaq 0x2cf7e(%rip), %rax     # 0x4bc68
movq %rax, (%r14)
movq $0x0, -0x8(%r14)
leaq 0x2ceac(%rip), %rsi     # 0x4bba8
leaq 0x88(%rsp), %rdi
callq 0x39330
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 0x1ed49
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x4340
jmp 0x1ed49
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x6aa2
jmp 0x1ed63
jmp 0x1ed60
jmp 0x1ed5b
jmp 0x1ed5b
movq %rax, %rbx
jmp 0x1ed7f
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x1ed7f
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4340
leaq 0x88(%rsp), %rdi
callq 0xcecc
movq %rbx, %rdi
callq 0x45a0