Skip to content

Commit

Permalink
Merge pull request #1578 from tripal/tv4g1-issue1414-ChadoAnalysisDef…
Browse files Browse the repository at this point in the history
…ault

Tripal 4 ChadoAnalysisDefault field
  • Loading branch information
laceysanderson committed Aug 10, 2023
2 parents 7284294 + 7095de9 commit 89adb84
Show file tree
Hide file tree
Showing 7 changed files with 906 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1202,21 +1202,21 @@ fields:
region: content
weight: 10

- name: phylotree_comment
- name: phylotree_dbxref_id
content_type: phylotree
label: Notes
type: chado_text_type
description: Comments, typically from users.
label: Dbxref ID
type: chado_integer_type
description: The primary key of the database reference (dbxref) to attach to this tree. This field is a placeholder until we develop a more user friendly database reference field.
cardinality: 1
required: false
required: true
storage_settings:
storage_plugin_id: chado_storage
storage_plugin_settings:
base_table: phylotree
base_column: comment
base_column: dbxref_id
settings:
termIdSpace: schema
termAccession: comment
termIdSpace: data
termAccession: "2091"
display:
view:
default:
Expand All @@ -1243,18 +1243,70 @@ fields:
type_column: type_id
settings:
termIdSpace: data
termAccession: 0872
termAccession: '0872'
fixed_value: data:0872
display:
view:
default:
region: content
label: above
weight: 10
weight: 20
form:
default:
region: content
weight: 10
weight: 20

- name: phylotree_analysis
content_type: phylotree
label: Analysis
type: chado_analysis_default
description: The analysis from which the tree was generated.
cardinality: 1
required: false
storage_settings:
storage_plugin_id: chado_storage
storage_plugin_settings:
base_table: phylotree
base_column: analysis_id
settings:
termIdSpace: operation
termAccession: '2945'
display:
view:
default:
region: content
label: above
weight: 25
form:
default:
region: content
weight: 25

- name: phylotree_comment
content_type: phylotree
label: Notes
type: chado_text_type
description: Comments, typically from users.
cardinality: 1
required: false
storage_settings:
storage_plugin_id: chado_storage
storage_plugin_settings:
base_table: phylotree
base_column: comment
settings:
termIdSpace: schema
termAccession: comment
display:
view:
default:
region: content
label: above
weight: 30
form:
default:
region: content
weight: 30

- name: physical_map_name
content_type: physical_map
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Drupal\tripal_chado\Plugin\Field\FieldFormatter;

use Drupal\tripal\TripalField\TripalFormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tripal_chado\TripalField\ChadoFormatterBase;

/**
* Plugin implementation of default Tripal analysis formatter.
*
* @FieldFormatter(
* id = "chado_analysis_formatter_default",
* label = @Translation("Chado analysis formatter"),
* description = @Translation("A chado analysis formatter"),
* field_types = {
* "chado_analysis_default"
* }
* )
*/
class ChadoAnalysisFormatterDefault extends ChadoFormatterBase {

/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];

foreach($items as $delta => $item) {
$elements[$delta] = [
"#markup" => $item->get('analysis_name')->getString()
];
}

return $elements;
}
}
168 changes: 168 additions & 0 deletions tripal_chado/src/Plugin/Field/FieldType/ChadoAnalysisDefault.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

namespace Drupal\tripal_chado\Plugin\Field\FieldType;

use Drupal\tripal_chado\TripalField\ChadoFieldItemBase;
use Drupal\tripal_chado\TripalStorage\ChadoVarCharStoragePropertyType;
use Drupal\tripal_chado\TripalStorage\ChadoIntStoragePropertyType;
use Drupal\tripal_chado\TripalStorage\ChadoTextStoragePropertyType;
use Drupal\tripal\TripalField\TripalFieldItemBase;
use Drupal\tripal\TripalStorage\StoragePropertyValue;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\core\Form\FormStateInterface;
use Drupal\core\Field\FieldDefinitionInterface;

/**
* Plugin implementation of default Tripal analysis field type.
*
* @FieldType(
* id = "chado_analysis_default",
* label = @Translation("Analysis"),
* description = @Translation("Application of analytical methods to existing data of a specific type"),
* default_widget = "chado_analysis_widget_default",
* default_formatter = "chado_analysis_formatter_default",
* cardinality = 1
* )
*/
class ChadoAnalysisDefault extends ChadoFieldItemBase {

public static $id = 'chado_analysis_default';

/**
* {@inheritdoc}
*/
public static function mainPropertyName() {
// Overrides the default of 'value'
return 'analysis_name';
}

/**
* {@inheritdoc}
*/
public static function defaultFieldSettings() {
$settings = parent::defaultFieldSettings();
$settings['termIdSpace'] = 'operation';
$settings['termAccession'] = '2945';
return $settings;
}

/**
* {@inheritdoc}
*/
public static function defaultStorageSettings() {
$settings = parent::defaultStorageSettings();
return $settings;
}

/**
* {@inheritdoc}
*/
public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {

$elements = parent::storageSettingsForm($form, $form_state, $has_data);
$storage_settings = $this->getSetting('storage_plugin_settings');
$base_table = $form_state->getValue(['settings', 'storage_plugin_settings', 'base_table']);

// Only present base tables that have a foreign key to analysis.
$elements['storage_plugin_settings']['base_table']['#options'] = $this->getBaseTables('analysis');

// Add a validation to make sure the base table has a foreign
// key to analysis_id in the chado.analysis table.
$elements['storage_plugin_settings']['base_table']['#element_validate'] = [[static::class, 'storageSettingsFormValidate']];
return $elements;
}

/**
* {@inheritdoc}
*/
public static function tripalTypes($field_definition) {
$entity_type_id = $field_definition->getTargetEntityTypeId();

// Get the Chado table and column this field maps to.
$storage_settings = $field_definition->getSetting('storage_plugin_settings');
$base_table = $storage_settings['base_table'];

// If we don't have a base table then we're not ready to specify the
// properties for this field.
if (!$base_table) {
$record_id_term = 'operation:2945';
return [
new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'record_id', $record_id_term, [
'action' => 'store_id',
'drupal_store' => TRUE,
])
];
}

// Get the connecting information for the foreign key from the
// base table to the analysis table.
$chado = \Drupal::service('tripal_chado.database');
$schema = $chado->schema();
$base_table_def = $schema->getTableDef($base_table, ['format' => 'Drupal']);
$analysis_table_def = $schema->getTableDef('analysis', ['format' => 'Drupal']);
$base_pkey_col = $base_table_def['primary key'];
$base_fkey_col = 'analysis_id';

// Create variables to store the terms for the analysis.
$storage = \Drupal::entityTypeManager()->getStorage('chado_term_mapping');
$mapping = $storage->load('core_mapping');
$record_id_term = 'SIO:000729';
$analysis_id_term = $mapping->getColumnTermId('analysis', 'analysis_id');
$analysis_name_term = $mapping->getColumnTermId('analysis', 'name');
$analysis_name_length = $analysis_table_def['fields']['name']['size'];

// Define field properties, this is a simple lookup in the analysis table
$properties = [];
$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'record_id', $record_id_term, [
'action' => 'store_id',
'drupal_store' => TRUE,
'chado_table' => $base_table,
'chado_column' => $base_pkey_col,
]);
$properties[] = new ChadoIntStoragePropertyType($entity_type_id, self::$id, 'analysis_id', $analysis_id_term, [
'action' => 'store',
'chado_table' => $base_table,
'chado_column' => $base_fkey_col,
]);
$properties[] = new ChadoVarCharStoragePropertyType($entity_type_id, self::$id, 'analysis_name', $analysis_name_term, $analysis_name_length, [
'action' => 'join',
'path' => $base_table . '.' . $base_fkey_col . '>analysis.analysis_id',
'chado_column' => 'name',
'as' => 'analysis_name',
]);

return $properties;
}

/**
* Form element validation handler
*
* @param array $form
* The form where the settings form is being included in.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state of the (entire) configuration form.
*/
public static function storageSettingsFormValidate(array $form, FormStateInterface $form_state) {
$settings = $form_state->getValue('settings');
if (!array_key_exists('storage_plugin_settings', $settings)) {
return;
}

// Validation confirms that the base table has a foreign key
// to analysis_id in the chado.analysis table.
// This validation might be redundant, since we only present
// valid base tables to the user, but let's play it safe.
$base_table = $settings['storage_plugin_settings']['base_table'];
$chado = \Drupal::service('tripal_chado.database');
$schema = $chado->schema();
$base_table_def = $schema->getTableDef($base_table, ['format' => 'Drupal']);
$base_fkey_col = 'analysis_id';
$fkeys = $base_table_def['foreign keys']['analysis']['columns'] ?? [];
if (!in_array($base_fkey_col, $fkeys)) {
$form_state->setErrorByName('storage_plugin_settings][base_table',
'The selected base table does not have a foreign key to analysis_id,'
. ' this field cannot be used on this content type.');
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Drupal\tripal_chado\Plugin\Field\FieldWidget;

use Drupal\tripal\TripalField\TripalWidgetBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tripal_chado\TripalField\ChadoWidgetBase;

/**
* Plugin implementation of default Chado analysis widget.
*
* @FieldWidget(
* id = "chado_analysis_widget_default",
* label = @Translation("Chado Analysis Widget"),
* description = @Translation("The default analysis widget."),
* field_types = {
* "chado_analysis_default"
* }
* )
*/
class ChadoAnalysisWidgetDefault extends ChadoWidgetBase {

/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

// Get the list of analyses.
$analyses = [];
$chado = \Drupal::service('tripal_chado.database');

// In addition to getting a sorted list of analyses, include
// the analysisprop rdfs:type when it is present, e.g.
// genome assembly or genome annotation.
$sql = 'SELECT A.analysis_id, A.name, TYPE.value FROM {1:analysis} A
LEFT JOIN (
SELECT AP.analysis_id, AP.value FROM {1:analysisprop} AP
LEFT JOIN {1:cvterm} T ON AP.type_id=T.cvterm_id
LEFT JOIN {1:cv} CV ON T.cv_id=CV.cv_id
WHERE T.name=:cvterm
AND CV.name=:cv
) AS TYPE
ON A.analysis_id=TYPE.analysis_id
ORDER BY A.name';
$results = $chado->query($sql, [':cvterm' => 'type', ':cv' => 'rdfs']);

while ($analysis = $results->fetchObject()) {
$type_text = $analysis->value ? ' (' . $analysis->value . ')' : '';
$analyses[$analysis->analysis_id] = $analysis->name . $type_text;
}

$item_vals = $items[$delta]->getValue();
$record_id = $item_vals['record_id'] ?? 0;
$analysis_id = $item_vals['analysis_id'] ?? 0;

$elements = [];
$elements['record_id'] = [
'#type' => 'value',
'#default_value' => $record_id,
];
$elements['analysis_id'] = $element + [
'#type' => 'select',
'#options' => $analyses,
'#default_value' => $analysis_id,
'#placeholder' => $this->getSetting('placeholder'),
'#empty_option' => '-- Select --',
];

return $elements;
}

}
1 change: 1 addition & 0 deletions tripal_chado/src/Plugin/TripalStorage/ChadoStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ protected function buildChadoRecords($values, bool $is_store) {

}
}
if (!array_key_exists('conditions', $record)) print_r($record);
foreach ($record['conditions'] as $chado_column => $val) {
if (is_array($val['value']) and $val['value'][0] == 'REPLACE_BASE_RECORD_ID') {
$base_table = $val['value'][1];
Expand Down

0 comments on commit 89adb84

Please sign in to comment.