// File: /workspace/github_cpp_makefile_star2_O2/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 0x12f41(%rip), %rdi     # 0x27c53
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2050
pushq $0x18
popq %rdi
callq 0x2170
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x11fcf(%rip), %rsi     # 0x26d08
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x2027c
testq %rax, %rax
je 0x14fa9
movq %rax, %r15
movq %rax, %rdi
callq 0x2017f
movq %rax, %r12
leaq 0x12e93(%rip), %rbx     # 0x27bf7
leaq 0x2c0(%rsp), %rbp
movq %r15, 0x18(%rsp)
testq %r12, %r12
je 0x14f56
movq %r12, %rdi
callq 0x13e9f
testb %al, %al
jne 0x14f3e
movq %r12, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
leaq 0xc(%rsp), %rcx
leaq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0x2150
cmpl $0x3, %eax
jne 0x14f91
movq %r12, %rdi
leaq 0x12f0d(%rip), %rsi     # 0x27cc4
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 0x2150
popq %rcx
popq %rdx
cmpl $0x6, %eax
jne 0x14ecb
movq %r14, %r15
movq %rbp, %r14
movq %rbx, %rbp
pushq $0x30
popq %rdi
callq 0x2170
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 0x14e39
leaq 0x1e0(%rsp), %rdi
callq 0x2280
movl %eax, 0x4(%r13)
cmpb $0x2e, 0x250(%rsp)
je 0x14e47
movb %bl, 0x8(%r13)
cmpb $0x2e, 0x170(%rsp)
je 0x14e64
leaq 0x170(%rsp), %rdi
callq 0x21f0
movsd %xmm0, 0x10(%r13)
cmpb $0x2e, 0x100(%rsp)
movq %rbp, %rbx
je 0x14e84
leaq 0x100(%rsp), %rdi
callq 0x21f0
movsd %xmm0, 0x18(%r13)
cmpb $0x2e, 0x90(%rsp)
movq %r14, %rbp
je 0x14ea4
leaq 0x90(%rsp), %rdi
callq 0x21f0
movsd %xmm0, 0x20(%r13)
cmpb $0x2e, 0x20(%rsp)
movq %r15, %r14
je 0x14ebc
leaq 0x20(%rsp), %rdi
callq 0x2280
movl %eax, 0x28(%r13)
movl $0x1234, (%r13)         # imm = 0x1234
movq 0x18(%rsp), %r15
jmp 0x14eda
leaq 0x1300e(%rip), %rdi     # 0x27ee0
callq 0x2270
xorl %r13d, %r13d
pushq $0x50
popq %rdi
callq 0x2170
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 0x21062
movq (%rsp), %rcx
movq %rax, (%rcx)
movq (%rsp), %rax
movq (%rax), %rdi
callq 0xf63c
movq (%rsp), %rax
movb $0x2b, 0x8(%rax)
movq %r13, 0x48(%rax)
movq %r14, %rdi
movq %rsp, %rsi
callq 0x7448
movq %r12, %rdi
callq 0x2210
movq %r15, %rdi
callq 0x2017f
movq %rax, %r12
jmp 0x14d71
movq %r15, %rdi
callq 0x2160
movq 0x8(%r14), %rsi
subq (%r14), %rsi
sarq $0x3, %rsi
leaq 0x12d76(%rip), %rdi     # 0x27ce6
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 0x12cfc(%rip), %rdi     # 0x27c94
movq %r12, %rsi
xorl %eax, %eax
callq 0x2050
xorl %edi, %edi
callq 0x21d0
leaq 0x12cbf(%rip), %rdi     # 0x27c6f
movq 0x10(%rsp), %rsi
jmp 0x14f9b