tripal_analysis.module 33 KB

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