Skip to content

Commit

Permalink
Add more export option
Browse files Browse the repository at this point in the history
  • Loading branch information
reynoldtan committed May 2, 2022
1 parent a7061c6 commit 755be53
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ncit__raw_data_formatter extends TripalFieldFormatter {
sprintf($img, '', $theme_path . 'icon-download-all.jpg', 'Download all for this trait'),
'Trait',
'Filter by:',
sprintf($img, '', $theme_path . 'icon-download.jpg', 'Download Location + Experiment')
sprintf($img, '', $theme_path . 'icon-download.jpg', 'Download')
);

// 2 select fields are required:
Expand Down
15 changes: 12 additions & 3 deletions includes/rawpheno.download.form.inc
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,29 @@ function rawpheno_download($form, &$form_state) {

if ($vars) {
parse_str($vars, $query_vars);
$param_experiment = (int) $query_vars['p'];
$param_experiment = $query_vars['p'];

global $user;
$user_experiment = rawpheno_function_user_project($user->uid);
$user_experiment = array_keys($user_experiment);
$allowed_experiment = array();

if (in_array($param_experiment, $user_experiment)) {
$experiments = explode('+', $param_experiment);
foreach($experiments as $exp) {
if (in_array($exp, $user_experiment)) {
$allowed_experiment[] = $exp;
}
}

if (count($allowed_experiment) > 0) {
$param_location = $query_vars['l'];
// Expected single value only for trait and germplasm.
$param_trait = (int) $query_vars['t'];
$param_stock = (int) $query_vars['g'];

if ($param_experiment > 0 && $param_location && $param_trait > 0) {
// Create query string.
$query_string = 'p=' . $param_experiment . '&l=' . $param_location . '&t=' . $param_trait . '&r=0&e=0&file=0&g=' . $param_stock;
$query_string = 'p=' . implode('+', $allowed_experiment) . '&l=' . $param_location . '&t=' . $param_trait . '&r=0&e=0&file=0&g=' . $param_stock;
drupal_goto('/phenotypes/raw/csv', array('query' => array('code' => base64_encode($query_string))));
}
}
Expand Down
36 changes: 23 additions & 13 deletions includes/rawpheno.tripaldownload.inc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function rawpheno_trpdownload_summarize_download($vars) {
list($project, $location, $traits, $r_version, $envdata, $envfile) = explode('&', $q);

$tmp = trim(str_replace('p=', '', $project));
$project = explode(',', $tmp);
$project = explode('+', $tmp);

$sql = "SELECT name FROM {project} WHERE project_id IN (:project)";
$args = array(':project' => $project);
Expand Down Expand Up @@ -210,11 +210,10 @@ function rawpheno_trpdownload_generate_file($variables, $job_id = NULL) {

$q = base64_decode($code);
list($project, $location, $traits, $r_version,,,$germplasm) = explode('&', $q);

// Projects:
$tmp = trim(str_replace('p=', '', $project));
$project = explode('+', $tmp);

// Locations:
$location = trim(str_replace('l=', '', $location));

Expand Down Expand Up @@ -283,11 +282,16 @@ function rawpheno_trpdownload_generate_file($variables, $job_id = NULL) {
// Add Name/Stock name column headers array.
$header = array('A0' => 'Name');

// Filter by germplasm.
if ($stock_id != '' && (int) $stock_id > 0) {
// Additional filter to limit datapoints to only those that match stock/germplasm.
$limit_stock = "AND plant_id IN (SELECT plant_id FROM pheno_plant WHERE stock_id = :stock_id)";
$arr_q_string[':stock_id'] = $stock_id;

// Add experiment to header if request comes from field.
// Experiment name is added to the result as export from multiple
// experiments is supported by the field.

// Query below is also updated to correspond to this header.
$header['E0'] = 'Experiment';
}

Expand Down Expand Up @@ -344,21 +348,26 @@ function rawpheno_trpdownload_generate_file($variables, $job_id = NULL) {
// Sort array by key.
ksort($header);

// Filter by germplasm.
$sql_experiment = '';
if ($stock_id != '' && (int) $stock_id > 0) {
// Include a query to mark datapoints at to which experiment to correspond
// to experiment header.
$sql_experiment = "
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 . "UNION ";
}

// Query to join data from different tables.
// Result: plant_id, trait_id, definition, data, and a grouping string
// The result is sorted by plant_id and the grouping string ensuring that the first
// 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 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
$sql = "%s
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)
Expand All @@ -383,7 +392,8 @@ function rawpheno_trpdownload_generate_file($variables, $job_id = NULL) {
// Lastly we order the results by plant_id and grouping string, and tid.
. "GROUP BY t1.plant_id, t1.type_id, t2.name
ORDER BY id, grp, tid ASC";


$sql = sprintf($sql, $sql_experiment);
$results = db_query($sql, $arr_q_string);

if ($results) {
Expand Down
1 change: 1 addition & 0 deletions theme/css/rawpheno.germplasmfield.style.css
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
position: relative;
padding: 0;
margin: 0;
min-width: 450px;
height: 238px;
overflow-y: scroll;
border: none;
Expand Down
14 changes: 7 additions & 7 deletions theme/js/rawpheno.germplasmfield.script.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
imgOpacity = '1';
var params = selectValue.split('#');
// Project id & location & trait.
downloadLink = 't=' + params[0] + '&p=' + params[1] + '&l=' + params[2] + '&g=' + params[3];
downloadLink = $.param({t:params[0], p:params[1], l:params[2], g:Drupal.settings.rawpheno.germ});
}

$('#' + selectId + '-img').css('opacity', imgOpacity);
Expand All @@ -57,7 +57,7 @@

if (imgOpacity == 1) {
window.open(
Drupal.settings.rawpheno.exportLink + '?code=' + encodeURIComponent(btoa(downloadLink)),
Drupal.settings.rawpheno.exportLink + '?code=' + btoa(downloadLink),
'_blank'
);
}
Expand All @@ -78,7 +78,7 @@
.filter(function(e) { return e['phenotype_customfield_terms:user_experiment'] == 1 });

// Experiments.
var exp = row.map(e => e['phenotype_customfield_terms:id'])
var exp = row.map(e => e['phenotype_customfield_terms:id'].toString())
.filter((value, index, self) => self.indexOf(value) === index);

// Locations.
Expand All @@ -87,10 +87,10 @@

if (exp.length > 0) {
// Export all.
var downloadLink = 't=' + i[4] + '&p=' + exp.join('%2B') + '&l=' + loc.join('%2B') + '&g=' + Drupal.settings.rawpheno.germ;
downloadLink = $.param({t:i[4], p:exp.join('+'), l:loc.join('+'), g:Drupal.settings.rawpheno.germ});

window.open(
Drupal.settings.rawpheno.exportLink + '?code=' + encodeURIComponent(btoa(downloadLink)),
Drupal.settings.rawpheno.exportLink + '?code=' + btoa(downloadLink),
'_blank'
);
}
Expand Down Expand Up @@ -249,7 +249,7 @@
$.each(value, function(i, v) {
var disabled = v['phenotype_customfield_terms:user_experiment'] == 1 ? '' : 'disabled';
element.append($('<option>', {
value: trait[1] + '#' + v['phenotype_customfield_terms:id'] + '#' + v['phenotype_customfield_terms:location'] + '#' + germplasm,
value: trait[1] + '#' + v['phenotype_customfield_terms:id'] + '#' + v['phenotype_customfield_terms:location'],
text : v['phenotype_customfield_terms:location'].toUpperCase() + '/' + v['phenotype_customfield_terms:name'],
disabled: disabled
}));
Expand All @@ -272,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('%2B') + '#' + germplasm,
value: trait[1] + '#' + v['phenotype_customfield_terms:id'] + '#' + ql.join('+'),
text : v['phenotype_customfield_terms:name'] + ' (' + l.length + ' Locations)',
title: ql.join(' + '),
disabled: disabled
Expand Down
2 changes: 1 addition & 1 deletion theme/rawpheno_germplasm_field.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</div>

<div id="rawphenotypes-germplasm-controls">
<span><input id="rawphenotypes-germplasm-controls-expand" type="checkbox"> Expand Table | <a id="rawphenotypes-germplasm-controls-search" href="#">Search</a></span>
<span><input id="rawphenotypes-germplasm-controls-expand" type="checkbox"> Expand Table | <a id="rawphenotypes-germplasm-controls-search" href="#">Search Trait</a></span>
<span id="rawphenotypes-germplasm-controls-selectby">Filter by: <span>Location + Experiment</span> | <a href="#">Experiment</a></span>

<div id="rawphenotypes-germplasm-controls-search-window">
Expand Down

0 comments on commit 755be53

Please sign in to comment.