123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- <?php
- /**
- * @file
- * Contains all functions for creating the blastdb node type
- */
- /**
- * Implements hook_node_info().
- */
- function blast_ui_node_info() {
- return array(
- 'blastdb' => array(
- 'name' => t('Blast Database'),
- 'base' => 'blastdb',
- 'description' => t('Registers a BLAST Database for use with the BLAST UI.'),
- ),
- );
- }
- /**
- * Implements hook_node_access().
- */
- function blastdb_node_access($node, $op, $account) {
- $node_type = $node;
- if (is_object($node)) {
- $node_type = $node->type;
- }
- if($node_type == 'blastdb') {
- if ($op == 'create') {
- if (!user_access('create Blast Database', $account)) {
- return NODE_ACCESS_DENY;
- }
- return NODE_ACCESS_ALLOW;
- }
- if ($op == 'update') {
- if (!user_access('edit Blast Database', $account)) {
- return NODE_ACCESS_DENY;
- }
- }
- if ($op == 'delete') {
- if (!user_access('delete Blast Database', $account)) {
- return NODE_ACCESS_DENY;
- }
- }
- if ($op == 'view') {
- if (!user_access('access Blast Database', $account)) {
- return NODE_ACCESS_DENY;
- }
- }
- return NODE_ACCESS_IGNORE;
- }
- }
- /**
- * Form constructor for the blastdb node
- *
- * @see blastdb_insert()
- * @see blastdb_update()
- * @see blastdb_delete()
- * @see blastdb_load()
- */
- function blastdb_form($node, &$form_state) {
- $form = array();
- $form['#validate'] = array('blastdb_form_validate');
-
- $form['#attached']['css'] = array(
- drupal_get_path('module', 'blast_ui') . '/theme/css/form.css',
- );
- $form['core'] = array(
- '#type' => 'fieldset',
- '#title' => 'General'
- );
- $form['core']['db_name']= array(
- '#type' => 'textfield',
- '#title' => t('Human-readable Name for Blast database'),
- '#required' => TRUE,
- '#default_value' => isset($node->db_name) ? $node->db_name : '',
- );
- $form['core']['db_path']= array(
- '#type' => 'textfield',
- '#title' => t('File Prefix including Full Path'),
- '#description' => t('The full path to your blast database including the file name but not the file type suffix. For example, /var/www/website/sites/default/files/myblastdb'),
- '#required' => TRUE,
- '#default_value' => isset($node->db_path) ? $node->db_path : '',
- );
- $form['core']['db_dbtype'] = array(
- '#type' => 'radios',
- '#title' => t('Type of the blast database'),
- '#options' => array(
- 'nucleotide' => t('Nucleotide'),
- 'protein' => t('Protein'),
- ),
- '#required' => TRUE,
- '#default_value' => isset($node->db_dbtype) ? $node->db_dbtype : 'n',
- );
- $form['dbxref'] = array(
- '#type' => 'fieldset',
- '#title' => 'Link-outs',
- '#description' => 'These settings will be used to <em>transform the hit name into a
- link to additional information</em>.',
- '#prefix' => '<div id="link-outs">',
- '#suffix' => '</div>',
- );
- $regex = array(
- 'default' => array(
- 'title' => 'Generic',
- 'help' => 'A single word followed by a free-text definition. '
- . 'The first word contains only alphanumeric characters and optionally '
- . 'underscores and will be used as the ID of the sequence.'
- ),
- 'genbank' => array(
- 'title' => 'NCBI GenBank',
- 'help' => 'Follows the same format as the first option '
- . 'except that the first "word" is of the following form: '
- . 'gb|accession|locus. The accession will be used as the ID of the sequence.'
- ),
- 'embl' => array(
- 'title' => 'EMBL Data Library',
- 'help' => 'Follows the same format as the first option '
- . 'except that the first "word" is of the following form: '
- . 'emb|accession|locus. The accession will be used as the ID of the sequence.'
- ),
- 'swissprot' => array(
- 'title' => 'SWISS-PROT',
- 'help' => 'Follows the same format as the first option '
- . 'except that the first "word" is of the following form: '
- . 'sp|accession|entry name. The accession will be used as the ID of the sequence.'
- ),
- 'custom' => array(
- 'title' => 'Custom Format',
- 'help' => 'Allows you to use a regular expression (define one below) to '
- . 'extract a specifc portion of the FASTA header to be used as the ID.'
- ),
- );
- $regex_type = (isset($node->linkout->regex_type)) ? $node->linkout->regex_type : 'default';
- $regex_type = (isset($form_state['values'])) ? $form_state['values']['dbxref_id_type'] : $regex_type;
- $form['dbxref']['dbxref_id_type'] = array(
- '#type' => 'radios',
- '#title' => 'FASTA header format',
- '#description' => 'Choose the format that matches the format of the FASTA '
- . 'headers in this BLAST database or choose custom to define your own '
- . 'using regular expressions. This ID will be used to create the URL for the link-out.',
- '#options' => array(
- 'default' => '<span title="' . $regex['default']['help'] . '">' . $regex['default']['title'] . '</span>',
- 'genbank' => '<span title="' . $regex['genbank']['help'] . '">' . $regex['genbank']['title'] . '</span>',
- 'embl' => '<span title="' . $regex['embl']['help'] . '">' . $regex['embl']['title'] . '</span>',
- 'swissprot' => '<span title="' . $regex['swissprot']['help'] . '">' . $regex['swissprot']['title'] . '</span>',
- 'custom' => '<span title="' . $regex['custom']['help'] . '">' . $regex['custom']['title'] . '</span>',
- ),
- '#default_value' => $regex_type,
- '#ajax' => array(
- 'callback' => 'ajax_blast_ui_node_linkout_custom_callback',
- 'wrapper' => 'link-outs',
- )
- );
- // Add information about each format to the description.
- if ($regex_type) {
- $form['dbxref']['dbxref_id_type']['#description'] .= '
- <p class="blastdb-extra-info"><strong>'.$regex[$regex_type]['title'].'</strong>: '.$regex[$regex_type]['help'].'</p>';
- }
- $hide_regex = TRUE;
- if (isset($form_state['values']['dbxref_id_type'])) {
- if ($form_state['values']['dbxref_id_type'] == 'custom') {
- $hide_regex = FALSE;
- }
- }
- $form['dbxref']['regex'] = array(
- '#type' => 'textfield',
- '#title' => 'Regular Expression',
- '#description' => t('A PHP Regular expression with curved brackets '
- . 'surrounding the ID that should be used in the URL to provide a link-'
- . 'out to additional information. See <a href="@url" target="_blank">PHP.net Regular '
- . 'Expression Documentation</a> for more information. <strong>Be sure '
- . 'to include the opening and closing slashes</strong>. This is only '
- . 'available if custom was choosen for the FASTA header format above.',
- array('@url' => 'http://php.net/manual/en/reference.pcre.pattern.syntax.php')),
- '#disabled' => $hide_regex,
- '#default_value' => (isset($node->linkout->regex)) ? $node->linkout->regex : ''
- );
- $db_options = tripal_get_db_select_options();
- $db_options[0] = '';
- asort($db_options);
- $form['dbxref']['db_id'] = array(
- '#type' => 'select',
- '#title' => 'External Database',
- '#description' => 'The external database you would like to link-out to. '
- . 'Note that this list includes all Tripal Databases and if the database '
- . 'you would like to link-out to is not included you can add it through '
- . l('Administration > Tripal > Chado Modules > Databases','admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
- . '.',
- '#options' => $db_options,
- '#default_value' => (isset($node->linkout->db_id->db_id)) ? $node->linkout->db_id->db_id : 0
- );
- $types = module_invoke_all('blast_linkout_info');
- $options = array();
- foreach ($types as $machine_name => $details) {
- $options[$machine_name] = (isset($details['name'])) ? $details['name'] : $machine_name;
- }
- $linkout_type = (isset($node->linkout->type)) ? $node->linkout->type : 'link';
- $linkout_type = (isset($form_state['values'])) ? $form_state['values']['dbxref_linkout_type'] : $linkout_type;
- $form['dbxref']['dbxref_linkout_type'] = array(
- '#type' => 'radios',
- '#title' => 'Link-out Type',
- '#description' => 'This determines how the URL to be linked to is formed. <strong>Make
- sure the database chosen supports this type of link</strong> (ie: the database
- should point to a GBrowse instance if you choose GBrowse here).',
- '#options' => $options,
- '#default_value' => $linkout_type,
- '#ajax' => array(
- 'callback' => 'ajax_blast_ui_node_linkout_custom_callback',
- 'wrapper' => 'link-outs',
- )
- );
- // Add information about each format to the description.
- if ($linkout_type) {
- $form['dbxref']['dbxref_linkout_type']['#description'] .= '
- <p class="blastdb-extra-info"><strong>'.$types[$linkout_type]['name'].'</strong>: '.$types[$linkout_type]['help'].'</p>';
- }
-
- return $form;
- }
- function blastdb_form_validate($form, $form_state) {
- if (!empty($form_state['values']['regex'])) {
- // Check that any supplied regex includes //.
- if (!preg_match('/\/.*\//', $form_state['values']['regex'])) {
- form_set_error('regex', 'Regular Expressions require opening and closing slashes to delinate them. For example, <em>/^(\s+) .*/</em>');
- }
- // Check that the supplied regex is valid.
- elseif (@preg_match($form_state['values']['regex'], NULL) === FALSE) {
- form_set_error('regex', 'Regular Expression not valid. See '
- . '<a href="http://php.net/manual/en/reference.pcre.pattern.syntax.php" target="_blank">PHP.net Regular '
- . 'Expression Documentation</a> for more information.');
- }
- }
- // Check that the supplied db actually contains a URL prefix.
- if ($form_state['values']['db_id']) {
- $db = tripal_get_db(array('db_id' => $form_state['values']['db_id']));
- if (empty($db)) {
- form_set_error('db_id', 'The database chosen no longer exists.');
- }
- if (empty($db->urlprefix)) {
- form_set_error('db_id', 'The database choosen does not have a URL prefix '
- . 'listed which means a link-out could not be created for BLAST hits. '
- . 'Please edit the database '
- . l('here', 'admin/tripal/chado/tripal_db/edit/' . $db->db_id,
- array('attributes' => array('target' => '_blank')))
- . ' to include a URL prefix before continuing'
- );
- }
- }
- }
- /**
- * Implements hook_insert().
- */
- function blastdb_insert($node) {
- // Handle Link-out Rules.
- if ($node->dbxref_id_type == 'custom') {
- $regex = $node->regex;
- }
- else {
- $regex = $node->dbxref_id_type;
- }
- if (!$node->dbxref_linkout_type) {
- $node->dbxref_linkout_type = 'link';
- }
-
- // Actually insert the record.
- db_insert('blastdb')->fields(array(
- 'nid' => $node->nid,
- 'name' => $node->db_name,
- 'path' => $node->db_path,
- 'dbtype' => $node->db_dbtype,
- 'dbxref_id_regex' => $regex,
- 'dbxref_db_id' => $node->db_id,
- 'dbxref_linkout_type' => $node->dbxref_linkout_type,
- ))->execute();
- }
- /**
- * Implements hook_node_insert().
- * This function acts on ALL NODES
- */
- function blast_ui_node_insert($node) {
- if ($node->type == 'blastdb') {
- $node->title = $node->db_name;
- }
- }
- /**
- * Implements hook_update().
- */
- function blastdb_update($node) {
- // Handle Link-out Rules.
- if ($node->dbxref_id_type == 'custom') {
- $regex = $node->regex;
- }
- else {
- $regex = $node->dbxref_id_type;
- }
- if (!$node->dbxref_linkout_type) {
- $node->dbxref_linkout_type = 'link';
- }
-
- // Update the record.
- db_update('blastdb')->fields(array(
- 'name' => $node->db_name,
- 'path' => $node->db_path,
- 'dbtype' => $node->db_dbtype,
- 'dbxref_id_regex' => $regex,
- 'dbxref_db_id' => $node->db_id,
- 'dbxref_linkout_type' => $node->dbxref_linkout_type,
- ))->condition('nid', $node->nid)->execute();
- }
- /**
- * Implements hook_node_update().
- * This function acts on ALL NODES
- */
- function blast_ui_node_update($node) {
- if ($node->type == 'blastdb') {
- $node->title = $node->db_name;
- }
- }
- /**
- * Implements hook_delete().
- */
- function blastdb_delete($node) {
- db_delete('blastdb')->condition('nid',$node->nid)->execute();
- }
- /**
- * Implements hook_load().
- */
- function blastdb_load($nodes) {
- $sql = "
- SELECT nid, name, path, dbtype, dbxref_id_regex, dbxref_db_id,
- dbxref_linkout_type
- FROM {blastdb}
- WHERE nid IN (:nids)";
- $result = db_query($sql, array(':nids' => array_keys($nodes)));
- foreach ($result as $record) {
- // Does this BLAST node have a custom linkout?
- // (Is there a better way to determine this?)
- $custom_linkout = ($record->dbxref_linkout_type != ''
- && $record->dbxref_linkout_type != 'link'
- && $record->dbxref_linkout_type != 'gbrowse'
- && $record->dbxref_linkout_type != 'jbrowse');
- $nodes[$record->nid]->db_name = $record->name;
- $nodes[$record->nid]->db_path = $record->path;
- $nodes[$record->nid]->title = $record->name;
- $nodes[$record->nid]->db_dbtype = $record->dbtype;
- // There will be a hit linkout if there is a regex pattern for the id
- // and a database (db record) to generate a URL
- // OR if a custom linkout has been set, which may or may not require a
- // regex and/or database.
- if ($record->dbxref_id_regex AND $record->dbxref_db_id || $custom_linkout) {
- $nodes[$record->nid]->linkout = new stdClass();
- if (preg_match('/\/.*\//', $record->dbxref_id_regex)) {
- $nodes[$record->nid]->linkout->regex_type = 'custom';
- $nodes[$record->nid]->linkout->regex = $record->dbxref_id_regex;
- }
- else {
- $nodes[$record->nid]->linkout->regex_type = $record->dbxref_id_regex;
- $nodes[$record->nid]->linkout->regex = get_blastdb_linkout_regex($nodes[$record->nid]);
- }
- $nodes[$record->nid]->linkout->db_id = tripal_get_db(array('db_id' => $record->dbxref_db_id));
- $nodes[$record->nid]->linkout->none = FALSE;
- // Support complex link-outs.
- $nodes[$record->nid]->linkout->type = $record->dbxref_linkout_type;
- $types = module_invoke_all('blast_linkout_info');
- if (isset($types[$record->dbxref_linkout_type])) {
- $nodes[$record->nid]->linkout->url_function = $types[$record->dbxref_linkout_type]['process function'];
- }
- else {
- $nodes[$record->nid]->linkout->url_function = '';
- tripal_report_error(
- 'blast_ui',
- TRIPAL_ERROR,
- 'Unable to find details on the type of link-out choosen (%type). Have you defined hook_blast_linkout_info()? Make sure to clear the cache once you do so.',
- array('%type' => $record->dbxref_linkout_type)
- );
- }
- }
- else {
- // No linkout
- $nodes[$record->nid]->linkout = new stdClass();
- $nodes[$record->nid]->linkout->regex = '';
- $nodes[$record->nid]->linkout->db_id = 0;
- $nodes[$record->nid]->linkout->none = TRUE;
- }
- }
- }
- /**
- * AJAX Callback: Update Node Link-out Regex Textfield.
- *
- * On BlastDB node form the Link-out (dbxref) options allow for settings of a
- * custom regex which should only be enabled when "Custom" is selected. This
- * callback refreshes the regex textfield so it can change (ie: become enabled)
- * when someone selects custom.
- */
- function ajax_blast_ui_node_linkout_custom_callback($form, $form_state) {
- return $form['dbxref'];
- }
|