// File: /workspace/github_cpp_makefile_star2_O1/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x6e8, %rsp            # imm = 0x6E8
movq %rdi, %rbx
movl $0x18, %edi
callq 0x2180
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
leaq 0x162df(%rip), %rsi     # 0x2dd08
movq %rbx, %rdi
callq 0x26387
movq %rax, %r12
leaq 0x290(%rsp), %rbp
leaq 0x300(%rsp), %r15
movq %r14, 0x50(%rsp)
movq %rax, 0x48(%rsp)
movq %r12, %rdi
callq 0x26258
testq %rax, %rax
je 0x17d6b
movq %rax, %rbx
movq %rax, %rdi
callq 0x17dac
testb %al, %al
jne 0x17d5a
movq %rbx, 0x28(%rsp)
movq %rbx, %rdi
leaq 0x16f80(%rip), %rsi     # 0x2ea01
movq %rbp, %rdx
leaq 0x44(%rsp), %rcx
leaq 0x40(%rsp), %r8
movq %r15, %r9
xorl %eax, %eax
leaq 0x140(%rsp), %r10
pushq %r10
leaq 0x1b8(%rsp), %r10
pushq %r10
leaq 0x2c(%rsp), %r10
pushq %r10
leaq 0x238(%rsp), %r10
pushq %r10
leaq 0x78(%rsp), %r10
pushq %r10
leaq 0x84(%rsp), %r10
pushq %r10
leaq 0x3f(%rsp), %r10
pushq %r10
leaq 0x74(%rsp), %r10
pushq %r10
callq 0x2160
addq $0x40, %rsp
cmpl $0xc, %eax
jne 0x17d92
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, 0x10(%rsp)
movl 0x44(%rsp), %ecx
movl %ecx, 0xc(%rax)
movl 0x40(%rsp), %ecx
movl %ecx, 0x20(%rax)
movq %rbp, %rdi
callq 0x273e0
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0x12f1c
movq %r15, %rdi
callq 0x273e0
movq 0x10(%rsp), %rcx
movq %rax, 0x18(%rcx)
movb 0xf(%rsp), %al
movb %al, 0x8(%rcx)
xorl %ebx, %ebx
movq %rbx, 0x48(%rcx)
movl 0x3c(%rsp), %eax
movl %eax, 0x10(%rcx)
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movq %rax, %r12
movups %xmm0, (%rax)
movq %rbx, 0x10(%rax)
movl %ebx, 0x38(%rsp)
movl %ebx, 0x34(%rsp)
cmpl %ebx, 0x1c(%rsp)
jle 0x17d19
xorl %r13d, %r13d
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movl $0x0, 0xc0(%rsp)
leaq 0x1b0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movl $0x64, %edx
leaq 0x16e82(%rip), %r14     # 0x2ea5a
movq %r14, %rcx
leaq 0x38(%rsp), %r8
callq 0x283ee
testb %al, %al
leaq 0xd0(%rsp), %r15
je 0x17d88
xorps %xmm0, %xmm0
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movl $0x0, 0x130(%rsp)
leaq 0x140(%rsp), %rdi
movq %r15, %rsi
movl $0x64, %edx
movq %r14, %rcx
leaq 0x34(%rsp), %r8
callq 0x283ee
testb %al, %al
je 0x17d8d
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, 0x20(%rsp)
movq %r15, %rbx
movq %rbp, %r15
movq %rbp, %rdi
callq 0x273e0
movq 0x20(%rsp), %rcx
movq %rax, (%rcx)
movq 0x20(%rsp), %rax
movq (%rax), %rdi
callq 0x12f1c
movq 0x10(%rsp), %rax
movl 0xc(%rax), %r14d
movq %rbx, %rdi
callq 0x2290
movl %eax, %ebp
addl %r14d, %ebp
movq 0x20(%rsp), %r14
movl %ebp, 0xc(%r14)
leaq 0x60(%rsp), %rdi
callq 0x2290
addl %ebp, %eax
movl %eax, 0x20(%r14)
xorl %eax, %eax
movq %rax, 0x18(%r14)
movq %rax, 0x48(%r14)
movb 0xf(%rsp), %al
movb %al, 0x8(%r14)
movq 0x8(%r12), %rsi
cmpq 0x10(%r12), %rsi
je 0x17cf8
movq %r14, (%rsi)
addq $0x8, 0x8(%r12)
jmp 0x17d05
movq %r12, %rdi
leaq 0x20(%rsp), %rdx
callq 0xb976
incl %r13d
cmpl 0x1c(%rsp), %r13d
xorps %xmm0, %xmm0
movq %r15, %rbp
jl 0x17b8a
movq 0x10(%rsp), %rax
movq %r12, 0x30(%rax)
movq 0x50(%rsp), %r14
movq 0x8(%r14), %rsi
cmpq 0x10(%r14), %rsi
je 0x17d3b
movq %rax, (%rsi)
addq $0x8, 0x8(%r14)
jmp 0x17d48
movq %r14, %rdi
leaq 0x10(%rsp), %rdx
callq 0xb976
movq 0x48(%rsp), %r12
leaq 0x300(%rsp), %r15
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x2220
testq %rbx, %rbx
jne 0x17a4e
movq %r12, %rdi
callq 0x2170
movq %r14, %rax
addq $0x6e8, %rsp            # imm = 0x6E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4cbf
callq 0x4ce3
leaq 0x16c8c(%rip), %rdi     # 0x2ea25
movq 0x28(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
xorl %edi, %edi
callq 0x21e0