// File: /workspace/github_cpp_makefile_star2_O2/gersteinlab[P]MUSIC/src/ms_genome_sequence_tools.cpp
// Function: batch_extract_interval_sequences(char*, std::vector<t_annot_region*, std::allocator<t_annot_region*>>*)
void batch_extract_interval_sequences(char* chr_data_dir, vector<t_annot_region*>* regions)
{
	t_sorted_annot_region_lists* retstr_regions = restructure_annot_regions(regions);

	for(int i_chr = 0; i_chr < (int)retstr_regions->chr_ids->size(); i_chr++)
	{
		fprintf(stderr, "Processing %s\n", retstr_regions->chr_ids->at(i_chr));

		vector<t_annot_region*>* cur_chr_regions = retstr_regions->regions_per_chrom[i_chr];
		for(int i_reg = 0; i_reg < (int)cur_chr_regions->size(); i_reg++)
		{
			// Process the current transcript: Merge the exons
			vector<t_annot_region*>* merged_exons = merge_annot_regions(cur_chr_regions->at(i_reg)->intervals, 0, true);

			// Sort the merged regions.
			sort(merged_exons->begin(), merged_exons->end(), sort_regions);

			t_string* cur_transcript_seq = new t_string();

			// Extract the sequence for all the exons.
			for(int i_ex = 0; i_ex < (int)merged_exons->size(); i_ex++)
			{
				if(merged_exons->at(i_ex)->strand == '-')
				{
					merged_exons->at(i_ex)->strand = '+';
				}

				char* cur_exon_seq = extract_sequence(chr_data_dir, retstr_regions->chr_ids->at(i_chr), merged_exons->at(i_ex)->start, merged_exons->at(i_ex)->end, merged_exons->at(i_ex)->strand);
				cur_transcript_seq->concat_string(cur_exon_seq);
				delete [] cur_exon_seq;
			} // i_ex loop.

			// If the transcript is on negative strand, revert the sequence.
			char* transcript_seq = t_string::copy_me_str(cur_transcript_seq->str());
			if(cur_chr_regions->at(i_reg)->strand == '-')
			{
				//cur_transcript_seq->revert();
				delete [] transcript_seq;
				transcript_seq = get_reverse_complement(cur_transcript_seq->str(), 0, t_string::string_length(cur_transcript_seq->str())-1);
			}

			// Copy the data.
			cur_chr_regions->at(i_reg)->data = transcript_seq;

			delete(cur_transcript_seq);
			// Memory for merged exons must be freeed.
		} // i_reg loop.
	} // i_chr loop.
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
movq %rsi, %rdi
callq 0x12b99
movq %rax, %r14
pushq $0x8
popq %r15
xorl %ecx, %ecx
movq 0x18(%r14), %rdi
movq 0x8(%rdi), %rax
subq (%rdi), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rcx
jge 0x1d15c
movq %rcx, %r12
movq 0x1808c(%rip), %rax     # 0x34fe0
movq (%rax), %rbx
movq %rcx, %rsi
callq 0x7342
movq (%rax), %rdx
movq %rbx, %rdi
leaq 0x7d31(%rip), %rsi      # 0x24c9d
xorl %eax, %eax
callq 0x2240
movq (%r14), %rax
movq %r12, (%rsp)
movq (%rax,%r12,8), %r13
xorl %ebp, %ebp
movq %r13, 0x8(%rsp)
movq 0x8(%r13), %rax
subq (%r13), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %rbp
jge 0x1d150
movq %r13, %rdi
movq %rbp, 0x10(%rsp)
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movq 0x30(%rax), %rdi
xorl %esi, %esi
pushq $0x1
popq %rdx
callq 0x16489
movq %rax, %rbp
movq (%rax), %rdi
movq 0x8(%rax), %rsi
leaq -0x9b37(%rip), %rdx     # 0x13497
callq 0x17bcf
movq %r15, %rdi
callq 0x2170
movq %rax, %r15
movq %rax, %rdi
callq 0x20cd8
xorl %r12d, %r12d
movq %r15, 0x20(%rsp)
movq 0x8(%rbp), %rax
subq (%rbp), %rax
shrq $0x3, %rax
cltq
cmpq %rax, %r12
jge 0x1d0b6
movq %rbp, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
cmpb $0x2d, 0x8(%rax)
jne 0x1d02b
movq %rbp, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movb $0x2b, 0x8(%rax)
movq %r14, %r13
movq 0x18(%r14), %rdi
movq (%rsp), %rsi
callq 0x7342
movq (%rax), %rbx
movq %rbp, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movl 0xc(%rax), %r14d
movq %rbp, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movl 0x20(%rax), %r15d
movq %rbp, %rdi
movq %r12, %rsi
callq 0x7404
movq (%rax), %rax
movsbl 0x8(%rax), %r8d
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movl %r14d, %edx
movl %r15d, %ecx
xorl %r9d, %r9d
callq 0x1d181
movq %rax, %rbx
movq 0x20(%rsp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x226c8
testq %rbx, %rbx
je 0x1d0ab
movq %rbx, %rdi
callq 0x2210
incq %r12
movq %r13, %r14
jmp 0x1cfee
movq %r15, %rdi
callq 0x211ec
movq %rax, %rdi
callq 0x21062
movq %rax, %r12
movq 0x8(%rsp), %r13
movq %r13, %rdi
movq 0x10(%rsp), %rbp
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
cmpb $0x2d, 0x8(%rax)
jne 0x1d11f
testq %r12, %r12
je 0x1d0f4
movq %r12, %rdi
callq 0x2210
movq %r15, %rdi
callq 0x211ec
movq %rax, %rbx
movq %r15, %rdi
callq 0x211ec
movq %rax, %rdi
callq 0x20d90
leal -0x1(%rax), %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1d36d
movq %rax, %r12
movq %r13, %rdi
movq %rbp, %rsi
callq 0x7404
movq (%rax), %rax
movq %r12, 0x48(%rax)
movq %r15, %rdi
callq 0x212b2
movq %r15, %rdi
pushq $0x8
popq %r15
movq %r15, %rsi
callq 0x2180
incq %rbp
jmp 0x1cf85
movq (%rsp), %rcx
incq %rcx
jmp 0x1cf30
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x2180
movq %rbx, %rdi
callq 0x22d0