| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 | <?php/** * @file * Installation of the feature module *//** * Implements hook_disable(). * * Disable default views when module is disabled * * @ingroup tripal_feature */function tripal_feature_disable() {  // Disable all default views provided by this module  require_once("tripal_feature.views_default.inc");  $views = tripal_feature_views_default_views();  foreach (array_keys($views) as $view_name) {    tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));  }}/** * Implements hook_requirements(). * * @ingroup tripal_feature */function tripal_feature_requirements($phase) {  $requirements = array();  if ($phase == 'install') {    // make sure chado is installed    if (!$GLOBALS["chado_is_installed"]) {      $requirements ['tripal_feature'] = array(          'title' => "t ripal_feature",          'value' => "ERROR: Chado must be installed before this module can be enabled",          'severity' => REQUIREMENT_ERROR,      );    }  }  return $requirements;}/** * Implements hook_install(). * * @ingroup tripal_feature */function tripal_feature_install() {  // Add the feature properties vocabulary provided by Chado.  $obo_path = '{tripal_feature}/files/feature_property.obo';  $obo_id = tripal_insert_obo('Chado Feature Properties', $obo_path);  tripal_submit_obo_job(array('obo_id' => $obo_id));  // Add the materialized view.  tripal_feature_add_organism_count_mview();  // Add the custom tables.  tripal_feature_add_tripal_gff_temp_table();  tripal_feature_add_tripal_gffcds_temp_table();  tripal_feature_add_tripal_gffprotein_temp_table();  // Add the vocabularies used by the feature module.  tripal_feature_add_cvs();  // Set the default vocabularies.  tripal_set_default_cv('feature', 'type_id', 'sequence');  tripal_set_default_cv('featureprop', 'type_id', 'feature_property');  tripal_set_default_cv('feature_relationship', 'type_id', 'feature_relationship');}/** * Implements hook_uninstall(). * * @ingroup tripal_feature */function tripal_feature_uninstall() {}function tripal_feature_add_tripal_gff_temp_table() {  $schema = array(    'table' => 'tripal_gff_temp',    'fields' => array(      'feature_id' => array(        'type' => 'int',        'not null' => TRUE,      ),      'organism_id' => array(        'type' => 'int',        'not null' => TRUE,      ),      'uniquename' => array(        'type' => 'text',        'not null' => TRUE,      ),      'type_name' => array(        'type' => 'varchar',        'length' => '1024',        'not null' => TRUE,      ),    ),    'indexes' => array(      'tripal_gff_temp_idx0' => array('feature_id'),      'tripal_gff_temp_idx0' => array('organism_id'),      'tripal_gff_temp_idx1' => array('uniquename'),    ),    'unique keys' => array(      'tripal_gff_temp_uq0' => array('feature_id'),      'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),    ),  );  chado_create_custom_table('tripal_gff_temp', $schema, TRUE);}/** * */function tripal_feature_add_tripal_gffcds_temp_table($skip_recreate = TRUE) {  $schema = array(    'table' => 'tripal_gffcds_temp',    'fields' => array(      'feature_id' => array(        'type' => 'int',        'not null' => TRUE,      ),      'parent_id' => array(        'type' => 'int',        'not null' => TRUE,      ),      'phase' => array(        'type' => 'int',        'not null' => TRUE,      ),      'strand' => array(        'type' => 'int',        'not null' => TRUE,      ),      'fmin' => array(        'type' => 'int',        'not null' => TRUE,      ),      'fmax' => array(        'type' => 'int',        'not null' => TRUE,      ),    ),    'indexes' => array(      'tripal_gff_temp_idx0' => array('feature_id'),      'tripal_gff_temp_idx0' => array('parent_id'),    ),  );  chado_create_custom_table('tripal_gffcds_temp', $schema, $skip_recreate);}/** * */function tripal_feature_add_tripal_gffprotein_temp_table() {  $schema = array(    'table' => 'tripal_gffprotein_temp',    'fields' => array(      'feature_id' => array(        'type' => 'int',        'not null' => TRUE,      ),      'parent_id' => array(        'type' => 'int',        'not null' => TRUE,      ),      'fmin' => array(        'type' => 'int',        'not null' => TRUE,      ),      'fmax' => array(        'type' => 'int',        'not null' => TRUE,      ),    ),    'indexes' => array(      'tripal_gff_temp_idx0' => array('feature_id'),      'tripal_gff_temp_idx0' => array('parent_id'),    ),    'unique keys' => array(      'tripal_gff_temp_uq0' => array('feature_id'),    ),  );  chado_create_custom_table('tripal_gffprotein_temp', $schema, TRUE);}/** * Implementation of hook_schema(). * * @ingroup tripal_feature */function tripal_feature_schema() {  $schema['chado_feature'] = array(    'fields' => array(      'vid' => array(        'type' => 'int',        'unsigned' => TRUE,        'not null' => TRUE,        'default' => 0       ),      'nid' => array(        'type' => 'int',        'unsigned' => TRUE,        'not null' => TRUE,        'default' => 0       ),      'feature_id' => array(        'type' => 'int',        'not null' => TRUE,        'default' => 0      ),      'sync_date' => array(        'type' => 'int',        'not null' => FALSE,        'description' => 'UNIX integer sync date/time'      ),    ),    'indexes' => array(      'chado_feature_idx1' => array('feature_id')    ),    'unique keys' => array(      'chado_feature_uq1' => array('nid', 'vid'),      'chado_feature_uq2' => array('vid')    ),    'primary key' => array('nid'),  );  return $schema;};/** * Creates a materialized view that stores the type & number of features per organism * * @ingroup tripal_feature */function tripal_feature_add_organism_count_mview() {  $view_name = 'organism_feature_count';  $comment = 'Stores the type and number of features per organism';  $schema = array(    'description' => $comment,    'table' => $view_name,    'fields' => array(      'organism_id' => array(        'size' => 'big',        'type' => 'int',        'not null' => TRUE,      ),      'genus' => array(        'type' => 'varchar',        'length' => '255',        'not null' => TRUE,      ),      'species' => array(        'type' => 'varchar',        'length' => '255',        'not null' => TRUE,      ),      'common_name' => array(        'type' => 'varchar',        'length' => '255',        'not null' => FALSE,      ),      'num_features' => array(        'type' => 'int',        'not null' => TRUE,      ),      'cvterm_id' => array(        'size' => 'big',        'type' => 'int',        'not null' => TRUE,      ),      'feature_type' => array(        'type' => 'varchar',        'length' => '255',        'not null' => TRUE,      ),    ),    'indexes' => array(      'organism_feature_count_idx1' => array('organism_id'),      'organism_feature_count_idx2' => array('cvterm_id'),      'organism_feature_count_idx3' => array('feature_type'),    ),  );  $sql = "    SELECT        O.organism_id, O.genus, O.species, O.common_name,        count(F.feature_id) as num_features,        CVT.cvterm_id, CVT.name as feature_type     FROM organism O        INNER JOIN feature F  ON O.Organism_id = F.organism_id        INNER JOIN cvterm CVT ON F.type_id     = CVT.cvterm_id     GROUP BY        O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name  ";  tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);}/** * Add cvs related to publications * * @ingroup tripal_pub */function tripal_feature_add_cvs() {  // Add cv for relationship types  tripal_insert_cv(    'feature_relationship',    'Contains types of relationships between features.'  );  // the feature_property CV already exists... it comes with chado, but we need to  // add it just in case it doesn't get added before the feature module is installed  tripal_insert_cv(    'feature_property',    'Stores properties about features'  );  // the feature type vocabulary should be the sequence ontology, and even though  // this ontology should get loaded we will create it here just so that we can  // set the default vocabulary for the feature.type_id field  tripal_insert_cv(    'sequence',    'The Sequence Ontology'  );}/** * This is the required update for tripal_feature when upgrading from Drupal core API 6.x. * This update may take some time to complete. */function tripal_feature_update_7200() {  // Make sure we have the full API loaded this will help during a  // site upgrade when the tripal_core module is disabled.  module_load_include('module', 'tripal_core', 'tripal_core');  tripal_core_import_api();  module_load_include('inc', 'tripal_cv', 'api/tripal_cv.api');  // During the upgrade from D6 to D7 the vocabulary terms assigned to features were  // copied to the field_data_taxonomyextra table rather than to the correct  // field_data_taxonomy_vocabulary_[vid] table. We'll move them.  $vid = db_query("SELECT vid FROM {taxonomy_vocabulary} WHERE name = 'Feature Type'")->fetchField();  if ($vid) {    try {      if (db_table_exists('field_data_taxonomyextra')) {        // first move from the field_data_taxonomyextra table        $sql = "          INSERT INTO {field_data_taxonomy_vocabulary_$vid}            (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)          (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid           FROM field_data_taxonomyextra           WHERE bundle = 'chado_feature')        ";        db_query($sql);        $sql = "DELETE FROM field_data_taxonomyextra WHERE bundle = 'chado_feature'";        db_query($sql);        // next move from the field_revision_taxonomyextra table        $sql = "          INSERT INTO {field_revision_taxonomy_vocabulary_$vid}            (entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomy_vocabulary_" . $vid. "_tid)          (SELECT entity_type, bundle, deleted, entity_id, revision_id, language, delta, taxonomyextra_tid           FROM field_revision_taxonomyextra           WHERE bundle = 'chado_feature')        ";        db_query($sql);        $sql = "DELETE FROM field_revision_taxonomyextra WHERE bundle = 'chado_feature'";        db_query($sql);      }    }    catch (\PDOException $e) {      $error = $e->getMessage();      throw new DrupalUpdateException('Could not move feature taxonomy terms: '. $error);    }  }  // set the default feature property vocabulary  try {    tripal_insert_cv(      'feature_relationship',      'Contains types of relationships between features.'    );    // the feature_property CV already exists... it comes with chado, but we need to    // add it just in case it doesn't get added before the feature module is installed    tripal_insert_cv(      'feature_property',      'Stores properties about features'    );    // the feature type vocabulary should be the sequence ontology, and even though    // this ontology should get loaded we will create it here just so that we can    // set the default vocabulary for the feature.type_id field    tripal_insert_cv(      'sequence',      'The Sequence Ontology'    );    tripal_set_default_cv('feature', 'type_id', 'sequence');    tripal_set_default_cv('featureprop', 'type_id', 'feature_property');    tripal_set_default_cv('feature_relationship', 'type_id', 'feature_relationship');  }  catch (\PDOException $e) {    $error = $e->getMessage();    throw new DrupalUpdateException('Failed to add sequence vocabulary which will be used for the sequence ontology: '. $error);  }}/** * Implementation of hook_update_dependencies(). * * It specifies a list of other modules whose updates must be run prior to * this one.  It also ensures the the Tripal API is in scope for site * upgrades when tripal_core is disabled. */function tripal_feature_update_dependencies() {  $dependencies = array();  // the tripal_cv update 7200 must run prior to update 7200 of this module  $dependencies['tripal_feature'][7200] = array(    'tripal_cv' => 7200  );  return $dependencies;}/** * Fixes an error with the materialized view installation * */function tripal_feature_update_7201() {  // Make sure we have the full API loaded this will help during a  // site upgrade when the tripal_core module is disabled.  module_load_include('module', 'tripal_core', 'tripal_core');  tripal_core_import_api();  try {    // there is a bug in the Tripal v2.0-alpha release that didn't add the    // materialized view schema to the mviews table.    // get the schema for the materialized view from the custom_tables table    // as there is a copy there, but only if the schema is missing from the    // materialized view table    $view_name = 'organism_feature_count';    $schema = db_select('tripal_mviews', 'tm')      ->fields('tm', array('mv_schema'))      ->condition('name', $view_name)      ->execute()      ->fetchField();    if (!$schema or $schema == 'Array') {      $schema = db_select('tripal_custom_tables', 'tct')        ->fields('tct', array('schema'))        ->condition('table_name', $view_name)        ->execute()        ->fetchField();      $schema_str = var_export(unserialize($schema), TRUE);      $schema_str = preg_replace('/=>\s+\n\s+array/', '=> array', $schema_str);      db_update('tripal_mviews')        ->fields(array(        'mv_schema' => $schema_str        ))        ->condition('name', $view_name)        ->execute();    }  }  catch (\PDOException $e) {    $error = $e->getMessage();    throw new DrupalUpdateException('Failed to complete update' . $error);  }}/** * Adds the temporary tables used for loading GFF files. */function tripal_feature_update_7202() {  // Make sure we have the full API loaded this will help during a  // site upgrade when the tripal_core module is disabled.  module_load_include('module', 'tripal_core', 'tripal_core');  tripal_core_import_api();  try {    tripal_feature_add_tripal_gff_temp_table();    tripal_feature_add_tripal_gffcds_temp_table();    tripal_feature_add_tripal_gffprotein_temp_table();  }  catch (\PDOException $e) {    $error = $e->getMessage();    throw new DrupalUpdateException('Failed to complete update' . $error);  }}/** * Removes the unique constraint on the tripal_gffcds_temp table. */function tripal_feature_update_7203() {  // Make sure we have the full API loaded this will help during a  // site upgrade when the tripal_core module is disabled.  module_load_include('module', 'tripal_core', 'tripal_core');  tripal_core_import_api();  try {    tripal_feature_add_tripal_gffcds_temp_table(FALSE);  }  catch (\PDOException $e) {    $error = $e->getMessage();    throw new DrupalUpdateException('Failed to complete update' . $error);  }}
 |