function tripal_analysis_sync_analyses

1.x tripal_analysis.admin.inc tripal_analysis_sync_analyses($analysis_id = NULL, $job_id = NULL)

Synchronize analyses from chado to drupal

Related topics

3 string references to 'tripal_analysis_sync_analyses'
drush_tripal_core_tripal_node_sync in tripal_core/tripal_core.drush.inc
Sync's chado records with drupal creating nodes for the given chado-centric module.
tripal_analysis_admin_validate in tripal_analysis/includes/tripal_analysis.admin.inc
Validate the administrative form @todo Stephen: Why is validate used rather then submit?
tripal_analysis_menu in tripal_analysis/tripal_analysis.module
Implementation of hook_menu(). Entry points and paths of the module

File

tripal_analysis/includes/tripal_analysis.admin.inc, line 398
Contains functions displaying administrative pages and forms

Code

function tripal_analysis_sync_analyses($analysis_id = NULL, $job_id = NULL) {
  global $user;
  $page_content = '';

  if (!$analysis_id) {
    $sql = "SELECT Analysis_id, name, description, program, " .
      "  programversion, algorithm, sourcename, sourceversion, sourceuri, " .
      "  timeexecuted " .
      "FROM {Analysis} ";
    $results = chado_query($sql);
  }
  else {
    $sql = "SELECT Analysis_id, name, description, program, " .
      "  programversion, algorithm, sourcename, sourceversion, sourceuri, " .
      "  timeexecuted " .
      "FROM {Analysis} " .
      "WHERE analysis_id = %d";
    $results = chado_query($sql, $analysis_id);
  }


  // We'll use the following SQL statement for checking if the analysis
  // already exists as a drupal node.
  $sql = "SELECT * FROM {chado_analysis} " .
    "WHERE analysis_id = %d";

  while ($analysis = db_fetch_object($results)) {
    print "syncing analysis ";
    print $analysis->name;
    print ", ";
    print $analysis->analysis_id;
    print "\n";

    // check if this analysis already exists in the drupal database. if it
    // does then skip this analysis and go to the next one.
    if (!db_fetch_object(db_query($sql, $analysis->analysis_id))) {

      $new_node = new stdClass();

      // try to access analysis type for this analysis
      $sql = "SELECT * FROM {analysisprop}
                    WHERE analysis_id = %d
                    AND type_id =
                        (SELECT cvterm_id from {cvterm} where name = '%s')
            ";
      $analysis_type = db_fetch_object(chado_query($sql, $analysis->analysis_id, "analysis_type"));

      // Get the type of analysis using cvterm_id
      // Current possibilities: kegg, unigene, interpro, blast
      if ($analysis_type) {

        // This is a unigene analysis
        if ($analysis_type->value == 'tripal_analysis_unigene') {
          $new_node->type = 'chado_analysis_unigene';
          // This is a blast analysis
        }
        elseif ($analysis_type->value == 'tripal_analysis_blast') {
          $new_node->type = 'chado_analysis_blast';
          // This is a interpro analysis
        }
        elseif ($analysis_type->value == 'tripal_analysis_interpro') {
          $new_node->type = 'chado_analysis_interpro';
          // This is a kegg analysis
        }
        elseif ($analysis_type->value == 'tripal_analysis_kegg') {
          $new_node->type = 'chado_analysis_kegg';
        }
        else {
          $new_node->type = 'chado_analysis';
        }
        // If it doesn't exist, this analysis is generic
      }
      else {
        $new_node->type = 'chado_analysis';
      }

      print "analysis type is $new_node->type\n";

      $new_node->uid = $user->uid;
      $new_node->analysis_id = $analysis->analysis_id;
      $new_node->analysisname = $analysis->name;
      $new_node->description = $analysis->description;
      $new_node->program = $analysis->program;
      $new_node->programversion = $analysis->programversion;
      $new_node->algorithm = $analysis->algorithm;
      $new_node->sourcename = $analysis->sourcename;
      $new_node->sourceversion = $analysis->sourceversion;
      $new_node->sourceuri = $analysis->sourceuri;
      $new_node->timeexecuted = $analysis->timeexecuted;

      // If the analysis has a name, use it as the node title. If not,
      // construct the title using program, programversion, and sourcename
      if ($new_node->analysisname) {
        $new_node->title = $new_node->analysisname;
      }
      else {
        //Construct node title as "program (version)"
        $new_node->title = "$analysis->program ($analysis->programversion)";
      }

      node_validate($new_node);

      $errors = form_get_errors();

      if ($errors) {
        print_r($errors);
      }
      else {
        // if(!form_get_errors()){
        $node = node_submit($new_node);
        node_save($node);

        if ($node->nid) {
          $page_content .= "Added $new_node->title<br />";
        }
      }
    }
    else {
      $page_content .= "Skipped $new_node->title<br />";
    }
  }
  return $page_content;
}