tripal_analysis.module 30 KB

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