tripal_feature.module 19 KB

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