Skip to content

Commit

Permalink
Merge pull request #99 from UofS-Pulse-Binfo/phenotype-ontology
Browse files Browse the repository at this point in the history
Phenotype ontology
  • Loading branch information
laceysanderson committed Sep 27, 2019
2 parents f9a708b + cdad63f commit a94baf8
Show file tree
Hide file tree
Showing 6 changed files with 705 additions and 0 deletions.
51 changes: 51 additions & 0 deletions includes/TripalFields/analyzedphenotypes.fields.inc
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ function analyzedphenotypes_bundle_fields_info($entity_type, $bundle) {
),
);

// Phenotype Ontology.
tripal_insert_cvterm(array(
'id' => 'NCIT:C52469',
'name' => 'Synonym',
'cv_name' => 'NCIT',
'definition' => 'A word having the same or nearly the same meaning as another word or other words in a language; a word or an expression that serves as a figurative or symbolic substitute for another',
));
$field_name = 'ncit__synonym';
$field_type = 'ncit__synonym';
$fields[$field_name] = array(
'field_name' => $field_name,
'type' => $field_type,
'cardinality' => 1,
'locked' => FALSE,
'storage' => array(
'type' => 'field_chado_storage',
),
);

// Phenotype Image
tripal_insert_cvterm(array(
'id' => 'NCIT:C48179',
Expand Down Expand Up @@ -503,6 +522,38 @@ function analyzedphenotypes_bundle_instances_info($entity_type, $bundle) {
),
);

// Phenotype Ontology - Synonym.
$field_name = 'ncit__synonym';
$field_type = 'ncit__synonym';
$instances[$field_name] = array(
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle->name,
'label' => 'Phenotype Ontology',
'description' => 'Phenotype Ontology.',
'required' => FALSE,
'settings' => array(
'term_vocabulary' => 'NCIT',
'term_name' => 'Synonym',
'term_accession' => 'c52469',
'auto_attach' => FALSE,
'chado_table' => $bundle->data_table,
'chado_column' => $bundle->data_table . '_id',
'base_table' => $bundle->data_table,
),
'widget' => array(
'type' => 'ncit__synonym_widget',
'settings' => array(),
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'ncit__synonym_formatter',
'settings' => array(),
),
),
);

// Phenotype Image.
$field_name = 'ncit__image';
$field_type = 'ncit__image';
Expand Down
220 changes: 220 additions & 0 deletions includes/TripalFields/ncit__synonym/ncit__synonym.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
<?php
/**
* @class
* Purpose: Provide quick browse functionality for entity pages
*
* Data: No data.
* Assumptions:
*/
class ncit__synonym extends ChadoField {
// --------------------------------------------------------------------------
// EDITABLE STATIC CONSTANTS
//
// The following constants SHOULD be set for each descendant class. They are
// used by the static functions to provide information to Drupal about
// the field and it's default widget and formatter.
// --------------------------------------------------------------------------
// The default label for this field.
public static $default_label = 'Crop Ontology';
// The default description for this field.
public static $default_description = 'Crop Ontology';
// The default widget for this field.
public static $default_widget = 'ncit__synonym_widget';
// The default formatter for this field.
public static $default_formatter = 'ncit__synonym_formatter';
// The module that manages this field.
public static $module = 'analyzedphenotypes';

// A list of global settings. These can be accessed within the
// globalSettingsForm. When the globalSettingsForm is submitted then
// Drupal will automatically change these settings for all fields.
// Once instances exist for a field type then these settings cannot be
// changed.
public static $default_settings = array(
'storage' => 'tripal_no_storage',
// It is expected that all fields set a 'value' in the load() function.
// In many cases, the value may be an associative array of key/value pairs.
// In order for Tripal to provide context for all data, the keys should
// be a controlled vocabulary term (e.g. rdfs:type). Keys in the load()
// function that are supported by the query() function should be
// listed here.
'browseable_keys' => 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 instance.
// 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. schema, SO, GO, PATO, etc.).
'term_vocabulary' => 'NCIT',
// The name of the term.
'term_name' => 'Synonym',
// The unique ID (i.e. accession) of the term.
'term_accession' => 'c52469',
// 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 where the options for this specific field are stored.
// This can be one of trpfancy_browse_options or trpfancy_browse_options_per_entity
// based on admin configuration. Default: trpfancy_browse_options.
'option_storage' => '',
// A list of browser types this field intends to provide.
'browser_types' => '',

'chado_table' => 'cvterm_relationship',
'chado_column' => 'cvterm_relationship_id',
'base_table' => 'cvterm_relationship'

);

// A boolean specifying that users should not be allowed to create
// fields and instances of this field type through the UI. Such
// fields can only be created programmatically with field_create_field()
// and field_create_instance().
public static $no_ui = FALSE;
// A boolean specifying that the field will not contain any data. This
// should exclude the field from web services or downloads. An example
// could be a quick browse field that appears on the page that redirects
// the user but otherwise provides no data.
public static $no_data = TRUE;

/**
* Loads the field values from the underlying data store.
*
* @param $entity
*
* @return
* An array of the following format:
* $entity->{$field_name}['und'][0]['value'] = $value;
* where:
* - $entity is the entity object to which this field is attached.
* - $field_name is the name of this field
* - 'und' is the language code (in this case 'und' == undefined)
* - 0 is the cardinality. Increment by 1 when more than one item is
* available.
* - 'value' is the key indicating the value of this field. It should
* always be set. The value of the 'value' key will be the contents
* used for web services and for downloadable content. The value
* should be of the follow format types: 1) A single value (text,
* numeric, etc.) 2) An array of key value pair. 3) If multiple entries
* then cardinality should incremented and format types 1 and 2 should
* be used for each item.
* The array may contain as many other keys at the same level as 'value'
* but those keys are for internal field use and are not considered the
* value of the field.
*
*
*/
public function load($entity) {
$record = $entity->chado_record_id;

if ($record) {
// We need the term configuration for term Related used when creating relationship.
$sysvar = ap_get_variablenames(
array('variablename' => 'related'),
array('set' => 'terms')
);
$sysvar_related = variable_get($sysvar);

if ($sysvar_related) {
// Inspect to see if this trait has crop ontology set in chado.cvterm_relationship
// using what is known - type_id of ontology Related (in configuration) and the object_id
// which is the current trait.

$identifiers = array(
'column' => array('cvterm_relationship_id', 'subject_id'),
'value' => array('type_id' => $sysvar_related, 'object_id' => $record)
);
$rel = chado_select_record('cvterm_relationship', $identifiers['column'], $identifiers['value']);

$field_name = $this->field['field_name'];
$field_table = 'chado-cvterm_relationship__';

if ($rel) {
// Has crop ontology.

// For chado mechnanism.
$entity->{$field_name}['und'][0] = array(
// Row id #.
$field_table . 'cvterm_relationship_id' => $rel[0]->cvterm_relationship_id,
// Relationship details.
$field_table . 'type_id' => $sysvar_related,
$field_table . 'object_id' => $entity->chado_record_id,
$field_table . 'subject_id' => $rel[0]->subject_id
);

// For end user.
// Sending the term, accession and cv name. Last two items are used to constuct
// direct link to vocabulary lookup to a more details about a term.
$termprop = chado_generate_var('cvterm', array(
'cvterm_id' => $rel[0]->subject_id
));

// Term.
$relationship_term = tripal_get_chado_semweb_term('cvterm_relationship', 'subject_id');
$entity->{$field_name}['und'][0]['value'][ $relationship_term ] = $termprop->name;

// Accession.
$accession = tripal_get_chado_semweb_term('dbxref', 'accession');
$entity->{$field_name}['und'][0]['value'][ $accession ] = $termprop->dbxref_id->accession;

// CV.
$cv_term = tripal_get_chado_semweb_term('cv', 'name');
$entity->{$field_name}['und'][0]['value'][ $cv_term ] = $termprop->cv_id->name;
}
else {
// Crop ontology not set.
// Nothing here, just forward what we know so far, Related term and the trait id.
$entity->{$field_name}['und'][0] = array(
'value' => array(),
$field_table . 'object_id' => $entity->chado_record_id,
$field_table . 'type_id' => $sysvar_related,
);
}
}
}
}

/**
* Provides a form for the 'Field Settings' of an instance of this field.
*
* This function corresponds to the hook_field_instance_settings_form()
* function of the Drupal Field API.
*
* Validation of the instance settings form is not supported by Drupal, but
* the TripalField class does provide a mechanism for supporting validation.
* To allow for validation of your setting form you must call the parent
* in your child class:
*
* @code
* $element = parent::instanceSettingsForm();
* @endcode
*
* Please note, the form generated with this function does not easily
* support AJAX calls in the same way that other Drupal forms do. If you
* need to use AJAX you must manually alter the $form in your ajax call.
* The typical way to handle updating the form via an AJAX call is to make
* the changes in the form function itself but that doesn't work here.
*/
public function instanceSettingsForm() {

// Retrieve the current settings.
// If this field was just created these will contain the default values.
$settings = $this->instance['settings'];

// Allow the parent Tripal Field to set up the form element for us.
$element = parent::instanceSettingsForm();

return $element;
}
}
77 changes: 77 additions & 0 deletions includes/TripalFields/ncit__synonym/ncit__synonym_formatter.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* @class
* Purpose: Provide a quick search on entity pages which submits/redirects to a full search.
*
* Display: A simple textfield search form.
* Configuration:
* - path to the full search.
* - the URL token (query parameter) the value applies to.
* - help text.
* - textfield placeholder.
* - search button text.
* - autocomplete path.
*/
class ncit__synonym_formatter extends TripalFieldFormatter {
// The default label for this field.
public static $default_label = 'Crop Ontology';

// The list of field types for which this formatter is appropriate.
public static $field_types = array('ncit__synonym');

/**
* Provides the display for a field
*
* This function corresponds to the hook_field_formatter_view()
* function of the Drupal Field API.
*
* This function provides the display for a field when it is viewed on
* the web page. The content returned by the formatter should only include
* what is present in the $items[$delta]['values] array. This way, the
* contents that are displayed on the page, via webservices and downloaded
* into a CSV file will always be identical. The view need not show all
* of the data in the 'values' array.
*
* @param $element
* @param $entity_type
* @param $entity
* @param $langcode
* @param $items
* @param $display
*
* @return
* An element array compatible with that returned by the
* hook_field_formatter_view() function.
*/
public function view(&$element, $entity_type, $entity, $langcode, $items, $display) {
// No show when no crop ontology for a trait.
$element = null;

// Render crop ontology for this trait.
if ($items[0]['value']) {
// Style information.
drupal_add_css(drupal_get_path('module', 'analyzedphenotypes') . '/includes/TripalFields/ncit__synonym/theme/style_cp_synonym_field.css');

// Refer to this ID for CSS styling.
$id = 'ap-field-crop_ontology_synonym';

list($term, $accession, $cv) = array_values($items[0]['value']);

// If we can't find the term then do not provide the link as it will just cause a term not found
// in the term lookup module.
// @see tripal_vocabulary_lookup_term_page()
// Tripal.term_lookup.inc.
$lookterm = tripal_get_term_details($cv, $accession);

$cvterm_lookup = (!$lookterm) ? ''
: l('Vocabulary Details', 'cv/lookup/' . $cv . '/' . $accession, array('attributes' => array('target' => '_blank')));

$element[0] = array(
'#type' => 'markup',
'#markup' => '<div id="' . $id . '">' . $term . '</div> ' . $cvterm_lookup,
);
}

return $element;
}
}
Loading

0 comments on commit a94baf8

Please sign in to comment.