// File: gersteinlab[P]MUSIC/src/ms_annot_region_tools.cpp
// Function: load_BED12(char*)
vector<t_annot_region*>* load_BED12(char* bed_fp)
{
	vector<t_annot_region*>* bed_regions = new vector<t_annot_region*>();

	FILE* f_bed = open_f(bed_fp, "r");
	
	while(1)
	{
		char* cur_line = getline(f_bed);
		if(cur_line == NULL)
		{
			break;
		}

		bool skip_line = check_line_skip(cur_line);

		if(!skip_line)
		{
			// Read the mandatory fields.
			int start;
			int end;
			char chrom[100];
			char name[1000];
			int score;
			char strand;
			int thick_start;
			int thick_end;
			char rgb[100];
			int n_exons;
			char block_sizes[100];
			char block_starts[100];
			// chrI    4283    5269    JUNC00000001    90      -       4283    5269    255,0,0 2       75,75   0,911
			if(sscanf(cur_line, "%s %d %d %s %d %c %d %d %s %d %s %s", chrom, &start, &end, name, &score, &strand, &thick_start, &thick_end, rgb, &n_exons, block_sizes, block_starts) != 12)
			{
				printf("Could not read the 12 fields from BED file line:\n%s\n", cur_line);
				exit(0);
			}

			t_annot_region* new_region = new t_annot_region();
			// Translate the start and end of the BED coordinates to the codebase coordinates.
			//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->start = start;
			new_region->end = end;

			new_region->chrom = t_string::copy_me_str(chrom);
			normalize_chr_id(new_region->chrom);
			new_region->name = t_string::copy_me_str(name);
			new_region->strand = strand;
			new_region->data = NULL;

			// The score is important since it tells how strong this junction is. (Total # of spanning junction reads.)
			new_region->score = score;

			vector<t_annot_region*>* intervals = new vector<t_annot_region*>();

			// Setup all the interval regions.
			int i_block_sizes_str_char = 0;
			int i_block_starts_str_char = 0;
            //fprintf(stderr, "%s\n", cur_line);
			for(int i_ex = 0; i_ex < n_exons; i_ex++)
			{
				char cur_block_size_str[100];
				memset(cur_block_size_str, 0, 100);
				if(!t_string::get_next_token(block_sizes, cur_block_size_str, 100, ",", i_block_sizes_str_char))
				{
					fprintf(stderr, "Could not parse the block sizes string.\n");
					exit(0);
				}

				char cur_block_start_str[100];
				memset(cur_block_start_str, 0, 100);
				if(!t_string::get_next_token(block_starts, cur_block_start_str, 100, ",", i_block_starts_str_char))
				{
					fprintf(stderr, "Could not parse the block starts string.\n");
					exit(0);
				}

				t_annot_region* cur_block = new t_annot_region();
				cur_block->chrom = t_string::copy_me_str(chrom);
				normalize_chr_id(cur_block->chrom);
				cur_block->start = new_region->start + atoi(cur_block_start_str);
				cur_block->end = cur_block->start + atoi(cur_block_size_str);
				cur_block->name = NULL;
				cur_block->data = NULL;
				cur_block->strand = strand;

				//fprintf(stderr, "Block in %s:%d-%d: %d-%d\n", chrom, start, end, cur_block->start, cur_block->end);
				//if(cur_block->start < start || cur_block->end > end)
				//{
				//	fprintf(stderr, "WTF: %s, %s (%s, %s)\n", block_sizes, block_starts, cur_block_start_str, cur_block_size_str);
				//	getc(stdin);
				//}

				intervals->push_back(cur_block);
			} // i_ex loop.
			//getc(stdin);

			new_region->intervals = intervals;
		
			bed_regions->push_back(new_region);
		} // line skip check.

		delete [] cur_line;
	} // file reading loop.

	fclose(f_bed);

	//printf("Loaded %d regions from %s\n", bed_regions->size(), bed_fp);

	return(bed_regions);
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x750, %rsp            # imm = 0x750
movq %rdi, 0x748(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x70(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x70(%rsp), %rdi
callq 0xc670
movq 0x70(%rsp), %rax
movq %rax, 0x740(%rsp)
movq 0x748(%rsp), %rdi
leaq 0x2a0e2(%rip), %rsi     # 0x550ce
callq 0x455f0
movq %rax, 0x738(%rsp)
movq 0x738(%rsp), %rdi
callq 0x45400
movq %rax, 0x730(%rsp)
cmpq $0x0, 0x730(%rsp)
jne 0x2b01e
jmp 0x2b42f
movq 0x730(%rsp), %rdi
callq 0x2b460
andb $0x1, %al
movb %al, 0x72f(%rsp)
testb $0x1, 0x72f(%rsp)
jne 0x2b40d
movq 0x730(%rsp), %rdi
leaq 0x6c0(%rsp), %rdx
leaq 0x2d0(%rsp), %r9
leaq 0x250(%rsp), %rbx
leaq 0x1e0(%rsp), %r10
leaq 0x170(%rsp), %rax
leaq 0x2ad48(%rip), %rsi     # 0x55dc1
leaq 0x728(%rsp), %rcx
leaq 0x724(%rsp), %r8
leaq 0x2cc(%rsp), %r13
leaq 0x2cb(%rsp), %r12
leaq 0x2c4(%rsp), %r15
leaq 0x2c0(%rsp), %r14
leaq 0x24c(%rsp), %r11
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movb $0x0, %al
callq 0x2170
cmpl $0xc, %eax
je 0x2b101
movq 0x730(%rsp), %rsi
leaq 0x2acf2(%rip), %rdi     # 0x55de5
movb $0x0, %al
callq 0x2050
xorl %edi, %edi
callq 0x21f0
movl $0x50, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x60(%rsp)
xorl %esi, %esi
movl $0x50, %edx
callq 0x2110
movq 0x60(%rsp), %rax
movq %rax, 0x168(%rsp)
movl 0x728(%rsp), %ecx
movq 0x168(%rsp), %rax
movl %ecx, 0xc(%rax)
movl 0x724(%rsp), %ecx
movq 0x168(%rsp), %rax
movl %ecx, 0x20(%rax)
leaq 0x6c0(%rsp), %rdi
callq 0x47b60
movq %rax, %rcx
movq 0x168(%rsp), %rax
movq %rcx, (%rax)
movq 0x168(%rsp), %rax
movq (%rax), %rdi
callq 0x22350
leaq 0x2d0(%rsp), %rdi
callq 0x47b60
movq %rax, %rcx
movq 0x168(%rsp), %rax
movq %rcx, 0x18(%rax)
movb 0x2cb(%rsp), %cl
movq 0x168(%rsp), %rax
movb %cl, 0x8(%rax)
movq 0x168(%rsp), %rax
movq $0x0, 0x48(%rax)
movl 0x2cc(%rsp), %ecx
movq 0x168(%rsp), %rax
movl %ecx, 0x10(%rax)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x68(%rsp)
xorl %esi, %esi
movl $0x18, %edx
callq 0x2110
movq 0x68(%rsp), %rdi
callq 0xc670
movq 0x68(%rsp), %rax
movq %rax, 0x160(%rsp)
movl $0x0, 0x15c(%rsp)
movl $0x0, 0x158(%rsp)
movl $0x0, 0x154(%rsp)
movl 0x154(%rsp), %eax
cmpl 0x24c(%rsp), %eax
jge 0x2b3e4
leaq 0xf0(%rsp), %rdi
xorl %esi, %esi
movl $0x64, %edx
callq 0x2110
leaq 0x1e0(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
movl $0x64, %edx
leaq 0x2abb5(%rip), %rcx     # 0x55e1a
leaq 0x15c(%rsp), %r8
callq 0x491d0
testb $0x1, %al
jne 0x2b295
movq 0x39d63(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x2ab95(%rip), %rsi     # 0x55e1c
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movl $0x64, %edx
callq 0x2110
leaq 0x170(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movl $0x64, %edx
leaq 0x2ab55(%rip), %rcx     # 0x55e1a
leaq 0x158(%rsp), %r8
callq 0x491d0
testb $0x1, %al
jne 0x2b2f5
movq 0x39d03(%rip), %rax     # 0x64fe0
movq (%rax), %rdi
leaq 0x2ab5e(%rip), %rsi     # 0x55e45
movb $0x0, %al
callq 0x2250
xorl %edi, %edi
callq 0x21f0
movl $0x50, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x50(%rsp)
xorl %esi, %esi
movl $0x50, %edx
callq 0x2110
movq 0x50(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0x6c0(%rsp), %rdi
callq 0x47b60
movq %rax, %rcx
movq 0x78(%rsp), %rax
movq %rcx, (%rax)
movq 0x78(%rsp), %rax
movq (%rax), %rdi
callq 0x22350
movq 0x168(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x58(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x2290
movl 0x58(%rsp), %ecx
addl %eax, %ecx
movq 0x78(%rsp), %rax
movl %ecx, 0xc(%rax)
movq 0x78(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0x5c(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x2290
movl 0x5c(%rsp), %ecx
addl %eax, %ecx
movq 0x78(%rsp), %rax
movl %ecx, 0x20(%rax)
movq 0x78(%rsp), %rax
movq $0x0, 0x18(%rax)
movq 0x78(%rsp), %rax
movq $0x0, 0x48(%rax)
movb 0x2cb(%rsp), %cl
movq 0x78(%rsp), %rax
movb %cl, 0x8(%rax)
movq 0x160(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xc8c0
movl 0x154(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x154(%rsp)
jmp 0x2b221
movq 0x160(%rsp), %rcx
movq 0x168(%rsp), %rax
movq %rcx, 0x30(%rax)
movq 0x740(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0xc8c0
movq 0x730(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, %rax
je 0x2b42a
movq 0x48(%rsp), %rdi
callq 0x2220
jmp 0x2aff9
movq 0x738(%rsp), %rdi
callq 0x2180
movq 0x740(%rsp), %rax
addq $0x750, %rsp            # imm = 0x750
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
nopw %cs:(%rax,%rax)