private function sbo__relationship::loadRelationship

3.x sbo__relationship.inc private sbo__relationship::loadRelationship($relationship, &$entity, $delta)
1 call to sbo__relationship::loadRelationship()
sbo__relationship::load in tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship.inc

File

tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship.inc, line 186

Class

sbo__relationship

Code

private function loadRelationship($relationship, &$entity, $delta) {

  $field_name = $this->field['field_name'];
  $field_table = $this->instance['settings']['chado_table'];
  $base_table = $this->instance['settings']['base_table'];

  $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
  $rel_type = $relationship->type_id->name;
  $verb = $this->get_rel_verb($rel_type);

  // Get the foreign keys for the subject and object tables
  $subject_fkey_table = '';
  $object_fkey_table = '';

  $schema = chado_get_schema($field_table);
  $pkey = $schema['primary key'][0];
  $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];

  // Not all tables have the columns named 'subject_id' and 'object_id'.
  // some have variations on that name and we need to determine what they are.
  $fkeys = $schema['foreign keys'];
  $subject_id_key = 'subject_id';
  $object_id_key = 'object_id';
  foreach ($fkeys as $fktable => $details) {
    foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
      if (preg_match('/^subject_.*id/', $fkey_lcolumn)) {
        $subject_fkey_table = $fktable;
        $subject_id_key = $fkey_lcolumn;
      }
      if (preg_match('/^object_.*id/', $fkey_lcolumn)) {
        $object_fkey_table = $fktable;
        $object_id_key = $fkey_lcolumn;
      }
    }
  }

  // Get the schemas for the subject and object table.  These should
  // be the same as the base table but just to be safe we'll get them
  // separately.
  $subject_schema = chado_get_schema($subject_fkey_table);
  $subject_pkey = $subject_schema['primary key'][0];
  $object_schema = chado_get_schema($object_fkey_table);
  $object_pkey = $object_schema['primary key'][0];

  // Not all realtionshp tables have a name field (e.g. organism_relationship)
  // threfore in some cases we need to dig a bit deeper to get the entity
  // name and the entity type name.
  $subject_name = '';
  $subject_type = '';
  $object_name = '';
  $object_type = '';

  // The linked to table of a relationship linker table may not always
  // have a type_id or name field.  So we have to be a bit more
  // specific about how we set some variables.
  switch ($relationship->tablename) {
    case 'acquisition_relationship':
      $subject_type = 'acquisition';
      $object_type = 'acquisition';
      break;
    case 'analysis_relationship':
      $subject_type = 'analysis';
      $object_type = 'analysis';
      break;
    case 'biomaterial_relationship':
      $subject_type = 'biomaterial';
      $object_type = 'biomaterial';
      break;
    case 'cell_line_relationship':
      $subject_type = 'cell_line';
      $object_type = 'cell_line';
      break;
    case 'element_relationship':
      $subject_name = $relationship->$subject_id_key->feature_id->name;
      $object_name = $relationship->$object_id_key->feature_id->name;
      break;
    case 'organism_relationship':
      $subject_name = $relationship->$subject_id_key->genus . ' ' . $relationship->$subject_id_key->species;
      $object_name = $relationship->$object_id_key->genus . ' ' . $relationship->$object_id_key->species;
      $subject_type = 'organism';
      $object_type = 'organism';
      break;
    case 'project_relationship':
      $subject_type = 'project';
      $object_type = 'project';
      break;
    case 'phylonode_relationship':
      $subject_name = $relationship->$subject_id_key->label;
      $object_name = $relationship->$object_id_key->label;
      break;
    case 'pub_relationship':
      $subject_name = $relationship->$subject_id_key->uniquename;
      $object_name = $relationship->$object_id_key->uniquename;
      break;
    case 'quantification_relationship':
      $subject_type = 'quantification';
      $object_type = 'quantification';
      break;
    default:
      $subject_name = isset($relationship->$subject_id_key->name) ? $relationship->$subject_id_key->name : '';
      $subject_type = isset($relationship->$subject_id_key->type_id) ? $relationship->$subject_id_key->type_id->name : '';
      $object_name = isset($relationship->$object_id_key->name) ? $relationship->$object_id_key->name : '';
      $object_type = isset($relationship->$object_id_key->type_id) ? $relationship->$object_id_key->type_id->name : '';
  }

  $entity->{$field_name}['und'][$delta]['value'] = array(
    'local:relationship_subject' => array(
      'rdfs:type' => $subject_type,
      'schema:name' => $subject_name,
    ),
    'local:relationship_type' => $relationship->type_id->name,
    'local:relationship_object' => array(
      'rdfs:type' => $object_type,
      'schema:name' => $object_name,
      'entity' => 'TripalEntity:' . $entity->id,
    )
  );

  // If the subject or object have a unqiuename then add that in for refernce.
  if (property_exists($relationship->$subject_id_key, 'uniquename')) {
    $entity->{$field_name}['und'][$delta]['value']['local:relationship_subject']['data:0842'] = $relationship->$subject_id_key->uniquename;
  }
  if (property_exists($relationship->$object_id_key, 'uniquename')) {
    $entity->{$field_name}['und'][$delta]['value']['local:relationship_object']['data:0842'] = $relationship->$object_id_key->uniquename;
  }

  // Add in the TripalEntity ids if these base records in the relationship
  // are published.
  if (property_exists($relationship->$subject_id_key, 'entity_id')) {
    $entity_id = $relationship->$subject_id_key->entity_id;
    $entity->{$field_name}['und'][$delta]['value']['local:relationship_subject']['entity'] = 'TripalEntity:' . $entity_id;
  }
  if (property_exists($relationship->$object_id_key, 'entity_id')) {
    $entity_id = $relationship->$object_id_key->entity_id;
    $entity->{$field_name}['und'][$delta]['value']['local:relationship_object']['entity'] = 'TripalEntity:' . $entity_id;
  }

  // Add the clause to the values array.  The clause is a written version
  // of the relationships.
  $rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
  // Remember the current entity could be either the subject or object!
  // Example: The genetic_marker, MARKER1 , derives from the sequence_variant, VARIANT1.
  // The above relationship will be shown both on marker and variant pages
  // and as such both subject and object names need to be shown.
  $clause = 'The ' . $subject_type . ', ' .
    $subject_name . ', ' . $verb . ' ' . $rel_type_clean . ' the ' .
    $object_type . ', ' . $object_name . '.';
  $entity->{$field_name}['und'][$delta]['value']['SIO:000493'] = $clause;

  // Adding a label allows us to provide a single text value for the
  // entire field. It is this text value that can be used in tab/csv
  // downloaders.
  $entity->{$field_name}['und'][$delta]['value']['rdfs:label'] = $clause;

  $entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__' . $pkey] = $relationship->$pkey;
  $entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__' . $subject_id_key] = $relationship->$subject_id_key->$subject_pkey;
  $entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__type_id'] = $relationship->type_id->cvterm_id;
  $entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__' . $object_id_key] = $relationship->$object_id_key->$object_pkey;

  // For the widget to work properly we will preform values.
  $entity->{$field_name}['und'][$delta]['type_name'] = $relationship->type_id->name;
  $entity->{$field_name}['und'][$delta]['subject_name'] = $subject_name . ' [id: ' . $relationship->$subject_id_key->$subject_pkey . ']';
  $entity->{$field_name}['und'][$delta]['object_name'] = $object_name . ' [id: ' . $relationship->$object_id_key->$object_pkey . ']';
  if (array_key_exists('value', $schema['fields'])) {
    $entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__value'] = $relationship->value;
  }
  if (array_key_exists('rank', $schema['fields'])) {
    $entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__rank'] = $relationship->rank;
  }
}