// File: /workspace/github_cpp_makefile_star2_O2/millanek[P]evo/evo_codingSeqs_fromGenomes.cpp
// Function: SeqFromGenomes(int, char**)
int SeqFromGenomes(int argc, char** argv) {
    parseSeqFromGenomesOptions(argc, argv);
    // Load up the annotation file
    std::ifstream* geneFile = new std::ifstream(opt::geneFile.c_str());
    Annotation wgAnnotation(geneFile, false);
    std::vector<std::vector<string> > annotation;
    
    std::cerr << "Getting subsequences defined in " << opt::geneFile << std::endl;
    
    std::vector<string> allGenomeFiles; string AAFileName;
    std::vector<string> allNewLabels;
    if (opt::genomeFile != "") {
        allGenomeFiles.push_back(opt::genomeFile);
        std::cerr << "from the genome in: " << stripExtension(opt::genomeFile) << std::endl;
    } else {
        std::ifstream* alignmentList = new std::ifstream(opt::genomeListFile.c_str());
        std::cerr << "from the genomes listed in: " << opt::genomeListFile << std::endl;
        std::string line;
        while (getline(*alignmentList, line)) {
            allGenomeFiles.push_back(line);
        }
    }
    if (opt::newLabelFile != "") {
        std::cerr << "using labels from: " << opt::newLabelFile << std::endl;
        std::ifstream* newLabels = new std::ifstream(opt::newLabelFile.c_str());
        std::string line;
        while (getline(*newLabels, line)) {
            allNewLabels.push_back(line);
        }
    }
    
    // Loop over the genome files and load all into memory (so can handle only a few genomes):
    std::vector<std::map<std::string,string>> allGenomes;
    for (std::vector<std::string>::size_type i = 0; i != allGenomeFiles.size(); i++) {
        std::ifstream* genome = new std::ifstream(allGenomeFiles[i].c_str());
        std::map<std::string,string> allSeqs;
        // Load the whole genome
        std::string scaffoldSeq; scaffoldSeq.reserve(20000000); scaffoldSeq = "";
        std::string line; std::string thisScaffold;
        while (getline(*genome, line)) {
            if (line[0] == '>') {
                if (scaffoldSeq != "") { allSeqs[thisScaffold] = scaffoldSeq;}
                thisScaffold = line.substr(1); scaffoldSeq = "";
                continue;
            }
            scaffoldSeq.append(line);
        } genome->close();
        allSeqs[thisScaffold] = scaffoldSeq; // The final scaffold
        std::cerr << "Loaded sequences from: " << allGenomeFiles[i] << std::endl;
        allGenomes.push_back(allSeqs);
    }
    
    for(std::map<std::string,string>::iterator itGenome = allGenomes[0].begin(); itGenome != allGenomes[0].end(); ++itGenome) {
        //std::cerr << itGenome->first << std::endl;
        
        annotation = wgAnnotation.annotationMap[itGenome->first]; // Get annotation for this scaffold
        if (annotation.size() == 0) continue; // if we don't have any, just move to the next scaffold
        
        // check that this scaffold is present in all genomes:
        bool scaffoldInAllGenomes = true;
        for (std::vector<std::map<std::string,string>>::size_type i = 1; i != allGenomes.size(); i++) {
            if (allGenomes[i].count(itGenome->first) == 0)
                scaffoldInAllGenomes = false;
        }
        if (!scaffoldInAllGenomes) continue; // if not, just move on to the next scaffold
        
        //std::cerr << "Going through the annotation..." << std::endl;
        for (std::vector<std::vector<string> >::size_type k = 0; k != annotation.size(); k++) {
            std::vector<string> annotLineVec = split(annotation[k][0], '\t');
            string thisGeneName = annotLineVec[4];
            // std::cerr << "Gene:" << thisGeneName << std::endl;
            string geneSequence = getReferenceForThisRegion(annotation[k], annotLineVec[3], itGenome->second);
            int geneLengthDivisibleByThree = geneSequence.length() % 3;
            
            std::ofstream* geneOutFiles;
            if (geneLengthDivisibleByThree == 0)
                geneOutFiles = new std::ofstream(thisGeneName.c_str());
            else
                geneOutFiles = new std::ofstream((opt::nonDivPrefix+'_'+thisGeneName).c_str());
            
            *geneOutFiles << ">" << stripExtension(allGenomeFiles[0]) << std::endl;
            *geneOutFiles << geneSequence << std::endl;
            for (std::vector<std::map<std::string,string>>::size_type i = 1; i != allGenomes.size(); i++) {
                *geneOutFiles << ">" << stripExtension(allGenomeFiles[i]) << std::endl;
                string geneSequence = getReferenceForThisRegion(annotation[k], annotLineVec[3], allGenomes[i][itGenome->first]);
                *geneOutFiles << geneSequence << std::endl;
            } geneOutFiles->close();
            //std::cerr << "Got all sequences for: " << thisGeneName << std::endl;
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
callq 0x31da3
movl $0x208, %edi            # imm = 0x208
callq 0x8490
movq %rax, %r14
movq 0xac7ca(%rip), %rsi     # 0xddb78
pushq $0x8
popq %rdx
movq %rax, %rdi
callq 0x8890
leaq 0x160(%rsp), %rsi
movq %r14, (%rsi)
leaq 0x188(%rsp), %rdi
xorl %edx, %edx
callq 0x32000
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
movq 0xabbfa(%rip), %rdi     # 0xdcfe8
leaq 0x67a26(%rip), %rsi     # 0x98e1b
callq 0x8480
leaq 0xac777(%rip), %rsi     # 0xddb78
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
andq $0x0, 0x70(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
leaq 0x178(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movaps %xmm0, 0x140(%rsp)
andq $0x0, 0x150(%rsp)
leaq 0xac6ad(%rip), %rdi     # 0xddaf8
leaq 0x689cd(%rip), %rsi     # 0x99e1f
callq 0x2ea9b
testb %al, %al
je 0x31562
leaq 0xac692(%rip), %rsi     # 0xddaf8
leaq 0x60(%rsp), %rdi
callq 0x2b0a4
movq 0xabb71(%rip), %rdi     # 0xdcfe8
leaq 0x679be(%rip), %rsi     # 0x98e3c
callq 0x8480
movq %rax, %rbx
leaq 0xac66b(%rip), %rsi     # 0xddaf8
leaq 0x10(%rsp), %rdi
callq 0x404d7
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
leaq 0x10(%rsp), %rdi
callq 0x8978
leaq 0xac67b(%rip), %rdi     # 0xddb38
leaq 0x6895b(%rip), %rsi     # 0x99e1f
callq 0x2ea9b
testb %al, %al
je 0x315fa
movq 0xabb10(%rip), %rdi     # 0xdcfe8
leaq 0x6798f(%rip), %rsi     # 0x98e6e
callq 0x8480
leaq 0xac64d(%rip), %rsi     # 0xddb38
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movl $0x208, %edi            # imm = 0x208
callq 0x8490
movq %rax, %r14
movq 0xac629(%rip), %rsi     # 0xddb38
pushq $0x8
popq %rdx
movq %rax, %rdi
callq 0x8890
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x10(%rsp), %rbx
leaq 0x140(%rsp), %r15
movq %r14, %rdi
movq %rbx, %rsi
callq 0x80a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x315f0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2b0a4
jmp 0x31538
movl $0x208, %edi            # imm = 0x208
callq 0x8490
movq %rax, %r14
movq 0xac5a2(%rip), %rsi     # 0xddb18
pushq $0x8
popq %rdx
movq %rax, %rdi
callq 0x8890
movq 0xaba60(%rip), %rdi     # 0xdcfe8
leaq 0x678c2(%rip), %rsi     # 0x98e51
callq 0x8480
leaq 0xac57d(%rip), %rsi     # 0xddb18
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x10(%rsp), %rbx
leaq 0x60(%rsp), %r15
movq %r14, %rdi
movq %rbx, %rsi
callq 0x80a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x314ac
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2b0a4
jmp 0x315c6
leaq 0x10(%rsp), %rdi
callq 0x8978
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0xf0(%rsp), %r14
leaq 0x98(%rsp), %r12
leaq 0x120(%rsp), %rbx
xorl %ecx, %ecx
movq 0x60(%rsp), %r15
movq 0x68(%rsp), %rax
subq %r15, %rax
sarq $0x5, %rax
movq %rcx, 0x8(%rsp)
cmpq %rax, %rcx
je 0x3183c
movl $0x208, %edi            # imm = 0x208
callq 0x8490
movq %rax, %rbp
movq 0x8(%rsp), %r13
shlq $0x5, %r13
movq (%r15,%r13), %rsi
movq %rax, %rdi
pushq $0x8
popq %rdx
callq 0x8890
andl $0x0, 0x18(%rsp)
andq $0x0, 0x20(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rax, 0x30(%rsp)
andq $0x0, 0x38(%rsp)
leaq 0x100(%rsp), %rax
movq %rax, 0xf0(%rsp)
andq $0x0, 0xf8(%rsp)
movb $0x0, 0x100(%rsp)
movl $0x1312d00, %esi        # imm = 0x1312D00
movq %r14, %rdi
callq 0x8700
leaq 0x68762(%rip), %r15     # 0x99e1f
movq %r14, %rdi
movq %r15, %rsi
callq 0x88f0
leaq 0xa8(%rsp), %rax
movq %rax, 0x98(%rsp)
andq $0x0, 0xa0(%rsp)
xorl %ecx, %ecx
movb %cl, 0xa8(%rsp)
leaq 0xe0(%rsp), %rax
movq %rax, 0xd0(%rsp)
andq $0x0, 0xd8(%rsp)
movb %cl, 0xe0(%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x80a0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x317a9
movq 0x98(%rsp), %rax
cmpb $0x3e, (%rax)
jne 0x31799
movq %r14, %rdi
movq %r15, %rsi
callq 0x2ea9b
testb %al, %al
je 0x31760
leaq 0x10(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x3210e
movq %rax, %rdi
movq %r14, %rsi
callq 0x82a0
movq %rbx, %rdi
movq %r12, %rsi
pushq $0x1
popq %rdx
pushq $-0x1
popq %rcx
callq 0x84b0
leaq 0xd0(%rsp), %rdi
movq %rbx, %rsi
callq 0x8550
movq %rbx, %rdi
callq 0x8978
movq %r14, %rdi
movq %r15, %rsi
callq 0x88f0
jmp 0x3170a
movq %r14, %rdi
movq %r12, %rsi
callq 0x8920
jmp 0x3170a
movq %rbp, %rdi
callq 0x80e0
leaq 0x10(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x3210e
movq %rax, %rdi
movq %r14, %rsi
callq 0x82a0
movq 0xab813(%rip), %rdi     # 0xdcfe8
leaq 0x676a6(%rip), %rsi     # 0x98e82
callq 0x8480
addq 0x60(%rsp), %r13
movq %rax, %rdi
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
leaq 0x40(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x32174
leaq 0xd0(%rsp), %rdi
callq 0x8978
movq %r12, %rdi
callq 0x8978
movq %r14, %rdi
callq 0x8978
leaq 0x10(%rsp), %rdi
callq 0x33008
movq 0x8(%rsp), %rcx
incq %rcx
jmp 0x31624
movq 0x40(%rsp), %rcx
movq 0x18(%rcx), %rax
leaq 0x98(%rsp), %r13
addq $0x8, %rcx
cmpq %rcx, %rax
je 0x31bb7
movq %rax, 0xb8(%rsp)
leaq 0x20(%rax), %rsi
leaq 0x188(%rsp), %rdi
movq %rsi, 0xc8(%rsp)
callq 0x2957c
leaq 0x80(%rsp), %rdi
movq %rax, %rsi
callq 0x295e2
movq 0x88(%rsp), %rax
cmpq 0x80(%rsp), %rax
je 0x31ba0
movb $0x1, %bl
pushq $0x30
popq %r14
pushq $0x1
popq %r15
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rax
subq %rdi, %rax
cqto
pushq $0x30
popq %rcx
idivq %rcx
cmpq %rax, %r15
je 0x318e6
addq %r14, %rdi
movq 0xc8(%rsp), %rsi
callq 0x321a0
testq %rax, %rax
setne %al
andb %al, %bl
incq %r15
addq $0x30, %r14
jmp 0x318ab
testb $0x1, %bl
je 0x31ba0
movq 0xb8(%rsp), %rax
addq $0x40, %rax
movq %rax, 0x110(%rsp)
xorl %edi, %edi
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %rax
subq %rcx, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
cmpq %rax, %rdi
je 0x31ba0
movq %rdi, 0x118(%rsp)
imulq $0x18, %rdi, %rax
movq %rax, 0xc0(%rsp)
movq (%rcx,%rax), %rsi
leaq 0x120(%rsp), %rdi
pushq $0x9
popq %rdx
callq 0x40349
movq 0x120(%rsp), %rsi
subq $-0x80, %rsi
leaq 0x10(%rsp), %rdi
callq 0x8600
movq 0x80(%rsp), %rsi
addq 0xc0(%rsp), %rsi
movq 0x120(%rsp), %rdx
addq $0x60, %rdx
leaq 0xf0(%rsp), %rdi
movq 0x110(%rsp), %rcx
callq 0x46a62
movq 0xf8(%rsp), %rax
xorl %edx, %edx
pushq $0x3
popq %rcx
divq %rcx
testq %rdx, %rdx
je 0x31a12
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rax, 0x8(%rsp)
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdi
leaq 0xac1ca(%rip), %rsi     # 0xddb98
pushq $0x5f
popq %rdx
callq 0x321ba
movq %r13, %rdi
movq %rbx, %rsi
leaq 0x10(%rsp), %rdx
callq 0x287e7
movq 0x98(%rsp), %rsi
movq 0x8(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x84e0
movq %r13, %rdi
callq 0x8978
leaq 0xd0(%rsp), %rdi
callq 0x8978
jmp 0x31a33
movl $0x200, %edi            # imm = 0x200
callq 0x8490
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x84e0
movq 0x8(%rsp), %rdi
leaq 0x6d0d3(%rip), %rsi     # 0x9eb12
callq 0x8480
movq %rax, %r15
movq 0x60(%rsp), %rsi
movq %r13, %rdi
callq 0x404d7
movq %r15, %rdi
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq %r13, %rdi
callq 0x8978
movq 0x8(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
pushq $0x20
popq %rbx
pushq $0x30
popq %rbp
pushq $0x1
popq %r12
movq 0x48(%rsp), %rax
subq 0x40(%rsp), %rax
cqto
pushq $0x30
popq %rcx
idivq %rcx
cmpq %rax, %r12
je 0x31b62
movq 0x8(%rsp), %rdi
leaq 0x6d058(%rip), %rsi     # 0x9eb12
callq 0x8480
movq %rax, %r15
movq 0x60(%rsp), %rsi
addq %rbx, %rsi
movq %r13, %rdi
callq 0x404d7
movq %r15, %rdi
movq %r13, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq %r13, %rdi
callq 0x8978
movq 0x80(%rsp), %r15
movq %r13, %r14
movq 0x120(%rsp), %r13
movq 0x40(%rsp), %rdi
addq %rbp, %rdi
movq 0xc8(%rsp), %rsi
callq 0x3210e
addq $0x60, %r13
addq 0xc0(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movq %r13, %rdx
movq %rax, %rcx
callq 0x46a62
movq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x8430
movq %r14, %r13
movq %rax, %rdi
callq 0x8230
movq %r13, %rdi
callq 0x8978
incq %r12
addq $0x30, %rbp
addq $0x20, %rbx
jmp 0x31a93
movq 0x8(%rsp), %rdi
callq 0x8620
leaq 0xf0(%rsp), %rdi
callq 0x8978
leaq 0x10(%rsp), %rdi
callq 0x8978
leaq 0x120(%rsp), %rdi
callq 0x28a08
movq 0x118(%rsp), %rdi
incq %rdi
jmp 0x31905
movq 0xb8(%rsp), %rdi
callq 0x8340
movq 0x40(%rsp), %rcx
jmp 0x3184d
leaq 0x40(%rsp), %rdi
callq 0x321f0
leaq 0x140(%rsp), %rdi
callq 0x28a08
leaq 0x168(%rsp), %rdi
callq 0x8978
leaq 0x60(%rsp), %rdi
callq 0x28a08
leaq 0x80(%rsp), %rdi
callq 0x29054
leaq 0x188(%rsp), %rdi
callq 0x32212
xorl %eax, %eax
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x31c19
jmp 0x31c47
jmp 0x31c47
movq %rax, %rbx
movl $0x208, %esi            # imm = 0x208
movq %r14, %rdi
callq 0x84a0
jmp 0x31d5d
jmp 0x31c70
movq %rax, %rbx
movl $0x208, %esi            # imm = 0x208
movq %r14, %rdi
callq 0x84a0
jmp 0x31d9b
jmp 0x31c47
movq %rax, %rbx
jmp 0x31d5d
movq %rax, %rbx
jmp 0x31d81
movq %rax, %rbx
movl $0x208, %esi            # imm = 0x208
movq %rbp, %rdi
callq 0x84a0
jmp 0x31d53
jmp 0x31cd7
jmp 0x31c70
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x8978
jmp 0x31d5d
jmp 0x31cd7
movq %rax, %rbx
jmp 0x31cfb
jmp 0x31c8b
movq %rax, %rbx
jmp 0x31cb2
movq %rax, %rbx
jmp 0x31ca5
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x8978
leaq 0xd0(%rsp), %rdi
callq 0x8978
movl $0x200, %esi            # imm = 0x200
movq 0x8(%rsp), %rdi
callq 0x84a0
jmp 0x31d2f
jmp 0x31cde
movq %rax, %rbx
jmp 0x31d46
jmp 0x31d2c
movq %rax, %rbx
jmp 0x31d3c
jmp 0x31cd7
jmp 0x31cde
jmp 0x31d1a
movq %rax, %rbx
jmp 0x31d53
jmp 0x31d2c
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x8978
leaq 0x98(%rsp), %rdi
callq 0x8978
leaq 0xf0(%rsp), %rdi
callq 0x8978
leaq 0x10(%rsp), %rdi
callq 0x33008
jmp 0x31d53
jmp 0x31d2c
jmp 0x31d2c
jmp 0x31d1a
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x8978
jmp 0x31d2f
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x8978
leaq 0x10(%rsp), %rdi
callq 0x8978
leaq 0x120(%rsp), %rdi
callq 0x28a08
leaq 0x40(%rsp), %rdi
callq 0x321f0
leaq 0x140(%rsp), %rdi
callq 0x28a08
leaq 0x168(%rsp), %rdi
callq 0x8978
leaq 0x60(%rsp), %rdi
callq 0x28a08
leaq 0x80(%rsp), %rdi
callq 0x29054
leaq 0x188(%rsp), %rdi
callq 0x32212
movq %rbx, %rdi
callq 0x8870