Skip to content

Commit

Permalink
Print d3 json on crispresso base
Browse files Browse the repository at this point in the history
  • Loading branch information
Snicker7 committed Mar 22, 2024
1 parent 31430c7 commit 82070a7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CRISPResso2/CRISPRessoCORE.py
Original file line number Diff line number Diff line change
Expand Up @@ -3619,7 +3619,7 @@ def count_alternate_alleles(sub_base_vectors, ref_name, ref_sequence, ref_total_
plot_2a_input = {
'nuc_pct_df': nuc_df_for_plot,
'mod_pct_df': mod_df_for_plot,
'fig_filename_root': plot_root,
'fig_filename_root': f'{_jp('2a.json')}' if not args.use_matplotlib and pro_installed else plot_root,
'save_also_png': save_png,
'sgRNA_intervals': sgRNA_intervals,
'sgRNA_names': sgRNA_names,
Expand Down
22 changes: 17 additions & 5 deletions CRISPResso2/CRISPRessoReports/CRISPRessoReport.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'''

import os
from jinja2 import Environment, FileSystemLoader
from jinja2 import Environment, FileSystemLoader, PackageLoader, ChoiceLoader
from jinja_partials import generate_render_partial, render_partial
from CRISPResso2 import CRISPRessoShared

Expand Down Expand Up @@ -115,6 +115,15 @@ def assemble_figs(run_data, crispresso_folder):
amplicons.append(amplicon_name)
amplicon_figures = {'names': [], 'locs': {}, 'titles': {}, 'captions': {}, 'datas': {}, 'htmls': {}}

if pro_installed:
for fig in ['2a', '2b', '4g', '11a', '11b']:
if os.path.exists(os.path.join(crispresso_folder, f'{fig}.json')):
with open(os.path.join(crispresso_folder, f'{fig}.json')) as fig_json_fh:
amplicon_figures['htmls'][fig] = f"""
<div class="d-flex justify-content-between" style="max-height: 80vh; overflow-y: auto;" id="{fig}"></div>
<script type="text/javascript">const {fig} = {fig_json_fh.read().strip()}</script>
"""

for fig in ['2a', '3a', '3b', '4a', '4b', '4c', '4d', '4e', '4f', '4g', '5', '6', '7', '8', '10a', '10b', '10c',
'11a']:
fig_name = 'plot_' + fig
Expand Down Expand Up @@ -147,6 +156,7 @@ def assemble_figs(run_data, crispresso_folder):
figures['datas'][amplicon_name] = amplicon_figures['datas']
figures['htmls'][amplicon_name] = amplicon_figures['htmls']
data = {'amplicons': amplicons, 'figures': figures}
breakpoint()
return data


Expand Down Expand Up @@ -175,15 +185,17 @@ def make_report(run_data, crispresso_report_file, crispresso_folder, _ROOT):
'crispresso_data_path': crispresso_data_path,
}

j2_env = Environment(loader=FileSystemLoader(os.path.join(_ROOT, 'CRISPRessoReports', 'templates')))
j2_env = Environment(
loader=ChoiceLoader([FileSystemLoader(os.path.join(_ROOT, 'CRISPRessoReports', 'templates')), PackageLoader('CRISPRessoPro', 'templates')]),
)

# dest_dir = os.path.dirname(crispresso_report_file)
# shutil.copy2(os.path.join(_ROOT,'templates','CRISPResso_justcup.png'),dest_dir)
# shutil.copy2(os.path.join(_ROOT,'templates','favicon.ico'),dest_dir)

with open(crispresso_report_file, 'w', encoding="utf-8") as outfile:
outfile.write(render_template(
'report.html', j2_env, report_data=report_data,
'report.html', j2_env, report_data=report_data, pro_installed=pro_installed,
))


Expand Down Expand Up @@ -326,7 +338,6 @@ def make_batch_report_from_folder(crispressoBatch_report_file, crispresso2_info,
nuc_conv_plots=nuc_conv_plots,
allele_modification_heatmap_plot=allele_modification_heatmap_plot,
allele_modification_line_plot=allele_modification_line_plot,
pro_installed=pro_installed,
)


Expand Down Expand Up @@ -509,7 +520,7 @@ def fill_default(dictionary, key, default_type=list):
dictionary[key] = default_type()

j2_env = Environment(
loader=FileSystemLoader([os.path.join(_ROOT, 'CRISPRessoReports', 'templates'), os.path.join(_ROOT, "CRISPRessoPro", "templates")]),
loader=ChoiceLoader([FileSystemLoader(os.path.join(_ROOT, 'CRISPRessoReports', 'templates')), PackageLoader('CRISPRessoPro', 'templates')]),
)
j2_env.filters['dirname'] = dirname
if crispresso_tool == 'batch':
Expand Down Expand Up @@ -594,6 +605,7 @@ def fill_default(dictionary, key, default_type=list):
allele_modification_line_plot_titles=allele_modification_line_plot['titles'],
allele_modification_line_plot_labels=allele_modification_line_plot['labels'],
allele_modification_line_plot_datas=allele_modification_line_plot['datas'],
pro_installed=pro_installed,
))


Expand Down
42 changes: 28 additions & 14 deletions CRISPResso2/CRISPRessoReports/templates/report.html
Original file line number Diff line number Diff line change
Expand Up @@ -295,26 +295,33 @@ <h5>Nucleotide composition for {{amplicon_name}}</h5>
{% endif %}
</div>
<div class='card-body'>

{% if '2a' in report_data['figures']['htmls'][amplicon_name] %}
{{ report_data['figures']['htmls'][amplicon_name]['2a']|safe }}
{% endif %}
{% for (data_label,data_path) in report_data['figures']['datas'][amplicon_name]['plot_2a'] %}
<p class="m-0"><small>Data: <a href="{{report_data['crispresso_data_path']}}{{data_path}}">{{data_label}}</a></small></p>
{% endfor %}
{% if report_data['figures']['locs'][amplicon_name]['plot_2a'] %}
<div class='div_zoom_outer d-none d-md-block' style="height:100px;border:1px solid #DDDDDD;position:relative">
<div id="zoomview_nucs_{{amplicon_name}}" style="position:absolute;left:0;height:100%;width:100%;background-image: url({{report_data['crispresso_data_path']}}{{report_data['figures']['locs'][amplicon_name]['plot_2a']}}.png);background-size:auto 100%;background-repeat:no-repeat;"></div>
<div class="d-lg-none" style="overflow-x:scroll;overflow-y:hidden;position:absolute;width:100%;height:100%">
<img src="{{report_data['crispresso_data_path']}}{{report_data['figures']['locs'][amplicon_name]['plot_2a']}}.png" style='height:100%'>
</div>
</div>
<div class='div_zoom_outer d-none d-md-block' style="height:100px;border:1px solid #DDDDDD;position:relative">
<div id="zoomview_nucs_{{amplicon_name}}" style="position:absolute;left:0;height:100%;width:100%;background-image: url({{report_data['crispresso_data_path']}}{{report_data['figures']['locs'][amplicon_name]['plot_2a']}}.png);background-size:auto 100%;background-repeat:no-repeat;"></div>
<div class="d-lg-none" style="overflow-x:scroll;overflow-y:hidden;position:absolute;width:100%;height:100%">
<img src="{{report_data['crispresso_data_path']}}{{report_data['figures']['locs'][amplicon_name]['plot_2a']}}.png" style='height:100%'>
</div>
</div>
<div style='position:relative;display:inline-block;width:95%'>
<a href="{{report_data['crispresso_data_path']}}{{report_data['figures']['locs'][amplicon_name]['plot_2a']}}.pdf">
<div id='zoomlens_nucs_{{amplicon_name}}' class="d-none d-lg-block" style='float: left;position: absolute;left: 0px;top: 0px;z-index: 1000;border: 1px solid #DDDDDD;height:100%;width:10%'></div>
<img id='tozoom_nucs_{{amplicon_name}}' src="{{report_data['crispresso_data_path']}}{{report_data['figures']['locs'][amplicon_name]['plot_2a']}}.png" width='100%' style='position:relative'></a>
</div>
<label class="labelpadding">
<span class='d-none d-md-block'>Hover your mouse over the bottom image to zoom in on a specific region.</span><br>
{{report_data['figures']['captions'][amplicon_name]['plot_2a']}}
</label>
{% for (data_label,data_path) in report_data['figures']['datas'][amplicon_name]['plot_2a'] %}
<p class="m-0"><small>Data: <a href="{{report_data['crispresso_data_path']}}{{data_path}}">{{data_label}}</a></small></p>
{% endfor %}
</div>
<label class="labelpadding">
<span class='d-none d-md-block'>Hover your mouse over the bottom image to zoom in on a specific region.</span><br>
{{report_data['figures']['captions'][amplicon_name]['plot_2a']}}
</label>
{% endif %}
{% for (data_label,data_path) in report_data['figures']['datas'][amplicon_name]['plot_2a'] %}
<p class="m-0"><small>Data: <a href="{{report_data['crispresso_data_path']}}{{data_path}}">{{data_label}}</a></small></p>
{% endfor %}

{% if report_data['figures']['sgRNA_based_names'][amplicon_name] and report_data['figures']['sgRNA_based_names'][amplicon_name]['2b']%}
{% for fig_name in report_data['figures']['sgRNA_based_names'][amplicon_name]['2b'] %}
Expand Down Expand Up @@ -715,4 +722,11 @@ <h5>Base editing for {{amplicon_name}}</h5>
{% endif %}
{% endfor %}
</script>


{% if pro_installed %}
<script src="https://unpkg.com/d3@5"></script>
{{ render_partial('partials/batch_d3.html', nucleotide_quilt_slugs=(['2a'])) }}
{% endif %}

{% endblock %}

0 comments on commit 82070a7

Please sign in to comment.