tripal_analysis.module

  1. 2.x tripal_analysis/tripal_analysis.module
  2. 3.x legacy/tripal_analysis/tripal_analysis.module
  3. 1.x tripal_analysis/tripal_analysis.module

Contains all the main hook implementations for the tripal_analysis module

tripal_analysis Analysis Module

Provides functions for managing chado analysis' including creating details pages for each one

File

tripal_analysis/tripal_analysis.module
View source
  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('api/tripal_analysis.api.inc');
  16. require('includes/tripal_analysis_privacy.inc');
  17. require('includes/tripal_analysis.admin.inc');
  18. require('includes/tripal_analysis.form.inc');
  19. /**
  20. * Add tripal javascript to page headers
  21. *
  22. * @ingroup tripal_analysis
  23. */
  24. function tripal_analysis_init() {
  25. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_analysis.js');
  26. drupal_add_css(drupal_get_path('theme', 'tripal') . '/css/tripal_analysis.css');
  27. }
  28. /**
  29. * Implementation of hook_menu().
  30. * Entry points and paths of the module
  31. *
  32. * @ingroup tripal_analysis
  33. */
  34. function tripal_analysis_menu() {
  35. //Sync analysis
  36. $items['chado_sync_analyses'] = array(
  37. 'title' => 'Sync Data',
  38. 'page callback' => 'tripal_analysis_sync_analyses',
  39. 'access arguments' => array('administer tripal analyses'),
  40. 'type' => MENU_CALLBACK
  41. );
  42. // Tripal Analysis administrative settings
  43. $items['admin/tripal/tripal_analysis'] = array(
  44. 'title' => 'Analyses',
  45. 'description' => 'Basic Description of Tripal Analysis Module Functionality.',
  46. 'page callback' => 'theme',
  47. 'page arguments' => array('tripal_analysis_admin'),
  48. 'access arguments' => array('administer tripal analyses'),
  49. 'type' => MENU_NORMAL_ITEM,
  50. 'file' => 'includes/tripal_analysis.admin.inc',
  51. );
  52. $items['admin/tripal/tripal_analysis/configuration'] = array(
  53. 'title' => 'Configuration',
  54. 'description' => 'Settings for the displays of analysis results.',
  55. 'page callback' => 'drupal_get_form',
  56. 'page arguments' => array('tripal_analysis_admin'),
  57. 'access arguments' => array('administer tripal analyses'),
  58. 'type' => MENU_NORMAL_ITEM,
  59. 'file' => 'includes/tripal_analysis.admin.inc',
  60. );
  61. // AJAX calls for adding/removing properties to a contact
  62. $items['tripal_analysis/properties/add'] = array(
  63. 'page callback' => 'tripal_analysis_property_add',
  64. 'access arguments' => array('edit chado_analysis content'),
  65. 'type ' => MENU_CALLBACK,
  66. );
  67. $items['tripal_analysis/properties/description'] = array(
  68. 'page callback' => 'tripal_analysis_property_get_description',
  69. 'access arguments' => array('edit chado_analysis content'),
  70. 'type ' => MENU_CALLBACK,
  71. );
  72. $items['tripal_analysis/properties/minus/%/%'] = array(
  73. 'page callback' => 'tripal_analysis_property_delete',
  74. 'page arguments' => array(3, 4),
  75. 'access arguments' => array('edit chado_analysis content'),
  76. 'type ' => MENU_CALLBACK,
  77. );
  78. return $items;
  79. }
  80. /**
  81. * Provide information to drupal about the node types that we're creating
  82. * in this module
  83. *
  84. * @ingroup tripal_analysis
  85. */
  86. function tripal_analysis_node_info() {
  87. $nodes = array();
  88. $nodes['chado_analysis'] = array(
  89. 'name' => t('Analysis'),
  90. 'module' => 'chado_analysis',
  91. 'description' => t('An analysis from the chado database'),
  92. 'has_title' => FALSE,
  93. 'title_label' => t('Analysis'),
  94. 'has_body' => FALSE,
  95. 'body_label' => t('Analysis Description'),
  96. 'locked' => TRUE
  97. );
  98. return $nodes;
  99. }
  100. /**
  101. * When a new chado_analysis node is created we also need to add information
  102. * to our chado_analysis table. This function is called on insert of a new
  103. * node of type 'chado_analysis' and inserts the necessary information.
  104. *
  105. * @ingroup tripal_analysis
  106. */
  107. function chado_analysis_insert($node) {
  108. global $user;
  109. // Create a timestamp so we can insert it into the chado database
  110. $time = $node->timeexecuted;
  111. $month = $time['month'];
  112. $day = $time['day'];
  113. $year = $time['year'];
  114. $timestamp = $month . '/' . $day . '/' . $year;
  115. // If this analysis already exists then don't recreate it in chado
  116. $analysis_id = $node->analysis_id;
  117. if ($analysis_id) {
  118. $values = array('analysis_id' => $node->analysis_id);
  119. $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
  120. if($result and count($result) > 0) {
  121. $analysis = $result[0];
  122. }
  123. }
  124. // If the analysis doesn't exist then let's create it in chado.
  125. if (!$analysis) {
  126. // insert and then get the newly inserted analysis record
  127. $values = array(
  128. 'name' => $node->analysisname,
  129. 'description' => $node->description,
  130. 'program' => $node->program,
  131. 'programversion' => $node->programversion,
  132. 'algorithm' => $node->algorithm,
  133. 'sourcename' => $node->sourcename,
  134. 'sourceversion' => $node->sourceversion,
  135. 'sourceuri' => $node->sourceuri,
  136. 'timeexecuted' => $timestamp
  137. );
  138. if (tripal_core_chado_insert('analysis', $values)) {
  139. $analysis = tripal_core_chado_select('analysis', array('*'), $values);
  140. $analysis_id = $analysis[0]->analysis_id;
  141. }
  142. }
  143. // Make sure the entry for this analysis doesn't already exist in the
  144. // chado_analysis table if it doesn't exist then we want to add it.
  145. $node_check_sql = "SELECT * FROM {chado_analysis} ".
  146. "WHERE analysis_id = %d";
  147. $node_check = db_fetch_object(db_query($node_check_sql, $analysis_id));
  148. if (!$node_check) {
  149. // next add the item to the drupal table
  150. $sql = "INSERT INTO {chado_analysis} (nid, vid, analysis_id) ".
  151. "VALUES (%d, %d, %d)";
  152. db_query($sql, $node->nid, $node->vid, $analysis_id);
  153. // Create a title for the analysis node using the unique keys so when the
  154. // node is saved, it will have a title
  155. $record = new stdClass();
  156. // If the analysis has a name, use it as the node title. If not, construct
  157. // the title using program, programversion, and sourcename
  158. if ($node->analysisname) {
  159. $record->title = $node->analysisname;
  160. }
  161. else {
  162. //Construct node title as "program (version)
  163. $record->title = "$node->program ($node->programversion)";
  164. }
  165. $record->nid = $node->nid;
  166. drupal_write_record('node', $record, 'nid');
  167. drupal_write_record('node_revisions', $record, 'nid');
  168. }
  169. // add the analysis to the node object for
  170. // use by other analysis modules that may be using this function
  171. $node->analysis = $analysis;
  172. $node->analysis_id = $analysis_id; // we need to set this for children
  173. // now add the properties
  174. $properties = array(); // stores all of the properties we need to add
  175. // get the list of properties for easy lookup (without doing lots of database queries
  176. $properties_list = array();
  177. $sql = "
  178. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  179. FROM {cvterm} CVT
  180. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  181. WHERE
  182. CV.name = 'analysis_property' AND
  183. NOT CVT.is_obsolete = 1
  184. ORDER BY CVT.name ASC
  185. ";
  186. $prop_types = chado_query($sql);
  187. while ($prop = db_fetch_object($prop_types)) {
  188. $properties_list[$prop->cvterm_id] = $prop->name;
  189. }
  190. // get the properties that should be added. Properties are in one of two forms:
  191. // 1) prop_value-[type id]-[index]
  192. // 2) new_value-[type id]-[index]
  193. // 3) new_id, new_value
  194. foreach ($node as $name => $value) {
  195. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  196. $type_id = $matches[1];
  197. $index = $matches[2];
  198. $name = $properties_list[$type_id];
  199. $properties[$name][$index] = trim($value);
  200. }
  201. }
  202. if ($node->new_id and $node->new_value) {
  203. $type_id = $node->new_id;
  204. $name = $properties_list[$type_id];
  205. $index = count($properties[$name]);
  206. $properties[$name][$index] = trim($node->new_value);
  207. }
  208. // now add in the properties
  209. foreach ($properties as $property => $elements) {
  210. foreach ($elements as $rank => $value) {
  211. $status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE, 'analysis_property');
  212. if (!$status) {
  213. drupal_set_message("Error cannot add property: $property", "error");
  214. watchdog('t_analysis', "Error cannot add property: %prop",
  215. array('%property' => $property), WATCHDOG_ERROR);
  216. }
  217. }
  218. }
  219. }
  220. /**
  221. * Removes analysis from the chado database
  222. *
  223. * @param $node
  224. * The node object specifying which chado record to delete
  225. *
  226. * @ingroup tripal_analysis
  227. */
  228. function chado_analysis_delete($node) {
  229. $analysis_id = chado_get_id_for_node('analysis', $node);
  230. // if we don't have an organism id for this node then this isn't a node of
  231. // type chado_organism or the entry in the chado_organism table was lost.
  232. if (!$analysis_id) {
  233. return;
  234. }
  235. // Remove data from the {chado_analysis}, {node}, and {node_revisions} tables
  236. $sql_del = "DELETE FROM {chado_analysis} ".
  237. "WHERE nid = %d ".
  238. "AND vid = %d";
  239. db_query($sql_del, $node->nid, $node->vid);
  240. $sql_del = "DELETE FROM {node} ".
  241. "WHERE nid = %d ".
  242. "AND vid = %d";
  243. db_query($sql_del, $node->nid, $node->vid);
  244. $sql_del = "DELETE FROM {node_revisions} ".
  245. "WHERE nid = %d ".
  246. "AND vid = %d";
  247. db_query($sql_del, $node->nid, $node->vid);
  248. //Remove from analysis and analysisprop tables of chado database as well
  249. chado_query("DELETE FROM {analysis} WHERE analysis_id = %d", $analysis_id);
  250. }
  251. /**
  252. * Update analyses
  253. *
  254. * @param $node
  255. * The updated node object
  256. *
  257. * @ingroup tripal_analysis
  258. */
  259. function chado_analysis_update($node) {
  260. global $user;
  261. if ($node->revision) {
  262. // TODO -- decide what to do about revisions
  263. }
  264. // Create a timestamp so we can insert it into the chado database
  265. $time = $node->timeexecuted;
  266. $month = $time['month'];
  267. $day = $time['day'];
  268. $year = $time['year'];
  269. $timestamp = $month . '/' . $day . '/' . $year;
  270. // get the analysis_id for this node:
  271. $sql = "SELECT analysis_id ".
  272. "FROM {chado_analysis} ".
  273. "WHERE nid = %d";
  274. $analysis_id = db_fetch_object(db_query($sql, $node->nid))->analysis_id;
  275. $sql = "UPDATE {analysis} ".
  276. "SET name = '%s', ".
  277. " description = '%s', ".
  278. " program = '%s', ".
  279. " programversion = '%s', ".
  280. " algorithm = '%s', ".
  281. " sourcename = '%s', ".
  282. " sourceversion = '%s', ".
  283. " sourceuri = '%s', ".
  284. " timeexecuted = '%s' ".
  285. "WHERE analysis_id = %d ";
  286. chado_query($sql, $node->analysisname, $node->description, $node->program,
  287. $node->programversion, $node->algorithm, $node->sourcename,
  288. $node->sourceversion, $node->sourceuri, $timestamp, $analysis_id);
  289. // Create a title for the analysis node using the unique keys so when the
  290. // node is saved, it will have a title
  291. $record = new stdClass();
  292. // If the analysis has a name, use it as the node title. If not, construct
  293. // the title using program, programversion, and sourcename
  294. if ($node->analysisname) {
  295. $record->title = $node->analysisname;
  296. }
  297. else {
  298. //Construct node title as "program (version)
  299. $record->title = "$node->program ($node->programversion)";
  300. }
  301. $record->nid = $node->nid;
  302. drupal_write_record('node', $record, 'nid');
  303. drupal_write_record('node_revisions', $record, 'nid');
  304. // now update the properties
  305. $properties = array(); // stores all of the properties we need to add
  306. // get the list of properties for easy lookup (without doing lots of database queries
  307. $properties_list = array();
  308. $sql = "
  309. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  310. FROM {cvterm} CVT
  311. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  312. WHERE
  313. CV.name = 'analysis_property' AND
  314. NOT CVT.is_obsolete = 1
  315. ORDER BY CVT.name ASC
  316. ";
  317. $prop_types = chado_query($sql);
  318. while ($prop = db_fetch_object($prop_types)) {
  319. $properties_list[$prop->cvterm_id] = $prop->name;
  320. }
  321. // get the properties that should be added. Properties are in one of three forms:
  322. // 1) prop_value-[type id]-[index]
  323. // 2) new_value-[type id]-[index]
  324. // 3) new_id, new_value
  325. // dpm($node);
  326. foreach ($node as $key => $value) {
  327. if (preg_match('/^prop_value-(\d+)-(\d+)/', $key, $matches)) {
  328. $type_id = $matches[1];
  329. $index = $matches[2];
  330. $name = $properties_list[$type_id];
  331. $properties[$name][$index] = trim($value);
  332. }
  333. if (preg_match('/^new_value-(\d+)-(\d+)/', $key, $matches)) {
  334. $type_id = $matches[1];
  335. $index = $matches[2];
  336. $name = $properties_list[$type_id];
  337. $properties[$name][$index] = trim($value);
  338. }
  339. }
  340. if ($node->new_id and $node->new_value) {
  341. $type_id = $node->new_id;
  342. $name = $properties_list[$type_id];
  343. $index = count($properties[$name]);
  344. $properties[$name][$index] = trim($node->new_value);
  345. }
  346. // now add in the properties by first removing any the analysis
  347. // already has and adding the ones we have
  348. $sql = "
  349. DELETE FROM {analysisprop} WHERE analysis_id = %d AND type_id IN (
  350. SELECT CVT.cvterm_id
  351. FROM {cvterm} CVT
  352. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  353. WHERE CV.name = 'analysis_property')
  354. ";
  355. $success = chado_query($sql, $analysis_id);
  356. if (!$success) {
  357. drupal_set_message("Cannot update analysis properties", "error");
  358. watchdog('t_analysis', "Cannot update analysis properties.", array(), WATCHDOG_ERROR);
  359. return;
  360. }
  361. foreach ($properties as $property => $elements) {
  362. foreach ($elements as $rank => $value) {
  363. $status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE, 'analysis_property');
  364. if (!$status) {
  365. drupal_set_message("Error cannot add property: '$property'", "error");
  366. watchdog('t_analysis', "Error cannot add property: '%prop'",
  367. array('%prop' => $property), WATCHDOG_ERROR);
  368. }
  369. }
  370. }
  371. }
  372. /**
  373. * When a node is requested by the user this function is called to allow us
  374. * to add auxiliary data to the node object.
  375. *
  376. * @ingroup tripal_analysis
  377. */
  378. function chado_analysis_load($node) {
  379. // get the feature details from chado
  380. $analysis_id = chado_get_id_for_node('analysis', $node);
  381. $values = array('analysis_id' => $analysis_id);
  382. $analysis = tripal_core_generate_chado_var('analysis', $values);
  383. $additions = new stdClass();
  384. $additions->analysis = $analysis;
  385. return $additions;
  386. }
  387. /**
  388. * This function customizes the view of the chado_analysis node. It allows
  389. * us to generate the markup.
  390. *
  391. * @ingroup tripal_analysis
  392. */
  393. function chado_analysis_view($node, $teaser = FALSE, $page = FALSE) {
  394. // use drupal's default node view:
  395. if (!$teaser) {
  396. $node = node_prepare($node, $teaser);
  397. // When previewing a node submitting form, it shows 'Array' instead of
  398. // correct date format. We need to format the date here
  399. $time = $node->timeexecuted;
  400. if (is_array($time)) {
  401. $month = $time['month'];
  402. $day = $time['day'];
  403. $year = $time['year'];
  404. $timestamp = $year . '-' . $month . '-' . $day;
  405. $node->timeexecuted = $timestamp;
  406. }
  407. }
  408. return $node;
  409. }
  410. /**
  411. * Display help and module information
  412. * @param path which path of the site we're displaying help
  413. * @param arg array that holds the current path as would be returned from arg()
  414. * function
  415. * @return help text for the path
  416. *
  417. * @ingroup tripal_analysis
  418. */
  419. function tripal_analysis_help($path, $arg) {
  420. $output = '';
  421. switch ($path) {
  422. case "admin/help#tripal_analysis":
  423. $output = '<p>' .
  424. t("Displays links to nodes created on this date") .
  425. '</p>';
  426. break;
  427. }
  428. return $output;
  429. }
  430. /**
  431. * Implement hook_access().
  432. *
  433. * This hook allows node modules to limit access to the node types they define.
  434. *
  435. * @param $op
  436. * The operation to be performed
  437. *
  438. * @param $node
  439. * The node on which the operation is to be performed, or, if it does not yet exist, the
  440. * type of node to be created
  441. *
  442. * @param $account
  443. * A user object representing the user for whom the operation is to be performed
  444. *
  445. * @return
  446. * If the permission for the specified operation is not set then return FALSE. If the
  447. * permission is set then return NULL as this allows other modules to disable
  448. * access. The only exception is when the $op == 'create'. We will always
  449. * return TRUE if the permission is set.
  450. *
  451. * @ingroup tripal_analysis
  452. */
  453. function chado_analysis_access($op, $node, $account) {
  454. if ($op == 'create') {
  455. if (!user_access('create chado_analysis content', $account)) {
  456. return FALSE;
  457. }
  458. return TRUE;
  459. }
  460. if ($op == 'update') {
  461. if (!user_access('edit chado_analysis content', $account)) {
  462. return FALSE;
  463. }
  464. }
  465. if ($op == 'delete') {
  466. if (!user_access('delete chado_analysis content', $account)) {
  467. return FALSE;
  468. }
  469. }
  470. if ($op == 'view') {
  471. if (!user_access('access chado_analysis content', $account)) {
  472. return FALSE;
  473. }
  474. }
  475. return NULL;
  476. }
  477. /**
  478. * Set the permission types that the chado module uses. Essentially we
  479. * want permissionis that protect creation, editing and deleting of chado
  480. * data objects
  481. *
  482. * @ingroup tripal_analysis
  483. */
  484. function tripal_analysis_perm() {
  485. return array(
  486. 'access chado_analysis content',
  487. 'create chado_analysis content',
  488. 'delete chado_analysis content',
  489. 'edit chado_analysis content',
  490. 'administer tripal analyses',
  491. );
  492. }
  493. /**
  494. * We need to let drupal know about our theme functions and their arguments.
  495. * We create theme functions to allow users of the module to customize the
  496. * look and feel of the output generated in this module
  497. *
  498. * @ingroup tripal_analysis
  499. */
  500. function tripal_analysis_theme() {
  501. $items = array(
  502. 'tripal_analysis_base' => array(
  503. 'arguments' => array('node' => NULL),
  504. 'template' => 'tripal_analysis_base',
  505. ),
  506. 'tripal_feature_analyses' => array(
  507. 'template' => 'tripal_feature_analyses',
  508. 'arguments' => array('node' => NULL),
  509. ),
  510. 'tripal_analysis_admin' => array(
  511. 'template' => 'tripal_analysis_admin',
  512. 'arguments' => array(NULL),
  513. 'path' => drupal_get_path('module', 'tripal_analysis') . '/theme',
  514. ),
  515. 'tripal_analysis_properties' => array(
  516. 'arguments' => array('node' => NULL)
  517. ),
  518. // Themed Forms
  519. 'chado_analysis_node_form' => array(
  520. 'arguments' => array('form'),
  521. ),
  522. );
  523. return $items;
  524. }
  525. /**
  526. *
  527. *
  528. * @ingroup tripal_feature
  529. */
  530. function tripal_analysis_block($op = 'list', $delta = 0, $edit=array()) {
  531. switch ($op) {
  532. case 'list':
  533. $blocks['base']['info'] = t('Tripal Analysis Details');
  534. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  535. $blocks['featureblast']['info'] = t('Tripal Feature Analyses');
  536. $blocks['featureblast']['cache'] = BLOCK_NO_CACHE;
  537. return $blocks;
  538. case 'view':
  539. if (user_access('access chado_analysis content') and arg(0) == 'node' and is_numeric(arg(1))) {
  540. $nid = arg(1);
  541. $node = node_load($nid);
  542. $block = array();
  543. switch ($delta) {
  544. case 'base':
  545. $block['subject'] = t('Analysis Details');
  546. $block['content'] = theme('tripal_analysis_base', $node);
  547. break;
  548. case 'tripal_feature_analyses':
  549. $block['subject'] = t('Feature Analyses');
  550. $block['content'] = theme('tripal_feature_analyses', $node);
  551. break;
  552. default :
  553. }
  554. return $block;
  555. }
  556. }
  557. }
  558. /*******************************************************************************
  559. * tripal_analysis_nodeapi()
  560. * HOOK: Implementation of hook_nodeapi()
  561. * Display blast results for allowed node types
  562. */
  563. function tripal_analysis_nodeapi(&$node, $op, $teaser, $page) {
  564. switch ($op) {
  565. case 'view':
  566. if ($teaser) {
  567. return '';
  568. }
  569. // Abort if this node is not one of the types we should show.
  570. if (strcmp($node->type, 'chado_feature') == 0) {
  571. if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
  572. // return results for searching
  573. }
  574. else {
  575. // return normal results
  576. $node->content['tripal_feature_analyses'] = array(
  577. '#value' => theme('tripal_feature_analyses', $node),
  578. '#weight' => 8
  579. );
  580. }
  581. }
  582. break;
  583. }
  584. }
  585. /**
  586. * Implements hook_views_api()
  587. * Purpose: Essentially this hook tells drupal that there is views support for
  588. * for this module which then includes tripal_analysis.views.inc where all the
  589. * views integration code is
  590. *
  591. * @ingroup tripal_analysis
  592. */
  593. function tripal_analysis_views_api() {
  594. return array(
  595. 'api' => 2.0,
  596. );
  597. }
  598. /*
  599. *
  600. */
  601. function tripal_analysis_form_alter(&$form, &$form_state, $form_id) {
  602. if ($form_id == "chado_analysis_node_form") {
  603. }
  604. }

Related topics