tripal_analysis.module 31 KB

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