// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x18
popq %rdi
callq 0x2170
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq %rax, 0x38(%rsp)
andq $0x0, 0x10(%rax)
leaq 0x131e2(%rip), %rsi     # 0x26d08
movq %rbx, %rdi
callq 0x2027c
movq %rax, %rbx
leaq 0x290(%rsp), %r14
leaq 0x300(%rsp), %r15
leaq 0x13f12(%rip), %r12     # 0x27a5a
pushq $0x64
popq %r13
movq %rax, 0x50(%rsp)
movq %rbx, %rdi
callq 0x2017f
testq %rax, %rax
je 0x13e33
movq %rax, %rdi
movq %rax, %rbp
callq 0x13e9f
testb %al, %al
jne 0x13e26
movq %rbp, 0x40(%rsp)
movq %rbp, %rdi
leaq 0x13e7d(%rip), %rsi     # 0x27a01
movq %r14, %rdx
leaq 0x34(%rsp), %rcx
leaq 0x30(%rsp), %r8
movq %r15, %r9
xorl %eax, %eax
leaq 0x140(%rsp), %r10
pushq %r10
leaq 0x1b8(%rsp), %r10
pushq %r10
leaq 0x38(%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 0x64(%rsp), %r10
pushq %r10
callq 0x2150
addq $0x40, %rsp
cmpl $0xc, %eax
jne 0x13e8a
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, 0x10(%rsp)
movl 0x34(%rsp), %ecx
movl %ecx, 0xc(%rax)
movl 0x30(%rsp), %ecx
movl %ecx, 0x20(%rax)
movq %r14, %rdi
callq 0x21062
movq 0x10(%rsp), %rcx
movq %rax, (%rcx)
movq 0x10(%rsp), %rax
movq (%rax), %rdi
callq 0xf63c
movq %r15, %rdi
callq 0x21062
movq 0x10(%rsp), %rcx
movq %rax, 0x18(%rcx)
movb 0xf(%rsp), %al
movb %al, 0x8(%rcx)
andq $0x0, 0x48(%rcx)
movl 0x2c(%rsp), %eax
movl %eax, 0x10(%rcx)
pushq $0x18
popq %rdi
callq 0x2170
andq $0x0, 0x10(%rax)
movq %rax, 0x48(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andl $0x0, 0x24(%rsp)
andl $0x0, 0x20(%rsp)
xorl %ebx, %ebx
cmpl 0x28(%rsp), %ebx
jge 0x13df7
xorps %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
andl $0x0, 0xc0(%rsp)
leaq 0x1b0(%rsp), %rdi
leaq 0x60(%rsp), %rsi
movl %r13d, %edx
movq %r12, %rcx
leaq 0x24(%rsp), %r8
callq 0x21c5c
testb %al, %al
leaq 0xd0(%rsp), %rbp
je 0x13e52
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)
andl $0x0, 0x130(%rsp)
leaq 0x140(%rsp), %rdi
movq %rbp, %rsi
movl %r13d, %edx
movq %r12, %rcx
leaq 0x20(%rsp), %r8
callq 0x21c5c
testb %al, %al
je 0x13e67
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, 0x18(%rsp)
movq %r14, %rdi
callq 0x21062
movq 0x18(%rsp), %rcx
movq %rax, (%rcx)
movq 0x18(%rsp), %rax
movq (%rax), %rdi
callq 0xf63c
movq 0x10(%rsp), %rax
movl 0xc(%rax), %r15d
movq %rbp, %rdi
callq 0x2280
movl %r13d, %ebp
movq %r12, %r13
movq %r14, %r12
movl %eax, %r14d
addl %r15d, %r14d
movq 0x18(%rsp), %r15
movl %r14d, 0xc(%r15)
leaq 0x60(%rsp), %rdi
callq 0x2280
addl %r14d, %eax
movq %r12, %r14
movq %r13, %r12
movl %ebp, %r13d
movl %eax, 0x20(%r15)
andq $0x0, 0x18(%r15)
andq $0x0, 0x48(%r15)
movb 0xf(%rsp), %al
movb %al, 0x8(%r15)
movq 0x48(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x7448
incl %ebx
jmp 0x13c82
movq 0x10(%rsp), %rax
movq 0x48(%rsp), %rcx
movq %rcx, 0x30(%rax)
movq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x7448
movq 0x50(%rsp), %rbx
leaq 0x300(%rsp), %r15
movq 0x40(%rsp), %rbp
movq %rbp, %rdi
callq 0x2210
jmp 0x13b51
movq %rbx, %rdi
callq 0x2160
movq 0x38(%rsp), %rax
addq $0x6e8, %rsp            # imm = 0x6E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x21187(%rip), %rax     # 0x34fe0
movq (%rax), %rcx
leaq 0x13bf9(%rip), %rdi     # 0x27a5c
pushq $0x28
jmp 0x13e7a
movq 0x21172(%rip), %rax     # 0x34fe0
movq (%rax), %rcx
leaq 0x13c0d(%rip), %rdi     # 0x27a85
pushq $0x29
popq %rsi
pushq $0x1
popq %rdx
callq 0x2330
xorl %edi, %edi
callq 0x21d0
leaq 0x13b94(%rip), %rdi     # 0x27a25
movq 0x40(%rsp), %rsi
xorl %eax, %eax
callq 0x2050
jmp 0x13e83