Tripal v1.0 (6.x-1.0)
tripal_stock.module
Go to the documentation of this file.
00001 <?php
00002 
00024 require_once("includes/tripal_stock-administration.inc");
00025 require_once("includes/other_module_api_functions.inc");
00026 require_once("includes/tripal_stock-secondary_tables.inc");
00027 require_once("includes/tripal_stock-properties.inc");
00028 require_once("includes/tripal_stock-relationships.inc");
00029 require_once("includes/tripal_stock-db_references.inc");
00030 require_once("api/tripal_stock.api.inc");
00039 function tripal_stock_menu() {
00040   $items = array();
00041 
00042   //Administrative settings menu-----------------
00043   $items['admin/tripal/tripal_stock'] = array(
00044     'title' => 'Stocks',
00045     'description' => 'Basic Description of Tripal Stock Module Functionality',
00046     'page callback' => 'theme',
00047     'page arguments' => array('tripal_stock_admin'),
00048     'access arguments' => array('administer tripal stocks'),
00049     'type' => MENU_NORMAL_ITEM
00050   );
00051 
00052   $items['admin/tripal/tripal_stock/configuration'] = array(
00053     'title' => 'Configuration',
00054     'description' => 'Settings for Chado Stocks',
00055     'page callback' => 'drupal_get_form',
00056     'page arguments' => array('tripal_stock_admin'),
00057     'access arguments' => array('administer tripal stocks'),
00058     'type' => MENU_NORMAL_ITEM
00059   );
00060 
00061   // Adding Secondary Properties-----------------
00062   $items['node/%cs_node/properties'] = array(
00063     'title' => 'Add Properties & Synonyms',
00064     'description' => 'Settings for Chado Stocks',
00065     'page callback' => 'tripal_stock_add_ALL_property_page',
00066     'page arguments' => array(1),
00067     'access arguments' => array('create chado_stock content'),
00068     'type' => MENU_CALLBACK
00069   );
00070 
00071   $items['node/%cs_node/db_references'] = array(
00072     'title' => 'Add Database References',
00073     'description' => 'Settings for Chado Stocks',
00074     'page callback' => 'tripal_stock_add_ALL_dbreferences_page',
00075     'page arguments' => array(1),
00076     'access arguments' => array('create chado_stock content'),
00077     'type' => MENU_CALLBACK
00078   );
00079 
00080   $items['node/%cs_node/relationships'] = array(
00081     'title' => 'Add Relationships',
00082     'description' => 'Settings for Chado Stocks',
00083     'page callback' => 'tripal_stock_add_ALL_relationships_page',
00084     'page arguments' => array(1),
00085     'access arguments' => array('create chado_stock content'),
00086     'type' => MENU_CALLBACK
00087   );
00088 
00089   //Edit/Deleting Secondary Properties-------------
00090   $items['node/%cs_node/edit_properties'] = array(
00091     'title' => 'Edit Properties',
00092     'description' => 'Settings for Chado Stocks',
00093     'page callback' => 'tripal_stock_edit_ALL_properties_page',
00094     'page arguments' => array(1),
00095     'access arguments' => array('edit chado_stock content'),
00096     'type' => MENU_LOCAL_TASK,
00097     'weight' => 8,
00098   );
00099 
00100     $items['node/%cs_node/edit_relationships'] = array(
00101     'title' => 'Edit Relationships',
00102     'description' => 'Settings for Chado Stocks',
00103     'page callback' => 'tripal_stock_edit_ALL_relationships_page',
00104     'page arguments' => array(1),
00105     'access arguments' => array('edit chado_stock content'),
00106     'type' => MENU_LOCAL_TASK,
00107     'weight' => 9,
00108   );
00109 
00110   $items['node/%cs_node/edit_db_references'] = array(
00111     'title' => 'Edit DB References',
00112     'description' => 'Settings for Chado Stocks',
00113     'page callback' => 'tripal_stock_edit_ALL_dbreferences_page',
00114     'page arguments' => array(1),
00115     'access arguments' => array('edit chado_stock content'),
00116     'type' => MENU_LOCAL_TASK,
00117     'weight' => 10,
00118   );
00119   return $items;
00120 }
00121 
00138 function cs_node_load($nid) {
00139   if (is_numeric($nid)) {
00140     $node = node_load($nid);
00141     if ($node->type == 'chado_stock') {
00142       return $node;
00143     }
00144   }
00145 
00146   return FALSE;
00147 }
00148 
00159 function tripal_stock_perm() {
00160   return array(
00161     'access chado_stock content',
00162     'create chado_stock content',
00163     'edit chado_stock content',
00164     'delete chado_stock content',
00165     'administer tripal stocks',
00166   );
00167 }
00168 
00184 function chado_stock_access($op, $node, $account) {
00185   if ($op == 'create') {
00186     if (!user_access('create chado_stock content', $account)) {
00187       return FALSE;
00188     }
00189   }
00190   if ($op == 'update') {
00191     if (!user_access('edit chado_stock content', $account)) {
00192       return FALSE;
00193     }
00194   }
00195   if ($op == 'delete') {
00196     if (!user_access('delete chado_stock content', $account)) {
00197       return FALSE;
00198     }
00199   }
00200   if ($op == 'view') {
00201     if (!user_access('access chado_stock content', $account)) {
00202       return FALSE;
00203     }
00204   }
00205   return NULL;
00206 }
00207 
00220 function tripal_stock_views_api() {
00221   return array(
00222     'api' => 2.0,
00223   );
00224 }
00225 
00234 function tripal_stock_theme() {
00235   return array(
00236 
00237     // Property edit forms--------------------------
00238     'tripal_stock_edit_ALL_properties_form' => array(
00239       'arguments' => array('form'),
00240       'function' => 'theme_tripal_stock_edit_ALL_properties_form',
00241     ),
00242     'tripal_stock_edit_ALL_db_references_form' => array(
00243       'arguments' => array('form'),
00244       'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
00245     ),
00246     'tripal_stock_edit_ALL_relationships_form' => array(
00247       'arguments' => array('form'),
00248       'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
00249     ),
00250     // Block Templates------------------------------
00251     'tripal_stock_base' => array(
00252       'arguments' => array('node' => NULL),
00253       'template' => 'tripal_stock_base',
00254     ),
00255     'tripal_stock_properties' => array(
00256       'arguments' => array('node' => NULL),
00257       'template' => 'tripal_stock_properties',
00258     ),
00259     'tripal_stock_references' => array(
00260       'arguments' => array('node' => NULL),
00261       'template' => 'tripal_stock_references',
00262     ),
00263     'tripal_stock_relationships' => array(
00264       'arguments' => array('node' => NULL),
00265       'template' => 'tripal_stock_relationships',
00266     ),
00267     'tripal_stock_synonyms' => array(
00268       'arguments' => array('node' => NULL),
00269       'template' => 'tripal_stock_synonyms',
00270     ),
00271     'tripal_stock_collections' => array(
00272        'arguments' => array('node' => NULL),
00273        'template' => 'tripal_stock_collections',
00274     ),
00275     'tripal_stock_collections' => array(
00276        'arguments' => array('node' => NULL),
00277        'template' => 'tripal_stock_collections',
00278     ),
00279     'tripal_stock_phenotypes' => array(
00280        'arguments' => array('node' => NULL),
00281        'template' => 'tripal_stock_phenotypes',
00282     ),
00283     'tripal_stock_genotypes' => array(
00284        'arguments' => array('node' => NULL),
00285        'template' => 'tripal_stock_genotypes',
00286     ),
00287     'tripal_stock_locations' => array(
00288        'arguments' => array('node' => NULL),
00289        'template' => 'tripal_stock_locations',
00290     ),    
00291     'tripal_organism_stocks' => array(
00292        'arguments' => array('node' => NULL),
00293        'template' => 'tripal_organism_stocks',
00294     ),    
00295     'tripal_stock_admin' => array(
00296       'template' => 'tripal_stock_admin',  
00297       'arguments' =>  array(NULL),  
00298       'path' => drupal_get_path('module', 'tripal_stock') . '/theme', 
00299     ),
00300   );
00301 }
00302 
00311 function tripal_stock_node_info() {
00312   return array(
00313     'chado_stock' => array(
00314       'name' => t('Stock'),
00315       'module' => 'chado_stock',
00316       'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
00317       'has_title' => TRUE,
00318       'has_body' => FALSE,
00319     ),
00320   );
00321 }
00322 
00334 function chado_stock_load($node) {
00335 
00336   // get the stock details from chado
00337   $stock_id = chado_get_id_for_node('stock', $node);
00338   
00339   // build the variable with all the stock details
00340   $values = array('stock_id' => $stock_id);
00341   $stock = tripal_core_generate_chado_var('stock', $values);
00342   
00343   // add this to the node
00344   $additions = new stdClass();
00345   $additions->stock = $stock;
00346   return $additions;
00347 }
00348 
00349 
00371 function chado_stock_form($node, $form_state) {
00372 
00373   // Expand all fields needed
00374   $fields_needed = array('stock.uniquename', 'stock.name', 'stock.stock_id', 'stock.type_id', 'stock.organism_id', 'stock.description', 'stock.dbxref_id', 'dbxref.accession', 'dbxref.description', 'dbxref.db_id', 'db.db_id');
00375   foreach ($fields_needed as $field_name) {
00376     // Check to see if it's excluded and expand it if so
00377     if ($node->expandable_fields) {
00378       if (in_array($field_name, $node->expandable_fields)) {
00379         $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
00380       }
00381     }
00382   }
00383 
00384   // This defines the path for the next step in a simulated multipart form
00385   // NOTE: The %node gets replaced with the nid in insert
00386   $form['next_step_path'] = array(
00387     '#type' => 'hidden',
00388     '#value' => 'node/%node/properties'
00389   );
00390 
00391   // If you don't want a multipart form set this to false
00392   // Will then do default redirect (to new node) on submit
00393   $form['simulate_multipart'] = array(
00394     '#type' => 'textfield',
00395     '#attributes' => array('style' => "display:none"),
00396     '#default_value' => TRUE
00397   );
00398 
00399   if (!isset($node->stock->uniquename)) {
00400     $form['progress'] = array(
00401       '#type' => 'item',
00402       '#value' => tripal_stock_add_chado_properties_progress('main')
00403     );
00404   }
00405 
00406   $form['names'] = array(
00407     '#type' => 'fieldset',
00408     '#title' => t('Stock Name')
00409   );
00410 
00411   $form['names']['title'] = array(
00412     '#type' => 'textfield',
00413     '#title' => t('Name'),
00414     '#default_value' => $node->stock->name,
00415     '#required'       => TRUE
00416   );
00417 
00418   $form['names']['uniquename'] = array(
00419     '#type' => 'textfield',
00420     '#title' => t('Unique Name'),
00421     '#default_value' => $node->stock->uniquename,
00422     '#required'       => TRUE
00423   );
00424 
00425   $form['names']['stock_id'] = array(
00426     '#type' => 'hidden',
00427     '#value' => $node->stock->stock_id
00428   );
00429 
00430   $form['details'] = array(
00431     '#type' => 'fieldset',
00432     '#title' =>  t('Stock Details')
00433   );
00434 
00435   $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'NULL') );
00436   $type_options[0] = 'Select a Type';
00437   if ($node->nid == '') {
00438   $type_default = 0; }
00439   else { $type_default = $node->stock->type_id->cvterm_id; }
00440   $form['details']['type_id'] = array(
00441     '#type' => 'select',
00442     '#title' => t('Type of Stock'),
00443     '#options' => $type_options,
00444     '#default_value' => $type_default,
00445     '#required'    => TRUE,
00446   );
00447 
00448  
00449   // get the list of organisms
00450   $sql = "SELECT * FROM {Organism} ORDER BY genus, species";
00451   $org_rset = chado_query($sql);
00452   $organisms = array();
00453   $organisms[''] = '';
00454   while ($organism = db_fetch_object($org_rset)) {
00455     $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
00456   }
00457   $form['details']['organism_id'] = array(
00458     '#type' => 'select',
00459     '#title' => t('Source Organism for stock'),
00460     '#default_value' => $organism_default,
00461     '#options' => $organisms,
00462     '#required'    => TRUE
00463   );
00464 
00465 
00466   $form['details']['stock_description'] = array(
00467     '#type' => 'textarea',
00468     '#title' => t('Notes'),
00469     '#default_value' => $node->stock->description,
00470     '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
00471   );
00472 
00473   $form['database_reference'] = array(
00474     '#type' => 'fieldset',
00475     '#title' => t('Stock Database Reference')
00476   );
00477 
00478   $form['database_reference']['accession'] = array(
00479     '#type' => 'textfield',
00480     '#title' => t('Accession'),
00481     '#default_value' => $node->stock->dbxref_id->accession
00482   );
00483 
00484   $form['database_reference']['db_description'] = array(
00485     '#type' => 'textarea',
00486     '#title' => t('Description of Database Reference'),
00487     '#default_value' => $node->stock->dbxref_id->description,
00488     '#description' => t('Optionally enter a description about the database accession.')
00489   );
00490 
00491   $db_options = tripal_db_get_db_options();
00492   $db_options[0] = 'Select a Database';
00493   if ($node->nid == '') {
00494   $db_default = 0; }
00495   else { $db_default = $node->stock->dbxref_id->db_id->db_id; }
00496   $form['database_reference']['database'] = array(
00497     '#type' => 'select',
00498     '#title' => t('Database'),
00499     '#options' => $db_options,
00500     '#default_value' => $db_default
00501   );
00502 
00503   return $form;
00504 }
00505 
00516 function chado_stock_validate($node, &$form) {
00517 
00518   $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
00519   $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
00520 
00521   // Validate Uniquename only if add
00522   if (empty($node->stock_id)) {
00523     $chado_row = db_fetch_object(chado_query("SELECT * FROM {stock} WHERE uniquename='" . $node->uniquename . "'"));
00524     if (!empty($chado_row->stock_id)) {
00525       $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=" . $chado_row->stock_id));
00526       if (!empty($drupal_row->nid)) {
00527         $link = l('node/' . $drupal_row->nid, $node->uniquename);
00528         form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
00529       }
00530       else {
00531         form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
00532       }
00533     }
00534   }
00535 
00536 
00537   // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
00538   if ( $node->type_id == 0) {
00539     form_set_error('type_id', 'Please select a type of stock.');
00540   }
00541   else {
00542     $num_rows = db_fetch_object(chado_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
00543     if ( $num_rows->count != 1) {
00544       form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
00545   }
00546 
00547   // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
00548   if ( $node->organism_id == 0) {
00549     form_set_error('organism_id', 'Please select a source organism for this stock');
00550   }
00551   else {
00552     $num_rows = db_fetch_object(chado_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
00553     if ( $num_rows->count != 1 ) {
00554       form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
00555   }
00556 
00557   // Check if Accession also database
00558   if ($node->accession != '') {
00559     if ($node->database == 0) {
00560       // there is an accession but no database selected
00561       form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
00562     }
00563   }
00564   else {
00565     if ($node->database > 0) {
00566       // there is a database selected but no accession
00567       form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
00568     }
00569   }
00570 
00571   // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
00572   if ( $node->database > 0) {
00573     $num_rows = db_fetch_object(chado_query($int_in_chado_sql, 'db', 'db_id', $node->database));
00574     if ($num_rows->count != 1) {
00575       form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
00576   }
00577 
00578 }
00579 
00591 function chado_stock_insert($node) {
00592 
00593   //If the chado stock exists
00594   // then don't create but simply link to node
00595   if ($node->chado_stock_exists) {
00596     if (!empty($node->stock_id)) {
00597       db_query(
00598         "INSERT INTO {chado_stock} (nid, vid, stock_id) "
00599         ."VALUES (%d, %d, %d)",
00600         $node->nid,
00601         $node->vid,
00602         $node->stock_id
00603       );
00604     }
00605     return $node;
00606   }
00607 
00608   // create dbxref
00609   if ( !empty($node->accession) ) {
00610     if ( !empty($node->database) ) {
00611       $values = array(
00612         'db_id' => $node->database,
00613         'accession' => $node->accession,
00614       );
00615       if (!tripal_core_chado_select('dbxref', array(dbxref_id), $values)) {
00616         $values['description'] = $node->db_description;
00617         $values['version'] = '1';
00618         $dbxref_status = tripal_core_chado_insert('dbxref', $values);
00619         if (!$dbxref_status) {
00620           drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
00621           watchdog('tripal_stock',
00622             'Insert Stock: Unable to create dbxref where values:%values',
00623             array('%values' => print_r($values, TRUE)),
00624             WATCHDOG_WARNING
00625           );
00626         }
00627       }
00628       else { $dbxref_status = 1; }
00629     }
00630   }
00631 
00632   // create stock
00633   if ($dbxref_status) {
00634     $values = array(
00635       'dbxref_id' => array(
00636         'db_id' => $node->database,
00637         'accession' => $node->accession
00638       ),
00639       'organism_id' => $node->organism_id,
00640       'name' => $node->title,
00641       'uniquename' => $node->uniquename,
00642       'description' => $node->stock_description,
00643       'type_id' => $node->type_id
00644     );
00645     $stock_status = tripal_core_chado_insert('stock', $values);
00646   }
00647   else {
00648     $values = array(
00649       'organism_id' => $node->organism_id,
00650       'name' => $node->title,
00651       'uniquename' => $node->uniquename,
00652       'description' => $node->stock_description,
00653       'type_id' => $node->type_id
00654     );
00655     $stock_status = tripal_core_chado_insert('stock', $values);
00656   }
00657 
00658   // create drupal chado_stock entry
00659   if ($stock_status) {
00660     $values = array(
00661       'organism_id' => $node->organism_id,
00662       'uniquename' => $node->uniquename,
00663       'type_id' => $node->type_id
00664     );
00665     $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
00666     if (!empty($chado_stock[0]->stock_id)) {
00667       db_query(
00668         "INSERT INTO {chado_stock} (nid, vid, stock_id) "
00669         ."VALUES (%d, %d, %d)",
00670         $node->nid,
00671         $node->vid,
00672         $chado_stock[0]->stock_id
00673       );
00674 
00675         //Move on to next stage of Stock Creation based on next_stage_path field
00676       if ($node->simulate_multipart) {
00677         $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
00678         $_REQUEST['destination'] = $next_stage_path;
00679       }
00680     }
00681     else {
00682       drupal_set_message(t('Error during stock creation.'), 'error');
00683       watchdog('tripal_stock',
00684         'Insert Stock: Unable to find newly created stock where values:%values',
00685         array('%values' => print_r($values, TRUE)),
00686         WATCHDOG_ERROR
00687       );
00688       return FALSE;
00689     }
00690   }
00691   else {
00692     drupal_set_message(t('Error during stock creation.'), 'error');
00693     watchdog('tripal_stock',
00694       'Insert Stock: Unable to create stock where values:%values',
00695       array('%values' => print_r($values, TRUE)),
00696       WATCHDOG_WARNING
00697     );
00698     return FALSE;
00699   }
00700 
00701 }
00702 
00716 function chado_stock_update($node) {
00717 
00718   if ($node->revision) {
00719     // there is no way to handle revisions in Chado but leave
00720     // this here just to make not we've addressed it.
00721   }
00722 //  if ($node->revision) {
00723 //    chado_stock_insert($node);
00724 //  }
00725 //  else {
00726 
00727   //update dbxref
00728   if ($node->database) {
00729     if ($node->accession) {
00730       $dbxref_mode = '';
00731       $stock = tripal_core_chado_select(
00732         'stock',
00733         array('dbxref_id', 'type_id'),
00734         array('stock_id' => $node->stock_id)
00735       );
00736 
00737       if ($stock[0]->dbxref_id) {
00738         $values = array(
00739           'db_id' => $node->database,
00740           'accession' => $node->accession,
00741           'description' => $node->db_description
00742         );
00743         $dbxref_status = tripal_core_chado_update(
00744           'dbxref',
00745           array('dbxref_id' => $stock[0]->dbxref_id),
00746           $values
00747         );
00748         $dbxref_mode = 'Update';
00749       }
00750       else {
00751         if ($stock[0]->type_id) {
00752           //create the dbxref
00753           //used the type_id as a control to check we have a stock but not a dbxref
00754           $values = array(
00755             'db_id' => $node->database,
00756             'accession' => $node->accession,
00757             'description' => $node->db_description,
00758             'version' => '1',
00759           );
00760           $dbxref_status = tripal_core_chado_insert(
00761             'dbxref',
00762             $values
00763           );
00764           $dbxref_mode = 'Create';
00765         }
00766         else {
00767           drupal_set_message(t('Unable to find stock to Update'), 'error');
00768           watchdog(
00769             'tripal_stock',
00770             'Stock Update: Unable to find stock to update using values: %values',
00771             array('%values', print_r($values, TRUE)),
00772             WATCHDOG_ERROR
00773           );
00774           return FALSE;
00775         }
00776       }
00777     }
00778   }
00779 
00780   if (!$dbxref_status) {
00781     watchdog(
00782       'tripal_stock',
00783       'Stock Update: Unable to %mode main stock dbxref with values: %values',
00784       array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
00785       WATCHDOG_WARNING
00786     );
00787   }
00788 
00789   //can't change stock id which is all thats stored in drupal thus only update chado
00790   $update_values = array(
00791     'organism_id' => $node->organism_id,
00792     'name' => $node->title,
00793     'uniquename' => $node->uniquename,
00794     'description' => $node->stock_description,
00795     'type_id' => $node->type_id,
00796   );
00797   if ($dbxref_status) {
00798     $update_values['dbxref_id'] = array(
00799       'db_id' => $node->database,
00800       'accession' => $node->accession
00801     );
00802   }
00803   $status = tripal_core_chado_update(
00804     'stock',
00805     array('stock_id' => $node->stock_id),
00806     $update_values
00807   );
00808 
00809   if (!$status) {
00810     drupal_set_message(t('Unable to update stock'), 'error');
00811     watchdog(
00812       'tripal_stock',
00813       'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
00814       array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE)),
00815       WATCHDOG_ERROR
00816     );
00817   }
00818 }
00819 
00833 function chado_stock_delete($node) {
00834 
00835   // Set stock in chado: is_obsolete = TRUE
00836   chado_query(
00837     "DELETE FROM {stock} WHERE stock_id=%d",
00838     $node->stock->stock_id
00839   );
00840 
00841   //remove drupal node and all revisions
00842   db_query(
00843     "DELETE FROM {chado_stock} WHERE nid=%d",
00844     $node->nid
00845   );
00846 }
00847 
00866 function tripal_stock_block($op = 'list', $delta = 0, $edit=array()) {
00867   switch ($op) {
00868     case 'list':
00869       $blocks['base']['info'] = t('Tripal Stock Details');
00870       $blocks['base']['cache'] = BLOCK_NO_CACHE;
00871 
00872       $blocks['properties']['info'] = t('Tripal Stock Properties');
00873       $blocks['properties']['cache'] = BLOCK_NO_CACHE;
00874 
00875       $blocks['references']['info'] = t('Tripal Stock References');
00876       $blocks['references']['cache'] = BLOCK_NO_CACHE;
00877 
00878       $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
00879       $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
00880 
00881       $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
00882       $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
00883       
00884       $blocks['collections']['info'] = t('Tripal Stock Collections');
00885       $blocks['collections']['cache'] = BLOCK_NO_CACHE;
00886       
00887       $blocks['phenotypes']['info'] = t('Tripal Stock Phenotypes');
00888       $blocks['phenotypes']['cache'] = BLOCK_NO_CACHE;
00889       
00890       $blocks['genotypes']['info'] = t('Tripal Stock Genotypes');
00891       $blocks['genotypes']['cache'] = BLOCK_NO_CACHE;
00892       
00893       $blocks['locations']['info'] = t('Tripal Stock Locations');
00894       $blocks['locations']['cache'] = BLOCK_NO_CACHE;
00895       
00896       $blocks['orgstocks']['info'] = t('Tripal Organism Stocks');
00897       $blocks['orgstocks']['cache'] = BLOCK_NO_CACHE;
00898       
00899       
00900 
00901       return $blocks;
00902 
00903     case 'view':
00904       if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
00905       $nid = arg(1);
00906       $node = node_load($nid);
00907 
00908       $block = array();
00909       switch ($delta) {
00910         case 'base':
00911           $block['subject'] = t('Stock Details');
00912           $block['content'] = theme('tripal_stock_base', $node);
00913           break;
00914 
00915         case 'properties':
00916           $block['subject'] = t('Properties');
00917           $block['content'] = theme('tripal_stock_properties', $node);
00918           break;
00919 
00920         case 'references':
00921           $block['subject'] = t('References');
00922           $block['content'] = theme('tripal_stock_references', $node);
00923           break;
00924 
00925         case 'relationships':
00926           $block['subject'] = t('Relationships');
00927           $block['content'] = theme('tripal_stock_relationships', $node);
00928           break;
00929 
00930         case 'synonyms':
00931           $block['subject'] = t('Synonyms');
00932           $block['content'] = theme('tripal_stock_synonyms', $node);
00933           break;
00934           
00935         case 'collections':
00936           $block['subject'] = t('Stock Collections');
00937           $block['content'] = theme('tripal_stock_collections', $node);
00938           break;
00939           
00940         case 'phenotypes':
00941           $block['subject'] = t('Stock Phenotypes');
00942           $block['content'] = theme('tripal_stock_phenotypes', $node);
00943           break;
00944           
00945         case 'genotypes':
00946           $block['subject'] = t('Stock Genotypes');
00947           $block['content'] = theme('tripal_stock_genotypes', $node);
00948           break;
00949           
00950         case 'locations':
00951           $block['subject'] = t('Stock Locations');
00952           $block['content'] = theme('tripal_stock_locations', $node);
00953           break;
00954           
00955         case 'orgstocks':
00956           $block['subject'] = t('Organism Stocks');
00957           $block['content'] = theme('tripal_organism_stocks', $node);
00958           break;
00959 
00960       }
00961       return $block;
00962     }
00963   }
00964 }
00965 
00971 function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
00972   // we want to provide a new variable that contains the matched stocks.
00973   $stock = $variables['node']->stock;
00974    
00975   // normally we would use tripal_core_expand_chado_vars to expand our
00976   // organism object and add in the relationships, however whan a large
00977   // number of relationships are present this significantly slows the
00978   // query, therefore we will manually perform the query
00979   $sql = "
00980     SELECT 
00981       S.name, S.uniquename, S.stock_id, CS.nid, 
00982       CVT.name as rel_type, CVTs.name as obj_type,
00983       SR.value
00984     FROM stock_relationship SR
00985       INNER JOIN stock S on SR.object_id = S.stock_id
00986       INNER JOIN cvterm CVT on SR.type_id = CVT.cvterm_id
00987       INNER JOIN cvterm CVTs on S.type_id = CVTs.cvterm_id
00988       LEFT JOIN chado_stock CS on S.stock_id = CS.stock_id
00989     WHERE SR.subject_id = %d      
00990   ";
00991   $as_subject = chado_query($sql, $stock->stock_id);
00992   $sql = "
00993     SELECT 
00994       S.name, S.uniquename,  S.stock_id, CS.nid, 
00995       CVT.name as rel_type, CVTs.name as sub_type,
00996       SR.value
00997     FROM stock_relationship SR
00998       INNER JOIN stock S on SR.subject_id = S.stock_id
00999       INNER JOIN cvterm CVT on SR.type_id = CVT.cvterm_id
01000       INNER JOIN cvterm CVTs on S.type_id = CVTs.cvterm_id
01001       LEFT JOIN chado_stock CS on S.stock_id = CS.stock_id
01002     WHERE SR.object_id = %d      
01003   ";
01004   $as_object = chado_query($sql, $stock->stock_id);   
01005   
01006   // combine both object and subject relationshisp into a single array
01007   $relationships = array();
01008   $relationships['object'] = array();
01009   $relationships['subject'] = array();
01010   
01011   // iterate through the object relationships
01012   while ($relationship = db_fetch_object($as_object)) {
01013      
01014      // get the relationship and child types
01015      $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
01016      $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
01017      
01018      if (!array_key_exists($rel_type, $relationships['object'])) {
01019        $relationships['object'][$rel_type] = array();   
01020      }
01021      if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
01022        $relationships['object'][$rel_type][$sub_type] = array();   
01023      }
01024      $relationships['object'][$rel_type][$sub_type][] = $relationship;     
01025   }
01026   
01027   // now add in the subject relationships
01028   while ($relationship = db_fetch_object($as_subject)) {
01029      
01030      // get the relationship and child types
01031      $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
01032      $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
01033      
01034      if (!array_key_exists($rel_type, $relationships['subject'])) {
01035        $relationships['subject'][$rel_type] = array();   
01036      }
01037      if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
01038        $relationships['subject'][$rel_type][$obj_type] = array();   
01039      }
01040      $relationships['subject'][$rel_type][$obj_type][] = $relationship;     
01041   }
01042   
01043   
01044   $stock->all_relationships = $relationships;
01045 
01046 }
01054 function tripal_stock_nodeapi(&$node, $op, $teaser, $page) {
01055 
01056   switch ($op) {
01057     // Note that this function only adds stock view to an organism/feature
01058     // node.
01059     case 'view':
01060       // add the stock to the organism/feature search indexing
01061       if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
01062 
01063       }
01064       elseif ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
01065 
01066       }
01067       else {
01068         switch ($node->type) {
01069           case 'chado_organism':
01070             // Show stock if the organism/feature is not at teaser view
01071             $node->content['tripal_organism_stocks'] = array(
01072               '#value' => theme('tripal_organism_stocks', $node),
01073             );
01074             break;          
01075         }
01076       }
01077       break;
01078   }
01079 }
01080 
01086 function tripal_stock_preprocess_tripal_organism_stocks(&$variables) {
01087   $organism = $variables['node']->organism;
01088 
01089   if (!$organism) {
01090     return;
01091   }
01092   
01093   // get the stocks for this organism
01094   $sql  = "SELECT S.name, S.stock_id, S.uniquename, CVT.name as cvname ".
01095           "FROM {stock} S ".
01096           "  INNER JOIN {cvterm} CVT on S.type_id = CVT.cvterm_id ".
01097           "WHERE organism_id = %s".
01098           "ORDER BY stock_id ASC";
01099 
01100   // the counting SQL
01101   $csql  = "SELECT count(*) ".
01102            "FROM {stock} S".
01103            "  INNER JOIN {cvterm} CVT on S.type_id = CVT.cvterm_id ".
01104            "WHERE organism_id = %s ".
01105            "GROUP BY organism_id ";
01106 
01107   $previous_db = tripal_db_set_active('chado');  // use chado database
01108   $org_stocks = pager_query($sql, 25, 0, $csql, $organism->organism_id);
01109   tripal_db_set_active($previous_db);  // now use drupal database
01110   
01111   // get the pager and reformat so it shows the right block
01112   $pager = theme('pager');  
01113 
01114   // prepare the query that will lookup node ids
01115   $sql = "SELECT nid FROM {chado_stock} ".
01116          "WHERE stock_id = %d";
01117   $i=0;
01118   $stocks = array();
01119   while ($stock = db_fetch_object($org_stocks)) {
01120     $node = db_fetch_object(db_query($sql, $stock->stock_id));
01121     $stock->nid = $node->nid;
01122     $stocks[$i++] = $stock;
01123   }
01124   
01125   $organism->stock_browser =  array('stocks' => $stocks, 'pager' => $pager, 'enabled' => TRUE );
01126 }
01127 
 All Classes Files Functions Variables