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() { // create the module's data directory tripal_create_files_dir('tripal_feature'); // add the materialized view tripal_feature_add_organism_count_mview(); // create the temp table we will use for loading GFF files tripal_cv_create_tripal_gff_temp(); // 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() { } /** * Create a temporary table used for loading gff3 files * * @ingroup tripal_feature */ function tripal_cv_create_tripal_gff_temp() { // the tripal_obo_temp table is used for temporary housing of records when loading OBO files // we create it here using plain SQL because we want it to be in the chado schema but we // do not want to use the Tripal Custom Table API because we don't want it to appear in the // list of custom tables. It needs to be available for the Tripal Chado API so we create it // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc if (!db_table_exists('chado.tripal_gff_temp')) { $sql = " CREATE TABLE {tripal_gff_temp} ( feature_id integer NOT NULL, organism_id integer NOT NULL, uniquename text NOT NULL, type_name character varying(1024) NOT NULL, CONSTRAINT tripal_gff_temp_uq0 UNIQUE (feature_id), CONSTRAINT tripal_gff_temp_uq1 UNIQUE (uniquename, organism_id, type_name) ); "; chado_query($sql); $sql = "CREATE INDEX tripal_gff_temp_idx0 ON {tripal_gff_temp} USING btree (feature_id)"; chado_query($sql); $sql = "CREATE INDEX tripal_gff_temp_idx1 ON {tripal_gff_temp} USING btree (organism_id)"; chado_query($sql); $sql = "CREATE INDEX tripal_gff_temp_idx2 ON {tripal_gff_temp} USING btree (uniquename)"; chado_query($sql); } } /** * 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( '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( '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_cv_add_cv( 'feature_relationship', 'Contains types of relationships between features.' ); // the feature_property CV already exists... it comes with chado, so no need to add it here // 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_cv_add_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() { // 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 { // 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 { $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'feature_property'")->fetchField(); db_insert('tripal_cv_defaults') ->fields(array( 'table_name' => 'featureprop', 'field_name' => 'type_id', 'cv_id' => $cv_id )) ->execute(); } catch (\PDOException $e) { $error = $e->getMessage(); throw new DrupalUpdateException('Failed to set feature_property vocabulary as default: '. $error); } // add the feature_relationshp CV try { $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'feature_relationship'")->fetchField(); if (!$cv_id) { // add the vocabulary $cv_id = db_insert('chado.cv') ->fields(array( 'name' => 'feature_relationship', 'definition' => 'Contains types of relationships between features.' )) ->execute(); } // use the new feature_relationship CV we just added db_insert('tripal_cv_defaults') ->fields(array( 'table_name' => 'feature_relationship', 'field_name' => 'type_id', 'cv_id' => $cv_id )) ->execute(); } catch (\PDOException $e) { $error = $e->getMessage(); throw new DrupalUpdateException('Failed to add feature_relationship vocabulary: '. $error); } // set the feature_type as the 'sequence' ontology try { $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'sequence'")->fetchField(); if (!$cv_id) { // add the vocabulary $cv_id = db_insert('chado.cv') ->fields(array( 'name' => 'sequence', 'definition' => 'The Sequence Ontology.' )) ->execute(); } db_insert('tripal_cv_defaults') ->fields(array( 'table_name' => 'feature', 'field_name' => 'type_id', 'cv_id' => $cv_id )) ->execute(); } 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. */ 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; }