Browse Source

split out child annotations into new field

bradfordcondon 6 years ago
parent
commit
9f82dc4557

+ 98 - 0
tripal_chado/includes/TripalFields/local__child_annotations/local__child_annotations.inc

@@ -0,0 +1,98 @@
+<?php
+
+/**
+ * @class
+ * Purpose:
+ *
+ * Data:
+ * Assumptions:
+ */
+class local__child_annotations extends ChadoField {
+
+  public static $default_label = 'Child Annotations';
+  public static $default_description = 'All of the annotations associated with child features.';
+  public static $default_widget = 'local__child_annotations_widget';
+  public static $default_formatter = 'local__child_annotations_formatter';
+  public static $module = 'tripal_chado';
+  public static $default_settings = [
+    'storage' => 'field_chado_storage',
+    'searchable_keys' => [],
+  ];
+  public static $download_formatters = [
+    'TripalTabDownloader',
+    'TripalCSVDownloader',
+  ];
+  public static $default_instance_settings = [
+    // 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.
+    'term_vocabulary' => 'local',
+    // The name of the term.
+    'term_name' => 'child_properties',
+    // The unique ID (i.e. accession) of the term.
+    'term_accession' => 'child_properties',
+    // Set to TRUE if the site admin is not allowed to change the term
+    // type, otherwise the admin can change the term mapped to a field.
+    'term_fixed' => FALSE,
+    // Indicates if this field should be automatically attached to display
+    // or web services or if this field should be loaded separately. This
+    // is convenient for speed.  Fields that are slow should for loading
+    // should have auto_attach set to FALSE so tha their values can be
+    // attached asynchronously.
+    'auto_attach' => FALSE,
+    // The table in Chado that the instance maps to.
+    'chado_table' => '',
+    // The column of the table in Chado where the value of the field comes from.
+    'chado_column' => '',
+    // The base table.
+    'base_table' => '',
+  ];
+
+  public static $no_ui = FALSE;
+  public static $no_data = FALSE;
+
+  /**
+   * @see ChadoField::load()
+   **/
+  public function load($entity) {
+    parent::load($entity);
+
+    $master = $entity->data__sequence_features['und'];
+
+    $properties = [];
+
+//    foreach ($master as $child){
+//
+//      //remove all the other fields from the expand_var.
+//
+//    }
+
+    //TODO: filter this to just hte annotations.
+      $entity->local__child_properties['und'] = $master;
+
+  }
+
+  /**
+   * @see ChadoField::query()
+   **/
+  public function query($query, $condition) {
+  }
+
+  /**
+   * @see ChadoField::queryOrder()
+   **/
+  public function queryOrder($query, $order) {
+  }
+
+  /**
+   * @see ChadoField::elementInfo()
+   **/
+  public function elementInfo() {
+    $field_term = $this->getFieldTermID();
+    return [
+      $field_term => [
+        'operations' => ['eq', 'ne', 'contains', 'starts'],
+        'sortable' => TRUE,
+        'searchable' => TRUE,
+      ],
+    ];
+  }
+}

+ 146 - 0
tripal_chado/includes/TripalFields/local__child_annotations/local__child_annotations_formatter.inc

@@ -0,0 +1,146 @@
+<?php
+
+/**
+ * @class
+ * Purpose:
+ *
+ * Display:
+ * Configuration:
+ */
+class local__child_annotations_formatter extends ChadoFieldFormatter {
+
+  public static $default_label = 'Child Annotations';
+  public static $field_types = ['local__child_annotations'];
+  public static $default_settings = [
+    'setting1' => 'default_value',
+  ];
+
+  /**
+   * @see ChadoFieldFormatter::settingsForm()
+   **/
+  public function settingsForm($view_mode, $form, &$form_state) {
+
+  }
+
+  /**
+   * @see ChadoFieldFormatter::View()
+   **/
+  public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
+
+    // Get the settings.
+    $settings = $display['settings'];
+
+    // Get the data for this field.
+    $data = $entity->{'local__child_annotations'}['und'];
+
+    if (!$data) {
+      return;
+    }
+
+    foreach ($data as $i => $value) {
+
+      $child = $value['value'];
+      // Assumption: we're on the gene entity.  We've got an array of mRNA feature ID's.
+      $header = [
+        'Feature Name',
+        'Feature Type',
+        'Property Name',
+        'Property Value',
+      ];
+
+      $info = $child['info'];
+      $name = $info->uniquename;
+      $element[0][$i] = [
+        '#type' => 'fieldset',
+        '#title' => $name,
+        '#attributes' => [
+          'class' => [
+            'collapsible',
+            'collapsed',
+          ],
+        ],
+        '#attached' => ['js' => ['misc/collapse.js', 'misc/form.js']],
+      ];
+
+      $rows = $this->getAnnotationRows($child);
+
+      $table = theme('table', ['rows' => $rows, 'header' => $header]);
+      $element[0][$i]['prop_table'] = [
+        '#markup' => $table,
+        '#title' => t("Child Annotations for !root", ['!root' => $name]),
+      ];
+    }
+  }
+
+  /**
+   * Recursively goes through the child feature array for annotation listings.
+   *   Such listings are in feature_cvterm.
+   *
+   * @param array $data
+   *   Data formatted by the data__sequence_features field.
+   *    Expects an info and a children key.
+   *
+   * @return array
+   *   Rows array suitable for table.
+   */
+  private function getAnnotationRows($data) {
+
+    $rows = [];
+
+    $info = $data['info'];
+
+    $children = $data['children'] ?? NULL;
+
+    $annotations = $info->feature_cvterm;
+
+    if ($annotations) {
+
+      if (is_array($annotations)) {
+        foreach ($annotations as $ann) {
+
+          $annotation_name = $ann->cvterm_id->name;
+          if ($ann->is_not) {
+            $annotation_name = "is not " . $annotation_name;
+          }
+
+          $rows[] = [
+            $info->uniquename,
+            $info->type_id->name,
+            $annotation_name,
+          ];
+        }
+      }
+      else {
+        $annotation_name = $annotations->cvterm_id->name;
+        if ($annotations->is_not) {
+          $annotation_name = "is not " . $annotation_name;
+        }
+
+        $rows[] = [
+          $info->uniquename,
+          $info->type_id->name,
+          $annotation_name,
+        ];
+
+      }
+
+    }
+
+    if ($children && !empty($children)) {
+
+      foreach ($children as $child) {
+        $rows = array_merge($this->getAnnotationRows($child), $rows);
+      }
+    }
+    return $rows;
+
+  }
+
+  /**
+   * @see ChadoFieldFormatter::settingsSummary()
+   **/
+  public function settingsSummary($view_mode) {
+    return '';
+  }
+
+}

+ 41 - 0
tripal_chado/includes/TripalFields/local__child_annotations/local__child_annotations_widget.inc

@@ -0,0 +1,41 @@
+<?php
+/**
+ * @class
+ * Purpose:
+ *
+ * Data:
+ * Assumptions:
+ */
+class local__child_annotations_widget extends ChadoFieldWidget {
+
+  // The default label for this field.
+  public static $default_label = 'Child Properties';
+
+  // The list of field types for which this formatter is appropriate.
+  public static $field_types = array('local__child_annotations');
+
+
+ /**
+  * @see ChadoFieldWidget::form()
+  *
+  **/
+
+  public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
+    parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
+  }
+
+  /**
+  * @see ChadoFieldWidget::validate()
+  *
+  **/
+  public function validate($element, $form, &$form_state, $langcode, $delta) {
+  }
+
+   /**
+  * @see ChadoFieldWidget::submit()
+  *
+  **/
+  public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
+  }
+
+}