Example #1
0
void
build_svg(
	const fs::path & file,
	const string & title,
	const seq_t & seq,
	float_t min_threshold,
	match_result_vec_t & results,
	size_t max_num_factors,
	bool show_labels,
	bool open,
	match_result_vec_t * max_chain,
	const std::string & notes,
	float max_threshold )
{
	XMLPlatformUtils::Initialize();

	//find the maximum score
	for (match_result_vec_t::const_iterator i = results.begin();
		results.end() != i;
		++i)
	{
		max_threshold = std::max( max_threshold, i->result.score );
	}

	SvgDomDocument doc(seq.size(), min_threshold, max_threshold, title, seq, show_labels);

	//for each hit result
	size_t idx = 0;
	unsigned num_added = 0;
	for (match_result_vec_t::iterator i = results.begin();
		results.end() != i;
		++i, ++idx)
	{
		i->number = idx;

		BiobaseTablePssmEntry * pssm_entry = BiobaseDb::singleton().get_pssm_entry(i->link);

		assert((size_t) i->result.position < seq.size());

		if (min_threshold <= i->result.score)
		{
			const bool is_in_max_chain =
				0 != max_chain
				&&
				max_chain->end() != std::find_if( max_chain->begin(), max_chain->end(), std::bind1st( detail::hit_is_same(), *i ) );

			doc.add_result(
				i->result,
				*pssm_entry,
				idx,
				is_in_max_chain );
			++num_added;
		}
	}
	if (0 == num_added)
	{
		throw std::logic_error( "No hits above threshold" );
	}

	factor_scores_map_t factors;
	find_factors_for_matches(results, factors);
	//cout << factors.size() << " factors associated with these matches" << std::endl;

	//for each factor - in score order
	while (! factors.empty() && 0 != max_num_factors--)
	{
		//find the factor with the highest score
		factor_scores_map_t::iterator best = factors.end();
		for (factor_scores_map_t::iterator f = factors.begin();
			factors.end() != f;
			++f)
		{
			if (factors.end() == best || f->second.score > best->second.score)
			{
				best = f;
			}
		}
		assert(best != factors.end());

		Factor * factor = BiobaseDb::singleton().get_entry<FACTOR_DATA>(best->first);
		if (0 != factor)
		{
			doc.add_factor(factor, best->second.hits);
		}

		factors.erase(best);
	}

	if( notes.size() )
	{
		doc.add_notes( notes );
	}

	add_tooltip_support(doc.doc, doc.doc_root);

	dom_print(doc.doc, file._BOOST_FS_NATIVE().c_str());

	//copy the script to the same directory
	try
	{
		fs::path script(BioEnvironment::singleton().get_svg_script_file());
		fs::path script_dest(file.branch_path());
		script_dest /= script.leaf();

		//only copy if destination older
		if (! fs::exists(script_dest) || fs::last_write_time(script) > fs::last_write_time(script_dest))
		{
			//remove first if exists
			if (fs::exists(script_dest))
			{
				fs::remove(script_dest);
			}

			fs::copy_file(script, script_dest);
		}
	}
	catch (const std::exception & ex)
	{
		cerr << ex.what() << endl;
	}

	XMLPlatformUtils::Terminate();

	if (open)
	{
		open_file(file);
	}

}