tripal_chado.property.api.inc 25 KB

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