Commit 1ff9ff9c authored by Floreal Cabanettes's avatar Floreal Cabanettes
Browse files

Code refactoring + start adding sort of contigs

parent 8e765e63
if (!d3 || !d3.boxplot) {
throw "d3.boxplot wasn't included!"
}
d3.boxplot.controls = {};
d3.boxplot.controls.init = function () {
$("#sort-contigs").click(d3.boxplot.controls.launch_sort_contigs)
};
d3.boxplot.controls.launch_sort_contigs = function () {
alert("Not supported yet!");
$.post(`/sort/${d3.boxplot.id_res}`,
{},
function (data) {
console.log(data);
}
);
};
\ No newline at end of file
......@@ -4,6 +4,8 @@ if (!d3) {
d3.boxplot = {};
//GLOBAL VARIABLES:
d3.boxplot.id_res = null;
d3.boxplot.svgcontainer = null;
d3.boxplot.container = null;
d3.boxplot.svgsupercontainer = null;
......@@ -46,6 +48,7 @@ d3.boxplot.break_lines_color = "#7c7c7c";
d3.boxplot.min_sizes = [0, 0.01, 0.02, 0.03, 0.05, 1, 2];
d3.boxplot.init = function (id, from_file=false) {
d3.boxplot.id_res = id;
$("#filter_size").val(0);
$("#stroke-linecap").prop("checked", false);
$("#stroke-width").val(1);
......@@ -53,17 +56,7 @@ d3.boxplot.init = function (id, from_file=false) {
$.post("/get_graph",
{"id": id},
function (data) {
let res = null;
try {
res = JSON.parse(data);
}
catch (e) {
console.log(data);
console.warn("Unable to load data");
}
if (res) {
d3.boxplot.launch(res);
}
d3.boxplot.launch(data);
}
)
}
......@@ -90,6 +83,7 @@ d3.boxplot.launch = function(res) {
aspectRatio: true
});
d3.boxplot.events.init();
d3.boxplot.controls.init();
};
d3.boxplot.select_zone = function (x, y) {
......
#!/usr/bin/env python3
class Paf:
limit_idy = 0.5
def __init__(self, paf, idx_q, idx_t):
self.paf = paf
self.idx_q = idx_q
self.idx_t = idx_t
self.len_q = None
self.len_t = None
self.min_idy = None
self.max_idy = None
self.lines = None
self.q_contigs = None
self.q_order = None
self.t_contigs = None
self.t_order = None
self.name_q = None
self.name_t = None
self.parsed = False
self.error = False
self.parse_paf()
def parse_paf(self):
len_q = 0
len_t = 0
min_idy = 10000000000
max_idy = -10000000000
name_q = None
name_t = None
lines = {
"pos+": [],
"pos-": [],
"neg+": [],
"neg-": []
}
try:
with open(self.paf, "r") as paf_file:
for line in paf_file:
parts = line.strip("\n").split("\t")
v1 = parts[0]
v6 = parts[5]
ignore = False
strand = 1 if parts[4] == "+" else -1
idy = int(parts[9]) / int(parts[10]) * strand
min_idy = min(min_idy, idy)
max_idy = max(max_idy, idy)
if name_q is None:
name_q = v1
elif name_q != v1:
ignore = True
if not ignore:
name_t = v6
len_q = int(parts[1])
len_t = int(parts[6])
# x1, x2, y1, y2, idy
x1 = int(parts[2])
x2 = int(parts[3])
y1 = int(parts[7 if strand == 1 else 8])
y2 = int(parts[8 if strand == 1 else 7])
if idy < -self.limit_idy:
class_idy = "neg-"
elif idy < 0:
class_idy = "neg+"
elif idy < self.limit_idy:
class_idy = "pos-"
else:
class_idy = "pos+"
lines[class_idy].append([x1, x2, y1, y2, idy])
except IOError:
self.error = "PAF file does not exist!"
return False
try:
with open(self.idx_q, "r") as idx_q_f:
q_order = []
q_contigs = {}
for line in idx_q_f:
parts = line.strip("\n").split("\t")
id_c = parts[0]
len_c = int(parts[1])
q_order.append(id_c)
q_contigs[id_c] = len_c
except IOError:
self.error = "Index file does not exist for query!"
return False
try:
with open(self.idx_t, "r") as idx_t_f:
t_order = []
t_contigs = {}
for line in idx_t_f:
parts = line.strip("\n").split("\t")
id_c = parts[0]
len_c = int(parts[1])
t_order.append(id_c)
t_contigs[id_c] = len_c
except IOError:
self.error = "Index file does not exist for target!"
return False
self.parsed = True
self.len_q = len_q
self.len_t = len_t
self.min_idy = min_idy
self.max_idy = max_idy
self.lines = lines
self.q_contigs = q_contigs
self.q_order = q_order
self.t_contigs = t_contigs
self.t_order = t_order
self.name_q = name_q
self.name_t = name_t
def get_d3js_data(self):
return {
'x_len': self.len_q,
'y_len': self.len_t,
'min_idy': self.min_idy,
'max_idy': self.max_idy,
'lines': self.lines,
'x_contigs': self.q_contigs,
'x_order': self.q_order,
'y_contigs': self.t_contigs,
'y_order': self.t_order,
'name_x': self.name_q,
'name_y': self.name_t,
'limit_idy': self.limit_idy
}
def save_json(self, out):
import json
success, data = self.parse_paf()
if success:
with open(out, "w") as out_f:
out_f.write(json.dumps(data))
else:
raise Exception(data)
#!/usr/bin/env python3
limit_idy = 0.5
def parse_paf(paf, idx1, idx2):
len_x = 0
len_y = 0
min_idy = 10000000000
max_idy = -10000000000
first_sample = None
second_sample = None
lines = {
"pos+": [],
"pos-": [],
"neg+": [],
"neg-": []
}
try:
with open(paf, "r") as paf_file:
for line in paf_file:
parts = line.strip("\n").split("\t")
v1 = parts[0]
v6 = parts[5]
ignore = False
strand = 1 if parts[4] == "+" else -1
idy = int(parts[9]) / int(parts[10]) * strand
min_idy = min(min_idy, idy)
max_idy = max(max_idy, idy)
if first_sample is None:
first_sample = v1
elif first_sample != v1:
ignore = True
if not ignore:
second_sample = v6
len_x = int(parts[1])
len_y = int(parts[6])
# x1, x2, y1, y2, idy
x1 = int(parts[2])
x2 = int(parts[3])
y1 = int(parts[7 if strand == 1 else 8])
y2 = int(parts[8 if strand == 1 else 7])
if idy < -limit_idy:
class_idy = "neg-"
elif idy < 0:
class_idy = "neg+"
elif idy < limit_idy:
class_idy = "pos-"
else:
class_idy = "pos+"
lines[class_idy].append([x1, x2, y1, y2, idy])
except IOError:
return False, "PAF file does not exist!"
try:
with open(idx1, "r") as idx1_f:
x_order = []
x_contigs = {}
for line in idx1_f:
parts = line.strip("\n").split("\t")
id_c = parts[0]
len_c = int(parts[1])
x_order.append(id_c)
x_contigs[id_c] = len_c
except IOError:
return False, "Index file does not exist for sample 1!"
try:
with open(idx2, "r") as idx2_f:
y_order = []
y_contigs = {}
for line in idx2_f:
parts = line.strip("\n").split("\t")
id_c = parts[0]
len_c = int(parts[1])
y_order.append(id_c)
y_contigs[id_c] = len_c
except IOError:
return False, "Index file does not exist for sample 2!"
return True, {
'x_len': len_x,
'y_len': len_y,
'min_idy': min_idy,
'max_idy': max_idy,
'lines': lines,
'x_contigs': x_contigs,
'x_order': x_order,
'y_contigs': y_contigs,
'y_order': y_order,
'name_x': first_sample,
'name_y': second_sample,
'limit_idy': limit_idy
}
def save_json(paf, idx1, idx2, out):
import json
success, data = parse_paf(paf, idx1, idx2)
if success:
with open(out, "w") as out_f:
out_f.write(json.dumps(data))
else:
raise Exception(data)
#!/usr/bin/env python3
import json
import time
import datetime
from flask import Flask, render_template, request, redirect, flash, url_for
from flask import Flask, render_template, request, redirect, flash, url_for, jsonify
from werkzeug.utils import secure_filename
from lib.parse_paf import parse_paf
from lib.paf import Paf
from config_reader import AppConfigReader
from lib.job_manager import JobManager
from lib.functions import *
......@@ -134,9 +133,15 @@ def get_graph():
idx1 = os.path.join(app_data, id_f, "query.idx")
idx2 = os.path.join(app_data, id_f, "target.idx")
success, res = parse_paf(paf, idx1, idx2)
paf = Paf(paf, idx1, idx2)
if success:
if paf.parsed:
res = paf.get_d3js_data()
res["success"] = True
return json.dumps(res)
return json.dumps({"success": False, "message": res})
return jsonify(res)
return jsonify({"success": False, "message": paf.error})
@app.route('/sort/<id_res>', methods=['POST'])
def sort_graph(id_res):
pass
......@@ -3,14 +3,11 @@
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
{% block scripts %}
<script src="{{ url_for('static', filename='js/jquery-3.2.1.min.js') }}" type="text/JavaScript"></script>
<script src="{{ url_for('static', filename='js/jquery-ui.min.js') }}" type="text/JavaScript"></script>
<script src="{{ url_for('static', filename='js/popper.min.js') }}" type="text/JavaScript"></script>
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}" type="text/JavaScript"></script>
<script src="{{ url_for('static', filename='js/d3.min.js') }}" type="text/JavaScript"></script>
<script src="{{ url_for('static', filename='js/d3.boxplot.js') }}" type="text/JavaScript"></script>
<script src="{{ url_for('static', filename='js/d3.boxplot.events.js') }}" type="text/JavaScript"></script>
{% endblock %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/jquery-ui.min.css') }}" type="text/css">
<link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap.min.css') }}" type="text/css">
<link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap-theme.min.css') }}" type="text/css">
......
{% extends 'base.html' %}
{% block scripts %}
{{ super() }}
<script src="{{ url_for('static', filename='js/jquery-ui.min.js') }}" type="text/JavaScript"></script>
<script src="{{ url_for('static', filename='js/d3.min.js') }}" type="text/JavaScript"></script>
<script src="{{ url_for('static', filename='js/d3.boxplot.js') }}" type="text/JavaScript"></script>
<script src="{{ url_for('static', filename='js/d3.boxplot.events.js') }}" type="text/JavaScript"></script>
<script src="{{ url_for('static', filename='js/d3.boxplot.controls.js') }}" type="text/JavaScript"></script>
{% endblock %}
{% block onload %}d3.boxplot.init('{{ id }}');{% endblock %}
{% block content %}
{{ super() }}
......@@ -19,6 +27,7 @@
<p><label class="input-checkbox-label"><input type="checkbox" id="stroke-linecap"/> Strong precision</label></p>
<p><label>Stroke width:<br/>
<input type="range" min="0" max="3" value="1" step="1" id="stroke-width"/></label></p>
<p><button id="sort-contigs">Sort contigs</button></p>
</div>
</div>
<div id="loading">
......
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