Commit 8b6e7fc6 authored by Floreal Cabanettes's avatar Floreal Cabanettes
Browse files

Add sort functionnality

parent 8fe8313c
......@@ -8,11 +8,24 @@ d3.boxplot.controls.init = function () {
};
d3.boxplot.controls.launch_sort_contigs = function () {
alert("Not supported yet!");
$.post(`/sort/${d3.boxplot.id_res}`,
{},
function (data) {
console.log(data);
}
);
$("#loading").find(".mylabel").html("Building...");
$("#loading").show();
window.setTimeout(() => {
$.post(`/sort/${d3.boxplot.id_res}`,
{},
function (data) {
if (data["success"]) {
$("#loading").find(".mylabel").html("Loading...");
window.setTimeout(() => {
d3.boxplot.launch(data, true);
}, 0);
}
else {
$("#loading").find(".mylabel").html("Loading...");
$("#loading").hide();
alert("An error occurred!");
}
}
);
}, 0);
};
\ No newline at end of file
......@@ -73,7 +73,7 @@ d3.boxplot.init = function (id, from_file=false) {
}
};
d3.boxplot.launch = function(res) {
d3.boxplot.launch = function(res, update=false) {
d3.boxplot.name_x = res["name_x"];
d3.boxplot.name_y = res["name_y"];
d3.boxplot.lines = res["lines"];
......@@ -83,11 +83,13 @@ d3.boxplot.launch = function(res) {
d3.boxplot.max_idy = res["max_idy"];
d3.boxplot.limit_idy = res["limit_idy"];
d3.boxplot.draw(res["x_contigs"], res["x_order"], res["y_contigs"], res["y_order"]);
$("div#draw").resizable({
aspectRatio: true
});
d3.boxplot.events.init();
d3.boxplot.controls.init();
if (!update) {
$("div#draw").resizable({
aspectRatio: true
});
d3.boxplot.events.init();
d3.boxplot.controls.init();
}
};
d3.boxplot.select_zone = function (x, y) {
......@@ -537,6 +539,7 @@ d3.boxplot._sort_color_idy = function(a, b) {
};
d3.boxplot.draw_legend = function () {
d3.select("#legend .draw").html(""); //Empty legend
let color_idy = d3.boxplot.color_idy;
let color_idy_len = Object.keys(color_idy).length;
let color_idy_order = ["pos+", "pos-", "neg+", "neg-"];
......
#!/usr/bin/env python3
from math import sqrt
class Paf:
limit_idy = 0.5
......@@ -13,11 +15,11 @@ class Paf:
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.lines = {}
self.q_contigs = {}
self.q_order = []
self.t_contigs = {}
self.t_order = []
self.name_q = None
self.name_t = None
self.parsed = False
......@@ -102,7 +104,7 @@ class Paf:
class_idy = "pos-"
else:
class_idy = "pos+"
lines[class_idy].append([x1, x2, y1, y2, idy])
lines[class_idy].append([x1, x2, y1, y2, idy, v1, v6])
except IOError:
self.error = "PAF file does not exist!"
return False
......@@ -144,3 +146,60 @@ class Paf:
out_f.write(json.dumps(data))
else:
raise Exception(data)
def sort(self):
gravity_contig = {}
lines_on_block = {}
# Compute size of blocks (in term of how many big match they have), and save median of each match on each one
# (for next step)
for line in [j for i in list(self.lines.values()) for j in i]:
x1 = int(line[0])
x2 = int(line[1])
y1 = int(line[2])
y2 = int(line[3])
idy = int(line[4])
contig = line[5]
chrm = line[6]
block = (contig, chrm)
med_q = x1 + (abs(x2 - x1) / 2)
len_m = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)) # Pow of len
len_m_2 = pow(1 + len_m, 2)
if block not in lines_on_block:
lines_on_block[block] = []
lines_on_block[block].append((med_q, len_m_2))
if contig not in gravity_contig:
gravity_contig[contig] = {}
if chrm not in gravity_contig[contig]:
gravity_contig[contig][chrm] = 0
gravity_contig[contig][chrm] += len_m_2
# For each contig, find best block, and deduce gravity of contig:
gravity_on_contig = {}
for contig, chr_blocks in gravity_contig.items():
# Find best block:
max_number = 0
max_chr = None
for chrm, size in chr_blocks.items():
if size > max_number:
max_number = size
max_chr = chrm
# Compute gravity of contig:
nb_items = 0
sum_items = 0
for med in lines_on_block[(contig, max_chr)]:
sum_items += med[0] * med[1]
nb_items += med[1]
gravity_on_contig[contig] = sum_items / nb_items
# Sort contigs:
self.q_order.sort(key=lambda x: gravity_on_contig[x] if x in gravity_on_contig else self.len_q + 1000)
with open(self.idx_q, "w") as idx_q_f:
idx_q_f.write(self.name_q + "\n")
for contig in self.q_order:
print(contig)
idx_q_f.write("\t".join([contig, str(self.q_contigs[contig])]) + "\n")
self.parsed = False
self.parse_paf()
......@@ -156,7 +156,17 @@ def get_graph():
@app.route('/sort/<id_res>', methods=['POST'])
def sort_graph(id_res):
pass
paf = os.path.join(app_data, id_res, "map.paf")
idx1 = os.path.join(app_data, id_res, "query.idx")
idx2 = os.path.join(app_data, id_res, "target.idx")
paf = Paf(paf, idx1, idx2)
paf.sort()
if paf.parsed:
res = paf.get_d3js_data()
res["success"] = True
return jsonify(res)
return jsonify({"success": False, "message": paf.error})
if __name__ == '__main__':
app.run()
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