// File: /workspace/github_cpp_makefile_star2_O2/millanek[P]evo/process_vcf_variant_sharing.cpp
// Function: variantSharingMain(int, char**)
int variantSharingMain(int argc, char** argv) {
    parseVariantSharingOptions(argc, argv);
    std::ios_base::openmode mode = std::ios_base::in;
    string fileName = opt::vcfFile;
    string fileRoot = stripExtension(fileName);
    
    // For variant sharing between Massoko and Malawi
    bool bCountWithBlue = !opt::withBlueIndivFile.empty();
    MassokoMalawiResult sharingResult;
    
    // Data structures to hold individual or population identifiers (headers in the output file(s))
    std::vector<std::string> populations;
    std::vector<std::string> pop_unique;
    std::map<std::string,int> fieldsPopMap;
    
    std::cerr << "Analysing sharing of fixed variants between Massoko and Lake Malawi" << std::endl;
    std::cerr << "The ind file should be: sample_individuals_All.txt" << std::endl;
    std::ifstream* indFile = new std::ifstream(opt::withBlueIndivFile.c_str(), mode);
    string ind; 
    while (getline(*indFile, ind)) {
        populations.push_back(ind);
    }
    if (populations.size() != 46) {
        std::cerr << "It seems you supplied wrong file; the correct file should have 46 individuals" << std::endl;
        exit(EXIT_FAILURE);
    }
    populations.erase(populations.begin(), populations.begin()+12); // Remove the Massoko fish from this list of individuals
    sharingResult.init(populations.size());
    
    // Start reading from the vcf file
    std::ifstream* inFile = new std::ifstream(fileName.c_str(), mode);
    bool gotChromosomeNumber = false;
    int numChromosomes;
    string line;
    int totalVariantNumber = 0;
    while (getline(*inFile, line)) {
        if (line[0] != '#') { 
            totalVariantNumber++;
            FilterResult result;
            std::vector<std::string> fields = split(line, '\t');
            if (!gotChromosomeNumber) {
                const std::vector<std::string>::size_type numSamples = fields.size() - NUM_NON_GENOTYPE_COLUMNS;
                numChromosomes = numSamples * 2;
                std::cerr << "Number of chromosomes: " << numChromosomes << std::endl;
                gotChromosomeNumber = true;
            }
            result.overallQuality = atoi(fields[5].c_str());
            result.counts = getThisVariantCounts(fields);
            if (bCountWithBlue) {
                massokoMalawiSharing(result, sharingResult);
            }   
        }
    }
    
    // Print out the results
    std::cerr << "Getting ready to print" << std::endl;
    std::ios_base::openmode mode_out = std::ios_base::out;
    string shareFileName = fileRoot + ".sharing.txt";
    std::ofstream* pShareOutFile = new std::ofstream(shareFileName.c_str(), mode_out);
    *pShareOutFile << "\t";
    for (int i = 0; i < populations.size(); i++) {
        if (i == (populations.size()-1))
            *pShareOutFile << populations[i] << std::endl;
        else 
            *pShareOutFile << populations[i] << "\t";
    }
    *pShareOutFile << "MassokoFixedBlue(hom/het/absent):\t";
    for (int i = 0; i < populations.size(); i++) {
        if (i == (populations.size()-1))
            *pShareOutFile << sharingResult.homsWithBlue[i] << "/" << sharingResult.hetsWithBlue[i] << "/" << sharingResult.absentWithBlue[i] << std::endl;
        else 
            *pShareOutFile << sharingResult.homsWithBlue[i] << "/" << sharingResult.hetsWithBlue[i] << "/" << sharingResult.absentWithBlue[i] << "\t";
    }
    *pShareOutFile << "MassokoFixedYellow(hom/het/absent):\t";
    for (int i = 0; i < populations.size(); i++) {
        if (i == (populations.size()-1))
            *pShareOutFile << sharingResult.homsWithYellow[i] << "/" << sharingResult.hetsWithYellow[i] << "/" << sharingResult.absentWithYellow[i] << std::endl;
        else 
            *pShareOutFile << sharingResult.homsWithYellow[i] << "/" << sharingResult.hetsWithYellow[i] << "/" << sharingResult.absentWithYellow[i] << "\t";
    }
        
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x398, %rsp            # imm = 0x398
callq 0x4ed7d
leaq 0x8fc47(%rip), %rsi     # 0xde308
leaq 0xa8(%rsp), %rbx
movq %rbx, %rdi
callq 0x8600
leaq 0x88(%rsp), %rdi
movq %rbx, %rsi
callq 0x404d7
movq 0x8fc88(%rip), %r13     # 0xde370
leaq 0xc8(%rsp), %rdi
movl $0x90, %edx
xorl %esi, %esi
callq 0x82b0
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
leaq 0x160(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq 0x8e8b0(%rip), %rdi     # 0xdcfe8
leaq 0x53ccf(%rip), %rsi     # 0xa240e
callq 0x8480
movq %rax, %rdi
callq 0x8230
movq 0x8e895(%rip), %rdi     # 0xdcfe8
leaq 0x53cf8(%rip), %rsi     # 0xa2452
callq 0x8480
movq %rax, %rdi
callq 0x8230
movl $0x208, %edi            # imm = 0x208
callq 0x8490
movq %rax, %rbx
movq 0x8fbed(%rip), %rsi     # 0xde368
pushq $0x8
popq %rdx
movq %rax, %rdi
callq 0x8890
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x68(%rsp), %r14
movq %rsp, %r15
movq %rbx, %rdi
movq %r14, %rsi
callq 0x80a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x4e7c5
movq %r15, %rdi
movq %r14, %rsi
callq 0x2b0a4
jmp 0x4e79f
movq (%rsp), %rsi
movq 0x8(%rsp), %rax
subq %rsi, %rax
cmpq $0x5c0, %rax            # imm = 0x5C0
jne 0x4ec62
leaq 0x180(%rsi), %rdx
movq %rsp, %rdi
callq 0x4ef54
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
sarq $0x5, %rsi
leaq 0xc8(%rsp), %rdi
callq 0x4ef5a
movl $0x208, %edi            # imm = 0x208
callq 0x8490
movq %rax, %r15
movq 0xa8(%rsp), %rsi
pushq $0x8
popq %rdx
movq %rax, %rdi
callq 0x8890
leaq 0x58(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorl %ebx, %ebx
leaq 0x48(%rsp), %r14
leaq 0x298(%rsp), %rbp
movq %r15, %rdi
movq %r14, %rsi
callq 0x80a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x4e940
movq 0x48(%rsp), %rax
cmpb $0x23, (%rax)
je 0x4e846
leaq 0x188(%rsp), %rdi
callq 0x4a6c6
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
pushq $0x9
popq %rdx
callq 0x40349
testb $0x1, %bl
jne 0x4e8cb
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %rbx
movq 0x8e748(%rip), %rdi     # 0xdcfe8
leaq 0x51d7c(%rip), %rsi     # 0xa0623
callq 0x8480
subq %r12, %rbx
shrq $0x4, %rbx
andl $-0x2, %ebx
addl $-0x12, %ebx
movq %rax, %rdi
movl %ebx, %esi
callq 0x8840
movq %rax, %rdi
callq 0x8230
movq 0x18(%rsp), %rax
movq 0xa0(%rax), %rdi
callq 0x86f0
movl %eax, 0x188(%rsp)
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0x40d83
leaq 0x190(%rsp), %rdi
movq %rbp, %rsi
callq 0x4e5f6
movq %rbp, %rdi
callq 0x289b4
testq %r13, %r13
je 0x4e922
leaq 0x188(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x42234
leaq 0x18(%rsp), %rdi
callq 0x28a08
leaq 0x190(%rsp), %rdi
callq 0x289b4
movb $0x1, %bl
jmp 0x4e846
movq 0x8e6a1(%rip), %rdi     # 0xdcfe8
leaq 0x53b85(%rip), %rsi     # 0xa24d3
callq 0x8480
movq %rax, %rdi
callq 0x8230
leaq 0x53b88(%rip), %rdx     # 0xa24ea
leaq 0x188(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x28806
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rax, %rbx
movq 0x188(%rsp), %rsi
pushq $0x10
popq %rdx
movq %rax, %rdi
callq 0x84e0
leaq 0x4b480(%rip), %rsi     # 0x99e1e
movq %rbx, %rdi
callq 0x8480
xorl %r15d, %r15d
leaq 0x4b46e(%rip), %r14     # 0x99e1e
xorl %r12d, %r12d
movq (%rsp), %rsi
movq 0x8(%rsp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %r12, %rax
jbe 0x4ea01
decq %rax
addq %r15, %rsi
cmpq %rax, %r12
jne 0x4e9e5
movq %rbx, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
jmp 0x4e9f8
movq %rbx, %rdi
callq 0x8430
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
incq %r12
addq $0x20, %r15
jmp 0x4e9b3
leaq 0x53aef(%rip), %rsi     # 0xa24f7
movq %rbx, %rdi
callq 0x8480
xorl %r12d, %r12d
leaq 0x53b00(%rip), %r14     # 0xa251a
leaq 0x4b3fd(%rip), %r15     # 0x99e1e
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x5, %rax
cmpq %r12, %rax
jbe 0x4eaf4
decq %rax
movq 0xf8(%rsp), %rcx
movl (%rcx,%r12,4), %esi
cmpq %rax, %r12
jne 0x4ea9b
movq %rbx, %rdi
callq 0x8840
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
movq 0xe0(%rsp), %rcx
movl (%rcx,%r12,4), %esi
movq %rax, %rdi
callq 0x8840
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
movq 0xc8(%rsp), %rcx
movl (%rcx,%r12,4), %esi
movq %rax, %rdi
callq 0x8840
movq %rax, %rdi
callq 0x8230
jmp 0x4eaec
movq %rbx, %rdi
callq 0x8840
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
movq 0xe0(%rsp), %rcx
movl (%rcx,%r12,4), %esi
movq %rax, %rdi
callq 0x8840
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
movq 0xc8(%rsp), %rcx
movl (%rcx,%r12,4), %esi
movq %rax, %rdi
callq 0x8840
movq %rax, %rdi
movq %r15, %rsi
callq 0x8480
incq %r12
jmp 0x4ea21
leaq 0x53a21(%rip), %rsi     # 0xa251c
movq %rbx, %rdi
callq 0x8480
xorl %r12d, %r12d
leaq 0x53a0d(%rip), %r14     # 0xa251a
leaq 0x4b30a(%rip), %r15     # 0x99e1e
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x5, %rax
cmpq %r12, %rax
jbe 0x4ebe7
decq %rax
movq 0x140(%rsp), %rcx
movl (%rcx,%r12,4), %esi
cmpq %rax, %r12
jne 0x4eb8e
movq %rbx, %rdi
callq 0x8840
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
movq 0x128(%rsp), %rcx
movl (%rcx,%r12,4), %esi
movq %rax, %rdi
callq 0x8840
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
movq 0x110(%rsp), %rcx
movl (%rcx,%r12,4), %esi
movq %rax, %rdi
callq 0x8840
movq %rax, %rdi
callq 0x8230
jmp 0x4ebdf
movq %rbx, %rdi
callq 0x8840
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
movq 0x128(%rsp), %rcx
movl (%rcx,%r12,4), %esi
movq %rax, %rdi
callq 0x8840
movq %rax, %rdi
movq %r14, %rsi
callq 0x8480
movq 0x110(%rsp), %rcx
movl (%rcx,%r12,4), %esi
movq %rax, %rdi
callq 0x8840
movq %rax, %rdi
movq %r15, %rsi
callq 0x8480
incq %r12
jmp 0x4eb14
leaq 0x188(%rsp), %rdi
callq 0x8978
leaq 0x48(%rsp), %rdi
callq 0x8978
leaq 0x68(%rsp), %rdi
callq 0x8978
leaq 0x158(%rsp), %rdi
callq 0x32fbe
leaq 0x30(%rsp), %rdi
callq 0x28a08
movq %rsp, %rdi
callq 0x28a08
leaq 0xc8(%rsp), %rdi
callq 0x4effc
leaq 0x88(%rsp), %rdi
callq 0x8978
leaq 0xa8(%rsp), %rdi
callq 0x8978
xorl %eax, %eax
addq $0x398, %rsp            # imm = 0x398
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8e37f(%rip), %rdi     # 0xdcfe8
leaq 0x53815(%rip), %rsi     # 0xa2485
callq 0x8480
movq %rax, %rdi
callq 0x8230
pushq $0x1
popq %rdi
callq 0x8540
jmp 0x4ecd6
jmp 0x4ecd6
movq %rax, %r14
jmp 0x4ece3
jmp 0x4ecfd
movq %rax, %r14
movl $0x200, %esi            # imm = 0x200
movq %rbx, %rdi
callq 0x84a0
jmp 0x4ed0e
jmp 0x4ecfd
movq %rax, %r14
movl $0x208, %esi            # imm = 0x208
movq %r15, %rdi
callq 0x84a0
jmp 0x4ed25
jmp 0x4ed04
jmp 0x4ed04
movq %rax, %r14
movl $0x208, %esi            # imm = 0x208
movq %rbx, %rdi
callq 0x84a0
jmp 0x4ed2f
jmp 0x4ecf8
movq %rax, %r14
jmp 0x4ed68
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x28a08
leaq 0x190(%rsp), %rdi
callq 0x289b4
jmp 0x4ed1b
jmp 0x4ed04
jmp 0x4ecfd
jmp 0x4ed0b
movq %rax, %r14
jmp 0x4ed2f
movq %rax, %r14
jmp 0x4ed1b
jmp 0x4ed0b
movq %rax, %r14
jmp 0x4ed25
jmp 0x4ed0b
movq %rax, %r14
leaq 0x188(%rsp), %rdi
callq 0x8978
leaq 0x48(%rsp), %rdi
callq 0x8978
leaq 0x68(%rsp), %rdi
callq 0x8978
leaq 0x158(%rsp), %rdi
callq 0x32fbe
leaq 0x30(%rsp), %rdi
callq 0x28a08
movq %rsp, %rdi
callq 0x28a08
leaq 0xc8(%rsp), %rdi
callq 0x4effc
leaq 0x88(%rsp), %rdi
callq 0x8978
leaq 0xa8(%rsp), %rdi
callq 0x8978
movq %r14, %rdi
callq 0x8870