tripal_analysis.module 33 KB

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