tripal_analysis.module 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. /**
  3. * @file
  4. * Contains all the main hook implementations for the tripal_analysis module
  5. *
  6. * @defgroup tripal_analysis Analysis Module
  7. * @ingroup tripal_modules
  8. * @{
  9. * Provides functions for managing chado analysis' including creating details pages for each one
  10. *
  11. * @}
  12. *
  13. *
  14. */
  15. require_once 'api/tripal_analysis.api.inc';
  16. require_once 'includes/tripal_analysis_privacy.inc';
  17. require_once 'includes/tripal_analysis.admin.inc';
  18. require_once 'includes/tripal_analysis.form.inc';
  19. require_once 'includes/tripal_analysis.sync.inc';
  20. require_once "api/tripal_analysis.schema.api.inc";
  21. /**
  22. * Add tripal javascript to page headers
  23. *
  24. * @ingroup tripal_analysis
  25. */
  26. function tripal_analysis_init() {
  27. drupal_add_js(drupal_get_path('module', 'tripal_analysis') . '/theme/js/tripal_analysis.js');
  28. drupal_add_css(drupal_get_path('module', 'tripal_analysis') . '/theme/css/tripal_analysis.css');
  29. }
  30. /**
  31. * Provide information to drupal about the node types that we're creating
  32. * in this module
  33. *
  34. * @ingroup tripal_analysis
  35. */
  36. function tripal_analysis_node_info() {
  37. $nodes = array();
  38. $nodes['chado_analysis'] = array(
  39. 'name' => t('Analysis'),
  40. 'base' => 'chado_analysis',
  41. 'description' => t('An analysis'),
  42. 'has_title' => FALSE,
  43. 'title_label' => t('Analysis'),
  44. 'locked' => TRUE
  45. );
  46. return $nodes;
  47. }
  48. /**
  49. * Implementation of hook_menu().
  50. * Entry points and paths of the module
  51. *
  52. * @ingroup tripal_analysis
  53. */
  54. function tripal_analysis_menu() {
  55. // Tripal Analysis administrative settings
  56. $items['admin/tripal/chado/tripal_analysis'] = array(
  57. 'title' => 'Analyses',
  58. 'description' => 'A bioinformatics analysis producing features.',
  59. 'page callback' => 'tripal_analysis_admin_analysis_view',
  60. 'access arguments' => array('administer tripal analyses'),
  61. 'type' => MENU_NORMAL_ITEM,
  62. );
  63. $items['admin/tripal/chado/tripal_analysis/help'] = array(
  64. 'title' => 'Help',
  65. 'description' => "A description of the Tripal Analysis module including a short description of it's usage.",
  66. 'page callback' => 'theme',
  67. 'page arguments' => array('tripal_analysis_help'),
  68. 'access arguments' => array('administer tripal analyses'),
  69. 'type' => MENU_LOCAL_TASK,
  70. 'weight' => 10,
  71. );
  72. $items['admin/tripal/chado/tripal_analysis/configuration'] = array(
  73. 'title' => 'Settings',
  74. 'description' => 'Settings for the displays of analysis results.',
  75. 'page callback' => 'drupal_get_form',
  76. 'page arguments' => array('tripal_analysis_admin'),
  77. 'access arguments' => array('administer tripal analyses'),
  78. 'type' => MENU_LOCAL_TASK,
  79. 'weight' => 5
  80. );
  81. $items['admin/tripal/chado/tripal_analysis/sync'] = array(
  82. 'title' => 'Sync',
  83. 'description' => 'Sync Chado analyses with Drupal.',
  84. 'page callback' => 'drupal_get_form',
  85. 'page arguments' => array('tripal_analysis_sync_form'),
  86. 'access arguments' => array('administer tripal analyses'),
  87. 'type' => MENU_LOCAL_TASK,
  88. 'weight' => 0
  89. );
  90. return $items;
  91. }
  92. /**
  93. * Implements hook_help()
  94. * Purpose: Adds a help page to the module list
  95. */
  96. function tripal_analysis_help ($path, $arg) {
  97. if ($path == 'admin/help#tripal_analysis') {
  98. return theme('tripal_analysis_help', array());
  99. }
  100. }
  101. /**
  102. * Set the permission types that the chado module uses. Essentially we
  103. * want permissionis that protect creation, editing and deleting of chado
  104. * data objects
  105. *
  106. * @ingroup tripal_analysis
  107. */
  108. function tripal_analysis_permission() {
  109. return array(
  110. 'access chado_analysis content' => array(
  111. 'title' => t('View Analyses'),
  112. 'description' => t('Allow users to view analysis pages.'),
  113. ),
  114. 'create chado_analysis content' => array(
  115. 'title' => t('Create Analyses'),
  116. 'description' => t('Allow users to create new analysis pages.'),
  117. ),
  118. 'delete chado_analysis content' => array(
  119. 'title' => t('Delete Analyses'),
  120. 'description' => t('Allow users to delete analysis pages.'),
  121. ),
  122. 'edit chado_analysis content' => array(
  123. 'title' => t('Edit Analyses'),
  124. 'description' => t('Allow users to edit analysis pages.'),
  125. ),
  126. 'adminster tripal analysis' => array(
  127. 'title' => t('Administer Analyses'),
  128. 'description' => t('Allow users to administer all analyses.'),
  129. ),
  130. );
  131. }
  132. /**
  133. * We need to let drupal know about our theme functions and their arguments.
  134. * We create theme functions to allow users of the module to customize the
  135. * look and feel of the output generated in this module
  136. *
  137. * @ingroup tripal_analysis
  138. */
  139. function tripal_analysis_theme($existing, $type, $theme, $path) {
  140. $core_path = drupal_get_path('module', 'tripal_core');
  141. $items = array(
  142. 'node__chado_analysis' => array(
  143. 'template' => 'node--chado-generic',
  144. 'render element' => 'node',
  145. 'base hook' => 'node',
  146. 'path' => "$core_path/theme",
  147. ),
  148. 'tripal_analysis_base' => array(
  149. 'variables' => array('node' => NULL),
  150. 'template' => 'tripal_analysis_base',
  151. 'path' => "$path/theme/tripal_analysis",
  152. ),
  153. 'tripal_analysis_properties' => array(
  154. 'variables' => array('node' => NULL),
  155. 'template' => 'tripal_analysis_properties',
  156. 'path' => "$path/theme/tripal_analysis",
  157. ),
  158. 'tripal_analysis_teaser' => array(
  159. 'variables' => array('node' => NULL),
  160. 'template' => 'tripal_analysis_teaser',
  161. 'path' => "$path/theme/tripal_analysis",
  162. ),
  163. 'tripal_analysis_help' => array(
  164. 'template' => 'tripal_analysis_help',
  165. 'variables' => array(NULL),
  166. 'path' => "$path/theme",
  167. ),
  168. // tripal_feature theme
  169. 'tripal_feature_analyses' => array(
  170. 'template' => 'tripal_feature_analyses',
  171. 'variables' => array('node' => NULL),
  172. 'path' => "$path/theme/tripal_analysis",
  173. ),
  174. );
  175. return $items;
  176. }
  177. /**
  178. *
  179. *
  180. * @ingroup tripal_analysis
  181. */
  182. function tripal_analysis_block_info() {
  183. $blocks['base']['info'] = t('Tripal Analysis Details');
  184. $blocks['base']['cache'] = DRUPAL_NO_CACHE;
  185. $blocks['featureblast']['info'] = t('Tripal Feature Analyses');
  186. $blocks['featureblast']['cache'] = DRUPAL_NO_CACHE;
  187. return $blocks;
  188. }
  189. /**
  190. *
  191. *
  192. * @ingroup tripal_analysis
  193. */
  194. function tripal_analysis_block_view($delta = '') {
  195. if (user_access('access chado_analysis content') and arg(0) == 'node' and is_numeric(arg(1))) {
  196. $nid = arg(1);
  197. $node = node_load($nid);
  198. $block = array();
  199. switch ($delta) {
  200. case 'base':
  201. $block['subject'] = t('Analysis Details');
  202. $block['content'] = theme('tripal_analysis_base', $node);
  203. break;
  204. case 'tripal_feature_analyses':
  205. $block['subject'] = t('Feature Analyses');
  206. $block['content'] = theme('tripal_feature_analyses', $node);
  207. break;
  208. default :
  209. }
  210. return $block;
  211. }
  212. }
  213. /**
  214. *
  215. * @ingroup tripal_feature
  216. */
  217. function tripal_analysis_node_view($node, $view_mode, $langcode) {
  218. switch ($node->type) {
  219. case 'chado_analysis':
  220. // Show feature browser and counts
  221. if ($view_mode == 'full') {
  222. $node->content['tripal_analysis_base'] = array(
  223. '#value' => theme('tripal_analysis_base', array('node' => $node)),
  224. );
  225. $node->content['tripal_analysis_properties'] = array(
  226. '#value' => theme('tripal_analysis_properties', array('node' => $node)),
  227. );
  228. }
  229. if ($view_mode == 'teaser') {
  230. $node->content['tripal_analysis_teaser'] = array(
  231. '#value' => theme('tripal_analysis_teaser', array('node' => $node)),
  232. );
  233. }
  234. break;
  235. }
  236. }
  237. /**
  238. *
  239. * @param $node
  240. */
  241. function tripal_analysis_node_presave($node) {
  242. // If this is an analysis of some type it will should have thre three required
  243. // fields for the Chado analysis table: program, programversion and sourcename.
  244. // So we will set the title for any node that has these three fields
  245. if (property_exists($node, 'program') and
  246. property_exists($node, 'programversion') and
  247. property_exists($node, 'sourcename')) {
  248. if ($node->analysisname) {
  249. $node->title = $node->analysisname;
  250. }
  251. else {
  252. $node->title = "$node->program ($node->programversion) $node->sourcename";
  253. }
  254. }
  255. }
  256. /**
  257. * Implements hook_views_api()
  258. * Purpose: Essentially this hook tells drupal that there is views support for
  259. * for this module which then includes tripal_analysis.views.inc where all the
  260. * views integration code is
  261. *
  262. * @ingroup tripal_analysis
  263. */
  264. function tripal_analysis_views_api() {
  265. return array(
  266. 'api' => 2.0,
  267. );
  268. }
  269. /**
  270. * Implementation of hook_form_alter()
  271. *
  272. * @param $form
  273. * @param $form_state
  274. * @param $form_id
  275. */
  276. function tripal_analysis_form_alter(&$form, &$form_state, $form_id) {
  277. // turn of preview button for insert/updates
  278. if ($form_id == "chado_analysis_node_form") {
  279. $form['actions']['preview']['#access'] = FALSE;
  280. }
  281. }
  282. /**
  283. * When a new chado_analysis node is created we also need to add information
  284. * to our chado_analysis table. This function is called on insert of a new
  285. * node of type 'chado_analysis' and inserts the necessary information.
  286. *
  287. * @ingroup tripal_analysis
  288. */
  289. function chado_analysis_insert($node) {
  290. $node->analysisname = trim($node->analysisname);
  291. $node->description = trim($node->description);
  292. $node->program = trim($node->program);
  293. $node->programversion = trim($node->programversion);
  294. $node->algorithm = trim($node->algorithm);
  295. $node->sourcename = trim($node->sourcename);
  296. $node->sourceversion = trim($node->sourceversion);
  297. $node->sourceuri = trim($node->sourceuri);
  298. // if there is an analysis_id in the $node object then this must be a sync so
  299. // we can skip adding the analysis as it is already there, although
  300. // we do need to proceed with the rest of the insert
  301. if (!property_exists($node, 'analysis_id')) {
  302. // Create a timestamp so we can insert it into the chado database
  303. $time = $node->timeexecuted;
  304. $month = $time['month'];
  305. $day = $time['day'];
  306. $year = $time['year'];
  307. $timestamp = $month . '/' . $day . '/' . $year;
  308. // insert and then get the newly inserted analysis record
  309. $values = array(
  310. 'name' => $node->analysisname,
  311. 'description' => $node->description,
  312. 'program' => $node->program,
  313. 'programversion' => $node->programversion,
  314. 'algorithm' => $node->algorithm,
  315. 'sourcename' => $node->sourcename,
  316. 'sourceversion' => $node->sourceversion,
  317. 'sourceuri' => $node->sourceuri,
  318. 'timeexecuted' => $timestamp
  319. );
  320. $analysis = tripal_core_chado_insert('analysis', $values);
  321. if (!$analysis) {
  322. drupal_set_message(t('Unable to add analysis.', 'warning'));
  323. watchdog('tripal_analysis', 'Insert analysis: Unable to create analysis where values:%values',
  324. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  325. return;
  326. }
  327. $analysis_id = $analysis['analysis_id'];
  328. // now add in the properties
  329. $properties = tripal_core_properties_form_retreive($node, 'analysis_property');
  330. foreach ($properties as $property => $elements) {
  331. foreach ($elements as $rank => $value) {
  332. $status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE, 'analysis_property');
  333. if (!$status) {
  334. drupal_set_message("Error cannot add property: $property", "error");
  335. watchdog('t_analysis', "Error cannot add property: %prop",
  336. array('%property' => $property), WATCHDOG_ERROR);
  337. }
  338. }
  339. }
  340. }
  341. else {
  342. $analysis_id = $node->analysis_id;
  343. }
  344. // Make sure the entry for this analysis doesn't already exist in the
  345. // chado_analysis table if it doesn't exist then we want to add it.
  346. $check_org_id = chado_get_id_for_node('analysis', $node->nid);
  347. if (!$check_org_id) {
  348. $record = new stdClass();
  349. $record->nid = $node->nid;
  350. $record->vid = $node->vid;
  351. $record->analysis_id = $analysis_id;
  352. drupal_write_record('chado_analysis', $record);
  353. }
  354. // add the analysis to the node object for
  355. // use by other analysis modules that may be using this function
  356. $node->analysis = $analysis;
  357. $node->analysis_id = $analysis_id; // we need to set this for children
  358. }
  359. /**
  360. * Removes analysis from the chado database
  361. *
  362. * @param $node
  363. * The node object specifying which chado record to delete
  364. *
  365. * @ingroup tripal_analysis
  366. */
  367. function chado_analysis_delete($node) {
  368. $analysis_id = chado_get_id_for_node('analysis', $node->nid);
  369. // if we don't have an analysis id for this node then this isn't a node of
  370. // type chado_analysis or the entry in the chado_analysis table was lost.
  371. if (!$analysis_id) {
  372. return;
  373. }
  374. // Remove data from the {chado_analysis}, {node}, and {node_revisions} tables
  375. $sql_del = "DELETE FROM {chado_analysis} WHERE nid = :nid AND vid = :vid";
  376. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  377. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  378. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  379. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  380. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  381. //Remove from analysis and analysisprop tables of chado database as well
  382. chado_query("DELETE FROM {analysis} WHERE analysis_id = :analysis_id", array(':analysis_id' => $analysis_id));
  383. }
  384. /**
  385. * Update analyses
  386. *
  387. * @param $node
  388. * The updated node object
  389. *
  390. * @ingroup tripal_analysis
  391. */
  392. function chado_analysis_update($node) {
  393. $node->analysisname = trim($node->analysisname);
  394. $node->description = trim($node->description);
  395. $node->program = trim($node->program);
  396. $node->programversion = trim($node->programversion);
  397. $node->algorithm = trim($node->algorithm);
  398. $node->sourcename = trim($node->sourcename);
  399. $node->sourceversion = trim($node->sourceversion);
  400. $node->sourceuri = trim($node->sourceuri);
  401. // Create a timestamp so we can insert it into the chado database
  402. $time = $node->timeexecuted;
  403. $month = $time['month'];
  404. $day = $time['day'];
  405. $year = $time['year'];
  406. $timestamp = $month . '/' . $day . '/' . $year;
  407. // update the record in Chado
  408. $analysis_id = chado_get_id_for_node('analysis', $node->nid);
  409. $sql = "
  410. UPDATE {analysis}
  411. SET name = :name, description = :description, program = :program,
  412. programversion = :programversion, algorithm = :algorithm, sourcename = :sourcename,
  413. sourceversion = :sourceversion, sourceuri = :sourceuri, timeexecuted = :timeexecuted
  414. WHERE analysis_id = :analysis_id
  415. ";
  416. $args = array(
  417. ':name' => $node->analysisname,
  418. ':description' => $node->description,
  419. ':program' => $node->program,
  420. ':programversion' => $node->programversion,
  421. ':algorithm' => $node->algorithm,
  422. ':sourcename' => $node->sourcename,
  423. ':sourceversion' => $node->sourceversion,
  424. ':sourceuri' => $node->sourceuri,
  425. ':timeexecuted' => $timestamp,
  426. ':analysis_id' => $analysis_id
  427. );
  428. chado_query($sql, $args);
  429. // now update the properties
  430. $properties = array(); // stores all of the properties we need to add
  431. // now add in the properties by first removing any the analysis
  432. // already has and adding the ones we have
  433. tripal_core_chado_delete('analysisprop', array('analysis_id' => $analysis_id));
  434. $properties = tripal_core_properties_form_retreive($node, 'analysis_property');
  435. foreach ($properties as $property => $elements) {
  436. foreach ($elements as $rank => $value) {
  437. $status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE, 'analysis_property');
  438. if (!$status) {
  439. drupal_set_message("Error cannot add property: '$property'", "error");
  440. watchdog('t_analysis', "Error cannot add property: '%prop'",
  441. array('%prop' => $property), WATCHDOG_ERROR);
  442. }
  443. }
  444. }
  445. }
  446. /**
  447. * When a node is requested by the user this function is called to allow us
  448. * to add auxiliary data to the node object.
  449. *
  450. * @ingroup tripal_analysis
  451. */
  452. function chado_analysis_load($nodes) {
  453. foreach ($nodes as $nid => $node) {
  454. // find the analysis and add in the details
  455. $analysis_id = chado_get_id_for_node('analysis', $nid);
  456. // build the analysis variable
  457. $values = array('analysis_id' => $analysis_id);
  458. $analysis = tripal_core_generate_chado_var('analysis', $values);
  459. // add in the description field
  460. $analysis = tripal_core_expand_chado_vars($analysis, 'field', 'analysis.description');
  461. $nodes[$nid]->analysis = $analysis;
  462. }
  463. }
  464. /**
  465. * Implement hook_access().
  466. *
  467. * This hook allows node modules to limit access to the node types they define.
  468. *
  469. * @param $node
  470. * The node on which the operation is to be performed, or, if it does not yet exist, the
  471. * type of node to be created
  472. *
  473. * @param $op
  474. * The operation to be performed
  475. *
  476. * @param $account
  477. * A user object representing the user for whom the operation is to be performed
  478. *
  479. * @return
  480. * If the permission for the specified operation is not set then return FALSE. If the
  481. * permission is set then return NULL as this allows other modules to disable
  482. * access. The only exception is when the $op == 'create'. We will always
  483. * return TRUE if the permission is set.
  484. *
  485. * @ingroup tripal_analysis
  486. */
  487. function chado_analysis_node_access($node, $op, $account) {
  488. if ($op == 'create') {
  489. if (!user_access('create chado_analysis content', $account)) {
  490. return FALSE;
  491. }
  492. return TRUE;
  493. }
  494. if ($op == 'update') {
  495. if (!user_access('edit chado_analysis content', $account)) {
  496. return FALSE;
  497. }
  498. }
  499. if ($op == 'delete') {
  500. if (!user_access('delete chado_analysis content', $account)) {
  501. return FALSE;
  502. }
  503. }
  504. if ($op == 'view') {
  505. if (!user_access('access chado_analysis content', $account)) {
  506. return FALSE;
  507. }
  508. }
  509. return NULL;
  510. }