tripal_cv.vocabulary_term.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. *
  4. *
  5. */
  6. class TrpVocabularyTermController extends DrupalDefaultEntityController {
  7. /**
  8. *
  9. * @param $ids
  10. * An array of entity IDs, or FALSE to load all entities.
  11. * @param $conditions
  12. * DEPRECATED. An array of conditions. Keys are field names on the entity's
  13. * base table.Values will be compared for equality. All the comparisons will
  14. * be ANDed together. This parameter is deprecated; use an EntityFieldQuery
  15. * instead.
  16. * @return multitype:An
  17. */
  18. public function load($ids = array(), $conditions = array()) {
  19. $entities = array();
  20. $queried_entities = array();
  21. // Get the list of records that match the loading criteria
  22. $sql = "
  23. SELECT CVT.cvterm_id, CVT.name, CVT.definition,
  24. DB.name as namespace, DBX.accession, CV.name as vocabulary,
  25. CV.cv_id
  26. FROM {cvterm} CVT
  27. INNER JOIN {cv} CV ON CV.cv_id = CVT.cv_id
  28. INNER JOIN {dbxref} DBX ON DBX.dbxref_id = CVT.dbxref_id
  29. INNER JOIN {db} DB ON DB.db_id = DBX.db_id
  30. ";
  31. $where = '';
  32. if ($ids) {
  33. $where .= 'CVT.cvterm_id IN (:ids) ';
  34. }
  35. if ($where) {
  36. $sql .= "WHERE $where ";
  37. }
  38. $sql .= "ORDER BY CVT.name ";
  39. $sql .= "LIMIT 10 OFFSET 0 ";
  40. $results = chado_query($sql, array(':ids' => $ids));
  41. while ($result = $results->fetchObject()) {
  42. $cvterm = new stdClass();
  43. $cvterm->vocabulary_internal_id = $result->cv_id;
  44. $cvterm->vocabulary = $result->vocabulary;
  45. $cvterm->namespace = $result->namespace;
  46. $cvterm->internal_id = $result->cvterm_id;
  47. $cvterm->id = $result->namespace . ':' . $result->accession;
  48. $cvterm->name = $result->name;
  49. $cvterm->def = $result->definition;
  50. // Find any alternate IDs or xrefs for this term.
  51. $sql = "
  52. SELECT DB.name as namespace, DBX.accession
  53. FROM {cvterm_dbxref} CVTDBX
  54. INNER JOIN {dbxref} DBX ON DBX.dbxref_id = CVTDBX.dbxref_id
  55. INNER JOIN {db} DB ON DB.db_id = DBX.db_id
  56. WHERE CVTDBX.cvterm_id = :cvterm_id
  57. ";
  58. $dbxref_results = chado_query($sql, array(':cvterm_id' => $result->cvterm_id));
  59. $xrefs = array();
  60. $alt_ids = array();
  61. while ($dbxref = $dbxref_results->fetchObject()) {
  62. if ($dbxref->namespace = $result->namespace) {
  63. $alt_ids[] = $dbxref->namespace . ':' . $dbxref->accession;
  64. }
  65. else {
  66. $xrefs[] = $dbxref->namespace . ':' . $dbxref->accession;
  67. }
  68. }
  69. if (count($xrefs) > 0) {
  70. $cvterm->xref_id = $xrefs;
  71. }
  72. if (count($alt_ids) > 0) {
  73. $cvterm->alt_id = $alt_ids;
  74. }
  75. // Find any comments for this term.
  76. $sql = "
  77. SELECT CVTP.value
  78. FROM {cvtermprop} CVTP
  79. INNER JOIN {cvterm} CVT on CVT.cvterm_id = CVTP.type_id
  80. WHERE CVT.name = 'comment' and CVTP.cvterm_id = :cvterm_id
  81. ";
  82. $comment_results = chado_query($sql, array(':cvterm_id' => $result->cvterm_id));
  83. $comment = $comment_results->fetchObject();
  84. if ($comment) {
  85. $cvterm->comment = $comment->value;
  86. }
  87. // Find any relationships of this term
  88. $sql = "
  89. SELECT CVT.name as rel, CVTO.name, DB.name as namespace, DBX.accession
  90. FROM {cvterm_relationship} CVTR
  91. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = CVTR.type_id
  92. INNER JOIN {cvterm} CVTO ON CVTO.cvterm_id = CVTR.object_id
  93. INNER JOIN {dbxref} DBX ON DBX.dbxref_id = CVTO.dbxref_id
  94. INNER JOIN {db} DB ON DB.db_id = DBX.db_id
  95. WHERE CVT.name = 'is_a' and CVTR.subject_id = :cvterm_id
  96. ";
  97. $rel_results = chado_query($sql, array(':cvterm_id' => $result->cvterm_id));
  98. $rels = array();
  99. while ($rel = $rel_results->fetchObject()) {
  100. $rel_text = $rel->namespace . ':' . $rel->accession;
  101. if ($rel->name) {
  102. $rel_text .= ' ! ' . $rel->name;
  103. }
  104. $rels[$rel->rel][] = $rel_text;
  105. }
  106. foreach ($rels as $rel_type => $rel_list) {
  107. $cvterm->$rel_type = $rel_list;
  108. }
  109. // Find any synonyms for this term
  110. $sql = "
  111. SELECT CVTS.synonym, CVTT.name as scope
  112. FROM {cvtermsynonym} CVTS
  113. INNER JOIN {cvterm} CVT ON CVT.cvterm_id = CVTS.cvterm_id
  114. INNER JOIN {cvterm} CVTT ON CVTT.cvterm_id = CVTS.type_id
  115. WHERE CVTS.cvterm_id = :cvterm_id
  116. ";
  117. $synonym_results = chado_query($sql, array(':cvterm_id' => $result->cvterm_id));
  118. $synonyms = array();
  119. while ($synonym = $synonym_results->fetchObject()) {
  120. $synonyms[] = '"' . $synonym->synonym . '" ' . strtoupper($synonym->scope);
  121. }
  122. if (count($synonyms) > 0) {
  123. $cvterm->synonyms = $synonyms;
  124. }
  125. $queried_entities[$cvterm->internal_id] = $cvterm;
  126. }
  127. // Pass all entities loaded from the database through $this->attachLoad(),
  128. // which attaches fields (if supported by the entity type) and calls the
  129. // entity type specific load callback, for example hook_node_load().
  130. if (!empty($queried_entities)) {
  131. // $this->attachLoad($queried_entities);
  132. $entities += $queried_entities;
  133. }
  134. // Ensure that the returned array is ordered the same as the original
  135. // $ids array if this was passed in and remove any invalid ids.
  136. return $entities;
  137. }
  138. }