blast_ui.node.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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['#attached']['css'] = array(
  63. drupal_get_path('module', 'blast_ui') . '/theme/css/form.css',
  64. );
  65. $form['core'] = array(
  66. '#type' => 'fieldset',
  67. '#title' => 'General'
  68. );
  69. $form['core']['db_name']= array(
  70. '#type' => 'textfield',
  71. '#title' => t('Human-readable Name for Blast database'),
  72. '#required' => TRUE,
  73. '#default_value' => isset($node->db_name) ? $node->db_name : '',
  74. );
  75. $form['core']['db_path']= array(
  76. '#type' => 'textfield',
  77. '#title' => t('File Prefix including Full Path'),
  78. '#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'),
  79. '#required' => TRUE,
  80. '#default_value' => isset($node->db_path) ? $node->db_path : '',
  81. );
  82. $form['core']['db_dbtype'] = array(
  83. '#type' => 'radios',
  84. '#title' => t('Type of the blast database'),
  85. '#options' => array(
  86. 'nucleotide' => t('Nucleotide'),
  87. 'protein' => t('Protein'),
  88. ),
  89. '#required' => TRUE,
  90. '#default_value' => isset($node->db_dbtype) ? $node->db_dbtype : 'n',
  91. );
  92. $form['dbxref'] = array(
  93. '#type' => 'fieldset',
  94. '#title' => 'Link-outs',
  95. '#description' => 'These settings will be used to <em>transform the hit name into a
  96. link to additional information</em>.',
  97. '#prefix' => '<div id="link-outs">',
  98. '#suffix' => '</div>',
  99. );
  100. $regex = array(
  101. 'default' => array(
  102. 'title' => 'Generic',
  103. 'help' => 'A single word followed by a free-text definition. '
  104. . 'The first word contains only alphanumeric characters and optionally '
  105. . 'underscores and will be used as the ID of the sequence.'
  106. ),
  107. 'genbank' => array(
  108. 'title' => 'NCBI GenBank',
  109. 'help' => 'Follows the same format as the first option '
  110. . 'except that the first "word" is of the following form: '
  111. . 'gb|accession|locus. The accession will be used as the ID of the sequence.'
  112. ),
  113. 'embl' => array(
  114. 'title' => 'EMBL Data Library',
  115. 'help' => 'Follows the same format as the first option '
  116. . 'except that the first "word" is of the following form: '
  117. . 'emb|accession|locus. The accession will be used as the ID of the sequence.'
  118. ),
  119. 'swissprot' => array(
  120. 'title' => 'SWISS-PROT',
  121. 'help' => 'Follows the same format as the first option '
  122. . 'except that the first "word" is of the following form: '
  123. . 'sp|accession|entry name. The accession will be used as the ID of the sequence.'
  124. ),
  125. 'custom' => array(
  126. 'title' => 'Custom Format',
  127. 'help' => 'Allows you to use a regular expression (define one below) to '
  128. . 'extract a specifc portion of the FASTA header to be used as the ID.'
  129. ),
  130. );
  131. $regex_type = (isset($node->linkout->regex_type)) ? $node->linkout->regex_type : 'default';
  132. $regex_type = (isset($form_state['values'])) ? $form_state['values']['dbxref_id_type'] : $regex_type;
  133. $form['dbxref']['dbxref_id_type'] = array(
  134. '#type' => 'radios',
  135. '#title' => 'FASTA header format',
  136. '#description' => 'Choose the format that matches the format of the FASTA '
  137. . 'headers in this BLAST database or choose custom to define your own '
  138. . 'using regular expressions. This ID will be used to create the URL for the link-out.',
  139. '#options' => array(
  140. 'default' => '<span title="' . $regex['default']['help'] . '">' . $regex['default']['title'] . '</span>',
  141. 'genbank' => '<span title="' . $regex['genbank']['help'] . '">' . $regex['genbank']['title'] . '</span>',
  142. 'embl' => '<span title="' . $regex['embl']['help'] . '">' . $regex['embl']['title'] . '</span>',
  143. 'swissprot' => '<span title="' . $regex['swissprot']['help'] . '">' . $regex['swissprot']['title'] . '</span>',
  144. 'custom' => '<span title="' . $regex['custom']['help'] . '">' . $regex['custom']['title'] . '</span>',
  145. ),
  146. '#default_value' => $regex_type,
  147. '#ajax' => array(
  148. 'callback' => 'ajax_blast_ui_node_linkout_custom_callback',
  149. 'wrapper' => 'link-outs',
  150. )
  151. );
  152. // Add information about each format to the description.
  153. if ($regex_type) {
  154. $form['dbxref']['dbxref_id_type']['#description'] .= '
  155. <p class="blastdb-extra-info"><strong>'.$regex[$regex_type]['title'].'</strong>: '.$regex[$regex_type]['help'].'</p>';
  156. }
  157. $hide_regex = TRUE;
  158. if (isset($form_state['values']['dbxref_id_type'])) {
  159. if ($form_state['values']['dbxref_id_type'] == 'custom') {
  160. $hide_regex = FALSE;
  161. }
  162. }
  163. $form['dbxref']['regex'] = array(
  164. '#type' => 'textfield',
  165. '#title' => 'Regular Expression',
  166. '#description' => t('A PHP Regular expression with curved brackets '
  167. . 'surrounding the ID that should be used in the URL to provide a link-'
  168. . 'out to additional information. See <a href="@url" target="_blank">PHP.net Regular '
  169. . 'Expression Documentation</a> for more information. <strong>Be sure '
  170. . 'to include the opening and closing slashes</strong>. This is only '
  171. . 'available if custom was choosen for the FASTA header format above.',
  172. array('@url' => 'http://php.net/manual/en/reference.pcre.pattern.syntax.php')),
  173. '#disabled' => $hide_regex,
  174. '#default_value' => (isset($node->linkout->regex)) ? $node->linkout->regex : ''
  175. );
  176. $db_options = tripal_get_db_select_options();
  177. $db_options[0] = '';
  178. asort($db_options);
  179. $form['dbxref']['db_id'] = array(
  180. '#type' => 'select',
  181. '#title' => 'External Database',
  182. '#description' => 'The external database you would like to link-out to. '
  183. . 'Note that this list includes all Tripal Databases and if the database '
  184. . 'you would like to link-out to is not included you can add it through '
  185. . l('Administration > Tripal > Chado Modules > Databases','admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  186. . '.',
  187. '#options' => $db_options,
  188. '#default_value' => (isset($node->linkout->db_id->db_id)) ? $node->linkout->db_id->db_id : 0
  189. );
  190. $types = module_invoke_all('blast_linkout_info');
  191. $options = array();
  192. foreach ($types as $machine_name => $details) {
  193. $options[$machine_name] = (isset($details['name'])) ? $details['name'] : $machine_name;
  194. }
  195. $linkout_type = (isset($node->linkout->type)) ? $node->linkout->type : 'link';
  196. $linkout_type = (isset($form_state['values'])) ? $form_state['values']['dbxref_linkout_type'] : $linkout_type;
  197. $form['dbxref']['dbxref_linkout_type'] = array(
  198. '#type' => 'radios',
  199. '#title' => 'Link-out Type',
  200. '#description' => 'This determines how the URL to be linked to is formed. <strong>Make
  201. sure the database chosen supports this type of link</strong> (ie: the database
  202. should point to a GBrowse instance if you choose GBrowse here).',
  203. '#options' => $options,
  204. '#default_value' => $linkout_type,
  205. '#ajax' => array(
  206. 'callback' => 'ajax_blast_ui_node_linkout_custom_callback',
  207. 'wrapper' => 'link-outs',
  208. )
  209. );
  210. // Add information about each format to the description.
  211. if ($linkout_type) {
  212. $form['dbxref']['dbxref_linkout_type']['#description'] .= '
  213. <p class="blastdb-extra-info"><strong>'.$types[$linkout_type]['name'].'</strong>: '.$types[$linkout_type]['help'].'</p>';
  214. }
  215. return $form;
  216. }
  217. function blastdb_form_validate($form, $form_state) {
  218. if (!empty($form_state['values']['regex'])) {
  219. // Check that any supplied regex includes //.
  220. if (!preg_match('/\/.*\//', $form_state['values']['regex'])) {
  221. form_set_error('regex', 'Regular Expressions require opening and closing slashes to delinate them. For example, <em>/^(\s+) .*/</em>');
  222. }
  223. // Check that the supplied regex is valid.
  224. elseif (@preg_match($form_state['values']['regex'], NULL) === FALSE) {
  225. form_set_error('regex', 'Regular Expression not valid. See '
  226. . '<a href="http://php.net/manual/en/reference.pcre.pattern.syntax.php" target="_blank">PHP.net Regular '
  227. . 'Expression Documentation</a> for more information.');
  228. }
  229. }
  230. // Check that the supplied db actually contains a URL prefix.
  231. if ($form_state['values']['db_id']) {
  232. $db = tripal_get_db(array('db_id' => $form_state['values']['db_id']));
  233. if (empty($db)) {
  234. form_set_error('db_id', 'The database chosen no longer exists.');
  235. }
  236. if (empty($db->urlprefix)) {
  237. form_set_error('db_id', 'The database choosen does not have a URL prefix '
  238. . 'listed which means a link-out could not be created for BLAST hits. '
  239. . 'Please edit the database '
  240. . l('here', 'admin/tripal/chado/tripal_db/edit/' . $db->db_id,
  241. array('attributes' => array('target' => '_blank')))
  242. . ' to include a URL prefix before continuing'
  243. );
  244. }
  245. }
  246. }
  247. /**
  248. * Implements hook_insert().
  249. */
  250. function blastdb_insert($node) {
  251. // Handle Link-out Rules.
  252. if ($node->dbxref_id_type == 'custom') {
  253. $regex = $node->regex;
  254. }
  255. else {
  256. $regex = $node->dbxref_id_type;
  257. }
  258. if (!$node->dbxref_linkout_type) {
  259. $node->dbxref_linkout_type = 'link';
  260. }
  261. // Actually insert the record.
  262. db_insert('blastdb')->fields(array(
  263. 'nid' => $node->nid,
  264. 'name' => $node->db_name,
  265. 'path' => $node->db_path,
  266. 'dbtype' => $node->db_dbtype,
  267. 'dbxref_id_regex' => $regex,
  268. 'dbxref_db_id' => $node->db_id,
  269. 'dbxref_linkout_type' => $node->dbxref_linkout_type,
  270. ))->execute();
  271. }
  272. /**
  273. * Implements hook_node_insert().
  274. * This function acts on ALL NODES
  275. */
  276. function blast_ui_node_insert($node) {
  277. if ($node->type == 'blastdb') {
  278. $node->title = $node->db_name;
  279. }
  280. }
  281. /**
  282. * Implements hook_update().
  283. */
  284. function blastdb_update($node) {
  285. // Handle Link-out Rules.
  286. if ($node->dbxref_id_type == 'custom') {
  287. $regex = $node->regex;
  288. }
  289. else {
  290. $regex = $node->dbxref_id_type;
  291. }
  292. if (!$node->dbxref_linkout_type) {
  293. $node->dbxref_linkout_type = 'link';
  294. }
  295. // Update the record.
  296. db_update('blastdb')->fields(array(
  297. 'name' => $node->db_name,
  298. 'path' => $node->db_path,
  299. 'dbtype' => $node->db_dbtype,
  300. 'dbxref_id_regex' => $regex,
  301. 'dbxref_db_id' => $node->db_id,
  302. 'dbxref_linkout_type' => $node->dbxref_linkout_type,
  303. ))->condition('nid', $node->nid)->execute();
  304. }
  305. /**
  306. * Implements hook_node_update().
  307. * This function acts on ALL NODES
  308. */
  309. function blast_ui_node_update($node) {
  310. if ($node->type == 'blastdb') {
  311. $node->title = $node->db_name;
  312. }
  313. }
  314. /**
  315. * Implements hook_delete().
  316. */
  317. function blastdb_delete($node) {
  318. db_delete('blastdb')->condition('nid',$node->nid)->execute();
  319. }
  320. /**
  321. * Implements hook_load().
  322. */
  323. function blastdb_load($nodes) {
  324. $sql = "
  325. SELECT nid, name, path, dbtype, dbxref_id_regex, dbxref_db_id,
  326. dbxref_linkout_type
  327. FROM {blastdb}
  328. WHERE nid IN (:nids)";
  329. $result = db_query($sql, array(':nids' => array_keys($nodes)));
  330. foreach ($result as $record) {
  331. // Does this BLAST node have a custom linkout?
  332. // (Is there a better way to determine this?)
  333. $custom_linkout = ($record->dbxref_linkout_type != ''
  334. && $record->dbxref_linkout_type != 'link'
  335. && $record->dbxref_linkout_type != 'gbrowse'
  336. && $record->dbxref_linkout_type != 'jbrowse');
  337. $nodes[$record->nid]->db_name = $record->name;
  338. $nodes[$record->nid]->db_path = $record->path;
  339. $nodes[$record->nid]->title = $record->name;
  340. $nodes[$record->nid]->db_dbtype = $record->dbtype;
  341. // There will be a hit linkout if there is a regex pattern for the id
  342. // and a database (db record) to generate a URL
  343. // OR if a custom linkout has been set, which may or may not require a
  344. // regex and/or database.
  345. if ($record->dbxref_id_regex AND $record->dbxref_db_id || $custom_linkout) {
  346. $nodes[$record->nid]->linkout = new stdClass();
  347. if (preg_match('/\/.*\//', $record->dbxref_id_regex)) {
  348. $nodes[$record->nid]->linkout->regex_type = 'custom';
  349. $nodes[$record->nid]->linkout->regex = $record->dbxref_id_regex;
  350. }
  351. else {
  352. $nodes[$record->nid]->linkout->regex_type = $record->dbxref_id_regex;
  353. $nodes[$record->nid]->linkout->regex = get_blastdb_linkout_regex($nodes[$record->nid]);
  354. }
  355. $nodes[$record->nid]->linkout->db_id = tripal_get_db(array('db_id' => $record->dbxref_db_id));
  356. $nodes[$record->nid]->linkout->none = FALSE;
  357. // Support complex link-outs.
  358. $nodes[$record->nid]->linkout->type = $record->dbxref_linkout_type;
  359. $types = module_invoke_all('blast_linkout_info');
  360. if (isset($types[$record->dbxref_linkout_type])) {
  361. $nodes[$record->nid]->linkout->url_function = $types[$record->dbxref_linkout_type]['process function'];
  362. }
  363. else {
  364. $nodes[$record->nid]->linkout->url_function = '';
  365. tripal_report_error(
  366. 'blast_ui',
  367. TRIPAL_ERROR,
  368. '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.',
  369. array('%type' => $record->dbxref_linkout_type)
  370. );
  371. }
  372. }
  373. else {
  374. // No linkout
  375. $nodes[$record->nid]->linkout = new stdClass();
  376. $nodes[$record->nid]->linkout->regex = '';
  377. $nodes[$record->nid]->linkout->db_id = 0;
  378. $nodes[$record->nid]->linkout->none = TRUE;
  379. }
  380. }
  381. }
  382. /**
  383. * AJAX Callback: Update Node Link-out Regex Textfield.
  384. *
  385. * On BlastDB node form the Link-out (dbxref) options allow for settings of a
  386. * custom regex which should only be enabled when "Custom" is selected. This
  387. * callback refreshes the regex textfield so it can change (ie: become enabled)
  388. * when someone selects custom.
  389. */
  390. function ajax_blast_ui_node_linkout_custom_callback($form, $form_state) {
  391. return $form['dbxref'];
  392. }