tripal_bulk_loader.loader.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Add Loader Job Form
  4. *
  5. * This form is meant to be included on the node page to allow users to submit/re-submit
  6. * loading jobs
  7. */
  8. function tripal_bulk_loader_add_loader_job_form ($form_state, $node) {
  9. $form = array();
  10. $form['nid'] = array(
  11. '#type' => 'hidden',
  12. '#value' => $node->nid,
  13. );
  14. $form['file'] = array(
  15. '#type' => 'hidden',
  16. '#value' => $node->file
  17. );
  18. $form['submit'] = array(
  19. '#type' => 'submit',
  20. '#value' => 'Submit Job'
  21. );
  22. return $form;
  23. }
  24. /**
  25. * Add Loader Job Form (Submit)
  26. */
  27. function tripal_bulk_loader_add_loader_job_form_submit ($form, $form_state) {
  28. global $user;
  29. if (preg_match('/Submit Job/', $form_state['values']['op'])) {
  30. //Submit Tripal Job
  31. $job_args[1] = $form_state['values']['nid'];
  32. if (is_readable($form_state['values']['file'])) {
  33. $fname = basename($form_state['values']['file']);
  34. tripal_add_job("Bulk Loading Job: $fname",'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  35. } else {
  36. drupal_set_message("Can not open ".$form_state['values']['file'].". Job not scheduled.");
  37. }
  38. }
  39. }
  40. /**
  41. * Tripal Bulk Loader
  42. *
  43. * This is the function that's run by tripal_launch_jobs to bulk load chado data.
  44. * @param $nid
  45. * The Node ID of the bulk loading job node to be loaded. All other needed data is expected to be
  46. * in the node (ie: template ID and file)
  47. *
  48. * Note: Instead of returning a value this function updates the tripal_bulk_loader.status and
  49. * Enters errors into tripal_bulk_loader_errors if they are encountered.
  50. */
  51. function tripal_bulk_loader_load_data($nid) {
  52. $node = node_load($nid);
  53. print "Template: ".$node->template->name." (".$node->template_id.")\n";
  54. print "File: ".$node->file."\n";
  55. // get a default values array to be passed into tripal_core_chado_insert
  56. // and get a mapping between table.field and spreadsheet column
  57. $default_values_array = array();
  58. $field2column = array();
  59. foreach ($node->template->template_array as $table => $table_array) {
  60. if (is_array($table_array)) {
  61. foreach ($table_array['field'] as $field_array) {
  62. if (preg_match('/table field/', $field_array['type'])) {
  63. $default_values_array[$table][$field_array['field']] = '';
  64. $field2column[$table][$field_array['field']] = $field_array['spreadsheet column'];
  65. } elseif (preg_match('/constant/', $field_array['type'])) {
  66. $default_values_array[$table][$field_array['field']] = $field_array['constant value'];
  67. } else {
  68. print 'WARNING: Unsupported type: '. $field_array['type'] . ' for ' . $table . '.' . $field_array['field']."!\n";
  69. }
  70. }
  71. }
  72. }
  73. //print "\nDefault Values Array: ".print_r($default_values_array, TRUE)."\n";
  74. //print "\nField to Column Mapping: ".print_r($field2column, TRUE)."\n";
  75. $file_handle = fopen($node->file, 'r');
  76. if (preg_match('/(t|true|1)/', $node->file_has_header)) { fgets($file_handle, 4096); }
  77. while (!feof($file_handle)) {
  78. $line = array();
  79. $raw_line = fgets($file_handle, 4096);
  80. $line = preg_split("/\t/", $raw_line);
  81. $values = $default_values_array;
  82. foreach ($values as $table => $table_array) {
  83. foreach ($table_array as $field => $value) {
  84. $column = $field2column[$table][$field] - 1;
  85. if ($line[$column]) {
  86. $values[$table][$field] = $line[$column];
  87. }
  88. }
  89. $has_record = tripal_core_chado_select($table, array_keys($values[$table]), $values[$table], array('has_record' => TRUE));
  90. if ($has_record) {
  91. $values_string = array();
  92. foreach ($values[$table] as $k => $v) {
  93. if (strlen($v) > 20) { $v = substr($v,0,20) . '...'; }
  94. $values_string[] = $k.' => '.$v;
  95. }
  96. print "\tWARNING: Record already exists in $table where ".implode(', ',$values_string).".\n";
  97. } else {
  98. $success = tripal_core_chado_insert($table, $values[$table]);
  99. if (!$success) {
  100. print "ERROR: Unable to insert the following record into $table: ".print_r($values[$table], TRUE)."\n";
  101. }
  102. }
  103. } //end of tables in $values
  104. } //end of file
  105. }