|
A library for working with phylogenetic and population genetic data.
v0.32.0
|
|
Go to the documentation of this file.
53 namespace population {
66 if( !cols_good || !rows_good ) {
67 throw std::domain_error(
68 "Invalid Cathedral Plot Record, where the parameters specify a width and height of " +
71 " pixels, but the contained value matrix has dimensions " +
77 throw std::domain_error(
78 "Invalid Cathedral Plot Record, where the value matrix has " +
80 " rows, but the window widths list contains " +
90 throw std::invalid_argument(
"cathedral_window_width invalid: width < 2 || height < 2" );
93 throw std::invalid_argument(
"cathedral_window_width invalid: row >= height" );
100 double const row_d =
static_cast<double>( row );
102 double window_width = 0.0;
105 auto const decay = - std::log( 1.0 / width_d ) / ( height_d - 1.0 );
106 window_width = chr_len * std::exp( -decay * row_d);
112 auto const scaler = width_d / height_d;
113 auto const denom = scaler * row_d + 1.0;
114 window_width = chr_len / denom;
120 auto const min_win_width = chr_len / width_d;
121 auto const max_win_width = chr_len;
125 auto const row_frac = row_d / height_d;
126 window_width = ( max_win_width - min_win_width ) * ( 1.0 - row_frac ) + min_win_width;
130 throw std::invalid_argument(
"cathedral_window_width requested with invalid method" );
140 return "exponential";
149 throw std::runtime_error(
"Invalid CathedralWindowWidthMethod" );
155 if( lower ==
"exponential" ) {
158 if( lower ==
"geometric" ) {
161 if( lower ==
"linear" ) {
164 throw std::invalid_argument(
165 "cathedral_window_width_method_from_string(): Invalid method name \"" + method +
"\""
179 auto document = JsonDocument::object();
180 auto& obj = document.get_object();
187 obj[
"width"] = JsonDocument::number_unsigned( parameters.
width );
188 obj[
"height"] = JsonDocument::number_unsigned( parameters.
height );
189 obj[
"windowWidthMethod"] = JsonDocument::string(
207 auto& obj = document.get_object();
208 obj[
"title"] = JsonDocument::string( record.
title );
209 obj[
"plotName"] = JsonDocument::string( record.
plot_name );
210 obj[
"chromosomeName"] = JsonDocument::string( record.
chromosome_name );
211 obj[
"chromosomeLength"] = JsonDocument::number_unsigned( record.
chromosome_length );
219 obj[
"minValue"] = JsonDocument::number_float( min_max.min );
220 obj[
"maxValue"] = JsonDocument::number_float( min_max.max );
228 std::shared_ptr<genesis::utils::BaseOutputTarget> json_target,
229 std::shared_ptr<genesis::utils::BaseOutputTarget> csv_target
235 if( record_document.
empty() ) {
236 throw std::invalid_argument(
237 "save_cathedral_plot_record_to_files(): Empty JSON document provided. "
238 "Call cathedral_plot_record_to_json_document() or related functions first."
241 if( record_value_matrix.
empty() ) {
242 throw std::invalid_argument(
243 "save_cathedral_plot_record_to_files(): Empty value matrix provided. "
244 "Call compute_cathedral_matrix() or related functions first."
256 std::string
const& base_path
262 to_file( base_path +
".json" ),
269 std::string
const& base_path
275 std::pair<genesis::utils::JsonDocument, genesis::utils::Matrix<double>>
277 std::string
const& base_path
284 auto json_file = base_path +
".json";
285 auto csv_file = base_path +
".csv";
290 json_file = base_path;
292 }
else if(
ends_with( base_path,
".csv" )) {
294 csv_file = base_path;
297 throw std::invalid_argument(
298 "load_cathedral_plot_record_components_from_files(): Cannot find json/csv files "
299 "for base path \"" + base_path +
"\""
305 return std::make_pair(
312 std::string
const& base_path
316 auto const& json = components.first;
321 result.
title = json[
"title" ].get_string();
322 result.
plot_name = json[
"plotName" ].get_string();
328 json[
"windowWidthMethod" ].get_string()
330 for(
auto const& elem : json[
"windowWidths" ].get_array() ) {
364 throw std::invalid_argument(
365 "Invalid call to make_cathedral_plot_svg() with image dimensions not fitting "
366 "with the data dimensions of the plot record."
370 throw std::invalid_argument(
371 "Invalid call to make_cathedral_plot_svg() with empty window widths list."
385 x_axis_settings.position = SvgAxisSettings::Position::kBottom;
388 auto const x_axis =
make_svg_axis( x_axis_settings, x_ticks,
"Genome position" );
391 std::vector<utils::Tickmarks::LabeledTick> y_ticks;
399 y_ticks.emplace_back( 0.0, min_win_width );
400 y_ticks.emplace_back( 1.0, max_win_width );
408 throw std::invalid_argument(
409 "make_cathedral_plot_svg(): Invalid CathedralWindowWidthMethod"
416 y_axis_settings.position = SvgAxisSettings::Position::kLeft;
418 auto const y_axis =
make_svg_axis( y_axis_settings, y_ticks,
"Window size" );
422 assert( color_norm );
426 color_bar_settings, heatmap_parameters.
color_map, *color_norm
431 svg.add( record.
title, image, x_axis, y_axis, color_bar );
432 return svg.document();
std::pair< SvgGradientLinear, SvgGroup > make_svg_color_bar(SvgColorBarSettings const &settings, ColorMap const &map, ColorNormalization const &norm, std::string const &id)
Provides functions for accessing the file system.
genesis::utils::JsonDocument cathedral_plot_parameters_to_json_document(CathedralPlotParameters const ¶meters)
Get a user-readable description of a CathedralPlotParameters as a JsonDocument.
ColorMap color_map
Set the ColorMap with all its properties to use for the heatmap.
std::shared_ptr< BaseInputSource > from_file(std::string const &file_name, bool detect_compression=true)
Obtain an input source for reading from a file.
std::vector< LabeledTick > linear_labels(double min, double max, size_t target_steps)
Return a set of labels with relative positions between min and max, where the labels correspond to th...
void write(JsonDocument const &document, std::shared_ptr< utils::BaseOutputTarget > target) const
Write a JsonDocument to an output target, using the JSON format.
CathedralPlotParameters parameters
Store a Json value of any kind.
CathedralWindowWidthMethod cathedral_window_width_method_from_string(std::string const &method)
Helper function to return a CathedralWindowWidthMethod from its textual representation.
bool ends_with(std::string const &text, std::string const &suffix)
Return whether a string ends with another string, i.e., check for a suffix.
Helper class to find "nice" tickmark intervals for creating scales and axes.
genesis::utils::SvgDocument make_cathedral_plot_svg(CathedralPlotRecord const &record, genesis::utils::HeatmapParameters const &heatmap_parameters, genesis::utils::Matrix< genesis::utils::Color > const &image)
Make a cathedral plot heat map and add it into an SVG document with legend and axes.
void write(Matrix< T > const &matrix, std::shared_ptr< utils::BaseOutputTarget > target, std::vector< std::string > row_names={}, std::vector< std::string > col_names={}, std::string corner="") const
Write a Matrix to an output target, using a specific MatrixWriter::Format and separator string.
std::string to_string(GenomeLocus const &locus)
std::string file_filename(std::string const &filename)
Remove extension if present.
std::pair< genesis::utils::JsonDocument, genesis::utils::Matrix< double > > load_cathedral_plot_record_components_from_files(std::string const &base_path)
Load the parts of a cathedral plot from a set of files.
Provides some commonly used string utility functions.
CathedralWindowWidthMethod window_width_method
Collection of the data used for making for a cathedral plot.
std::vector< LabeledTick > logarithmic_labels(double min, double max, double base=10.0)
Return a set of labels with relative positions between min and max, where the labels correspond to lo...
double cathedral_window_width(CathedralPlotRecord const &record, size_t row)
Compute the window width for a row in a cathedral plot.
genesis::utils::Matrix< double > value_matrix
Read Json data into a JsonDocument.
Write Json data from a JsonDocument.
std::unique_ptr< ColorNormalization > make_heatmap_color_norm(Matrix< double > const &values, HeatmapParameters const ¶meters)
void save_cathedral_plot_record_to_targets(genesis::utils::JsonDocument const &record_document, genesis::utils::Matrix< double > const &record_value_matrix, std::shared_ptr< genesis::utils::BaseOutputTarget > json_target, std::shared_ptr< genesis::utils::BaseOutputTarget > csv_target)
Save the record of a cathedral plot in a set of output targets.
genesis::utils::Matrix< genesis::utils::Color > make_cathedral_plot_heatmap(CathedralPlotRecord const &record, genesis::utils::HeatmapParameters const &heatmap_parameters)
Make a cathedral plot heat map as a color matrix.
std::string chromosome_name
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
CathedralWindowWidthMethod
Interpolation algorithm for window sizes across the rows of a cathedral plot.
std::shared_ptr< BaseOutputTarget > to_file(std::string const &file_name, GzipCompressionLevel compression_level, bool auto_adjust_filename=true)
Obtain an output target for writing to a file.
void validate_cathedral_plot_record(CathedralPlotRecord const &record)
Check a Cathedral Plot record for internal consistency.
SvgGroup make_svg_axis(SvgAxisSettings const &settings, std::map< double, std::string > const &labels, std::string const &name)
Simple helper to make an axis.
bool file_exists(std::string const &filename)
Return true iff the file exists (and is in fact a file, and not, e.g., a directory).
std::string cathedral_window_width_method_to_string(CathedralWindowWidthMethod method)
Helper function to return a textual representation of the method.
void save_cathedral_plot_record_to_files(genesis::utils::JsonDocument const &record_document, genesis::utils::Matrix< double > const &record_value_matrix, std::string const &base_path)
Save the record of a cathedral plot in a set of files.
genesis::utils::JsonDocument cathedral_plot_record_to_json_document(CathedralPlotRecord const &record)
Get a user-readable description of the data of a CathedralPlotRecord as a JsonDocument.
constexpr char to_lower(char c) noexcept
Return the lower case version of a letter, ASCII-only.
std::vector< double > window_widths
MinMaxPair< double > finite_minimum_maximum(ForwardIterator first, ForwardIterator last)
Return the minimum and the maximum of a range of double values.
Matrix< Color > make_heatmap_matrix(Matrix< double > const &values, HeatmapParameters const ¶meters)
Plot parameters to make a cathedral plot.
CathedralPlotRecord load_cathedral_plot_record_from_files(std::string const &base_path)
Load the record of a cathedral plot from a set of files.