Tripal 0.3b
tripal_feature/tripal_feature.install
Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004 *  Implementation of hook_install();
00005 *
00006 * @ingroup tripal_feature
00007 */
00008 function tripal_feature_install(){
00009    // create the module's data directory
00010    tripal_create_moddir('tripal_feature');
00011 
00012    // create the tables that correlate drupal nodes with chado 
00013    // features, organisms, etc....
00014    drupal_install_schema('tripal_feature');
00015   
00016    // add the materialized view
00017    tripal_feature_add_organism_count_mview();
00018 
00019 }
00020 /**
00021 *  Update for Drupal 6.x, Tripal 0.2b, Feature Module 0.2
00022 *  This update adjusts the materialized view by adding a 'cvterm_id' column
00023 *
00024 * @ingroup tripal_feature
00025 */
00026 function tripal_feature_update_6000(){
00027    // recreate the materialized view
00028    tripal_feature_add_organism_count_mview();
00029    $ret = array(
00030       '#finished' => 1,
00031    );
00032    
00033    return $ret;
00034 }
00035 
00036 /**
00037 *
00038 * @ingroup tripal_feature
00039 */
00040 function tripal_feature_update_6300(){
00041    // add the relationship aggregator table to the database
00042    $schema = tripal_feature_get_schemas('tripal_feature_relagg');
00043    $ret = array();
00044    db_create_table($ret, 'tripal_feature_relagg', $schema['tripal_feature_relagg']);
00045    
00046    return $ret;
00047 }
00048 /**
00049 *
00050 * @ingroup tripal_feature
00051 */
00052 function tripal_feature_add_organism_count_mview(){
00053    $view_name = 'organism_feature_count';
00054 
00055    // Drop the MView table if it exists
00056    $mview_id = tripal_mviews_get_mview_id($view_name);
00057    if($mview_id){
00058       tripal_mviews_action("delete",$mview_id);
00059    }
00060 
00061    // Create the MView
00062    tripal_add_mview(
00063       // view name
00064       $view_name,
00065       // tripal module name
00066       'tripal_feature',
00067       // table name
00068       $view_name,
00069       // table schema definition
00070       'organism_id integer, genus character varying(255), '.
00071       '  species character varying(255), '.
00072       '  common_name character varying(255), '.
00073       '  num_features integer, cvterm_id integer, '.
00074       '  feature_type character varying(255)',
00075       // columns for indexing
00076       'organism_id,cvterm_id,feature_type',
00077       // SQL statement to populate the view
00078       'SELECT O.organism_id, O.genus, O.species, O.common_name, 
00079           count(F.feature_id) as num_features, 
00080           CVT.cvterm_id, CVT.name as feature_type 
00081        FROM {Organism} O 
00082           INNER JOIN Feature F           ON O.Organism_id = F.organism_id 
00083           INNER JOIN Cvterm CVT          ON F.type_id = CVT.cvterm_id 
00084        GROUP BY O.Organism_id, O.genus, O.species, O.common_name,
00085           CVT.cvterm_id, CVT.name',
00086       // special index
00087       ''
00088    );
00089 
00090    // add a job to the job queue so this view gets updated automatically next
00091    // time the job facility is run
00092    $mview_id = tripal_mviews_get_mview_id($view_name);
00093    if($mview_id){
00094       tripal_mviews_action('update',$mview_id);
00095    }
00096 }
00097 /**
00098 * Implementation of hook_schema().
00099 *
00100 * @ingroup tripal_feature
00101 */
00102 function tripal_feature_schema() {
00103    $schema = tripal_feature_get_schemas();
00104    return $schema;
00105 }
00106 /**
00107 * Implementation of hook_uninstall()
00108 *
00109 * @ingroup tripal_feature
00110 */
00111 function tripal_feature_uninstall(){
00112 
00113    // Drop the MView table if it exists
00114    $mview_id = tripal_mviews_get_mview_id('organism_feature_count');
00115    if($mview_id){
00116       tripal_mviews_action("delete",$mview_id);
00117    }
00118 
00119    drupal_uninstall_schema('tripal_feature');
00120 
00121    // Get the list of nodes to remove
00122    $sql_feature_id = "SELECT nid, vid ".
00123                  "FROM {node} ".
00124                  "WHERE type='chado_feature'";
00125    $result = db_query($sql_feature_id);
00126    while ($node = db_fetch_object($result)) {
00127       node_delete($node->nid);
00128    }
00129 }
00130 
00131 /**
00132 * This function simply defines all tables needed for the module to work
00133 * correctly.  By putting the table definitions in a separate function we 
00134 * can easily provide the entire list for hook_install or individual
00135 * tables for an update.
00136 *
00137 * @ingroup tripal_feature
00138 */
00139 function tripal_feature_get_schemas ($table = NULL){  
00140   $schema = array();
00141 
00142 
00143   if(!$table or strcmp($table,'chado_feature')==0){
00144      $schema['chado_feature'] = array(
00145          'fields' => array(
00146             'vid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
00147             'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
00148             'feature_id' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
00149             'sync_date' => array ('type' => 'int', 'not null' => FALSE, 'description' => 'UNIX integer sync date/time'),
00150          ),
00151          'indexes' => array(
00152             'feature_id' => array('feature_id')
00153           ),
00154          'unique keys' => array(
00155             'nid_vid' => array('nid','vid'),
00156             'vid' => array('vid')
00157          ),
00158          'primary key' => array('nid'),
00159      );
00160   }
00161   if(!$table or strcmp($table,'tripal_feature_relagg')==0){
00162      $schema['tripal_feature_relagg'] = array(
00163         'fields' => array(
00164            'type_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
00165            'rel_type_id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
00166         ),
00167         'indexes' => array(
00168            'type_id' => array('type_id')
00169         ),
00170      );
00171   }
00172 
00173   return $schema;
00174 }
00175 
00176 /**
00177  * Implementation of hook_requirements(). Make sure 'Tripal Core' is enabled
00178  * before installation
00179  *
00180  * @ingroup tripal_feature
00181  */
00182 function tripal_feature_requirements($phase) {
00183    $requirements = array();
00184    if ($phase == 'install') {
00185       // make sure the core module is installed...
00186       if (!function_exists('tripal_create_moddir')) {
00187          $requirements ['tripal_feature'] = array(
00188             'title' => "tripal_feature",
00189             'value' => "error. Some required modules are just being installed. Please try again.",
00190             'severity' => REQUIREMENT_ERROR,
00191          );
00192       }
00193    }
00194    return $requirements;
00195 }
 All Classes Files Functions Variables