123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- <?php
- ///////////////////////////////////////////////////////////////////////////
- // Module: tripal_core
- ///////////////////////////////////////////////////////////////////////////
- /*************************************************************************
- * Purpose: Get max rank for a given set of criteria
- * This function was developed with the many property tables in chado in mind
- *
- * @params tablename: the name of the chado table you want to select the max rank from
- * this table must contain a rank column of type integer
- * @params where_options: array(
- * <column_name> => array(
- * 'type' => <type of column: INT/STRING>,
- * 'value' => <the value you want to filter on>,
- * 'exact' => <if TRUE use =; if FALSE use ~>,
- * )
- * )
- * where options should include the id and type for that table to correctly
- * group a set of records together where the only difference are the value and rank
- * @return the maximum rank
- *
- */
- function get_max_chado_rank ($tablename, $where_options) {
- $where= array();
- //generate the where clause from supplied options
- // the key is the column name
- foreach ($where_options as $key => $val_array) {
- if (preg_match('/INT/', $val_array['type'])) {
- $where[] = $key."=".$val_array['value'];
- } else {
- if ($val_array['exact']) { $operator='='; }
- else { $operator='~'; }
- $where[] = $key.$operator."'".$val_array['value']."'";
- }
- }
-
- $previous_db = tripal_db_set_active('chado');
- $result = db_fetch_object(db_query(
- "SELECT max(rank) as max_rank, count(rank) as count FROM %s WHERE %s",
- $tablename,
- implode(' AND ',$where)
- ));
- tripal_db_set_active($previous_db);
- //drupal_set_message("Max Rank Query=SELECT max(rank) as max_rank, count(rank) as count FROM ".$tablename." WHERE ".implode(' AND ',$where));
- if ($result->count > 0) {
- return $result->max_rank;
- } else {
- return -1;
- }
- }
- ///////////////////////////////////////////////////////////////////////////
- // Module: tripal_cv
- ///////////////////////////////////////////////////////////////////////////
- /*************************************************************************
- * Purpose: To retrieve a chado cv object
- *
- * @params where_options: array(
- * <column_name> => array(
- * 'type' => <type of column: INT/STRING>,
- * 'value' => <the vlaue you want to filter on>,
- * 'exact' => <if TRUE use =; if FALSE use ~>,
- * )
- * )
- * @return chado cv object with all fields from the chado cv table
- */
- function get_chado_cv ($where_options) {
- $previous_db = tripal_db_set_active('chado');
- $where= array();
- //generate the where clause from supplied options
- // the key is the column name
- foreach ($where_options as $key => $val_array) {
- if (preg_match('/INT/', $val_array['type'])) {
- $where[] = $key."=".$val_array['value'];
- } else {
- if ($val_array['exact']) { $operator='='; }
- else { $operator='~'; }
- $where[] = $key.$operator."'".$val_array['value']."'";
- }
- }
-
- $r = db_fetch_object(db_query(
- "SELECT * FROM cv WHERE ".implode(' AND ',$where)
- ));
-
- tripal_db_set_active($previous_db);
- return $r;
- }
- /*************************************************************************
- * Purpose: Create an options array to be used in a form element
- * which provides a list of all chado cvs
- *
- * @return an array(cv_id => name) for each cv in the chado cv table
- */
- function get_chado_cv_options() {
- $previous_db = tripal_db_set_active('chado');
- $result = db_query(
- "SELECT cv_id, name FROM cv"
- );
- db_set_active($previous_db);
- $options = array();
- while ( $r = db_fetch_object($result) ) {
- $options[$r->cv_id] = $r->name;
- }
- return $options;
- }
- /*************************************************************************
- * Purpose: To retrieve a chado cvterm object
- *
- * @params where_options: array(
- * <column_name> => array(
- * 'type' => <type of column: INT/STRING>,
- * 'value' => <the vlaue you want to filter on>,
- * 'exact' => <if TRUE use =; if FALSE use ~>,
- * )
- * )
- * @return chado cvterm object with all fields from the chado cvterm table
- */
- function get_chado_cvterm ($where_options) {
- $previous_db = tripal_db_set_active('chado');
- $where= array();
- //generate the where clause from supplied options
- // the key is the column name
- foreach ($where_options as $key => $val_array) {
- if (preg_match('/INT/', $val_array['type'])) {
- $where[] = $key."=".$val_array['value'];
- } else {
- if ($val_array['exact']) { $operator='='; }
- else { $operator='~'; }
- $where[] = $key.$operator."'".$val_array['value']."'";
- }
- }
-
- $r = db_fetch_object(db_query(
- "SELECT * FROM cvterm WHERE ".implode(' AND ',$where)
- ));
-
- tripal_db_set_active($previous_db);
- return $r;
- }
- /*************************************************************************
- * Purpose: Create an options array to be used in a form element
- * which provides a list of all chado cvterms
- *
- * @params cv_id: the chado cv_id
- * only cvterms with the supplied cv_id will be returned
- * @return an array(cvterm_id => name)
- * for each cvterm in the chado cvterm table where cv_id=that supplied
- */
- function get_chado_cvterm_options($cv_id = 0) {
- $previous_db = tripal_db_set_active('chado');
- if ($cv_id > 0) {
- $result = db_query(
- "SELECT cvterm_id, name FROM cvterm WHERE cv_id=%d", $cv_id
- );
- } else {
- $result = db_query(
- "SELECT cvterm_id, name FROM cvterm"
- );
- }
- db_set_active($previous_db);
- $options = array();
- while ( $r = db_fetch_object($result) ) {
- $options[$r->cvterm_id] = $r->name;
- }
- return $options;
- }
- ///////////////////////////////////////////////////////////////////////////
- // Module: tripal_db
- ///////////////////////////////////////////////////////////////////////////
- /*************************************************************************
- * Purpose: To retrieve a chado db object
- *
- * @params where_options: array(
- * <column_name> => array(
- * 'type' => <type of column: INT/STRING>,
- * 'value' => <the vlaue you want to filter on>,
- * 'exact' => <if TRUE use =; if FALSE use ~>,
- * )
- * )
- * @return chado db object with all fields from the chado db table
- */
- function get_chado_db ($where_options) {
- $previous_db = tripal_db_set_active('chado');
- $where= array();
- //generate the where clause from supplied options
- // the key is the column name
- foreach ($where_options as $key => $val_array) {
- if (preg_match('/INT/', $val_array['type'])) {
- $where[] = $key."=".$val_array['value'];
- } else {
- if ($val_array['exact']) { $operator='='; }
- else { $operator='~'; }
- $where[] = $key.$operator."'".$val_array['value']."'";
- }
- }
-
- $r = db_fetch_object(db_query(
- "SELECT * FROM db WHERE ".implode(' AND ',$where)
- ));
-
- tripal_db_set_active($previous_db);
- return $r;
- }
- /*************************************************************************
- * Purpose: Create an options array to be used in a form element
- * which provides a list of all chado dbs
- *
- * @return an array(db_id => name) for each db in the chado db table
- */
- function get_chado_db_options() {
- $previous_db = tripal_db_set_active('chado');
- $result = db_query(
- "SELECT db_id, name FROM db"
- );
- db_set_active($previous_db);
- $options = array();
- while ( $r = db_fetch_object($result) ) {
- $options[$r->db_id] = $r->name;
- }
- return $options;
- }
- /*************************************************************************
- * Purpose: To retrieve a chado dbxref object
- *
- * @params where_options: array(
- * <column_name> => array(
- * 'type' => <type of column: INT/STRING>,
- * 'value' => <the vlaue you want to filter on>,
- * 'exact' => <if TRUE use =; if FALSE use ~>,
- * )
- * )
- * @return chado dbxref object with all fields from the chado dbxref table
- */
- function get_chado_dbxref ($where_options) {
- $previous_db = tripal_db_set_active('chado');
- $where= array();
- //generate the where clause from supplied options
- // the key is the column name
- foreach ($where_options as $key => $val_array) {
- if (preg_match('/INT/', $val_array['type'])) {
- $where[] = $key."=".$val_array['value'];
- } else {
- if ($val_array['exact']) { $operator='='; }
- else { $operator='~'; }
- $where[] = $key.$operator."'".$val_array['value']."'";
- }
- }
-
- $r = db_fetch_object(db_query(
- "SELECT * FROM dbxref WHERE ".implode(' AND ',$where)
- ));
-
- tripal_db_set_active($previous_db);
- return $r;
- }
- ///////////////////////////////////////////////////////////////////////////
- // Module: tripal_organism
- ///////////////////////////////////////////////////////////////////////////
- /*************************************************************************
- * Purpose: Create an options array to be used in a form element
- * which provides a list of all chado organisms
- *
- * @return an array(organism_id => common_name)
- * for each organism in the chado organism table
- */
- function get_chado_organism_options() {
- $previous_db = tripal_db_set_active('chado');
- $result = db_query(
- "SELECT organism_id, common_name FROM organism"
- );
- tripal_db_set_active($previous_db);
- $options = array();
- while ( $r = db_fetch_object($result) ) {
- $options[$r->organism_id] = $r->common_name;
- }
- return $options;
- }
- /*************************************************************************
- * Purpose: Return a given organism object using the nid or organism id
- *
- * @return organism object created by node load
- */
- function get_chado_organism($nid=0, $organism_id=0) {
- if (!empty($nid)) {
- return node_load($nid);
- } else {
- if (!empty($organism_id)) {
- $sql = "SELECT nid FROM {chado_organism} WHERE organism_id=%d";
- $r = db_fetch_object(db_query($sql, $organism_id));
- if (!empty($r->nid)) {
- return node_load($r->nid);
- } else {
- drupal_set_message("Function: get_chado_organism() -no organism with that organism id sync'd with drupal", 'error');
- }
- } else {
- drupal_set_message("Function: get_chado_organism() -need to supply at least one of node ID or Organism ID",'error');
- }
- }
- return 0;
-
- }
|