cvterms.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @defgroup tripal_core_cv_api Core Module CV API
  4. * @{
  5. * For working with controlled vocabularies see the Controlled Vocabulary
  6. * API. However, there are cases where a CV does not exist. For example in
  7. * some cases a module may want to support adding of properties (such as to
  8. * for an analysis) to a record but an organized CV does not exist. The
  9. * progammer may create their own ontology and populate it, or they may use
  10. * the basic CV interface provided by Tripal. Tripal provides a CV named
  11. * 'Tripal' and modules may add terms to this CV to support the functionality
  12. * of their modules.
  13. * @}
  14. * @ingroup tripal_api
  15. */
  16. /**
  17. * Add a materialized view to the chado database to help speed data access.
  18. *
  19. * @param $name
  20. * The name of the term
  21. * @param $definition
  22. * The definition for the term
  23. * @param $cv_name
  24. * The name of the controlled vocabulary to which the term will be added.
  25. * The default CV is 'tripal'.
  26. * @param $db_name
  27. * CV records also have a corresponding database reference. This argument
  28. * specifies the name of the database to which this term belongs. The default
  29. * database name is 'tripal'.
  30. *
  31. * @ingroup tripal_core_cv_api
  32. */
  33. function tripal_add_cvterms ($name,$definition,$cv_name = 'tripal',$db_name='tripal'){
  34. $previous_db = tripal_db_set_active('chado'); // use chado database
  35. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE name = '$cv_name'"));
  36. if (!$cv->cv_id) {
  37. tripal_db_set_active($previous_db);
  38. tripal_add_cv('tripal', 'Terms used by Tripal for modules to manage data such as that
  39. stored in property tables like featureprop, analysisprop, etc');
  40. tripal_db_set_active('chado');
  41. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE name = '$cv_name'"));
  42. }
  43. $db = db_fetch_object(db_query("SELECT * FROM {db} WHERE name = '$db_name'"));
  44. if (!$db->db_id) {
  45. tripal_db_set_active($previous_db);
  46. tripal_add_db('tripal', 'Used as a database placeholder for tripal defined objects such as tripal cvterms', '', '');
  47. tripal_db_set_active('chado');
  48. $db = db_fetch_object(db_query("SELECT * FROM {db} WHERE name = '$db_name'"));
  49. }
  50. // check to see if the dbxref already exists if not then add it
  51. $sql = "SELECT * FROM {dbxref} WHERE db_id = $db->db_id and accession = '$name'";
  52. $dbxref = db_fetch_object(db_query($sql));
  53. if(!$dbxref){
  54. db_query("INSERT INTO {dbxref} (db_id,accession) VALUES ($db->db_id,'$name')");
  55. $dbxref = db_fetch_object(db_query($sql));
  56. }
  57. // now add the cvterm only if it doesn't already exist
  58. $sql = "SELECT * FROM {cvterm} ".
  59. "WHERE cv_id = $cv->cv_id and dbxref_id = $dbxref->dbxref_id and name = '$name'";
  60. $cvterm = db_fetch_object(db_query($sql));
  61. if(!$cvterm){
  62. $result = db_query("INSERT INTO {cvterm} (cv_id,name,definition,dbxref_id) ".
  63. "VALUES ($cv->cv_id,'$name','$definition',$dbxref->dbxref_id)");
  64. }
  65. tripal_db_set_active($previous_db); // now use drupal database
  66. if(!$result){
  67. // TODO -- ERROR HANDLING
  68. }
  69. }
  70. /**
  71. * Add a database to the Chado 'db' table.
  72. * NOTE: This function is deprecated and may not be used in future releases of Tripal
  73. *
  74. * @ingroup tripal_core_cv_api
  75. */
  76. function tripal_add_db($db_name,$description,$urlprefix,$url){
  77. $previous_db = tripal_db_set_active('chado'); // use chado database
  78. // use this SQL statement to get the db_id for the database name
  79. $id_sql = "SELECT db_id FROM {db} WHERE name = '%s'";
  80. $db = db_fetch_object(db_query($id_sql,$db_name));
  81. // if the database doesn't exist then let's add it.
  82. if(!$db->db_id){
  83. $sql = "
  84. INSERT INTO {db} (name,description,urlprefix,url) VALUES
  85. ('%s','%s','%s','%s');
  86. ";
  87. db_query($sql,$db_name,$description,$urlprefix,$url);
  88. # now get the id for this new db entry
  89. $db = db_fetch_object(db_query($id_sql,$db_name));
  90. }
  91. tripal_db_set_active($previous_db); // now use drupal database
  92. return $db->db_id;
  93. }
  94. /**
  95. * Get cvterm_id for a tripal term by passing its name
  96. * NOTE: This function is deprecated and may not be used in future releases of Tripal
  97. *
  98. * @ingroup tripal_core_cv_api
  99. */
  100. function tripal_get_cvterm_id ($cvterm){
  101. $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT
  102. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  103. WHERE CVT.name = '$cvterm'
  104. AND CV.name = 'tripal'";
  105. return db_result(chado_query($sql));
  106. }