cvterms.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * DEPRECATED: This function should no longer be used. But rather the
  18. * tripal_cv_add_cvterm function should be used.
  19. *
  20. * This function adds a term to the cvterm table of Chado.
  21. *
  22. * @param $name
  23. * The name of the term
  24. * @param $definition
  25. * The definition for the term
  26. * @param $cv_name
  27. * The name of the controlled vocabulary to which the term will be added.
  28. * The default CV is 'tripal'.
  29. * @param $db_name
  30. * CV records also have a corresponding database reference. This argument
  31. * specifies the name of the database to which this term belongs. The default
  32. * database name is 'tripal'.
  33. *
  34. * @ingroup tripal_core_cv_api
  35. */
  36. function tripal_add_cvterms ($name,$definition,$cv_name = 'tripal',$db_name='tripal'){
  37. $previous_db = tripal_db_set_active('chado'); // use chado database
  38. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE name = '$cv_name'"));
  39. if (!$cv->cv_id) {
  40. tripal_db_set_active($previous_db);
  41. tripal_add_cv('tripal', 'Terms used by Tripal for modules to manage data such as that
  42. stored in property tables like featureprop, analysisprop, etc');
  43. tripal_db_set_active('chado');
  44. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE name = '$cv_name'"));
  45. }
  46. $db = db_fetch_object(db_query("SELECT * FROM {db} WHERE name = '$db_name'"));
  47. if (!$db->db_id) {
  48. tripal_db_set_active($previous_db);
  49. tripal_add_db('tripal', 'Used as a database placeholder for tripal defined objects such as tripal cvterms', '', '');
  50. tripal_db_set_active('chado');
  51. $db = db_fetch_object(db_query("SELECT * FROM {db} WHERE name = '$db_name'"));
  52. }
  53. // check to see if the dbxref already exists if not then add it
  54. $sql = "SELECT * FROM {dbxref} WHERE db_id = $db->db_id and accession = '$name'";
  55. $dbxref = db_fetch_object(db_query($sql));
  56. if(!$dbxref){
  57. db_query("INSERT INTO {dbxref} (db_id,accession) VALUES ($db->db_id,'$name')");
  58. $dbxref = db_fetch_object(db_query($sql));
  59. }
  60. // now add the cvterm only if it doesn't already exist
  61. $sql = "SELECT * FROM {cvterm} ".
  62. "WHERE cv_id = $cv->cv_id and dbxref_id = $dbxref->dbxref_id and name = '$name'";
  63. $cvterm = db_fetch_object(db_query($sql));
  64. if(!$cvterm){
  65. $result = db_query("INSERT INTO {cvterm} (cv_id,name,definition,dbxref_id) ".
  66. "VALUES ($cv->cv_id,'$name','$definition',$dbxref->dbxref_id)");
  67. }
  68. tripal_db_set_active($previous_db); // now use drupal database
  69. if(!$result){
  70. // TODO -- ERROR HANDLING
  71. }
  72. }
  73. /**
  74. * Add a database to the Chado 'db' table.
  75. * NOTE: This function is deprecated and may not be used in future releases of Tripal
  76. *
  77. * @ingroup tripal_core_cv_api
  78. */
  79. function tripal_add_db($db_name,$description,$urlprefix,$url){
  80. $previous_db = tripal_db_set_active('chado'); // use chado database
  81. // use this SQL statement to get the db_id for the database name
  82. $id_sql = "SELECT db_id FROM {db} WHERE name = '%s'";
  83. $db = db_fetch_object(db_query($id_sql,$db_name));
  84. // if the database doesn't exist then let's add it.
  85. if(!$db->db_id){
  86. $sql = "
  87. INSERT INTO {db} (name,description,urlprefix,url) VALUES
  88. ('%s','%s','%s','%s');
  89. ";
  90. db_query($sql,$db_name,$description,$urlprefix,$url);
  91. # now get the id for this new db entry
  92. $db = db_fetch_object(db_query($id_sql,$db_name));
  93. }
  94. tripal_db_set_active($previous_db); // now use drupal database
  95. return $db->db_id;
  96. }
  97. /**
  98. * Get cvterm_id for a tripal term by passing its name
  99. * NOTE: This function is deprecated and may not be used in future releases of Tripal
  100. *
  101. * @ingroup tripal_core_cv_api
  102. */
  103. function tripal_get_cvterm_id ($cvterm){
  104. $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT
  105. INNER JOIN cv ON cv.cv_id = CVT.cv_id
  106. WHERE CVT.name = '$cvterm'
  107. AND CV.name = 'tripal'";
  108. return db_result(chado_query($sql));
  109. }