tripal_chado.property.api.inc 25 KB

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