tripal_analysis.module 36 KB

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