// File: /workspace/github_cpp_makefile_star2_O2/millanek[P]evo/evo_agpToNewFasta.cpp
// Function: agpFastaMain(int, char**)
int agpFastaMain(int argc, char** argv) {
    
    parseAgpFastaOptions(argc, argv);
    
    std::cerr << "Generating a new genome sequence with rearrangements as indicated in an AGP file: " << opt::agpFile << std::endl;
    std::cerr << "from the original reference genome: " << opt::genomeFile << std::endl;
    
    // Read from the agp and reference genome files
    std::istream* agpFile = createReader(opt::agpFile.c_str());
    std::map<string, string> fastaSeqs = readMultiFastaToMap(opt::genomeFile);
    std::map<string, std::vector<std::vector<string> > > newAgpStructure = readAGPstructureToMap(agpFile);
    std::map<string, string> outputSeqs;
    
    for(std::map<string,std::vector<std::vector<string> > >::iterator it = newAgpStructure.begin(); it != newAgpStructure.end(); ++it) {
        string processedScaffold = it->first;
        std::vector<std::vector<string> > processedScaffoldStructure = it->second;
        outputSeqs[processedScaffold] = ""; outputSeqs[processedScaffold].reserve(50000000);
        
        for (std::vector<std::string>::size_type i = 0; i != processedScaffoldStructure.size(); i++) {
            string toAdd;
            if (processedScaffoldStructure[i][6] == "scaffold") {
                toAdd = HUNDRED_Ns;
            } else {
                string originScaffold = processedScaffoldStructure[i][5];
                int originStart = atoi(processedScaffoldStructure[i][6].c_str()); int originEnd = atoi(processedScaffoldStructure[i][7].c_str());
                int originLength = (originEnd - originStart) + 1;
                toAdd = fastaSeqs.at(originScaffold).substr(originStart-1,originLength);
                // If the sequence should be inverted, get its reverse complement
                if (processedScaffoldStructure[i][8] == "-") {
                    std::reverse(toAdd.begin(), toAdd.end());
                    // complement
                    for (std::string::size_type j = 0; j != toAdd.length(); j++) {
                        switch (toAdd[j]) {
                            case 'A': toAdd[j] = 'T'; break;
                            case 'T': toAdd[j] = 'A'; break;
                            case 'C': toAdd[j] = 'G'; break;
                            case 'G': toAdd[j] = 'C'; break;
                            case 'a': toAdd[j] = 't'; break;
                            case 't': toAdd[j] = 'a'; break;
                            case 'c': toAdd[j] = 'g'; break;
                            case 'g': toAdd[j] = 'c'; break;
                            default:
                                break;
                        }
                    }
                }
                std::cerr << "Added " << originScaffold << " coords: " << originStart << " " << originEnd << std::endl;
            }
            outputSeqs[processedScaffold].append(toAdd);
        }
    }
    
    for(std::map<string,std::vector<std::vector<string> > >::iterator it = newAgpStructure.begin(); it != newAgpStructure.end(); ++it) {
        string processedScaffold = it->first;
        std::cerr << "New scaffold " << processedScaffold << " length: " << outputSeqs[processedScaffold].length() << std::endl;
        std::cout << ">" << processedScaffold << std::endl;
        print80bpPerLineStdOut(std::cout, outputSeqs[processedScaffold]);
    }
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
callq 0x87d27
movq 0x55823(%rip), %rbx     # 0xdcfe8
leaq 0x295ad(%rip), %rsi     # 0xb0d79
movq %rbx, %rdi
callq 0x8480
leaq 0x5792d(%rip), %rsi     # 0xdf108
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
leaq 0x295da(%rip), %rsi     # 0xb0dcc
movq %rbx, %rdi
callq 0x8480
leaq 0x57927(%rip), %rsi     # 0xdf128
movq %rax, %rdi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq 0x578f0(%rip), %rsi     # 0xdf108
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 0x578d7(%rip), %rsi     # 0xdf128
leaq 0x98(%rsp), %rdi
callq 0x42a1e
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
callq 0x876a0
leaq 0xd0(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x70(%rsp), %rax
movq 0x10(%rax), %rbx
leaq 0x8(%rsp), %r15
leaq 0x118(%rsp), %r14
leaq 0x70(%rsp), %rax
cmpq %rax, %rbx
je 0x87b51
leaq 0x20(%rbx), %rsi
leaq 0x48(%rsp), %rdi
callq 0x8600
leaq 0x40(%rbx), %rsi
leaq 0x28(%rsp), %rdi
callq 0x2a906
movq %rbx, 0x40(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x3210e
movq %rax, %rdi
leaq 0x12536(%rip), %rsi     # 0x99e1f
callq 0x88f0
leaq 0xc8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x3210e
movl $0x2faf080, %esi        # imm = 0x2FAF080
movq %rax, %rdi
callq 0x8700
xorl %ebx, %ebx
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
subq %rcx, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
cmpq %rax, %rbx
je 0x87b2b
leaq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
andq $0x0, 0x10(%rsp)
movb $0x0, 0x18(%rsp)
imulq $0x18, %rbx, %r13
movq (%rcx,%r13), %rdi
movl $0xc0, %eax
addq %rax, %rdi
leaq 0x1bf6b(%rip), %rsi     # 0xa38c4
callq 0x2193f
testb %al, %al
je 0x87976
movq %r15, %rdi
leaq 0x29485(%rip), %rsi     # 0xb0df1
callq 0x88f0
jmp 0x87afe
movq 0x28(%rsp), %rax
movq (%rax,%r13), %rsi
movl $0xa0, %eax
addq %rax, %rsi
movq %r14, %rdi
callq 0x8600
movq 0x28(%rsp), %rax
movq (%rax,%r13), %rbp
movq 0xc0(%rbp), %rdi
callq 0x86f0
movl %eax, %r12d
movq 0xe0(%rbp), %rdi
callq 0x86f0
movl %eax, %ebp
leaq 0x98(%rsp), %rdi
movq %r14, %r15
movq %r14, %rsi
callq 0x87ec2
leaq 0x138(%rsp), %r14
movl %ebp, %ecx
subl %r12d, %ecx
incl %ecx
leal -0x1(%r12), %edx
movslq %edx, %rdx
movslq %ecx, %rcx
movq %r14, %rdi
movq %rax, %rsi
callq 0x84b0
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x8550
movq %r14, %rdi
callq 0x8978
movq 0x28(%rsp), %rax
movq (%rax,%r13), %rdi
movl $0x100, %eax            # imm = 0x100
addq %rax, %rdi
leaq 0x11036(%rip), %rsi     # 0x98a50
callq 0x2193f
testb %al, %al
movq %r15, %r14
je 0x87a98
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
addq %rdi, %rsi
callq 0x87f07
xorl %eax, %eax
cmpq 0x10(%rsp), %rax
je 0x87a98
movq 0x8(%rsp), %rcx
movzbl (%rcx,%rax), %edx
cmpl $0x41, %edx
je 0x87a8e
cmpl $0x43, %edx
je 0x87a82
cmpl $0x47, %edx
je 0x87a8a
cmpl $0x74, %edx
je 0x87a86
cmpl $0x61, %edx
je 0x87a76
cmpl $0x63, %edx
je 0x87a7e
cmpl $0x67, %edx
je 0x87a7a
cmpl $0x54, %edx
jne 0x87a93
movb $0x41, %dl
jmp 0x87a90
movb $0x74, %dl
jmp 0x87a90
movb $0x63, %dl
jmp 0x87a90
movb $0x67, %dl
jmp 0x87a90
movb $0x47, %dl
jmp 0x87a90
movb $0x61, %dl
jmp 0x87a90
movb $0x43, %dl
jmp 0x87a90
movb $0x54, %dl
movb %dl, (%rcx,%rax)
incq %rax
jmp 0x87a3a
movq 0x55549(%rip), %rdi     # 0xdcfe8
leaq 0x293b0(%rip), %rsi     # 0xb0e56
callq 0x8480
movq %rax, %rdi
movq %r14, %rsi
callq 0x8430
movq %rax, %rdi
leaq 0x2939d(%rip), %rsi     # 0xb0e5d
callq 0x8480
movq %rax, %rdi
movl %r12d, %esi
callq 0x8840
movq %rax, %rdi
leaq 0x2ab2d(%rip), %rsi     # 0xb2607
callq 0x8480
movq %rax, %rdi
movl %ebp, %esi
callq 0x8840
movq %rax, %rdi
callq 0x8230
movq %r14, %rdi
callq 0x8978
leaq 0x8(%rsp), %r15
leaq 0xc8(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x3210e
movq %rax, %rdi
movq %r15, %rsi
callq 0x8920
movq %r15, %rdi
callq 0x8978
incq %rbx
jmp 0x8790f
leaq 0x28(%rsp), %rdi
callq 0x29054
leaq 0x48(%rsp), %rdi
callq 0x8978
movq 0x40(%rsp), %rdi
callq 0x8340
movq %rax, %rbx
jmp 0x8789e
movq 0x80(%rsp), %r12
leaq 0x48(%rsp), %rbx
leaq 0xc8(%rsp), %rbp
movq 0x5544b(%rip), %r14     # 0xdcfb8
leaq 0xf8(%rsp), %r13
leaq 0x70(%rsp), %rax
cmpq %rax, %r12
je 0x87c42
leaq 0x20(%r12), %rsi
movq %rbx, %rdi
callq 0x8600
movq 0x55451(%rip), %rdi     # 0xdcfe8
leaq 0x292c9(%rip), %rsi     # 0xb0e67
callq 0x8480
movq %rax, %rdi
movq %rbx, %rsi
callq 0x8430
movq %rax, %rdi
leaq 0x1b912(%rip), %rsi     # 0xa34ca
callq 0x8480
movq %rax, %r15
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3210e
movq 0x8(%rax), %rsi
movq %r15, %rdi
callq 0x8320
movq %rax, %rdi
callq 0x8230
movq %r14, %rdi
leaq 0x16f29(%rip), %rsi     # 0x9eb12
callq 0x8480
movq %rax, %rdi
movq %rbx, %rsi
callq 0x8430
movq %rax, %rdi
callq 0x8230
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3210e
movq %r13, %rdi
movq %rax, %rsi
callq 0x8600
movq %r14, %rdi
movq %r13, %rsi
callq 0x426a5
movq %r13, %rdi
callq 0x8978
movq %rbx, %rdi
callq 0x8978
movq %r12, %rdi
callq 0x8340
movq %rax, %r12
jmp 0x87b75
leaq 0xc8(%rsp), %rdi
callq 0x33008
leaq 0x68(%rsp), %rdi
callq 0x2a588
leaq 0x98(%rsp), %rdi
callq 0x33008
xorl %eax, %eax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x87d12
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x8978
jmp 0x87d1f
movq %rax, %rbx
jmp 0x87d1f
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x8978
jmp 0x87cf1
jmp 0x87cb3
movq %rax, %rbx
jmp 0x87cfb
jmp 0x87cbf
movq %rax, %rbx
jmp 0x87ce7
movq %rax, %rbx
jmp 0x87cf1
jmp 0x87cc8
jmp 0x87ccd
movq %rax, %rbx
jmp 0x87cdd
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x8978
leaq 0x8(%rsp), %rdi
callq 0x8978
leaq 0x28(%rsp), %rdi
callq 0x29054
leaq 0x48(%rsp), %rdi
callq 0x8978
leaq 0xc8(%rsp), %rdi
callq 0x33008
leaq 0x68(%rsp), %rdi
callq 0x2a588
leaq 0x98(%rsp), %rdi
callq 0x33008
movq %rbx, %rdi
callq 0x8870