<commit_before>/*
 * Copyright (c) 2017-2018 <NAME>
 * This code is licensed under MIT license (see LICENSE for details).
 */

#include <boost/io/ios_state.hpp>
#include <vcf2multialign/check_overlapping_non_nested_variants.hh>
#include <vcf2multialign/check_ploidy.hh>
#include <vcf2multialign/read_single_fasta_seq.hh>
#include <vcf2multialign/generate_context_base.hh>
#include <vcf2multialign/variant_format.hh>


namespace lb	= libbio;
namespace v2m	= vcf2multialign;


namespace {
	
	bool compare_references(
		v2m::vector_type const &ref,
		std::string_view const &var_ref,
		std::size_t const var_pos,
		std::size_t /* out */ &idx
	)
	{
		char const *var_ref_data(var_ref.data());
		auto const var_ref_len(var_ref.size());
		
		char const *ref_data(ref.data());
		auto const ref_len(ref.size());
		
		if (! (var_pos + var_ref_len <= ref_len))
		{
			idx = 0;
			return false;
		}
		
		auto const var_ref_end(var_ref_data + var_ref_len);
		auto const p(std::mismatch(var_ref_data, var_ref_end, ref_data + var_pos));
		if (var_ref_end != p.first)
		{
			idx = p.first - var_ref_data;
			return false;
		}
		
		return true;
	}
}


namespace vcf2multialign {
	
	void generate_context_base::open_files(
		char const *reference_fname,
		char const *ref_seq_name,
		char const *variants_fname,
		char const *report_fname
	)
	{
		lb::mmap_handle <char> ref_handle;
		ref_handle.open(reference_fname);
		
		m_vcf_handle.open(variants_fname);
		m_vcf_input.reset_range();
		
		if (report_fname)
		{
			auto const mode(lb::make_writing_open_mode({
				lb::writing_open_mode::CREATE,
				(m_should_overwrite_files ? lb::writing_open_mode::OVERWRITE : lb::writing_open_mode::NONE)
			}));
			
			lb::open_file_for_writing(report_fname, m_error_logger.output_stream(), mode);
			m_error_logger.write_header();
		}
		
		lb::add_reserved_info_keys(m_vcf_reader.info_fields());
		lb::add_reserved_genotype_keys(m_vcf_reader.genotype_fields());
		
		m_vcf_reader.set_variant_format(new variant_format());
		m_vcf_reader.set_input(m_vcf_input);
		m_vcf_reader.fill_buffer();
		m_vcf_reader.read_header();
		
		// Read the reference file and place its contents into reference.
		read_single_fasta_seq(ref_handle, m_reference, ref_seq_name);
	}
	
		
	void generate_context_base::check_ploidy()
	{
		::vcf2multialign::check_ploidy(m_vcf_reader, m_ploidy);
	}
	
	
	void generate_context_base::check_ref()
	{
		m_vcf_reader.reset();
		m_vcf_reader.set_parsed_fields(lb::vcf_field::REF);
		bool found_mismatch(false);
		std::size_t i(0);
		
		bool should_continue(false);
		do {
			m_vcf_reader.fill_buffer();
			should_continue = m_vcf_reader.parse(
				[this, &found_mismatch, &i]
				(lb::transient_variant const &var)
				-> bool
			{
				if (0 == m_chromosome_name.size() || var.chrom_id() == m_chromosome_name)
				{
					auto const var_ref(var.ref());
					auto const var_pos(var.zero_based_pos());
					auto const lineno(var.lineno());
					std::size_t diff_pos{0};<commit_msg>Read the reference before starting to process the variant file<commit_after>/*
 * Copyright (c) 2017-2018 <NAME>
 * This code is licensed under MIT license (see LICENSE for details).
 */

#include <boost/io/ios_state.hpp>
#include <vcf2multialign/check_overlapping_non_nested_variants.hh>
#include <vcf2multialign/check_ploidy.hh>
#include <vcf2multialign/read_single_fasta_seq.hh>
#include <vcf2multialign/generate_context_base.hh>
#include <vcf2multialign/variant_format.hh>


namespace lb	= libbio;
namespace v2m	= vcf2multialign;


namespace {
	
	bool compare_references(
		v2m::vector_type const &ref,
		std::string_view const &var_ref,
		std::size_t const var_pos,
		std::size_t /* out */ &idx
	)
	{
		char const *var_ref_data(var_ref.data());
		auto const var_ref_len(var_ref.size());
		
		char const *ref_data(ref.data());
		auto const ref_len(ref.size());
		
		if (! (var_pos + var_ref_len <= ref_len))
		{
			idx = 0;
			return false;
		}
		
		auto const var_ref_end(var_ref_data + var_ref_len);
		auto const p(std::mismatch(var_ref_data, var_ref_end, ref_data + var_pos));
		if (var_ref_end != p.first)
		{
			idx = p.first - var_ref_data;
			return false;
		}
		
		return true;
	}
}


namespace vcf2multialign {
	
	void generate_context_base::open_files(
		char const *reference_fname,
		char const *ref_seq_name,
		char const *variants_fname,
		char const *report_fname
	)
	{
		lb::mmap_handle <char> ref_handle;
		ref_handle.open(reference_fname);
		
		// Read the reference file and place its contents into reference.
		read_single_fasta_seq(ref_handle, m_reference, ref_seq_name);
		
		// Open the variant file.
		m_vcf_handle.open(variants_fname);
		m_vcf_input.reset_range();
		
		if (report_fname)
		{
			auto const mode(lb::make_writing_open_mode({
				lb::writing_open_mode::CREATE,
				(m_should_overwrite_files ? lb::writing_open_mode::OVERWRITE : lb::writing_open_mode::NONE)
			}));
			
			lb::open_file_for_writing(report_fname, m_error_logger.output_stream(), mode);
			m_error_logger.write_header();
		}
		
		lb::add_reserved_info_keys(m_vcf_reader.info_fields());
		lb::add_reserved_genotype_keys(m_vcf_reader.genotype_fields());
		
		m_vcf_reader.set_variant_format(new variant_format());
		m_vcf_reader.set_input(m_vcf_input);
		m_vcf_reader.fill_buffer();
		m_vcf_reader.read_header();
	}
	
		
	void generate_context_base::check_ploidy()
	{
		::vcf2multialign::check_ploidy(m_vcf_reader, m_ploidy);
	}
	
	
	void generate_context_base::check_ref()
	{
		m_vcf_reader.reset();
		m_vcf_reader.set_parsed_fields(lb::vcf_field::REF);
		bool found_mismatch(false);
		std::size_t i(0);
		
		bool should_continue(false);
		do {
			m_vcf_reader.fill_buffer();
			should_continue = m_vcf_reader.parse(
				[this, &found_mismatch, &i]
				(lb::transient_variant const &var)
				-> bool
			{
				if (0 == m_chromosome_name.size() || var.chrom_id() == m_chromosome_name)
				{
					auto const var_ref(var.ref());
					auto const var_pos(var.zero_based_pos());
					auto const lineno(var.lineno());
					std::size_t diff_pos{0};
{'fraction_non_alphanumeric': 0.12057209379677365, 'fraction_numerical': 0.007317478795942125, 'mean_word_length': 4.705882352941177, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5382095', 'n_tokens_mistral': 2625, 'n_tokens_neox': 2287, 'n_words': 382}