Skip to content

Commit

Permalink
Add formatter parameter to csv-report (#137)
Browse files Browse the repository at this point in the history
* Add formatter

* Finalize formatting
Bootstrap-table initalization now via javascript

* Expand help text for csv-report formatting parameter

* Update CHANGELOG.md

* Update doc link in help text for formatter parameter of csv-report

* Remove unused context

* Update CHANGELOG.md

Co-authored-by: Johannes Köster <[email protected]>
  • Loading branch information
fxwiegand and johanneskoester committed Mar 10, 2021
1 parent 2cb5a5b commit f4deedb
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 58 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [0.20.0] - 2021-03-10
### Added
- New parameter `--formatter` for `rbt csv-report`.
- `--formatter` allows the specification of custom formatting functions for one or multiple columns of the given csv file.

## [0.19.7] - 2021-03-09
### Changed
- Performance improvement for `rbt csv-report`.
Expand Down
3 changes: 0 additions & 3 deletions src/bcf/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ pub fn embed_js(
("report.js", include_str!("js/report.js")),
("gene-report.js", include_str!("js/gene-report.js")),
];
let csv_report_files = vec![("csv_report.js", include_str!("../../csv/csv_report.js"))];
if vcf_report {
files.extend(vcf_report_files.iter());
if let Some(path) = custom_table_report_js {
Expand All @@ -54,8 +53,6 @@ pub fn embed_js(
} else {
files.push(("table-report.js", include_str!("js/table-report.js")))
}
} else {
files.extend(csv_report_files.iter());
}
for (name, file) in files {
let mut out_file = File::create(js_path.to_owned() + name)?;
Expand Down
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ pub(crate) enum Command {
#[structopt(long, short = "s", default_value = ",")]
separator: char,

/// Configure a custom formatter function for each column by providing a file containing a javascript object with csv column title as the key and a format function as the value.
/// More information on the formatting functions and how to use them here: https://bootstrap-table.com/docs/api/column-options/#formatter.
#[structopt(long, short = "f")]
formatter: Option<String>,

/// Relative output path for the report files. Default value is the current directory.
#[structopt(default_value = ".")]
output_path: String,
Expand Down
2 changes: 2 additions & 0 deletions src/csv/csv_report.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.table-container { display: none; }
.loading { height: 100%; padding-top: 25%;}
.active-row { background: lightgray !important; }
th { white-space: nowrap; }
#table { max-width: 90%; }
25 changes: 1 addition & 24 deletions src/csv/csv_report.html.tera
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,7 @@
</div>
</div>
<div class="col-md-12 table-container" style="margin-top: 15px;">
<table id="table" class="table table-hover" data-toggle="table" data-height="510" style="max-width: 90%;">
<thead>
<tr>
{% for title in titles %}
<th style="white-space: nowrap;">{{ title }}
<a class="sym" data-toggle="modal" data-target="#{{ title }}" onclick="vegaEmbed('#plot_{{ loop.index0 }}', plot_{{ loop.index0 }})">
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-bar-chart-fill" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<rect width="4" height="5" x="1" y="10" rx="1"/>
<rect width="4" height="9" x="6" y="6" rx="1"/>
<rect width="4" height="14" x="11" y="1" rx="1"/>
</svg>
</a>
<a class="sym" data-toggle="modal" data-target="#{{ title }}-search">
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-search" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M10.442 10.442a1 1 0 0 1 1.415 0l3.85 3.85a1 1 0 0 1-1.414 1.415l-3.85-3.85a1 1 0 0 1 0-1.415z"/>
<path fill-rule="evenodd" d="M6.5 12a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zM13 6.5a6.5 6.5 0 1 1-13 0 6.5 6.5 0 0 1 13 0z"/>
</svg>
</a>
</th>
{% endfor %}
</tr>
</thead>
<tbody>
</tbody>
<table id="table" class="table table-hover">
</table>
</div>
</div>
Expand Down
30 changes: 0 additions & 30 deletions src/csv/csv_report.js

This file was deleted.

41 changes: 41 additions & 0 deletions src/csv/csv_report.js.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
$(document).ready(function() {
$('.table-container').show();
$('.loading').hide();
$('#table').bootstrapTable( 'resetView' , {height: window.innerHeight - 200} );
$('.modal').on('shown.bs.modal', function () {
window.dispatchEvent(new Event('resize'));
});
var decompressed = JSON.parse(LZString.decompressFromUTF16(data));

{% if formatter %}var format = {{ formatter }}{% endif %}

$('#table').bootstrapTable({
height: 510,
columns: [{% for title in titles %}{
field: '{{ title }}',
title: '{{ title }}\r\n <a class=\"sym\" data-toggle=\"modal\" data-target=\"#{{ title }}\" onclick=\"vegaEmbed(\'#plot_{{ loop.index0 }}\', plot_{{ loop.index0 }})\">\r\n <svg width=\"1em\" height=\"1em\" viewBox=\"0 0 16 16\" class=\"bi bi-bar-chart-fill\" fill=\"currentColor\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\r\n <rect width=\"4\" height=\"5\" x=\"1\" y=\"10\" rx=\"1\"\/>\r\n <rect width=\"4\" height=\"9\" x=\"6\" y=\"6\" rx=\"1\"\/>\r\n <rect width=\"4\" height=\"14\" x=\"11\" y=\"1\" rx=\"1\"\/>\r\n <\/svg>\r\n <\/a>\r\n <a class=\"sym\" data-toggle=\"modal\" data-target=\"#{{ title }}-search\">\r\n <svg width=\"1em\" height=\"1em\" viewBox=\"0 0 16 16\" class=\"bi bi-search\" fill=\"currentColor\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\r\n <path fill-rule=\"evenodd\" d=\"M10.442 10.442a1 1 0 0 1 1.415 0l3.85 3.85a1 1 0 0 1-1.414 1.415l-3.85-3.85a1 1 0 0 1 0-1.415z\"\/>\r\n <path fill-rule=\"evenodd\" d=\"M6.5 12a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zM13 6.5a6.5 6.5 0 1 1-13 0 6.5 6.5 0 0 1 13 0z\"\/>\r\n <\/svg>\r\n <\/a>'{% if formatter %},
formatter: format["{{ title }}"]{% endif %}
}{% if not loop.last %},{% endif %}{% endfor %}],
data: []
})

let columns = [{% for title in titles %}"{{ title }}"{% if not loop.last %},{% endif %}{% endfor %}];
var table_rows = [];
for (const r of decompressed) {
var i = 0;
row = {};
for (const el of r) {
row[columns[i]] = el;
i++;
}
table_rows.push(row);
}
$('#table').bootstrapTable('append', table_rows)
let to_be_highlighted = window.location.href.toString().split("highlight=").pop();
let rows = $("table > tbody > tr");
rows.each(function() {
if (this.dataset.index === to_be_highlighted) {
$(this).children().addClass('active-row');
}
});
});
29 changes: 28 additions & 1 deletion src/csv/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use serde_json::json;
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use std::fs;
use std::io::Write;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use std::str::FromStr;
use tera::{Context, Tera};
Expand All @@ -25,6 +26,7 @@ pub(crate) fn csv_report(
separator: char,
sort_column: Option<&str>,
ascending: Option<bool>,
formatter: Option<&str>,
) -> Result<()> {
let mut rdr = csv::ReaderBuilder::new()
.delimiter(separator as u8)
Expand Down Expand Up @@ -293,6 +295,31 @@ pub(crate) fn csv_report(
}
}

let formatter_object = if let Some(f) = formatter {
let mut file_string = "".to_string();
let mut custom_file =
File::open(f).context("Unable to open given file for formatting colums")?;
custom_file
.read_to_string(&mut file_string)
.context("Unable to read string from formatting file")?;

Some(file_string)
} else {
None
};

let mut templates = Tera::default();
templates.add_raw_template("csv_report.js.tera", include_str!("csv_report.js.tera"))?;
let mut context = Context::new();
context.insert("titles", &titles);
context.insert("formatter", &formatter_object);

let js = templates.render("csv_report.js.tera", &context)?;

let file_path = output_path.to_owned() + "/js/csv_report.js";
let mut file = fs::File::create(file_path)?;
file.write_all(js.as_bytes())?;

for (i, current_table) in table.chunks(rows_per_page).enumerate() {
let page = i + 1;

Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fn main() -> Result<()> {
sort_column,
sort_order,
separator,
formatter,
output_path,
} => {
if !Path::new(&output_path).exists() {
Expand All @@ -107,6 +108,7 @@ fn main() -> Result<()> {
separator,
sort_column.as_deref(),
order,
formatter.as_deref(),
)?
}
VcfReport {
Expand Down

0 comments on commit f4deedb

Please sign in to comment.