// File: /workspace/github_cpp_makefile_star2_O3/gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: load_narrowPeak(char*)
vector<t_annot_region*>* load_narrowPeak(char* narrowPeak_fp)
{
	printf("Loading narrowPeak file %s\n", narrowPeak_fp);

	vector<t_annot_region*>* np_regions = new vector<t_annot_region*>();

	FILE* f_np = open_f(narrowPeak_fp, "r");
	if(f_np == NULL)
	{
		printf("Could not open narrowPeak file @ %s\n", narrowPeak_fp);
		exit(0);
	}

	char* cur_line = getline(f_np);
	while(cur_line != NULL)
	{
		bool skip_line = check_line_skip(cur_line);

		if(!skip_line)
		{
			int start;
			int end;
			char chrom[100];

			if(sscanf(cur_line, "%s %d %d", chrom, &start, &end) != 3)
			{
				printf("Could not parse narrowPeak file entry line:\n%s\n", cur_line);
				exit(0);
			}

			t_narrowPeak_info* new_np_info = NULL;
			char score_str[100];
			char strand_str[100];
			char signal_val_str[100];
			char pval_str[100];
			char qval_str[100];
			char peak_pos_str[100];
			if(sscanf(cur_line, "%*s %*s %*s %*s %s %s %s %s %s %s",
								score_str,
								strand_str,
								signal_val_str,
								pval_str,
								qval_str,
								peak_pos_str) != 6)
			{
				printf("Could not read narrowPeak entries, not setting custom narrowPeak info.\n");
			}
			else
			{
				new_np_info = new t_narrowPeak_info();
				if(score_str[0] != '.')
				{
					new_np_info->score = atoi(score_str);
				}

				if(strand_str[0] != '.')
				{
					new_np_info->strand = score_str[0];
				}

				if(signal_val_str[0] != '.')
				{
					new_np_info->signal_value = atof(signal_val_str);
				}

				if(pval_str[0] != '.')
				{
					new_np_info->p_value = atof(pval_str);
				}

				if(qval_str[0] != '.')
				{
					new_np_info->q_value = atof(qval_str);
				}

				if(peak_pos_str[0] != '.')
				{
					new_np_info->peak_pos = atoi(peak_pos_str);
					//printf("Setting peak position at %d\n", new_np_info->peak_pos);
				}

				new_np_info->info_type = NARROWPEAK_INFO;
			}

			// narrowPeak files do not contain strand information, everything is on forward strand.
			char strand = '+'; 

			// Allocate and initialize the new region.
			t_annot_region* new_region = new t_annot_region();
			//new_region->start = start - BED_START_BASE + CODEBASE_START_BASE;
			//new_region->end = end - BED_END_BASE + CODEBASE_END_BASE;
			new_region->start = translate_coord(start, BED_COORDS::start_base, CODEBASE_COORDS::start_base);
			new_region->end = translate_coord(end, BED_COORDS::end_base, CODEBASE_COORDS::end_base);
			new_region->chrom = t_string::copy_me_str(chrom);
			normalize_chr_id(new_region->chrom);
			new_region->strand = strand;
			new_region->data = new_np_info;

			// Add the new region to the list.
			np_regions->push_back(new_region);
		}

		delete [] cur_line;
		cur_line = getline(f_np);
	} // narrowPeak file reading loop.

	fclose(f_np);

	printf("Loaded %ld regions from %s\n", np_regions->size(), narrowPeak_fp);
	return(np_regions);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x328, %rsp            # imm = 0x328
movq %rdi, %rbx
leaq 0x14fa3(%rip), %rdi     # 0x2dc53
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
movl $0x18, %edi
callq 0x2180
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
leaq 0x1402c(%rip), %rsi     # 0x2cd08
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x25d5a
testq %rax, %rax
je 0x18f64
movq %rax, %r15
movq %rax, %rdi
callq 0x25c1e
testq %rax, %rax
je 0x18f11
movq %rax, %r12
leaq 0x14ee7(%rip), %rbx     # 0x2dbf7
leaq 0x2c0(%rsp), %rbp
movq %r15, 0x18(%rsp)
movq %r12, %rdi
callq 0x17a51
testb %al, %al
jne 0x18ef5
movq %r12, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
leaq 0xc(%rsp), %rcx
leaq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0x2160
cmpl $0x3, %eax
jne 0x18f4c
movq %r12, %rdi
leaq 0x14f6a(%rip), %rsi     # 0x2dcc4
leaq 0x1e0(%rsp), %rdx
leaq 0x250(%rsp), %rcx
leaq 0x170(%rsp), %r8
leaq 0x100(%rsp), %r9
xorl %eax, %eax
leaq 0x20(%rsp), %r10
pushq %r10
leaq 0x98(%rsp), %r10
pushq %r10
callq 0x2160
addq $0x10, %rsp
cmpl $0x6, %eax
jne 0x18e6c
movq %rbp, %r15
movq %rbx, %rbp
movl $0x30, %edi
callq 0x2180
movq %rax, %r13
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movb 0x1e0(%rsp), %bl
cmpb $0x2e, %bl
je 0x18ddd
leaq 0x1e0(%rsp), %rdi
callq 0x2290
movl %eax, 0x4(%r13)
cmpb $0x2e, 0x250(%rsp)
je 0x18deb
movb %bl, 0x8(%r13)
cmpb $0x2e, 0x170(%rsp)
je 0x18e08
leaq 0x170(%rsp), %rdi
callq 0x2200
movsd %xmm0, 0x10(%r13)
cmpb $0x2e, 0x100(%rsp)
movq %rbp, %rbx
je 0x18e28
leaq 0x100(%rsp), %rdi
callq 0x2200
movsd %xmm0, 0x18(%r13)
cmpb $0x2e, 0x90(%rsp)
movq %r15, %rbp
je 0x18e48
leaq 0x90(%rsp), %rdi
callq 0x2200
movsd %xmm0, 0x20(%r13)
cmpb $0x2e, 0x20(%rsp)
movq 0x18(%rsp), %r15
je 0x18e62
leaq 0x20(%rsp), %rdi
callq 0x2290
movl %eax, 0x28(%r13)
movl $0x1234, (%r13)         # imm = 0x1234
jmp 0x18e7b
leaq 0x1506d(%rip), %rdi     # 0x2dee0
callq 0x2280
xorl %r13d, %r13d
movl $0x50, %edi
callq 0x2180
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movq %rax, (%rsp)
movl 0xc(%rsp), %ecx
incl %ecx
movl %ecx, 0xc(%rax)
movl 0x8(%rsp), %ecx
movl %ecx, 0x20(%rax)
movq %rbp, %rdi
callq 0x26db4
movq (%rsp), %rcx
movq %rax, (%rcx)
movq (%rsp), %rax
movq (%rax), %rdi
callq 0x12c04
movq (%rsp), %rax
movb $0x2b, 0x8(%rax)
movq %r13, 0x48(%rax)
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x18eea
movq %rax, (%rsi)
addq $0x8, 0x8(%r14)
jmp 0x18ef5
movq %r14, %rdi
movq %rsp, %rdx
callq 0xb74c
movq %r12, %rdi
callq 0x2220
movq %r15, %rdi
callq 0x25c1e
movq %rax, %r12
testq %rax, %rax
jne 0x18d1d
movq %r15, %rdi
callq 0x2170
movq 0x8(%r14), %rsi
subq (%r14), %rsi
sarq $0x3, %rsi
leaq 0x14dbb(%rip), %rdi     # 0x2dce6
movq 0x10(%rsp), %rdx
xorl %eax, %eax
callq 0x2050
movq %r14, %rax
addq $0x328, %rsp            # imm = 0x328
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x14d41(%rip), %rdi     # 0x2dc94
movq %r12, %rsi
xorl %eax, %eax
callq 0x2050
xorl %edi, %edi
callq 0x21e0
leaq 0x14d04(%rip), %rdi     # 0x2dc6f
movq 0x10(%rsp), %rsi
jmp 0x18f56