Skip to content

Commit

Permalink
More download options @todo: test all components
Browse files Browse the repository at this point in the history
  • Loading branch information
reynoldtan committed May 1, 2022
1 parent 3ef128b commit a7061c6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = '<option value ="' . base64_encode($select_value) . '" ' . $disabled . '>' . strtoupper($experiment_loc) . '/' . $experiment_name . '</option>';
}
// 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[] = '<option value ="' . base64_encode($select_value) . '" ' . $disabled . '>' . $experiment_name . ' (' . count($experiment_loc[ $experiment_id ]). ' Locations)' . '</option>';
}
// SELECT BY LOCATION + EXPERIMENT FIELD.
$select_location_experiment = '
<select class="form-select rawphenotypes-germplasm-field-selectby-le" id="rawphenotypes-germplasm-field-selectby-le-' . $trait['id'] . '">
<option value="0" selected>Select (' . count($experiment) . ' Locations, ' . count($experiments) . ' Experiments)</option> %s
</select>
';
// SELECT BY EXPERIMENT FIELD.
$select_experiment = '
<select class="form-select rawphenotypes-germplasm-field-selectby-e" id="rawphenotypes-germplasm-field-selectby-e-' . $trait['id'] . '">
<option value="0" selected>Select (' . count($experiments) . ' Experiments)</option> %s
</select>
';
return sprintf($select_location_experiment, implode('', $option_loc_exp)) . sprintf($select_experiment, implode('', $option_exp));
*/
}
52 changes: 36 additions & 16 deletions includes/rawpheno.tripaldownload.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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

Expand Down Expand Up @@ -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;
Expand Down
44 changes: 34 additions & 10 deletions theme/js/rawpheno.germplasmfield.script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}
Expand All @@ -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.
Expand Down Expand Up @@ -194,6 +218,7 @@

/**
* Remove option element from a select field.
*
* @param select
* Object, reference to select element.
*/
Expand Down Expand Up @@ -231,7 +256,7 @@
});
});
}
else if(set == 'e') {
else if (set == 'e') {
// EXPERIMENT:
$.each(dataset.germRawdata, function(index, value){
var expCache = new Array();
Expand All @@ -247,7 +272,7 @@

var disabled = v['phenotype_customfield_terms:user_experiment'] == 1 ? '' : 'disabled';
element.append($('<option>', {
value: trait[1] + '#' + v['phenotype_customfield_terms:id'] + '#' + ql.join('+') + '#' + germplasm,
value: trait[1] + '#' + v['phenotype_customfield_terms:id'] + '#' + ql.join('%2B') + '#' + germplasm,
text : v['phenotype_customfield_terms:name'] + ' (' + l.length + ' Locations)',
title: ql.join(' + '),
disabled: disabled
Expand All @@ -257,5 +282,4 @@
});
}
}

}};}(jQuery));

0 comments on commit a7061c6

Please sign in to comment.