// File: 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
movq %rsp, %rbp
subq $0x350, %rsp            # imm = 0x350
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movl -0x4(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x112e40
movq 0x86c4b(%rip), %rdi     # 0x198ff0
movq %rdi, -0x230(%rbp)
leaq 0x3b215(%rip), %rsi     # 0x14d5c8
callq 0xa5c0
movq %rax, %rdi
leaq 0x89206(%rip), %rsi     # 0x19b5c8
movq %rsi, -0x220(%rbp)
callq 0xa520
movq %rax, %rdi
movq 0x86bb0(%rip), %rsi     # 0x198f88
movq %rsi, -0x228(%rbp)
callq 0xa610
movq -0x230(%rbp), %rdi
leaq 0x3b229(%rip), %rsi     # 0x14d61b
callq 0xa5c0
movq %rax, %rdi
leaq 0x891e7(%rip), %rsi     # 0x19b5e8
callq 0xa520
movq -0x228(%rbp), %rsi
movq %rax, %rdi
callq 0xa610
movq -0x220(%rbp), %rdi
callq 0xa220
movq %rax, -0x218(%rbp)
leaq -0x39(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0xab60
movq -0x218(%rbp), %rsi
movq -0x210(%rbp), %rdx
leaq -0x38(%rbp), %rdi
callq 0x2d6c0
jmp 0x112451
leaq -0x38(%rbp), %rdi
movl $0x8, %esi
callq 0x80e30
movq %rax, -0x238(%rbp)
jmp 0x112468
leaq -0x38(%rbp), %rdi
callq 0xac88
leaq -0x39(%rbp), %rdi
callq 0xa640
movq -0x238(%rbp), %rax
movq %rax, -0x18(%rbp)
leaq 0x8915c(%rip), %rsi     # 0x19b5e8
leaq -0x80(%rbp), %rdi
callq 0x817c0
movq -0x18(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x1121d0
jmp 0x1124a7
leaq -0xe0(%rbp), %rdi
callq 0x584b0
leaq -0xb0(%rbp), %rdi
callq 0x59460
movq %rax, -0xe8(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x42260
movq %rax, -0xf0(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x90110
testb $0x1, %al
jne 0x1124f5
jmp 0x112be4
leaq -0xe8(%rbp), %rdi
callq 0x90140
movq %rax, %rsi
leaq -0x110(%rbp), %rdi
callq 0xa250
jmp 0x112512
leaq -0xe8(%rbp), %rdi
callq 0x90140
movq %rax, %rsi
addq $0x20, %rsi
leaq -0x128(%rbp), %rdi
callq 0x45190
jmp 0x112533
leaq -0xe0(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x584d0
movq %rax, -0x240(%rbp)
jmp 0x11254f
movq -0x240(%rbp), %rdi
leaq 0x23982(%rip), %rsi     # 0x135edf
callq 0xa810
jmp 0x112564
leaq -0xe0(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x584d0
movq %rax, -0x248(%rbp)
jmp 0x112580
movq -0x248(%rbp), %rdi
movl $0x2faf080, %esi        # imm = 0x2FAF080
callq 0xa980
jmp 0x112593
movq $0x0, -0x130(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0x250(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x409f0
movq %rax, %rcx
movq -0x250(%rbp), %rax
cmpq %rcx, %rax
je 0x112b9e
leaq -0x150(%rbp), %rdi
callq 0xa820
movq -0x130(%rbp), %rsi
leaq -0x128(%rbp), %rdi
callq 0x3ec10
movq %rax, %rdi
movl $0x6, %esi
callq 0x3d940
movq %rax, %rdi
leaq 0x2d897(%rip), %rsi     # 0x13fe98
callq 0x2d770
movb %al, -0x251(%rbp)
jmp 0x11260e
movb -0x251(%rbp), %al
testb $0x1, %al
jne 0x11261d
jmp 0x1126bd
leaq 0x3b01c(%rip), %rsi     # 0x14d640
leaq -0x150(%rbp), %rdi
callq 0xa810
jmp 0x112632
jmp 0x112b3c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x11265a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x38(%rbp), %rdi
callq 0xac88
leaq -0x39(%rbp), %rdi
callq 0xa640
jmp 0x112e29
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x112e20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x112e08
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x112bd3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x112bc7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x112b90
movq -0x130(%rbp), %rsi
leaq -0x128(%rbp), %rdi
callq 0x3ec10
movq %rax, %rdi
movl $0x5, %esi
callq 0x3d940
movq %rax, %rsi
leaq -0x170(%rbp), %rdi
callq 0xa250
jmp 0x1126ee
movq -0x130(%rbp), %rsi
leaq -0x128(%rbp), %rdi
movq %rdi, -0x268(%rbp)
callq 0x3ec10
movq %rax, %rdi
movl $0x6, %esi
callq 0x3d940
movq %rax, %rdi
callq 0xa220
movq %rax, %rdi
callq 0xa970
movq -0x268(%rbp), %rdi
movl %eax, -0x174(%rbp)
movq -0x130(%rbp), %rsi
callq 0x3ec10
movq %rax, %rdi
movl $0x7, %esi
callq 0x3d940
movq %rax, %rdi
callq 0xa220
movq %rax, %rdi
callq 0xa970
movl %eax, -0x178(%rbp)
movl -0x178(%rbp), %eax
movl -0x174(%rbp), %ecx
subl %ecx, %eax
incl %eax
movl %eax, -0x17c(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x1130e0
movq %rax, -0x260(%rbp)
jmp 0x112790
movq -0x260(%rbp), %rsi
movl -0x174(%rbp), %eax
decl %eax
movslq %eax, %rdx
movslq -0x17c(%rbp), %rcx
leaq -0x1a0(%rbp), %rdi
callq 0xa5f0
jmp 0x1127b7
leaq -0x150(%rbp), %rdi
leaq -0x1a0(%rbp), %rsi
movq %rsi, -0x278(%rbp)
callq 0xa710
movq -0x278(%rbp), %rdi
callq 0xac88
movq -0x130(%rbp), %rsi
leaq -0x128(%rbp), %rdi
callq 0x3ec10
movq %rax, %rdi
movl $0x8, %esi
callq 0x3d940
movq %rax, %rdi
leaq 0x22309(%rip), %rsi     # 0x134b10
callq 0x2d770
movb %al, -0x269(%rbp)
jmp 0x112814
movb -0x269(%rbp), %al
testb $0x1, %al
jne 0x112823
jmp 0x112a75
leaq -0x150(%rbp), %rdi
movq %rdi, -0x280(%rbp)
callq 0xa4f0
movq -0x280(%rbp), %rdi
movq %rax, -0x1a8(%rbp)
callq 0xa330
movq %rax, -0x1b0(%rbp)
movq -0x1a8(%rbp), %rdi
movq -0x1b0(%rbp), %rsi
callq 0x113180
jmp 0x112865
movq $0x0, -0x1b8(%rbp)
movq -0x1b8(%rbp), %rax
movq %rax, -0x288(%rbp)
leaq -0x150(%rbp), %rdi
callq 0xa930
movq %rax, %rcx
movq -0x288(%rbp), %rax
cmpq %rcx, %rax
je 0x112a73
movq -0x1b8(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0xac60
movq %rax, -0x290(%rbp)
jmp 0x1128b9
movq -0x290(%rbp), %rax
movsbl (%rax), %eax
addl $-0x41, %eax
movl %eax, %ecx
movq %rcx, -0x298(%rbp)
subl $0x33, %eax
ja 0x112a58
movq -0x298(%rbp), %rax
leaq 0x3ac12(%rip), %rcx     # 0x14d4f8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x170(%rbp), %rdi
callq 0xac88
jmp 0x112b90
movq -0x1b8(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0xac60
movq %rax, -0x2a0(%rbp)
jmp 0x112928
movq -0x2a0(%rbp), %rax
movb $0x54, (%rax)
jmp 0x112a5a
movq -0x1b8(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0xac60
movq %rax, -0x2a8(%rbp)
jmp 0x112953
movq -0x2a8(%rbp), %rax
movb $0x41, (%rax)
jmp 0x112a5a
movq -0x1b8(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0xac60
movq %rax, -0x2b0(%rbp)
jmp 0x11297e
movq -0x2b0(%rbp), %rax
movb $0x47, (%rax)
jmp 0x112a5a
movq -0x1b8(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0xac60
movq %rax, -0x2b8(%rbp)
jmp 0x1129a9
movq -0x2b8(%rbp), %rax
movb $0x43, (%rax)
jmp 0x112a5a
movq -0x1b8(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0xac60
movq %rax, -0x2c0(%rbp)
jmp 0x1129d4
movq -0x2c0(%rbp), %rax
movb $0x74, (%rax)
jmp 0x112a5a
movq -0x1b8(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0xac60
movq %rax, -0x2c8(%rbp)
jmp 0x1129fc
movq -0x2c8(%rbp), %rax
movb $0x61, (%rax)
jmp 0x112a5a
movq -0x1b8(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0xac60
movq %rax, -0x2d0(%rbp)
jmp 0x112a24
movq -0x2d0(%rbp), %rax
movb $0x67, (%rax)
jmp 0x112a5a
movq -0x1b8(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0xac60
movq %rax, -0x2d8(%rbp)
jmp 0x112a4c
movq -0x2d8(%rbp), %rax
movb $0x63, (%rax)
jmp 0x112a5a
jmp 0x112a5a
jmp 0x112a5c
movq -0x1b8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1b8(%rbp)
jmp 0x112870
jmp 0x112a75
movq 0x86574(%rip), %rdi     # 0x198ff0
leaq 0x3ac22(%rip), %rsi     # 0x14d6a5
callq 0xa5c0
movq %rax, -0x2e0(%rbp)
jmp 0x112a91
movq -0x2e0(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0xa520
movq %rax, -0x2e8(%rbp)
jmp 0x112aad
movq -0x2e8(%rbp), %rdi
leaq 0x3abf1(%rip), %rsi     # 0x14d6ac
callq 0xa5c0
movq %rax, -0x2f0(%rbp)
jmp 0x112ac9
movq -0x2f0(%rbp), %rdi
movl -0x174(%rbp), %esi
callq 0xab20
movq %rax, -0x2f8(%rbp)
jmp 0x112ae4
movq -0x2f8(%rbp), %rdi
leaq 0x3c350(%rip), %rsi     # 0x14ee42
callq 0xa5c0
movq %rax, -0x300(%rbp)
jmp 0x112b00
movq -0x300(%rbp), %rdi
movl -0x178(%rbp), %esi
callq 0xab20
movq %rax, -0x308(%rbp)
jmp 0x112b1b
movq -0x308(%rbp), %rdi
movq 0x8645f(%rip), %rsi     # 0x198f88
callq 0xa610
jmp 0x112b30
leaq -0x170(%rbp), %rdi
callq 0xac88
leaq -0xe0(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x584d0
movq %rax, -0x310(%rbp)
jmp 0x112b58
movq -0x310(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0xac20
jmp 0x112b6d
leaq -0x150(%rbp), %rdi
callq 0xac88
movq -0x130(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x130(%rbp)
jmp 0x11259e
leaq -0x150(%rbp), %rdi
callq 0xac88
jmp 0x112bc7
leaq -0x128(%rbp), %rdi
callq 0x3ec50
leaq -0x110(%rbp), %rdi
callq 0xac88
leaq -0xe8(%rbp), %rdi
callq 0x43240
jmp 0x1124c6
leaq -0x128(%rbp), %rdi
callq 0x3ec50
leaq -0x110(%rbp), %rdi
callq 0xac88
jmp 0x112e08
leaq -0xb0(%rbp), %rdi
callq 0x59460
movq %rax, -0x1c0(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x42260
movq %rax, -0x1c8(%rbp)
leaq -0x1c0(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x90110
testb $0x1, %al
jne 0x112c26
jmp 0x112ddc
leaq -0x1c0(%rbp), %rdi
callq 0x90140
movq %rax, %rsi
leaq -0x1e8(%rbp), %rdi
callq 0xa250
jmp 0x112c43
movq 0x863a6(%rip), %rdi     # 0x198ff0
leaq 0x3aa65(%rip), %rsi     # 0x14d6b6
callq 0xa5c0
movq %rax, -0x318(%rbp)
jmp 0x112c5f
movq -0x318(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0xa520
movq %rax, -0x320(%rbp)
jmp 0x112c7b
movq -0x320(%rbp), %rdi
leaq 0x2ce15(%rip), %rsi     # 0x13fa9e
callq 0xa5c0
movq %rax, -0x328(%rbp)
jmp 0x112c97
leaq -0xe0(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x584d0
movq %rax, -0x330(%rbp)
jmp 0x112cb3
movq -0x330(%rbp), %rdi
callq 0xa930
movq -0x328(%rbp), %rdi
movq %rax, %rsi
callq 0xa140
movq %rax, -0x338(%rbp)
jmp 0x112cd7
movq -0x338(%rbp), %rdi
movq 0x862a3(%rip), %rsi     # 0x198f88
callq 0xa610
jmp 0x112cec
movq 0x862c5(%rip), %rdi     # 0x198fb8
leaq 0x27f2c(%rip), %rsi     # 0x13ac26
callq 0xa5c0
movq %rax, -0x340(%rbp)
jmp 0x112d08
movq -0x340(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0xa520
movq %rax, -0x348(%rbp)
jmp 0x112d24
movq -0x348(%rbp), %rdi
movq 0x86256(%rip), %rsi     # 0x198f88
callq 0xa610
jmp 0x112d39
leaq -0xe0(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x584d0
movq %rax, -0x350(%rbp)
jmp 0x112d55
movq -0x350(%rbp), %rsi
leaq -0x208(%rbp), %rdi
callq 0xa250
jmp 0x112d6a
movq 0x86247(%rip), %rdi     # 0x198fb8
leaq -0x208(%rbp), %rsi
callq 0x81090
jmp 0x112d7f
leaq -0x208(%rbp), %rdi
callq 0xac88
leaq -0x1e8(%rbp), %rdi
callq 0xac88
leaq -0x1c0(%rbp), %rdi
callq 0x43240
jmp 0x112bf7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x112dce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x208(%rbp), %rdi
callq 0xac88
leaq -0x1e8(%rbp), %rdi
callq 0xac88
jmp 0x112e08
leaq -0xe0(%rbp), %rdi
callq 0x58620
leaq -0xb0(%rbp), %rdi
callq 0x3f5b0
leaq -0x80(%rbp), %rdi
callq 0x58620
xorl %eax, %eax
addq $0x350, %rsp            # imm = 0x350
popq %rbp
retq
leaq -0xe0(%rbp), %rdi
callq 0x58620
leaq -0xb0(%rbp), %rdi
callq 0x3f5b0
leaq -0x80(%rbp), %rdi
callq 0x58620
movq -0x48(%rbp), %rdi
callq 0xab50
nopw %cs:(%rax,%rax)