chado_feature__seqlen.inc 5.6 KB

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