Skip to content

Commit

Permalink
Merge 8a2a167 into 5dc2b40
Browse files Browse the repository at this point in the history
  • Loading branch information
antgonza committed May 12, 2017
2 parents 5dc2b40 + 8a2a167 commit af7be28
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 108 deletions.
88 changes: 64 additions & 24 deletions qiita_pet/handlers/qiita_redbiom.py
Expand Up @@ -6,6 +6,7 @@
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from future.utils import viewitems
from requests import ConnectionError
import redbiom.summarize
import redbiom.search
Expand Down Expand Up @@ -83,42 +84,81 @@ def _redbiom_search(self, query, search_on, callback):
(prep_template_id)
JOIN qiita.study USING (study_id)
WHERE sample_id IN %s
GROUP BY study_title, study_id, artifact_id)
SELECT study_title, study_id, samples,
name, command_id,
(main_query.children).artifact_id AS artifact_id
FROM main_query
JOIN qiita.artifact a ON
(main_query.children).artifact_id = a.artifact_id
JOIN qiita.artifact_type at ON (
at.artifact_type_id = a.artifact_type_id
AND artifact_type = 'BIOM')
ORDER BY artifact_id
GROUP BY study_title, study_id, artifact_id),
artifact_query AS (
SELECT study_title, study_id, samples,
name, command_id,
(main_query.children).artifact_id AS
artifact_id
FROM main_query
JOIN qiita.artifact a ON
(main_query.children).artifact_id =
a.artifact_id
JOIN qiita.artifact_type at ON (
at.artifact_type_id = a.artifact_type_id
AND artifact_type = 'BIOM')),
parent_query AS (
SELECT artifact_query.*,
array_agg(parent_params) as parent_parameters
FROM artifact_query
LEFT JOIN qiita.parent_artifact pa ON (
artifact_query.artifact_id = pa.artifact_id)
LEFT JOIN qiita.artifact a ON (
pa.parent_id = a.artifact_id),
json_each_text(command_parameters)
parent_params
GROUP BY artifact_query.study_title,
artifact_query.study_id,
artifact_query.samples, artifact_query.name,
artifact_query.command_id,
artifact_query.artifact_id)
SELECT * FROM parent_query
ORDER BY parent_parameters, artifact_id
"""

sql_params = """
SELECT parameter_set_name, array_agg(ps) AS param_set
FROM qiita.default_parameter_set,
json_each_text(parameter_set) ps
GROUP BY parameter_set_name"""

with qdbsc.TRN:
qdbsc.TRN.add(sql, [tuple(features)])
results = []
commands = {}
# obtaining all existing parameters, note that
# they are not that many (~40) and we don't expect
# to have a huge growth in the near future
qdbsc.TRN.add(sql_params)
params = {pname: eval(params) for pname, params
in qdbsc.TRN.execute_fetchindex()}

# now let's get the actual artifacts
qdbsc.TRN.add(sql, [tuple(features)])
for row in qdbsc.TRN.execute_fetchindex():
title, sid, samples, name, cid, aid = row
title, sid, samples, name, cid, aid, pp = row
nr = {'study_title': title, 'study_id': sid,
'artifact_id': aid, 'aname': name,
'samples': samples}
if cid is not None:
if cid not in commands:
c = qdb.software.Command(cid)
commands[cid] = {
'sfwn': c.software.name,
'sfv': c.software.version,
'cmdn': c.name
}
nr['command'] = commands[cid]['cmdn']
nr['software'] = commands[cid]['sfwn']
nr['version'] = commands[cid]['sfv']
commands[cid] = '%s - %s v%s' % (
c.name, c.software.name,
c.software.version)

# [-1] taking the last cause it's sorted by
# the number of overlapping parameters
# [0] then taking the first element that is
# the name of the parameter set
ppc = sorted(
[[k, len(eval(pp) & v)]
for k, v in viewitems(params)],
key=lambda x: x[1])[-1][0]

nr['command'] = '%s @ %s' % (
commands[cid], ppc)
else:
nr['command'] = None
nr['software'] = None
nr['version'] = None
nr['command'] = ''
results.append(nr)
else:
sql = """
Expand Down
14 changes: 0 additions & 14 deletions qiita_pet/static/js/qiita.js
Expand Up @@ -301,17 +301,3 @@ function display_number_of_samples_added(num_samples) {
bootstrapAlert(num_samples + ' samples selected.', "success", 10000);
$('#dflt-sel-info').css('color', 'rgb(0, 160, 0)');
}

/**
*
* Function to show the loading gif in a given div
*
* @param div_name string with the name of the div to populate with the loading gif
*
* This function replaces the content of the given div with the
* gif to show that the section of page is loading
*
*/
function show_loading(div_name) {
$("#" + div_name).html("<img src='{% raw qiita_config.portal_dir %}/static/img/waiting.gif' style='display:block;margin-left: auto;margin-right: auto'/>");
}
134 changes: 82 additions & 52 deletions qiita_pet/templates/redbiom.html
Expand Up @@ -36,15 +36,34 @@
{ "data": null, "width": "20%" },
{ "data": null, "width": "10%" }
],
"order": [[ 3, 'asc' ]],
// "displayLength": 25,
"drawCallback": function ( settings ) {
var api = this.api();
var rows = api.rows( {page:'current'} ).nodes();
var last=null;

api.column(3, {page:'current'} ).data().each( function ( data, i ) {
group = data.command;
if ( last !== group ) {
$(rows).eq( i ).before(
'<tr class="group"><td colspan="5">'+group+'</td></tr>'
);
last = group;
}
});
},
columnDefs: [
{type:'natural', targets:[1,2,3,4]},
{ "visible": false, "targets": 3 },
{ type:'natural', targets: [1,2,4] },
// render Add to Analysis button
{"render": function ( data, type, row, meta ) {
var text = '';
{% if current_user is not None %}
var text = '<input type="button" class="btn btn-sm" value="Add to analysis" onclick="redbiom_send_to_moi(' +
row.artifact_id + ', ' + meta.row + ')">';
{% else %}
var text = '';
if ($("#search_on").val() != 'categories'){
var text = '<input type="button" class="btn btn-sm" value="Add to analysis" onclick="redbiom_send_to_moi(' +
row.artifact_id + ', ' + meta.row + ')">';
}
{% end %}
return text;
}, targets: [0]},
Expand All @@ -58,21 +77,15 @@
{% end %}
return text;
}, targets: [1]},
// render Artifact Processing
// render Artifact Name or Metadata Categories
{"render": function ( data, type, row, meta ) {
var text = '';
if (!(!row.command)) {
text += row.command;
}
if (!(!row.software)) {
text += ' ' + row.software;
}
if (!(!row.version)) {
text += ' v' + row.version;
}
var text = row.aname;
if ($("#search_on").val() == 'categories'){
text = row.samples.join(', ');
}
return text;
}, targets: [3]},
// render Artifact Processing
}, targets: [2]},
// render # of samples/categories
{"render": function ( data, type, row, meta ) {
return row.samples.length;
}, targets: [4]}
Expand All @@ -84,46 +97,63 @@
},
});

$("#submitForm").submit(function(event){
event.preventDefault();
$("#submitForm").submit(function(event){
event.preventDefault();

show_loading("redbiom-info");
show_loading("redbiom-info");

var search = $("#search").val();
var search_on = $("#search_on").val();
var search = $("#search").val();
var search_on = $("#search_on").val();

$.post("{% raw qiita_config.portal_dir %}/redbiom/", {'search': search, 'search_on': search_on})
.done(function ( data ){
var redbiom_table = $('#redbiom-table').DataTable();
var redbiom_info = $('#redbiom-info');
redbiom_table.clear().draw();
if(data.status == "error") {
bootstrapAlert(data.message.replace("\n", "<br/>"), "danger");
} else {
if(data.message != ''){
redbiom_info.html(
`<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Warning!</strong> ` + data.message + `</div><br/>`)
} else if(data.data){
$('#redbiom-table tr:eq(0) th:eq(1)').text("Study Title");
if (search_on != 'categories') {
{% if current_user is not None %}
$('#redbiom-table tr:eq(0) th:eq(0)').text("Add to Analysis");
{% end %}
$('#redbiom-table tr:eq(0) th:eq(2)').text("Artifact Name");
$('#redbiom-table tr:eq(0) th:eq(3)').text("Artifact Processing");
$('#redbiom-table tr:eq(0) th:eq(4)').text("# of Samples Found");
} else {
$('#redbiom-table tr:eq(0) th:eq(3)').text("Metadata Categories");
$('#redbiom-table tr:eq(0) th:eq(4)').text("# of Categories Found");
}
redbiom_table.rows.add(data.data).draw();
redbiom_info.html('');
$.post("{% raw qiita_config.portal_dir %}/redbiom/", {'search': search, 'search_on': search_on})
.done(function ( data ){
var redbiom_table = $('#redbiom-table').DataTable();
var redbiom_info = $('#redbiom-info');
redbiom_table.clear().draw();
if(data.status == "error") {
bootstrapAlert(data.message.replace("\n", "<br/>"), "danger");
} else {
if(data.message != ''){
redbiom_info.html(
`<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Warning!</strong> ` + data.message + `</div><br/>`)
} else if(data.data){
{% if current_user is not None %}
var header_0 = "Add to Analysis";
{% else %}
var header_0 = "";
{% end %}
var header_2 = "Artifact Name";
var header_3 = "# of Samples Found";
if (search_on == 'categories') {
header_2 = "Metadata Categories";
header_3 = "# of Categories Found";
}
$('#redbiom-table tr:eq(0) th:eq(0)').text(header_0);
$('#redbiom-table tr:eq(0) th:eq(1)').text("Study Title");
$('#redbiom-table tr:eq(0) th:eq(2)').text(header_2);
$('#redbiom-table tr:eq(0) th:eq(3)').text(header_3);

redbiom_table.rows.add(data.data).draw();
redbiom_info.html('');
}
});
}
});
});

// Order by the grouping
$('#redbiom-table tbody').on( 'click', 'tr.group', function () {
var table = $('#redbiom-table').DataTable();
var currentOrder = table.order()[0];
if (currentOrder[0] === 3 && currentOrder[1] === 'asc') {
table.order([ 3, 'desc' ]).draw();
}
else {
table.order([ 3, 'asc' ]).draw();
}
});

});
</script>

Expand Down
14 changes: 14 additions & 0 deletions qiita_pet/templates/sitebase.html
Expand Up @@ -54,6 +54,20 @@
<link rel="shortcut icon" href="{% raw qiita_config.portal_dir %}/static/img/favicon.ico">

<script>
/**
*
* Function to show the loading gif in a given div
*
* @param div_name string with the name of the div to populate with the loading gif
*
* This function replaces the content of the given div with the
* gif to show that the section of page is loading
*
*/
function show_loading(div_name) {
$("#" + div_name).html("<img src='{% raw qiita_config.portal_dir %}/static/img/waiting.gif' style='display:block;margin-left: auto;margin-right: auto'/>");
}

function overlay_check() {
// from http://stackoverflow.com/q/5916900/379593
var ua=navigator.userAgent,tem,M=ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
Expand Down
42 changes: 24 additions & 18 deletions qiita_pet/test/test_qiita_redbiom.py
Expand Up @@ -123,41 +123,47 @@ def test_post_errors(self):


OBSERVATION = [
{'artifact_id': 4, 'study_id': 1, 'version': '1.9.1',
'command': 'Pick closed-reference OTUs', 'samples': [
{'artifact_id': 4, 'study_id': 1,
'command': 'Pick closed-reference OTUs - QIIME v1.9.1 @ Defaults',
'samples': [
'1.SKD1.640179', '1.SKD2.640178', '1.SKD3.640198', '1.SKD4.640185',
'1.SKD5.640186', '1.SKD6.640190', '1.SKD7.640191', '1.SKD8.640184',
'1.SKD9.640182'],
'study_title': 'Identification of the Microbiomes for Cannabis Soils',
'aname': 'BIOM', 'software': 'QIIME'},
{'artifact_id': 5, 'study_id': 1, 'version': '1.9.1',
'command': 'Pick closed-reference OTUs', 'samples': [
'aname': 'BIOM'},
{'artifact_id': 5, 'study_id': 1,
'command': 'Pick closed-reference OTUs - QIIME v1.9.1 @ Defaults',
'samples': [
'1.SKD1.640179', '1.SKD2.640178', '1.SKD3.640198', '1.SKD4.640185',
'1.SKD5.640186', '1.SKD6.640190', '1.SKD7.640191', '1.SKD8.640184',
'1.SKD9.640182'],
'study_title': 'Identification of the Microbiomes for Cannabis Soils',
'aname': 'BIOM', 'software': 'QIIME'},
{'artifact_id': 6, 'study_id': 1, 'version': u'1.9.1',
'command': 'Pick closed-reference OTUs', 'samples': [
'aname': 'BIOM'},
{'artifact_id': 6, 'study_id': 1,
'command': 'Pick closed-reference OTUs - QIIME v1.9.1 @ Defaults',
'samples': [
'1.SKD1.640179', '1.SKD2.640178', '1.SKD3.640198', '1.SKD4.640185',
'1.SKD5.640186', '1.SKD6.640190', '1.SKD7.640191', '1.SKD8.640184',
'1.SKD9.640182'],
'study_title': 'Identification of the Microbiomes for Cannabis Soils',
'aname': 'BIOM', 'software': 'QIIME'}]
'aname': 'BIOM'}]

SEQUENCE = [
{'artifact_id': 4, 'study_id': 1, 'version': '1.9.1',
'command': 'Pick closed-reference OTUs', 'samples': ['1.SKM3.640197'],
{'artifact_id': 4, 'study_id': 1,
'command': 'Pick closed-reference OTUs - QIIME v1.9.1 @ Defaults',
'samples': ['1.SKM3.640197'],
'study_title': 'Identification of the Microbiomes for Cannabis Soils',
'aname': 'BIOM', 'software': 'QIIME'},
{'artifact_id': 5, 'study_id': 1, 'version': '1.9.1',
'command': 'Pick closed-reference OTUs', 'samples': ['1.SKM3.640197'],
'aname': 'BIOM'},
{'artifact_id': 5, 'study_id': 1,
'command': 'Pick closed-reference OTUs - QIIME v1.9.1 @ Defaults',
'samples': ['1.SKM3.640197'],
'study_title': 'Identification of the Microbiomes for Cannabis Soils',
'aname': 'BIOM', 'software': 'QIIME'},
{'artifact_id': 6, 'study_id': 1, 'version': u'1.9.1',
'command': 'Pick closed-reference OTUs', 'samples': ['1.SKM3.640197'],
'aname': 'BIOM'},
{'artifact_id': 6, 'study_id': 1,
'command': 'Pick closed-reference OTUs - QIIME v1.9.1 @ Defaults',
'samples': ['1.SKM3.640197'],
'study_title': 'Identification of the Microbiomes for Cannabis Soils',
'aname': 'BIOM', 'software': 'QIIME'}]
'aname': 'BIOM'}]

CATEGORIES = [
{'artifact_id': None, 'study_id': 1, 'version': None,
Expand Down

0 comments on commit af7be28

Please sign in to comment.