tripal_chado.property.api.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage data withing the Chado database.
  5. */
  6. /**
  7. * @defgroup tripal_chado_prop_api Chado Properties
  8. *
  9. * @ingroup tripal_api
  10. * The Chado Properties API provides a set of functions for interacting
  11. * with the any chado prop table.
  12. *
  13. */
  14. /**
  15. * Retrieve a property for a given base table record.
  16. *
  17. * @param $record
  18. * An array used to identify the record to which the property is associated.
  19. * The following keys must be used:
  20. * -table: The base table for which the property should be updated.
  21. * Thus to update a property for a feature the base_table=feature.
  22. * -id: The primary key value of the base table. The property will be
  23. * associated with the record that matches this id.
  24. * -prop_id: The primary key in the [table]prop table. If this value
  25. * is supplied then the 'table' and 'id' keys are not needed.
  26. * @param $property
  27. * An associative array used to specify the property to be selected. It can
  28. * contain the following keys. The keys must be specified to uniquely identify
  29. * the term to be applied. If the options identify more than one CV term
  30. * then an error will occur.
  31. * -type_name: The cvterm name to be selected.
  32. * -type_id: The cvterm_id of the term to be selected.
  33. * -cv_id: The cv_id of the CV that contains the term.
  34. * -cv_name: The name of the CV that contains the term.
  35. * -value: The specific value for the property.
  36. * -rank: The specific rank for the property.
  37. * @return
  38. * An array in the same format as that generated by the function
  39. * chado_generate_var(). If only one record is returned it
  40. * is a single object. If more than one record is returned then it is an array
  41. * of objects
  42. *
  43. * @ingroup tripal_chado_prop_api
  44. */
  45. function chado_get_property($record, $property) {
  46. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  47. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  48. $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
  49. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  50. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  51. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  52. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  53. $value = array_key_exists('value', $property) ? $property['value'] : '';
  54. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  55. // Build the values array for checking if the CVterm exists and for
  56. // retrieving the term as a property.
  57. $type = array();
  58. if ($cv_id) {
  59. $type['cv_id'] = $cv_id;
  60. }
  61. if ($cv_name) {
  62. $type['cv_id'] = array(
  63. 'name' => $cv_name,
  64. );
  65. }
  66. if ($type_name) {
  67. $type['name'] = $type_name;
  68. }
  69. if ($type_id) {
  70. $type['cvterm_id'] = $type_id;
  71. }
  72. // Make sure the CV term exists;
  73. $options = array();
  74. $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
  75. if (!$term or count($term) == 0) {
  76. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_get_property: " .
  77. "Cannot find the term described by: %property.",
  78. array('%property' => print_r($property, TRUE)));
  79. return FALSE;
  80. }
  81. if (count($term) > 1) {
  82. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_get_property: " .
  83. "Multiple terms found. Cannot add the property. Property was described " .
  84. "by: %property.",
  85. array('%property' => print_r($property, TRUE))); return FALSE;
  86. }
  87. // get the foreign key for this property table
  88. $table_desc = chado_get_schema($base_table . 'prop');
  89. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  90. // construct the array of values to be selected
  91. $values = array(
  92. $fkcol => $base_id,
  93. 'type_id' => $type,
  94. );
  95. // if we have the unique property_id make sure to add that to the values
  96. if ($prop_id) {
  97. $property_pkey = $table_desc['primary key'][0];
  98. $values[$property_pkey] = $prop_id;
  99. }
  100. $results = chado_generate_var($base_table . 'prop', $values);
  101. if ($results) {
  102. $results = chado_expand_var($results, 'field', $base_table . 'prop.value');
  103. }
  104. return $results;
  105. }
  106. /**
  107. * Insert a property for a given base table.
  108. *
  109. * By default if the property already exists a new property is added with the
  110. * next available rank. If the option 'update_if_present' is specified then
  111. * the record will be updated if it exists rather than adding a new property.
  112. *
  113. * @param $record
  114. * An associative array used to identify the record to which the property
  115. * should be assigned. The following keys must be used:
  116. * -table: The base table for which the property should be inserted.
  117. * Thus to insert a property for a feature the base_table=feature and
  118. * property is inserted into featureprop
  119. * -id: The primary key value of the base table. The property will be
  120. * associated with the record that matches this id.
  121. * @param $property
  122. * An associative array used to specify the property to be added. It can
  123. * contain the following keys. The keys must be specified to uniquely identify
  124. * the term to be applied. If the options identify more than one CV term
  125. * then an error will occur.
  126. * -type_name: The cvterm name to be selected.
  127. * -type_id: The cvterm_id of the term to be selected.
  128. * -cv_id: The cv_id of the CV that contains the term.
  129. * -cv_name: The name of the CV that contains the term.
  130. * -value: The specific value for the property.
  131. * -rank: The specific rank for the property.
  132. * @param $options
  133. * An associative array containing the following keys:
  134. * -update_if_present: A boolean indicating whether an existing record
  135. * should be updated. If the property already exists and this value is
  136. * not specified or is zero then a new property will be added with the
  137. * next largest rank.
  138. * -force_rank: If the specified rank is already used by another property
  139. * recrod for the same base_id, then set force_rank to TRUE to require
  140. * that only the specified rank can be used. Otherwise, the next
  141. * available rank will be used. If 'update_if_present' is FALSE and
  142. * 'force_rank' is set then an error will occur.
  143. *
  144. * @return
  145. * Return TRUE if successful and FALSE otherwise
  146. *
  147. * @ingroup tripal_chado_prop_api
  148. */
  149. function chado_insert_property($record, $property, $options = array()) {
  150. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  151. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  152. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  153. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  154. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  155. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  156. $value = array_key_exists('value', $property) ? $property['value'] : '';
  157. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  158. $update_if_present = array_key_exists('update_if_present', $options) ? $options['update_if_present'] : FALSE;
  159. $force_rank = array_key_exists('force_rank', $options) ? $options['force_rank'] : FALSE;
  160. // First see if the property is already assigned to the record. I
  161. $props = chado_get_property($record, $property);
  162. if (!is_array($props)) {
  163. if ($props) {
  164. $props = array($props);
  165. }
  166. else {
  167. $props = array();
  168. }
  169. }
  170. if (count($props) > 0) {
  171. // The property is already assigned, so, see if we should update it.
  172. if ($update_if_present) {
  173. return chado_update_property($record, $property);
  174. }
  175. else {
  176. if (!$force_rank) {
  177. // iterate through the properties returned and check to see if the
  178. // property with this value already exists if not, get the largest rank
  179. // and insert the same property but with this new value
  180. foreach ($props as $prop) {
  181. if ($prop->rank > $rank) {
  182. $rank = $prop->rank;
  183. }
  184. if (strcmp($prop->value, $value) == 0) {
  185. return TRUE;
  186. }
  187. }
  188. // now add 1 to the rank
  189. $rank++;
  190. }
  191. else {
  192. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
  193. "The property is already assigned to the record with the following " .
  194. "rank. And, because the 'force_rank' option is used, the property " .
  195. "cannot be added: %property.",
  196. array('%property' => print_r($property, TRUE)));
  197. return FALSE;
  198. }
  199. }
  200. }
  201. // Build the values array for checking if the CVterm exists and for
  202. // inserting the term as a property.
  203. $values = array();
  204. if ($cv_id) {
  205. $values['cv_id'] = $cv_id;
  206. }
  207. if ($cv_name) {
  208. $values['cv_id'] = array(
  209. 'name' => $cv_name,
  210. );
  211. }
  212. if ($type_name) {
  213. $values['name'] = $type_name;
  214. }
  215. if ($type_id) {
  216. $values['cvterm_id'] = $type_id;
  217. }
  218. // Make sure the CV term exists;
  219. $options = array();
  220. $term = chado_select_record('cvterm', array('cvterm_id'), $values, $options);
  221. if (!$term or count($term) == 0) {
  222. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
  223. "Cannot find the term described by: %property.",
  224. array('%property' => print_r($property, TRUE)));
  225. return FALSE;
  226. }
  227. if (count($term) > 1) {
  228. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
  229. "Multiple terms found. Cannot add the property. Property was described " .
  230. "by: %property.",
  231. array('%property' => print_r($property, TRUE))); return FALSE;
  232. }
  233. // Get the foreign key for this property table
  234. $table_desc = chado_get_schema($base_table . 'prop');
  235. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  236. // Add the property to the record.
  237. $values = array(
  238. $fkcol => $base_id,
  239. 'type_id' => $values,
  240. 'value' => $value,
  241. 'rank' => $rank,
  242. );
  243. $result = chado_insert_record($base_table . 'prop', $values);
  244. return $result;
  245. }
  246. /**
  247. * Update a property for a given base table record and property name.
  248. *
  249. * @param $record
  250. * An associative array used to identify the record to which the property
  251. * should be updated. The following keys must be used:
  252. * -table: The base table for which the property should be updated.
  253. * Thus to update a property for a feature the base_table=feature.
  254. * -id: The primary key value of the base table. The property will be
  255. * associated with the record that matches this id.
  256. * -prop_id: The primary key in the [table]prop table. If this value
  257. * is supplied then the 'table' and 'id' keys are not needed.
  258. * @param $property
  259. * An associative array used to specify the property to be updated. It can
  260. * contain the following keys. The keys must be specified to uniquely identify
  261. * the term to be applied. If the options identify more than one CV term
  262. * then an error will occur.
  263. * -type_name: The cvterm name to be selected.
  264. * -type_id: The cvterm_id of the term to be selected.
  265. * -cv_id: The cv_id of the CV that contains the term.
  266. * -cv_name: The name of the CV that contains the term.
  267. * -value: The specific value for the property.
  268. * -rank: The specific rank for the property.
  269. * @param $options
  270. * An associative array containing the following keys:
  271. * -insert_if_missing: A boolean indicating whether a record should be
  272. * inserted if one doesn't exist to update.
  273. *
  274. *
  275. * @return
  276. * Return TRUE on Update/Insert and FALSE otherwise
  277. *
  278. * @ingroup tripal_chado_prop_api
  279. */
  280. function chado_update_property($record, $property, $options = array()) {
  281. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  282. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  283. $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
  284. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  285. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  286. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  287. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  288. $value = array_key_exists('value', $property) ? $property['value'] : '';
  289. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  290. $insert_if_missing = array_key_exists('insert_if_missing', $options) ? $options['insert_if_missing'] : FALSE;
  291. // first see if the property is missing (we can't update a missing property
  292. $prop = chado_get_property($record, $property);
  293. if (count($prop) == 0) {
  294. if ($insert_if_missing) {
  295. return chado_insert_property($record, $property);
  296. }
  297. else {
  298. return FALSE;
  299. }
  300. }
  301. // Build the values array for checking if the CVterm exists.
  302. $type = array();
  303. if ($cv_id) {
  304. $type['cv_id'] = $cv_id;
  305. }
  306. if ($cv_name) {
  307. $type['cv_id'] = array(
  308. 'name' => $cv_name,
  309. );
  310. }
  311. if ($type_name) {
  312. $type['name'] = $type_name;
  313. }
  314. if ($type_id) {
  315. $type['cvterm_id'] = $type_id;
  316. }
  317. // Make sure the CV term exists;
  318. $options = array();
  319. $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
  320. if (!$term or count($term) == 0) {
  321. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  322. "Cannot find the term described by: %property.",
  323. array('%property' => print_r($property, TRUE)));
  324. return FALSE;
  325. }
  326. if (count($term) > 1) {
  327. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  328. "Multiple terms found. Cannot add the property. Property was described " .
  329. "by: %property.",
  330. array('%property' => print_r($property, TRUE)));
  331. return FALSE;
  332. }
  333. // Get the foreign key for this property table
  334. $table_desc = chado_get_schema($base_table . 'prop');
  335. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  336. // construct the array that will match the exact record to update
  337. $match = array(
  338. $fkcol => $base_id,
  339. 'type_id' => $type,
  340. );
  341. // If we have the unique property_id, make sure to use it in the match to
  342. // ensure we get the exact record. Doesn't rely on there only being one
  343. // property of that type.
  344. if ($prop_id) {
  345. $property_pkey = $table_desc['primary key'][0];
  346. $match = array(
  347. $property_pkey => $prop_id,
  348. );
  349. }
  350. // Construct the array of values to be updated.
  351. $values = array();
  352. $values['value'] = $value;
  353. if ($rank) {
  354. $values['rank'] = $rank;
  355. }
  356. // If we have the unique property_id then we can also update the type
  357. // thus add it to the values to be updated
  358. if ($prop_id) {
  359. $values['type_id'] = $type;
  360. }
  361. return chado_update_record($base_table . 'prop', $match, $values);
  362. }
  363. /**
  364. * Deletes a property for a given base table record using the property name
  365. *
  366. * @param $record
  367. * An associative array used to identify the record to which the property
  368. * should be deleted. The following keys must be used:
  369. * -table: The base table for which the property should be deleted.
  370. * Thus to update a property for a feature the base_table=feature.
  371. * -id: The primary key value of the base table. The property will be
  372. * deleted from the record that matches this id.
  373. * -prop_id: The primary key in the [table]prop table to be deleted. If
  374. * this value is supplied then the 'table' and 'id' keys are not needed.
  375. * @param $property
  376. * An associative array used to specify the property to be updated. It can
  377. * contain the following keys. The keys must be specified to uniquely identify
  378. * the term to be applied. If the options identify more than one CV term
  379. * then an error will occur.
  380. * -type_name: The cvterm name to be selected.
  381. * -type_id: The cvterm_id of the term to be selected.
  382. * -cv_id: The cv_id of the CV that contains the term.
  383. * -cv_name: The name of the CV that contains the term.
  384. * -value: The specific value for the property.
  385. * -rank: The specific rank for the property.
  386. *
  387. * @return
  388. * Return TRUE on successful deletion and FALSE otherwise
  389. *
  390. * @ingroup tripal_chado_prop_api
  391. */
  392. function chado_delete_property($record, $property) {
  393. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  394. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  395. $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
  396. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  397. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  398. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  399. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  400. $value = array_key_exists('value', $property) ? $property['value'] : '';
  401. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  402. // Build the values array for checking if the CVterm exists
  403. $type = array();
  404. if ($cv_id) {
  405. $type['cv_id'] = $cv_id;
  406. }
  407. if ($cv_name) {
  408. $type['cv_id'] = array(
  409. 'name' => $cv_name,
  410. );
  411. }
  412. if ($type_name) {
  413. $type['name'] = $type_name;
  414. }
  415. if ($type_id) {
  416. $type['cvterm_id'] = $type_id;
  417. }
  418. // Make sure the CV term exists;
  419. $options = array();
  420. $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
  421. if (!$term or count($term) == 0) {
  422. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_delete_property: " .
  423. "Cannot find the term described by: %property.",
  424. array('%property' => print_r($property, TRUE)));
  425. return FALSE;
  426. }
  427. if (count($term) > 1) {
  428. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_delete_property: " .
  429. "Multiple terms found. Cannot add the property. Property was described " .
  430. "by: %property.",
  431. array('%property' => print_r($property, TRUE))); return FALSE;
  432. }
  433. // get the foreign key for this property table
  434. $table_desc = chado_get_schema($base_table . 'prop');
  435. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  436. // If we have the unique property_id, make sure to use it in the match to ensure
  437. // we get the exact record. Doesn't rely on there only being one property of that type
  438. if ($prop_id) {
  439. $property_pkey = $table_desc['primary key'][0];
  440. $match = array(
  441. $property_pkey => $prop_id
  442. );
  443. }
  444. // construct the array that will match the exact record to update
  445. else {
  446. $match = array(
  447. $fkcol => $record_id,
  448. 'type_id' => $type,
  449. );
  450. }
  451. return chado_delete_record($base_table . 'prop', $match);
  452. }
  453. /**
  454. * Get all records in the base table assigned one or more properties.
  455. *
  456. * The property or properties of interest are specified using the $property
  457. * argument.
  458. *
  459. * @param $record
  460. * An associative array used to identify the table and subset of records to
  461. * to be searched:
  462. * -table: The base table for which the property should be updated.
  463. * Thus to update a property for a feature the base_table=feature.
  464. * -base_records: An array in the format accepted by the chado_select_record
  465. * for specifying a subset of records in the base table.
  466. * @param $property
  467. * An associative array used to specify the property to be selected for. It
  468. * can contain the following keys. The keys must be specified to uniquely
  469. * identify the term to be searched. If the options identify more than one
  470. * CV term then an error will occur.
  471. * -type_name: The cvterm name to be selected.
  472. * -type_id: The cvterm_id of the term to be selected.
  473. * -cv_id: The cv_id of the CV that contains the term.
  474. * -cv_name: The name of the CV that contains the term.
  475. * -value: The specific value for the property.
  476. * -rank: The specific rank for the property.
  477. * @param $options
  478. * An array of options supported by chado_generate_var(). These keys
  479. * are used for generating the cvterm objects returned by this function.
  480. *
  481. * @return
  482. * An array of chado variables with the given property
  483. *
  484. * @ingroup tripal_chado_prop_api
  485. */
  486. function chado_get_record_with_property($record, $property, $options = array()) {
  487. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  488. $base_records= array_key_exists('base_records', $record) ? $record['base_records'] : array();
  489. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  490. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  491. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  492. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  493. $value = array_key_exists('value', $property) ? $property['value'] : '';
  494. $rank = array_key_exists('rank', $property) ? $property['rank'] : '';
  495. $property_table = $base_table . 'prop';
  496. $foreignkey_name = $base_table . '_id';
  497. // Build the values array for checking if the CVterm exists and for
  498. // inserting the term as a property.
  499. $type = array();
  500. if ($cv_id) {
  501. $type['cv_id'] = $cv_id;
  502. }
  503. if ($cv_name) {
  504. $type['cv_id'] = array(
  505. 'name' => $cv_name,
  506. );
  507. }
  508. if ($type_name) {
  509. $type['name'] = $type_name;
  510. }
  511. if ($type_id) {
  512. $type['cvterm_id'] = $type_id;
  513. }
  514. // Make sure the CV term exists;
  515. $term = chado_select_record('cvterm', array('cvterm_id'), $type);
  516. if (!$term or count($term) == 0) {
  517. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  518. "Cannot find the term described by: %property.",
  519. array('%property' => print_r($property, TRUE)));
  520. return FALSE;
  521. }
  522. if (count($term) > 1) {
  523. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  524. "Multiple terms found. Cannot add the property. Property was described " .
  525. "by: %property.",
  526. array('%property' => print_r($property, TRUE))); return FALSE;
  527. }
  528. // Build the array for identifying the property.
  529. $values = array();
  530. $values['type_id'] = $type;
  531. if ($rank) {
  532. $values['rank'] = $rank;
  533. }
  534. if ($value) {
  535. $values['value'] = $value;
  536. }
  537. // Add the base records details to the values array.
  538. if (!empty($base_records)) {
  539. $values[$foreignkey_name] = $base_records;
  540. }
  541. // Now select the ids of the base table that have the properties we want that match.
  542. $select = chado_select_record($property_table, array($foreignkey_name), $values);
  543. // For each of these ids, pull out the full base records
  544. $records = array();
  545. foreach ($select as $s) {
  546. $id = $s->{$foreignkey_name};
  547. $values = array($foreignkey_name => $id);
  548. $records[$id] = chado_generate_var($base_table, $values, $options);
  549. }
  550. return $records;
  551. }
  552. /**
  553. * Retrieves all of the property types currently availalbe in a prop table.
  554. *
  555. * @param $prop_table
  556. * The name of the property table.
  557. * @throws Exception
  558. * @return
  559. * An array of cvterm objects as created by chado_generate_var().
  560. * @ingroup tripal_chado_prop_api
  561. */
  562. function chado_get_table_property_types($prop_table) {
  563. // Make sure this is a prop table.
  564. if (!preg_match('/prop$/', $prop_table)) {
  565. throw new Exception('Please provide a valid Chado property table');
  566. }
  567. $sql = 'SELECT DISTINCT type_id FROM {' . $prop_table . '}';
  568. $results = chado_query($sql);
  569. $types = array();
  570. foreach ($results as $result) {
  571. $types[] = chado_generate_var('cvterm', array('cvterm_id' => $result->type_id));
  572. }
  573. return $types;
  574. }