// File: /workspace/github_c_makefile_star2_O1/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, 0x1c(%rsp)
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x24(%rsp)
leaq 0x1807e(%rip), %r15     # 0x1d01f
leaq 0x273d8(%rip), %r14     # 0x2c380
leaq 0x18(%rsp), %r12
movl $0x0, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movq $0x0, 0x48(%rsp)
movl $0x0, 0x18(%rsp)
movl %ebx, %edi
movq %r13, %rsi
movq %r15, %rdx
movq %r14, %rcx
movq %r12, %r8
callq 0x31f0
cmpl $-0x1, %eax
je 0x5113
testl %eax, %eax
jne 0x561a
movslq 0x18(%rsp), %rax
shlq $0x5, %rax
movq (%rax,%r14), %rbp
movq %rbp, %rdi
leaq 0x18017(%rip), %rsi     # 0x1d021
callq 0x3330
testl %eax, %eax
je 0x511a
movq %rbp, %rdi
leaq 0x18043(%rip), %rsi     # 0x1d064
callq 0x3330
testl %eax, %eax
je 0x514b
movq %rbp, %rdi
leaq 0x1806d(%rip), %rsi     # 0x1d0a5
callq 0x3330
testl %eax, %eax
je 0x5177
movq %rbp, %rdi
leaq 0x18059(%rip), %rsi     # 0x1d0a8
callq 0x3330
testl %eax, %eax
je 0x518d
movq %rbp, %rdi
leaq 0x18045(%rip), %rsi     # 0x1d0ab
callq 0x3330
testl %eax, %eax
je 0x51d9
movq %rbp, %rdi
leaq 0x18033(%rip), %rsi     # 0x1d0b0
callq 0x3330
testl %eax, %eax
je 0x51f2
movq %rbp, %rdi
leaq 0x1803c(%rip), %rsi     # 0x1d0d0
callq 0x3330
testl %eax, %eax
je 0x5220
movq %rbp, %rdi
leaq 0x18030(%rip), %rsi     # 0x1d0db
callq 0x3330
testl %eax, %eax
je 0x5238
movq %rbp, %rdi
leaq 0x180b9(%rip), %rsi     # 0x1d17b
callq 0x3330
testl %eax, %eax
jne 0x55c8
movq 0x26f02(%rip), %rbp     # 0x2bfd8
movq (%rbp), %rdi
leaq 0x180a4(%rip), %rsi     # 0x1d185
callq 0x3530
testq %rax, %rax
je 0x55e0
movq %rax, 0x28(%rsp)
movq %rax, %rdi
callq 0x7ac9
movq (%rbp), %rsi
leaq 0x180b0(%rip), %rdi     # 0x1d1b7
xorl %eax, %eax
callq 0x3210
jmp 0x51ae
xorl %ebp, %ebp
jmp 0x51b1
movq 0x26eb7(%rip), %rax     # 0x2bfd8
movq (%rax), %rdi
callq 0x3590
movb $0x1, %bpl
cmpl $0x15, %eax
jl 0x5171
leaq 0x17eee(%rip), %rdi     # 0x1d026
movl %eax, %esi
xorl %eax, %eax
callq 0x3210
movl $0x4, 0x1c(%rsp)
jmp 0x51b1
movq 0x26e86(%rip), %rax     # 0x2bfd8
movq (%rax), %rdi
callq 0x3590
movb $0x1, %bpl
testl %eax, %eax
je 0x516b
cmpl $0x21, %eax
je 0x516b
cmpl $0x40, %eax
jne 0x51bf
movl %eax, 0x14(%rsp)
jmp 0x51b1
movl %eax, 0x1c(%rsp)
jmp 0x51b1
movq 0x26e5a(%rip), %rax     # 0x2bfd8
movq (%rax), %rsi
leaq 0x60(%rsp), %rdi
callq 0x30e0
jmp 0x51ae
movl $0x3c0, %edi            # imm = 0x3C0
callq 0x3420
movq 0x26e3a(%rip), %rcx     # 0x2bfd8
movq (%rcx), %rsi
movq %rax, 0x48(%rsp)
movq %rax, %rdi
callq 0xc2df
movb $0x1, %bpl
testb %bpl, %bpl
jne 0x4fc7
jmp 0x52a6
leaq 0x17ea3(%rip), %rdi     # 0x1d069
movl %eax, %esi
xorl %eax, %eax
callq 0x3210
movl $0x21, 0x14(%rsp)
jmp 0x51b1
leaq 0x181ea(%rip), %rdi     # 0x1d3ca
callq 0x3120
movb $0x1, %bpl
movl $0x0, 0x24(%rsp)
jmp 0x51b1
movq 0x26ddf(%rip), %rax     # 0x2bfd8
movq (%rax), %rdi
callq 0x3170
callq 0xa878
xorl %eax, %eax
callq 0xa881
movb $0x1, %bpl
leaq 0x17e9f(%rip), %rdi     # 0x1d0b6
movb $0x1, %al
callq 0x3210
jmp 0x51b1
xorl %eax, %eax
callq 0xa8d1
leaq 0x1816d(%rip), %rdi     # 0x1d39b
callq 0x3120
jmp 0x51ae
movq 0x26d99(%rip), %rax     # 0x2bfd8
movq (%rax), %rbp
testq %rbp, %rbp
leaq 0x50(%rsp), %rax
cmoveq %rax, %rbp
leaq 0x34(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
leaq 0x200f2(%rip), %rsi     # 0x25353
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 0x5602
leaq 0x17e9a(%rip), %rdi     # 0x1d126
movq %rbp, %rsi
xorl %eax, %eax
callq 0x3210
movb $0x1, %bpl
movl $0x1, 0x20(%rsp)
jmp 0x51b1
movq 0x26d23(%rip), %rax     # 0x2bfd0
movslq (%rax), %rax
subl %eax, %ebx
cmpl $0x2, %ebx
jg 0x52c1
callq 0x4f1c
jmp 0x559e
movq (%r13,%rax,8), %r12
movq 0x8(%r13,%rax,8), %rbp
movq 0x10(%r13,%rax,8), %rbx
leaq 0x17fe9(%rip), %rdi     # 0x1d2c0
movq %r12, %rsi
xorl %eax, %eax
callq 0x3210
leaq 0x17f32(%rip), %rdi     # 0x1d21a
movq %rbp, %rsi
xorl %eax, %eax
callq 0x3210
leaq 0x17f3a(%rip), %rdi     # 0x1d233
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3210
movq %r12, %rdi
callq 0xc240
movq %rax, %r15
leaq 0x180d0(%rip), %rdi     # 0x1d3e5
callq 0x3120
movq %rbp, %rdi
callq 0xc085
movq %rax, %r14
movq 0x10(%r15), %rdi
movq %rax, %rsi
callq 0xb5e3
testl %eax, %eax
je 0x55b2
cmpb $0x0, 0x60(%rsp)
je 0x538b
leaq 0x17f3d(%rip), %rdi     # 0x1d284
leaq 0x60(%rsp), %r13
movq %r13, %rsi
xorl %eax, %eax
callq 0x3210
movl $0x756277, 0x18(%rsp)   # imm = 0x756277
cmpb $0x2d, (%r13)
je 0x536a
movb $0x0, 0x1a(%rsp)
movq 0x10(%r15), %rdx
leaq 0x60(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x13e7b
testq %rax, %rax
je 0x55e5
movq %rax, %r13
jmp 0x538e
xorl %r13d, %r13d
leaq 0x18067(%rip), %rdi     # 0x1d3fc
callq 0x3120
movq 0x48(%rsp), %rbp
testq %rbp, %rbp
je 0x53b4
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xc53a
movq %rbp, %r12
jmp 0x53f8
leaq 0x18070(%rip), %rdi     # 0x1d42b
callq 0x3120
movsd 0x17c40(%rip), %xmm0    # 0x1d008
movq %r15, %rdi
movl 0x14(%rsp), %esi
callq 0xc724
movq %rax, %rbp
movq %rax, %rdi
movq %rbx, %rsi
callq 0xc53a
movq %r15, %rdi
callq 0x142d1
movq %r12, %rdi
movq %rbp, %r12
callq 0xc240
movq %rax, %r15
xorps %xmm0, %xmm0
cvtsi2sdq 0x380(%r12), %xmm0
movsd %xmm0, 0x98(%r14)
leaq 0x1805a(%rip), %rdi     # 0x1d46f
callq 0x3120
cmpl $0x0, 0x20(%rsp)
je 0x5462
movl 0x44(%rsp), %esi
movl 0x40(%rsp), %edx
movl 0x3c(%rsp), %ecx
movl 0x38(%rsp), %r8d
movl 0x34(%rsp), %r9d
movq %r14, %rdi
callq 0xa65c
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq 0x28(%rsp), %rbp
movq %rbp, %r8
callq 0x994f
movq %r14, %rdi
callq 0xa80d
jmp 0x547b
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq 0x28(%rsp), %rbp
movq %rbp, %r8
callq 0x994f
testq %rbp, %rbp
je 0x54a2
movq %rbp, %rdi
callq 0x3360
testq %rax, %rax
jle 0x549a
movl $0xa, %edi
movq %rbp, %rsi
callq 0x32a0
movq %rbp, %rdi
callq 0x31c0
leaq 0x17feb(%rip), %rdi     # 0x1d494
callq 0x3120
movq %r14, %rdi
movl 0x1c(%rsp), %esi
callq 0x6668
leaq 0x17ff1(%rip), %rdi     # 0x1d4b2
callq 0x3120
leaq 0x18006(%rip), %rdi     # 0x1d4d3
callq 0x3120
movq %r14, %rdi
movq %r12, %rbp
movq %r12, %rsi
callq 0x839b
leaq 0x1800a(%rip), %rdi     # 0x1d4f1
callq 0x3120
leaq 0x1801f(%rip), %rdi     # 0x1d512
callq 0x3120
movq %r14, %rdi
callq 0x825a
leaq 0x18037(%rip), %rdi     # 0x1d53e
callq 0x3120
leaq 0x17c72(%rip), %rsi     # 0x1d185
movq %rbx, %rdi
callq 0x3530
movq %rax, %r12
testq %rax, %rax
jne 0x5534
leaq 0x17da4(%rip), %rdi     # 0x1d2ce
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3210
movq %r14, %rdi
movl 0x24(%rsp), %esi
movq %r12, %rdx
callq 0xbce1
movq %r12, %rdi
callq 0x31c0
leaq 0x17da3(%rip), %rdi     # 0x1d2f5
movq %rbx, %rsi
xorl %eax, %eax
callq 0x3210
testq %r13, %r13
je 0x5575
leaq 0x18005(%rip), %rdi     # 0x1d56d
callq 0x3120
movq %r13, %rdi
callq 0x142d1
leaq 0x18008(%rip), %rdi     # 0x1d584
callq 0x3120
movq %r15, %rdi
callq 0x142d1
testq %rbp, %rbp
je 0x5596
movq %rbp, %rdi
callq 0x3070
movq %r14, %rdi
callq 0xc1eb
xorl %eax, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17c8f(%rip), %rdi     # 0x1d248
movq %rbp, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0x3210
jmp 0x55f8
leaq 0x17c07(%rip), %rsi     # 0x1d1d6
movq %rbp, %rdi
callq 0x3330
testl %eax, %eax
jne 0x5607
callq 0x3709
callq 0x3742
leaq 0x17cac(%rip), %rdi     # 0x1d298
leaq 0x60(%rsp), %rsi
xorl %eax, %eax
callq 0x3210
movl $0x1, %edi
callq 0x35c0
callq 0x376d
leaq 0x17bf3(%rip), %rsi     # 0x1d201
movq %rbp, %rdi
callq 0x3330
testl %eax, %eax
jne 0x5626
callq 0x4f1c
xorl %edi, %edi
callq 0x35c0
movq %rbp, %rdi
callq 0x36e0
nop