cvterms.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. //
  3. // Copyright 2009 Clemson University
  4. //
  5. /************************************************************************
  6. *
  7. */
  8. function tripal_add_cvterms ($name,$definition,$cv_name = 'tripal',$db_name='tripal'){
  9. $previous_db = tripal_db_set_active('chado'); // use chado database
  10. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE name = '$cv_name'"));
  11. if (!$cv->cv_id) {
  12. tripal_db_set_active($previous_db);
  13. tripal_add_cv('tripal', 'Terms used by Tripal for modules to manage data such as that
  14. stored in property tables like featureprop, analysisprop, etc');
  15. tripal_db_set_active('chado');
  16. $cv = db_fetch_object(db_query("SELECT * FROM {cv} WHERE name = '$cv_name'"));
  17. }
  18. $db = db_fetch_object(db_query("SELECT * FROM {db} WHERE name = '$db_name'"));
  19. if (!$db->db_id) {
  20. tripal_db_set_active($previous_db);
  21. tripal_add_db('tripal', 'Used as a database placeholder for tripal defined objects such as tripal cvterms', '', '');
  22. tripal_db_set_active('chado');
  23. $db = db_fetch_object(db_query("SELECT * FROM {db} WHERE name = '$db_name'"));
  24. }
  25. // check to see if the dbxref already exists if not then add it
  26. $sql = "SELECT * FROM {dbxref} WHERE db_id = $db->db_id and accession = '$name'";
  27. $dbxref = db_fetch_object(db_query($sql));
  28. if(!$dbxref){
  29. db_query("INSERT INTO {dbxref} (db_id,accession) VALUES ($db->db_id,'$name')");
  30. $dbxref = db_fetch_object(db_query($sql));
  31. }
  32. // now add the cvterm only if it doesn't already exist
  33. $sql = "SELECT * FROM {cvterm} ".
  34. "WHERE cv_id = $cv->cv_id and dbxref_id = $dbxref->dbxref_id and name = '$name'";
  35. $cvterm = db_fetch_object(db_query($sql));
  36. if(!$cvterm){
  37. $result = db_query("INSERT INTO {cvterm} (cv_id,name,definition,dbxref_id) ".
  38. "VALUES ($cv->cv_id,'$name','$definition',$dbxref->dbxref_id)");
  39. }
  40. tripal_db_set_active($previous_db); // now use drupal database
  41. if(!$result){
  42. // TODO -- ERROR HANDLING
  43. }
  44. }
  45. /************************************************************************
  46. *
  47. */
  48. function tripal_add_db($db_name,$description,$urlprefix,$url){
  49. $previous_db = tripal_db_set_active('chado'); // use chado database
  50. // use this SQL statement to get the db_id for the database name
  51. $id_sql = "SELECT db_id FROM {db} WHERE name = '%s'";
  52. $db = db_fetch_object(db_query($id_sql,$db_name));
  53. // if the database doesn't exist then let's add it.
  54. if(!$db->db_id){
  55. $sql = "
  56. INSERT INTO {db} (name,description,urlprefix,url) VALUES
  57. ('%s','%s','%s','%s');
  58. ";
  59. db_query($sql,$db_name,$description,$urlprefix,$url);
  60. # now get the id for this new db entry
  61. $db = db_fetch_object(db_query($id_sql,$db_name));
  62. }
  63. tripal_db_set_active($previous_db); // now use drupal database
  64. return $db->db_id;
  65. }
  66. /************************************************************************
  67. *
  68. */
  69. function tripal_delete_db($db_name){
  70. $previous_db = tripal_db_set_active('chado'); // use chado database
  71. $sql = "DELETE FROM {db} WHERE name ='%s'";
  72. db_query($sql,$db_name);
  73. tripal_db_set_active($previous_db); // now use drupal database
  74. }
  75. /************************************************************************
  76. *
  77. */
  78. function tripal_add_cv($cv_name,$definition){
  79. $previous_db = tripal_db_set_active('chado'); // use chado database
  80. // use this SQL statement to get the db_id for the database name
  81. $id_sql = "SELECT cv_id FROM {cv} WHERE name = '%s'";
  82. $cv = db_fetch_object(db_query($sql,$cv_name));
  83. // if the database doesn't exist then let's add it.
  84. if(!$cv){
  85. $sql = "
  86. INSERT INTO {cv} (name,definition) VALUES
  87. ('%s','%s');
  88. ";
  89. db_query($sql,$cv_name,$definition);
  90. # now get the id for this new db entry
  91. $cv = db_fetch_object(db_query($sql,$cv_name));
  92. }
  93. tripal_db_set_active($previous_db); // now use drupal database
  94. return $cv->cv_id;
  95. }