tripal_feature.form.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Implementation of hook_form
  4. *
  5. * @param $node
  6. * @param $param
  7. *
  8. * @return
  9. * Drupal form array
  10. *
  11. * @ingroup tripal_feature
  12. */
  13. function chado_feature_form($node, &$form_state) {
  14. $form = array();
  15. // Default values can come in the following ways:
  16. //
  17. // 1) as elements of the $node object. This occurs when editing an existing feature
  18. // 2) in the $form_state['values'] array which occurs on a failed validation or
  19. // ajax callbacks from non submit form elements
  20. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  21. // form elements and the form is being rebuilt
  22. //
  23. // set form field defaults
  24. $feature = null;
  25. $feature_id = null;
  26. $uniquename = '';
  27. $fname = '';
  28. $feature_type = '';
  29. $organism_id = '';
  30. $residues = '';
  31. $is_obsolete = '';
  32. $analyses = '';
  33. $references = '';
  34. $synonyms = '';
  35. // if we are editing an existing node then the feature is already part of the node
  36. if (property_exists($node, 'feature')) {
  37. $feature = $node->feature;
  38. $feature = tripal_core_expand_chado_vars($feature, 'field', 'feature.residues');
  39. $feature_id = $feature->feature_id;
  40. $uniquename = $feature->uniquename;
  41. $fname = $feature->name;
  42. $feature_type = $feature->type_id->name;
  43. $organism_id = $feature->organism_id->organism_id;
  44. $residues = $feature->residues;
  45. $is_obsolete = $feature->is_obsolete;
  46. // get the synonyms from a previous post
  47. $synonyms = '';
  48. if(property_exists($node, 'synonyms')) {
  49. $synonyms = $node->synonyms;
  50. }
  51. // get synonyms from the database if we don't already have them
  52. if (!$synonyms) {
  53. $options = array('return_array' => 1);
  54. $feature = tripal_core_expand_chado_vars($feature, 'table', 'feature_synonym', $options);
  55. $feature_synonyms = $feature->feature_synonym;
  56. foreach ($feature_synonyms as $index => $synonym) {
  57. $synonyms .= $synonym->synonym_id->name . "\n";
  58. }
  59. }
  60. }
  61. // if we are re constructing the form from a failed validation or ajax callback
  62. // then use the $form_state['values'] values
  63. if (array_key_exists('values', $form_state)) {
  64. $uniquename = $form_state['values']['uniquename'];
  65. $fname = $form_state['values']['fname'];
  66. $feature_type = $form_state['values']['feature_type'];
  67. $organism_id = $form_state['values']['organism_id'];
  68. $residues = $form_state['values']['residues'];
  69. $is_obsolete = $form_state['values']['is_obsolete'];
  70. $synonyms = $form_state['values']['synonyms'];
  71. }
  72. // if we are re building the form from after submission (from ajax call) then
  73. // the values are in the $form_state['input'] array
  74. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  75. $uniquename = $form_state['input']['uniquename'];
  76. $fname = $form_state['input']['fname'];
  77. $feature_type = $form_state['input']['feature_type'];
  78. $organism_id = $form_state['input']['organism_id'];
  79. $residues = $form_state['input']['residues'];
  80. $is_obsolete = array_key_exists('is_obsolete', $form_state['input']) ? $form_state['input']['is_obsolete'] : FALSE;
  81. $synonyms = $form_state['input']['synonyms'];
  82. }
  83. // We need to pass above variables for preview to show
  84. $form['feature'] = array(
  85. '#type' => 'value',
  86. '#value' => $feature
  87. );
  88. // keep track of the feature id if we have one. If we do have one then
  89. // this would indicate an update as opposed to an insert.
  90. $form['feature_id'] = array(
  91. '#type' => 'value',
  92. '#value' => $feature_id,
  93. );
  94. $form['fname']= array(
  95. '#type' => 'textfield',
  96. '#title' => t('Feature Name'),
  97. '#required' => TRUE,
  98. '#default_value' => $fname,
  99. '#description' => t('Enter the name used by humans to refer to this feature.'),
  100. '#maxlength' => 255
  101. );
  102. $form['uniquename']= array(
  103. '#type' => 'textfield',
  104. '#title' => t('Unique Feature Name'),
  105. '#required' => TRUE,
  106. '#default_value' => $uniquename,
  107. '#description' => t('Enter a unique name for this feature. This name must be unique for the organism and feature type.'),
  108. '#maxlength' => 255
  109. );
  110. // get the sequence ontology CV ID
  111. $values = array('name' => 'sequence');
  112. $cv = tripal_core_chado_select('cv', array('cv_id'), $values);
  113. $cv_id = $cv[0]->cv_id;
  114. $form['feature_type'] = array(
  115. '#title' => t('Feature Type'),
  116. '#type' => 'textfield',
  117. '#description' => t("Choose the feature type."),
  118. '#required' => TRUE,
  119. '#default_value' => $feature_type,
  120. '#autocomplete_path' => "admin/tripal/tripal_cv/cvterm/auto_name/$cv_id",
  121. );
  122. // get the list of organisms
  123. $sql = "SELECT * FROM {Organism} ORDER BY genus, species";
  124. $org_rset = chado_query($sql);
  125. $organisms = array();
  126. $organisms[''] = '';
  127. while ($organism = $org_rset->fetchObject()) {
  128. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  129. }
  130. $form['organism_id'] = array(
  131. '#title' => t('Organism'),
  132. '#type' => t('select'),
  133. '#description' => t("Choose the organism with which this feature is associated"),
  134. '#required' => TRUE,
  135. '#default_value' => $organism_id,
  136. '#options' => $organisms,
  137. );
  138. // Get synonyms
  139. $syn_text = '';
  140. if ($synonyms) {
  141. if (is_array($synonyms)) {
  142. foreach ($synonyms as $synonym) {
  143. $syn_text .= "$synonym->name\n";
  144. }
  145. }
  146. else {
  147. $syn_text = $synonyms;
  148. }
  149. }
  150. $form['synonyms']= array(
  151. '#type' => 'textarea',
  152. '#title' => t('Synonyms'),
  153. '#required' => FALSE,
  154. '#default_value' => $syn_text,
  155. '#description' => t('Enter alternate names (synonmys) for this feature to help in searching and identification. You may enter as many alternate names as needed each on different lines.'),
  156. );
  157. $form['residues']= array(
  158. '#type' => 'textarea',
  159. '#title' => t('Residues'),
  160. '#required' => FALSE,
  161. '#default_value' => $residues,
  162. '#description' => t('Enter the nucelotide sequences for this feature'),
  163. );
  164. $checked = '';
  165. if ($is_obsolete == 't') {
  166. $checked = '1';
  167. }
  168. $form['is_obsolete']= array(
  169. '#type' => 'checkbox',
  170. '#title' => t('Is Obsolete'),
  171. '#required' => FALSE,
  172. '#default_value' => $checked,
  173. '#description' => t('Check this box if this sequence should be retired'),
  174. );
  175. return $form;
  176. }
  177. /**
  178. * Implementation of hook_validate
  179. *
  180. * This validation is being used for three activities:
  181. * CASE A: Update a node that exists in both drupal and chado
  182. * CASE B: Synchronizing a node from chado to drupal
  183. * CASE C: Inserting a new node that exists in niether drupal nor chado
  184. *
  185. * @param $node
  186. *
  187. *
  188. * @ingroup tripal_feature
  189. */
  190. function chado_feature_validate($node, &$form_state) {
  191. // remove surrounding white-space on submitted values
  192. $node->uniquename = trim($node->uniquename);
  193. $node->fname = trim($node->fname);
  194. $node->feature_type = trim($node->feature_type);
  195. $node->residues = trim($node->residues);
  196. $result = 0;
  197. // CASE A: if the nid exists then this is an update
  198. if (property_exists($node, 'nid')) {
  199. // make sure the feature type is a real sequence ontology term
  200. $type = tripal_cv_get_cvterm_by_name($node->feature_type, NULL, 'sequence');
  201. if (!$type) {
  202. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  203. }
  204. // if this is an update, we want to make sure that a different feature for
  205. // the organism doesn't already have this uniquename. We don't want to give
  206. // two sequences the same uniquename
  207. if (property_exists($node, 'feature_id') and $node->feature_id != 0) {
  208. $sql = "
  209. SELECT *
  210. FROM {feature} F
  211. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  212. WHERE
  213. F.uniquename = :uname AND
  214. F.organism_id = :organism_id AND
  215. CVT.name = :cvtname AND
  216. NOT f.feature_id = :feature_id
  217. ";
  218. $args = array(':uname' => $node->uniquename, ':organism_id' => $node->organism_id,
  219. ':cvtname' => $node->feature_type, ':feature_id' => $node->feature_id);
  220. $result = chado_query($sql, $args)->fetchObject();
  221. if ($result) {
  222. form_set_error('uniquename', t("Feature update cannot proceed. The feature name '$node->uniquename' is not unique for this organism. Please provide a unique name for this feature."));
  223. }
  224. }
  225. }
  226. else {
  227. // To differentiate if we are syncing or creating a new feature altogther, see if a feature_id already exists
  228. if (property_exists($node, 'feature_id') and $node->feature_id != 0) {
  229. // CASE B: Synchronizing a node from chado to drupal
  230. // we don't need to do anything.
  231. }
  232. else {
  233. // CASE C: We are validating a form for inserting a new node
  234. // make sure the feature type is a real sequence ontology term
  235. $type = tripal_cv_get_cvterm_by_name($node->feature_type, NULL, 'sequence');
  236. if (!$type) {
  237. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  238. }
  239. // if this is an insert then we just need to make sure this name doesn't
  240. // already exist for this organism if it does then we need to throw an error
  241. $sql = "
  242. SELECT *
  243. FROM {feature} F
  244. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  245. WHERE
  246. F.uniquename = :name AND
  247. F.organism_id = :organism_id AND
  248. CVT.name = :cvtname
  249. ";
  250. $args = array(':name' => $node->uniquename, ':organism_id' => $node->organism_id, ':cvtname' => $node->feature_type);
  251. $result = chado_query($sql, $args)->fetchObject();
  252. if ($result) {
  253. form_set_error('uniquename', t("Feature insert cannot proceed. The feature name '$node->uniquename' already exists for this organism. Please provide a unique name for this feature."));
  254. }
  255. }
  256. }
  257. }