Selaa lähdekoodia

Forgot to add new files

Stephen Ficklin 10 vuotta sitten
vanhempi
commit
dd7dd9c21e

+ 68 - 0
tripal_cv/entities/tripal_cv.entities.inc

@@ -0,0 +1,68 @@
+<?php
+
+require_once 'tripal_cv.vocabulary.inc';
+require_once 'tripal_cv.vocabulary_term.inc';
+require_once 'tripal_cv.organism.inc';
+require_once 'tripal_cv.feature.inc';
+
+/**
+ * Implements hook_entity_info().
+ */
+function tripal_cv_entity_info() {
+  $entities = array();
+
+  // The vocabulary and vocabulary term entities.
+  $entities['trp_vocabulary'] = array(
+    'label' => t('Controlled Vocabulary'),
+    'plural label' => t('Controlled Vocabularies'),
+    'entity class' => 'TrpVocabulary',
+    'controller class' => 'TrpVocabularyController',
+    'entity keys' => array(
+      'id' => 'internal_id'
+    ),
+  );
+  $entities['trp_vocabulary_term'] = array(
+    'label' => t('Controlled Vocabulary Term'),
+    'plural label' => t('Controlled Vocabulary Terms'),
+    'entity class' => 'TrpVocabularyTerm',
+    'controller class' => 'TrpVocabularyTermController',
+    'entity keys' => array(
+      'id' => 'internal_id'
+    ),
+  );
+
+  // We want to dynamically add entities for terms specifically set
+  // by the site admin
+  $values = array(
+    'cv_id' => array(
+      'name' => 'sequence'
+    ),
+    'name' => 'gene'
+  );
+  $cvterm = chado_generate_var('cvterm', $values);
+  $entity_id = 'trp_' . $cvterm->dbxref_id->db_id->name . '_' . $cvterm->dbxref_id->accession;
+  $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
+  $plural_label = $label . '(s)';
+  $entity_class = 'TrpTerm' . $entity_id;
+  $entities[$entity_id] = array(
+    'label' => $label,
+    'plural label' => $plural_label,
+    'entity class' => $entity_class,
+    'controller class' => 'TrpFeatureController',
+    'entity keys' => array(
+      'id' => 'internal_id'
+    ),
+  );
+
+  // The organism entity.
+  $entities['trp_organism'] = array(
+    'label' => t('Organism'),
+    'plural label' => t('Organism'),
+    'entity class' => 'TrpOrganism',
+    'controller class' => 'TrpOrganismController',
+    'entity keys' => array(
+      'id' => 'internal_id'
+    ),
+  );
+  return $entities;
+}

+ 101 - 0
tripal_cv/entities/tripal_cv.feature.inc

@@ -0,0 +1,101 @@
+<?php
+/**
+ *
+ *
+ */
+class TrpFeatureController extends DrupalDefaultEntityController {
+
+  /**
+   *
+   * @param $ids
+   *   An array of entity IDs, or FALSE to load all entities.
+   * @param $conditions
+   *   DEPRECATED. An array of conditions. Keys are field names on the entity's
+   *   base table.Values will be compared for equality. All the comparisons will
+   *   be ANDed together. This parameter is deprecated; use an EntityFieldQuery
+   *   instead.
+   * @return multitype:An
+   */
+  public function load($ids = array(), $conditions = array()) {
+    $entities = array();
+    $queried_entities = array();
+
+    $values = array('feature_id' => $ids);
+    $options = array('return_array' => TRUE);
+    $features = chado_generate_var('feature', $values, $options);
+    foreach ($features as $result) {
+      $feature = new stdClass();
+      $feature->internal_id = $result->feature_id;
+      $feature->type_id = $result->type_id->dbxref_id->db_id->name . ':' . $result->type_id->dbxref_id->accession;
+
+      // Add the names by which this feature is known.
+      $names = array();
+      $names[] = $result->name;
+      $names[] = $result->uniquename;
+      $feature->name = array_unique($names);
+
+      // Add in the synonyms.
+      $sql = "
+        SELECT *
+        FROM {feature_synonym} FS
+          INNER JOIN {synonym} S ON FS.synonym_id = S.synonym_id
+        WHERE FS.feature_id = :feature_id
+      ";
+      $synonym_results = chado_query($sql, array(':feature_id' => $result->feature_id));
+      $synonyms = array();
+      while ($synonym = $synonym_results->fetchObject()) {
+        $synonyms[] = $synonym->name;
+      }
+      if (count($synonyms) > 0) {
+        $feature->synonyms = $synonyms;
+      }
+
+      // Add in database cross-references.
+      $xrefs = array();
+      if ($result->dbxref_id) {
+        $xrefs[] = $result->dbxref_id->db_id->name . ':' . $result->dbxref_id->accession;
+      }
+      $sql = "
+        SELECT DB.name as namespace, DBX.accession
+        FROM {feature_dbxref} FDBX
+          INNER JOIN {dbxref} DBX ON DBX.dbxref_id = FDBX.dbxref_id
+          INNER JOIN {db} DB      ON DB.db_id      = DBX.db_id
+        WHERE FDBX.feature_id = :feature_id
+      ";
+      $dbxref_results = chado_query($sql, array(':feature_id' => $result->feature_id));
+
+      while ($dbxref = $dbxref_results->fetchObject()) {
+        if ($dbxref->namespace != 'GFF_source') {
+          $xrefs[] = $dbxref->namespace . ':' . $dbxref->accession;
+        }
+      }
+      if (count($xrefs) > 0) {
+        $feature->xref_id = $xrefs;
+      }
+
+      // Add in the expand array
+      $expand = array();
+      $expand[] = 'chado:organism';
+      $feature->expand = $expand;
+
+      // Add in the embeded elements
+      $embedded = array();
+      $embedded['chado:organism'] = entity_load('trp_organism', array($result->organism_id->organism_id));
+      $feature->embedded = $embedded;
+
+      $queried_entities[$feature->internal_id] = $feature;
+    }
+
+    // Pass all entities loaded from the database through $this->attachLoad(),
+    // which attaches fields (if supported by the entity type) and calls the
+    // entity type specific load callback, for example hook_node_load().
+    if (!empty($queried_entities)) {
+      //      $this->attachLoad($queried_entities);
+      $entities += $queried_entities;
+    }
+
+    // Ensure that the returned array is ordered the same as the original
+    // $ids array if this was passed in and remove any invalid ids.
+    return $entities;
+  }
+}

+ 72 - 0
tripal_cv/entities/tripal_cv.organism.inc

@@ -0,0 +1,72 @@
+<?php
+/**
+ *
+ *
+ */
+class TrpOrganismController extends DrupalDefaultEntityController {
+
+  /**
+   *
+   * @param $ids
+   *   An array of entity IDs, or FALSE to load all entities.
+   * @param $conditions
+   *   DEPRECATED. An array of conditions. Keys are field names on the entity's
+   *   base table.Values will be compared for equality. All the comparisons will
+   *   be ANDed together. This parameter is deprecated; use an EntityFieldQuery
+   *   instead.
+   * @return multitype:An
+   */
+  public function load($ids = array(), $conditions = array()) {
+    $entities = array();
+    $queried_entities = array();
+
+    $values = array('organism_id' => $ids);
+    $options = array('return_array' => TRUE);
+    $organisms = chado_generate_var('organism', $values, $options);
+    $organisms = chado_expand_var($organisms, 'field', 'organism.comment');
+    foreach ($organisms as $result) {
+      $organism = new stdClass();
+      $organism->internal_id = $result->organism_id;
+
+      // Add the names by which this feature is known.
+      $names = array();
+      $names[] = $result->genus . ' ' . $result->species;
+      $names[] = $result->common_name;
+      $names[] = $result->abbreviation;
+      $organism->name = array_unique($names);
+
+      // Add in properties.
+
+      // Add in the expand array.
+      $expand = array();
+      $organism->expand = $expand;
+
+      // Add in the embeded elements.
+      $embedded = array();
+      $organism->embedded = $embedded;
+
+
+      // Add in site-specific properties.
+      $organism->genus = $result->genus;
+      $organism->species = $result->species;
+      $organism->description = $result->comment;
+
+      // Add in the phylogeny if the NCBI taxonomy is loaded.
+
+
+      $queried_entities[$organism->internal_id] = $organism;
+    }
+
+    // Pass all entities loaded from the database through $this->attachLoad(),
+    // which attaches fields (if supported by the entity type) and calls the
+    // entity type specific load callback, for example hook_node_load().
+    if (!empty($queried_entities)) {
+      //      $this->attachLoad($queried_entities);
+      $entities += $queried_entities;
+    }
+
+    // Ensure that the returned array is ordered the same as the original
+    // $ids array if this was passed in and remove any invalid ids.
+    return $entities;
+  }
+}

+ 65 - 0
tripal_cv/entities/tripal_cv.vocabulary.inc

@@ -0,0 +1,65 @@
+<?php
+/**
+ *
+ *
+ */
+class TrpVocabularyController extends DrupalDefaultEntityController {
+
+  /**
+   *
+   * @param $ids
+   *   An array of entity IDs, or FALSE to load all entities.
+   * @param $conditions
+   *   DEPRECATED. An array of conditions. Keys are field names on the entity's
+   *   base table.Values will be compared for equality. All the comparisons will
+   *   be ANDed together. This parameter is deprecated; use an EntityFieldQuery
+   *   instead.
+   * @return multitype:An
+   */
+  public function load($ids = array(), $conditions = array()) {
+    $entities = array();
+    $queried_entities = array();
+
+    // Get the list of records that match the loading criteria
+    $sql = "
+      SELECT CVDB.cv_id, CVDB.name, CVDB.namespace, DB.url, DB.urlprefix
+      FROM {cv_db} CVDB
+        INNER JOIN {db} DB on DB.db_id = CVDB.db_id
+        LEFT JOIN {cvprop} CVP on CVP.cv_id = CVDB.cv_id
+    ";
+    $where = '';
+    if ($ids) {
+      $where .= ' CVDB.cv_id IN (:ids)';
+    }
+    if ($where) {
+      $sql .= "WHERE $where";
+    }
+    $sql .= "ORDER BY CVDB.name ";
+    $sql .= "LIMIT 10 OFFSET 0 ";
+
+    $results = chado_query($sql, array(':ids' => $ids));
+    while ($result = $results->fetchObject()) {
+       $cv = new stdClass();
+       $cv->internal_id = $result->cv_id;
+       $cv->name = $result->name;
+       $cv->format = 'OBO v1.4';
+       $cv->namespace = $result->namespace;
+       $cv->url = $result->url;
+       $cv->term_url = $result->urlprefix ? $result->urlprefix . '{id}' : '';
+       $queried_entities[$cv->internal_id] = $cv;
+    }
+
+    // Pass all entities loaded from the database through $this->attachLoad(),
+    // which attaches fields (if supported by the entity type) and calls the
+    // entity type specific load callback, for example hook_node_load().
+    if (!empty($queried_entities)) {
+//      $this->attachLoad($queried_entities);
+      $entities += $queried_entities;
+    }
+
+    // Ensure that the returned array is ordered the same as the original
+    // $ids array if this was passed in and remove any invalid ids.
+    return $entities;
+  }
+
+}

+ 149 - 0
tripal_cv/entities/tripal_cv.vocabulary_term.inc

@@ -0,0 +1,149 @@
+<?php
+/**
+ *
+ *
+ */
+class TrpVocabularyTermController extends DrupalDefaultEntityController {
+
+  /**
+   *
+   * @param $ids
+   *   An array of entity IDs, or FALSE to load all entities.
+   * @param $conditions
+   *   DEPRECATED. An array of conditions. Keys are field names on the entity's
+   *   base table.Values will be compared for equality. All the comparisons will
+   *   be ANDed together. This parameter is deprecated; use an EntityFieldQuery
+   *   instead.
+   * @return multitype:An
+   */
+  public function load($ids = array(), $conditions = array()) {
+    $entities = array();
+    $queried_entities = array();
+
+    // Get the list of records that match the loading criteria
+    $sql = "
+      SELECT CVT.cvterm_id, CVT.name, CVT.definition,
+        DB.name as namespace, DBX.accession, CV.name as vocabulary,
+        CV.cv_id
+      FROM {cvterm} CVT
+        INNER JOIN {cv} CV      ON CV.cv_id      = CVT.cv_id
+        INNER JOIN {dbxref} DBX ON DBX.dbxref_id = CVT.dbxref_id
+        INNER JOIN {db} DB      ON DB.db_id      = DBX.db_id
+    ";
+    $where = '';
+    if ($ids) {
+      $where .= 'CVT.cvterm_id IN (:ids) ';
+    }
+    if ($where) {
+      $sql .= "WHERE $where ";
+    }
+    $sql .= "ORDER BY CVT.name ";
+    $sql .= "LIMIT 10 OFFSET 0 ";
+
+    $results = chado_query($sql, array(':ids' => $ids));
+    while ($result = $results->fetchObject()) {
+      $cvterm = new stdClass();
+      $cvterm->vocabulary_internal_id = $result->cv_id;
+      $cvterm->vocabulary = $result->vocabulary;
+      $cvterm->namespace = $result->namespace;
+      $cvterm->internal_id = $result->cvterm_id;
+      $cvterm->id = $result->namespace . ':' . $result->accession;
+      $cvterm->name = $result->name;
+      $cvterm->def = $result->definition;
+
+      // Find any alternate IDs or xrefs for this term.
+      $sql = "
+        SELECT DB.name as namespace, DBX.accession
+        FROM {cvterm_dbxref} CVTDBX
+          INNER JOIN {dbxref} DBX ON DBX.dbxref_id = CVTDBX.dbxref_id
+          INNER JOIN {db} DB      ON DB.db_id = DBX.db_id
+        WHERE CVTDBX.cvterm_id = :cvterm_id
+      ";
+      $dbxref_results = chado_query($sql, array(':cvterm_id' => $result->cvterm_id));
+      $xrefs = array();
+      $alt_ids = array();
+      while ($dbxref = $dbxref_results->fetchObject()) {
+        if ($dbxref->namespace = $result->namespace) {
+          $alt_ids[] = $dbxref->namespace . ':' . $dbxref->accession;
+        }
+        else {
+          $xrefs[] = $dbxref->namespace . ':' . $dbxref->accession;
+        }
+      }
+      if (count($xrefs) > 0) {
+        $cvterm->xref_id = $xrefs;
+      }
+      if (count($alt_ids) > 0) {
+        $cvterm->alt_id = $alt_ids;
+      }
+
+      // Find any comments for this term.
+      $sql = "
+        SELECT CVTP.value
+        FROM {cvtermprop} CVTP
+          INNER JOIN {cvterm} CVT on CVT.cvterm_id = CVTP.type_id
+        WHERE CVT.name = 'comment' and CVTP.cvterm_id = :cvterm_id
+      ";
+      $comment_results = chado_query($sql, array(':cvterm_id' => $result->cvterm_id));
+      $comment = $comment_results->fetchObject();
+      if ($comment) {
+        $cvterm->comment = $comment->value;
+      }
+
+      // Find any relationships of this term
+      $sql = "
+        SELECT CVT.name as rel, CVTO.name, DB.name as namespace, DBX.accession
+        FROM {cvterm_relationship} CVTR
+          INNER JOIN {cvterm} CVT  ON CVT.cvterm_id  = CVTR.type_id
+          INNER JOIN {cvterm} CVTO ON CVTO.cvterm_id = CVTR.object_id
+          INNER JOIN {dbxref} DBX  ON DBX.dbxref_id  = CVTO.dbxref_id
+          INNER JOIN {db}     DB   ON DB.db_id       = DBX.db_id
+        WHERE CVT.name = 'is_a' and CVTR.subject_id = :cvterm_id
+      ";
+      $rel_results = chado_query($sql, array(':cvterm_id' => $result->cvterm_id));
+      $rels = array();
+      while ($rel = $rel_results->fetchObject()) {
+        $rel_text = $rel->namespace . ':' . $rel->accession;
+        if ($rel->name) {
+          $rel_text .= ' ! ' . $rel->name;
+        }
+        $rels[$rel->rel][] = $rel_text;
+      }
+      foreach ($rels as $rel_type => $rel_list) {
+        $cvterm->$rel_type = $rel_list;
+      }
+
+      // Find any synonyms for this term
+      $sql = "
+        SELECT CVTS.synonym, CVTT.name as scope
+        FROM {cvtermsynonym} CVTS
+          INNER JOIN {cvterm} CVT  ON  CVT.cvterm_id = CVTS.cvterm_id
+          INNER JOIN {cvterm} CVTT ON CVTT.cvterm_id = CVTS.type_id
+        WHERE CVTS.cvterm_id = :cvterm_id
+      ";
+      $synonym_results = chado_query($sql, array(':cvterm_id' => $result->cvterm_id));
+      $synonyms = array();
+      while ($synonym = $synonym_results->fetchObject()) {
+        $synonyms[] = '"' . $synonym->synonym . '" ' . strtoupper($synonym->scope);
+      }
+      if (count($synonyms) > 0) {
+        $cvterm->synonyms = $synonyms;
+      }
+
+      $queried_entities[$cvterm->internal_id] = $cvterm;
+    }
+
+    // Pass all entities loaded from the database through $this->attachLoad(),
+    // which attaches fields (if supported by the entity type) and calls the
+    // entity type specific load callback, for example hook_node_load().
+    if (!empty($queried_entities)) {
+//      $this->attachLoad($queried_entities);
+      $entities += $queried_entities;
+    }
+
+    // Ensure that the returned array is ordered the same as the original
+    // $ids array if this was passed in and remove any invalid ids.
+    return $entities;
+  }
+
+}