Skip to content

Commit

Permalink
Tripal 4 files for sequence_coordinates-field: tv4g1-issue1414-data__…
Browse files Browse the repository at this point in the history
…sequence_coordinates
  • Loading branch information
Malladi committed Nov 28, 2023
1 parent 3d1f4be commit e884fe8
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,33 @@ fields:
region: content
weight: 10

- name: gene_sequence_coordinates
content_type: gene
label: Sequence Coordinates
type: chado_sequence_coordinates_default
description: Locations on reference sequences where the feature is located.
cardinality: -1
required: false
storage_settings:
storage_plugin_id: chado_storage
storage_plugin_settings:
base_table: featureloc
linker_table: feature
linker_fkey_column: feature_id
settings:
termIdSpace: data
termAccession: "2012"
display:
view:
default:
region: content
label: above
weight: 10
form:
default:
region: content
weight: 10

- name: mrna_name
content_type: mRNA
label: Name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Drupal\tripal_chado\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\tripal_chado\TripalField\ChadoFormatterBase;

/**
* Plugin implementation of default Tripal string type formatter.
*
* @FieldFormatter(
* id = "chado_sequence_coordinates_formatter_default",
* label = @Translation("Chado sequence coordinates default formatter"),
* description = @Translation("The default sequence coordinates formatter allows curators to view sequence coordinates (min, max, strand and phase) of the feature."),
* field_types = {
* "chado_sequence_coordinates_default"
* }
* )
*/
class ChadoSequenceCoordinatesFormatterDefault extends ChadoFormatterBase
{
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode)
{
$elements = [];
$reference_term = 'data:3002';

$locations = [];

foreach ($items as $item) {

if (!empty($item['value'])) {
$loc_rec = '';
$feature_ref_val = $item['value'][$reference_term];

$fmin_val = $item->get('fmin')->getString();
if (!empty($fmin_val)) {
$loc_rec .= $item['value'][$feature_ref_val] . ':' .$fmin_val . "..";
}
$fmax_val = $item->get('fmax')->getString();
if (!empty($fmax_val)) {
$loc_rec .= $item['value'][$fmax_val].'; ';
}
$phase_val = $item->get('phase')->getString();
if (!empty($phase_val)) {
$loc_rec .= $item['value'][$phase_val].'; ';
}
$strand_term = $item->get('strand')->getString();
if (!empty($strand_val)) {
$strand_symb = match( $item['value'][$strand_term] ) {
-1 => '-',
1 => '+',
default => '',
};
$loc_rec .= $strand_symb;
}
$locations[] = $loc_rec;
}
}
if ( !$locations ) {
$content = 'This feature is not located on any sequence.';
}
else {
$content = implode('<br />', $locations);
}
// The cardinality of this field is always 1, so only create element for $delta of zero.
$elements[0] = [
'#type' => 'markup',
'#markup' => $content,
];
return $elements;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Drupal\tripal_chado\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\tripal_chado\TripalField\ChadoFormatterBase;

/**
* Plugin implementation of default Tripal string type formatter.
*
* @FieldFormatter(
* id = "chado_sequence_coordinates_formatter_table",
* label = @Translation("Chado sequence coordinates table formatter"),
* description = @Translation("The sequence coordinates widget allows curators to manually enter feature sequence coordinates information on the content edit page."),
* field_types = {
* "chado_sequence_coordinates_default"
* }
* )
*/
class ChadoSequenceCoordinatesFormatterTable extends ChadoFormatterBase {

public static $default_settings = [
'caption' => 'This @content_type has the following sequence coordinates:',
'expand_strand' => TRUE,
];

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

// Get the settings and set defaults.
$settings = $display['settings'];
foreach ($this::$default_settings as $key => $value) {
if (!isset($settings[$key])) {
$settings[$key] = $value;
}
}

// Replace tokens in the caption.
$settings['caption'] = t($settings['caption'],
['@content_type' => $entity->rdfs__type['und'][0]['value']]);

// For each location, add it to the table.
$header = ['Name', 'Location', 'Strand', 'Phase'];

$locations = [];

foreach ($items as $item) {

if (!empty($item['value'])) {
$fmin_term = $item->get('fmin')->getString();
$fmax_term = $item->get('fmax')->getString();
$strand_term = $item->get('strand')->getString();
$phase_term = $item->get('phase')->getString();

$strand_val = $item['value'][$strand_term];
if ($settings['expand_strand']) {
$strand_symb = match( $strand_val ) {
-1 => '-',
1 => '+',
default => '<span style="color:#B3B3B3">unknown</span>',
};
}
}

$locations[] = [
$item['value'][$reference_term],
$item['value'][$fmin_term] . '..' . $fmax = $item['value'][$fmax_term],
$strand_symb, $item['value'][$phase_term],
];
}

if ( !$locations ) {
$content = 'This feature is not located on any sequence.';
}
else {
$content = $locations;
}

// The cardinality of this field is always 1, so only create element for $delta of zero.
$elements[0] = [
'#type' => 'markup',
'#theme' => 'table',
'#header' => $header,
'#rows' => $content,
'#caption' => $settings['caption'],
];

return $elements;
}
}

0 comments on commit e884fe8

Please sign in to comment.