Browse Source

combined object and subject relationships into a single template

spficklin 12 years ago
parent
commit
b4ad2e1b5d
2 changed files with 159 additions and 34 deletions
  1. 1 1
      tripal_feature/includes/gff_loader.inc
  2. 158 33
      tripal_feature/tripal_feature.module

+ 1 - 1
tripal_feature/includes/gff_loader.inc

@@ -1051,7 +1051,7 @@ function tripal_feature_load_gff3_alias($feature, $aliases, $log) {
     if (!$pub) {
       // prepare the statement
       if (!tripal_core_is_sql_prepared('ins_pub_uniquename_typeid')) {
-        $psql = "PREPARE ins_pub_uniquename_typeid (text, text)
+        $psql = "PREPARE ins_pub_uniquename_typeid (text, text) AS
                  INSERT INTO pub (uniquename,type_id) VALUES ('%s',
                  (SELECT cvterm_id
                   FROM cvterm CVT

+ 158 - 33
tripal_feature/tripal_feature.module

@@ -343,11 +343,8 @@ function tripal_feature_block($op = 'list', $delta = 0, $edit=array()) {
       $blocks['alignments']['info'] = t('Tripal Feature Alignments');
       $blocks['alignments']['cache'] = BLOCK_NO_CACHE;
 
-      $blocks['object_relationships']['info'] = t('Tripal Feature Object Relationships');
-      $blocks['object_relationships']['cache'] = BLOCK_NO_CACHE;
-
-      $blocks['subject_relationships']['info'] = t('Tripal Feature Subject Relationships');
-      $blocks['subject_relationships']['cache'] = BLOCK_NO_CACHE;
+      $blocks['relationships']['info'] = t('Tripal Feature Relationships');
+      $blocks['relationships']['cache'] = BLOCK_NO_CACHE;
 
       $blocks['org_feature_counts']['info'] = t('Tripal Organism Feature Counts');
       $blocks['org_feature_counts']['cache'] = BLOCK_NO_CACHE;
@@ -397,13 +394,9 @@ function tripal_feature_block($op = 'list', $delta = 0, $edit=array()) {
           $block['subject'] = t('Alignments');
           $block['content'] = theme('tripal_feature_alignments', $node);
           break;
-        case 'object_relationships':
-          $block['subject'] = t('Object Relationships');
-          $block['content'] = theme('tripal_feature_object_relationships', $node);
-          break;
-        case 'subject_relationships':
-          $block['subject'] = t('Subject Relationships');
-          $block['content'] = theme('tripal_feature_object_relationships', $node);
+        case 'relationships':
+          $block['subject'] = t('Relationships');
+          $block['content'] = theme('tripal_feature_relationships', $node);
           break;
         case 'org_feature_counts':
           $block['subject'] = t('Feature Type Summary');
@@ -1847,69 +1840,201 @@ function tripal_feature_theme() {
   return array(
     'tripal_feature_search_index' => array(
       'arguments' => array('node'),
-  ),
+    ),
     'tripal_feature_search_results' => array(
        'arguments' => array('node'),
-  ),
+    ),
     'tripal_organism_feature_browser' => array(
        'arguments' => array('node' => NULL),
        'template' => 'tripal_organism_feature_browser',
-  ),
+    ),
     'tripal_organism_feature_counts' => array(
        'arguments' => array('node' => NULL),
        'template' => 'tripal_organism_feature_counts',
-  ),
+    ),
     'tripal_library_feature_browser' => array(
        'arguments' => array('node' => NULL),
        'template' => 'tripal_library_feature_browser',
-  ),
+    ),
     'tripal_analysis_feature_browser' => array(
        'arguments' => array('node' => NULL),
        'template' => 'tripal_analysis_feature_browser',
-  ),
+    ),
     'tripal_feature_base' => array(
        'arguments' => array('node' => NULL),
        'template' => 'tripal_feature_base',
-  ),
+    ),
     'tripal_feature_sequence' => array(
        'arguments' => array('node' => NULL),
        'template' => 'tripal_feature_sequence',
-  ),
+    ),
     'tripal_feature_synonyms' => array(
        'arguments' => array('node' => NULL),
        'template' => 'tripal_feature_synonyms',
-  ),
+    ),
     'tripal_feature_featureloc_sequences' => array(
        'arguments' => array('node' => NULL),
        'template' => 'tripal_feature_featureloc_sequences',
-  ),
+    ),
     'tripal_feature_references' => array(
        'arguments' => array('node' => NULL),
        'template' => 'tripal_feature_references',
-  ),
+    ),
     'tripal_feature_properties' => array(
        'arguments' => array('node' => NULL),
        'template' => 'tripal_feature_properties',
-  ),
+    ),
     'tripal_feature_alignments' => array(
        'arguments' => array('node' => NULL),
        'template' => 'tripal_feature_alignments',
-  ),
-    'tripal_feature_object_relationships' => array(
-       'arguments' => array('node' => NULL),
-       'template' => 'tripal_feature_object_relationships',
-  ),
-    'tripal_feature_subject_relationships' => array(
+    ),
+    'tripal_feature_relationships' => array(
        'arguments' => array('node' => NULL),
-       'template' => 'tripal_feature_subject_relationships',
-  ),
+       'template' => 'tripal_feature_relationships',
+    ),
     'tripal_feature_edit_ALL_properties_form' => array(
       'arguments' => array('form' => NULL),
       'function' => 'theme_tripal_feature_edit_ALL_properties_form',
-  ),
+    ),
   );
 }
+/**
+ *
+ *
+ * @ingroup tripal_feature
+ */
+function tripal_feature_preprocess_tripal_feature_relationships(&$variables) {
+  // we want to provide a new variable that contains the matched features.
+  $feature = $variables['node']->feature;
+   
+  // expand the feature object to include the feature relationships.
+  $feature = tripal_core_expand_chado_vars($feature,
+     'table','feature_relationship', array('order_by'=>array('rank' => 'ASC')));
+
+  // get the subject relationships
+  $srelationships = $feature->feature_relationship->subject_id;
+  if (!$srelationships) {
+     $srelationships = array();
+  } 
+  elseif (!is_array($srelationships)) { 
+     $srelationships = array($srelationships); 
+  }
+  
+  // get the object relationships
+  $orelationships = $feature->feature_relationship->object_id;
+  if (!$orelationships) {
+     $orelationships = array();
+  } 
+  elseif (!is_array($orelationships)) { 
+     $orelationships = array($orelationships); 
+  }
+  
+  // get alignment as child
+  $cfeaturelocs = $feature->featureloc->feature_id;
+  if (!$cfeaturelocs) {
+     $cfeaturelocs = array();
+  } 
+  elseif (!is_array($cfeaturelocs)) { 
+     $cfeaturelocs = array($cfeaturelocs); 
+  }
 
+  
+  // prepare the SQL statement to get the featureloc for the 
+  // feature in the relationships. 
+  if(!tripal_core_is_sql_prepared('sel_featureloc_preprocess_relationships')){
+    $psql = "PREPARE sel_featureloc_preprocess_relationships (int, int) AS " .
+            "SELECT FL.featureloc_id, F.name as srcfeature_name, FL.srcfeature_id, ".
+            "  FL.feature_id, FL.fmin, FL.fmax, FL.strand, FL.phase ".
+            "FROM featureloc FL " .
+            "  INNER JOIN feature F ON F.feature_id = FL.srcfeature_id ".
+            "WHERE FL.feature_id = $1 and FL.srcfeature_id = $2";
+    $prepared = chado_query($psql);
+    if(!$prepared){
+      watchdog('tripal_feature', "tripal_feature_preprocess_tripal_feature_relationships: not able to prepare '%name' statement for: %sql", array('%name' => 'sel_featureloc_preprocess_relationships', '%sql' => $psql), 'WATCHDOG ERROR');
+      return FALSE;
+    }
+  }  
+  
+  // combine both object and subject relationshisp into a single array
+  $relationships = array();
+  $relationships['object'] = array();
+  $relationships['subject'] = array();
+  
+  // iterate through the object relationships
+  foreach ($orelationships as $relationship) {
+     $rel = new stdClass(); 
+     // get locations where the child feature and this feature overlap with the
+     // same landmark feature.
+     $rel->child_featurelocs = array();     
+     foreach ($cfeaturelocs as $featureloc) {
+        $res = chado_query("EXECUTE sel_featureloc_preprocess_relationships (%d, %d)", 
+          $relationship->subject_id->feature_id, 
+          $featureloc->srcfeature_id->feature_id);        
+        while ($loc = db_fetch_object($res)){
+           // add in the node id of the src feature if it exists and save this location
+           $loc->nid = $featureloc->srcfeature_id->nid;
+           $rel->child_featurelocs[] = $loc;
+        }
+     }
+     $rel->record = $relationship;    
+     
+     // get the relationship and child types
+     $rel_type = t(preg_replace('/_/'," ",$relationship->type_id->name));
+     $child_type = $relationship->subject_id->type_id->name;
+     
+     // get the node id of the subject
+     $sql = "SELECT nid FROM chado_feature WHERE feature_id = %d";
+     $n = db_fetch_object(db_query($sql,$relationship->subject_id->feature_id));
+     if($n){
+        $rel->record->nid = $n->nid;
+     }
+
+     if (!array_key_exists($rel_type, $relationships['object'])) {
+       $relationships['object'][$rel_type] = array();   
+     }
+     if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
+       $relationships['object'][$rel_type][$child_type] = array();   
+     }
+     $relationships['object'][$rel_type][$child_type][] = $rel;     
+  }
+  
+  // now add in the subject relationships
+  foreach ($srelationships as $relationship) {
+     $rel = new stdClass(); 
+     // get locations where this feature overlaps with the parent
+     $rel->parent_featurelocs = array();     
+     foreach ($cfeaturelocs as $featureloc) {
+        $res = chado_query("EXECUTE sel_featureloc_preprocess_relationships (%d, %d)", 
+          $relationship->object_id->feature_id, 
+          $featureloc->srcfeature_id->feature_id);
+        while ($loc = db_fetch_object($res)){
+           // add in the node id of the src feature if it exists and save this location
+           $loc->nid = $featureloc->srcfeature_id->nid;
+           $rel->parent_featurelocs[] = $loc;
+        }
+     }
+     $rel->record = $relationship;
+     $rel_type = t(preg_replace('/_/'," ",$relationship->type_id->name));
+     $parent_type = $relationship->object_id->type_id->name;
+     
+     // get the node id of the subject
+     $sql = "SELECT nid FROM chado_feature WHERE feature_id = %d";
+     $n = db_fetch_object(db_query($sql,$relationship->object_id->feature_id));
+     if($n){
+        $rel->record->nid = $n->nid;
+     }
+     
+     if (!array_key_exists($rel_type, $relationships['subject'])) {
+       $relationships['subject'][$rel_type] = array();   
+     }
+     if (!array_key_exists($child_type, $relationships['subject'][$rel_type])) {
+       $relationships['subject'][$rel_type][$parent_type] = array();   
+     }
+     $relationships['subject'][$rel_type][$parent_type][] = $rel;
+  }
+  $feature->all_relationships = $relationships;
+
+}
 /**
  *
  *