data__sequence_features.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * @class
  4. * Purpose:
  5. *
  6. * Data:
  7. * Assumptions:
  8. */
  9. class data__sequence_features extends ChadoField {
  10. // --------------------------------------------------------------------------
  11. // EDITABLE STATIC CONSTANTS
  12. //
  13. // The following constants SHOULD be set for each descendant class. They are
  14. // used by the static functions to provide information to Drupal about
  15. // the field and it's default widget and formatter.
  16. // --------------------------------------------------------------------------.
  17. /**
  18. * The default label for this field.
  19. */
  20. public static $default_label = 'Child Features';
  21. /**
  22. * The default description for this field.
  23. */
  24. public static $default_description = 'Gathers information about all subfeatures (mRNA, CDS, proteins) associated with a top-level feature (gene)';
  25. /**
  26. * The default widget for this field.
  27. */
  28. public static $default_widget = 'data__sequence_features_widget';
  29. /**
  30. * The default formatter for this field.
  31. */
  32. public static $default_formatter = 'data__sequence_features_formatter';
  33. // The module that manages this field.
  34. // If no module manages the field (IE it's added via libraries)
  35. /**
  36. * Set this to 'tripal_chado'.
  37. */
  38. public static $module = 'tripal_manage_analyses';
  39. // A list of global settings. These can be accessed within the
  40. // globalSettingsForm. When the globalSettingsForm is submitted then
  41. // Drupal will automatically change these settings for all fields.
  42. // Once instances exist for a field type then these settings cannot be.
  43. /**
  44. * Changed.
  45. */
  46. public static $default_settings = array(
  47. 'storage' => 'field_chado_storage',
  48. // It is expected that all fields set a 'value' in the load() function.
  49. // In many cases, the value may be an associative array of key/value pairs.
  50. // In order for Tripal to provide context for all data, the keys should
  51. // be a controlled vocabulary term (e.g. rdfs:type). Keys in the load()
  52. // function that are supported by the query() function should be
  53. // listed here.
  54. 'searchable_keys' => array(),
  55. );
  56. // Indicates the download formats for this field. The list must be the.
  57. /**
  58. * Name of a child class of the TripalFieldDownloader.
  59. */
  60. public static $download_formatters = array(
  61. 'TripalTabDownloader',
  62. 'TripalCSVDownloader',
  63. );
  64. // Provide a list of instance specific settings. These can be access within
  65. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  66. // then Drupal with automatically change these settings for the instance.
  67. // It is recommended to put settings at the instance level whenever possible.
  68. // If you override this variable in a child class be sure to replicate the
  69. // term_name, term_vocab, term_accession and term_fixed keys as these are.
  70. /**
  71. * Required for all TripalFields.
  72. */
  73. public static $default_instance_settings = array(
  74. // The DATABASE name, as it appears in chado.db. This also builds the link-out url. In most cases this will simply be the CV name. In some cases (EDAM) this will be the SUBONTOLOGY.
  75. 'term_vocabulary' => 'data',
  76. // The name of the term.
  77. 'term_name' => 'Sequence features',
  78. // The unique ID (i.e. accession) of the term.
  79. 'term_accession' => '1255',
  80. // Set to TRUE if the site admin is not allowed to change the term
  81. // type, otherwise the admin can change the term mapped to a field.
  82. 'term_fixed' => FALSE,
  83. // Indicates if this field should be automatically attached to display
  84. // or web services or if this field should be loaded separately. This
  85. // is convenient for speed. Fields that are slow should for loading
  86. // should have auto_attach set to FALSE so tha their values can be
  87. // attached asynchronously.
  88. 'auto_attach' => FALSE,
  89. // The table in Chado that the instance maps to.
  90. 'chado_table' => '',
  91. // The column of the table in Chado where the value of the field comes from.
  92. 'chado_column' => '',
  93. // The base table.
  94. 'base_table' => '',
  95. );
  96. // A boolean specifying that users should not be allowed to create
  97. // fields and instances of this field type through the UI. Such
  98. // fields can only be created programmatically with field_create_field()
  99. /**
  100. * And field_create_instance().
  101. */
  102. public static $no_ui = FALSE;
  103. // A boolean specifying that the field will not contain any data. This
  104. // should exclude the field from web services or downloads. An example
  105. // could be a quick search field that appears on the page that redirects.
  106. /**
  107. * The user but otherwise provides no data.
  108. */
  109. public static $no_data = FALSE;
  110. /**
  111. * Load field.
  112. *
  113. * @see ChadoField::load()
  114. */
  115. public function load($entity) {
  116. // ChadoFields automatically load the chado column specified in the
  117. // default settings above. If that is all you need then you don't even
  118. // need to implement this function. However, if you need to add any
  119. // additional data to be used in the display, you should add it here.
  120. parent::load($entity);
  121. $field = get_class($this);
  122. $parent = $entity->chado_record->feature_id;
  123. $children = $this->findChildFeatures($parent);
  124. $i = 0;
  125. foreach ($children as $child_id => $child) {
  126. $entity->{$field}['und'][$i]['value'] = $child;
  127. $i++;
  128. }
  129. return $entity;
  130. }
  131. /**
  132. * @see ChadoField::query()
  133. **/
  134. public function query($query, $condition) {
  135. }
  136. /**
  137. * @see ChadoField::queryOrder()
  138. **/
  139. public function queryOrder($query, $order) {
  140. }
  141. /**
  142. * @see ChadoField::elementInfo()
  143. **/
  144. public function elementInfo() {
  145. $field_term = $this->getFieldTermID();
  146. return array(
  147. $field_term => array(
  148. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  149. 'sortable' => TRUE,
  150. 'searchable' => TRUE,
  151. ),
  152. );
  153. }
  154. /**
  155. * For a given feature, find all child features. For each child feature,
  156. * return:
  157. * - the type name
  158. * - annotation names in feature_cvterm
  159. * - featureloc info, including source feature name.
  160. *
  161. * @param string $feature_id
  162. * Chado feature.feature_id.
  163. *
  164. * @return array
  165. */
  166. private function findChildFeatures(string $feature_id) {
  167. $this_children = [];
  168. $prev_db = chado_set_active('chado');
  169. $query = db_select('chado.feature_relationship', 'fr')
  170. ->fields('fr')
  171. ->condition('object_id', $feature_id)
  172. ->execute()
  173. ->fetchAll();
  174. foreach ($query as $child) {
  175. $child_id = $child->subject_id;
  176. // Expand var on this.
  177. $feature = chado_generate_var('feature', ['feature_id' => $child_id]);
  178. $feature = chado_expand_var($feature, 'field', 'feature.residues');
  179. $feature = chado_expand_var($feature, 'table', 'featureloc');
  180. $feature = chado_expand_var($feature, 'table', 'featureprop');
  181. $feature = chado_expand_var($feature, 'table', 'feature_cvterm');
  182. $this_children[$child_id]['info'] = $feature;
  183. $grand_children = $this->findChildFeatures($child->subject_id);
  184. if (!empty($grand_children)) {
  185. $this_children[$child_id]['children'] = $grand_children;
  186. }
  187. }
  188. chado_set_active($prev_db);
  189. return $this_children;
  190. }
  191. }