function chado_analysis_insert

2.x tripal_analysis.chado_node.inc chado_analysis_insert($node)
3.x tripal_analysis.chado_node.inc chado_analysis_insert($node)
1.x tripal_analysis.module chado_analysis_insert($node)

When a new chado_analysis node is created we also need to add information to our chado_analysis table. This function is called on insert of a new node of type 'chado_analysis' and inserts the necessary information.

Related topics

File

tripal_analysis/tripal_analysis.module, line 120
Contains all the main hook implementations for the tripal_analysis module

Code

function chado_analysis_insert($node) {
  global $user;

  // Create a timestamp so we can insert it into the chado database
  $time = $node->timeexecuted;
  $month = $time['month'];
  $day = $time['day'];
  $year = $time['year'];
  $timestamp = $month . '/' . $day . '/' . $year;

  // If this analysis already exists then don't recreate it in chado
  $analysis_id = $node->analysis_id;
  if ($analysis_id) {
    $values = array('analysis_id' => $node->analysis_id);
    $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
    if ($result and count($result) > 0) {
      $analysis = $result[0];
    }
  }

  // If the analysis doesn't exist then let's create it in chado.
  if (!$analysis) {
    // insert and then get the newly inserted analysis record
    $values = array(
      'name' => $node->analysisname,
      'description' => $node->description,
      'program' => $node->program,
      'programversion' => $node->programversion,
      'algorithm' => $node->algorithm,
      'sourcename' => $node->sourcename,
      'sourceversion' => $node->sourceversion,
      'sourceuri' => $node->sourceuri,
      'timeexecuted' => $timestamp
    );
    if (tripal_core_chado_insert('analysis', $values)) {
      $analysis = tripal_core_chado_select('analysis', array('*'), $values);
      $analysis_id = $analysis[0]->analysis_id;
    }
  }

  // Make sure the entry for this analysis doesn't already exist in the
  // chado_analysis table if it doesn't exist then we want to add it.
  $node_check_sql = "SELECT * FROM {chado_analysis} " .
    "WHERE analysis_id = %d";
  $node_check = db_fetch_object(db_query($node_check_sql, $analysis_id));
  if (!$node_check) {
    // next add the item to the drupal table
    $sql = "INSERT INTO {chado_analysis} (nid, vid, analysis_id) " .
      "VALUES (%d, %d, %d)";
    db_query($sql, $node->nid, $node->vid, $analysis_id);
    // Create a title for the analysis node using the unique keys so when the
    // node is saved, it will have a title
    $record = new stdClass();
    // If the analysis has a name, use it as the node title. If not, construct
    // the title using program, programversion, and sourcename
    if ($node->analysisname) {
      $record->title = $node->analysisname;
    }
    else {
      //Construct node title as "program (version)
      $record->title = "$node->program ($node->programversion)";
    }
    $record->nid = $node->nid;
    drupal_write_record('node', $record, 'nid');
    drupal_write_record('node_revisions', $record, 'nid');
  }

  // add the analysis to the node object for
  // use by other analysis modules that may be using this function
  $node->analysis = $analysis;
  $node->analysis_id = $analysis_id; // we need to set this for children

  // now add the properties
  $properties = array(); // stores all of the properties we need to add    
  // get the list of properties for easy lookup (without doing lots of database queries
  $properties_list = array();
  $sql = "
      SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
      FROM  {cvterm} CVT
        INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
      WHERE
        CV.name = 'analysis_property' AND
        NOT CVT.is_obsolete = 1
      ORDER BY CVT.name ASC
  ";
  $prop_types = chado_query($sql);
  while ($prop = db_fetch_object($prop_types)) {
    $properties_list[$prop->cvterm_id] = $prop->name;
  }

  // get the properties that should be added. Properties are in one of two forms:
  //  1) prop_value-[type id]-[index]
  //  2) new_value-[type id]-[index]
  //  3) new_id, new_value

  foreach ($node as $name => $value) {
    if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
      $type_id = $matches[1];
      $index = $matches[2];
      $name = $properties_list[$type_id];
      $properties[$name][$index] = trim($value);
    }
  }
  if ($node->new_id and $node->new_value) {
    $type_id = $node->new_id;
    $name = $properties_list[$type_id];
    $index = count($properties[$name]);
    $properties[$name][$index] = trim($node->new_value);
  }
  // now add in the properties
  foreach ($properties as $property => $elements) {
    foreach ($elements as $rank => $value) {
      $status = tripal_analysis_insert_property($analysis_id, $property, $value, FALSE, 'analysis_property');
      if (!$status) {
        drupal_set_message("Error cannot add property: $property", "error");
        watchdog('t_analysis', "Error cannot add property: %prop", 
        array('%property' => $property), WATCHDOG_ERROR);
      }
    }
  }
}