// File: /workspace/github_cpp_makefile_star2_O3/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, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
leaq 0x1564d(%rip), %rsi     # 0x2cd08
movq %rbx, %rdi
callq 0x25d5a
movq %rax, %rbx
movq %rax, %rdi
callq 0x25c1e
testq %rax, %rax
je 0x17a10
movq %rax, %r14
leaq 0x290(%rsp), %rbp
leaq 0x16371(%rip), %r13     # 0x2da5a
leaq 0xd0(%rsp), %r15
movq %r12, 0x50(%rsp)
movq %rbx, 0x40(%rsp)
movq %r14, %rdi
callq 0x17a51
testb %al, %al
jne 0x179f4
movq %r14, 0x20(%rsp)
movq %r14, %rdi
leaq 0x162e7(%rip), %rsi     # 0x2da01
movq %rbp, %rdx
leaq 0x3c(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x300(%rsp), %rbx
movq %rbx, %r9
xorl %eax, %eax
leaq 0x140(%rsp), %r10
pushq %r10
leaq 0x1b8(%rsp), %r10
pushq %r10
leaq 0x24(%rsp), %r10
pushq %r10
leaq 0x238(%rsp), %r10
pushq %r10
leaq 0x78(%rsp), %r10
pushq %r10
leaq 0x84(%rsp), %r10
pushq %r10
leaq 0x37(%rsp), %r10
pushq %r10
leaq 0x6c(%rsp), %r10
pushq %r10
callq 0x2160
addq $0x40, %rsp
cmpl $0xc, %eax
jne 0x17a37
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, 0x8(%rsp)
movl 0x3c(%rsp), %ecx
movl %ecx, 0xc(%rax)
movl 0x38(%rsp), %ecx
movl %ecx, 0x20(%rax)
movq %rbp, %rdi
callq 0x26db4
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movq 0x8(%rsp), %rax
movq (%rax), %rdi
callq 0x12c04
movq %rbx, %rdi
callq 0x26db4
movq 0x8(%rsp), %r14
movq %rax, 0x18(%r14)
movb 0x7(%rsp), %al
movb %al, 0x8(%r14)
xorl %ebx, %ebx
movq %rbx, 0x48(%r14)
movl 0x34(%rsp), %eax
movl %eax, 0x10(%r14)
movl $0x18, %edi
callq 0x2180
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x48(%rsp)
movq %rbx, 0x10(%rax)
movl %ebx, 0x30(%rsp)
movl %ebx, 0x2c(%rsp)
cmpl %ebx, 0x14(%rsp)
jle 0x179b8
xorl %r14d, %r14d
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
movq %r13, %rcx
leaq 0x30(%rsp), %r8
callq 0x27d40
testb %al, %al
je 0x17a32
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 %r13, %rcx
leaq 0x2c(%rsp), %r8
callq 0x27d40
testb %al, %al
je 0x17a2d
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, 0x18(%rsp)
movq %rbp, %r13
movq %rbp, %rdi
callq 0x26db4
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq 0x18(%rsp), %rax
movq (%rax), %rdi
callq 0x12c04
movq 0x8(%rsp), %rax
movl 0xc(%rax), %ebx
movq %r15, %r12
movq %r15, %rdi
callq 0x2290
movl %eax, %ebp
addl %ebx, %ebp
movq 0x18(%rsp), %rbx
movl %ebp, 0xc(%rbx)
leaq 0x60(%rsp), %rdi
callq 0x2290
addl %ebp, %eax
movl %eax, 0x20(%rbx)
xorl %eax, %eax
movq %rax, 0x18(%rbx)
movq %rax, 0x48(%rbx)
movb 0x7(%rsp), %al
movb %al, 0x8(%rbx)
movq 0x48(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x1798b
movq %rbx, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x17995
leaq 0x18(%rsp), %rdx
callq 0xb74c
incl %r14d
cmpl 0x14(%rsp), %r14d
xorps %xmm0, %xmm0
movq %r13, %rbp
leaq 0x160b0(%rip), %r13     # 0x2da5a
movq %r12, %r15
jl 0x1782f
movq 0x8(%rsp), %r14
movq 0x48(%rsp), %rax
movq %rax, 0x30(%r14)
movq 0x50(%rsp), %r12
movq 0x8(%r12), %rsi
cmpq 0x10(%r12), %rsi
je 0x179dd
movq %r14, (%rsi)
addq $0x8, 0x8(%r12)
jmp 0x179ea
movq %r12, %rdi
leaq 0x8(%rsp), %rdx
callq 0xb74c
movq 0x40(%rsp), %rbx
movq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x25c1e
movq %rax, %r14
testq %rax, %rax
jne 0x176fb
movq %rbx, %rdi
callq 0x2170
movq %r12, %rax
addq $0x6e8, %rsp            # imm = 0x6E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x4b78
callq 0x4b54
leaq 0x15fe7(%rip), %rdi     # 0x2da25
movq 0x20(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
xorl %edi, %edi
callq 0x21e0