tripal_analysis.chado_node.inc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <?php
  2. /**
  3. * When editing or creating a new node of type 'chado_analysis' we need
  4. * a form. This function creates the form that will be used for this.
  5. *
  6. * @ingroup tripal_analysis
  7. */
  8. function chado_analysis_form($node, &$form_state) {
  9. $form = array();
  10. // Default values can come in the following ways:
  11. //
  12. // 1) as elements of the $node object. This occurs when editing an existing analysis
  13. // 2) in the $form_state['values'] array which occurs on a failed validation or
  14. // ajax callbacks from non submit form elements
  15. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  16. // form elements and the form is being rebuilt
  17. //
  18. // set form field defaults
  19. $analysis_id = null;
  20. $analysisname = '';
  21. $program = '';
  22. $programversion = '';
  23. $algorithm = '';
  24. $sourcename = '';
  25. $sourceversion = '';
  26. $sourceuri = '';
  27. $timeexecuted = '';
  28. $description = '';
  29. $d_removed = array(); // lists removed properties
  30. $num_new = 0; // the number of new rows
  31. // if we are editing an existing node then the analysis is already part of the node
  32. if (property_exists($node, 'analysis')) {
  33. $analysis = $node->analysis;
  34. $analysis = tripal_core_expand_chado_vars($analysis, 'field', 'analysis.description');
  35. $analysis_id = $analysis->analysis_id;
  36. // get form defaults
  37. $analysisname = $analysis->name;
  38. $program = $analysis->program;
  39. $programversion = $analysis->programversion;
  40. $algorithm = $analysis->algorithm;
  41. $sourcename = $analysis->sourcename;
  42. $sourceversion = $analysis->sourceversion;
  43. $sourceuri = $analysis->sourceuri;
  44. $timeexecuted = $analysis->timeexecuted;
  45. $description = $analysis->description;
  46. // set the organism_id in the form
  47. $form['analysis_id'] = array(
  48. '#type' => 'value',
  49. '#value' => $analysis->analysis_id,
  50. );
  51. }
  52. // if we are re constructing the form from a failed validation or ajax callback
  53. // then use the $form_state['values'] values
  54. if (array_key_exists('values', $form_state)) {
  55. $analysisname = $form_state['values']['analysisname'];
  56. $program = $form_state['values']['program'];
  57. $programversion = $form_state['values']['programversion'];
  58. $algorithm = $form_state['values']['algorithm'];
  59. $sourcename = $form_state['values']['sourcename'];
  60. $sourceversion = $form_state['values']['sourceversion'];
  61. $sourceuri = $form_state['values']['sourceuri'];
  62. $timeexecuted = $form_state['values']['timeexecuted'];
  63. $description = $form_state['values']['description'];
  64. $d_removed = $form_state['values']['removed'];
  65. $num_new = $form_state['values']['num_new'] ? $form_state['values']['num_new'] : 0;
  66. }
  67. // if we are re building the form from after submission (from ajax call) then
  68. // the values are in the $form_state['input'] array
  69. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  70. $analysisname = $form_state['input']['analysisname'];
  71. $program = $form_state['input']['program'];
  72. $programversion = $form_state['input']['programversion'];
  73. $algorithm = $form_state['input']['algorithm'];
  74. $sourcename = $form_state['input']['sourcename'];
  75. $sourceversion = $form_state['input']['sourceversion'];
  76. $sourceuri = $form_state['input']['sourceuri'];
  77. $timeexecuted = $form_state['input']['timeexecuted'];
  78. $description = $form_state['input']['description'];
  79. $d_removed = $form_state['input']['removed'];
  80. $num_new = $form_state['input']['num_new'] ? $form_state['input']['num_new'] : 0;
  81. }
  82. $form['title']= array(
  83. '#type' => 'value',
  84. '#default_value' => $node->title,
  85. );
  86. $form['instructions'] = array(
  87. '#markup' => t('<b>Note</b>: When adding any type of data it is good to associate it with
  88. an analysis so that site visitors can identify the source of the data including
  89. necessary materials and methods. The fields below imply that all analyses
  90. are derived from some software package. But, data can also be derived via retreival
  91. from an external source or an analysis pipeline with multipel software components.
  92. In these cases, provide values for the fields below that best makes sense
  93. '),
  94. );
  95. $form['analysisname']= array(
  96. '#type' => 'textfield',
  97. '#title' => t('Analysis Name'),
  98. '#required' => TRUE,
  99. '#default_value' => $analysisname,
  100. '#description' => t("This should be a brief name that
  101. describes the analysis succintly. This name will helps the user find analyses."),
  102. );
  103. $form['program']= array(
  104. '#type' => 'textfield',
  105. '#title' => t('Program, Pipeline Name or Method Name'),
  106. '#required' => TRUE,
  107. '#default_value' => $program,
  108. '#description' => t("Program name, e.g. blastx, blastp, sim4, genscan. If the analysis was not derived from a software package, provide a very brief description of the pipeline or method."),
  109. );
  110. $form['programversion']= array(
  111. '#type' => 'textfield',
  112. '#title' => t('Program, Pipeline or Method version'),
  113. '#required' => TRUE,
  114. '#default_value' => $programversion,
  115. '#description' => t("Version description, e.g. TBLASTX 2.0MP-WashU [09-Nov-2000]. Enter 'n/a' if no version is available or applicable."),
  116. );
  117. $form['algorithm']= array(
  118. '#type' => 'textfield',
  119. '#title' => t('Algorithm'),
  120. '#required' => FALSE,
  121. '#default_value' => $algorithm,
  122. '#description' => t("Algorithm name, e.g. blast."),
  123. );
  124. $form['sourcename']= array(
  125. '#type' => 'textfield',
  126. '#title' => t('Source Name'),
  127. '#required' => TRUE,
  128. '#default_value' => $sourcename,
  129. '#description' => t('The name of the source data. This could be a file name, data set name or a
  130. small description for how the data was collected. For long descriptions use the description field below'),
  131. );
  132. $form['sourceversion']= array(
  133. '#type' => 'textfield',
  134. '#title' => t('Source Version'),
  135. '#required' => FALSE,
  136. '#default_value' => $sourceversion,
  137. '#description' => t('If the source dataset has a version, include it here'),
  138. );
  139. $form['sourceuri']= array(
  140. '#type' => 'textfield',
  141. '#title' => t('Source URI'),
  142. '#required' => FALSE,
  143. '#default_value' => $sourceuri,
  144. '#description' => t("This is a permanent URL or URI for the source of the analysis.
  145. Someone could recreate the analysis directly by going to this URI and
  146. fetching the source data (e.g. the blast database, or the training model)."),
  147. );
  148. // Get time saved in chado
  149. $default_time = $timeexecuted;
  150. $year = preg_replace("/^(\d+)-\d+-\d+ .*/", "$1", $default_time);
  151. $month = preg_replace("/^\d+-0?(\d+)-\d+ .*/", "$1", $default_time);
  152. $day = preg_replace("/^\d+-\d+-0?(\d+) .*/", "$1", $default_time);
  153. // If the time is not set, use current time
  154. if (!$default_time) {
  155. $default_time = REQUEST_TIME;
  156. $year = format_date($default_time, 'custom', 'Y');
  157. $month = format_date($default_time, 'custom', 'n');
  158. $day = format_date($default_time, 'custom', 'j');
  159. }
  160. $form['timeexecuted']= array(
  161. '#type' => 'date',
  162. '#title' => t('Time Executed'),
  163. '#required' => TRUE,
  164. '#default_value' => array(
  165. 'year' => $year,
  166. 'month' => $month,
  167. 'day' => $day,
  168. ),
  169. );
  170. $form['description']= array(
  171. '#type' => 'textarea',
  172. '#rows' => 15,
  173. '#title' => t('Materials & Methods (Description and/or Program Settings)'),
  174. '#required' => FALSE,
  175. '#default_value' => $description,
  176. '#description' => t('Please provide all necessary information to allow
  177. someone to recreate the analysis, including materials and methods
  178. for collection of the source data and performing the analysis'),
  179. );
  180. // get the analysis properties
  181. $properties = array();
  182. $properties[] = 'Select a Property';
  183. $sql = "
  184. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  185. FROM {cvterm} CVT
  186. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  187. WHERE
  188. CV.name = 'analysis_property' AND
  189. NOT CVT.is_obsolete = 1
  190. ORDER BY CVT.name ASC
  191. ";
  192. $prop_types = chado_query($sql);
  193. while ($prop = $prop_types->fetchObject()) {
  194. $properties[$prop->cvterm_id] = $prop->name;
  195. }
  196. $exclude = array();
  197. $include = array();
  198. $instructions = t('To add additional properties to the drop down. ' . l("Add terms to the analysis_property vocabulary", "admin/tripal/chado/tripal_cv/cvterm/add") . ".");
  199. tripal_core_properties_form($form, $form_state, 'analysisprop', 'analysis_id', 'analysis_property',
  200. $properties, $analysis_id, $exclude, $include, $instructions, 'Properties');
  201. return $form;
  202. }
  203. /**
  204. * Validates the user input before creating an analysis node
  205. *
  206. * @ingroup tripal_analysis
  207. */
  208. function chado_analysis_validate($node, $form, &$form_state) {
  209. // use the analysis parent to validate the node
  210. tripal_analysis_validate($node, $form, $form_state);
  211. }
  212. /**
  213. * This validation is being used for three activities:
  214. * CASE A: Update a node that exists in both drupal and chado
  215. * CASE B: Synchronizing a node from chado to drupal
  216. * CASE C: Inserting a new node that exists in niether drupal nor chado
  217. *
  218. * @ingroup tripal_analysis
  219. */
  220. function tripal_analysis_validate($node, $form, &$form_state) {
  221. // remove surrounding white-space on submitted values
  222. $node->analysisname = trim($node->analysisname);
  223. $node->description = trim($node->description);
  224. $node->program = trim($node->program);
  225. $node->programversion = trim($node->programversion);
  226. $node->algorithm = trim($node->algorithm);
  227. $node->sourcename = trim($node->sourcename);
  228. $node->sourceversion = trim($node->sourceversion);
  229. $node->sourceuri = trim($node->sourceuri);
  230. // if this is a delete then don't validate
  231. if($node->op == 'Delete') {
  232. return;
  233. }
  234. // we are syncing if we do not have a node ID but we do have a analysis_id. We don't
  235. // need to validate during syncing so just skip it.
  236. if (is_null($node->nid) and property_exists($node, 'analysis_id') and $node->analysis_id != 0) {
  237. return;
  238. }
  239. // Validating for an update
  240. if (!is_null($node->nid)) {
  241. // get the existing node
  242. $values = array('analysis_id' => $node->analysis_id);
  243. $result = tripal_core_chado_select('analysis', array('*'), $values);
  244. $analysis = $result[0];
  245. // if the name has changed make sure it doesn't conflict with an existing name
  246. if ($analysis->name != $node->analysisname) {
  247. $values = array('name' => $node->analysisname);
  248. $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
  249. if ($result and count($result) > 0) {
  250. form_set_error('analysisname', 'Cannot update the analysis with this analysis name. An analysis with this name already exists.');
  251. return;
  252. }
  253. }
  254. // if the unique constraint has changed check to make sure it doesn't conflict with an
  255. // existing record
  256. if ($analysis->program != $node->program or $analysis->programversion != $node->programversion or
  257. $analysis->sourcename != $node->sourcename) {
  258. $values = array(
  259. 'program' => $node->program,
  260. 'programversion' => $node->programversion,
  261. 'sourcename' => $node->sourcename,
  262. );
  263. $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
  264. if ($result and count($result) > 0) {
  265. if ($analysis->program != $node->program) {
  266. $field = 'program';
  267. }
  268. if ($analysis->programversion != $node->programversion) {
  269. $field = 'programversion';
  270. }
  271. if ($analysis->sourcename != $node->sourcename) {
  272. $field = 'sourcename';
  273. }
  274. form_set_error($field, 'Cannot update the analysis with this program,
  275. program version and source name. An analysis with these values already exists.');
  276. return;
  277. }
  278. }
  279. }
  280. // Validating for an insert
  281. else {
  282. $values = array(
  283. 'program' => $node->program,
  284. 'programversion' => $node->programversion,
  285. 'sourcename' => $node->sourcename,
  286. );
  287. $analysis = tripal_core_chado_select('analysis', array('analysis_id'), $values);
  288. if ($analysis and count($analysis) > 0) {
  289. form_set_error('program', 'Cannot add the analysis with this program,
  290. program version and source name. An analysis with these values already exists.');
  291. return;
  292. }
  293. // make sure we have a unique analysis name. This is not a requirement
  294. // for the analysis table but we use the analysis name for the Drupal node
  295. // title, so it should be unique
  296. $values = array('name' => $node->analysisname);
  297. $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
  298. if ($result and count($result) > 0) {
  299. form_set_error('analysisname', 'Cannot add the analysis with this analysis name. An analysis with this name already exists.');
  300. return;
  301. }
  302. }
  303. }
  304. /**
  305. * When a new chado_analysis node is created we also need to add information
  306. * to our chado_analysis table. This function is called on insert of a new
  307. * node of type 'chado_analysis' and inserts the necessary information.
  308. *
  309. * @ingroup tripal_analysis
  310. */
  311. function chado_analysis_insert($node) {
  312. $node->analysisname = trim($node->analysisname);
  313. $node->description = trim($node->description);
  314. $node->program = trim($node->program);
  315. $node->programversion = trim($node->programversion);
  316. $node->algorithm = trim($node->algorithm);
  317. $node->sourcename = trim($node->sourcename);
  318. $node->sourceversion = trim($node->sourceversion);
  319. $node->sourceuri = trim($node->sourceuri);
  320. // if there is an analysis_id in the $node object then this must be a sync so
  321. // we can skip adding the analysis as it is already there, although
  322. // we do need to proceed with the rest of the insert
  323. if (!property_exists($node, 'analysis_id')) {
  324. // Create a timestamp so we can insert it into the chado database
  325. $time = $node->timeexecuted;
  326. $month = $time['month'];
  327. $day = $time['day'];
  328. $year = $time['year'];
  329. $timestamp = $month . '/' . $day . '/' . $year;
  330. // insert and then get the newly inserted analysis record
  331. $values = array(
  332. 'name' => $node->analysisname,
  333. 'description' => $node->description,
  334. 'program' => $node->program,
  335. 'programversion' => $node->programversion,
  336. 'algorithm' => $node->algorithm,
  337. 'sourcename' => $node->sourcename,
  338. 'sourceversion' => $node->sourceversion,
  339. 'sourceuri' => $node->sourceuri,
  340. 'timeexecuted' => $timestamp
  341. );
  342. $analysis = tripal_core_chado_insert('analysis', $values);
  343. if (!$analysis) {
  344. drupal_set_message(t('Unable to add analysis.', 'warning'));
  345. watchdog('tripal_analysis', 'Insert analysis: Unable to create analysis where values:%values',
  346. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  347. return;
  348. }
  349. $analysis_id = $analysis['analysis_id'];
  350. // now add in the properties
  351. $properties = tripal_core_properties_form_retreive($node, 'analysis_property');
  352. foreach ($properties as $property => $elements) {
  353. foreach ($elements as $rank => $value) {
  354. $status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE, 'analysis_property');
  355. if (!$status) {
  356. drupal_set_message("Error cannot add property: $property", "error");
  357. watchdog('t_analysis', "Error cannot add property: %prop",
  358. array('%property' => $property), WATCHDOG_ERROR);
  359. }
  360. }
  361. }
  362. }
  363. else {
  364. $analysis_id = $node->analysis_id;
  365. }
  366. // Make sure the entry for this analysis doesn't already exist in the
  367. // chado_analysis table if it doesn't exist then we want to add it.
  368. $check_org_id = chado_get_id_for_node('analysis', $node->nid);
  369. if (!$check_org_id) {
  370. $record = new stdClass();
  371. $record->nid = $node->nid;
  372. $record->vid = $node->vid;
  373. $record->analysis_id = $analysis_id;
  374. drupal_write_record('chado_analysis', $record);
  375. }
  376. // add the analysis to the node object for
  377. // use by other analysis modules that may be using this function
  378. $node->analysis = $analysis;
  379. $node->analysis_id = $analysis_id; // we need to set this for children
  380. }
  381. /**
  382. * Removes analysis from the chado database
  383. *
  384. * @param $node
  385. * The node object specifying which chado record to delete
  386. *
  387. * @ingroup tripal_analysis
  388. */
  389. function chado_analysis_delete($node) {
  390. $analysis_id = chado_get_id_for_node('analysis', $node->nid);
  391. // if we don't have an analysis id for this node then this isn't a node of
  392. // type chado_analysis or the entry in the chado_analysis table was lost.
  393. if (!$analysis_id) {
  394. return;
  395. }
  396. // Remove data from the {chado_analysis}, {node}, and {node_revisions} tables
  397. $sql_del = "DELETE FROM {chado_analysis} WHERE nid = :nid AND vid = :vid";
  398. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  399. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  400. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  401. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  402. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  403. //Remove from analysis and analysisprop tables of chado database as well
  404. chado_query("DELETE FROM {analysis} WHERE analysis_id = :analysis_id", array(':analysis_id' => $analysis_id));
  405. }
  406. /**
  407. * Update analyses
  408. *
  409. * @param $node
  410. * The updated node object
  411. *
  412. * @ingroup tripal_analysis
  413. */
  414. function chado_analysis_update($node) {
  415. $node->analysisname = trim($node->analysisname);
  416. $node->description = trim($node->description);
  417. $node->program = trim($node->program);
  418. $node->programversion = trim($node->programversion);
  419. $node->algorithm = trim($node->algorithm);
  420. $node->sourcename = trim($node->sourcename);
  421. $node->sourceversion = trim($node->sourceversion);
  422. $node->sourceuri = trim($node->sourceuri);
  423. // Create a timestamp so we can insert it into the chado database
  424. $time = $node->timeexecuted;
  425. $month = $time['month'];
  426. $day = $time['day'];
  427. $year = $time['year'];
  428. $timestamp = $month . '/' . $day . '/' . $year;
  429. // update the record in Chado
  430. $analysis_id = chado_get_id_for_node('analysis', $node->nid);
  431. $match = array(
  432. 'analysis_id' => $node->analysis_id,
  433. );
  434. $values = array(
  435. 'name' => $node->analysisname,
  436. 'description' => $node->description,
  437. 'program' => $node->program,
  438. 'programversion' => $node->programversion,
  439. 'algorithm' => $node->algorithm,
  440. 'sourcename' => $node->sourcename,
  441. 'sourceversion' => $node->sourceversion,
  442. 'sourceuri' => $node->sourceuri,
  443. 'timeexecuted' => $timestamp,
  444. 'analysis_id' => $analysis_id
  445. );
  446. $status = tripal_core_chado_update('analysis', $match, $values);
  447. if (!$status) {
  448. drupal_set_message(t('Unable to update analysis.', 'warning'));
  449. watchdog('tripal_analysis', 'Update analysis: Unable to update analysis where values: %values',
  450. array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
  451. }
  452. // now add in the properties by first removing any the analysis
  453. // already has and adding the ones we have
  454. tripal_core_chado_delete('analysisprop', array('analysis_id' => $analysis_id));
  455. $properties = tripal_core_properties_form_retreive($node, 'analysis_property');
  456. foreach ($properties as $property => $elements) {
  457. foreach ($elements as $rank => $value) {
  458. $status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE, 'analysis_property');
  459. if (!$status) {
  460. drupal_set_message("Error cannot add property: '$property'", "error");
  461. watchdog('t_analysis', "Error cannot add property: '%prop'",
  462. array('%prop' => $property), WATCHDOG_ERROR);
  463. }
  464. }
  465. }
  466. }
  467. /**
  468. * When a node is requested by the user this function is called to allow us
  469. * to add auxiliary data to the node object.
  470. *
  471. * @ingroup tripal_analysis
  472. */
  473. function chado_analysis_load($nodes) {
  474. foreach ($nodes as $nid => $node) {
  475. // find the analysis and add in the details
  476. $analysis_id = chado_get_id_for_node('analysis', $nid);
  477. // build the analysis variable
  478. $values = array('analysis_id' => $analysis_id);
  479. $analysis = tripal_core_generate_chado_var('analysis', $values);
  480. // add in the description field
  481. $analysis = tripal_core_expand_chado_vars($analysis, 'field', 'analysis.description');
  482. $nodes[$nid]->analysis = $analysis;
  483. }
  484. }
  485. /**
  486. * Implement hook_access().
  487. *
  488. * This hook allows node modules to limit access to the node types they define.
  489. *
  490. * @param $node
  491. * The node on which the operation is to be performed, or, if it does not yet exist, the
  492. * type of node to be created
  493. *
  494. * @param $op
  495. * The operation to be performed
  496. *
  497. * @param $account
  498. * A user object representing the user for whom the operation is to be performed
  499. *
  500. * @return
  501. * If the permission for the specified operation is not set then return FALSE. If the
  502. * permission is set then return NULL as this allows other modules to disable
  503. * access. The only exception is when the $op == 'create'. We will always
  504. * return TRUE if the permission is set.
  505. *
  506. * @ingroup tripal_analysis
  507. */
  508. function chado_analysis_node_access($node, $op, $account) {
  509. if ($op == 'create') {
  510. if (!user_access('create chado_analysis content', $account)) {
  511. return FALSE;
  512. }
  513. return TRUE;
  514. }
  515. if ($op == 'update') {
  516. if (!user_access('edit chado_analysis content', $account)) {
  517. return FALSE;
  518. }
  519. }
  520. if ($op == 'delete') {
  521. if (!user_access('delete chado_analysis content', $account)) {
  522. return FALSE;
  523. }
  524. }
  525. if ($op == 'view') {
  526. if (!user_access('access chado_analysis content', $account)) {
  527. return FALSE;
  528. }
  529. }
  530. return NULL;
  531. }