tripal_feature.module 20 KB

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