tripal_chado.api.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /**
  3. * Retrieves an entity that matches the given table and record id.
  4. *
  5. * @param $table
  6. * The name of the Chado table.
  7. * @param $record_id
  8. * The record's primary key in the table specified by $table.
  9. *
  10. * @return
  11. * A chado_entity object.
  12. */
  13. function tripal_load_chado_entity($table, $record_id) {
  14. $entity_id = db_select('chado_entity', 'ce')
  15. ->fields('ce', array('entity_id'))
  16. ->condition('ce.record_id', $record_id)
  17. ->condition('ce.data_table', $table)
  18. ->execute()
  19. ->fetchField();
  20. if ($entity_id) {
  21. $entity = entity_load('TripalEntity', array($entity_id));
  22. return reset($entity);
  23. }
  24. return NULL;
  25. }
  26. /**
  27. * Retrieves the entity ID using a record ID.
  28. *
  29. * @param unknown $data_table
  30. * @param unknown $record_id
  31. */
  32. function tripal_get_chado_entity_id($data_table, $record_id) {
  33. return db_select('chado_entity', 'ce')
  34. ->fields('ce', array('entity_id'))
  35. ->condition('data_table', $data_table)
  36. ->condition('record_id', $record_id)
  37. ->execute()
  38. ->fetchField();
  39. }
  40. /**
  41. * Publishes content in Chado as a new TripalEntity entity.
  42. *
  43. * @param $values
  44. * A key/value associative array that supports the following keys:
  45. * - bundle_name: The name of the the TripalBundle (e.g. bio_data-12345).
  46. * @param $job_id
  47. * (Optional) The numeric job ID as provided by the Tripal jobs system. There
  48. * is no need to specify this argument if this function is being called
  49. * outside of the jobs systems.
  50. *
  51. * @return boolean
  52. * TRUE if all of the records of the given bundle type were published, and
  53. * FALSE if a failure occured.
  54. */
  55. function tripal_chado_publish_records($values, $job_id = NULL) {
  56. // Make sure we have the required options: bundle_name.
  57. if (!array_key_exists('bundle_name', $values) or !$values['bundle_name']) {
  58. tripal_report_error('tripal_chado', TRIPAL_ERROR,
  59. "Could not publish record: @error",
  60. array('@error' => 'The bundle name must be provided'));
  61. return FALSE;
  62. }
  63. // Get the incoming arguments from the $values array.
  64. $bundle_name = $values['bundle_name'];
  65. $filters = array_key_exists('filters', $values) ? $values['filters'] : array();
  66. $sync_node = array_key_exists('sync_node', $values) ? $values['sync_node'] : '';
  67. // Load the bundle entity so we can get information about which Chado
  68. // table/field this entity belongs to.
  69. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  70. if (!$bundle) {
  71. tripal_report_error('tripal_chado', TRIPAL_ERROR,
  72. "Unknown bundle. Could not publish record: @error",
  73. array('@error' => 'The bundle name must be provided'));
  74. return FALSE;
  75. }
  76. $table = $bundle->data_table;
  77. $type_column = $bundle->type_column;
  78. $type_linker_table = $bundle->type_linker_table;
  79. $cvterm_id = $bundle->type_id;
  80. // Get the table information for the Chado table.
  81. $table_schema = chado_get_schema($table);
  82. $pkey_field = $table_schema['primary key'][0];
  83. // Construct the SQL for identifying which records should be published.
  84. $args = array();
  85. $select = "SELECT $pkey_field as record_id ";
  86. $from = "
  87. FROM {" . $table . "} T
  88. LEFT JOIN public.chado_entity CE on CE.record_id = T.$pkey_field
  89. AND CE.data_table = :table
  90. ";
  91. // For migration of Tripal v2 nodes to entities we want to include the
  92. // coresponding chado linker table.
  93. if ($sync_node && db_table_exists('chado_' . $table)) {
  94. $select = "SELECT T.$pkey_field as record_id, CT.nid ";
  95. $from .= " INNER JOIN public.chado_$table CT ON CT.$pkey_field = T.$pkey_field";
  96. }
  97. $where = " WHERE CE.record_id IS NULL ";
  98. $args[':table'] = $table;
  99. // Handle bundles that use a linker property table for identifying the type
  100. // of record to publish.
  101. if ($type_linker_table and $type_column) {
  102. $propschema = chado_get_schema($type_linker_table);
  103. $fkeys = $propschema['foreign keys'][$table]['columns'];
  104. foreach ($fkeys as $leftkey => $rightkey) {
  105. if ($rightkey == $pkey_field) {
  106. $from .= " INNER JOIN {" . $type_linker_table . "} LT ON T.$pkey_field = LT.$leftkey ";
  107. }
  108. }
  109. $where .= "AND LT.$type_column = :cvterm_id";
  110. $args[':cvterm_id'] = $cvterm_id;
  111. }
  112. // If the type column is in the base table then add in the SQL for that.
  113. if (!$type_linker_table and $type_column) {
  114. $where .= "AND T.$type_column = :cvterm_id";
  115. $args[':cvterm_id'] = $cvterm_id;
  116. }
  117. // If no type column is specified then we have a problem.
  118. if ($type_linker_table and !$type_column) {
  119. tripal_report_error('tripal_chado', TRIPAL_ERROR,
  120. "Could not publish record: @error",
  121. array('@error' => 'The bundle does not properly map to Chado.'));
  122. return FALSE;
  123. }
  124. // Now add in any additional filters
  125. $fields = field_info_field_map();
  126. foreach ($fields as $field_name => $details) {
  127. if (array_key_exists('TripalEntity', $details['bundles']) and
  128. in_array($bundle_name, $details['bundles']['TripalEntity']) and
  129. in_array($field_name, array_keys($filters))){
  130. $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
  131. $chado_table = $instance['settings']['chado_table'];
  132. $chado_column = $instance['settings']['chado_column'];
  133. if ($chado_table == $table) {
  134. $where .= " AND T.$chado_column = :$field_name";
  135. $args[":$field_name"] = $filters[$field_name];
  136. }
  137. }
  138. }
  139. // First get the count
  140. $sql = "SELECT count(*) as num_records " . $from . $where;
  141. $result = chado_query($sql, $args);
  142. $count = $result->fetchField();
  143. //If this is the publication table remove one record, record_id = 1
  144. // which is the null publication.
  145. if($table == 'pub'){
  146. $count = $count - 1;
  147. }
  148. // calculate the interval for updates
  149. $interval = intval($count / 1000);
  150. if ($interval < 1) {
  151. $interval = 1;
  152. }
  153. // Perform the query.
  154. $sql = $select . $from . $where;
  155. $records = chado_query($sql, $args);
  156. //$transaction = db_transaction();
  157. print "\nNOTE: publishing records is performed using a database transaction. \n" .
  158. "If the load fails or is terminated prematurely then the entire set of \n" .
  159. "is rolled back with no changes to the database\n\n";
  160. printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, 0, number_format(memory_get_usage()));
  161. try {
  162. $i = 0;
  163. while($record = $records->fetchObject()) {
  164. if($table == 'pub' && $record->record_id == 1){
  165. // Do not create the chado entity.
  166. }
  167. else {
  168. // update the job status every interval
  169. //if ($jobid and $i % $interval == 0) {
  170. $complete = ($i / $count) * 33.33333333;
  171. //tripal_set_job_progress($jobid, intval($complete + 33.33333333));
  172. printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
  173. //}
  174. // First save the tripal_entity record.
  175. $record_id = $record->record_id;
  176. $ec = entity_get_controller('TripalEntity');
  177. $entity = $ec->create(array(
  178. 'bundle' => $bundle_name,
  179. 'term_id' => $bundle->term_id,
  180. 'chado_record' => chado_generate_var($table, array($pkey_field => $record_id)),
  181. 'chado_record_id' => $record_id,
  182. ));
  183. $entity = $entity->save();
  184. if (!$entity) {
  185. throw new Exception('Could not create entity.');
  186. }
  187. // Next save the chado_entity record.
  188. $entity_record = array(
  189. 'entity_id' => $entity->id,
  190. 'record_id' => $record_id,
  191. 'data_table' => $table,
  192. );
  193. // For the Tv2 to Tv3 migration we want to add the nid to the
  194. // entity so we can associate the node with the entity.
  195. if (property_exists($record, 'nid')) {
  196. $entity_record['nid'] = $record->nid;
  197. }
  198. $success = drupal_write_record('chado_entity', $entity_record);
  199. }
  200. $i++;
  201. }
  202. }
  203. catch (Exception $e) {
  204. $transaction->rollback();
  205. $error = $e->getMessage();
  206. tripal_report_error('tripal_chado', TRIPAL_ERROR, "Could not publish record: @error", array('@error' => $error));
  207. drupal_set_message('Failed publishing record. See recent logs for more details.', 'error');
  208. return FALSE;
  209. }
  210. drupal_set_message("Succesfully published $i " . $bundle->label . " record(s).");
  211. return TRUE;
  212. }
  213. /**
  214. * Returns an array of tokens based on Tripal Entity Fields.
  215. *
  216. * @param $base_table
  217. * The name of a base table in Chado.
  218. * @return
  219. * An array of tokens where the key is the machine_name of the token.
  220. */
  221. function tripal_get_chado_tokens($base_table) {
  222. $tokens = array();
  223. $table_descrip = chado_get_schema($base_table);
  224. foreach ($table_descrip['fields'] as $field_name => $field_details) {
  225. $token = '[' . $base_table . '.' . $field_name . ']';
  226. $location = implode(' > ',array($base_table, $field_name));
  227. $tokens[$token] = array(
  228. 'name' => ucwords(str_replace('_',' ',$base_table)) . ': ' . ucwords(str_replace('_',' ',$field_name)),
  229. 'table' => $base_table,
  230. 'field' => $field_name,
  231. 'token' => $token,
  232. 'description' => array_key_exists('description', $field_details) ? $field_details['description'] : '',
  233. 'location' => $location
  234. );
  235. if (!array_key_exists('description', $field_details) or preg_match('/TODO/',$field_details['description'])) {
  236. $tokens[$token]['description'] = 'The '.$field_name.' field of the '.$base_table.' table.';
  237. }
  238. }
  239. // RECURSION:
  240. // Follow the foreign key relationships recursively
  241. if (array_key_exists('foreign keys', $table_descrip)) {
  242. foreach ($table_descrip['foreign keys'] as $table => $details) {
  243. foreach ($details['columns'] as $left_field => $right_field) {
  244. $sub_token_prefix = $base_table . '.' . $left_field;
  245. $sub_location_prefix = implode(' > ',array($base_table, $left_field));
  246. $sub_tokens = tripal_get_chado_tokens($table);
  247. if (is_array($sub_tokens)) {
  248. $tokens = array_merge($tokens, $sub_tokens);
  249. }
  250. }
  251. }
  252. }
  253. return $tokens;
  254. }
  255. /**
  256. * Replace all Chado Tokens in a given string.
  257. *
  258. * NOTE: If there is no value for a token then the token is removed.
  259. *
  260. * @param string $string
  261. * The string containing tokens.
  262. * @param $record
  263. * A Chado record as generated by chado_generate_var()
  264. *
  265. * @return
  266. * The string will all tokens replaced with values.
  267. */
  268. function tripal_replace_chado_tokens($string, $record) {
  269. // Get the list of tokens
  270. $tokens = tripal_get_chado_tokens($record->tablename);
  271. // Determine which tokens were used in the format string
  272. if (preg_match_all('/\[[^]]+\]/', $string, $used_tokens)) {
  273. // Get the value for each token used
  274. foreach ($used_tokens[0] as $token) {
  275. $token_info = $tokens[$token];
  276. if (!empty($token_info)) {
  277. $table = $token_info['table'];
  278. $var = $record;
  279. $value = '';
  280. // Iterate through each portion of the location string. An example string
  281. // might be: stock > type_id > name.
  282. $location = explode('>', $token_info['location']);
  283. foreach ($location as $index) {
  284. $index = trim($index);
  285. // if $var is an object then it is the $node object or a table
  286. // that has been expanded.
  287. if (is_object($var)) {
  288. // check to see if the index is a member of the object. If so,
  289. // then reset the $var to this value.
  290. if (property_exists($var, $index)) {
  291. $value = $var->$index;
  292. }
  293. }
  294. // if the $var is an array then there are multiple instances of the same
  295. // table in a FK relationship (e.g. relationship tables)
  296. elseif (is_array($var)) {
  297. $value = $var[$index];
  298. }
  299. else {
  300. tripal_report_error('tripal_chado', TRIPAL_WARNING,
  301. 'Tokens: Unable to determine the value of %token. Things went awry when trying ' .
  302. 'to access \'%index\' for the following: \'%var\'.',
  303. array('%token' => $token, '%index' => $index, '%var' => print_r($var,TRUE))
  304. );
  305. }
  306. }
  307. $string = str_replace($token, $value, $string);
  308. }
  309. }
  310. }
  311. return $string;
  312. }