Bladeren bron

Completion of sequence handler

spficklin 12 jaren geleden
bovenliggende
commit
83bb80cb48

+ 2 - 1
tripal_views/tripal_views.views.inc

@@ -551,9 +551,10 @@ function tripal_views_data_export_download_form(&$form_state, $view, $display_id
 
       $path = $display->display_options['path'];
       $query = $view->get_exposed_input();  // retrieves elements in $_GET array
+
       $urls[$display->id]['path'] = $path;
       $urls[$display->id]['query'] = $query;
-
+      
       // add the new item to the options array
       $options[$display->id] = $display->display_title;
     }

+ 50 - 12
tripal_views/views/handlers/tripal_views_handler_field_sequence.inc

@@ -150,6 +150,14 @@ class tripal_views_handler_field_sequence extends chado_views_handler_field {
       '#title' => 'Format Output',
       '#description' => t('Alter the way a sequence is displayed')
     );
+    $default_num_bases_per_line = '50';
+    if($this->options['display']['num_bases_per_line']){
+       $default_num_bases_per_line = $this->options['display']['num_bases_per_line'];
+    }
+    $default_output_format = 'raw';
+    if($this->options['display']['output_format']){
+       $default_ouput_format = $this->options['display']['output_format'];
+    }
 
     $form['display']['num_bases_per_line'] = array(
       '#type' => 'textfield',
@@ -157,7 +165,7 @@ class tripal_views_handler_field_sequence extends chado_views_handler_field {
       '#description' => t('Specify the number of bases per line. An HTML <br> tag ' .
         'will be inserted after the number of bases indicated. If no value is ' .
         'provided. The sequence will be one long string (default)'),
-      '#default_value' => $this->options['display']['num_bases_per_line'],
+      '#default_value' => $default_num_bases_per_line,
     );
     $form['display']['derive_from_parent'] = array(
       '#type' => 'checkbox',
@@ -177,6 +185,17 @@ class tripal_views_handler_field_sequence extends chado_views_handler_field {
         'without intronic sequence'),
       '#default_value' => $this->options['display']['aggregate'],
     );
+    $form['display']['output_format'] = array(
+      '#type' => 'radios',
+      '#title' => t('Output format'),
+      '#options' => array(
+         'raw' => 'Raw sequence data (no formatting)',
+         'fasta_html' => 'FASTA in HTML format',
+         'fasta_txt'  => 'FASTA in text format',
+      ),
+      '#description' => t('Select an output format.  Raw output cannot be used when the sequence is derived from the parent.'),
+      '#default_value' => $default_ouput_format,
+    );
   }  
 
   /**
@@ -200,9 +219,10 @@ class tripal_views_handler_field_sequence extends chado_views_handler_field {
   */
   function render($values) {
     $residues = '';
-      
+             
     // get the number of bases to show per line
     $num_bases_per_line = $this->options['display']['num_bases_per_line'];
+    $output_format = $this->options['display']['output_format'];
 
     // get the residues from the feature.residues column
     $field = $this->field_alias;
@@ -210,11 +230,11 @@ class tripal_views_handler_field_sequence extends chado_views_handler_field {
     // get the feature id
     $feature_id = $values->feature_feature_id;
     $feature_name = $values->feature_name;
-    
+        
     // the upstream and downstream values get set by the 
     // tripal_views_handlers_filter_sequence.inc
-    $upstream = $this->view->sequence_q['upstream'];
-    $downstream = $this->view->sequence_q['downstream'];           
+    $upstream = $_SESSION['upstream'];
+    $downstream = $_SESSION['downstream'];
     if (!$upstream) {
        $upstream = 0;
     }
@@ -247,11 +267,11 @@ class tripal_views_handler_field_sequence extends chado_views_handler_field {
                  
           // iterate through the sub features and concat their sequences. They
           // should already be in order.
-          $types = '';
+          $types = array();
           $i = 0;
           while($child = db_fetch_object($children)) {
             // keep up with the types
-            if(!in_array($child->type_name,$types)){
+            if (!in_array($child->type_name,$types)) {
               $types[] = $child->type_name;
             }
             
@@ -312,7 +332,12 @@ class tripal_views_handler_field_sequence extends chado_views_handler_field {
         }
         
         // now format for display
-        $seq = wordwrap($seq, $num_bases_per_line, "<br>", TRUE);
+        if ($output_format == 'fasta_html') {
+           $seq = wordwrap($seq, $num_bases_per_line, "<br>", TRUE);
+        } 
+        elseif ($output_format == 'fasta_txt') {
+           $seq = wordwrap($seq, $num_bases_per_line, "\n", TRUE);
+        }
         $residues .= ">$feature_name ($parent->typename) $parent->srcname:" . ($parent->adjfmin + 1) . ".." . $parent->adjfmax ." ($dir). ";
         if (count($types) > 0) {
           $residues .= "Excludes all bases but those of type(s): " . implode(', ',$types) . ". " ;
@@ -324,10 +349,16 @@ class tripal_views_handler_field_sequence extends chado_views_handler_field {
            $residues .= "Includes " . $parent->downstream . " bases downstream.  ";
         }
         if (!$seq) {
-          $residues .= "<br>\nNo sequence available\n<br>";          
+          $residues .= "No sequence available\n<br>";          
         }
         else {
-          $residues .= "<br>\n" . $seq . "\n<br>";          
+          if ($output_format == 'fasta_html') {
+            $residues .= "<br>";
+          }
+          $residues .= "\n" . $seq . "\n";          
+          if ($output_format == 'fasta_html') {
+            $residues .= "<br>";
+          }
         }
       }
     }
@@ -335,12 +366,19 @@ class tripal_views_handler_field_sequence extends chado_views_handler_field {
     // use what comes through from the feature record
     else {
       $residues = $values->$field; 
-      $residues = wordwrap($residues, $num_bases_per_line, "<br>", TRUE);  
+      if ($output_format == 'fasta_html') {
+         $residues = wordwrap($residues, $num_bases_per_line, "<br>", TRUE);  
+      } 
+      elseif ($output_format == 'fasta_txt') {
+         $residues = wordwrap($residues, $num_bases_per_line, "\n", TRUE);  
+      }
     }
     
     // format the residues for display
     if($residues and $num_bases_per_line){
-      $residues = '<span style="font-family: monospace;">' . $residues . '</span>';
+      if ($output_format == 'fasta_html') {
+         $residues = '<span style="font-family: monospace;">' . $residues . '</span>';
+      }
     }
     return $residues;         
   } 

+ 16 - 9
tripal_views/views/handlers/tripal_views_handler_filter_sequence.inc

@@ -49,17 +49,24 @@ class tripal_views_handler_filter_sequence extends views_handler_filter {
     }
   }
 
+ 
   /**
-   *  Validates the input form
+   *
    */
-  function exposed_validate(&$form, &$form_state) {
-    $upstream = $form_state['values'][$this->field]['upstream'];
-    $downstream = $form_state['values'][$this->field]['downstream'];       
-     
+  function query() {
+    $this->ensure_my_table();
+
+    $upstream = $this->value[0]['upstream'];
+    $downstream = $this->value[0]['downstream'];       
+    
     // we need the values provided by the user so that the field
-    // handler can generate the results properly.  These values
-    // will get stored in the view object.  This may not be the best place
-    $this->view->sequence_q['upstream'] = $upstream;
-    $this->view->sequence_q['downstream'] = $downstream;
+    // handler can generate the results properly.  Saving these as session
+    // variables may not be the best way but it works.
+    if($upstream){
+      $_SESSION['upstream'] = $upstream;
+    }
+    if($downstream){
+      $_SESSION['downstream'] = $downstream;
+    }    
   }
 }

+ 6 - 1
tripal_views/views/handlers/tripal_views_handler_filter_textarea.inc

@@ -67,7 +67,12 @@ class tripal_views_handler_filter_textarea extends views_handler_filter {
     $field = "$this->table.$this->real_field";
 
     // get the form element value
-    $value = $this->value;
+    if (is_array($this->value)) {
+      $value = $this->value[0];
+    } 
+    else {
+      $value = $this->value;
+    }
 
     // remove extra spaces and new lines
     $value = preg_replace("/\s+,/", ",", $value);

+ 40 - 5
tripal_views/views_data_export/plugins/tripal_views_plugin_style_export_fasta.inc

@@ -37,13 +37,48 @@ class tripal_views_plugin_style_export_fasta extends views_data_export_plugin_st
   function options_form(&$form, &$form_state) {
     parent::options_form($form, $form_state);
 
-    $form['defline_fields'] = array(
-      '#prefix' => '<div><div id="edit-options-newline-replacement">',
-      '#suffix' => '</div></div>',
+
+    $form['display'] = array(
+      '#type' => 'fieldset',
+      '#title' => 'Format Output',
+    );
+    
+    $form['display']['residues_colname'] = array(
       '#type' => 'textfield',
-      '#title' => t('Defenition line fields'),
+      '#title' => t('The name of the residues column'),
+      '#description' => t("The name of the column that contains the sequence ".
+         "residues. To discover this field edit the feature residues field ".
+         "and click the checkbox \"Rewrite ".
+         "the output of this field\".  In the replacement patterns section are ".
+         "field names.  The field name for the residues column should be the ".
+         "last one"),
       '#required' => TRUE,
-      '#default_value' => $this->options['defline_fields'],
+      '#default_value' => $this->options['display']['residues_colname'],
+    );
+    
+    $form['display']['use_residues'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Use the value as is'),
+      '#description' => t('Check this box if you do not want the FASTA exporter ' .
+         'to provide a definition line or add line breaks to the sequence. This is '.
+         'useful when the field options provided for the residues column has '.
+         'already been formatted'),
+      '#default_value' => $this->options['display']['use_residues'],
+    );
+
+
+    $form['display']['num_bases_per_line'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Number of bases per line'),
+      '#description' => t('Specify the number of bases per line. If no value is ' .
+        'provided. The sequence will be one long string (default)'),
+      '#default_value' => $this->options['display']['num_bases_per_line'],
+    );
+    
+    $form['display']['defline_fields'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Defenition line fields'),
+      '#default_value' => $this->options['display']['defline_fields'],
       '#description' => t("This field controls the information present in the definition ".
          "line for each sequence. Any of the fields in the view can be used in the ".
          "definition line.  To discover these fields, add the feature residues as the ".

+ 4 - 2
tripal_views/views_data_export/theme/tripal_views_data_export.theme.inc

@@ -15,10 +15,12 @@
 function template_preprocess_views_data_export_fasta_body(&$vars) {
   _views_data_export_header_shared_preprocess($vars);
   _views_data_export_body_shared_preprocess($vars);
-  $defline_tpl = $vars['options']['defline_fields'];
+  
+  // get export settings
+  $defline_tpl = $vars['options']['display']['defline_fields'];
 
   // iterate through the rows and replace the field tokens with values
-  // to generate the definition line
+  // to generate the definition line  
   foreach ($vars['themed_rows'] as $i => $fields) {
     $defline = $defline_tpl;
     foreach ($fields as $key => $value) {

+ 31 - 18
tripal_views/views_data_export/theme/views-data-export-fasta-body.tpl.php

@@ -4,33 +4,46 @@
  * @file
  * Renders the body portion of a FASTA views data export
  */
+$defline_tpl = $variables['options']['display']['defline_fields'];
+$num_bases_per_line = $variables['options']['display']['num_bases_per_line'];
+$use_residues = $variables['options']['display']['use_residues'];
+$residues_colname = $variables['options']['display']['residues_colname'];
 
-//print_r($themed_rows);
-
-// print the first FASTA record header
-// this is needed due to the order of the fields
-print $defline;
+if(!$num_bases_per_line){
+   $num_bases_per_line = 50;
+}
 
 // foreach row in the views table
 foreach ($themed_rows as $index => $fields) {
   $defline = array();
   $residues = '';
-  foreach ($fields as $key => $value) {
-
-    // wrap the sequence
-    if (strcmp($key, 'residues') == 0) {
-      $residues = wordwrap($value, 60, "\r\n", TRUE);
+  
+  // if we're using the residues as is then we assume the residues are already
+  // formatted in FASTA format.   We just need to print
+  if ($use_residues) {
+     print "$fields[$residues_colname]\r\n";
+  }
+  // if we're not using the residues as is then wrap the residues
+  // and generate a proper FASTA format
+  else {
+  
+    foreach ($fields as $key => $value) {
+
+      // if the setup indicates, wrap the sequence 
+      if (strcmp($key, $residues_colname) == 0) {
+        $residues = wordwrap($value, $num_bases_per_line, "\r\n", TRUE);
+      }
+
+      // set the FASTA header
+      if (strcmp($key, 'defline') == 0) {
+        $defline = $value;
+      }
     }
 
-    // set the FASTA header
-    if (strcmp($key, 'defline') == 0) {
-      $defline = $value;
-    }
+    // print the FASTA record
+    print ">$defline\r\n";
+    print "$residues\r\n";
   }
-
-  // print the FASTA record
-  print ">$defline\r\n";
-  print "$residues\r\n";
 }