// File: 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:
subq $0x378, %rsp            # imm = 0x378
movq %rdi, 0x370(%rsp)
movq 0x370(%rsp), %rsi
leaq 0x29525(%rip), %rdi     # 0x56013
movb $0x0, %al
callq 0x2050
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x28(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x28(%rsp), %rdi
callq 0xc670
movq 0x28(%rsp), %rax
movq %rax, 0x368(%rsp)
movq 0x370(%rsp), %rdi
leaq 0x28595(%rip), %rsi     # 0x550ce
callq 0x455f0
movq %rax, 0x360(%rsp)
cmpq $0x0, 0x360(%rsp)
jne 0x2cb6e
movq 0x370(%rsp), %rsi
leaq 0x294cf(%rip), %rdi     # 0x5602f
movb $0x0, %al
callq 0x2050
xorl %edi, %edi
callq 0x21f0
movq 0x360(%rsp), %rdi
callq 0x45400
movq %rax, 0x358(%rsp)
cmpq $0x0, 0x358(%rsp)
je 0x2ce7d
movq 0x358(%rsp), %rdi
callq 0x2b460
andb $0x1, %al
movb %al, 0x357(%rsp)
testb $0x1, 0x357(%rsp)
jne 0x2ce46
movq 0x358(%rsp), %rdi
leaq 0x2e0(%rsp), %rdx
leaq 0x293ea(%rip), %rsi     # 0x55fb7
leaq 0x350(%rsp), %rcx
leaq 0x34c(%rsp), %r8
movb $0x0, %al
callq 0x2170
cmpl $0x3, %eax
je 0x2cc06
movq 0x358(%rsp), %rsi
leaq 0x2945c(%rip), %rdi     # 0x56054
movb $0x0, %al
callq 0x2050
xorl %edi, %edi
callq 0x21f0
movq $0x0, 0x2d8(%rsp)
movq 0x358(%rsp), %rdi
leaq 0x270(%rsp), %rdx
leaq 0x200(%rsp), %rcx
leaq 0x190(%rsp), %r8
leaq 0x120(%rsp), %r9
leaq 0xb0(%rsp), %r10
leaq 0x40(%rsp), %rax
leaq 0x29436(%rip), %rsi     # 0x56084
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0x2170
cmpl $0x6, %eax
je 0x2cc76
leaq 0x2943c(%rip), %rdi     # 0x560a6
movb $0x0, %al
callq 0x2050
jmp 0x2cd8b
movl $0x30, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x20(%rsp)
xorl %esi, %esi
movl $0x30, %edx
callq 0x2110
movq 0x20(%rsp), %rax
movq %rax, 0x2d8(%rsp)
movsbl 0x270(%rsp), %eax
cmpl $0x2e, %eax
je 0x2ccc8
leaq 0x270(%rsp), %rdi
callq 0x2290
movl %eax, %ecx
movq 0x2d8(%rsp), %rax
movl %ecx, 0x4(%rax)
movsbl 0x200(%rsp), %eax
cmpl $0x2e, %eax
je 0x2cce7
movb 0x270(%rsp), %cl
movq 0x2d8(%rsp), %rax
movb %cl, 0x8(%rax)
movsbl 0x190(%rsp), %eax
cmpl $0x2e, %eax
je 0x2cd0e
leaq 0x190(%rsp), %rdi
callq 0x2210
movq 0x2d8(%rsp), %rax
movsd %xmm0, 0x10(%rax)
movsbl 0x120(%rsp), %eax
cmpl $0x2e, %eax
je 0x2cd35
leaq 0x120(%rsp), %rdi
callq 0x2210
movq 0x2d8(%rsp), %rax
movsd %xmm0, 0x18(%rax)
movsbl 0xb0(%rsp), %eax
cmpl $0x2e, %eax
je 0x2cd5c
leaq 0xb0(%rsp), %rdi
callq 0x2210
movq 0x2d8(%rsp), %rax
movsd %xmm0, 0x20(%rax)
movsbl 0x40(%rsp), %eax
cmpl $0x2e, %eax
je 0x2cd7d
leaq 0x40(%rsp), %rdi
callq 0x2290
movl %eax, %ecx
movq 0x2d8(%rsp), %rax
movl %ecx, 0x28(%rax)
movq 0x2d8(%rsp), %rax
movl $0x1234, (%rax)         # imm = 0x1234
movb $0x2b, 0x3f(%rsp)
movl $0x50, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x18(%rsp)
xorl %esi, %esi
movl $0x50, %edx
callq 0x2110
movq 0x18(%rsp), %rax
movq %rax, 0x30(%rsp)
movl 0x350(%rsp), %edi
xorl %esi, %esi
movl $0x1, %edx
callq 0x1fd20
movl %eax, %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0x34c(%rsp), %edi
movl $0x1, %edx
movl %edx, %esi
callq 0x1fd20
movl %eax, %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x20(%rax)
leaq 0x2e0(%rsp), %rdi
callq 0x47b60
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, (%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rdi
callq 0x22350
movb 0x3f(%rsp), %cl
movq 0x30(%rsp), %rax
movb %cl, 0x8(%rax)
movq 0x2d8(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x48(%rax)
movq 0x368(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xc8c0
movq 0x358(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x2ce63
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x360(%rsp), %rdi
callq 0x45400
movq %rax, 0x358(%rsp)
jmp 0x2cb83
movq 0x360(%rsp), %rdi
callq 0x2180
movq 0x368(%rsp), %rdi
callq 0xc680
movq %rax, %rsi
movq 0x370(%rsp), %rdx
leaq 0x29245(%rip), %rdi     # 0x560ee
movb $0x0, %al
callq 0x2050
movq 0x368(%rsp), %rax
addq $0x378, %rsp            # imm = 0x378
retq