|
@@ -206,9 +206,56 @@ function tripal_stock_theme() {
|
|
|
'arguments' => array('form'),
|
|
|
'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
|
|
|
),
|
|
|
+// 'tripal_stock_base' => array (
|
|
|
+// 'arguments' => array('node'=> null),
|
|
|
+// 'template' => 'tripal_stock_base',
|
|
|
+// ),
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+/*************************************************************************
|
|
|
+ * Purpose: Implement Blocks relating to stock content
|
|
|
+ */
|
|
|
+function tripal_stock_block ($op = 'list', $delta = 0, $edit=array()) {
|
|
|
+ switch($op) {
|
|
|
+ case 'list':
|
|
|
+ $blocks['base']['info'] = t('Tripal Stock Details');
|
|
|
+ $blocks['base']['cache'] = BLOCK_NO_CACHE;
|
|
|
+
|
|
|
+ return $blocks;
|
|
|
+
|
|
|
+ case 'view':
|
|
|
+ if(user_access('access chado_feature content') and arg(0) == 'node' and is_numeric(arg(1))) {
|
|
|
+ $nid = arg(1);
|
|
|
+ $node = node_load($nid);
|
|
|
+
|
|
|
+ $block = array();
|
|
|
+ switch($delta){
|
|
|
+ case 'base':
|
|
|
+ $block['subject'] = t('Stock Details');
|
|
|
+// $block['content'] = theme('tripal_stock_base',$node);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return $blocks;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*************************************************************************
|
|
|
+ * Purpose:
|
|
|
+ */
|
|
|
+function tripal_stock_nodeapi (&$node, $op, $teaser, $page) {
|
|
|
+ if ($node->type == 'chado_stock') {
|
|
|
+ switch ($op) {
|
|
|
+ case 'view':
|
|
|
+ $node->content['tripal_stock_base'] = array(
|
|
|
+// '#value' => theme('tripal_stock_base', $node),
|
|
|
+ '#weight' => 0
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
/*************************************************************************
|
|
|
* Purpose: show stocks stored in drupals chado_stock table
|
|
|
*/
|
|
@@ -327,11 +374,13 @@ function chado_stock_load($node) {
|
|
|
)
|
|
|
);
|
|
|
$results = tripal_core_chado_select('stockprop', $columns, $values);
|
|
|
- foreach ($results as $r) {
|
|
|
- $r->type = 'synonym';
|
|
|
- $node->synonyms[] = $r;
|
|
|
- }
|
|
|
-
|
|
|
+ if (!empty($results)) {
|
|
|
+ foreach ($results as $r) {
|
|
|
+ $r->type = 'synonym';
|
|
|
+ $node->synonyms[] = $r;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// Add properties for stock (not including synonyms)-------------------------------------------
|
|
|
// $node->properties is an array of objects where each object describes a single property and has a type and value
|
|
|
$columns = array('stockprop_id', 'type_id', 'value', 'rank');
|
|
@@ -403,9 +452,15 @@ function chado_stock_load($node) {
|
|
|
$node->object_relationships = array();
|
|
|
foreach ($results as $r) {
|
|
|
$columns = array('name', 'uniquename', 'description', 'type_id', 'is_obsolete', 'organism_id', 'dbxref_id');
|
|
|
+
|
|
|
$values = array('stock_id' => $r->object_id);
|
|
|
$results = tripal_core_chado_select('stock', $columns, $values);
|
|
|
|
|
|
+ // Type
|
|
|
+ $type_results = tripal_core_chado_select('cvterm', array('name'), array('cvterm_id' => $r->type_id));
|
|
|
+ $r->type = $type_results[0]->name;
|
|
|
+
|
|
|
+ // Object
|
|
|
$r->object->stock_id = $r->object_id;
|
|
|
unset($r->object_id);
|
|
|
|
|
@@ -434,6 +489,11 @@ function chado_stock_load($node) {
|
|
|
$values = array('stock_id' => $r->subject_id);
|
|
|
$results = tripal_core_chado_select('stock', $columns, $values);
|
|
|
|
|
|
+ // Type
|
|
|
+ $type_results = tripal_core_chado_select('cvterm', array('name'), array('cvterm_id' => $r->type_id));
|
|
|
+ $r->type = $type_results[0]->name;
|
|
|
+
|
|
|
+ // Subject
|
|
|
$r->subject->stock_id = $r->subject_id;
|
|
|
unset($r->subject_id);
|
|
|
|
|
@@ -441,7 +501,8 @@ function chado_stock_load($node) {
|
|
|
$r->subject->uniquename = $results[0]->uniquename;
|
|
|
$r->subject->description = $results[0]->description;
|
|
|
$r->subject->stock_type_id = $results[0]->type_id;
|
|
|
- $r->subject->organism->organism_id = $results[0]->organism_id; $r->subject->main_db_reference->dbxref_id = $results[0]->dbxref_id;
|
|
|
+ $r->subject->organism->organism_id = $results[0]->organism_id;
|
|
|
+ $r->subject->main_db_reference->dbxref_id = $results[0]->dbxref_id;
|
|
|
|
|
|
$sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
|
|
|
$subject_node = db_fetch_object(db_query($sql, $r->subject->stock_id));
|
|
@@ -666,64 +727,93 @@ function chado_stock_validate($node, &$form) {
|
|
|
*/
|
|
|
function chado_stock_insert($node) {
|
|
|
|
|
|
- $previous_db = tripal_db_set_active('chado');
|
|
|
// create dbxref
|
|
|
if ( !empty($node->accession) ) {
|
|
|
if ( !empty($node->database) ) {
|
|
|
- db_query(
|
|
|
- "INSERT INTO dbxref (db_id, accession, description) VALUES (%d, '%s', '%s')",
|
|
|
- $node->database,
|
|
|
- $node->accession,
|
|
|
- $node->db_description
|
|
|
+ $values = array(
|
|
|
+ 'db_id' => $node->database,
|
|
|
+ 'accession' => $node->accession,
|
|
|
);
|
|
|
-
|
|
|
- $dbxref = tripal_db_get_dbxref_by_accession($node->accession, $node->database);
|
|
|
+ if (!tripal_core_chado_select('dbxref',array(dbxref_id), $values)) {
|
|
|
+ $values['description'] = $node->db_description;
|
|
|
+ $values['version'] = '1';
|
|
|
+ $dbxref_status = tripal_core_chado_insert('dbxref', $values);
|
|
|
+ if (!$dbxref_status) {
|
|
|
+ drupal_set_message('Unable to add database reference to this stock.', 'warning');
|
|
|
+ watchdog('tripal_stock',
|
|
|
+ 'Insert Stock: Unable to create dbxref where values:%values',
|
|
|
+ array('%values' => print_r($values, TRUE)),
|
|
|
+ WATCHDOG_WARNING
|
|
|
+ );
|
|
|
+ }
|
|
|
+ } else { $dbxref_status = 1; }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// create stock
|
|
|
- if($dbxref->dbxref_id != 0) {
|
|
|
- db_query(
|
|
|
- "INSERT INTO stock (dbxref_id, organism_id, name, uniquename, description, type_id, is_obsolete) VALUES (%d, %d, '%s', '%s', '%s', %d, 'f')",
|
|
|
- $dbxref->dbxref_id,
|
|
|
- $node->organism_id,
|
|
|
- $node->title,
|
|
|
- $node->uniquename,
|
|
|
- $node->description,
|
|
|
- $node->type_id
|
|
|
+ if($dbxref_status) {
|
|
|
+ $values = array(
|
|
|
+ 'dbxref_id' => array(
|
|
|
+ 'db_id' => $node->database,
|
|
|
+ 'accession' => $node->accession
|
|
|
+ ),
|
|
|
+ 'organism_id' => $node->organism_id,
|
|
|
+ 'name' => $node->title,
|
|
|
+ 'uniquename' => $node->uniquename,
|
|
|
+ 'description' => $node->description,
|
|
|
+ 'type_id' => $node->type_id
|
|
|
);
|
|
|
+ $stock_status = tripal_core_chado_insert('stock', $values);
|
|
|
} else {
|
|
|
- db_query(
|
|
|
- "INSERT INTO stock (organism_id, name, uniquename, description, type_id, is_obsolete) VALUES (%d, '%s', '%s', '%s', %d, 'f')",
|
|
|
- $node->organism_id,
|
|
|
- $node->title,
|
|
|
- $node->uniquename,
|
|
|
- $node->description,
|
|
|
- $node->type_id
|
|
|
+ $values = array(
|
|
|
+ 'organism_id' => $node->organism_id,
|
|
|
+ 'name' => $node->title,
|
|
|
+ 'uniquename' => $node->uniquename,
|
|
|
+ 'description' => $node->description,
|
|
|
+ 'type_id' => $node->type_id
|
|
|
);
|
|
|
+ $stock_status = tripal_core_chado_insert('stock', $values);
|
|
|
}
|
|
|
- tripal_db_set_active($previous_db);
|
|
|
|
|
|
// create drupal chado_stock entry
|
|
|
- $previous_db = tripal_db_set_active('chado');
|
|
|
- $chado_stock = db_fetch_object(db_query("SELECT stock_id FROM stock WHERE uniquename='".$node->uniquename."' AND type_id=".$node->type_id.' AND organism_id='.$node->organism_id));
|
|
|
- tripal_db_set_active($previous_db);
|
|
|
- if (!empty($chado_stock->stock_id)) {
|
|
|
- db_query(
|
|
|
- "INSERT INTO {chado_stock} (nid, vid, stock_id) "
|
|
|
- ."VALUES (%d, %d, %d)",
|
|
|
- $node->nid,
|
|
|
- $node->vid,
|
|
|
- $chado_stock->stock_id
|
|
|
+ if ($stock_status) {
|
|
|
+ $values = array(
|
|
|
+ 'organism_id' => $node->organism_id,
|
|
|
+ 'uniquename' => $node->uniquename,
|
|
|
+ 'type_id' => $node->type_id
|
|
|
);
|
|
|
-
|
|
|
- //Move on to next stage of Stock Creation based on next_stage_path field
|
|
|
- if ($node->simulate_multipart) {
|
|
|
- $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
|
|
|
- $_REQUEST['destination'] = $next_stage_path;
|
|
|
+ $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
|
|
|
+ if (!empty($chado_stock[0]->stock_id)) {
|
|
|
+ db_query(
|
|
|
+ "INSERT INTO {chado_stock} (nid, vid, stock_id) "
|
|
|
+ ."VALUES (%d, %d, %d)",
|
|
|
+ $node->nid,
|
|
|
+ $node->vid,
|
|
|
+ $chado_stock[0]->stock_id
|
|
|
+ );
|
|
|
+
|
|
|
+ //Move on to next stage of Stock Creation based on next_stage_path field
|
|
|
+ if ($node->simulate_multipart) {
|
|
|
+ $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
|
|
|
+ $_REQUEST['destination'] = $next_stage_path;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ drupal_set_message('Error during stock creation.', 'error');
|
|
|
+ watchdog('tripal_stock',
|
|
|
+ 'Insert Stock: Unable to find newly created stock where values:%values',
|
|
|
+ array('%values' => print_r($values, TRUE)),
|
|
|
+ WATCHDOG_ERROR
|
|
|
+ );
|
|
|
+ return FALSE;
|
|
|
}
|
|
|
} else {
|
|
|
- drupal_set_message('Error during stock creation.', 'error');
|
|
|
+ drupal_set_message('Error during stock creation.', 'error');
|
|
|
+ watchdog('tripal_stock',
|
|
|
+ 'Insert Stock: Unable to create stock where values:%values',
|
|
|
+ array('%values' => print_r($values, TRUE)),
|
|
|
+ WATCHDOG_WARNING
|
|
|
+ );
|
|
|
+ return FALSE;
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -739,18 +829,95 @@ function chado_stock_update($node) {
|
|
|
chado_stock_insert($node);
|
|
|
} else {
|
|
|
|
|
|
+ //update dbxref
|
|
|
+ if ($node->database) {
|
|
|
+ if($node->accession) {
|
|
|
+ $dbxref_mode = '';
|
|
|
+ $stock = tripal_core_chado_select(
|
|
|
+ 'stock',
|
|
|
+ array('dbxref_id', 'type_id'),
|
|
|
+ array('stock_id' => $node->stock_id)
|
|
|
+ );
|
|
|
+
|
|
|
+ if ($stock[0]->dbxref_id) {
|
|
|
+ $values = array(
|
|
|
+ 'db_id' => $node->database,
|
|
|
+ 'accession' => $node->accession,
|
|
|
+ 'description' => $node->db_description
|
|
|
+ );
|
|
|
+ $dbxref_status = tripal_core_chado_update(
|
|
|
+ 'dbxref',
|
|
|
+ array('dbxref_id' => $stock[0]->dbxref_id),
|
|
|
+ $values
|
|
|
+ );
|
|
|
+ $dbxref_mode = 'Update';
|
|
|
+ } else {
|
|
|
+ if ($stock[0]->type_id) {
|
|
|
+ //create the dbxref
|
|
|
+ //used the type_id as a control to check we have a stock but not a dbxref
|
|
|
+ $values = array(
|
|
|
+ 'db_id' => $node->database,
|
|
|
+ 'accession' => $node->accession,
|
|
|
+ 'description' => $node->db_description,
|
|
|
+ 'version' => '1',
|
|
|
+ );
|
|
|
+ $dbxref_status = tripal_core_chado_insert(
|
|
|
+ 'dbxref',
|
|
|
+ $values
|
|
|
+ );
|
|
|
+ $dbxref_mode = 'Create';
|
|
|
+ } else {
|
|
|
+ drupal_set_message('Unable to find stock to Update', 'error');
|
|
|
+ watchdog(
|
|
|
+ 'tripal_stock',
|
|
|
+ 'Stock Update: Unable to find stock to update using values: %values',
|
|
|
+ array('%values', print_r($values, TRUE)),
|
|
|
+ WATCHDOG_ERROR
|
|
|
+ );
|
|
|
+ return FALSE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$dbxref_status) {
|
|
|
+ watchdog(
|
|
|
+ 'tripal_stock',
|
|
|
+ 'Stock Update: Unable to %mode main stock dbxref with values: %values',
|
|
|
+ array('%values' => print_r($values,TRUE), '%mode' => $dbxref_mode),
|
|
|
+ WATCHDOG_WARNING
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
//can't change stock id which is all thats stored in drupal thus only update chado
|
|
|
- $previous_db = tripal_db_set_active('chado');
|
|
|
- db_query(
|
|
|
- "UPDATE stock SET organism_id=%d, name='%s', uniquename='%s', description='%s', type_id=%d WHERE stock_id=%d",
|
|
|
- $node->organism_id,
|
|
|
- $node->title,
|
|
|
- $node->uniquename,
|
|
|
- $node->description,
|
|
|
- $node->type_id,
|
|
|
- $node->stock_id
|
|
|
+ $update_values = array(
|
|
|
+ 'organism_id' => $node->organism_id,
|
|
|
+ 'name' => $node->title,
|
|
|
+ 'uniquename' => $node->uniquename,
|
|
|
+ 'description' => $node->description,
|
|
|
+ 'type_id' => $node->type_id,
|
|
|
);
|
|
|
- tripal_db_set_active($previous_db);
|
|
|
+ if ($dbxref_status) {
|
|
|
+ $update_values['dbxref_id'] = array(
|
|
|
+ 'db_id' => $node->database,
|
|
|
+ 'accession' => $node->accession
|
|
|
+ );
|
|
|
+ }
|
|
|
+ $status = tripal_core_chado_update(
|
|
|
+ 'stock',
|
|
|
+ array('stock_id' => $node->stock_id),
|
|
|
+ $update_values
|
|
|
+ );
|
|
|
+
|
|
|
+ if (!$status) {
|
|
|
+ drupal_set_message('Unable to update stock', 'error');
|
|
|
+ watchdog(
|
|
|
+ 'tripal_stock',
|
|
|
+ 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
|
|
|
+ array('%mvalues' => print_r(array('stock_id' => $node->stock_id),TRUE), '%uvalues' => print_r($update_values,TRUE)),
|
|
|
+ WATCHDOG_ERROR
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|