tripal_analysis.module 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. * @{
  8. * Provides functions for managing chado analysis' including creating details pages for each one
  9. * @}
  10. * @ingroup tripal_modules
  11. */
  12. require('api/tripal_analysis.api.inc');
  13. require('includes/tripal_analysis_privacy.inc');
  14. /**
  15. * Register tripal_analysis sub-modules
  16. *
  17. * @param $modulename
  18. * The name of the module to be registered as a tripal analysis submodule
  19. *
  20. * @ingroup tripal_analysis
  21. */
  22. function tripal_analysis_register_child($modulename) {
  23. $sql = "INSERT INTO {tripal_analysis} (modulename) VALUES ('%s')";
  24. db_query($sql, $modulename);
  25. }
  26. /**
  27. * Un-register a tripal analysis sub-module
  28. *
  29. * @param $modulename
  30. * The name of the module to un-register
  31. *
  32. * @ingroup tripal_analysis
  33. */
  34. function tripal_analysis_unregister_child($modulename) {
  35. if (db_table_exists('tripal_analysis')) {
  36. $sql = "DELETE FROM {tripal_analysis} WHERE modulename = '%s'";
  37. db_query($sql, $modulename);
  38. }
  39. }
  40. /**
  41. * Add tripal javascript to page headers
  42. *
  43. * @ingroup tripal_analysis
  44. */
  45. function tripal_analysis_init() {
  46. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_analysis.js');
  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. //Sync analysis
  56. $items['chado_sync_analyses'] = array(
  57. 'title' => 'Sync Data',
  58. 'page callback' => 'tripal_analysis_sync_analyses',
  59. 'access arguments' => array('administer site configuration'),
  60. 'type' => MENU_CALLBACK
  61. );
  62. // Tripal Analysis administrative settings
  63. $items['admin/tripal/tripal_analysis'] = array(
  64. 'title' => 'Analyses',
  65. 'description' => 'Basic Description of Tripal Analysis Module Functionality.',
  66. 'page callback' => 'tripal_analysis_module_description_page',
  67. 'access arguments' => array('administer site configuration'),
  68. 'type' => MENU_NORMAL_ITEM,
  69. 'file' => 'includes/tripal_analysis.admin.inc',
  70. );
  71. $items['admin/tripal/tripal_analysis/configuration'] = array(
  72. 'title' => 'Configuration',
  73. 'description' => 'Settings for the displays of analysis results.',
  74. 'page callback' => 'drupal_get_form',
  75. 'page arguments' => array('tripal_analysis_admin'),
  76. 'access arguments' => array('administer site configuration'),
  77. 'type' => MENU_NORMAL_ITEM,
  78. 'file' => 'includes/tripal_analysis.admin.inc',
  79. );
  80. return $items;
  81. }
  82. /**
  83. * Provide information to drupal about the node types that we're creating
  84. * in this module
  85. *
  86. * @ingroup tripal_analysis
  87. */
  88. function tripal_analysis_node_info() {
  89. $nodes = array();
  90. $nodes['chado_analysis'] = array(
  91. 'name' => t('Analysis'),
  92. 'module' => 'chado_analysis',
  93. 'description' => t('An analysis from the chado database'),
  94. 'has_title' => FALSE,
  95. 'title_label' => t('Analysis'),
  96. 'has_body' => FALSE,
  97. 'body_label' => t('Analysis Description'),
  98. 'locked' => TRUE
  99. );
  100. return $nodes;
  101. }
  102. /**
  103. * When a new chado_analysis node is created we also need to add information
  104. * to our chado_analysis table. This function is called on insert of a new
  105. * node of type 'chado_analysis' and inserts the necessary information.
  106. *
  107. * @ingroup tripal_analysis
  108. */
  109. function chado_analysis_insert($node) {
  110. global $user;
  111. // Create a timestamp so we can insert it into the chado database
  112. $time = $node->timeexecuted;
  113. $month = $time['month'];
  114. $day = $time['day'];
  115. $year = $time['year'];
  116. $timestamp = $month . '/' . $day . '/' . $year;
  117. // If this analysis already exists then don't recreate it in chado
  118. $analysis_id = $node->analysis_id;
  119. if ($analysis_id) {
  120. $values = array('analysis_id' => $node->analysis_id);
  121. $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
  122. if($result and count($result) > 0) {
  123. $analysis = $result[0];
  124. }
  125. }
  126. // If the analysis doesn't exist then let's create it in chado.
  127. if (!$analysis) {
  128. // insert and then get the newly inserted analysis record
  129. $values = array(
  130. 'name' => $node->analysisname,
  131. 'description' => $node->description,
  132. 'program' => $node->program,
  133. 'programversion' => $node->programversion,
  134. 'algorithm' => $node->algorithm,
  135. 'sourcename' => $node->sourcename,
  136. 'sourceversion' => $node->sourceversion,
  137. 'sourceuri' => $node->sourceuri,
  138. 'timeexecuted' => $timestamp
  139. );
  140. if (tripal_core_chado_insert('analysis', $values)) {
  141. $analysis = tripal_core_chado_select('analysis', array('*'), $values);
  142. $analysis_id = $analysis[0]->analysis_id;
  143. }
  144. }
  145. // Make sure the entry for this analysis doesn't already exist in the
  146. // chado_analysis table if it doesn't exist then we want to add it.
  147. $node_check_sql = "SELECT * FROM {chado_analysis} ".
  148. "WHERE analysis_id = %d";
  149. $node_check = db_fetch_object(db_query($node_check_sql, $analysis_id));
  150. if (!$node_check) {
  151. // next add the item to the drupal table
  152. $sql = "INSERT INTO {chado_analysis} (nid, vid, analysis_id) ".
  153. "VALUES (%d, %d, %d)";
  154. db_query($sql, $node->nid, $node->vid, $analysis_id);
  155. // Create a title for the analysis node using the unique keys so when the
  156. // node is saved, it will have a title
  157. $record = new stdClass();
  158. // If the analysis has a name, use it as the node title. If not, construct
  159. // the title using program, programversion, and sourcename
  160. if ($node->analysisname) {
  161. $record->title = $node->analysisname;
  162. }
  163. else {
  164. //Construct node title as "program (version)
  165. $record->title = "$node->program ($node->programversion)";
  166. }
  167. $record->nid = $node->nid;
  168. drupal_write_record('node', $record, 'nid');
  169. drupal_write_record('node_revisions', $record, 'nid');
  170. }
  171. // add the analysis to the node object for
  172. // use by other analysis modules that may be using this function
  173. $node->analysis = $analysis;
  174. $node->analysis_id = $analysis_id; // we need to set this for children
  175. }
  176. /**
  177. * Removes analysis from the chado database
  178. *
  179. * @param $node
  180. * The node object specifying which chado record to delete
  181. *
  182. * @ingroup tripal_analysis
  183. */
  184. function chado_analysis_delete($node) {
  185. $analysis_id = chado_get_id_for_node('analysis', $node);
  186. // if we don't have an organism id for this node then this isn't a node of
  187. // type chado_organism or the entry in the chado_organism table was lost.
  188. if (!$analysis_id) {
  189. return;
  190. }
  191. // Remove data from the {chado_analysis}, {node}, and {node_revisions} tables
  192. $sql_del = "DELETE FROM {chado_analysis} ".
  193. "WHERE nid = %d ".
  194. "AND vid = %d";
  195. db_query($sql_del, $node->nid, $node->vid);
  196. $sql_del = "DELETE FROM {node} ".
  197. "WHERE nid = %d ".
  198. "AND vid = %d";
  199. db_query($sql_del, $node->nid, $node->vid);
  200. $sql_del = "DELETE FROM {node_revisions} ".
  201. "WHERE nid = %d ".
  202. "AND vid = %d";
  203. db_query($sql_del, $node->nid, $node->vid);
  204. //Remove from analysis and analysisprop tables of chado database as well
  205. chado_query("DELETE FROM {analysis} WHERE analysis_id = %d", $analysis_id);
  206. }
  207. /**
  208. * Update analyses
  209. *
  210. * @param $node
  211. * The updated node object
  212. *
  213. * @ingroup tripal_analysis
  214. */
  215. function chado_analysis_update($node) {
  216. global $user;
  217. if ($node->revision) {
  218. // TODO -- decide what to do about revisions
  219. }
  220. // Create a timestamp so we can insert it into the chado database
  221. $time = $node->timeexecuted;
  222. $month = $time['month'];
  223. $day = $time['day'];
  224. $year = $time['year'];
  225. $timestamp = $month . '/' . $day . '/' . $year;
  226. // get the analysis_id for this node:
  227. $sql = "SELECT analysis_id ".
  228. "FROM {chado_analysis} ".
  229. "WHERE nid = %d";
  230. $analysis_id = db_fetch_object(db_query($sql, $node->nid))->analysis_id;
  231. $sql = "UPDATE {analysis} ".
  232. "SET name = '%s', ".
  233. " description = '%s', ".
  234. " program = '%s', ".
  235. " programversion = '%s', ".
  236. " algorithm = '%s', ".
  237. " sourcename = '%s', ".
  238. " sourceversion = '%s', ".
  239. " sourceuri = '%s', ".
  240. " timeexecuted = '%s' ".
  241. "WHERE analysis_id = %d ";
  242. chado_query($sql, $node->analysisname, $node->description, $node->program,
  243. $node->programversion, $node->algorithm, $node->sourcename,
  244. $node->sourceversion, $node->sourceuri, $timestamp, $analysis_id);
  245. // Create a title for the analysis node using the unique keys so when the
  246. // node is saved, it will have a title
  247. $record = new stdClass();
  248. // If the analysis has a name, use it as the node title. If not, construct
  249. // the title using program, programversion, and sourcename
  250. if ($node->analysisname) {
  251. $record->title = $node->analysisname;
  252. }
  253. else {
  254. //Construct node title as "program (version)
  255. $record->title = "$node->program ($node->programversion)";
  256. }
  257. $record->nid = $node->nid;
  258. drupal_write_record('node', $record, 'nid');
  259. drupal_write_record('node_revisions', $record, 'nid');
  260. }
  261. /**
  262. * When editing or creating a new node of type 'chado_analysis' we need
  263. * a form. This function creates the form that will be used for this.
  264. *
  265. * @ingroup tripal_analysis
  266. */
  267. function chado_analysis_form($node) {
  268. $analysis = $node->analysis;
  269. // add in the description column. It is a text field and may not be included
  270. // if the text is too big.
  271. $analysis = tripal_core_expand_chado_vars($analysis, 'field', 'analysis.description');
  272. // get form defaults
  273. $analysis_id = $node->analysis_id;
  274. if (!$analysis_id) {
  275. $analysis_id = $analysis->analysis_id;
  276. }
  277. $analysisname = $node->analysisname;
  278. if (!$analysisname) {
  279. $analysisname = $analysis->name;
  280. }
  281. $program = $node->program;
  282. if (!$program) {
  283. $program = $analysis->program;
  284. }
  285. $programversion = $node->programversion;
  286. if (!$programversion) {
  287. $programversion = $analysis->programversion;
  288. }
  289. $algorithm = $node->algorithm;
  290. if (!$algorithm) {
  291. $algorithm = $analysis->algorithm;
  292. }
  293. $sourcename = $node->sourcename;
  294. if (!$sourcename) {
  295. $sourcename = $analysis->sourcename;
  296. }
  297. $sourceversion = $node->sourceversion;
  298. if (!$sourceversion) {
  299. $sourceversion = $analysis->sourceversion;
  300. }
  301. $sourceuri = $node->sourceuri;
  302. if (!$sourceuri) {
  303. $sourceuri = $analysis->sourceuri;
  304. }
  305. $timeexecuted = $node->timeexecuted;
  306. if (!$timeexecuted) {
  307. $timeexecuted = $analysis->timeexecuted;
  308. }
  309. $description = $node->description;
  310. if (!$description) {
  311. $description = $analysis->description;
  312. }
  313. $form = array();
  314. $form['title']= array(
  315. '#type' => 'hidden',
  316. '#default_value' => $node->title,
  317. );
  318. $form['analysis_id']= array(
  319. '#type' => 'hidden',
  320. '#default_value' => $analysis_id,
  321. );
  322. $form['analysisname']= array(
  323. '#type' => 'textfield',
  324. '#title' => t('Analysis Name'),
  325. '#required' => TRUE,
  326. '#default_value' => $analysisname,
  327. '#description' => t("This should be a brief name that
  328. describes the analysis succintly. This name will helps the user find analyses."),
  329. );
  330. $form['program']= array(
  331. '#type' => 'textfield',
  332. '#title' => t('Program'),
  333. '#required' => TRUE,
  334. '#default_value' => $program,
  335. '#description' => t("Program name, e.g. blastx, blastp, sim4, genscan."),
  336. );
  337. $form['programversion']= array(
  338. '#type' => 'textfield',
  339. '#title' => t('Program Version'),
  340. '#required' => TRUE,
  341. '#default_value' => $programversion,
  342. '#description' => t("Version description, e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter 'n/a' if no version is available."),
  343. );
  344. $form['algorithm']= array(
  345. '#type' => 'textfield',
  346. '#title' => t('Algorithm'),
  347. '#required' => FALSE,
  348. '#default_value' => $algorithm,
  349. '#description' => t("Algorithm name, e.g. blast."),
  350. );
  351. $form['sourcename']= array(
  352. '#type' => 'textfield',
  353. '#title' => t('Source Name'),
  354. '#required' => TRUE,
  355. '#default_value' => $sourcename,
  356. '#description' => t('The name of the source data. This could be a file name, data set name or a
  357. small description for how the data was collected. For long descriptions use the description field below'),
  358. );
  359. $form['sourceversion']= array(
  360. '#type' => 'textfield',
  361. '#title' => t('Source Version'),
  362. '#required' => FALSE,
  363. '#default_value' => $sourceversion,
  364. '#description' => t('If the source dataset has a version, include it here'),
  365. );
  366. $form['sourceuri']= array(
  367. '#type' => 'textfield',
  368. '#title' => t('Source URI'),
  369. '#required' => FALSE,
  370. '#default_value' => $sourceuri,
  371. '#description' => t("This is a permanent URL or URI for the source of the analysis.
  372. Someone could recreate the analysis directly by going to this URI and
  373. fetching the source data (e.g. the blast database, or the training model)."),
  374. );
  375. // Get time saved in chado
  376. $default_time = $timeexecuted;
  377. $year = preg_replace("/^(\d+)-\d+-\d+ .*/", "$1", $default_time);
  378. $month = preg_replace("/^\d+-0?(\d+)-\d+ .*/", "$1", $default_time);
  379. $day = preg_replace("/^\d+-\d+-0?(\d+) .*/", "$1", $default_time);
  380. // If the time is not set, use current time
  381. if (!$default_time) {
  382. $default_time = time();
  383. $year = format_date($default_time, 'custom', 'Y');
  384. $month = format_date($default_time, 'custom', 'n');
  385. $day = format_date($default_time, 'custom', 'j');
  386. }
  387. $form['timeexecuted']= array(
  388. '#type' => 'date',
  389. '#title' => t('Time Executed'),
  390. '#required' => TRUE,
  391. '#default_value' => array(
  392. 'year' => $year,
  393. 'month' => $month,
  394. 'day' => $day,
  395. ),
  396. );
  397. $form['description']= array(
  398. '#type' => 'textarea',
  399. '#rows' => 15,
  400. '#title' => t('Materials & Methods (Description and/or Program Settings)'),
  401. '#required' => FALSE,
  402. '#default_value' => $description,
  403. '#description' => t('Please provide all necessary information to allow
  404. someone to recreate the analysis, including materials and methods
  405. for collection of the source data and performing the analysis'),
  406. );
  407. return $form;
  408. }
  409. /**
  410. * When a node is requested by the user this function is called to allow us
  411. * to add auxiliary data to the node object.
  412. *
  413. * @ingroup tripal_analysis
  414. */
  415. function chado_analysis_load($node) {
  416. // get the feature details from chado
  417. $analysis_id = chado_get_id_for_node('analysis', $node);
  418. $values = array('analysis_id' => $analysis_id);
  419. $analysis = tripal_core_generate_chado_var('analysis', $values);
  420. $additions = new stdClass();
  421. $additions->analysis = $analysis;
  422. return $additions;
  423. }
  424. /**
  425. * This function customizes the view of the chado_analysis node. It allows
  426. * us to generate the markup.
  427. *
  428. * @ingroup tripal_analysis
  429. */
  430. function chado_analysis_view($node, $teaser = FALSE, $page = FALSE) {
  431. // use drupal's default node view:
  432. if (!$teaser) {
  433. $node = node_prepare($node, $teaser);
  434. // When previewing a node submitting form, it shows 'Array' instead of
  435. // correct date format. We need to format the date here
  436. $time = $node->timeexecuted;
  437. if (is_array($time)) {
  438. $month = $time['month'];
  439. $day = $time['day'];
  440. $year = $time['year'];
  441. $timestamp = $year . '-' . $month . '-' . $day;
  442. $node->timeexecuted = $timestamp;
  443. }
  444. }
  445. return $node;
  446. }
  447. /**
  448. * Synchronize analyses from chado to drupal
  449. *
  450. * @ingroup tripal_analysis
  451. */
  452. function tripal_analysis_sync_analyses($analysis_id = NULL, $job_id = NULL) {
  453. global $user;
  454. $page_content = '';
  455. if (!$analysis_id) {
  456. $sql = "SELECT Analysis_id, name, description, program, ".
  457. " programversion, algorithm, sourcename, sourceversion, sourceuri, ".
  458. " timeexecuted ".
  459. "FROM {Analysis} ";
  460. $results = chado_query($sql);
  461. }
  462. else {
  463. $sql = "SELECT Analysis_id, name, description, program, ".
  464. " programversion, algorithm, sourcename, sourceversion, sourceuri, ".
  465. " timeexecuted ".
  466. "FROM {Analysis} ".
  467. "WHERE analysis_id = %d";
  468. $results = chado_query($sql, $analysis_id);
  469. }
  470. // We'll use the following SQL statement for checking if the analysis
  471. // already exists as a drupal node.
  472. $sql = "SELECT * FROM {chado_analysis} ".
  473. "WHERE analysis_id = %d";
  474. while ($analysis = db_fetch_object($results)) {
  475. print "syncing analysis ";
  476. print $analysis->name;
  477. print ", ";
  478. print $analysis->analysis_id;
  479. print "\n";
  480. // check if this analysis already exists in the drupal database. if it
  481. // does then skip this analysis and go to the next one.
  482. if (!db_fetch_object(db_query($sql, $analysis->analysis_id))) {
  483. $new_node = new stdClass();
  484. // try to access analysis type for this analysis
  485. $sql = "SELECT * FROM {analysisprop}
  486. WHERE analysis_id = %d
  487. AND type_id =
  488. (SELECT cvterm_id from {cvterm} where name = '%s')
  489. ";
  490. $analysis_type = db_fetch_object(chado_query($sql, $analysis->analysis_id, "analysis_type"));
  491. // Get the type of analysis using cvterm_id
  492. // Current possibilities: kegg, unigene, interpro, blast
  493. if ($analysis_type) {
  494. // This is a unigene analysis
  495. if ($analysis_type->value == 'tripal_analysis_unigene') {
  496. $new_node->type = 'chado_analysis_unigene';
  497. // This is a blast analysis
  498. }
  499. elseif ($analysis_type->value == 'tripal_analysis_blast') {
  500. $new_node->type = 'chado_analysis_blast';
  501. // This is a interpro analysis
  502. }
  503. elseif ($analysis_type->value == 'tripal_analysis_interpro') {
  504. $new_node->type = 'chado_analysis_interpro';
  505. // This is a kegg analysis
  506. }
  507. elseif ($analysis_type->value == 'tripal_analysis_kegg' ) {
  508. $new_node->type = 'chado_analysis_kegg';
  509. }
  510. else {
  511. $new_node->type = 'chado_analysis';
  512. }
  513. // If it doesn't exist, this analysis is generic
  514. }
  515. else {
  516. $new_node->type = 'chado_analysis';
  517. }
  518. print "analysis type is $new_node->type\n";
  519. $new_node->uid = $user->uid;
  520. $new_node->analysis_id = $analysis->analysis_id;
  521. $new_node->analysisname = $analysis->name;
  522. $new_node->description = $analysis->description;
  523. $new_node->program = $analysis->program;
  524. $new_node->programversion = $analysis->programversion;
  525. $new_node->algorithm = $analysis->algorithm;
  526. $new_node->sourcename = $analysis->sourcename;
  527. $new_node->sourceversion = $analysis->sourceversion;
  528. $new_node->sourceuri = $analysis->sourceuri;
  529. $new_node->timeexecuted = $analysis->timeexecuted;
  530. // If the analysis has a name, use it as the node title. If not,
  531. // construct the title using program, programversion, and sourcename
  532. if ($new_node->analysisname) {
  533. $new_node->title = $new_node->analysisname;
  534. }
  535. else {
  536. //Construct node title as "program (version)"
  537. $new_node->title = "$analysis->program ($analysis->programversion)";
  538. }
  539. node_validate($new_node);
  540. $errors = form_get_errors();
  541. if ($errors) {
  542. print_r($errors);
  543. }
  544. else{
  545. // if(!form_get_errors()){
  546. $node = node_submit($new_node);
  547. node_save($node);
  548. if ($node->nid) {
  549. $page_content .= "Added $new_node->title<br />";
  550. }
  551. }
  552. }
  553. else {
  554. $page_content .= "Skipped $new_node->title<br />";
  555. }
  556. }
  557. return $page_content;
  558. }
  559. /**
  560. * Validates the user input before creating an analysis node
  561. *
  562. * @ingroup tripal_analysis
  563. */
  564. function chado_analysis_validate($node, &$form) {
  565. // use the analysis parent to validate the node
  566. tripal_analysis_validate($node, $form);
  567. }
  568. /**
  569. * This validation is being used for three activities:
  570. * CASE A: Update a node that exists in both drupal and chado
  571. * CASE B: Synchronizing a node from chado to drupal
  572. * CASE C: Inserting a new node that exists in niether drupal nor chado
  573. *
  574. * @ingroup tripal_analysis
  575. */
  576. function tripal_analysis_validate($node, &$form) {
  577. // Only nodes being updated will have an nid already
  578. if (!is_null($node->nid)) {
  579. // CASE A: We are validating a form for updating an existing node
  580. // get the existing node
  581. $values = array('analysis_id' => $node->analysis_id);
  582. $result = tripal_core_chado_select('analysis', array('*'), $values);
  583. $analysis = $result[0];
  584. // if the name has changed make sure it doesn't conflict with an existing name
  585. if($analysis->name != $node->analysisname) {
  586. $values = array('name' => $node->analysisname);
  587. $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
  588. if($result and count($result) > 0) {
  589. form_set_error('analysisname', 'Cannot update the analysis with this analysis name. An analysis with this name already exists.');
  590. return;
  591. }
  592. }
  593. // if the unique constraint has changed check to make sure it doesn't conflict with an
  594. // existing record
  595. if($analysis->program != $node->program or $analysis->programversion != $node->programversion or
  596. $analysis->sourcename != $node->sourcename) {
  597. $values = array(
  598. 'program' => $node->program,
  599. 'programversion' => $node->programversion,
  600. 'sourcename' => $node->sourcename,
  601. );
  602. $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
  603. if ($result and count($result) > 0) {
  604. if ($analysis->program != $node->program) {
  605. $field = 'program';
  606. }
  607. if ($analysis->programversion != $node->programversion) {
  608. $field = 'programversion';
  609. }
  610. if ($analysis->sourcename != $node->sourcename) {
  611. $field = 'sourcename';
  612. }
  613. form_set_error($field, 'Cannot update the analysis with this program,
  614. program version and source name. An analysis with these values already exists.');
  615. return;
  616. }
  617. }
  618. }
  619. else{
  620. // To differentiate if we are syncing or creating a new analysis altogther, see if an
  621. // analysis_id already exists
  622. if ($node->analysis_id and $node->analysis_id != 0) {
  623. // CASE B: Synchronizing a node from chado to drupal
  624. // we don't need to do anything.
  625. }
  626. else {
  627. // CASE C: We are validating a form for inserting a new node
  628. // The unique constraint for the chado analysis table is: program, programversion, sourcename
  629. $values = array(
  630. 'program' => $node->program,
  631. 'programversion' => $node->programversion,
  632. 'sourcename' => $node->sourcename,
  633. );
  634. $analysis = tripal_core_chado_select('analysis', array('analysis_id'), $values);
  635. if ($analysis and count($analysis) > 0) {
  636. form_set_error('program', 'Cannot add the analysis with this program,
  637. program version and source name. An analysis with these values already exists.');
  638. return;
  639. }
  640. // make sure we have a unique analysis name. This is not a requirement
  641. // for the analysis table but we use the analysis name for the Drupal node
  642. // title, so it should be unique
  643. $values = array('name' => $node->analysisname);
  644. $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
  645. if($result and count($result) > 0) {
  646. form_set_error('analysisname', 'Cannot add the analysis with this analysis name. An analysis with this name already exists.');
  647. return;
  648. }
  649. }
  650. }
  651. }
  652. /**
  653. * Display help and module information
  654. * @param path which path of the site we're displaying help
  655. * @param arg array that holds the current path as would be returned from arg()
  656. * function
  657. * @return help text for the path
  658. *
  659. * @ingroup tripal_analysis
  660. */
  661. function tripal_analysis_help($path, $arg) {
  662. $output = '';
  663. switch ($path) {
  664. case "admin/help#tripal_analysis":
  665. $output = '<p>' .
  666. t("Displays links to nodes created on this date") .
  667. '</p>';
  668. break;
  669. }
  670. return $output;
  671. }
  672. /**
  673. * The following function proves access control for users trying to
  674. * perform actions on data managed by this module
  675. *
  676. * @ingroup tripal_analysis
  677. */
  678. function chado_analysis_access($op, $node, $account) {
  679. if ($op == 'create') {
  680. if (!user_access('create chado_analysis content', $account)) {
  681. return FALSE;
  682. }
  683. }
  684. if ($op == 'update') {
  685. if (!user_access('edit chado_analysis content', $account)) {
  686. return FALSE;
  687. }
  688. }
  689. if ($op == 'delete') {
  690. if (!user_access('delete chado_analysis content', $account)) {
  691. return FALSE;
  692. }
  693. }
  694. if ($op == 'view') {
  695. if (!user_access('access chado_analysis content', $account)) {
  696. return FALSE;
  697. }
  698. }
  699. return NULL;
  700. }
  701. /**
  702. * Set the permission types that the chado module uses. Essentially we
  703. * want permissionis that protect creation, editing and deleting of chado
  704. * data objects
  705. *
  706. * @ingroup tripal_analysis
  707. */
  708. function tripal_analysis_perm() {
  709. return array(
  710. 'access chado_analysis content',
  711. 'create chado_analysis content',
  712. 'delete chado_analysis content',
  713. 'edit chado_analysis content',
  714. );
  715. }
  716. /**
  717. * We need to let drupal know about our theme functions and their arguments.
  718. * We create theme functions to allow users of the module to customize the
  719. * look and feel of the output generated in this module
  720. *
  721. * @ingroup tripal_analysis
  722. */
  723. function tripal_analysis_theme() {
  724. return array(
  725. 'tripal_analysis_base' => array(
  726. 'arguments' => array('node' => NULL),
  727. 'template' => 'tripal_analysis_base',
  728. ),
  729. 'tripal_feature_analyses' => array(
  730. 'template' => 'tripal_feature_analyses',
  731. 'arguments' => array('node' => NULL),
  732. ),
  733. );
  734. }
  735. /**
  736. *
  737. *
  738. * @ingroup tripal_feature
  739. */
  740. function tripal_analysis_block($op = 'list', $delta = 0, $edit=array()) {
  741. switch ($op) {
  742. case 'list':
  743. $blocks['base']['info'] = t('Tripal Analysis Details');
  744. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  745. $blocks['featureblast']['info'] = t('Tripal Feature Analyses');
  746. $blocks['featureblast']['cache'] = BLOCK_NO_CACHE;
  747. return $blocks;
  748. case 'view':
  749. if (user_access('access chado_analysis content') and arg(0) == 'node' and is_numeric(arg(1))) {
  750. $nid = arg(1);
  751. $node = node_load($nid);
  752. $block = array();
  753. switch ($delta) {
  754. case 'base':
  755. $block['subject'] = t('Analysis Details');
  756. $block['content'] = theme('tripal_analysis_base', $node);
  757. break;
  758. case 'tripal_feature_analyses':
  759. $block['subject'] = t('Feature Analyses');
  760. $block['content'] = theme('tripal_feature_analyses', $node);
  761. break;
  762. default :
  763. }
  764. return $block;
  765. }
  766. }
  767. }
  768. /**
  769. * This function uses analysis_id's of all drupal analysis nodes as input and
  770. * pull the analysis information (name, description, program, programversion,
  771. * algorithm, sourcename, sourceversion, sourceuri, timeexecuted) from
  772. * chado database. The return type is an object array that stores $analysis
  773. * objects sorted by program
  774. *
  775. * @ingroup tripal_analysis
  776. */
  777. function get_chado_analyses() {
  778. $sql_drupal = "SELECT COUNT (analysis_id) FROM {chado_analysis}";
  779. $no_orgs = db_result(db_query($sql_drupal));
  780. if ($no_orgs != 0) {
  781. $sql = "SELECT analysis_id, CA.nid, type FROM {chado_analysis} CA INNER JOIN node ON CA.nid = node.nid";
  782. $result = db_query($sql);
  783. $sql = "SELECT Analysis_id, name, description, program,
  784. programversion, algorithm, sourcename, sourceversion,
  785. sourceuri, timeexecuted
  786. FROM {Analysis} WHERE analysis_id=%d";
  787. $analyses = array();
  788. $count = 0;
  789. while ($data = db_fetch_object($result)) {
  790. $analysis = db_fetch_object(chado_query($sql, $data->analysis_id));
  791. $analysis->node_id = $data->nid;
  792. $analysis->node_type = $data->type;
  793. // Use node_type as the key so we can sort by node type
  794. // Since node_type is not unique by itself, we need to add
  795. // $count to the key
  796. $sortedBy = $analysis->timeexecuted;
  797. $analyses ["$sortedBy$count"] = $analysis;
  798. $count ++;
  799. }
  800. //Sort analyses by time, descending order
  801. krsort($analyses, SORT_STRING);
  802. return $analyses;
  803. }
  804. }
  805. /**
  806. * Remove orphaned drupal nodes
  807. *
  808. * @param $dummy
  809. * Not Used -kept for backwards compatibility
  810. * @param $job_id
  811. * The id of the tripal job executing this function
  812. *
  813. * @ingroup tripal_analysis
  814. */
  815. function tripal_analyses_cleanup($dummy = NULL, $job_id = NULL) {
  816. return tripal_core_clean_orphaned_nodes('analysis', $job_id);
  817. }
  818. /*******************************************************************************
  819. * tripal_analysis_nodeapi()
  820. * HOOK: Implementation of hook_nodeapi()
  821. * Display blast results for allowed node types
  822. */
  823. function tripal_analysis_nodeapi(&$node, $op, $teaser, $page) {
  824. switch ($op) {
  825. case 'view':
  826. if ($teaser) {
  827. return '';
  828. }
  829. // Abort if this node is not one of the types we should show.
  830. if (strcmp($node->type, 'chado_feature') == 0) {
  831. if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
  832. // return results for searching
  833. }
  834. else {
  835. // return normal results
  836. $node->content['tripal_feature_analyses'] = array(
  837. '#value' => theme('tripal_feature_analyses', $node),
  838. '#weight' => 8
  839. );
  840. }
  841. }
  842. break;
  843. }
  844. }
  845. /**
  846. * Implements hook_views_api()
  847. * Purpose: Essentially this hook tells drupal that there is views support for
  848. * for this module which then includes tripal_analysis.views.inc where all the
  849. * views integration code is
  850. *
  851. * @ingroup tripal_analysis
  852. */
  853. function tripal_analysis_views_api() {
  854. return array(
  855. 'api' => 2.0,
  856. );
  857. }