data__sequence_features.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @class
  4. * Purpose: Follows the feature relationships of a given feature,
  5. * and stores information about all child features.
  6. *
  7. */
  8. class data__sequence_features extends ChadoField {
  9. public static $default_label = 'Child Features';
  10. public static $default_description = 'Gathers information about all subfeatures (mRNA, CDS, proteins) associated with a top-level feature (gene)';
  11. public static $default_widget = 'data__sequence_features_widget';
  12. public static $default_formatter = 'data__sequence_features_formatter';
  13. public static $module = 'tripal_chado';
  14. public static $default_settings = array(
  15. 'storage' => 'field_chado_storage',
  16. 'searchable_keys' => array(),
  17. );
  18. // public static $download_formatters = array(
  19. // 'TripalTabDownloader',
  20. // 'TripalCSVDownloader',
  21. // );
  22. public static $default_instance_settings = array(
  23. // 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.
  24. 'term_vocabulary' => 'data',
  25. // The name of the term.
  26. 'term_name' => 'Sequence features',
  27. // The unique ID (i.e. accession) of the term.
  28. 'term_accession' => '1255',
  29. // Set to TRUE if the site admin is not allowed to change the term
  30. // type, otherwise the admin can change the term mapped to a field.
  31. 'term_fixed' => FALSE,
  32. // Indicates if this field should be automatically attached to display
  33. // or web services or if this field should be loaded separately. This
  34. // is convenient for speed. Fields that are slow should for loading
  35. // should have auto_attach set to FALSE so tha their values can be
  36. // attached asynchronously.
  37. 'auto_attach' => FALSE,
  38. // The table in Chado that the instance maps to.
  39. 'chado_table' => '',
  40. // The column of the table in Chado where the value of the field comes from.
  41. 'chado_column' => '',
  42. // The base table.
  43. 'base_table' => '',
  44. );
  45. public static $no_ui = FALSE;
  46. public static $no_data = FALSE;
  47. /**
  48. * Load field.
  49. *
  50. * @see ChadoField::load()
  51. */
  52. public function load($entity) {
  53. parent::load($entity);
  54. $field = get_class($this);
  55. $parent = $entity->chado_record->feature_id;
  56. $children = $this->findChildFeatures($parent);
  57. if (empty($children)) {
  58. unset($entity->{$field});
  59. return;
  60. }
  61. $i = 0;
  62. foreach ($children as $child_id => $child) {
  63. $entity->{$field}['und'][$i]['value'] = $child;
  64. $i++;
  65. }
  66. return $entity;
  67. }
  68. /**
  69. * @see ChadoField::query()
  70. **/
  71. public function query($query, $condition) {
  72. }
  73. /**
  74. * @see ChadoField::queryOrder()
  75. **/
  76. public function queryOrder($query, $order) {
  77. }
  78. /**
  79. * @see ChadoField::elementInfo()
  80. **/
  81. public function elementInfo() {
  82. $field_term = $this->getFieldTermID();
  83. return array(
  84. $field_term => array(
  85. 'operations' => array('eq', 'ne', 'contains', 'starts'),
  86. 'sortable' => TRUE,
  87. 'searchable' => TRUE,
  88. ),
  89. );
  90. }
  91. /**
  92. * For a given feature, find all child features. For each child feature,
  93. * return:
  94. * - the type name
  95. * - annotation names in feature_cvterm
  96. * - featureloc info, including source feature name.
  97. *
  98. * @param string $feature_id
  99. * Chado feature.feature_id.
  100. *
  101. * @return array
  102. */
  103. private function findChildFeatures(string $feature_id) {
  104. $this_children = [];
  105. $prev_db = chado_set_active('chado');
  106. $query = db_select('chado.feature_relationship', 'fr')
  107. ->fields('fr')
  108. ->condition('object_id', $feature_id)
  109. ->execute()
  110. ->fetchAll();
  111. chado_set_active($prev_db);
  112. foreach ($query as $child) {
  113. $child_id = $child->subject_id;
  114. // If any additional fields want to use this
  115. // field's data, expand the desired table below.
  116. $feature = chado_generate_var('feature', ['feature_id' => $child_id]);
  117. $feature = chado_expand_var($feature, 'field', 'feature.residues');
  118. $feature = chado_expand_var($feature, 'table', 'featureloc');
  119. $feature = chado_expand_var($feature, 'table', 'featureprop');
  120. $feature = chado_expand_var($feature, 'table', 'feature_cvterm');
  121. $this_children[$child_id]['info'] = $feature;
  122. $grand_children = $this->findChildFeatures($child->subject_id);
  123. if (!empty($grand_children)) {
  124. $this_children[$child_id]['children'] = $grand_children;
  125. }
  126. }
  127. return $this_children;
  128. }
  129. }