-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: New subcommand for plotting bam files (#156)
* Add new subcommand plot-bam * Minor fixes * Visual updates * Remove debug * Format * Support for multiple bam files * Format * Plot BAM files below each other * Update README.md * Minor clippy fixes * Write output to stdout * Format * Add navigation for samples * Bump xlsxwriter version * Update Cargo.toml * Update Cargo.toml * Update Cargo.toml * Fix cli help text and wrong default value * pack chrom, start, stop into Region struct instead; replace Path arg types with P: AsRef<Path> * Use context for anyhow error message * Update README.md Co-authored-by: Till Hartmann <[email protected]> Co-authored-by: Johannes Köster <[email protected]>
- Loading branch information
1 parent
5a91142
commit 707ef77
Showing
12 changed files
with
274 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//! Tools that work on BAM files | ||
pub mod collapse_reads_to_fragments; | ||
pub mod depth; | ||
pub mod plot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<title>rbt bam report</title> | ||
<meta charset="UTF-8"> | ||
<script src="https://cdn.jsdelivr.net/npm/vega@5"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/vega-lite@5"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script> | ||
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> | ||
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/lz-string/1.4.4/lz-string.min.js" integrity="sha512-qoCTmFwBtCPvFhA+WAqatSOrghwpDhFHxwAGh+cppWonXbHA09nG1z5zi4/NGnp8dUhXiVrzA6EnKgJA+fyrpw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> | ||
<script src="https://unpkg.com/[email protected]/build/jsonm.js"></script> | ||
<!-- CSS only --> | ||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> | ||
</head> | ||
|
||
<body style="text-align: center;"> | ||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark"> | ||
<a class="navbar-brand" href="#">rbt report</a> | ||
<div class="collapse navbar-collapse" id="navbarText"> | ||
<ul class="navbar-nav mr-auto"> | ||
<li class="nav-item"> | ||
<a class="nav-link" href="https://github.com/rust-bio/rust-bio-tools/blob/master/CHANGELOG.md">{{ version }}</a> | ||
</li> | ||
<li class="nav-item"> | ||
<a class="nav-link" href="https://github.com/rust-bio/rust-bio-tools">github</a> | ||
</li> | ||
</ul> | ||
<span class="navbar-text"> | ||
created {{ time }} | ||
</span> | ||
</div> | ||
</nav> | ||
<nav aria-label="breadcrumb"> | ||
<ul class="breadcrumb"> | ||
<li class="breadcrumb-item active" style="padding-top: 8px;">chrom: {{ chrom|safe }}</li> | ||
<li class="breadcrumb-item active" style="padding-top: 8px;" aria-current="page">region: {{ start }} - {{ end }}</li> | ||
<button class="btn btn-secondary dropdown-toggle ml-auto" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> | ||
Samples | ||
</button> | ||
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton" style="overflow-y: auto; max-height: 200px; z-index: 1001;"> | ||
{% for bam in bams %}<div class="dropdown-item"> | ||
<input class="form-check-input" type="checkbox" checked="checked" data-toggle="collapse" data-target="#collapse_{{loop.index}}" value="" id="defaultCheck{{ loop.index }}"> | ||
<label class="form-check-label" for="defaultCheck{{ loop.index }}"> | ||
{{ bam }} | ||
</label> | ||
</div>{% endfor %} | ||
</div> | ||
</ul> | ||
</nav> | ||
|
||
{% for plot in plots %}<div id="collapse_{{loop.index}}" class="row justify-content-center collapse show" style="margin-top: 25px;"> | ||
<div id="plot_{{loop.index}}" style="overflow-y: auto; height: calc(50vh - 50px);"></div> | ||
</div>{% endfor %} | ||
<script> | ||
let plots = [{% for plot in plots %}{{ plot | safe }}{% if not loop.last %},{% endif %}{% endfor %}]; | ||
let bams = [{% for bam in bams %}"{{ bam }}"{% if not loop.last %},{% endif %}{% endfor %}]; | ||
let i = 1; | ||
|
||
let decompressed_plots = []; | ||
|
||
for (plot of plots) { | ||
let decompressed = LZString.decompressFromUTF16(plot); | ||
let unpacker = new jsonm.Unpacker(); | ||
unpacker.setMaxDictSize(100000); | ||
let unpacked_specs = unpacker.unpack(JSON.parse(decompressed)); | ||
unpacked_specs["width"] = $(window).width() - 100; | ||
unpacked_specs["title"] = bams[i - 1]; | ||
decompressed_plots.push(unpacked_specs); | ||
let plot_id = `#plot_${i}`; | ||
vegaEmbed(plot_id, unpacked_specs); | ||
i++; | ||
} | ||
|
||
$(window).resize(function() { | ||
let j = 1; | ||
for (plot of decompressed_plots) { | ||
let plot_id = `#plot_${j}`; | ||
plot["width"] = $(window).width() - 100; | ||
vegaEmbed(plot_id, plot); | ||
} | ||
}) | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod plot_bam; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use crate::bcf::report::table_report::create_report_table::create_report_data; | ||
use crate::bcf::report::table_report::create_report_table::manipulate_json; | ||
use crate::common::Region; | ||
use anyhow::Result; | ||
use chrono::{DateTime, Local}; | ||
use itertools::Itertools; | ||
use std::io; | ||
use std::io::Write; | ||
use std::path::Path; | ||
use tera::{Context, Tera}; | ||
|
||
pub(crate) fn plot_bam<P: AsRef<Path>>( | ||
bam_paths: &[P], | ||
fasta_path: P, | ||
region: &Region, | ||
max_read_depth: u32, | ||
) -> Result<()> { | ||
let mut plots = Vec::new(); | ||
|
||
let Region { target, start, end } = region.clone(); | ||
for bam_path in bam_paths { | ||
let (content, max_rows) = | ||
create_report_data(&fasta_path, None, bam_path, region, max_read_depth)?; | ||
let visualization = manipulate_json(content, start, end, max_rows)?; | ||
|
||
plots.push(visualization); | ||
} | ||
|
||
let bams = bam_paths | ||
.iter() | ||
.map(|b| b.as_ref().iter().last().unwrap().to_str().unwrap()) | ||
.collect_vec(); | ||
|
||
let mut templates = Tera::default(); | ||
templates.add_raw_template("bam_plot.html.tera", include_str!("bam_plot.html.tera"))?; | ||
let mut context = Context::new(); | ||
let local: DateTime<Local> = Local::now(); | ||
context.insert("time", &local.format("%a %b %e %T %Y").to_string()); | ||
context.insert("version", &env!("CARGO_PKG_VERSION")); | ||
context.insert("plots", &plots); | ||
context.insert("bams", &bams); | ||
context.insert("chrom", &target); | ||
context.insert("start", &start); | ||
context.insert("end", &end); | ||
|
||
let html = templates.render("bam_plot.html.tera", &context)?; | ||
io::stdout().write_all(html.as_bytes())?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.