tripal_chado.migrate.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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] = 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'] = 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'] = 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. $tv3_content_type [] = array(
  145. 'namespace' => $namespace,
  146. 'accession' => $accession
  147. );
  148. }
  149. }
  150. }
  151. // Submit a job to migrate content
  152. global $user;
  153. $args = array(
  154. array(
  155. 'tv2_content_type' => $tv2_content_type,
  156. 'tv3_content_type' => $tv3_content_type
  157. )
  158. );
  159. $includes = array(
  160. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.migrate'),
  161. );
  162. if ($tv2_content_type == 'all' || count($tv3_content_type) != 0) {
  163. return tripal_add_job("Migrate $tv2_content_type Tripal v2 content.",
  164. 'tripal_chado', 'tripal_chado_migrate_records', $args, $user->uid, 10, $includes);
  165. }
  166. else {
  167. return drupal_set_message('Nothing to do. No data for migration.');
  168. }
  169. }
  170. }
  171. /**
  172. * Ajax call back that returns the entire form
  173. *
  174. * The callback is triggered by ajax elements on the form which leads to the update of
  175. * entire form according to the values set on the form
  176. *
  177. * @param $form
  178. * @param $form_state
  179. * @return $form
  180. */
  181. function tripal_chado_migrate_form_ajax_callback($form, $form_state) {
  182. return $form;
  183. }
  184. /**
  185. * Get available Tripal v2 content types
  186. *
  187. * @param boolean $all_option
  188. * Include an 'all' option in the returned array
  189. * @return string[]
  190. * Return a string array keyed by the node type
  191. */
  192. function tripal_chado_get_tripal_v2_content_type_options ($all_option = FALSE) {
  193. // Get all available Tripal v2 chado tables
  194. $sql =
  195. "SELECT table_name
  196. FROM information_schema.tables
  197. WHERE table_schema = 'public' AND table_name LIKE 'chado_%'";
  198. $result = db_query($sql);
  199. $tables = array();
  200. while ($field = $result->fetchField()) {
  201. $count = db_query("SELECT count(*) FROM $field")->fetchField();
  202. if ($count != 0) {
  203. array_push($tables, $field);
  204. }
  205. }
  206. // List all available Tripal v2 content types
  207. $result = db_select('node_type', 'nt')
  208. ->fields('nt', array('type', 'name', 'description'))
  209. ->condition('type', 'chado_%', 'LIKE')
  210. ->execute();
  211. $options = array();
  212. if ($all_option) {
  213. $options['all'] = 'All';
  214. }
  215. while ($obj = $result->fetchObject()) {
  216. if (in_array($obj->type, $tables)) {
  217. $options[$obj->type] = $obj->name;
  218. }
  219. }
  220. return $options;
  221. }
  222. /**
  223. * Tripal job callback to migrate Tripal v2 content into Tripal v3 content
  224. *
  225. * @param $migration
  226. * @param $job_id
  227. */
  228. function tripal_chado_migrate_records($migration, $job_id = NULL) {
  229. $tv2_content_type = $migration['tv2_content_type'];
  230. $tv3_content_type = $migration['tv3_content_type'];
  231. // If tv2_content_type is 'all', migrate all existing Tripal v2 content
  232. if ($tv2_content_type == 'all') {
  233. print "Migrating all Tripal v2 content...\n";
  234. tripal_chado_migrate_all_types ();
  235. }
  236. // Otherwise, migrate only selected Tripal v2 content
  237. else {
  238. print "Migrating selected Tripal v2 content...\n";
  239. tripal_chado_migrate_selected_types ($tv3_content_type);
  240. }
  241. }
  242. /**
  243. * Migrate all Tripal v2 content types
  244. */
  245. function tripal_chado_migrate_all_types () {
  246. // Get all available Tripal v2 content types
  247. $tv2_content_types = tripal_chado_get_tripal_v2_content_type_options();
  248. $types = array();
  249. foreach($tv2_content_types AS $tv2_content_type => $value) {
  250. $table = str_replace('chado_', '', $tv2_content_type);
  251. $schema = chado_get_schema($table);
  252. $pkey = $schema['primary key'][0];
  253. $fkeys = $schema['foreign keys'];
  254. if (key_exists('cvterm', $fkeys) && key_exists('type_id', $fkeys['cvterm']['columns'])) {
  255. // Get all Tripal v2 node types from the chad_* linking table
  256. $sql =
  257. "SELECT X.accession, db.name AS namespace
  258. FROM chado.$table T
  259. INNER JOIN $tv2_content_type CT ON T.$pkey = CT.$pkey
  260. INNER JOIN chado.cvterm V ON V.cvterm_id = T.type_id
  261. INNER JOIN chado.dbxref X ON X.dbxref_id = V.dbxref_id
  262. INNER JOIN chado.db ON db.db_id = X.db_id
  263. GROUP BY V.name, X.accession, db.name";
  264. $tv3_content_types = db_query($sql);
  265. while($tv3_content_type = $tv3_content_types->fetchObject()) {
  266. array_push($types, array(
  267. 'namespace' => $tv3_content_type->namespace,
  268. 'accession' => $tv3_content_type->accession
  269. ));
  270. }
  271. }
  272. else if ($table == 'organism') {
  273. array_push($types, array(
  274. 'namespace' => 'local',
  275. 'accession' => 'organism'
  276. ));
  277. }
  278. else if ($table == 'analysis') {
  279. array_push($types, array(
  280. 'namespace' => 'local',
  281. 'accession' => 'analysis'
  282. ));
  283. }
  284. }
  285. tripal_chado_migrate_selected_types($types);
  286. }
  287. /**
  288. * Migrate only selected Tripal v2 content types
  289. *
  290. * @param unknown $tv3_content_type
  291. */
  292. function tripal_chado_migrate_selected_types ($tv3_content_types) {
  293. print_r($tv3_content_types);
  294. }