function tripal_feature_sync_feature

1.x tripal_feature.sync_features.inc tripal_feature_sync_feature($feature_id)

Related topics

2 calls to tripal_feature_sync_feature()
drush_tripal_feature_sync in tripal_feature/tripal_feature.drush.inc
tripal_feature.sync_features.inc in tripal_feature/includes/tripal_feature.sync_features.inc
@todo Add file header description

File

tripal_feature/includes/tripal_feature.sync_features.inc, line 409
@todo Add file header description

Code

function tripal_feature_sync_feature($feature_id) {
  //print "\tSyncing feature $feature_id\n";

  global $user;
  $create_node = 1; // set to 0 if the node exists and we just sync and not create

  // get the accession prefix
  $aprefix = variable_get('chado_feature_accession_prefix', 'FID');

  // if we don't have a feature_id then return
  if (!$feature_id) {
    drupal_set_message(t("Please provide a feature_id to sync"));
    return '';
  }

  // get information about this feature
  $fsql = "SELECT F.feature_id, F.name, F.uniquename,O.genus, " .
    "    O.species,CVT.name as cvname,F.residues,F.organism_id " .
    "FROM {FEATURE} F " .
    "  INNER JOIN {Cvterm} CVT ON F.type_id = CVT.cvterm_id " .
    "  INNER JOIN {Organism} O ON F.organism_id = O.organism_ID " .
    "WHERE F.feature_id = %d";
  $feature = db_fetch_object(chado_query($fsql, $feature_id));

  // get the synonyms for this feature
  $synsql = "SELECT S.name " .
    "FROM {feature_synonym} FS " .
    "  INNER JOIN {synonym} S on FS.synonym_id = S.synonym_id " .
    "WHERE FS.feature_id = %d";
  $synonyms = chado_query($synsql, $feature_id);

  // now add these synonyms to the feature object as a single string
  $synstring = '';
  while ($synonym = db_fetch_object($synonyms)) {
    $synstring .= "$synonym->name\n";
  }
  $feature->synonyms = $synstring;

  // check to make sure that we don't have any nodes with this feature name as a title
  // but without a corresponding entry in the chado_feature table if so then we want to
  // clean up that node.  (If a node is found we don't know if it belongs to our feature or
  // not since features can have the same name/title.)
  $tsql = "SELECT * FROM {node} N " .
    "WHERE title = '%s'";
  $cnsql = "SELECT * FROM {chado_feature} " .
    "WHERE nid = %d";
  $nodes = db_query($tsql, $feature->name);
  // cycle through all nodes that may have this title
  while ($node = db_fetch_object($nodes)) {
    $feature_nid = db_fetch_object(db_query($cnsql, $node->nid));
    if (!$feature_nid) {
      drupal_set_message(t("%feature_id: A node is present but the chado_feature entry is missing... correcting", array('%feature_id' => $feature_id)));
      node_delete($node->nid);
    }
  }

  // check if this feature already exists in the chado_feature table.
  // if we have a chado feature, we want to check to see if we have a node
  $cfsql = "SELECT * FROM {chado_feature} " .
    "WHERE feature_id = %d";
  // @coder-ignore: don't need to use db_rewrite_sql() since need all nodes regardless of access control
  $nsql = "SELECT * FROM {node} N " .
    "WHERE nid = %d";
  $chado_feature = db_fetch_object(db_query($cfsql, $feature->feature_id));
  if ($chado_feature) {
    drupal_set_message(t("%feature_id: A chado_feature entry exists", array('%feature_id' => $feature_id)));
    $node = db_fetch_object(db_query($nsql, $chado_feature->nid));
    if (!$node) {
      // if we have a chado_feature but not a node then we have a problem and
      // need to cleanup
      drupal_set_message(t("%feature_id: The node is missing, but has a chado_feature entry... correcting", array('%feature_id' => $feature_id)));
      $df_sql = "DELETE FROM {chado_feature} WHERE feature_id = %d";
      db_query($df_sql, $feature_id);
    }
    else {
      drupal_set_message(t("%feature_id: A corresponding node exists", array('%feature_id' => $feature_id)));
      $create_node = 0;
    }
  }

  // if we've encountered an error then just return.
  if ($error_msg = db_error()) {
    //print "$error_msg\n";
    return '';
  }

  // if a drupal node does not exist for this feature then we want to
  // create one.  Note that the node_save call in this block
  // will call the hook_submit function which
  if ($create_node) {
    // get the organism for this feature
    $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
    $organism = db_fetch_object(chado_query($sql, $feature->organism_id));

    drupal_set_message(t("%feature_id: Creating node $feature->name", array('%feature_id' => $feature_id)));
    $new_node = new stdClass();
    $new_node->type = 'chado_feature';
    $new_node->uid = $user->uid;
    $new_node->title = "$feature->name, $feature->uniquename ($feature->cvname) $organism->genus $organism->species";
    $new_node->fname = "$feature->name";
    $new_node->uniquename = "$feature->uniquename";
    $new_node->feature_id = $feature->feature_id;
    $new_node->residues = $feature->residues;
    $new_node->organism_id = $feature->organism_id;
    $new_node->feature_type = $feature->cvname;
    $new_node->synonyms = $feature->synonyms;

    // validate the node and if okay then submit
    node_validate($new_node);
    if ($errors = form_get_errors()) {
      print "Error encountered validating new node. Cannot sync\n";
      foreach ($errors as $key => $msg) {
        watchdog('trp-fsync', "%msg", array('%msg' => $msg), 'error');
      }
      exit;
    }
    else {
      $node = node_submit($new_node);
      node_save($node);
    }
  }
  else {
    $node = $chado_feature;
  }


  // set the taxonomy for this node
  drupal_set_message(t("%feature_id ($node->nid): setting taxonomy", array('%feature_id' => $feature_id)));
  tripal_feature_set_taxonomy($node, $feature_id);


  return '';
}