Last active
January 12, 2017 23:03
-
-
Save almasaeed2010/2f7874c15ecc2e5872678ecfa26bf7bb to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Add a fasta line to a file | |
* | |
* @param $FILE file handle | |
* @param $uniquename the unique identifier | |
* @param $data any data on the first line after the unique identifier | |
* @param $sequence the sequence to print under the first line | |
*/ | |
function fputfasta($FILE, $uniquename, $data, $sequence) { | |
fwrite($FILE, ">$uniquename $data\n"); | |
fwrite($FILE, "$sequence\n\n"); | |
} | |
/** | |
* Generate a fasta file from chado | |
* | |
* @param $variables | |
* @param null $job_id | |
*/ | |
function trpdownload_formats_feature_fasta_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'); | |
// Determine which where criteria to include based on what is in query parameters. | |
$where = array(); | |
$where_args = array(); | |
// feature.name | |
if (isset($variables['q']['name']) AND !empty($variables['q']['name'])) { | |
$where[] = "feature.name ~ :name"; | |
$where_args[':name'] = $variables['q']['name']; | |
} | |
// feature.uniquename | |
if (isset($variables['q']['uniquename']) AND !empty($variables['q']['uniquename'])) { | |
$where[] = "feature.uniquename ~ :uniquename"; | |
$where_args[':uniquename'] = $variables['q']['uniquename']; | |
} | |
// feature.type_id | |
if (isset($variables['q']['type_id']) AND !empty($variables['q']['type_id'])) { | |
$where[] = "feature.type_id = :type_id"; | |
$where_args[':type_id'] = $variables['q']['type_id']; | |
} | |
// 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 | |
node_chado_feature.nid AS node_chado_feature_nid, | |
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, | |
feature.residues AS feature_residues | |
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 {cvterm} cvterm ON feature.type_id = cvterm.cvterm_id | |
LEFT JOIN {organism} organism ON feature.organism_id = organism.organism_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 ;-). | |
fputfasta($FILE, $row->feature_uniquename, ' ', $row->feature_residues); | |
} | |
// Finally, close the file. | |
fclose($FILE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment