Commit 9ea8a152 authored by Floreal Cabanettes's avatar Floreal Cabanettes
Browse files

Align file: client ready

parent 91b17afc
...@@ -11,7 +11,9 @@ from Bio import SeqIO ...@@ -11,7 +11,9 @@ from Bio import SeqIO
from jinja2 import Template from jinja2 import Template
from dgenies.config_reader import AppConfigReader from dgenies.config_reader import AppConfigReader
ALLOWED_EXTENSIONS = ['fa', 'fasta', 'fna', 'fa.gz', 'fasta.gz', 'fna.gz'] ALLOWED_EXTENSIONS = {"fasta": ['fa', 'fasta', 'fna', 'fa.gz', 'fasta.gz', 'fna.gz'],
"idx": ['idx'],
"map": ['paf', 'maf']}
class Functions: class Functions:
...@@ -19,10 +21,13 @@ class Functions: ...@@ -19,10 +21,13 @@ class Functions:
config = AppConfigReader() config = AppConfigReader()
@staticmethod @staticmethod
def allowed_file(filename): def allowed_file(filename, file_formats=("fasta",)):
return '.' in filename and \ for file_format in file_formats:
(filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS or ".".join(filename.rsplit('.', 2)[1:]).lower() if '.' in filename and \
in ALLOWED_EXTENSIONS) (filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS[file_format]
or ".".join(filename.rsplit('.', 2)[1:]).lower() in ALLOWED_EXTENSIONS[file_format]):
return True
return False
@staticmethod @staticmethod
def random_string(s_len): def random_string(s_len):
......
...@@ -29,10 +29,11 @@ dgenies.run.init = function (s_id, allowed_ext, max_upload_file_size=1073741824, ...@@ -29,10 +29,11 @@ dgenies.run.init = function (s_id, allowed_ext, max_upload_file_size=1073741824,
}; };
dgenies.run.restore_form = function () { dgenies.run.restore_form = function () {
dgenies.run.change_fasta_type("query", $("select.query").find(":selected").text().toLowerCase(), true); let ftypes = ["query", "target", "alignfile", "queryidx", "targetidx"];
dgenies.run.change_fasta_type("target", $("select.target").find(":selected").text().toLowerCase(), true); for (let f in ftypes) {
dgenies.run.change_fasta_type("queryidx", $("select.query").find(":selected").text().toLowerCase(), true); let ftype = ftypes[f];
dgenies.run.change_fasta_type("targetidx", $("select.target").find(":selected").text().toLowerCase(), true); dgenies.run.change_fasta_type(ftype, $(`select.${ftype}`).find(":selected").text().toLowerCase(), true);
}
}; };
dgenies.run.upload_next = function () { dgenies.run.upload_next = function () {
...@@ -54,105 +55,76 @@ dgenies.run.__upload_server_error = function(fasta, data) { ...@@ -54,105 +55,76 @@ dgenies.run.__upload_server_error = function(fasta, data) {
dgenies.run.enable_form(); dgenies.run.enable_form();
}; };
dgenies.run.allowed_file = function (filename) { dgenies.run.allowed_file = function (filename, formats) {
let allowed_ext = [];
for (let f in formats) {
let format = formats[f];
allowed_ext += dgenies.run.allowed_ext[format];
}
return filename.indexOf('.') !== -1 && return filename.indexOf('.') !== -1 &&
(dgenies.run.allowed_ext.indexOf(filename.rsplit('.', 1)[1].toLowerCase()) !== -1 || (allowed_ext.indexOf(filename.rsplit('.', 1)[1].toLowerCase()) !== -1 ||
dgenies.run.allowed_ext.indexOf(filename.rsplit('.', 2).splice(1).join(".").toLowerCase()) !== -1); allowed_ext.indexOf(filename.rsplit('.', 2).splice(1).join(".").toLowerCase()) !== -1);
};
dgenies.run._init_fileupload = function(fasta) {
//TODO: factorise init_file_uploads
}; };
dgenies.run.init_fileuploads = function () { dgenies.run._init_fileupload = function(ftype, formats, position) {
$('input.file-query').fileupload({ $(`input.file-${ftype}`).fileupload({
dataType: 'json', dataType: 'json',
formData: { formData: {
"s_id": dgenies.run.s_id "s_id": dgenies.run.s_id,
"formats": formats
}, },
add: function (e, data) { add: function (e, data) {
let filename = data.files[0].name; let filename = data.files[0].name;
if (dgenies.run.allowed_file(filename)) if (dgenies.run.allowed_file(filename, formats))
dgenies.run.files[0] = data; dgenies.run.files[position] = data;
else { else {
$("input.file-query").trigger("change"); // The value is null after fired $(`input.file-${ftype}`).trigger("change"); // The value is null after fired
dgenies.notify(`File <b>${filename}</b> is not supported!`, "danger", 3000) dgenies.notify(`File <b>${filename}</b> is not supported!`, "danger", 3000)
} }
}, },
progressall: function (e, data) { progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10); var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress-query').find('.bar').css( $(`#progress-${ftype}`).find('.bar').css(
'width', 'width',
progress + '%' progress + '%'
); );
}, },
success: function (data, success) { success: function (data, success) {
if (data["success"] !== "OK") { if (data["success"] !== "OK") {
dgenies.run.__upload_server_error("query", data); dgenies.run.__upload_server_error(ftype, data);
} }
else if ("error" in data["files"][0]) { else if ("error" in data["files"][0]) {
dgenies.run.add_error("Query file: " + data["files"][0]["error"], "error"); dgenies.run.add_error("Query file: " + data["files"][0]["error"], "error");
dgenies.run.enable_form(); dgenies.run.enable_form();
} }
else { else {
$("input#query").val(data["files"][0]["name"]); $(`input#ftype`).val(data["files"][0]["name"]);
dgenies.run.hide_loading("query"); dgenies.run.hide_loading(ftype);
dgenies.run.show_success("query"); dgenies.run.show_success(ftype);
dgenies.run.upload_next();
}
},
error: function (data, success) {
dgenies.run.__upload_server_error("query", data);
}
});
$('input.file-target').fileupload({
dataType: 'json',
formData: {
"s_id": dgenies.run.s_id
},
add: function (e, data) {
let filename = data.files[0].name
if (dgenies.run.allowed_file(filename))
dgenies.run.files[1] = data;
else {
$("input.file-target").trigger("change"); // The value is null after fired
dgenies.notify(`File <b>${filename}</b> is not supported!`, "danger", 3000)
}
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress-target').find('.bar').css(
'width',
progress + '%'
);
},
success: function (data, success) {
if (data["success"] !== "OK") {
dgenies.run.__upload_server_error("target", data);
}
else if ("error" in data["files"][0]) {
dgenies.run.add_error("Target file: " + data["files"][0]["error"], "error");
dgenies.run.enable_form();
}
else {
$("input#target").val(data["files"][0]["name"]);
dgenies.run.hide_loading("target");
dgenies.run.show_success("target");
dgenies.run.upload_next(); dgenies.run.upload_next();
} }
}, },
error: function (data, success) { error: function (data, success) {
dgenies.run.__upload_server_error("target", data); dgenies.run.__upload_server_error(ftype, data);
} }
}); });
};
//Trigger events on hidden file inputs: dgenies.run.init_fileuploads = function () {
$("button#button-query").click(function() { let ftypes = {"query": {"formats": ["fasta",], "position": 0},
$("input.file-query").trigger("click"); "target": {"formats": ["fasta",], "position": 1},
}) "queryidx": {"formats": ["fasta", "idx"], "position": 2},
$("button#button-target").click(function() { "targetidx": {"formats": ["fasta", "idx"], "position": 3},
$("input.file-target").trigger("click"); "alignfile": {"formats": ["map"], "position": 4},};
}) for (let ftype in ftypes) {
let formats = ftypes[ftype]["formats"];
let position = ftypes[ftype]["position"];
dgenies.run._init_fileupload(ftype, formats, position);
//Trigger events on hidden file inputs:
$(`button#button-${ftype}`).click(function() {
$(`input.file-${ftype}`).trigger("click");
});
}
}; };
dgenies.run.get_file_size_str = function(size) { dgenies.run.get_file_size_str = function(size) {
...@@ -169,6 +141,7 @@ dgenies.run.get_file_size_str = function(size) { ...@@ -169,6 +141,7 @@ dgenies.run.get_file_size_str = function(size) {
}; };
dgenies.run.fill_examples = function () { dgenies.run.fill_examples = function () {
dgenies.run.show_tab("tab1");
$("select.target").val("1").trigger("change"); $("select.target").val("1").trigger("change");
$("input#target").val("example://" + dgenies.run.target_example); $("input#target").val("example://" + dgenies.run.target_example);
if (dgenies.run.query_example !== "") { if (dgenies.run.query_example !== "") {
...@@ -177,65 +150,52 @@ dgenies.run.fill_examples = function () { ...@@ -177,65 +150,52 @@ dgenies.run.fill_examples = function () {
} }
}; };
dgenies.run.set_events = function() { dgenies.run._set_file_event = function(ftype) {
let max_file_size_txt = dgenies.run.get_file_size_str(dgenies.run.max_upload_file_size); let max_file_size_txt = dgenies.run.get_file_size_str(dgenies.run.max_upload_file_size);
$("input.file-query").change(function () { $(`input.file-${ftype}`).change(function () {
let file_size_query = $("div.file-size.query"); let file_size = $(`div.file-size.${ftype}`);
if (this.files.length > 0) if (this.files.length > 0)
if (this.files[0].size <= dgenies.run.max_upload_file_size) { if (this.files[0].size <= dgenies.run.max_upload_file_size) {
file_size_query.html(dgenies.run.get_file_size_str(this.files[0].size)); file_size.html(dgenies.run.get_file_size_str(this.files[0].size));
dgenies.run.set_filename(this.files[0].name, "query"); dgenies.run.set_filename(this.files[0].name, ftype);
} }
else { else {
$(this).val(""); $(this).val("");
dgenies.run.set_filename("", "query"); dgenies.run.set_filename("", ftype);
dgenies.notify(`File exceed the size limit (${max_file_size_txt})`, "danger", 2000); dgenies.notify(`File exceed the size limit (${max_file_size_txt})`, "danger", 2000);
file_size_query.html(""); file_size.html("");
} }
else { else {
dgenies.run.set_filename("", "query"); dgenies.run.set_filename("", ftype);
file_size_query.html(""); file_size.html("");
} }
}); });
};
$("input.file-target").change(function () { dgenies.run._set_file_select_event = function(ftype) {
let file_size_target = $("div.file-size.target"); $(`select.${ftype}`).change(function() {
if (this.files.length > 0) { dgenies.run.change_fasta_type(ftype, $(`select.${ftype}`).find(":selected").text().toLowerCase())
if (this.files[0].size <= dgenies.run.max_upload_file_size) {
file_size_target.html(dgenies.run.get_file_size_str(this.files[0].size));
dgenies.run.set_filename(this.files[0].name, "target");
}
else {
$(this).val("");
dgenies.run.set_filename("", "target");
dgenies.notify(`File exceed the size limit (${max_file_size_txt})`, "danger", 2000);
file_size_target.html("");
}
}
else {
dgenies.run.set_filename("", "target");
file_size_target.html("");
}
}); });
};
dgenies.run.show_tab = function(tab) {
$(`#tabs #${tab}`).addClass("active");
$(`#tabs .tab:not(#${tab})`).removeClass("active");
$(`.tabx:not(${tab})`).hide();
$(`.tabx.${tab}`).show();
};
dgenies.run.set_events = function() {
let ftypes = ["query", "target", "alignfile", "queryidx", "targetidx"];
for (let f in ftypes) {
let ftype = ftypes[f];
dgenies.run._set_file_event(ftype);
dgenies.run._set_file_select_event(ftype);
}
$("button#submit").click(function () { $("button#submit").click(function () {
dgenies.run.submit(); dgenies.run.submit();
}); });
$("select.query").change(function() {
dgenies.run.change_fasta_type("query", $("select.query").find(":selected").text().toLowerCase())
});
$("select.target").change(function() {
dgenies.run.change_fasta_type("target", $("select.target").find(":selected").text().toLowerCase())
});
$("select.queryidx").change(function() {
dgenies.run.change_fasta_type("queryidx", $("select.queryidx").find(":selected").text().toLowerCase())
});
$("select.targetidx").change(function() {
dgenies.run.change_fasta_type("targetidx", $("select.targetidx").find(":selected").text().toLowerCase())
});
$("select.alignfile").change(function() {
dgenies.run.change_fasta_type("alignfile", $("select.alignfile").find(":selected").text().toLowerCase())
});
$("button#example").click(function() { $("button#example").click(function() {
dgenies.run.fill_examples(); dgenies.run.fill_examples();
}); });
...@@ -244,13 +204,6 @@ dgenies.run.set_events = function() { ...@@ -244,13 +204,6 @@ dgenies.run.set_events = function() {
}) })
}; };
dgenies.run.show_tab = function(tab) {
$(`#tabs #${tab}`).addClass("active");
$(`#tabs .tab:not(#${tab})`).removeClass("active");
$(`.tabx:not(${tab})`).hide();
$(`.tabx.${tab}`).show();
};
dgenies.run.change_fasta_type = function (fasta, type, keep_url=false) { dgenies.run.change_fasta_type = function (fasta, type, keep_url=false) {
let button = $("button#button-" + fasta); let button = $("button#button-" + fasta);
let input = $("input#" + fasta); let input = $("input#" + fasta);
...@@ -288,28 +241,65 @@ dgenies.run.enable_form = function () { ...@@ -288,28 +241,65 @@ dgenies.run.enable_form = function () {
$("input, select, button").prop("disabled", false); $("input, select, button").prop("disabled", false);
$("div#uploading-loading").hide(); $("div#uploading-loading").hide();
$("button#submit").show(); $("button#submit").show();
dgenies.run.hide_loading("query"); let ftypes = ["query", "target", "alignfile", "targetidx", "queryidx"];
dgenies.run.hide_loading("target"); for (let f in ftypes) {
dgenies.run.hide_success("query"); let ftype = ftypes[f];
dgenies.run.hide_success("target"); dgenies.run.hide_loading(ftype);
dgenies.run.files = [undefined, undefined]; dgenies.run.hide_success(ftype);
}
dgenies.run.files = [undefined, undefined, undefined, undefined, undefined];
dgenies.run.restore_form(); dgenies.run.restore_form();
dgenies.run.enabled = true; dgenies.run.enabled = true;
}; };
dgenies.run.reset_file_form = function(tab) {
let ftypes = [];
let i = 0;
if (tab === "tab2") {
ftypes = ["alignfile", "queryidx", "targetidx"];
i = 2;
}
else {
ftypes = ["query", "target"];
i = 0;
}
for (let f in ftypes) {
let ftype = ftypes[f];
dgenies.run.change_fasta_type(ftype, $(`select.${ftype}`).find(":selected").text().toLowerCase(), true);
dgenies.run.files[i] = undefined;
i++;
}
};
dgenies.run.do_submit = function () { dgenies.run.do_submit = function () {
$("div#uploading-loading").html("Submitting form..."); let data = {
dgenies.post("/launch_analysis", "id_job": $("input#id_job").val(),
{ "email": dgenies.mode === "webserver" ? $("input#email").val() : "",
"id_job": $("input#id_job").val(), "s_id": dgenies.run.s_id
"email": dgenies.mode === "webserver" ? $("input#email").val() : "", };
let tab = $("#tabs .tab.active").attr("id");
if (tab === "tab1") {
data = Object.assign({}, data, {
"query": $("input#query").val(), "query": $("input#query").val(),
"query_type": $("select.query").find(":selected").text().toLowerCase(), "query_type": $("select.query").find(":selected").text().toLowerCase(),
"target": $("input#target").val(), "target": $("input#target").val(),
"target_type": $("select.target").find(":selected").text().toLowerCase(), "target_type": $("select.target").find(":selected").text().toLowerCase(),
"s_id": dgenies.run.s_id,
"tool": $("input[name=tool]:checked").val() "tool": $("input[name=tool]:checked").val()
}, });
}
else {
data = Object.assign({}, data, {
"alignfile": $("input#alignfile").val(),
"alignfile_type": $("select.alignfile").find(":selected").text().toLowerCase(),
"query": $("input#queryidx").val(),
"query_type": $("select.queryidx").find(":selected").text().toLowerCase(),
"target": $("input#targetidx").val(),
"target_type": $("select.targetidx").find(":selected").text().toLowerCase(),
});
}
$("div#uploading-loading").html("Submitting form...");
dgenies.post("/launch_analysis",
data,
function (data, status) { function (data, status) {
if (data["success"]) { if (data["success"]) {
window.location = data["redirect"]; window.location = data["redirect"];
...@@ -476,38 +466,42 @@ dgenies.run.reset_other_tab = function(tab) { ...@@ -476,38 +466,42 @@ dgenies.run.reset_other_tab = function(tab) {
} }
}; };
dgenies.run.start_uploads = function() { dgenies.run._start_upload = function(ftype, fname) {
let tab = $("#tabs .tab.active").attr("id");
let query_type = parseInt($("select.query").val());
let has_uploads = false; let has_uploads = false;
let query_val = $("input#query").val(); let fasta_type = parseInt($(`select.${ftype}`).val());
if (query_type === 0 && query_val.length > 0) { let fasta_val = $(`input#${ftype}`).val();
$("button#button-query").hide(); if (fasta_type === 0 && fasta_val.length > 0) {
dgenies.run.show_loading("query"); $(`button#button-${ftype}`).hide();
dgenies.run.show_loading(ftype);
has_uploads = true; has_uploads = true;
} }
else { else {
dgenies.run.files[0] = undefined; dgenies.run.files[0] = undefined;
if (query_val !== "" && !dgenies.run.check_url(query_val)) { if (fasta_val !== "" && !dgenies.run.check_url(fasta_val)) {
dgenies.run.add_error("Query file: invalid URL", "error"); dgenies.run.add_error(`${fname} file: invalid URL`, "error");
dgenies.run.enable_form(); dgenies.run.enable_form();
return false; return false;
} }
} }
let target_type = parseInt($("select.target").val()); return has_uploads;
let target_val = $("input#target").val(); };
if (target_type === 0 && target_val.length > 0) {
$("button#button-target").hide(); dgenies.run.start_uploads = function() {
dgenies.run.show_loading("target"); let has_uploads = false;
has_uploads = true; let tab = $("#tabs .tab.active").attr("id");
let inputs = [];
if (tab === "tab1") {
dgenies.run.reset_file_form("tab2");
inputs = [["query", "Query"], ["target", "Target"]];
} }
else { else {
dgenies.run.files[1] = undefined; dgenies.run.reset_file_form("tab1");
if (target_val !== "" && !dgenies.run.check_url(target_val)) { inputs = [["queryidx", "Query"], ["targetidx", "Target"], ["alignfile", "Alignment"]]
dgenies.run.add_error("Target file: invalid URL", "error"); }
dgenies.run.enable_form(); for (let i in inputs) {
return false; let input = inputs[i];
} let test_has_uploads = dgenies.run._start_upload(input[0], input[1]);
has_uploads = has_uploads || test_has_uploads;
} }
if (has_uploads) { if (has_uploads) {
$("div#uploading-loading").html("Asking for upload..."); $("div#uploading-loading").html("Asking for upload...");
......
...@@ -139,8 +139,14 @@ ...@@ -139,8 +139,14 @@
</td> </td>
</tr> </tr>
{% else %} {% else %}
<input type="radio" name="tool" id="tool" value="{{ tools[0] }}" style="display: none;" <tr class="tabx tab1">
checked/> <td colspan="2">
{% for tool in tools_names %}
<input type="radio" name="tool" id="tool" value="{{ tool }}" style="display: none;"
checked/>
{% endfor %}
</td>
</tr>
{% endif %} {% endif %}
{# Tab 2 #} {# Tab 2 #}
...@@ -253,6 +259,9 @@ ...@@ -253,6 +259,9 @@
<div class="hidden-input-files"> <div class="hidden-input-files">
<input type="file" name="file-query" class="file-query" data-url="/upload" title=""/> <input type="file" name="file-query" class="file-query" data-url="/upload" title=""/>
<input type="file" name="file-target" class="file-target" data-url="/upload"/> <input type="file" name="file-target" class="file-target" data-url="/upload"/>
<input type="file" name="file-alignfile" class="file-alignfile" data-url="/upload"/>
<input type="file" name="file-targetidx" class="file-targetidx" data-url="/upload"/>
<input type="file" name="file-queryidx" class="file-queryidx" data-url="/upload"/>
</div> </div>
</form> </form>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -6,6 +6,7 @@ import datetime ...@@ -6,6 +6,7 @@ import datetime
import shutil