tripal_analysis.module 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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. // Display available analyses
  56. $items['analyses'] = array(
  57. 'menu_name' => ('primary-links'), //Enable the 'Analysis' primary link
  58. 'title' => 'Analyses',
  59. 'page callback' => 'tripal_analysis_show_analyses',
  60. 'access arguments' => array('access chado_analysis content'),
  61. 'type' => MENU_NORMAL_ITEM
  62. );
  63. //Sync analysis
  64. $items['chado_sync_analyses'] = array(
  65. 'title' => 'Sync Data',
  66. 'page callback' => 'tripal_analysis_sync_analyses',
  67. 'access arguments' => array('administer site configuration'),
  68. 'type' => MENU_CALLBACK
  69. );
  70. // Tripal Analysis administrative settings
  71. $items['admin/tripal/tripal_analysis'] = array(
  72. 'title' => 'Analyses',
  73. 'description' => 'Basic Description of Tripal Analysis Module Functionality.',
  74. 'page callback' => 'tripal_analysis_module_description_page',
  75. 'access arguments' => array('administer site configuration'),
  76. 'type' => MENU_NORMAL_ITEM,
  77. 'file' => 'tripal_analysis.admin.inc',
  78. );
  79. $items['admin/tripal/tripal_analysis/configuration'] = array(
  80. 'title' => 'Configuration',
  81. 'description' => 'Settings for the displays of analysis results.',
  82. 'page callback' => 'drupal_get_form',
  83. 'page arguments' => array('tripal_analysis_admin'),
  84. 'access arguments' => array('administer site configuration'),
  85. 'type' => MENU_NORMAL_ITEM,
  86. 'file' => 'tripal_analysis.admin.inc',
  87. );
  88. return $items;
  89. }
  90. /**
  91. * Display the summary view of analyses when click on the 'Analyses'
  92. * primary-link
  93. *
  94. * @ingroup tripal_analysis
  95. */
  96. function tripal_analysis_show_analyses() {
  97. // Show libraries stored in Drupal's {chado_analysis} table
  98. $sql = "SELECT COUNT(analysis_id) FROM {chado_analysis}";
  99. $no_ana = db_result(db_query($sql));
  100. if ($no_ana != 0) {
  101. $analyses = get_chado_analyses();
  102. return theme('tripal_analysis_analysis_page', $analyses);
  103. }
  104. else {
  105. return t("No analysis available at this time.");
  106. }
  107. }
  108. /**
  109. * Provide information to drupal about the node types that we're creating
  110. * in this module
  111. *
  112. * @ingroup tripal_analysis
  113. */
  114. function tripal_analysis_node_info() {
  115. $nodes = array();
  116. $nodes['chado_analysis'] = array(
  117. 'name' => t('Analysis'),
  118. 'module' => 'chado_analysis',
  119. 'description' => t('An analysis from the chado database'),
  120. 'has_title' => FALSE,
  121. 'title_label' => t('Analysis'),
  122. 'has_body' => FALSE,
  123. 'body_label' => t('Analysis Description'),
  124. 'locked' => TRUE
  125. );
  126. return $nodes;
  127. }
  128. /**
  129. * When a new chado_analysis node is created we also need to add information
  130. * to our chado_analysis table. This function is called on insert of a new
  131. * node of type 'chado_analysis' and inserts the necessary information.
  132. *
  133. * @ingroup tripal_analysis
  134. */
  135. function chado_analysis_insert($node) {
  136. global $user;
  137. // Create a timestamp so we can insert it into the chado database
  138. $time = $node->timeexecuted;
  139. $month = $time['month'];
  140. $day = $time['day'];
  141. $year = $time['year'];
  142. $timestamp = $month . '/' . $day . '/' . $year;
  143. // If this analysis already exists then don't recreate it in chado
  144. $analysis_id = $node->analysis_id;
  145. if ($analysis_id) {
  146. $sql = "SELECT analysis_id ".
  147. "FROM {Analysis} ".
  148. "WHERE analysis_id = %d ";
  149. $previous_db = tripal_db_set_active('chado');
  150. $analysis = db_fetch_object(db_query($sql, $node->analysis_id));
  151. tripal_db_set_active($previous_db);
  152. }
  153. // If the analysis doesn't exist then let's create it in chado.
  154. if (!$analysis) {
  155. // insert and then get the newly inserted analysis record
  156. $values = array(
  157. 'name' => $node->analysisname,
  158. 'description' => $node->description,
  159. 'program' => $node->program,
  160. 'programversion' => $node->programversion,
  161. 'algorithm' => $node->algorithm,
  162. 'sourcename' => $node->sourcename,
  163. 'sourceversion' => $node->sourceversion,
  164. 'sourceuri' => $node->sourceuri,
  165. 'timeexecuted' => $timestamp
  166. );
  167. if (tripal_core_chado_insert('analysis', $values)) {
  168. $analysis = tripal_core_chado_select('analysis', array('*'), $values);
  169. $analysis_id = $analysis[0]->analysis_id;
  170. }
  171. }
  172. // Make sure the entry for this analysis doesn't already exist in the
  173. // chado_analysis table if it doesn't exist then we want to add it.
  174. $node_check_sql = "SELECT * FROM {chado_analysis} ".
  175. "WHERE analysis_id = %d";
  176. $node_check = db_fetch_object(db_query($node_check_sql, $analysis_id));
  177. if (!$node_check) {
  178. // next add the item to the drupal table
  179. $sql = "INSERT INTO {chado_analysis} (nid, vid, analysis_id) ".
  180. "VALUES (%d, %d, %d)";
  181. db_query($sql, $node->nid, $node->vid, $analysis_id);
  182. // Create a title for the analysis node using the unique keys so when the
  183. // node is saved, it will have a title
  184. $record = new stdClass();
  185. // If the analysis has a name, use it as the node title. If not, construct
  186. // the title using program, programversion, and sourcename
  187. if ($node->analysisname) {
  188. $record->title = $node->analysisname;
  189. }
  190. else {
  191. //Construct node title as "program (version)
  192. $record->title = "$node->program ($node->programversion)";
  193. }
  194. $record->nid = $node->nid;
  195. drupal_write_record('node', $record, 'nid');
  196. drupal_write_record('node_revisions', $record, 'nid');
  197. }
  198. if ($node->setpermissions) {
  199. $job_args[0] = $analysis_id;
  200. $job_args[1] = $node->nid;
  201. tripal_add_job("Set permission for analysis associated features", 'tripal_analysis',
  202. 'tripal_analysis_set_feature_permission', $job_args, $user->uid);
  203. }
  204. // add the analysis to the node object for
  205. // use by other analysis modules that may be using this function
  206. $node->analysis = $analysis;
  207. $node->analysis_id = $analysis_id; // we need to set this for children
  208. }
  209. /**
  210. * Removes analysis from the chado database
  211. *
  212. * @param $node
  213. * The node object specifying which chado record to delete
  214. *
  215. * @ingroup tripal_analysis
  216. */
  217. function chado_analysis_delete($node) {
  218. // Before removing, get analysis_id so we can remove it from chado database
  219. // later
  220. $sql_drupal = "SELECT analysis_id ".
  221. "FROM {chado_analysis} ".
  222. "WHERE nid = %d ".
  223. "AND vid = %d";
  224. $analysis_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
  225. // Remove data from the {chado_analysis}, {node}, and {node_revisions} tables
  226. $sql_del = "DELETE FROM {chado_analysis} ".
  227. "WHERE nid = %d ".
  228. "AND vid = %d";
  229. db_query($sql_del, $node->nid, $node->vid);
  230. $sql_del = "DELETE FROM {node} ".
  231. "WHERE nid = %d ".
  232. "AND vid = %d";
  233. db_query($sql_del, $node->nid, $node->vid);
  234. $sql_del = "DELETE FROM {node_revisions} ".
  235. "WHERE nid = %d ".
  236. "AND vid = %d";
  237. db_query($sql_del, $node->nid, $node->vid);
  238. //Remove from analysis and analysisprop tables of chado database as well
  239. $previous_db = tripal_db_set_active('chado');
  240. db_query("DELETE FROM {analysis} WHERE analysis_id = %d", $analysis_id);
  241. tripal_db_set_active($previous_db);
  242. }
  243. /**
  244. * Update analyses
  245. *
  246. * @param $node
  247. * The updated node object
  248. *
  249. * @ingroup tripal_analysis
  250. */
  251. function chado_analysis_update($node) {
  252. global $user;
  253. if ($node->revision) {
  254. // TODO -- decide what to do about revisions
  255. }
  256. // Create a timestamp so we can insert it into the chado database
  257. $time = $node->timeexecuted;
  258. $month = $time['month'];
  259. $day = $time['day'];
  260. $year = $time['year'];
  261. $timestamp = $month . '/' . $day . '/' . $year;
  262. // get the analysis_id for this node:
  263. $sql = "SELECT analysis_id ".
  264. "FROM {chado_analysis} ".
  265. "WHERE nid = %d";
  266. $analysis_id = db_fetch_object(db_query($sql, $node->nid))->analysis_id;
  267. $sql = "UPDATE {analysis} ".
  268. "SET name = '%s', ".
  269. " description = '%s', ".
  270. " program = '%s', ".
  271. " programversion = '%s', ".
  272. " algorithm = '%s', ".
  273. " sourcename = '%s', ".
  274. " sourceversion = '%s', ".
  275. " sourceuri = '%s', ".
  276. " timeexecuted = '%s' ".
  277. "WHERE analysis_id = %d ";
  278. $previous_db = tripal_db_set_active('chado'); // use chado database
  279. db_query($sql, $node->analysisname, $node->description, $node->program,
  280. $node->programversion, $node->algorithm, $node->sourcename,
  281. $node->sourceversion, $node->sourceuri, $timestamp, $analysis_id);
  282. tripal_db_set_active($previous_db); // switch back to drupal database
  283. // Create a title for the analysis node using the unique keys so when the
  284. // node is saved, it will have a title
  285. $record = new stdClass();
  286. // If the analysis has a name, use it as the node title. If not, construct
  287. // the title using program, programversion, and sourcename
  288. if ($node->analysisname) {
  289. $record->title = $node->analysisname;
  290. }
  291. else {
  292. //Construct node title as "program (version)
  293. $record->title = "$node->program ($node->programversion)";
  294. }
  295. if ($node->setpermissions) {
  296. $job_args[0] = $analysis_id;
  297. $job_args[1] = $node->nid;
  298. tripal_add_job("Set permission for analysis associated features", 'tripal_analysis',
  299. 'tripal_analysis_set_feature_permission', $job_args, $user->uid);
  300. }
  301. $record->nid = $node->nid;
  302. drupal_write_record('node', $record, 'nid');
  303. drupal_write_record('node_revisions', $record, 'nid');
  304. }
  305. /**
  306. * When editing or creating a new node of type 'chado_analysis' we need
  307. * a form. This function creates the form that will be used for this.
  308. *
  309. * @ingroup tripal_analysis
  310. */
  311. function chado_analysis_form($node) {
  312. $analysis = $node->analysis;
  313. // add in the description column. It is a text field and may not be included
  314. // if the text is too big.
  315. $analysis = tripal_core_expand_chado_vars($analysis, 'field', 'analysis.description');
  316. // get form defaults
  317. $analysis_id = $node->analysis_id;
  318. if (!$analysis_id) {
  319. $analysis_id = $analysis->analysis_id;
  320. }
  321. $analysisname = $node->analysisname;
  322. if (!$analysisname) {
  323. $analysisname = $analysis->name;
  324. }
  325. $program = $node->program;
  326. if (!$program) {
  327. $program = $analysis->program;
  328. }
  329. $programversion = $node->programversion;
  330. if (!$programversion) {
  331. $programversion = $analysis->programversion;
  332. }
  333. $algorithm = $node->algorithm;
  334. if (!$algorithm) {
  335. $algorithm = $analysis->algorithm;
  336. }
  337. $sourcename = $node->sourcename;
  338. if (!$sourcename) {
  339. $sourcename = $analysis->sourcename;
  340. }
  341. $sourceversion = $node->sourceversion;
  342. if (!$sourceversion) {
  343. $sourceversion = $analysis->sourceversion;
  344. }
  345. $sourceuri = $node->sourceuri;
  346. if (!$sourceuri) {
  347. $sourceuri = $analysis->sourceuri;
  348. }
  349. $timeexecuted = $node->timeexecuted;
  350. if (!$timeexecuted) {
  351. $timeexecuted = $analysis->timeexecuted;
  352. }
  353. $description = $node->description;
  354. if (!$description) {
  355. $description = $analysis->description;
  356. }
  357. $form = array();
  358. $form['title']= array(
  359. '#type' => 'hidden',
  360. '#default_value' => $node->title,
  361. );
  362. $form['analysis_id']= array(
  363. '#type' => 'hidden',
  364. '#default_value' => $analysis_id,
  365. );
  366. $form['analysisname']= array(
  367. '#type' => 'textfield',
  368. '#title' => t('Analysis Name'),
  369. '#required' => FALSE,
  370. '#default_value' => $analysisname,
  371. '#description' => t("This should be a handy short identifier that
  372. describes the analysis succintly as possible which helps the user find analyses."),
  373. );
  374. $form['program']= array(
  375. '#type' => 'textfield',
  376. '#title' => t('Program'),
  377. '#required' => TRUE,
  378. '#default_value' => $program,
  379. '#description' => t("Program name, e.g. blastx, blastp, sim4, genscan."),
  380. );
  381. $form['programversion']= array(
  382. '#type' => 'textfield',
  383. '#title' => t('Program Version'),
  384. '#required' => TRUE,
  385. '#default_value' => $programversion,
  386. '#description' => t("Version description, e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]"),
  387. );
  388. $form['algorithm']= array(
  389. '#type' => 'textfield',
  390. '#title' => t('Algorithm'),
  391. '#required' => FALSE,
  392. '#default_value' => $algorithm,
  393. '#description' => t("Algorithm name, e.g. blast."),
  394. );
  395. $form['sourcename']= array(
  396. '#type' => 'textfield',
  397. '#title' => t('Source Name'),
  398. '#required' => TRUE,
  399. '#default_value' => $sourcename,
  400. '#description' => t('The name of the source data. This could be a file name, data set name or a
  401. small description for how the data was collected. For long descriptions use the description field below'),
  402. );
  403. $form['sourceversion']= array(
  404. '#type' => 'textfield',
  405. '#title' => t('Source Version'),
  406. '#required' => FALSE,
  407. '#default_value' => $sourceversion,
  408. '#description' => t('If the source dataset has a version, include it here'),
  409. );
  410. $form['sourceuri']= array(
  411. '#type' => 'textfield',
  412. '#title' => t('Source URI'),
  413. '#required' => FALSE,
  414. '#default_value' => $sourceuri,
  415. '#description' => t("This is a permanent URL or URI for the source of the analysis.
  416. Someone could recreate the analysis directly by going to this URI and
  417. fetching the source data (e.g. the blast database, or the training model)."),
  418. );
  419. // Get time saved in chado
  420. $default_time = $timeexecuted;
  421. $year = preg_replace("/^(\d+)-\d+-\d+ .*/", "$1", $default_time);
  422. $month = preg_replace("/^\d+-0?(\d+)-\d+ .*/", "$1", $default_time);
  423. $day = preg_replace("/^\d+-\d+-0?(\d+) .*/", "$1", $default_time);
  424. // If the time is not set, use current time
  425. if (!$default_time) {
  426. $default_time = time();
  427. $year = format_date($default_time, 'custom', 'Y');
  428. $month = format_date($default_time, 'custom', 'n');
  429. $day = format_date($default_time, 'custom', 'j');
  430. }
  431. $form['timeexecuted']= array(
  432. '#type' => 'date',
  433. '#title' => t('Time Executed'),
  434. '#required' => TRUE,
  435. '#default_value' => array(
  436. 'year' => $year,
  437. 'month' => $month,
  438. 'day' => $day,
  439. ),
  440. );
  441. $form['description']= array(
  442. '#type' => 'textarea',
  443. '#rows' => 15,
  444. '#title' => t('Description and/or Program Settings'),
  445. '#required' => FALSE,
  446. '#default_value' => $description,
  447. '#description' => t('Please provide all necessary information to allow
  448. someone to recreate the analysis, including materials and methods
  449. for collection of the source data and performing the analysis'),
  450. );
  451. /* Set permissions for all features associated with this analysis */
  452. if (module_exists('node_privacy_byrole')) {
  453. $form['setpermissions'] = array(
  454. '#type' => 'checkbox',
  455. '#title' => t('Submit a job to set the same permissions for all features associated with this analysis'),
  456. '#default_value' => FALSE,
  457. '#weight' => 10
  458. );
  459. }
  460. return $form;
  461. }
  462. /**
  463. * When a node is requested by the user this function is called to allow us
  464. * to add auxiliary data to the node object.
  465. *
  466. * @ingroup tripal_analysis
  467. */
  468. function chado_analysis_load($node) {
  469. // get the feature details from chado
  470. $analysis_id = chado_get_id_for_node('analysis', $node);
  471. $values = array('analysis_id' => $analysis_id);
  472. $analysis = tripal_core_generate_chado_var('analysis', $values);
  473. $additions = new stdClass();
  474. $additions->analysis = $analysis;
  475. return $additions;
  476. }
  477. /**
  478. * This function customizes the view of the chado_analysis node. It allows
  479. * us to generate the markup.
  480. *
  481. * @ingroup tripal_analysis
  482. */
  483. function chado_analysis_view($node, $teaser = FALSE, $page = FALSE) {
  484. // use drupal's default node view:
  485. if (!$teaser) {
  486. $node = node_prepare($node, $teaser);
  487. // When previewing a node submitting form, it shows 'Array' instead of
  488. // correct date format. We need to format the date here
  489. $time = $node->timeexecuted;
  490. if (is_array($time)) {
  491. $month = $time['month'];
  492. $day = $time['day'];
  493. $year = $time['year'];
  494. $timestamp = $year . '-' . $month . '-' . $day;
  495. $node->timeexecuted = $timestamp;
  496. }
  497. }
  498. return $node;
  499. }
  500. /**
  501. * Synchronize analyses from chado to drupal
  502. *
  503. * @ingroup tripal_analysis
  504. */
  505. function tripal_analysis_sync_analyses($analysis_id = NULL, $job_id = NULL) {
  506. global $user;
  507. $page_content = '';
  508. if (!$analysis_id) {
  509. $sql = "SELECT Analysis_id, name, description, program, ".
  510. " programversion, algorithm, sourcename, sourceversion, sourceuri, ".
  511. " timeexecuted ".
  512. "FROM {Analysis} ";
  513. $previous_db = tripal_db_set_active('chado'); // use chado database
  514. $results = db_query($sql);
  515. tripal_db_set_active($previous_db); // now use drupal database
  516. }
  517. else {
  518. $sql = "SELECT Analysis_id, name, description, program, ".
  519. " programversion, algorithm, sourcename, sourceversion, sourceuri, ".
  520. " timeexecuted ".
  521. "FROM {Analysis} ".
  522. "WHERE analysis_id = %d";
  523. $previous_db = tripal_db_set_active('chado'); // use chado database
  524. $results = db_query($sql, $analysis_id);
  525. tripal_db_set_active($previous_db); // now use drupal database
  526. }
  527. // We'll use the following SQL statement for checking if the analysis
  528. // already exists as a drupal node.
  529. $sql = "SELECT * FROM {chado_analysis} ".
  530. "WHERE analysis_id = %d";
  531. while ($analysis = db_fetch_object($results)) {
  532. print "syncing analysis ";
  533. print $analysis->name;
  534. print ", ";
  535. print $analysis->analysis_id;
  536. print "\n";
  537. // check if this analysis already exists in the drupal database. if it
  538. // does then skip this analysis and go to the next one.
  539. if (!db_fetch_object(db_query($sql, $analysis->analysis_id))) {
  540. $new_node = new stdClass();
  541. // try to access analysis type for this analysis
  542. $sql = "SELECT * FROM {analysisprop}
  543. WHERE analysis_id = %d
  544. AND type_id =
  545. (SELECT cvterm_id from {cvterm} where name = '%s')
  546. ";
  547. $previous_db = tripal_db_set_active('chado');
  548. $analysis_type = db_fetch_object(db_query($sql, $analysis->analysis_id, "analysis_type"));
  549. tripal_db_set_active($previous_db);
  550. // Get the type of analysis using cvterm_id
  551. // Current possibilities: kegg, unigene, interpro, blast
  552. if ($analysis_type) {
  553. // This is a unigene analysis
  554. if ($analysis_type->value == 'tripal_analysis_unigene') {
  555. $new_node->type = 'chado_analysis_unigene';
  556. // This is a blast analysis
  557. }
  558. elseif ($analysis_type->value == 'tripal_analysis_blast') {
  559. $new_node->type = 'chado_analysis_blast';
  560. // This is a interpro analysis
  561. }
  562. elseif ($analysis_type->value == 'tripal_analysis_interpro') {
  563. $new_node->type = 'chado_analysis_interpro';
  564. // This is a kegg analysis
  565. }
  566. elseif ($analysis_type->value == 'tripal_analysis_kegg' ) {
  567. $new_node->type = 'chado_analysis_kegg';
  568. }
  569. else {
  570. $new_node->type = 'chado_analysis';
  571. }
  572. // If it doesn't exist, this analysis is generic
  573. }
  574. else {
  575. $new_node->type = 'chado_analysis';
  576. }
  577. print "analysis type is $new_node->type\n";
  578. $new_node->uid = $user->uid;
  579. $new_node->analysis_id = $analysis->analysis_id;
  580. $new_node->analysisname = $analysis->name;
  581. $new_node->description = $analysis->description;
  582. $new_node->program = $analysis->program;
  583. $new_node->programversion = $analysis->programversion;
  584. $new_node->algorithm = $analysis->algorithm;
  585. $new_node->sourcename = $analysis->sourcename;
  586. $new_node->sourceversion = $analysis->sourceversion;
  587. $new_node->sourceuri = $analysis->sourceuri;
  588. $new_node->timeexecuted = $analysis->timeexecuted;
  589. // If the analysis has a name, use it as the node title. If not,
  590. // construct the title using program, programversion, and sourcename
  591. if ($new_node->analysisname) {
  592. $new_node->title = $new_node->analysisname;
  593. }
  594. else {
  595. //Construct node title as "program (version)"
  596. $new_node->title = "$analysis->program ($analysis->programversion)";
  597. }
  598. node_validate($new_node);
  599. $errors = form_get_errors();
  600. if ($errors) {
  601. print_r($errors);
  602. }
  603. else{
  604. // if(!form_get_errors()){
  605. $node = node_submit($new_node);
  606. node_save($node);
  607. if ($node->nid) {
  608. $page_content .= "Added $new_node->title<br />";
  609. }
  610. }
  611. }
  612. else {
  613. $page_content .= "Skipped $new_node->title<br />";
  614. }
  615. }
  616. return $page_content;
  617. }
  618. /**
  619. * Validates the user input before creating an analysis node
  620. *
  621. * @ingroup tripal_analysis
  622. */
  623. function chado_analysis_validate($node, &$form) {
  624. // use the analysis parent to validate the node
  625. tripal_analysis_validate($node, $form);
  626. }
  627. /**
  628. * This validation is being used for three activities:
  629. * CASE A: Update a node that exists in both drupal and chado
  630. * CASE B: Synchronizing a node from chado to drupal
  631. * CASE C: Inserting a new node that exists in niether drupal nor chado
  632. *
  633. * @ingroup tripal_analysis
  634. */
  635. function tripal_analysis_validate($node, &$form) {
  636. // Only nodes being updated will have an nid already
  637. if ($node->nid) {
  638. //---------------------------------------------------
  639. // CASE A: We are validating a form for updating an existing node
  640. //---------------------------------------------------
  641. // TO DO: check that the new fields don't yield a non-unique primary key in chado
  642. }
  643. else{
  644. // To differentiate if we are syncing or creating a new analysis altogther, see if an
  645. // analysis_id already exists
  646. if ($node->analysis_id) {
  647. //---------------------------------------------------
  648. // CASE B: Synchronizing a node from chado to drupal
  649. //---------------------------------------------------
  650. }
  651. else{
  652. //---------------------------------------------------
  653. // CASE C: We are validating a form for inserting a new node
  654. //---------------------------------------------------
  655. // The primary key for the chado analysis table is
  656. // program, programversion, sourcename
  657. $values = array(
  658. 'program' => $node->program,
  659. 'programversion' => $node->programversion,
  660. 'sourcename' => $node->sourcename,
  661. );
  662. $analysis = tripal_core_chado_select('analysis', array('analysis_id'), $values);
  663. if (sizeof($analysis) > 0) {
  664. form_set_error('program', 'Cannot add the analysis with this program,
  665. program version and source name. An analysis with these values already exists.');
  666. return;
  667. }
  668. }
  669. }
  670. }
  671. /**
  672. * Display help and module information
  673. * @param path which path of the site we're displaying help
  674. * @param arg array that holds the current path as would be returned from arg()
  675. * function
  676. * @return help text for the path
  677. *
  678. * @ingroup tripal_analysis
  679. */
  680. function tripal_analysis_help($path, $arg) {
  681. $output = '';
  682. switch ($path) {
  683. case "admin/help#tripal_analysis":
  684. $output = '<p>' .
  685. t("Displays links to nodes created on this date") .
  686. '</p>';
  687. break;
  688. }
  689. return $output;
  690. }
  691. /**
  692. * The following function proves access control for users trying to
  693. * perform actions on data managed by this module
  694. *
  695. * @ingroup tripal_analysis
  696. */
  697. function chado_analysis_access($op, $node, $account) {
  698. if ($op == 'create') {
  699. if (!user_access('create chado_analysis content', $account)) {
  700. return FALSE;
  701. }
  702. }
  703. if ($op == 'update') {
  704. if (!user_access('edit chado_analysis content', $account)) {
  705. return FALSE;
  706. }
  707. }
  708. if ($op == 'delete') {
  709. if (!user_access('delete chado_analysis content', $account)) {
  710. return FALSE;
  711. }
  712. }
  713. if ($op == 'view') {
  714. if (!user_access('access chado_analysis content', $account)) {
  715. return FALSE;
  716. }
  717. }
  718. return NULL;
  719. }
  720. /**
  721. * Set the permission types that the chado module uses. Essentially we
  722. * want permissionis that protect creation, editing and deleting of chado
  723. * data objects
  724. *
  725. * @ingroup tripal_analysis
  726. */
  727. function tripal_analysis_perm() {
  728. return array(
  729. 'access chado_analysis content',
  730. 'create chado_analysis content',
  731. 'delete chado_analysis content',
  732. 'edit chado_analysis content',
  733. );
  734. }
  735. /**
  736. * We need to let drupal know about our theme functions and their arguments.
  737. * We create theme functions to allow users of the module to customize the
  738. * look and feel of the output generated in this module
  739. *
  740. * @ingroup tripal_analysis
  741. */
  742. function tripal_analysis_theme() {
  743. return array(
  744. 'tripal_analysis_analysis_page' => array(
  745. 'arguments' => array('analyses'),
  746. ),
  747. );
  748. }
  749. /**
  750. * This function uses analysis_id's of all drupal analysis nodes as input and
  751. * pull the analysis information (name, description, program, programversion,
  752. * algorithm, sourcename, sourceversion, sourceuri, timeexecuted) from
  753. * chado database. The return type is an object array that stores $analysis
  754. * objects sorted by program
  755. *
  756. * @ingroup tripal_analysis
  757. */
  758. function get_chado_analyses() {
  759. $sql_drupal = "SELECT COUNT (analysis_id) FROM {chado_analysis}";
  760. $no_orgs = db_result(db_query($sql_drupal));
  761. if ($no_orgs != 0) {
  762. $sql = "SELECT analysis_id, CA.nid, type FROM {chado_analysis} CA INNER JOIN node ON CA.nid = node.nid";
  763. $result = db_query($sql);
  764. $previous_db = tripal_db_set_active('chado');
  765. $sql = "SELECT Analysis_id, name, description, program,
  766. programversion, algorithm, sourcename, sourceversion,
  767. sourceuri, timeexecuted
  768. FROM {Analysis} WHERE analysis_id=%d";
  769. $analyses = array();
  770. $count = 0;
  771. while ($data = db_fetch_object($result)) {
  772. $analysis = db_fetch_object(db_query($sql, $data->analysis_id));
  773. $analysis->node_id = $data->nid;
  774. $analysis->node_type = $data->type;
  775. // Use node_type as the key so we can sort by node type
  776. // Since node_type is not unique by itself, we need to add
  777. // $count to the key
  778. $sortedBy = $analysis->timeexecuted;
  779. $analyses ["$sortedBy$count"] = $analysis;
  780. $count ++;
  781. }
  782. tripal_db_set_active($previous_db);
  783. //Sort analyses by time, descending order
  784. krsort($analyses, SORT_STRING);
  785. return $analyses;
  786. }
  787. }
  788. /**
  789. * Theme the List Analysis page
  790. *
  791. * @param $analyses
  792. * An array of analyses to list
  793. *
  794. * @ingroup tripal_analysis
  795. */
  796. function theme_tripal_analysis_analysis_page($analyses) {
  797. $output = "<br />Analyses are listed in the descending order of their execution time.<br /><a id=\"tripal_expandableBox_toggle_button\" onClick=\"toggleExpandableBoxes()\">[-] Collapse All</a>";
  798. foreach ($analyses as $analysis) {
  799. // Prepare information for html output
  800. $ana_node_url = url("node/$analysis->node_id");
  801. if ($analysis->sourceversion) {
  802. $ver = "($analysis->sourceversion)";
  803. }
  804. $date = preg_replace("/^(\d+-\d+-\d+) .*/", "$1", $analysis->timeexecuted);
  805. // Generate html output
  806. $output .= "<div class=\"tripal_chado_analysis-info-box\" style=\"padding:5px\">
  807. <div class=\"tripal_expandableBox\">
  808. <h3>$analysis->name ($date)</h3>
  809. </div>
  810. <div class=\"tripal_expandableBoxContent\">
  811. <span>
  812. <table class=\"tripal_chado_analysis_content\">
  813. <tr><td>
  814. Name: <a href=\"$ana_node_url\">$analysis->name</a>
  815. </td></tr>
  816. <tr><td>
  817. Program: $analysis->program ($analysis->programversion)
  818. </td></tr>
  819. <tr><td>
  820. Algorithm: $analysis->algorithm
  821. </td></tr>
  822. <tr><td>
  823. Source: $analysis->sourcename $ver
  824. </td></tr>
  825. <tr><td>
  826. Source URI: $analysis->sourceuri
  827. </td></tr>
  828. <tr><td>
  829. Executed Time:$date
  830. </td></tr>
  831. <tr><td>
  832. Description: $analysis->description
  833. </td></tr>
  834. </table>
  835. </span>
  836. </div>
  837. </div>";
  838. }
  839. return $output;
  840. }
  841. /**
  842. * Remove orphaned drupal nodes or chado analysis
  843. *
  844. * @param $dummy
  845. * Not Used -kept for backwards compatibility
  846. * @param $job_id
  847. * The id of the tripal job executing this function
  848. *
  849. * @ingroup tripal_analysis
  850. */
  851. function tripal_analyses_cleanup($dummy = NULL, $job_id = NULL) {
  852. // select each node from node table with chado_analysis as type
  853. // check to make sure it also exists in chado_analysis table, delete if it doesn't
  854. // (this should never, ever happen, but we'll double check anyway)
  855. $sql_drupal_node = "SELECT * FROM {node} WHERE type LIKE 'chado_analysis%' order by nid";
  856. $sql_drupal_ca = "SELECT * from {chado_analysis} WHERE nid = %d";
  857. $results = db_query($sql_drupal_node);
  858. while ($node = db_fetch_object($results)) {
  859. $ca_record = db_fetch_object(db_query($sql_drupal_ca, $node->nid));
  860. if (!$ca_record) {
  861. node_delete($node->nid);
  862. $message = "Missing in chado_analysis table.... DELETING node: $nid->nid\n";
  863. watchdog('tripal_analysis', $message, array(), WATCHDOG_WARNING);
  864. }
  865. }
  866. // get nodes from chado_analysis table and load into array, saving chado analysis_id
  867. // as we iterate through, we'll check that they are actual nodes and
  868. // delete if they aren't
  869. // (this should never, ever happen, but we'll double check anyway)
  870. $sql_drupal_ca2 = "SELECT * FROM {chado_analysis}";
  871. $sql_drupal_node2 = "SELECT * FROM {node} WHERE type LIKE 'chado_analysis%' AND nid = %d";
  872. $results = db_query($sql_drupal_ca2);
  873. $nid2aid = array();
  874. while ($ca_record = db_fetch_object($results)) {
  875. $node = db_fetch_object(db_query($sql_drupal_node2, $ca_record->nid));
  876. if (!$node) {
  877. db_query("DELETE FROM {chado_analysis} WHERE nid = %nid", $ca_record->nid);
  878. $message = "chado_analysis missing node.... DELETING chado_analysis record with nid: $ca_record->nid\n";
  879. watchdog('tripal_analysis', $message, array(), WATCHDOG_WARNING);
  880. }
  881. else{
  882. $nid2aid[$ca_record->nid] = $ca_record->analysis_id;
  883. }
  884. }
  885. // iterate through all of the chado_analysis nodes in drupal
  886. // and delete those that aren't valid in chado
  887. $sql_chado = "SELECT analysis_id from {analysis} WHERE analysis_id = %d";
  888. foreach ($nid2aid as $nid => $aid) {
  889. $previous_db = tripal_db_set_active('chado');
  890. $chado_record = db_fetch_object(db_query($sql_chado, $aid));
  891. tripal_db_set_active($previous_db);
  892. if (!$chado_record) {
  893. node_delete($nid);
  894. $message = "Missing in analysis table in chado.... DELETING node: $nid\n";
  895. watchdog('tripal_analysis', $message, array(), WATCHDOG_WARNING);
  896. }
  897. }
  898. return '';
  899. }
  900. /**
  901. function tripal_analysis_reindex_features ($analysis_id = NULL, $job_id = NULL){
  902. $i = 0;
  903. // if the caller provided a analysis_id then get all of the features
  904. // associated with the analysis. Otherwise get all sequences associated
  905. // with all libraries.
  906. if(!$analysis_id){
  907. $sql = "SELECT Analysis_id, Feature_id ".
  908. "FROM {Analysisfeature} ".
  909. "ORDER BY analysis_id";
  910. $previous_db = tripal_db_set_active('chado'); // use chado database
  911. $results = db_query($sql);
  912. tripal_db_set_active($previous_db); // now use drupal database
  913. } else {
  914. $sql = "SELECT Analysis_id, Feature_id ".
  915. "FROM {Analysisfeature} ".
  916. "WHERE analysis_id = %d";
  917. "ORDER BY analysis_id";
  918. $previous_db = tripal_db_set_active('chado'); // use chado database
  919. $results = db_query($sql,$analysis_id);
  920. tripal_db_set_active($previous_db); // now use drupal database
  921. }
  922. // load into ids array
  923. $count = 0;
  924. $ids = array();
  925. while($id = db_fetch_object($results)){
  926. $ids[$count] = $id->feature_id;
  927. $count++;
  928. }
  929. $interval = intval($count * 0.01);
  930. foreach($ids as $feature_id){
  931. // update the job status every 1% features
  932. if($job_id and $i % interval == 0){
  933. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  934. }
  935. tripal_feature_sync_feature ($feature_id);
  936. $i++;
  937. }
  938. } */
  939. /**
  940. function tripal_analysis_taxonify_features ($analysis_id = NULL, $job_id = NULL){
  941. $i = 0;
  942. // if the caller provided a analysis_id then get all of the features
  943. // associated with the analysis. Otherwise get all sequences assoicated
  944. // with all libraries.
  945. if(!$analysis_id){
  946. $sql = "SELECT Analysis_id, Feature_id ".
  947. "FROM {Analysisfeature} ".
  948. "ORDER BY analysis_id";
  949. $previous_db = tripal_db_set_active('chado'); // use chado database
  950. $results = db_query($sql);
  951. tripal_db_set_active($previous_db); // now use drupal database
  952. } else {
  953. $sql = "SELECT Analysis_id, Feature_id ".
  954. "FROM {Analysisfeature} ".
  955. "WHERE analysis_id = %d";
  956. "ORDER BY analysis_id";
  957. $previous_db = tripal_db_set_active('chado'); // use chado database
  958. $results = db_query($sql,$analysis_id);
  959. tripal_db_set_active($previous_db); // now use drupal database
  960. }
  961. // load into ids array
  962. $count = 0;
  963. $ids = array();
  964. while($id = db_fetch_object($results)){
  965. $ids[$count] = $id->feature_id;
  966. $count++;
  967. }
  968. // make sure our vocabularies are set before proceeding
  969. tripal_feature_set_vocabulary();
  970. // use this SQL for getting the nodes
  971. $nsql = "SELECT * FROM {chado_feature} CF ".
  972. " INNER JOIN {node} N ON N.nid = CF.nid ".
  973. "WHERE feature_id = %d";
  974. // iterate through the features and set the taxonomy
  975. $interval = intval($count * 0.01);
  976. foreach($ids as $feature_id){
  977. // update the job status every 1% features
  978. if($job_id and $i % $interval == 0){
  979. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  980. }
  981. $node = db_fetch_object(db_query($nsql,$feature_id));
  982. tripal_feature_set_taxonomy($node,$feature_id);
  983. $i++;
  984. }
  985. }
  986. */
  987. /**
  988. * Implements hook_views_api()
  989. * Purpose: Essentially this hook tells drupal that there is views support for
  990. * for this module which then includes tripal_analysis.views.inc where all the
  991. * views integration code is
  992. *
  993. * @ingroup tripal_analysis
  994. */
  995. function tripal_analysis_views_api() {
  996. return array(
  997. 'api' => 2.0,
  998. );
  999. }