tripal_chado.property.api.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. $cvalue_id = array_key_exists('cvalue_id', $property) ? $property['cvalue_id'] : '';
  159. $update_if_present = array_key_exists('update_if_present', $options) ? $options['update_if_present'] : FALSE;
  160. $force_rank = array_key_exists('force_rank', $options) ? $options['force_rank'] : FALSE;
  161. // First see if the property is already assigned to the record. I
  162. $props = chado_get_property($record, $property);
  163. if (!is_array($props)) {
  164. if ($props) {
  165. $props = array($props);
  166. }
  167. else {
  168. $props = array();
  169. }
  170. }
  171. if (count($props) > 0) {
  172. // The property is already assigned, so, see if we should update it.
  173. if ($update_if_present) {
  174. return chado_update_property($record, $property);
  175. }
  176. else {
  177. if (!$force_rank) {
  178. // iterate through the properties returned and check to see if the
  179. // property with this value already exists if not, get the largest rank
  180. // and insert the same property but with this new value
  181. foreach ($props as $prop) {
  182. if ($prop->rank > $rank) {
  183. $rank = $prop->rank;
  184. }
  185. if (strcmp($prop->value, $value) == 0) {
  186. return TRUE;
  187. }
  188. }
  189. // now add 1 to the rank
  190. $rank++;
  191. }
  192. else {
  193. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
  194. "The property is already assigned to the record with the following " .
  195. "rank. And, because the 'force_rank' option is used, the property " .
  196. "cannot be added: %property.",
  197. array('%property' => print_r($property, TRUE)));
  198. return FALSE;
  199. }
  200. }
  201. }
  202. // Build the values array for checking if the CVterm exists and for
  203. // inserting the term as a property.
  204. $values = array();
  205. if ($cv_id) {
  206. $values['cv_id'] = $cv_id;
  207. }
  208. if ($cv_name) {
  209. $values['cv_id'] = array(
  210. 'name' => $cv_name,
  211. );
  212. }
  213. if ($type_name) {
  214. $values['name'] = $type_name;
  215. }
  216. if ($type_id) {
  217. $values['cvterm_id'] = $type_id;
  218. }
  219. // Make sure the CV term exists;
  220. $options = array();
  221. $term = chado_select_record('cvterm', array('cvterm_id'), $values, $options);
  222. if (!$term or count($term) == 0) {
  223. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
  224. "Cannot find the term described by: %property.",
  225. array('%property' => print_r($property, TRUE)));
  226. return FALSE;
  227. }
  228. if (count($term) > 1) {
  229. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
  230. "Multiple terms found. Cannot add the property. Property was described " .
  231. "by: %property.",
  232. array('%property' => print_r($property, TRUE))); return FALSE;
  233. }
  234. //Check that the cvalue property exists
  235. if ($cvalue_id){
  236. $term = chado_select_record('cvterm', array('cvterm_id'), array('cvterm_id' => $cvalue_id), $options);
  237. if (!$term or count($term) == 0) {
  238. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
  239. "Cannot find the term for the property value described by: %property.",
  240. array('%property' => print_r($property, TRUE)));
  241. return FALSE;
  242. }
  243. $values['cvalue'] = $cvalue_id;
  244. }
  245. // Get the foreign key for this property table
  246. $table_desc = chado_get_schema($base_table . 'prop');
  247. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  248. // Add the property to the record.
  249. $values = array(
  250. $fkcol => $base_id,
  251. 'type_id' => $values,
  252. 'value' => $value,
  253. 'rank' => $rank,
  254. );
  255. $result = chado_insert_record($base_table . 'prop', $values);
  256. return $result;
  257. }
  258. /**
  259. * Update a property for a given base table record and property name.
  260. *
  261. * @param $record
  262. * An associative array used to identify the record to which the property
  263. * should be updated. The following keys must be used:
  264. * -table: The base table for which the property should be updated.
  265. * Thus to update a property for a feature the base_table=feature.
  266. * -id: The primary key value of the base table. The property will be
  267. * associated with the record that matches this id.
  268. * -prop_id: The primary key in the [table]prop table. If this value
  269. * is supplied then the 'table' and 'id' keys are not needed.
  270. * @param $property
  271. * An associative array used to specify the property to be updated. It can
  272. * contain the following keys. The keys must be specified to uniquely identify
  273. * the term to be applied. If the options identify more than one CV term
  274. * then an error will occur.
  275. * -type_name: The cvterm name to be selected.
  276. * -type_id: The cvterm_id of the term to be selected.
  277. * -cv_id: The cv_id of the CV that contains the term.
  278. * -cv_name: The name of the CV that contains the term.
  279. * -value: The specific value for the property.
  280. * -rank: The specific rank for the property.
  281. * @param $options
  282. * An associative array containing the following keys:
  283. * -insert_if_missing: A boolean indicating whether a record should be
  284. * inserted if one doesn't exist to update.
  285. *
  286. *
  287. * @return
  288. * Return TRUE on Update/Insert and FALSE otherwise
  289. *
  290. * @ingroup tripal_chado_prop_api
  291. */
  292. function chado_update_property($record, $property, $options = array()) {
  293. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  294. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  295. $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
  296. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  297. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  298. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  299. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  300. $value = array_key_exists('value', $property) ? $property['value'] : '';
  301. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  302. $cvalue_id = array_key_exists('cvalue_id', $property) ? $property['cvalue_id'] : '';
  303. $insert_if_missing = array_key_exists('insert_if_missing', $options) ? $options['insert_if_missing'] : FALSE;
  304. // first see if the property is missing (we can't update a missing property
  305. $prop = chado_get_property($record, $property);
  306. if (count($prop) == 0) {
  307. if ($insert_if_missing) {
  308. return chado_insert_property($record, $property);
  309. }
  310. else {
  311. return FALSE;
  312. }
  313. }
  314. // Build the values array for checking if the CVterm exists.
  315. $type = array();
  316. if ($cv_id) {
  317. $type['cv_id'] = $cv_id;
  318. }
  319. if ($cv_name) {
  320. $type['cv_id'] = array(
  321. 'name' => $cv_name,
  322. );
  323. }
  324. if ($type_name) {
  325. $type['name'] = $type_name;
  326. }
  327. if ($type_id) {
  328. $type['cvterm_id'] = $type_id;
  329. }
  330. // Make sure the CV term exists;
  331. $options = array();
  332. $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
  333. if (!$term or count($term) == 0) {
  334. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  335. "Cannot find the term described by: %property.",
  336. array('%property' => print_r($property, TRUE)));
  337. return FALSE;
  338. }
  339. if (count($term) > 1) {
  340. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  341. "Multiple terms found. Cannot add the property. Property was described " .
  342. "by: %property.",
  343. array('%property' => print_r($property, TRUE)));
  344. return FALSE;
  345. }
  346. // Get the foreign key for this property table
  347. $table_desc = chado_get_schema($base_table . 'prop');
  348. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  349. // construct the array that will match the exact record to update
  350. $match = array(
  351. $fkcol => $base_id,
  352. 'type_id' => $type,
  353. );
  354. // If we have the unique property_id, make sure to use it in the match to
  355. // ensure we get the exact record. Doesn't rely on there only being one
  356. // property of that type.
  357. if ($prop_id) {
  358. $property_pkey = $table_desc['primary key'][0];
  359. $match = array(
  360. $property_pkey => $prop_id,
  361. );
  362. }
  363. // Construct the array of values to be updated.
  364. $values = array();
  365. $values['value'] = $value;
  366. if ($rank) {
  367. $values['rank'] = $rank;
  368. }
  369. // If a cvalue_id is supplied, check that it is a valid cvterm
  370. if ($cvalue_id) {
  371. $term = chado_select_record('cvterm', array('cvterm_id'), array('cvterm_id' => $cvalue_id), $options);
  372. if (!$term or count($term) == 0) {
  373. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
  374. "Cannot find the term for the property value described by: %property.",
  375. array('%property' => print_r($property, TRUE)));
  376. return FALSE;
  377. }
  378. $values['cvalue_id'] = $cvalue_id;
  379. }
  380. // If we have the unique property_id then we can also update the type
  381. // thus add it to the values to be updated
  382. if ($prop_id) {
  383. $values['type_id'] = $type;
  384. }
  385. return chado_update_record($base_table . 'prop', $match, $values);
  386. }
  387. /**
  388. * Deletes a property for a given base table record using the property name
  389. *
  390. * @param $record
  391. * An associative array used to identify the record to which the property
  392. * should be deleted. The following keys must be used:
  393. * -table: The base table for which the property should be deleted.
  394. * Thus to update a property for a feature the base_table=feature.
  395. * -id: The primary key value of the base table. The property will be
  396. * deleted from the record that matches this id.
  397. * -prop_id: The primary key in the [table]prop table to be deleted. If
  398. * this value is supplied then the 'table' and 'id' keys are not needed.
  399. * @param $property
  400. * An associative array used to specify the property to be updated. It can
  401. * contain the following keys. The keys must be specified to uniquely identify
  402. * the term to be applied. If the options identify more than one CV term
  403. * then an error will occur.
  404. * -type_name: The cvterm name to be selected.
  405. * -type_id: The cvterm_id of the term to be selected.
  406. * -cv_id: The cv_id of the CV that contains the term.
  407. * -cv_name: The name of the CV that contains the term.
  408. * -value: The specific value for the property.
  409. * -rank: The specific rank for the property.
  410. *
  411. * @return
  412. * Return TRUE on successful deletion and FALSE otherwise
  413. *
  414. * @ingroup tripal_chado_prop_api
  415. */
  416. function chado_delete_property($record, $property) {
  417. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  418. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  419. $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
  420. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  421. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  422. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  423. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  424. $value = array_key_exists('value', $property) ? $property['value'] : '';
  425. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  426. // Build the values array for checking if the CVterm exists
  427. $type = array();
  428. if ($cv_id) {
  429. $type['cv_id'] = $cv_id;
  430. }
  431. if ($cv_name) {
  432. $type['cv_id'] = array(
  433. 'name' => $cv_name,
  434. );
  435. }
  436. if ($type_name) {
  437. $type['name'] = $type_name;
  438. }
  439. if ($type_id) {
  440. $type['cvterm_id'] = $type_id;
  441. }
  442. // Make sure the CV term exists;
  443. $options = array();
  444. $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
  445. if (!$term or count($term) == 0) {
  446. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_delete_property: " .
  447. "Cannot find the term described by: %property.",
  448. array('%property' => print_r($property, TRUE)));
  449. return FALSE;
  450. }
  451. if (count($term) > 1) {
  452. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_delete_property: " .
  453. "Multiple terms found. Cannot add the property. Property was described " .
  454. "by: %property.",
  455. array('%property' => print_r($property, TRUE))); return FALSE;
  456. }
  457. // get the foreign key for this property table
  458. $table_desc = chado_get_schema($base_table . 'prop');
  459. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  460. // If we have the unique property_id, make sure to use it in the match to ensure
  461. // we get the exact record. Doesn't rely on there only being one property of that type
  462. if ($prop_id) {
  463. $property_pkey = $table_desc['primary key'][0];
  464. $match = array(
  465. $property_pkey => $prop_id
  466. );
  467. }
  468. // construct the array that will match the exact record to update
  469. else {
  470. $match = array(
  471. $fkcol => $record_id,
  472. 'type_id' => $type,
  473. );
  474. }
  475. return chado_delete_record($base_table . 'prop', $match);
  476. }
  477. /**
  478. * Get all records in the base table assigned one or more properties.
  479. *
  480. * The property or properties of interest are specified using the $property
  481. * argument.
  482. *
  483. * @param $record
  484. * An associative array used to identify the table and subset of records to
  485. * to be searched:
  486. * -table: The base table for which the property should be updated.
  487. * Thus to update a property for a feature the base_table=feature.
  488. * -base_records: An array in the format accepted by the chado_select_record
  489. * for specifying a subset of records in the base table.
  490. * @param $property
  491. * An associative array used to specify the property to be selected for. It
  492. * can contain the following keys. The keys must be specified to uniquely
  493. * identify the term to be searched. If the options identify more than one
  494. * CV term then an error will occur.
  495. * -type_name: The cvterm name to be selected.
  496. * -type_id: The cvterm_id of the term to be selected.
  497. * -cv_id: The cv_id of the CV that contains the term.
  498. * -cv_name: The name of the CV that contains the term.
  499. * -value: The specific value for the property.
  500. * -rank: The specific rank for the property.
  501. * @param $options
  502. * An array of options supported by chado_generate_var(). These keys
  503. * are used for generating the cvterm objects returned by this function.
  504. *
  505. * @return
  506. * An array of chado variables with the given property
  507. *
  508. * @ingroup tripal_chado_prop_api
  509. */
  510. function chado_get_record_with_property($record, $property, $options = array()) {
  511. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  512. $base_records= array_key_exists('base_records', $record) ? $record['base_records'] : array();
  513. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  514. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  515. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  516. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  517. $value = array_key_exists('value', $property) ? $property['value'] : '';
  518. $rank = array_key_exists('rank', $property) ? $property['rank'] : '';
  519. $property_table = $base_table . 'prop';
  520. $foreignkey_name = $base_table . '_id';
  521. // Build the values array for checking if the CVterm exists and for
  522. // inserting the term as a property.
  523. $type = array();
  524. if ($cv_id) {
  525. $type['cv_id'] = $cv_id;
  526. }
  527. if ($cv_name) {
  528. $type['cv_id'] = array(
  529. 'name' => $cv_name,
  530. );
  531. }
  532. if ($type_name) {
  533. $type['name'] = $type_name;
  534. }
  535. if ($type_id) {
  536. $type['cvterm_id'] = $type_id;
  537. }
  538. // Make sure the CV term exists;
  539. $term = chado_select_record('cvterm', array('cvterm_id'), $type);
  540. if (!$term or count($term) == 0) {
  541. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  542. "Cannot find the term described by: %property.",
  543. array('%property' => print_r($property, TRUE)));
  544. return FALSE;
  545. }
  546. if (count($term) > 1) {
  547. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  548. "Multiple terms found. Cannot add the property. Property was described " .
  549. "by: %property.",
  550. array('%property' => print_r($property, TRUE))); return FALSE;
  551. }
  552. // Build the array for identifying the property.
  553. $values = array();
  554. $values['type_id'] = $type;
  555. if ($rank) {
  556. $values['rank'] = $rank;
  557. }
  558. if ($value) {
  559. $values['value'] = $value;
  560. }
  561. // Add the base records details to the values array.
  562. if (!empty($base_records)) {
  563. $values[$foreignkey_name] = $base_records;
  564. }
  565. // Now select the ids of the base table that have the properties we want that match.
  566. $select = chado_select_record($property_table, array($foreignkey_name), $values);
  567. // For each of these ids, pull out the full base records
  568. $records = array();
  569. foreach ($select as $s) {
  570. $id = $s->{$foreignkey_name};
  571. $values = array($foreignkey_name => $id);
  572. $records[$id] = chado_generate_var($base_table, $values, $options);
  573. }
  574. return $records;
  575. }
  576. /**
  577. * Retrieves all of the property types currently availalbe in a prop table.
  578. *
  579. * @param $prop_table
  580. * The name of the property table.
  581. * @throws Exception
  582. * @return
  583. * An array of cvterm objects as created by chado_generate_var().
  584. * @ingroup tripal_chado_prop_api
  585. */
  586. function chado_get_table_property_types($prop_table) {
  587. // Make sure this is a prop table.
  588. if (!preg_match('/prop$/', $prop_table)) {
  589. throw new Exception('Please provide a valid Chado property table');
  590. }
  591. $sql = 'SELECT DISTINCT type_id FROM {' . $prop_table . '}';
  592. $results = chado_query($sql);
  593. $types = array();
  594. foreach ($results as $result) {
  595. $types[] = chado_generate_var('cvterm', array('cvterm_id' => $result->type_id));
  596. }
  597. return $types;
  598. }