Browse Source

Tripal: Various changes to Genetic and Natural Diversity

laceysanderson 13 years ago
parent
commit
f5edcfd022

+ 21 - 2
tripal_gbrowse/tripal_gbrowse.module

@@ -48,7 +48,26 @@ function tripal_gbrowse_menu() {
     'access arguments' => array('administer site configuration'),
     'type' => MENU_NORMAL_ITEM
   );
- 
+
+  $items['admin/tripal/tripal_gbrowse/register_gbrowse'] = array(
+    'title' => t('Register GBrowse Instance'),
+    'description' => t('Interface to enter gbrowse details and save to the database'), 
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('tripal_gbrowse_register_gbrowse_form'), 
+    'access arguments' => array('administer site configuration'),
+    'weight' => 20,
+    'type' => MENU_NORMAL_ITEM
+  );
+  
+  $items['admin/tripal/tripal_gbrowse/unregister_gbrowse']=array(
+    'title' => t('Un-Register GBrowse Instance'),
+    'description' => t('Interface to remove gbrowse details from the database'), 
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('tripal_gbrowse_unregister_gbrowse_instances_form'), 
+    'access arguments' => array('administer site configuration'),
+    'type' => MENU_NORMAL_ITEM
+  );
+  
   $items['admin/tripal/tripal_gbrowse/load_library_features'] = array(
     'title' => t('Load Library Features'),
     'description' => t('Load features associated with a specified library into a GBrowse instance.'),
@@ -1095,7 +1114,7 @@ function tripal_gbrowse_administration_form_submit ($form, $form_state) {
 
 /**
 *
-* Register & Create GBrowse instance
+* Register & Create GBrowse instance ( called by the node form )
 *
 * This form submit takes it's fields from the form for registering a GBrowse instance. The 'sed'command
 * is used to select certain lines of a template 'conf' file and replaces them with the user specified GBrowse instance

+ 73 - 0
tripal_natural_diversity/theme/tripal_feature-genotype_experiments.tpl.php

@@ -0,0 +1,73 @@
+
+<?php
+  $num_results_per_page = 25;
+  $feature = $variables['node']->feature;
+  
+  // get all genotypes associatated with the current feature
+  $query = "SELECT * FROM genotype WHERE genotype_id IN (SELECT genotype_id FROM feature_genotype WHERE feature_id=%d)";
+  $resource = db_query($query, $feature->feature_id);
+  $genotypes = array();
+  while( $r = db_fetch_array($resource)) {
+    $genotypes[$r['genotype_id']] = $r;
+  }
+  
+  if (!empty($genotypes)) {
+    // SELECT all nd_experiments where type=genotype and experiment is connected to the current feature
+    $query = "SELECT nd_experiment_id, genotype_id FROM nd_experiment_genotype "
+      ."WHERE genotype_id IN (%s) "
+      ."ORDER BY nd_experiment_id";
+    $resource = pager_query($query, $num_results_per_page, 0, NULL, implode(',',array_keys($genotypes)));
+    $results = array();
+    while ($r = db_fetch_object($resource)) {
+    
+      // Get the stock associated with each experiment
+      $query2 = "SELECT s.* FROM stock s "
+        ."WHERE s.stock_id IN (SELECT stock_id FROM nd_experiment_stock WHERE nd_experiment_id=%d)";
+      $stock = db_fetch_array(db_query($query2, $r->nd_experiment_id));
+      
+      $item = array(
+        'nd_experiment' => array(
+          'nd_experiment_id' => $r->nd_experiment_id
+        ),
+        'genotype' =>  $genotypes[$r->genotype_id],
+        'stock' => $stock,
+      );
+      
+      // Get the nid associated with the feature (used for linking)
+      $query3 = "SELECT nid FROM chado_stock WHERE stock_id=%d";
+      $nid = db_fetch_object(db_query($query3,$stock['stock_id']));
+      $item['stock']['nid'] = $nid->nid;
+      
+      $results[$r->nd_experiment_id] = $item;
+    }
+  }
+?>
+
+<?php if(count($results) > 0){ ?>
+<div id="tripal_feature-genotype_experiments-box" class="tripal_feature-info-box tripal-info-box">
+  <div class="tripal_feature-info-box-title tripal-info-box-title">Genotype Experiments</div>
+  <div class="tripal_feature-info-box-desc tripal-info-box-desc">
+    Genotypes of this <?php print $feature->type_id->name; ?> in various germplasm
+  </div>
+  <table>
+    <tr><th>Germplasm Assayed</th><th>Genotype Observed</th></tr>
+    <?php foreach ($results as $r) { 
+        $genotype = $r['genotype']['description'];
+        if (preg_match('/insufficient/',$genotype)) { $genotype = "<font color='grey'>".$genotype.'</font>'; }
+        $stock_name = $r['stock']['name'];
+        if ($r['stock']['nid']) {
+          $stock_link = 'node/'.$r['stock']['nid'];
+          $stock = l($stock_name, $stock_link);
+        } else {
+          $stock = $stock_name;
+        }
+    ?>
+      <tr><td><?php print $stock; ?></td><td><?php print $genotype; ?></td></tr>
+    <?php } ?>
+  </table>
+  <?php 
+   print theme('pager', array(), $num_results_per_page, 0, array('block'=>'genotype_experiments'), 5); 
+  ?>
+</div>
+<?php } ?>
+

+ 80 - 0
tripal_natural_diversity/theme/tripal_stock-genotype_experiments.tpl.php

@@ -0,0 +1,80 @@
+
+<?php
+  $num_results_per_page = 25;
+  
+  // SELECT all nd_experiments where type=genotype and experiment is connected to the current stock
+  $query = "SELECT nd_experiment_id FROM nd_experiment "
+    ."WHERE nd_experiment_id IN (SELECT nd_experiment_id FROM nd_experiment_stock WHERE stock_id=%d) "
+      ."AND type_id IN (SELECT cvterm_id FROM cvterm WHERE name='genotype') "
+    ."ORDER BY nd_experiment_id";
+  $resource = pager_query($query, $num_results_per_page, 0, NULL, $node->stock->stock_id);
+  $results = array();
+  while ($r = db_fetch_object($resource)) {
+
+    // Get the genotype & feature associated with each experiment
+    $query2 = "SELECT g.*, f.uniquename as feature_uniquename, f.name as feature_name, f.feature_id FROM genotype g "
+      ."LEFT JOIN feature_genotype fg ON fg.genotype_id=g.genotype_id "
+      ."LEFT JOIN feature f ON fg.feature_id=f.feature_id "
+      ."WHERE g.genotype_id IN (SELECT genotype_id FROM nd_experiment_genotype WHERE nd_experiment_id=%d)";
+    $genotype_feature = db_fetch_object(db_query($query2, $r->nd_experiment_id));
+
+    $item = array(
+      'nd_experiment' => array(
+        'nd_experiment_id' => $r->nd_experiment_id
+      ),
+      'genotype' => array(
+        'genotype_id' => $genotype_feature->genotype_id,
+        'uniquename' => $genotype_feature->uniquename,
+        'description' => $genotype_feature->description,
+      ),
+      'feature' => array(
+        'feature_id' => $genotype_feature->feature_id,
+        'uniquename' => $genotype_feature->feature_uniquename,
+        'name' => $genotype_feature->feature_name,
+      ),
+    );
+    
+    // Get the nid associated with the feature (used for linking)
+    $query3 = "SELECT nid FROM chado_feature WHERE feature_id=%d";
+    $nid = db_fetch_object(db_query($query3,$genotype_feature->feature_id));
+    $item['feature']['nid'] = $nid->nid;
+    
+    $results[$r->nd_experiment_id] = $item;
+  }
+?>
+
+<?php if (!empty($results)) { ?>
+  <div id="tripal_stock-genotype_experiments-box" class="tripal_stock-info-box tripal-info-box">
+    <div class="tripal_stock-info-box-title tripal-info-box-title">Genotype Experiments</div>
+    <div class="tripal_stock-info-box-desc tripal-info-box-desc"></div>
+    <table>
+      <tr><th>Marker Assayed</th><th>Genotype Observed</th></tr>
+      <?php
+        foreach ($results as $r) {
+          // genotype
+          $genotype = $r['genotype']['description'];
+          if (preg_match('/insufficient/',$genotype)) { 
+            $genotype = "<font color='grey'>".$genotype.'</font>'; 
+          }
+          
+          // feature name
+          if ($r['feature']['name']) {
+            $marker_name = $r['feature']['name'];
+          } else {
+            $marker_name = $r['feature']['uniquename'];
+          }
+          
+          // add link if feature sync'd
+          if ($r['feature']['nid']) {
+            $marker_link = 'node/'.$r['feature']['nid'];
+            $marker = l($marker_name, $marker_link);
+          } else {
+            $marker = $marker_name;
+          }
+      ?>
+      <tr><td><?php print $marker; ?></td><td><?php print $genotype; ?></td></tr>
+      <?php } ?>
+    </table>
+    <?php print theme('pager', array(), $num_results_per_page, 0, array('block'=>'genotype_experiments'), 5); ?>
+  </div>
+<?php } ?>

+ 34 - 0
tripal_natural_diversity/tripal_natural_diversity.module

@@ -21,4 +21,38 @@ function tripal_natural_diversity_views_api() {
    return array(
       'api' => 2.0,
    );
+}
+
+/**
+ * Implements hook_theme
+ */
+function tripal_natural_diversity_theme() {
+  return array(
+    'tripal_feature_genotype_experiments' => array(
+      'arguments' => array('node' => NULL),
+      'template' => 'tripal_feature-genotype_experiments',      
+    ),
+    'tripal_stock_genotype_experiments' => array(
+      'arguments' => array('node' => NULL),
+      'template' => 'tripal_feature-stock_experiments',      
+    ),
+  );
+}
+
+/**
+ *
+ */
+function tripal_natural_diversity_nodeapi (&$node, $op, $teaser, $page) {
+  switch ($op) {
+    case 'view':
+      if ($node->type == 'chado_feature') {
+        $node->content['tripal_feature_genotype_experiments'] = array(
+           '#value' => theme('tripal_feature_genotype_experiments', $node),
+        );      
+      } elseif ($node->type == 'chado_stock') {
+        $node->content['tripal_stock_genotype_experiments'] = array(
+           '#value' => theme('tripal_stock_genotype_experiments', $node),
+        );      
+      }
+  }
 }

+ 97 - 0
tripal_project/tripal_project.admin.inc

@@ -0,0 +1,97 @@
+<?php
+
+function tripal_project_administration_description_page () {
+ $output = '';
+ 
+ 
+ return $output;
+}
+
+function tripal_project_configuration_page () {
+  $output = '';
+  
+  $output .= drupal_get_form('tripal_project_sync_projects_form');
+  
+  return $output;
+}
+
+function tripal_project_sync_projects_form ($form_state = NULL) {
+  $form = array();
+  
+  $form['sync'] = array(
+    '#type' => 'fieldset',
+    '#title' => 'Sync Projects'
+  );
+  
+  $form['sync']['description'] = array(
+    '#type' => 'item',
+    '#value' => 'Many of the details for projects are stored in chado. Often other tripal '
+      .'modules may create projects as a means of grouping data together. Sync\'ing projects '
+      .'in chado created drupal pages (known as nodes) which display the data to priviledged users.'
+  );
+  
+  $form['sync']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => 'Sync All Projects',
+    
+  );
+  
+  return $form;
+}
+
+function tripal_project_sync_projects_form_submit ($form, &$form_state) {
+  global $user;
+  
+  //sync'ing is done by a tripal_job that is added here
+  $job_id = tripal_add_job('Sync Projects', 'tripal_project', 
+	'tripal_project_sync_all_projects', array(), $user->uid);
+
+}
+
+function tripal_project_sync_all_projects () {
+
+  //retrieve all projects in drupal
+  $resource = db_query('SELECT project_id FROM {chado_project}');
+  $drupal_projects = array();
+  while ($r = db_fetch_object($resource)) {
+    $drupal_projects[$r->project_id] = $r->project_id;
+  }
+  
+  // retrieve all projects in chado
+  $chado_projects = array();
+  $previous_db = tripal_db_set_active('chado');
+  $resource = db_query('SELECT project_id FROM project');
+  tripal_db_set_active($previous_db);
+  while ($r = db_fetch_object($resource)) {
+    // if not already in drupal add to list to be sync'd
+    if (!isset($drupal_projects[$r->project_id])) {
+      $chado_projects[$r->project_id] = $r->project_id;
+    }
+  }
+  
+  print 'Number of Projects to Sync: '.sizeof($chado_projects)."\n";
+  
+  foreach ($chado_projects as $project_id) {
+    $project = tripal_core_chado_select('project',array('name','description'),array('project_id'=>$project_id));
+    print 'Project:'.print_r($project,TRUE)."\n";
+    print 'Sync\'ing '.$project[0]->name.'...';
+    
+    // create node
+    $form_state = array();
+    $form_state['values']['name'] = $project[0]->name;
+    $form_state['values']['description'] = $project[0]->description;
+    $form_state['values']['project_id'] = $project_id;
+    $form_state['clicked_button'] = 'Save';
+    drupal_execute('tripal_project_node_form',$form_state, (object)array('type' => 'tripal_project'));
+    $errors = form_get_errors();
+    if ($errors) {
+      print "Error Encountered!\n";
+      print "Errors:".print_r($errors,TRUE)."\n";
+    } else {
+      print "Successful (".$node->nid.")\n";
+    }
+    
+  }
+  
+  
+}

+ 101 - 74
tripal_project/tripal_project.module

@@ -1,5 +1,7 @@
 <?php
 
+require('tripal_project.admin.inc');
+
 /**
  *  @file
  * This file contains the basic functions needed for this drupal module.
@@ -28,74 +30,27 @@ function tripal_project_views_api() {
    );
 }
 
-
-/**
-* Implementation of hook_node_info()
-*
-* This node_info, is a simple node that describes the functionallity of the module. It specifies
-* that the title(Project Name) and body(Description) set to true so that they information can be
-* entered
-*
-*/
-function tripal_project_node_info() {
-  return array(
-    'tripal_project' => array(
-      'name' => t('Project'),
-      'module' => 'tripal_project',
-      'description' => t('A module for interfacing the GMOD chado database with Drupal, providing viewing of projects'),
-    	'has_title' => TRUE,
-    	'title_label' =>t('Project Name'),
-    	'had_body' => TRUE,
-    	'body_label' =>t('Description'),
-    )
-  );
-}
-
-
 /**
-* Implementation of hook_form().
-*
-*  This form takes the Project Title infromation and the Project description from the user. It 
-*  then puts the infromation into the Chado_project database table.
-*
-*  @parm &$node
-*    The node that is created when the database is initialized
-*
-*  @parm $form_state
-*    The state of the form, that has the user entered information that is neccessary for, setting
-*    up the database of the project
-*
-*  @return $form
-*    The information that was enterd allong with   
-*
-*/
-function tripal_project_form(&$node, $form_state) {
-  
-  $type = node_get_types('type', $node);
-
-  //define form elements for the node's title and body.
-  $form['title'] = array(
-    '#type' => 'textfield',
-    '#title' => check_plain($type->title_label),
-    '#required' => TRUE,
-    '#default_value' => $node->title,
-    '#weight' => -5
-  );
-  
-  // Putting body and filter elements to be adjacent them into a sub-array together
-  $form['body_filter']['body'] = array(
-    '#type' => 'textarea',
-    '#title' => check_plain($type->body_label),
-    '#default_value' => $node->body,
-    '#required' => FALSE
-  );
+ * Implements hook_menu
+ */
+function tripal_project_menu() {
+  $items[ 'admin/tripal/tripal_project' ]= array(
+    'title' => t('Projects'),
+    'page callback' => 'tripal_project_administration_description_page',
+    'access arguments' => array('administer site configuration'),
+    'type' => MENU_NORMAL_ITEM
+  ); 
+
+  $items[ 'admin/tripal/tripal_project/configuration' ]= array(
+    'title' => t('Configuration'),
+    'page callback' => 'tripal_project_configuration_page',
+    'access arguments' => array('administer site configuration'),
+    'type' => MENU_NORMAL_ITEM
+  ); 
   
-  $form['body_filter']['filter'] = filter_form($node->format);
-
-  return $form;
+  return $items;
 }
 
-
 /**
  *  Implements Hook_help()
  *
@@ -120,7 +75,6 @@ function tripal_project_help($path, $arg) {
   }
 }
 
-
 /**
  * Implements hook_perm()
  *
@@ -136,7 +90,6 @@ function tripal_project_perm() {
   );
 }
 
-
 /**
  * Implements hook_access()
  *
@@ -168,7 +121,7 @@ function tripal_project_access($op, $node, $account) {
   // Users who create a node may edit or delete it later, assuming they have the necessary permissions.
   if ($op == 'update' || $op == 'delete') {
   
-    if(!user_access('edit own tripal_project',$account){
+    if(!user_access('edit own tripal_project',$account)){
        return FALSE;
     }
     if(user_access('edit own tripal_project',$account) &&
@@ -179,6 +132,80 @@ function tripal_project_access($op, $node, $account) {
   return NULL;
 }
 
+//-----------------------------------------------------------------------------
+//  SECTION: Node Functionality
+//-----------------------------------------------------------------------------
+
+/**
+* Implementation of hook_node_info()
+*
+* This node_info, is a simple node that describes the functionallity of the module. It specifies
+* that the title(Project Name) and body(Description) set to true so that they information can be
+* entered
+*
+*/
+function tripal_project_node_info() {
+  return array(
+    'tripal_project' => array(
+      'name' => t('Project'),
+      'module' => 'tripal_project',
+      'description' => t('A module for interfacing the GMOD chado database with Drupal, providing viewing of projects'),
+    	'has_title' => TRUE,
+    	'title_label' =>t('Project Name'),
+    	'had_body' => TRUE,
+    	'body_label' =>t('Description'),
+    )
+  );
+}
+
+/**
+* Implementation of hook_form().
+*
+*  This form takes the Project Title infromation and the Project description from the user. It 
+*  then puts the infromation into the Chado_project database table.
+*
+*  @parm &$node
+*    The node that is created when the database is initialized
+*
+*  @parm $form_state
+*    The state of the form, that has the user entered information that is neccessary for, setting
+*    up the database of the project
+*
+*  @return $form
+*    The information that was enterd allong with   
+*
+*/
+function tripal_project_form(&$node, $form_state) {
+  
+  $type = node_get_types('type', $node);
+
+  //define form elements for the node's title and body.
+  $form['title'] = array(
+    '#type' => 'textfield',
+    '#title' => check_plain($type->title_label),
+    '#required' => TRUE,
+    '#default_value' => $node->title,
+    '#weight' => -5
+  );
+  
+  // Putting body and filter elements to be adjacent them into a sub-array together
+  $form['body_filter']['body'] = array(
+    '#type' => 'textarea',
+    '#title' => check_plain($type->body_label),
+    '#default_value' => $node->body,
+    '#required' => FALSE
+  );
+  
+  $form['body_filter']['filter'] = filter_form($node->format);
+  
+  // whether or not the project exists in chado
+  $form['project_id'] = array(
+    '#type' => 'value',
+    '#value' => FALSE,
+  );
+
+  return $form;
+}
 
 /**
 * Implementation of hook_insert()
@@ -194,15 +221,17 @@ function tripal_project_insert($node) {
      'description' => $node->body,
    );
    
-   //inserts info into chado table.
-   $result = tripal_core_chado_insert('project',$values);
-
+   if (!$node->project_id) {
+     //inserts info into chado table.
+     $result = tripal_core_chado_insert('project',$values);
+     $node->project_id = $result['project_id'];
+  } 
+  
 	//inserts the row of vid,nid,project_id into the chado_project table
-  db_query("INSERT INTO {chado_project} (vid, nid, project_id) VALUES (%d, %d, %d)", $node->vid, $node->nid, $result['project_id']);
+  db_query("INSERT INTO {chado_project} (vid, nid, project_id) VALUES (%d, %d, %d)", $node->vid, $node->nid, $node->project_id);
   
 }
 
-
 /**
 *
 * Implementation of hook_delete().
@@ -227,7 +256,6 @@ function tripal_project_delete($node) {
 
 }
 
-
 /**
 * Implements hook_update()
 *
@@ -254,7 +282,6 @@ function tripal_project_update($node){
 
 }
 
-
 /**
 * Implementation of tripal_project_load().
 *