tripal_analysis.module 29 KB

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