123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853 |
- <?php
- function tripal_validate_phylotree($val_type, &$options, &$errors, &$warnings) {
- if ($val_type != 'insert' and $val_type != 'update') {
- tripal_report_error('tripal_phylogeny', TRIPAL_ERROR, "The $val_type argument must be either 'update or 'insert'.");
- }
-
- if ($val_type == 'insert') {
-
- if (!array_key_exists('match', $options)) {
- $options['match'] = 'name';
- }
-
- if (!array_key_exists('name_re', $options)) {
- $options['name_re'] = '^(.*)$';
- }
-
-
-
-
- if (!array_key_exists('dbxref', $options)) {
- $options['dbxref'] = "null:local:null";
- }
- }
-
- if ($val_type == 'insert') {
- if (!array_key_exists('name', $options)) {
- $errors['name'] = t('Please provide the name of the tree.');
- return FALSE;
- }
- if (!array_key_exists('description', $options)) {
- $errors['description'] = t('Please provide a description for this tree.');
- return FALSE;
- }
- if (!array_key_exists('analysis', $options) and !array_key_exists('analysis_id', $options)) {
- $errors['analysis'] = t('Please provide an analysis or analysis_id for this tree.');
- return FALSE;
- }
- if (!array_key_exists('tree_file', $options)) {
- $errors['tree_file'] = t('Please provide either the full path to the tree_file or a Drupal managed file ID number.');
- return FALSE;
- }
- if (!array_key_exists('format', $options) or !$options['format']) {
- $errors['format'] = t('Please provide a file format for the tree file.');
- return FALSE;
- }
-
- if ($options['format'] != 'newick' and $options['format'] != 'taxonomy') {
- $errors['format'] = t('The file format is not supported. Currently only the "newick" file format is supported.');
- return FALSE;
- }
- }
- else {
-
- if (!array_key_exists('phylotree_id', $options)) {
- $errors['phylotree_id'] = t('Please provide the ID for the tree.');
- return FALSE;
- }
- $exists = chado_select_record('phylotree', array('phylotree_id'),
- array('phylotree_id' => $options['phylotree_id']), array('has_record' => 1));
- if (!$exists) {
- $errors['phylotree_id'] = t('The phylotree_id does not exist.');
- return FALSE;
- }
- }
-
- if (array_key_exists('tree_file', $options) and $options['tree_file']) {
-
- if (!is_numeric($options['tree_file'])) {
- if (!file_exists($options['tree_file'])) {
- $errors['tree_file'] = t('The file provided does not exists.');
- return FALSE;
- }
- }
-
- if (!array_key_exists('format', $options) or
- ($options['format'] != 'newick' and $options['format'] != 'taxonomy')) {
- $errors['format'] = t('Please provide a supported file format. Currently only the "newick" file format is supported.');
- return FALSE;
- }
-
- if (!array_key_exists('leaf_type', $options) or !$options['leaf_type']) {
- $options['leaf_type'] = 'polypeptide';
- }
- }
-
- $analysis = NULL;
- if (array_key_exists('analysis_id', $options) and $options['analysis_id']) {
- $analysis = chado_select_record('analysis', array('analysis_id'), array('analysis_id' => $options['analysis_id']));
- if (!$analysis) {
- $errors['analysis_id'] = t('The analysis name provided does not exist.');
- return FALSE;
- }
- $options['analysis_id'] = $analysis[0]->analysis_id;
- }
- if (array_key_exists('analysis', $options) and $options['analysis']) {
- $analysis = chado_select_record('analysis', array('analysis_id'), array('name' => $options['analysis']));
- if (!$analysis) {
- $errors['analysis'] = t('The analysis ID provided does not exist.');
- return FALSE;
- }
- $options['analysis_id'] = $analysis[0]->analysis_id;
- }
-
- $type = NULL;
- if (array_key_exists('leaf_type', $options) and $options['leaf_type']) {
- if ($options['leaf_type'] == 'taxonomy') {
- $values = array(
- 'cv_id' => array(
- 'name' => 'tripal_phylogeny'
- ),
- 'name' => 'taxonomy'
- );
- $type = chado_select_record('cvterm', array('cvterm_id'), $values);
- }
- else {
- $values = array(
- 'cv_id' => array(
- 'name' => 'sequence'
- ),
- 'name' => $options['leaf_type']
- );
- $type = chado_select_record('cvterm', array('cvterm_id'), $values);
- if (!$type) {
- $errors['leaf_type'] = t('The leaf_type provided is not a valid Sequence Ontology term: %term.');
- return FALSE;
- }
- }
- $options['type_id'] = $type[0]->cvterm_id;
- }
-
-
-
-
- if (array_key_exists('dbxref', $options)) {
- if (!$options['dbxref']) {
- $options['dbxref'] = 'null:local:null';
- }
- $matches = array();
- preg_match('/^(.*?):(.*)$/', $options['dbxref'], $matches);
- $db_name = $matches[1];
- $accession = $matches[2];
- $values = array(
- 'accession' => $accession,
- 'db_id' => array(
- 'name' => $db_name
- ),
- );
- $dbxref = chado_generate_var('dbxref', $values);
- if (!$dbxref) {
- $errors['dbxref'] = t('The dbxref provided does not exist in the database: %dbxref.', array('%dbxref' => $dbxref));
- return FALSE;
- }
- $options['dbxref_id'] = $dbxref->dbxref_id;
- }
-
- if (array_key_exists('name', $options) and $options['name']) {
- $sql = "
- SELECT *
- FROM {phylotree} P
- WHERE
- P.name = :name
- ";
- $args = array(':name' => $options['name']);
- if ($val_type == 'update') {
- $sql .= " AND NOT P.phylotree_id = :phylotree_id";
- $args[':phylotree_id'] = $options['phylotree_id'];
- }
- $result = chado_query($sql, $args)->fetchObject();
- if ($result) {
- $errors['name'] = t("The tree name is in use by another tree. Please provide a different unique name for this tree.");
- }
- }
- return TRUE;
- }
- function tripal_insert_phylotree(&$options, &$errors, &$warnings) {
- global $user;
- $options['name_re'] = trim($options['name_re']);
- $options['leaf_type'] = trim($options['leaf_type']);
- $options['name'] = trim($options['name']);
- $options['format'] = trim($options['format']);
- $options['tree_file'] = trim($options['tree_file']);
-
- $success = tripal_validate_phylotree('insert', $options, $errors, $warnings);
- if (!$success) {
- foreach ($errors as $field => $message) {
- tripal_report_error('tripal_phylogeny', TRIPAL_ERROR, $message);
- }
- return FALSE;
- }
-
- $values = array(
- 'analysis_id' => $options['analysis_id'],
- 'name' => $options['name'],
- 'dbxref_id' => $options['dbxref_id'],
- 'comment' => $options['description'],
- 'type_id' => $options['type_id'],
- );
- $phylotree = chado_insert_record('phylotree', $values);
- if (!$phylotree) {
- drupal_set_message(t('Unable to add phylotree.'), 'warning');
- tripal_report_error('tripal_phylogeny', TRIPAL_WARNING, 'Insert phylotree: Unable to create phylotree where values: %values',
- array('%values' => print_r($values, TRUE)));
- return FALSE;
- }
- $phylotree_id = $phylotree['phylotree_id'];
- $options['phylotree_id'] = $phylotree_id;
-
-
- if (is_numeric($options['tree_file'])) {
- $file = NULL;
- $file = file_load($options['tree_file']);
- $file->status = FILE_STATUS_PERMANENT;
- $file = file_save($file);
- file_usage_add($file, 'tripal_phylogeny', $options['format'], $phylotree_id);
- $real_file_path = drupal_realpath($file->uri);
- }
- else {
- $real_file_path = $options['tree_file'];
- }
-
-
- if (!array_key_exists('no_load', $options) or !$options['no_load']) {
- if (array_key_exists('load_now', $options) and $options['load_now']) {
- $args = array(
- 'phylotree_id' => $phylotree_id,
- 'leaf_type' => $options['leaf_type'],
- 'match' => $options['match'] ? 'uniquename' : 'name',
- 'name_re' => $options['name_re'],
- );
- tripal_phylogeny_import_tree_file($real_file_path, $options['format'], $args);
- }
- else {
- $args = array(
- $real_file_path,
- 'newick',
- array(
- 'phylotree_id' => $phylotree_id,
- 'leaf_type' => $options['leaf_type'],
- 'match' => $options['match'] ? 'uniquename' : 'name',
- 'name_re' => $options['name_re'],
- ),
- );
- if (tripal_add_job("Import Tree File: " . $file->filename, 'tripal_phylogeny',
- 'tripal_phylogeny_import_tree_file', $args, $user->uid)) {
- drupal_set_message(t('The tree visualizations will appear once the tree is fully imported.'));
- }
- }
- }
- return TRUE;
- }
- function tripal_update_phylotree($phylotree_id, &$options) {
- global $user;
-
- $errors = array();
- $warnings = array();
- $success = tripal_validate_phylotree('update', $options, $errors, $warnings);
- if (!$success) {
- foreach ($errors as $field => $message) {
- tripal_report_error('tripal_phylogeny', TRIPAL_ERROR, $message);
- }
- return FALSE;
- }
-
- $match = array(
- 'phylotree_id' => $phylotree_id,
- );
- if (array_key_exists('name', $options) and $options['name']) {
- $values['name'] = $options['name'];
- }
- if (array_key_exists('analysis_id', $options) and $options['analysis_id']) {
- $values['analysis_id'] = $options['analysis_id'];
- }
- if (array_key_exists('dbxref_id', $options) and $options['dbxref_id']) {
- $values['dbxref_id'] = $options['dbxref_id'];
- }
- if (array_key_exists('description', $options) and $options['description']) {
- $values['comment'] = $options['description'];
- }
- if (array_key_exists('type_id', $options) and $options['type_id']) {
- $values['type_id'] = $options['type_id'];
- }
- $phylotree = chado_update_record('phylotree', $match, $values, array('return_record' => TRUE));
- if (!$phylotree) {
- drupal_set_message(t('Unable to update phylotree.'), 'warning');
- tripal_report_error('tripal_phylogeny', TRIPAL_WARNING,
- 'Update phylotree: Unable to update phylotree where values: %values',
- array('%values' => print_r($values, TRUE))
- );
- }
-
- if (array_key_exists('tree_file', $options) and $options['tree_file']) {
-
- chado_delete_record('phylonode', array('phylotree_id' => $options['phylotree_id']));
-
- $sql = "
- SELECT FM.fid
- FROM {file_managed} FM
- INNER JOIN {file_usage} FU on FM.fid = FU.fid
- WHERE FU.id = :id and FU.module = 'tripal_phylogeny'
- ";
- $fid = db_query($sql, array(':id' => $options['phylotree_id']))->fetchField();
- if ($fid) {
- $file = file_load($fid);
- file_delete($file, TRUE);
- }
-
-
- if (is_numeric($options['tree_file'])) {
- $file = file_load($options['tree_file']);
- $file->status = FILE_STATUS_PERMANENT;
- $file = file_save($file);
- file_usage_add($file, 'tripal_phylogeny', 'newick', $options['phylotree_id']);
-
- $real_file_path = drupal_realpath($file->uri);
- }
- else {
- $real_file_path = $options['tree_file'];
- }
-
-
- if (array_key_exists('load_now', $options) and $options['load_now']) {
- $args = array(
- 'phylotree_id' => $options['phylotree_id'],
- 'leaf_type' => $options['leaf_type'],
- 'match' => $options['match'] ? 'uniquename' : 'name',
- 'name_re' => $options['name_re'],
- );
- tripal_phylogeny_import_tree_file($real_file_path, $options['format'], $args);
- }
- else {
- $args = array(
- $real_file_path,
- 'newick',
- array(
- 'phylotree_id' => $options['phylotree_id'],
- 'leaf_type' => $options['leaf_type'],
- 'match' => $options['match'] ? 'uniquename' : 'name',
- 'name_re' => $options['name_re'],
- ),
- );
- if (tripal_add_job("Import Tree File: " . $file->filename, 'tripal_phylogeny',
- 'tripal_phylogeny_import_tree_file', $args, $user->uid)) {
- drupal_set_message(t('The tree visualizations will appear once the tree is fully imported.'));
- }
- }
- }
- return TRUE;
- }
- function tripal_delete_phylotree($phylotree_id) {
-
-
- if (!$phylotree_id) {
- tripal_report_error('tripal_phylogeny', TRIPAL_ERROR,
- 'Please provide a phylotree_id to delete a tree.');
- return FALSE;
- }
-
- $values = array('phylotree_id' => $phylotree_id);
- return chado_delete_record('phylotree', $values);
- }
- function tripal_assign_phylogeny_tree_indices(&$tree, &$index = 1) {
-
-
-
-
- if (array_key_exists('name', $tree)) {
- $tree['left_index'] = $index += 100;
- if (array_key_exists('is_leaf', $tree)) {
- $tree['right_index'] = $index += 100;
- }
- }
- if (array_key_exists('branch_set', $tree)) {
- foreach ($tree['branch_set'] as $key => $node) {
- tripal_assign_phylogeny_tree_indices($tree['branch_set'][$key], $index);
- $tree['right_index'] = $index += 100;
- }
- }
- }
- function tripal_phylogeny_import_tree(&$tree, $phylotree, $options, $vocab = array(), $parent = NULL) {
-
-
- if (count($vocab) == 0) {
- $vocab = tripal_phylogeny_get_node_types_vocab();
- }
- if (is_array($tree) and array_key_exists('name', $tree)) {
- $values = array(
- 'phylotree_id' => $phylotree->phylotree_id,
- 'left_idx' => $tree['left_index'],
- 'right_idx' => $tree['right_index'],
- );
-
- if (!empty($tree['name']) and $tree['name'] != '') {
- $values['label'] = $tree['name'];
- }
- if (!empty($tree['length']) and $tree['length'] != '') {
- $values['distance'] = $tree['length'];
- }
-
- if ($tree['is_root']) {
- $values['type_id'] = $vocab['root']->cvterm_id;
- }
- else if ($tree['is_internal']) {
- $values['type_id'] = $vocab['internal']->cvterm_id;
- $values['parent_phylonode_id'] = $parent['phylonode_id'];
-
-
-
- }
- else if ($tree['is_leaf']) {
- $values['type_id'] = $vocab['leaf']->cvterm_id;
- $values['parent_phylonode_id'] = $parent['phylonode_id'];
-
-
- if (!empty($tree['name']) and $tree['name'] != '') {
- if (!$options['taxonomy']) {
-
-
- $matches = array();
- $sel_values = array();
- if ($options['match'] == "name") {
- $sel_values['name'] = $tree['name'];
- $re = $options['name_re'];
- if (preg_match("/$re/", $tree['name'], $matches)) {
- $sel_values['name'] = $matches[1];
- }
- }
- else {
- $sel_values['uniquename'] = $tree['name'];
- $re = $options['name_re'];
- if (preg_match("/$re/", $tree['name'], $matches)) {
- $sel_values['uniquename'] = $matches[1];
- }
- }
- $sel_values['type_id'] = array(
- 'name' => $options['leaf_type'],
- 'cv_id' => array(
- 'name' => 'sequence'
- ),
- );
- $sel_columns = array('feature_id');
- $feature = chado_select_record('feature', $sel_columns, $sel_values);
- if (count($feature) > 1) {
-
- }
- else if (count($feature) == 1) {
- $values['feature_id'] = $feature[0]->feature_id;
- }
- else {
-
- }
- }
- }
- }
-
- $phylonode = chado_insert_record('phylonode', $values);
- $tree['phylonode_id'] = $phylonode['phylonode_id'];
-
-
- if (array_key_exists('organism_id', $tree)) {
- $values = array(
- 'phylonode_id' => $tree['phylonode_id'],
- 'organism_id' => $tree['organism_id']
- );
- $pylonode_organism = chado_insert_record('phylonode_organism', $values);
- }
-
- if (array_key_exists('properties', $tree)) {
- foreach ($tree['properties'] as $type_id => $value) {
- $values = array(
- 'phylonode_id' => $tree['phylonode_id'],
- 'type_id' => $type_id,
- 'value' => $value,
- );
- $pylonode_organism = chado_insert_record('phylonodeprop', $values);
- }
- }
- }
- if (is_array($tree) and array_key_exists('branch_set', $tree)) {
- foreach ($tree['branch_set'] as $key => $node) {
- tripal_phylogeny_import_tree($tree['branch_set'][$key], $phylotree, $options, $vocab, $tree);
- }
- }
- }
- function tripal_phylogeny_get_node_types_vocab() {
-
- $values = array(
- 'name' => 'phylo_leaf',
- 'cv_id' => array(
- 'name' => 'tripal_phylogeny',
- ),
- );
- $leaf = chado_generate_var('cvterm', $values);
- if (!$leaf) {
- tripal_report_error('tripal_phylogeny', TRIPAL_ERROR,
- "Could not find the leaf vocabulary term: 'phylo_leaf'. It should " .
- "already be present as part of the tripal_phylogeny vocabulary.");
- return FALSE;
- }
- $values['name'] = 'phylo_interior';
- $internal = chado_generate_var('cvterm', $values);
- if (!$internal) {
- tripal_report_error('tripal_phylogeny', TRIPAL_ERROR,
- "Could not find the leaf vocabulary term: 'phylo_interior'. It should " .
- "already be present as part of the tripal_phylogeny vocabulary.");
- return FALSE;
- }
- $values['name'] = 'phylo_root';
- $root = chado_generate_var('cvterm', $values);
- if (!$root) {
- tripal_report_error('tripal_phylogeny', TRIPAL_ERROR,
- "Could not find the leaf vocabulary term: 'phylo_root'. It should " .
- "already be present as part of the tripal_phylogeny vocabulary.");
- return FALSE;
- }
- $vocab = array(
- 'leaf' => $leaf,
- 'internal' => $internal,
- 'root' => $root,
- );
- return $vocab;
- }
- function tripal_phylogeny_import_tree_file($file_name, $format, $options = array(), $job_id = NULL) {
-
- if (!array_key_exists('leaf_type', $options)) {
- $options['leaf_type'] = 'polypeptide';
- }
- if (!array_key_exists('match', $options)) {
- $options['match'] = 'name';
- }
- if (!array_key_exists('name_re', $options)) {
- $options['name_re'] = '^(.*)$';
- }
- $options['name_re'] = trim($options['name_re']);
-
-
- if (!array_key_exists('phylotree_id', $options)) {
- if (!array_key_exists('name', $options)) {
- tripal_report_error('tripal_phylogeny', TRIPAL_ERROR,
- 'The phylotree_id is required for importing the tree.');
- return FALSE;
- }
- }
-
- $values = array('phylotree_id' => $options['phylotree_id']);
- $phylotree = chado_generate_var('phylotree', $values);
- if (!$phylotree) {
- tripal_report_error('tripal_phylogeny', TRIPAL_ERROR,
- 'Could not find the phylotree using the ID provided: %phylotree_id.',
- array('%phylotree_id' => $options['phylotree_id']));
- return FALSE;
- }
- $transaction = db_transaction();
- print "\nNOTE: Loading of this tree file is performed using a database transaction. \n" .
- "If the load fails or is terminated prematurely then the entire set of \n" .
- "insertions/updates is rolled back and will not be found in the database\n\n";
- try {
-
- if ($format == 'newick') {
-
- module_load_include('inc', 'tripal_phylogeny', 'includes/parsers/tripal_phylogeny.newick_parser');
- $tree = tripal_phylogeny_parse_newick_file($file_name);
-
- tripal_assign_phylogeny_tree_indices($tree);
- }
-
-
- tripal_phylogeny_import_tree($tree, $phylotree, $options);
- }
- catch (Exception $e) {
- $transaction->rollback();
- watchdog_exception('tripal_phylogeny', $e);
- print "\nFAILED: Rolling back database changes...\n";
- }
- print "\nDone Importing Tree.\n";
- }
|