tripal_analysis.module 30 KB

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