blast_ui.node.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. asort($db_options);
  156. $form['dbxref']['db_id'] = array(
  157. '#type' => 'select',
  158. '#title' => 'External Database',
  159. '#description' => 'The external database you would like to link-out to. '
  160. . 'Note that this list includes all Tripal Databases and if the database '
  161. . 'you would like to link-out to is not included you can add it through '
  162. . l('Administration > Tripal > Chado Modules > Databases','admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  163. . '.',
  164. '#options' => $db_options,
  165. '#default_value' => (isset($node->linkout->db_id->db_id)) ? $node->linkout->db_id->db_id : 0
  166. );
  167. $types = module_invoke_all('blast_linkout_info');
  168. $options = array();
  169. foreach ($types as $machine_name => $details) {
  170. $options[$machine_name] = (isset($details['name'])) ? $details['name'] : $machine_name;
  171. }
  172. $form['dbxref']['dbxref_linkout_type'] = array(
  173. '#type' => 'select',
  174. '#title' => 'Link-out Type',
  175. '#description' => 'This determines how the URL to be linked to is formed. '
  176. . 'NOTE: this is very dependant on the External Database '
  177. . 'chosen above since it needs to be able to support the '
  178. . 'type of linking choosen. For example, only External '
  179. . 'Databases which reference a GBrowse instance can use the '
  180. . 'GBrowse link type. If a link requires customized code, '
  181. . 'select "custom" and add the code to the file '
  182. . 'includes/blast_ui.custom/inc.',
  183. '#options' => $options,
  184. '#default_value' => (isset($node->linkout->type)) ? $node->linkout->type : 'link'
  185. );
  186. return $form;
  187. }
  188. function blastdb_form_validate($form, $form_state) {
  189. if (!empty($form_state['values']['regex'])) {
  190. // Check that any supplied regex includes //.
  191. if (!preg_match('/\/.*\//', $form_state['values']['regex'])) {
  192. form_set_error('regex', 'Regular Expressions require opening and closing slashes to delinate them. For example, <em>/^(\s+) .*/</em>');
  193. }
  194. // Check that the supplied regex is valid.
  195. elseif (@preg_match($form_state['values']['regex'], NULL) === FALSE) {
  196. form_set_error('regex', 'Regular Expression not valid. See '
  197. . '<a href="http://php.net/manual/en/reference.pcre.pattern.syntax.php" target="_blank">PHP.net Regular '
  198. . 'Expression Documentation</a> for more information.');
  199. }
  200. }
  201. // Check that the supplied db actually contains a URL prefix.
  202. if ($form_state['values']['db_id']) {
  203. $db = tripal_get_db(array('db_id' => $form_state['values']['db_id']));
  204. if (empty($db)) {
  205. form_set_error('db_id', 'The database chosen no longer exists.');
  206. }
  207. if (empty($db->urlprefix)) {
  208. form_set_error('db_id', 'The database choosen does not have a URL prefix '
  209. . 'listed which means a link-out could not be created for BLAST hits. '
  210. . 'Please edit the database '
  211. . l('here', 'admin/tripal/chado/tripal_db/edit/' . $db->db_id,
  212. array('attributes' => array('target' => '_blank')))
  213. . ' to include a URL prefix before continuing'
  214. );
  215. }
  216. }
  217. }
  218. /**
  219. * Implements hook_insert().
  220. */
  221. function blastdb_insert($node) {
  222. // Handle Link-out Rules.
  223. if ($node->dbxref_id_type == 'custom') {
  224. $regex = $node->regex;
  225. }
  226. else {
  227. $regex = $node->dbxref_id_type;
  228. }
  229. if (!$node->dbxref_linkout_type) {
  230. $node->dbxref_linkout_type = 'link';
  231. }
  232. // Actually insert the record.
  233. db_insert('blastdb')->fields(array(
  234. 'nid' => $node->nid,
  235. 'name' => $node->db_name,
  236. 'path' => $node->db_path,
  237. 'dbtype' => $node->db_dbtype,
  238. 'dbxref_id_regex' => $regex,
  239. 'dbxref_db_id' => $node->db_id,
  240. 'dbxref_linkout_type' => $node->dbxref_linkout_type,
  241. ))->execute();
  242. }
  243. /**
  244. * Implements hook_node_insert().
  245. * This function acts on ALL NODES
  246. */
  247. function blast_ui_node_insert($node) {
  248. if ($node->type == 'blastdb') {
  249. $node->title = $node->db_name;
  250. }
  251. }
  252. /**
  253. * Implements hook_update().
  254. */
  255. function blastdb_update($node) {
  256. // Handle Link-out Rules.
  257. if ($node->dbxref_id_type == 'custom') {
  258. $regex = $node->regex;
  259. }
  260. else {
  261. $regex = $node->dbxref_id_type;
  262. }
  263. if (!$node->dbxref_linkout_type) {
  264. $node->dbxref_linkout_type = 'link';
  265. }
  266. // Update the record.
  267. db_update('blastdb')->fields(array(
  268. 'name' => $node->db_name,
  269. 'path' => $node->db_path,
  270. 'dbtype' => $node->db_dbtype,
  271. 'dbxref_id_regex' => $regex,
  272. 'dbxref_db_id' => $node->db_id,
  273. 'dbxref_linkout_type' => $node->dbxref_linkout_type,
  274. ))->condition('nid', $node->nid)->execute();
  275. }
  276. /**
  277. * Implements hook_node_update().
  278. * This function acts on ALL NODES
  279. */
  280. function blast_ui_node_update($node) {
  281. if ($node->type == 'blastdb') {
  282. $node->title = $node->db_name;
  283. }
  284. }
  285. /**
  286. * Implements hook_delete().
  287. */
  288. function blastdb_delete($node) {
  289. db_delete('blastdb')->condition('nid',$node->nid)->execute();
  290. }
  291. /**
  292. * Implements hook_load() .
  293. */
  294. function blastdb_load($nodes) {
  295. $sql = "
  296. SELECT nid, name, path, dbtype, dbxref_id_regex, dbxref_db_id,
  297. dbxref_linkout_type
  298. FROM {blastdb}
  299. WHERE nid IN (:nids)";
  300. $result = db_query($sql, array(':nids' => array_keys($nodes)));
  301. foreach ($result as $record) {
  302. $nodes[$record->nid]->db_name = $record->name;
  303. $nodes[$record->nid]->db_path = $record->path;
  304. $nodes[$record->nid]->title = $record->name;
  305. $nodes[$record->nid]->db_dbtype = $record->dbtype;
  306. if ($record->dbxref_id_regex) {
  307. $nodes[$record->nid]->linkout = new stdClass();
  308. if (preg_match('/\/.*\//', $record->dbxref_id_regex)) {
  309. $nodes[$record->nid]->linkout->regex_type = 'custom';
  310. $nodes[$record->nid]->linkout->regex = $record->dbxref_id_regex;
  311. }
  312. else {
  313. $nodes[$record->nid]->linkout->regex_type = $record->dbxref_id_regex;
  314. $nodes[$record->nid]->linkout->regex = get_blastdb_linkout_regex($nodes[$record->nid]);
  315. }
  316. $nodes[$record->nid]->linkout->db_id = tripal_get_db(array('db_id' => $record->dbxref_db_id));
  317. $nodes[$record->nid]->linkout->none = FALSE;
  318. // Support complex link-outs.
  319. $nodes[$record->nid]->linkout->type = $record->dbxref_linkout_type;
  320. $types = module_invoke_all('blast_linkout_info');
  321. if (isset($types[$record->dbxref_linkout_type])) {
  322. $nodes[$record->nid]->linkout->url_function = $types[$record->dbxref_linkout_type]['process function'];
  323. }
  324. else {
  325. $nodes[$record->nid]->linkout->url_function = '';
  326. tripal_report_error(
  327. 'blast_ui',
  328. TRIPAL_ERROR,
  329. '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.',
  330. array('%type' => $record->dbxref_linkout_type)
  331. );
  332. }
  333. }
  334. else {
  335. $nodes[$record->nid]->linkout = new stdClass();
  336. $nodes[$record->nid]->linkout->regex = '';
  337. $nodes[$record->nid]->linkout->db_id = 0;
  338. $nodes[$record->nid]->linkout->none = TRUE;
  339. }
  340. }
  341. }
  342. /**
  343. * AJAX Callback: Update Node Link-out Regex Textfield.
  344. *
  345. * On BlastDB node form the Link-out (dbxref) options allow for settings of a
  346. * custom regex which should only be enabled when "Custom" is selected. This
  347. * callback refreshes the regex textfield so it can change (ie: become enabled)
  348. * when someone selects custom.
  349. */
  350. function ajax_blast_ui_node_linkout_custom_callback($form, $form_state) {
  351. return $form['dbxref']['regex'];
  352. }