// File: /workspace/github_cpp_makefile_star2_O2/superyuan[P]refeditor/MappingConvertor.cpp
// Function: main
int main(int argc, char *argv[])
{
	if (parseParameters (argc, argv) == false) 	{
		cerr << "Version: 1.0-r7" << endl;
		cerr << "Contact: shuaiyuan.emory@gmail.com" << endl << endl;	
		cerr << "Usage: " << argv[0] <<" -i input.sam -m dipmap -o output.sam" << endl << endl;
		cerr << "Convert the sam file created from being mapped to a diploid reference genome " << endl;
		cerr << "  to being mapped to a haploid reference genome according to a dipmap file." << endl << endl;
		cerr << "Example: " << argv[0] << " -i input.sam -m hg19.di.fa.dipmap -o output.sam" << endl << endl;
		cerr << "Parameters (mandatory): " << endl;
		cerr << "  -i\t\tinput SAM file " << endl;
		cerr << "  -m\t\tdipmap file created by DiploidConstructor" << endl;
		cerr << "  -o\t\toutput SAM file" << endl;
		exit(-1);
	}

	CLineFields inputSamFile;
	if (inputSamFile.openFile(inputSamFileName)==false) {
		cerr << "Can not open sam file: " << inputSamFileName << endl;
		exit (-1);
	}
	
	ofstream outputSamFile;
	outputSamFile.open(outputSamFileName);

	// process the map file
	readMapFile(dipmapFileName);
	
	// process sam file
	inputSamFile.readline(removeHeader);
	vector<string> locations;
	while (inputSamFile.endofFile()==false) {
		if (inputSamFile.field[0][0]=='@' || inputSamFile.field[0][0]=='#') {// it is the header
			outputSamFile << inputSamFile.line << endl;
		} else {
			unsigned int flag = atoi(inputSamFile.field[1].c_str());
			if (flag & 4) {
				// the flag indicates unmapped read, so we set mapping quality to zero.
				inputSamFile.field[4] = "0";
			}
			// convert  (chr3b.300, 2)  to (chr3, 301)
			GenomePosition gp = GenomeLib::toRef(inputSamFile.field[2], inputSamFile.field[3], removeB);
			readLength = inputSamFile.field[9].length();
			// map from the extended reference allele minichromes to the real position on reference allelel.
            mapExtendedChrome(gp);
			int mm = 0;
			if (inputSamFile.field.size()>=14)
				mm = atoi(inputSamFile.field[13].substr(5).c_str());

			locations.clear();
			if (inputSamFile.field[inputSamFile.field.size()-1].substr(0,5)=="XA:Z:") { 
				// mapping quality==0 and we have chance to fix it                
    			inputSamFile.divide(inputSamFile.field[inputSamFile.field.size()-1].substr(5),";",locations);

				for (int i=0; i<(int)locations.size(); i++) {
					if(locations[i].length()==0) {
						locations.erase(locations.begin()+i);
						i=-1;//invalidate the for loop.
						continue;
					}
					vector<string> f;
					inputSamFile.divide(locations[i],",",f);
					if (f.size()<4)
						break; //skip reads with positions like:  chr3;.28387,+24,36M,2

					GenomePosition gp2 = GenomeLib::toRef(f[0], f[1], removeB);
					
					// map from the extended reference allele minichromes to the real position on reference allelel.
					mapExtendedChrome(gp2);					

					int new_mis = atoi(f[3].c_str());
					if (gp2.allele != 0) { // on non-ref alleles, maybe only partially cover the allele.
						int more_mis = abs(atoi(f[1].c_str()))-readLength;
						if (more_mis>0)
							new_mis += more_mis;  // the tail of the read is outside of the mini-chromosome.
					}
					if ((gp.chrName==gp2.chrName && gp.pos==gp2.pos) || new_mis != mm) {
						// eg. the read is mapped to both (chr1, 201) and (chr1b.200, 2).
						// or the mismatch != the first hit
						if (new_mis < mm) {
							// this hit is better than the first hit, use this to replace the first hit.
							gp = gp2;
							mm=new_mis;
						}
						locations.erase(locations.begin()+i);
						i=-1;//invalidate the for loop.
					}						
				}
				if (locations.empty()) {
					if (mm/readLength < 0.06)
						inputSamFile.field[4]="37"; // make up the mapping quality
					else
						inputSamFile.field[4]="25"; // make up the mapping quality
					flag &= ~4; //clear the "unmapped read bit"
					if (gp.strand<0)
						flag |= 16; //negtive strand;
				}
			}
			outputSamFile << inputSamFile.field[0] << "\t" << flag << "\t" << gp.chrName << "\t" << gp.pos;
			for (int i=4, size=inputSamFile.field.size(); i<size; i++) {
				outputSamFile << "\t" << inputSamFile.field[i];
			}
			outputSamFile << endl;
		}
		inputSamFile.readline(removeHeader);
	}
	inputSamFile.fp->close();
	outputSamFile.close();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x2a1f(%rip), %r14      # 0x520f
leaq 0x5a29(%rip), %r15      # 0x8220
movq 0x57c2(%rip), %r12      # 0x7fc0
leaq 0x5b1b(%rip), %r13      # 0x8320
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2080
movq %r15, %rdi
cmpl $0x69, %eax
je 0x283f
cmpl $0x6d, %eax
je 0x2838
cmpl $0x6f, %eax
je 0x2833
cmpl $-0x1, %eax
je 0x284a
cmpl $0x3f, %eax
jne 0x2805
jmp 0x2eb6
movq %r13, %rdi
jmp 0x283f
leaq 0x5be1(%rip), %rdi      # 0x8420
movq (%r12), %rsi
callq 0x21d0
jmp 0x2805
cmpb $0x0, 0x59cf(%rip)      # 0x8220
je 0x2eb6
cmpb $0x0, 0x5bc2(%rip)      # 0x8420
je 0x2eb6
cmpb $0x0, 0x5ab5(%rip)      # 0x8320
je 0x2eb6
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
callq 0x3d28
leaq 0x599b(%rip), %rsi      # 0x8220
movq %rbx, %rdi
callq 0x3c7c
testb %al, %al
je 0x2fff
leaq 0x228(%rsp), %rdi
callq 0x2330
leaq 0x5a77(%rip), %rsi      # 0x8320
leaq 0x228(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x2040
leaq 0x5b60(%rip), %rdi      # 0x8420
callq 0x254c
movb 0x5c55(%rip), %bl       # 0x8520
leaq 0x2766(%rip), %rsi      # 0x5038
leaq 0xe8(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0x3172
movzbl %bl, %esi
leaq 0x48(%rsp), %rdi
leaq 0xe8(%rsp), %rdx
callq 0x3d58
leaq 0xe8(%rsp), %rdi
callq 0x2100
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x48(%rsp), %r12
movq %r12, %rdi
callq 0x3d44
testb %al, %al
jne 0x2e6a
movq 0x48(%rsp), %r15
movq (%r15), %rax
movzbl (%rax), %eax
cmpl $0x40, %eax
je 0x2943
cmpl $0x23, %eax
jne 0x2962
leaq 0x228(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x21c0
movq %rax, %rdi
callq 0x2110
jmp 0x2e2a
movq 0x20(%r15), %rdi
callq 0x22e0
movl %eax, 0xc(%rsp)
testb $0x4, %al
je 0x298b
subq $-0x80, %r15
movq %r15, %rdi
leaq 0x287c(%rip), %rsi      # 0x51fd
callq 0x2390
movq 0x48(%rsp), %r15
addq $0x40, %r15
leaq 0x168(%rsp), %rdi
movq %r15, %rsi
callq 0x2290
movq 0x48(%rsp), %rsi
addq $0x60, %rsi
leaq 0x208(%rsp), %r15
movq %r15, %rdi
callq 0x2290
movzbl 0x5829(%rip), %ecx      # 0x81e8
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
leaq 0x168(%rsp), %r14
movq %r14, %rsi
movq %r15, %rdx
callq 0x3a34
movq %r15, %rdi
callq 0x2100
movq %r14, %rdi
callq 0x2100
movq 0x48(%rsp), %rax
movl 0x128(%rax), %eax
movl %eax, 0x5b26(%rip)      # 0x8524
movq %rbx, %rdi
callq 0x2738
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rax
subq %rsi, %rax
movl $0x0, 0x8(%rsp)
cmpq $0x1a1, %rax            # imm = 0x1A1
jb 0x2a59
addq $0x1a0, %rsi            # imm = 0x1A0
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
pushq $0x5
popq %rdx
pushq $-0x1
popq %rcx
callq 0x2210
movq 0x88(%rsp), %rdi
callq 0x22e0
movl %eax, 0x8(%rsp)
movq %rbx, %rdi
callq 0x2100
leaq 0x10(%rsp), %rdi
callq 0x324e
movq 0x50(%rsp), %rsi
addq $-0x20, %rsi
leaq 0x88(%rsp), %rbx
movq %rbx, %rdi
xorl %edx, %edx
pushq $0x5
popq %rcx
callq 0x2210
movq %rbx, %rdi
leaq 0x2774(%rip), %rsi      # 0x51ff
callq 0x3256
movl %eax, %ebp
movq %rbx, %rdi
callq 0x2100
testb %bpl, %bpl
je 0x2d59
movq 0x50(%rsp), %rsi
addq $-0x20, %rsi
leaq 0x148(%rsp), %rdi
pushq $0x5
popq %rdx
pushq $-0x1
popq %rcx
callq 0x2210
leaq 0x1e8(%rsp), %r14
movq %r14, %rdi
leaq 0x2734(%rip), %rsi      # 0x5205
leaq 0x88(%rsp), %rdx
callq 0x3172
movq %r12, %rdi
leaq 0x148(%rsp), %rbx
movq %rbx, %rsi
movq %r14, %rdx
leaq 0x10(%rsp), %rcx
callq 0x3ba2
movq %r14, %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x2100
xorl %ebx, %ebx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
shrq $0x5, %rdx
cmpl %edx, %ebx
jge 0x2d0d
movslq %ebx, %rbp
shlq $0x5, %rbp
leaq (%rax,%rbp), %rsi
cmpq $0x0, 0x8(%rax,%rbp)
je 0x2c5f
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x2290
leaq 0x1c8(%rsp), %r15
movq %r15, %rdi
leaq 0x269c(%rip), %rsi      # 0x5207
leaq 0x88(%rsp), %rdx
callq 0x3172
movq %r12, %rdi
leaq 0x128(%rsp), %r14
movq %r14, %rsi
movq %r15, %rdx
leaq 0x30(%rsp), %rcx
callq 0x3ba2
movq %r15, %rdi
callq 0x2100
movq %r14, %rdi
callq 0x2100
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %r13
subq %rsi, %r13
cmpq $0x60, %r13
jbe 0x2cec
leaq 0x108(%rsp), %rdi
callq 0x2290
movq 0x30(%rsp), %rsi
addq $0x20, %rsi
leaq 0x1a8(%rsp), %r12
movq %r12, %rdi
callq 0x2290
movzbl 0x5601(%rip), %ecx      # 0x81e8
leaq 0x88(%rsp), %r14
movq %r14, %rdi
leaq 0x108(%rsp), %r15
movq %r15, %rsi
movq %r12, %rdx
callq 0x3a34
movq %r12, %rdi
callq 0x2100
movq %r15, %rdi
callq 0x2100
movq %r14, %rdi
callq 0x2738
movq 0x30(%rsp), %r14
movq 0x60(%r14), %rdi
callq 0x22e0
movl %eax, %r15d
cmpl $0x0, 0xac(%rsp)
je 0x2c71
movq 0x20(%r14), %rdi
callq 0x22e0
movl %eax, %r14d
negl %r14d
cmovsl %eax, %r14d
subl 0x58d2(%rip), %r14d     # 0x8524
testl %r14d, %r14d
jg 0x2c5a
xorl %r14d, %r14d
addl %r15d, %r14d
jmp 0x2c74
leaq 0x10(%rsp), %rdi
callq 0x3264
pushq $-0x1
popq %rbx
jmp 0x2cfc
movl %r15d, %r14d
leaq 0xb8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x3269
testb %al, %al
leaq 0x48(%rsp), %r12
je 0x2ca2
movl 0xd8(%rsp), %eax
cmpl 0xa8(%rsp), %eax
je 0x2ca9
cmpl 0x8(%rsp), %r14d
je 0x2cdf
cmpl 0x8(%rsp), %r14d
jge 0x2cca
leaq 0xb8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x3292
movl %r14d, 0x8(%rsp)
addq 0x10(%rsp), %rbp
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x3264
pushq $-0x1
popq %rbx
leaq 0x88(%rsp), %rdi
callq 0x2100
leaq 0x30(%rsp), %rdi
callq 0x32bc
cmpq $0x60, %r13
jbe 0x2d03
incl %ebx
jmp 0x2b0b
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rcx, %rax
jne 0x2d59
movl 0x8(%rsp), %eax
cltd
idivl 0x5807(%rip)            # 0x8524
movq 0x48(%rsp), %rdi
subq $-0x80, %rdi
testl %eax, %eax
leaq 0x24dd(%rip), %rsi      # 0x520c
leaq 0x24d3(%rip), %rax      # 0x5209
cmovleq %rax, %rsi
callq 0x2390
movl 0xc(%rsp), %ecx
andl $-0x5, %ecx
movl 0xe0(%rsp), %eax
shrl $0x1b, %eax
andl $-0x10, %eax
orl %eax, %ecx
movl %ecx, 0xc(%rsp)
movq 0x48(%rsp), %rsi
leaq 0x228(%rsp), %rdi
callq 0x21c0
movq %rax, %rdi
leaq 0x22c3(%rip), %rsi      # 0x5038
callq 0x21e0
movl 0xc(%rsp), %esi
movq %rax, %rdi
callq 0x2140
movq %rax, %rdi
leaq 0x22a8(%rip), %rsi      # 0x5038
callq 0x21e0
movq %rax, %rdi
leaq 0xb8(%rsp), %rsi
callq 0x21c0
movq %rax, %rdi
leaq 0x2289(%rip), %rsi      # 0x5038
callq 0x21e0
movl 0xd8(%rsp), %esi
movq %rax, %rdi
callq 0x2340
movq 0x50(%rsp), %rax
subq 0x48(%rsp), %rax
shrq $0x5, %rax
movslq %eax, %rbx
movl $0x80, %r14d
pushq $0x4
popq %r15
cmpq %rbx, %r15
jge 0x2e10
leaq 0x228(%rsp), %rdi
leaq 0x2246(%rip), %rsi      # 0x5038
callq 0x21e0
movq 0x48(%rsp), %rsi
addq %r14, %rsi
movq %rax, %rdi
callq 0x21c0
incq %r15
addq $0x20, %r14
jmp 0x2dde
leaq 0x228(%rsp), %rdi
callq 0x2110
leaq 0xb8(%rsp), %rdi
callq 0x2100
movb 0x56f0(%rip), %bl       # 0x8520
leaq 0x188(%rsp), %r14
movq %r14, %rdi
leaq 0x21f6(%rip), %rsi      # 0x5038
leaq 0xb8(%rsp), %rdx
callq 0x3172
movzbl %bl, %esi
movq %r12, %rdi
movq %r14, %rdx
callq 0x3d58
movq %r14, %rdi
callq 0x2100
jmp 0x291e
movq 0x60(%rsp), %rdi
callq 0x2070
leaq 0x228(%rsp), %rdi
callq 0x22a0
leaq 0x10(%rsp), %rdi
callq 0x32bc
leaq 0x228(%rsp), %rdi
callq 0x22c0
leaq 0x48(%rsp), %rdi
callq 0x3238
xorl %eax, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5133(%rip), %r14      # 0x7ff0
leaq 0x2176(%rip), %rsi      # 0x503a
movq %r14, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2110
leaq 0x216f(%rip), %rsi      # 0x504a
movq %r14, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x2110
leaq 0x2173(%rip), %rsi      # 0x506d
movq %r14, %rdi
callq 0x21e0
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x21e0
leaq 0x2161(%rip), %rsi      # 0x5075
movq %rax, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x2110
leaq 0x2168(%rip), %rsi      # 0x509b
movq %r14, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2110
leaq 0x219f(%rip), %rsi      # 0x50e9
movq %r14, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x2110
leaq 0x21cc(%rip), %rsi      # 0x5135
movq %r14, %rdi
callq 0x21e0
movq (%rbx), %rsi
movq %rax, %rdi
callq 0x21e0
leaq 0x21bc(%rip), %rsi      # 0x513f
movq %rax, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x2110
leaq 0x21ce(%rip), %rsi      # 0x5170
movq %r14, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2110
leaq 0x21d0(%rip), %rsi      # 0x5189
movq %r14, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2110
leaq 0x21cf(%rip), %rsi      # 0x519f
movq %r14, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2110
leaq 0x21e8(%rip), %rsi      # 0x51cf
movq %r14, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2110
pushq $-0x1
popq %rdi
callq 0x2240
movq 0x4fea(%rip), %rdi      # 0x7ff0
leaq 0x21d8(%rip), %rsi      # 0x51e5
callq 0x21e0
leaq 0x5207(%rip), %rsi      # 0x8220
movq %rax, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2110
jmp 0x2ff7
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x2100
jmp 0x314c
jmp 0x3051
jmp 0x3044
movq %rax, %rbx
jmp 0x3159
jmp 0x312b
movq %rax, %rbx
jmp 0x314c
movq %rax, %rbx
leaq 0x1e8(%rsp), %rdi
callq 0x2100
jmp 0x306e
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
callq 0x2100
jmp 0x3135
jmp 0x3132
jmp 0x312b
jmp 0x30e9
jmp 0x3132
movq %rax, %rbx
leaq 0x208(%rsp), %rdi
callq 0x2100
jmp 0x30a6
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
jmp 0x313d
jmp 0x312b
jmp 0x3132
jmp 0x30e9
jmp 0x312b
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
jmp 0x313d
jmp 0x311c
movq %rax, %rbx
jmp 0x30df
movq %rax, %rbx
leaq 0x1a8(%rsp), %rdi
callq 0x2100
leaq 0x108(%rsp), %rdi
jmp 0x3115
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
jmp 0x3115
jmp 0x3132
movq %rax, %rbx
leaq 0x1c8(%rsp), %rdi
callq 0x2100
jmp 0x310d
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x2100
jmp 0x311f
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x32bc
jmp 0x3135
movq %rax, %rbx
jmp 0x3142
jmp 0x3132
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x2100
leaq 0x10(%rsp), %rdi
callq 0x32bc
leaq 0x228(%rsp), %rdi
callq 0x22c0
leaq 0x48(%rsp), %rdi
callq 0x3238
movq %rbx, %rdi
callq 0x2350
nop