tripal_chado.property.api.inc 25 KB

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