tripal_api.chado_query.manage_properties.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * @file
  4. * Functions to insert/update delete records in any prop table
  5. */
  6. /**
  7. * Retrieve a property for a given base table record
  8. *
  9. * @param $basetable
  10. * The base table for which the property should be retrieved. Thus to retrieve a property
  11. * for a feature the basetable=feature and property is retrieved from featureprop
  12. * @param $record_id
  13. * The foriegn key field of the base table. This should be in integer.
  14. * @param $property
  15. * The cvterm name describing the type of properties to be retrieved
  16. * @param $cv_name
  17. * The name of the cv that the above cvterm is part of
  18. *
  19. * @return
  20. * An array in the same format as that generated by the function
  21. * tripal_api_chado_generate_chado_var(). If only one record is returned it
  22. * is a single object. If more than one record is returned then it is an array
  23. * of objects
  24. *
  25. * @ingroup tripal_properties_api
  26. */
  27. function tripal_api_chado_get_property($basetable, $record_id, $property, $cv_name) {
  28. // get the foreign key for this property table
  29. $table_desc = tripal_api_chado_get_chado_table_schema($basetable . 'prop');
  30. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  31. // construct the array of values to be selected
  32. $values = array(
  33. $fkcol => $record_id,
  34. 'type_id' => array(
  35. 'cv_id' => array(
  36. 'name' => $cv_name,
  37. ),
  38. 'name' => $property,
  39. 'is_obsolete' => 0
  40. ),
  41. );
  42. $results = tripal_api_chado_generate_chado_var($basetable . 'prop', $values);
  43. if ($results) {
  44. $results = tripal_api_chado_expand_chado_vars($results, 'field', $basetable . 'prop.value');
  45. }
  46. return $results;
  47. }
  48. /**
  49. * Insert a property for a given base table. By default if the property already
  50. * exists a new property is added with the next available rank. If
  51. * $update_if_present argument is specified then the record will be updated if it
  52. * exists rather than adding a new property.
  53. *
  54. * @param $basetable
  55. * The base table for which the property should be inserted. Thus to insert a property
  56. * for a feature the basetable=feature and property is inserted into featureprop
  57. * @param $record_id
  58. * The foriegn key value of the base table. This should be in integer.
  59. * @param $property
  60. * The cvterm name describing the type of properties to be inserted
  61. * @param $cv_name
  62. * The name of the cv that the above cvterm is part of
  63. * @param $value
  64. * The value of the property to be inserted (can be empty)
  65. * @param $update_if_present
  66. * A boolean indicating whether an existing record should be updated. If the
  67. * property already exists and this value is not specified or is zero then
  68. * a new property will be added with the next largest rank.
  69. *
  70. * @return
  71. * Return True on Insert/Update and False otherwise
  72. *
  73. * @ingroup tripal_properties_api
  74. */
  75. function tripal_api_chado_insert_property($basetable, $record_id, $property,
  76. $cv_name, $value, $update_if_present = 0) {
  77. // first see if the property already exists, if the user want's to update
  78. // then we can do that, but otherwise we want to increment the rank and
  79. // insert
  80. $props = tripal_api_chado_get_property($basetable, $record_id, $property, $cv_name);
  81. if (!is_array($props) and $props) {
  82. $props = array($props);
  83. }
  84. $rank = 0;
  85. if (count($props) > 0) {
  86. if ($update_if_present) {
  87. return tripal_api_chado_update_property($basetable, $record_id, $property, $cv_name, $value);
  88. }
  89. else {
  90. // iterate through the properties returned and check to see if the
  91. // property with this value already exists if not, get the largest rank
  92. // and insert the same property but with this new value
  93. foreach ($props as $p) {
  94. if ($p->rank > $rank) {
  95. $rank = $p->rank;
  96. }
  97. if (strcmp($p->value, $value) == 0) {
  98. return TRUE;
  99. }
  100. }
  101. // now add 1 to the rank
  102. $rank++;
  103. }
  104. }
  105. // make sure the cvterm exists. Otherwise we'll get an error with
  106. // prepared statements not matching
  107. $values = array(
  108. 'cv_id' => array(
  109. 'name' => $cv_name,
  110. ),
  111. 'name' => $property,
  112. );
  113. $options = array();
  114. $term = tripal_api_chado_chado_select('cvterm', array('cvterm_id'), $values, $options);
  115. if (!$term or count($term) == 0) {
  116. watchdog('tripal_api_chado', "Cannot find property '%prop_name' in vocabulary '%cvname'.",
  117. array('%prop_name' => $property, '%cvname' => $cv_name), WATCHDOG_ERROR);
  118. return FALSE;
  119. }
  120. // get the foreign key for this property table
  121. $table_desc = tripal_api_chado_get_chado_table_schema($basetable . 'prop');
  122. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  123. // construct the array of values to be inserted
  124. $values = array(
  125. $fkcol => $record_id,
  126. 'type_id' => array(
  127. 'cv_id' => array(
  128. 'name' => $cv_name,
  129. ),
  130. 'name' => $property,
  131. ),
  132. 'value' => $value,
  133. 'rank' => $rank,
  134. );
  135. $options = array();
  136. $result = tripal_api_chado_chado_insert($basetable . 'prop', $values, $options);
  137. return $result;
  138. }
  139. /**
  140. * Update a property for a given base table record and property name. This
  141. * function should be used only if one record of the property will be present.
  142. * If the property name can have multiple entries (with increasing rank) then
  143. * use the function named tripal_api_chado_update_property_by_id
  144. *
  145. * @param $basetable
  146. * The base table for which the property should be updated. The property table
  147. * is constructed using a combination of the base table name and the suffix
  148. * 'prop' (e.g. basetable = feature then property tabie is featureprop).
  149. * @param $record_id
  150. * The foreign key of the basetable to update a property for. This should be in integer.
  151. * For example, if the basetable is 'feature' then the $record_id should be the feature_id
  152. * @param $property
  153. * The cvterm name of property to be updated
  154. * @param $cv_name
  155. * The name of the cv that the above cvterm is part of
  156. * @param $value
  157. * The value of the property to be inserted (can be empty)
  158. * @param $insert_if_missing
  159. * A boolean indicating whether a record should be inserted if one doesn't exist to update
  160. *
  161. * Note: The property to be updated is select via the unique combination of $record_id and
  162. * $property and then it is updated with the supplied value
  163. *
  164. * @return
  165. * Return True on Update/Insert and False otherwise
  166. *
  167. * @ingroup tripal_properties_api
  168. */
  169. function tripal_api_chado_update_property($basetable, $record_id, $property,
  170. $cv_name, $value, $insert_if_missing = 0) {
  171. // first see if the property is missing (we can't update a missing property
  172. $prop = tripal_api_chado_get_property($basetable, $record_id, $property, $cv_name);
  173. if (count($prop)==0) {
  174. if ($insert_if_missing) {
  175. return tripal_api_chado_insert_property($basetable, $record_id, $property, $cv_name, $value);
  176. }
  177. else {
  178. return FALSE;
  179. }
  180. }
  181. // get the foreign key for this property table
  182. $table_desc = tripal_api_chado_get_chado_table_schema($basetable . 'prop');
  183. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  184. // construct the array that will match the exact record to update
  185. $match = array(
  186. $fkcol => $record_id,
  187. 'type_id' => array(
  188. 'cv_id' => array(
  189. 'name' => $cv_name,
  190. ),
  191. 'name' => $property,
  192. ),
  193. );
  194. // construct the array of values to be updated
  195. $values = array(
  196. 'value' => $value,
  197. );
  198. return tripal_api_chado_chado_update($basetable . 'prop', $match, $values);
  199. }
  200. /**
  201. * Update a property for a given base table record. This function should be
  202. * used if multiple records of the same property will be present. Also, use this
  203. * function to change the property name of an existing property.
  204. *
  205. * @param $basetable
  206. * The base table for which the property should be updated. The property table
  207. * is constructed using a combination of the base table name and the suffix
  208. * 'prop' (e.g. basetable = feature then property tabie is featureprop).
  209. * @param $record_id
  210. * The primary key of the base table. This should be in integer.
  211. * For example, if the basetable is 'feature' then the $record_id should be the featureprop_id
  212. * @param $property
  213. * The cvterm name of property to be updated
  214. * @param $cv_name
  215. * The name of the cv that the above cvterm is part of
  216. * @param $value
  217. * The value of the property to be inserted (can be empty)
  218. *
  219. * @return
  220. * Return True on Update/Insert and False otherwise
  221. *
  222. * @ingroup tripal_properties_api
  223. */
  224. function tripal_api_chado_update_property_by_id($basetable, $record_id, $property,
  225. $cv_name, $value) {
  226. // get the primary key for this property table
  227. $table_desc = tripal_api_chado_get_chado_table_schema($basetable . 'prop');
  228. $pkcol = $table_desc['primary key'][0];
  229. // construct the array that will match the exact record to update
  230. $match = array(
  231. $pkcol => $record_id,
  232. );
  233. // construct the array of values to be updated
  234. $values = array(
  235. 'type_id' => array(
  236. 'cv_id' => array(
  237. 'name' => $cv_name,
  238. ),
  239. 'name' => $property,
  240. ),
  241. 'value' => $value,
  242. );
  243. return tripal_api_chado_chado_update($basetable . 'prop', $match, $values);
  244. }
  245. /**
  246. * Deletes a property for a given base table record using the property name
  247. *
  248. * @param $basetable
  249. * The base table for which the property should be deleted. Thus to deleted a property
  250. * for a feature the basetable=feature and property is deleted from featureprop
  251. * @param $record_id
  252. * The primary key of the basetable to delete a property for. This should be in integer.
  253. * @param $property
  254. * The cvterm name describing the type of property to be deleted
  255. * @param $cv_name
  256. * The name of the cv that the above cvterm is part of
  257. *
  258. * Note: The property to be deleted is select via the unique combination of $record_id and $property
  259. *
  260. * @return
  261. * Return True on Delete and False otherwise
  262. *
  263. * @ingroup tripal_properties_api
  264. */
  265. function tripal_api_chado_delete_property($basetable, $record_id, $property, $cv_name) {
  266. // get the foreign key for this property table
  267. $table_desc = tripal_api_chado_get_chado_table_schema($basetable . 'prop');
  268. $fkcol = key($table_desc['foreign keys'][$basetable]['columns']);
  269. // construct the array that will match the exact record to update
  270. $match = array(
  271. $fkcol => $record_id,
  272. 'type_id' => array(
  273. 'cv_id' => array(
  274. 'name' => $cv_name,
  275. ),
  276. 'name' => $property,
  277. ),
  278. );
  279. return tripal_api_chado_chado_delete($basetable . 'prop', $match);
  280. }
  281. /**
  282. * Deletes a property using the property ID
  283. *
  284. * @param $basetable
  285. * The base table for which the property should be deleted. Thus to deleted a property
  286. * for a feature the basetable=feature and property is deleted from featureprop
  287. * @param $record_id
  288. * The primary key of the basetable to delete a property for. This should be in integer.
  289. *
  290. * @return
  291. * Return True on Delete and False otherwise
  292. *
  293. * @ingroup tripal_properties_api
  294. */
  295. function tripal_api_chado_delete_property_by_id($basetable, $record_id) {
  296. // get the foreign key for this property table
  297. $table_desc = tripal_api_chado_get_chado_table_schema($basetable . 'prop');
  298. $pkcol = $table_desc['primary key'][0];
  299. // construct the array that will match the exact record to update
  300. $match = array(
  301. $pkcol => $record_id,
  302. );
  303. return tripal_api_chado_chado_delete($basetable . 'prop', $match);
  304. }