// File: /workspace/github_c_makefile_star2_O3/sc932[P]ALE/src/ALE.c
// Function: main
int main(int argc, char **argv){
	int c = 0, digit_optind = 0;
    int kmerLen = 4;
    char placementOut[256];
    samfile_t *placementBam = NULL;
    *placementOut = '\0';
    libraryParametersT *libParams = NULL;
    double outlierFraction = 0.02;
    int qOff = -1;
    int printAllAleOutput = 1;

    int doRealign = 0;
    // use BWA bwasw default matrix
    char defaultRealignOptions[] = DEFAULT_REALIGN_OPTIONS;
    int matchScore, mismatchPenalty, gapOpenPenalty, gapExtendPenalty, minSoftClip;
    FILE *snpPhaseFile = NULL;


    while(1) {
        int this_option_optind = optind ? optind : 1;
        int option_index = 0;
        c = getopt_long(argc, argv, "h",
                 long_options, &option_index);
        if (c == -1)
        	break;
        switch(c) {
        case 0:
        	// long options
        	if (strcmp(long_options[option_index].name, "kmer") == 0) {
                kmerLen = atoi(optarg);
                if(kmerLen > 20){
                    printf("--kmer option of %i not in range [2,20], set to default [4].\n", kmerLen);
                    kmerLen = 4;
                }
        	} else if (strcmp(long_options[option_index].name, "qOff") == 0) {
                qOff = atoi(optarg);
                if(qOff != 64 && qOff != 33 && qOff != 0){
                    printf("--qOff option of %i not in set [33,64], will be set to 33.\n", qOff);
                    qOff = 33;  // SAM/BAM specification is for ascii - 33.
                }
        	} else if (strcmp(long_options[option_index].name, "pl") == 0) {
                strcpy(placementOut, optarg);
        	} else if (strcmp(long_options[option_index].name, "pm") == 0) {
                libParams = malloc(sizeof(libraryParametersT));
                importLibraryParameters(libParams, optarg);
        	} else if (strcmp(long_options[option_index].name, "nout") == 0) {
                printAllAleOutput = 0;
                printf("Turned off per-base output\n");
        	} else if (strcmp(long_options[option_index].name, "minLL") == 0) {
            	setMinLogLike(atof(optarg));
            	printf("Set minLogLike to: %0.1f\n", getMinLogLike());
        	} else if (strcmp(long_options[option_index].name, "metagenome") == 0) {
            	setMetagenome();
            	printf("This dataset will be evaluated as a Metagenome\n");
        	} else if (strcmp(long_options[option_index].name, "realign") == 0) {
            	doRealign = 1;
            	char *swopts = defaultRealignOptions;
            	if (optarg) {
            		swopts = optarg;
            	}
            	// parse SW options...
            	if (5 != sscanf(swopts, "%d,%d,%d,%d,%d", &matchScore, &mismatchPenalty, &gapOpenPenalty, &gapExtendPenalty, &minSoftClip)) {
            		usage();
            		fprintf(stderr, "Could not parse %s as --realign parameter.  Expecting 5 integers.\n", optarg);
            		exit(1);
            	}
            	printf("This dataset will be realigned with Smith-Waterman letting ambiguous bases match %s\n", swopts);
        	} else if (strcmp(long_options[option_index].name, "SNPreport") == 0) {
        		snpPhaseFile = fopen(optarg, "w");
        		if (snpPhaseFile == NULL) {
        			fprintf(stderr, "Could not open '%s' for writing the SNPreport!\n", optarg);
        			exit(1);
        		}
        		printSNPhaseHeader(snpPhaseFile);
        		printf("Reporting SNP phasing into %s\n", optarg);
        	} else if (strcmp(long_options[option_index].name, "minQual") == 0) {
        		fprintf(stderr, "Using minimum quality score of %s\n", optarg);
        		setMinimumQuality( atoi(optarg) );
        		exit(0);
         	} else if (strcmp(long_options[option_index].name, "help") == 0) {
        		usage();
        		exit(0);
         	} else {
         		fprintf(stderr, "unknown option: %s\n", long_options[option_index].name);
         		usage();
         		exit(1);
         	}
        	break;
        case 'h':
        default:
        	usage();
        	exit(0);
        }
    }

    if (argc - optind < 3) {
        // the input was shorter than anticipated, print long options if -h specified
    	usage();
        return 0;
    }
    
    // input and output files
    char *bamFile = argv[optind], *assemblyFile = argv[optind+1], *aleFile = argv[optind+2];
    printf("BAM file: %s\n", bamFile);
    printf("Assembly fasta file: %s\n", assemblyFile);
    printf("ALE Output file: %s\n", aleFile);

    // attempt to open the bam input file
    samfile_t *ins = openSamOrBam(bamFile);
    
    printf("Reading in assembly...\n");
    assemblyT *theAssembly = loadAssembly(assemblyFile);
    if (!validateAssemblyIsSameAsAlignment(ins->header, theAssembly)) {
        printf("Error! Assembly fasta %s does not match alignment file %s!\n", assemblyFile, bamFile);
        exit(1);
    }
    
    if (*placementOut != '\0') {
        printf("Placement file: %s\n", placementOut);
        char mode[] = "wbu";
        if (placementOut[0] != '-' )
            mode[2] = '\0'; // compress bam if not part of a pipe
        placementBam = samopen(placementOut, mode, ins->header);
        if(placementBam == 0){
            printf("Error! Could not write to the placement BAM file: %s\n", placementOut);
            exit(1);
        }
    }

    printf("Reading in the map and computing statistics...\n");

    if(libParams != NULL){
      saveLibraryParameters(libParams, aleFile); // the ALE output file name
    }

    // calculate the insert mean/std if not given
    if(libParams == NULL){
        printf("Insert length and std not given, will be calculated from input map.\n");

        libParams = computeLibraryParameters(ins, outlierFraction, qOff);
        saveLibraryParameters(libParams, aleFile); // the ALE output file name

        // close and re-open bam file
        samclose(ins);
        ins = openSamOrBam(bamFile);
    }

    // set assembly avgReadSize to that of library (rounded to nearest int)
    theAssembly->avgReadSize = (double)libParams->avgReadSize;

    // place reads and compute statistics on the assembly
    printf("Computing read placements and depths\n");
    if (doRealign)
    	initRefNumForRealign(theAssembly, matchScore, mismatchPenalty, gapOpenPenalty, gapExtendPenalty, minSoftClip);
    computeReadPlacements(ins, theAssembly, libParams, placementBam, snpPhaseFile);
    if (doRealign)
    	destroyRefNumForRealign(theAssembly);
    if (snpPhaseFile != NULL) {
    	if (ftell(snpPhaseFile) > 0) {
    		fprintf(snpPhaseFile, "\n");
    	}
    	fclose(snpPhaseFile);
    	snpPhaseFile = NULL;
    }
    
    // compute statistics on assembly
    printf("Computing k-mer statistics...\n");
    computeKmerStats(theAssembly, kmerLen);
    printf("Done computing k-mer statistics.\n");
    
    printf("Computing depth statistics...\n");
    computeDepthStats(theAssembly, libParams);
    printf("Done computing depth statistics.\n");

    printf("Computing statistics on expected missing...\n");
    applyExpectedMissingLength(theAssembly);
    printf("Done computing statistics on expected missing.\n");
    
    FILE *out = fopen(aleFile, "w");
    if(out == NULL){
        printf("Error! Could not open output file: %s\n", aleFile);
    }
    writeToOutput(theAssembly, printAllAleOutput, out);
    fclose(out);
    
    printf("Output is in file: %s\n", aleFile);

    if (placementBam != NULL) {
        printf("Closing placement file\n");
        samclose(placementBam);
    }

    printf("Closing input file\n");
    samclose(ins);
    
    if (libParams != NULL)
        free(libParams);

    freeAssembly(theAssembly);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rsi, %r13
movl %edi, %ebx
leaq 0x60(%rsp), %rax
movb $0x0, (%rax)
movabsq $0x342c31312c332c31, %rax # imm = 0x342C31312C332C31
leaq 0x50(%rsp), %rcx
movq %rax, (%rcx)
movl $0x382c34, 0x7(%rcx)    # imm = 0x382C34
movl $0x4, %ebp
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movl $0x1, 0x14(%rsp)
leaq 0x271b2(%rip), %r15     # 0x2c380
movq 0x26e03(%rip), %r12     # 0x2bfd8
movl $0x0, 0x10(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x48(%rsp)
movl $0x0, 0xc(%rsp)
movl %ebx, %edi
movq %r13, %rsi
leaq 0x17e1c(%rip), %rdx     # 0x1d01f
movq %r15, %rcx
leaq 0xc(%rsp), %r8
callq 0x31f0
testl %eax, %eax
jne 0x5489
movslq 0xc(%rsp), %rax
shlq $0x5, %rax
movq (%rax,%r15), %r14
movq %r14, %rdi
leaq 0x17df2(%rip), %rsi     # 0x1d021
callq 0x3330
testl %eax, %eax
je 0x532d
movq %r14, %rdi
leaq 0x17e1e(%rip), %rsi     # 0x1d064
callq 0x3330
testl %eax, %eax
je 0x535b
cmpb $0x70, (%r14)
jne 0x527d
cmpb $0x6c, 0x1(%r14)
jne 0x526b
cmpb $0x0, 0x2(%r14)
je 0x53f1
cmpb $0x6d, 0x1(%r14)
jne 0x527d
cmpb $0x0, 0x2(%r14)
je 0x5404
movq %r14, %rdi
leaq 0x17e24(%rip), %rsi     # 0x1d0ab
callq 0x3330
testl %eax, %eax
je 0x537b
movq %r14, %rdi
leaq 0x17e12(%rip), %rsi     # 0x1d0b0
callq 0x3330
testl %eax, %eax
je 0x5394
movq %r14, %rdi
leaq 0x17e1b(%rip), %rsi     # 0x1d0d0
callq 0x3330
testl %eax, %eax
je 0x53d9
movq %r14, %rdi
leaq 0x17e0f(%rip), %rsi     # 0x1d0db
callq 0x3330
testl %eax, %eax
je 0x5424
movq %r14, %rdi
leaq 0x17e98(%rip), %rsi     # 0x1d17b
callq 0x3330
testl %eax, %eax
jne 0x57b5
movq (%r12), %rdi
leaq 0x17e8a(%rip), %rsi     # 0x1d185
callq 0x3530
testq %rax, %rax
je 0x57b0
movq %rax, 0x28(%rsp)
movq %rax, %rdi
callq 0x7e09
movq (%r12), %rsi
leaq 0x17e96(%rip), %rdi     # 0x1d1b7
xorl %eax, %eax
callq 0x3210
jmp 0x51ef
movq (%r12), %rdi
callq 0x3590
movl %eax, %ebp
cmpl $0x15, %eax
jl 0x51ef
leaq 0x17cde(%rip), %rdi     # 0x1d026
movl %ebp, %esi
xorl %eax, %eax
callq 0x3210
movl $0x4, %ebp
jmp 0x51ef
movq (%r12), %rdi
callq 0x3590
testl %eax, %eax
je 0x5372
cmpl $0x21, %eax
je 0x5372
cmpl $0x40, %eax
jne 0x53bc
movl %eax, 0x8(%rsp)
jmp 0x51ef
leaq 0x18048(%rip), %rdi     # 0x1d3ca
callq 0x3120
movl $0x0, 0x14(%rsp)
jmp 0x51ef
movq (%r12), %rdi
callq 0x3170
callq 0xab40
xorl %eax, %eax
callq 0xab49
leaq 0x17d06(%rip), %rdi     # 0x1d0b6
movb $0x1, %al
callq 0x3210
jmp 0x51ef
leaq 0x17ca6(%rip), %rdi     # 0x1d069
movl %eax, %esi
xorl %eax, %eax
callq 0x3210
movl $0x21, 0x8(%rsp)
jmp 0x51ef
xorl %eax, %eax
callq 0xab99
leaq 0x17fb4(%rip), %rdi     # 0x1d39b
callq 0x3120
jmp 0x51ef
movq (%r12), %rsi
leaq 0x60(%rsp), %rdi
callq 0x30e0
jmp 0x51ef
movl $0x3c0, %edi            # imm = 0x3C0
callq 0x3420
movq (%r12), %rsi
movq %rax, 0x48(%rsp)
movq %rax, %rdi
callq 0xc552
jmp 0x51ef
movq (%r12), %r14
testq %r14, %r14
leaq 0x50(%rsp), %rax
cmoveq %rax, %r14
leaq 0x34(%rsp), %rax
movq %rax, (%rsp)
movq %r14, %rdi
leaq 0x1ffdc(%rip), %rsi     # 0x25423
leaq 0x44(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x3c(%rsp), %r8
leaq 0x38(%rsp), %r9
xorl %eax, %eax
callq 0x3450
cmpl $0x5, %eax
jne 0x57cd
leaq 0x17cb4(%rip), %rdi     # 0x1d126
movq %r14, %rsi
xorl %eax, %eax
callq 0x3210
movl $0x1, 0x10(%rsp)
jmp 0x51ef
cmpl $-0x1, %eax
jne 0x57e5
movq 0x26b37(%rip), %rax     # 0x2bfd0
movslq (%rax), %rax
subl %eax, %ebx
cmpl $0x2, %ebx
jg 0x54ad
callq 0x514c
jmp 0x579c
movq (%r13,%rax,8), %rbx
movq 0x8(%r13,%rax,8), %r15
movq 0x10(%r13,%rax,8), %r14
leaq 0x17dfd(%rip), %rdi     # 0x1d2c0
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3210
leaq 0x17d46(%rip), %rdi     # 0x1d21a
movq %r15, %rsi
xorl %eax, %eax
callq 0x3210
leaq 0x17d4e(%rip), %rdi     # 0x1d233
movq %r14, 0x18(%rsp)
movq %r14, %rsi
xorl %eax, %eax
callq 0x3210
movq %rbx, %rdi
callq 0xc4b3
movq %rax, %r13
leaq 0x17edf(%rip), %rdi     # 0x1d3e5
callq 0x3120
movq %r15, %rdi
callq 0xc2fb
movq %rax, %r14
movq 0x10(%r13), %rdi
movq %rax, %rsi
callq 0xb857
testl %eax, %eax
je 0x57f1
cmpb $0x0, 0x60(%rsp)
je 0x557d
leaq 0x17d4c(%rip), %rdi     # 0x1d284
leaq 0x60(%rsp), %r15
movq %r15, %rsi
xorl %eax, %eax
callq 0x3210
movl $0x756277, 0xc(%rsp)    # imm = 0x756277
cmpb $0x2d, (%r15)
je 0x555a
movb $0x0, 0xe(%rsp)
movq 0x10(%r13), %rdx
leaq 0x60(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x14196
testq %rax, %rax
je 0x580f
movq %rax, 0x20(%rsp)
jmp 0x5586
movq $0x0, 0x20(%rsp)
leaq 0x17e6f(%rip), %rdi     # 0x1d3fc
callq 0x3120
movq 0x48(%rsp), %r15
testq %r15, %r15
je 0x55ae
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0xc7ad
movq %r15, %r12
jmp 0x55f1
leaq 0x17e76(%rip), %rdi     # 0x1d42b
callq 0x3120
movsd 0x17a46(%rip), %xmm0    # 0x1d008
movq %r13, %rdi
movl 0x8(%rsp), %esi
callq 0xc997
movq %rax, %r12
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0xc7ad
movq %r13, %rdi
callq 0x14613
movq %rbx, %rdi
callq 0xc4b3
movq %rax, %r13
xorps %xmm0, %xmm0
cvtsi2sdq 0x380(%r12), %xmm0
movsd %xmm0, 0x98(%r14)
leaq 0x17e61(%rip), %rdi     # 0x1d46f
callq 0x3120
cmpl $0x0, 0x10(%rsp)
movq 0x20(%rsp), %rbx
je 0x5660
movl 0x44(%rsp), %esi
movl 0x40(%rsp), %edx
movl 0x3c(%rsp), %ecx
movl 0x38(%rsp), %r8d
movl 0x34(%rsp), %r9d
movq %r14, %rdi
callq 0xa930
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %rbx, %rcx
movq 0x28(%rsp), %rbx
movq %rbx, %r8
callq 0x9c04
movq %r14, %rdi
callq 0xaad3
jmp 0x5679
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %rbx, %rcx
movq 0x28(%rsp), %rbx
movq %rbx, %r8
callq 0x9c04
movq 0x18(%rsp), %r15
testq %rbx, %rbx
je 0x56a5
movq %rbx, %rdi
callq 0x3360
testq %rax, %rax
jle 0x569d
movl $0xa, %edi
movq %rbx, %rsi
callq 0x32a0
movq %rbx, %rdi
callq 0x31c0
leaq 0x17de8(%rip), %rdi     # 0x1d494
callq 0x3120
movq %r14, %rdi
movl %ebp, %esi
callq 0x6994
leaq 0x17df0(%rip), %rdi     # 0x1d4b2
callq 0x3120
leaq 0x17e05(%rip), %rdi     # 0x1d4d3
callq 0x3120
movq %r14, %rdi
movq %r12, %rsi
callq 0x86b9
leaq 0x17e0c(%rip), %rdi     # 0x1d4f1
callq 0x3120
leaq 0x17e21(%rip), %rdi     # 0x1d512
callq 0x3120
movq %r14, %rdi
callq 0x8577
leaq 0x17e39(%rip), %rdi     # 0x1d53e
callq 0x3120
leaq 0x17a74(%rip), %rsi     # 0x1d185
movq %r15, %rdi
callq 0x3530
movq %rax, %rbx
testq %rax, %rax
jne 0x5732
leaq 0x17ba6(%rip), %rdi     # 0x1d2ce
movq %r15, %rsi
xorl %eax, %eax
callq 0x3210
movq %r14, %rdi
movl 0x14(%rsp), %esi
movq %rbx, %rdx
callq 0xbf56
movq %rbx, %rdi
callq 0x31c0
leaq 0x17ba5(%rip), %rdi     # 0x1d2f5
movq %r15, %rsi
xorl %eax, %eax
callq 0x3210
movq 0x20(%rsp), %rbx
testq %rbx, %rbx
je 0x5778
leaq 0x17e02(%rip), %rdi     # 0x1d56d
callq 0x3120
movq %rbx, %rdi
callq 0x14613
leaq 0x17e05(%rip), %rdi     # 0x1d584
callq 0x3120
movq %r13, %rdi
callq 0x14613
movq %r12, %rdi
callq 0x3070
movq %r14, %rdi
callq 0xc45c
xorl %eax, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3742
leaq 0x17a1a(%rip), %rsi     # 0x1d1d6
movq %r14, %rdi
callq 0x3330
testl %eax, %eax
jne 0x57d2
callq 0x3709
callq 0x376d
leaq 0x17a28(%rip), %rsi     # 0x1d201
movq %r14, %rdi
callq 0x3330
testl %eax, %eax
jne 0x5807
callq 0x514c
xorl %edi, %edi
callq 0x35c0
leaq 0x17a50(%rip), %rdi     # 0x1d248
movq %r15, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3210
jmp 0x5822
movq %r14, %rdi
callq 0x36e0
leaq 0x17a82(%rip), %rdi     # 0x1d298
leaq 0x60(%rsp), %rsi
xorl %eax, %eax
callq 0x3210
movl $0x1, %edi
callq 0x35c0