tripal_feature.module 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <?php
  2. /**
  3. * @file
  4. * Basic functionality for the tripal module
  5. */
  6. /**
  7. * @defgroup tripal_feature Feature Module
  8. * @ingroup tripal_modules
  9. * @{
  10. * Integrates the Chado Sequence module with Drupal Nodes & Views
  11. * @}
  12. */
  13. require_once 'api/tripal_feature.DEPRECATED.inc';
  14. require_once 'theme/tripal_feature.theme.inc';
  15. require_once 'includes/tripal_feature.admin.inc';
  16. require_once 'includes/tripal_feature.delete.inc';
  17. require_once 'includes/tripal_feature.chado_node.inc';
  18. /**
  19. * Implements hook_views_api().
  20. *
  21. * Essentially this hook tells drupal that there is views support for
  22. * for this module which then includes tripal_db.views.inc where all the
  23. * views integration code is
  24. *
  25. * @ingroup tripal_feature
  26. */
  27. function tripal_feature_views_api() {
  28. return array(
  29. 'api' => 3.0,
  30. );
  31. }
  32. /**
  33. * Implements hook_help().
  34. *
  35. * Display help and module information
  36. *
  37. * @param
  38. * path which path of the site we're displaying help
  39. * @param
  40. * arg array that holds the current path as would be returned from arg() function
  41. *
  42. * @return
  43. * help text for the path
  44. *
  45. * @ingroup tripal_feature
  46. */
  47. function tripal_feature_help($path, $arg) {
  48. $output = '';
  49. switch ($path) {
  50. case "admin/help#tripal_feature":
  51. $output='<p>' . t("Displays links to nodes created on this date") . '</p>';
  52. break;
  53. }
  54. return $output;
  55. }
  56. /**
  57. * Implements hook_permission().
  58. *
  59. * Set the permission types that the chado module uses. Essentially we
  60. * want permissionis that protect creation, editing and deleting of chado
  61. * data objects
  62. *
  63. * @ingroup tripal_feature
  64. */
  65. function tripal_feature_permission() {
  66. return array(
  67. /*
  68. 'access chado_feature content' => array(
  69. 'title' => t('View Features'),
  70. 'description' => t('Allow users to view feature pages.'),
  71. ),
  72. 'create chado_feature content' => array(
  73. 'title' => t('Create Features'),
  74. 'description' => t('Allow users to create new feature pages.'),
  75. ),
  76. 'delete chado_feature content' => array(
  77. 'title' => t('Delete Features'),
  78. 'description' => t('Allow users to delete feature pages.'),
  79. ),
  80. 'edit chado_feature content' => array(
  81. 'title' => t('Edit Features'),
  82. 'description' => t('Allow users to edit feature pages.'),
  83. ),
  84. 'administer tripal feature' => array(
  85. 'title' => t('Administer Features'),
  86. 'description' => t('Allow users to administer all features.'),
  87. ),
  88. */
  89. );
  90. }
  91. /**
  92. * Implements hook_menu().
  93. *
  94. * Menu items are automatically added for the new node types created
  95. * by this module to the 'Create Content' Navigation menu item. This function
  96. * adds more menu items needed for this module.
  97. *
  98. * @ingroup tripal_feature
  99. */
  100. function tripal_feature_menu() {
  101. $items = array();
  102. // the administative settings menu
  103. $items['find/sequences'] = array(
  104. 'title' => 'Sequence Retrieval',
  105. 'description' => 'Download a file of sequences',
  106. 'page callback' => 'tripal_feature_seq_extract_page',
  107. 'access arguments' => array('access chado_feature content'),
  108. 'type' => MENU_CALLBACK,
  109. );
  110. $items['find/sequences/ajax'] = array(
  111. 'title' => 'Sequence Retrieval',
  112. 'page callback' => 'tripal_feature_seq_extract_form_ahah_update',
  113. 'access arguments' => array('access chado_feature content'),
  114. 'type' => MENU_CALLBACK,
  115. );
  116. // the menu link for addressing any feature (by name, uniquename, synonym)
  117. $items['feature/%'] = array(
  118. 'page callback' => 'tripal_feature_match_features_page',
  119. 'page arguments' => array(1),
  120. 'access arguments' => array('access chado_feature content'),
  121. 'type' => MENU_LOCAL_TASK,
  122. );
  123. // the administative settings menu
  124. /*
  125. $items['admin/tripal/chado/tripal_feature'] = array(
  126. 'title' => 'Features',
  127. 'description' => 'A biological sequence or a section of a biological sequence, or a collection of such sections.',
  128. 'page callback' => 'tripal_feature_admin_feature_view',
  129. 'access arguments' => array('administer tripal feature'),
  130. 'type' => MENU_NORMAL_ITEM,
  131. );
  132. $items['admin/tripal/chado/tripal_feature/delete'] = array(
  133. 'title' => ' Delete',
  134. 'description' => 'Delete multiple features from Chado',
  135. 'page callback' => 'drupal_get_form',
  136. 'page arguments' => array('tripal_feature_delete_form'),
  137. 'access arguments' => array('administer tripal feature'),
  138. 'type' => MENU_LOCAL_TASK,
  139. 'weight' => 2
  140. );
  141. $items['admin/tripal/chado/tripal_feature/sync'] = array(
  142. 'title' => ' Sync',
  143. 'description' => 'Create pages on this site for features stored in Chado',
  144. 'page callback' => 'drupal_get_form',
  145. 'page arguments' => array('chado_node_sync_form', 'tripal_feature', 'chado_feature'),
  146. 'access arguments' => array('administer tripal feature'),
  147. 'type' => MENU_LOCAL_TASK,
  148. 'weight' => 1
  149. );
  150. $items['admin/tripal/chado/tripal_feature/chado_feature_toc'] = array(
  151. 'title' => ' TOC',
  152. 'description' => 'Manage the table of contents for feature nodes.',
  153. 'page callback' => 'drupal_get_form',
  154. 'page arguments' => array('tripal_core_content_type_toc_form', 'chado_feature'),
  155. 'access arguments' => array('administer tripal feature'),
  156. 'type' => MENU_LOCAL_TASK,
  157. 'file' => 'includes/tripal_core.toc.inc',
  158. 'file path' => drupal_get_path('module', 'tripal_core'),
  159. 'weight' => 3
  160. );
  161. $items['admin/tripal/chado/tripal_feature/configuration'] = array(
  162. 'title' => 'Settings',
  163. 'description' => 'Configure the Tripal Feature module.',
  164. 'page callback' => 'drupal_get_form',
  165. 'page arguments' => array('tripal_feature_admin'),
  166. 'access arguments' => array('administer tripal feature'),
  167. 'type' => MENU_LOCAL_TASK,
  168. 'weight' => 5
  169. );
  170. $items['admin/tripal/chado/tripal_feature/help'] = array(
  171. 'title' => 'Help',
  172. 'description' => 'Help with the Tripal Feature module.',
  173. 'page callback' => 'theme',
  174. 'page arguments' => array('tripal_feature_help'),
  175. 'access arguments' => array('administer tripal feature'),
  176. 'type' => MENU_LOCAL_TASK,
  177. 'weight' => 10
  178. );
  179. // Enable admin view
  180. $items['admin/tripal/chado/tripal_feature/views/features/enable'] = array(
  181. 'title' => 'Enable feature Administrative View',
  182. 'page callback' => 'tripal_enable_view',
  183. 'page arguments' => array('tripal_feature_admin_features', 'admin/tripal/chado/tripal_feature'),
  184. 'access arguments' => array('administer tripal feature'),
  185. 'type' => MENU_CALLBACK,
  186. );
  187. */
  188. return $items;
  189. }
  190. /**
  191. * Implements hook_search_biological_data_views().
  192. *
  193. * Adds the described views to the "Search Data" Page created by Tripal Views
  194. */
  195. function tripal_feature_search_biological_data_views() {
  196. return array(
  197. 'tripal_feature_user_features' => array(
  198. 'machine_name' => 'tripal_feature_user_features',
  199. 'human_name' => 'Features',
  200. 'description' => 'A biological sequence or a section of a biological sequence, or a collection of such sections.',
  201. 'link' => 'chado/feature'
  202. ),
  203. );
  204. }
  205. /**
  206. * Implements hook_theme().
  207. *
  208. * We need to let drupal know about our theme functions and their arguments.
  209. * We create theme functions to allow users of the module to customize the
  210. * look and feel of the output generated in this module
  211. *
  212. * @ingroup tripal_feature
  213. */
  214. function tripal_feature_theme($existing, $type, $theme, $path) {
  215. $core_path = drupal_get_path('module', 'tripal_core');
  216. // Feature Node Page Templates.
  217. $items = array(
  218. 'node__chado_feature' => array(
  219. 'template' => 'node--chado-generic',
  220. 'render element' => 'node',
  221. 'base hook' => 'node',
  222. 'path' => "$core_path/theme/templates",
  223. ),
  224. 'tripal_feature_alignments' => array(
  225. 'variables' => array('node' => NULL),
  226. 'template' => 'tripal_feature_alignments',
  227. 'path' => "$path/theme/templates",
  228. ),
  229. 'tripal_feature_analyses' => array(
  230. 'variables' => array('node' => NULL),
  231. 'template' => 'tripal_feature_analyses',
  232. 'path' => "$path/theme/templates",
  233. ),
  234. 'tripal_feature_base' => array(
  235. 'variables' => array('node' => NULL),
  236. 'template' => 'tripal_feature_base',
  237. 'path' => "$path/theme/templates",
  238. ),
  239. 'tripal_feature_sequence' => array(
  240. 'variables' => array('node' => NULL),
  241. 'template' => 'tripal_feature_sequence',
  242. 'path' => "$path/theme/templates",
  243. ),
  244. 'tripal_feature_proteins' => array(
  245. 'variables' => array('node' => NULL),
  246. 'template' => 'tripal_feature_proteins',
  247. 'path' => "$path/theme/templates",
  248. ),
  249. 'tripal_feature_publications' => array(
  250. 'variables' => array('node' => NULL),
  251. 'template' => 'tripal_feature_publications',
  252. 'path' => "$path/theme/templates",
  253. ),
  254. 'tripal_feature_synonyms' => array(
  255. 'variables' => array('node' => NULL),
  256. 'template' => 'tripal_feature_synonyms',
  257. 'path' => "$path/theme/templates",
  258. ),
  259. 'tripal_feature_references' => array(
  260. 'variables' => array('node' => NULL),
  261. 'template' => 'tripal_feature_references',
  262. 'path' => "$path/theme/templates",
  263. ),
  264. 'tripal_feature_properties' => array(
  265. 'variables' => array('node' => NULL),
  266. 'template' => 'tripal_feature_properties',
  267. 'path' => "$path/theme/templates",
  268. ),
  269. 'tripal_feature_terms' => array(
  270. 'variables' => array('node' => NULL),
  271. 'template' => 'tripal_feature_terms',
  272. 'path' => "$path/theme/templates",
  273. ),
  274. 'tripal_feature_relationships' => array(
  275. 'variables' => array('node' => NULL),
  276. 'template' => 'tripal_feature_relationships',
  277. 'path' => "$path/theme/templates",
  278. ),
  279. );
  280. // Feature Node Teaser
  281. $items['tripal_feature_teaser'] = array(
  282. 'variables' => array('node' => NULL),
  283. 'template' => 'tripal_feature_teaser',
  284. 'path' => "$path/theme/templates",
  285. );
  286. // Templates for other node pages.
  287. // Organism Feature Browser.
  288. $items['tripal_organism_feature_browser'] = array(
  289. 'variables' => array('node' => NULL),
  290. 'template' => 'tripal_organism_feature_browser',
  291. 'path' => "$path/theme/templates",
  292. );
  293. $items['tripal_organism_feature_counts'] = array(
  294. 'variables' => array('node' => NULL),
  295. 'template' => 'tripal_organism_feature_counts',
  296. 'path' => "$path/theme/templates",
  297. );
  298. // Administrative Help Template.
  299. $items['tripal_feature_help'] = array(
  300. 'template' => 'tripal_feature_help',
  301. 'variables' => array(NULL),
  302. 'path' => "$path/theme/templates"
  303. );
  304. // Themed Forms
  305. $items['tripal_feature_seq_extract_form'] = array(
  306. 'arguments' => array('form'),
  307. );
  308. // D3 Charts.
  309. // Feature Type/Organism Stacked Bar Chart.
  310. $items['tripal_feature_bar_chart_type_organism_summary'] = array(
  311. 'template' => 'tripal_feature_bar_chart_type_organism_summary',
  312. 'variables' => array(NULL),
  313. 'path' => "$path/theme/templates"
  314. );
  315. return $items;
  316. }
  317. /**
  318. * Implements hook_job_describe_args() in order to describe the various feature jobs
  319. * to the tripal jobs interface.
  320. *
  321. * @ingroup tripal_feature
  322. */
  323. function tripal_feature_job_describe_args($callback, $args) {
  324. $new_args = array();
  325. if ($callback == 'tripal_feature_load_fasta') {
  326. $new_args['FASTA file'] = $args[0];
  327. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $args[1]));
  328. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  329. $new_args['Sequence Type'] = $args[2];
  330. $new_args['Name Match Type'] = $args[14];
  331. $new_args['Name RE'] = $args[4];
  332. $new_args['Unique Name RE'] = $args[5];
  333. // add in the relationship arguments
  334. $new_args['Relationship Type'] = $args[8];
  335. $new_args['Relationship Parent RE'] = $args[9];
  336. $new_args['Relationship Parent Type'] = $args[10];
  337. // add in the database reference arguments
  338. if ($args[7]) {
  339. $db = chado_select_record('db', array('name'), array('db_id' => $args[7]));
  340. }
  341. $new_args['Database Reference'] = $db[0]->name;
  342. $new_args['Accession RE'] = $args[6];
  343. $new_args['Method'] = $args[11];
  344. // add in the analysis
  345. if ($args[13]) {
  346. $analysis = chado_select_record('analysis', array('name'), array('analysis_id' => $args[13]));
  347. }
  348. $new_args['Analysis'] = $analysis[0]->name;
  349. }
  350. if ($callback == 'tripal_feature_delete_features') {
  351. if ($args[0]) {
  352. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  353. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  354. }
  355. else {
  356. $new_args['Organism'] = '';
  357. }
  358. if ($args[1]) {
  359. $analysis = chado_select_record('analysis', array('name'), array('analysis_id' => $args[1]));
  360. $new_args['Analysis'] = $analysis[0]->name;
  361. }
  362. else {
  363. $new_args['Analysis'] = '';
  364. }
  365. $new_args['Sequence Type'] = $args[2];
  366. $new_args['Is Unique Name'] = $args[3] ? 'Yes' : 'No';
  367. $new_args['Features Names'] = $args[4];
  368. }
  369. elseif ($callback == 'tripal_feature_load_gff3') {
  370. $new_args['GFF File'] = $args[0];
  371. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $args[1]));
  372. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  373. $analysis = chado_select_record('analysis', array('name'), array('analysis_id' => $args[2]));
  374. $new_args['Analysis'] = $analysis[0]->name;
  375. $new_args['Use a Transaction'] = ($args[7] == 1) ? "Yes" : "No";
  376. $new_args['Import only new features'] = ($args[3] == 1) ? "Yes" : "No";
  377. $new_args['Import all and update'] = ($args[4] == 1) ? "Yes" : "No";
  378. $new_args['Import all and replace'] = ($args[5] == 1) ? "Yes" : "No";
  379. $new_args['Delete features'] = ($args[6] == 1) ? "Yes" : "No";
  380. if ($args[8]) {
  381. $target_organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $args[8]));
  382. $new_args['Target organism'] = $target_organism[0]->genus . " " . $target_organism[0]->species;
  383. }
  384. else {
  385. $new_args['Target organism'] = '';
  386. }
  387. $new_args['Target type'] = $args[9];
  388. $new_args['Create target'] = ($args[10] == 1) ? "Yes" : "No";
  389. $new_args['Starting line'] = $args[11];
  390. $new_args['Landmark Type'] = $args[12];
  391. $new_args['Alternate ID attribute'] = $args[13];
  392. $new_args['Create Organism'] = ($args[14] == 1) ? "Yes" : "No";
  393. }
  394. return $new_args;
  395. }
  396. /**
  397. * Implements hook_coder_ignore().
  398. *
  399. * Defines the path to the file (tripal_core.coder_ignores.txt) where ignore rules for
  400. * coder are stored
  401. *
  402. * @ingroup tripal_feature
  403. */
  404. function tripal_feature_coder_ignore() {
  405. return array(
  406. 'path' => drupal_get_path('module', 'tripal_feature'),
  407. 'line prefix' => drupal_get_path('module', 'tripal_feature'),
  408. );
  409. }
  410. /*
  411. * Uses the value provided in the $id argument to find all features that match
  412. * that ID by name, featurename or synonym. If it matches uniquenly to a single
  413. * feature it will redirect to that feature page, otherwise, a list of matching
  414. * features is shown.
  415. *
  416. * @ingroup tripal_feature
  417. */
  418. function tripal_feature_match_features_page($id) {
  419. // first check to see if the URL (e.g. /feature/$id) is already
  420. // assigned to a node. If so, then just go there. Otherwise,
  421. // try to find the feature.
  422. $sql = "
  423. SELECT source
  424. FROM {url_alias}
  425. WHERE alias = :alias
  426. ";
  427. $match = db_query($sql, array(':alias' => "feature/$id"))->fetchObject();
  428. if ($match) {
  429. drupal_goto($match->source);
  430. return;
  431. }
  432. $sql = "
  433. SELECT
  434. F.name, F.uniquename, F.feature_id,
  435. O.genus, O.species, O.organism_id,
  436. CVT.cvterm_id, CVT.name as type_name,
  437. CF.nid,
  438. array_agg(S.name) as synonyms
  439. FROM {feature} F
  440. INNER JOIN {organism} O on F.organism_id = O.organism_id
  441. INNER JOIN {cvterm} CVT on CVT.cvterm_id = F.type_id
  442. LEFT JOIN {feature_synonym} FS on FS.feature_id = F.feature_id
  443. LEFT JOIN {synonym} S on S.synonym_id = FS.synonym_id
  444. INNER JOIN public.chado_feature CF on CF.feature_id = F.feature_id
  445. WHERE
  446. F.uniquename = :uname or
  447. F.name = :fname or
  448. S.name = :sname
  449. GROUP BY F.name, F.uniquename, F.feature_id, O.genus, O.species,
  450. O.organism_id, CVT.cvterm_id, CVT.name, CF.nid
  451. ";
  452. $args = array(':uname' => $id, ':fname' => $id, ':sname' => $id);
  453. $results = chado_query($sql, $args);
  454. $num_matches = 0;
  455. // iterate through the matches and build the table for showing matches
  456. $header = array('Uniquename', 'Name', 'Type', 'Species', 'Synonyms');
  457. $rows = array();
  458. $curr_match;
  459. while ($match = $results->fetchObject()) {
  460. $curr_match = $match;
  461. $synonyms = $match->synonyms;
  462. $synonyms = preg_replace('/[\"\{\}]/', '', $synonyms);
  463. $rows[] = array(
  464. $match->uniquename,
  465. "<a href=\"" . url("node/" . $match->nid) . "\">" . $match->name . "</a>",
  466. $match->type_name,
  467. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  468. $synonyms,
  469. );
  470. $num_matches++;
  471. }
  472. // if we have more than one match then generate the table, otherwise, redirect
  473. // to the matched feature
  474. if ($num_matches == 1) {
  475. drupal_goto("node/" . $curr_match->nid);
  476. }
  477. if ($num_matches == 0) {
  478. return "<p>No features matched the given name '$id'</p>";
  479. }
  480. $table_attrs = array('class' => 'tripal-data-table');
  481. $output = "<p>The following features match the name '$id'.</p>";
  482. $output .= theme_table($header, $rows, $table_attrs, $caption);
  483. return $output;
  484. }
  485. /**
  486. * Implementation of hook_form_alter()
  487. *
  488. * @param $form
  489. * @param $form_state
  490. * @param $form_id
  491. *
  492. * @ingroup tripal_feature
  493. */
  494. function tripal_feature_form_alter(&$form, &$form_state, $form_id) {
  495. if ($form_id == "tripal_feature_seq_extract_form") {
  496. // updating the form through the ahah callback sets the action of
  497. // the form to the ahah callback URL. We need to set it back
  498. // to the normal form URL
  499. $form['#action'] = url("find/sequences");
  500. }
  501. // turn off preview button for insert/updates
  502. if ($form_id == "chado_feature_node_form") {
  503. $form['actions']['preview']['#access'] = FALSE;
  504. //remove the body field
  505. unset($form['body']);
  506. }
  507. }
  508. /**
  509. * Implements hook_exclude_field_from_<tablename>_by_default()
  510. *
  511. * This hooks allows fields from a specified table that match a specified criteria to be excluded by
  512. * default from any table when chado_generate_var() is called. Keep in mind that if
  513. * fields are excluded by default they can always be expanded at a later date using
  514. * chado_expand_var().
  515. *
  516. * Criteria are php strings that evaluate to either TRUE or FALSE. These strings are evaluated using
  517. * drupal_eval() which suppresses syntax errors and throws watchdog entries of type php. There are
  518. * also watchdog entries of type tripal_core stating the exact criteria evaluated. Criteria can
  519. * contain the following tokens:
  520. * - <field_name>
  521. * Replaced by the name of the field to be excluded
  522. * - <field_value>
  523. * Replaced by the value of the field in the current record
  524. * Also keep in mind that if your criteria doesn't contain the <field_value> token then it will be
  525. * evaluated before the query is executed and if the field is excluded it won't be included in the
  526. * query.
  527. *
  528. * @return
  529. * An array of field => criteria where the type is excluded if the criteria evaluates to TRUE
  530. *
  531. * @ingroup tripal_feature
  532. */
  533. function tripal_feature_exclude_field_from_feature_by_default() {
  534. return array('residues' => 'TRUE');
  535. }