blast_ui.node.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /**
  3. * @file
  4. * Contains all functions for creating the blastdb node type
  5. */
  6. /**
  7. * Implements hook_node_info().
  8. */
  9. function blast_ui_node_info() {
  10. return array(
  11. 'blastdb' => array(
  12. 'name' => t('Blast Database'),
  13. 'base' => 'blastdb',
  14. 'description' => t('Registers a BLAST Database for use with the BLAST UI.'),
  15. ),
  16. );
  17. }
  18. /**
  19. * Implements hook_node_access().
  20. */
  21. function blastdb_node_access($node, $op, $account) {
  22. $node_type = $node;
  23. if (is_object($node)) {
  24. $node_type = $node->type;
  25. }
  26. if($node_type == 'blastdb') {
  27. if ($op == 'create') {
  28. if (!user_access('create Blast Database', $account)) {
  29. return NODE_ACCESS_DENY;
  30. }
  31. return NODE_ACCESS_ALLOW;
  32. }
  33. if ($op == 'update') {
  34. if (!user_access('edit Blast Database', $account)) {
  35. return NODE_ACCESS_DENY;
  36. }
  37. }
  38. if ($op == 'delete') {
  39. if (!user_access('delete Blast Database', $account)) {
  40. return NODE_ACCESS_DENY;
  41. }
  42. }
  43. if ($op == 'view') {
  44. if (!user_access('access Blast Database', $account)) {
  45. return NODE_ACCESS_DENY;
  46. }
  47. }
  48. return NODE_ACCESS_IGNORE;
  49. }
  50. }
  51. /**
  52. * Form constructor for the blastdb node
  53. *
  54. * @see blastdb_insert()
  55. * @see blastdb_update()
  56. * @see blastdb_delete()
  57. * @see blastdb_load()
  58. */
  59. function blastdb_form($node, &$form_state) {
  60. $form = array();
  61. $form['#validate'] = array('blastdb_form_validate');
  62. $form['core'] = array(
  63. '#type' => 'fieldset',
  64. '#title' => 'General'
  65. );
  66. $form['core']['db_name']= array(
  67. '#type' => 'textfield',
  68. '#title' => t('Human-readable Name for Blast database'),
  69. '#required' => TRUE,
  70. '#default_value' => isset($node->db_name) ? $node->db_name : '',
  71. );
  72. $form['core']['db_path']= array(
  73. '#type' => 'textfield',
  74. '#title' => t('File Prefix including Full Path'),
  75. '#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'),
  76. '#required' => TRUE,
  77. '#default_value' => isset($node->db_path) ? $node->db_path : '',
  78. );
  79. $form['core']['db_dbtype'] = array(
  80. '#type' => 'radios',
  81. '#title' => t('Type of the blast database'),
  82. '#options' => array(
  83. 'nucleotide' => t('Nucleotide'),
  84. 'protein' => t('Protein'),
  85. ),
  86. '#required' => TRUE,
  87. '#default_value' => isset($node->db_dbtype) ? $node->db_dbtype : 'n',
  88. );
  89. $form['dbxref'] = array(
  90. '#type' => 'fieldset',
  91. '#title' => 'Database References',
  92. '#description' => 'These settings can be used to tell the BLAST UI that '
  93. . 'information about the records in this BLAST database can be found '
  94. . 'either in the current website or an external website.'
  95. );
  96. $description = array(
  97. 'default' => 'A single word followed by a free-text definition. '
  98. . 'The first word contains only alphanumeric characters and optionally '
  99. . 'underscores and will be used as the ID of the sequence.',
  100. 'genbank' => 'Follows the same format as the first option '
  101. . 'except that the first "word" is of the following form: '
  102. . 'gb|accession|locus. The accession will be used as the ID of the sequence.',
  103. 'embl' => 'Follows the same format as the first option '
  104. . 'except that the first "word" is of the following form: '
  105. . 'emb|accession|locus. The accession will be used as the ID of the sequence.',
  106. 'swissprot' => 'Follows the same format as the first option '
  107. . 'except that the first "word" is of the following form: '
  108. . 'sp|accession|entry name. The accession will be used as the ID of the sequence.',
  109. 'custom' => 'Allows you to use a regular expression (define one below) to '
  110. . 'extract a specifc portion of the FASTA header to be used as the ID.'
  111. );
  112. $form['dbxref']['dbxref_id_type'] = array(
  113. '#type' => 'radios',
  114. '#title' => 'FASTA header format',
  115. '#description' => 'Choose the format that matches the format of the FASTA '
  116. . 'headers in this BLAST database or choose custom to define your own '
  117. . 'using regular expressions. This ID will be appended to the URL Prefix '
  118. . ' of the database selected below.',
  119. '#options' => array(
  120. 'default' => '<span title="' . $description['default'] . '">Generic</span>',
  121. 'genbank' => '<span title="' . $description['genbank'] . '">NCBI GenBank</span>',
  122. 'embl' => '<span title="' . $description['embl'] . '">EMBL Data Library</span>',
  123. 'swissprot' => '<span title="' . $description['swissprot'] . '">SWISS-PROT</span>',
  124. 'custom' => '<span title="' . $description['custom'] . '">Custom Format</span>',
  125. ),
  126. '#default_value' => (isset($node->linkout->regex_type)) ? $node->linkout->regex_type : 'default',
  127. '#ajax' => array(
  128. 'callback' => 'ajax_blast_ui_node_linkout_custom_callback',
  129. 'wrapper' => 'link-regex',
  130. )
  131. );
  132. $hide_regex = TRUE;
  133. if (isset($form_state['values']['dbxref_id_type'])) {
  134. if ($form_state['values']['dbxref_id_type'] == 'custom') {
  135. $hide_regex = FALSE;
  136. }
  137. }
  138. $form['dbxref']['regex'] = array(
  139. '#type' => 'textfield',
  140. '#title' => 'Regular Expression',
  141. '#description' => t('A PHP Regular expression with curved brackets '
  142. . 'surrounding the ID that should be used in the URL to provide a link-'
  143. . 'out to additional information. See <a href="@url" target="_blank">PHP.net Regular '
  144. . 'Expression Documentation</a> for more information. <strong>Be sure '
  145. . 'to include the opening and closing slashes</strong>. This is only '
  146. . 'available if custom was choosen for the FASTA header format above.',
  147. array('@url' => 'http://php.net/manual/en/reference.pcre.pattern.syntax.php')),
  148. '#disabled' => $hide_regex,
  149. '#prefix' => '<div id="link-regex">',
  150. '#suffix' => '</div>',
  151. '#default_value' => (isset($node->linkout->regex)) ? $node->linkout->regex : ''
  152. );
  153. $db_options = tripal_get_db_select_options();
  154. $db_options[0] = '';
  155. $form['dbxref']['db_id'] = array(
  156. '#type' => 'select',
  157. '#title' => 'External Database',
  158. '#description' => 'The external database you would like to link-out to. '
  159. . 'Note that this list includes all Tripal Databases and if the database '
  160. . 'you would like to link-out to is not included you can add it '
  161. . '<a href="" target="_blank">Here</a>.',
  162. '#options' => $db_options,
  163. '#default_value' => (isset($node->linkout->db_id->db_id)) ? $node->linkout->db_id->db_id : 0
  164. );
  165. return $form;
  166. }
  167. function blastdb_form_validate($form, $form_state) {
  168. if (!empty($form_state['values']['regex'])) {
  169. // Check that any supplied regex includes //.
  170. if (!preg_match('/\/.*\//', $form_state['values']['regex'])) {
  171. form_set_error('regex', 'Regular Expressions require opening and closing slashes to delinate them. For example, <em>/^(\s+) .*/</em>');
  172. }
  173. // Check that the supplied regex is valid.
  174. elseif (@preg_match($form_state['values']['regex'], NULL) === FALSE) {
  175. form_set_error('regex', 'Regular Expression not valid. See '
  176. . '<a href="http://php.net/manual/en/reference.pcre.pattern.syntax.php" target="_blank">PHP.net Regular '
  177. . 'Expression Documentation</a> for more information.');
  178. }
  179. }
  180. // Check that the supplied db actually contains a URL prefix.
  181. if ($form_state['values']['db_id']) {
  182. $db = tripal_get_db(array('db_id' => $form_state['values']['db_id']));
  183. if (empty($db)) {
  184. form_set_error('db_id', 'The database chosen no longer exists.');
  185. }
  186. if (empty($db->urlprefix)) {
  187. form_set_error('db_id', 'The database choosen does not have a URL prefix '
  188. . 'listed which means a link-out could not be created for BLAST hits. '
  189. . 'Please edit the database '
  190. . l('here', 'admin/tripal/chado/tripal_db/edit/' . $db->db_id,
  191. array('attributes' => array('target' => '_blank')))
  192. . ' to include a URL prefix before continuing'
  193. );
  194. }
  195. }
  196. }
  197. /**
  198. * Implements hook_insert().
  199. */
  200. function blastdb_insert($node) {
  201. // Hangle Link-out Rules.
  202. if ($node->dbxref_id_type == 'custom') {
  203. $regex = $node->regex;
  204. }
  205. else {
  206. $regex = $node->dbxref_id_type;
  207. }
  208. // Actually insert the record.
  209. db_insert('blastdb')->fields(array(
  210. 'nid' => $node->nid,
  211. 'name' => $node->db_name,
  212. 'path' => $node->db_path,
  213. 'dbtype' => $node->db_dbtype,
  214. 'dbxref_id_regex' => $regex,
  215. 'dbxref_db_id' => $node->db_id,
  216. ))->execute();
  217. }
  218. /**
  219. * Implements hook_node_insert().
  220. * This function acts on ALL NODES
  221. */
  222. function blast_ui_node_insert($node) {
  223. if ($node->type == 'blastdb') {
  224. $node->title = $node->db_name;
  225. }
  226. }
  227. /**
  228. * Implements hook_update().
  229. */
  230. function blastdb_update($node) {
  231. // Hangle Link-out Rules.
  232. if ($node->dbxref_id_type == 'custom') {
  233. $regex = $node->regex;
  234. }
  235. else {
  236. $regex = $node->dbxref_id_type;
  237. }
  238. // Actually insert the record.
  239. db_update('blastdb')->fields(array(
  240. 'name' => $node->db_name,
  241. 'path' => $node->db_path,
  242. 'dbtype' => $node->db_dbtype,
  243. 'dbxref_id_regex' => $regex,
  244. 'dbxref_db_id' => $node->db_id,
  245. ))->condition('nid', $node->nid)->execute();
  246. }
  247. /**
  248. * Implements hook_node_update().
  249. * This function acts on ALL NODES
  250. */
  251. function blast_ui_node_update($node) {
  252. if ($node->type == 'blastdb') {
  253. $node->title = $node->db_name;
  254. }
  255. }
  256. /**
  257. * Implements hook_delete().
  258. */
  259. function blastdb_delete($node) {
  260. db_delete('blastdb')->condition('nid',$node->nid)->execute();
  261. }
  262. /**
  263. * Implements hook_load() .
  264. */
  265. function blastdb_load($nodes) {
  266. $result = db_query('SELECT nid, name, path, dbtype, dbxref_id_regex, dbxref_db_id FROM {blastdb} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes)));
  267. foreach ($result as $record) {
  268. $nodes[$record->nid]->db_name = $record->name;
  269. $nodes[$record->nid]->db_path = $record->path;
  270. $nodes[$record->nid]->title = $record->name;
  271. $nodes[$record->nid]->db_dbtype = $record->dbtype;
  272. if ($record->dbxref_id_regex) {
  273. $nodes[$record->nid]->linkout = new stdClass();
  274. if (preg_match('/\/.*\//', $record->dbxref_id_regex)) {
  275. $nodes[$record->nid]->linkout->regex_type = 'custom';
  276. $nodes[$record->nid]->linkout->regex = $record->dbxref_id_regex;
  277. }
  278. else {
  279. $nodes[$record->nid]->linkout->regex_type = $record->dbxref_id_regex;
  280. $nodes[$record->nid]->linkout->regex = get_blastdb_linkout_regex($nodes[$record->nid]);
  281. }
  282. $nodes[$record->nid]->linkout->db_id = tripal_get_db(array('db_id' => $record->dbxref_db_id));
  283. $nodes[$record->nid]->linkout->none = FALSE;
  284. }
  285. else {
  286. $nodes[$record->nid]->linkout = new stdClass();
  287. $nodes[$record->nid]->linkout->regex = '';
  288. $nodes[$record->nid]->linkout->db_id = 0;
  289. $nodes[$record->nid]->linkout->none = TRUE;
  290. }
  291. }
  292. }
  293. /**
  294. * AJAX Callback: Update Node Link-out Regex Textfield.
  295. *
  296. * On BlastDB node form the Link-out (dbxref) options allow for settings of a
  297. * custom regex which should only be enabled when "Custom" is selected. This
  298. * callback refreshes the regex textfield so it can change (ie: become enabled)
  299. * when someone selects custom.
  300. */
  301. function ajax_blast_ui_node_linkout_custom_callback($form, $form_state) {
  302. return $form['dbxref']['regex'];
  303. }