From a7061c671c30d589cb72992cfe5e2bcbfe59d25d Mon Sep 17 00:00:00 2001 From: "reynoldltan@gmail.com" Date: Sun, 1 May 2022 17:25:58 -0600 Subject: [PATCH] More download options @TODO: test all components --- .../ncit__raw_data_formatter.inc | 92 +------------------ includes/rawpheno.tripaldownload.inc | 52 +++++++---- theme/js/rawpheno.germplasmfield.script.js | 44 +++++++-- 3 files changed, 71 insertions(+), 117 deletions(-) diff --git a/includes/TripalFields/ncit__raw_data/ncit__raw_data_formatter.inc b/includes/TripalFields/ncit__raw_data/ncit__raw_data_formatter.inc index 89b52d1..7e57853 100644 --- a/includes/TripalFields/ncit__raw_data/ncit__raw_data_formatter.inc +++ b/includes/TripalFields/ncit__raw_data/ncit__raw_data_formatter.inc @@ -161,94 +161,4 @@ class ncit__raw_data_formatter extends TripalFieldFormatter { return theme('select', array('element' => array('#attributes' => $attributes, '#options' => $options))); } -} - - - - - - - - - - - - - -/* - // Array to hold select option for select field by location + experiment. - $option_loc_exp = array(); - // Array to hold select option for select field by experiment. - $option_exp = array(); - - // Create select by location + experiment at the same time - // prepare values for the other select (by experiment). - foreach($experiment as $exp_loc) { - $experiment_name = $exp_loc['phenotype_customfield_terms:name']; - $experiment_id = $exp_loc['phenotype_customfield_terms:id']; - $experiment_loc = $exp_loc['phenotype_customfield_terms:location']; - - $disabled = (in_array($experiment_id, $user_experiment)) ? '' : 'disabled'; - - // Select by Location + Experiment. - $select_value = $trait['id'] . '#' . $experiment_id . '#' . $germplasm . '#' . $experiment_loc; - $option_loc_exp[] = ''; - } - - // Construct select by experiment. - unset($experiment_name, $experiment_id, $experiment_loc); - // All unique experiments this trait was measured. - $experiments = array_column($experiment, 'phenotype_customfield_terms:name', 'phenotype_customfield_terms:id'); - - foreach($experiments as $experiment_id => $experiment_name) { - $experiment_loc = array_column($experiment, 'phenotype_customfield_terms:location', $experiment_id); - - $disabled = (in_array($experiment_id, $user_experiment)) ? '' : 'disabled'; - - $select_value = $experiment_id . '#' . implode('+', array_values($experiment_loc)); - $option_exp[] = ''; - } - - - - - - - - - - - // SELECT BY LOCATION + EXPERIMENT FIELD. - $select_location_experiment = ' - - '; - - - - - - - - - - - - - - - - - - - - // SELECT BY EXPERIMENT FIELD. - $select_experiment = ' - - '; - - return sprintf($select_location_experiment, implode('', $option_loc_exp)) . sprintf($select_experiment, implode('', $option_exp)); - */ \ No newline at end of file +} \ No newline at end of file diff --git a/includes/rawpheno.tripaldownload.inc b/includes/rawpheno.tripaldownload.inc index afa4fc7..a3b3c33 100644 --- a/includes/rawpheno.tripaldownload.inc +++ b/includes/rawpheno.tripaldownload.inc @@ -213,11 +213,7 @@ function rawpheno_trpdownload_generate_file($variables, $job_id = NULL) { // Projects: $tmp = trim(str_replace('p=', '', $project)); - $project = explode(',', $tmp); - - if ($project[0]) { - $project_name = rawpheno_function_getproject($project[0]); - } + $project = explode('+', $tmp); // Locations: $location = trim(str_replace('l=', '', $location)); @@ -245,6 +241,9 @@ function rawpheno_trpdownload_generate_file($variables, $job_id = NULL) { } else { $traits = explode(',', $traits); + // Backup trait selection to account for trait w/o plant id + // but is a trait in experiment trait list. + $rd_field_trait = $traits; } // Rversion @@ -266,8 +265,10 @@ function rawpheno_trpdownload_generate_file($variables, $job_id = NULL) { } // Germplasm/Stock id - request from germplasm field. - $stock_id = trim(str_replace('g=', '', $germplasm)); - $limit_stock = ''; + if ($germplasm) { + $stock_id = trim(str_replace('g=', '', $germplasm)); + $limit_stock = ''; + } // Sub-query to select plant_id given a location and project. // NOTE: leading and trailing spaces are required. @@ -287,7 +288,7 @@ function rawpheno_trpdownload_generate_file($variables, $job_id = NULL) { $arr_q_string[':stock_id'] = $stock_id; // Add experiment to header if request comes from field. - $header['D0'] = 'Experiment'; + $header['E0'] = 'Experiment'; } $sub_sql = sprintf($sub_sql, $limit_stock); @@ -320,11 +321,26 @@ function rawpheno_trpdownload_generate_file($variables, $job_id = NULL) { // When no equivalent R version is available, load the definition instead. $def = ($rfreindly == null) ? $r->name : $rfreindly; } - + // Column headers array. $header[ $r->id ] = $def; } + // When request is from field, it has additional filter to phenotypes + // by comparing stock id + plant id. In some case when a trait has no values, + // the final result will exclude this trait inspite this trait being part + // of trait set of the experiment, since it has no plant_id (stock_id) to compare. + + // Include the trait and the subsequent code will assign the value NA. This to show + // that trait is part of the experiment. + // NOTE: rversion is always off when request comes from field. + foreach($rd_field_trait as $exp_trait) { + if (!in_array($exp_trait, array_keys($header))) { + $t = chado_get_cvterm(array('cvterm_id' => $exp_trait)); + $header[ 'C' . $t->cvterm_id ] = $t->name; + } + } + // Sort array by key. ksort($header); @@ -334,8 +350,17 @@ function rawpheno_trpdownload_generate_file($variables, $job_id = NULL) { // row is Name - containing the stock name. // The result will be sorted into standard order: plot,entry,name,rep,location,traits..... - // Thus first we select the name. Note that the tid is 0 because this doesn't have a cvterm (ie: not a trait). - $sql = "SELECT t2.plant_id AS id, '0' AS tid, 'Name' AS def, t1.name AS value, 'A' AS grp + // Thus first we select experiment and name. Note that the tid is 0 because this doesn't have a cvterm (ie: not a trait). + $sql = "SELECT t1.plant_id AS id, 0 AS tid, 'e' AS def, t2.name AS value, 'E' AS grp + FROM {pheno_plant_project} AS t1 + INNER JOIN {chado.project} AS t2 ON t1.project_id = t2.project_id + WHERE plant_id IN" . $sub_sql + + // Name - stock or germplasm. + + . "UNION + + SELECT t2.plant_id AS id, '0' AS tid, 'Name' AS def, t1.name AS value, 'A' AS grp FROM {chado.stock} AS t1 INNER JOIN {pheno_plant} AS t2 USING(stock_id) WHERE t2.plant_id IN" . $sub_sql @@ -381,11 +406,6 @@ function rawpheno_trpdownload_generate_file($variables, $job_id = NULL) { $rows = array(); foreach($results as $r) { $rows[ $r->id ][ $r->grp . $r->tid ] = $r->value; - - if ($stock_id != '' && (int) $stock_id > 0) { - // Add experiment name when request comes from field. - $rows[ $r->id ][ 'D0' ] = $project_name; - } } // Total lines; diff --git a/theme/js/rawpheno.germplasmfield.script.js b/theme/js/rawpheno.germplasmfield.script.js index 48009eb..e3c9146 100644 --- a/theme/js/rawpheno.germplasmfield.script.js +++ b/theme/js/rawpheno.germplasmfield.script.js @@ -57,7 +57,7 @@ if (imgOpacity == 1) { window.open( - Drupal.settings.rawpheno.exportLink + '?code=' + btoa(downloadLink), + Drupal.settings.rawpheno.exportLink + '?code=' + encodeURIComponent(btoa(downloadLink)), '_blank' ); } @@ -67,13 +67,37 @@ $('#rawphenotypes-germplasm-field-table td:first-child img').click(function(e) { var imgId = e.target.id; var i = imgId.split('-'); - // rawphenotypes-germplasm-field-filterby-%s-img - var downloadLink = 't=' + i[4] + '&p=All&l=All&g=' + Drupal.settings.rawpheno.germ; - window.open( - Drupal.settings.rawpheno.exportLink + '?code=' + btoa(downloadLink), - '_blank' - ); + // Trait data. + var key = Object.keys(Drupal.settings.rawpheno.germRawdata).filter(function(k) { + return k.indexOf(i[4]) !== -1; + }); + + // All experiment user has access to in a trait. + var row = Drupal.settings.rawpheno.germRawdata[ key ] + .filter(function(e) { return e['phenotype_customfield_terms:user_experiment'] == 1 }); + + // Experiments. + var exp = row.map(e => e['phenotype_customfield_terms:id']) + .filter((value, index, self) => self.indexOf(value) === index); + + // Locations. + var loc = row.map(e => e['phenotype_customfield_terms:location']) + .filter((value, index, self) => self.indexOf(value) === index); + + if (exp.length > 0) { + // Export all. + var downloadLink = 't=' + i[4] + '&p=' + exp.join('%2B') + '&l=' + loc.join('%2B') + '&g=' + Drupal.settings.rawpheno.germ; + + window.open( + Drupal.settings.rawpheno.exportLink + '?code=' + encodeURIComponent(btoa(downloadLink)), + '_blank' + ); + } + else { + // None of the experiments in a trait is accessible to current user. + alert('You do not have access to experiments in this trait. Please contact us to request permission.'); + } }); // Listen to controls search, expand and select by. @@ -194,6 +218,7 @@ /** * Remove option element from a select field. + * * @param select * Object, reference to select element. */ @@ -231,7 +256,7 @@ }); }); } - else if(set == 'e') { + else if (set == 'e') { // EXPERIMENT: $.each(dataset.germRawdata, function(index, value){ var expCache = new Array(); @@ -247,7 +272,7 @@ var disabled = v['phenotype_customfield_terms:user_experiment'] == 1 ? '' : 'disabled'; element.append($('