tripal_analysis.module 30 KB

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