// File: /workspace/github_cpp_makefile_star2_O2/millanek[P]evo/evo_getInformativeReadsFromSam.cpp
// Function: InfoReadsMain(int, char**)
int InfoReadsMain(int argc, char** argv) {
    parseInfoReadsOptions(argc, argv);
    string line; // for reading the input files
    
    std::istream* hetFile = createReader(opt::hetFile.c_str());
    std::istream* samtoolsFile = &std::cin;
    
    std::unordered_set<int> phasedHetsPos;
    std::map<int,PhaseInfo*> positionToPhase;
    std::map<string,std::vector<string> > readNameToSamRecords;
    
    
    if (opt::hapcutFormat) {
        // Parse the Hapcut blocks file
        while (getline(*hetFile, line)) {
            if (line[0] == '*') {
            
            } else if (line[0] == 'B' && line[1] == 'L') { // New block - should in the future separate the hets by blocks
                
            } else {
                std::vector<string> phasedSNPdetails = split(line, '\t');
                int snpPos = atoi(phasedSNPdetails[4].c_str());
                int H1phase = atoi(phasedSNPdetails[1].c_str());
                int H2phase = atoi(phasedSNPdetails[2].c_str());
                //std::cout << "line: " << line << std::endl;
                // std::cout << "phasedSNPdetails[5]: " << phasedSNPdetails[5] << std::endl;
                assert(phasedSNPdetails[5].length() == 1); assert(phasedSNPdetails[6].length() == 1);
                char refBase = phasedSNPdetails[5][0];
                char altBase = phasedSNPdetails[6][0];
                double phaseQual = stringToDouble(phasedSNPdetails[10].c_str());
                int snpCoverage = atoi(phasedSNPdetails[11].c_str());
                std::vector<char> phasedVars;
                if (H1phase == 0 && H2phase == 1) {
                    phasedVars.push_back(refBase); phasedVars.push_back(altBase);
                } else if (H1phase == 1 && H2phase == 0) {
                    phasedVars.push_back(altBase); phasedVars.push_back(refBase);
                } else{
                    continue;
                }
                PhaseInfo* thisPhase = new PhaseInfo(snpPos,phaseQual,snpCoverage, phasedVars,1);
                positionToPhase[snpPos] = thisPhase;
            }
        }
    } else {
        while (getline(*hetFile, line)) {
            std::vector<string> phasedSNPdetails = split(line, '\t');
            int snpPos = atoi(phasedSNPdetails[1].c_str());
            phasedHetsPos.insert(snpPos);
        }
    }
    
    std::cerr << "Finished reading het sites. There are " << positionToPhase.size() << " hets." << std::endl;
    std::cerr << "Processing reads:" << std::endl;
    
    int readsProcessed = 0;
    // Now parse the pairtools file to find read pairs that can be informative about the phasing and recombination
    while (getline(*samtoolsFile,line)) {
        readsProcessed++;
        if (readsProcessed % 100000 == 0) {
            std::cerr << "Processed " << readsProcessed << " reads" << std::endl;
        }
        // std::cerr << line << std::endl;
        std::vector<string> samRecVec = split(line, '\t'); //assert(pairVec.size() == 8);
        int flag = atoi(samRecVec[1].c_str());
        if (flag > 2000) continue;
        
        int MQ = atoi(samRecVec[4].c_str());
        if (MQ < opt::minMQ) continue;
        
        
        RecombRead* thisRead = new RecombRead(samRecVec);
        
        thisRead->findHetsInRead(positionToPhase);
        
        if (thisRead->hetSites.size() > 0) {
            readNameToSamRecords[thisRead->readName].push_back(line);
        }
        delete thisRead;
    }
    
    for (std::map<string,std::vector<string>>::iterator it = readNameToSamRecords.begin(); it != readNameToSamRecords.end(); it++) {
        if (it->second.size() > 1) {
            std::cout << it->second[0] << std::endl;
            std::cout << it->second[1] << std::endl;
        }
    }
    
    return 0;
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
callq 0x8edba
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x50be6(%rip), %rsi     # 0xdf3a8
leaq 0x98(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x21904
leaq 0x98(%rsp), %rdi
pushq $0x8
popq %rsi
callq 0x42587
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x8978
leaq 0xc8(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
leaq 0x70(%rsp), %rcx
andl $0x0, (%rcx)
andq $0x0, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
andq $0x0, 0x20(%rcx)
leaq 0xd8(%rsp), %rcx
andl $0x0, (%rcx)
andq $0x0, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
andq $0x0, 0x20(%rcx)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
cmpb $0x0, 0x50b90(%rip)     # 0xdf3e8
je 0x8ea13
leaq 0x8(%rsp), %r12
leaq 0x20(%rsp), %r13
movq %rbx, %rdi
leaq 0x48(%rsp), %rsi
callq 0x80a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x8ea7a
movq 0x48(%rsp), %rax
movzbl (%rax), %ecx
cmpl $0x2a, %ecx
je 0x8e868
cmpl $0x42, %ecx
jne 0x8e89f
cmpb $0x4c, 0x1(%rax)
je 0x8e868
movq %r12, %r14
movq %r12, %rdi
leaq 0x48(%rsp), %rsi
pushq $0x9
popq %rdx
callq 0x40349
movq 0x8(%rsp), %rbp
movq 0x80(%rbp), %rdi
callq 0x86f0
movl %eax, 0x4(%rsp)
movq 0x20(%rbp), %rdi
callq 0x86f0
movl %eax, %r15d
movq 0x40(%rbp), %rdi
callq 0x86f0
cmpq $0x1, 0xa8(%rbp)
jne 0x8eccf
cmpq $0x1, 0xc8(%rbp)
jne 0x8ecc6
movl %eax, %r12d
movq 0xa0(%rbp), %rax
movb (%rax), %al
movb %al, 0x3(%rsp)
movq 0xc0(%rbp), %rax
movb (%rax), %al
movb %al, 0x2(%rsp)
movq 0x140(%rbp), %rsi
leaq 0x118(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rdx
callq 0x21904
movq %rbp, %rdi
callq 0x402aa
movsd %xmm0, 0x40(%rsp)
movq %rbp, %rdi
callq 0x8978
movq 0x8(%rsp), %rax
movq 0x160(%rax), %rdi
callq 0x86f0
movl %eax, %ebp
andq $0x0, 0x30(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movl %r12d, %eax
xorl $0x1, %eax
orl %r15d, %eax
jne 0x8e988
movq %r13, %rdi
leaq 0x3(%rsp), %rsi
callq 0x5504a
leaq 0x2(%rsp), %rsi
movq %r14, %r12
jmp 0x8e9a6
xorl $0x1, %r15d
orl %r12d, %r15d
jne 0x8e9fb
movq %r13, %rdi
leaq 0x2(%rsp), %rsi
callq 0x5504a
leaq 0x3(%rsp), %rsi
movq %r14, %r12
movq %r13, %rdi
callq 0x5504a
pushq $0x30
popq %rdi
callq 0x8490
movq %rax, %r15
movl 0x4(%rsp), %esi
movq %rax, %rdi
movsd 0x40(%rsp), %xmm0
movl %ebp, %edx
movq %r13, %rcx
pushq $0x1
popq %r8
callq 0x8cc7a
leaq 0x68(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq 0x8ccc8
movq %r15, (%rax)
movq %r13, %rdi
callq 0x55604
movq %r12, %rdi
callq 0x28a08
jmp 0x8e868
movq %r13, %rdi
callq 0x55604
movq %r14, %r12
movq %r14, %rdi
callq 0x28a08
jmp 0x8e868
leaq 0x48(%rsp), %r14
leaq 0x8(%rsp), %r15
pushq $0x9
popq %rbp
leaq 0x98(%rsp), %r12
leaq 0x20(%rsp), %r13
movq %rbx, %rdi
movq %r14, %rsi
callq 0x80a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x8ea7a
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x40349
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rdi
callq 0x86f0
movl %eax, 0x20(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x8b044
movq %r15, %rdi
callq 0x28a08
jmp 0x8ea2d
movq 0x4e567(%rip), %rdi     # 0xdcfe8
leaq 0x2371d(%rip), %rsi     # 0xb21a5
callq 0x8480
movq 0x90(%rsp), %rsi
movq %rax, %rdi
callq 0x8320
leaq 0x23728(%rip), %rsi     # 0xb21cc
movq %rax, %rdi
callq 0x8480
movq %rax, %rdi
callq 0x8230
movq 0x4e52d(%rip), %rdi     # 0xdcfe8
leaq 0x23711(%rip), %rsi     # 0xb21d3
callq 0x8480
movq %rax, %rdi
callq 0x8230
pushq $-0x1
popq %r13
pushq $0x1
popq %rbx
leaq 0x48(%rsp), %r15
leaq 0x8(%rsp), %r12
movl %ebx, %eax
xorl %edx, %edx
movl $0x186a0, %ecx          # imm = 0x186A0
divl %ecx
movl %eax, %ebp
movq 0x4e4dc(%rip), %rdi     # 0xdcfd0
movq %r15, %rsi
callq 0x80a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x8ec22
imull $0x186a0, %ebp, %eax    # imm = 0x186A0
addl %r13d, %eax
jne 0x8eb4d
movq 0x4e4c8(%rip), %rdi     # 0xdcfe8
leaq 0x11b7f(%rip), %rsi     # 0xa06a6
callq 0x8480
movq %rax, %rdi
movl %ebx, %esi
callq 0x8840
movq %rax, %rdi
leaq 0x236a5(%rip), %rsi     # 0xb21e5
callq 0x8480
movq %rax, %rdi
callq 0x8230
movq %r12, %rdi
movq %r15, %rsi
pushq $0x9
popq %rdx
callq 0x40349
movq 0x8(%rsp), %rbp
movq 0x20(%rbp), %rdi
callq 0x86f0
cmpl $0x7d0, %eax            # imm = 0x7D0
jg 0x8ec10
movq 0x80(%rbp), %rdi
callq 0x86f0
cmpl 0x4eaaa(%rip), %eax     # 0xdd630
jl 0x8ec10
movl $0x150, %edi            # imm = 0x150
callq 0x8490
movq %rax, %rbp
leaq 0x100(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x297dc
movq %rbp, %rdi
movq %r14, %rsi
callq 0x8cd16
movq %r14, %rdi
callq 0x28a08
movq %rbp, %rdi
leaq 0x68(%rsp), %rsi
callq 0x8f11e
movq 0x110(%rbp), %rax
cmpq 0x108(%rbp), %rax
je 0x8ebfb
movq %rbp, %rsi
addq $0x28, %rsi
leaq 0xd0(%rsp), %rdi
callq 0x3abd8
movq %rax, %rdi
movq %r15, %rsi
callq 0x2b0a4
movq %rbp, %rdi
callq 0x8ef80
movl $0x150, %esi            # imm = 0x150
movq %rbp, %rdi
callq 0x84a0
movq %r12, %rdi
callq 0x28a08
decl %r13d
incl %ebx
jmp 0x8eae0
movq 0xe8(%rsp), %r14
movq 0x4e387(%rip), %rbx     # 0xdcfb8
leaq 0xd8(%rsp), %r15
cmpq %r15, %r14
je 0x8ec84
movq 0x40(%r14), %rsi
movq 0x48(%r14), %rax
subq %rsi, %rax
cmpq $0x21, %rax
jb 0x8ec77
movq %rbx, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq 0x40(%r14), %rsi
addq $0x20, %rsi
movq %rbx, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq %r14, %rdi
callq 0x8340
movq %rax, %r14
jmp 0x8ec39
leaq 0xd0(%rsp), %rdi
callq 0x32f44
leaq 0x68(%rsp), %rdi
callq 0x8d40e
leaq 0x98(%rsp), %rdi
callq 0x8af9c
leaq 0x48(%rsp), %rdi
callq 0x8978
xorl %eax, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x22f75(%rip), %rdi     # 0xb1c42
jmp 0x8ecd6
leaq 0x22f0b(%rip), %rdi     # 0xb1be1
leaq 0x23485(%rip), %rsi     # 0xb2162
leaq 0x234a1(%rip), %rcx     # 0xb2185
pushq $0x5d
popq %rdx
callq 0x82f0
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x8978
jmp 0x8eda8
movq %rax, %rbx
jmp 0x8eda8
movq %rax, %rbx
pushq $0x30
popq %rsi
movq %r15, %rdi
callq 0x84a0
jmp 0x8ed5a
jmp 0x8ed81
movq %rax, %rbx
jmp 0x8ed30
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x28a08
movl $0x150, %esi            # imm = 0x150
movq %rbp, %rdi
callq 0x84a0
jmp 0x8ed71
jmp 0x8ed6e
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x8978
jmp 0x8ed71
jmp 0x8ed81
jmp 0x8ed57
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x55604
jmp 0x8ed71
jmp 0x8ed81
jmp 0x8ed6e
jmp 0x8ed81
jmp 0x8ed81
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x28a08
jmp 0x8ed84
jmp 0x8ed81
jmp 0x8ed81
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x32f44
leaq 0x68(%rsp), %rdi
callq 0x8d40e
leaq 0x98(%rsp), %rdi
callq 0x8af9c
leaq 0x48(%rsp), %rdi
callq 0x8978
movq %rbx, %rdi
callq 0x8870