tripal_chado.property.api.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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 '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
  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. $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. * -cvalue_id: The cvterm_id of the value for the property.
  282. * **note** cvalue_id is an anticipated column in the the next Chado
  283. * release (1.4). It is included here for early adopters.
  284. *
  285. * @param $options
  286. * An associative array containing the following keys:
  287. * -insert_if_missing: A boolean indicating whether a record should be
  288. * inserted if one doesn't exist to update.
  289. *
  290. *
  291. * @return
  292. * Return TRUE on Update/Insert and FALSE otherwise.
  293. *
  294. * @ingroup tripal_chado_prop_api
  295. */
  296. function chado_update_property($record, $property, $options = array()) {
  297. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  298. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  299. $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
  300. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  301. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  302. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  303. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  304. $value = array_key_exists('value', $property) ? $property['value'] : '';
  305. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  306. $cvalue_id = array_key_exists('cvalue_id', $property) ? $property['cvalue_id'] : '';
  307. $insert_if_missing = array_key_exists('insert_if_missing', $options) ? $options['insert_if_missing'] : FALSE;
  308. // First see if the property is missing (we can't update a missing property.
  309. $prop = chado_get_property($record, $property);
  310. if (!is_array($prop) or count($prop) == 0) {
  311. if ($insert_if_missing) {
  312. return chado_insert_property($record, $property);
  313. }
  314. else {
  315. return FALSE;
  316. }
  317. }
  318. // Build the values array for checking if the CVterm exists.
  319. $type = array();
  320. if ($cv_id) {
  321. $type['cv_id'] = $cv_id;
  322. }
  323. if ($cv_name) {
  324. $type['cv_id'] = array(
  325. 'name' => $cv_name,
  326. );
  327. }
  328. if ($type_name) {
  329. $type['name'] = $type_name;
  330. }
  331. if ($type_id) {
  332. $type['cvterm_id'] = $type_id;
  333. }
  334. // Make sure the CV term exists.
  335. $options = array();
  336. $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
  337. if (!$term or count($term) == 0) {
  338. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  339. "Cannot find the term described by: %property.",
  340. array('%property' => print_r($property, TRUE)));
  341. return FALSE;
  342. }
  343. if (count($term) > 1) {
  344. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  345. "Multiple terms found. Cannot add the property. Property was described " .
  346. "by: %property.",
  347. array('%property' => print_r($property, TRUE)));
  348. return FALSE;
  349. }
  350. // Get the foreign key for this property table.
  351. $table_desc = chado_get_schema($base_table . 'prop');
  352. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  353. // Construct the array that will match the exact record to update.
  354. $match = array(
  355. $fkcol => $base_id,
  356. 'type_id' => $type,
  357. );
  358. // If we have the unique property_id, make sure to use it in the match to
  359. // ensure we get the exact record. Doesn't rely on there only being one
  360. // property of that type.
  361. if ($prop_id) {
  362. $property_pkey = $table_desc['primary key'][0];
  363. $match = array(
  364. $property_pkey => $prop_id,
  365. );
  366. }
  367. // Construct the array of values to be updated.
  368. $values = array();
  369. $values['value'] = $value;
  370. if ($rank) {
  371. $values['rank'] = $rank;
  372. }
  373. // If a cvalue_id is supplied, check that it is a valid cvterm.
  374. if ($cvalue_id) {
  375. $term = chado_select_record('cvterm', array('cvterm_id'), array('cvterm_id' => $cvalue_id), $options);
  376. if (!$term or count($term) == 0) {
  377. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
  378. "Cannot find the term for the property value described by: %property.",
  379. array('%property' => print_r($property, TRUE)));
  380. return FALSE;
  381. }
  382. $values['cvalue_id'] = $cvalue_id;
  383. }
  384. // If we have the unique property_id then we can also update the type
  385. // thus add it to the values to be updated.
  386. if ($prop_id) {
  387. $values['type_id'] = $type;
  388. }
  389. return chado_update_record($base_table . 'prop', $match, $values);
  390. }
  391. /**
  392. * Deletes a property for a given base table record using the property name.
  393. *
  394. * @param $record
  395. * An associative array used to identify the record to which the property
  396. * should be deleted. The following keys must be used:
  397. * -table: The base table for which the property should be deleted.
  398. * Thus to update a property for a feature the base_table=feature.
  399. * -id: The primary key value of the base table. The property will be
  400. * deleted from the record that matches this id.
  401. * -prop_id: The primary key in the [table]prop table to be deleted. If
  402. * this value is supplied then the 'table' and 'id' keys are not needed.
  403. * @param $property
  404. * An associative array used to specify the property to be updated. It can
  405. * contain the following keys. The keys must be specified to uniquely identify
  406. * the term to be applied. If the options identify more than one CV term
  407. * then an error will occur.
  408. * -type_name: The cvterm name to be selected.
  409. * -type_id: The cvterm_id of the term to be selected.
  410. * -cv_id: The cv_id of the CV that contains the term.
  411. * -cv_name: The name of the CV that contains the term.
  412. * -value: The specific value for the property.
  413. * -rank: The specific rank for the property.
  414. *
  415. * @return
  416. * Return TRUE on successful deletion and FALSE otherwise.
  417. *
  418. * @ingroup tripal_chado_prop_api
  419. */
  420. function chado_delete_property($record, $property) {
  421. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  422. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  423. $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
  424. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  425. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  426. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  427. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  428. $value = array_key_exists('value', $property) ? $property['value'] : '';
  429. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  430. // Build the values array for checking if the CVterm exists.
  431. $type = array();
  432. if ($cv_id) {
  433. $type['cv_id'] = $cv_id;
  434. }
  435. if ($cv_name) {
  436. $type['cv_id'] = array(
  437. 'name' => $cv_name,
  438. );
  439. }
  440. if ($type_name) {
  441. $type['name'] = $type_name;
  442. }
  443. if ($type_id) {
  444. $type['cvterm_id'] = $type_id;
  445. }
  446. // Make sure the CV term exists.
  447. $options = array();
  448. $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
  449. if (!$term or count($term) == 0) {
  450. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_delete_property: " .
  451. "Cannot find the term described by: %property.",
  452. array('%property' => print_r($property, TRUE)));
  453. return FALSE;
  454. }
  455. if (count($term) > 1) {
  456. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_delete_property: " .
  457. "Multiple terms found. Cannot add the property. Property was described " .
  458. "by: %property.",
  459. array('%property' => print_r($property, TRUE))); return FALSE;
  460. }
  461. // Get the foreign key for this property table.
  462. $table_desc = chado_get_schema($base_table . 'prop');
  463. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  464. // If we have the unique property_id, make sure to use it in the match to
  465. // ensure we get the exact record. Doesn't rely on there only being one
  466. // property of that type.
  467. if ($prop_id) {
  468. $property_pkey = $table_desc['primary key'][0];
  469. $match = array(
  470. $property_pkey => $prop_id
  471. );
  472. }
  473. // Construct the array that will match the exact record to update.
  474. else {
  475. $match = array(
  476. $fkcol => $record_id,
  477. 'type_id' => $type,
  478. );
  479. }
  480. return chado_delete_record($base_table . 'prop', $match);
  481. }
  482. /**
  483. * Get all records in the base table assigned one or more properties.
  484. *
  485. * The property or properties of interest are specified using the $property
  486. * argument.
  487. *
  488. * @param $record
  489. * An associative array used to identify the table and subset of records to
  490. * to be searched:
  491. * -table: The base table for which the property should be updated.
  492. * Thus to update a property for a feature the base_table=feature.
  493. * -base_records: An array in the format accepted by the chado_select_record
  494. * for specifying a subset of records in the base table.
  495. * @param $property
  496. * An associative array used to specify the property to be selected for. It
  497. * can contain the following keys. The keys must be specified to uniquely
  498. * identify the term to be searched. If the options identify more than one
  499. * CV term then an error will occur.
  500. * -type_name: The cvterm name to be selected.
  501. * -type_id: The cvterm_id of the term to be selected.
  502. * -cv_id: The cv_id of the CV that contains the term.
  503. * -cv_name: The name of the CV that contains the term.
  504. * -value: The specific value for the property.
  505. * -rank: The specific rank for the property.
  506. * -cvalue_id: The cvterm_id of the value for the property.
  507. * **note** cvalue_id is an anticipated column in the the next Chado
  508. * release (1.4). It is included here for early adopters.
  509. *
  510. * @param $options
  511. * An array of options supported by chado_generate_var(). These keys
  512. * are used for generating the cvterm objects returned by this function.
  513. *
  514. * @return
  515. * An array of chado variables with the given property.
  516. *
  517. * @ingroup tripal_chado_prop_api
  518. */
  519. function chado_get_record_with_property($record, $property, $options = array()) {
  520. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  521. $base_records= array_key_exists('base_records', $record) ? $record['base_records'] : array();
  522. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  523. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  524. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  525. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  526. $value = array_key_exists('value', $property) ? $property['value'] : '';
  527. $rank = array_key_exists('rank', $property) ? $property['rank'] : '';
  528. $property_table = $base_table . 'prop';
  529. $foreignkey_name = $base_table . '_id';
  530. // Build the values array for checking if the CVterm exists and for
  531. // inserting the term as a property.
  532. $type = array();
  533. if ($cv_id) {
  534. $type['cv_id'] = $cv_id;
  535. }
  536. if ($cv_name) {
  537. $type['cv_id'] = array(
  538. 'name' => $cv_name,
  539. );
  540. }
  541. if ($type_name) {
  542. $type['name'] = $type_name;
  543. }
  544. if ($type_id) {
  545. $type['cvterm_id'] = $type_id;
  546. }
  547. // Make sure the CV term exists;
  548. $term = chado_select_record('cvterm', array('cvterm_id'), $type);
  549. if (!$term or count($term) == 0) {
  550. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  551. "Cannot find the term described by: %property.",
  552. array('%property' => print_r($property, TRUE)));
  553. return FALSE;
  554. }
  555. if (count($term) > 1) {
  556. tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
  557. "Multiple terms found. Cannot add the property. Property was described " .
  558. "by: %property.",
  559. array('%property' => print_r($property, TRUE))); return FALSE;
  560. }
  561. // Build the array for identifying the property.
  562. $values = array();
  563. $values['type_id'] = $type;
  564. if ($rank) {
  565. $values['rank'] = $rank;
  566. }
  567. if ($value) {
  568. $values['value'] = $value;
  569. }
  570. // Add the base records details to the values array.
  571. if (!empty($base_records)) {
  572. $values[$foreignkey_name] = $base_records;
  573. }
  574. // Now select the ids of the base table that have the properties we want that
  575. // match.
  576. $select = chado_select_record($property_table, array($foreignkey_name), $values);
  577. // For each of these ids, pull out the full base records.
  578. $records = array();
  579. foreach ($select as $s) {
  580. $id = $s->{$foreignkey_name};
  581. $values = array($foreignkey_name => $id);
  582. $records[$id] = chado_generate_var($base_table, $values, $options);
  583. }
  584. return $records;
  585. }
  586. /**
  587. * Retrieves all of the property types currently availalbe in a prop table.
  588. *
  589. * @param $prop_table
  590. * The name of the property table.
  591. * @throws Exception
  592. *
  593. * @return
  594. * An array of cvterm objects as created by chado_generate_var().
  595. *
  596. * @ingroup tripal_chado_prop_api
  597. */
  598. function chado_get_table_property_types($prop_table) {
  599. // Make sure this is a prop table.
  600. if (!preg_match('/prop$/', $prop_table)) {
  601. throw new Exception('Please provide a valid Chado property table');
  602. }
  603. $sql = 'SELECT DISTINCT type_id FROM {' . $prop_table . '}';
  604. $results = chado_query($sql);
  605. $types = array();
  606. foreach ($results as $result) {
  607. $types[] = chado_generate_var('cvterm', array('cvterm_id' => $result->type_id));
  608. }
  609. return $types;
  610. }