chado_feature__seqlen.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. class chado_feature__seqlen extends TripalField {
  3. /**
  4. * @see TripalField::fieldInfo()
  5. */
  6. static function fieldInfo() {
  7. return array(
  8. 'label' => t('Sequence length'),
  9. 'description' => t('A field for calculating the length of a sequence.'),
  10. 'default_widget' => 'chado_feature__seqlen_widget',
  11. 'default_formatter' => 'chado_feature__seqlen_formatter',
  12. 'settings' => array(),
  13. 'storage' => array(
  14. 'type' => 'field_chado_storage',
  15. 'module' => 'tripal_chado',
  16. 'active' => TRUE
  17. ),
  18. );
  19. }
  20. /**
  21. * @see TripalField::can_attach()
  22. */
  23. protected function setCanAttach() {
  24. $table_name = $this->details['chado_table'];
  25. $type_table = $this->details['chado_type_table'];
  26. $type_field = $this->details['chado_type_column'];
  27. $cv_id = $this->details['chado_cv_id'];
  28. $cvterm_id = $this->details['chado_cvterm_id'];
  29. if ($table_name == 'feature') {
  30. $this->can_attach = TRUE;
  31. return;
  32. }
  33. $this->can_attach = FALSE;
  34. }
  35. /**
  36. * @see TripalField::setFieldName()
  37. */
  38. protected function setFieldName() {
  39. $table_name = $this->details['chado_table'];
  40. $type_table = $this->details['chado_type_table'];
  41. $type_field = $this->details['chado_type_column'];
  42. $cv_id = $this->details['chado_cv_id'];
  43. $cvterm_id = $this->details['chado_cvterm_id'];
  44. $this->field_name = 'feature__seqlen';
  45. }
  46. /**
  47. * @see TripalField::create_info()
  48. */
  49. function createInfo() {
  50. if (!$this->can_attach) {
  51. return;
  52. }
  53. $table_name = $this->details['chado_table'];
  54. $type_table = $this->details['chado_type_table'];
  55. $type_field = $this->details['chado_type_column'];
  56. $cv_id = $this->details['chado_cv_id'];
  57. $cvterm_id = $this->details['chado_cvterm_id'];
  58. return array(
  59. 'field_name' => $this->field_name,
  60. 'type' => 'chado_feature__seqlen',
  61. 'cardinality' => 1,
  62. 'locked' => FALSE,
  63. 'storage' => array(
  64. 'type' => 'field_chado_storage',
  65. ),
  66. 'settings' => array(
  67. 'chado_table' => $table_name,
  68. 'chado_column' => 'seqlen',
  69. 'semantic_web' => tripal_get_chado_semweb_term($table_name, 'seqlen'),
  70. ),
  71. );
  72. }
  73. /**
  74. * @see TripalField::createInstanceInfo()
  75. */
  76. function createInstanceInfo() {
  77. if (!$this->can_attach) {
  78. return;
  79. }
  80. $table_name = $this->details['chado_table'];
  81. $type_table = $this->details['chado_type_table'];
  82. $type_field = $this->details['chado_type_column'];
  83. $cv_id = $this->details['chado_cv_id'];
  84. $cvterm_id = $this->details['chado_cvterm_id'];
  85. return array(
  86. 'field_name' => $this->field_name,
  87. 'entity_type' => $this->entity_type,
  88. 'bundle' => $this->bundle->name,
  89. 'label' => 'Raw Sequence Length',
  90. 'description' => 'The number of residues in the raw sequence. This length
  91. is only for the assigned raw sequence and does not represent the length of any
  92. sequences derived from alignments. If this value is zero but aligned sequences
  93. are present then this record has no official assigned sequence.',
  94. 'required' => FALSE,
  95. 'settings' => array(
  96. 'auto_attach' => TRUE,
  97. ),
  98. 'widget' => array(
  99. 'type' => 'chado_feature__seqlen_widget',
  100. 'settings' => array(
  101. 'display_label' => 1,
  102. ),
  103. ),
  104. 'display' => array(
  105. 'deafult' => array(
  106. 'label' => 'above',
  107. 'type' => 'chado_feature__seqlen_formatter',
  108. 'settings' => array(),
  109. ),
  110. ),
  111. );
  112. }
  113. /**
  114. * @see TripalField::widgetInfo()
  115. */
  116. public static function widgetInfo() {
  117. return array(
  118. 'chado_feature__seqlen_widget' => array(
  119. 'label' => t('Sequence Length'),
  120. 'field types' => array('chado_feature__seqlen'),
  121. ),
  122. );
  123. }
  124. /**
  125. * @see TripalField::formatterInfo()
  126. */
  127. static function formatterInfo() {
  128. return array(
  129. 'chado_feature__seqlen_formatter' => array(
  130. 'label' => t('Residues Length'),
  131. 'field types' => array('chado_feature__seqlen'),
  132. 'settings' => array(
  133. ),
  134. ),
  135. );
  136. }
  137. /**
  138. * @see TripalField::formatterView()
  139. */
  140. static function formatterView(&$element, $entity_type, $entity, $field,
  141. $instance, $langcode, $items, $display) {
  142. foreach ($items as $delta => $item) {
  143. $element[$delta] = array(
  144. '#type' => 'markup',
  145. '#markup' => $item['value'],
  146. );
  147. }
  148. }
  149. /**
  150. * @see TripalField::widgetForm()
  151. */
  152. public static function widgetForm(&$widget, &$form, &$form_state, $field, $instance,
  153. $langcode, $items, $delta, $element) {
  154. $settings = $field['settings'];
  155. $field_name = $field['field_name'];
  156. $field_type = $field['type'];
  157. $field_table = $field['settings']['chado_table'];
  158. $field_column = $field['settings']['chado_column'];
  159. $widget['value'] = array(
  160. '#type' => 'value',
  161. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  162. );
  163. $widget['feature__seqlen'] = array(
  164. '#type' => 'value',
  165. '#value' => 0,
  166. '#title' => $element['#title'],
  167. '#description' => $element['#description'],
  168. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  169. '#delta' => $delta,
  170. '#element_validate' => array('chado_feature__seqlen_widget_validate'),
  171. );
  172. }
  173. }
  174. /**
  175. * Callback function for validating the chado_feature__seqlen_widget.
  176. */
  177. function chado_feature__seqlen_widget_validate($element, &$form_state) {
  178. $field_name = $element['#parents'][0];
  179. // Get the residues so we can calculate teh length.
  180. $residues = tripal_chado_get_field_form_values('feature__residues', $form_state, 0, 'feature__residues');
  181. // Remove any white spaces.
  182. if ($residues) {
  183. $residues = preg_replace('/\s/', '', $residues);
  184. tripal_chado_set_field_form_values($field_name, $form_state, strlen($residues), 0, 'feature__seqlen');
  185. }
  186. else {
  187. // Otherwise, remove the seqlen value
  188. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, 'feature_seqlen');
  189. }
  190. }