Explorar o código

Merge pull request #440 from tripal/432-tv3-array_desgin_field

Added array design field
Lacey-Anne Sanderson %!s(int64=6) %!d(string=hai) anos
pai
achega
7f35a59c5e

+ 3 - 1
tripal_chado/api/tripal_chado.query.api.inc

@@ -1646,7 +1646,9 @@ function chado_select_record_check_value_type(&$op, &$value, $type) {
  *            F.uniquename = :feature_uniquename";
  * $args = array( ':feature_uniquename' => $form_state['values']['uniquename'] );
  * $result = chado_query($sql, $args);
- * foreach ($result as $r) { [Do something with the records here] }
+ * while ($r = $results->fetchObject()) { 
+ *   // Do something with the record object $r
+ * }
  * @endcode
  *
  * @ingroup tripal_chado_query_api

+ 201 - 0
tripal_chado/includes/TripalFields/efo__array_design/efo__array_design.inc

@@ -0,0 +1,201 @@
+<?php
+
+class efo__array_design extends ChadoField {
+
+  // The default lable for this field.
+  public static $default_label = 'Array Design';
+
+  // The default description for this field.
+  public static $description = 'An instrument design which describes the design of the array.';
+
+  // Provide a list of instance specific settings. These can be access within
+  // the instanceSettingsForm.  When the instanceSettingsForm is submitted
+  // then Drupal with automatically change these settings for the instnace.
+  // It is recommended to put settings at the instance level whenever possible.
+  // If you override this variable in a child class be sure to replicate the
+  // term_name, term_vocab, term_accession and term_fixed keys as these are
+  // required for all TripalFields.
+  public static $default_instance_settings  = array(
+    // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
+    'term_vocabulary' => 'EFO',
+    // The name of the term.
+    'term_name' => 'array design',
+    // The unique ID (i.e. accession) of the term.
+    'term_accession' => '0000269',
+    // Set to TRUE if the site admin is allowed to change the term
+    // type. This will create form elements when editing the field instance
+    // to allow the site admin to change the term settings above.
+    'term_fixed' => FALSE,
+  );
+
+  // The default widget for this field.
+  public static $default_widget = 'efo__array_design_widget';
+
+  // The default formatter for this field.
+  public static $default_formatter = 'efo__array_design_formatter';
+
+
+  /**
+   * @see TripalField::validate()
+   */
+  public function validate($entity_type, $entity, $langcode, $items, &$errors) {
+
+    // If we don't have an entity then we don't want to validate.  The case
+    // where this could happen is when a user is editing the field settings
+    // and trying to set a default value. In that case there's no entity and
+    // we don't want to validate.  There will always be an entity for creation
+    // and update operations of a content type.
+    if (!$entity) {
+      return;
+    }
+    $settings = $this->field['settings'];
+    $field_name = $this->field['field_name'];
+    $field_type = $this->field['type'];
+    $field_table = $this->instance['settings']['chado_table'];
+    $field_column = $this->instance['settings']['chado_column'];
+    $linker_field = 'chado-' . $field_table . '__' . $field_column;
+    
+    // Get the field values.
+    foreach ($items as $delta => $values) {
+
+      // Get the field values.
+      $arraydesign_id = $values[$linker_field];
+      if (!$arraydesign_id or $arraydesign_id == 0) {
+        $errors[$field_name]['und'][0][] = array(
+          'message' =>  t("Please specify an array design."),
+          'error' => 'efo__array_design'
+        );
+      }
+    }
+  }
+
+  /**
+   * @see TripalField::load()
+   */
+  public function load($entity) {
+
+    $record = $entity->chado_record;
+    $settings = $this->instance['settings'];
+
+    $field_name = $this->field['field_name'];
+    $field_type = $this->field['type'];
+    $field_table = $this->instance['settings']['chado_table'];
+    $field_column = $this->instance['settings']['chado_column'];
+
+    // Get the terms for each of the keys for the 'values' property.
+    $name_term = chado_get_semweb_term('arraydesign', 'name');
+    $version_term = chado_get_semweb_term('arraydesign', 'version');
+
+    // Set some defaults for the empty record.
+    $entity->{$field_name}['und'][0] = array(
+      'value' => array(),
+    );
+
+    if (!$record) {
+      return;
+    }
+    $linker_field = 'chado-' . $field_table . '__' . $field_column;
+    $entity->{$field_name}['und'][0]['value'] = array(
+      $name_term => $record->{$field_column}->name,
+      $version_term => $record->{$field_column}->version,
+    );
+    $entity->{$field_name}['und'][0][$linker_field] = $record->{$field_column}->arraydesign_id;
+
+    // Is there a published entity for this arraydesign?
+    if (property_exists($record->{$field_column}, 'entity_id')) {
+      $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $record->{$field_column}->entity_id;
+    }
+  }
+
+  
+  /**
+   * @see TripalField::elementInfo()
+   */
+  public function elementInfo() {
+    $field_term = $this->getFieldTermID();
+
+    $name_term = chado_get_semweb_term('arraydesign', 'name');
+    $version_term = chado_get_semweb_term('arraydesign', 'version');
+    
+    return array(
+      $field_term => array(
+        'operations' => array('eq', 'contains', 'starts'),
+        'sortable' => TRUE,
+        'searchable' => TRUE,
+        'readonly' => FALSE,
+        'type' => 'xs:complexType',
+        'elements' => array(
+          $name_term => array(
+            'searchable' => TRUE,
+            'name' => 'name',
+            'operations' => array('eq', 'ne', 'contains', 'starts'),
+            'sortable' => FALSE,
+            'type' => 'xs:string',
+            'readonly' => TRUE,
+            'required' => FALSE,
+          ),
+          $version_term => array(
+            'searchable' => TRUE,
+            'name' => 'version',
+            'operations' => array('eq', 'ne'),
+            'sortable' => TRUE,
+            'readonly' => FALSE,
+            'type' => 'xs:string',
+            'required' => TRUE,
+          ),
+          'entity' => array(
+            'searchable' => FALSE,
+          ),
+        ),
+      ),
+    );
+  }
+
+  /**
+   * @see ChadoField::query()
+   */
+  public function query($query, $condition) {
+    $alias = $this->field['field_name'];
+    $operator = $condition['operator'];
+
+    $field_term_id = $this->getFieldTermID();
+    $name_term = $field_term_id . ',' . chado_get_semweb_term('arraydesign', 'name');
+    $version_term = $field_term_id . ',' . chado_get_semweb_term('arraydesign', 'version');
+
+    // Join to the organism table for this field.
+    $this->queryJoinOnce($query, 'arraydesign', $alias, "base.arraydesign_id = $alias.arraydesign_id");
+
+    // If the column is the field name then we're during a search on the full
+    // scientific name.
+    if ($condition['column'] == $field_term_id or 
+        $condition['column'] == $name_term) {      
+      $query->condition("$alias.name", $condition['value'], $operator);
+    }
+    // If the column is a subfield.
+    if ($condition['column'] == $version_term) {
+      $query->condition("$alias.version", $condition['value'], $operator);
+    }
+  }
+
+  /**
+   * @see ChadoField::queryOrder()
+   */
+  public function queryOrder($query, $order) {
+    $alias = $this->field['field_name'];
+
+    $field_term_id = $this->getFieldTermID();
+    $name_term = $field_term_id . ',' . chado_get_semweb_term('arraydesign', 'name');
+    $version_term = $field_term_id . ',' . chado_get_semweb_term('arraydesign', 'version');
+
+    // Join to the organism table for this field.
+    $this->queryJoinOnce($query, 'arraydesign', $alias, "base.arraydesign_id = $alias.arraydesign_id");
+
+    // Now perform the sort.
+    if ($order['column'] == $name_term) {
+      $query->orderBy("$alias.name", $order['direction']);
+    }
+    if ($order['column'] == $version_term) {
+      $query->orderBy("$alias.version", $order['direction']);
+    }
+  }
+}

+ 37 - 0
tripal_chado/includes/TripalFields/efo__array_design/efo__array_design_formatter.inc

@@ -0,0 +1,37 @@
+<?php
+
+class efo__array_design_formatter extends ChadoFieldFormatter {
+
+  // The default lable for this field.
+  public static $default_label = 'Array Design';
+
+  // The list of field types for which this formatter is appropriate.
+  public static $field_types = array('efo__array_design');
+
+  /**
+   * @see TripalFieldFormatter::view()
+   */
+  public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
+    if (count($items) > 0) {
+      
+      $name_term = chado_get_semweb_term('arraydesign', 'name');
+      $version_term = chado_get_semweb_term('arraydesign', 'version');
+      
+      $content = $items[0]['value'][$name_term];
+      if (isset($items[0]['value'][$version_term])) {
+        $content . '(' . $items[0]['value'][$version_term] . ')';
+      }
+      if (array_key_exists('entity', $items[0]['value'])) {
+        list($entity_type, $entity_id) = explode(':', $items[0]['value']['entity']);
+        $content = l($content, 'bio_data/' . $entity_id);
+      }
+
+      // The cardinality of this field is 1 so we don't have to
+      // iterate through the items array, as there will never be more than 1.
+      $element[0] = array(
+        '#type' => 'markup',
+        '#markup' => $content,
+      );
+    }
+  }
+}

+ 75 - 0
tripal_chado/includes/TripalFields/efo__array_design/efo__array_design_widget.inc

@@ -0,0 +1,75 @@
+<?php
+
+class efo__array_design_widget extends ChadoFieldWidget {
+
+  // The default lable for this field.
+  public static $default_label = 'Array Design';
+
+  // The list of field types for which this formatter is appropriate.
+  public static $field_types = array('efo__array_design');
+
+
+  /**
+   * @see TripalFieldWidget::form()
+   */
+  public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
+
+    parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
+
+    $settings = $this->field['settings'];
+    $field_name = $this->field['field_name'];
+    $field_type = $this->field['type'];
+    $field_table = $this->instance['settings']['chado_table'];
+    $field_column = $this->instance['settings']['chado_column'];
+
+    // Set the linker field appropriately.
+    $linker_field = 'chado-' . $field_table . '__' . $field_column;
+
+    $arraydesign_id = 0;
+    if (count($items) > 0 and array_key_exists($linker_field, $items[0])) {
+      $arraydesign_id = $items[0][$linker_field];
+    }
+
+    $widget['value'] = array(
+      '#type' => 'value',
+      '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
+    );
+    $sql = "SELECT arraydesign_id, name, version FROM {arraydesign} ORDER BY name";
+    $results = chado_query($sql);
+    $options = ['' => '- Select an array design -'];
+    while ($r = $results->fetchObject()) {
+      $options[$r->arraydesign_id] = $r->name;
+      if ($r->version) {
+        $options[$r->arraydesign_id] = $r->name . '(' . $r->version . ')';
+      }
+    }
+    $widget[$linker_field] = array(
+      '#type' => 'select',
+      '#title' => $element['#title'],
+      '#description' => $element['#description'],
+      '#options' => $options,
+      '#default_value' => $arraydesign_id,
+      '#required' => $element['#required'],
+      '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
+      '#delta' => $delta,
+    );
+  }
+
+  /**
+   * @see TripalFieldWidget::validate()
+   */
+  public function validate($element, $form, &$form_state, $langcode, $delta) {
+
+    $field_name = $this->field['field_name'];
+    $field_type = $this->field['type'];
+    $field_table = $this->instance['settings']['chado_table'];
+    $field_column = $this->instance['settings']['chado_column'];
+
+    // Set the linker field appropriately.
+    $linker_field = 'chado-' . $field_table . '__' . $field_column;
+    
+    // Make sure the value is set to the organism_id
+    $arraydesign_id = $form_state['values'][$field_name]['und'][0][$linker_field];
+    $form_state['values'][$field_name]['und'][0]['value'] = $arraydesign_id;
+  }
+}

+ 0 - 4
tripal_chado/includes/TripalFields/local__contact/local__contact_widget.inc

@@ -57,10 +57,6 @@ class local__contact_widget extends ChadoFieldWidget {
       '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
     );
 
-    $widget['chado-' . $field_table . '__' . $pkey] = array(
-      '#type' => 'value',
-      '#default_value' => $record_id,
-    );
     $widget[$linker_field] = array(
       '#type' => 'value',
       '#default_value' => $contact_id,

+ 2 - 2
tripal_chado/includes/TripalFields/obi__organism/obi__organism.inc

@@ -31,10 +31,10 @@ class obi__organism extends ChadoField {
   );
 
   // The default widget for this field.
-  public static $default_widget = 'OBI__organism_widget';
+  public static $default_widget = 'obi__organism_widget';
 
   // The default formatter for this field.
-  public static $default_formatter = 'OBI__organism_formatter';
+  public static $default_formatter = 'obi__organism_formatter';
 
 
   /**

+ 44 - 0
tripal_chado/includes/tripal_chado.fields.inc

@@ -532,6 +532,17 @@ function tripal_chado_bundle_fields_info_custom(&$info, $details, $entity_type,
           'type' => 'field_chado_storage',
         ),
       );
+      $field_name = 'efo__array_design';
+      $field_type = 'efo__array_design';
+      $info[$field_name] = array(
+        'field_name' => $field_name,
+        'type' => $field_type,
+        'cardinality' => 1,
+        'locked' => FALSE,
+        'storage' => array(
+          'type' => 'field_chado_storage',
+        ),
+      );
     }
   
   // For the pub_id field in the base table.
@@ -2077,6 +2088,39 @@ function tripal_chado_bundle_instances_info_custom(&$info, $entity_type, $bundle
         ),
       ),
     );
+    
+    $field_name = 'efo__array_design';
+    $info[$field_name] =  array(
+      'field_name' => $field_name,
+      'entity_type' => $entity_type,
+      'bundle' => $bundle->name,
+      'label' => 'Array design',
+      'description' => 'An instrument design which describes the design of the array.',
+      'required' => TRUE,
+      'settings' => array(
+        'auto_attach' => TRUE,
+        'chado_table' => 'assay',
+        'chado_column' => 'arraydesign_id',
+        'base_table' => 'assay',
+        'term_vocabulary' => 'EFO',
+        'term_name' => 'array design',
+        'term_accession' => '0000269',
+        
+      ),
+      'widget' => array(
+        'type' => 'efo__array_design_widget',
+        'settings' => array(
+          'display_label' => 1,
+        ),
+      ),
+      'display' => array(
+        'default' => array(
+          'label' => 'inline',
+          'type' => 'efo__array_design_formatter',
+          'settings' => array(),
+        ),
+      ),
+    );
   } 
   // Analysis Id
   if (array_key_exists('analysis_id', $schema['fields'])) {