tripal_core.chado_general.api.inc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage data withing the Chado database.
  5. */
  6. require_once 'tripal_core.schema_v1.2.api.inc';
  7. require_once 'tripal_core.schema_v1.11.api.inc';
  8. /**
  9. * @defgroup tripal_chado_api Chado API
  10. * @ingroup tripal_core_api
  11. * @{
  12. * Provides an application programming interface (API) to manage data withing the Chado database.
  13. *
  14. * This includes functions for selecting, inserting, updating and deleting records
  15. * in Chado tables. The functions will ensure proper integrity contraints are met
  16. * for inserts and updates.
  17. *
  18. * Also, a set of functions is provided for creating template variables. First,
  19. * is the chado_generate_var which is used to select one ore more
  20. * records from a table and return an array with foreign key relationships fully
  21. * populated. For example, if selecting a feature, the organism_id and type_id
  22. * would be present in the returned array as a nested array with their respective
  23. * foreign keys also nested. The only fields that are not included are text
  24. * fields (which may be very large) or many-to-many foreign key relationships.
  25. * However, these fields and relationships can be expanded using the
  26. * chado_expand_var.
  27. *
  28. * When a row from a chado table is selected using these two functions, it provides
  29. * a way for users who want to cutomize Drupal template files to access all data
  30. * associate with a specific record.
  31. *
  32. * Finally, the property tables in Chado generally follow the same format. Therefore
  33. * there is a set of functions for inserting, updating and deleting properties for
  34. * any table. This provides quick lookup of properties (provided the CV term is
  35. * known).
  36. *
  37. * @}
  38. *
  39. */
  40. /**
  41. * Set the Tripal Database
  42. *
  43. * The chado_set_active function is used to prevent namespace collisions
  44. * when Chado and Drupal are installed in the same database but in different
  45. * schemas. It is also used when using Drupal functions such as
  46. * db_table_exists().
  47. *
  48. * The connection settings can be altered through the hook
  49. * hook_chado_connection_alter.
  50. *
  51. * Current active connection name is stored in the global variable
  52. * $GLOBALS['chado_active_db'].
  53. *
  54. * @see hook_chado_connection_alter()
  55. *
  56. * @ingroup tripal_chado_api
  57. */
  58. function chado_set_active($dbname = 'default') {
  59. // Check if the chado_active_db has been set yet.
  60. if (!array_key_exists('chado_active_db', $GLOBALS)) {
  61. $GLOBALS['chado_active_db'] = 'default';
  62. }
  63. $previous_db = $active_db = $GLOBALS['chado_active_db'];
  64. $search_path = 'public';
  65. // Change only if 'chado' has been specified.
  66. if ($dbname == 'chado') {
  67. $active_db = 'chado';
  68. $search_path = 'chado,public';
  69. }
  70. $settings = array(
  71. 'dbname' => $dbname,
  72. 'new_active_db' => &$active_db,
  73. 'new_search_path' => &$search_path,
  74. );
  75. // Will call all modules implementing hook_chado_search_path_alter
  76. // note: hooks can alter $active_db and $search_path.
  77. drupal_alter('chado_connection', $settings);
  78. // set chado_active_db to remember active db
  79. $GLOBALS['chado_active_db'] = $active_db;
  80. // set PostgreSQL search_path
  81. db_query('SET search_path TO ' . $search_path);
  82. return $previous_db;
  83. }
  84. /**
  85. * Alter Chado connection settings.
  86. *
  87. * This hook is useful for multi-chado instances. Tripal core functions
  88. * call the chado_set_active() function (e.g. chado_query) but there is no
  89. * opportunity elsewhere to set the active database. This is useful in two
  90. * cases: 1) Users are managed at the database level as in the case of
  91. * SouthGreen Bioinformatics Platform tools (e.g. Banana Genone Hub).
  92. * This allows custom modules to change the database connections on a per-user
  93. * basis, and each user permissions is managed at the database level. Users
  94. * are managed at the database level to provid the same access restrictions
  95. * across various tools that use Chado (e,g, Artemis) 2) When there are
  96. * simply two Chado instances housed in different Chado databases and the
  97. * module needs to control which one is being used at any given time.
  98. *
  99. * @param $settings
  100. * An array containing
  101. *
  102. * @see chado_set_active()
  103. *
  104. * @ingroup tripal_chado_api
  105. */
  106. function hook_chado_connection_alter(&$settings) {
  107. // This example shows how we could make sure no table of the 'public' schema
  108. // would be allowed in the coming queries: to do so, the caller will call
  109. // "chado_set_active('chado_only');" and the hook will remove 'public' from
  110. // the search path.
  111. if ('chado_only' == $settings['dbname']) {
  112. $settings['new_active_db'] = 'chado';
  113. // We don't include 'public' in search path.
  114. $settings['new_search_path'] = 'chado';
  115. }
  116. }
  117. /**
  118. * Get max rank for a given set of criteria
  119. * This function was developed with the many property tables in chado in mind but will
  120. * work for any table with a rank
  121. *
  122. * @params tablename: the name of the chado table you want to select the max rank from
  123. * this table must contain a rank column of type integer
  124. * @params where_options: array(
  125. * <column_name> => array(
  126. * 'type' => <type of column: INT/STRING>,
  127. * 'value' => <the value you want to filter on>,
  128. * 'exact' => <if TRUE use =; if FALSE use ~>,
  129. * )
  130. * )
  131. * where options should include the id and type for that table to correctly
  132. * group a set of records together where the only difference are the value and rank
  133. * @return the maximum rank
  134. *
  135. * @ingroup tripal_chado_api
  136. */
  137. function chado_get_table_max_rank($tablename, $where_options) {
  138. $where_clauses = array();
  139. $where_args = array();
  140. //generate the where clause from supplied options
  141. // the key is the column name
  142. $i = 0;
  143. $sql = "
  144. SELECT max(rank) as max_rank, count(rank) as count
  145. FROM {".$tablename."}
  146. WHERE
  147. ";
  148. foreach ($where_options as $key => $value) {
  149. $where_clauses[] = "$key = :$key";
  150. $where_args[":$key"] = $value;
  151. }
  152. $sql .= implode($where_clauses, ' AND ');
  153. $result = chado_query($sql, $where_args)->fetchObject();
  154. if ($result->count > 0) {
  155. return $result->max_rank;
  156. }
  157. else {
  158. return -1;
  159. }
  160. }
  161. /**
  162. * Retrieve a property for a given base table record.
  163. *
  164. * @param $record
  165. * An array used to identify the record to which the property is associated.
  166. * The following keys must be used:
  167. * -table: The base table for which the property should be updated.
  168. * Thus to update a property for a feature the base_table=feature.
  169. * -id: The primary key value of the base table. The property will be
  170. * associated with the record that matches this id.
  171. * -prop_id: The primary key in the [table]prop table. If this value
  172. * is supplied then the 'table' and 'id' keys are not needed.
  173. * @param $property
  174. * An associative array used to specify the property to be selected. It can
  175. * contain the following keys. The keys must be specified to uniquely identify
  176. * the term to be applied. If the options identify more than one CV term
  177. * then an error will occur.
  178. * -type_name: The cvterm name to be selected.
  179. * -type_id: The cvterm_id of the term to be selected.
  180. * -cv_id: The cv_id of the CV that contains the term.
  181. * -cv_name: The name of the CV that contains the term.
  182. * -value: The specific value for the property.
  183. * -rank: The specific rank for the property.
  184. * @return
  185. * An array in the same format as that generated by the function
  186. * chado_generate_var(). If only one record is returned it
  187. * is a single object. If more than one record is returned then it is an array
  188. * of objects
  189. *
  190. * @ingroup tripal_chado_api
  191. */
  192. function chado_get_property($record, $property) {
  193. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  194. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  195. $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
  196. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  197. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  198. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  199. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  200. $value = array_key_exists('value', $property) ? $property['value'] : '';
  201. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  202. // Build the values array for checking if the CVterm exists and for
  203. // retrieving the term as a property.
  204. $type = array();
  205. if ($cv_id) {
  206. $type['cv_id'] = $cv_id;
  207. }
  208. if ($cv_name) {
  209. $type['cv_id'] = array(
  210. 'name' => $cv_name,
  211. );
  212. }
  213. if ($type_name) {
  214. $type['name'] = $type_name;
  215. }
  216. if ($type_id) {
  217. $type['cvterm_id'] = $type_id;
  218. }
  219. // Make sure the CV term exists;
  220. $options = array();
  221. $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
  222. if (!$term or count($term) == 0) {
  223. tripal_report_error('tripal_core', TRIPAL_ERROR, "chado_get_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_core', TRIPAL_ERROR, "chado_get_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. // get the foreign key for this property table
  235. $table_desc = chado_get_schema($base_table . 'prop');
  236. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  237. // construct the array of values to be selected
  238. $values = array(
  239. $fkcol => $base_id,
  240. 'type_id' => $type,
  241. );
  242. // if we have the unique property_id make sure to add that to the values
  243. if ($prop_id) {
  244. $property_pkey = $table_desc['primary key'][0];
  245. $values[$property_pkey] = $prop_id;
  246. }
  247. $results = chado_generate_var($base_table . 'prop', $values);
  248. if ($results) {
  249. $results = chado_expand_var($results, 'field', $base_table . 'prop.value');
  250. }
  251. return $results;
  252. }
  253. /**
  254. * Insert a property for a given base table.
  255. *
  256. * By default if the property already exists a new property is added with the
  257. * next available rank. If the option 'update_if_present' is specified then
  258. * the record will be updated if it exists rather than adding a new property.
  259. *
  260. * @param $record
  261. * An associative array used to identify the record to which the property
  262. * should be assigned. The following keys must be used:
  263. * -table: The base table for which the property should be inserted.
  264. * Thus to insert a property for a feature the base_table=feature and
  265. * property is inserted into featureprop
  266. * -id: The primary key value of the base table. The property will be
  267. * associated with the record that matches this id.
  268. * @param $property
  269. * An associative array used to specify the property to be added. It can
  270. * contain the following keys. The keys must be specified to uniquely identify
  271. * the term to be applied. If the options identify more than one CV term
  272. * then an error will occur.
  273. * -type_name: The cvterm name to be selected.
  274. * -type_id: The cvterm_id of the term to be selected.
  275. * -cv_id: The cv_id of the CV that contains the term.
  276. * -cv_name: The name of the CV that contains the term.
  277. * -value: The specific value for the property.
  278. * -rank: The specific rank for the property.
  279. * @param $options
  280. * An associative array containing the following keys:
  281. * -update_if_present: A boolean indicating whether an existing record
  282. * should be updated. If the property already exists and this value is
  283. * not specified or is zero then a new property will be added with the
  284. * next largest rank.
  285. * -force_rank: If the specified rank is already used by another property
  286. * recrod for the same base_id, then set force_rank to TRUE to require
  287. * that only the specified rank can be used. Otherwise, the next
  288. * available rank will be used. If 'update_if_present' is FALSE and
  289. * 'force_rank' is set then an error will occur.
  290. *
  291. * @return
  292. * Return TRUE if successful and FALSE otherwise
  293. *
  294. * @ingroup tripal_chado_api
  295. */
  296. function chado_insert_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. $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. $update_if_present = array_key_exists('update_if_present', $options) ? $options['update_if_present'] : FALSE;
  306. $force_rank = array_key_exists('force_rank', $options) ? $options['force_rank'] : FALSE;
  307. // First see if the property is already assigned to the record. I
  308. $props = chado_get_property($record, $property);
  309. if (!is_array($props)) {
  310. if ($props) {
  311. $props = array($props);
  312. }
  313. else {
  314. $props = array();
  315. }
  316. }
  317. if (count($props) > 0) {
  318. // The property is already assigned, so, see if we should update it.
  319. if ($update_if_present) {
  320. return chado_update_property($record, $property);
  321. }
  322. else {
  323. if (!$force_rank) {
  324. // iterate through the properties returned and check to see if the
  325. // property with this value already exists if not, get the largest rank
  326. // and insert the same property but with this new value
  327. foreach ($props as $prop) {
  328. if ($prop->rank > $rank) {
  329. $rank = $prop->rank;
  330. }
  331. if (strcmp($prop->value, $value) == 0) {
  332. return TRUE;
  333. }
  334. }
  335. // now add 1 to the rank
  336. $rank++;
  337. }
  338. else {
  339. tripal_report_error('tripal_core', TRIPAL_ERROR, "chado_insert_property: " .
  340. "The property is already assigned to the record with the following " .
  341. "rank. And, because the 'force_rank' option is used, the property " .
  342. "cannot be added: %property.",
  343. array('%property' => print_r($property, true)));
  344. return FALSE;
  345. }
  346. }
  347. }
  348. // Build the values array for checking if the CVterm exists and for
  349. // inserting the term as a property.
  350. $values = array();
  351. if ($cv_id) {
  352. $values['cv_id'] = $cv_id;
  353. }
  354. if ($cv_name) {
  355. $values['cv_id'] = array(
  356. 'name' => $cv_name,
  357. );
  358. }
  359. if ($type_name) {
  360. $values['name'] = $type_name;
  361. }
  362. if ($type_id) {
  363. $values['cvterm_id'] = $type_id;
  364. }
  365. // Make sure the CV term exists;
  366. $options = array();
  367. $term = chado_select_record('cvterm', array('cvterm_id'), $values, $options);
  368. if (!$term or count($term) == 0) {
  369. tripal_report_error('tripal_core', TRIPAL_ERROR, "chado_insert_property: " .
  370. "Cannot find the term described by: %property.",
  371. array('%property' => print_r($property, TRUE)));
  372. return FALSE;
  373. }
  374. if (count($term) > 1) {
  375. tripal_report_error('tripal_core', TRIPAL_ERROR, "chado_insert_property: " .
  376. "Multiple terms found. Cannot add the property. Property was described " .
  377. "by: %property.",
  378. array('%property' => print_r($property, TRUE))); return FALSE;
  379. }
  380. // Get the foreign key for this property table
  381. $table_desc = chado_get_schema($base_table . 'prop');
  382. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  383. // Add the property to the record.
  384. $values = array(
  385. $fkcol => $base_id,
  386. 'type_id' => $values,
  387. 'value' => $value,
  388. 'rank' => $rank,
  389. );
  390. $result = chado_insert_record($base_table . 'prop', $values);
  391. return $result;
  392. }
  393. /**
  394. * Update a property for a given base table record and property name.
  395. *
  396. * @param $record
  397. * An associative array used to identify the record to which the property
  398. * should be updated. The following keys must be used:
  399. * -table: The base table for which the property should be updated.
  400. * Thus to update a property for a feature the base_table=feature.
  401. * -id: The primary key value of the base table. The property will be
  402. * associated with the record that matches this id.
  403. * -prop_id: The primary key in the [table]prop table. If this value
  404. * is supplied then the 'table' and 'id' keys are not needed.
  405. * @param $property
  406. * An associative array used to specify the property to be updated. It can
  407. * contain the following keys. The keys must be specified to uniquely identify
  408. * the term to be applied. If the options identify more than one CV term
  409. * then an error will occur.
  410. * -type_name: The cvterm name to be selected.
  411. * -type_id: The cvterm_id of the term to be selected.
  412. * -cv_id: The cv_id of the CV that contains the term.
  413. * -cv_name: The name of the CV that contains the term.
  414. * -value: The specific value for the property.
  415. * -rank: The specific rank for the property.
  416. * @param $options
  417. * An associative array containing the following keys:
  418. * -insert_if_missing: A boolean indicating whether a record should be
  419. * inserted if one doesn't exist to update.
  420. *
  421. *
  422. * @return
  423. * Return TRUE on Update/Insert and FALSE otherwise
  424. *
  425. * @ingroup tripal_chado_api
  426. */
  427. function chado_update_property($record, $property, $options = array()) {
  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. $insert_if_missing = array_key_exists('insert_if_missing', $options) ? $options['insert_if_missing'] : FALSE;
  438. // first see if the property is missing (we can't update a missing property
  439. $prop = chado_get_property($record, $property);
  440. if (count($prop) == 0) {
  441. if ($insert_if_missing) {
  442. return chado_insert_property($record, $property);
  443. }
  444. else {
  445. return FALSE;
  446. }
  447. }
  448. // Build the values array for checking if the CVterm exists.
  449. $type = array();
  450. if ($cv_id) {
  451. $type['cv_id'] = $cv_id;
  452. }
  453. if ($cv_name) {
  454. $type['cv_id'] = array(
  455. 'name' => $cv_name,
  456. );
  457. }
  458. if ($type_name) {
  459. $type['name'] = $type_name;
  460. }
  461. if ($type_id) {
  462. $type['cvterm_id'] = $type_id;
  463. }
  464. // Make sure the CV term exists;
  465. $options = array();
  466. $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
  467. if (!$term or count($term) == 0) {
  468. tripal_report_error('tripal_core', TRIPAL_ERROR, "chado_update_property: " .
  469. "Cannot find the term described by: %property.",
  470. array('%property' => print_r($property, TRUE)));
  471. return FALSE;
  472. }
  473. if (count($term) > 1) {
  474. tripal_report_error('tripal_core', TRIPAL_ERROR, "chado_update_property: " .
  475. "Multiple terms found. Cannot add the property. Property was described " .
  476. "by: %property.",
  477. array('%property' => print_r($property, TRUE)));
  478. return FALSE;
  479. }
  480. // Get the foreign key for this property table
  481. $table_desc = chado_get_schema($base_table . 'prop');
  482. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  483. // construct the array that will match the exact record to update
  484. $match = array(
  485. $fkcol => $base_id,
  486. 'type_id' => $type,
  487. );
  488. // If we have the unique property_id, make sure to use it in the match to
  489. // ensure we get the exact record. Doesn't rely on there only being one
  490. // property of that type.
  491. if ($prop_id) {
  492. $property_pkey = $table_desc['primary key'][0];
  493. $match = array(
  494. $property_pkey => $prop_id,
  495. );
  496. }
  497. // Construct the array of values to be updated.
  498. $values = array();
  499. $values['value'] = $value;
  500. if ($rank) {
  501. $values['rank'] = $rank;
  502. }
  503. // If we have the unique property_id then we can also update the type
  504. // thus add it to the values to be updated
  505. if ($prop_id) {
  506. $values['type_id'] = $type;
  507. }
  508. return chado_update_record($base_table . 'prop', $match, $values);
  509. }
  510. /**
  511. * Deletes a property for a given base table record using the property name
  512. *
  513. * @param $record
  514. * An associative array used to identify the record to which the property
  515. * should be deleted. The following keys must be used:
  516. * -table: The base table for which the property should be deleted.
  517. * Thus to update a property for a feature the base_table=feature.
  518. * -id: The primary key value of the base table. The property will be
  519. * deleted from the record that matches this id.
  520. * -prop_id: The primary key in the [table]prop table to be deleted. If
  521. * this value is supplied then the 'table' and 'id' keys are not needed.
  522. * @param $property
  523. * An associative array used to specify the property to be updated. It can
  524. * contain the following keys. The keys must be specified to uniquely identify
  525. * the term to be applied. If the options identify more than one CV term
  526. * then an error will occur.
  527. * -type_name: The cvterm name to be selected.
  528. * -type_id: The cvterm_id of the term to be selected.
  529. * -cv_id: The cv_id of the CV that contains the term.
  530. * -cv_name: The name of the CV that contains the term.
  531. * -value: The specific value for the property.
  532. * -rank: The specific rank for the property.
  533. *
  534. * @return
  535. * Return TRUE on successful deletion and FALSE otherwise
  536. *
  537. * @ingroup tripal_chado_api
  538. */
  539. function chado_delete_property($record, $property) {
  540. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  541. $base_id = array_key_exists('id', $record) ? $record['id'] : '';
  542. $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
  543. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  544. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  545. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  546. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  547. $value = array_key_exists('value', $property) ? $property['value'] : '';
  548. $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
  549. // Build the values array for checking if the CVterm exists
  550. $type = array();
  551. if ($cv_id) {
  552. $type['cv_id'] = $cv_id;
  553. }
  554. if ($cv_name) {
  555. $type['cv_id'] = array(
  556. 'name' => $cv_name,
  557. );
  558. }
  559. if ($type_name) {
  560. $type['name'] = $type_name;
  561. }
  562. if ($type_id) {
  563. $type['cvterm_id'] = $type_id;
  564. }
  565. // Make sure the CV term exists;
  566. $options = array();
  567. $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
  568. if (!$term or count($term) == 0) {
  569. tripal_report_error('tripal_core', TRIPAL_ERROR, "chado_delete_property: " .
  570. "Cannot find the term described by: %property.",
  571. array('%property' => print_r($property, TRUE)));
  572. return FALSE;
  573. }
  574. if (count($term) > 1) {
  575. tripal_report_error('tripal_core', TRIPAL_ERROR, "chado_delete_property: " .
  576. "Multiple terms found. Cannot add the property. Property was described " .
  577. "by: %property.",
  578. array('%property' => print_r($property, TRUE))); return FALSE;
  579. }
  580. // get the foreign key for this property table
  581. $table_desc = chado_get_schema($base_table . 'prop');
  582. $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
  583. // If we have the unique property_id, make sure to use it in the match to ensure
  584. // we get the exact record. Doesn't rely on there only being one property of that type
  585. if ($prop_id) {
  586. $property_pkey = $table_desc['primary key'][0];
  587. $match = array(
  588. $property_pkey => $prop_id
  589. );
  590. }
  591. // construct the array that will match the exact record to update
  592. else {
  593. $match = array(
  594. $fkcol => $record_id,
  595. 'type_id' => $type,
  596. );
  597. }
  598. return chado_delete_record($base_table . 'prop', $match);
  599. }
  600. /**
  601. * Get all records in the base table assigned one or more properties.
  602. *
  603. * The property or properties of interest are specified using the $property
  604. * argument.
  605. *
  606. * @param $record
  607. * An associative array used to identify the table and subset of records to
  608. * to be searched:
  609. * -table: The base table for which the property should be updated.
  610. * Thus to update a property for a feature the base_table=feature.
  611. * -base_records: An array in the format accepted by the chado_select_record
  612. * for specifying a subset of records in the base table.
  613. * @param $property
  614. * An associative array used to specify the property to be selected for. It
  615. * can contain the following keys. The keys must be specified to uniquely
  616. * identify the term to be searched. If the options identify more than one
  617. * CV term then an error will occur.
  618. * -type_name: The cvterm name to be selected.
  619. * -type_id: The cvterm_id of the term to be selected.
  620. * -cv_id: The cv_id of the CV that contains the term.
  621. * -cv_name: The name of the CV that contains the term.
  622. * -value: The specific value for the property.
  623. * -rank: The specific rank for the property.
  624. * @param $options
  625. * An array of options supported by chado_generate_var(). These keys
  626. * are used for generating the cvterm objects returned by this function.
  627. *
  628. * @return
  629. * An array of chado variables with the given property
  630. *
  631. * @ingroup tripal_chado_api
  632. */
  633. function chado_get_record_with_property($record, $property, $options = array()) {
  634. $base_table = array_key_exists('table', $record) ? $record['table'] : '';
  635. $base_records= array_key_exists('base_records', $record) ? $record['base_records'] : array();
  636. $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
  637. $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
  638. $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
  639. $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
  640. $value = array_key_exists('value', $property) ? $property['value'] : '';
  641. $rank = array_key_exists('rank', $property) ? $property['rank'] : '';
  642. $property_table = $base_table . 'prop';
  643. $foreignkey_name = $base_table . '_id';
  644. // Build the values array for checking if the CVterm exists and for
  645. // inserting the term as a property.
  646. $type = array();
  647. if ($cv_id) {
  648. $type['cv_id'] = $cv_id;
  649. }
  650. if ($cv_name) {
  651. $type['cv_id'] = array(
  652. 'name' => $cv_name,
  653. );
  654. }
  655. if ($type_name) {
  656. $type['name'] = $type_name;
  657. }
  658. if ($type_id) {
  659. $type['cvterm_id'] = $type_id;
  660. }
  661. // Make sure the CV term exists;
  662. $term = chado_select_record('cvterm', array('cvterm_id'), $type);
  663. if (!$term or count($term) == 0) {
  664. tripal_report_error('tripal_core', TRIPAL_ERROR, "chado_update_property: " .
  665. "Cannot find the term described by: %property.",
  666. array('%property' => print_r($property, TRUE)));
  667. return FALSE;
  668. }
  669. if (count($term) > 1) {
  670. tripal_report_error('tripal_core', TRIPAL_ERROR, "chado_update_property: " .
  671. "Multiple terms found. Cannot add the property. Property was described " .
  672. "by: %property.",
  673. array('%property' => print_r($property, TRUE))); return FALSE;
  674. }
  675. // Build the array for identifying the property.
  676. $values = array();
  677. $values['type_id'] = $type;
  678. if ($rank) {
  679. $values['rank'] = $rank;
  680. }
  681. if ($value) {
  682. $values['value'] = $value;
  683. }
  684. // Add the base records details to the values array.
  685. if (!empty($base_records)) {
  686. $values[$foreignkey_name] = $base_records;
  687. }
  688. // Now select the ids of the base table that have the properties we want that match.
  689. $select = chado_select_record($property_table, array($foreignkey_name), $values);
  690. // For each of these ids, pull out the full base records
  691. $records = array();
  692. foreach ($select as $s) {
  693. $id = $s->{$foreignkey_name};
  694. $values = array($foreignkey_name => $id);
  695. $records[$id] = chado_generate_var($base_table, $values, $options);
  696. }
  697. return $records;
  698. }