// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %rsp
movq %rdi, %r12
movq %rsi, %rdi
callq 0x163f1
movq %rax, %r15
movq 0x18(%rax), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x22424
xorl %ebp, %ebp
movq %r12, 0x20(%rsp)
movq %r15, 0x28(%rsp)
cmpq %rbp, %rdx
jbe 0x2243f
movq 0x16fab(%rip), %rcx     # 0x38fe0
movq (%rcx), %rdi
movq (%rax,%rbp,8), %rdx
leaq 0x8c5a(%rip), %rsi      # 0x2ac9d
xorl %eax, %eax
callq 0x2250
movq (%r15), %rax
movq (%rax,%rbp,8), %rcx
movq (%rcx), %rax
movq %rcx, (%rsp)
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x223fc
xorl %ebx, %ebx
movq %rbp, 0x30(%rsp)
cmpq %rbx, %rdx
jbe 0x22433
movq %rbx, 0x10(%rsp)
movq (%rax,%rbx,8), %rax
movq 0x30(%rax), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0x1a6af
movq (%rax), %r13
movq %rax, 0x18(%rsp)
movq 0x8(%rax), %r14
cmpq %r14, %r13
je 0x2225a
movq %r14, %rbx
subq %r13, %rbx
movq %rbx, %rax
sarq $0x3, %rax
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r13, %rdi
movq %r14, %rsi
leaq -0xb1cc(%rip), %rcx     # 0x16f04
callq 0x1c455
leaq 0x8(%r13), %rax
cmpq $0x81, %rbx
movq %r14, 0x8(%rsp)
jl 0x221da
movl $0x8, %r14d
movq %rax, 0x38(%rsp)
movq %rax, %rbx
movq %r13, %r15
leaq (%r14,%r13), %r12
movq (%r13,%r14), %rdi
movq (%r13), %rsi
callq 0x16f04
movq (%r13,%r14), %rbp
testb %al, %al
movq %r12, 0x40(%rsp)
je 0x22131
movq 0x38(%rsp), %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x22b0
movq %r13, %r12
jmp 0x22161
movq (%r15), %rsi
movq %rbp, %rdi
callq 0x16f04
testb %al, %al
je 0x22161
movq %rbx, %rcx
leaq -0x8(%rcx), %r12
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %rbp, %rdi
callq 0x16f04
movq %r12, %rcx
testb %al, %al
jne 0x22143
movq %rbp, (%r12)
addq $0x8, %r14
addq $0x8, %rbx
cmpq $0x80, %r14
movq 0x40(%rsp), %r15
jne 0x220fc
subq $-0x80, %r13
cmpq 0x8(%rsp), %r13
movq 0x20(%rsp), %r12
je 0x2225a
movq -0x8(%r13), %rsi
movq (%r13), %rbx
movq %rbx, %rdi
callq 0x16f04
movq %r13, %r14
testb %al, %al
je 0x221c7
movq %r13, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %rbx, %rdi
callq 0x16f04
movq %r14, %rcx
testb %al, %al
jne 0x221a9
movq %rbx, (%r14)
addq $0x8, %r13
cmpq 0x8(%rsp), %r13
jne 0x2218f
jmp 0x2225a
cmpq %r14, %rax
je 0x2225a
movq %r13, %rbp
movq %rbp, %r14
movq %rax, %rbp
movq (%rax), %rdi
movq (%r13), %rsi
callq 0x16f04
movq (%rbp), %rbx
testb %al, %al
je 0x22219
movq %rbp, %rdx
subq %r13, %rdx
subq %rdx, %r14
addq $0x10, %r14
movq %r14, %rdi
movq %r13, %rsi
callq 0x22b0
movq %r13, %r14
jmp 0x2224c
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x16f04
movq %rbp, %r14
testb %al, %al
je 0x2224c
movq %rbp, %rcx
leaq -0x8(%rcx), %r14
movq -0x10(%rcx), %rsi
movq -0x8(%rcx), %rax
movq %rax, (%rcx)
movq %rbx, %rdi
callq 0x16f04
movq %r14, %rcx
testb %al, %al
jne 0x2222e
movq %rbx, (%r14)
leaq 0x8(%rbp), %rax
cmpq 0x8(%rsp), %rax
jne 0x221e2
movl $0x8, %edi
callq 0x2180
movq %rax, %r13
movq %rax, %rdi
callq 0x2678c
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
movq 0x28(%rsp), %r15
movq 0x30(%rsp), %rbp
jle 0x2232c
xorl %ebx, %ebx
cmpq %rbx, %rdx
jbe 0x22433
movq (%rax,%rbx,8), %rax
movb 0x8(%rax), %dil
cmpb $0x2d, %dil
jne 0x222b4
movb $0x2b, 0x8(%rax)
movb $0x2b, %dil
movq 0x18(%r15), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
cmpq %rbp, %rdx
jbe 0x2243f
movq (%rcx,%rbp,8), %rsi
movl 0xc(%rax), %edx
movl 0x20(%rax), %ecx
movsbl %dil, %r8d
movq %r12, %rdi
xorl %r9d, %r9d
callq 0x22468
movq %rax, %r14
movq %r13, %rdi
movq %rax, %rsi
callq 0x28c06
testq %r14, %r14
je 0x22303
movq %r14, %rdi
callq 0x2220
incq %rbx
movq 0x18(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbx
jl 0x22296
movq %r13, %rdi
callq 0x26fb2
movq %rax, %rdi
callq 0x26db4
movq (%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
movq 0x10(%rsp), %rbx
cmpq %rbx, %rdx
jbe 0x22433
movq (%rcx,%rbx,8), %rsi
cmpb $0x2d, 0x8(%rsi)
jne 0x223b2
testq %rax, %rax
je 0x22373
movq %rax, %rdi
callq 0x2220
movq %r13, %rdi
callq 0x26fb2
movq %rax, %rbx
movq %r13, %rdi
callq 0x26fb2
movq %rax, %rdi
callq 0x26856
leal -0x1(%rax), %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x22661
movq (%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
movq 0x10(%rsp), %rbx
cmpq %rbx, %rdx
jbe 0x22433
movq (%rcx,%rbx,8), %rcx
movq %rax, 0x48(%rcx)
movq %r13, %rdi
callq 0x27096
movl $0x8, %esi
movq %r13, %rdi
callq 0x2190
incq %rbx
movq (%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbx
jl 0x22072
incq %rbp
movq 0x18(%r15), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbp
jl 0x22025
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa496(%rip), %rdi      # 0x2c8d0
movq %rbx, %rsi
jmp 0x22449
leaq 0xa48a(%rip), %rdi      # 0x2c8d0
movq %rbp, %rsi
xorl %eax, %eax
callq 0x2270
movq %rax, %rbx
movl $0x8, %esi
movq %r13, %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x22e0