Created
January 13, 2017 01:01
-
-
Save Viktoru/eb487b6f1c4205ef2a08e51e72f873d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package = Tripal Download API Test | |
name = Test Download API | |
description = Provides an api for creating download pages. Uses tripal Jobs to generate files and provides a progress bar to users. | |
version = 7.x-0.x | |
core = 7.x | |
dependencies[] = tripal_core | |
dependencies[] = trpdownload_api |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Provides some examples showing how module developers could use the | |
* Tripal Download API. | |
*/ | |
/** | |
* Implements hook_register_trpdownload_type(). | |
*/ | |
function tripal_test_register_trpdownload_type() { | |
$types = array(); | |
$types['organism_csv'] = array( | |
'type_name' => 'Organism CSV', | |
'format' => 'Comma-separated Values', | |
'functions' => array( | |
'generate_file' => 'tripal_test_organism_csv_generate_file', | |
), | |
); | |
return $types; | |
} | |
/** | |
* Implements hook_menu(). | |
*/ | |
function tripal_test_menu() { | |
$items = array(); | |
$items['chado/organism/csv'] = array( | |
'title' => 'Download Features: CSV', | |
'page callback' => 'trpdownload_download_page', | |
'page arguments' => array('organism_csv', 3), | |
'access arguments' => array('access content'), | |
'type' => MENU_CALLBACK, | |
); | |
return $items; | |
} | |
// /** | |
// * Generates a file listing feature in CSV. | |
// * | |
// * @param $variables | |
// * An associative array of parameters including: | |
// * - q: all the query paramters. | |
// * - site_safe_name: a sanitized version of your site name for use in variables & filenames. | |
// * - type_info: an array of info for the download type. | |
// * - suffix: the file format suffix. | |
// * - filename: the filename of the file to generate not including path. | |
// * - fullpath: the full path and filename of the file to generate. | |
// * - format_name: a human-readable description of the format. | |
// * @param $job_id | |
// * The ID of the tripal job executing this function ;-). | |
// */ | |
// | |
function tripal_test_organism_csv_generate_file($variables, $job_id = NULL) { | |
// Create the file and ready it for writting to. | |
$filepath = variable_get('trpdownload_fullpath', '') . $variables['filename']; | |
drush_print("File: " . $filepath); | |
$FILE = fopen($filepath, 'w') or die ('Uable to create file to write to'); | |
// Add header to the file. | |
fputcsv($FILE, array( | |
'Genus', | |
'Species', | |
'Scientific Name', | |
'Common Name', | |
'Abbreviation' | |
)); | |
// Determine which where criteria to include based on what is in query parameters. | |
$where = array('true'); | |
$where_args = array(); | |
// feature.name | |
if (isset($variables['q']['common_name']) AND !empty($variables['q']['common_name'])) { | |
$where[] = "organism.common_name ~ :common_name"; | |
$where_args[':common_name'] = $variables['q']['common_name']; | |
} | |
// feature.uniquename | |
if (isset($variables['q']['genus']) AND !empty($variables['q']['genus'])) { | |
$where[] = "organism.genus ~ :genus"; | |
$where_args[':uniquename'] = $variables['q']['uniquename']; | |
} | |
// feature.type_id | |
if (isset($variables['q']['species']) AND !empty($variables['q']['species'])) { | |
$where[] = "organism.species = :species"; | |
$where_args[':species'] = $variables['q']['species']; | |
} | |
// // organism.common_name | |
// if (isset($variables['q']['organism']) | |
// AND !empty($variables['q']['organism']) | |
// AND $variables['q']['organism']!= 'All') { | |
// $where[] = "organism.common_name = :organism"; | |
// $where_args[':organism'] = $variables['q']['organism']; | |
// } | |
// | |
// Query copied from the views interface with where arguments determined above. | |
$query = "SELECT organism.genus AS organism_genus, organism.species AS organism_species, organism.common_name AS organism_common_name, organism.abbreviation AS organism_abbreviation FROM | |
{organism} organism | |
LEFT JOIN [chado_organism] chado_organism ON organism.organism_id = chado_organism.organism_id | |
LEFT JOIN [node] node_chado_organism ON chado_organism.nid = node_chado_organism.nid | |
LEFT JOIN [node] node ON chado_organism.nid = node.nid | |
WHERE " . implode(' AND ', $where) ." | |
ORDER BY organism_genus ASC, organism_species ASC OFFSET 0"; | |
// SELECT | |
// feature.uniquename AS feature_uniquename, | |
// feature.name AS feature_name, | |
// cvterm.name AS cvterm_name, | |
// organism.common_name AS organism_common_name, | |
// feature.seqlen AS feature_seqlen, | |
// feature.is_obsolete AS feature_is_obsolete | |
// FROM | |
// {feature} feature | |
// LEFT JOIN [chado_feature] chado_feature ON feature.feature_id = chado_feature.feature_id | |
// LEFT JOIN [node] node_chado_feature ON chado_feature.nid = node_chado_feature.nid | |
// LEFT JOIN {organism} organism ON feature.organism_id = organism.organism_id | |
// LEFT JOIN {cvterm} cvterm ON feature.type_id = cvterm.cvterm_id | |
// WHERE " . implode(' AND ', $where) ." | |
// ORDER BY organism.common_name ASC, cvterm.name ASC, feature.name ASC"; | |
// Determine the total number of lines resulting from the query | |
// for tracking progress. | |
$count_query = preg_replace('/SELECT.*FROM/s', 'SELECT count(*) as num_lines FROM', $query); | |
$count_query = preg_replace('/ORDER BY .*$/', '', $count_query); | |
$total_lines = chado_query($count_query, $where_args)->fetchField(); | |
drush_print('Total Lines: '.$total_lines); | |
// Execute the original query to get the results. | |
$resource = chado_query($query, $where_args); | |
// | |
// For each db result write a CSV line to the file. | |
$cur_line = 0; | |
foreach ($resource as $row) { | |
// Output the progress. | |
$cur_line++; | |
$percent = $cur_line/$total_lines * 100; | |
if ($percent%5 == 0) { | |
drush_print(round($percent,2).'% Complete.'); | |
db_query('UPDATE {tripal_jobs} SET progress=:percent WHERE job_id=:id', | |
array(':percent' => round($percent), ':id' => $job_id)); | |
} | |
// Don't forget to write the line to the file ;-). | |
fputcsv($FILE, (array)$row ); | |
} | |
// Finally, close the file. | |
fclose($FILE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment