123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- <?php
- /**
- * Implements hook_vocab_storage_info().
- *
- * This hook is created by the Tripal module and is not a Drupal hook.
- */
- function tripal_chado_vocab_storage_info() {
- return array(
- 'term_chado_storage' => array(
- 'label' => t('Chado'),
- 'module' => 'tripal_chado',
- 'description' => t('Integrates terms stored in the local Chado database
- with Tripal entities.'),
- 'settings' => array(),
- ),
- );
- }
- /**
- * Implements hook_vocab_get_vocabularies().
- *
- * This hook is created by the Tripal module and is not a Drupal hook.
- */
- function tripal_chado_vocab_get_vocabularies() {
- $vocabs = array();
- // It's possible that Chado is not available (i.e. it gets renamed
- // for copying) but Tripal has already been prepared and the
- // entities exist. If this is the case we don't want to run the
- // commands below.
- if (!chado_table_exists('cv')) {
- return FALSE;
- }
- // Make sure the materiailzd view is present.
- if (!chado_table_exists('db2cv_mview')) {
- drupal_set_message('Please update the database using "drush updatedb" before continuing');
- return FALSE;
- }
- $sql = "
- SELECT DB.name as short_name, DB.description, DB.url, DB.urlprefix,
- SUM(DBCVM.num_terms) as num_terms,
- array_to_string(array_agg(DBCVM.cvname), ', ') as name
- FROM {db} DB
- INNER JOIN {db2cv_mview} DBCVM ON DBCVM.db_id = DB.db_id
- GROUP BY DB.name, DB.description, DB.url, DB.urlprefix
- ORDER BY DB.name
- ";
- $results = chado_query($sql, array());
- while ($result = $results->fetchAssoc()) {
- if (!$result['name']) {
- $result['name'] = $result['short_name'];
- }
- $sw_url = $result['urlprefix'];
- if ($sw_url) {
- $sw_url = preg_replace('/\{db\}/', $result['short_name'], $sw_url);
- $sw_url = preg_replace('/\{accession\}/', '', $sw_url);
- $sw_url = url($sw_url, array('absolute' => TRUE));
- }
- $result['sw_url'] = $sw_url;
- $vocabs[] = $result;
- }
- if (count($vocabs) == 0) {
- $link = l('populating', 'admin/tripal/storage/chado/mviews/update', array('attributes' => array('target' => '_blank')));
- $message = t('If there are no controlled vocabularies it is mostly likely because the db2cv_mview is not populated. Try !populating this mview.', array('!populating' => $link));
- tripal_set_message($message, TRIPAL_NOTICE);
- }
- return $vocabs;
- }
- /**
- * Implements hook_vocab_get_vocabulary().
- *
- * This hook is created by the Tripal module and is not a Drupal hook.
- */
- function tripal_chado_vocab_get_vocabulary($vocabulary) {
- // It's possible that Chado is not available (i.e. it gets renamed
- // for copying) but Tripal has already been prepared and the
- // entities exist. If this is the case we don't want to run the
- // commands below.
- if (!chado_table_exists('cv')) {
- return FALSE;
- }
- // Make sure the materiailzd view is present.
- if (!chado_table_exists('db2cv_mview')) {
- drupal_set_message('Please update the database using "drush updatedb" before continuing');
- return FALSE;
- }
- $sql = "
- SELECT DB.name as short_name, DB.description, DB.url, DB.urlprefix,
- SUM(DBCVM.num_terms) as num_terms,
- array_to_string(array_agg(DBCVM.cvname), ', ') as name
- FROM {db} DB
- INNER JOIN {db2cv_mview} DBCVM ON DBCVM.db_id = DB.db_id
- WHERE DB.name = :name
- GROUP BY DB.name, DB.description, DB.url, DB.urlprefix
- ";
- $result = chado_query($sql, array(':name' => $vocabulary));
- $result = $result->fetchAssoc();
- if (!$result) {
- drupal_set_message("Could not find details about the vocabulary: $vocabulary");
- return FALSE;
- }
- if (!$result['name']) {
- $result['name'] = $result['short_name'];
- }
- $sw_url = $result['urlprefix'];
- if ($sw_url) {
- $sw_url = preg_replace('/\{db\}/', $result['short_name'], $sw_url);
- $sw_url = preg_replace('/\{accession\}/', '', $sw_url);
- $sw_url = url($sw_url, array('absolute' => TRUE));
- }
- $result['sw_url'] = $sw_url;
- return $result;
- }
- /**
- * Implements hook_vocab_get_root_terms().
- *
- * This hook is created by the Tripal module and is not a Drupal hook.
- */
- function tripal_chado_vocab_get_root_terms($vocabulary) {
- $terms = array();
- // It's possible that Chado is not available (i.e. it gets renamed
- // for copying) but Tripal has already been prepared and the
- // entities exist. If this is the case we don't want to run the
- // commands below.
- if (!chado_table_exists('db')) {
- return FALSE;
- }
- // Get the list of CV's that belong to this vocabulary and get their
- // roots.
- $sql = "
- SELECT *
- FROM {db2cv_mview} WHERE dbname = :dbname
- ";
- $cvs = chado_query($sql, array(':dbname' => $vocabulary));
- while ($cv = $cvs->fetchObject()) {
- $sql = "
- SELECT cvterm_id
- FROM {cv_root_mview} CRM
- WHERE CRM.cv_id = :cv_id
- ";
- $results = chado_query($sql, array(':cv_id' => $cv->cv_id));
- while($cvterm_id = $results->fetchField()) {
- $match = array('cvterm_id' => $cvterm_id);
- $cvterm = chado_generate_var('cvterm', $match);
- $terms[] = _tripal_chado_format_term_description($cvterm);
- }
- }
- return $terms;
- }
- /**
- * Implements hook_vocab_get_terms().
- *
- * This hook is created by the Tripal module and is not a Drupal hook.
- */
- function tripal_chado_vocab_get_terms($vocabulary, $limit = 25, $element = 0) {
- // It's possible that Chado is not available (i.e. it gets renamed
- // for copying) but Tripal has already been prepared and the
- // entities exist. If this is the case we don't want to run the
- // commands below.
- if (!chado_table_exists('cvterm')) {
- return FALSE;
- }
- $sql = "
- SELECT CVT.cvterm_id
- FROM {cvterm} CVT
- INNER JOIN {dbxref} DBX on DBX.dbxref_id = CVT.dbxref_id
- INNER JOIN {db} DB on DB.db_id = DBX.db_id
- WHERE db.name = :dbname
- ORDER BY CVT.name
- ";
- $csql = "
- SELECT COUNT(CVT.cvterm_id)
- FROM {cvterm} CVT
- INNER JOIN {dbxref} DBX on DBX.dbxref_id = CVT.dbxref_id
- INNER JOIN {db} DB on DB.db_id = DBX.db_id
- WHERE db.name = :dbname
- ";
- $results = chado_pager_query($sql, array(':dbname' => $vocabulary), $limit, $element, $csql);
- $terms = array();
- while($cvterm_id = $results->fetchField()) {
- $match = array('cvterm_id' => $cvterm_id);
- $cvterm = chado_generate_var('cvterm', $match);
- $terms[] = _tripal_chado_format_term_description($cvterm);
- }
- return $terms;
- }
- /**
- * Implements hook_vocab_get_term_children().
- *
- * This hook is created by the Tripal module and is not a Drupal hook.
- */
- function tripal_chado_vocab_get_term_children($vocabulary, $accession) {
- $terms = array();
- // It's possible that Chado is not available (i.e. it gets renamed
- // for copying) but Tripal has already been prepared and the
- // entities exist. If this is the case we don't want to run the
- // commands below.
- if (!chado_table_exists('cvtermpath')) {
- return FALSE;
- }
- // Get the parent cvterm.
- $match = array(
- 'dbxref_id' => array(
- 'db_id' => array(
- 'name' => $vocabulary,
- ),
- 'accession' => $accession,
- ),
- );
- $cvterm = chado_generate_var('cvterm', $match);
- if (!$cvterm) {
- return FALSE;
- }
- $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
- // Get the children.
- $sql = "
- SELECT DISTINCT subject_id
- FROM {cvterm_relationship} CVTR
- WHERE object_id = :object_id
- ";
- $results = chado_query($sql, array(':object_id' => $cvterm->cvterm_id));
- while($cvterm_id = $results->fetchField()) {
- $match = array('cvterm_id' => $cvterm_id);
- $cvterm = chado_generate_var('cvterm', $match);
- $terms[] = _tripal_chado_format_term_description($cvterm);
- }
- return $terms;
- }
- /**
- * Implements hook_vocab_get_term().
- *
- * This hook is created by the Tripal module and is not a Drupal hook.
- */
- function tripal_chado_vocab_get_term($vocabulary, $accession) {
- // It's possible that Chado is not available (i.e. it gets renamed
- // for copying) but Tripal has already been prepared and the
- // entities exist. If this is the case we don't want to run the
- // commands below.
- if (!chado_table_exists('cvterm')) {
- return FALSE;
- }
- $match = array(
- 'dbxref_id' => array(
- 'db_id' => array(
- 'name' => $vocabulary,
- ),
- 'accession' => $accession,
- ),
- );
- $cvterm = chado_generate_var('cvterm', $match);
- if (!$cvterm) {
- return FALSE;
- }
- $cvterm = chado_expand_var($cvterm, 'field', 'cvterm.definition');
- return _tripal_chado_format_term_description($cvterm);
- }
- /**
- * A helper functions for the hook_vocab_xxx functions.
- *
- * @param $cvterm
- * A cvterm object.
- */
- function _tripal_chado_format_term_description($cvterm) {
- $url = $cvterm->dbxref_id->db_id->url;
- $urlprefix = $cvterm->dbxref_id->db_id->urlprefix;
- // Generate the URL that can be used for semantic web applications.
- $sw_url = $urlprefix;
- if ($sw_url) {
- $sw_url = preg_replace('/{db}/', $cvterm->dbxref_id->db_id->name, $sw_url);
- $sw_url = preg_replace('/{accession}/', '', $sw_url);
- $sw_url = url($sw_url, array('absolute' => TRUE));
- }
- $vocabulary = tripal_chado_vocab_get_vocabulary($cvterm->dbxref_id->db_id->name);
- $term = array(
- 'vocabulary' => $vocabulary,
- 'accession' => $cvterm->dbxref_id->accession,
- 'name' => $cvterm->name,
- 'url' => chado_get_dbxref_url($cvterm->dbxref_id),
- 'definition' => (isset($cvterm->definition)) ? $cvterm->definition : '',
- );
- return $term;
- }
- /**
- * Implements hook_vocab_add_term().
- *
- * This hook is created by the Tripal module and is not a Drupal hook.
- */
- function tripal_chado_vocab_add_term($details) {
- $vocabulary = $details['vocab']['name'];
- $accession = $details['accession'];
- // First check to make sure the term doesn't already exist
- $term = tripal_chado_vocab_get_term($vocabulary, $accession);
- if ($term) {
- return TRUE;
- }
- // First make sure the vocabulary is added.
- $values = array(
- 'name' => $vocabulary,
- 'description' => $details['vocab']['description'],
- 'url' => $details['vocab']['url'],
- // TODO: deal with the URL prefix
- );
- $options = array('update_existing' => TRUE);
- chado_insert_db($values, $options);
- // Second make sure the term is added.
- $term = chado_insert_cvterm(array(
- 'id' => $vocabulary . ':' . $accession,
- 'name' => $details['name'],
- 'definition' => $details['definition'],
- 'cv_name' => $details['vocab']['name'],
- ));
- // Return TRUE on success.
- if (!$term) {
- return FALSE;
- }
- return TRUE;
- }
- /**
- * Implements hook_vocab_import_form();
- */
- function tripal_chado_vocab_import_form($form, &$form_state) {
- module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
- return tripal_cv_obo_form($form, $form_state);
- }
- /**
- * Implements hook_vocab_import_form_validate();
- */
- function tripal_chado_vocab_import_form_validate($form, &$form_state) {
- module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
- return tripal_cv_obo_form_validate($form, $form_state);
- }
- /**
- * Implements hook_vocab_import_form_submit();
- */
- function tripal_chado_vocab_import_form_submit($form, &$form_state) {
- module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader');
- return tripal_cv_obo_form_submit($form, $form_state);
- }
|