tripal_chado.migrate.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /**
  3. * Implements hook_form()
  4. *
  5. * Provide a form to select Tripal v2 content types for migration
  6. *
  7. * @param $form
  8. * @param $form_state
  9. */
  10. function tripal_chado_migrate_form($form, &$form_state) {
  11. $tv2_content_type = 'all';
  12. if (array_key_exists('values', $form_state)) {
  13. $tv2_content_type = $form_state['values']['tv2_content_type'];
  14. }
  15. $options = tripal_chado_get_tripal_v2_content_type_options(TRUE);
  16. $form['tv2_content_type'] = array(
  17. '#type' => 'select',
  18. '#title' => 'Tripal v2 Content Type',
  19. '#description' => t('Select the Tripal v2 content type to migrate.'),
  20. '#options' => $options,
  21. '#default_value' => $tv2_content_type,
  22. '#ajax' => array(
  23. 'callback' => "tripal_chado_migrate_form_ajax_callback",
  24. 'wrapper' => "tripal-chado-migrate-form",
  25. 'effect' => 'fade',
  26. 'method' => 'replace'
  27. ),
  28. );
  29. // Add a review button that allows reviewing migratable content types
  30. if ($tv2_content_type != 'all') {
  31. $table = str_replace('chado_', '', $tv2_content_type);
  32. $schema = chado_get_schema($table);
  33. $pkey = $schema['primary key'][0];
  34. $fkeys = $schema['foreign keys'];
  35. $form['tv3_content_type'] = array(
  36. '#type' => 'fieldset',
  37. '#title' => 'Tripal v3 Content Type',
  38. '#description' => "Click the 'Get Tripal v3 Types' button to retrieve a list of Tripal v3
  39. content types to which this Tripal v2 type can be converted. This may take a while
  40. depending on the size of your database. The number of items to be converted is
  41. shown beside the type."
  42. );
  43. $form['tv3_content_type']['get_v3_type_btn'] = array(
  44. '#type' => 'button',
  45. '#name' => 'get_v3_type_btn',
  46. '#value' => "Get Tripal v3 Types",
  47. '#ajax' => array(
  48. 'callback' => "tripal_chado_migrate_form_ajax_callback",
  49. 'wrapper' => "tripal-chado-migrate-form",
  50. 'effect' => 'fade',
  51. 'method' => 'replace'
  52. ),
  53. );
  54. if ($form_state['clicked_button']['#name'] == 'get_v3_type_btn') {
  55. // Migrate all
  56. $form['tv3_content_type']['tv3_migrate_all'] = array(
  57. '#type' => 'checkbox',
  58. '#title' => 'Migrate All'
  59. );
  60. // Migrate selection only
  61. if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
  62. // Get all Tripal v2 node types from the chad_* linking table
  63. $sql =
  64. "SELECT V.name AS type, X.accession, db.name AS namespace , count(*) AS num
  65. FROM chado.$table T
  66. INNER JOIN $tv2_content_type CT ON T.$pkey = CT.$pkey
  67. INNER JOIN chado.cvterm V ON V.cvterm_id = T.type_id
  68. INNER JOIN chado.dbxref X ON X.dbxref_id = V.dbxref_id
  69. INNER JOIN chado.db ON db.db_id = X.db_id
  70. GROUP BY V.name, X.accession, db.name";
  71. $tv3_content_types = db_query($sql);
  72. while($tv3_content_type = $tv3_content_types->fetchObject()) {
  73. $form['tv3_content_type']['tv3_content_type--' . $tv3_content_type->namespace .
  74. '--' . $tv3_content_type->accession . '--' . $tv3_content_type->type] = array(
  75. '#type' => 'checkbox',
  76. '#title' => $tv3_content_type->type . ' (' . $tv3_content_type->num . ')',
  77. );
  78. }
  79. }
  80. else if ($table == 'organism') {
  81. $sql =
  82. "SELECT count(*)
  83. FROM chado.organism O
  84. INNER JOIN chado_organism CO ON O.organism_id = CO.organism_id";
  85. $org_count = db_query($sql)->fetchField();
  86. $form['tv3_content_type']['tv3_content_type--local--organism--organism'] = array(
  87. '#type' => 'checkbox',
  88. '#title' => 'Organism (' . $org_count . ')',
  89. );
  90. }
  91. else if ($table == 'analysis') {
  92. $sql =
  93. "SELECT count(*)
  94. FROM chado.analysis A
  95. INNER JOIN chado_analysis CA ON A.analysis_id = CA.analysis_id";
  96. $ana_count = db_query($sql)->fetchField();
  97. $form['tv3_content_type']['tv3_content_type--local--analysis--analysis'] = array(
  98. '#type' => 'checkbox',
  99. '#title' => 'Analysis (' . $ana_count . ')',
  100. );
  101. }
  102. }
  103. }
  104. // Submit button
  105. if ($tv2_content_type == 'all' || key_exists('tv3_migrate_all', $form['tv3_content_type'])) {
  106. $form['migrate_btn'] = array(
  107. '#type' => 'submit',
  108. '#name' => 'migrate_btn',
  109. '#value' => "Migrate $options[$tv2_content_type]",
  110. );
  111. }
  112. $form['#prefix'] = '<div id="tripal-chado-migrate-form">';
  113. $form['#suffix'] = '</div>';
  114. return $form;
  115. }
  116. /**
  117. * Implements hook_validate()
  118. *
  119. * @param $form
  120. * @param $form_state
  121. */
  122. function tripal_chado_migrate_form_validate($form, &$form_state) {
  123. }
  124. /**
  125. * Implements hook_submit()
  126. *
  127. * By submiting the form, a Tripal job to migrate Tripal v2 content is submitted
  128. *
  129. * @param $form
  130. * @param $form_state
  131. */
  132. function tripal_chado_migrate_form_submit($form, &$form_state) {
  133. if ($form_state['clicked_button']['#name'] == 'migrate_btn') {
  134. global $user;
  135. $values = $form_state['values'];
  136. $tv2_content_type = $form_state['values']['tv2_content_type'];
  137. $tv3_content_type = array();
  138. foreach ($values AS $key => $value) {
  139. if ($tv2_content_type != 'all') {
  140. if (preg_match('/^tv3_content_type--(.+)--(.+)--(.+)/', $key, $matches) &&
  141. ($value == 1 || $values['tv3_migrate_all'] == 1)) {
  142. $namespace = $matches[1];
  143. $accession = $matches[2];
  144. $type = $matches[3];
  145. $tv3_content_type [] = array(
  146. 'namespace' => $namespace,
  147. 'accession' => $accession,
  148. 'term_name' => $type
  149. );
  150. }
  151. }
  152. }
  153. // Submit a job to migrate content
  154. global $user;
  155. $args = array(
  156. array(
  157. 'tv2_content_type' => $tv2_content_type,
  158. 'tv3_content_type' => $tv3_content_type
  159. )
  160. );
  161. $includes = array(
  162. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.migrate'),
  163. );
  164. if ($tv2_content_type == 'all' || count($tv3_content_type) != 0) {
  165. return tripal_add_job("Migrate $tv2_content_type Tripal v2 content.",
  166. 'tripal_chado', 'tripal_chado_migrate_records', $args, $user->uid, 10, $includes);
  167. }
  168. else {
  169. return drupal_set_message('Nothing to do. No data for migration.');
  170. }
  171. }
  172. }
  173. /**
  174. * Ajax call back that returns the entire form
  175. *
  176. * The callback is triggered by ajax elements on the form which leads to the update of
  177. * entire form according to the values set on the form
  178. *
  179. * @param $form
  180. * @param $form_state
  181. * @return $form
  182. */
  183. function tripal_chado_migrate_form_ajax_callback($form, $form_state) {
  184. return $form;
  185. }
  186. /**
  187. * Get available Tripal v2 content types
  188. *
  189. * @param boolean $all_option
  190. * Include an 'all' option in the returned array
  191. * @return string[]
  192. * Return a string array keyed by the node type
  193. */
  194. function tripal_chado_get_tripal_v2_content_type_options($all_option = FALSE) {
  195. // Get all available Tripal v2 chado tables
  196. $sql =
  197. "SELECT table_name
  198. FROM information_schema.tables
  199. WHERE table_schema = 'public' AND table_name LIKE 'chado_%'";
  200. $result = db_query($sql);
  201. $tables = array();
  202. while ($field = $result->fetchField()) {
  203. $count = db_query("SELECT count(*) FROM $field")->fetchField();
  204. if ($count != 0) {
  205. array_push($tables, $field);
  206. }
  207. }
  208. // List all available Tripal v2 content types
  209. $result = db_select('node_type', 'nt')
  210. ->fields('nt', array('type', 'name', 'description'))
  211. ->condition('type', 'chado_%', 'LIKE')
  212. ->execute();
  213. $options = array();
  214. if ($all_option) {
  215. $options['all'] = 'All';
  216. }
  217. while ($obj = $result->fetchObject()) {
  218. if (in_array($obj->type, $tables)) {
  219. $options[$obj->type] = $obj->name;
  220. }
  221. }
  222. return $options;
  223. }
  224. /**
  225. * Tripal job callback to migrate Tripal v2 content into Tripal v3 content
  226. *
  227. * @param $migration
  228. * @param $job_id
  229. */
  230. function tripal_chado_migrate_records($migration, $job_id = NULL) {
  231. $tv2_content_type = $migration['tv2_content_type'];
  232. $tv3_content_type = $migration['tv3_content_type'];
  233. // If tv2_content_type is 'all', migrate all existing Tripal v2 content
  234. if ($tv2_content_type == 'all') {
  235. print "Migrating all Tripal v2 content...\n";
  236. tripal_chado_migrate_all_types();
  237. }
  238. // Otherwise, migrate only selected Tripal v2 content
  239. else {
  240. print "Migrating selected Tripal v2 content...\n";
  241. tripal_chado_migrate_selected_types($tv3_content_type);
  242. }
  243. }
  244. /**
  245. * Migrate all Tripal v2 content types
  246. */
  247. function tripal_chado_migrate_all_types() {
  248. // Get all available Tripal v2 content types
  249. $tv2_content_types = tripal_chado_get_tripal_v2_content_type_options();
  250. $types = array();
  251. foreach($tv2_content_types AS $tv2_content_type => $value) {
  252. $table = str_replace('chado_', '', $tv2_content_type);
  253. $schema = chado_get_schema($table);
  254. $pkey = $schema['primary key'][0];
  255. $fkeys = $schema['foreign keys'];
  256. if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
  257. // Get all Tripal v2 node types from the chad_* linking table
  258. $sql = "
  259. SELECT V.name AS type, X.accession, db.name AS namespace
  260. FROM chado.$table T
  261. INNER JOIN $tv2_content_type CT ON T.$pkey = CT.$pkey
  262. INNER JOIN chado.cvterm V ON V.cvterm_id = T.type_id
  263. INNER JOIN chado.dbxref X ON X.dbxref_id = V.dbxref_id
  264. INNER JOIN chado.db ON db.db_id = X.db_id
  265. GROUP BY V.name, X.accession, db.name
  266. ";
  267. $tv3_content_types = db_query($sql);
  268. while($tv3_content_type = $tv3_content_types->fetchObject()) {
  269. array_push($types, array(
  270. 'namespace' => $tv3_content_type->namespace,
  271. 'accession' => $tv3_content_type->accession,
  272. 'term_name' => $tv3_content_type->type
  273. ));
  274. }
  275. }
  276. else if ($table == 'organism') {
  277. array_push($types, array(
  278. 'namespace' => 'local',
  279. 'accession' => 'organism',
  280. 'term_name' => 'organism'
  281. ));
  282. }
  283. else if ($table == 'analysis') {
  284. array_push($types, array(
  285. 'namespace' => 'local',
  286. 'accession' => 'analysis',
  287. 'term_name' => 'analysis'
  288. ));
  289. }
  290. }
  291. tripal_chado_migrate_selected_types($types);
  292. }
  293. /**
  294. * Migrate only selected Tripal v2 content types
  295. *
  296. * @param unknown $tv3_content_type
  297. */
  298. function tripal_chado_migrate_selected_types($tv3_content_types) {
  299. foreach ($tv3_content_types AS $tv3_content_type) {
  300. // Check if the term already exists
  301. $term = tripal_load_term_entity($tv3_content_type);
  302. // If term doesn't exist, create a new bundle for this term
  303. if (!$term) {
  304. print("Creating bundle for term '" . $tv3_content_type['term_name'] . "'...\n");
  305. $success = tripal_create_bundle($tv3_content_type['namespace'], $tv3_content_type['accession'], $tv3_content_type['term_name']);
  306. $term = tripal_load_term_entity($tv3_content_type);
  307. }
  308. // Create bundle name
  309. $bundle_name = 'bio-data_' . $term->id;
  310. // Publish records for the bundle
  311. tripal_chado_publish_records (array('bundle_name' => $bundle_name));
  312. }
  313. }