blast_ui.node.inc 13 KB

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