// File: 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:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movq %rsi, 0xc8(%rsp)
movq 0xc8(%rsp), %rdi
callq 0x29200
movq %rax, 0xc0(%rsp)
movl $0x0, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x64(%rsp)
movq 0xc0(%rsp), %rax
movq 0x18(%rax), %rdi
callq 0xc220
movq %rax, %rcx
movl 0x64(%rsp), %eax
cmpl %ecx, %eax
jge 0x3d3de
movq 0x27fa7(%rip), %rax     # 0x64fe0
movq (%rax), %rax
movq %rax, 0x58(%rsp)
movq 0xc0(%rsp), %rax
movq 0x18(%rax), %rdi
movslq 0xbc(%rsp), %rsi
callq 0xc240
movq 0x58(%rsp), %rdi
movq (%rax), %rdx
leaq 0x15c30(%rip), %rsi     # 0x52c99
movb $0x0, %al
callq 0x2250
movq 0xc0(%rsp), %rax
movq (%rax), %rax
movslq 0xbc(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0xb0(%rsp)
movl $0x0, 0xac(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x54(%rsp)
movq 0xb0(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jge 0x3d3c6
movq 0xb0(%rsp), %rdi
movslq 0xac(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movq 0x30(%rax), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x2f380
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rdi
callq 0xc7d0
movq %rax, 0x98(%rsp)
movq 0xa0(%rsp), %rdi
callq 0xc760
movq %rax, 0x90(%rsp)
movq 0x98(%rsp), %rdi
movq 0x90(%rsp), %rsi
leaq -0x12d92(%rip), %rdx    # 0x2a3a0
callq 0x31d00
movl $0x8, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x48(%rsp)
callq 0x474d0
jmp 0x3d153
movq 0x48(%rsp), %rax
movq %rax, 0x88(%rsp)
movl $0x0, 0x78(%rsp)
movl 0x78(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0xa0(%rsp), %rdi
callq 0xc680
movq %rax, %rcx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0x3d2c7
movq 0xa0(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movsbl 0x8(%rax), %eax
cmpl $0x2d, %eax
jne 0x3d1ea
movq 0xa0(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movb $0x2b, 0x8(%rax)
jmp 0x3d1ea
movq 0x48(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
movl $0x8, %esi
callq 0x21a0
jmp 0x3d3e6
movq 0xd0(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xc0(%rsp), %rax
movq 0x18(%rax), %rdi
movslq 0xbc(%rsp), %rsi
callq 0xc240
movq (%rax), %rax
movq %rax, 0x28(%rsp)
movq 0xa0(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0xc(%rax), %eax
movl %eax, 0x30(%rsp)
movq 0xa0(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movl 0x20(%rax), %eax
movl %eax, 0x34(%rsp)
movq 0xa0(%rsp), %rdi
movslq 0x78(%rsp), %rsi
callq 0xc800
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl 0x30(%rsp), %edx
movl 0x34(%rsp), %ecx
movq (%rax), %rax
xorl %r9d, %r9d
movsbl 0x8(%rax), %r8d
callq 0x3d400
movq %rax, 0x70(%rsp)
movq 0x88(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x4a6e0
movq 0x70(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x3d2b5
movq 0x38(%rsp), %rdi
callq 0x2220
jmp 0x3d2b7
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x3d168
movq 0x88(%rsp), %rdi
callq 0x47e10
movq %rax, %rdi
callq 0x47b60
movq %rax, 0x68(%rsp)
movq 0xb0(%rsp), %rdi
movslq 0xac(%rsp), %rsi
callq 0xc800
movq (%rax), %rax
movsbl 0x8(%rax), %eax
cmpl $0x2d, %eax
jne 0x3d359
movq 0x68(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x3d31c
movq 0x18(%rsp), %rdi
callq 0x2220
movq 0x88(%rsp), %rdi
callq 0x47e10
movq %rax, 0x10(%rsp)
movq 0x88(%rsp), %rdi
callq 0x47e10
movq %rax, %rdi
callq 0x47640
movq 0x10(%rsp), %rdi
movl %eax, %edx
subl $0x1, %edx
xorl %esi, %esi
callq 0x3d720
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rax
movq %rax, (%rsp)
movq 0xb0(%rsp), %rdi
movslq 0xac(%rsp), %rsi
callq 0xc800
movq (%rsp), %rcx
movq (%rax), %rax
movq %rcx, 0x48(%rax)
movq 0x88(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x3d3ae
movq 0x8(%rsp), %rdi
callq 0x47f70
movq 0x8(%rsp), %rdi
movl $0x8, %esi
callq 0x21a0
jmp 0x3d3b0
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x3d09a
jmp 0x3d3c8
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x3d007
addq $0xd8, %rsp
retq
movq 0x80(%rsp), %rdi
callq 0x2300
nopw %cs:(%rax,%rax)