tripal_organism.api.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @file
  4. * Provides an API to tripal organisms
  5. */
  6. /**
  7. * Purpose: Create an options array to be used in a form element
  8. * which provides a list of all chado organisms
  9. *
  10. * @return an array(organism_id => common_name)
  11. * for each organism in the chado organism table
  12. *
  13. * @ingroup tripal_organism_api
  14. */
  15. function tripal_organism_get_organism_options() {
  16. $previous_db = tripal_db_set_active('chado');
  17. $result = db_query(
  18. "SELECT organism_id, common_name FROM {organism}"
  19. );
  20. tripal_db_set_active($previous_db);
  21. $options = array();
  22. while ( $r = db_fetch_object($result) ) {
  23. $options[$r->organism_id] = $r->common_name;
  24. }
  25. return $options;
  26. }
  27. /**
  28. * Purpose: Return a given organism object using the nid
  29. *
  30. * @return organism object created by node load
  31. *
  32. * @ingroup tripal_organism_api
  33. */
  34. function tripal_organism_get_organism_by_nid($nid) {
  35. return node_load($nid);
  36. }
  37. /**
  38. * Purpose: Return a given organism object using the organism id
  39. *
  40. * @return organism object created by node load
  41. *
  42. * @ingroup tripal_organism_api
  43. */
  44. function tripal_organism_get_organism_by_organism_id($organism_id) {
  45. $sql = "SELECT nid FROM {chado_organism} WHERE organism_id=%d";
  46. $r = db_fetch_object(db_query($sql, $organism_id));
  47. if (!empty($r->nid)) {
  48. return node_load($r->nid);
  49. }
  50. else {
  51. drupal_set_message(t("Function: tripal_organism_get_organism_by_organism_id() -no organism with that organism id sync'd with drupal"), 'error');
  52. }
  53. return 0;
  54. }
  55. /**
  56. * @section Chado Table Descriptions
  57. * There should be a default table description for all chado tables included
  58. * in core.
  59. */
  60. /**
  61. * Implements hook_chado_organism_schema()
  62. *
  63. * Purpose: To add descriptions and foreign keys to default table description
  64. * Note: This array will be merged with the array from all other implementations
  65. *
  66. * @return
  67. * Array describing the organism table
  68. *
  69. * @ingroup tripal_schema_api
  70. */
  71. function tripal_organism_chado_organism_schema() {
  72. $description = array();
  73. $referring_tables = array(
  74. 'biomaterial',
  75. 'feature',
  76. 'library',
  77. 'organism_dbxref',
  78. 'organismprop',
  79. 'phylonode_organism',
  80. 'stock',
  81. 'wwwuser_organism'
  82. );
  83. $description['referring_tables'] = $referring_tables;
  84. return $description;
  85. }
  86. /**
  87. * Implements hook_chado_organismprop_schema()
  88. *
  89. * Purpose: To add descriptions and foreign keys to default table description
  90. * Note: This array will be merged with the array from all other implementations
  91. *
  92. * @return
  93. * Array describing the organismprop table
  94. *
  95. * @ingroup tripal_schema_api
  96. */
  97. function tripal_organism_chado_organismprop_schema() {
  98. $description = array();
  99. $description['foreign keys']['cvterm'] = array(
  100. 'table' => 'cvterm',
  101. 'columns' => array(
  102. 'type_id' => 'cvterm_id',
  103. ),
  104. );
  105. $description['foreign keys']['organism'] = array(
  106. 'table' => 'organism',
  107. 'columns' => array(
  108. 'organism_id' => 'organism_id',
  109. ),
  110. );
  111. return $description;
  112. }
  113. /**
  114. * Returns a list of organisms that are currently synced with Drupal
  115. * @ingroup tripal_organism_api
  116. */
  117. function tripal_organism_get_synced() {
  118. // use this SQL for getting synced organisms
  119. $dsql = "SELECT * FROM {chado_organism}";
  120. $orgs = db_query($dsql);
  121. // use this SQL statement for getting the organisms
  122. $csql = "SELECT * FROM {Organism} ".
  123. "WHERE organism_id = %d";
  124. $org_list = array();
  125. // iterate through the organisms and build an array of those that are synced
  126. while ($org = db_fetch_object($orgs)) {
  127. $previous_db = tripal_db_set_active('chado'); // use chado database
  128. $info = db_fetch_object(db_query($csql, $org->organism_id));
  129. tripal_db_set_active($previous_db); // now use drupal database
  130. $org_list[] = $info;
  131. }
  132. return $org_list;
  133. }