// File: /workspace/github_c_makefile_star2_O2/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, %ebp
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
pushq $0x4
popq %r14
pushq $-0x1
popq %rax
movl %eax, 0x8(%rsp)
pushq $0x1
popq %rax
leaq 0x25b39(%rip), %r15     # 0x29380
movq 0x2578a(%rip), %r12     # 0x28fd8
movl %eax, 0x10(%rsp)
movl $0x0, 0x14(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x48(%rsp)
andl $0x0, 0xc(%rsp)
movl %ebp, %edi
movq %r13, %rsi
leaq 0x157a2(%rip), %rdx     # 0x1901f
movq %r15, %rcx
leaq 0xc(%rsp), %r8
callq 0x31f0
testl %eax, %eax
jne 0x3af4
movslq 0xc(%rsp), %rax
shlq $0x5, %rax
movq (%rax,%r15), %rbx
movq %rbx, %rdi
leaq 0x15778(%rip), %rsi     # 0x19021
callq 0x3330
testl %eax, %eax
je 0x39a2
movq %rbx, %rdi
leaq 0x157a4(%rip), %rsi     # 0x19064
callq 0x3330
testl %eax, %eax
je 0x39d1
cmpb $0x70, (%rbx)
jne 0x38f2
cmpb $0x6c, 0x1(%rbx)
jne 0x38e2
cmpb $0x0, 0x2(%rbx)
je 0x3a5d
cmpb $0x6d, 0x1(%rbx)
jne 0x38f2
cmpb $0x0, 0x2(%rbx)
je 0x3a70
movq %rbx, %rdi
leaq 0x157af(%rip), %rsi     # 0x190ab
callq 0x3330
testl %eax, %eax
je 0x3a04
movq %rbx, %rdi
leaq 0x1579d(%rip), %rsi     # 0x190b0
callq 0x3330
testl %eax, %eax
je 0x3a1d
movq %rbx, %rdi
leaq 0x157a6(%rip), %rsi     # 0x190d0
callq 0x3330
testl %eax, %eax
je 0x3a45
movq %rbx, %rdi
leaq 0x1579a(%rip), %rsi     # 0x190db
callq 0x3330
testl %eax, %eax
je 0x3a90
movq %rbx, %rdi
leaq 0x15823(%rip), %rsi     # 0x1917b
callq 0x3330
testl %eax, %eax
jne 0x3e2f
movq (%r12), %rdi
leaq 0x15815(%rip), %rsi     # 0x19185
callq 0x3530
testq %rax, %rax
je 0x3e18
movq %rax, 0x28(%rsp)
movq %rax, %rdi
callq 0x5f1c
movq (%r12), %rsi
leaq 0x15821(%rip), %rdi     # 0x191b7
xorl %eax, %eax
callq 0x3210
jmp 0x386c
movq (%r12), %rdi
callq 0x3590
movl %eax, %r14d
cmpl $0x15, %eax
jl 0x386c
leaq 0x15668(%rip), %rdi     # 0x19026
movl %r14d, %esi
xorl %eax, %eax
callq 0x3210
pushq $0x4
popq %r14
jmp 0x386c
movq (%r12), %rdi
callq 0x3590
testl %eax, %eax
je 0x39fb
cmpl $0x21, %eax
je 0x39fb
cmpl $0x40, %eax
je 0x39fb
leaq 0x1567a(%rip), %rdi     # 0x19069
movl %eax, %esi
xorl %eax, %eax
callq 0x3210
pushq $0x21
popq %rax
movl %eax, 0x8(%rsp)
jmp 0x386c
leaq 0x159bf(%rip), %rdi     # 0x193ca
callq 0x3120
movl $0x0, 0x10(%rsp)
jmp 0x386c
movq (%r12), %rdi
callq 0x3170
callq 0x8a3c
xorl %eax, %eax
callq 0x8a45
leaq 0x1567d(%rip), %rdi     # 0x190b6
movb $0x1, %al
callq 0x3210
jmp 0x386c
xorl %eax, %eax
callq 0x8a95
leaq 0x15948(%rip), %rdi     # 0x1939b
callq 0x3120
jmp 0x386c
movq (%r12), %rsi
leaq 0x60(%rsp), %rdi
callq 0x30e0
jmp 0x386c
movl $0x3c0, %edi            # imm = 0x3C0
callq 0x3420
movq (%r12), %rsi
movq %rax, 0x48(%rsp)
movq %rax, %rdi
callq 0xa18d
jmp 0x386c
movq (%r12), %rbx
testq %rbx, %rbx
leaq 0x50(%rsp), %rax
cmoveq %rax, %rbx
leaq 0x34(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdi
leaq 0x1d8d0(%rip), %rsi     # 0x21383
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 0x3e70
leaq 0x15648(%rip), %rdi     # 0x19126
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3210
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
jmp 0x386c
cmpl $-0x1, %eax
jne 0x3eac
movq 0x254cc(%rip), %rax     # 0x28fd0
movslq (%rax), %rax
subl %eax, %ebp
cmpl $0x2, %ebp
jg 0x3b18
callq 0x37cc
jmp 0x3e04
movq (%r13,%rax,8), %rbp
movq 0x8(%r13,%rax,8), %rbx
movq 0x10(%r13,%rax,8), %r15
leaq 0x15792(%rip), %rdi     # 0x192c0
movq %rbp, %rsi
xorl %eax, %eax
callq 0x3210
leaq 0x156db(%rip), %rdi     # 0x1921a
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3210
leaq 0x156e3(%rip), %rdi     # 0x19233
movq %r15, 0x20(%rsp)
movq %r15, %rsi
xorl %eax, %eax
callq 0x3210
movq %rbp, %rdi
callq 0xa0f0
movq %rax, %r13
leaq 0x15874(%rip), %rdi     # 0x193e5
callq 0x3120
movq %rbx, %rdi
callq 0x9f4a
movq %rax, %r15
movq 0x10(%r13), %rdi
movq %rax, %rsi
callq 0x95c8
testl %eax, %eax
je 0x3eb8
cmpb $0x0, 0x60(%rsp)
je 0x3be7
leaq 0x156e1(%rip), %rdi     # 0x19284
leaq 0x60(%rsp), %rbx
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3210
movl $0x756277, 0xc(%rsp)    # imm = 0x756277
cmpb $0x2d, (%rbx)
je 0x3bc4
movb $0x0, 0xe(%rsp)
movq 0x10(%r13), %rdx
leaq 0x60(%rsp), %rdi
leaq 0xc(%rsp), %rsi
callq 0x10e5d
testq %rax, %rax
je 0x3ef0
movq %rax, 0x18(%rsp)
jmp 0x3bf0
movq $0x0, 0x18(%rsp)
leaq 0x15805(%rip), %rdi     # 0x193fc
callq 0x3120
movq 0x48(%rsp), %rbx
testq %rbx, %rbx
je 0x3c18
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0xa3e8
movq %rbx, %r12
jmp 0x3c5b
leaq 0x1580c(%rip), %rdi     # 0x1942b
callq 0x3120
movsd 0x153dc(%rip), %xmm0    # 0x19008
movq %r13, %rdi
movl 0x8(%rsp), %esi
callq 0xa5ce
movq %rax, %r12
movq %rax, %rdi
movq 0x20(%rsp), %rsi
callq 0xa3e8
movq %r13, %rdi
callq 0x112dd
movq %rbp, %rdi
callq 0xa0f0
movq %rax, %r13
movl 0x10(%rsp), %ebp
cvtsi2sdq 0x380(%r12), %xmm0
movsd %xmm0, 0x98(%r15)
leaq 0x157f6(%rip), %rdi     # 0x1946f
callq 0x3120
cmpl $0x0, 0x14(%rsp)
movq 0x18(%rsp), %rbx
je 0x3ccb
movl 0x44(%rsp), %esi
movl 0x40(%rsp), %edx
movl 0x3c(%rsp), %ecx
movl 0x38(%rsp), %r8d
movl 0x34(%rsp), %r9d
movq %r15, %rdi
callq 0x87f4
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
movq 0x28(%rsp), %rbx
movq %rbx, %r8
callq 0x7afb
movq %r15, %rdi
callq 0x89db
jmp 0x3ce4
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
movq 0x28(%rsp), %rbx
movq %rbx, %r8
callq 0x7afb
testq %rbx, %rbx
je 0x3d09
movq %rbx, %rdi
callq 0x3360
testq %rax, %rax
jle 0x3d01
pushq $0xa
popq %rdi
movq %rbx, %rsi
callq 0x32a0
movq %rbx, %rdi
callq 0x31c0
leaq 0x15784(%rip), %rdi     # 0x19494
callq 0x3120
movq %r15, %rdi
movl %r14d, %esi
callq 0x4deb
leaq 0x1578b(%rip), %rdi     # 0x194b2
callq 0x3120
leaq 0x157a0(%rip), %rdi     # 0x194d3
callq 0x3120
movq %r15, %rdi
movq %r12, %rsi
callq 0x674e
leaq 0x157a7(%rip), %rdi     # 0x194f1
callq 0x3120
leaq 0x157bc(%rip), %rdi     # 0x19512
callq 0x3120
movq %r15, %rdi
callq 0x660a
leaq 0x157d4(%rip), %rdi     # 0x1953e
callq 0x3120
leaq 0x1540f(%rip), %rsi     # 0x19185
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x3530
movq %rax, %rbx
testq %rax, %rax
jne 0x3d9c
leaq 0x1553c(%rip), %rdi     # 0x192ce
movq %r14, %rsi
xorl %eax, %eax
callq 0x3210
movq %r15, %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0x9bee
movq %rbx, %rdi
callq 0x31c0
leaq 0x1553d(%rip), %rdi     # 0x192f5
movq %r14, %rsi
xorl %eax, %eax
callq 0x3210
movq 0x18(%rsp), %rbx
testq %rbx, %rbx
je 0x3de0
leaq 0x1579a(%rip), %rdi     # 0x1956d
callq 0x3120
movq %rbx, %rdi
callq 0x112dd
leaq 0x1579d(%rip), %rdi     # 0x19584
callq 0x3120
movq %r13, %rdi
callq 0x112dd
movq %r12, %rdi
callq 0x3070
movq %r15, %rdi
callq 0xa0a5
xorl %eax, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x251d9(%rip), %rax     # 0x28ff8
movq (%rax), %rdi
movq (%r12), %rdx
leaq 0x1535a(%rip), %rsi     # 0x19187
jmp 0x3e8a
leaq 0x153a0(%rip), %rsi     # 0x191d6
movq %rbx, %rdi
callq 0x3330
testl %eax, %eax
jne 0x3e99
movq 0x251af(%rip), %rax     # 0x28ff8
movq (%rax), %rdi
movq (%r12), %rdx
leaq 0x15387(%rip), %rsi     # 0x191de
xorl %eax, %eax
callq 0x3350
movq (%r12), %rdi
callq 0x3590
movl %eax, %edi
callq 0x8ad5
jmp 0x3eb1
callq 0x37cc
movq 0x2517c(%rip), %rax     # 0x28ff8
movq (%rax), %rdi
movq (%r12), %rdx
leaq 0x15259(%rip), %rsi     # 0x190e3
xorl %eax, %eax
callq 0x3350
pushq $0x1
popq %rdi
callq 0x35c0
leaq 0x15361(%rip), %rsi     # 0x19201
movq %rbx, %rdi
callq 0x3330
testl %eax, %eax
jne 0x3ece
callq 0x37cc
xorl %edi, %edi
callq 0x35c0
leaq 0x15389(%rip), %rdi     # 0x19248
movq %rbx, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x3210
jmp 0x3e91
movq 0x25123(%rip), %rax     # 0x28ff8
movq (%rax), %rdi
leaq 0x15327(%rip), %rsi     # 0x19206
movq %rbx, %rdx
xorl %eax, %eax
callq 0x3350
callq 0x37cc
jmp 0x3e91
leaq 0x153a1(%rip), %rdi     # 0x19298
leaq 0x60(%rsp), %rsi
xorl %eax, %eax
callq 0x3210
jmp 0x3e91
nopl (%rax)