tripal_analysis.module 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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->nid);
  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. tripal_core_chado_delete('analysisprop', array('analysis_id' => $analysis_id));
  349. foreach ($properties as $property => $elements) {
  350. foreach ($elements as $rank => $value) {
  351. $status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE, 'analysis_property');
  352. if (!$status) {
  353. drupal_set_message("Error cannot add property: '$property'", "error");
  354. watchdog('t_analysis', "Error cannot add property: '%prop'",
  355. array('%prop' => $property), WATCHDOG_ERROR);
  356. }
  357. }
  358. }
  359. }
  360. /**
  361. * When a node is requested by the user this function is called to allow us
  362. * to add auxiliary data to the node object.
  363. *
  364. * @ingroup tripal_analysis
  365. */
  366. function chado_analysis_load($node) {
  367. // get the feature details from chado
  368. $analysis_id = chado_get_id_for_node('analysis', $node->nid);
  369. $values = array('analysis_id' => $analysis_id);
  370. $analysis = tripal_core_generate_chado_var('analysis', $values);
  371. $additions = new stdClass();
  372. $additions->analysis = $analysis;
  373. return $additions;
  374. }
  375. /**
  376. * This function customizes the view of the chado_analysis node. It allows
  377. * us to generate the markup.
  378. *
  379. * @ingroup tripal_analysis
  380. */
  381. function chado_analysis_view($node, $teaser = FALSE, $page = FALSE) {
  382. // use drupal's default node view:
  383. if (!$teaser) {
  384. $node = node_prepare($node, $teaser);
  385. // When previewing a node submitting form, it shows 'Array' instead of
  386. // correct date format. We need to format the date here
  387. $time = $node->timeexecuted;
  388. if (is_array($time)) {
  389. $month = $time['month'];
  390. $day = $time['day'];
  391. $year = $time['year'];
  392. $timestamp = $year . '-' . $month . '-' . $day;
  393. $node->timeexecuted = $timestamp;
  394. }
  395. }
  396. return $node;
  397. }
  398. /**
  399. * Display help and module information
  400. * @param path which path of the site we're displaying help
  401. * @param arg array that holds the current path as would be returned from arg()
  402. * function
  403. * @return help text for the path
  404. *
  405. * @ingroup tripal_analysis
  406. */
  407. function tripal_analysis_help($path, $arg) {
  408. $output = '';
  409. switch ($path) {
  410. case "admin/help#tripal_analysis":
  411. $output = '<p>' .
  412. t("Displays links to nodes created on this date") .
  413. '</p>';
  414. break;
  415. }
  416. return $output;
  417. }
  418. /**
  419. * Implement hook_access().
  420. *
  421. * This hook allows node modules to limit access to the node types they define.
  422. *
  423. * @param $op
  424. * The operation to be performed
  425. *
  426. * @param $node
  427. * The node on which the operation is to be performed, or, if it does not yet exist, the
  428. * type of node to be created
  429. *
  430. * @param $account
  431. * A user object representing the user for whom the operation is to be performed
  432. *
  433. * @return
  434. * If the permission for the specified operation is not set then return FALSE. If the
  435. * permission is set then return NULL as this allows other modules to disable
  436. * access. The only exception is when the $op == 'create'. We will always
  437. * return TRUE if the permission is set.
  438. *
  439. * @ingroup tripal_analysis
  440. */
  441. function chado_analysis_access($op, $node, $account) {
  442. if ($op == 'create') {
  443. if (!user_access('create chado_analysis content', $account)) {
  444. return FALSE;
  445. }
  446. return TRUE;
  447. }
  448. if ($op == 'update') {
  449. if (!user_access('edit chado_analysis content', $account)) {
  450. return FALSE;
  451. }
  452. }
  453. if ($op == 'delete') {
  454. if (!user_access('delete chado_analysis content', $account)) {
  455. return FALSE;
  456. }
  457. }
  458. if ($op == 'view') {
  459. if (!user_access('access chado_analysis content', $account)) {
  460. return FALSE;
  461. }
  462. }
  463. return NULL;
  464. }
  465. /**
  466. * Set the permission types that the chado module uses. Essentially we
  467. * want permissionis that protect creation, editing and deleting of chado
  468. * data objects
  469. *
  470. * @ingroup tripal_analysis
  471. */
  472. function tripal_analysis_perm() {
  473. return array(
  474. 'access chado_analysis content',
  475. 'create chado_analysis content',
  476. 'delete chado_analysis content',
  477. 'edit chado_analysis content',
  478. 'administer tripal analyses',
  479. );
  480. }
  481. /**
  482. * We need to let drupal know about our theme functions and their arguments.
  483. * We create theme functions to allow users of the module to customize the
  484. * look and feel of the output generated in this module
  485. *
  486. * @ingroup tripal_analysis
  487. */
  488. function tripal_analysis_theme() {
  489. $items = array(
  490. 'tripal_analysis_base' => array(
  491. 'arguments' => array('node' => NULL),
  492. 'template' => 'tripal_analysis_base',
  493. ),
  494. 'tripal_feature_analyses' => array(
  495. 'template' => 'tripal_feature_analyses',
  496. 'arguments' => array('node' => NULL),
  497. ),
  498. 'tripal_analysis_admin' => array(
  499. 'template' => 'tripal_analysis_admin',
  500. 'arguments' => array(NULL),
  501. 'path' => drupal_get_path('module', 'tripal_analysis') . '/theme',
  502. ),
  503. 'tripal_analysis_properties' => array(
  504. 'arguments' => array('node' => NULL)
  505. ),
  506. // Themed Forms
  507. 'chado_analysis_node_form' => array(
  508. 'arguments' => array('form'),
  509. ),
  510. );
  511. return $items;
  512. }
  513. /**
  514. *
  515. *
  516. * @ingroup tripal_feature
  517. */
  518. function tripal_analysis_block($op = 'list', $delta = 0, $edit=array()) {
  519. switch ($op) {
  520. case 'list':
  521. $blocks['base']['info'] = t('Tripal Analysis Details');
  522. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  523. $blocks['featureblast']['info'] = t('Tripal Feature Analyses');
  524. $blocks['featureblast']['cache'] = BLOCK_NO_CACHE;
  525. return $blocks;
  526. case 'view':
  527. if (user_access('access chado_analysis content') and arg(0) == 'node' and is_numeric(arg(1))) {
  528. $nid = arg(1);
  529. $node = node_load($nid);
  530. $block = array();
  531. switch ($delta) {
  532. case 'base':
  533. $block['subject'] = t('Analysis Details');
  534. $block['content'] = theme('tripal_analysis_base', $node);
  535. break;
  536. case 'tripal_feature_analyses':
  537. $block['subject'] = t('Feature Analyses');
  538. $block['content'] = theme('tripal_feature_analyses', $node);
  539. break;
  540. default :
  541. }
  542. return $block;
  543. }
  544. }
  545. }
  546. /*******************************************************************************
  547. * tripal_analysis_nodeapi()
  548. * HOOK: Implementation of hook_nodeapi()
  549. * Display blast results for allowed node types
  550. */
  551. function tripal_analysis_nodeapi(&$node, $op, $teaser, $page) {
  552. switch ($op) {
  553. case 'view':
  554. if ($teaser) {
  555. return '';
  556. }
  557. // Abort if this node is not one of the types we should show.
  558. if (strcmp($node->type, 'chado_feature') == 0) {
  559. if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
  560. // return results for searching
  561. }
  562. else {
  563. // return normal results
  564. $node->content['tripal_feature_analyses'] = array(
  565. '#value' => theme('tripal_feature_analyses', $node),
  566. '#weight' => 8
  567. );
  568. }
  569. }
  570. break;
  571. }
  572. }
  573. /**
  574. * Implements hook_views_api()
  575. * Purpose: Essentially this hook tells drupal that there is views support for
  576. * for this module which then includes tripal_analysis.views.inc where all the
  577. * views integration code is
  578. *
  579. * @ingroup tripal_analysis
  580. */
  581. function tripal_analysis_views_api() {
  582. return array(
  583. 'api' => 2.0,
  584. );
  585. }
  586. /*
  587. *
  588. */
  589. function tripal_analysis_form_alter(&$form, &$form_state, $form_id) {
  590. if ($form_id == "chado_analysis_node_form") {
  591. }
  592. }