Commit 8a2348c8 authored by MARIJON Pierre's avatar MARIJON Pierre

Add script for html report generation

parent 7b84fbfa
......@@ -4,3 +4,4 @@ include knot/config.yaml
recursive-include knot/extremity_search *
recursive-include knot/filter_tig *
recursive-include knot/path_search *
recursive-include knot/analysis *
......@@ -50,6 +50,7 @@ def main(args=None):
multi_adjacent = {k for k, count in counter.items() if count > 1}
print("type,ext1,ext2", file=args["output"])
for key in paths:
if not paths[key]["adjacent"]:
path_type = "distant"
......@@ -58,7 +59,7 @@ def main(args=None):
else:
path_type = "single adjacency"
print("{},{}".format(path_type, "-".join(key)), file=args["output"])
print("{},{},{}".format(path_type, *key), file=args["output"])
def adjacent_len(length, threshold):
return int(length) <= threshold
......
#!/usr/bin/env python3
# std import
import os
import csv
import sys
import argparse
from jinja2 import FileSystemLoader
from jinja2 import Environment
from Bio import SeqIO
def main(args=None):
if args is None:
args = sys.argv[1:]
parser = argparse.ArgumentParser(prog="knot.analysis.generate_report")
parser.add_argument("-a", "--aag", type=argparse.FileType('r'), help="AAG filepath")
parser.add_argument("-C", "--contig", type=argparse.FileType('r'), help="contig filepath", required=True)
parser.add_argument("-c", "--classification", type=argparse.FileType('r'), help="path classification filepath", required=True)
parser.add_argument("-p", "--hamilton-path", type=argparse.FileType('r'), help="hamilton path filepath")
parser.add_argument("-o", "--output", type=argparse.FileType('wb'), help="path where report was write")
args = vars(parser.parse_args(args))
template_path = os.path.dirname(__file__) + os.sep + "template" + os.sep
env = Environment(loader=FileSystemLoader(template_path))
template = env.get_template('report.jinja2.html')
param = dict()
contig_info(param, args["contig"])
classification_info(param, args["classification"])
if args["hamilton_path"] is not None:
hamilton_info(param, args["hamilton_path"])
full_aag_info(param, args["aag"])
args["aag"].seek(0)
build_AAG_representation(param, args["aag"])
args["output"].write(template.render(param).encode("utf-8"))
def contig_info(param, contig_file):
tig2len = dict()
for record in SeqIO.parse(contig_file, "fasta"):
tig2len[record.id] = len(record.seq)
param["tig_info"] = tig2len
def classification_info(param, classification_file):
ext2type = dict()
for row in csv.DictReader(classification_file):
ext2type[(row["ext1"], row["ext2"])] = row["type"]
param["classification_info"] = ext2type
def hamilton_info(param, hamilton_file):
hamilton_path = list()
for row in csv.reader(hamilton_file, delimiter=";"):
hamilton_path.append((list(row[0].split(",")), row[1]))
param["hamilton_info"] = hamilton_path
def full_aag_info(param, aag_file):
aag_record = list()
for row in csv.DictReader(aag_file):
aag_record.append((row["tig1"], row["tig2"], row["nb_base"], row["nb_read"], row["nbread_contig"].split(";"), row["read1"], row["read2"]))
param["full_aag_info"] = aag_record
def build_AAG_representation(param, aag_file):
nodes = ""
edges = ""
edges_dict = dict()
tig_set = set()
for row in csv.DictReader(aag_file):
tig_set.add(row["tig1"].split("_")[0])
tig_set.add(row["tig2"].split("_")[0])
key = frozenset((row["tig1"], row["tig2"]))
if row["paths"] == "not_found":
continue
if key in edges_dict and edges_dict[key] < int(row["nb_base"]):
continue
edges_dict[frozenset((row["tig1"], row["tig2"]))] = int(row["nb_base"])
for (tig1, tig2), length in edges_dict.items():
if length <= 0:
length = "overlap"
edges += "{{ from: '{}', to: '{}', label: '{}', id: '{}' }},\n".format(tig1, tig2, length, tig1+"-"+tig2)
for tig in tig_set:
nodes += "{{ id: '{}_begin', label: '{}_begin' }},\n".format(tig, tig)
nodes += "{{ id: '{}_end', label: '{}_end' }},\n".format(tig, tig)
for tig in tig_set:
e1 = tig + "_begin"
e2 = tig + "_end"
edges += "{{from: '{}', to: '{}', label: '{}', width: 10, length: 1}},\n".format(e1, e2, tig)
param["aag_nodes"] = nodes
param["aag_edges"] = edges
if __name__ == "__main__":
main()
<h1>Augmented Assembly Graph</h1>
<div id="aag"></div>
<script type="text/javascript">
// create an array with nodes
var nodes = new vis.DataSet([
{{aag_nodes}}
]);
// create an array with edges
var edges = new vis.DataSet([
{{aag_edges}}
]);
// create a network
var container = document.getElementById('aag');
var data = {
nodes: nodes,
edges: edges
};
var options = {
height: '600px',
width: '600px',
nodes: {
shape: 'dot',
},
physics: {
forceAtlas2Based: {
avoidOverlap: 400
},
solver: 'forceAtlas2Based',
}
};
var network = new vis.Network(container, data, options);
</script>
<h1>Contig information</h1>
<table class="dataframe", border=1>
<tr>
<th>tig name</th>
<th>tig length</th>
</tr>
{% for tig, len in tig_info.items() %}
<tr>
<td>{{tig}}</td>
<td>{{len}} bases</td>
</tr>
{% endfor %}
</table>
<style>
img {
max-width: 90%;
height: auto;
}
h1 {
font-family : Verdana;
border-left : 10px solid black;
border-bottom : 1px solid black;
padding-left: 10px;
width: 100%;
}
h3 {
font-family: Verdana;
margin-left: 20px;
}
table {
border-collapse: collapse;
border: solid 1px #333333;
}
table tr:nth-child(odd){
background-color:#cccccc;
}
table tr:nth-child(even){
background-color:#ffffff;
}
table tr td {
text-align:right;
}
div.graph_image {
display: none;
border-color: black;
border-width: 2px;
border-style: solid;
}
.dataframe {
font-family : Monaco;
font-size : small;
}
.dataframe th {
color: white;
background: #2778b2;
}
.path {
text-align: center;
}
.eighty_img {
max-width: 80%;
height: auto;
}
.twenty_img {
max-width: 19%;
height: auto;
}
table.sortable th:not(.sorttable_sorted):not(.sorttable_sorted_reverse):not(.sorttable_nosort):after {
content: " \25B4\25BE"
}
.navigation {
top: 0px;
right: 0px;
width: 20%;
position: fixed;
list-style: none;
margin: 0;
padding: 0;
background: #fff;
border-color: black;
border-width: 2px;
border-style: solid;
}
.navigation ul {
}
.navigation li a {
text-align: center;
text-decoration: none;
}
.navigation li a:hover, .navigation li a:focus, .navigation li a:active {
background: #cccccc;
text-decoration: underline ;
}
</style>
{% include 'visjs.css.jinja2.html' %}
<h1>All AAG information</h1>
<table class="dataframe", border=1>
<tr>
<th>extremity 1</th>
<th>extremity 2</th>
<th>path len in bases</th>
<th>path len in reads</th>
<th>number of read assign to tig in path</th>
<th>read at extremity 1</th>
<th>read at extremity 2</th>
<!--- <th>paths</th> --->
</tr>
{% for record in full_aag_info %}
<tr>
{% for elt in record %}
<td>{{elt}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
<h1>Hamiltonian path</h1>
<table class="dataframe", border=1>
<tr>
<th>path</th>
<th>weight</th>
</tr>
{% for path, weight in hamilton_info %}
<tr>
<td>{{path}}</td>
<td>{{weight}} bases</td>
</tr>
{% endfor %}
</table>
<script>
function generate_navigation_menu()
{
var nav_list = document.getElementById("navigation_list");
var nav_list_keep = nav_list;
var h1_list = document.querySelectorAll("h1, h3");
var last_list_item;
var last_element = false;
for(var i = 0; i < h1_list.length; i++)
{
var actual_h1 = h1_list[i];
if(actual_h1.tagName == "H3" && last_element && last_element.tagName == "H1")
{
var sublist = document.createElement("ul");
last_list_item.appendChild(sublist)
nav_list = sublist;
}
if(actual_h1.tagName == "H1" && last_element && last_element.tagName == "H3")
{
nav_list = nav_list_keep;
}
var list_item = document.createElement("li");
var link_item = document.createElement("a");
link_item.href = "#" + actual_h1.getAttribute("id");
link_item.innerHTML = actual_h1.innerHTML;
list_item.appendChild(link_item);
nav_list.appendChild(list_item);
last_list_item = list_item;
last_element = actual_h1;
}
}
function onload_call_function()
{
// Navigation menu
generate_navigation_menu();
}
document.addEventListener('DOMContentLoaded', onload_call_function, false);
</script>
{% include 'visjs.javascript.jinja2.html' %}
<nav id="navigation" class="navigation">
<ul id="navigation_list">
</ul>
</nav>
<h1>Path classification</h1>
<table class="dataframe", border=1>
<tr>
<th>contig extremity 1</th>
<th>contig extremity 1</th>
<th>type</th>
</tr>
{% for (ext1, ext2), type in classification_info.items() %}
<tr>
<td>{{ext1}}</td>
<td>{{ext2}}</td>
<td>{{type}}</td>
</tr>
{% endfor %}
</table>
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>knot report</title>
{% include 'css.jinja2.html' %}
{% include 'javascript.jinja2.html' %}
</head>
<body>
{% include 'navigation.jinja2.html' %}
{% include 'contig_report.jinja2.html' %}
{% include 'path_classification.jinja2.html' %}
{% include 'aag.jinja2.html' %}
{% if hamilton_info is defined %}
{% include 'hamilton_path.jinja2.html' %}
{% endif %}
{% include 'full_aag_info.jinja2.html'%}
</body>
</html>
This diff is collapsed.
This diff is collapsed.
......@@ -42,9 +42,10 @@ setup(
'knot = knot.__main__:main',
'knot.path_search = knot.path_search.__main__:main',
'knot.sg_generation = knot.sg_generation.__main__:main',
'knot.extremity_search = knot.extremity_search.__main__:main'
'knot.analysis.classifications = knot.analysis.classifications:main'
'knot.analysis.hamilton_path = knot.analysis.hamilton_path:main'
'knot.extremity_search = knot.extremity_search.__main__:main',
'knot.analysis.classifications = knot.analysis.classifications:main',
'knot.analysis.hamilton_path = knot.analysis.hamilton_path:main',
'knot.analysis.generation_report = knot.analysis.generation_report:main'
]
}
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment