blast_ui.node.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. );
  98. $types = module_invoke_all('blast_linkout_info');
  99. $options = array();
  100. foreach ($types as $machine_name => $details) {
  101. $options[$machine_name] = (isset($details['name'])) ? $details['name'] : $machine_name;
  102. }
  103. $linkout_type = (isset($node->linkout->type)) ? $node->linkout->type : 'none';
  104. $linkout_type = (isset($form_state['values'])) ? $form_state['values']['dbxref_linkout_type'] : $linkout_type;
  105. $form['dbxref']['dbxref_linkout_type'] = array(
  106. '#type' => 'radios',
  107. '#title' => 'Link-out Type',
  108. '#description' => 'This determines how the URL to be linked to is formed. <strong>Make
  109. sure the database chosen supports this type of link</strong> (ie: the database
  110. should point to a GBrowse instance if you choose GBrowse here).',
  111. '#options' => $options,
  112. '#default_value' => $linkout_type,
  113. '#ajax' => array(
  114. 'callback' => 'ajax_blast_ui_node_linkout_custom_callback',
  115. 'wrapper' => 'link-outs',
  116. )
  117. );
  118. $form['dbxref']['details'] = array(
  119. '#prefix' => '<div id="link-outs">',
  120. '#suffix' => '</div>',
  121. );
  122. // Add information about each format to the description.
  123. if ($linkout_type) {
  124. $form['dbxref']['details']['dbxref_linkout_description'] = array(
  125. '#type' => 'item',
  126. '#markup' => '<p class="blastdb-extra-info">'
  127. .'<strong>'.$types[$linkout_type]['name'].'</strong>: '
  128. .$types[$linkout_type]['help']
  129. .'</p>',
  130. '#prefix' => '<div id="link-outs">',
  131. '#suffix' => '</div>',
  132. );
  133. }
  134. if ($types[$linkout_type]['require_regex']) {
  135. $regex = array(
  136. 'default' => array(
  137. 'title' => 'Generic',
  138. 'help' => 'A single word followed by a free-text definition. '
  139. . 'The first word contains only alphanumeric characters and optionally '
  140. . 'underscores and will be used as the ID of the sequence.'
  141. ),
  142. 'genbank' => array(
  143. 'title' => 'NCBI GenBank',
  144. 'help' => 'Follows the same format as the first option '
  145. . 'except that the first "word" is of the following form: '
  146. . 'gb|accession|locus. The accession will be used as the ID of the sequence.'
  147. ),
  148. 'embl' => array(
  149. 'title' => 'EMBL Data Library',
  150. 'help' => 'Follows the same format as the first option '
  151. . 'except that the first "word" is of the following form: '
  152. . 'emb|accession|locus. The accession will be used as the ID of the sequence.'
  153. ),
  154. 'swissprot' => array(
  155. 'title' => 'SWISS-PROT',
  156. 'help' => 'Follows the same format as the first option '
  157. . 'except that the first "word" is of the following form: '
  158. . 'sp|accession|entry name. The accession will be used as the ID of the sequence.'
  159. ),
  160. 'custom' => array(
  161. 'title' => 'Custom Format',
  162. 'help' => 'Allows you to use a regular expression (define one below) to '
  163. . 'extract a specifc portion of the FASTA header to be used as the ID.'
  164. ),
  165. );
  166. $regex_type = (isset($node->linkout->regex_type)) ? $node->linkout->regex_type : 'default';
  167. $regex_type = (isset($form_state['values'])) ? $form_state['values']['dbxref_id_type'] : $regex_type;
  168. $form['dbxref']['details']['dbxref_id_type'] = array(
  169. '#type' => 'radios',
  170. '#title' => 'FASTA header format',
  171. '#description' => 'Choose the format that matches the format of the FASTA '
  172. . 'headers in this BLAST database or choose custom to define your own '
  173. . 'using regular expressions. This ID will be used to create the URL for the link-out.',
  174. '#options' => array(
  175. 'default' => '<span title="' . $regex['default']['help'] . '">' . $regex['default']['title'] . '</span>',
  176. 'genbank' => '<span title="' . $regex['genbank']['help'] . '">' . $regex['genbank']['title'] . '</span>',
  177. 'embl' => '<span title="' . $regex['embl']['help'] . '">' . $regex['embl']['title'] . '</span>',
  178. 'swissprot' => '<span title="' . $regex['swissprot']['help'] . '">' . $regex['swissprot']['title'] . '</span>',
  179. 'custom' => '<span title="' . $regex['custom']['help'] . '">' . $regex['custom']['title'] . '</span>',
  180. ),
  181. '#default_value' => $regex_type,
  182. '#required' => TRUE,
  183. '#ajax' => array(
  184. 'callback' => 'ajax_blast_ui_node_linkout_custom_callback',
  185. 'wrapper' => 'link-outs',
  186. )
  187. );
  188. // Add information about each format to the description.
  189. if ($regex_type) {
  190. $form['dbxref']['details']['dbxref_id_type']['#description'] .= '
  191. <p class="blastdb-extra-info"><strong>'.$regex[$regex_type]['title'].'</strong>: '.$regex[$regex_type]['help'].'</p>';
  192. }
  193. if ($regex_type == 'custom') {
  194. $form['dbxref']['details']['regex'] = array(
  195. '#type' => 'textfield',
  196. '#title' => 'Regular Expression',
  197. '#description' => t('A PHP Regular expression with curved brackets '
  198. . 'surrounding the ID that should be used in the URL to provide a link-'
  199. . 'out to additional information. See <a href="@url" target="_blank">PHP.net Regular '
  200. . 'Expression Documentation</a> for more information. <strong>Be sure '
  201. . 'to include the opening and closing slashes</strong>. This is only '
  202. . 'available if custom was choosen for the FASTA header format above.',
  203. array('@url' => 'http://php.net/manual/en/reference.pcre.pattern.syntax.php')),
  204. '#required' => TRUE,
  205. '#default_value' => (isset($node->linkout->regex)) ? $node->linkout->regex : ''
  206. );
  207. }
  208. }
  209. if ($types[$linkout_type]['require_db']) {
  210. $db_options = tripal_get_db_select_options();
  211. $db_options[0] = '';
  212. asort($db_options);
  213. $form['dbxref']['details']['db_id'] = array(
  214. '#type' => 'select',
  215. '#title' => 'External Database',
  216. '#description' => 'The external database you would like to link-out to. '
  217. . 'Note that this list includes all Tripal Databases and if the database '
  218. . 'you would like to link-out to is not included you can add it through '
  219. . l('Administration > Tripal > Data Loaders > Chado Databases','admin/tripal/loaders/chado_db/add', array('attributes' => array('target' => '_blank')))
  220. . '.',
  221. '#options' => $db_options,
  222. '#required' => TRUE,
  223. '#default_value' => (isset($node->linkout->db_id->db_id)) ? $node->linkout->db_id->db_id : 0
  224. );
  225. }
  226. // CViTjs settings, if enabled
  227. if (variable_get('blast_ui_cvitjs_enabled', false)) {
  228. $form['cvitjs'] = array(
  229. '#type' => 'fieldset',
  230. '#title' => 'Whole Genome Visualization',
  231. '#description' => 'Settings for the display of BLAST hits on an entire genome assembly using CViTjs.',
  232. '#prefix' => '<div id="cvitjs-settings">',
  233. '#suffix' => '</div>',
  234. );
  235. $form['cvitjs']['cvitjs_enabled'] = array(
  236. '#type' => 'checkbox',
  237. '#title' => t('Show BLAST hits on the genome in the results page.'),
  238. '#description' => t('Uses CViTjs to display BLAST hits on the entire genome'),
  239. '#default_value' => (isset($node->cvitjs_enabled)) ? $node->cvitjs_enabled : false,
  240. );
  241. $cvitjs_msg_class = 'blastdb-extra-info';
  242. $cvitjs_msg = 'Target Genome Configuration should be under <strong>[data.'.$node->db_name.']</strong> in the main cvit.conf.';
  243. $conf_section = blast_ui_get_cvit_conf_text('data.'.$node->db_name);
  244. if (!$conf_section) {
  245. $cvitjs_msg_class .= ' messages warning';
  246. $cvitjs_msg .= '<br /><br />There is no section for this genome target defined in the CViTjs
  247. configuration file. <strong>No genome visualization will be shown until you define a
  248. configuration section, "[data.'.$form_state['values']['db_name'].']", at '
  249. .l('Admin > Tripal > Extensions > Tripal BLAST > BLAST UI', 'admin/tripal/extension/tripal_blast')
  250. .'</strong>.';
  251. }
  252. else {
  253. $cvitjs_msg .= '<br /><br /><strong>Current Configuration:</strong><pre>'.$conf_section.'</pre>';
  254. }
  255. $form['cvitjs']['cvitjs_enabled']['#description'] .= '<div class="'.$cvitjs_msg_class.'">'.$cvitjs_msg.'</p>';
  256. }
  257. return $form;
  258. }
  259. function blastdb_form_validate($form, $form_state) {
  260. if (isset($form_state['values']['regex']) AND !empty($form_state['values']['regex'])) {
  261. // Check that any supplied regex includes //.
  262. if (!preg_match('/\/.*\//', $form_state['values']['regex'])) {
  263. form_set_error('regex', 'Regular Expressions require opening and closing slashes to delinate them. For example, <em>/^(\s+) .*/</em>');
  264. }
  265. // Check that the supplied regex is valid.
  266. elseif (@preg_match($form_state['values']['regex'], NULL) === FALSE) {
  267. form_set_error('regex', 'Regular Expression not valid. See '
  268. . '<a href="http://php.net/manual/en/reference.pcre.pattern.syntax.php" target="_blank">PHP.net Regular '
  269. . 'Expression Documentation</a> for more information.');
  270. }
  271. }
  272. // Check that the supplied db actually contains a URL prefix.
  273. if (isset($form_state['values']['db_id'])) {
  274. $db = tripal_get_db(array('db_id' => $form_state['values']['db_id']));
  275. if (empty($db)) {
  276. form_set_error('db_id', 'The database chosen no longer exists.');
  277. }
  278. if (empty($db->urlprefix)) {
  279. form_set_error('db_id', 'The database choosen does not have a URL prefix '
  280. . 'listed which means a link-out could not be created for BLAST hits. '
  281. . 'Please edit the database '
  282. . l('here', 'admin/tripal/chado/tripal_db/edit/' . $db->db_id,
  283. array('attributes' => array('target' => '_blank')))
  284. . ' to include a URL prefix before continuing'
  285. );
  286. }
  287. }
  288. // Check that there is a cvitjs section for the current
  289. if (isset($form_state['values']['cvitjs_enabled']) AND $form_state['values']['cvitjs_enabled']) {
  290. $conf_section = blast_ui_get_cvit_conf_text('data.'.$form_state['values']['db_name']);
  291. if (!$conf_section) {
  292. drupal_set_message('There is no section for this genome target defined in the CViTjs
  293. configuration file. <strong>No genome visualization will be shown until you define a
  294. configuration section, "[data.'.$form_state['values']['db_name'].']", at '
  295. .l('Admin > Tripal > Extensions > Tripal BLAST > BLAST UI', 'admin/tripal/extension/tripal_blast')
  296. .'</strong>.',
  297. 'warning');
  298. }
  299. }
  300. }
  301. /**
  302. * Implements hook_insert().
  303. */
  304. function blastdb_insert($node) {
  305. // Handle Link-out Rules.
  306. $regex = '';
  307. if (isset($node->dbxref_id_type)) {
  308. if ($node->dbxref_id_type == 'custom') {
  309. $regex = $node->regex;
  310. }
  311. else {
  312. $regex = $node->dbxref_id_type;
  313. }
  314. }
  315. $db_id = 0;
  316. if (isset($node->db_id)) {
  317. $db_id = $node->db_id;
  318. }
  319. if (!$node->dbxref_linkout_type) {
  320. $node->dbxref_linkout_type = 'none';
  321. }
  322. if (!isset($node->cvitjs_enabled)) {
  323. $node->cvitjs_enabled = 0;
  324. }
  325. // Actually insert the record.
  326. db_insert('blastdb')->fields(array(
  327. 'nid' => $node->nid,
  328. 'name' => $node->db_name,
  329. 'path' => trim($node->db_path),
  330. 'dbtype' => $node->db_dbtype,
  331. 'dbxref_id_regex' => $regex,
  332. 'dbxref_db_id' => $db_id,
  333. 'dbxref_linkout_type' => $node->dbxref_linkout_type,
  334. 'cvitjs_enabled' => $node->cvitjs_enabled,
  335. ))->execute();
  336. }
  337. /**
  338. * Implements hook_node_insert().
  339. * This function acts on ALL NODES
  340. */
  341. function blast_ui_node_insert($node) {
  342. if ($node->type == 'blastdb') {
  343. $node->title = $node->db_name;
  344. }
  345. }
  346. /**
  347. * Implements hook_update().
  348. */
  349. function blastdb_update($node) {
  350. // Handle Link-out Rules.
  351. $regex = '';
  352. if (isset($node->dbxref_id_type)) {
  353. if ($node->dbxref_id_type == 'custom') {
  354. $regex = $node->regex;
  355. }
  356. else {
  357. $regex = $node->dbxref_id_type;
  358. }
  359. }
  360. $db_id = 0;
  361. if (isset($node->db_id)) {
  362. $db_id = $node->db_id;
  363. }
  364. if (!$node->cvitjs_enabled) {
  365. $node->cvitjs_enabled = 0;
  366. }
  367. if (!$node->dbxref_linkout_type) {
  368. $node->dbxref_linkout_type = 'none';
  369. }
  370. // Update the record.
  371. db_update('blastdb')->fields(array(
  372. 'name' => $node->db_name,
  373. 'path' => $node->db_path,
  374. 'dbtype' => $node->db_dbtype,
  375. 'dbxref_id_regex' => $regex,
  376. 'dbxref_db_id' => $db_id,
  377. 'dbxref_linkout_type' => $node->dbxref_linkout_type,
  378. 'cvitjs_enabled' => $node->cvitjs_enabled,
  379. ))->condition('nid', $node->nid)->execute();
  380. }
  381. /**
  382. * Implements hook_node_update().
  383. * This function acts on ALL NODES
  384. */
  385. function blast_ui_node_update($node) {
  386. if ($node->type == 'blastdb') {
  387. $node->title = $node->db_name;
  388. }
  389. }
  390. /**
  391. * Implements hook_delete().
  392. */
  393. function blastdb_delete($node) {
  394. db_delete('blastdb')->condition('nid',$node->nid)->execute();
  395. }
  396. /**
  397. * Implements hook_load().
  398. */
  399. function blastdb_load($nodes) {
  400. $sql = "
  401. SELECT nid, name, path, dbtype, dbxref_id_regex, dbxref_db_id,
  402. dbxref_linkout_type, cvitjs_enabled
  403. FROM {blastdb}
  404. WHERE nid IN (:nids)";
  405. $result = db_query($sql, array(':nids' => array_keys($nodes)));
  406. foreach ($result as $record) {
  407. // Add basic blast node information.
  408. $nodes[$record->nid]->db_name = $record->name;
  409. $nodes[$record->nid]->db_path = $record->path;
  410. $nodes[$record->nid]->title = $record->name;
  411. $nodes[$record->nid]->db_dbtype = $record->dbtype;
  412. // CViTjs status
  413. $nodes[$record->nid]->cvitjs_enabled = $record->cvitjs_enabled;
  414. // Determine the type of link-out chosen.
  415. $types = module_invoke_all('blast_linkout_info');
  416. $type = NULL;
  417. if (isset($types[ $record->dbxref_linkout_type ])) {
  418. $type = $types[ $record->dbxref_linkout_type ];
  419. }
  420. else {
  421. tripal_report_error(
  422. 'blast_ui',
  423. TRIPAL_ERROR,
  424. '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.',
  425. array('%type' => $record->dbxref_linkout_type)
  426. );
  427. }
  428. // Now determine if this node meets the requirements for the linkout
  429. // chosen before adding linkout information.
  430. $add_linkout = TRUE;
  431. if (!$type OR $record->dbxref_linkout_type == 'none') {
  432. $add_linkout = FALSE;
  433. }
  434. else {
  435. if ($type['require_regex'] AND !$record->dbxref_id_regex) {
  436. $add_linkout = FALSE;
  437. }
  438. if ($type['require_db'] AND !$record->dbxref_db_id) {
  439. $add_linkout = FALSE;
  440. }
  441. }
  442. // Add information related to link-outs to the node.
  443. if ($add_linkout) {
  444. $nodes[$record->nid]->linkout = new stdClass();
  445. // If the link-out type requires a regex then provide one.
  446. if ($type['require_regex']) {
  447. if (preg_match('/\/.*\//', $record->dbxref_id_regex)) {
  448. $nodes[$record->nid]->linkout->regex_type = 'custom';
  449. $nodes[$record->nid]->linkout->regex = $record->dbxref_id_regex;
  450. }
  451. else {
  452. $nodes[$record->nid]->linkout->regex_type = $record->dbxref_id_regex;
  453. $nodes[$record->nid]->linkout->regex = get_blastdb_linkout_regex($nodes[$record->nid]);
  454. }
  455. }
  456. else {
  457. $nodes[$record->nid]->linkout->regex_type = 'none';
  458. $nodes[$record->nid]->linkout->regex = NULL;
  459. }
  460. // If the link-out type requires a db then provide one.
  461. if (isset($type['require_db'])) {
  462. $nodes[$record->nid]->linkout->db_id = tripal_get_db(array('db_id' => $record->dbxref_db_id));
  463. }
  464. else {
  465. $nodes[$record->nid]->linkout->db_id = NULL;
  466. }
  467. $nodes[$record->nid]->linkout->none = FALSE;
  468. // Support complex link-outs.
  469. $nodes[$record->nid]->linkout->type = $record->dbxref_linkout_type;
  470. $nodes[$record->nid]->linkout->url_function = $type['process function'];
  471. }
  472. // If there is no linkout then provide some defaults.
  473. else {
  474. $nodes[$record->nid]->linkout = new stdClass();
  475. $nodes[$record->nid]->linkout->regex = '';
  476. $nodes[$record->nid]->linkout->db_id = 0;
  477. $nodes[$record->nid]->linkout->none = TRUE;
  478. }
  479. }
  480. }
  481. /**
  482. * AJAX Callback: Update Node Link-out Regex Textfield.
  483. *
  484. * On BlastDB node form the Link-out (dbxref) options allow for settings of a
  485. * custom regex which should only be enabled when "Custom" is selected. This
  486. * callback refreshes the regex textfield so it can change (ie: become enabled)
  487. * when someone selects custom.
  488. */
  489. function ajax_blast_ui_node_linkout_custom_callback($form, $form_state) {
  490. return $form['dbxref']['details'];
  491. }