tripal_analysis.module 36 KB

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