tripal_chado.api.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * This file contains miscellaneous API functions specific to working with
  6. * records in Chado that do not have a home in any other sub category of
  7. * API functions.
  8. */
  9. /**
  10. * @defgroup tripal_chado_api Chado
  11. *
  12. * @ingroup tripal_api
  13. * The Tripal Chado API is a set of functions for interacting with data
  14. * inside of a Chado relational database. Entities (or pages) in Drupal
  15. * that are provided by Tripal can supply data from any supported database
  16. * back-end, and Chado is the default. This API contains a variety of sub
  17. * categories (or groups) where functions are organized. Any extension module
  18. * that desires to work with data in Chado will find these functions useful.
  19. */
  20. /**
  21. * Publishes content in Chado as a new TripalEntity entity.
  22. *
  23. * @param $values
  24. * A key/value associative array that supports the following keys:
  25. * - bundle_name: The name of the the TripalBundle (e.g. bio_data-12345).
  26. * @param $job_id
  27. * (Optional) The numeric job ID as provided by the Tripal jobs system. There
  28. * is no need to specify this argument if this function is being called
  29. * outside of the jobs systems.
  30. *
  31. * @return boolean
  32. * TRUE if all of the records of the given bundle type were published, and
  33. * FALSE if a failure occured.
  34. *
  35. * @ingroup tripal_chado_api
  36. */
  37. function chado_publish_records($values, $job_id = NULL) {
  38. // @performance remove after development
  39. $started_at = microtime(true);
  40. // We want the job object in order to report progress.
  41. if (is_object($job_id)) {
  42. $job = $job_id;
  43. }
  44. if (is_numeric($job_id)) {
  45. $job = new TripalJob();
  46. $job->load($job_id);
  47. }
  48. $report_progress = FALSE;
  49. if (is_object($job)) {
  50. $report_progress = TRUE;
  51. }
  52. // Make sure we have the required options: bundle_name.
  53. if (!array_key_exists('bundle_name', $values) or !$values['bundle_name']) {
  54. tripal_report_error('tripal_chado', TRIPAL_ERROR,
  55. "Could not publish record: @error",
  56. array('@error' => 'The bundle name must be provided'));
  57. return FALSE;
  58. }
  59. // Get the incoming arguments from the $values array.
  60. $bundle_name = $values['bundle_name'];
  61. $filters = array_key_exists('filters', $values) ? $values['filters'] : array();
  62. $sync_node = array_key_exists('sync_node', $values) ? $values['sync_node'] : '';
  63. // We want to break the number of records to publish into chunks in order to ensure
  64. // transactions do not run for too long (performance issue). The number of records
  65. // to be processed per chunk is set here:
  66. $chunk_size = 500;
  67. // @performance remove after development: 0.00059294700622559s
  68. // Load the bundle entity so we can get information about which Chado
  69. // table/field this entity belongs to.
  70. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  71. if (!$bundle) {
  72. tripal_report_error('tripal_chado', TRIPAL_ERROR,
  73. "Unknown bundle. Could not publish record: @error",
  74. array('@error' => 'The bundle name must be provided'));
  75. return FALSE;
  76. }
  77. $chado_entity_table = chado_get_bundle_entity_table($bundle);
  78. // @performance remove after development: 0.05065393447876s
  79. // Get the mapping of the bio data type to the Chado table.
  80. $chado_bundle = db_select('chado_bundle', 'cb')
  81. ->fields('cb')
  82. ->condition('bundle_id', $bundle->id)
  83. ->execute()
  84. ->fetchObject();
  85. if(!$chado_bundle) {
  86. tripal_report_error('tripal_chado', TRIPAL_ERROR,
  87. "Cannot find mapping of bundle to Chado tables. Could not publish record.");
  88. return FALSE;
  89. }
  90. $table = $chado_bundle->data_table;
  91. $type_column = $chado_bundle->type_column;
  92. $type_linker_table = $chado_bundle->type_linker_table;
  93. $cvterm_id = $chado_bundle->type_id;
  94. $type_value = $chado_bundle->type_value;
  95. // @performance remove after development:0.051163911819458s
  96. // Get the table information for the Chado table.
  97. $table_schema = chado_get_schema($table);
  98. $pkey_field = $table_schema['primary key'][0];
  99. // @performance remove after development:0.05134105682373s
  100. // Construct the SQL for identifying which records should be published.
  101. // @performance find a way to optimize this?
  102. $args = array();
  103. $select = "SELECT T.$pkey_field as record_id ";
  104. $from = "
  105. FROM {" . $table . "} T
  106. LEFT JOIN [" . $chado_entity_table . "] CE on CE.record_id = T.$pkey_field
  107. ";
  108. // For migration of Tripal v2 nodes to entities we want to include the
  109. // coresponding chado linker table.
  110. if ($sync_node && db_table_exists('chado_' . $table)) {
  111. $select = "SELECT T.$pkey_field as record_id, CT.nid ";
  112. $from .= " INNER JOIN [chado_" . $table . "] CT ON CT.$pkey_field = T.$pkey_field";
  113. }
  114. $where = " WHERE CE.record_id IS NULL ";
  115. // Handle records that are mapped to property tables.
  116. if ($type_linker_table and $type_column and $type_value) {
  117. $propschema = chado_get_schema($type_linker_table);
  118. $fkeys = $propschema['foreign keys'][$table]['columns'];
  119. foreach ($fkeys as $leftkey => $rightkey) {
  120. if ($rightkey == $pkey_field) {
  121. $from .= " INNER JOIN {" . $type_linker_table . "} LT ON T.$pkey_field = LT.$leftkey ";
  122. }
  123. }
  124. $where .= "AND LT.$type_column = :cvterm_id and LT.value = :prop_value";
  125. $args[':cvterm_id'] = $cvterm_id;
  126. $args[':prop_value'] = $type_value;
  127. }
  128. // Handle records that are mapped to cvterm linking tables.
  129. if ($type_linker_table and $type_column and !$type_value) {
  130. $cvtschema = chado_get_schema($type_linker_table);
  131. $fkeys = $cvtschema['foreign keys'][$table]['columns'];
  132. foreach ($fkeys as $leftkey => $rightkey) {
  133. if ($rightkey == $pkey_field) {
  134. $from .= " INNER JOIN {" . $type_linker_table . "} LT ON T.$pkey_field = LT.$leftkey ";
  135. }
  136. }
  137. $where .= "AND LT.$type_column = :cvterm_id";
  138. $args[':cvterm_id'] = $cvterm_id;
  139. }
  140. // Handle records that are mapped via a type_id column in the base table.
  141. if (!$type_linker_table and $type_column) {
  142. $where .= "AND T.$type_column = :cvterm_id";
  143. $args[':cvterm_id'] = $cvterm_id;
  144. }
  145. // Handle the case where records are in the cvterm table and mapped via a single
  146. // vocab. Here we use the type_value for the cv_id.
  147. if ($table == 'cvterm' and $type_value) {
  148. $where .= "AND T.cv_id = :cv_id";
  149. $args[':cv_id'] = $type_value;
  150. }
  151. // Handle the case where records are in the cvterm table but we want to
  152. // use all of the child terms.
  153. if ($table == 'cvterm' and !$type_value) {
  154. $where .= "AND T.cvterm_id IN (
  155. SELECT CVTP.subject_id
  156. FROM {cvtermpath} CVTP
  157. WHERE CVTP.object_id = :cvterm_id)
  158. ";
  159. $args[':cvterm_id'] = $cvterm_id;
  160. }
  161. // Now add in any additional filters
  162. $fields = field_info_field_map();
  163. foreach ($fields as $field_name => $details) {
  164. if (array_key_exists('TripalEntity', $details['bundles']) and
  165. in_array($bundle_name, $details['bundles']['TripalEntity']) and
  166. in_array($field_name, array_keys($filters))){
  167. $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
  168. $chado_table = $instance['settings']['chado_table'];
  169. $chado_column = $instance['settings']['chado_column'];
  170. if ($chado_table == $table) {
  171. $where .= " AND T.$chado_column = :$field_name";
  172. $args[":$field_name"] = $filters[$field_name];
  173. }
  174. }
  175. }
  176. // @performance remove after development:0.060441970825195s
  177. // First get the count
  178. // @performance optimize, estimate or remove this. It's only used for reporting progress on the command-line.
  179. $sql = "SELECT count(*) as num_records " . $from . $where;
  180. $result = chado_query($sql, $args);
  181. $count = $result->fetchField();
  182. print "\nThere are $count records to publish.\n";
  183. // @performance remove after development:0.25212502479553s
  184. // @performance print 'Count amount to do :' . (microtime(true) - $started_at) . "s.\n";
  185. print "\nNOTE: publishing records is performed using database transactions. If the job fails\n" .
  186. "or is terminated prematurely then the current set of $chunk_size is rolled back with\n" .
  187. "no changes to the database. Simply re-run the publishing job to publish any remaining\n".
  188. "content after fixing the issue that caused the job to fail.\n\n" .
  189. "Also, the following progress only updates every $chunk_size records.\n";
  190. // Perform the query.
  191. $sql = $select . $from . $where . ' LIMIT '.$chunk_size;
  192. $more_records_to_publish = TRUE;
  193. $total_published = 0;
  194. while ($more_records_to_publish) {
  195. // @performance remove after development:0.43729090690613s
  196. // @performance limiting this query DRASTICALLY decreases query execution time: 0.26s
  197. // @performance print 'Perform Query :' . (microtime(true) - $started_at) . "s.\n\n";
  198. $records = chado_query($sql, $args);
  199. // Update the job status every chunk start.
  200. // Because this is outside of hte transaction, we can update the admin through the jobs UI.
  201. $complete = ($total_published / $count) * 33.33333333;
  202. if ($report_progress) { $job->setProgress(intval($complete * 3)); }
  203. if ($total_published === 0) {
  204. printf("%d of %d records. (%0.2f%%) Memory: %s bytes.\r",
  205. $i, $count, 0, number_format(memory_get_usage()), 0);
  206. }
  207. else {
  208. printf("%d of %d records. (%0.2f%%) Memory: %s bytes; Current run time: %s minutes.\r",
  209. $total_published, $count, $complete * 3, number_format(memory_get_usage()), number_format((microtime(true) - $started_at)/60, 2));
  210. }
  211. // @performance evaluate this transaction. Long running transactions can have serious
  212. // performance issues in PostgreSQL. One option is to move the transaction within the
  213. // loop so that each one is not very long but then we end up with more overhead creating
  214. // transactions. A better albeit more complicated approach might be to break the job into
  215. // chunks where each one is a single transaction.
  216. $transaction = db_transaction();
  217. try {
  218. $i = 0;
  219. while($record = $records->fetchObject()) {
  220. // @performance remove after development
  221. // print 'Start current entity :' . (microtime(true) - $started_at) . "s.\n";
  222. // First save the tripal_entity record.
  223. // @performace This is likely a bottleneck. Too bad we can't create
  224. // multiple entities at once... sort of like the copy method.
  225. $record_id = $record->record_id;
  226. $ec = entity_get_controller('TripalEntity');
  227. $entity = $ec->create(array(
  228. 'bundle' => $bundle_name,
  229. 'term_id' => $bundle->term_id,
  230. // Add in the Chado details for when the hook_entity_create()
  231. // is called and our tripal_chado_entity_create() implementation
  232. // can deal with it.
  233. // @performance maybe there is something we can easily do here?
  234. 'chado_record' => chado_generate_var($table, array($pkey_field => $record_id)),
  235. 'chado_record_id' => $record_id,
  236. 'publish' => TRUE,
  237. ));
  238. $entity = $entity->save();
  239. if (!$entity) {
  240. throw new Exception('Could not create entity.');
  241. }
  242. // @performance remove after development: this takes 0.2-0.3s.
  243. //print 'Create entity itself :' . (microtime(true) - $started_at) . "s.\n";
  244. // Next save the chado entity record.
  245. $entity_record = array(
  246. 'entity_id' => $entity->id,
  247. 'record_id' => $record_id,
  248. );
  249. // For the Tv2 to Tv3 migration we want to add the nid to the
  250. // entity so we can associate the node with the entity.
  251. if (property_exists($record, 'nid')) {
  252. $entity_record['nid'] = $record->nid;
  253. }
  254. $result = db_insert($chado_entity_table)
  255. ->fields($entity_record)
  256. ->execute();
  257. if(!$result){
  258. throw new Exception('Could not create mapping of entity to Chado record.');
  259. }
  260. // @performance remove after development: this takes <0.001s.
  261. // print 'Relate back to chado :' . (microtime(true) - $started_at) . "s.\n";
  262. $i++;
  263. $total_published++;
  264. }
  265. }
  266. catch (Exception $e) {
  267. $transaction->rollback();
  268. $error = $e->getMessage();
  269. tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not publish record: @error", array('@error' => $error));
  270. drupal_set_message('Failed publishing record. See recent logs for more details.', 'error');
  271. return FALSE;
  272. }
  273. // If we get through the loop and haven't completed 100 records, then we're done!
  274. if ($i < $chunk_size) {
  275. $more_records_to_publish = FALSE;
  276. }
  277. // Commit our current chunk.
  278. unset($transaction);
  279. }
  280. drupal_set_message("Succesfully published $i " . $bundle->label . " record(s).");
  281. // @performance remove after development
  282. print 'Complete! Runtime:' . number_format(microtime(true) - $started_at) . " seconds.\n";
  283. return TRUE;
  284. }
  285. /**
  286. * Returns an array of tokens based on Tripal Entity Fields.
  287. *
  288. * @param $base_table
  289. * The name of a base table in Chado.
  290. * @return
  291. * An array of tokens where the key is the machine_name of the token.
  292. *
  293. * @ingroup tripal_chado_api
  294. */
  295. function chado_get_tokens($base_table) {
  296. $tokens = array();
  297. $table_descrip = chado_get_schema($base_table);
  298. foreach ($table_descrip['fields'] as $field_name => $field_details) {
  299. $token = '[' . $base_table . '.' . $field_name . ']';
  300. $location = implode(' > ',array($base_table, $field_name));
  301. $tokens[$token] = array(
  302. 'name' => ucwords(str_replace('_',' ',$base_table)) . ': ' . ucwords(str_replace('_',' ',$field_name)),
  303. 'table' => $base_table,
  304. 'field' => $field_name,
  305. 'token' => $token,
  306. 'description' => array_key_exists('description', $field_details) ? $field_details['description'] : '',
  307. 'location' => $location
  308. );
  309. if (!array_key_exists('description', $field_details) or preg_match('/TODO/',$field_details['description'])) {
  310. $tokens[$token]['description'] = 'The '.$field_name.' field of the '.$base_table.' table.';
  311. }
  312. }
  313. // RECURSION:
  314. // Follow the foreign key relationships recursively
  315. if (array_key_exists('foreign keys', $table_descrip)) {
  316. foreach ($table_descrip['foreign keys'] as $table => $details) {
  317. foreach ($details['columns'] as $left_field => $right_field) {
  318. $sub_token_prefix = $base_table . '.' . $left_field;
  319. $sub_location_prefix = implode(' > ',array($base_table, $left_field));
  320. $sub_tokens = chado_get_tokens($table);
  321. if (is_array($sub_tokens)) {
  322. $tokens = array_merge($tokens, $sub_tokens);
  323. }
  324. }
  325. }
  326. }
  327. return $tokens;
  328. }
  329. /**
  330. * Replace all Chado Tokens in a given string.
  331. *
  332. * NOTE: If there is no value for a token then the token is removed.
  333. *
  334. * @param string $string
  335. * The string containing tokens.
  336. * @param $record
  337. * A Chado record as generated by chado_generate_var()
  338. *
  339. * @return
  340. * The string will all tokens replaced with values.
  341. *
  342. * @ingroup tripal_chado_api
  343. */
  344. function chado_replace_tokens($string, $record) {
  345. // Get the list of tokens
  346. $tokens = chado_get_tokens($record->tablename);
  347. // Determine which tokens were used in the format string
  348. if (preg_match_all('/\[[^]]+\]/', $string, $used_tokens)) {
  349. // Get the value for each token used
  350. foreach ($used_tokens[0] as $token) {
  351. $token_info = $tokens[$token];
  352. if (!empty($token_info)) {
  353. $table = $token_info['table'];
  354. $var = $record;
  355. $value = '';
  356. // Iterate through each portion of the location string. An example string
  357. // might be: stock > type_id > name.
  358. $location = explode('>', $token_info['location']);
  359. foreach ($location as $index) {
  360. $index = trim($index);
  361. // if $var is an object then it is the $node object or a table
  362. // that has been expanded.
  363. if (is_object($var)) {
  364. // check to see if the index is a member of the object. If so,
  365. // then reset the $var to this value.
  366. if (property_exists($var, $index)) {
  367. $value = $var->$index;
  368. }
  369. }
  370. // if the $var is an array then there are multiple instances of the same
  371. // table in a FK relationship (e.g. relationship tables)
  372. elseif (is_array($var)) {
  373. $value = $var[$index];
  374. }
  375. else {
  376. tripal_report_error('tripal_chado', TRIPAL_WARNING,
  377. 'Tokens: Unable to determine the value of %token. Things went awry when trying ' .
  378. 'to access \'%index\' for the following: \'%var\'.',
  379. array('%token' => $token, '%index' => $index, '%var' => print_r($var,TRUE))
  380. );
  381. }
  382. }
  383. $string = str_replace($token, $value, $string);
  384. }
  385. }
  386. }
  387. return $string;
  388. }