tripal_analysis.module 35 KB

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