function tripal_stock_sync_stock
1.x tripal_stock.sync_stocks.inc | tripal_stock_sync_stock($stock_id) |
Related topics
1 call to tripal_stock_sync_stock()
- tripal_stock.sync_stocks.inc in tripal_stock/
includes/ tripal_stock.sync_stocks.inc - @todo Add file header description
File
- tripal_stock/
includes/ tripal_stock.sync_stocks.inc, line 412 - @todo Add file header description
Code
function tripal_stock_sync_stock($stock_id) {
print "\tSyncing stock $stock_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_stock_accession_prefix', 'SID');
// if we don't have a stock_id then return
if (!$stock_id) {
drupal_set_message(t("Please provide a stock_id to sync"));
return '';
}
// get information about this stock
$fsql = "SELECT S.*, O.genus, O.species,CVT.name as cvname " .
"FROM {stock} S " .
" INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id " .
" INNER JOIN {organism} O ON S.organism_id = O.organism_ID " .
"WHERE S.stock_id = %d";
$stock = db_fetch_object(chado_query($fsql, $stock_id));
/*
// get the synonyms for this stock
$synsql = "SELECT S.name ".
"FROM {stock_synonym} SS ".
" INNER JOIN {synonym} S on SS.synonym_id = S.synonym_id ".
"WHERE SS.stock_id = %d";
$synonyms = chado_query($synsql, $stock_id);
// now add these synonyms to the stock object as a single string
$synstring = '';
while ($synonym = db_fetch_object($synonyms)) {
$synstring .= "$synonym->name\n";
}
$stock->synonyms = $synstring;
*/
// check to make sure that we don't have any nodes with this stock name as a title
// but without a corresponding entry in the chado_stock 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 stock or
// not since stocks can have the same name/title.)
$tsql = "SELECT * FROM {node} N " .
"WHERE title = '%s'";
$cnsql = "SELECT * FROM {chado_stock} " .
"WHERE nid = %d";
$nodes = db_query($tsql, $stock->name);
// cycle through all nodes that may have this title
while ($node = db_fetch_object($nodes)) {
$stock_nid = db_fetch_object(db_query($cnsql, $node->nid));
if (!$stock_nid) {
drupal_set_message(t("%stock_id: A node is present but the chado_stock entry is missing... correcting", array('%stock_id' => $stock_id)));
node_delete($node->nid);
}
}
// check if this stock already exists in the chado_stock table.
// if we have a chado stock, we want to check to see if we have a node
$cfsql = "SELECT * FROM {chado_stock} " .
"WHERE stock_id = %d";
$nsql = "SELECT * FROM {node} N " .
"WHERE nid = %d";
$chado_stock = db_fetch_object(db_query($cfsql, $stock->stock_id));
if ($chado_stock) {
drupal_set_message(t("%stock_id: A chado_stock entry exists", array('%stock_id' => $stock_id)));
$node = db_fetch_object(db_query($nsql, $chado_stock->nid));
if (!$node) {
// if we have a chado_stock but not a node then we have a problem and
// need to cleanup
drupal_set_message(t("%stock_id: The node is missing, but has a chado_stock entry... correcting", array('%stock_id' => $stock_id)));
$df_sql = "DELETE FROM {chado_stock} WHERE stock_id = %d";
db_query($df_sql, $stock_id);
}
else {
drupal_set_message(t("%stock_id: A corresponding node exists", array('%stock_id' => $stock_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 stock 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 stock
$sql = "SELECT * FROM {organism} WHERE organism_id = %d";
$organism = db_fetch_object(chado_query($sql, $stock->organism_id));
drupal_set_message(t("%stock_id: Creating node $stock->name", array('%stock_id' => $stock_id)));
$new_node = new stdClass();
$new_node->type = 'chado_stock';
$new_node->uid = $user->uid;
$new_node->title = "$stock->name, $stock->uniquename ($stock->cvname) $organism->genus $organism->species";
$new_node->sname = "$stock->name";
$new_node->uniquename = "$stock->uniquename";
$new_node->type_id = $stock->type_id;
$new_node->organism_id = $stock->organism_id;
$new_node->stock_id = $stock->stock_id;
$new_node->chado_stock_exists = TRUE;
// 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: $msg\n";
foreach ($errors as $key => $msg) {
watchdog('trp-fsync', "%msg", array('%msg' => $msg), WATCHDOG_ERROR);
}
exit;
}
else {
$node = node_submit($new_node);
node_save($node);
}
}
else {
$node = $chado_stock;
}
return '';
}