diff --git a/qiita_pet/handlers/qiita_redbiom.py b/qiita_pet/handlers/qiita_redbiom.py index 74e18e1cc..eb5a56bd9 100644 --- a/qiita_pet/handlers/qiita_redbiom.py +++ b/qiita_pet/handlers/qiita_redbiom.py @@ -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 @@ -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 = """ diff --git a/qiita_pet/static/js/qiita.js b/qiita_pet/static/js/qiita.js index a2a20c6e7..04c9b199a 100644 --- a/qiita_pet/static/js/qiita.js +++ b/qiita_pet/static/js/qiita.js @@ -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(""); -} diff --git a/qiita_pet/templates/redbiom.html b/qiita_pet/templates/redbiom.html index bf352a7e8..d38f2c10d 100644 --- a/qiita_pet/templates/redbiom.html +++ b/qiita_pet/templates/redbiom.html @@ -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( + ''+group+'' + ); + 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 = ''; - {% else %} - var text = ''; + if ($("#search_on").val() != 'categories'){ + var text = ''; + } {% end %} return text; }, targets: [0]}, @@ -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]} @@ -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", "
"), "danger"); - } else { - if(data.message != ''){ - redbiom_info.html( - `
`) - } 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", "
"), "danger"); + } else { + if(data.message != ''){ + redbiom_info.html( + `
`) + } 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(); + } }); + }); diff --git a/qiita_pet/templates/sitebase.html b/qiita_pet/templates/sitebase.html index 9477b5379..22b7705b1 100644 --- a/qiita_pet/templates/sitebase.html +++ b/qiita_pet/templates/sitebase.html @@ -54,6 +54,20 @@