123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782 |
- <?php
- function tripal_phylogeny_node_info() {
- $nodes = array();
- $nodes['chado_phylotree'] = array(
- 'name' => t('Phylotree'),
- 'base' => 'chado_phylotree',
- 'description' => t('A phylotree from the chado database'),
- 'has_title' => TRUE,
- 'locked' => TRUE,
- 'chado_node_api' => array(
- 'base_table' => 'phylotree',
- 'hook_prefix' => 'chado_phylotree',
- 'record_type_title' => array(
- 'singular' => t('Phylotree'),
- 'plural' => t('Phylotrees')
- ),
-
- 'sync_filters' => array(
- 'type_id' => FALSE,
- 'organism_id' => FALSE
- ),
- )
- );
- return $nodes;
- }
- function tripal_phylogeny_node_view($node, $view_mode, $langcode) {
- if($node->type != 'chado_phylotree') { return; }
- switch($view_mode) {
- case 'full':
- $node->content['tripal_phylogeny_base'] = array(
- '#theme' => 'tripal_phylogeny_base',
- '#node' => $node,
- '#tripal_toc_id' => 'base',
- '#tripal_toc_title' => 'Overview',
- '#weight' => -100,
- );
- $node->content['tripal_phylogeny_phylogram'] = array(
- '#theme' => 'tripal_phylogeny_phylogram',
- '#node' => $node,
- '#tripal_toc_id' => 'phylotree_phylogram',
- '#tripal_toc_title' => 'Phylogram',
- '#weight' => -90,
- );
- $node->content['tripal_phylogeny_taxonomic_tree'] = array(
- '#theme' => 'tripal_phylogeny_taxonomic_tree',
- '#node' => $node,
- '#tripal_toc_id' => 'tripal_phylogeny_taxonomic_tree',
- '#tripal_toc_title' => 'Taxonomic Tree',
- '#weight' => -80,
- );
- $node->content['tripal_phylogeny_organisms'] = array(
- '#theme' => 'tripal_phylogeny_organisms',
- '#node' => $node,
- '#tripal_toc_id' => 'phylotree_organisms',
- '#tripal_toc_title' => 'Organisms',
- '#weight' => -70,
- );
- $node->content['tripal_phylogeny_references'] = array(
- '#theme' => 'tripal_phylogeny_references',
- '#node' => $node,
- '#tripal_toc_id' => 'phylotree_references',
- '#tripal_toc_title' => 'Cross References',
- );
- $node->content['tripal_phylogeny_analysis'] = array(
- '#theme' => 'tripal_phylogeny_analysis',
- '#node' => $node,
- '#tripal_toc_id' => 'phylotree_analysis',
- '#tripal_toc_title' => 'Analysis',
- );
- break;
- case 'teaser':
- $node->content['tripal_phylogeny_teaser'] = array(
- '#theme' => 'tripal_phylogeny_teaser',
- '#node' => $node,
- );
- break;
- }
- }
- function chado_phylotree_form($node, &$form_state) {
- $form = array();
-
-
-
-
-
-
-
-
-
- $phylotree = null;
- $phylotree_id = null;
- $tree_name = '';
- $leaf_type = '';
- $analysis_id = '';
- $dbxref = '';
- $comment = '';
- $tree_required = TRUE;
- $tree_file = '';
- $name_re = '';
- $match = '';
-
- if (property_exists($node, 'phylotree')) {
- $phylotree = $node->phylotree;
- $phylotree = chado_expand_var($phylotree, 'field', 'phylotree.comment');
- $phylotree_id = $phylotree->phylotree_id;
- $tree_name = $phylotree->name;
- $leaf_type = $phylotree->type_id ? $phylotree->type_id->name : '';
- $comment = $phylotree->comment;
- $analysis_id = $phylotree->analysis_id ? $phylotree->analysis_id->analysis_id : '';
- $dbxref = $phylotree->dbxref_id->db_id->name . ":" . $phylotree->dbxref_id->accession;
- $name_re = $phylotree->tripal_variables->phylotree_name_re;
- $match = $phylotree->tripal_variables->phylotree_use_uniquename;
-
- if ($phylotree->dbxref_id->db_id->name == 'null') {
- $dbxref = '';
- }
-
-
-
-
- $file_id = $phylotree->tripal_variables->phylotree_tree_file;
- if (is_numeric($file_id)) {
- $file = file_load($file_id);
- if ($file) {
- $tree_file = $file->filename;
- }
- }
- else {
- $tree_file = $file_id;
- }
-
- $tree_required = FALSE;
-
- $form['phylotree_id'] = array(
- '#type' => 'value',
- '#value' => $phylotree_id,
- );
- }
-
-
- if (array_key_exists('values', $form_state) and isset($form_state['values']['tree_name'])) {
- $tree_name = $form_state['values']['tree_name'];
- $leaf_type = $form_state['values']['leaf_type'];
- $analysis_id = $form_state['values']['analysis_id'];
- $dbxref = $form_state['values']['dbxref'];
- $comment = $form_state['values']['description'];
- }
-
-
- if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
- $tree_name = $form_state['input']['tree_name'];
- $leaf_type = $form_state['input']['leaf_type'];
- $analysis_id = $form_state['input']['analysis_id'];
- $comment = $form_state['input']['description'];
- $dbxref = $form_state['input']['dbxref'];
- }
- $form['tree_name']= array(
- '#type' => 'textfield',
- '#title' => t('Tree Name'),
- '#required' => TRUE,
- '#default_value' => $tree_name,
- '#description' => t('Enter the name used to refer to this phylogenetic tree.'),
- '#maxlength' => 255
- );
- $type_cv = tripal_get_default_cv('phylotree', 'type_id');
- $so_cv = tripal_get_cv(array('name' => 'sequence'));
- $cv_id = $so_cv->cv_id;
- if (!$so_cv) {
- drupal_set_message('The Sequence Ontolgoy does not appear to be imported.
- Please import the Sequence Ontology before adding a tree.', 'error');
- }
- $form['leaf_type'] = array(
- '#title' => t('Tree Type'),
- '#type' => 'textfield',
- '#description' => t("Choose the tree type. The type is
- a valid Sequence Ontology (SO) term. For example, trees derived
- from protein sequences should use the SO term 'polypeptide'.
- Alternatively, a phylotree can be used for representing a taxonomic
- tree. In this case, the word 'taxonomy' should be used."),
- '#required' => TRUE,
- '#default_value' => $leaf_type,
- '#autocomplete_path' => "admin/tripal/legacy/tripal_cv/cvterm/auto_name/$cv_id",
- );
-
- $sql = "SELECT * FROM {analysis} ORDER BY name";
- $arset = chado_query($sql);
- $analyses = array();
- $analyses[''] = '';
- while ($analysis = $arset->fetchObject()) {
- $analyses[$analysis->analysis_id] = $analysis->name;
- }
- $form['analysis_id'] = array(
- '#title' => t('Analysis'),
- '#type' => 'select',
- '#description' => t("Choose the analysis from which this phylogenetic tree was derived"),
- '#required' => TRUE,
- '#default_value' => $analysis_id,
- '#options' => $analyses,
- );
- $form['dbxref'] = array(
- '#title' => t('Database Cross-Reference'),
- '#type' => 'textfield',
- '#description' => t("Enter a database cross-reference of the form
- [DB name]:[accession]. The database name must already exist in the
- database. If the accession does not exist it is automatically added."),
- '#required' => FALSE,
- '#default_value' => $dbxref,
- );
- $form['description']= array(
- '#type' => 'textarea',
- '#title' => t('Description'),
- '#required' => TRUE,
- '#default_value' => $comment,
- '#description' => t('Enter a description for this tree.'),
- );
- $upload_location = tripal_get_files_stream('tripal_phylogeny');
- $form['tree_file'] = array(
- '#type' => 'fieldset',
- '#title' => t('Tree File Import'),
- '#collapsible' => FALSE,
- );
- $description = t('Please provide a file in the Newick format that contains
- the nodes of this tree.');
- if ($tree_file) {
- $form['tree_file']['curr_file'] = array(
- '#type' => 'item',
- '#title' => 'Current Tree File',
- '#markup' => $tree_file,
- );
- $description = t('Please provide a file in the Newick format that
- contains the nodes of this tree. Please note that uploading a new
- file will overwrite the current tree.');
- }
- $form['tree_file']['tree_file'] = array(
- '#type' => 'managed_file',
- '#title' => t('New Tree File'),
- '#description' => $description,
- '#upload_location' => $upload_location,
- '#upload_validators' => array(
-
- 'file_validate_extensions' => array(),
-
- 'chado_phylotree_validate_newick_format' => array(),
- ),
- '#required' => $tree_required,
- );
- $form['tree_file']['name_re'] = array(
- '#title' => t('Feature Name Regular Expression'),
- '#type' => 'textfield',
- '#description' => t('If this is a phylogenetic (non taxonomic) tree, then
- the tree nodes will be automatically associated with features. However,
- if the nodes in the tree file are not exactly as the names of features
- but have enough information to uniquely identify the feature then you
- may provide a regular expression that the importer will use to extract
- the feature names from the node names.'),
- '#default_value' => $name_re,
- );
- $form['tree_file']['match'] = array(
- '#title' => t('Use Unique Feature Name'),
- '#type' => 'checkbox',
- '#description' => t('If this is a phylogenetic (non taonomic tree) and the nodes ' .
- 'should match the unique name of the feature rather than the name of the feautre ' .
- 'then select this box. If unselected the loader will try to match the feature ' .
- 'using the feature name.'),
- '#default_value' => $match,
- );
- return $form;
- }
- function chado_phylotree_validate_newick_format(stdClass $file) {
-
-
- $errors = array();
-
- return $errors;
- }
- function chado_phylotree_validate($node, $form, &$form_state) {
-
-
- if (is_null($node->nid) and property_exists($node, 'phylotree_id') and $node->phylotree_id != 0) {
- return;
- }
-
- $node->tree_name = trim($node->tree_name);
- $node->description = trim($node->description);
- $node->dbxref = trim($node->dbxref);
-
- if ($node->op == 'Delete') {
- return;
- }
- $errors = array();
- $warnings = array();
- $options = array(
- 'name' => $node->tree_name,
- 'description' => $node->description,
- 'analysis_id' => $node->analysis_id,
- 'leaf_type' => $node->leaf_type,
- 'tree_file' => $node->tree_file,
- 'format' => 'newick',
- 'dbxref' => $node->dbxref,
- 'match' => $node->match,
- 'name_re' => $node->name_re,
- );
-
- if ($node->nid) {
- $options['phylotree_id'] = $node->phylotree_id;
- tripal_validate_phylotree('update', $options, $errors, $warnings);
- }
- else {
- tripal_validate_phylotree('insert', $options, $errors, $warnings);
- }
-
- if (count($errors) > 0) {
- foreach($errors as $field => $message) {
- if ($field == 'name') {
- $field = 'tree_name';
- }
- form_set_error($field, $message);
- }
- }
-
- if (count($warnings) > 0) {
- foreach($warnings as $field => $message) {
- drupal_set_message($message, 'warning');
- }
- }
- }
- function tripal_phylogeny_node_presave($node) {
- switch ($node->type) {
-
-
-
-
-
-
- case 'chado_phylotree':
-
-
- if (property_exists($node, 'phylotreename')) {
-
- $node->title = $node->tree_name;
- }
- else if (property_exists($node, 'phylotree')) {
- $node->title = $node->phylotree->name;
- }
- break;
- }
- }
- function tripal_phylogeny_node_insert($node) {
- switch ($node->type) {
- case 'chado_phylotree':
- $phylotree_id = chado_get_id_from_nid('phylotree', $node->nid);
- $values = array('phylotree_id' => $phylotree_id);
- $phylotree = chado_generate_var('phylotree', $values);
- $phylotree = chado_expand_var($phylotree, 'field', 'phylotree.comment');
- $node->phylotree = $phylotree;
-
- chado_set_node_url($node);
-
- $node->title = chado_get_node_title($node);
- break;
- }
- }
- function tripal_phylogeny_node_update($node) {
- switch ($node->type) {
- case 'chado_phylotree':
- $phylotree_id = chado_get_id_from_nid('phylotree', $node->nid);
- $values = array('phylotree_id' => $phylotree_id);
- $phylotree = chado_generate_var('phylotree', $values);
- $phylotree = chado_expand_var($phylotree, 'field', 'phylotree.comment');
- $node->phylotree = $phylotree;
-
- $node->title = chado_get_node_title($node);
- break;
- }
- }
- function chado_phylotree_chado_node_default_title_format() {
- return '[phylotree.name]';
- }
- function chado_phylotree_chado_node_default_url_format() {
- return '/phylotree/[phylotree.name]';
- }
- function chado_phylotree_insert($node) {
- global $user;
- $node->tree_name = trim($node->tree_name);
- $node->description = trim($node->description);
- $node->dbxref = trim($node->dbxref);
-
-
-
-
- $phylotree_id = NULL;
- if (!property_exists($node, 'phylotree_id')) {
- $options = array(
- 'name' => $node->tree_name,
- 'description' => $node->description,
- 'analysis_id' => $node->analysis_id,
- 'leaf_type' => $node->leaf_type,
- 'tree_file' => $node->tree_file,
- 'format' => 'newick',
- 'dbxref' => $node->dbxref,
- 'match' => $node->match,
- 'name_re' => $node->name_re,
- );
- $errors = array();
- $warnings = array();
- if (tripal_insert_phylotree($options, $errors, $warnings)) {
- $phylotree_id = $options['phylotree_id'];
-
- tripal_add_node_variable($node->nid, 'phylotree_name_re', $node->name_re);
- tripal_add_node_variable($node->nid, 'phylotree_use_uniquename', $node->match);
- tripal_add_node_variable($node->nid, 'phylotree_tree_file', $node->tree_file);
- }
- else {
- drupal_set_message(t('Unable to insert phylotree.'), 'error');
- tripal_report_error('tripal_phylogeny', TRIPAL_WARNING,
- 'Insert phylotree: Unable to insert phylotree where values: %values',
- array('%values' => print_r($options, TRUE))
- );
- }
- }
- else {
- $phylotree_id = $node->phylotree_id;
- }
-
-
- $check_org_id = chado_get_id_from_nid('phylotree', $node->nid);
- if (!$check_org_id) {
- $record = new stdClass();
- $record->nid = $node->nid;
- $record->vid = $node->vid;
- $record->phylotree_id = $phylotree_id;
- drupal_write_record('chado_phylotree', $record);
- }
- }
- function chado_phylotree_update($node) {
- global $user;
- $node->tree_name = trim($node->tree_name);
- $node->description = trim($node->description);
- $node->dbxref = trim($node->dbxref);
-
- $phylotree_id = chado_get_id_from_nid('phylotree', $node->nid) ;
- $options = array(
- 'phylotree_id' => $node->phylotree_id,
- 'name' => $node->tree_name,
- 'description' => $node->description,
- 'analysis_id' => $node->analysis_id,
- 'leaf_type' => $node->leaf_type,
- 'tree_file' => $node->tree_file,
- 'format' => 'newick',
- 'dbxref' => $node->dbxref,
- 'match' => $node->match,
- 'name_re' => $node->name_re,
- );
- $success = tripal_update_phylotree($phylotree_id, $options);
- if (!$success) {
- drupal_set_message("Unable to update phylotree.", "error");
- tripal_report_error('tripal_phylogeny', TRIPAL_WARNING,
- 'Update phylotree: Unable to update phylotree where values: %values',
- array('%values' => print_r($options, TRUE))
- );
- return;
- }
-
- tripal_delete_node_variables($node->nid);
- tripal_add_node_variable($node->nid, 'phylotree_name_re', $node->name_re);
- tripal_add_node_variable($node->nid, 'phylotree_use_uniquename', $node->match);
- tripal_add_node_variable($node->nid, 'phylotree_tree_file', $node->tree_file);
- }
- function chado_phylotree_load($nodes) {
- foreach ($nodes as $nid => $node) {
- $phylotree_id = chado_get_id_from_nid('phylotree', $nid);
-
-
- if (!$phylotree_id) {
- continue;
- }
-
- $values = array('phylotree_id' => $phylotree_id);
- $phylotree = chado_generate_var('phylotree', $values);
- $nodes[$nid]->phylotree = $phylotree;
-
-
- $phylotree = chado_expand_var($phylotree, 'field', 'phylotree.comment');
-
-
- $phylotree->tripal_variables = new stdClass;
- $variables = tripal_get_node_variables($nid, 'phylotree_name_re');
- $phylotree->tripal_variables->phylotree_name_re = count($variables) > 0 ? $variables[0]->value : '';
- $variables = tripal_get_node_variables($nid, 'phylotree_use_uniquename');
- $phylotree->tripal_variables->phylotree_use_uniquename = count($variables) > 0 ? $variables[0]->value : '';
- $variables = tripal_get_node_variables($nid, 'phylotree_tree_file');
- $phylotree->tripal_variables->phylotree_tree_file = count($variables) > 0 ? $variables[0]->value : '';
-
- $node->title = chado_get_node_title($node);
- }
- }
- function chado_phylotree_delete(&$node) {
- $phylotree_id = chado_get_id_from_nid('phylotree', $node->nid);
-
-
- if (!$phylotree_id) {
- return;
- }
-
-
- $sql_del = "DELETE FROM {chado_phylotree} WHERE nid = :nid AND vid = :vid";
- db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
- $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
- db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
- $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
- db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
-
-
- chado_query("DELETE FROM {phylotree} WHERE phylotree_id = :phylotree_id", array(':phylotree_id' => $phylotree_id));
- }
- function chado_phylotree_node_access($node, $op, $account) {
- $node_type = $node;
- if (is_object($node)) {
- $node_type = $node->type;
- }
- if($node_type == 'chado_phylotree') {
- if ($op == 'create') {
- if (!user_access('create chado_phylotree content', $account)) {
- return NODE_ACCESS_DENY;
- }
- return NODE_ACCESS_ALLOW;
- }
- if ($op == 'update') {
- if (!user_access('edit chado_phylotree content', $account)) {
- return NODE_ACCESS_DENY;
- }
- }
- if ($op == 'delete') {
- if (!user_access('delete chado_phylotree content', $account)) {
- return NODE_ACCESS_DENY;
- }
- }
- if ($op == 'view') {
- if (!user_access('access chado_phylotree content', $account)) {
- return NODE_ACCESS_DENY;
- }
- }
- return NODE_ACCESS_IGNORE;
- }
- }
- function phylotree_feature_summary($phylotree_id) {
- $sql = "
- SELECT o.abbreviation, COUNT(o.organism_id) AS count
- FROM {phylonode} n
- LEFT OUTER JOIN {feature} f ON n.feature_id = f.feature_id
- LEFT OUTER JOIN {organism} o ON f.organism_id = o.organism_id
- WHERE n.phylotree_id = :phylotree_id
- AND n.feature_id IS NOT NULL
- GROUP BY o.organism_id
- ";
- $args = array(':phylotree_id' => $phylotree_id);
- $result = chado_query($sql, $args);
- $summary = array();
- foreach($result as $r) {
- $summary[$r->abbreviation] = $r->count;
- }
- return $summary;
- }
|